2020-02-12 07:12:22 +01:00
|
|
|
package envoy.client.data;
|
2019-11-22 06:37:59 +01:00
|
|
|
|
2020-09-21 20:52:13 +02:00
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
2020-09-22 14:42:51 +02:00
|
|
|
import java.util.logging.Level;
|
2019-11-22 06:37:59 +01:00
|
|
|
import java.util.prefs.Preferences;
|
|
|
|
|
2020-09-22 14:42:51 +02:00
|
|
|
import envoy.client.event.EnvoyCloseEvent;
|
|
|
|
import envoy.util.*;
|
|
|
|
|
|
|
|
import dev.kske.eventbus.*;
|
|
|
|
import dev.kske.eventbus.EventListener;
|
2019-11-23 13:25:12 +01:00
|
|
|
|
2019-11-22 06:37:59 +01:00
|
|
|
/**
|
2019-12-20 20:25:54 +01:00
|
|
|
* Manages all application settings, which are different objects that can be
|
|
|
|
* changed during runtime and serialized them by using either the file system or
|
2020-06-26 23:36:14 +02:00
|
|
|
* the {@link Preferences} API.
|
2019-12-20 20:25:54 +01:00
|
|
|
*
|
2019-11-22 06:37:59 +01:00
|
|
|
* @author Leon Hofmeister
|
2019-12-07 10:58:03 +01:00
|
|
|
* @author Maximilian Käfer
|
2019-12-07 11:48:41 +01:00
|
|
|
* @author Kai S. K. Engelbart
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2019-11-22 06:37:59 +01:00
|
|
|
*/
|
2020-09-22 14:42:51 +02:00
|
|
|
public final class Settings implements EventListener {
|
2019-11-22 06:37:59 +01:00
|
|
|
|
2019-12-07 11:48:41 +01:00
|
|
|
// Actual settings accessible by the rest of the application
|
2020-07-07 17:02:39 +02:00
|
|
|
private Map<String, SettingsItem<?>> items;
|
2019-12-07 10:58:03 +01:00
|
|
|
|
2019-12-07 11:48:41 +01:00
|
|
|
/**
|
2019-12-23 14:25:46 +01:00
|
|
|
* Settings are stored in this file.
|
2019-12-07 11:48:41 +01:00
|
|
|
*/
|
2020-03-05 14:27:40 +01:00
|
|
|
private static final File settingsFile = new File(ClientConfig.getInstance().getHomeDirectory(), "settings.ser");
|
2019-12-07 11:48:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Singleton instance of this class.
|
|
|
|
*/
|
|
|
|
private static Settings settings = new Settings();
|
2019-11-22 06:37:59 +01:00
|
|
|
|
|
|
|
/**
|
2020-05-09 10:57:30 +02:00
|
|
|
* The way to instantiate the settings. Is set to private to deny other
|
|
|
|
* instances of that object.
|
2019-12-20 20:25:54 +01:00
|
|
|
*
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2019-11-22 06:37:59 +01:00
|
|
|
*/
|
2019-12-23 14:25:46 +01:00
|
|
|
private Settings() {
|
2020-09-22 14:42:51 +02:00
|
|
|
EventBus.getInstance().registerListener(this);
|
|
|
|
|
2019-12-23 14:25:46 +01:00
|
|
|
// Load settings from settings file
|
|
|
|
try {
|
|
|
|
items = SerializationUtils.read(settingsFile, HashMap.class);
|
2019-12-29 11:54:05 +01:00
|
|
|
} catch (ClassNotFoundException | IOException e) {
|
2019-12-23 14:25:46 +01:00
|
|
|
items = new HashMap<>();
|
|
|
|
}
|
|
|
|
supplementDefaults();
|
2019-11-22 06:37:59 +01:00
|
|
|
}
|
|
|
|
|
2019-12-23 14:25:46 +01:00
|
|
|
/**
|
|
|
|
* This method is used to ensure that there is only one instance of Settings.
|
|
|
|
*
|
|
|
|
* @return the instance of Settings
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2019-12-23 14:25:46 +01:00
|
|
|
*/
|
2020-06-06 15:03:43 +02:00
|
|
|
public static Settings getInstance() { return settings; }
|
2019-12-23 14:25:46 +01:00
|
|
|
|
2019-12-07 17:58:59 +01:00
|
|
|
/**
|
2019-12-11 18:52:30 +01:00
|
|
|
* Updates the preferences when the save button is clicked.
|
2019-12-20 20:25:54 +01:00
|
|
|
*
|
2020-04-18 21:37:44 +02:00
|
|
|
* @throws IOException if an error occurs while saving the themes
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2019-12-07 17:58:59 +01:00
|
|
|
*/
|
2020-10-17 16:40:13 +02:00
|
|
|
@Event(eventType = EnvoyCloseEvent.class)
|
2020-09-22 14:42:51 +02:00
|
|
|
private void save() {
|
|
|
|
EnvoyLog.getLogger(Settings.class).log(Level.INFO, "Saving settings...");
|
2020-04-18 21:37:44 +02:00
|
|
|
|
2019-12-23 14:25:46 +01:00
|
|
|
// Save settings to settings file
|
2020-09-22 14:42:51 +02:00
|
|
|
try {
|
|
|
|
SerializationUtils.write(settingsFile, items);
|
|
|
|
} catch (final IOException e) {
|
|
|
|
EnvoyLog.getLogger(Settings.class).log(Level.SEVERE, "Unable to save settings file: ", e);
|
|
|
|
}
|
2019-12-23 14:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void supplementDefaults() {
|
2020-06-06 15:03:43 +02:00
|
|
|
items.putIfAbsent("enterToSend", new SettingsItem<>(true, "Enter to send", "Sends a message by pressing the enter key."));
|
2020-09-21 20:52:13 +02:00
|
|
|
items.putIfAbsent("hideOnClose", new SettingsItem<>(false, "Hide on close", "Hides the chat window when it is closed."));
|
2020-06-06 15:03:43 +02:00
|
|
|
items.putIfAbsent("currentTheme", new SettingsItem<>("dark", "Current Theme Name", "The name of the currently selected theme."));
|
2020-07-27 22:52:43 +02:00
|
|
|
items.putIfAbsent("downloadLocation",
|
|
|
|
new SettingsItem<>(new File(System.getProperty("user.home") + "/Downloads/"), "Download location",
|
|
|
|
"The location where files will be saved to"));
|
|
|
|
items.putIfAbsent("autoSaveDownloads", new SettingsItem<>(false, "Save without asking?", "Should downloads be saved without asking?"));
|
2020-09-23 17:03:32 +02:00
|
|
|
items.putIfAbsent("askForConfirmation",
|
|
|
|
new SettingsItem<>(true, "Ask for confirmation", "Will ask for confirmation before doing certain things"));
|
2019-12-07 10:58:03 +01:00
|
|
|
}
|
|
|
|
|
2019-12-07 17:58:59 +01:00
|
|
|
/**
|
2020-06-08 09:14:57 +02:00
|
|
|
* @return the name of the currently active theme
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2019-12-07 17:58:59 +01:00
|
|
|
*/
|
2020-06-08 10:02:39 +02:00
|
|
|
public String getCurrentTheme() { return (String) items.get("currentTheme").get(); }
|
2020-03-21 16:10:59 +01:00
|
|
|
|
|
|
|
/**
|
2020-06-08 09:14:57 +02:00
|
|
|
* Sets the name of the current theme.
|
2019-12-20 20:25:54 +01:00
|
|
|
*
|
|
|
|
* @param themeName the name to set
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2019-12-07 17:58:59 +01:00
|
|
|
*/
|
2020-06-06 15:03:43 +02:00
|
|
|
public void setCurrentTheme(String themeName) { ((SettingsItem<String>) items.get("currentTheme")).set(themeName); }
|
2020-05-09 10:57:30 +02:00
|
|
|
|
2020-07-07 17:02:39 +02:00
|
|
|
/**
|
|
|
|
* @return true if the currently used theme is one of the default themes
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public boolean isUsingDefaultTheme() {
|
|
|
|
final var theme = getCurrentTheme();
|
|
|
|
return theme.equals("dark") || theme.equals("light");
|
|
|
|
}
|
|
|
|
|
2019-11-22 06:37:59 +01:00
|
|
|
/**
|
2019-12-20 20:25:54 +01:00
|
|
|
* @return {@code true}, if pressing the {@code Enter} key suffices to send a
|
|
|
|
* message. Otherwise it has to be pressed in conjunction with the
|
|
|
|
* {@code Control} key.
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2019-11-22 06:37:59 +01:00
|
|
|
*/
|
2020-06-06 15:03:43 +02:00
|
|
|
public Boolean isEnterToSend() { return (Boolean) items.get("enterToSend").get(); }
|
2019-11-22 06:37:59 +01:00
|
|
|
|
|
|
|
/**
|
2019-12-20 20:25:54 +01:00
|
|
|
* Changes the keystrokes performed by the user to send a message.
|
|
|
|
*
|
|
|
|
* @param enterToSend If set to {@code true} a message can be sent by pressing
|
|
|
|
* the {@code Enter} key. Otherwise it has to be pressed in
|
|
|
|
* conjunction with the {@code Control} key.
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2019-11-22 06:37:59 +01:00
|
|
|
*/
|
2020-06-06 15:03:43 +02:00
|
|
|
public void setEnterToSend(boolean enterToSend) { ((SettingsItem<Boolean>) items.get("enterToSend")).set(enterToSend); }
|
2019-11-22 06:37:59 +01:00
|
|
|
|
2020-07-27 22:52:43 +02:00
|
|
|
/**
|
|
|
|
* @return whether Envoy will prompt a dialogue before saving an
|
|
|
|
* {@link envoy.data.Attachment}
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public Boolean isDownloadSavedWithoutAsking() { return (Boolean) items.get("autoSaveDownloads").get(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether Envoy will prompt a dialogue before saving an
|
|
|
|
* {@link envoy.data.Attachment}.
|
|
|
|
*
|
|
|
|
* @param autosaveDownload whether a download should be saved without asking
|
|
|
|
* before
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
2020-08-22 13:37:07 +02:00
|
|
|
public void setDownloadSavedWithoutAsking(boolean autosaveDownload) {
|
|
|
|
((SettingsItem<Boolean>) items.get("autoSaveDownloads")).set(autosaveDownload);
|
|
|
|
}
|
2020-07-27 22:52:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the path where downloads should be saved
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public File getDownloadLocation() { return (File) items.get("downloadLocation").get(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the path where downloads should be saved.
|
|
|
|
*
|
|
|
|
* @param downloadLocation the path to set
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public void setDownloadLocation(File downloadLocation) { ((SettingsItem<File>) items.get("downloadLocation")).set(downloadLocation); }
|
|
|
|
|
2019-12-07 17:58:59 +01:00
|
|
|
/**
|
2019-12-23 14:25:46 +01:00
|
|
|
* @return the current on close mode.
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2019-12-07 17:58:59 +01:00
|
|
|
*/
|
2020-07-24 09:57:09 +02:00
|
|
|
public Boolean isHideOnClose() { return (Boolean) items.get("hideOnClose").get(); }
|
2019-11-22 06:37:59 +01:00
|
|
|
|
2019-12-07 17:58:59 +01:00
|
|
|
/**
|
2019-12-23 14:25:46 +01:00
|
|
|
* Sets the current on close mode.
|
2019-12-20 20:25:54 +01:00
|
|
|
*
|
2020-07-24 10:25:35 +02:00
|
|
|
* @param hideOnClose whether the application should be minimized on close
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2019-12-07 17:58:59 +01:00
|
|
|
*/
|
2020-07-24 09:57:09 +02:00
|
|
|
public void setHideOnClose(boolean hideOnClose) { ((SettingsItem<Boolean>) items.get("hideOnClose")).set(hideOnClose); }
|
2019-12-22 21:48:19 +01:00
|
|
|
|
2020-09-23 17:03:32 +02:00
|
|
|
/**
|
|
|
|
* @return whether a confirmation dialog should be displayed before certain
|
|
|
|
* actions
|
|
|
|
* @since Envoy Client v0.2-alpha
|
|
|
|
*/
|
|
|
|
public Boolean isAskForConfirmation() { return (Boolean) items.get("askForConfirmation").get(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the behavior of calling certain functionality by displaying a
|
|
|
|
* confirmation dialog before executing it.
|
|
|
|
*
|
|
|
|
* @param askForConfirmation whether confirmation dialogs should be displayed
|
|
|
|
* before certain actions
|
|
|
|
* @since Envoy Client v0.2-alpha
|
|
|
|
*/
|
|
|
|
public void setAskForConfirmation(boolean askForConfirmation) {
|
|
|
|
((SettingsItem<Boolean>) items.get("askForConfirmation")).set(askForConfirmation);
|
|
|
|
}
|
|
|
|
|
2019-12-23 14:51:52 +01:00
|
|
|
/**
|
|
|
|
* @return the items
|
|
|
|
*/
|
2020-06-06 15:03:43 +02:00
|
|
|
public Map<String, SettingsItem<?>> getItems() { return items; }
|
2019-12-23 14:51:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param items the items to set
|
|
|
|
*/
|
2020-06-06 15:03:43 +02:00
|
|
|
public void setItems(Map<String, SettingsItem<?>> items) { this.items = items; }
|
2020-03-14 19:59:37 +01:00
|
|
|
}
|