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 GPSBabel import file format */
49 public static final String KEY_IMPORT_FILE_FORMAT = "prune.lastimportfileformat";
50 /** Key for Povray font */
51 public static final String KEY_POVRAY_FONT = "prune.povrayfont";
52 /** Key for the selected unit set */
53 public static final String KEY_UNITSET_KEY = "prune.unitsetkey";
54 /** Key for index of map source */
55 public static final String KEY_MAPSOURCE_INDEX = "prune.mapsource";
56 /** Key for number of fixed map sources */
57 public static final String KEY_NUM_FIXED_MAPS = "prune.numfixedmapsources";
58 /** Key for String containing custom map sources */
59 public static final String KEY_MAPSOURCE_LIST = "prune.mapsourcelist";
60 /** Key for show map flag */
61 public static final String KEY_SHOW_MAP = "prune.showmap";
62 /** Key for path to disk cache */
63 public static final String KEY_DISK_CACHE = "prune.diskcache";
64 /** Key for working online flag */
65 public static final String KEY_ONLINE_MODE = "prune.onlinemode";
66 /** Key for width of thumbnails in kmz */
67 public static final String KEY_KMZ_IMAGE_SIZE = "prune.kmzimagewidth";
68 /** Key for gpsbabel path */
69 public static final String KEY_GPSBABEL_PATH = "prune.gpsbabelpath";
70 /** Key for gnuplot path */
71 public static final String KEY_GNUPLOT_PATH = "prune.gnuplotpath";
72 /** Key for exiftool path */
73 public static final String KEY_EXIFTOOL_PATH = "prune.exiftoolpath";
74 /** Key for colour scheme */
75 public static final String KEY_COLOUR_SCHEME = "prune.colourscheme";
76 /** Key for line width used for drawing */
77 public static final String KEY_LINE_WIDTH = "prune.linewidth";
78 /** Key for kml track colour */
79 public static final String KEY_KML_TRACK_COLOUR = "prune.kmltrackcolour";
80 /** Key for autosaving settings */
81 public static final String KEY_AUTOSAVE_SETTINGS = "prune.autosavesettings";
82 /** Key for recently used files */
83 public static final String KEY_RECENT_FILES = "prune.recentfiles";
84 /** Key for estimation parameters */
85 public static final String KEY_ESTIMATION_PARAMS = "prune.estimationparams";
86 /** Key for 3D exaggeration factor */
87 public static final String KEY_HEIGHT_EXAGGERATION = "prune.heightexaggeration";
90 /** Initialise the default properties */
93 _configValues = getDefaultProperties();
97 * Load the default configuration file
99 public static void loadDefaultFile()
101 if (DEFAULT_CONFIG_FILE.exists())
104 loadFile(DEFAULT_CONFIG_FILE);
107 catch (ConfigException ce) {} // ignore
109 if (HOME_CONFIG_FILE.exists())
112 loadFile(HOME_CONFIG_FILE);
114 catch (ConfigException ce) {} // ignore
120 * Load configuration from file
121 * @param inFile file to load
122 * @throws ConfigException if specified file couldn't be read
124 public static void loadFile(File inFile) throws ConfigException
126 // Start with default properties
127 Properties props = getDefaultProperties();
128 // Try to load the file into a properties object
129 boolean loadFailed = false;
130 FileInputStream fis = null;
133 fis = new FileInputStream(inFile);
136 catch (Exception e) {
140 if (fis != null) try {
143 catch (Exception e) {}
145 // Save all properties from file
146 _configValues.putAll(props);
147 _colourScheme.loadFromHex(_configValues.getProperty(KEY_COLOUR_SCHEME));
148 _recentFiles = new RecentFileList(_configValues.getProperty(KEY_RECENT_FILES));
149 _unitSet = UnitSetLibrary.getUnitSet(_configValues.getProperty(KEY_UNITSET_KEY));
150 // Adjust map source index if necessary
154 throw new ConfigException();
156 // Store location of successfully loaded config file
157 _configFile = inFile;
161 * @return Properties object containing default values
163 private static Properties getDefaultProperties()
165 Properties props = new Properties();
167 props.put(KEY_GPS_DEVICE, "usb:");
168 props.put(KEY_GPS_FORMAT, "garmin");
169 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
170 props.put(KEY_SHOW_MAP, "0"); // hide by default
171 props.put(KEY_EXIFTOOL_PATH, "exiftool");
172 props.put(KEY_GNUPLOT_PATH, "gnuplot");
173 props.put(KEY_GPSBABEL_PATH, "gpsbabel");
174 props.put(KEY_IMPORT_FILE_FORMAT, "-1"); // no file format selected
175 props.put(KEY_KMZ_IMAGE_SIZE, "240");
176 props.put(KEY_AUTOSAVE_SETTINGS, "0"); // autosave false by default
177 props.put(KEY_UNITSET_KEY, "unitset.kilometres"); // metric by default
178 props.put(KEY_HEIGHT_EXAGGERATION, "100"); // 100%, no exaggeration
183 * Adjust the index of the selected map
184 * (only required if config was loaded from a previous version of GpsPrune)
186 private static void adjustSelectedMap()
188 int sourceNum = getConfigInt(Config.KEY_MAPSOURCE_INDEX);
189 int prevNumFixed = getConfigInt(Config.KEY_NUM_FIXED_MAPS);
190 // Number of fixed maps not specified in version <=13, default to 6
191 if (prevNumFixed == 0) prevNumFixed = 6;
192 int currNumFixed = MapSourceLibrary.getNumFixedSources();
193 // Only need to do something if the number has changed
194 if (currNumFixed != prevNumFixed && (sourceNum >= prevNumFixed || sourceNum >= currNumFixed))
196 sourceNum += (currNumFixed - prevNumFixed);
197 setConfigInt(Config.KEY_MAPSOURCE_INDEX, sourceNum);
199 setConfigInt(Config.KEY_NUM_FIXED_MAPS, currNumFixed);
203 * @param inString String to parse
204 * @return int value of String, or 0 if unparseable
206 private static int parseInt(String inString)
210 val = Integer.parseInt(inString);
212 catch (Exception e) {} // ignore, value stays zero
216 /** @return File from which config was loaded (or null) */
217 public static File getConfigFile()
223 * @return config Properties object to allow all config values to be saved
225 public static Properties getAllConfig()
227 // Update recently-used files
228 _configValues.setProperty(KEY_RECENT_FILES, _recentFiles.getConfigString());
229 return _configValues;
233 * @return the current colour scheme
235 public static ColourScheme getColourScheme()
237 return _colourScheme;
241 * @return list of recently used files
243 public static RecentFileList getRecentFileList()
249 * Store the given configuration setting
250 * @param inKey key (from constants)
251 * @param inValue value as string
253 public static void setConfigString(String inKey, String inValue)
255 if (inValue == null || inValue.equals("")) {
256 _configValues.remove(inKey);
259 _configValues.put(inKey, inValue);
264 * Store the given configuration setting
265 * @param inKey key (from constants)
266 * @param inValue value as boolean
268 public static void setConfigBoolean(String inKey, boolean inValue)
270 if (inKey != null && !inKey.equals(""))
272 _configValues.put(inKey, (inValue?"1":"0"));
277 * Store the given configuration setting
278 * @param inKey key (from constants)
279 * @param inValue value as int
281 public static void setConfigInt(String inKey, int inValue)
283 if (inKey != null && !inKey.equals(""))
285 _configValues.put(inKey, "" + inValue);
290 * Get the given configuration setting as a String
292 * @return configuration setting as a String
294 public static String getConfigString(String inKey)
296 return _configValues.getProperty(inKey);
300 * Get the given configuration setting as a boolean
302 * @return configuration setting as a boolean (default to true)
304 public static boolean getConfigBoolean(String inKey)
306 String val = _configValues.getProperty(inKey);
307 return (val == null || val.equals("1"));
311 * Get the given configuration setting as an int
313 * @return configuration setting as an int
315 public static int getConfigInt(String inKey)
317 return parseInt(_configValues.getProperty(inKey));
321 * Check whether the given key corresponds to a boolean property
322 * @param inKey key to check
323 * @return true if corresponding property is boolean
325 public static boolean isKeyBoolean(String inKey)
327 // Only one boolean key so far (after metric flag was removed)
328 return inKey != null && (
329 inKey.equals(KEY_SHOW_MAP));
333 * Update the colour scheme property from the current settings
335 public static void updateColourScheme()
337 setConfigString(KEY_COLOUR_SCHEME, _colourScheme.toString());
341 * @return the current unit set
343 public static UnitSet getUnitSet() {
347 public static void selectUnitSet(int inIndex)
349 _unitSet = UnitSetLibrary.getUnitSet(inIndex);
350 // Set name of set in config
351 setConfigString(KEY_UNITSET_KEY, _unitSet.getNameKey());