summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/Config.java
blob: 80a506850799499cb9b18e21ea6c44c52403e5d6 (plain) (tree)
1
2
3
4
5
6
7
8
                           
 


                           
                               
                      
 





                                             
                     

           




                                                                            

                                                        
                                             
           
                                       


                                                        
                                                          

                                                          

                                                        
                                                    
           

                              
                                                      

                                                

                                         

                               


                                                                            



                                                                                        
                                                     
                                                                                   
                                
                                                                 
                                                                 
                                                                                                                         
                         

                                                                                                     
                        


                                                                                                              
                 
 
                                         
                                                                    

                                                          
                                                                                                  
                 
 
                                                 
                                                           
                                                                                                              
                                              
                                                                   
                                                                                                                
                         
                 
 
                                                   
                                           
                                              
                                                   
 
                                                     
                                                    
                                                           
                                                                                
                                                                                 






                                                                                   
                                            
 
                                
                                                                                                                         
                         
                        
                                                   
                                                                    
                 


                             
                                                   
           







                                                                        
 
           
                                                                         


                                                                          
           

                                                                         
         
 

                                                                   


                                                                          
           

                                                                             
         
 

                                                                         
           


                                                                
                                                         
         
 

                                                                                 


                                                                           
           

                                                                                           
         
 

                                                                               
           

                                                                                    

                                                                                         
         
 

                                                  
           

                             
                                                    
                                                                  
         
 

                                                                    
           


                                                        
                                                                
         
 
           
                                                                                
           

                                                        

                                                                         
         
 

                                                                          
           

                                                        

                                                                             
         
 

                                                                                
           


                                                         
                                                            
         
 
           


                                                                                

                                                        

                                                                 
         
 
           


                                                                              

                                                        

                                                               
         
 

                                                                       
           

                                                        

                                                                     
         
 
           


                                                                                    


                                                                     
                                                               
         
 









                                                                       


                                            
         
 


                                                                        
           


                                                                              
                  

                                                                                             





                                                                                    
 



                                                                      
                                                                         

                                                                                   

                                                                           

                                                                                          





                                                                                    
 


                                                                  

                                                           




                                                                                      

                                                            
 


                                                                  

                                                           




                                                                                    

                                                            
 
 
package org.openslx.dozmod;

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\<user>\AppData\Roaming
			String appDataPath = System.getenv("APPDATA");
			if (!appDataPath.isEmpty()) {
				configPath = appDataPath + "\\bwSuite\\config.ini";
			} else {
				// APPDATA was empty, let's guess
				LOGGER.warn("APPDATA is empty.");
				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, try fallback to relative path
			// TODO MacOS Support?
			configPath = "." + File.separatorChar + "bwSuite" + File.separatorChar + "config.ini";
		}

		// Check if we got a path
		if (!(configPath.isEmpty() || configPath == null)) {
			configFile = new File(configPath);
		} else {
			throw new IOException("Could not determine the path to the config file.");
		}

		// Check if the directory exists.
		if (!configFile.getParentFile().exists()) {
			LOGGER.info("Folder " + configFile.getParentFile() + " does not exist, creating it.");
			// Does not, create it
			if (!configFile.getParentFile().mkdirs()) {
				throw new IOException("Could not create '" + configFile.getParentFile() + "'.");
			}
		}

		// 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("Creating '" + configFile + "'...");
				// write default configuration options and values
				ini.put("main", "disclaimer_agreement", false);
				ini.put("main", "vmware_license_agreement", false);
				ini.put("main", "auth_method", "");
				ini.put("main", "username", "");
				ini.put("main", "download_path", "");
				ini.put("main", "upload_path", "");
				ini.put("main", "identity_provider", "");
				ini.store();

			} else {
				throw new IOException("Can not write to '" + configFile + "'. Do you have permissions?");
			}
		} else {
			ini = new Wini(configFile);
			LOGGER.info("Loaded '" + configFile + "'.");
		}
	} // end constructor.

	/**
	 * 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 value of 'BillOfRights' from the configuration file.
	 * 
	 * @return true if the user already accepted bill of rights, false
	 *         otherwise.
	 */
	public static boolean getDisclaimerAgreement() {
		return getBoolean("main", "disclaimer_agreement", false);
	}

	/**
	 * Query the value of 'vmware' from the configuration file.
	 * 
	 * @return true if the user already accepted vmware license, false
	 *         otherwise.
	 */
	public static boolean getVmwareLicenseAgreement() {
		return getBoolean("main", "vmware_license_agreement", 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", "username", "");
	}

	/**
	 * 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 getDownloadPath() {
		return getString("main", "download_path", 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 getUploadPath() {
		return getString("main", "upload_path", System.getProperty("user.home"));
	}

	/**
	 * Query the IdP of the configuration file
	 * 
	 * @return stored IdP
	 */
	public static String getIdentityProvider() {
		return getString("main", "identity_provider", "");
	}

	/**
	 * Query the authentication method of the configuration file
	 * 
	 * @return stored IdP
	 */
	public static String getAuthenticationMethod() {
		return getString("main", "auth_method", "bwlp");
	}

	/**
	 * Sets the value of 'BillOfRights' in the configuration file to 'value'
	 * 
	 * @return true if it succeeded, false otherwise
	 */
	public static boolean setDisclaimerAgreement(boolean value) {
		return setBoolean("main", "disclaimer_agreement", value);
	}

	/**
	 * Sets the value of 'vmware' in the configuration file to 'value'
	 * 
	 * @return true if it succeeded, false otherwise
	 */
	public static boolean setVmwareLicenseAgreement(boolean value) {
		return setBoolean("main", "vmware_license_agreement", 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", "username", value);
	}

	/**
	 * Sets the value of 'Letzter Downloadpfad' in the configuration file to
	 * 'value'
	 * 
	 * @return true if it succeeded, false otherwise
	 */
	public static boolean setDownloadPath(String value) {
		return setString("main", "download_path", value);
	}

	/**
	 * Sets the value of "Letzter Uploadpfad" in the configuration file to
	 * 'value'
	 * 
	 * @return true if it succeeded, false otherwise
	 */
	public static boolean setUploadPath(String value) {
		return setString("main", "upload_path", value);
	}

	/**
	 * Sets the value of "IdP" in the configuration file to 'value'
	 * 
	 * @return true if it succeeded, false otherwise
	 */
	public static boolean setIdentityProvider(String value) {
		return setString("main", "identity_provider", value);
	}

	/**
	 * Sets the value of the selected authentication method in the configuration
	 * file to 'value'
	 * 
	 * @return true if it succeeded, false otherwise
	 */
	public static boolean setAuthenticationMethod(String value) {
		return setString("main", "auth_method", 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;
	}

}