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 GPSBabel filter string */
46 public static final String KEY_GPSBABEL_FILTER = "prune.gpsbabelfilter";
47 /** Key for Povray font */
48 public static final String KEY_POVRAY_FONT = "prune.povrayfont";
49 /** Key for the selected unit set */
50 public static final String KEY_UNITSET_KEY = "prune.unitsetkey";
51 /** Key for index of map source */
52 public static final String KEY_MAPSOURCE_INDEX = "prune.mapsource";
53 /** Key for number of fixed map sources */
54 public static final String KEY_NUM_FIXED_MAPS = "prune.numfixedmapsources";
55 /** Key for String containing custom map sources */
56 public static final String KEY_MAPSOURCE_LIST = "prune.mapsourcelist";
57 /** Key for show map flag */
58 public static final String KEY_SHOW_MAP = "prune.showmap";
59 /** Key for path to disk cache */
60 public static final String KEY_DISK_CACHE = "prune.diskcache";
61 /** Key for working online flag */
62 public static final String KEY_ONLINE_MODE = "prune.onlinemode";
63 /** Key for width of thumbnails in kmz */
64 public static final String KEY_KMZ_IMAGE_SIZE = "prune.kmzimagewidth";
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";
81 /** Key for estimation parameters */
82 public static final String KEY_ESTIMATION_PARAMS = "prune.estimationparams";
85 /** Initialise the default properties */
88 _configValues = getDefaultProperties();
92 * Load the default configuration file
94 public static void loadDefaultFile()
96 if (DEFAULT_CONFIG_FILE.exists())
99 loadFile(DEFAULT_CONFIG_FILE);
102 catch (ConfigException ce) {} // ignore
104 if (HOME_CONFIG_FILE.exists())
107 loadFile(HOME_CONFIG_FILE);
109 catch (ConfigException ce) {} // ignore
115 * Load configuration from file
116 * @param inFile file to load
117 * @throws ConfigException if specified file couldn't be read
119 public static void loadFile(File inFile) throws ConfigException
121 // Start with default properties
122 Properties props = getDefaultProperties();
123 // Try to load the file into a properties object
124 boolean loadFailed = false;
125 FileInputStream fis = null;
128 fis = new FileInputStream(inFile);
131 catch (Exception e) {
135 if (fis != null) try {
138 catch (Exception e) {}
140 // Save all properties from file
141 _configValues.putAll(props);
142 _colourScheme.loadFromHex(_configValues.getProperty(KEY_COLOUR_SCHEME));
143 _recentFiles = new RecentFileList(_configValues.getProperty(KEY_RECENT_FILES));
144 _unitSet = UnitSetLibrary.getUnitSet(_configValues.getProperty(KEY_UNITSET_KEY));
147 throw new ConfigException();
149 // Store location of successfully loaded config file
150 _configFile = inFile;
154 * @return Properties object containing default values
156 private static Properties getDefaultProperties()
158 Properties props = new Properties();
160 props.put(KEY_GPS_DEVICE, "usb:");
161 props.put(KEY_GPS_FORMAT, "garmin");
162 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
163 props.put(KEY_SHOW_MAP, "0"); // hide by default
164 props.put(KEY_EXIFTOOL_PATH, "exiftool");
165 props.put(KEY_GNUPLOT_PATH, "gnuplot");
166 props.put(KEY_GPSBABEL_PATH, "gpsbabel");
167 props.put(KEY_KMZ_IMAGE_SIZE, "240");
168 props.put(KEY_AUTOSAVE_SETTINGS, "0"); // autosave false by default
169 props.put(KEY_UNITSET_KEY, "unitset.kilometres"); // metric by default
174 * @param inString String to parse
175 * @return int value of String, or 0 if unparseable
177 private static int parseInt(String inString)
181 val = Integer.parseInt(inString);
183 catch (Exception e) {} // ignore, value stays zero
187 /** @return File from which config was loaded (or null) */
188 public static File getConfigFile()
194 * @return config Properties object to allow all config values to be saved
196 public static Properties getAllConfig()
198 // Update recently-used files
199 _configValues.setProperty(KEY_RECENT_FILES, _recentFiles.getConfigString());
200 return _configValues;
204 * @return the current colour scheme
206 public static ColourScheme getColourScheme()
208 return _colourScheme;
212 * @return list of recently used files
214 public static RecentFileList getRecentFileList()
220 * Store the given configuration setting
221 * @param inKey key (from constants)
222 * @param inValue value as string
224 public static void setConfigString(String inKey, String inValue)
226 if (inValue == null || inValue.equals("")) {
227 _configValues.remove(inKey);
230 _configValues.put(inKey, inValue);
235 * Store the given configuration setting
236 * @param inKey key (from constants)
237 * @param inValue value as boolean
239 public static void setConfigBoolean(String inKey, boolean inValue)
241 if (inKey != null && !inKey.equals(""))
243 _configValues.put(inKey, (inValue?"1":"0"));
248 * Store the given configuration setting
249 * @param inKey key (from constants)
250 * @param inValue value as int
252 public static void setConfigInt(String inKey, int inValue)
254 if (inKey != null && !inKey.equals(""))
256 _configValues.put(inKey, "" + inValue);
261 * Get the given configuration setting as a String
263 * @return configuration setting as a String
265 public static String getConfigString(String inKey)
267 return _configValues.getProperty(inKey);
271 * Get the given configuration setting as a boolean
273 * @return configuration setting as a boolean (default to true)
275 public static boolean getConfigBoolean(String inKey)
277 String val = _configValues.getProperty(inKey);
278 return (val == null || val.equals("1"));
282 * Get the given configuration setting as an int
284 * @return configuration setting as an int
286 public static int getConfigInt(String inKey)
288 return parseInt(_configValues.getProperty(inKey));
292 * Check whether the given key corresponds to a boolean property
293 * @param inKey key to check
294 * @return true if corresponding property is boolean
296 public static boolean isKeyBoolean(String inKey)
298 // Only one boolean key so far (after metric flag was removed)
299 return inKey != null && (
300 inKey.equals(KEY_SHOW_MAP));
304 * Update the colour scheme property from the current settings
306 public static void updateColourScheme()
308 setConfigString(KEY_COLOUR_SCHEME, _colourScheme.toString());
312 * @return the current unit set
314 public static UnitSet getUnitSet() {
318 public static void selectUnitSet(int inIndex)
320 _unitSet = UnitSetLibrary.getUnitSet(inIndex);
321 // Set name of set in config
322 setConfigString(KEY_UNITSET_KEY, _unitSet.getNameKey());