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