1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-11 03:45:06 -05:00
filebot/source/net/sourceforge/filebot/HistorySpooler.java
2012-07-09 10:50:18 +00:00

103 lines
2.2 KiB
Java

package net.sourceforge.filebot;
import static net.sourceforge.filebot.History.*;
import static net.sourceforge.filebot.Settings.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.filebot.History.Element;
public final class HistorySpooler {
private static final HistorySpooler instance = new HistorySpooler();
public static HistorySpooler getInstance() {
return instance;
}
private final File file = new File(getApplicationFolder(), "history.xml");
private final History sessionHistory = new History();
public synchronized History getCompleteHistory() {
History history = new History();
// add persistent history
if (file.exists()) {
try {
history.addAll(importHistory(file).sequences());
} catch (IOException e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Failed to load history", e);
}
}
// add session history
history.addAll(sessionHistory.sequences());
return history;
}
public History getSessionHistory() {
return sessionHistory;
}
public synchronized void append(Iterable<Entry<File, File>> elements) {
List<Element> sequence = new ArrayList<Element>();
for (Entry<File, File> element : elements) {
sequence.add(new Element(element.getKey().getName(), element.getValue().getPath(), element.getKey().getParentFile()));
}
// append to session history
if (sequence.size() > 0) {
sessionHistory.add(sequence);
}
}
public synchronized void commit(History history) {
try {
exportHistory(history, file);
// clear session history
sessionHistory.clear();
} catch (IOException e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Failed to store history", e);
}
}
public synchronized void commit() {
// check if session history is not empty
if (sessionHistory.sequences().size() > 0) {
commit(getCompleteHistory());
}
}
private HistorySpooler() {
// commit session history on shutdown
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
commit();
}
}));
}
}