📄 exporthelper.java
字号:
/*
* 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 java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import edu.tsinghua.lumaqq.LumaQQ;
import edu.tsinghua.lumaqq.MessageEntry;
import edu.tsinghua.lumaqq.ui.MainShell;
import edu.tsinghua.lumaqq.ui.dialogs.ExportProgressDialog;
import edu.tsinghua.lumaqq.widgets.IRecordExporter;
/**
* 记录导出帮助类
*
* @author luma
*/
public class ExportHelper {
private static Log log = LogFactory.getLog(ExportHelper.class);
private MainShell main;
public ExportHelper(MainShell main) {
this.main = main;
}
/**
* 导出所有的聊天记录,每个好友,每个群分别生成一个聊天记录文件
*/
public void exportAllMessage() {
// 选择目录
DirectoryDialog dialog = new DirectoryDialog(main.getShell(), SWT.OPEN);
dialog.setMessage(LumaQQ.getString("dir.dialog.export.message"));
dialog.setText(LumaQQ.getString("dir.dialog.common.title"));
String dir = dialog.open();
// 导出
if(dir != null) {
// 给目录名后面添加分隔符
if(!dir.endsWith(File.separator))
dir += File.separator;
new ExportProgressDialog(main, dir).open();
}
}
/**
* 导出聊天记录为文本文件
* @param qqNum
* 好友的QQ号,或者是群的内部ID
* @param exporter
* 导出器
*/
public void exportMessage(int qqNum, IRecordExporter exporter) {
// 创建文件对话框,设置参数,打开
FileDialog dialog = new FileDialog(main.getShell(), SWT.SAVE);
dialog.setFilterExtensions(new String[] { "*.txt", "*.*" } );
dialog.setFileName(String.valueOf(qqNum) + ".txt");
String filename = dialog.open();
// 导出
exportMessage(qqNum, exporter, filename);
}
/**
* 以指定的文件名导出聊天记录
*
* @param qqNum
* 要导出记录的好友或者群号
* @param exporter
* 导出器
* @param filename
* 目标文件名
*/
public void exportMessage(int qqNum, IRecordExporter exporter, String filename) {
// 如果filename不等于null则导出
if(filename != null) {
// 得到所有消息
List list = main.getMessageManager().getMessages(qqNum);
BufferedWriter writer = null;
try {
// 创建文本文件
File file = new File(filename);
if(!file.exists())
file.createNewFile();
// 创建流
writer = new BufferedWriter(new FileWriter(file));
// 导出头部
writer.write(exporter.exportHeader());
// 得到TableItem总数,然后得到TableItem,交给exporter处理
int size = list.size();
for(int i = 0; i < size; i++) {
writer.write(exporter.exportString((MessageEntry)list.get(i)));
}
writer.flush();
} catch (IOException ex) {
log.error("保存聊天记录到文本文件失败");
} finally {
try {
if(writer != null)
writer.close();
} catch (IOException e1) {
log.error(e1.getMessage());
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -