diff options
author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2023-09-16 12:58:48 -0500 |
---|---|---|
committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2023-09-16 12:58:48 -0500 |
commit | c8ac31e271afe78f6127de737c616c6f6fab05cf (patch) | |
tree | f1552bc4682ccfb7718263b726716ce85ebd1a69 /src/main/java/sevenUnitsGUI | |
parent | 85d07c240348e3a2ff85cedfc7aaef8126222a69 (diff) |
Move config file to standard location
The user's config directory is taken as ~\AppData\Local\ on Windows and
is XDG-compliant elsewhere. The config file's location is <config
dir>/SevenUnits/config.txt. The SevenUnits directory is created if
nonexistent.
The previous location inside the git directory would not work for
someone actually installing and using 7Units, so this change was made.
Diffstat (limited to 'src/main/java/sevenUnitsGUI')
-rw-r--r-- | src/main/java/sevenUnitsGUI/Presenter.java | 48 |
1 files changed, 42 insertions, 6 deletions
diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java index abdd1f6..3ecfba6 100644 --- a/src/main/java/sevenUnitsGUI/Presenter.java +++ b/src/main/java/sevenUnitsGUI/Presenter.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -57,9 +58,8 @@ import sevenUnits.utils.UncertainDouble; * @since 2021-12-15 */ public final class Presenter { - /** The default place where settings are stored. */ - private static final Path DEFAULT_SETTINGS_FILEPATH = Path - .of("settings.txt"); + /** The place where settings are stored. */ + private static final Optional<Path> CONFIG_FILE = getConfigPath(); /** The default place where units are stored. */ private static final String DEFAULT_UNITS_FILEPATH = "/unitsfile.txt"; /** The default place where dimensions are stored. */ @@ -67,6 +67,38 @@ public final class Presenter { /** The default place where exceptions are stored. */ private static final String DEFAULT_EXCEPTIONS_FILEPATH = "/metric_exceptions.txt"; + /** Gets the configuration file, creating the directory if needed. */ + private static final Optional<Path> getConfigPath() { + final Path configDir; + if (System.getProperty("os.name").startsWith("Windows")) { + final String envFolder = System.getenv("LOCALAPPDATA"); + if (envFolder == null || "".equals(envFolder)) { + configDir = Paths.get(System.getenv("USERPROFILE"), "AppData", "Local", "SevenUnits"); + } else { + configDir = Paths.get(envFolder, "SevenUnits"); + } + } else { + final String envFolder = System.getenv("XDG_CONFIG_HOME"); + if (envFolder == null || "".equals(envFolder)) { + configDir = Paths.get(System.getenv("HOME"), ".config", "SevenUnits"); + } else { + configDir = Paths.get(envFolder, "SevenUnits"); + } + } + + // try to create config directory + if (!Files.exists(configDir)) { + try { + Files.createDirectory(configDir); + } catch (IOException e) { + e.printStackTrace(); + return Optional.empty(); + } + } + + return Optional.of(configDir.resolve("config.txt")); + } + /** * Adds default units and dimensions to a database. * @@ -267,7 +299,9 @@ public final class Presenter { } // set default settings temporarily - this.loadSettings(DEFAULT_SETTINGS_FILEPATH); + CONFIG_FILE.ifPresentOrElse(this::loadSettings, () -> { + this.view.showErrorMessage("Config Loading Error", "Could not load config file - using default settings."); + }); // a Predicate that returns true iff the argument is a full base unit final Predicate<Unit> isFullBase = unit -> unit instanceof LinearUnit @@ -661,10 +695,12 @@ public final class Presenter { /** * Saves the presenter's current settings to its default filepath. * + * @return true iff this operation succeeded * @since 2022-04-19 */ - public void saveSettings() { - this.saveSettings(DEFAULT_SETTINGS_FILEPATH); + public boolean saveSettings() { + CONFIG_FILE.ifPresent(this::saveSettings); + return CONFIG_FILE.isPresent(); } /** |