2 * Instantlauncher. Copyright ©2012-2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
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.
9 package eu.svjatoslav.instantlauncher.configuration;
11 import com.esotericsoftware.yamlbeans.YamlConfig;
12 import com.esotericsoftware.yamlbeans.YamlReader;
13 import com.esotericsoftware.yamlbeans.YamlWriter;
16 import java.util.HashSet;
18 public class ConfigurationManager {
20 private static final String CONFIG_FILE_NAME = ".instantlauncher";
21 private Configuration configuration;
23 public ConfigurationManager() throws IOException {
27 public Configuration getConfiguration() {
31 private File getPropertiesFile() {
32 return new File(System.getProperty("user.home") + "/" + CONFIG_FILE_NAME);
36 private void initConfig() throws IOException {
37 final File propertiesFile = getPropertiesFile();
38 if (!propertiesFile.exists()) {
39 initDefaultConfiguration();
41 loadConfigFile(propertiesFile);
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();
52 private void saveConfigFile() throws IOException {
53 YamlWriter writer = new YamlWriter(new FileWriter(getPropertiesFile()));
54 configureYaml(writer.getConfig());
55 writer.write(configuration);
59 private void configureYaml(YamlConfig config) {
60 config.setClassTag("configuration", Configuration.class);
61 config.setPropertyElementType(Configuration.class, "fileAssociations", FileAssociation.class);
64 private void registerFileAssociation(String fileRegex, String command) {
65 FileAssociation association = new FileAssociation(fileRegex, command);
66 configuration.fileAssociations.add(association);
69 private void initDefaultConfiguration() throws IOException {
70 configuration = new Configuration();
71 configuration.fileAssociations = new HashSet<>();
73 configuration.navigationRootPath = System.getProperty("user.home") + "/";
74 configuration.directoryOpenCommand = "nautilus -w {file}";
75 configuration.directoryTerminalOpenCommand = "gnome-terminal --working-directory={file}";
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}");