]> git.street.me.uk Git - andy/viking.git/blobdiff - src/preferences.c
Remove now unused alphabetical sort functions as they have been superseded.
[andy/viking.git] / src / preferences.c
index 9fdc8628038b61a3f2d22b4f0c7518ba6ef38bdf..e54fcd1ae0079950113190204cb95c4847ddcba3 100644 (file)
@@ -1,23 +1,39 @@
+/*
+ * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
+ *
+ * Copyright (C) 2003-2007, Evan Battaglia <gtoevan@gmx.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
 #include <gtk/gtk.h>
 #include <glib/gi18n.h>
 #include <string.h>
 #include <gtk/gtk.h>
 #include <glib/gi18n.h>
 #include <string.h>
-#include <stdlib.h>
 #include <stdio.h>
 #include <glib/gstdio.h>
 #include "preferences.h"
 #include <stdio.h>
 #include <glib/gstdio.h>
 #include "preferences.h"
+#include "dir.h"
 #include "file.h"
 #include "file.h"
+#include "util.h"
 
 
-// TODO: register_group
 // TODO: STRING_LIST
 // TODO: share code in file reading
 // TODO: remove hackaround in show_window
 // TODO: STRING_LIST
 // TODO: share code in file reading
 // TODO: remove hackaround in show_window
-// TODO: move typeddata to uibuilder, make it more used & general, it's a "prettier" solution methinks
-// maybe this wasn't such a good idea...
 
 #define VIKING_PREFS_FILE "viking.prefs"
 
 
 #define VIKING_PREFS_FILE "viking.prefs"
 
-#define TEST_BOOLEAN(str) (! ((str)[0] == '\0' || (str)[0] == '0' || (str)[0] == 'n' || (str)[0] == 'N' || (str)[0] == 'f' || (str)[0] == 'F') )
-
 static GPtrArray *params;
 static GHashTable *values;
 gboolean loaded;
 static GPtrArray *params;
 static GHashTable *values;
 gboolean loaded;
@@ -42,17 +58,17 @@ static void preferences_groups_uninit()
 void a_preferences_register_group ( const gchar *key, const gchar *name )
 {
   if ( g_hash_table_lookup ( groups_keys_to_indices, key ) )
 void a_preferences_register_group ( const gchar *key, const gchar *name )
 {
   if ( g_hash_table_lookup ( groups_keys_to_indices, key ) )
-    g_error("Duplicate preferences group keys");
+    g_critical("Duplicate preferences group keys");
   else {
     g_ptr_array_add ( groups_names, g_strdup(name) );
   else {
     g_ptr_array_add ( groups_names, g_strdup(name) );
-    g_hash_table_insert ( groups_keys_to_indices, g_strdup(key), (gpointer) ((gint) groups_names->len ) ); /* index + 1 */
+    g_hash_table_insert ( groups_keys_to_indices, g_strdup(key), GINT_TO_POINTER ( (gint) groups_names->len ) ); /* index + 1 */
   }
 }
 
 /* returns -1 if not found. */
 static gint16 preferences_groups_key_to_index( const gchar *key )
 {
   }
 }
 
 /* returns -1 if not found. */
 static gint16 preferences_groups_key_to_index( const gchar *key )
 {
-  gint index = (gint) g_hash_table_lookup ( groups_keys_to_indices, key );
+  gint index = GPOINTER_TO_INT ( g_hash_table_lookup ( groups_keys_to_indices, key ) );
   if ( ! index )
     return VIK_LAYER_GROUP_NONE; /* which should be -1 anyway */
   return (gint16) (index - 1);
   if ( ! index )
     return VIK_LAYER_GROUP_NONE; /* which should be -1 anyway */
   return (gint16) (index - 1);
@@ -60,104 +76,6 @@ static gint16 preferences_groups_key_to_index( const gchar *key )
 
 /*****************************/
 
 
 /*****************************/
 
-/************/
-
-typedef struct {
-  VikLayerParamData data;
-  guint8 type;
-  gpointer freeme; // because data.s is const and the compiler complains
-} VikLayerTypedParamData;
-
-void layer_typed_param_data_free(gpointer p)
-{
-  VikLayerTypedParamData *val = (VikLayerTypedParamData *)p;
-  switch ( val->type ) {
-    case VIK_LAYER_PARAM_STRING:
-      if ( val->freeme )
-        g_free ( val->freeme );
-      break;
-    /* TODO: APPLICABLE TO US? NOTE: string layer works auniquely: data.sl should NOT be free'd when
-     * the internals call get_param -- i.e. it should be managed w/in the layer.
-     * The value passed by the internals into set_param should also be managed
-     * by the layer -- i.e. free'd by the layer.
-     */
-    case VIK_LAYER_PARAM_STRING_LIST:
-      g_error ( "Param strings not implemented in preferences"); //fake it
-      break;
-  }
-  g_free ( val );
-}
-
-VikLayerTypedParamData *layer_typed_param_data_copy_from_data(guint8 type, VikLayerParamData val) {
-  VikLayerTypedParamData *newval = g_new(VikLayerTypedParamData,1);
-  newval->data = val;
-  newval->type = type;
-  switch ( newval->type ) {
-    case VIK_LAYER_PARAM_STRING: {
-      gchar *s = g_strdup(newval->data.s);
-      newval->data.s = s;
-      newval->freeme = s;
-      break;
-    }
-    /* TODO: APPLICABLE TO US? NOTE: string layer works auniquely: data.sl should NOT be free'd when
-     * the internals call get_param -- i.e. it should be managed w/in the layer.
-     * The value passed by the internals into set_param should also be managed
-     * by the layer -- i.e. free'd by the layer.
-     */
-    case VIK_LAYER_PARAM_STRING_LIST:
-      g_error ( "Param strings not implemented in preferences"); //fake it
-      break;
-  }
-  return newval;
-}
-
-/* TODO: share this code with file.c */
-VikLayerTypedParamData *layer_data_typed_param_copy_from_string ( guint8 type, const gchar *str )
-{
-  g_assert ( type != VIK_LAYER_PARAM_STRING_LIST );
-  VikLayerTypedParamData *rv = g_new(VikLayerTypedParamData,1);
-  rv->type = type;
-  switch ( type )
-  {
-    case VIK_LAYER_PARAM_DOUBLE: rv->data.d = strtod(str, NULL); break;
-    case VIK_LAYER_PARAM_UINT: rv->data.u = strtoul(str, NULL, 10); break;
-    case VIK_LAYER_PARAM_INT: rv->data.i = strtol(str, NULL, 10); break;
-    case VIK_LAYER_PARAM_BOOLEAN: rv->data.b = TEST_BOOLEAN(str); break;
-    case VIK_LAYER_PARAM_COLOR: memset(&(rv->data.c), 0, sizeof(rv->data.c)); /* default: black */
-      gdk_color_parse ( str, &(rv->data.c) ); break;
-    /* STRING or STRING_LIST -- if STRING_LIST, just set param to add a STRING */
-    default: {
-      gchar *s = g_strdup(str);
-      rv->data.s = s;
-      rv->freeme = s;
-    }
-  }
-  return rv;
-}
-
-/************/
-
-/* MAKES A COPY OF THE KEY!!! */
-static gboolean preferences_load_parse_param(gchar *buf, gchar **key, gchar **val )
-{
-  gchar *eq_pos;
-  gint len;
-
-  // comments, special characters in viking file format
-  if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
-    return FALSE;
-  eq_pos = strchr ( buf, '=' );
-  if ( ! eq_pos )
-    return FALSE;
-  *key = g_strndup ( buf, eq_pos - buf );
-  *val = eq_pos + 1;
-  len = strlen(*val);
-  if ( len > 0 )
-    if ( (*val)[len - 1] == '\n' )
-      (*val) [ len - 1 ] = '\0'; /* cut off newline */
-  return TRUE;
-}
-
 static gboolean preferences_load_from_file()
 {
   gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
 static gboolean preferences_load_from_file()
 {
   gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
@@ -169,25 +87,27 @@ static gboolean preferences_load_from_file()
     gchar *key, *val;
     VikLayerTypedParamData *oldval, *newval;
     while ( ! feof (f) ) {
     gchar *key, *val;
     VikLayerTypedParamData *oldval, *newval;
     while ( ! feof (f) ) {
-      fgets(buf,sizeof(buf),f);
-      if ( preferences_load_parse_param(buf, &key, &val ) ) {
+      if (fgets(buf,sizeof(buf),f) == NULL)
+        break;
+      if ( split_string_from_file_on_equals ( buf, &key, &val ) ) {
         // if it's not in there, ignore it
         oldval = g_hash_table_lookup ( values, key );
         if ( ! oldval ) {
           g_free(key);
         // if it's not in there, ignore it
         oldval = g_hash_table_lookup ( values, key );
         if ( ! oldval ) {
           g_free(key);
+          g_free(val);
           continue;
         }
 
         // otherwise change it (you know the type!)
         // if it's a string list do some funky stuff ... yuck... not yet.
         if ( oldval->type == VIK_LAYER_PARAM_STRING_LIST )
           continue;
         }
 
         // otherwise change it (you know the type!)
         // if it's a string list do some funky stuff ... yuck... not yet.
         if ( oldval->type == VIK_LAYER_PARAM_STRING_LIST )
-          g_error ( "Param strings not implemented in preferences"); // fake it
+          g_critical ( "Param strings not implemented in preferences"); // fake it
 
 
-        newval = layer_data_typed_param_copy_from_string ( oldval->type, val );
+        newval = vik_layer_data_typed_param_copy_from_string ( oldval->type, val );
         g_hash_table_insert ( values, key, newval );
 
         g_free(key);
         g_hash_table_insert ( values, key, newval );
 
         g_free(key);
-
+        g_free(val);
         // change value
       }
     }
         // change value
       }
     }
@@ -201,26 +121,40 @@ static gboolean preferences_load_from_file()
 static void preferences_run_setparam ( gpointer notused, guint16 i, VikLayerParamData data, VikLayerParam *params )
 {
   if ( params[i].type == VIK_LAYER_PARAM_STRING_LIST )
 static void preferences_run_setparam ( gpointer notused, guint16 i, VikLayerParamData data, VikLayerParam *params )
 {
   if ( params[i].type == VIK_LAYER_PARAM_STRING_LIST )
-    g_error ( "Param strings not implemented in preferences"); //fake it
-  g_hash_table_insert ( values, (gchar *)(params[i].name), layer_typed_param_data_copy_from_data(params[i].type, data) );
+    g_critical ( "Param strings not implemented in preferences"); //fake it
+  g_hash_table_insert ( values, (gchar *)(params[i].name), vik_layer_typed_param_data_copy_from_data(params[i].type, data) );
+}
+
+/* Allow preferences to be manipulated externally */
+void a_preferences_run_setparam ( VikLayerParamData data, VikLayerParam *params )
+{
+  preferences_run_setparam (NULL, 0, data, params);
 }
 
 }
 
-static VikLayerParamData preferences_run_getparam ( gpointer notused, guint16 i )
+static VikLayerParamData preferences_run_getparam ( gpointer notused, guint16 i, gboolean notused2 )
 {
   VikLayerTypedParamData *val = (VikLayerTypedParamData *) g_hash_table_lookup ( values, ((VikLayerParam *)g_ptr_array_index(params,i))->name );
   g_assert ( val != NULL );
   if ( val->type == VIK_LAYER_PARAM_STRING_LIST )
 {
   VikLayerTypedParamData *val = (VikLayerTypedParamData *) g_hash_table_lookup ( values, ((VikLayerParam *)g_ptr_array_index(params,i))->name );
   g_assert ( val != NULL );
   if ( val->type == VIK_LAYER_PARAM_STRING_LIST )
-    g_error ( "Param strings not implemented in preferences"); //fake it
+    g_critical ( "Param strings not implemented in preferences"); //fake it
   return val->data;
 }
 
   return val->data;
 }
 
-/* TRUE on success */
-static gboolean preferences_save_to_file()
+/**
+ * a_preferences_save_to_file:
+ * 
+ * Returns: TRUE on success
+ */
+gboolean a_preferences_save_to_file()
 {
   gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
 
   // TODO: error checking
   FILE *f = g_fopen(fn, "w");
 {
   gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
 
   // TODO: error checking
   FILE *f = g_fopen(fn, "w");
+  /* Since preferences files saves OSM login credentials,
+   * it'll be better to store it in secret.
+   */
+  g_chmod(fn, 0600);
   g_free ( fn );
 
   if ( f ) {
   g_free ( fn );
 
   if ( f ) {
@@ -253,12 +187,12 @@ void a_preferences_show_window(GtkWindow *parent) {
     }
     loaded = TRUE;
     preferences_load_from_file();
     }
     loaded = TRUE;
     preferences_load_from_file();
-    if ( a_uibuilder_properties_factory ( parent, contiguous_params, params_count,
+    if ( a_uibuilder_properties_factory ( _("Preferences"), parent, contiguous_params, params_count,
                                (gchar **) groups_names->pdata, groups_names->len, // groups, groups_count, // groups? what groups?!
                                (gchar **) groups_names->pdata, groups_names->len, // groups, groups_count, // groups? what groups?!
-                                (gboolean (*) (gpointer,guint16,VikLayerParamData,gpointer)) preferences_run_setparam,
+                               (gboolean (*) (gpointer,guint16,VikLayerParamData,gpointer,gboolean)) preferences_run_setparam,
                                NULL /* not used */, contiguous_params,
                                 preferences_run_getparam, NULL /* not used */ ) ) {
                                NULL /* not used */, contiguous_params,
                                 preferences_run_getparam, NULL /* not used */ ) ) {
-      preferences_save_to_file();
+      a_preferences_save_to_file();
     }
     g_free ( contiguous_params );
 }
     }
     g_free ( contiguous_params );
 }
@@ -268,7 +202,7 @@ void a_preferences_register(VikLayerParam *pref, VikLayerParamData defaultval, c
   /* copy value */
   VikLayerParam *newpref = g_new(VikLayerParam,1);
   *newpref = *pref;
   /* copy value */
   VikLayerParam *newpref = g_new(VikLayerParam,1);
   *newpref = *pref;
-  VikLayerTypedParamData *newval = layer_typed_param_data_copy_from_data(pref->type, defaultval);
+  VikLayerTypedParamData *newval = vik_layer_typed_param_data_copy_from_data(pref->type, defaultval);
   if ( group_key )
     newpref->group = preferences_groups_key_to_index ( group_key );
 
   if ( group_key )
     newpref->group = preferences_groups_key_to_index ( group_key );
 
@@ -284,7 +218,7 @@ void a_preferences_init()
   params = g_ptr_array_new ();
 
   /* key not copied (same ptr as in pref), actual param data yes */
   params = g_ptr_array_new ();
 
   /* key not copied (same ptr as in pref), actual param data yes */
-  values = g_hash_table_new_full ( g_str_hash, g_str_equal, NULL, layer_typed_param_data_free);
+  values = g_hash_table_new_full ( g_str_hash, g_str_equal, NULL, vik_layer_typed_param_data_free);
 
   loaded = FALSE;
 }
 
   loaded = FALSE;
 }