From 71548a4f41f58e70e9a51181058640ba61cc885e Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 4 Aug 2016 15:05:54 +0800 Subject: [PATCH] Refactor ApplicationFolder --- source/net/filebot/ApplicationFolder.java | 48 ++++++++++++ source/net/filebot/CacheManager.java | 1 - source/net/filebot/HistorySpooler.java | 5 +- source/net/filebot/Main.java | 1 - source/net/filebot/Settings.java | 74 ------------------- source/net/filebot/cli/GroovyPad.java | 2 +- .../net/filebot/format/MediaBindingBean.java | 2 +- .../filebot/format/SecureCompiledScript.java | 2 +- source/net/filebot/mac/DropToUnlock.java | 2 +- source/net/filebot/mac/MacAppUtilities.java | 2 +- source/net/filebot/media/ReleaseInfo.java | 17 ++--- .../ui/rename/ExpressionFormatter.java | 2 +- source/net/filebot/ui/rename/RenamePanel.java | 1 + 13 files changed, 65 insertions(+), 94 deletions(-) create mode 100644 source/net/filebot/ApplicationFolder.java diff --git a/source/net/filebot/ApplicationFolder.java b/source/net/filebot/ApplicationFolder.java new file mode 100644 index 00000000..927a049b --- /dev/null +++ b/source/net/filebot/ApplicationFolder.java @@ -0,0 +1,48 @@ +package net.filebot; + +import static net.filebot.Logging.*; +import static net.filebot.Settings.*; +import static net.filebot.util.FileUtilities.*; + +import java.io.File; +import java.util.logging.Level; + +public enum ApplicationFolder { + + // real user home (the user.home will point to the application-specific container in sandbox environments) + UserHome(isMacSandbox() ? "/Users/" + System.getProperty("user.name") : System.getProperty("user.home")), + + AppData(System.getProperty("application.dir", UserHome.path(".filebot").getPath())), + + Temp(System.getProperty("java.io.tmpdir")), + + Cache(System.getProperty("application.cache", AppData.path("cache").getPath())); + + private final File path; + + ApplicationFolder(String path) { + this.path = new File(path); + } + + public File get() { + return path; + } + + public File path(String name) { + return new File(path, name); + } + + public File resolve(String name) { + return new File(getCanonicalFile(), name); + } + + public File getCanonicalFile() { + try { + return createFolders(path.getCanonicalFile()); + } catch (Exception e) { + debug.log(Level.SEVERE, String.format("Failed to create application folder: %s => %s", this, path), e); + return path; + } + } + +} diff --git a/source/net/filebot/CacheManager.java b/source/net/filebot/CacheManager.java index 08501919..8937f769 100644 --- a/source/net/filebot/CacheManager.java +++ b/source/net/filebot/CacheManager.java @@ -13,7 +13,6 @@ import java.nio.file.StandardOpenOption; import java.util.Scanner; import java.util.logging.Level; -import net.filebot.Settings.ApplicationFolder; import net.sf.ehcache.CacheException; import net.sf.ehcache.config.Configuration; import net.sf.ehcache.config.DiskStoreConfiguration; diff --git a/source/net/filebot/HistorySpooler.java b/source/net/filebot/HistorySpooler.java index 5acc8c5e..8f21c812 100644 --- a/source/net/filebot/HistorySpooler.java +++ b/source/net/filebot/HistorySpooler.java @@ -1,7 +1,6 @@ package net.filebot; import static net.filebot.Logging.*; -import static net.filebot.Settings.*; import java.io.File; import java.io.IOException; @@ -15,11 +14,11 @@ import java.util.Map; import java.util.Map.Entry; import java.util.logging.Level; -import net.filebot.History.Element; - import org.apache.commons.io.input.CloseShieldInputStream; import org.apache.commons.io.output.CloseShieldOutputStream; +import net.filebot.History.Element; + public final class HistorySpooler { private static final HistorySpooler instance = new HistorySpooler(); diff --git a/source/net/filebot/Main.java b/source/net/filebot/Main.java index e5dd774b..716afd65 100644 --- a/source/net/filebot/Main.java +++ b/source/net/filebot/Main.java @@ -40,7 +40,6 @@ import javax.swing.UIManager; import org.kohsuke.args4j.CmdLineException; import org.w3c.dom.Document; -import net.filebot.Settings.ApplicationFolder; import net.filebot.cli.ArgumentBean; import net.filebot.cli.ArgumentProcessor; import net.filebot.format.ExpressionFormat; diff --git a/source/net/filebot/Settings.java b/source/net/filebot/Settings.java index 5b2ecdc5..f8a9ecbe 100644 --- a/source/net/filebot/Settings.java +++ b/source/net/filebot/Settings.java @@ -1,7 +1,6 @@ package net.filebot; import static net.filebot.Logging.*; -import static net.filebot.util.FileUtilities.*; import java.awt.GraphicsEnvironment; import java.io.File; @@ -193,79 +192,6 @@ public final class Settings { return ApplicationFolder.AppData.get(); // added for script compatibility } - 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 { - return createFolders(path.getCanonicalFile()); - } 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)); } diff --git a/source/net/filebot/cli/GroovyPad.java b/source/net/filebot/cli/GroovyPad.java index 9592c3ad..049e00cf 100644 --- a/source/net/filebot/cli/GroovyPad.java +++ b/source/net/filebot/cli/GroovyPad.java @@ -35,9 +35,9 @@ import org.fife.ui.rsyntaxtextarea.SyntaxConstants; import org.fife.ui.rsyntaxtextarea.TextEditorPane; import org.fife.ui.rtextarea.RTextScrollPane; +import net.filebot.ApplicationFolder; import net.filebot.ResourceManager; import net.filebot.Settings; -import net.filebot.Settings.ApplicationFolder; import net.filebot.util.TeePrintStream; public class GroovyPad extends JFrame { diff --git a/source/net/filebot/format/MediaBindingBean.java b/source/net/filebot/format/MediaBindingBean.java index a2ae5fae..8c33824a 100644 --- a/source/net/filebot/format/MediaBindingBean.java +++ b/source/net/filebot/format/MediaBindingBean.java @@ -38,13 +38,13 @@ import java.util.regex.Pattern; import com.cedarsoftware.util.io.JsonWriter; +import net.filebot.ApplicationFolder; import net.filebot.Cache; import net.filebot.CacheType; import net.filebot.Language; import net.filebot.MediaTypes; import net.filebot.MetaAttributeView; import net.filebot.Settings; -import net.filebot.Settings.ApplicationFolder; import net.filebot.WebServices; import net.filebot.hash.HashType; import net.filebot.media.NamingStandard; diff --git a/source/net/filebot/format/SecureCompiledScript.java b/source/net/filebot/format/SecureCompiledScript.java index 74235a1c..f00894a7 100644 --- a/source/net/filebot/format/SecureCompiledScript.java +++ b/source/net/filebot/format/SecureCompiledScript.java @@ -21,7 +21,7 @@ import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptException; -import net.filebot.Settings.ApplicationFolder; +import net.filebot.ApplicationFolder; import net.filebot.util.ExceptionUtilities; public class SecureCompiledScript extends CompiledScript { diff --git a/source/net/filebot/mac/DropToUnlock.java b/source/net/filebot/mac/DropToUnlock.java index 31149083..469d7da9 100644 --- a/source/net/filebot/mac/DropToUnlock.java +++ b/source/net/filebot/mac/DropToUnlock.java @@ -118,7 +118,7 @@ public class DropToUnlock extends JList { }).filter(f -> f != null && isLockedFolder(f)).sorted().distinct().collect(Collectors.toList()); } - public static boolean showUnlockFoldersDialog(final Window owner, final Collection files) { + public static boolean showUnlockFoldersDialog(Window owner, Collection files) { final List model = getParentFolders(files); // immediately return if there is nothing that needs to be unlocked diff --git a/source/net/filebot/mac/MacAppUtilities.java b/source/net/filebot/mac/MacAppUtilities.java index 820a43b9..02483073 100644 --- a/source/net/filebot/mac/MacAppUtilities.java +++ b/source/net/filebot/mac/MacAppUtilities.java @@ -170,7 +170,7 @@ public class MacAppUtilities { return folder.isDirectory() && !folder.canRead(); } - public static boolean askUnlockFolders(final Window owner, final Collection files) { + public static boolean askUnlockFolders(Window owner, Collection files) { return DropToUnlock.showUnlockFoldersDialog(owner, files); } diff --git a/source/net/filebot/media/ReleaseInfo.java b/source/net/filebot/media/ReleaseInfo.java index 12281e1e..d49c7bb4 100644 --- a/source/net/filebot/media/ReleaseInfo.java +++ b/source/net/filebot/media/ReleaseInfo.java @@ -42,10 +42,10 @@ import java.util.stream.IntStream; import org.tukaani.xz.XZInputStream; +import net.filebot.ApplicationFolder; import net.filebot.Cache; import net.filebot.CacheType; import net.filebot.Resource; -import net.filebot.Settings.ApplicationFolder; import net.filebot.util.FileUtilities.RegexFileFilter; import net.filebot.util.SystemProperty; import net.filebot.web.Movie; @@ -220,12 +220,11 @@ public class ReleaseInfo { if (volumeRoots == null) { Set volumes = new HashSet(); - File userHome = ApplicationFolder.UserHome.get(); + File home = ApplicationFolder.UserHome.get(); List roots = getFileSystemRoots(); // user root folder - volumes.add(userHome); - volumes.addAll(getChildren(userHome, FOLDERS)); + volumes.add(home); // Windows / Linux / Mac system roots volumes.addAll(roots); @@ -245,12 +244,12 @@ public class ReleaseInfo { // Mac if (isMacSandbox()) { - File sandboxUserHome = new File(System.getProperty("user.home")); - - // e.g. ignore default Movie folder on Mac - for (File userFolder : getChildren(sandboxUserHome, FOLDERS)) { - volumes.add(new File(userHome, userFolder.getName())); + // e.g. ignore default Movie folder (user.home and real user home are different in the sandbox environment) + for (File userFolder : getChildren(new File(System.getProperty("user.home")), FOLDERS)) { + volumes.add(new File(home, userFolder.getName())); } + } else { + volumes.addAll(getChildren(home, FOLDERS)); } volumeRoots = unmodifiableSet(volumes); diff --git a/source/net/filebot/ui/rename/ExpressionFormatter.java b/source/net/filebot/ui/rename/ExpressionFormatter.java index f7cf3909..e55fdfb5 100644 --- a/source/net/filebot/ui/rename/ExpressionFormatter.java +++ b/source/net/filebot/ui/rename/ExpressionFormatter.java @@ -11,7 +11,7 @@ import java.util.logging.Level; import javax.script.ScriptException; -import net.filebot.Settings.ApplicationFolder; +import net.filebot.ApplicationFolder; import net.filebot.format.ExpressionFormat; import net.filebot.format.MediaBindingBean; import net.filebot.similarity.Match; diff --git a/source/net/filebot/ui/rename/RenamePanel.java b/source/net/filebot/ui/rename/RenamePanel.java index c8ede9f3..f91373b2 100644 --- a/source/net/filebot/ui/rename/RenamePanel.java +++ b/source/net/filebot/ui/rename/RenamePanel.java @@ -52,6 +52,7 @@ import com.google.common.eventbus.Subscribe; import ca.odell.glazedlists.EventList; import ca.odell.glazedlists.ListSelection; import ca.odell.glazedlists.swing.DefaultEventSelectionModel; +import net.filebot.ApplicationFolder; import net.filebot.History; import net.filebot.HistorySpooler; import net.filebot.Language;