Portability: remove dirname needs with g_mkdir_with_parents
[viking.git] / src / preferences.c
blob844bb5164b61d1e9d66c54803bf26423e40e7c0d
1 #include <gtk/gtk.h>
2 #include <glib/gi18n.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include "preferences.h"
7 #include "file.h"
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...
16 #define VIKING_PREFS_FILE "viking.prefs"
18 #define TEST_BOOLEAN(str) (! ((str)[0] == '\0' || (str)[0] == '0' || (str)[0] == 'n' || (str)[0] == 'N' || (str)[0] == 'f' || (str)[0] == 'F') )
20 static GPtrArray *params;
21 static GHashTable *values;
22 gboolean loaded;
24 /************ groups *********/
26 static GPtrArray *groups_names;
27 static GHashTable *groups_keys_to_indices; // contains gint, NULL (0) is not found, instead 1 is used for 0, 2 for 1, etc.
29 static void preferences_groups_init()
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 );
35 static void preferences_groups_uninit()
37 g_ptr_array_free ( groups_names, TRUE );
38 g_hash_table_destroy ( groups_keys_to_indices );
41 void a_preferences_register_group ( const gchar *key, const gchar *name )
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 */
51 /* returns -1 if not found. */
52 static gint16 preferences_groups_key_to_index( const gchar *key )
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);
60 /*****************************/
62 /************/
64 typedef struct {
65 VikLayerParamData data;
66 guint8 type;
67 gpointer freeme; // because data.s is const and the compiler complains
68 } VikLayerTypedParamData;
70 void layer_typed_param_data_free(gpointer p)
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.
83 case VIK_LAYER_PARAM_STRING_LIST:
84 g_error ( "Param strings not implemented in preferences"); //fake it
85 break;
87 g_free ( val );
90 VikLayerTypedParamData *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;
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.
106 case VIK_LAYER_PARAM_STRING_LIST:
107 g_error ( "Param strings not implemented in preferences"); //fake it
108 break;
110 return newval;
113 /* TODO: share this code with file.c */
114 VikLayerTypedParamData *layer_data_typed_param_copy_from_string ( guint8 type, const gchar *str )
116 g_assert ( type != VIK_LAYER_PARAM_STRING_LIST );
117 VikLayerTypedParamData *rv = g_new(VikLayerTypedParamData,1);
118 rv->type = type;
119 switch ( type )
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;
134 return rv;
137 /************/
139 /* MAKES A COPY OF THE KEY!!! */
140 static gboolean preferences_load_parse_param(gchar *buf, gchar **key, gchar **val )
142 gchar *eq_pos;
143 gint len;
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;
153 len = strlen(*val);
154 if ( len > 0 )
155 if ( (*val)[len - 1] == '\n' )
156 (*val) [ len - 1 ] = '\0'; /* cut off newline */
157 return TRUE;
160 static gboolean preferences_load_from_file()
162 gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
163 FILE *f = g_fopen(fn, "r");
164 g_free ( fn );
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;
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
185 newval = layer_data_typed_param_copy_from_string ( oldval->type, val );
186 g_hash_table_insert ( values, key, newval );
188 g_free(key);
190 // change value
193 fclose(f);
194 f = NULL;
195 return TRUE;
197 return FALSE;
200 static void preferences_run_setparam ( gpointer notused, guint16 i, VikLayerParamData data, VikLayerParam *params )
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) );
207 static VikLayerParamData preferences_run_getparam ( gpointer notused, guint16 i )
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;
216 /* TRUE on success */
217 static gboolean preferences_save_to_file()
219 gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
221 // TODO: error checking
222 FILE *f = g_fopen(fn, "w");
223 g_free ( fn );
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 );
235 fclose(f);
236 f = NULL;
237 return TRUE;
240 return FALSE;
244 void 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)));
253 loaded = TRUE;
254 preferences_load_from_file();
255 if ( a_uibuilder_properties_factory ( parent, contiguous_params, params_count,
256 (gchar **) groups_names->pdata, groups_names->len, // groups, groups_count, // groups? what groups?!
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();
262 g_free ( contiguous_params );
265 void a_preferences_register(VikLayerParam *pref, VikLayerParamData defaultval, const gchar *group_key )
267 /* copy value */
268 VikLayerParam *newpref = g_new(VikLayerParam,1);
269 *newpref = *pref;
270 VikLayerTypedParamData *newval = layer_typed_param_data_copy_from_data(pref->type, defaultval);
271 if ( group_key )
272 newpref->group = preferences_groups_key_to_index ( group_key );
274 g_ptr_array_add ( params, newpref );
275 g_hash_table_insert ( values, (gchar *)pref->name, newval );
278 void a_preferences_init()
280 preferences_groups_init();
282 /* not copied */
283 params = g_ptr_array_new ();
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);
288 loaded = FALSE;
291 void a_preferences_uninit()
293 preferences_groups_uninit();
295 g_ptr_array_free ( params, TRUE );
296 g_hash_table_destroy ( values );
301 VikLayerParamData *a_preferences_get(const gchar *key)
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;
309 return g_hash_table_lookup ( values, key );