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 = null;
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 number of fixed map sources */
48 public static final String KEY_NUM_FIXED_MAPS = "prune.numfixedmapsources";
49 /** Key for String containing custom map sources */
50 public static final String KEY_MAPSOURCE_LIST = "prune.mapsourcelist";
51 /** Key for show map flag */
52 public static final String KEY_SHOW_MAP = "prune.showmap";
53 /** Key for path to disk cache */
54 public static final String KEY_DISK_CACHE = "prune.diskcache";
55 /** Key for working online flag */
56 public static final String KEY_ONLINE_MODE = "prune.onlinemode";
57 /** Key for width of thumbnails in kmz */
58 public static final String KEY_KMZ_IMAGE_WIDTH = "prune.kmzimagewidth";
59 /** Key for height of thumbnails in kmz */
60 public static final String KEY_KMZ_IMAGE_HEIGHT = "prune.kmzimageheight";
61 /** Key for gpsbabel path */
62 public static final String KEY_GPSBABEL_PATH = "prune.gpsbabelpath";
63 /** Key for gnuplot path */
64 public static final String KEY_GNUPLOT_PATH = "prune.gnuplotpath";
65 /** Key for exiftool path */
66 public static final String KEY_EXIFTOOL_PATH = "prune.exiftoolpath";
67 /** Key for colour scheme */
68 public static final String KEY_COLOUR_SCHEME = "prune.colourscheme";
69 /** Key for line width used for drawing */
70 public static final String KEY_LINE_WIDTH = "prune.linewidth";
71 /** Key for kml track colour */
72 public static final String KEY_KML_TRACK_COLOUR = "prune.kmltrackcolour";
73 /** Key for autosaving settings */
74 public static final String KEY_AUTOSAVE_SETTINGS = "prune.autosavesettings";
75 /** Key for recently used files */
76 public static final String KEY_RECENT_FILES = "prune.recentfiles";
79 /** Initialise the default properties */
82 _configValues = getDefaultProperties();
86 * Load the default configuration file
88 public static void loadDefaultFile()
90 if (DEFAULT_CONFIG_FILE.exists())
93 loadFile(DEFAULT_CONFIG_FILE);
96 catch (ConfigException ce) {} // ignore
98 if (HOME_CONFIG_FILE.exists())
101 loadFile(HOME_CONFIG_FILE);
103 catch (ConfigException ce) {} // ignore
109 * Load configuration from file
110 * @param inFile file to load
111 * @throws ConfigException if specified file couldn't be read
113 public static void loadFile(File inFile) throws ConfigException
115 // Start with default properties
116 Properties props = getDefaultProperties();
117 // Try to load the file into a properties object
118 boolean loadFailed = false;
119 FileInputStream fis = null;
122 fis = new FileInputStream(inFile);
125 catch (Exception e) {
129 if (fis != null) try {
132 catch (Exception e) {}
134 // Save all properties from file
135 _configValues.putAll(props);
136 _colourScheme.loadFromHex(_configValues.getProperty(KEY_COLOUR_SCHEME));
137 _recentFiles = new RecentFileList(_configValues.getProperty(KEY_RECENT_FILES));
139 throw new ConfigException();
141 // Store location of successfully loaded config file
142 _configFile = inFile;
146 * @return Properties object containing default values
148 private static Properties getDefaultProperties()
150 Properties props = new Properties();
152 props.put(KEY_GPS_DEVICE, "usb:");
153 props.put(KEY_GPS_FORMAT, "garmin");
154 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
155 props.put(KEY_SHOW_MAP, "0"); // hide by default
156 props.put(KEY_EXIFTOOL_PATH, "exiftool");
157 props.put(KEY_GNUPLOT_PATH, "gnuplot");
158 props.put(KEY_GPSBABEL_PATH, "gpsbabel");
159 props.put(KEY_KMZ_IMAGE_WIDTH, "240");
160 props.put(KEY_KMZ_IMAGE_HEIGHT, "240");
161 props.put(KEY_AUTOSAVE_SETTINGS, "0"); // autosave false by default
166 * @param inString String to parse
167 * @return int value of String, or 0 if unparseable
169 private static int parseInt(String inString)
173 val = Integer.parseInt(inString);
175 catch (Exception e) {} // ignore, value stays zero
179 /** @return File from which config was loaded (or null) */
180 public static File getConfigFile()
186 * @return config Properties object to allow all config values to be saved
188 public static Properties getAllConfig()
190 // Update recently-used files
191 _configValues.setProperty(KEY_RECENT_FILES, _recentFiles.getConfigString());
192 return _configValues;
196 * @return the current colour scheme
198 public static ColourScheme getColourScheme()
200 return _colourScheme;
204 * @return list of recently used files
206 public static RecentFileList getRecentFileList()
212 * Store the given configuration setting
213 * @param inKey key (from constants)
214 * @param inValue value as string
216 public static void setConfigString(String inKey, String inValue)
218 if (inValue == null || inValue.equals("")) {
219 _configValues.remove(inKey);
222 _configValues.put(inKey, inValue);
227 * Store the given configuration setting
228 * @param inKey key (from constants)
229 * @param inValue value as boolean
231 public static void setConfigBoolean(String inKey, boolean inValue)
233 if (inKey != null && !inKey.equals(""))
235 _configValues.put(inKey, (inValue?"1":"0"));
240 * Store the given configuration setting
241 * @param inKey key (from constants)
242 * @param inValue value as int
244 public static void setConfigInt(String inKey, int inValue)
246 if (inKey != null && !inKey.equals(""))
248 _configValues.put(inKey, "" + inValue);
253 * Get the given configuration setting as a String
255 * @return configuration setting as a String
257 public static String getConfigString(String inKey)
259 return _configValues.getProperty(inKey);
263 * Get the given configuration setting as a boolean
265 * @return configuration setting as a boolean (default to true)
267 public static boolean getConfigBoolean(String inKey)
269 String val = _configValues.getProperty(inKey);
270 return (val == null || val.equals("1"));
274 * Get the given configuration setting as an int
276 * @return configuration setting as an int
278 public static int getConfigInt(String inKey)
280 return parseInt(_configValues.getProperty(inKey));
284 * Check whether the given key corresponds to a boolean property
285 * @param inKey key to check
286 * @return true if corresponding property is boolean
288 public static boolean isKeyBoolean(String inKey)
290 // Only two boolean keys so far
291 return inKey != null && (
292 inKey.equals(KEY_METRIC_UNITS) || inKey.equals(KEY_SHOW_MAP));
296 * Update the colour scheme property from the current settings
298 public static void updateColourScheme()
300 setConfigString(KEY_COLOUR_SCHEME, _colourScheme.toString());