+package eu.svjatoslav.instantlauncher.configuration;
+
+import com.esotericsoftware.yamlbeans.YamlReader;
+import com.esotericsoftware.yamlbeans.YamlWriter;
+
+import java.io.*;
+import java.util.HashSet;
+import java.util.Map;
+
+public class ConfigurationManager {
+
+ private static final String CONFIG_FILE_NAME = ".instantlauncher";
+
+ private boolean propertiesChanged = false;
+
+ Configuration configuration;
+
+ public ConfigurationManager() throws IOException {
+ initialize();
+ }
+
+ private File getPropertiesFile() {
+ return new File(System.getProperty("user.home") + "/" + CONFIG_FILE_NAME);
+ }
+
+ public File getNavigationRootDirectory() {
+ if (configuration.navigationRootPath == null){
+ configuration.navigationRootPath = System.getProperty("user.home") + "/";
+ propertiesChanged = true;
+ registerDefaultAssociations();
+ }
+
+ return new File(configuration.navigationRootPath);
+ }
+
+ private void initialize() throws IOException {
+
+ loadIfFileExists();
+
+ validatePropertiesFile();
+
+ if (propertiesChanged) {
+ saveFile();
+ }
+ }
+
+ private void loadIfFileExists() throws IOException {
+ final File propertiesFile = getPropertiesFile();
+ if (!propertiesFile.exists())
+ return;
+
+ YamlReader reader = new YamlReader(new FileReader(propertiesFile));
+ configuration = reader.read(Configuration.class);
+ if (configuration == null) {
+ configuration = new Configuration();
+ configuration.fileAssociations = new HashSet<>();
+ };
+ }
+
+ private void saveFile() throws IOException {
+ YamlWriter writer = new YamlWriter(new FileWriter(getPropertiesFile()));
+ writer.write(configuration);
+ writer.close();
+ }
+
+ private void validatePropertiesFile() {
+ getNavigationRootDirectory();
+ }
+
+ private void registerFileAssociation(String fileRegex, String command){
+ FileAssociation association = new FileAssociation(fileRegex, command);
+ configuration.fileAssociations.add(association);
+ propertiesChanged = true;
+ }
+
+ private void registerDefaultAssociations(){
+ registerFileAssociation(".jpeg$", "eog {file}");
+ }
+}