package envoy.client.data; import java.io.*; import java.util.*; import java.util.logging.Level; import java.util.prefs.Preferences; import dev.kske.eventbus.core.*; import envoy.util.*; import envoy.client.event.EnvoyCloseEvent; /** * Manages all application settings, which are different objects that can be changed during runtime * and serialized them by using either the file system or the {@link Preferences} API. * * @author Leon Hofmeister * @author Maximilian Käfer * @author Kai S. K. Engelbart * @since Envoy Client v0.2-alpha */ public final class Settings { // Actual settings accessible by the rest of the application private Map> items; /** * Settings are stored in this file. */ private static final File settingsFile = new File(ClientConfig.getInstance().getHomeDirectory(), "settings.ser"); /** * Singleton instance of this class. */ private static Settings settings = new Settings(); /** * The way to instantiate the settings. Is set to private to deny other instances of that * object. * * @since Envoy Client v0.2-alpha */ private Settings() { EventBus.getInstance().registerListener(this); // Load settings from settings file try { items = SerializationUtils.read(settingsFile, HashMap.class); } catch (ClassNotFoundException | IOException e) { items = new HashMap<>(); } supplementDefaults(); } /** * This method is used to ensure that there is only one instance of Settings. * * @return the instance of Settings * @since Envoy Client v0.2-alpha */ public static Settings getInstance() { return settings; } /** * Updates the preferences when the save button is clicked. * * @throws IOException if an error occurs while saving the themes * @since Envoy Client v0.2-alpha */ @Event(EnvoyCloseEvent.class) private void save() { EnvoyLog.getLogger(Settings.class).log(Level.INFO, "Saving settings..."); // Save settings to settings file try { SerializationUtils.write(settingsFile, items); } catch (final IOException e) { EnvoyLog.getLogger(Settings.class).log(Level.SEVERE, "Unable to save settings file: ", e); } } private void supplementDefaults() { items.putIfAbsent("enterToSend", new SettingsItem<>(true, "Enter to send", "Sends a message by pressing the enter key.")); items.putIfAbsent("hideOnClose", new SettingsItem<>(false, "Hide on close", "Hides the chat window when it is closed.")); items.putIfAbsent("currentTheme", new SettingsItem<>("dark", "Current Theme Name", "The name of the currently selected theme.")); 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?")); items.putIfAbsent("askForConfirmation", new SettingsItem<>(true, "Ask for confirmation", "Will ask for confirmation before doing certain things")); } /** * @return the name of the currently active theme * @since Envoy Client v0.2-alpha */ public String getCurrentTheme() { return (String) items.get("currentTheme").get(); } /** * Sets the name of the current theme. * * @param themeName the name to set * @since Envoy Client v0.2-alpha */ public void setCurrentTheme(String themeName) { ((SettingsItem) items.get("currentTheme")).set(themeName); } /** * @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"); } /** * @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. * @since Envoy Client v0.2-alpha */ public Boolean isEnterToSend() { return (Boolean) items.get("enterToSend").get(); } /** * 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. * @since Envoy Client v0.2-alpha */ public void setEnterToSend(boolean enterToSend) { ((SettingsItem) items.get("enterToSend")).set(enterToSend); } /** * @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 */ public void setDownloadSavedWithoutAsking(boolean autosaveDownload) { ((SettingsItem) items.get("autoSaveDownloads")).set(autosaveDownload); } /** * @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) items.get("downloadLocation")).set(downloadLocation); } /** * @return the current on close mode. * @since Envoy Client v0.3-alpha */ public Boolean isHideOnClose() { return (Boolean) items.get("hideOnClose").get(); } /** * Sets the current on close mode. * * @param hideOnClose whether the application should be minimized on close * @since Envoy Client v0.3-alpha */ public void setHideOnClose(boolean hideOnClose) { ((SettingsItem) items.get("hideOnClose")).set(hideOnClose); } /** * @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) items.get("askForConfirmation")).set(askForConfirmation); } /** * @return the items */ public Map> getItems() { return items; } /** * @param items the items to set */ public void setItems(Map> items) { this.items = items; } }