4 import java.io.FileInputStream;
5 import java.util.Properties;
8 * Abstract class to hold application-wide configuration
10 public abstract class Config
12 /** File from which Config was loaded */
13 private static File _configFile = null;
15 /** Hashtable containing all config values */
16 private static Properties _configValues = new Properties();
18 /** Default config file */
19 private static final File DEFAULT_CONFIG_FILE = new File(".pruneconfig");
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";
54 * Load the default configuration file
56 public static void loadDefaultFile()
60 loadFile(DEFAULT_CONFIG_FILE);
62 catch (ConfigException ce) {} // ignore
67 * Load configuration from file
68 * @param inFile file to load
69 * @throws ConfigException if specified file couldn't be read
71 public static void loadFile(File inFile) throws ConfigException
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;
80 fis = new FileInputStream(inFile);
87 if (fis != null) try {
90 catch (Exception e) {}
92 // Save all properties from file
93 _configValues.putAll(props);
95 throw new ConfigException();
97 // Store location of successfully loaded config file
102 * @return Properties object containing default values
104 private static Properties getDefaultProperties()
106 Properties props = new Properties();
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");
121 * @param inString String to parse
122 * @return int value of String, or 0 if unparseable
124 private static int parseInt(String inString)
128 val = Integer.parseInt(inString);
130 catch (Exception e) {} // ignore, value stays zero
134 /** @return File from which config was loaded (or null) */
135 public static File getConfigFile()
141 * @return config Properties object to allow all config values to be saved
143 public static Properties getAllConfig()
145 return _configValues;
149 * Store the given configuration setting
150 * @param inKey key (from constants)
151 * @param inValue value as string
153 public static void setConfigString(String inKey, String inValue)
155 if (inKey != null && !inKey.equals(""))
157 _configValues.put(inKey, inValue);
162 * Store the given configuration setting
163 * @param inKey key (from constants)
164 * @param inValue value as boolean
166 public static void setConfigBoolean(String inKey, boolean inValue)
168 if (inKey != null && !inKey.equals(""))
170 _configValues.put(inKey, (inValue?"1":"0"));
175 * Store the given configuration setting
176 * @param inKey key (from constants)
177 * @param inValue value as int
179 public static void setConfigInt(String inKey, int inValue)
181 if (inKey != null && !inKey.equals(""))
183 _configValues.put(inKey, "" + inValue);
188 * Get the given configuration setting as a String
190 * @return configuration setting as a String
192 public static String getConfigString(String inKey)
194 return _configValues.getProperty(inKey);
198 * Get the given configuration setting as a boolean
200 * @return configuration setting as a boolean
202 public static boolean getConfigBoolean(String inKey)
204 String val = _configValues.getProperty(inKey);
205 return (val == null || val.equals("1"));
209 * Get the given configuration setting as an int
211 * @return configuration setting as an int
213 public static int getConfigInt(String inKey)
215 return parseInt(_configValues.getProperty(inKey));
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
223 public static boolean isKeyBoolean(String inKey)
225 // Only two boolean keys so far
226 return inKey != null && (inKey.equals(KEY_METRIC_UNITS) || inKey.equals(KEY_SHOW_PACE));