1 package eu.svjatoslav.instantlauncher.configuration;
3 import com.esotericsoftware.yamlbeans.YamlReader;
4 import com.esotericsoftware.yamlbeans.YamlWriter;
7 import java.util.HashSet;
10 public class ConfigurationManager {
12 private static final String CONFIG_FILE_NAME = ".instantlauncher";
14 private boolean propertiesChanged = false;
16 Configuration configuration;
18 public ConfigurationManager() throws IOException {
22 private File getPropertiesFile() {
23 return new File(System.getProperty("user.home") + "/" + CONFIG_FILE_NAME);
26 public File getNavigationRootDirectory() {
27 if (configuration.navigationRootPath == null){
28 configuration.navigationRootPath = System.getProperty("user.home") + "/";
29 propertiesChanged = true;
30 registerDefaultAssociations();
33 return new File(configuration.navigationRootPath);
36 private void initialize() throws IOException {
40 validatePropertiesFile();
42 if (propertiesChanged) {
47 private void loadIfFileExists() throws IOException {
48 final File propertiesFile = getPropertiesFile();
49 if (!propertiesFile.exists())
52 YamlReader reader = new YamlReader(new FileReader(propertiesFile));
53 configuration = reader.read(Configuration.class);
54 if (configuration == null) {
55 configuration = new Configuration();
56 configuration.fileAssociations = new HashSet<>();
60 private void saveFile() throws IOException {
61 YamlWriter writer = new YamlWriter(new FileWriter(getPropertiesFile()));
62 writer.write(configuration);
66 private void validatePropertiesFile() {
67 getNavigationRootDirectory();
70 private void registerFileAssociation(String fileRegex, String command){
71 FileAssociation association = new FileAssociation(fileRegex, command);
72 configuration.fileAssociations.add(association);
73 propertiesChanged = true;
76 private void registerDefaultAssociations(){
77 registerFileAssociation(".jpeg$", "eog {file}");