/*
* Gamers Own Instant Messenger
* Copyright (C) 2005-2006 Herbert Poul (kahless@sphene.net)
* http://goim.sphene.net
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package net.sphene.goim.rcp.history;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import net.sphene.goim.rcp.GOIMPlugin;
import net.sphene.goim.rcp.beans.GOIMAccount;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
public class HistorySaver {
GOIMAccount account;
XMPPConnection conn;
File historyFolder;
BlockingQueue<PacketWrapper> writeQueue = new LinkedBlockingQueue<PacketWrapper>();
private HistoryWriter historyWriter;
public void init(GOIMAccount account) {
this.account = account;
this.conn = account.xmpp.getConnection();
try {
historyFolder = new File(FileLocator.resolve(Platform.getInstanceLocation().getURL()).getFile() + "/.metadata/.plugins/" + GOIMPlugin.ID + "/history/" + account.name + "/raw");
historyFolder.mkdirs();
} catch (IOException e) {
e.printStackTrace();
}
if(historyWriter != null) historyWriter.closeAllWriter();
else {
historyWriter = new HistoryWriter();
historyWriter.start();
}
this.conn.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
writeQueue.add(new PacketWrapper(packet,PacketWrapper.TYPE_RECEIVED));
} },new AndFilter());
this.conn.addPacketWriterListener(new PacketListener(){
public void processPacket(Packet packet) {
writeQueue.add(new PacketWrapper(packet,PacketWrapper.TYPE_SENT));
}},new AndFilter());
}
public static class PacketWrapper {
public static int TYPE_RECEIVED = 1;
public static int TYPE_SENT = 2;
public Packet packet;
public int type;
public Date date;
public PacketWrapper(Packet packet,int type) { this.packet = packet; this.type = type; this.date = new Date(); }
}
public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public class HistoryWriter extends Thread {
private boolean running = true;
PrintWriterWrapper mainHistory = null;
PrintWriterWrapper noEntryHistory = null;
Map<String,PrintWriterWrapper> entryHistory = new HashMap<String,PrintWriterWrapper>();
public HistoryWriter() {
}
public void run() {
try {
while(running) {
PacketWrapper wrapper = writeQueue.take();
Packet packet = wrapper.packet;
if(packet instanceof IQ) continue; // Ignore IQ packets ...
String logtarget = packet.getFrom();
if(wrapper.type == PacketWrapper.TYPE_SENT)
logtarget = packet.getTo();
PrintWriterWrapper stream = null;
if(logtarget != null) {
logtarget = StringUtils.parseBareAddress(logtarget);
Roster roster = conn.getRoster();
RosterEntry entry = null;
if(roster != null)
entry = roster.getEntry(logtarget);
if(entry == null)
stream = getPrintWriterForNoEntry(logtarget);
else
stream = getPrintWriterForEntry(logtarget);
} else {
stream = getPrintWriterFromMainHistory();
}
if(wrapper.type == PacketWrapper.TYPE_RECEIVED)
stream.print("<packet type=\"received\" date=\"");
else
stream.print("<packet type=\"sent\" date=\"");
stream.print(dateFormat.format(wrapper.date));
stream.print("\">");
stream.println(packet.toXML());
stream.println("</packet>");
}
} catch(InterruptedException e) {
e.printStackTrace();
closeAllWriter();
}
}
public synchronized void closeAllWriter() {
if(mainHistory != null) mainHistory.close();
if(noEntryHistory != null) noEntryHistory.close();
for(PrintWriterWrapper wrapper : entryHistory.values())
wrapper.close();
mainHistory = null;
noEntryHistory = null;
entryHistory.clear();
}
public synchronized PrintWriterWrapper getPrintWriterFromMainHistory() {
if(mainHistory != null) return mainHistory;
mainHistory = getPrintWriterForFile("__main.xml");
return mainHistory;
}
public synchronized PrintWriterWrapper getPrintWriterForEntry(String from) {
PrintWriterWrapper wrapper = entryHistory.get(from);
if(wrapper != null) return wrapper;
wrapper = getPrintWriterForFile("history_" + from.replace("\\","_") + ".xml");
entryHistory.put(from,wrapper);
return wrapper;
}
public synchronized PrintWriterWrapper getPrintWriterForNoEntry(String from) {
if(noEntryHistory != null) return noEntryHistory;
noEntryHistory = getPrintWriterForFile("__notInRoster.xml");
return noEntryHistory;
}
protected PrintWriterWrapper getPrintWriterForFile(String filename) {
File file = new File(historyFolder,filename);
PrintWriter stream = null;
if(!file.exists())
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
stream = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file,true),"UTF-8"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return new PrintWriterWrapper(stream);
}
}
public static class PrintWriterWrapper {
private PrintWriter writer;
private Date lastwritten;
public PrintWriterWrapper(PrintWriter writer) {
this.writer = writer; lastwritten = new Date();
}
public void print(String str) { writer.print(str); touch(); }
public void println(String str) { writer.println(str); touch(); }
private void touch() {
lastwritten.setTime(System.currentTimeMillis());
writer.flush();
}
public void close() { writer.close(); }
}
}