]> git.street.me.uk Git - andy/viking.git/blob - src/preferences.c
[QA] Rename variables to avoid shadowing global variables.
[andy/viking.git] / src / preferences.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2007, Evan Battaglia <gtoevan@gmx.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21 #include <gtk/gtk.h>
22 #include <glib/gi18n.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <glib/gstdio.h>
26 #include "preferences.h"
27 #include "dir.h"
28 #include "file.h"
29 #include "util.h"
30
31 // TODO: STRING_LIST
32 // TODO: share code in file reading
33 // TODO: remove hackaround in show_window
34
35 #define VIKING_PREFS_FILE "viking.prefs"
36
37 static GPtrArray *params;
38 static GHashTable *values;
39 gboolean loaded;
40
41 /************ groups *********/
42
43 static GPtrArray *groups_names;
44 static GHashTable *groups_keys_to_indices; // contains gint, NULL (0) is not found, instead 1 is used for 0, 2 for 1, etc.
45
46 static void preferences_groups_init()
47 {
48   groups_names = g_ptr_array_new();
49   groups_keys_to_indices = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, NULL );
50 }
51
52 static void preferences_groups_uninit()
53 {
54   g_ptr_array_foreach ( groups_names, (GFunc)g_free, NULL );
55   g_ptr_array_free ( groups_names, TRUE );
56   g_hash_table_destroy ( groups_keys_to_indices );
57 }
58
59 void a_preferences_register_group ( const gchar *key, const gchar *name )
60 {
61   if ( g_hash_table_lookup ( groups_keys_to_indices, key ) )
62     g_critical("Duplicate preferences group keys");
63   else {
64     g_ptr_array_add ( groups_names, g_strdup(name) );
65     g_hash_table_insert ( groups_keys_to_indices, g_strdup(key), GINT_TO_POINTER ( (gint) groups_names->len ) ); /* index + 1 */
66   }
67 }
68
69 /* returns -1 if not found. */
70 static gint16 preferences_groups_key_to_index( const gchar *key )
71 {
72   gint index = GPOINTER_TO_INT ( g_hash_table_lookup ( groups_keys_to_indices, key ) );
73   if ( ! index )
74     return VIK_LAYER_GROUP_NONE; /* which should be -1 anyway */
75   return (gint16) (index - 1);
76 }
77
78 /*****************************/
79
80 static gboolean preferences_load_from_file()
81 {
82   gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
83   FILE *f = g_fopen(fn, "r");
84   g_free ( fn );
85
86   if ( f ) {
87     gchar buf[4096];
88     gchar *key, *val;
89     VikLayerTypedParamData *oldval, *newval;
90     while ( ! feof (f) ) {
91       if (fgets(buf,sizeof(buf),f) == NULL)
92         break;
93       if ( split_string_from_file_on_equals ( buf, &key, &val ) ) {
94         // if it's not in there, ignore it
95         oldval = g_hash_table_lookup ( values, key );
96         if ( ! oldval ) {
97           g_free(key);
98           g_free(val);
99           continue;
100         }
101
102         // otherwise change it (you know the type!)
103         // if it's a string list do some funky stuff ... yuck... not yet.
104         if ( oldval->type == VIK_LAYER_PARAM_STRING_LIST )
105           g_critical ( "Param strings not implemented in preferences"); // fake it
106
107         newval = vik_layer_data_typed_param_copy_from_string ( oldval->type, val );
108         g_hash_table_insert ( values, key, newval );
109
110         g_free(key);
111         g_free(val);
112         // change value
113       }
114     }
115     fclose(f);
116     f = NULL;
117     return TRUE;
118   }
119   return FALSE;
120 }
121
122 static void preferences_run_setparam ( gpointer notused, guint16 i, VikLayerParamData data, VikLayerParam *vlparams )
123 {
124   if ( vlparams[i].type == VIK_LAYER_PARAM_STRING_LIST )
125     g_critical ( "Param strings not implemented in preferences"); //fake it
126   g_hash_table_insert ( values, (gchar *)(vlparams[i].name), vik_layer_typed_param_data_copy_from_data(vlparams[i].type, data) );
127 }
128
129 /* Allow preferences to be manipulated externally */
130 void a_preferences_run_setparam ( VikLayerParamData data, VikLayerParam *vlparams )
131 {
132   preferences_run_setparam (NULL, 0, data, vlparams);
133 }
134
135 static VikLayerParamData preferences_run_getparam ( gpointer notused, guint16 i, gboolean notused2 )
136 {
137   VikLayerTypedParamData *val = (VikLayerTypedParamData *) g_hash_table_lookup ( values, ((VikLayerParam *)g_ptr_array_index(params,i))->name );
138   g_assert ( val != NULL );
139   if ( val->type == VIK_LAYER_PARAM_STRING_LIST )
140     g_critical ( "Param strings not implemented in preferences"); //fake it
141   return val->data;
142 }
143
144 /**
145  * a_preferences_save_to_file:
146  * 
147  * Returns: TRUE on success
148  */
149 gboolean a_preferences_save_to_file()
150 {
151   gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
152
153   // TODO: error checking
154   FILE *f = g_fopen(fn, "w");
155   /* Since preferences files saves OSM login credentials,
156    * it'll be better to store it in secret.
157    */
158   g_chmod(fn, 0600);
159   g_free ( fn );
160
161   if ( f ) {
162     VikLayerParam *param;
163     VikLayerTypedParamData *val;
164     int i;
165     for ( i = 0; i < params->len; i++ ) {
166       param = (VikLayerParam *) g_ptr_array_index(params,i);
167       val = (VikLayerTypedParamData *) g_hash_table_lookup ( values, param->name );
168       g_assert ( val != NULL );
169       file_write_layer_param ( f, param->name, val->type, val->data );
170     }
171     fclose(f);
172     f = NULL;
173     return TRUE;
174   }
175
176   return FALSE;
177 }
178
179
180 void a_preferences_show_window(GtkWindow *parent) {
181     //VikLayerParamData *a_uibuilder_run_dialog ( GtkWindow *parent, VikLayerParam \*params, // guint16 params_count, gchar **groups, guint8 groups_count, // VikLayerParamData *params_defaults )
182     // TODO: THIS IS A MAJOR HACKAROUND, but ok when we have only a couple preferences.
183     gint params_count = params->len;
184     VikLayerParam *contiguous_params = g_new(VikLayerParam,params_count);
185     int i;
186     for ( i = 0; i < params->len; i++ ) {
187       contiguous_params[i] = *((VikLayerParam*)(g_ptr_array_index(params,i)));
188     }
189     loaded = TRUE;
190     preferences_load_from_file();
191     if ( a_uibuilder_properties_factory ( _("Preferences"), parent, contiguous_params, params_count,
192                                 (gchar **) groups_names->pdata, groups_names->len, // groups, groups_count, // groups? what groups?!
193                                 (gboolean (*) (gpointer,guint16,VikLayerParamData,gpointer,gboolean)) preferences_run_setparam,
194                                 NULL /* not used */, contiguous_params,
195                                 preferences_run_getparam, NULL, NULL /* not used */ ) ) {
196       a_preferences_save_to_file();
197     }
198     g_free ( contiguous_params );
199 }
200
201 void a_preferences_register(VikLayerParam *pref, VikLayerParamData defaultval, const gchar *group_key )
202 {
203   /* copy value */
204   VikLayerParam *newpref = g_new(VikLayerParam,1);
205   *newpref = *pref;
206   VikLayerTypedParamData *newval = vik_layer_typed_param_data_copy_from_data(pref->type, defaultval);
207   if ( group_key )
208     newpref->group = preferences_groups_key_to_index ( group_key );
209
210   g_ptr_array_add ( params, newpref );
211   g_hash_table_insert ( values, (gchar *)pref->name, newval );
212 }
213
214 void a_preferences_init()
215 {
216   preferences_groups_init();
217
218   /* not copied */
219   params = g_ptr_array_new ();
220
221   /* key not copied (same ptr as in pref), actual param data yes */
222   values = g_hash_table_new_full ( g_str_hash, g_str_equal, NULL, vik_layer_typed_param_data_free);
223
224   loaded = FALSE;
225 }
226
227 void a_preferences_uninit()
228 {
229   preferences_groups_uninit();
230
231   g_ptr_array_foreach ( params, (GFunc)g_free, NULL );
232   g_ptr_array_free ( params, TRUE );
233   g_hash_table_destroy ( values );
234 }
235
236
237
238 VikLayerParamData *a_preferences_get(const gchar *key)
239 {
240   if ( ! loaded ) {
241     /* since we can't load the file in a_preferences_init (no params registered yet),
242      * do it once before we get the first key. */
243     preferences_load_from_file();
244     loaded = TRUE;
245   }
246   return g_hash_table_lookup ( values, key );
247 }