initial commit
[instantlauncher.git] / src / main / java / eu / svjatoslav / instantlauncher / Configuration.java
1 package eu.svjatoslav.instantlauncher;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.util.Properties;
9
10 public class Configuration {
11
12     private static final String KEY_ROOT_DIRECTORY = "KEY_ROOT_DIRECTORY";
13     private static final String CONFIG_FILE_NAME = ".instantlauncher";
14     Properties properties = new Properties();
15
16     boolean propertiesChanged = false;
17
18     public Configuration() throws FileNotFoundException, IOException {
19         initialize();
20     }
21
22     public File getPropertiesFile() {
23         return new File(System.getProperty("user.home") + "/" + CONFIG_FILE_NAME);
24     }
25
26     public File getRootDirectory() {
27
28         if (properties.containsKey(KEY_ROOT_DIRECTORY)) {
29             return new File(properties.getProperty(KEY_ROOT_DIRECTORY));
30         } else {
31             properties.put(KEY_ROOT_DIRECTORY, System.getProperty("user.home") + "/");
32             propertiesChanged = true;
33             return getRootDirectory();
34         }
35
36     }
37
38     public void initialize() throws FileNotFoundException, IOException {
39
40         loadIfFileExists();
41
42         validatePropertiesFile();
43
44         if (propertiesChanged) {
45             saveFile();
46         }
47     }
48
49     public void loadIfFileExists() throws FileNotFoundException, IOException {
50         final File propertiesFile = getPropertiesFile();
51
52         if (propertiesFile.exists()) {
53             final FileInputStream inStream = new FileInputStream(propertiesFile);
54             properties.load(inStream);
55             inStream.close();
56         }
57     }
58
59     public void saveFile() throws FileNotFoundException, IOException {
60         properties.store(new FileOutputStream(getPropertiesFile()), "Instantlauncher configuration file.");
61     }
62
63     public void validatePropertiesFile() {
64         getRootDirectory();
65     }
66
67 }