1 package tim.prune.config;
4 import java.io.FileInputStream;
5 import java.util.Properties;
7 import tim.prune.data.RecentFileList;
11 * Abstract class to hold application-wide configuration
13 public abstract class Config
15 /** File from which Config was loaded */
16 private static File _configFile = null;
18 /** Hashtable containing all config values */
19 private static Properties _configValues = new Properties();
20 /** Colour scheme object is also part of config */
21 private static ColourScheme _colourScheme = new ColourScheme();
22 /** Recently-used file list */
23 private static RecentFileList _recentFiles = new RecentFileList();
25 /** Default config file */
26 public static final File DEFAULT_CONFIG_FILE = new File(".pruneconfig");
27 public static final File HOME_CONFIG_FILE = new File(System.getProperty("user.home"), ".pruneconfig");
29 /** Key for track directory */
30 public static final String KEY_TRACK_DIR = "prune.trackdirectory";
31 /** Key for photo directory */
32 public static final String KEY_PHOTO_DIR = "prune.photodirectory";
33 /** Key for language code */
34 public static final String KEY_LANGUAGE_CODE = "prune.languagecode";
35 /** Key for language file */
36 public static final String KEY_LANGUAGE_FILE = "prune.languagefile";
37 /** Key for GPS device */
38 public static final String KEY_GPS_DEVICE = "prune.gpsdevice";
39 /** Key for GPS format */
40 public static final String KEY_GPS_FORMAT = "prune.gpsformat";
41 /** Key for Povray font */
42 public static final String KEY_POVRAY_FONT = "prune.povrayfont";
43 /** Key for metric/imperial */
44 public static final String KEY_METRIC_UNITS = "prune.metricunits";
45 /** Key for index of map source */
46 public static final String KEY_MAPSOURCE_INDEX = "prune.mapsource";
47 /** Key for String containing custom map sources */
48 public static final String KEY_MAPSOURCE_LIST = "prune.mapsourcelist";
49 /** Key for show map flag */
50 public static final String KEY_SHOW_MAP = "prune.showmap";
51 /** Key for path to disk cache */
52 public static final String KEY_DISK_CACHE = "prune.diskcache";
53 /** Key for working online flag */
54 public static final String KEY_ONLINE_MODE = "prune.onlinemode";
55 /** Key for width of thumbnails in kmz */
56 public static final String KEY_KMZ_IMAGE_WIDTH = "prune.kmzimagewidth";
57 /** Key for height of thumbnails in kmz */
58 public static final String KEY_KMZ_IMAGE_HEIGHT = "prune.kmzimageheight";
59 /** Key for gpsbabel path */
60 public static final String KEY_GPSBABEL_PATH = "prune.gpsbabelpath";
61 /** Key for gnuplot path */
62 public static final String KEY_GNUPLOT_PATH = "prune.gnuplotpath";
63 /** Key for exiftool path */
64 public static final String KEY_EXIFTOOL_PATH = "prune.exiftoolpath";
65 /** Key for colour scheme */
66 public static final String KEY_COLOUR_SCHEME = "prune.colourscheme";
67 /** Key for line width used for drawing */
68 public static final String KEY_LINE_WIDTH = "prune.linewidth";
69 /** Key for kml track colour */
70 public static final String KEY_KML_TRACK_COLOUR = "prune.kmltrackcolour";
71 /** Key for autosaving settings */
72 public static final String KEY_AUTOSAVE_SETTINGS = "prune.autosavesettings";
73 /** Key for recently used files */
74 public static final String KEY_RECENT_FILES = "prune.recentfiles";
78 * Load the default configuration file
80 public static void loadDefaultFile()
82 if (DEFAULT_CONFIG_FILE.exists())
85 loadFile(DEFAULT_CONFIG_FILE);
88 catch (ConfigException ce) {} // ignore
90 if (HOME_CONFIG_FILE.exists())
93 loadFile(HOME_CONFIG_FILE);
95 catch (ConfigException ce) {} // ignore
101 * Load configuration from file
102 * @param inFile file to load
103 * @throws ConfigException if specified file couldn't be read
105 public static void loadFile(File inFile) throws ConfigException
107 // Start with default properties
108 Properties props = getDefaultProperties();
109 // Try to load the file into a properties object
110 boolean loadFailed = false;
111 FileInputStream fis = null;
114 fis = new FileInputStream(inFile);
117 catch (Exception e) {
121 if (fis != null) try {
124 catch (Exception e) {}
126 // Save all properties from file
127 _configValues.putAll(props);
128 _colourScheme.loadFromHex(_configValues.getProperty(KEY_COLOUR_SCHEME));
129 _recentFiles = new RecentFileList(_configValues.getProperty(KEY_RECENT_FILES));
131 throw new ConfigException();
133 // Store location of successfully loaded config file
134 _configFile = inFile;
138 * @return Properties object containing default values
140 private static Properties getDefaultProperties()
142 Properties props = new Properties();
144 props.put(KEY_GPS_DEVICE, "usb:");
145 props.put(KEY_GPS_FORMAT, "garmin");
146 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
147 props.put(KEY_SHOW_MAP, "0"); // hide by default
148 props.put(KEY_EXIFTOOL_PATH, "exiftool");
149 props.put(KEY_GNUPLOT_PATH, "gnuplot");
150 props.put(KEY_GPSBABEL_PATH, "gpsbabel");
151 props.put(KEY_KMZ_IMAGE_WIDTH, "240");
152 props.put(KEY_KMZ_IMAGE_HEIGHT, "240");
153 props.put(KEY_AUTOSAVE_SETTINGS, "0"); // autosave false by default
158 * @param inString String to parse
159 * @return int value of String, or 0 if unparseable
161 private static int parseInt(String inString)
165 val = Integer.parseInt(inString);
167 catch (Exception e) {} // ignore, value stays zero
171 /** @return File from which config was loaded (or null) */
172 public static File getConfigFile()
178 * @return config Properties object to allow all config values to be saved
180 public static Properties getAllConfig()
182 // Update recently-used files
183 _configValues.setProperty(KEY_RECENT_FILES, _recentFiles.getConfigString());
184 return _configValues;
188 * @return the current colour scheme
190 public static ColourScheme getColourScheme()
192 return _colourScheme;
196 * @return list of recently used files
198 public static RecentFileList getRecentFileList()
204 * Store the given configuration setting
205 * @param inKey key (from constants)
206 * @param inValue value as string
208 public static void setConfigString(String inKey, String inValue)
210 if (inValue == null || inValue.equals("")) {
211 _configValues.remove(inKey);
214 _configValues.put(inKey, inValue);
219 * Store the given configuration setting
220 * @param inKey key (from constants)
221 * @param inValue value as boolean
223 public static void setConfigBoolean(String inKey, boolean inValue)
225 if (inKey != null && !inKey.equals(""))
227 _configValues.put(inKey, (inValue?"1":"0"));
232 * Store the given configuration setting
233 * @param inKey key (from constants)
234 * @param inValue value as int
236 public static void setConfigInt(String inKey, int inValue)
238 if (inKey != null && !inKey.equals(""))
240 _configValues.put(inKey, "" + inValue);
245 * Get the given configuration setting as a String
247 * @return configuration setting as a String
249 public static String getConfigString(String inKey)
251 return _configValues.getProperty(inKey);
255 * Get the given configuration setting as a boolean
257 * @return configuration setting as a boolean (default to true)
259 public static boolean getConfigBoolean(String inKey)
261 String val = _configValues.getProperty(inKey);
262 return (val == null || val.equals("1"));
266 * Get the given configuration setting as an int
268 * @return configuration setting as an int
270 public static int getConfigInt(String inKey)
272 return parseInt(_configValues.getProperty(inKey));
276 * Check whether the given key corresponds to a boolean property
277 * @param inKey key to check
278 * @return true if corresponding property is boolean
280 public static boolean isKeyBoolean(String inKey)
282 // Only two boolean keys so far
283 return inKey != null && (
284 inKey.equals(KEY_METRIC_UNITS) || inKey.equals(KEY_SHOW_MAP));
288 * Update the colour scheme property from the current settings
290 public static void updateColourScheme()
292 setConfigString(KEY_COLOUR_SCHEME, _colourScheme.toString());