Refactor ApplicationFolder

This commit is contained in:
Reinhard Pointner 2016-08-04 15:05:54 +08:00
parent 851cd9dc3b
commit 71548a4f41
13 changed files with 65 additions and 94 deletions

View File

@ -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;
}
}
}

View File

@ -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;

View File

@ -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();

View File

@ -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;

View File

@ -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));
}

View File

@ -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 {

View File

@ -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;

View File

@ -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 {

View File

@ -118,7 +118,7 @@ public class DropToUnlock extends JList<File> {
}).filter(f -> f != null && isLockedFolder(f)).sorted().distinct().collect(Collectors.toList());
}
public static boolean showUnlockFoldersDialog(final Window owner, final Collection<File> files) {
public static boolean showUnlockFoldersDialog(Window owner, Collection<File> files) {
final List<File> model = getParentFolders(files);
// immediately return if there is nothing that needs to be unlocked

View File

@ -170,7 +170,7 @@ public class MacAppUtilities {
return folder.isDirectory() && !folder.canRead();
}
public static boolean askUnlockFolders(final Window owner, final Collection<File> files) {
public static boolean askUnlockFolders(Window owner, Collection<File> files) {
return DropToUnlock.showUnlockFoldersDialog(owner, files);
}

View File

@ -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<File> volumes = new HashSet<File>();
File userHome = ApplicationFolder.UserHome.get();
File home = ApplicationFolder.UserHome.get();
List<File> 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);

View File

@ -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;

View File

@ -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;