1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-15 13:55:03 -05:00
filebot/source/net/filebot/Settings.java

203 lines
5.2 KiB
Java
Raw Normal View History

2014-04-19 02:30:29 -04:00
package net.filebot;
2014-04-19 02:30:29 -04:00
import static net.filebot.util.StringUtilities.*;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
2014-04-19 02:30:29 -04:00
import net.filebot.cli.ArgumentBean;
import net.filebot.util.ExceptionUtilities;
import net.filebot.util.PreferencesList;
import net.filebot.util.PreferencesMap;
import net.filebot.util.PreferencesMap.PreferencesEntry;
import net.filebot.util.PreferencesMap.StringAdapter;
public final class Settings {
2014-01-25 22:51:47 -05:00
public static String getApplicationName() {
return getApplicationProperty("application.name");
2011-12-24 02:30:54 -05:00
}
2014-01-25 22:51:47 -05:00
public static String getApplicationVersion() {
return getApplicationProperty("application.version");
2011-12-24 02:30:54 -05:00
}
2014-01-25 22:51:47 -05:00
public static int getApplicationRevisionNumber() {
try {
return Integer.parseInt(getApplicationProperty("application.revision"));
} catch (Exception e) {
return 0;
}
}
2014-01-25 22:51:47 -05:00
public static String getApplicationProperty(String key) {
return ResourceBundle.getBundle(Settings.class.getName(), Locale.ROOT).getString(key);
}
2014-01-25 22:51:47 -05:00
public static boolean isUnixFS() {
return Boolean.parseBoolean(System.getProperty("unixfs"));
}
2014-01-25 22:51:47 -05:00
public static boolean useNativeShell() {
return Boolean.parseBoolean(System.getProperty("useNativeShell"));
}
2014-01-25 22:51:47 -05:00
public static boolean useGVFS() {
return Boolean.parseBoolean(System.getProperty("useGVFS"));
}
2014-01-25 22:51:47 -05:00
public static boolean useExtendedFileAttributes() {
return Boolean.parseBoolean(System.getProperty("useExtendedFileAttributes"));
}
2014-01-25 22:51:47 -05:00
public static boolean useCreationDate() {
return Boolean.parseBoolean(System.getProperty("useCreationDate"));
}
public static boolean useDonationReminder() {
return !("ppa".equals(getApplicationDeployment()) || "mas".equals(getApplicationDeployment()));
}
public static boolean isSandboxed() {
return "mas".equals(System.getProperty("application.deployment"));
}
2014-01-25 22:51:47 -05:00
public static int getPreferredThreadPoolSize() {
try {
return Integer.parseInt(System.getProperty("threadPool"));
} catch (Exception e) {
return Runtime.getRuntime().availableProcessors();
}
}
2014-01-25 22:51:47 -05:00
public static String getApplicationDeployment() {
return System.getProperty("application.deployment");
}
2014-01-25 22:51:47 -05:00
public static File getApplicationFolder() {
2011-12-28 08:51:36 -05:00
String applicationDirPath = System.getProperty("application.dir");
File applicationFolder = null;
2014-01-25 22:51:47 -05:00
2011-12-28 08:51:36 -05:00
if (applicationDirPath != null && applicationDirPath.length() > 0) {
// use given path
applicationFolder = new File(applicationDirPath);
} else {
2011-12-28 08:51:36 -05:00
// create folder in user home (can't use working directory for web start applications)
applicationFolder = new File(System.getProperty("user.home"), ".filebot");
}
2014-01-25 22:51:47 -05:00
// create folder if necessary
2011-12-28 08:51:36 -05:00
if (!applicationFolder.exists()) {
applicationFolder.mkdirs();
}
return applicationFolder;
2014-07-28 09:29:00 -04:00
}
public static File getApplicationCache() {
String cacheDefaultPath = System.getProperty("application.cache");
File cacheDir = null;
if (cacheDefaultPath != null) {
cacheDir = new File(cacheDefaultPath);
} else {
cacheDir = new File(getApplicationFolder(), "cache");
}
// create folder if necessary
if (!cacheDir.exists()) {
cacheDir.mkdirs();
}
return cacheDir;
}
2014-01-25 22:51:47 -05:00
public static Settings forPackage(Class<?> type) {
return new Settings(Preferences.userNodeForPackage(type));
}
2014-01-25 22:51:47 -05:00
private final Preferences prefs;
2014-01-25 22:51:47 -05:00
private Settings(Preferences prefs) {
this.prefs = prefs;
}
2014-01-25 22:51:47 -05:00
public Settings node(String nodeName) {
return new Settings(prefs.node(nodeName));
}
2014-01-25 22:51:47 -05:00
public String get(String key) {
return get(key, null);
}
2014-01-25 22:51:47 -05:00
public String get(String key, String def) {
return prefs.get(key, def);
}
2014-01-25 22:51:47 -05:00
public void put(String key, String value) {
if (value != null) {
prefs.put(key, value);
} else {
remove(key);
}
}
2014-01-25 22:51:47 -05:00
public void remove(String key) {
prefs.remove(key);
}
2014-01-25 22:51:47 -05:00
public PreferencesEntry<String> entry(String key) {
return new PreferencesEntry<String>(prefs, key, new StringAdapter());
}
2014-01-25 22:51:47 -05:00
public PreferencesMap<String> asMap() {
return PreferencesMap.map(prefs);
}
2014-01-25 22:51:47 -05:00
public PreferencesList<String> asList() {
return PreferencesList.map(prefs);
}
2014-01-25 22:51:47 -05:00
public void clear() {
try {
// remove child nodes
for (String nodeName : prefs.childrenNames()) {
prefs.node(nodeName).removeNode();
}
2014-01-25 22:51:47 -05:00
// remove entries
prefs.clear();
} catch (BackingStoreException e) {
throw ExceptionUtilities.asRuntimeException(e);
}
}
2014-01-25 22:51:47 -05:00
public static String getApplicationIdentifier() {
return joinBy(" ", getApplicationName(), getApplicationVersion(), String.format("(r%s)", getApplicationRevisionNumber()));
}
2014-01-25 22:51:47 -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);
}
2014-01-25 22:51:47 -05:00
private static String[] applicationArgumentArray;
protected static void setApplicationArgumentArray(String[] args) {
applicationArgumentArray = args;
}
public static ArgumentBean getApplicationArguments() {
try {
return ArgumentBean.parse(applicationArgumentArray);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}