edu.tsinghua.lumaqq.ui.helper.ExportHelper.java Source code

Java tutorial

Introduction

Here is the source code for edu.tsinghua.lumaqq.ui.helper.ExportHelper.java

Source

/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.ui.helper;

import static edu.tsinghua.lumaqq.resource.Messages.dir_dialog_common_title;
import static edu.tsinghua.lumaqq.resource.Messages.dir_dialog_export_message;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;

import edu.tsinghua.lumaqq.models.Cluster;
import edu.tsinghua.lumaqq.models.User;
import edu.tsinghua.lumaqq.record.IKeyConstants;
import edu.tsinghua.lumaqq.record.RecordEntry;
import edu.tsinghua.lumaqq.template.RecordExporterFactory;
import edu.tsinghua.lumaqq.ui.MainShell;
import edu.tsinghua.lumaqq.ui.dialogs.RecordExportTypeDialog;
import edu.tsinghua.lumaqq.ui.jobs.DialogJobExecutor;
import edu.tsinghua.lumaqq.ui.jobs.ExportAllRecordJob;
import edu.tsinghua.lumaqq.ui.jobs.IExecutor;
import edu.tsinghua.lumaqq.widgets.record.IRecordExporter;
import edu.tsinghua.lumaqq.widgets.record.IRecordExporterFactory;

/**
 * 
 * 
 * @author luma
 */
public class ExportHelper {
    private static Log log = LogFactory.getLog(ExportHelper.class);

    private IRecordExporterFactory factory;
    private Map<String, Object> arg;
    private MainShell main;

    public ExportHelper(MainShell main) {
        this.main = main;
        factory = new RecordExporterFactory();
        arg = new HashMap<String, Object>();
    }

    /**
     * 
     * 
     * @param exporter
     *       
     * @param argument
     *       ?
     * @param filename
     *       ??
     */
    private void exportToFile(IRecordExporter exporter, Object argument, String filename) {
        // filename?null
        if (filename != null) {
            BufferedWriter writer = null;
            try {
                // 
                File file = new File(filename);
                if (!file.exists())
                    file.createNewFile();

                writer = new BufferedWriter(new FileWriter(file));
                writer.write(exporter.generate(argument));
                writer.flush();
            } catch (IOException ex) {
                log.error("?");
            } finally {
                try {
                    if (writer != null)
                        writer.close();
                } catch (IOException e1) {
                    log.error(e1.getMessage());
                }
            }
        }
    }

    /**
     * ?
     * 
     * @param arg
     *       ?
     * @param defaultFileName
     *       ???
     */
    public void exportMessage(Map<String, Object> arg, String defaultFileName) {
        // ?
        RecordExportTypeDialog dialog = new RecordExportTypeDialog(main.getShell(), factory);
        if (dialog.open() == IDialogConstants.CANCEL_ID)
            return;

        String fileTypeName = dialog.getFileTypeName();
        IRecordExporter exporter = factory.getExporter(fileTypeName);
        if (exporter == null)
            return;

        // ??
        FileDialog fd = new FileDialog(main.getShell(), SWT.SAVE);
        fd.setFilterExtensions(new String[] { '*' + factory.getFilterExtension(fileTypeName), "*.*" });
        fd.setFilterNames(new String[] { factory.getFilterName(fileTypeName), "All Files" });
        fd.setFileName(defaultFileName + factory.getFilterExtension(fileTypeName));
        String filename = fd.open();
        if (filename == null)
            return;

        exportToFile(exporter, arg, filename);
    }

    /**
     * ?
     * 
     * @param fileTypeName
     *       ??null?
     * @param arg
     *       ?
     * @param defaultFileName
     *       ???
     * @param showFileDialog
     *       true???
     */
    public void exportMessage(String fileTypeName, Map<String, Object> arg, String defaultFileName,
            boolean showFileDialog) {
        if (fileTypeName == null)
            fileTypeName = factory.getDefaultExporterName();
        IRecordExporter exporter = factory.getExporter(fileTypeName);
        if (exporter == null)
            return;

        // ??
        String filename = defaultFileName;
        if (showFileDialog) {
            FileDialog fd = new FileDialog(main.getShell(), SWT.SAVE);
            fd.setFilterExtensions(new String[] { '*' + factory.getFilterExtension(fileTypeName), "*.*" });
            fd.setFilterNames(new String[] { factory.getFilterName(fileTypeName), "All Files" });
            fd.setFileName(defaultFileName + factory.getFilterExtension(fileTypeName));
            filename = fd.open();
        }
        if (filename == null)
            return;

        exportToFile(exporter, arg, filename);
    }

