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