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