    /**
      * ??????
      */
    public void exportAllMessage() {
        // ?
        RecordExportTypeDialog d = new RecordExportTypeDialog(main.getShell(), factory);
        if (d.open() == IDialogConstants.CANCEL_ID)
            return;
        String fileTypeName = d.getFileTypeName();

        // 
        DirectoryDialog dialog = new DirectoryDialog(main.getShell(), SWT.OPEN);
        dialog.setMessage(dir_dialog_export_message);
        dialog.setText(dir_dialog_common_title);
        String dir = dialog.open();
        // 
        if (dir != null) {
            // ????
            if (!dir.endsWith(File.separator))
                dir += File.separator;

            IExecutor executor = new DialogJobExecutor(main);
            executor.addJob(new ExportAllRecordJob(dir, fileTypeName, factory));
            executor.execute();
        }
    }

    /**
     * 
     * 
     * @param mobile
     */
    public void exportMessage(String mobile) {
        exportMessage(mobile, null);
    }

    /**
     * ??
     * 
     * @param f
     */
    public void exportMessage(User f) {
        exportMessage(f, null);
    }

    /**
     * ?
     * 
     * @param c
     *       
     */
    public void exportMessage(Cluster c) {
        exportMessage(c, null);
    }

    /**
     * ?
     * 
     * @param c
     * @param entries
     */
    public void exportMessage(Cluster c, List<RecordEntry> entries) {
        // ?
        RecordExportTypeDialog dialog = new RecordExportTypeDialog(main.getShell(), factory);
        if (dialog.open() == IDialogConstants.CANCEL_ID)
            return;
        String fileTypeName = dialog.getFileTypeName();

        // ??
        FileDialog fd = new FileDialog(main.getShell(), SWT.SAVE);
        fd.setFilterExtensions(new String[] { '*' + factory.getFilterExtension(fileTypeName), "*.*" });
        fd.setFilterNames(new String[] { factory.getFilterName(fileTypeName), "All Files" });
        fd.setFileName(
                '(' + String.valueOf(c.externalId) + ')' + c.name + factory.getFilterExtension(fileTypeName));
        String filename = fd.open();
        if (filename == null)
            return;

        exportMessage(c, fileTypeName, filename, entries);
    }

    /**
     * ?
     * 
     * @param f
     * @param entries
     */
    public void exportMessage(User f, List<RecordEntry> entries) {
        // ?
        RecordExportTypeDialog dialog = new RecordExportTypeDialog(main.getShell(), factory);
        if (dialog.open() == IDialogConstants.CANCEL_ID)
            return;
        String fileTypeName = dialog.getFileTypeName();

        // ??
        FileDialog fd = new FileDialog(main.getShell(), SWT.SAVE);
        fd.setFilterExtensions(new String[] { '*' + factory.getFilterExtension(fileTypeName), "*.*" });
        fd.setFilterNames(new String[] { factory.getFilterName(fileTypeName), "All Files" });
        fd.setFileName(String.valueOf(f.qq) + factory.getFilterExtension(fileTypeName));
        String filename = fd.open();
        if (filename == null)
            return;

        exportMessage(f, fileTypeName, filename, entries);
    }

    /**
     * 
     * 
     * @param mobile
     * @param entries
     */
    public void exportMessage(String mobile, List<RecordEntry> entries) {
        // ?
        RecordExportTypeDialog dialog = new RecordExportTypeDialog(main.getShell(), factory);
        if (dialog.open() == IDialogConstants.CANCEL_ID)
            return;
        String fileTypeName = dialog.getFileTypeName();

        // ??
        FileDialog fd = new FileDialog(main.getShell(), SWT.SAVE);
        fd.setFilterExtensions(new String[] { '*' + factory.getFilterExtension(fileTypeName), "*.*" });
        fd.setFilterNames(new String[] { factory.getFilterName(fileTypeName), "All Files" });
        fd.setFileName(mobile + factory.getFilterExtension(fileTypeName));
        String filename = fd.open();
        if (filename == null)
            return;

        exportMessage(mobile, fileTypeName, filename, entries);
    }

