* added donation reminder for power users

This commit is contained in:
Reinhard Pointner 2013-02-25 17:27:34 +00:00
parent 4e8df5b005
commit 346601acad
6 changed files with 141 additions and 22 deletions

View File

@ -583,6 +583,7 @@
<include name="*.ico" /> <include name="*.ico" />
<include name="images/**" /> <include name="images/**" />
<include name="screenshots/**" /> <include name="screenshots/**" />
<include name=".htaccess" />
</fileset> </fileset>
</copy> </copy>

View File

@ -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() { public void clear() {
sequences.clear(); sequences.clear();
} }

View File

@ -3,7 +3,6 @@ package net.sourceforge.filebot;
import static net.sourceforge.filebot.History.*; import static net.sourceforge.filebot.History.*;
import static net.sourceforge.filebot.Settings.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -25,23 +24,21 @@ public final class HistorySpooler {
return instance; return instance;
} }
private HistoryStorage persistentHistory = null;
private final File file = new File(getApplicationFolder(), "history.xml"); private History sessionHistory = new History();
private final History sessionHistory = new History();
public synchronized History getCompleteHistory() { public synchronized History getCompleteHistory() {
History history = new History(); History history = new History();
// add persistent history // add persistent history
if (file.exists()) {
try { try {
history.addAll(importHistory(file).sequences()); if (getPersistentHistory() != null) {
history.addAll(getPersistentHistory().read().sequences());
}
} catch (IOException e) { } catch (IOException e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Failed to load history", e); Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Failed to load history", e);
} }
}
// add session history // add session history
history.addAll(sessionHistory.sequences()); history.addAll(sessionHistory.sequences());
@ -71,7 +68,9 @@ public final class HistorySpooler {
public synchronized void commit(History history) { public synchronized void commit(History history) {
try { try {
exportHistory(history, file); if (getPersistentHistory() != null) {
getPersistentHistory().write(history);
}
// clear session history // clear session history
sessionHistory.clear(); sessionHistory.clear();
@ -89,14 +88,49 @@ public final class HistorySpooler {
} }
private HistorySpooler() { public synchronized void setPersistentHistory(HistoryStorage persistentHistory) {
// commit session history on shutdown this.persistentHistory = persistentHistory;
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { }
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 @Override
public void run() { public History read() throws IOException {
commit(); if (file.exists()) {
} return importHistory(file);
})); } else {
return new History();
} }
} }
@Override
public void write(History history) throws IOException {
exportHistory(history, file);
}
}
}

View File

@ -4,7 +4,7 @@ package net.sourceforge.filebot;
import static java.awt.GraphicsEnvironment.*; import static java.awt.GraphicsEnvironment.*;
import static java.util.regex.Pattern.*; 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.filebot.Settings.*;
import static net.sourceforge.tuned.FileUtilities.*; import static net.sourceforge.tuned.FileUtilities.*;
import static net.sourceforge.tuned.ui.TunedUtilities.*; import static net.sourceforge.tuned.ui.TunedUtilities.*;
@ -30,6 +30,7 @@ import java.security.ProtectionDomain;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -39,6 +40,7 @@ import javax.swing.JButton;
import javax.swing.JDialog; import javax.swing.JDialog;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.UIManager; import javax.swing.UIManager;
@ -46,6 +48,8 @@ import javax.xml.parsers.DocumentBuilderFactory;
import net.miginfocom.swing.MigLayout; import net.miginfocom.swing.MigLayout;
import net.sf.ehcache.CacheManager; 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.ArgumentBean;
import net.sourceforge.filebot.cli.ArgumentProcessor; import net.sourceforge.filebot.cli.ArgumentProcessor;
import net.sourceforge.filebot.cli.CmdlineOperations; import net.sourceforge.filebot.cli.CmdlineOperations;
@ -93,6 +97,7 @@ public class Main {
// initialize this stuff before anything else // initialize this stuff before anything else
initializeCache(); initializeCache();
initializeSecurityManager(); initializeSecurityManager();
HistorySpooler.getInstance().setPersistentHistory(new HistoryFileStorage(new File(getApplicationFolder(), "history.xml")));
if (args.clearUserData()) { if (args.clearUserData()) {
System.out.println("Reset preferences"); System.out.println("Reset preferences");
@ -132,6 +137,15 @@ public class Main {
// CLI mode => run command-line interface and then exit // CLI mode => run command-line interface and then exit
if (args.runCLI()) { 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) // default cross-platform laf used in scripting to nimbus instead of metal (if possible)
if (args.script != null && !isHeadless()) { if (args.script != null && !isHeadless()) {
try { try {
@ -192,6 +206,48 @@ public class Main {
Logger.getLogger(Main.class.getName()).log(Level.WARNING, "Failed to check for updates", e); 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<String> 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, "<html><p style='font-size:16pt; font-weight:bold'>Thank you for using FileBot!</p><br><p>It has taken many nights to develop this application. If you enjoy using it,<br>please consider a donation to the author of this software. It will help to<br>make FileBot even better!<p><p style='font-size:14pt; font-weight:bold'>You've renamed %,d files.</p><br><html>", 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) { } catch (CmdLineException e) {
// illegal arguments => just print CLI error message and stop // illegal arguments => just print CLI error message and stop
System.err.println(e.getMessage()); System.err.println(e.getMessage());
@ -213,7 +269,15 @@ public class Main {
} }
frame.setLocationByPlatform(true); 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 { try {
// restore previous size and location // restore previous size and location

View File

@ -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() { public static int getPreferredThreadPoolSize() {
try { try {
return Integer.parseInt(System.getProperty("threadPool")); return Integer.parseInt(System.getProperty("threadPool"));

View File

@ -5,6 +5,7 @@ application.revision: @{svn.revision}
# application updates # application updates
update.url: http://filebot.sourceforge.net/update.xml update.url: http://filebot.sourceforge.net/update.xml
donate.url: http://filebot.sourceforge.net/donate.html
# base URL for resolving script resources # base URL for resolving script resources
script.fn: http://filebot.sourceforge.net/scripts/%s.groovy script.fn: http://filebot.sourceforge.net/scripts/%s.groovy