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 public Configuration getConfiguration() {
20 private Configuration configuration;
22 public ConfigurationManager() throws IOException {
26 private File getPropertiesFile() {
27 return new File(System.getProperty("user.home") + "/" + CONFIG_FILE_NAME);
30 public File getNavigationRootDirectory() {
31 if (configuration.navigationRootPath == null){
32 configuration.navigationRootPath = System.getProperty("user.home") + "/";
33 propertiesChanged = true;
34 registerDefaultAssociations();
37 return new File(configuration.navigationRootPath);
40 private void initialize() throws IOException {
44 validatePropertiesFile();
46 if (propertiesChanged) {
51 private void loadIfFileExists() throws IOException {
52 final File propertiesFile = getPropertiesFile();
53 if (!propertiesFile.exists())
56 YamlReader reader = new YamlReader(new FileReader(propertiesFile));
57 configuration = reader.read(Configuration.class);
58 if (configuration == null) {
59 configuration = new Configuration();
60 configuration.fileAssociations = new HashSet<>();
64 private void saveFile() throws IOException {
65 YamlWriter writer = new YamlWriter(new FileWriter(getPropertiesFile()));
66 writer.write(configuration);
70 private void validatePropertiesFile() {
71 getNavigationRootDirectory();
74 private void registerFileAssociation(String fileRegex, String command){
75 FileAssociation association = new FileAssociation(fileRegex, command);
76 configuration.fileAssociations.add(association);
77 propertiesChanged = true;
81 private void registerDefaultAssociations(){
82 registerFileAssociation(".jpeg$", "eog {file}");