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