2014-04-19 02:30:29 -04:00
|
|
|
package net.filebot;
|
2009-01-24 19:08:57 -05:00
|
|
|
|
2014-04-19 02:30:29 -04:00
|
|
|
import static net.filebot.util.StringUtilities.*;
|
2011-11-28 22:14:39 -05:00
|
|
|
|
|
|
|
import java.awt.GraphicsEnvironment;
|
2009-07-27 18:34:42 -04:00
|
|
|
import java.io.File;
|
|
|
|
import java.util.Locale;
|
2014-08-07 05:35:19 -04:00
|
|
|
import java.util.MissingResourceException;
|
2009-07-27 18:34:42 -04:00
|
|
|
import java.util.ResourceBundle;
|
2014-11-04 08:45:27 -05:00
|
|
|
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;
|
|
|
|
|
2014-07-29 02:40:35 -04:00
|
|
|
import net.filebot.UserFiles.FileChooser;
|
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;
|
2009-01-24 19:08:57 -05:00
|
|
|
|
|
|
|
public final class Settings {
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public static String getApplicationName() {
|
2009-07-27 18:34:42 -04:00
|
|
|
return getApplicationProperty("application.name");
|
2011-12-24 02:30:54 -05:00
|
|
|
}
|
2014-01-25 22:51:47 -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");
|
2011-12-24 02:30:54 -05:00
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2012-02-22 03:11:56 -05:00
|
|
|
public static int getApplicationRevisionNumber() {
|
2012-03-07 09:06:10 -05:00
|
|
|
try {
|
|
|
|
return Integer.parseInt(getApplicationProperty("application.revision"));
|
|
|
|
} catch (Exception e) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-22 03:11:56 -05:00
|
|
|
}
|
2014-01-25 22:51:47 -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);
|
|
|
|
}
|
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) {
|
2014-08-09 01:10:38 -04:00
|
|
|
// ignore, fall back to default
|
2014-08-07 05:35:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return bundle.getString("apikey." + name);
|
|
|
|
}
|
|
|
|
|
2012-06-30 04:46:55 -04:00
|
|
|
public static boolean isUnixFS() {
|
|
|
|
return Boolean.parseBoolean(System.getProperty("unixfs"));
|
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2012-07-17 16:52:03 -04:00
|
|
|
public static boolean useNativeShell() {
|
2012-07-19 23:38:47 -04:00
|
|
|
return Boolean.parseBoolean(System.getProperty("useNativeShell"));
|
2012-07-17 16:52:03 -04:00
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2013-01-11 21:19:47 -05:00
|
|
|
public static boolean useGVFS() {
|
|
|
|
return Boolean.parseBoolean(System.getProperty("useGVFS"));
|
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2012-10-23 15:05:55 -04: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"));
|
|
|
|
}
|
|
|
|
|
2014-08-10 02:31:47 -04:00
|
|
|
public static boolean isMacApp() {
|
|
|
|
return "mas".equals(getApplicationDeployment()) || "app".equals(getApplicationDeployment());
|
|
|
|
}
|
|
|
|
|
2014-08-07 05:35:19 -04:00
|
|
|
public static boolean isAppStore() {
|
|
|
|
return "mas".equals(getApplicationDeployment()) || "usc".equals(getApplicationDeployment());
|
2014-07-28 08:54:34 -04:00
|
|
|
}
|
|
|
|
|
2014-07-30 21:20:27 -04:00
|
|
|
public static boolean isMacSandbox() {
|
2014-07-28 15:20:55 -04:00
|
|
|
return "mas".equals(getApplicationDeployment());
|
2013-02-25 12:27:34 -05:00
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2014-07-29 02:40:35 -04:00
|
|
|
public static FileChooser getPreferredFileChooser() {
|
2014-08-30 08:07:47 -04:00
|
|
|
return FileChooser.valueOf(System.getProperty("net.filebot.UserFiles.fileChooser", "Swing"));
|
2014-07-29 02:40:35 -04:00
|
|
|
}
|
|
|
|
|
2012-06-30 04:46:55 -04:00
|
|
|
public static int getPreferredThreadPoolSize() {
|
|
|
|
try {
|
2014-11-04 08:45:27 -05:00
|
|
|
String threadPool = System.getProperty("threadPool");
|
|
|
|
if (threadPool != null) {
|
|
|
|
return Integer.parseInt(threadPool);
|
|
|
|
}
|
2012-06-30 04:46:55 -04:00
|
|
|
} catch (Exception e) {
|
2014-11-04 08:45:27 -05:00
|
|
|
Logger.getLogger(Settings.class.getName()).log(Level.WARNING, e.toString());
|
2012-06-30 04:46:55 -04:00
|
|
|
}
|
2014-11-04 08:45:27 -05:00
|
|
|
|
|
|
|
return Runtime.getRuntime().availableProcessors();
|
2012-06-30 04:46:55 -04:00
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2011-09-21 09:29:21 -04:00
|
|
|
public static String getApplicationDeployment() {
|
2014-07-28 08:54:34 -04:00
|
|
|
return System.getProperty("application.deployment");
|
2011-09-21 09:29:21 -04:00
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2009-07-27 18:34:42 -04:00
|
|
|
public static File getApplicationFolder() {
|
2014-08-30 08:07:47 -04:00
|
|
|
String applicationFolderPath = System.getProperty("application.dir");
|
2011-12-28 08:51:36 -05:00
|
|
|
File applicationFolder = null;
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2014-08-30 08:07:47 -04:00
|
|
|
if (applicationFolderPath != null && !applicationFolderPath.isEmpty()) {
|
2011-12-28 08:51:36 -05:00
|
|
|
// use given path
|
2014-08-30 08:07:47 -04:00
|
|
|
applicationFolder = new File(applicationFolderPath);
|
2013-02-02 19:46:46 -05:00
|
|
|
} 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");
|
2009-07-27 18:34:42 -04:00
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2012-07-19 23:38:47 -04: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() {
|
2014-08-30 08:07:47 -04:00
|
|
|
String cacheFolderPath = System.getProperty("application.cache");
|
|
|
|
File cacheFolder = null;
|
2014-07-28 09:29:00 -04:00
|
|
|
|
2014-08-30 08:07:47 -04:00
|
|
|
if (cacheFolderPath != null && !cacheFolderPath.isEmpty()) {
|
|
|
|
cacheFolder = new File(cacheFolderPath);
|
2014-07-28 09:29:00 -04:00
|
|
|
} else {
|
2014-08-30 08:07:47 -04:00
|
|
|
cacheFolder = new File(getApplicationFolder(), "cache");
|
2014-07-28 09:29:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// create folder if necessary
|
2014-08-30 08:07:47 -04:00
|
|
|
if (!cacheFolder.exists()) {
|
|
|
|
cacheFolder.mkdirs();
|
2014-07-28 09:29:00 -04:00
|
|
|
}
|
2014-08-30 08:07:47 -04:00
|
|
|
return cacheFolder;
|
2014-10-27 15:09:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static File getRealUserHome() {
|
|
|
|
if (isMacSandbox()) {
|
|
|
|
// when running sandboxed applications user.home may point to the application-specific container
|
|
|
|
String username = System.getProperty("user.name");
|
|
|
|
if (username != null && username.length() > 0) {
|
|
|
|
return new File("/Users", username);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// default home
|
|
|
|
return new File(System.getProperty("user.home"));
|
2009-07-27 18:34:42 -04:00
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2009-07-29 16:31:08 -04:00
|
|
|
public static Settings forPackage(Class<?> type) {
|
|
|
|
return new Settings(Preferences.userNodeForPackage(type));
|
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
private final Preferences prefs;
|
2014-01-25 22:51:47 -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
|
|
|
}
|
2014-01-25 22:51:47 -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
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2009-03-12 16:08:42 -04:00
|
|
|
public String get(String key) {
|
|
|
|
return get(key, null);
|
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2009-03-12 16:08:42 -04:00
|
|
|
public String get(String key, String def) {
|
|
|
|
return prefs.get(key, def);
|
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
public void put(String key, String value) {
|
2013-02-26 01:21:02 -05:00
|
|
|
if (value != null) {
|
|
|
|
prefs.put(key, value);
|
|
|
|
} else {
|
|
|
|
remove(key);
|
|
|
|
}
|
2009-01-24 19:08:57 -05:00
|
|
|
}
|
2014-01-25 22:51:47 -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
|
|
|
}
|
2014-01-25 22:51:47 -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
|
|
|
}
|
2014-01-25 22:51:47 -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
|
|
|
}
|
2014-01-25 22:51:47 -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
|
|
|
}
|
2014-01-25 22:51:47 -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();
|
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2011-12-22 14:36:31 -05:00
|
|
|
public static String getApplicationIdentifier() {
|
|
|
|
return joinBy(" ", getApplicationName(), getApplicationVersion(), String.format("(r%s)", getApplicationRevisionNumber()));
|
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
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);
|
|
|
|
}
|
2014-01-25 22:51:47 -05:00
|
|
|
|
2014-04-13 15:35:23 -04: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-24 19:08:57 -05:00
|
|
|
}
|