1 package tim.prune.config;
4 import java.io.FileInputStream;
5 import java.util.Properties;
9 * Abstract class to hold application-wide configuration
11 public abstract class Config
13 /** File from which Config was loaded */
14 private static File _configFile = null;
16 /** Hashtable containing all config values */
17 private static Properties _configValues = new Properties();
18 /** Colour scheme object is also part of config */
19 private static ColourScheme _colourScheme = new ColourScheme();
21 /** Default config file */
22 private static final File DEFAULT_CONFIG_FILE = new File(".pruneconfig");
24 /** Key for track directory */
25 public static final String KEY_TRACK_DIR = "prune.trackdirectory";
26 /** Key for photo directory */
27 public static final String KEY_PHOTO_DIR = "prune.photodirectory";
28 /** Key for language code */
29 public static final String KEY_LANGUAGE_CODE = "prune.languagecode";
30 /** Key for language file */
31 public static final String KEY_LANGUAGE_FILE = "prune.languagefile";
32 /** Key for GPS device */
33 public static final String KEY_GPS_DEVICE = "prune.gpsdevice";
34 /** Key for GPS format */
35 public static final String KEY_GPS_FORMAT = "prune.gpsformat";
36 /** Key for Povray font */
37 public static final String KEY_POVRAY_FONT = "prune.povrayfont";
38 /** Key for metric/imperial */
39 public static final String KEY_METRIC_UNITS = "prune.metricunits";
40 /** Key for index of map source */
41 public static final String KEY_MAPSOURCE_INDEX = "prune.mapsource";
42 /** Key for String containing custom map sources */
43 public static final String KEY_MAPSOURCE_LIST = "prune.mapsourcelist";
44 /** Key for show map flag */
45 public static final String KEY_SHOW_MAP = "prune.showmap";
46 /** Key for path to disk cache */
47 public static final String KEY_DISK_CACHE = "prune.diskcache";
48 /** Key for working online flag */
49 public static final String KEY_ONLINE_MODE = "prune.onlinemode";
50 /** Key for width of thumbnails in kmz */
51 public static final String KEY_KMZ_IMAGE_WIDTH = "prune.kmzimagewidth";
52 /** Key for height of thumbnails in kmz */
53 public static final String KEY_KMZ_IMAGE_HEIGHT = "prune.kmzimageheight";
54 /** Key for gpsbabel path */
55 public static final String KEY_GPSBABEL_PATH = "prune.gpsbabelpath";
56 /** Key for gnuplot path */
57 public static final String KEY_GNUPLOT_PATH = "prune.gnuplotpath";
58 /** Key for exiftool path */
59 public static final String KEY_EXIFTOOL_PATH = "prune.exiftoolpath";
60 /** Key for colour scheme */
61 public static final String KEY_COLOUR_SCHEME = "prune.colourscheme";
62 /** Key for kml track colour */
63 public static final String KEY_KML_TRACK_COLOUR = "prune.kmltrackcolour";
67 * Load the default configuration file
69 public static void loadDefaultFile()
73 loadFile(DEFAULT_CONFIG_FILE);
75 catch (ConfigException ce) {} // ignore
80 * Load configuration from file
81 * @param inFile file to load
82 * @throws ConfigException if specified file couldn't be read
84 public static void loadFile(File inFile) throws ConfigException
86 // Start with default properties
87 Properties props = getDefaultProperties();
88 // Try to load the file into a properties object
89 boolean loadFailed = false;
90 FileInputStream fis = null;
93 fis = new FileInputStream(inFile);
100 if (fis != null) try {
103 catch (Exception e) {}
105 // Save all properties from file
106 _configValues.putAll(props);
107 _colourScheme.loadFromHex(_configValues.getProperty(KEY_COLOUR_SCHEME));
109 throw new ConfigException();
111 // Store location of successfully loaded config file
112 _configFile = inFile;
116 * @return Properties object containing default values
118 private static Properties getDefaultProperties()
120 Properties props = new Properties();
122 props.put(KEY_GPS_DEVICE, "usb:");
123 props.put(KEY_GPS_FORMAT, "garmin");
124 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
125 props.put(KEY_SHOW_MAP, "0"); // hide by default
126 props.put(KEY_EXIFTOOL_PATH, "exiftool");
127 props.put(KEY_GNUPLOT_PATH, "gnuplot");
128 props.put(KEY_GPSBABEL_PATH, "gpsbabel");
129 props.put(KEY_KMZ_IMAGE_WIDTH, "240");
130 props.put(KEY_KMZ_IMAGE_HEIGHT, "240");
135 * @param inString String to parse
136 * @return int value of String, or 0 if unparseable
138 private static int parseInt(String inString)
142 val = Integer.parseInt(inString);
144 catch (Exception e) {} // ignore, value stays zero
148 /** @return File from which config was loaded (or null) */
149 public static File getConfigFile()
155 * @return config Properties object to allow all config values to be saved
157 public static Properties getAllConfig()
159 return _configValues;
163 * @return the current colour scheme
165 public static ColourScheme getColourScheme()
167 return _colourScheme;
171 * Store the given configuration setting
172 * @param inKey key (from constants)
173 * @param inValue value as string
175 public static void setConfigString(String inKey, String inValue)
177 if (inValue == null || inValue.equals("")) {
178 _configValues.remove(inKey);
181 _configValues.put(inKey, inValue);
186 * Store the given configuration setting
187 * @param inKey key (from constants)
188 * @param inValue value as boolean
190 public static void setConfigBoolean(String inKey, boolean inValue)
192 if (inKey != null && !inKey.equals(""))
194 _configValues.put(inKey, (inValue?"1":"0"));
199 * Store the given configuration setting
200 * @param inKey key (from constants)
201 * @param inValue value as int
203 public static void setConfigInt(String inKey, int inValue)
205 if (inKey != null && !inKey.equals(""))
207 _configValues.put(inKey, "" + inValue);
212 * Get the given configuration setting as a String
214 * @return configuration setting as a String
216 public static String getConfigString(String inKey)
218 return _configValues.getProperty(inKey);
222 * Get the given configuration setting as a boolean
224 * @return configuration setting as a boolean (default to true)
226 public static boolean getConfigBoolean(String inKey)
228 String val = _configValues.getProperty(inKey);
229 return (val == null || val.equals("1"));
233 * Get the given configuration setting as an int
235 * @return configuration setting as an int
237 public static int getConfigInt(String inKey)
239 return parseInt(_configValues.getProperty(inKey));
243 * Check whether the given key corresponds to a boolean property
244 * @param inKey key to check
245 * @return true if corresponding property is boolean
247 public static boolean isKeyBoolean(String inKey)
249 // Only two boolean keys so far
250 return inKey != null && (
251 inKey.equals(KEY_METRIC_UNITS) || inKey.equals(KEY_SHOW_MAP));
255 * Update the colour scheme property from the current settings
257 public static void updateColourScheme()
259 setConfigString(KEY_COLOUR_SCHEME, _colourScheme.toString());