diff --git a/build.xml b/build.xml index 0151d3e9..8b8fe15f 100644 --- a/build.xml +++ b/build.xml @@ -583,6 +583,7 @@ + diff --git a/source/net/sourceforge/filebot/History.java b/source/net/sourceforge/filebot/History.java index d5def149..d955fb7a 100644 --- a/source/net/sourceforge/filebot/History.java +++ b/source/net/sourceforge/filebot/History.java @@ -173,6 +173,15 @@ public class History { } + public int totalSize() { + int i = 0; + for (Sequence it : sequences()) { + i += it.elements.size(); + } + return i; + } + + public void clear() { sequences.clear(); } diff --git a/source/net/sourceforge/filebot/HistorySpooler.java b/source/net/sourceforge/filebot/HistorySpooler.java index a7a12b23..990ba4bf 100644 --- a/source/net/sourceforge/filebot/HistorySpooler.java +++ b/source/net/sourceforge/filebot/HistorySpooler.java @@ -3,7 +3,6 @@ package net.sourceforge.filebot; import static net.sourceforge.filebot.History.*; -import static net.sourceforge.filebot.Settings.*; import java.io.File; import java.io.IOException; @@ -25,22 +24,20 @@ public final class HistorySpooler { return instance; } - - private final File file = new File(getApplicationFolder(), "history.xml"); - - private final History sessionHistory = new History(); + private HistoryStorage persistentHistory = null; + private 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); + try { + if (getPersistentHistory() != null) { + history.addAll(getPersistentHistory().read().sequences()); } + } catch (IOException e) { + Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Failed to load history", e); } // add session history @@ -71,7 +68,9 @@ public final class HistorySpooler { public synchronized void commit(History history) { try { - exportHistory(history, file); + if (getPersistentHistory() != null) { + getPersistentHistory().write(history); + } // clear session history sessionHistory.clear(); @@ -89,14 +88,49 @@ public final class HistorySpooler { } - private HistorySpooler() { - // commit session history on shutdown - Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { - - @Override - public void run() { - commit(); - } - })); + public synchronized void setPersistentHistory(HistoryStorage persistentHistory) { + this.persistentHistory = persistentHistory; } + + + public synchronized HistoryStorage getPersistentHistory() { + return persistentHistory; + } + + + public static interface HistoryStorage { + + History read() throws IOException; + + + void write(History history) throws IOException; + } + + + public static class HistoryFileStorage implements HistoryStorage { + + private final File file; + + + public HistoryFileStorage(File file) { + this.file = file; + } + + + @Override + public History read() throws IOException { + if (file.exists()) { + return importHistory(file); + } else { + return new History(); + } + } + + + @Override + public void write(History history) throws IOException { + exportHistory(history, file); + } + } + } diff --git a/source/net/sourceforge/filebot/Main.java b/source/net/sourceforge/filebot/Main.java index 41971652..3541ae1b 100644 --- a/source/net/sourceforge/filebot/Main.java +++ b/source/net/sourceforge/filebot/Main.java @@ -4,7 +4,7 @@ package net.sourceforge.filebot; import static java.awt.GraphicsEnvironment.*; import static java.util.regex.Pattern.*; -import static javax.swing.JFrame.*; +import static javax.swing.JOptionPane.*; import static net.sourceforge.filebot.Settings.*; import static net.sourceforge.tuned.FileUtilities.*; import static net.sourceforge.tuned.ui.TunedUtilities.*; @@ -30,6 +30,7 @@ import java.security.ProtectionDomain; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Locale; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; @@ -39,6 +40,7 @@ import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; +import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.UIManager; @@ -46,6 +48,8 @@ import javax.xml.parsers.DocumentBuilderFactory; import net.miginfocom.swing.MigLayout; import net.sf.ehcache.CacheManager; +import net.sourceforge.filebot.HistorySpooler.HistoryFileStorage; +import net.sourceforge.filebot.HistorySpooler.HistoryStorage; import net.sourceforge.filebot.cli.ArgumentBean; import net.sourceforge.filebot.cli.ArgumentProcessor; import net.sourceforge.filebot.cli.CmdlineOperations; @@ -93,6 +97,7 @@ public class Main { // initialize this stuff before anything else initializeCache(); initializeSecurityManager(); + HistorySpooler.getInstance().setPersistentHistory(new HistoryFileStorage(new File(getApplicationFolder(), "history.xml"))); if (args.clearUserData()) { System.out.println("Reset preferences"); @@ -132,6 +137,15 @@ public class Main { // CLI mode => run command-line interface and then exit if (args.runCLI()) { + // commit session history on shutdown + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + + @Override + public void run() { + HistorySpooler.getInstance().commit(); + } + })); + // default cross-platform laf used in scripting to nimbus instead of metal (if possible) if (args.script != null && !isHeadless()) { try { @@ -192,6 +206,48 @@ public class Main { Logger.getLogger(Main.class.getName()).log(Level.WARNING, "Failed to check for updates", e); } } + + // hook donation reminder into rename history + if (useDonationReminder()) { + System.out.println("Main.main()"); + final HistoryStorage fileStorage = HistorySpooler.getInstance().getPersistentHistory(); + HistorySpooler.getInstance().setPersistentHistory(new HistoryStorage() { + + @Override + public void write(History history) throws IOException { + // store history + fileStorage.write(history); + + // display donation reminder + try { + PreferencesEntry persistentDonateLv = Settings.forPackage(Main.class).entry("donate.lv").defaultValue("0"); + int donateLv = Integer.parseInt(persistentDonateLv.getValue()); + int donateStep = 10000; + int usage = history.totalSize(); + + if (usage / donateStep > donateLv) { + persistentDonateLv.setValue(String.valueOf(Math.max(donateLv + 1, usage / donateStep))); + + String message = String.format(Locale.ROOT, "

Thank you for using FileBot!


It has taken many nights to develop this application. If you enjoy using it,
please consider a donation to the author of this software. It will help to
make FileBot even better!

You've renamed %,d files.


", history.totalSize()); + String[] actions = new String[] { "Donate!", "Later" }; + JOptionPane pane = new JOptionPane(message, INFORMATION_MESSAGE, YES_NO_OPTION, ResourceManager.getIcon("message.donate"), actions, actions[0]); + pane.createDialog(null, "Please Donate").setVisible(true); + if (pane.getValue() == actions[0]) { + Desktop.getDesktop().browse(URI.create(getApplicationProperty("donate.url"))); + } + } + } catch (Exception e) { + Logger.getLogger(Main.class.getName()).log(Level.WARNING, e.getMessage(), e); + } + } + + + @Override + public History read() throws IOException { + return fileStorage.read(); + } + }); + } } catch (CmdLineException e) { // illegal arguments => just print CLI error message and stop System.err.println(e.getMessage()); @@ -213,7 +269,15 @@ public class Main { } frame.setLocationByPlatform(true); - frame.setDefaultCloseOperation(EXIT_ON_CLOSE); + frame.addWindowListener(new WindowAdapter() { + + @Override + public void windowClosing(WindowEvent e) { + e.getWindow().setVisible(false); + HistorySpooler.getInstance().commit(); + System.exit(0); + } + }); try { // restore previous size and location diff --git a/source/net/sourceforge/filebot/Settings.java b/source/net/sourceforge/filebot/Settings.java index e6f0b7d7..2e35077b 100644 --- a/source/net/sourceforge/filebot/Settings.java +++ b/source/net/sourceforge/filebot/Settings.java @@ -64,6 +64,16 @@ public final class Settings { } + public static boolean useDonationReminder() { + String value = System.getProperty("useDonationReminder"); + if (value != null) { + return Boolean.parseBoolean(value); + } else { + return getApplicationDeployment() == null || !getApplicationDeployment().equalsIgnoreCase("ppa"); + } + } + + public static int getPreferredThreadPoolSize() { try { return Integer.parseInt(System.getProperty("threadPool")); diff --git a/source/net/sourceforge/filebot/Settings.properties b/source/net/sourceforge/filebot/Settings.properties index 781303ea..4039a3b6 100644 --- a/source/net/sourceforge/filebot/Settings.properties +++ b/source/net/sourceforge/filebot/Settings.properties @@ -5,6 +5,7 @@ application.revision: @{svn.revision} # application updates update.url: http://filebot.sourceforge.net/update.xml +donate.url: http://filebot.sourceforge.net/donate.html # base URL for resolving script resources script.fn: http://filebot.sourceforge.net/scripts/%s.groovy