2009-01-24 19:08:57 -05:00
|
|
|
|
|
|
|
package net.sourceforge.filebot;
|
|
|
|
|
|
|
|
|
2011-11-28 22:14:39 -05:00
|
|
|
import static net.sourceforge.tuned.StringUtilities.*;
|
|
|
|
|
|
|
|
import java.awt.GraphicsEnvironment;
|
2009-07-27 18:34:42 -04:00
|
|
|
import java.io.File;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.ResourceBundle;
|
2011-11-28 22:14:39 -05:00
|
|
|
import java.util.jar.Manifest;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2009-01-24 19:08:57 -05:00
|
|
|
import java.util.prefs.BackingStoreException;
|
|
|
|
import java.util.prefs.Preferences;
|
|
|
|
|
2009-02-09 15:56:20 -05:00
|
|
|
import net.sourceforge.tuned.ExceptionUtilities;
|
2009-01-24 19:08:57 -05:00
|
|
|
import net.sourceforge.tuned.PreferencesList;
|
|
|
|
import net.sourceforge.tuned.PreferencesMap;
|
2009-03-08 14:41:11 -04:00
|
|
|
import net.sourceforge.tuned.PreferencesMap.PreferencesEntry;
|
|
|
|
import net.sourceforge.tuned.PreferencesMap.StringAdapter;
|
2009-01-24 19:08:57 -05:00
|
|
|
|
|
|
|
|
|
|
|
public final class Settings {
|
|
|
|
|
|
|
|
public static String getApplicationName() {
|
2009-07-27 18:34:42 -04:00
|
|
|
return getApplicationProperty("application.name");
|
2009-01-24 19:08:57 -05:00
|
|
|
};
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public static String getApplicationVersion() {
|
2009-07-27 18:34:42 -04:00
|
|
|
return getApplicationProperty("application.version");
|
2009-01-24 19:08:57 -05:00
|
|
|
};
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-07-27 18:34:42 -04:00
|
|
|
public static String getApplicationProperty(String key) {
|
|
|
|
return ResourceBundle.getBundle(Settings.class.getName(), Locale.ROOT).getString(key);
|
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2011-09-21 09:29:21 -04:00
|
|
|
public static String getApplicationDeployment() {
|
|
|
|
String deployment = System.getProperty("application.deployment");
|
|
|
|
if (deployment != null)
|
|
|
|
return deployment;
|
|
|
|
|
|
|
|
if (System.getProperty("javawebstart.version") != null)
|
|
|
|
return "webstart";
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-07-27 18:34:42 -04:00
|
|
|
public static File getApplicationFolder() {
|
|
|
|
// special handling for web start
|
2011-09-21 09:29:21 -04:00
|
|
|
if (getApplicationDeployment() != null) {
|
2009-07-27 18:34:42 -04:00
|
|
|
// can't use working directory for web start applications
|
2011-01-06 22:53:59 -05:00
|
|
|
File folder = new File(System.getProperty("user.home"), ".filebot");
|
2009-07-27 18:34:42 -04:00
|
|
|
|
|
|
|
// create folder if necessary
|
|
|
|
if (!folder.exists()) {
|
|
|
|
folder.mkdir();
|
|
|
|
}
|
|
|
|
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
|
|
|
|
// use working directory
|
|
|
|
return new File(System.getProperty("user.dir"));
|
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-07-29 16:31:08 -04:00
|
|
|
public static Settings forPackage(Class<?> type) {
|
|
|
|
return new Settings(Preferences.userNodeForPackage(type));
|
|
|
|
}
|
2009-01-24 19:08:57 -05:00
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
private final Preferences prefs;
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-02-22 05:45:41 -05:00
|
|
|
private Settings(Preferences prefs) {
|
|
|
|
this.prefs = prefs;
|
2009-01-24 19:08:57 -05:00
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public Settings node(String nodeName) {
|
2009-03-15 13:44:39 -04:00
|
|
|
return new Settings(prefs.node(nodeName));
|
2009-01-24 19:08:57 -05:00
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-03-12 16:08:42 -04:00
|
|
|
public String get(String key) {
|
|
|
|
return get(key, null);
|
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-03-12 16:08:42 -04:00
|
|
|
public String get(String key, String def) {
|
|
|
|
return prefs.get(key, def);
|
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public void put(String key, String value) {
|
|
|
|
prefs.put(key, value);
|
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-03-12 16:08:42 -04:00
|
|
|
public void remove(String key) {
|
|
|
|
prefs.remove(key);
|
2009-01-24 19:08:57 -05:00
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-03-12 16:08:42 -04:00
|
|
|
public PreferencesEntry<String> entry(String key) {
|
|
|
|
return new PreferencesEntry<String>(prefs, key, new StringAdapter());
|
2009-01-24 19:08:57 -05:00
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-07-18 18:06:32 -04:00
|
|
|
public PreferencesMap<String> asMap() {
|
2009-03-08 14:41:11 -04:00
|
|
|
return PreferencesMap.map(prefs);
|
2009-01-24 19:08:57 -05:00
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-07-18 18:06:32 -04:00
|
|
|
public PreferencesList<String> asList() {
|
2009-03-08 14:41:11 -04:00
|
|
|
return PreferencesList.map(prefs);
|
2009-01-24 19:08:57 -05:00
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public void clear() {
|
|
|
|
try {
|
|
|
|
// remove child nodes
|
|
|
|
for (String nodeName : prefs.childrenNames()) {
|
|
|
|
prefs.node(nodeName).removeNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove entries
|
|
|
|
prefs.clear();
|
|
|
|
} catch (BackingStoreException e) {
|
2009-02-09 15:56:20 -05:00
|
|
|
throw ExceptionUtilities.asRuntimeException(e);
|
2009-01-24 19:08:57 -05:00
|
|
|
}
|
|
|
|
}
|
2011-11-28 22:14:39 -05:00
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
|
|
|
public static int getApplicationRevisionNumber() {
|
2011-11-28 22:14:39 -05:00
|
|
|
try {
|
|
|
|
Manifest manifest = new Manifest(Settings.class.getResourceAsStream("/META-INF/MANIFEST.MF"));
|
2011-12-22 14:36:31 -05:00
|
|
|
String rev = manifest.getMainAttributes().getValue("Built-Revision");
|
|
|
|
return Integer.parseInt(rev);
|
|
|
|
} catch (Exception e) {
|
2011-11-28 22:14:39 -05:00
|
|
|
Logger.getLogger(Settings.class.getName()).log(Level.WARNING, e.getMessage());
|
2011-12-22 14:36:31 -05:00
|
|
|
return 0;
|
2011-11-28 22:14:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
|
|
|
|
public static String getApplicationIdentifier() {
|
|
|
|
return joinBy(" ", getApplicationName(), getApplicationVersion(), String.format("(r%s)", getApplicationRevisionNumber()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-28 22:14:39 -05:00
|
|
|
public static String getJavaRuntimeIdentifier() {
|
|
|
|
String name = System.getProperty("java.runtime.name");
|
|
|
|
String version = System.getProperty("java.version");
|
|
|
|
String headless = GraphicsEnvironment.isHeadless() ? "(headless)" : null;
|
|
|
|
return joinBy(" ", name, version, headless);
|
|
|
|
}
|
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
}
|