]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/Config.java
Version 7, February 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         /** Working directory for loading and saving */
13         private static File _workingDir = null;
14         /** Default language */
15         private static String _langCode = null;
16         /** GPS device name */
17         private static String _gpsDevice = null;
18         /** GPS format name */
19         private static String _gpsFormat = null;
20         /** Font to use for povray */
21         private static String _povrayFont = null;
22         /** True to use metric units */
23         private static boolean _metricUnits = true;
24         /** Path to gnuplot executable */
25         private static String _gnuplotPath = null;
26         /** Index of selected map tile server */
27         private static int _mapTileServerIndex = -1;
28         /** URL for freeform map tile server */
29         private static String _mapTileServerUrl = null;
30         /** File from which Config was loaded */
31         private static File _configFile = null;
32
33         // TODO: Need setters for all these parameters if want to make the config saveable
34
35         /** Default config file */
36         private static final File DEFAULT_CONFIG_FILE = new File(".pruneconfig");
37
38         /** Key for working directory */
39         private static final String KEY_WORKING_DIR = "prune.directory";
40         /** Key for language code */
41         private static final String KEY_LANGUAGE_CODE = "prune.languagecode";
42         /** Key for GPS device */
43         private static final String KEY_GPS_DEVICE = "prune.gpsdevice";
44         /** Key for GPS format */
45         private static final String KEY_GPS_FORMAT = "prune.gpsformat";
46         /** Key for Povray font */
47         private static final String KEY_POVRAY_FONT = "prune.povrayfont";
48         /** Key for metric/imperial */
49         private static final String KEY_METRIC_UNITS = "prune.metricunits";
50         /** Key for gpsbabel path */
51         private static final String KEY_GNUPLOTPATH = "prune.gnuplotpath";
52         /** Key for map server index */
53         private static final String KEY_MAPSERVERINDEX = "prune.mapserverindex";
54         /** Key for map server url */
55         private static final String KEY_MAPSERVERURL = "prune.mapserverurl";
56
57
58         /**
59          * @return working directory for loading and saving
60          */
61         public static File getWorkingDirectory()
62         {
63                 return _workingDir;
64         }
65
66         /**
67          * @param inDirectory working directory to use
68          */
69         public static void setWorkingDirectory(File inDirectory)
70         {
71                 _workingDir = inDirectory;
72         }
73
74         /**
75          * Load the default configuration file
76          */
77         public static void loadDefaultFile()
78         {
79                 try
80                 {
81                         loadFile(DEFAULT_CONFIG_FILE);
82                 }
83                 catch (ConfigException ce) {} // ignore
84         }
85
86
87         /**
88          * Load configuration from file
89          * @param inFile file to load
90          * @throws ConfigException if specified file couldn't be read
91          */
92         public static void loadFile(File inFile) throws ConfigException
93         {
94                 // Start with default properties
95                 Properties props = getDefaultProperties();
96                 // Try to load the file into a properties object
97                 boolean loadFailed = false;
98                 FileInputStream fis = null;
99                 try
100                 {
101                         fis = new FileInputStream(inFile);
102                         props.load(fis);
103                 }
104                 catch (Exception e) {
105                         loadFailed = true;
106                 }
107                 finally {
108                         if (fis != null) try {
109                                 fis.close();
110                         }
111                         catch (Exception e) {}
112                 }
113                 // Save the properties we know about, ignore the rest
114                 _langCode = props.getProperty(KEY_LANGUAGE_CODE);
115                 String dir = props.getProperty(KEY_WORKING_DIR);
116                 if (dir != null) {setWorkingDirectory(new File(dir));}
117                 _gpsDevice = props.getProperty(KEY_GPS_DEVICE);
118                 _gpsFormat = props.getProperty(KEY_GPS_FORMAT);
119                 _povrayFont = props.getProperty(KEY_POVRAY_FONT);
120                 String useMetric = props.getProperty(KEY_METRIC_UNITS);
121                 _metricUnits = (useMetric == null || useMetric.equals("") || useMetric.toLowerCase().equals("y"));
122                 _gnuplotPath = props.getProperty(KEY_GNUPLOTPATH);
123                 if (_gnuplotPath == null || _gnuplotPath.equals("")) {_gnuplotPath = "gnuplot";}
124                 _mapTileServerIndex = parseInt(props.getProperty(KEY_MAPSERVERINDEX));
125                 _mapTileServerUrl = props.getProperty(KEY_MAPSERVERURL);
126                 if (loadFailed) {
127                         throw new ConfigException();
128                 }
129                 // Store location of successfully loaded config file
130                 _configFile = inFile;
131         }
132
133         /**
134          * @return Properties object containing default values
135          */
136         private static Properties getDefaultProperties()
137         {
138                 Properties props = new Properties();
139                 // Fill in defaults
140                 props.put(KEY_GPS_DEVICE, "usb:");
141                 props.put(KEY_GPS_FORMAT, "garmin");
142                 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
143                 return props;
144         }
145
146         /**
147          * @param inString String to parse
148          * @return int value of String, or 0 if unparseable
149          */
150         private static int parseInt(String inString)
151         {
152                 int val = 0;
153                 try {
154                         val = Integer.parseInt(inString);
155                 }
156                 catch (Exception e) {} // ignore, value stays zero
157                 return val;
158         }
159
160         /** @return language code */
161         public static String getLanguageCode()
162         {
163                 return _langCode;
164         }
165
166         /** @return gps device */
167         public static String getGpsDevice()
168         {
169                 return _gpsDevice;
170         }
171
172         /** @return gps format */
173         public static String getGpsFormat()
174         {
175                 return _gpsFormat;
176         }
177
178         /** @return povray font */
179         public static String getPovrayFont()
180         {
181                 return _povrayFont;
182         }
183
184         /** @return true to use metric units */
185         public static boolean getUseMetricUnits()
186         {
187                 return _metricUnits;
188         }
189
190         /** @param inMetric true to use metric units */
191         public static void setUseMetricUnits(boolean inMetric)
192         {
193                 _metricUnits = inMetric;
194         }
195
196         /** @return path to gnuplot */
197         public static String getGnuplotPath()
198         {
199                 return _gnuplotPath;
200         }
201
202         /** @param inPath path to Gnuplot */
203         public static void setGnuplotPath(String inPath)
204         {
205                 _gnuplotPath = inPath;
206         }
207
208         /** @return index of map server */
209         public static int getMapServerIndex()
210         {
211                 return _mapTileServerIndex;
212         }
213
214         /** @param inIndex selected index */
215         public static void setMapServerIndex(int inIndex)
216         {
217                 _mapTileServerIndex = inIndex;
218         }
219
220         /** @return url of map server */
221         public static String getMapServerUrl()
222         {
223                 return _mapTileServerUrl;
224         }
225
226         /** @param inUrl url of map server */
227         public static void setMapServerUrl(String inUrl)
228         {
229                 _mapTileServerUrl = inUrl;
230         }
231
232         /** @return File from which config was loaded (or null) */
233         public static File getConfigFile()
234         {
235                 return _configFile;
236         }
237 }