Default config paths added

This commit is contained in:
qjex 2017-10-15 15:21:58 +03:00
parent da69d690af
commit dd81f7acf6
3 changed files with 58 additions and 14 deletions

View File

@ -224,12 +224,12 @@ public class App {
} }
private static void loadHistory() { private static void loadHistory() {
File historyFile = new File("history.json"); File historyFile = new File(Utils.getConfigDir() + File.separator + "history.json");
HISTORY.clear(); HISTORY.clear();
if (historyFile.exists()) { if (historyFile.exists()) {
try { try {
logger.info("Loading history from history.json"); logger.info("Loading history from " + historyFile.getCanonicalPath());
HISTORY.fromFile("history.json"); HISTORY.fromFile(historyFile.getCanonicalPath());
} catch (IOException e) { } catch (IOException e) {
logger.error("Failed to load history from file " + historyFile, e); logger.error("Failed to load history from file " + historyFile, e);
System.out.println( System.out.println(

View File

@ -31,6 +31,9 @@ import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URL; 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.Collections;
import java.util.Date; import java.util.Date;
import java.util.Enumeration; import java.util.Enumeration;
@ -1001,12 +1004,12 @@ public final class MainWindow implements Runnable, RipStatusHandler {
} }
private void loadHistory() { private void loadHistory() {
File historyFile = new File("history.json"); File historyFile = new File(Utils.getConfigDir() + File.separator + "history.json");
HISTORY.clear(); HISTORY.clear();
if (historyFile.exists()) { if (historyFile.exists()) {
try { try {
logger.info("Loading history from history.json"); logger.info("Loading history from " + historyFile.getCanonicalPath());
HISTORY.fromFile("history.json"); HISTORY.fromFile(historyFile.getCanonicalPath());
} catch (IOException e) { } catch (IOException e) {
logger.error("Failed to load history from file " + historyFile, e); logger.error("Failed to load history from file " + historyFile, e);
JOptionPane.showMessageDialog(null, JOptionPane.showMessageDialog(null,
@ -1044,11 +1047,17 @@ public final class MainWindow implements Runnable, RipStatusHandler {
} }
private void saveHistory() { private void saveHistory() {
Path historyFile = Paths.get(Utils.getConfigDir() + File.separator + "history.json");
try { 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()); Utils.setConfigList("download.history", Collections.emptyList());
} catch (IOException e) { } catch (IOException e) {
logger.error("Failed to save history to file history.json", e); logger.error("Failed to save history to file " + historyFile, e);
} }
} }

View File

@ -36,12 +36,13 @@ import com.rarchives.ripme.ripper.AbstractRipper;
public class Utils { public class Utils {
public static final String RIP_DIRECTORY = "rips"; public static final String RIP_DIRECTORY = "rips";
private static final String configFile = "rip.properties"; 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 final Logger logger = Logger.getLogger(Utils.class);
private static PropertiesConfiguration config; private static PropertiesConfiguration config;
static { static {
try { try {
String configPath = getConfigPath(); String configPath = getConfigFilePath();
File f = new File(configPath); File f = new File(configPath);
if (!f.exists()) { if (!f.exists()) {
// Use default bundled with .jar // Use default bundled with .jar
@ -132,18 +133,52 @@ public class Utils {
public static void saveConfig() { public static void saveConfig() {
try { try {
config.save(getConfigPath()); config.save(getConfigFilePath());
logger.info("Saved configuration to " + getConfigPath()); logger.info("Saved configuration to " + getConfigFilePath());
} catch (ConfigurationException e) { } catch (ConfigurationException e) {
logger.error("Error while saving configuration: ", e); logger.error("Error while saving configuration: ", e);
} }
} }
private static String getConfigPath() {
try { private static boolean isWindows() {
return new File(".").getCanonicalPath() + File.separator + configFile; return OS.contains("win");
} catch (Exception e) {
return "." + File.separator + configFile;
} }
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();
} catch (Exception e) {
return ".";
}
}
private static String getConfigFilePath() {
return getConfigDir() + File.separator + configFile;
} }
/** /**