]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/Config.java
Version 8, September 2009
[GpsPrune.git] / tim / prune / Config.java
index d667b599e6748ed302d1e2a1f29e6c02d8442525..befbaccc1b4819216576f8d6a3c653ad7baca9dd 100644 (file)
@@ -9,54 +9,47 @@ import java.util.Properties;
  */
 public abstract class Config
 {
-       /** Working directory for loading and saving */
-       private static File _workingDir = null;
-       /** Default language */
-       private static String _langCode = null;
-       /** GPS device name */
-       private static String _gpsDevice = null;
-       /** GPS format name */
-       private static String _gpsFormat = null;
-       /** Font to use for povray */
-       private static String _povrayFont = null;
-       /** True to use metric units */
-       private static boolean _metricUnits = true;
+       /** File from which Config was loaded */
+       private static File _configFile = null;
 
+       /** Hashtable containing all config values */
+       private static Properties _configValues = new Properties();
 
        /** Default config file */
        private static final File DEFAULT_CONFIG_FILE = new File(".pruneconfig");
 
-       /** Key for working directory */
-       private static final String KEY_WORKING_DIR = "prune.directory";
+       /** Key for track directory */
+       public static final String KEY_TRACK_DIR = "prune.trackdirectory";
+       /** Key for photo directory */
+       public static final String KEY_PHOTO_DIR = "prune.photodirectory";
        /** Key for language code */
-       private static final String KEY_LANGUAGE_CODE = "prune.languagecode";
+       public static final String KEY_LANGUAGE_CODE = "prune.languagecode";
        /** Key for GPS device */
-       private static final String KEY_GPS_DEVICE = "prune.gpsdevice";
+       public static final String KEY_GPS_DEVICE = "prune.gpsdevice";
        /** Key for GPS format */
-       private static final String KEY_GPS_FORMAT = "prune.gpsformat";
+       public static final String KEY_GPS_FORMAT = "prune.gpsformat";
        /** Key for Povray font */
-       private static final String KEY_POVRAY_FONT = "prune.povrayfont";
+       public static final String KEY_POVRAY_FONT = "prune.povrayfont";
        /** Key for metric/imperial */
-       private static final String KEY_METRIC_UNITS = "prune.metricunits";
-       // TODO: Save config file location so save possible
+       public static final String KEY_METRIC_UNITS = "prune.metricunits";
+       /** Key for map server index */
+       public static final String KEY_MAPSERVERINDEX = "prune.mapserverindex";
+       /** Key for map server url */
+       public static final String KEY_MAPSERVERURL = "prune.mapserverurl";
+       /** Key for show pace flag */
+       public static final String KEY_SHOW_PACE = "prune.showpace";
+       /** Key for width of thumbnails in kmz */
+       public static final String KEY_KMZ_IMAGE_WIDTH = "prune.kmzimagewidth";
+       /** Key for height of thumbnails in kmz */
+       public static final String KEY_KMZ_IMAGE_HEIGHT = "prune.kmzimageheight";
+       /** Key for gpsbabel path */
+       public static final String KEY_GPSBABEL_PATH = "prune.gpsbabelpath";
+       /** Key for gnuplot path */
+       public static final String KEY_GNUPLOT_PATH = "prune.gnuplotpath";
+       /** Key for exiftool path */
+       public static final String KEY_EXIFTOOL_PATH = "prune.exiftoolpath";
 
 
-       /**
-        * @return working directory for loading and saving
-        */
-       public static File getWorkingDirectory()
-       {
-               return _workingDir;
-       }
-
-       /**
-        * @param inDirectory working directory to use
-        */
-       public static void setWorkingDirectory(File inDirectory)
-       {
-               _workingDir = inDirectory;
-       }
-
        /**
         * Load the default configuration file
         */
@@ -73,6 +66,7 @@ public abstract class Config
        /**
         * Load configuration from file
         * @param inFile file to load
+        * @throws ConfigException if specified file couldn't be read
         */
        public static void loadFile(File inFile) throws ConfigException
        {
@@ -80,26 +74,28 @@ public abstract class Config
                Properties props = getDefaultProperties();
                // Try to load the file into a properties object
                boolean loadFailed = false;
+               FileInputStream fis = null;
                try
                {
-                       props.load(new FileInputStream(inFile));
+                       fis = new FileInputStream(inFile);
+                       props.load(fis);
                }
-               catch (Exception e)
-               {
+               catch (Exception e) {
                        loadFailed = true;
                }
-               // Save the properties we know about, ignore the rest
-               _langCode = props.getProperty(KEY_LANGUAGE_CODE);
-               String dir = props.getProperty(KEY_WORKING_DIR);
-               if (dir != null) {setWorkingDirectory(new File(dir));}
-               _gpsDevice = props.getProperty(KEY_GPS_DEVICE);
-               _gpsFormat = props.getProperty(KEY_GPS_FORMAT);
-               _povrayFont = props.getProperty(KEY_POVRAY_FONT);
-               String useMetric = props.getProperty(KEY_METRIC_UNITS);
-               _metricUnits = (useMetric == null || useMetric.equals("") || useMetric.toLowerCase().equals("y"));
+               finally {
+                       if (fis != null) try {
+                               fis.close();
+                       }
+                       catch (Exception e) {}
+               }
+               // Save all properties from file
+               _configValues.putAll(props);
                if (loadFailed) {
                        throw new ConfigException();
                }
+               // Store location of successfully loaded config file
+               _configFile = inFile;
        }
 
        /**
@@ -112,36 +108,121 @@ public abstract class Config
                props.put(KEY_GPS_DEVICE, "usb:");
                props.put(KEY_GPS_FORMAT, "garmin");
                props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
+               props.put(KEY_SHOW_PACE, "0"); // hide by default
+               props.put(KEY_EXIFTOOL_PATH, "exiftool");
+               props.put(KEY_GNUPLOT_PATH, "gnuplot");
+               props.put(KEY_GPSBABEL_PATH, "gpsbabel");
+               props.put(KEY_KMZ_IMAGE_WIDTH, "240");
+               props.put(KEY_KMZ_IMAGE_HEIGHT, "180");
                return props;
        }
 
-       /** @return language code */
-       public static String getLanguageCode()
+       /**
+        * @param inString String to parse
+        * @return int value of String, or 0 if unparseable
+        */
+       private static int parseInt(String inString)
+       {
+               int val = 0;
+               try {
+                       val = Integer.parseInt(inString);
+               }
+               catch (Exception e) {} // ignore, value stays zero
+               return val;
+       }
+
+       /** @return File from which config was loaded (or null) */
+       public static File getConfigFile()
+       {
+               return _configFile;
+       }
+
+       /**
+        * @return config Properties object to allow all config values to be saved
+        */
+       public static Properties getAllConfig()
+       {
+               return _configValues;
+       }
+
+       /**
+        * Store the given configuration setting
+        * @param inKey key (from constants)
+        * @param inValue value as string
+        */
+       public static void setConfigString(String inKey, String inValue)
+       {
+               if (inKey != null && !inKey.equals(""))
+               {
+                       _configValues.put(inKey, inValue);
+               }
+       }
+
+       /**
+        * Store the given configuration setting
+        * @param inKey key (from constants)
+        * @param inValue value as boolean
+        */
+       public static void setConfigBoolean(String inKey, boolean inValue)
+       {
+               if (inKey != null && !inKey.equals(""))
+               {
+                       _configValues.put(inKey, (inValue?"1":"0"));
+               }
+       }
+
+       /**
+        * Store the given configuration setting
+        * @param inKey key (from constants)
+        * @param inValue value as int
+        */
+       public static void setConfigInt(String inKey, int inValue)
        {
-               return _langCode;
+               if (inKey != null && !inKey.equals(""))
+               {
+                       _configValues.put(inKey, "" + inValue);
+               }
        }
 
-       /** @return gps device */
-       public static String getGpsDevice()
+       /**
+        * Get the given configuration setting as a String
+        * @param inKey key
+        * @return configuration setting as a String
+        */
+       public static String getConfigString(String inKey)
        {
-               return _gpsDevice;
+               return _configValues.getProperty(inKey);
        }
 
-       /** @return gps format */
-       public static String getGpsFormat()
+       /**
+        * Get the given configuration setting as a boolean
+        * @param inKey key
+        * @return configuration setting as a boolean
+        */
+       public static boolean getConfigBoolean(String inKey)
        {
-               return _gpsFormat;
+               String val = _configValues.getProperty(inKey);
+               return (val == null || val.equals("1"));
        }
 
-       /** @return povray font */
-       public static String getPovrayFont()
+       /**
+        * Get the given configuration setting as an int
+        * @param inKey key
+        * @return configuration setting as an int
+        */
+       public static int getConfigInt(String inKey)
        {
-               return _povrayFont;
+               return parseInt(_configValues.getProperty(inKey));
        }
 
-       /** @return true to use metric units */
-       public static boolean getUseMetricUnits()
+       /**
+        * Check whether the given key corresponds to a boolean property
+        * @param inKey key to check
+        * @return true if corresponding property is boolean
+        */
+       public static boolean isKeyBoolean(String inKey)
        {
-               return _metricUnits;
+               // Only two boolean keys so far
+               return inKey != null && (inKey.equals(KEY_METRIC_UNITS) || inKey.equals(KEY_SHOW_PACE));
        }
 }