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();
33 loadConfigFile(propertiesFile);
36 private void loadConfigFile(File propertiesFile) throws FileNotFoundException, com.esotericsoftware.yamlbeans.YamlException {
37 YamlReader reader = new YamlReader(new FileReader(propertiesFile));
38 configuration = reader.read(Configuration.class);
39 if (configuration == null) {
40 configuration = new Configuration();
41 configuration.fileAssociations = new HashSet<>();
45 private void saveConfigFile() throws IOException {
46 YamlWriter writer = new YamlWriter(new FileWriter(getPropertiesFile()));
47 writer.write(configuration);
51 private void registerFileAssociation(String fileRegex, String command) {
52 FileAssociation association = new FileAssociation(fileRegex, command);
53 configuration.fileAssociations.add(association);
56 private void initDefaultConfiguration() {
57 configuration.navigationRootPath = System.getProperty("user.home") + "/";
58 configuration.directoryOpenCommand = "nautilus -w {file}";
60 registerFileAssociation(".*\\.jpeg$", "eog {file}");
61 registerFileAssociation(".*\\.jpg$", "eog {file}");
62 registerFileAssociation(".*\\.png$", "eog {file}");
63 registerFileAssociation(".*\\.txt$", "emacs {file}");
64 registerFileAssociation(".*\\.org$", "emacs {file}");
65 registerFileAssociation(".*\\.avi$", "vlc {file}");
66 registerFileAssociation(".*\\.mp4$", "vlc {file}");
67 registerFileAssociation(".*\\.mkv$", "vlc {file}");