    /**
     * ?
     */
    public void exportSystemMessage() {
        exportSystemMessage(null);
    }

    /**
     * ?
     * 
     * @param entries
     */
    public void exportSystemMessage(List<RecordEntry> entries) {
        // ?
        RecordExportTypeDialog dialog = new RecordExportTypeDialog(main.getShell(), factory);
        if (dialog.open() == IDialogConstants.CANCEL_ID)
            return;
        String fileTypeName = dialog.getFileTypeName();

        // ??
        FileDialog fd = new FileDialog(main.getShell(), SWT.SAVE);
        fd.setFilterExtensions(new String[] { '*' + factory.getFilterExtension(fileTypeName), "*.*" });
        fd.setFilterNames(new String[] { factory.getFilterName(fileTypeName), "All Files" });
        fd.setFileName("system" + factory.getFilterExtension(fileTypeName));
        String filename = fd.open();
        if (filename == null)
            return;

        exportSystemMessge(fileTypeName, filename, entries);
    }

    /**
     * 
     * 
     * @param mobile
     *       ?
     * @param fileTypeName
     *       
     * @param filename
     *       ??
     */
    public void exportMessage(String mobile, String fileTypeName, String filename, List<RecordEntry> entries) {
        arg.clear();
        arg.put(IRecordExporter.EXPORT_TYPE, IRecordExporter.EXPORT_SMS);
        arg.put(IRecordExporter.MOBILE_NUMBER, mobile);
        arg.put(IRecordExporter.MY_MODEL, main.getMyModel());
        if (entries == null) {
            List<RecordEntry> temp = main.getRecordManager().getRecord(9999, IKeyConstants.ALL,
                    IKeyConstants.SUB_ALL);
            entries = new ArrayList<RecordEntry>();
            for (RecordEntry entry : temp) {
                if (entry.message.startsWith(mobile))
                    entries.add(entry);
            }
        }
        arg.put(IRecordExporter.RECORD_ENTRIES, entries);
        exportMessage(fileTypeName, arg, filename, false);
    }

    /**
     * ?
     * 
     * @param c
     *       
     * @param fileTypeName
     *       
     * @param filename
     *       ??
     */
    public void exportMessage(Cluster c, String fileTypeName, String filename, List<RecordEntry> entries) {
        arg.clear();
        arg.put(IRecordExporter.EXPORT_TYPE, IRecordExporter.EXPORT_CLUSTER);
        arg.put(IRecordExporter.CLUSTER_MODEL, c);
        arg.put(IRecordExporter.RECORD_ENTRIES,
                (entries == null)
                        ? main.getRecordManager().getRecord(c.clusterId, IKeyConstants.ALL, IKeyConstants.SUB_ALL)
                        : entries);
        exportMessage(fileTypeName, arg, filename, false);
    }

    /**
     * ??
     * 
     * @param f
     *       ?
     * @param fileTypeName
     *       ??
     * @param filename
     *       ??
     */
    public void exportMessage(User f, String fileTypeName, String filename, List<RecordEntry> entries) {
        arg.clear();
        arg.put(IRecordExporter.EXPORT_TYPE, IRecordExporter.EXPORT_FRIEND);
        arg.put(IRecordExporter.MY_MODEL, main.getMyModel());
        arg.put(IRecordExporter.FRIEND_MODEL, f);
        arg.put(IRecordExporter.RECORD_ENTRIES,
                (entries == null)
                        ? main.getRecordManager().getRecord(f.qq, IKeyConstants.ALL, IKeyConstants.SUB_ALL)
                        : entries);
        exportMessage(fileTypeName, arg, filename, false);
    }

    /**
     * ?
     * 
     * @param fileTypeName
     * @param filename
     * @param entries
     */
    public void exportSystemMessge(String fileTypeName, String filename, List<RecordEntry> entries) {
        arg.clear();
        arg.put(IRecordExporter.EXPORT_TYPE, IRecordExporter.EXPORT_SYSTEM);
        arg.put(IRecordExporter.RECORD_ENTRIES,
                (entries == null)
                        ? main.getRecordManager().getRecord(10000, IKeyConstants.ALL, IKeyConstants.SUB_ALL)
                        : entries);
        exportMessage(fileTypeName, arg, filename, false);
    }
}