]> git.street.me.uk Git - andy/viking.git/blob - src/preferences.c
Fix <GTK 2.24 combo box usage.
[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 <stdlib.h>
25 #include <stdio.h>
26 #include <glib/gstdio.h>
27 #include "preferences.h"
28 #include "file.h"
29
30 // TODO: register_group
31 // TODO: STRING_LIST
32 // TODO: share code in file reading
33 // TODO: remove hackaround in show_window
34 // TODO: move typeddata to uibuilder, make it more used & general, it's a "prettier" solution methinks
35 // maybe this wasn't such a good idea...
36
37 #define VIKING_PREFS_FILE "viking.prefs"
38
39 #define TEST_BOOLEAN(str) (! ((str)[0] == '\0' || (str)[0] == '0' || (str)[0] == 'n' || (str)[0] == 'N' || (str)[0] == 'f' || (str)[0] == 'F') )
40
41 static GPtrArray *params;
42 static GHashTable *values;
43 gboolean loaded;
44
45 /************ groups *********/
46
47 static GPtrArray *groups_names;
48 static GHashTable *groups_keys_to_indices; // contains gint, NULL (0) is not found, instead 1 is used for 0, 2 for 1, etc.
49
50 static void preferences_groups_init()
51 {
52   groups_names = g_ptr_array_new();
53   groups_keys_to_indices = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, NULL );
54 }
55
56 static void preferences_groups_uninit()
57 {
58   g_ptr_array_free ( groups_names, TRUE );
59   g_hash_table_destroy ( groups_keys_to_indices );
60 }
61
62 void a_preferences_register_group ( const gchar *key, const gchar *name )
63 {
64   if ( g_hash_table_lookup ( groups_keys_to_indices, key ) )
65     g_error("Duplicate preferences group keys");
66   else {
67     g_ptr_array_add ( groups_names, g_strdup(name) );
68     g_hash_table_insert ( groups_keys_to_indices, g_strdup(key), GINT_TO_POINTER ( (gint) groups_names->len ) ); /* index + 1 */
69   }
70 }
71
72 /* returns -1 if not found. */
73 static gint16 preferences_groups_key_to_index( const gchar *key )
74 {
75   gint index = GPOINTER_TO_INT ( g_hash_table_lookup ( groups_keys_to_indices, key ) );
76   if ( ! index )
77     return VIK_LAYER_GROUP_NONE; /* which should be -1 anyway */
78   return (gint16) (index - 1);
79 }
80
81 /*****************************/
82
83 /************/
84
85 typedef struct {
86   VikLayerParamData data;
87   guint8 type;
88   gpointer freeme; // because data.s is const and the compiler complains
89 } VikLayerTypedParamData;
90
91 void layer_typed_param_data_free(gpointer p)
92 {
93   VikLayerTypedParamData *val = (VikLayerTypedParamData *)p;
94   switch ( val->type ) {
95     case VIK_LAYER_PARAM_STRING:
96       if ( val->freeme )
97         g_free ( val->freeme );
98       break;
99     /* TODO: APPLICABLE TO US? NOTE: string layer works auniquely: data.sl should NOT be free'd when
100      * the internals call get_param -- i.e. it should be managed w/in the layer.
101      * The value passed by the internals into set_param should also be managed
102      * by the layer -- i.e. free'd by the layer.
103      */
104     case VIK_LAYER_PARAM_STRING_LIST:
105       g_error ( "Param strings not implemented in preferences"); //fake it
106       break;
107   }
108   g_free ( val );
109 }
110
111 VikLayerTypedParamData *layer_typed_param_data_copy_from_data(guint8 type, VikLayerParamData val) {
112   VikLayerTypedParamData *newval = g_new(VikLayerTypedParamData,1);
113   newval->data = val;
114   newval->type = type;
115   switch ( newval->type ) {
116     case VIK_LAYER_PARAM_STRING: {
117       gchar *s = g_strdup(newval->data.s);
118       newval->data.s = s;
119       newval->freeme = s;
120       break;
121     }
122     /* TODO: APPLICABLE TO US? NOTE: string layer works auniquely: data.sl should NOT be free'd when
123      * the internals call get_param -- i.e. it should be managed w/in the layer.
124      * The value passed by the internals into set_param should also be managed
125      * by the layer -- i.e. free'd by the layer.
126      */
127     case VIK_LAYER_PARAM_STRING_LIST:
128       g_error ( "Param strings not implemented in preferences"); //fake it
129       break;
130   }
131   return newval;
132 }
133
134 /* TODO: share this code with file.c */
135 VikLayerTypedParamData *layer_data_typed_param_copy_from_string ( guint8 type, const gchar *str )
136 {
137   g_assert ( type != VIK_LAYER_PARAM_STRING_LIST );
138   VikLayerTypedParamData *rv = g_new(VikLayerTypedParamData,1);
139   rv->type = type;
140   switch ( type )
141   {
142     case VIK_LAYER_PARAM_DOUBLE: rv->data.d = strtod(str, NULL); break;
143     case VIK_LAYER_PARAM_UINT: rv->data.u = strtoul(str, NULL, 10); break;
144     case VIK_LAYER_PARAM_INT: rv->data.i = strtol(str, NULL, 10); break;
145     case VIK_LAYER_PARAM_BOOLEAN: rv->data.b = TEST_BOOLEAN(str); break;
146     case VIK_LAYER_PARAM_COLOR: memset(&(rv->data.c), 0, sizeof(rv->data.c)); /* default: black */
147       gdk_color_parse ( str, &(rv->data.c) ); break;
148     /* STRING or STRING_LIST -- if STRING_LIST, just set param to add a STRING */
149     default: {
150       gchar *s = g_strdup(str);
151       rv->data.s = s;
152       rv->freeme = s;
153     }
154   }
155   return rv;
156 }
157
158 /************/
159
160 /* MAKES A COPY OF THE KEY!!! */
161 static gboolean preferences_load_parse_param(gchar *buf, gchar **key, gchar **val )
162 {
163   gchar *eq_pos;
164   gint len;
165
166   // comments, special characters in viking file format
167   if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
168     return FALSE;
169   eq_pos = strchr ( buf, '=' );
170   if ( ! eq_pos )
171     return FALSE;
172   *key = g_strndup ( buf, eq_pos - buf );
173   *val = eq_pos + 1;
174   len = strlen(*val);
175   if ( len > 0 )
176     if ( (*val)[len - 1] == '\n' )
177       (*val) [ len - 1 ] = '\0'; /* cut off newline */
178   return TRUE;
179 }
180
181 static gboolean preferences_load_from_file()
182 {
183   gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
184   FILE *f = g_fopen(fn, "r");
185   g_free ( fn );
186
187   if ( f ) {
188     gchar buf[4096];
189     gchar *key, *val;
190     VikLayerTypedParamData *oldval, *newval;
191     while ( ! feof (f) ) {
192       if (fgets(buf,sizeof(buf),f) == NULL)
193         break;
194       if ( preferences_load_parse_param(buf, &key, &val ) ) {
195         // if it's not in there, ignore it
196         oldval = g_hash_table_lookup ( values, key );
197         if ( ! oldval ) {
198           g_free(key);
199           continue;
200         }
201
202         // otherwise change it (you know the type!)
203         // if it's a string list do some funky stuff ... yuck... not yet.
204         if ( oldval->type == VIK_LAYER_PARAM_STRING_LIST )
205           g_error ( "Param strings not implemented in preferences"); // fake it
206
207         newval = layer_data_typed_param_copy_from_string ( oldval->type, val );
208         g_hash_table_insert ( values, key, newval );
209
210         g_free(key);
211
212         // change value
213       }
214     }
215     fclose(f);
216     f = NULL;
217     return TRUE;
218   }
219   return FALSE;
220 }
221
222 static void preferences_run_setparam ( gpointer notused, guint16 i, VikLayerParamData data, VikLayerParam *params )
223 {
224   if ( params[i].type == VIK_LAYER_PARAM_STRING_LIST )
225     g_error ( "Param strings not implemented in preferences"); //fake it
226   g_hash_table_insert ( values, (gchar *)(params[i].name), layer_typed_param_data_copy_from_data(params[i].type, data) );
227 }
228
229 /* Allow preferences to be manipulated externally */
230 void a_preferences_run_setparam ( VikLayerParamData data, VikLayerParam *params )
231 {
232   preferences_run_setparam (NULL, 0, data, params);
233 }
234
235 static VikLayerParamData preferences_run_getparam ( gpointer notused, guint16 i, gboolean notused2 )
236 {
237   VikLayerTypedParamData *val = (VikLayerTypedParamData *) g_hash_table_lookup ( values, ((VikLayerParam *)g_ptr_array_index(params,i))->name );
238   g_assert ( val != NULL );
239   if ( val->type == VIK_LAYER_PARAM_STRING_LIST )
240     g_error ( "Param strings not implemented in preferences"); //fake it
241   return val->data;
242 }
243
244 /* TRUE on success */
245 gboolean a_preferences_save_to_file()
246 {
247   gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
248
249   // TODO: error checking
250   FILE *f = g_fopen(fn, "w");
251   /* Since preferences files saves OSM login credentials,
252    * it'll be better to store it in secret.
253    */
254   g_chmod(fn, 0600);
255   g_free ( fn );
256
257   if ( f ) {
258     VikLayerParam *param;
259     VikLayerTypedParamData *val;
260     int i;
261     for ( i = 0; i < params->len; i++ ) {
262       param = (VikLayerParam *) g_ptr_array_index(params,i);
263       val = (VikLayerTypedParamData *) g_hash_table_lookup ( values, param->name );
264       g_assert ( val != NULL );
265       file_write_layer_param ( f, param->name, val->type, val->data );
266     }
267     fclose(f);
268     f = NULL;
269     return TRUE;
270   }
271
272   return FALSE;
273 }
274
275
276 void a_preferences_show_window(GtkWindow *parent) {
277     //VikLayerParamData *a_uibuilder_run_dialog ( GtkWindow *parent, VikLayerParam \*params, // guint16 params_count, gchar **groups, guint8 groups_count, // VikLayerParamData *params_defaults )
278     // TODO: THIS IS A MAJOR HACKAROUND, but ok when we have only a couple preferences.
279     gint params_count = params->len;
280     VikLayerParam *contiguous_params = g_new(VikLayerParam,params_count);
281     int i;
282     for ( i = 0; i < params->len; i++ ) {
283       contiguous_params[i] = *((VikLayerParam*)(g_ptr_array_index(params,i)));
284     }
285     loaded = TRUE;
286     preferences_load_from_file();
287     if ( a_uibuilder_properties_factory ( _("Preferences"), parent, contiguous_params, params_count,
288                                 (gchar **) groups_names->pdata, groups_names->len, // groups, groups_count, // groups? what groups?!
289                                 (gboolean (*) (gpointer,guint16,VikLayerParamData,gpointer,gboolean)) preferences_run_setparam,
290                                 NULL /* not used */, contiguous_params,
291                                 preferences_run_getparam, NULL /* not used */ ) ) {
292       a_preferences_save_to_file();
293     }
294     g_free ( contiguous_params );
295 }
296
297 void a_preferences_register(VikLayerParam *pref, VikLayerParamData defaultval, const gchar *group_key )
298 {
299   /* copy value */
300   VikLayerParam *newpref = g_new(VikLayerParam,1);
301   *newpref = *pref;
302   VikLayerTypedParamData *newval = layer_typed_param_data_copy_from_data(pref->type, defaultval);
303   if ( group_key )
304     newpref->group = preferences_groups_key_to_index ( group_key );
305
306   g_ptr_array_add ( params, newpref );
307   g_hash_table_insert ( values, (gchar *)pref->name, newval );
308 }
309
310 void a_preferences_init()
311 {
312   preferences_groups_init();
313
314   /* not copied */
315   params = g_ptr_array_new ();
316
317   /* key not copied (same ptr as in pref), actual param data yes */
318   values = g_hash_table_new_full ( g_str_hash, g_str_equal, NULL, layer_typed_param_data_free);
319
320   loaded = FALSE;
321 }
322
323 void a_preferences_uninit()
324 {
325   preferences_groups_uninit();
326
327   g_ptr_array_free ( params, TRUE );
328   g_hash_table_destroy ( values );
329 }
330
331
332
333 VikLayerParamData *a_preferences_get(const gchar *key)
334 {
335   if ( ! loaded ) {
336     /* since we can't load the file in a_preferences_init (no params registered yet),
337      * do it once before we get the first key. */
338     preferences_load_from_file();
339     loaded = TRUE;
340   }
341   return g_hash_table_lookup ( values, key );
342 }