From 3ce5ac886ef6caf4ace2a4db29222603138eb00b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=F8=EDk?= Date: Sun, 24 Mar 2013 11:49:20 +0100 Subject: [PATCH] Storing configuration to user home (AppData on Windows) --- .../jpexs/decompiler/flash/Configuration.java | 86 +++++++++++++++++-- .../src/com/jpexs/decompiler/flash/Main.java | 3 +- 2 files changed, 80 insertions(+), 9 deletions(-) diff --git a/trunk/src/com/jpexs/decompiler/flash/Configuration.java b/trunk/src/com/jpexs/decompiler/flash/Configuration.java index c746e9423..d30236c55 100644 --- a/trunk/src/com/jpexs/decompiler/flash/Configuration.java +++ b/trunk/src/com/jpexs/decompiler/flash/Configuration.java @@ -18,6 +18,8 @@ package com.jpexs.decompiler.flash; import com.jpexs.proxy.Replacement; import java.io.*; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -26,17 +28,85 @@ import javax.swing.JOptionPane; public class Configuration { - private static final String CONFIG_NAME = "asdec.cfg"; - private static final String REPLACEMENTS_NAME = "replacements.ini"; + private static final String CONFIG_NAME = "config.bin"; + private static final String REPLACEMENTS_NAME = "replacements.cfg"; private static HashMap config = new HashMap(); + private static final File unspecifiedFile = new File("unspecified"); + private static File directory = unspecifiedFile; + + private enum OSId { + + WINDOWS, OSX, UNIX + } + + private static OSId getOSId() { + PrivilegedAction doGetOSName = new PrivilegedAction() { + @Override + public String run() { + return System.getProperty("os.name"); + } + }; + OSId id = OSId.UNIX; + String osName = AccessController.doPrivileged(doGetOSName); + if (osName != null) { + if (osName.toLowerCase().startsWith("mac os x")) { + id = OSId.OSX; + } else if (osName.contains("Windows")) { + id = OSId.WINDOWS; + } + } + return id; + } public static String getASDecHome() { - String dir = ".";//System.getProperty("user.home"); - if (!dir.endsWith(File.separator)) { - dir += File.separator; + if (directory == unspecifiedFile) { + directory = null; + String userHome = null; + try { + userHome = System.getProperty("user.home"); + } catch (SecurityException ignore) { + } + if (userHome != null) { + String applicationId = Main.shortApplicationName; + OSId osId = getOSId(); + if (osId == OSId.WINDOWS) { + File appDataDir = null; + try { + String appDataEV = System.getenv("APPDATA"); + if ((appDataEV != null) && (appDataEV.length() > 0)) { + appDataDir = new File(appDataEV); + } + } catch (SecurityException ignore) { + } + String vendorId = Main.vendor; + if ((appDataDir != null) && appDataDir.isDirectory()) { + // ${APPDATA}\{vendorId}\${applicationId} + String path = vendorId + "\\" + applicationId + "\\"; + directory = new File(appDataDir, path); + } else { + // ${userHome}\Application Data\${vendorId}\${applicationId} + String path = "Application Data\\" + vendorId + "\\" + applicationId + "\\"; + directory = new File(userHome, path); + } + } else if (osId == OSId.OSX) { + // ${userHome}/Library/Application Support/${applicationId} + String path = "Library/Application Support/" + applicationId + "/"; + directory = new File(userHome, path); + } else { + // ${userHome}/.${applicationId}/ + String path = "." + applicationId + "/"; + directory = new File(userHome, path); + } + } } - dir += "config" + File.separator; - return dir; + if(!directory.exists()){ + directory.mkdirs(); + } + String ret=directory.getAbsolutePath(); + if(!ret.endsWith(File.separator)){ + ret+=File.separator; + } + return ret; } private static String getReplacementsFile() { @@ -139,7 +209,7 @@ public class Configuration { oos = new ObjectOutputStream(new FileOutputStream(getConfigFile())); oos.writeObject(config); } catch (IOException ex) { - JOptionPane.showMessageDialog(null, "Cannot save configuration.\nPlease make application directory writable.", "Error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(null, "Cannot save configuration.", "Error", JOptionPane.ERROR_MESSAGE); Logger.getLogger(SWFInputStream.class.getName()).severe("Configuration directory is read only."); } finally { if (oos != null) { diff --git a/trunk/src/com/jpexs/decompiler/flash/Main.java b/trunk/src/com/jpexs/decompiler/flash/Main.java index 855760544..6b5ca1cb4 100644 --- a/trunk/src/com/jpexs/decompiler/flash/Main.java +++ b/trunk/src/com/jpexs/decompiler/flash/Main.java @@ -66,6 +66,7 @@ public class Main { public static final String shortApplicationName = "FFDec"; public static final String shortApplicationVerName = shortApplicationName + " v." + version; public static final String projectPage = "http://www.free-decompiler.com/flash"; + public static final String vendor = "JPEXS"; public static LoadingDialog loadingDialog; public static ModeFrame modeFrame; private static boolean working = false; @@ -779,7 +780,7 @@ public class Main { try { Logger logger = Logger.getLogger(""); logger.setLevel(debug ? Level.CONFIG : Level.WARNING); - FileHandler fileTxt = new FileHandler("log.txt"); + FileHandler fileTxt = new FileHandler(Configuration.getASDecHome()+File.separator+"log.txt"); SimpleFormatter formatterTxt = new SimpleFormatter(); fileTxt.setFormatter(formatterTxt);