2009-05-11 20:17:53 -04:00
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
package net.sourceforge.filebot;
|
2009-05-11 20:17:53 -04:00
|
|
|
|
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
import static net.sourceforge.filebot.History.*;
|
2009-07-27 18:34:42 -04:00
|
|
|
import static net.sourceforge.filebot.Settings.*;
|
2009-05-17 13:14:03 -04:00
|
|
|
|
2009-05-11 20:17:53 -04:00
|
|
|
import java.io.File;
|
2009-05-17 13:14:03 -04:00
|
|
|
import java.io.IOException;
|
2009-07-20 07:03:24 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2009-05-11 20:17:53 -04:00
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
import net.sourceforge.filebot.History.Element;
|
2009-07-20 07:03:24 -04:00
|
|
|
|
2009-05-11 20:17:53 -04:00
|
|
|
|
2011-09-09 10:50:01 -04:00
|
|
|
public final class HistorySpooler {
|
2009-05-11 20:17:53 -04:00
|
|
|
|
|
|
|
private static final HistorySpooler instance = new HistorySpooler();
|
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
|
2009-05-11 20:17:53 -04:00
|
|
|
public static HistorySpooler getInstance() {
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
|
2009-07-27 18:34:42 -04:00
|
|
|
private final File file = new File(getApplicationFolder(), "history.xml");
|
2009-05-11 20:17:53 -04:00
|
|
|
|
2009-05-17 13:14:03 -04:00
|
|
|
private final History sessionHistory = new History();
|
2009-05-11 20:17:53 -04:00
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
|
2009-05-17 13:14:03 -04:00
|
|
|
public synchronized History getCompleteHistory() {
|
2009-05-11 20:17:53 -04:00
|
|
|
History history = new History();
|
|
|
|
|
|
|
|
// add persistent history
|
|
|
|
if (file.exists()) {
|
|
|
|
try {
|
2009-05-17 13:14:03 -04:00
|
|
|
history.addAll(importHistory(file).sequences());
|
|
|
|
} catch (IOException e) {
|
2009-07-18 18:06:32 -04:00
|
|
|
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Failed to load history", e);
|
2009-05-11 20:17:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add session history
|
2009-05-17 13:14:03 -04:00
|
|
|
history.addAll(sessionHistory.sequences());
|
2009-05-11 20:17:53 -04:00
|
|
|
|
|
|
|
return history;
|
|
|
|
}
|
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
|
|
|
|
public History getSessionHistory() {
|
|
|
|
return sessionHistory;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-10 12:21:54 -04:00
|
|
|
public synchronized void append(Iterable<Entry<File, File>> elements) {
|
2009-07-20 07:03:24 -04:00
|
|
|
List<Element> sequence = new ArrayList<Element>();
|
|
|
|
|
2011-10-10 12:21:54 -04:00
|
|
|
for (Entry<File, File> element : elements) {
|
|
|
|
sequence.add(new Element(element.getKey().getName(), element.getValue().getPath(), element.getKey().getParentFile()));
|
2009-07-20 07:03:24 -04:00
|
|
|
}
|
|
|
|
|
2009-05-11 20:17:53 -04:00
|
|
|
// append to session history
|
2011-09-09 10:50:01 -04:00
|
|
|
if (sequence.size() > 0) {
|
|
|
|
sessionHistory.add(sequence);
|
|
|
|
}
|
2009-05-11 20:17:53 -04:00
|
|
|
}
|
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
|
2009-05-17 13:14:03 -04:00
|
|
|
public synchronized void commit(History history) {
|
|
|
|
try {
|
|
|
|
exportHistory(history, file);
|
|
|
|
|
|
|
|
// clear session history
|
|
|
|
sessionHistory.clear();
|
|
|
|
} catch (IOException e) {
|
2009-07-18 18:06:32 -04:00
|
|
|
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Failed to store history", e);
|
2009-05-17 13:14:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
|
2009-05-11 20:17:53 -04:00
|
|
|
public synchronized void commit() {
|
2009-05-17 13:14:03 -04:00
|
|
|
// check if session history is not empty
|
|
|
|
if (sessionHistory.sequences().size() > 0) {
|
|
|
|
commit(getCompleteHistory());
|
2009-05-11 20:17:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
|
2009-05-11 20:17:53 -04:00
|
|
|
private HistorySpooler() {
|
|
|
|
// commit session history on shutdown
|
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
commit();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|