diff options
author | Jonathan Bauer | 2014-09-18 13:44:08 +0200 |
---|---|---|
committer | Jonathan Bauer | 2014-09-18 13:44:08 +0200 |
commit | b819dffd8a2e46c900f50c9f873c0603a83e8c79 (patch) | |
tree | 59cfcc46dcd54935a637fa77ebf0d5a7f5acab81 /dozentenmodul/src/main/java/config/Config.java | |
parent | Revert "[client] center GUIs in the primary display only" (diff) | |
download | tutor-module-b819dffd8a2e46c900f50c9f873c0603a83e8c79.tar.gz tutor-module-b819dffd8a2e46c900f50c9f873c0603a83e8c79.tar.xz tutor-module-b819dffd8a2e46c900f50c9f873c0603a83e8c79.zip |
[client] moved main function to App.java & refined Config stuff
Diffstat (limited to 'dozentenmodul/src/main/java/config/Config.java')
-rw-r--r-- | dozentenmodul/src/main/java/config/Config.java | 219 |
1 files changed, 178 insertions, 41 deletions
diff --git a/dozentenmodul/src/main/java/config/Config.java b/dozentenmodul/src/main/java/config/Config.java index 13bdf830..95547b5f 100644 --- a/dozentenmodul/src/main/java/config/Config.java +++ b/dozentenmodul/src/main/java/config/Config.java @@ -5,19 +5,39 @@ import java.io.IOException; import org.ini4j.Wini; +/** + * Represents the configuration of the client + * + * @author Jonathan Bauer + */ + public class Config { - - // Nur die Wini als member + + /** + * The main configuration object is of type Wini + * It contains the content of the config.ini as + * determined in the init() function. + */ private static Wini ini = null; - - // Konstruktor ermittelt der Pfad zur Konfigurationsdatei. - // Unterscheidet dabei Windows/Linux + + /** + * Initializes the class by determining the path + * to the config.ini on the system and setting the + * private creating the ini member from that file. + * + * This function will make a distinction between + * Linux and Windows OS's, as the standard paths + * for configuration files obviously differ. + * + * @throws IOException + */ public static void init() throws IOException { - - // Hauptvariablen nur lokal notwendig + + // Variables only needed locally String configPath = null; File configFile = null; - // Unterscheide zwischen Windows/Unix + + // Determine OS String OSName = System.getProperty("os.name").toLowerCase(); System.out.println("Machine's OS: " + OSName); if (OSName.contains("windows")) { @@ -36,36 +56,36 @@ public class Config { } else if (OSName.contains("linux")) { configPath=System.getProperty("user.home") + "/.config/bwSuite/config.ini"; } else { - // Nicht Windows oder Linux, TODO MacOS Support? + // Not Windows nor Linux, TODO MacOS Support? configPath = null; } - //File fuer den Ordner erzeugen + // Check if we got a path if (!(configPath.isEmpty()||configPath == null)) { configFile = new File(configPath); } else { throw new IOException("Konnte kein Pfad für die Konfigurationsdatei ermitteln."); } - // Checke, ob der Ordner existiert. + // Check if the directory exists. if (!configFile.getParentFile().exists()) { System.out.println("Ordner " + configFile.getParentFile() + " exisitiert nicht - lege ihn jetzt an."); - // Wenn nicht, erzeuge Ordner + // Does not, create it if (!configFile.getParentFile().mkdirs()) { throw new IOException("Konnte '" + configFile.getParentFile() + "' nicht erstellen."); } } - // Pruefen, ob Datei schon existiert + // Check if the file already exists if (!configFile.exists()) { - // Erzeuge eine neue Datei + // Does not, create it configFile.createNewFile(); - // Wenn in die Datei geschrieben werden kann + // Check if file is writeable if(configFile.canWrite()) { ini = new Wini(configFile); System.out.println("Erzeuge '" + configFile + "'..."); - //Schreibe Config + // write default configuration options and values ini.put("main", "BillOfRights", false); ini.put("main", "vmware", false); ini.put("main", "Benutzername speichern", false); @@ -81,16 +101,120 @@ public class Config { System.out.println("'" + configFile + "' existiert bereits - keine weitere Aktion."); ini = new Wini(configFile); } + } // end constructor. + + /** + * Query the value of 'BillOfRights' from the configuration file. + * @return true if the user already accepted bill of rights, false otherwise. + */ + public static boolean getBillOfRights() { + return getBoolean("main", "BillOfRights", false); + } + /** + * Query the value of 'vmware' from the configuration file. + * @return true if the user already accepted vmware license, false otherwise. + */ + public static boolean getVmwareLicense() { + return getBoolean("main", "vmware", false); + } + /** + * Query the value of 'Benutzername speichern' from the configuration file. + * @return true if the username should be saved, false otherwise. + */ + public static boolean getSaveUsername() { + return getBoolean("main", "Benutzername speichern", false); + } + /** + * Query the value of 'Benutzername' from the configuration file. + * @return username if saved, an empty string otherwise. + */ + public static String getUsername() { + return getString("main", "Benutzername", ""); + } + /** + * Query the value of 'Letzter Downloadpfad' from the configuration file. + * @return last download path if saved, the path to the user's home otherwise. + */ + public static String getLastDownloadPath() { + return getString("main", "Letzter Downloadpfad", System.getProperty("user.home")); + } + /** + * Query the value of 'Letzter Uploadpfad' from the configuration file. + * @return last upload path if saved, the path to the user's home otherwise. + */ + public static String getLastUploadPath() { + return getString("main", "Letzter Uploadpfad", System.getProperty("user.home")); + } + + /** + * Sets the value of 'BillOfRights' in the configuration file to 'value' + * @return true if it succeeded, false otherwise + */ + public static boolean setBillOfRights(boolean value) { + return setBoolean("main", "BillOfRights", value); + } + /** + * Sets the value of 'vmware' in the configuration file to 'value' + * @return true if it succeeded, false otherwise + */ + public static boolean setVmwareLicense(boolean value) { + return setBoolean("main", "vmware", value); + } + /** + * Sets the value of 'Benutzername speichern' in the configuration file to 'value' + * @return true if it succeeded, false otherwise + */ + public static boolean setSaveUsername(boolean value) { + return setBoolean("main", "Benutzername speichern", value); + } + /** + * Sets the value of 'Benutzername' in the configuration file to 'value' + * @return true if it succeeded, false otherwise + */ + public static boolean setUsername(String value) { + return setString("main", "Benutzername", value); + } + /** + * Sets the value of 'Letzter Downloadpfad' in the configuration file to 'value' + * @return true if it succeeded, false otherwise + */ + public static boolean setLastDownloadPath(String value) { + return setString("main", "Letzter Downloadpfad", value); + } + /** + * Sets the value of "Letzter Uploadpfad" in the configuration file to 'value' + * @return true if it succeeded, false otherwise + */ + public static boolean setLastUploadPath(String value) { + return setString("main", "Letzter Uploadpfad", value); + } + + /** + * Save the changes to the ini file to actual file on the disk. + * + * @return true if succeeded, false otherwise + */ + public static boolean store() { + try { + ini.store(); + return true; + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } } - // Ende Konstruktor. - // Public funktion zur Abfrage der Boolean-Werte - // Gibt Wert von Attribut 'key' in Sektion 'section', falls es existiert. - // Sonst gibt es 'defaultValue' zurück. - // - // Usage: getBoolean(<section>, <key>, <defaultValue>) - // Ex.: getBoolean("main", "Benutzername", "NichtGesetzt"); - public static boolean getBoolean(String section, String key, Boolean defaultValue) { + /** + * Gets the boolean from the given 'key' in the given 'section'. + * If nothing is found, return the given 'defaultValue' + * + * @param section section to search the key in + * @param key key to query in that section + * @param defaultValue default value to be returned, if none is found. + * @return + */ + private static boolean getBoolean(String section, String key, Boolean defaultValue) { if (ini.containsKey(section) && ini.get(section).containsKey(key)) { return ini.get(section, key, Boolean.class); } else { @@ -98,32 +222,45 @@ public class Config { } } - // Public funktion zur Abfrage der String-Werte - // Usage: getBoolean(<key>) - public static String getString(String section, String key, String defaultValue) { + /** + * Gets the string from the given 'key' in the given 'section' + * If nothing is found, return the given 'defaultValue' + * + * @param section section of the configuration file to search for + * @param key key to lookup in the section + * @param defaultValue default value to return if none is found in the file + * @return value of 'key' in 'section' if it exists, 'defaultValue' otherwise + */ + private static String getString(String section, String key, String defaultValue) { if (ini.containsKey(section) && ini.get(section).containsKey(key)) { return ini.get(section, key); } else { return defaultValue; } } - - public static boolean setBoolean(String section, String key, Boolean value) { + /** + * Sets the given 'key' in the given 'section' to 'value'. + * Restricted to boolean. + * + * @param section section of the configuration file + * @param key key to set + * @param value value to assign to key + * @return true if it succeeded, false otherwise + */ + private static boolean setBoolean(String section, String key, boolean value) { return ini.put(section, key, value) != null; } - public static boolean setString(String section, String key, String value) { + /** + * Sets the given 'key' in the given 'section' to 'value'. + * Restricted to string. + * + * @param section section of the configuration file + * @param key key to set + * @param value value to assign to key + * @return true if it succeeded, false otherwise + */ + private static boolean setString(String section, String key, String value) { return ini.put(section, key, value) != null; } - - public static boolean store() { - try { - ini.store(); - return true; - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } - } } |