From dd81f7acf659182f46911fc644be27dece4ca4f0 Mon Sep 17 00:00:00 2001 From: qjex Date: Sun, 15 Oct 2017 15:21:58 +0300 Subject: [PATCH] Default config paths added --- src/main/java/com/rarchives/ripme/App.java | 6 +-- .../com/rarchives/ripme/ui/MainWindow.java | 19 ++++++-- .../java/com/rarchives/ripme/utils/Utils.java | 47 ++++++++++++++++--- 3 files changed, 58 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/rarchives/ripme/App.java b/src/main/java/com/rarchives/ripme/App.java index 5d3ec550..51a16cef 100644 --- a/src/main/java/com/rarchives/ripme/App.java +++ b/src/main/java/com/rarchives/ripme/App.java @@ -224,12 +224,12 @@ public class App { } private static void loadHistory() { - File historyFile = new File("history.json"); + File historyFile = new File(Utils.getConfigDir() + File.separator + "history.json"); HISTORY.clear(); if (historyFile.exists()) { try { - logger.info("Loading history from history.json"); - HISTORY.fromFile("history.json"); + logger.info("Loading history from " + historyFile.getCanonicalPath()); + HISTORY.fromFile(historyFile.getCanonicalPath()); } catch (IOException e) { logger.error("Failed to load history from file " + historyFile, e); System.out.println( diff --git a/src/main/java/com/rarchives/ripme/ui/MainWindow.java b/src/main/java/com/rarchives/ripme/ui/MainWindow.java index ee9d8a9c..3b553deb 100644 --- a/src/main/java/com/rarchives/ripme/ui/MainWindow.java +++ b/src/main/java/com/rarchives/ripme/ui/MainWindow.java @@ -31,6 +31,9 @@ import java.io.IOException; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Collections; import java.util.Date; import java.util.Enumeration; @@ -1001,12 +1004,12 @@ public final class MainWindow implements Runnable, RipStatusHandler { } private void loadHistory() { - File historyFile = new File("history.json"); + File historyFile = new File(Utils.getConfigDir() + File.separator + "history.json"); HISTORY.clear(); if (historyFile.exists()) { try { - logger.info("Loading history from history.json"); - HISTORY.fromFile("history.json"); + logger.info("Loading history from " + historyFile.getCanonicalPath()); + HISTORY.fromFile(historyFile.getCanonicalPath()); } catch (IOException e) { logger.error("Failed to load history from file " + historyFile, e); JOptionPane.showMessageDialog(null, @@ -1044,11 +1047,17 @@ public final class MainWindow implements Runnable, RipStatusHandler { } private void saveHistory() { + Path historyFile = Paths.get(Utils.getConfigDir() + File.separator + "history.json"); try { - HISTORY.toFile("history.json"); + if (!Files.exists(historyFile)) { + Files.createDirectories(historyFile.getParent()); + Files.createFile(historyFile); + } + + HISTORY.toFile(historyFile.toString()); Utils.setConfigList("download.history", Collections.emptyList()); } catch (IOException e) { - logger.error("Failed to save history to file history.json", e); + logger.error("Failed to save history to file " + historyFile, e); } } diff --git a/src/main/java/com/rarchives/ripme/utils/Utils.java b/src/main/java/com/rarchives/ripme/utils/Utils.java index 451fbf1a..bc574cd3 100644 --- a/src/main/java/com/rarchives/ripme/utils/Utils.java +++ b/src/main/java/com/rarchives/ripme/utils/Utils.java @@ -36,12 +36,13 @@ import com.rarchives.ripme.ripper.AbstractRipper; public class Utils { public static final String RIP_DIRECTORY = "rips"; private static final String configFile = "rip.properties"; + private static final String OS = System.getProperty("os.name").toLowerCase(); private static final Logger logger = Logger.getLogger(Utils.class); private static PropertiesConfiguration config; static { try { - String configPath = getConfigPath(); + String configPath = getConfigFilePath(); File f = new File(configPath); if (!f.exists()) { // Use default bundled with .jar @@ -132,20 +133,54 @@ public class Utils { public static void saveConfig() { try { - config.save(getConfigPath()); - logger.info("Saved configuration to " + getConfigPath()); + config.save(getConfigFilePath()); + logger.info("Saved configuration to " + getConfigFilePath()); } catch (ConfigurationException e) { logger.error("Error while saving configuration: ", e); } } - private static String getConfigPath() { + + private static boolean isWindows() { + return OS.contains("win"); + } + + private static boolean isMacOS() { + return OS.contains("mac"); + } + + private static boolean isUnix() { + return OS.contains("nix") || OS.contains("nux") || OS.contains("bsd"); + } + + private static String getWindowsConfigDir() { + return System.getenv("LOCALAPPDATA") + File.separator + "ripme"; + } + + private static String getUnixConfigDir() { + return System.getProperty("user.home") + File.separator + ".config" + File.separator + "ripme"; + } + + private static String getMacOSConfigDir() { + return System.getProperty("user.home") + + File.separator + "Library" + File.separator + "Application Support" + File.separator + "ripme"; + } + + public static String getConfigDir() { + if (isWindows()) return getWindowsConfigDir(); + if (isMacOS()) return getMacOSConfigDir(); + if (isUnix()) return getUnixConfigDir(); + try { - return new File(".").getCanonicalPath() + File.separator + configFile; + return new File(".").getCanonicalPath(); } catch (Exception e) { - return "." + File.separator + configFile; + return "."; } } + private static String getConfigFilePath() { + return getConfigDir() + File.separator + configFile; + } + /** * Removes the current working directory (CWD) from a File. * @param saveAs