Changed license to CC0
[instantlauncher.git] / src / main / java / eu / svjatoslav / instantlauncher / configuration / ConfigurationManager.java
1 /*
2  * Instantlauncher. Author: Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.instantlauncher.configuration;
6
7 import com.esotericsoftware.yamlbeans.YamlConfig;
8 import com.esotericsoftware.yamlbeans.YamlReader;
9 import com.esotericsoftware.yamlbeans.YamlWriter;
10
11 import java.io.*;
12 import java.util.HashSet;
13
14 public class ConfigurationManager {
15
16     private static final String CONFIG_FILE_NAME = ".instantlauncher";
17     private Configuration configuration;
18
19     public ConfigurationManager() throws IOException {
20         initConfig();
21     }
22
23     public Configuration getConfiguration() {
24         return configuration;
25     }
26
27     private File getPropertiesFile() {
28         return new File(System.getProperty("user.home") + "/" + CONFIG_FILE_NAME);
29     }
30
31
32     private void initConfig() throws IOException {
33         final File propertiesFile = getPropertiesFile();
34         if (!propertiesFile.exists()) {
35             initDefaultConfiguration();
36         } else
37             loadConfigFile(propertiesFile);
38     }
39
40     private void loadConfigFile(File propertiesFile) throws IOException {
41         YamlReader reader = new YamlReader(new FileReader(propertiesFile));
42         configureYaml(reader.getConfig());
43         configuration = reader.read(Configuration.class);
44         if (configuration == null)
45             initDefaultConfiguration();
46     }
47
48     private void saveConfigFile() throws IOException {
49         YamlWriter writer = new YamlWriter(new FileWriter(getPropertiesFile()));
50         configureYaml(writer.getConfig());
51         writer.write(configuration);
52         writer.close();
53     }
54
55     private void configureYaml(YamlConfig config) {
56         config.setClassTag("configuration",  Configuration.class);
57         config.setPropertyElementType(Configuration.class, "fileAssociations", FileAssociation.class);
58     }
59
60     private void registerFileAssociation(String fileRegex, String command) {
61         FileAssociation association = new FileAssociation(fileRegex, command);
62         configuration.fileAssociations.add(association);
63     }
64
65     private void initDefaultConfiguration() throws IOException {
66         configuration = new Configuration();
67         configuration.fileAssociations = new HashSet<>();
68
69         configuration.navigationRootPath = System.getProperty("user.home") + "/";
70         configuration.directoryOpenCommand = "nautilus -w {file}";
71         configuration.directoryTerminalOpenCommand = "gnome-terminal --working-directory={file}";
72
73         registerFileAssociation(".*\\.jpeg$", "eog {file}");
74         registerFileAssociation(".*\\.jpg$", "eog {file}");
75         registerFileAssociation(".*\\.png$", "eog {file}");
76         registerFileAssociation(".*\\.txt$", "emacs {file}");
77         registerFileAssociation(".*\\.org$", "emacs {file}");
78         registerFileAssociation(".*\\.avi$", "vlc {file}");
79         registerFileAssociation(".*\\.mp4$", "vlc {file}");
80         registerFileAssociation(".*\\.mkv$", "vlc {file}");
81
82         saveConfigFile();
83     }
84 }