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