summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/sevenUnitsGUI/Presenter.java62
1 files changed, 30 insertions, 32 deletions
diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java
index e471b01..0b5247d 100644
--- a/src/main/java/sevenUnitsGUI/Presenter.java
+++ b/src/main/java/sevenUnitsGUI/Presenter.java
@@ -57,8 +57,11 @@ import sevenUnits.utils.UncertainDouble;
* @since 2021-12-15
*/
public final class Presenter {
- /** The place where settings are stored. */
- private static final Optional<Path> CONFIG_FILE = getConfigPath();
+ /**
+ * The place where settings are stored.
+ * Both this path and its parent directory may not exist.
+ */
+ private static final Path CONFIG_FILE = userConfigDir().resolve("SevenUnits").resolve("config.txt");
/** The default place where units are stored. */
private static final String DEFAULT_UNITS_FILEPATH = "/unitsfile.txt";
/** The default place where dimensions are stored. */
@@ -66,43 +69,27 @@ 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;
+ private static final Path userConfigDir() {
if (System.getProperty("os.name").startsWith("Windows")) {
final String envFolder = System.getenv("LOCALAPPDATA");
if (envFolder == null || "".equals(envFolder)) {
- configDir = Path.of(System.getenv("USERPROFILE"), "AppData", "Local", "SevenUnits");
+ return Path.of(System.getenv("USERPROFILE"), "AppData", "Local");
} else {
- configDir = Path.of(envFolder, "SevenUnits");
+ return Path.of(envFolder);
}
} else {
final String envFolder = System.getenv("XDG_CONFIG_HOME");
if (envFolder == null || "".equals(envFolder)) {
- configDir = Path.of(System.getenv("HOME"), ".config", "SevenUnits");
+ return Path.of(System.getenv("HOME"), ".config");
} else {
- configDir = Path.of(envFolder, "SevenUnits");
- }
- }
-
- // try to create config directory
- if (!Files.exists(configDir)) {
- try {
- Files.createDirectory(configDir);
- } catch (IOException e) {
- e.printStackTrace();
- return Optional.empty();
+ return Path.of(envFolder);
}
}
-
- return Optional.of(configDir.resolve("config.txt"));
}
/** Gets a Path from a pathname in the config file. */
private static Path pathFromConfig(String pathname) {
- return CONFIG_FILE.map(configFile ->
- configFile.getParent().resolve(pathname)
- ).orElse(Path.of(pathname));
+ return CONFIG_FILE.getParent().resolve(pathname);
}
/**
@@ -305,9 +292,9 @@ public final class Presenter {
}
// set default settings temporarily
- CONFIG_FILE.ifPresentOrElse(this::loadSettings, () -> {
- this.view.showErrorMessage("Config Loading Error", "Could not load config file - using default settings.");
- });
+ if (Files.exists(CONFIG_FILE)) {
+ this.loadSettings(CONFIG_FILE);
+ }
// a Predicate that returns true iff the argument is a full base unit
final Predicate<Unit> isFullBase = unit -> unit instanceof LinearUnit
@@ -710,14 +697,23 @@ public final class Presenter {
}
/**
- * Saves the presenter's current settings to its default filepath.
+ * Saves the presenter's current settings to the config file,
+ * creating it if it doesn't exist.
*
- * @return true iff this operation succeeded
+ * @return false iff the presenter could not write to the file
* @since 2022-04-19
*/
public boolean saveSettings() {
- CONFIG_FILE.ifPresent(this::saveSettings);
- return CONFIG_FILE.isPresent();
+ final Path configDir = CONFIG_FILE.getParent();
+ if (!Files.exists(configDir)) {
+ try {
+ Files.createDirectories(configDir);
+ } catch (IOException e) {
+ return false;
+ }
+ }
+
+ return this.writeSettings(CONFIG_FILE);
}
/**
@@ -726,7 +722,7 @@ public final class Presenter {
* @param settingsFile file settings should be saved to
* @since 2021-12-15
*/
- void saveSettings(Path settingsFile) {
+ boolean writeSettings(Path settingsFile) {
try (BufferedWriter writer = Files.newBufferedWriter(settingsFile)) {
writer.write(String.format("number_display_rule=%s\n",
this.numberDisplayRule));
@@ -738,11 +734,13 @@ public final class Presenter {
String.format("include_duplicates=%s\n", this.showDuplicates));
writer.write(
String.format("search_prefix_rule=%s\n", this.searchRule));
+ return true;
} catch (final IOException e) {
e.printStackTrace();
this.view.showErrorMessage("I/O Error",
"Error occurred while saving settings: "
+ e.getLocalizedMessage());
+ return false;
}
}