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