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