1 package tim.prune.config;
4 import java.io.FileInputStream;
5 import java.util.Properties;
7 import tim.prune.data.RecentFileList;
8 import tim.prune.data.UnitSet;
9 import tim.prune.data.UnitSetLibrary;
13 * Abstract class to hold application-wide configuration
15 public abstract class Config
17 /** File from which Config was loaded */
18 private static File _configFile = null;
20 /** Hashtable containing all config values */
21 private static Properties _configValues = null;
22 /** Colour scheme object is also part of config */
23 private static ColourScheme _colourScheme = new ColourScheme();
24 /** Recently-used file list */
25 private static RecentFileList _recentFiles = new RecentFileList();
26 /** Current unit set */
27 private static UnitSet _unitSet = UnitSetLibrary.getUnitSet(null);
29 /** Default config file */
30 public static final File DEFAULT_CONFIG_FILE = new File(".pruneconfig");
31 public static final File HOME_CONFIG_FILE = new File(System.getProperty("user.home"), ".pruneconfig");
33 /** Key for track directory */
34 public static final String KEY_TRACK_DIR = "prune.trackdirectory";
35 /** Key for photo directory */
36 public static final String KEY_PHOTO_DIR = "prune.photodirectory";
37 /** Key for language code */
38 public static final String KEY_LANGUAGE_CODE = "prune.languagecode";
39 /** Key for language file */
40 public static final String KEY_LANGUAGE_FILE = "prune.languagefile";
41 /** Key for GPS device */
42 public static final String KEY_GPS_DEVICE = "prune.gpsdevice";
43 /** Key for GPS format */
44 public static final String KEY_GPS_FORMAT = "prune.gpsformat";
45 /** Key for Povray font */
46 public static final String KEY_POVRAY_FONT = "prune.povrayfont";
47 /** Key for the selected unit set */
48 public static final String KEY_UNITSET_KEY = "prune.unitsetkey";
49 /** Key for index of map source */
50 public static final String KEY_MAPSOURCE_INDEX = "prune.mapsource";
51 /** Key for number of fixed map sources */
52 public static final String KEY_NUM_FIXED_MAPS = "prune.numfixedmapsources";
53 /** Key for String containing custom map sources */
54 public static final String KEY_MAPSOURCE_LIST = "prune.mapsourcelist";
55 /** Key for show map flag */
56 public static final String KEY_SHOW_MAP = "prune.showmap";
57 /** Key for path to disk cache */
58 public static final String KEY_DISK_CACHE = "prune.diskcache";
59 /** Key for working online flag */
60 public static final String KEY_ONLINE_MODE = "prune.onlinemode";
61 /** Key for width of thumbnails in kmz */
62 public static final String KEY_KMZ_IMAGE_WIDTH = "prune.kmzimagewidth";
63 /** Key for height of thumbnails in kmz */
64 public static final String KEY_KMZ_IMAGE_HEIGHT = "prune.kmzimageheight";
65 /** Key for gpsbabel path */
66 public static final String KEY_GPSBABEL_PATH = "prune.gpsbabelpath";
67 /** Key for gnuplot path */
68 public static final String KEY_GNUPLOT_PATH = "prune.gnuplotpath";
69 /** Key for exiftool path */
70 public static final String KEY_EXIFTOOL_PATH = "prune.exiftoolpath";
71 /** Key for colour scheme */
72 public static final String KEY_COLOUR_SCHEME = "prune.colourscheme";
73 /** Key for line width used for drawing */
74 public static final String KEY_LINE_WIDTH = "prune.linewidth";
75 /** Key for kml track colour */
76 public static final String KEY_KML_TRACK_COLOUR = "prune.kmltrackcolour";
77 /** Key for autosaving settings */
78 public static final String KEY_AUTOSAVE_SETTINGS = "prune.autosavesettings";
79 /** Key for recently used files */
80 public static final String KEY_RECENT_FILES = "prune.recentfiles";
83 /** Initialise the default properties */
86 _configValues = getDefaultProperties();
90 * Load the default configuration file
92 public static void loadDefaultFile()
94 if (DEFAULT_CONFIG_FILE.exists())
97 loadFile(DEFAULT_CONFIG_FILE);
100 catch (ConfigException ce) {} // ignore
102 if (HOME_CONFIG_FILE.exists())
105 loadFile(HOME_CONFIG_FILE);
107 catch (ConfigException ce) {} // ignore
113 * Load configuration from file
114 * @param inFile file to load
115 * @throws ConfigException if specified file couldn't be read
117 public static void loadFile(File inFile) throws ConfigException
119 // Start with default properties
120 Properties props = getDefaultProperties();
121 // Try to load the file into a properties object
122 boolean loadFailed = false;
123 FileInputStream fis = null;
126 fis = new FileInputStream(inFile);
129 catch (Exception e) {
133 if (fis != null) try {
136 catch (Exception e) {}
138 // Save all properties from file
139 _configValues.putAll(props);
140 _colourScheme.loadFromHex(_configValues.getProperty(KEY_COLOUR_SCHEME));
141 _recentFiles = new RecentFileList(_configValues.getProperty(KEY_RECENT_FILES));
142 _unitSet = UnitSetLibrary.getUnitSet(_configValues.getProperty(KEY_UNITSET_KEY));
145 throw new ConfigException();
147 // Store location of successfully loaded config file
148 _configFile = inFile;
152 * @return Properties object containing default values
154 private static Properties getDefaultProperties()
156 Properties props = new Properties();
158 props.put(KEY_GPS_DEVICE, "usb:");
159 props.put(KEY_GPS_FORMAT, "garmin");
160 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
161 props.put(KEY_SHOW_MAP, "0"); // hide by default
162 props.put(KEY_EXIFTOOL_PATH, "exiftool");
163 props.put(KEY_GNUPLOT_PATH, "gnuplot");
164 props.put(KEY_GPSBABEL_PATH, "gpsbabel");
165 props.put(KEY_KMZ_IMAGE_WIDTH, "240");
166 props.put(KEY_KMZ_IMAGE_HEIGHT, "240");
167 props.put(KEY_AUTOSAVE_SETTINGS, "0"); // autosave false by default
168 props.put(KEY_UNITSET_KEY, "unitset.kilometres"); // metric by default
173 * @param inString String to parse
174 * @return int value of String, or 0 if unparseable
176 private static int parseInt(String inString)
180 val = Integer.parseInt(inString);
182 catch (Exception e) {} // ignore, value stays zero
186 /** @return File from which config was loaded (or null) */
187 public static File getConfigFile()
193 * @return config Properties object to allow all config values to be saved
195 public static Properties getAllConfig()
197 // Update recently-used files
198 _configValues.setProperty(KEY_RECENT_FILES, _recentFiles.getConfigString());
199 return _configValues;
203 * @return the current colour scheme
205 public static ColourScheme getColourScheme()
207 return _colourScheme;
211 * @return list of recently used files
213 public static RecentFileList getRecentFileList()
219 * Store the given configuration setting
220 * @param inKey key (from constants)
221 * @param inValue value as string
223 public static void setConfigString(String inKey, String inValue)
225 if (inValue == null || inValue.equals("")) {
226 _configValues.remove(inKey);
229 _configValues.put(inKey, inValue);
234 * Store the given configuration setting
235 * @param inKey key (from constants)
236 * @param inValue value as boolean
238 public static void setConfigBoolean(String inKey, boolean inValue)
240 if (inKey != null && !inKey.equals(""))
242 _configValues.put(inKey, (inValue?"1":"0"));
247 * Store the given configuration setting
248 * @param inKey key (from constants)
249 * @param inValue value as int
251 public static void setConfigInt(String inKey, int inValue)
253 if (inKey != null && !inKey.equals(""))
255 _configValues.put(inKey, "" + inValue);
260 * Get the given configuration setting as a String
262 * @return configuration setting as a String
264 public static String getConfigString(String inKey)
266 return _configValues.getProperty(inKey);
270 * Get the given configuration setting as a boolean
272 * @return configuration setting as a boolean (default to true)
274 public static boolean getConfigBoolean(String inKey)
276 String val = _configValues.getProperty(inKey);
277 return (val == null || val.equals("1"));
281 * Get the given configuration setting as an int
283 * @return configuration setting as an int
285 public static int getConfigInt(String inKey)
287 return parseInt(_configValues.getProperty(inKey));
291 * Check whether the given key corresponds to a boolean property
292 * @param inKey key to check
293 * @return true if corresponding property is boolean
295 public static boolean isKeyBoolean(String inKey)
297 // Only one boolean key so far (after metric flag was removed)
298 return inKey != null && (
299 inKey.equals(KEY_SHOW_MAP));
303 * Update the colour scheme property from the current settings
305 public static void updateColourScheme()
307 setConfigString(KEY_COLOUR_SCHEME, _colourScheme.toString());
311 * @return the current unit set
313 public static UnitSet getUnitSet() {
317 public static void selectUnitSet(int inIndex)
319 _unitSet = UnitSetLibrary.getUnitSet(inIndex);
320 // Set name of set in config
321 setConfigString(KEY_UNITSET_KEY, _unitSet.getNameKey());