]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/config/Config.java
0873b0f505be1ded6b2985b6da0509631890964e
[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 line width used for drawing */
63         public static final String KEY_LINE_WIDTH = "prune.linewidth";
64         /** Key for kml track colour */
65         public static final String KEY_KML_TRACK_COLOUR = "prune.kmltrackcolour";
66
67
68         /**
69          * Load the default configuration file
70          */
71         public static void loadDefaultFile()
72         {
73                 try
74                 {
75                         loadFile(DEFAULT_CONFIG_FILE);
76                 }
77                 catch (ConfigException ce) {} // ignore
78         }
79
80
81         /**
82          * Load configuration from file
83          * @param inFile file to load
84          * @throws ConfigException if specified file couldn't be read
85          */
86         public static void loadFile(File inFile) throws ConfigException
87         {
88                 // Start with default properties
89                 Properties props = getDefaultProperties();
90                 // Try to load the file into a properties object
91                 boolean loadFailed = false;
92                 FileInputStream fis = null;
93                 try
94                 {
95                         fis = new FileInputStream(inFile);
96                         props.load(fis);
97                 }
98                 catch (Exception e) {
99                         loadFailed = true;
100                 }
101                 finally {
102                         if (fis != null) try {
103                                 fis.close();
104                         }
105                         catch (Exception e) {}
106                 }
107                 // Save all properties from file
108                 _configValues.putAll(props);
109                 _colourScheme.loadFromHex(_configValues.getProperty(KEY_COLOUR_SCHEME));
110                 if (loadFailed) {
111                         throw new ConfigException();
112                 }
113                 // Store location of successfully loaded config file
114                 _configFile = inFile;
115         }
116
117         /**
118          * @return Properties object containing default values
119          */
120         private static Properties getDefaultProperties()
121         {
122                 Properties props = new Properties();
123                 // Fill in defaults
124                 props.put(KEY_GPS_DEVICE, "usb:");
125                 props.put(KEY_GPS_FORMAT, "garmin");
126                 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
127                 props.put(KEY_SHOW_MAP, "0"); // hide by default
128                 props.put(KEY_EXIFTOOL_PATH, "exiftool");
129                 props.put(KEY_GNUPLOT_PATH, "gnuplot");
130                 props.put(KEY_GPSBABEL_PATH, "gpsbabel");
131                 props.put(KEY_KMZ_IMAGE_WIDTH, "240");
132                 props.put(KEY_KMZ_IMAGE_HEIGHT, "240");
133                 return props;
134         }
135
136         /**
137          * @param inString String to parse
138          * @return int value of String, or 0 if unparseable
139          */
140         private static int parseInt(String inString)
141         {
142                 int val = 0;
143                 try {
144                         val = Integer.parseInt(inString);
145                 }
146                 catch (Exception e) {} // ignore, value stays zero
147                 return val;
148         }
149
150         /** @return File from which config was loaded (or null) */
151         public static File getConfigFile()
152         {
153                 return _configFile;
154         }
155
156         /**
157          * @return config Properties object to allow all config values to be saved
158          */
159         public static Properties getAllConfig()
160         {
161                 return _configValues;
162         }
163
164         /**
165          * @return the current colour scheme
166          */
167         public static ColourScheme getColourScheme()
168         {
169                 return _colourScheme;
170         }
171
172         /**
173          * Store the given configuration setting
174          * @param inKey key (from constants)
175          * @param inValue value as string
176          */
177         public static void setConfigString(String inKey, String inValue)
178         {
179                 if (inValue == null || inValue.equals("")) {
180                         _configValues.remove(inKey);
181                 }
182                 else {
183                         _configValues.put(inKey, inValue);
184                 }
185         }
186
187         /**
188          * Store the given configuration setting
189          * @param inKey key (from constants)
190          * @param inValue value as boolean
191          */
192         public static void setConfigBoolean(String inKey, boolean inValue)
193         {
194                 if (inKey != null && !inKey.equals(""))
195                 {
196                         _configValues.put(inKey, (inValue?"1":"0"));
197                 }
198         }
199
200         /**
201          * Store the given configuration setting
202          * @param inKey key (from constants)
203          * @param inValue value as int
204          */
205         public static void setConfigInt(String inKey, int inValue)
206         {
207                 if (inKey != null && !inKey.equals(""))
208                 {
209                         _configValues.put(inKey, "" + inValue);
210                 }
211         }
212
213         /**
214          * Get the given configuration setting as a String
215          * @param inKey key
216          * @return configuration setting as a String
217          */
218         public static String getConfigString(String inKey)
219         {
220                 return _configValues.getProperty(inKey);
221         }
222
223         /**
224          * Get the given configuration setting as a boolean
225          * @param inKey key
226          * @return configuration setting as a boolean (default to true)
227          */
228         public static boolean getConfigBoolean(String inKey)
229         {
230                 String val = _configValues.getProperty(inKey);
231                 return (val == null || val.equals("1"));
232         }
233
234         /**
235          * Get the given configuration setting as an int
236          * @param inKey key
237          * @return configuration setting as an int
238          */
239         public static int getConfigInt(String inKey)
240         {
241                 return parseInt(_configValues.getProperty(inKey));
242         }
243
244         /**
245          * Check whether the given key corresponds to a boolean property
246          * @param inKey key to check
247          * @return true if corresponding property is boolean
248          */
249         public static boolean isKeyBoolean(String inKey)
250         {
251                 // Only two boolean keys so far
252                 return inKey != null && (
253                         inKey.equals(KEY_METRIC_UNITS) || inKey.equals(KEY_SHOW_MAP));
254         }
255
256         /**
257          * Update the colour scheme property from the current settings
258          */
259         public static void updateColourScheme()
260         {
261                 setConfigString(KEY_COLOUR_SCHEME, _colourScheme.toString());
262         }
263 }