#include <glib/gi18n.h>
#include <string.h>
#include <stdlib.h>
+#include <stdio.h>
+#include <glib/gstdio.h>
#include "preferences.h"
#include "file.h"
#define TEST_BOOLEAN(str) (! ((str)[0] == '\0' || (str)[0] == '0' || (str)[0] == 'n' || (str)[0] == 'N' || (str)[0] == 'f' || (str)[0] == 'F') )
-/** application wide parameters. */
-static VikLayerParam prefs[] = {
- { "geocaching.username", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("geocaching.com username:"), VIK_LAYER_WIDGET_ENTRY },
- { "geocaching.password", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("geocaching.com password:"), VIK_LAYER_WIDGET_ENTRY },
-};
-
static GPtrArray *params;
static GHashTable *values;
+gboolean loaded;
+
+/************ groups *********/
+
+static GPtrArray *groups_names;
+static GHashTable *groups_keys_to_indices; // contains gint, NULL (0) is not found, instead 1 is used for 0, 2 for 1, etc.
+
+static void preferences_groups_init()
+{
+ groups_names = g_ptr_array_new();
+ groups_keys_to_indices = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, NULL );
+}
+
+static void preferences_groups_uninit()
+{
+ g_ptr_array_free ( groups_names, TRUE );
+ g_hash_table_destroy ( groups_keys_to_indices );
+}
+
+void a_preferences_register_group ( const gchar *key, const gchar *name )
+{
+ if ( g_hash_table_lookup ( groups_keys_to_indices, key ) )
+ g_error("Duplicate preferences group keys");
+ else {
+ g_ptr_array_add ( groups_names, g_strdup(name) );
+ g_hash_table_insert ( groups_keys_to_indices, g_strdup(key), GINT_TO_POINTER ( (gint) groups_names->len ) ); /* index + 1 */
+ }
+}
+
+/* returns -1 if not found. */
+static gint16 preferences_groups_key_to_index( const gchar *key )
+{
+ gint index = GPOINTER_TO_INT ( g_hash_table_lookup ( groups_keys_to_indices, key ) );
+ if ( ! index )
+ return VIK_LAYER_GROUP_NONE; /* which should be -1 anyway */
+ return (gint16) (index - 1);
+}
+
+/*****************************/
/************/
static gboolean preferences_load_parse_param(gchar *buf, gchar **key, gchar **val )
{
gchar *eq_pos;
+ gint len;
+
// comments, special characters in viking file format
if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
return FALSE;
return FALSE;
*key = g_strndup ( buf, eq_pos - buf );
*val = eq_pos + 1;
+ len = strlen(*val);
+ if ( len > 0 )
+ if ( (*val)[len - 1] == '\n' )
+ (*val) [ len - 1 ] = '\0'; /* cut off newline */
return TRUE;
}
static gboolean preferences_load_from_file()
{
gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
- FILE *f = fopen(fn, "r");
+ FILE *f = g_fopen(fn, "r");
g_free ( fn );
if ( f ) {
if ( oldval->type == VIK_LAYER_PARAM_STRING_LIST )
g_error ( "Param strings not implemented in preferences"); // fake it
- g_free(key);
-
newval = layer_data_typed_param_copy_from_string ( oldval->type, val );
g_hash_table_insert ( values, key, newval );
+ g_free(key);
+
// change value
}
}
fclose(f);
+ f = NULL;
return TRUE;
}
return FALSE;
gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
// TODO: error checking
- FILE *f = fopen(fn, "w");
+ FILE *f = g_fopen(fn, "w");
g_free ( fn );
if ( f ) {
file_write_layer_param ( f, param->name, val->type, val->data );
}
fclose(f);
+ f = NULL;
return TRUE;
}
for ( i = 0; i < params->len; i++ ) {
contiguous_params[i] = *((VikLayerParam*)(g_ptr_array_index(params,i)));
}
-
+ loaded = TRUE;
preferences_load_from_file();
if ( a_uibuilder_properties_factory ( parent, contiguous_params, params_count,
- NULL, 0, // groups, groups_count, // groups? what groups?!
+ (gchar **) groups_names->pdata, groups_names->len, // groups, groups_count, // groups? what groups?!
(gboolean (*) (gpointer,guint16,VikLayerParamData,gpointer)) preferences_run_setparam,
NULL /* not used */, contiguous_params,
preferences_run_getparam, NULL /* not used */ ) ) {
g_free ( contiguous_params );
}
-void a_preferences_register(VikLayerParam *pref, VikLayerParamData defaultval)
+void a_preferences_register(VikLayerParam *pref, VikLayerParamData defaultval, const gchar *group_key )
{
/* copy value */
+ VikLayerParam *newpref = g_new(VikLayerParam,1);
+ *newpref = *pref;
VikLayerTypedParamData *newval = layer_typed_param_data_copy_from_data(pref->type, defaultval);
+ if ( group_key )
+ newpref->group = preferences_groups_key_to_index ( group_key );
- g_ptr_array_add ( params, pref );
+ g_ptr_array_add ( params, newpref );
g_hash_table_insert ( values, (gchar *)pref->name, newval );
}
void a_preferences_init()
{
+ preferences_groups_init();
+
/* not copied */
params = g_ptr_array_new ();
/* key not copied (same ptr as in pref), actual param data yes */
values = g_hash_table_new_full ( g_str_hash, g_str_equal, NULL, layer_typed_param_data_free);
- VikLayerParamData tmp;
- tmp.s = "hello world";
- a_preferences_register(prefs, tmp);
- a_preferences_register(prefs+1, tmp);
-
- /* load from file? */
+ loaded = FALSE;
}
void a_preferences_uninit()
{
- g_ptr_array_free ( params, FALSE );
+ preferences_groups_uninit();
+
+ g_ptr_array_free ( params, TRUE );
g_hash_table_destroy ( values );
}
VikLayerParamData *a_preferences_get(const gchar *key)
{
+ if ( ! loaded ) {
+ /* since we can't load the file in a_preferences_init (no params registered yet),
+ * do it once before we get the first key. */
+ preferences_load_from_file();
+ loaded = TRUE;
+ }
return g_hash_table_lookup ( values, key );
}