2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2007, Evan Battaglia <gtoevan@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <glib/gi18n.h>
25 #include <glib/gstdio.h>
26 #include "preferences.h"
32 // TODO: share code in file reading
33 // TODO: remove hackaround in show_window
35 #define VIKING_PREFS_FILE "viking.prefs"
37 static GPtrArray *params;
38 static GHashTable *values;
41 /************ groups *********/
43 static GPtrArray *groups_names;
44 static GHashTable *groups_keys_to_indices; // contains gint, NULL (0) is not found, instead 1 is used for 0, 2 for 1, etc.
46 static void preferences_groups_init()
48 groups_names = g_ptr_array_new();
49 groups_keys_to_indices = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, NULL );
52 static void preferences_groups_uninit()
54 g_ptr_array_free ( groups_names, TRUE );
55 g_hash_table_destroy ( groups_keys_to_indices );
58 void a_preferences_register_group ( const gchar *key, const gchar *name )
60 if ( g_hash_table_lookup ( groups_keys_to_indices, key ) )
61 g_critical("Duplicate preferences group keys");
63 g_ptr_array_add ( groups_names, g_strdup(name) );
64 g_hash_table_insert ( groups_keys_to_indices, g_strdup(key), GINT_TO_POINTER ( (gint) groups_names->len ) ); /* index + 1 */
68 /* returns -1 if not found. */
69 static gint16 preferences_groups_key_to_index( const gchar *key )
71 gint index = GPOINTER_TO_INT ( g_hash_table_lookup ( groups_keys_to_indices, key ) );
73 return VIK_LAYER_GROUP_NONE; /* which should be -1 anyway */
74 return (gint16) (index - 1);
77 /*****************************/
79 static gboolean preferences_load_from_file()
81 gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
82 FILE *f = g_fopen(fn, "r");
88 VikLayerTypedParamData *oldval, *newval;
89 while ( ! feof (f) ) {
90 if (fgets(buf,sizeof(buf),f) == NULL)
92 if ( split_string_from_file_on_equals ( buf, &key, &val ) ) {
93 // if it's not in there, ignore it
94 oldval = g_hash_table_lookup ( values, key );
101 // otherwise change it (you know the type!)
102 // if it's a string list do some funky stuff ... yuck... not yet.
103 if ( oldval->type == VIK_LAYER_PARAM_STRING_LIST )
104 g_critical ( "Param strings not implemented in preferences"); // fake it
106 newval = vik_layer_data_typed_param_copy_from_string ( oldval->type, val );
107 g_hash_table_insert ( values, key, newval );
121 static void preferences_run_setparam ( gpointer notused, guint16 i, VikLayerParamData data, VikLayerParam *params )
123 if ( params[i].type == VIK_LAYER_PARAM_STRING_LIST )
124 g_critical ( "Param strings not implemented in preferences"); //fake it
125 g_hash_table_insert ( values, (gchar *)(params[i].name), vik_layer_typed_param_data_copy_from_data(params[i].type, data) );
128 /* Allow preferences to be manipulated externally */
129 void a_preferences_run_setparam ( VikLayerParamData data, VikLayerParam *params )
131 preferences_run_setparam (NULL, 0, data, params);
134 static VikLayerParamData preferences_run_getparam ( gpointer notused, guint16 i, gboolean notused2 )
136 VikLayerTypedParamData *val = (VikLayerTypedParamData *) g_hash_table_lookup ( values, ((VikLayerParam *)g_ptr_array_index(params,i))->name );
137 g_assert ( val != NULL );
138 if ( val->type == VIK_LAYER_PARAM_STRING_LIST )
139 g_critical ( "Param strings not implemented in preferences"); //fake it
144 * a_preferences_save_to_file:
146 * Returns: TRUE on success
148 gboolean a_preferences_save_to_file()
150 gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL);
152 // TODO: error checking
153 FILE *f = g_fopen(fn, "w");
154 /* Since preferences files saves OSM login credentials,
155 * it'll be better to store it in secret.
161 VikLayerParam *param;
162 VikLayerTypedParamData *val;
164 for ( i = 0; i < params->len; i++ ) {
165 param = (VikLayerParam *) g_ptr_array_index(params,i);
166 val = (VikLayerTypedParamData *) g_hash_table_lookup ( values, param->name );
167 g_assert ( val != NULL );
168 file_write_layer_param ( f, param->name, val->type, val->data );
179 void a_preferences_show_window(GtkWindow *parent) {
180 //VikLayerParamData *a_uibuilder_run_dialog ( GtkWindow *parent, VikLayerParam \*params, // guint16 params_count, gchar **groups, guint8 groups_count, // VikLayerParamData *params_defaults )
181 // TODO: THIS IS A MAJOR HACKAROUND, but ok when we have only a couple preferences.
182 gint params_count = params->len;
183 VikLayerParam *contiguous_params = g_new(VikLayerParam,params_count);
185 for ( i = 0; i < params->len; i++ ) {
186 contiguous_params[i] = *((VikLayerParam*)(g_ptr_array_index(params,i)));
189 preferences_load_from_file();
190 if ( a_uibuilder_properties_factory ( _("Preferences"), parent, contiguous_params, params_count,
191 (gchar **) groups_names->pdata, groups_names->len, // groups, groups_count, // groups? what groups?!
192 (gboolean (*) (gpointer,guint16,VikLayerParamData,gpointer,gboolean)) preferences_run_setparam,
193 NULL /* not used */, contiguous_params,
194 preferences_run_getparam, NULL /* not used */ ) ) {
195 a_preferences_save_to_file();
197 g_free ( contiguous_params );
200 void a_preferences_register(VikLayerParam *pref, VikLayerParamData defaultval, const gchar *group_key )
203 VikLayerParam *newpref = g_new(VikLayerParam,1);
205 VikLayerTypedParamData *newval = vik_layer_typed_param_data_copy_from_data(pref->type, defaultval);
207 newpref->group = preferences_groups_key_to_index ( group_key );
209 g_ptr_array_add ( params, newpref );
210 g_hash_table_insert ( values, (gchar *)pref->name, newval );
213 void a_preferences_init()
215 preferences_groups_init();
218 params = g_ptr_array_new ();
220 /* key not copied (same ptr as in pref), actual param data yes */
221 values = g_hash_table_new_full ( g_str_hash, g_str_equal, NULL, vik_layer_typed_param_data_free);
226 void a_preferences_uninit()
228 preferences_groups_uninit();
230 g_ptr_array_free ( params, TRUE );
231 g_hash_table_destroy ( values );
236 VikLayerParamData *a_preferences_get(const gchar *key)
239 /* since we can't load the file in a_preferences_init (no params registered yet),
240 * do it once before we get the first key. */
241 preferences_load_from_file();
244 return g_hash_table_lookup ( values, key );