package config; import java.io.File; import java.io.IOException; import org.apache.log4j.Logger; import org.ini4j.Wini; /** * Represents the configuration of the client * * @author Jonathan Bauer */ public class Config { /** * Logger for this class */ private final static Logger LOGGER = Logger.getLogger(Config.class); /** * 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; /** * 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 { // Variables only needed locally String configPath = null; File configFile = null; // Determine OS String OSName = System.getProperty("os.name").toLowerCase(); LOGGER.info("Machine's OS: " + OSName); if (OSName.contains("windows")) { // Windows machine. Use the environment variable 'APPDATA' which // should point to a path similar to: // C:\Users\\AppData\Roaming String appDataPath = System.getenv("APPDATA"); if (!appDataPath.isEmpty()) { configPath = appDataPath + "\\bwSuite\\config.ini"; } else { // APPDATA was empty, let's build it ourselves... LOGGER.warn("APPDATA ist leer."); configPath = System.getProperty("user.home") + "\\AppData\\Roaming\\bwSuite\\config.ini"; } } else if (OSName.contains("linux")) { configPath=System.getProperty("user.home") + "/.config/bwSuite/config.ini"; } else { // Not Windows nor Linux, TODO MacOS Support? configPath = null; } // Check if we got a path if (!(configPath.isEmpty()||configPath == null)) { configFile = new File(configPath); } else { throw new IOException("Konnte keinen Pfad für die Konfigurationsdatei ermitteln."); } // Check if the directory exists. if (!configFile.getParentFile().exists()) { LOGGER.info("Ordner " + configFile.getParentFile() + " exisitiert nicht - lege ihn jetzt an."); // Does not, create it if (!configFile.getParentFile().mkdirs()) { throw new IOException("Konnte '" + configFile.getParentFile() + "' nicht erstellen."); } } // Check if the file already exists if (!configFile.exists()) { // Does not, create it configFile.createNewFile(); // Check if file is writeable if(configFile.canWrite()) { ini = new Wini(configFile); LOGGER.info("Erzeuge '" + configFile + "'..."); // write default configuration options and values ini.put("main", "BillOfRights", false); ini.put("main", "vmware", false); ini.put("main", "Benutzername speichern", false); ini.put("main", "Benutzername", ""); ini.put("main", "Letzter Downloadpfad", ""); ini.put("main", "Letzter Uploadpfad", ""); ini.put("main", "IdP", ""); ini.store(); } else { throw new IOException("Konnte nicht in '" + configFile + "' schreiben. Haben Sie Rechte dazu?"); } } else { LOGGER.info("'" + 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")); } /** * Query the path of the configuration file * @return path to the configuration file */ public static String getPath() { if (ini.getFile().getParentFile().isDirectory()) return ini.getFile().getParentFile().toString(); else return null; } /** * Query the IdP of the configuration file * @return stored IdP */ public static int getIdP() { return Integer.parseInt(getString("main", "IdP", "")); } /** * 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); } /** * Sets the value of "IdP" in the configuration file to 'value' * @return true if it succeeded, false otherwise */ public static boolean setIdP(String value) { return setString("main", "IdP", 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) { e.printStackTrace(); return false; } } /** * 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 { return 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; } } /** * 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; } /** * 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; } }