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;
10 import tim.prune.gui.map.MapSourceLibrary;
14 * Abstract class to hold application-wide configuration
16 public abstract class Config
18 /** File from which Config was loaded */
19 private static File _configFile = null;
21 /** Hashtable containing all config values */
22 private static Properties _configValues = null;
23 /** Colour scheme object is also part of config */
24 private static ColourScheme _colourScheme = new ColourScheme();
25 /** Recently-used file list */
26 private static RecentFileList _recentFiles = new RecentFileList();
27 /** Current unit set */
28 private static UnitSet _unitSet = UnitSetLibrary.getUnitSet(null);
30 /** Default config file */
31 public static final File DEFAULT_CONFIG_FILE = new File(".pruneconfig");
32 public static final File HOME_CONFIG_FILE = new File(System.getProperty("user.home"), ".pruneconfig");
34 /** Key for track directory */
35 public static final String KEY_TRACK_DIR = "prune.trackdirectory";
36 /** Key for photo directory */
37 public static final String KEY_PHOTO_DIR = "prune.photodirectory";
38 /** Key for language code */
39 public static final String KEY_LANGUAGE_CODE = "prune.languagecode";
40 /** Key for language file */
41 public static final String KEY_LANGUAGE_FILE = "prune.languagefile";
42 /** Key for GPS device */
43 public static final String KEY_GPS_DEVICE = "prune.gpsdevice";
44 /** Key for GPS format */
45 public static final String KEY_GPS_FORMAT = "prune.gpsformat";
46 /** Key for GPSBabel filter string */
47 public static final String KEY_GPSBABEL_FILTER = "prune.gpsbabelfilter";
48 /** Key for Povray font */
49 public static final String KEY_POVRAY_FONT = "prune.povrayfont";
50 /** Key for the selected unit set */
51 public static final String KEY_UNITSET_KEY = "prune.unitsetkey";
52 /** Key for index of map source */
53 public static final String KEY_MAPSOURCE_INDEX = "prune.mapsource";
54 /** Key for number of fixed map sources */
55 public static final String KEY_NUM_FIXED_MAPS = "prune.numfixedmapsources";
56 /** Key for String containing custom map sources */
57 public static final String KEY_MAPSOURCE_LIST = "prune.mapsourcelist";
58 /** Key for show map flag */
59 public static final String KEY_SHOW_MAP = "prune.showmap";
60 /** Key for path to disk cache */
61 public static final String KEY_DISK_CACHE = "prune.diskcache";
62 /** Key for working online flag */
63 public static final String KEY_ONLINE_MODE = "prune.onlinemode";
64 /** Key for width of thumbnails in kmz */
65 public static final String KEY_KMZ_IMAGE_SIZE = "prune.kmzimagewidth";
66 /** Key for gpsbabel path */
67 public static final String KEY_GPSBABEL_PATH = "prune.gpsbabelpath";
68 /** Key for gnuplot path */
69 public static final String KEY_GNUPLOT_PATH = "prune.gnuplotpath";
70 /** Key for exiftool path */
71 public static final String KEY_EXIFTOOL_PATH = "prune.exiftoolpath";
72 /** Key for colour scheme */
73 public static final String KEY_COLOUR_SCHEME = "prune.colourscheme";
74 /** Key for line width used for drawing */
75 public static final String KEY_LINE_WIDTH = "prune.linewidth";
76 /** Key for kml track colour */
77 public static final String KEY_KML_TRACK_COLOUR = "prune.kmltrackcolour";
78 /** Key for autosaving settings */
79 public static final String KEY_AUTOSAVE_SETTINGS = "prune.autosavesettings";
80 /** Key for recently used files */
81 public static final String KEY_RECENT_FILES = "prune.recentfiles";
82 /** Key for estimation parameters */
83 public static final String KEY_ESTIMATION_PARAMS = "prune.estimationparams";
84 /** Key for 3D exaggeration factor */
85 public static final String KEY_HEIGHT_EXAGGERATION = "prune.heightexaggeration";
88 /** Initialise the default properties */
91 _configValues = getDefaultProperties();
95 * Load the default configuration file
97 public static void loadDefaultFile()
99 if (DEFAULT_CONFIG_FILE.exists())
102 loadFile(DEFAULT_CONFIG_FILE);
105 catch (ConfigException ce) {} // ignore
107 if (HOME_CONFIG_FILE.exists())
110 loadFile(HOME_CONFIG_FILE);
112 catch (ConfigException ce) {} // ignore
118 * Load configuration from file
119 * @param inFile file to load
120 * @throws ConfigException if specified file couldn't be read
122 public static void loadFile(File inFile) throws ConfigException
124 // Start with default properties
125 Properties props = getDefaultProperties();
126 // Try to load the file into a properties object
127 boolean loadFailed = false;
128 FileInputStream fis = null;
131 fis = new FileInputStream(inFile);
134 catch (Exception e) {
138 if (fis != null) try {
141 catch (Exception e) {}
143 // Save all properties from file
144 _configValues.putAll(props);
145 _colourScheme.loadFromHex(_configValues.getProperty(KEY_COLOUR_SCHEME));
146 _recentFiles = new RecentFileList(_configValues.getProperty(KEY_RECENT_FILES));
147 _unitSet = UnitSetLibrary.getUnitSet(_configValues.getProperty(KEY_UNITSET_KEY));
148 // Adjust map source index if necessary
152 throw new ConfigException();
154 // Store location of successfully loaded config file
155 _configFile = inFile;
159 * @return Properties object containing default values
161 private static Properties getDefaultProperties()
163 Properties props = new Properties();
165 props.put(KEY_GPS_DEVICE, "usb:");
166 props.put(KEY_GPS_FORMAT, "garmin");
167 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
168 props.put(KEY_SHOW_MAP, "0"); // hide by default
169 props.put(KEY_EXIFTOOL_PATH, "exiftool");
170 props.put(KEY_GNUPLOT_PATH, "gnuplot");
171 props.put(KEY_GPSBABEL_PATH, "gpsbabel");
172 props.put(KEY_KMZ_IMAGE_SIZE, "240");
173 props.put(KEY_AUTOSAVE_SETTINGS, "0"); // autosave false by default
174 props.put(KEY_UNITSET_KEY, "unitset.kilometres"); // metric by default
175 props.put(KEY_HEIGHT_EXAGGERATION, "100"); // 100%, no exaggeration
180 * Adjust the index of the selected map
181 * (only required if config was loaded from a previous version of GpsPrune)
183 private static void adjustSelectedMap()
185 int sourceNum = getConfigInt(Config.KEY_MAPSOURCE_INDEX);
186 int prevNumFixed = getConfigInt(Config.KEY_NUM_FIXED_MAPS);
187 // Number of fixed maps not specified in version <=13, default to 6
188 if (prevNumFixed == 0) prevNumFixed = 6;
189 int currNumFixed = MapSourceLibrary.getNumFixedSources();
190 // Only need to do something if the number has changed
191 if (currNumFixed != prevNumFixed && (sourceNum >= prevNumFixed || sourceNum >= currNumFixed))
193 sourceNum += (currNumFixed - prevNumFixed);
194 setConfigInt(Config.KEY_MAPSOURCE_INDEX, sourceNum);
196 setConfigInt(Config.KEY_NUM_FIXED_MAPS, currNumFixed);
200 * @param inString String to parse
201 * @return int value of String, or 0 if unparseable
203 private static int parseInt(String inString)
207 val = Integer.parseInt(inString);
209 catch (Exception e) {} // ignore, value stays zero
213 /** @return File from which config was loaded (or null) */
214 public static File getConfigFile()
220 * @return config Properties object to allow all config values to be saved
222 public static Properties getAllConfig()
224 // Update recently-used files
225 _configValues.setProperty(KEY_RECENT_FILES, _recentFiles.getConfigString());
226 return _configValues;
230 * @return the current colour scheme
232 public static ColourScheme getColourScheme()
234 return _colourScheme;
238 * @return list of recently used files
240 public static RecentFileList getRecentFileList()
246 * Store the given configuration setting
247 * @param inKey key (from constants)
248 * @param inValue value as string
250 public static void setConfigString(String inKey, String inValue)
252 if (inValue == null || inValue.equals("")) {
253 _configValues.remove(inKey);
256 _configValues.put(inKey, inValue);
261 * Store the given configuration setting
262 * @param inKey key (from constants)
263 * @param inValue value as boolean
265 public static void setConfigBoolean(String inKey, boolean inValue)
267 if (inKey != null && !inKey.equals(""))
269 _configValues.put(inKey, (inValue?"1":"0"));
274 * Store the given configuration setting
275 * @param inKey key (from constants)
276 * @param inValue value as int
278 public static void setConfigInt(String inKey, int inValue)
280 if (inKey != null && !inKey.equals(""))
282 _configValues.put(inKey, "" + inValue);
287 * Get the given configuration setting as a String
289 * @return configuration setting as a String
291 public static String getConfigString(String inKey)
293 return _configValues.getProperty(inKey);
297 * Get the given configuration setting as a boolean
299 * @return configuration setting as a boolean (default to true)
301 public static boolean getConfigBoolean(String inKey)
303 String val = _configValues.getProperty(inKey);
304 return (val == null || val.equals("1"));
308 * Get the given configuration setting as an int
310 * @return configuration setting as an int
312 public static int getConfigInt(String inKey)
314 return parseInt(_configValues.getProperty(inKey));
318 * Check whether the given key corresponds to a boolean property
319 * @param inKey key to check
320 * @return true if corresponding property is boolean
322 public static boolean isKeyBoolean(String inKey)
324 // Only one boolean key so far (after metric flag was removed)
325 return inKey != null && (
326 inKey.equals(KEY_SHOW_MAP));
330 * Update the colour scheme property from the current settings
332 public static void updateColourScheme()
334 setConfigString(KEY_COLOUR_SCHEME, _colourScheme.toString());
338 * @return the current unit set
340 public static UnitSet getUnitSet() {
344 public static void selectUnitSet(int inIndex)
346 _unitSet = UnitSetLibrary.getUnitSet(inIndex);
347 // Set name of set in config
348 setConfigString(KEY_UNITSET_KEY, _unitSet.getNameKey());