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