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