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