]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/config/Config.java
385a5d9f12dc64d387711cf54abc4fbbb5644633
[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
11
12 /**
13  * Abstract class to hold application-wide configuration
14  */
15 public abstract class Config
16 {
17         /** File from which Config was loaded */
18         private static File _configFile = null;
19
20         /** Hashtable containing all config values */
21         private static Properties _configValues = null;
22         /** Colour scheme object is also part of config */
23         private static ColourScheme _colourScheme = new ColourScheme();
24         /** Recently-used file list */
25         private static RecentFileList _recentFiles = new RecentFileList();
26         /** Current unit set */
27         private static UnitSet _unitSet = UnitSetLibrary.getUnitSet(null);
28
29         /** Default config file */
30         public static final File DEFAULT_CONFIG_FILE = new File(".pruneconfig");
31         public static final File HOME_CONFIG_FILE = new File(System.getProperty("user.home"), ".pruneconfig");
32
33         /** Key for track directory */
34         public static final String KEY_TRACK_DIR = "prune.trackdirectory";
35         /** Key for photo directory */
36         public static final String KEY_PHOTO_DIR = "prune.photodirectory";
37         /** Key for language code */
38         public static final String KEY_LANGUAGE_CODE = "prune.languagecode";
39         /** Key for language file */
40         public static final String KEY_LANGUAGE_FILE = "prune.languagefile";
41         /** Key for GPS device */
42         public static final String KEY_GPS_DEVICE = "prune.gpsdevice";
43         /** Key for GPS format */
44         public static final String KEY_GPS_FORMAT = "prune.gpsformat";
45         /** Key for GPSBabel filter string */
46         public static final String KEY_GPSBABEL_FILTER = "prune.gpsbabelfilter";
47         /** Key for Povray font */
48         public static final String KEY_POVRAY_FONT = "prune.povrayfont";
49         /** Key for the selected unit set */
50         public static final String KEY_UNITSET_KEY  = "prune.unitsetkey";
51         /** Key for index of map source */
52         public static final String KEY_MAPSOURCE_INDEX = "prune.mapsource";
53         /** Key for number of fixed map sources */
54         public static final String KEY_NUM_FIXED_MAPS = "prune.numfixedmapsources";
55         /** Key for String containing custom map sources */
56         public static final String KEY_MAPSOURCE_LIST = "prune.mapsourcelist";
57         /** Key for show map flag */
58         public static final String KEY_SHOW_MAP = "prune.showmap";
59         /** Key for path to disk cache */
60         public static final String KEY_DISK_CACHE = "prune.diskcache";
61         /** Key for working online flag */
62         public static final String KEY_ONLINE_MODE = "prune.onlinemode";
63         /** Key for width of thumbnails in kmz */
64         public static final String KEY_KMZ_IMAGE_SIZE = "prune.kmzimagewidth";
65         /** Key for gpsbabel path */
66         public static final String KEY_GPSBABEL_PATH = "prune.gpsbabelpath";
67         /** Key for gnuplot path */
68         public static final String KEY_GNUPLOT_PATH = "prune.gnuplotpath";
69         /** Key for exiftool path */
70         public static final String KEY_EXIFTOOL_PATH = "prune.exiftoolpath";
71         /** Key for colour scheme */
72         public static final String KEY_COLOUR_SCHEME = "prune.colourscheme";
73         /** Key for line width used for drawing */
74         public static final String KEY_LINE_WIDTH = "prune.linewidth";
75         /** Key for kml track colour */
76         public static final String KEY_KML_TRACK_COLOUR = "prune.kmltrackcolour";
77         /** Key for autosaving settings */
78         public static final String KEY_AUTOSAVE_SETTINGS = "prune.autosavesettings";
79         /** Key for recently used files */
80         public static final String KEY_RECENT_FILES = "prune.recentfiles";
81         /** Key for estimation parameters */
82         public static final String KEY_ESTIMATION_PARAMS = "prune.estimationparams";
83
84
85         /** Initialise the default properties */
86         static
87         {
88                 _configValues = getDefaultProperties();
89         }
90
91         /**
92          * Load the default configuration file
93          */
94         public static void loadDefaultFile()
95         {
96                 if (DEFAULT_CONFIG_FILE.exists())
97                 {
98                         try {
99                                 loadFile(DEFAULT_CONFIG_FILE);
100                                 return;
101                         }
102                         catch (ConfigException ce) {} // ignore
103                 }
104                 if (HOME_CONFIG_FILE.exists())
105                 {
106                         try {
107                                 loadFile(HOME_CONFIG_FILE);
108                         }
109                         catch (ConfigException ce) {} // ignore
110                 }
111         }
112
113
114         /**
115          * Load configuration from file
116          * @param inFile file to load
117          * @throws ConfigException if specified file couldn't be read
118          */
119         public static void loadFile(File inFile) throws ConfigException
120         {
121                 // Start with default properties
122                 Properties props = getDefaultProperties();
123                 // Try to load the file into a properties object
124                 boolean loadFailed = false;
125                 FileInputStream fis = null;
126                 try
127                 {
128                         fis = new FileInputStream(inFile);
129                         props.load(fis);
130                 }
131                 catch (Exception e) {
132                         loadFailed = true;
133                 }
134                 finally {
135                         if (fis != null) try {
136                                 fis.close();
137                         }
138                         catch (Exception e) {}
139                 }
140                 // Save all properties from file
141                 _configValues.putAll(props);
142                 _colourScheme.loadFromHex(_configValues.getProperty(KEY_COLOUR_SCHEME));
143                 _recentFiles = new RecentFileList(_configValues.getProperty(KEY_RECENT_FILES));
144                 _unitSet = UnitSetLibrary.getUnitSet(_configValues.getProperty(KEY_UNITSET_KEY));
145
146                 if (loadFailed) {
147                         throw new ConfigException();
148                 }
149                 // Store location of successfully loaded config file
150                 _configFile = inFile;
151         }
152
153         /**
154          * @return Properties object containing default values
155          */
156         private static Properties getDefaultProperties()
157         {
158                 Properties props = new Properties();
159                 // Fill in defaults
160                 props.put(KEY_GPS_DEVICE, "usb:");
161                 props.put(KEY_GPS_FORMAT, "garmin");
162                 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
163                 props.put(KEY_SHOW_MAP, "0"); // hide by default
164                 props.put(KEY_EXIFTOOL_PATH, "exiftool");
165                 props.put(KEY_GNUPLOT_PATH, "gnuplot");
166                 props.put(KEY_GPSBABEL_PATH, "gpsbabel");
167                 props.put(KEY_KMZ_IMAGE_SIZE, "240");
168                 props.put(KEY_AUTOSAVE_SETTINGS, "0"); // autosave false by default
169                 props.put(KEY_UNITSET_KEY, "unitset.kilometres"); // metric by default
170                 return props;
171         }
172
173         /**
174          * @param inString String to parse
175          * @return int value of String, or 0 if unparseable
176          */
177         private static int parseInt(String inString)
178         {
179                 int val = 0;
180                 try {
181                         val = Integer.parseInt(inString);
182                 }
183                 catch (Exception e) {} // ignore, value stays zero
184                 return val;
185         }
186
187         /** @return File from which config was loaded (or null) */
188         public static File getConfigFile()
189         {
190                 return _configFile;
191         }
192
193         /**
194          * @return config Properties object to allow all config values to be saved
195          */
196         public static Properties getAllConfig()
197         {
198                 // Update recently-used files
199                 _configValues.setProperty(KEY_RECENT_FILES, _recentFiles.getConfigString());
200                 return _configValues;
201         }
202
203         /**
204          * @return the current colour scheme
205          */
206         public static ColourScheme getColourScheme()
207         {
208                 return _colourScheme;
209         }
210
211         /**
212          * @return list of recently used files
213          */
214         public static RecentFileList getRecentFileList()
215         {
216                 return _recentFiles;
217         }
218
219         /**
220          * Store the given configuration setting
221          * @param inKey key (from constants)
222          * @param inValue value as string
223          */
224         public static void setConfigString(String inKey, String inValue)
225         {
226                 if (inValue == null || inValue.equals("")) {
227                         _configValues.remove(inKey);
228                 }
229                 else {
230                         _configValues.put(inKey, inValue);
231                 }
232         }
233
234         /**
235          * Store the given configuration setting
236          * @param inKey key (from constants)
237          * @param inValue value as boolean
238          */
239         public static void setConfigBoolean(String inKey, boolean inValue)
240         {
241                 if (inKey != null && !inKey.equals(""))
242                 {
243                         _configValues.put(inKey, (inValue?"1":"0"));
244                 }
245         }
246
247         /**
248          * Store the given configuration setting
249          * @param inKey key (from constants)
250          * @param inValue value as int
251          */
252         public static void setConfigInt(String inKey, int inValue)
253         {
254                 if (inKey != null && !inKey.equals(""))
255                 {
256                         _configValues.put(inKey, "" + inValue);
257                 }
258         }
259
260         /**
261          * Get the given configuration setting as a String
262          * @param inKey key
263          * @return configuration setting as a String
264          */
265         public static String getConfigString(String inKey)
266         {
267                 return _configValues.getProperty(inKey);
268         }
269
270         /**
271          * Get the given configuration setting as a boolean
272          * @param inKey key
273          * @return configuration setting as a boolean (default to true)
274          */
275         public static boolean getConfigBoolean(String inKey)
276         {
277                 String val = _configValues.getProperty(inKey);
278                 return (val == null || val.equals("1"));
279         }
280
281         /**
282          * Get the given configuration setting as an int
283          * @param inKey key
284          * @return configuration setting as an int
285          */
286         public static int getConfigInt(String inKey)
287         {
288                 return parseInt(_configValues.getProperty(inKey));
289         }
290
291         /**
292          * Check whether the given key corresponds to a boolean property
293          * @param inKey key to check
294          * @return true if corresponding property is boolean
295          */
296         public static boolean isKeyBoolean(String inKey)
297         {
298                 // Only one boolean key so far (after metric flag was removed)
299                 return inKey != null && (
300                         inKey.equals(KEY_SHOW_MAP));
301         }
302
303         /**
304          * Update the colour scheme property from the current settings
305          */
306         public static void updateColourScheme()
307         {
308                 setConfigString(KEY_COLOUR_SCHEME, _colourScheme.toString());
309         }
310
311         /**
312          * @return the current unit set
313          */
314         public static UnitSet getUnitSet() {
315                 return _unitSet;
316         }
317
318         public static void selectUnitSet(int inIndex)
319         {
320                 _unitSet = UnitSetLibrary.getUnitSet(inIndex);
321                 // Set name of set in config
322                 setConfigString(KEY_UNITSET_KEY, _unitSet.getNameKey());
323         }
324 }