1 package eu.svjatoslav.instantlauncher.configuration;
3 import com.esotericsoftware.yamlbeans.YamlReader;
4 import com.esotericsoftware.yamlbeans.YamlWriter;
7 import java.util.HashSet;
9 public class ConfigurationManager {
11 private static final String CONFIG_FILE_NAME = ".instantlauncher";
12 private Configuration configuration;
14 public ConfigurationManager() throws IOException {
18 public Configuration getConfiguration() {
22 private File getPropertiesFile() {
23 return new File(System.getProperty("user.home") + "/" + CONFIG_FILE_NAME);
27 private void initConfig() throws IOException {
28 final File propertiesFile = getPropertiesFile();
29 if (!propertiesFile.exists()) {
30 initDefaultConfiguration();
32 loadConfigFile(propertiesFile);
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();
42 private void saveConfigFile() throws IOException {
43 YamlWriter writer = new YamlWriter(new FileWriter(getPropertiesFile()));
44 writer.write(configuration);
48 private void registerFileAssociation(String fileRegex, String command) {
49 FileAssociation association = new FileAssociation(fileRegex, command);
50 configuration.fileAssociations.add(association);
53 private void initDefaultConfiguration() throws IOException {
54 configuration = new Configuration();
55 configuration.fileAssociations = new HashSet<>();
57 configuration.navigationRootPath = System.getProperty("user.home") + "/";
58 configuration.directoryOpenCommand = "nautilus -w {file}";
59 configuration.directoryTerminalOpenCommand = "gnome-terminal --working-directory={file}";
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}");