summaryrefslogtreecommitdiff
path: root/src/com/delwink/icebox/Config.java
blob: 66ae602081fe992adc9328b8db26d4d4dead406c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * IceBox - inventory management software for restaurants
 * Copyright (C) 2016 Delwink, LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, version 3 only.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.delwink.icebox;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Static class for accessing user-specific preferences.
 * @author David McMackins II
 */
public final class Config {
    private static final File CONFIG_FILE = DataDir.getDataFile("icebox.properties");
    private static final Config CONFIG = new Config();
    
    private final Properties PROPERTIES;
    
    private Config() {
        PROPERTIES = new Properties();
        
        PROPERTIES.setProperty("lang", "en_US");
        PROPERTIES.setProperty("mainwindow.maximized", "n");
        PROPERTIES.setProperty("mainwindow.width", "300");
        PROPERTIES.setProperty("mainwindow.height", "600");
        PROPERTIES.setProperty("setlaf", "y");
        
        if (CONFIG_FILE.exists()) {
            try {
                PROPERTIES.load(new FileInputStream(CONFIG_FILE));
            } catch (IOException ex) {
                System.err.println("Failed to load config file");
            }
        }
    }
    
    /**
     * Sets a user-specific preference.
     * @param key The name of the preference.
     * @param value The value of the preference.
     */
    public static void put(String key, String value) {
        key = key.toLowerCase();
        
        if (!value.equals(get(key))) {
            CONFIG.PROPERTIES.setProperty(key, value);
            
            try {
                CONFIG.PROPERTIES.store(new FileOutputStream(CONFIG_FILE), "Generated by IceBox");
            } catch (IOException ex) {
                Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    /**
     * Gets a user-specific preference.
     * @param key The name of the preference.
     * @return The value of the preference.
     */
    public static String get(String key) {
        return CONFIG.PROPERTIES.getProperty(key.toLowerCase());
    }
}