]> git.street.me.uk Git - andy/viking.git/blob - src/preferences.c
[QA] Move VikLayerTypedParamData from preferences into viklayer header
[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
30 // TODO: STRING_LIST
31 // TODO: share code in file reading
32 // TODO: remove hackaround in show_window
33
34 #define VIKING_PREFS_FILE "viking.prefs"
35
36 static GPtrArray *params;
37 static GHashTable *values;
38 gboolean loaded;
39
40 /************ groups *********/
41
42 static GPtrArray *groups_names;
43 static GHashTable *groups_keys_to_indices; // contains gint, NULL (0) is not found, instead 1 is used for 0, 2 for 1, etc.
44
45 static void preferences_groups_init()
46 {
47   groups_names = g_ptr_array_new();
48   groups_keys_to_indices = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, NULL );
49 }
50
51 static void preferences_groups_uninit()
52 {
53   g_ptr_array_free ( groups_names, TRUE );
54   g_hash_table_destroy ( groups_keys_to_indices );
55 }
56
57 void a_preferences_register_group ( const gchar *key, const gchar *name )
58 {
59   if ( g_hash_table_lookup ( groups_keys_to_indices, key ) )
60     g_critical("Duplicate preferences group keys");
61   else {
62     g_ptr_array_add ( groups_names, g_strdup(name) );
63     g_hash_table_insert ( groups_keys_to_indices, g_strdup(key), GINT_TO_POINTER ( (gint) groups_names->len ) ); /* index + 1 */
64   }
65 }
66
67 /* returns -1 if not found. */
68 static gint16 preferences_groups_key_to_index( const gchar *key )
69 {
70   gint index = GPOINTER_TO_INT ( g_hash_table_lookup ( groups_keys_to_indices, key ) );
71   if ( ! index )
72     return VIK_LAYER_GROUP_NONE; /* which should be -1 anyway */
73   return (gint16) (index - 1);
74 }
75
76 /*****************************/
77
78 /* MAKES A COPY OF THE KEY!!! */
79 static gboolean preferences_load_parse_param(gchar *buf, gchar **key, gchar **val )
80 {
81   gchar *eq_pos;
82   gint len;
83
84   // comments, special characters in viking file format
85   if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
86     return FALSE;
87   eq_pos = strchr ( buf, '=' );
88   if ( ! eq_pos )
89     return FALSE;
90   *key = g_strndup ( buf, eq_pos - buf );
91   *val = eq_pos + 1;
92   len = strlen(*val);
93   if ( len > 0 )
94     if ( (*val)[len - 1] == '\n' )
95       (*val) [ len - 1 ] = '\0'; /* cut off newline */
96   return TRUE;
97 }
98
99 static gboolean preferences_load_from_file()
100 {
101   gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
102   FILE *f = g_fopen(fn, "r");
103   g_free ( fn );
104
105   if ( f ) {
106     gchar buf[4096];
107     gchar *key, *val;
108     VikLayerTypedParamData *oldval, *newval;
109     while ( ! feof (f) ) {
110       if (fgets(buf,sizeof(buf),f) == NULL)
111         break;
112       if ( preferences_load_parse_param(buf, &key, &val ) ) {
113         // if it's not in there, ignore it
114         oldval = g_hash_table_lookup ( values, key );
115         if ( ! oldval ) {
116           g_free(key);
117           continue;
118         }
119
120         // otherwise change it (you know the type!)
121         // if it's a string list do some funky stuff ... yuck... not yet.
122         if ( oldval->type == VIK_LAYER_PARAM_STRING_LIST )
123           g_critical ( "Param strings not implemented in preferences"); // fake it
124
125         newval = vik_layer_data_typed_param_copy_from_string ( oldval->type, val );
126         g_hash_table_insert ( values, key, newval );
127
128         g_free(key);
129         // change value
130       }
131     }
132     fclose(f);
133     f = NULL;
134     return TRUE;
135   }
136   return FALSE;
137 }
138
139 static void preferences_run_setparam ( gpointer notused, guint16 i, VikLayerParamData data, VikLayerParam *params )
140 {
141   if ( params[i].type == VIK_LAYER_PARAM_STRING_LIST )
142     g_critical ( "Param strings not implemented in preferences"); //fake it
143   g_hash_table_insert ( values, (gchar *)(params[i].name), vik_layer_typed_param_data_copy_from_data(params[i].type, data) );
144 }
145
146 /* Allow preferences to be manipulated externally */
147 void a_preferences_run_setparam ( VikLayerParamData data, VikLayerParam *params )
148 {
149   preferences_run_setparam (NULL, 0, data, params);
150 }
151
152 static VikLayerParamData preferences_run_getparam ( gpointer notused, guint16 i, gboolean notused2 )
153 {
154   VikLayerTypedParamData *val = (VikLayerTypedParamData *) g_hash_table_lookup ( values, ((VikLayerParam *)g_ptr_array_index(params,i))->name );
155   g_assert ( val != NULL );
156   if ( val->type == VIK_LAYER_PARAM_STRING_LIST )
157     g_critical ( "Param strings not implemented in preferences"); //fake it
158   return val->data;
159 }
160
161 /* TRUE on success */
162 gboolean a_preferences_save_to_file()
163 {
164   gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
165
166   // TODO: error checking
167   FILE *f = g_fopen(fn, "w");
168   /* Since preferences files saves OSM login credentials,
169    * it'll be better to store it in secret.
170    */
171   g_chmod(fn, 0600);
172   g_free ( fn );
173
174   if ( f ) {
175     VikLayerParam *param;
176     VikLayerTypedParamData *val;
177     int i;
178     for ( i = 0; i < params->len; i++ ) {
179       param = (VikLayerParam *) g_ptr_array_index(params,i);
180       val = (VikLayerTypedParamData *) g_hash_table_lookup ( values, param->name );
181       g_assert ( val != NULL );
182       file_write_layer_param ( f, param->name, val->type, val->data );
183     }
184     fclose(f);
185     f = NULL;
186     return TRUE;
187   }
188
189   return FALSE;
190 }
191
192
193 void a_preferences_show_window(GtkWindow *parent) {
194     //VikLayerParamData *a_uibuilder_run_dialog ( GtkWindow *parent, VikLayerParam \*params, // guint16 params_count, gchar **groups, guint8 groups_count, // VikLayerParamData *params_defaults )
195     // TODO: THIS IS A MAJOR HACKAROUND, but ok when we have only a couple preferences.
196     gint params_count = params->len;
197     VikLayerParam *contiguous_params = g_new(VikLayerParam,params_count);
198     int i;
199     for ( i = 0; i < params->len; i++ ) {
200       contiguous_params[i] = *((VikLayerParam*)(g_ptr_array_index(params,i)));
201     }
202     loaded = TRUE;
203     preferences_load_from_file();
204     if ( a_uibuilder_properties_factory ( _("Preferences"), parent, contiguous_params, params_count,
205                                 (gchar **) groups_names->pdata, groups_names->len, // groups, groups_count, // groups? what groups?!
206                                 (gboolean (*) (gpointer,guint16,VikLayerParamData,gpointer,gboolean)) preferences_run_setparam,
207                                 NULL /* not used */, contiguous_params,
208                                 preferences_run_getparam, NULL /* not used */ ) ) {
209       a_preferences_save_to_file();
210     }
211     g_free ( contiguous_params );
212 }
213
214 void a_preferences_register(VikLayerParam *pref, VikLayerParamData defaultval, const gchar *group_key )
215 {
216   /* copy value */
217   VikLayerParam *newpref = g_new(VikLayerParam,1);
218   *newpref = *pref;
219   VikLayerTypedParamData *newval = vik_layer_typed_param_data_copy_from_data(pref->type, defaultval);
220   if ( group_key )
221     newpref->group = preferences_groups_key_to_index ( group_key );
222
223   g_ptr_array_add ( params, newpref );
224   g_hash_table_insert ( values, (gchar *)pref->name, newval );
225 }
226
227 void a_preferences_init()
228 {
229   preferences_groups_init();
230
231   /* not copied */
232   params = g_ptr_array_new ();
233
234   /* key not copied (same ptr as in pref), actual param data yes */
235   values = g_hash_table_new_full ( g_str_hash, g_str_equal, NULL, vik_layer_typed_param_data_free);
236
237   loaded = FALSE;
238 }
239
240 void a_preferences_uninit()
241 {
242   preferences_groups_uninit();
243
244   g_ptr_array_free ( params, TRUE );
245   g_hash_table_destroy ( values );
246 }
247
248
249
250 VikLayerParamData *a_preferences_get(const gchar *key)
251 {
252   if ( ! loaded ) {
253     /* since we can't load the file in a_preferences_init (no params registered yet),
254      * do it once before we get the first key. */
255     preferences_load_from_file();
256     loaded = TRUE;
257   }
258   return g_hash_table_lookup ( values, key );
259 }