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