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