2 #include <glib/gi18n.h>
6 #include <glib/gstdio.h>
7 #include "preferences.h"
10 // TODO: register_group
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...
17 #define VIKING_PREFS_FILE "viking.prefs"
19 #define TEST_BOOLEAN(str) (! ((str)[0] == '\0' || (str)[0] == '0' || (str)[0] == 'n' || (str)[0] == 'N' || (str)[0] == 'f' || (str)[0] == 'F') )
21 static GPtrArray
*params
;
22 static GHashTable
*values
;
25 /************ groups *********/
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.
30 static void preferences_groups_init()
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
);
36 static void preferences_groups_uninit()
38 g_ptr_array_free ( groups_names
, TRUE
);
39 g_hash_table_destroy ( groups_keys_to_indices
);
42 void a_preferences_register_group ( const gchar
*key
, const gchar
*name
)
44 if ( g_hash_table_lookup ( groups_keys_to_indices
, key
) )
45 g_error("Duplicate preferences group keys");
47 g_ptr_array_add ( groups_names
, g_strdup(name
) );
48 g_hash_table_insert ( groups_keys_to_indices
, g_strdup(key
), GINT_TO_POINTER ( (gint
) groups_names
->len
) ); /* index + 1 */
52 /* returns -1 if not found. */
53 static gint16
preferences_groups_key_to_index( const gchar
*key
)
55 gint index
= GPOINTER_TO_INT ( g_hash_table_lookup ( groups_keys_to_indices
, key
) );
57 return VIK_LAYER_GROUP_NONE
; /* which should be -1 anyway */
58 return (gint16
) (index
- 1);
61 /*****************************/
66 VikLayerParamData data
;
68 gpointer freeme
; // because data.s is const and the compiler complains
69 } VikLayerTypedParamData
;
71 void layer_typed_param_data_free(gpointer p
)
73 VikLayerTypedParamData
*val
= (VikLayerTypedParamData
*)p
;
74 switch ( val
->type
) {
75 case VIK_LAYER_PARAM_STRING
:
77 g_free ( val
->freeme
);
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.
84 case VIK_LAYER_PARAM_STRING_LIST
:
85 g_error ( "Param strings not implemented in preferences"); //fake it
91 VikLayerTypedParamData
*layer_typed_param_data_copy_from_data(guint8 type
, VikLayerParamData val
) {
92 VikLayerTypedParamData
*newval
= g_new(VikLayerTypedParamData
,1);
95 switch ( newval
->type
) {
96 case VIK_LAYER_PARAM_STRING
: {
97 gchar
*s
= g_strdup(newval
->data
.s
);
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.
107 case VIK_LAYER_PARAM_STRING_LIST
:
108 g_error ( "Param strings not implemented in preferences"); //fake it
114 /* TODO: share this code with file.c */
115 VikLayerTypedParamData
*layer_data_typed_param_copy_from_string ( guint8 type
, const gchar
*str
)
117 g_assert ( type
!= VIK_LAYER_PARAM_STRING_LIST
);
118 VikLayerTypedParamData
*rv
= g_new(VikLayerTypedParamData
,1);
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 */
130 gchar
*s
= g_strdup(str
);
140 /* MAKES A COPY OF THE KEY!!! */
141 static gboolean
preferences_load_parse_param(gchar
*buf
, gchar
**key
, gchar
**val
)
146 // comments, special characters in viking file format
147 if ( buf
== NULL
|| buf
[0] == '\0' || buf
[0] == '~' || buf
[0] == '=' || buf
[0] == '#' )
149 eq_pos
= strchr ( buf
, '=' );
152 *key
= g_strndup ( buf
, eq_pos
- buf
);
156 if ( (*val
)[len
- 1] == '\n' )
157 (*val
) [ len
- 1 ] = '\0'; /* cut off newline */
161 static gboolean
preferences_load_from_file()
163 gchar
*fn
= g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE
, NULL
);
164 FILE *f
= g_fopen(fn
, "r");
170 VikLayerTypedParamData
*oldval
, *newval
;
171 while ( ! feof (f
) ) {
172 fgets(buf
,sizeof(buf
),f
);
173 if ( preferences_load_parse_param(buf
, &key
, &val
) ) {
174 // if it's not in there, ignore it
175 oldval
= g_hash_table_lookup ( values
, key
);
181 // otherwise change it (you know the type!)
182 // if it's a string list do some funky stuff ... yuck... not yet.
183 if ( oldval
->type
== VIK_LAYER_PARAM_STRING_LIST
)
184 g_error ( "Param strings not implemented in preferences"); // fake it
186 newval
= layer_data_typed_param_copy_from_string ( oldval
->type
, val
);
187 g_hash_table_insert ( values
, key
, newval
);
201 static void preferences_run_setparam ( gpointer notused
, guint16 i
, VikLayerParamData data
, VikLayerParam
*params
)
203 if ( params
[i
].type
== VIK_LAYER_PARAM_STRING_LIST
)
204 g_error ( "Param strings not implemented in preferences"); //fake it
205 g_hash_table_insert ( values
, (gchar
*)(params
[i
].name
), layer_typed_param_data_copy_from_data(params
[i
].type
, data
) );
208 static VikLayerParamData
preferences_run_getparam ( gpointer notused
, guint16 i
)
210 VikLayerTypedParamData
*val
= (VikLayerTypedParamData
*) g_hash_table_lookup ( values
, ((VikLayerParam
*)g_ptr_array_index(params
,i
))->name
);
211 g_assert ( val
!= NULL
);
212 if ( val
->type
== VIK_LAYER_PARAM_STRING_LIST
)
213 g_error ( "Param strings not implemented in preferences"); //fake it
217 /* TRUE on success */
218 static gboolean
preferences_save_to_file()
220 gchar
*fn
= g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE
, NULL
);
222 // TODO: error checking
223 FILE *f
= g_fopen(fn
, "w");
224 /* Since preferences files saves OSM login credentials,
225 * it'll be better to store it in secret.
231 VikLayerParam
*param
;
232 VikLayerTypedParamData
*val
;
234 for ( i
= 0; i
< params
->len
; i
++ ) {
235 param
= (VikLayerParam
*) g_ptr_array_index(params
,i
);
236 val
= (VikLayerTypedParamData
*) g_hash_table_lookup ( values
, param
->name
);
237 g_assert ( val
!= NULL
);
238 file_write_layer_param ( f
, param
->name
, val
->type
, val
->data
);
249 void a_preferences_show_window(GtkWindow
*parent
) {
250 //VikLayerParamData *a_uibuilder_run_dialog ( GtkWindow *parent, VikLayerParam \*params, // guint16 params_count, gchar **groups, guint8 groups_count, // VikLayerParamData *params_defaults )
251 // TODO: THIS IS A MAJOR HACKAROUND, but ok when we have only a couple preferences.
252 gint params_count
= params
->len
;
253 VikLayerParam
*contiguous_params
= g_new(VikLayerParam
,params_count
);
255 for ( i
= 0; i
< params
->len
; i
++ ) {
256 contiguous_params
[i
] = *((VikLayerParam
*)(g_ptr_array_index(params
,i
)));
259 preferences_load_from_file();
260 if ( a_uibuilder_properties_factory ( parent
, contiguous_params
, params_count
,
261 (gchar
**) groups_names
->pdata
, groups_names
->len
, // groups, groups_count, // groups? what groups?!
262 (gboolean (*) (gpointer
,guint16
,VikLayerParamData
,gpointer
)) preferences_run_setparam
,
263 NULL
/* not used */, contiguous_params
,
264 preferences_run_getparam
, NULL
/* not used */ ) ) {
265 preferences_save_to_file();
267 g_free ( contiguous_params
);
270 void a_preferences_register(VikLayerParam
*pref
, VikLayerParamData defaultval
, const gchar
*group_key
)
273 VikLayerParam
*newpref
= g_new(VikLayerParam
,1);
275 VikLayerTypedParamData
*newval
= layer_typed_param_data_copy_from_data(pref
->type
, defaultval
);
277 newpref
->group
= preferences_groups_key_to_index ( group_key
);
279 g_ptr_array_add ( params
, newpref
);
280 g_hash_table_insert ( values
, (gchar
*)pref
->name
, newval
);
283 void a_preferences_init()
285 preferences_groups_init();
288 params
= g_ptr_array_new ();
290 /* key not copied (same ptr as in pref), actual param data yes */
291 values
= g_hash_table_new_full ( g_str_hash
, g_str_equal
, NULL
, layer_typed_param_data_free
);
296 void a_preferences_uninit()
298 preferences_groups_uninit();
300 g_ptr_array_free ( params
, TRUE
);
301 g_hash_table_destroy ( values
);
306 VikLayerParamData
*a_preferences_get(const gchar
*key
)
309 /* since we can't load the file in a_preferences_init (no params registered yet),
310 * do it once before we get the first key. */
311 preferences_load_from_file();
314 return g_hash_table_lookup ( values
, key
);