]> git.street.me.uk Git - andy/viking.git/blobdiff - src/preferences.c
Fix <GTK 2.24 combo box usage.
[andy/viking.git] / src / preferences.c
index 57e394369fe566ea9a594ca666b14830208faa22..5d413424c0702a3a295b9326c3a8404c75466cdd 100644 (file)
@@ -1,7 +1,29 @@
+/*
+ * 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 <stdlib.h>
+#include <stdio.h>
+#include <glib/gstdio.h>
 #include "preferences.h"
 #include "file.h"
 
@@ -43,14 +65,14 @@ void a_preferences_register_group ( const gchar *key, const gchar *name )
     g_error("Duplicate preferences group keys");
   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 )
 {
-  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);
@@ -151,14 +173,15 @@ static gboolean preferences_load_parse_param(gchar *buf, gchar **key, gchar **va
   *val = eq_pos + 1;
   len = strlen(*val);
   if ( len > 0 )
-    (*val) [ len - 1 ] = '\0'; /* cut off newline */
+    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);
-  FILE *f = fopen(fn, "r");
+  FILE *f = g_fopen(fn, "r");
   g_free ( fn );
 
   if ( f ) {
@@ -166,7 +189,8 @@ static gboolean preferences_load_from_file()
     gchar *key, *val;
     VikLayerTypedParamData *oldval, *newval;
     while ( ! feof (f) ) {
-      fgets(buf,sizeof(buf),f);
+      if (fgets(buf,sizeof(buf),f) == NULL)
+        break;
       if ( preferences_load_parse_param(buf, &key, &val ) ) {
         // if it's not in there, ignore it
         oldval = g_hash_table_lookup ( values, key );
@@ -189,6 +213,7 @@ static gboolean preferences_load_from_file()
       }
     }
     fclose(f);
+    f = NULL;
     return TRUE;
   }
   return FALSE;
@@ -201,7 +226,13 @@ static void preferences_run_setparam ( gpointer notused, guint16 i, VikLayerPara
   g_hash_table_insert ( values, (gchar *)(params[i].name), layer_typed_param_data_copy_from_data(params[i].type, data) );
 }
 
-static VikLayerParamData preferences_run_getparam ( gpointer notused, guint16 i )
+/* 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, gboolean notused2 )
 {
   VikLayerTypedParamData *val = (VikLayerTypedParamData *) g_hash_table_lookup ( values, ((VikLayerParam *)g_ptr_array_index(params,i))->name );
   g_assert ( val != NULL );
@@ -211,12 +242,16 @@ static VikLayerParamData preferences_run_getparam ( gpointer notused, guint16 i
 }
 
 /* TRUE on success */
-static gboolean preferences_save_to_file()
+gboolean a_preferences_save_to_file()
 {
   gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
 
   // TODO: error checking
-  FILE *f = fopen(fn, "w");
+  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 ) {
@@ -230,6 +265,7 @@ static gboolean preferences_save_to_file()
       file_write_layer_param ( f, param->name, val->type, val->data );
     }
     fclose(f);
+    f = NULL;
     return TRUE;
   }
 
@@ -248,12 +284,12 @@ void a_preferences_show_window(GtkWindow *parent) {
     }
     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?!
-                                (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 */ ) ) {
-      preferences_save_to_file();
+      a_preferences_save_to_file();
     }
     g_free ( contiguous_params );
 }