]> git.street.me.uk Git - andy/viking.git/blame - src/preferences.c
Allow zoom to 1/32.
[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{
54 g_ptr_array_free ( groups_names, TRUE );
55 g_hash_table_destroy ( groups_keys_to_indices );
56}
57
58void a_preferences_register_group ( const gchar *key, const gchar *name )
59{
60 if ( g_hash_table_lookup ( groups_keys_to_indices, key ) )
24ff9c7b 61 g_critical("Duplicate preferences group keys");
a5c8699d
EB
62 else {
63 g_ptr_array_add ( groups_names, g_strdup(name) );
dc2c040e 64 g_hash_table_insert ( groups_keys_to_indices, g_strdup(key), GINT_TO_POINTER ( (gint) groups_names->len ) ); /* index + 1 */
a5c8699d
EB
65 }
66}
67
68/* returns -1 if not found. */
69static gint16 preferences_groups_key_to_index( const gchar *key )
70{
dc2c040e 71 gint index = GPOINTER_TO_INT ( g_hash_table_lookup ( groups_keys_to_indices, key ) );
a5c8699d
EB
72 if ( ! index )
73 return VIK_LAYER_GROUP_NONE; /* which should be -1 anyway */
74 return (gint16) (index - 1);
75}
76
77/*****************************/
8339c44d 78
8339c44d
EB
79static gboolean preferences_load_from_file()
80{
81 gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
8c060406 82 FILE *f = g_fopen(fn, "r");
8339c44d
EB
83 g_free ( fn );
84
85 if ( f ) {
86 gchar buf[4096];
87 gchar *key, *val;
88 VikLayerTypedParamData *oldval, *newval;
89 while ( ! feof (f) ) {
f61ca8cb
JB
90 if (fgets(buf,sizeof(buf),f) == NULL)
91 break;
0da89d90 92 if ( split_string_from_file_on_equals ( buf, &key, &val ) ) {
8339c44d
EB
93 // if it's not in there, ignore it
94 oldval = g_hash_table_lookup ( values, key );
95 if ( ! oldval ) {
96 g_free(key);
0da89d90 97 g_free(val);
8339c44d
EB
98 continue;
99 }
100
101 // otherwise change it (you know the type!)
102 // if it's a string list do some funky stuff ... yuck... not yet.
103 if ( oldval->type == VIK_LAYER_PARAM_STRING_LIST )
24ff9c7b 104 g_critical ( "Param strings not implemented in preferences"); // fake it
8339c44d 105
176e0989 106 newval = vik_layer_data_typed_param_copy_from_string ( oldval->type, val );
8339c44d
EB
107 g_hash_table_insert ( values, key, newval );
108
a5c8699d 109 g_free(key);
0da89d90 110 g_free(val);
8339c44d
EB
111 // change value
112 }
113 }
114 fclose(f);
8c060406 115 f = NULL;
8339c44d
EB
116 return TRUE;
117 }
118 return FALSE;
119}
120
121static void preferences_run_setparam ( gpointer notused, guint16 i, VikLayerParamData data, VikLayerParam *params )
122{
123 if ( params[i].type == VIK_LAYER_PARAM_STRING_LIST )
24ff9c7b 124 g_critical ( "Param strings not implemented in preferences"); //fake it
176e0989 125 g_hash_table_insert ( values, (gchar *)(params[i].name), vik_layer_typed_param_data_copy_from_data(params[i].type, data) );
8339c44d
EB
126}
127
45e2a963
RN
128/* Allow preferences to be manipulated externally */
129void a_preferences_run_setparam ( VikLayerParamData data, VikLayerParam *params )
130{
131 preferences_run_setparam (NULL, 0, data, params);
132}
133
153af709 134static VikLayerParamData preferences_run_getparam ( gpointer notused, guint16 i, gboolean notused2 )
8339c44d
EB
135{
136 VikLayerTypedParamData *val = (VikLayerTypedParamData *) g_hash_table_lookup ( values, ((VikLayerParam *)g_ptr_array_index(params,i))->name );
137 g_assert ( val != NULL );
138 if ( val->type == VIK_LAYER_PARAM_STRING_LIST )
24ff9c7b 139 g_critical ( "Param strings not implemented in preferences"); //fake it
8339c44d
EB
140 return val->data;
141}
142
143/* TRUE on success */
45e2a963 144gboolean a_preferences_save_to_file()
8339c44d
EB
145{
146 gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
147
148 // TODO: error checking
8c060406 149 FILE *f = g_fopen(fn, "w");
3e9ca23e
GS
150 /* Since preferences files saves OSM login credentials,
151 * it'll be better to store it in secret.
152 */
153 g_chmod(fn, 0600);
8339c44d
EB
154 g_free ( fn );
155
156 if ( f ) {
157 VikLayerParam *param;
158 VikLayerTypedParamData *val;
159 int i;
160 for ( i = 0; i < params->len; i++ ) {
161 param = (VikLayerParam *) g_ptr_array_index(params,i);
162 val = (VikLayerTypedParamData *) g_hash_table_lookup ( values, param->name );
163 g_assert ( val != NULL );
164 file_write_layer_param ( f, param->name, val->type, val->data );
165 }
166 fclose(f);
8c060406 167 f = NULL;
8339c44d
EB
168 return TRUE;
169 }
170
171 return FALSE;
172}
173
174
175void a_preferences_show_window(GtkWindow *parent) {
176 //VikLayerParamData *a_uibuilder_run_dialog ( GtkWindow *parent, VikLayerParam \*params, // guint16 params_count, gchar **groups, guint8 groups_count, // VikLayerParamData *params_defaults )
177 // TODO: THIS IS A MAJOR HACKAROUND, but ok when we have only a couple preferences.
178 gint params_count = params->len;
179 VikLayerParam *contiguous_params = g_new(VikLayerParam,params_count);
180 int i;
181 for ( i = 0; i < params->len; i++ ) {
182 contiguous_params[i] = *((VikLayerParam*)(g_ptr_array_index(params,i)));
183 }
a5c8699d 184 loaded = TRUE;
8339c44d 185 preferences_load_from_file();
13fde155 186 if ( a_uibuilder_properties_factory ( _("Preferences"), parent, contiguous_params, params_count,
a5c8699d 187 (gchar **) groups_names->pdata, groups_names->len, // groups, groups_count, // groups? what groups?!
153af709 188 (gboolean (*) (gpointer,guint16,VikLayerParamData,gpointer,gboolean)) preferences_run_setparam,
8339c44d
EB
189 NULL /* not used */, contiguous_params,
190 preferences_run_getparam, NULL /* not used */ ) ) {
45e2a963 191 a_preferences_save_to_file();
8339c44d
EB
192 }
193 g_free ( contiguous_params );
194}
195
a5c8699d 196void a_preferences_register(VikLayerParam *pref, VikLayerParamData defaultval, const gchar *group_key )
8339c44d
EB
197{
198 /* copy value */
a5c8699d
EB
199 VikLayerParam *newpref = g_new(VikLayerParam,1);
200 *newpref = *pref;
176e0989 201 VikLayerTypedParamData *newval = vik_layer_typed_param_data_copy_from_data(pref->type, defaultval);
a5c8699d
EB
202 if ( group_key )
203 newpref->group = preferences_groups_key_to_index ( group_key );
8339c44d 204
a5c8699d 205 g_ptr_array_add ( params, newpref );
8339c44d
EB
206 g_hash_table_insert ( values, (gchar *)pref->name, newval );
207}
208
209void a_preferences_init()
210{
a5c8699d
EB
211 preferences_groups_init();
212
8339c44d
EB
213 /* not copied */
214 params = g_ptr_array_new ();
215
216 /* key not copied (same ptr as in pref), actual param data yes */
176e0989 217 values = g_hash_table_new_full ( g_str_hash, g_str_equal, NULL, vik_layer_typed_param_data_free);
8339c44d 218
a5c8699d 219 loaded = FALSE;
8339c44d
EB
220}
221
222void a_preferences_uninit()
223{
a5c8699d
EB
224 preferences_groups_uninit();
225
226 g_ptr_array_free ( params, TRUE );
8339c44d
EB
227 g_hash_table_destroy ( values );
228}
229
230
231
232VikLayerParamData *a_preferences_get(const gchar *key)
233{
a5c8699d
EB
234 if ( ! loaded ) {
235 /* since we can't load the file in a_preferences_init (no params registered yet),
236 * do it once before we get the first key. */
237 preferences_load_from_file();
238 loaded = TRUE;
239 }
8339c44d
EB
240 return g_hash_table_lookup ( values, key );
241}