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 line width used for drawing */
63 public static final String KEY_LINE_WIDTH = "prune.linewidth";
64 /** Key for kml track colour */
65 public static final String KEY_KML_TRACK_COLOUR = "prune.kmltrackcolour";
69 * Load the default configuration file
71 public static void loadDefaultFile()
75 loadFile(DEFAULT_CONFIG_FILE);
77 catch (ConfigException ce) {} // ignore
82 * Load configuration from file
83 * @param inFile file to load
84 * @throws ConfigException if specified file couldn't be read
86 public static void loadFile(File inFile) throws ConfigException
88 // Start with default properties
89 Properties props = getDefaultProperties();
90 // Try to load the file into a properties object
91 boolean loadFailed = false;
92 FileInputStream fis = null;
95 fis = new FileInputStream(inFile);
102 if (fis != null) try {
105 catch (Exception e) {}
107 // Save all properties from file
108 _configValues.putAll(props);
109 _colourScheme.loadFromHex(_configValues.getProperty(KEY_COLOUR_SCHEME));
111 throw new ConfigException();
113 // Store location of successfully loaded config file
114 _configFile = inFile;
118 * @return Properties object containing default values
120 private static Properties getDefaultProperties()
122 Properties props = new Properties();
124 props.put(KEY_GPS_DEVICE, "usb:");
125 props.put(KEY_GPS_FORMAT, "garmin");
126 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
127 props.put(KEY_SHOW_MAP, "0"); // hide by default
128 props.put(KEY_EXIFTOOL_PATH, "exiftool");
129 props.put(KEY_GNUPLOT_PATH, "gnuplot");
130 props.put(KEY_GPSBABEL_PATH, "gpsbabel");
131 props.put(KEY_KMZ_IMAGE_WIDTH, "240");
132 props.put(KEY_KMZ_IMAGE_HEIGHT, "240");
137 * @param inString String to parse
138 * @return int value of String, or 0 if unparseable
140 private static int parseInt(String inString)
144 val = Integer.parseInt(inString);
146 catch (Exception e) {} // ignore, value stays zero
150 /** @return File from which config was loaded (or null) */
151 public static File getConfigFile()
157 * @return config Properties object to allow all config values to be saved
159 public static Properties getAllConfig()
161 return _configValues;
165 * @return the current colour scheme
167 public static ColourScheme getColourScheme()
169 return _colourScheme;
173 * Store the given configuration setting
174 * @param inKey key (from constants)
175 * @param inValue value as string
177 public static void setConfigString(String inKey, String inValue)
179 if (inValue == null || inValue.equals("")) {
180 _configValues.remove(inKey);
183 _configValues.put(inKey, inValue);
188 * Store the given configuration setting
189 * @param inKey key (from constants)
190 * @param inValue value as boolean
192 public static void setConfigBoolean(String inKey, boolean inValue)
194 if (inKey != null && !inKey.equals(""))
196 _configValues.put(inKey, (inValue?"1":"0"));
201 * Store the given configuration setting
202 * @param inKey key (from constants)
203 * @param inValue value as int
205 public static void setConfigInt(String inKey, int inValue)
207 if (inKey != null && !inKey.equals(""))
209 _configValues.put(inKey, "" + inValue);
214 * Get the given configuration setting as a String
216 * @return configuration setting as a String
218 public static String getConfigString(String inKey)
220 return _configValues.getProperty(inKey);
224 * Get the given configuration setting as a boolean
226 * @return configuration setting as a boolean (default to true)
228 public static boolean getConfigBoolean(String inKey)
230 String val = _configValues.getProperty(inKey);
231 return (val == null || val.equals("1"));
235 * Get the given configuration setting as an int
237 * @return configuration setting as an int
239 public static int getConfigInt(String inKey)
241 return parseInt(_configValues.getProperty(inKey));
245 * Check whether the given key corresponds to a boolean property
246 * @param inKey key to check
247 * @return true if corresponding property is boolean
249 public static boolean isKeyBoolean(String inKey)
251 // Only two boolean keys so far
252 return inKey != null && (
253 inKey.equals(KEY_METRIC_UNITS) || inKey.equals(KEY_SHOW_MAP));
257 * Update the colour scheme property from the current settings
259 public static void updateColourScheme()
261 setConfigString(KEY_COLOUR_SCHEME, _colourScheme.toString());