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