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