1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-25 17:28:51 -05:00
filebot/source/net/filebot/Settings.java

322 lines
7.9 KiB
Java
Raw Normal View History

2014-04-19 02:30:29 -04:00
package net.filebot;
2016-03-09 15:36:28 -05:00
import static net.filebot.Logging.*;
import static net.filebot.util.FileUtilities.*;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.util.Locale;
2014-08-07 05:35:19 -04:00
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import net.filebot.UserFiles.FileChooser;
import net.filebot.archive.Archive.Extractor;
2014-04-19 02:30:29 -04:00
import net.filebot.cli.ArgumentBean;
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
2014-08-07 05:35:19 -04:00
public static String getApiKey(String name) {
ResourceBundle bundle = ResourceBundle.getBundle(Settings.class.getName(), Locale.ROOT);
if (isAppStore()) {
try {
2014-08-11 01:51:58 -04:00
return bundle.getString("apikey.appstore." + name);
2014-08-07 05:35:19 -04:00
} catch (MissingResourceException e) {
2016-03-08 04:43:59 -05:00
// use default value
2014-08-07 05:35:19 -04:00
}
}
return bundle.getString("apikey." + name);
}
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"));
}
2016-03-05 16:06:26 -05:00
public static boolean useRenameHistory() {
return Boolean.parseBoolean(System.getProperty("application.rename.history", "true"));
}
public static String getApplicationDeployment() {
return System.getProperty("application.deployment", "jar");
2014-08-10 02:31:47 -04:00
}
2014-08-07 05:35:19 -04:00
public static boolean isAppStore() {
return isApplicationDeployment("mas", "usc");
}
public static boolean isUbuntuApp() {
return isApplicationDeployment("usc");
}
public static boolean isMacApp() {
return isApplicationDeployment("mas", "app");
}
public static boolean isMacSandbox() {
return isApplicationDeployment("mas");
}
public static boolean isInstalled() {
return isApplicationDeployment("mas", "usc", "msi", "spk", "aur");
}
private static boolean isApplicationDeployment(String... ids) {
String current = getApplicationDeployment();
for (String id : ids) {
if (current != null && current.equals(id))
return true;
}
return false;
}
2014-01-25 22:51:47 -05:00
public static FileChooser getPreferredFileChooser() {
2014-08-30 08:07:47 -04:00
return FileChooser.valueOf(System.getProperty("net.filebot.UserFiles.fileChooser", "Swing"));
}
public static Extractor getPreferredArchiveExtractor() {
return Extractor.valueOf(System.getProperty("net.filebot.Archive.extractor", "SevenZipNativeBindings"));
}
public static int getPreferredThreadPoolSize() {
try {
String threadPool = System.getProperty("threadPool");
if (threadPool != null) {
return Integer.parseInt(threadPool);
}
} catch (Exception e) {
2016-03-09 15:36:28 -05:00
debug.log(Level.WARNING, e.getMessage(), e);
}
return Runtime.getRuntime().availableProcessors();
}
2014-01-25 22:51:47 -05:00
public static String getAppStoreName() {
if (isMacApp())
return "Mac App Store";
if (isUbuntuApp())
return "Ubuntu Software Center";
return null;
}
public static String getAppStoreLink() {
if (isMacApp())
return getApplicationProperty("link.mas");
if (isUbuntuApp())
return getApplicationProperty("link.usc");
return null;
2015-05-09 04:07:38 -04:00
}
public static String getDonateURL() {
2015-06-04 11:59:36 -04:00
return getApplicationProperty("donate.url") + "?src=" + getApplicationDeployment();
}
public static String getEmbeddedHelpURL() {
// add #hash so we can dynamically adjust the slides for the various platforms via JavaScript
return getApplicationProperty("link.app.help") + '#' + getApplicationDeployment();
}
2016-03-11 06:14:50 -05:00
public static String getApplicationIdentifier() {
return String.format("%s %s (r%d)", getApplicationName(), getApplicationVersion(), getApplicationRevisionNumber());
}
public static String getJavaRuntimeIdentifier() {
return String.format("%s %s %s", System.getProperty("java.runtime.name"), System.getProperty("java.version"), GraphicsEnvironment.isHeadless() ? "(headless)" : "").trim();
}
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);
}
}
2016-03-12 13:26:27 -05:00
public static File getApplicationFolder() {
return ApplicationFolder.AppData.get(); // added for script compatibility
2016-03-12 10:19:22 -05:00
}
2016-03-11 06:14:50 -05:00
public static enum ApplicationFolder {
AppData {
@Override
public File get() {
String appdata = System.getProperty("application.dir");
if (appdata != null) {
// use given $APP_DATA folder
return new File(appdata);
} else {
// use $HOME/.filebot as application data folder
return new File(System.getProperty("user.home"), ".filebot");
}
}
},
UserHome {
@Override
public File get() {
// The user.home of sandboxed applications will point to the application-specific container
if (isMacSandbox()) {
return new File("/Users", System.getProperty("user.name", "anonymous"));
}
// default user home
return new File(System.getProperty("user.home"));
}
},
Temp {
@Override
public File get() {
return new File(System.getProperty("java.io.tmpdir"));
}
},
Cache {
@Override
public File get() {
String cache = System.getProperty("application.cache");
if (cache != null) {
return new File(cache);
}
// default to $APP_DATA/cache
return AppData.resolve("cache");
}
};
public abstract File get();
public File resolve(String name) {
return new File(getCanonicalFile(), name);
}
public File getCanonicalFile() {
File path = get();
try {
2016-03-11 06:26:59 -05:00
return createFolders(path.getCanonicalFile());
2016-03-11 06:14:50 -05:00
} catch (Exception e) {
debug.log(Level.SEVERE, String.format("Failed to create application folder: %s => %s", this, path), e);
return path;
}
}
}
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) {
2016-03-27 16:40:27 -04:00
debug.warning(e.getMessage());
}
}
2014-01-25 22:51:47 -05:00
}