]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/config/Config.java
Version 16, February 2014
[GpsPrune.git] / tim / prune / config / Config.java
1 package tim.prune.config;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.util.Properties;
6
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;
11
12
13 /**
14  * Abstract class to hold application-wide configuration
15  */
16 public abstract class Config
17 {
18         /** File from which Config was loaded */
19         private static File _configFile = null;
20
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);
29
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");
33
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";
86
87
88         /** Initialise the default properties */
89         static
90         {
91                 _configValues = getDefaultProperties();
92         }
93
94         /**
95          * Load the default configuration file
96          */
97         public static void loadDefaultFile()
98         {
99                 if (DEFAULT_CONFIG_FILE.exists())
100                 {
101                         try {
102                                 loadFile(DEFAULT_CONFIG_FILE);
103                                 return;
104                         }
105                         catch (ConfigException ce) {} // ignore
106                 }
107                 if (HOME_CONFIG_FILE.exists())
108                 {
109                         try {
110                                 loadFile(HOME_CONFIG_FILE);
111                         }
112                         catch (ConfigException ce) {} // ignore
113                 }
114         }
115
116
117         /**
118          * Load configuration from file
119          * @param inFile file to load
120          * @throws ConfigException if specified file couldn't be read
121          */
122         public static void loadFile(File inFile) throws ConfigException
123         {
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;
129                 try
130                 {
131                         fis = new FileInputStream(inFile);
132                         props.load(fis);
133                 }
134                 catch (Exception e) {
135                         loadFailed = true;
136                 }
137                 finally {
138                         if (fis != null) try {
139                                 fis.close();
140                         }
141                         catch (Exception e) {}
142                 }
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
149                 adjustSelectedMap();
150
151                 if (loadFailed) {
152                         throw new ConfigException();
153                 }
154                 // Store location of successfully loaded config file
155                 _configFile = inFile;
156         }
157
158         /**
159          * @return Properties object containing default values
160          */
161         private static Properties getDefaultProperties()
162         {
163                 Properties props = new Properties();
164                 // Fill in defaults
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
176                 return props;
177         }
178
179         /**
180          * Adjust the index of the selected map
181          * (only required if config was loaded from a previous version of GpsPrune)
182          */
183         private static void adjustSelectedMap()
184         {
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))
192                 {
193                         sourceNum += (currNumFixed - prevNumFixed);
194                         setConfigInt(Config.KEY_MAPSOURCE_INDEX, sourceNum);
195                 }
196                 setConfigInt(Config.KEY_NUM_FIXED_MAPS, currNumFixed);
197         }
198
199         /**
200          * @param inString String to parse
201          * @return int value of String, or 0 if unparseable
202          */
203         private static int parseInt(String inString)
204         {
205                 int val = 0;
206                 try {
207                         val = Integer.parseInt(inString);
208                 }
209                 catch (Exception e) {} // ignore, value stays zero
210                 return val;
211         }
212
213         /** @return File from which config was loaded (or null) */
214         public static File getConfigFile()
215         {
216                 return _configFile;
217         }
218
219         /**
220          * @return config Properties object to allow all config values to be saved
221          */
222         public static Properties getAllConfig()
223         {
224                 // Update recently-used files
225                 _configValues.setProperty(KEY_RECENT_FILES, _recentFiles.getConfigString());
226                 return _configValues;
227         }
228
229         /**
230          * @return the current colour scheme
231          */
232         public static ColourScheme getColourScheme()
233         {
234                 return _colourScheme;
235         }
236
237         /**
238          * @return list of recently used files
239          */
240         public static RecentFileList getRecentFileList()
241         {
242                 return _recentFiles;
243         }
244
245         /**
246          * Store the given configuration setting
247          * @param inKey key (from constants)
248          * @param inValue value as string
249          */
250         public static void setConfigString(String inKey, String inValue)
251         {
252                 if (inValue == null || inValue.equals("")) {
253                         _configValues.remove(inKey);
254                 }
255                 else {
256                         _configValues.put(inKey, inValue);
257                 }
258         }
259
260         /**
261          * Store the given configuration setting
262          * @param inKey key (from constants)
263          * @param inValue value as boolean
264          */
265         public static void setConfigBoolean(String inKey, boolean inValue)
266         {
267                 if (inKey != null && !inKey.equals(""))
268                 {
269                         _configValues.put(inKey, (inValue?"1":"0"));
270                 }
271         }
272
273         /**
274          * Store the given configuration setting
275          * @param inKey key (from constants)
276          * @param inValue value as int
277          */
278         public static void setConfigInt(String inKey, int inValue)
279         {
280                 if (inKey != null && !inKey.equals(""))
281                 {
282                         _configValues.put(inKey, "" + inValue);
283                 }
284         }
285
286         /**
287          * Get the given configuration setting as a String
288          * @param inKey key
289          * @return configuration setting as a String
290          */
291         public static String getConfigString(String inKey)
292         {
293                 return _configValues.getProperty(inKey);
294         }
295
296         /**
297          * Get the given configuration setting as a boolean
298          * @param inKey key
299          * @return configuration setting as a boolean (default to true)
300          */
301         public static boolean getConfigBoolean(String inKey)
302         {
303                 String val = _configValues.getProperty(inKey);
304                 return (val == null || val.equals("1"));
305         }
306
307         /**
308          * Get the given configuration setting as an int
309          * @param inKey key
310          * @return configuration setting as an int
311          */
312         public static int getConfigInt(String inKey)
313         {
314                 return parseInt(_configValues.getProperty(inKey));
315         }
316
317         /**
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
321          */
322         public static boolean isKeyBoolean(String inKey)
323         {
324                 // Only one boolean key so far (after metric flag was removed)
325                 return inKey != null && (
326                         inKey.equals(KEY_SHOW_MAP));
327         }
328
329         /**
330          * Update the colour scheme property from the current settings
331          */
332         public static void updateColourScheme()
333         {
334                 setConfigString(KEY_COLOUR_SCHEME, _colourScheme.toString());
335         }
336
337         /**
338          * @return the current unit set
339          */
340         public static UnitSet getUnitSet() {
341                 return _unitSet;
342         }
343
344         public static void selectUnitSet(int inIndex)
345         {
346                 _unitSet = UnitSetLibrary.getUnitSet(inIndex);
347                 // Set name of set in config
348                 setConfigString(KEY_UNITSET_KEY, _unitSet.getNameKey());
349         }
350 }