diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | .settings/org.eclipse.core.resources.prefs | 2 | ||||
-rw-r--r-- | docs/roadmap.org | 2 | ||||
-rw-r--r-- | src/main/java/sevenUnitsGUI/Presenter.java | 48 |
4 files changed, 43 insertions, 10 deletions
@@ -7,6 +7,5 @@ build # 7Units gitignore files *.class *~ -settings.txt /src/test/resources/test-settings.txt /bin/ diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 9bca13f..0000000 --- a/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -encoding/settings.txt=UTF-8 diff --git a/docs/roadmap.org b/docs/roadmap.org index 2ff5ab5..a68dae5 100644 --- a/docs/roadmap.org +++ b/docs/roadmap.org @@ -17,7 +17,7 @@ Feature Requirements: Data/Configuration Requirements: - All of the data formats (e.g. unit files) should be standardized. -- 7Units should put its configuration files in a standard place for the operating system ([[https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html][XDG Base Directory Specification]] compliant on Unix). The settings file should be able to specify the location of the data files. +- The config file should be able to specify the location of the data files. Documentation/Testing Requirements: - 7Units should be fully documented. 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(); } /** |