Convert NASettings to a private singleton
[nautilus-actions.git] / src / core / na-settings.c
blob6ed27675343c75731932406494a5aabbbe016c3b
1 /*
2 * Nautilus-Actions
3 * A Nautilus extension which offers configurable context menu actions.
5 * Copyright (C) 2005 The GNOME Foundation
6 * Copyright (C) 2006, 2007, 2008 Frederic Ruaudel and others (see AUTHORS)
7 * Copyright (C) 2009, 2010, 2011 Pierre Wieser and others (see AUTHORS)
9 * This Program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This Program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this Library; see the file COPYING. If not,
21 * write to the Free Software Foundation, Inc., 59 Temple Place,
22 * Suite 330, Boston, MA 02111-1307, USA.
24 * Authors:
25 * Frederic Ruaudel <grumz@grumz.net>
26 * Rodrigo Moya <rodrigo@gnome-db.org>
27 * Pierre Wieser <pwieser@trychlos.org>
28 * ... and many others (see AUTHORS)
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include <gio/gio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <strings.h>
40 #include <api/na-boxed.h>
41 #include <api/na-data-types.h>
42 #include <api/na-core-utils.h>
43 #include <api/na-timeout.h>
45 #include "na-settings.h"
46 #include "na-marshal.h"
48 #define NA_SETTINGS_TYPE ( settings_get_type())
49 #define NA_SETTINGS( object ) ( G_TYPE_CHECK_INSTANCE_CAST( object, NA_SETTINGS_TYPE, NASettings ))
50 #define NA_SETTINGS_CLASS( klass ) ( G_TYPE_CHECK_CLASS_CAST( klass, NA_SETTINGS_TYPE, NASettingsClass ))
51 #define NA_IS_SETTINGS( object ) ( G_TYPE_CHECK_INSTANCE_TYPE( object, NA_SETTINGS_TYPE ))
52 #define NA_IS_SETTINGS_CLASS( klass ) ( G_TYPE_CHECK_CLASS_TYPE(( klass ), NA_SETTINGS_TYPE ))
53 #define NA_SETTINGS_GET_CLASS( object ) ( G_TYPE_INSTANCE_GET_CLASS(( object ), NA_SETTINGS_TYPE, NASettingsClass ))
55 typedef struct _NASettingsPrivate NASettingsPrivate;
57 typedef struct {
58 /*< private >*/
59 GObject parent;
60 NASettingsPrivate *private;
62 NASettings;
64 typedef struct _NASettingsClassPrivate NASettingsClassPrivate;
66 typedef struct {
67 /*< private >*/
68 GObjectClass parent;
69 NASettingsClassPrivate *private;
71 NASettingsClass;
73 /* private class data
75 struct _NASettingsClassPrivate {
76 void *empty; /* so that gcc -pedantic is happy */
79 /* The characteristics of a configuration file.
80 * We manage two configuration files:
81 * - the global configuration file handles mandatory preferences;
82 * - the user configuration file handles.. well, user preferences.
84 typedef struct {
85 gchar *fname;
86 GKeyFile *key_file;
87 GFileMonitor *monitor;
88 gulong handler;
90 KeyFile;
92 /* Each consumer may register a callback function which will be triggered
93 * when a key is modified.
95 * The monitored key usually is the real key read in the file;
96 * as a special case, composite keys are defined:
97 * - NA_IPREFS_IO_PROVIDERS_READ_STATUS monitors the 'readable' key for all i/o providers
99 * Note that we actually monitor the _user_view_ of the configuration:
100 * e.g. if a key has a mandatory value in global conf, then the same
101 * key in user conf will just be ignored.
103 typedef struct {
104 gchar *monitored_key;
105 GCallback callback;
106 gpointer user_data;
108 Consumer;
110 /* private instance data
112 struct _NASettingsPrivate {
113 gboolean dispose_has_run;
114 KeyFile *mandatory;
115 KeyFile *user;
116 GList *content;
117 GList *consumers;
118 NATimeout timeout;
121 #define GROUP_NACT "nact"
122 #define GROUP_RUNTIME "runtime"
124 typedef struct {
125 const gchar *key;
126 const gchar *group;
127 guint type;
128 const gchar *default_value;
130 KeyDef;
132 static const KeyDef st_def_keys[] = {
133 { NA_IPREFS_ADMIN_PREFERENCES_LOCKED, GROUP_NACT, NA_DATA_TYPE_BOOLEAN, "false" },
134 { NA_IPREFS_ADMIN_IO_PROVIDERS_LOCKED, GROUP_RUNTIME, NA_DATA_TYPE_BOOLEAN, "false" },
135 { NA_IPREFS_ASSISTANT_ESC_CONFIRM, GROUP_NACT, NA_DATA_TYPE_BOOLEAN, "true" },
136 { NA_IPREFS_ASSISTANT_ESC_QUIT, GROUP_NACT, NA_DATA_TYPE_BOOLEAN, "true" },
137 { NA_IPREFS_CAPABILITY_ADD_CAPABILITY_WSP, GROUP_NACT, NA_DATA_TYPE_UINT_LIST, "" },
138 { NA_IPREFS_COMMAND_CHOOSER_WSP, GROUP_NACT, NA_DATA_TYPE_UINT_LIST, "" },
139 { NA_IPREFS_COMMAND_CHOOSER_URI, GROUP_NACT, NA_DATA_TYPE_STRING, "file:///bin" },
140 { NA_IPREFS_COMMAND_LEGEND_WSP, GROUP_NACT, NA_DATA_TYPE_UINT_LIST, "" },
141 { NA_IPREFS_CONFIRM_LOGOUT_WSP, GROUP_NACT, NA_DATA_TYPE_UINT_LIST, "" },
142 { NA_IPREFS_DESKTOP_ENVIRONMENT, GROUP_RUNTIME, NA_DATA_TYPE_STRING, "" },
143 { NA_IPREFS_WORKING_DIR_WSP, GROUP_NACT, NA_DATA_TYPE_UINT_LIST, "" },
144 { NA_IPREFS_WORKING_DIR_URI, GROUP_NACT, NA_DATA_TYPE_STRING, "file:///" },
145 { NA_IPREFS_SHOW_IF_RUNNING_WSP, GROUP_NACT, NA_DATA_TYPE_UINT_LIST, "" },
146 { NA_IPREFS_SHOW_IF_RUNNING_URI, GROUP_NACT, NA_DATA_TYPE_STRING, "file:///bin" },
147 { NA_IPREFS_TRY_EXEC_WSP, GROUP_NACT, NA_DATA_TYPE_UINT_LIST, "" },
148 { NA_IPREFS_TRY_EXEC_URI, GROUP_NACT, NA_DATA_TYPE_STRING, "file:///bin" },
149 { NA_IPREFS_EXPORT_ASK_USER_WSP, GROUP_NACT, NA_DATA_TYPE_UINT_LIST, "" },
150 { NA_IPREFS_EXPORT_ASK_USER_LAST_FORMAT, GROUP_NACT, NA_DATA_TYPE_STRING, NA_IPREFS_DEFAULT_EXPORT_FORMAT },
151 { NA_IPREFS_EXPORT_ASK_USER_KEEP_LAST_CHOICE, GROUP_NACT, NA_DATA_TYPE_BOOLEAN, "false" },
152 { NA_IPREFS_EXPORT_ASSISTANT_WSP, GROUP_NACT, NA_DATA_TYPE_UINT_LIST, "" },
153 { NA_IPREFS_EXPORT_ASSISTANT_URI, GROUP_NACT, NA_DATA_TYPE_STRING, "file:///tmp" },
154 { NA_IPREFS_EXPORT_PREFERRED_FORMAT, GROUP_NACT, NA_DATA_TYPE_STRING, NA_IPREFS_DEFAULT_EXPORT_FORMAT },
155 { NA_IPREFS_FOLDER_CHOOSER_WSP, GROUP_NACT, NA_DATA_TYPE_UINT_LIST, "" },
156 { NA_IPREFS_FOLDER_CHOOSER_URI, GROUP_NACT, NA_DATA_TYPE_STRING, "file:///" },
157 { NA_IPREFS_IMPORT_ASK_USER_WSP, GROUP_NACT, NA_DATA_TYPE_UINT_LIST, "" },
158 { NA_IPREFS_IMPORT_ASK_USER_LAST_MODE, GROUP_NACT, NA_DATA_TYPE_STRING, NA_IPREFS_DEFAULT_IMPORT_MODE },
159 { NA_IPREFS_IMPORT_ASSISTANT_WSP, GROUP_NACT, NA_DATA_TYPE_UINT_LIST, "" },
160 { NA_IPREFS_IMPORT_ASSISTANT_URI, GROUP_NACT, NA_DATA_TYPE_STRING, "file:///tmp" },
161 { NA_IPREFS_IMPORT_ASK_USER_KEEP_LAST_CHOICE, GROUP_NACT, NA_DATA_TYPE_BOOLEAN, "false" },
162 { NA_IPREFS_IMPORT_PREFERRED_MODE, GROUP_NACT, NA_DATA_TYPE_STRING, NA_IPREFS_DEFAULT_IMPORT_MODE },
163 { NA_IPREFS_IO_PROVIDERS_WRITE_ORDER, GROUP_NACT, NA_DATA_TYPE_STRING_LIST, "" },
164 { NA_IPREFS_ICON_CHOOSER_URI, GROUP_NACT, NA_DATA_TYPE_STRING, "file:///" },
165 { NA_IPREFS_ICON_CHOOSER_PANED, GROUP_NACT, NA_DATA_TYPE_UINT, "200" },
166 { NA_IPREFS_ICON_CHOOSER_WSP, GROUP_NACT, NA_DATA_TYPE_UINT_LIST, "" },
167 { NA_IPREFS_ITEMS_ADD_ABOUT_ITEM, GROUP_RUNTIME, NA_DATA_TYPE_BOOLEAN, "true" },
168 { NA_IPREFS_ITEMS_CREATE_ROOT_MENU, GROUP_RUNTIME, NA_DATA_TYPE_BOOLEAN, "true" },
169 { NA_IPREFS_ITEMS_LEVEL_ZERO_ORDER, GROUP_RUNTIME, NA_DATA_TYPE_STRING_LIST, "" },
170 { NA_IPREFS_ITEMS_LIST_ORDER_MODE, GROUP_RUNTIME, NA_DATA_TYPE_STRING, NA_IPREFS_DEFAULT_LIST_ORDER_MODE },
171 { NA_IPREFS_MAIN_PANED, GROUP_NACT, NA_DATA_TYPE_UINT, "200" },
172 { NA_IPREFS_MAIN_SAVE_AUTO, GROUP_NACT, NA_DATA_TYPE_BOOLEAN, "false" },
173 { NA_IPREFS_MAIN_SAVE_PERIOD, GROUP_NACT, NA_DATA_TYPE_UINT, "5" },
174 { NA_IPREFS_MAIN_TOOLBAR_EDIT_DISPLAY, GROUP_NACT, NA_DATA_TYPE_BOOLEAN, "true" },
175 { NA_IPREFS_MAIN_TOOLBAR_FILE_DISPLAY, GROUP_NACT, NA_DATA_TYPE_BOOLEAN, "true" },
176 { NA_IPREFS_MAIN_TOOLBAR_HELP_DISPLAY, GROUP_NACT, NA_DATA_TYPE_BOOLEAN, "true" },
177 { NA_IPREFS_MAIN_TOOLBAR_TOOLS_DISPLAY, GROUP_NACT, NA_DATA_TYPE_BOOLEAN, "false" },
178 { NA_IPREFS_MAIN_WINDOW_WSP, GROUP_NACT, NA_DATA_TYPE_UINT_LIST, "" },
179 { NA_IPREFS_PREFERENCES_WSP, GROUP_NACT, NA_DATA_TYPE_UINT_LIST, "" },
180 { NA_IPREFS_PLUGIN_LOG, GROUP_RUNTIME, NA_DATA_TYPE_BOOLEAN, "false" },
181 { NA_IPREFS_RELABEL_DUPLICATE_ACTION, GROUP_NACT, NA_DATA_TYPE_BOOLEAN, "false" },
182 { NA_IPREFS_RELABEL_DUPLICATE_MENU, GROUP_NACT, NA_DATA_TYPE_BOOLEAN, "false" },
183 { NA_IPREFS_RELABEL_DUPLICATE_PROFILE, GROUP_NACT, NA_DATA_TYPE_BOOLEAN, "false" },
184 { NA_IPREFS_SCHEME_ADD_SCHEME_WSP, GROUP_NACT, NA_DATA_TYPE_UINT_LIST, "" },
185 { NA_IPREFS_SCHEME_DEFAULT_LIST, GROUP_NACT, NA_DATA_TYPE_STRING_LIST, "" },
186 { NA_IPREFS_TERMINAL_PREFIX, GROUP_RUNTIME, NA_DATA_TYPE_STRING, "" },
187 { NA_IPREFS_IO_PROVIDER_READABLE, NA_IPREFS_IO_PROVIDER_GROUP, NA_DATA_TYPE_BOOLEAN, "true" },
188 { NA_IPREFS_IO_PROVIDER_WRITABLE, NA_IPREFS_IO_PROVIDER_GROUP, NA_DATA_TYPE_BOOLEAN, "true" },
189 { 0 }
192 /* The configuration content is handled as a GList of KeyValue structs.
193 * This list is loaded at initialization time, and then compared each
194 * time our file monitors signal us that a change has occured.
196 typedef struct {
197 const KeyDef *def;
198 const gchar *group;
199 gboolean mandatory;
200 NABoxed *boxed;
202 KeyValue;
204 /* signals
206 enum {
207 KEY_CHANGED,
208 LAST_SIGNAL
211 static GObjectClass *st_parent_class = NULL;
212 static gint st_burst_timeout = 100; /* burst timeout in msec */
213 static gint st_signals[ LAST_SIGNAL ] = { 0 };
214 static NASettings *st_settings = NULL;
216 static GType settings_get_type( void );
217 static GType register_type( void );
218 static void class_init( NASettingsClass *klass );
219 static void instance_init( GTypeInstance *instance, gpointer klass );
220 static void instance_dispose( GObject *object );
221 static void instance_finalize( GObject *object );
223 static void settings_new( void );
225 static GList *content_diff( GList *old, GList *new );
226 static GList *content_load( void );
227 static GList *content_load_keys( GList *content, KeyFile *key_file, gboolean mandatory );
228 static KeyDef *get_key_def( const gchar *key );
229 static KeyFile *key_file_new( const gchar *dir );
230 static void on_keyfile_changed( GFileMonitor *monitor, GFile *file, GFile *other_file, GFileMonitorEvent event_type );
231 static void on_keyfile_changed_timeout( void );
232 static void on_key_changed_final_handler( NASettings *settings, gchar *group, gchar *key, NABoxed *new_value, gboolean mandatory );
233 static KeyValue *peek_key_value_from_content( GList *content, const gchar *group, const gchar *key );
234 static KeyValue *read_key_value( const gchar *group, const gchar *key, gboolean *found, gboolean *mandatory );
235 static KeyValue *read_key_value_from_key_file( GKeyFile *key_file, const gchar *group, const gchar *key, const KeyDef *key_def );
236 static void release_consumer( Consumer *consumer );
237 static void release_key_file( KeyFile *key_file );
238 static void release_key_value( KeyValue *value );
239 static gboolean set_key_value( const gchar *group, const gchar *key, const gchar *string );
240 static gboolean write_user_key_file( void );
242 static GType
243 settings_get_type( void )
245 static GType object_type = 0;
247 if( !object_type ){
248 object_type = register_type();
251 return( object_type );
254 static GType
255 register_type( void )
257 static const gchar *thisfn = "na_settings_register_type";
258 GType type;
260 static GTypeInfo info = {
261 sizeof( NASettingsClass ),
262 ( GBaseInitFunc ) NULL,
263 ( GBaseFinalizeFunc ) NULL,
264 ( GClassInitFunc ) class_init,
265 NULL,
266 NULL,
267 sizeof( NASettings ),
269 ( GInstanceInitFunc ) instance_init
272 g_debug( "%s", thisfn );
274 type = g_type_register_static( G_TYPE_OBJECT, "NASettings", &info, 0 );
276 return( type );
279 static void
280 class_init( NASettingsClass *klass )
282 static const gchar *thisfn = "na_settings_class_init";
283 GObjectClass *object_class;
285 g_debug( "%s: klass=%p", thisfn, ( void * ) klass );
287 st_parent_class = g_type_class_peek_parent( klass );
289 object_class = G_OBJECT_CLASS( klass );
290 object_class->dispose = instance_dispose;
291 object_class->finalize = instance_finalize;
293 klass->private = g_new0( NASettingsClassPrivate, 1 );
296 * NASettings::settings-key-changed:
298 * This signal is sent by NASettings when the value of a key is modified.
300 * Arguments are the group, the key, the new value as a NABoxed,
301 * and whether it is mandatory.
303 * Handler is of type:
304 * void ( *handler )( NASettings *settings,
305 * const gchar *group,
306 * const gchar *key,
307 * NABoxed *value,
308 * gboolean mandatory,
309 * gpointer user_data );
311 * The default class handler frees these datas.
313 st_signals[ KEY_CHANGED ] = g_signal_new_class_handler(
314 SETTINGS_SIGNAL_KEY_CHANGED,
315 NA_SETTINGS_TYPE,
316 G_SIGNAL_RUN_CLEANUP | G_SIGNAL_ACTION,
317 G_CALLBACK( on_key_changed_final_handler ),
318 NULL, /* accumulator */
319 NULL, /* accumulator data */
320 na_cclosure_marshal_VOID__STRING_STRING_POINTER_BOOLEAN,
321 G_TYPE_NONE,
323 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_BOOLEAN );
326 static void
327 instance_init( GTypeInstance *instance, gpointer klass )
329 static const gchar *thisfn = "na_settings_instance_init";
330 NASettings *self;
332 g_return_if_fail( NA_IS_SETTINGS( instance ));
334 g_debug( "%s: instance=%p (%s), klass=%p",
335 thisfn, ( void * ) instance, G_OBJECT_TYPE_NAME( instance ), ( void * ) klass );
337 self = NA_SETTINGS( instance );
339 self->private = g_new0( NASettingsPrivate, 1 );
341 self->private->dispose_has_run = FALSE;
342 self->private->mandatory = NULL;
343 self->private->user = NULL;
344 self->private->content = NULL;
345 self->private->consumers = NULL;
347 self->private->timeout.timeout = st_burst_timeout;
348 self->private->timeout.handler = ( NATimeoutFunc ) on_keyfile_changed_timeout;
349 self->private->timeout.user_data = NULL;
350 self->private->timeout.source_id = 0;
353 static void
354 instance_dispose( GObject *object )
356 static const gchar *thisfn = "na_settings_instance_dispose";
357 NASettings *self;
359 g_return_if_fail( NA_IS_SETTINGS( object ));
361 self = NA_SETTINGS( object );
363 if( !self->private->dispose_has_run ){
365 g_debug( "%s: object=%p (%s)", thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ));
367 self->private->dispose_has_run = TRUE;
369 release_key_file( self->private->mandatory );
370 release_key_file( self->private->user );
372 if( G_OBJECT_CLASS( st_parent_class )->dispose ){
373 G_OBJECT_CLASS( st_parent_class )->dispose( object );
378 static void
379 instance_finalize( GObject *object )
381 static const gchar *thisfn = "na_settings_instance_finalize";
382 NASettings *self;
384 g_return_if_fail( NA_IS_SETTINGS( object ));
386 g_debug( "%s: object=%p (%s)", thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ));
388 self = NA_SETTINGS( object );
390 g_list_foreach( self->private->content, ( GFunc ) release_key_value, NULL );
391 g_list_free( self->private->content );
393 g_list_foreach( self->private->consumers, ( GFunc ) release_consumer, NULL );
394 g_list_free( self->private->consumers );
396 g_free( self->private );
398 /* chain call to parent class */
399 if( G_OBJECT_CLASS( st_parent_class )->finalize ){
400 G_OBJECT_CLASS( st_parent_class )->finalize( object );
405 * na_settings_new:
407 * Returns: a new #NASettings object which should be g_object_unref()
408 * by the caller.
410 static void
411 settings_new( void )
413 static const gchar *thisfn = "na_settings_new";
414 gchar *dir;
416 if( !st_settings ){
417 st_settings = g_object_new( NA_SETTINGS_TYPE, NULL );
419 dir = g_build_filename( SYSCONFDIR, "xdg", PACKAGE, NULL );
420 g_debug( "%s: reading mandatory configuration", thisfn );
421 st_settings->private->mandatory = key_file_new( dir );
422 g_free( dir );
424 dir = g_build_filename( g_get_home_dir(), ".config", PACKAGE, NULL );
425 g_debug( "%s: reading user configuration", thisfn );
426 st_settings->private->user = key_file_new( dir );
427 g_free( dir );
429 st_settings->private->content = content_load();
434 * na_settings_free:
436 void
437 na_settings_free( void )
439 if( st_settings ){
440 g_object_unref( st_settings );
441 st_settings = NULL;
446 * na_settings_register_key_callback:
447 * @key: the key to be monitored.
448 * @callback: the function to be called when the value of the key changes.
449 * @user_data: data to be passed to the @callback function.
451 * Registers a new consumer of the monitoring of the @key.
453 * Since: 3.1.0
455 void
456 na_settings_register_key_callback( const gchar *key, GCallback callback, gpointer user_data )
458 static const gchar *thisfn = "na_settings_register_key_callback";
460 g_debug( "%s: key=%s, callback=%p, user_data=%p",
461 thisfn, key, ( void * ) callback, ( void * ) user_data );
463 Consumer *consumer = g_new0( Consumer, 1 );
464 consumer->monitored_key = g_strdup( key );
465 consumer->callback = callback;
466 consumer->user_data = user_data;
468 settings_new();
469 st_settings->private->consumers = g_list_prepend( st_settings->private->consumers, consumer );
473 * na_settings_get_boolean:
474 * @key: the key whose value is to be returned.
475 * @found: if not %NULL, a pointer to a gboolean in which we will store
476 * whether the searched @key has been found (%TRUE), or if the returned
477 * value comes from default (%FALSE).
478 * @mandatory: if not %NULL, a pointer to a gboolean in which we will store
479 * whether the returned value has been read from mandatory preferences
480 * (%TRUE), or from the user preferences (%FALSE). When the @key has not
481 * been found, @mandatory is set to %FALSE.
483 * This function should only be called for unambiguous keys; the resultat
484 * is otherwise undefined (and rather unpredictable).
486 * Returns: the value of the key, of its default value if not found.
488 * Since: 3.1.0
490 gboolean
491 na_settings_get_boolean( const gchar *key, gboolean *found, gboolean *mandatory )
493 return( na_settings_get_boolean_ex( NULL, key, found, mandatory ));
497 * na_settings_get_boolean_ex:
498 * @group: the group where the @key is to be searched for. May be %NULL.
499 * @key: the key whose value is to be returned.
500 * @found: if not %NULL, a pointer to a gboolean in which we will store
501 * whether the searched @key has been found (%TRUE), or if the returned
502 * value comes from default (%FALSE).
503 * @mandatory: if not %NULL, a pointer to a gboolean in which we will store
504 * whether the returned value has been read from mandatory preferences
505 * (%TRUE), or from the user preferences (%FALSE). When the @key has not
506 * been found, @mandatory is set to %FALSE.
508 * Returns: the value of the key, of its default value if not found.
510 * Since: 3.1.0
512 gboolean
513 na_settings_get_boolean_ex( const gchar *group, const gchar *key, gboolean *found, gboolean *mandatory )
515 gboolean value;
516 KeyValue *key_value;
517 KeyDef *key_def;
519 value = FALSE;
520 key_value = read_key_value( group, key, found, mandatory );
522 if( key_value ){
523 value = na_boxed_get_boolean( key_value->boxed );
524 release_key_value( key_value );
526 } else {
527 key_def = get_key_def( key );
528 if( key_def ){
529 value = ( key_def->default_value ? ( strcasecmp( key_def->default_value, "true" ) == 0 || atoi( key_def->default_value ) != 0 ) : FALSE );
533 return( value );
537 * na_settings_get_string:
538 * @key: the key whose value is to be returned.
539 * @found: if not %NULL, a pointer to a gboolean in which we will store
540 * whether the searched @key has been found (%TRUE), or if the returned
541 * value comes from default (%FALSE).
542 * @mandatory: if not %NULL, a pointer to a gboolean in which we will store
543 * whether the returned value has been read from mandatory preferences
544 * (%TRUE), or from the user preferences (%FALSE). When the @key has not
545 * been found, @mandatory is set to %FALSE.
547 * This function should only be called for unambiguous keys; the resultat
548 * is otherwise undefined (and rather unpredictable).
550 * Returns: the value of the key as a newly allocated string, which should
551 * be g_free() by the caller.
553 * Since: 3.1.0
555 gchar *
556 na_settings_get_string( const gchar *key, gboolean *found, gboolean *mandatory )
558 gchar *value;
559 KeyValue *key_value;
560 KeyDef *key_def;
562 value = NULL;
563 key_value = read_key_value( NULL, key, found, mandatory );
565 if( key_value ){
566 value = na_boxed_get_string( key_value->boxed );
567 release_key_value( key_value );
569 } else {
570 key_def = get_key_def( key );
571 if( key_def && key_def->default_value ){
572 value = g_strdup( key_def->default_value );
576 return( value );
580 * na_settings_get_string_list:
581 * @key: the key whose value is to be returned.
582 * @found: if not %NULL, a pointer to a gboolean in which we will store
583 * whether the searched @key has been found (%TRUE), or if the returned
584 * value comes from default (%FALSE).
585 * @mandatory: if not %NULL, a pointer to a gboolean in which we will store
586 * whether the returned value has been read from mandatory preferences
587 * (%TRUE), or from the user preferences (%FALSE). When the @key has not
588 * been found, @mandatory is set to %FALSE.
590 * This function should only be called for unambiguous keys; the resultat
591 * is otherwise undefined (and rather unpredictable).
593 * Returns: the value of the key as a newly allocated list of strings.
594 * The returned list should be na_core_utils_slist_free() by the caller.
596 * Since: 3.1.0
598 GSList *
599 na_settings_get_string_list( const gchar *key, gboolean *found, gboolean *mandatory )
601 GSList *value;
602 KeyValue *key_value;
603 KeyDef *key_def;
605 value = NULL;
606 key_value = read_key_value( NULL, key, found, mandatory );
608 if( key_value ){
609 value = na_boxed_get_string_list( key_value->boxed );
610 release_key_value( key_value );
612 } else {
613 key_def = get_key_def( key );
614 if( key_def && key_def->default_value && strlen( key_def->default_value )){
615 value = g_slist_append( NULL, g_strdup( key_def->default_value ));
619 return( value );
623 * na_settings_get_uint:
624 * @key: the key whose value is to be returned.
625 * @found: if not %NULL, a pointer to a gboolean in which we will store
626 * whether the searched @key has been found (%TRUE), or if the returned
627 * value comes from default (%FALSE).
628 * @mandatory: if not %NULL, a pointer to a gboolean in which we will store
629 * whether the returned value has been read from mandatory preferences
630 * (%TRUE), or from the user preferences (%FALSE). When the @key has not
631 * been found, @mandatory is set to %FALSE.
633 * This function should only be called for unambiguous keys; the resultat
634 * is otherwise undefined (and rather unpredictable).
636 * Returns: the value of the key.
638 * Since: 3.1.0
640 guint
641 na_settings_get_uint( const gchar *key, gboolean *found, gboolean *mandatory )
643 guint value;
644 KeyDef *key_def;
645 KeyValue *key_value;
647 value = 0;
648 key_value = read_key_value( NULL, key, found, mandatory );
650 if( key_value ){
651 value = na_boxed_get_uint( key_value->boxed );
652 release_key_value( key_value );
654 } else {
655 key_def = get_key_def( key );
656 if( key_def && key_def->default_value ){
657 value = atoi( key_def->default_value );
661 return( value );
665 * na_settings_get_uint_list:
666 * @key: the key whose value is to be returned.
667 * @found: if not %NULL, a pointer to a gboolean in which we will store
668 * whether the searched @key has been found (%TRUE), or if the returned
669 * value comes from default (%FALSE).
670 * @mandatory: if not %NULL, a pointer to a gboolean in which we will store
671 * whether the returned value has been read from mandatory preferences
672 * (%TRUE), or from the user preferences (%FALSE). When the @key has not
673 * been found, @mandatory is set to %FALSE.
675 * This function should only be called for unambiguous keys; the resultat
676 * is otherwise undefined (and rather unpredictable).
678 * Returns: the value of the key as a newly allocated list of uints.
679 * The returned list should be g_list_free() by the caller.
681 * Since: 3.1.0
683 GList *
684 na_settings_get_uint_list( const gchar *key, gboolean *found, gboolean *mandatory )
686 GList *value;
687 KeyDef *key_def;
688 KeyValue *key_value;
690 value = NULL;
691 key_value = read_key_value( NULL, key, found, mandatory );
693 if( key_value ){
694 value = na_boxed_get_uint_list( key_value->boxed );
695 release_key_value( key_value );
697 } else {
698 key_def = get_key_def( key );
699 if( key_def && key_def->default_value ){
700 value = g_list_append( NULL, GUINT_TO_POINTER( atoi( key_def->default_value )));
704 return( value );
708 * na_settings_set_boolean:
709 * @key: the key whose value is to be returned.
710 * @value: the boolean to be written.
712 * This function writes @value as a user preference.
714 * This function should only be called for unambiguous keys; the resultat
715 * is otherwise undefined (and rather unpredictable).
717 * Returns: %TRUE is the writing has been successful, %FALSE else.
719 * Since: 3.1.0
721 gboolean
722 na_settings_set_boolean( const gchar *key, gboolean value )
724 gchar *string;
725 gboolean ok;
727 string = g_strdup_printf( "%s", value ? "true" : "false" );
728 ok = set_key_value( NULL, key, string );
729 g_free( string );
731 return( ok );
735 * na_settings_set_boolean_ex:
736 * @group: the group in the keyed file;
737 * @key: the key whose value is to be returned.
738 * @value: the boolean to be written.
740 * This function writes @value as a user preference.
742 * Returns: %TRUE is the writing has been successful, %FALSE else.
744 * Since: 3.1.0
746 gboolean
747 na_settings_set_boolean_ex( const gchar *group, const gchar *key, gboolean value )
749 gchar *string;
750 gboolean ok;
752 string = g_strdup_printf( "%s", value ? "true" : "false" );
753 ok = set_key_value( group, key, string );
754 g_free( string );
756 return( ok );
760 * na_settings_set_string:
761 * @key: the key whose value is to be returned.
762 * @value: the string to be written.
764 * This function writes @value as a user preference.
766 * This function should only be called for unambiguous keys; the resultat
767 * is otherwise undefined (and rather unpredictable).
769 * Returns: %TRUE is the writing has been successful, %FALSE else.
771 * Since: 3.1.0
773 gboolean
774 na_settings_set_string( const gchar *key, const gchar *value )
776 return( set_key_value( NULL, key, value ));
780 * na_settings_set_string_list:
781 * @key: the key whose value is to be returned.
782 * @value: the list of strings to be written.
784 * This function writes @value as a user preference.
786 * This function should only be called for unambiguous keys; the resultat
787 * is otherwise undefined (and rather unpredictable).
789 * Returns: %TRUE is the writing has been successful, %FALSE else.
791 * Since: 3.1.0
793 gboolean
794 na_settings_set_string_list( const gchar *key, const GSList *value )
796 GString *string;
797 const GSList *it;
798 gboolean ok;
800 string = g_string_new( "" );
801 for( it = value ; it ; it = it->next ){
802 g_string_append_printf( string, "%s;", ( const gchar * ) it->data );
804 ok = set_key_value( NULL, key, string->str );
805 g_string_free( string, TRUE );
807 return( ok );
811 * na_settings_set_uint:
812 * @key: the key whose value is to be returned.
813 * @value: the unsigned integer to be written.
815 * This function writes @value as a user preference.
817 * This function should only be called for unambiguous keys; the resultat
818 * is otherwise undefined (and rather unpredictable).
820 * Returns: %TRUE is the writing has been successful, %FALSE else.
822 * Since: 3.1.0
824 gboolean
825 na_settings_set_uint( const gchar *key, guint value )
827 gchar *string;
828 gboolean ok;
830 string = g_strdup_printf( "%u", value );
831 ok = set_key_value( NULL, key, string );
832 g_free( string );
834 return( ok );
838 * na_settings_set_uint_list:
839 * @key: the key whose value is to be returned.
840 * @value: the list of unsigned integers to be written.
842 * This function writes @value as a user preference.
844 * This function should only be called for unambiguous keys; the resultat
845 * is otherwise undefined (and rather unpredictable).
847 * Returns: %TRUE is the writing has been successful, %FALSE else.
849 * Since: 3.1.0
851 gboolean
852 na_settings_set_uint_list( const gchar *key, const GList *value )
854 GString *string;
855 const GList *it;
856 gboolean ok;
858 string = g_string_new( "" );
859 for( it = value ; it ; it = it->next ){
860 g_string_append_printf( string, "%u;", GPOINTER_TO_UINT( it->data ));
862 ok = set_key_value( NULL, key, string->str );
863 g_string_free( string, TRUE );
865 return( ok );
869 * na_settings_get_groups:
871 * Returns: the list of groups in the configuration; this list should be
872 * na_core_utils_slist_free() by the caller.
874 * This function participates to a rather bad hack to obtain the list of
875 * known i/o providers from preferences. We do not care of returning unique
876 * or sorted group names.
878 * Since: 3.1.0
880 GSList *
881 na_settings_get_groups( void )
883 GSList *groups;
884 gchar **array;
886 groups = NULL;
887 settings_new();
889 array = g_key_file_get_groups( st_settings->private->mandatory->key_file, NULL );
890 if( array ){
891 groups = na_core_utils_slist_from_array(( const gchar ** ) array );
892 g_strfreev( array );
895 array = g_key_file_get_groups( st_settings->private->user->key_file, NULL );
896 if( array ){
897 groups = g_slist_concat( groups, na_core_utils_slist_from_array(( const gchar ** ) array ));
898 g_strfreev( array );
901 return( groups );
905 * returns a list of modified KeyValue
906 * - order in the lists is not signifiant
907 * - the mandatory flag is not signifiant
908 * - a key is modified:
909 * > if it appears in new
910 * > if it disappears: the value is so reset to its default
911 * > if the value has been modified
913 * we return here a new list, with newly allocated KeyValue structs
914 * which hold the new value of each modified key
916 static GList *
917 content_diff( GList *old, GList *new )
919 GList *diffs, *io, *in;
920 KeyValue *kold, *knew, *kdiff;
921 gboolean found;
923 diffs = NULL;
925 for( io = old ; io ; io = io->next ){
926 kold = ( KeyValue * ) io->data;
927 found = FALSE;
928 for( in = new ; in && !found ; in = in->next ){
929 knew = ( KeyValue * ) in->data;
930 if( !strcmp( kold->group, knew->group ) && ( gpointer ) kold->def == ( gpointer ) knew->def ){
931 found = TRUE;
932 if( !na_boxed_are_equal( kold->boxed, knew->boxed )){
933 /* a key has been modified */
934 kdiff = g_new0( KeyValue, 1 );
935 kdiff->group = g_strdup( knew->group );
936 kdiff->def = knew->def;
937 kdiff->mandatory = knew->mandatory;
938 kdiff->boxed = na_boxed_copy( knew->boxed );
939 diffs = g_list_prepend( diffs, kdiff );
943 if( !found ){
944 /* a key has disappeared */
945 kdiff = g_new0( KeyValue, 1 );
946 kdiff->group = g_strdup( kold->group );
947 kdiff->def = kold->def;
948 kdiff->mandatory = FALSE;
949 kdiff->boxed = na_boxed_new_from_string( kold->def->type, kold->def->default_value );
950 diffs = g_list_prepend( diffs, kdiff );
954 for( in = new ; in ; in = in->next ){
955 knew = ( KeyValue * ) in->data;
956 found = FALSE;
957 for( io = old ; io && !found ; io = io->next ){
958 kold = ( KeyValue * ) io->data;
959 if( !strcmp( kold->group, knew->group ) && ( gpointer ) kold->def == ( gpointer ) knew->def ){
960 found = TRUE;
963 if( !found ){
964 /* a key is new */
965 kdiff = g_new0( KeyValue, 1 );
966 kdiff->group = g_strdup( knew->group );
967 kdiff->def = knew->def;
968 kdiff->mandatory = knew->mandatory;
969 kdiff->boxed = na_boxed_copy( knew->boxed );
970 diffs = g_list_prepend( diffs, kdiff );
974 return( diffs );
977 /* load the content of the two configuration files (actually of _the_ configuration)
978 * taking care of not overriding mandatory preferences with user ones
980 static GList *
981 content_load( void )
983 GList *content;
985 settings_new();
986 content = content_load_keys( NULL, st_settings->private->mandatory, TRUE );
987 content = content_load_keys( content, st_settings->private->user, FALSE );
989 return( content );
992 static GList *
993 content_load_keys( GList *content, KeyFile *key_file, gboolean mandatory )
995 static const gchar *thisfn = "na_settings_content_load_keys";
996 GError *error;
997 gchar **groups, **ig;
998 gchar **keys, **ik;
999 KeyValue *key_value;
1000 KeyDef *key_def;
1002 error = NULL;
1003 if( !g_key_file_load_from_file( key_file->key_file, key_file->fname, G_KEY_FILE_KEEP_COMMENTS, &error )){
1004 if( error->code != G_FILE_ERROR_NOENT ){
1005 g_warning( "%s: %s (%d) %s", thisfn, key_file->fname, error->code, error->message );
1007 g_error_free( error );
1008 error = NULL;
1010 } else {
1011 groups = g_key_file_get_groups( key_file->key_file, NULL );
1012 ig = groups;
1013 while( *ig ){
1014 keys = g_key_file_get_keys( key_file->key_file, *ig, NULL, NULL );
1015 ik = keys;
1016 while( *ik ){
1017 key_def = get_key_def( *ik );
1018 if( key_def ){
1019 key_value = peek_key_value_from_content( content, *ig, *ik );
1020 if( !key_value ){
1021 key_value = read_key_value_from_key_file( key_file->key_file, *ig, *ik, key_def );
1022 if( key_value ){
1023 key_value->mandatory = mandatory;
1024 content = g_list_prepend( content, key_value );
1028 ik++;
1030 g_strfreev( keys );
1031 ig++;
1033 g_strfreev( groups );
1036 return( content );
1039 static KeyDef *
1040 get_key_def( const gchar *key )
1042 static const gchar *thisfn = "na_settings_get_key_def";
1043 KeyDef *found = NULL;
1044 KeyDef *idef;
1046 idef = ( KeyDef * ) st_def_keys;
1047 while( idef->key && !found ){
1048 if( !strcmp( idef->key, key )){
1049 found = idef;
1051 idef++;
1053 if( !found ){
1054 g_warning( "%s: no KeyDef found for key=%s", thisfn, key );
1057 return( found );
1061 * called from na_settings_new
1062 * allocate and load the key files for global and user preferences
1064 static KeyFile *
1065 key_file_new( const gchar *dir )
1067 static const gchar *thisfn = "na_settings_key_file_new";
1068 KeyFile *key_file;
1069 GError *error;
1070 GFile *file;
1072 key_file = g_new0( KeyFile, 1 );
1074 key_file->key_file = g_key_file_new();
1075 key_file->fname = g_strdup_printf( "%s/%s.conf", dir, PACKAGE );
1077 error = NULL;
1078 file = g_file_new_for_path( key_file->fname );
1079 key_file->monitor = g_file_monitor_file( file, 0, NULL, &error );
1080 if( error ){
1081 g_warning( "%s: %s: %s", thisfn, key_file->fname, error->message );
1082 g_error_free( error );
1083 error = NULL;
1084 } else {
1085 key_file->handler = g_signal_connect( key_file->monitor, "changed", ( GCallback ) on_keyfile_changed, NULL );
1087 g_object_unref( file );
1089 return( key_file );
1093 * one of the two monitored configuration files have changed on the disk
1094 * we do not try to identify which keys have actually change
1095 * instead we trigger each registered consumer for the 'global' event
1097 * consumers which register for the 'global_conf' event are recorded
1098 * with a NULL key
1100 static void
1101 on_keyfile_changed( GFileMonitor *monitor,
1102 GFile *file, GFile *other_file, GFileMonitorEvent event_type )
1104 settings_new();
1105 na_timeout_event( &st_settings->private->timeout );
1108 static void
1109 on_keyfile_changed_timeout( void )
1111 static const gchar *thisfn = "na_settings_on_keyfile_changed_timeout";
1112 GList *new_content;
1113 GList *modifs;
1114 GList *ic, *im;
1115 const KeyValue *changed;
1116 const Consumer *consumer;
1117 gchar *group_prefix, *key;
1118 #ifdef NA_MAINTAINER_MODE
1119 gchar *value;
1120 #endif
1122 /* last individual notification is older that the st_burst_timeout
1123 * we may so suppose that the burst is terminated
1125 new_content = content_load();
1126 modifs = content_diff( st_settings->private->content, new_content );
1128 #ifdef NA_MAINTAINER_MODE
1129 g_debug( "%s: %d found update(s)", thisfn, g_list_length( modifs ));
1130 for( im = modifs ; im ; im = im->next ){
1131 changed = ( const KeyValue * ) im->data;
1132 value = na_boxed_get_string( changed->boxed );
1133 g_debug( "%s: group=%s, key=%s, value=%s", thisfn, changed->group, changed->def->key, value );
1134 g_free( value );
1136 #endif
1138 /* for each modification found,
1139 * - check if a consumer has registered for this key, and triggers callback if apply
1140 * - send a notification message
1142 for( im = modifs ; im ; im = im->next ){
1143 changed = ( const KeyValue * ) im->data;
1145 for( ic = st_settings->private->consumers ; ic ; ic = ic->next ){
1146 consumer = ( const Consumer * ) ic->data;
1147 group_prefix = NULL;
1149 if( !strcmp( consumer->monitored_key, NA_IPREFS_IO_PROVIDERS_READ_STATUS )){
1150 group_prefix = g_strdup_printf( "%s ", NA_IPREFS_IO_PROVIDER_GROUP );
1151 key = NA_IPREFS_IO_PROVIDER_READABLE;
1152 } else {
1153 key = consumer->monitored_key;
1156 if(( !group_prefix || g_str_has_prefix( changed->group, group_prefix )) && !strcmp( changed->def->key, key )){
1157 ( *( NASettingsKeyCallback ) consumer->callback )(
1158 changed->group,
1159 changed->def->key,
1160 na_boxed_get_pointer( changed->boxed ),
1161 changed->mandatory,
1162 consumer->user_data );
1165 g_free( group_prefix );
1168 g_debug( "%s: sending signal for group=%s, key=%s", thisfn, changed->group, changed->def->key );
1169 g_signal_emit_by_name( st_settings,
1170 SETTINGS_SIGNAL_KEY_CHANGED,
1171 changed->group, changed->def->key, changed->boxed, changed->mandatory );
1174 g_debug( "%s: releasing content", thisfn );
1175 g_list_foreach( st_settings->private->content, ( GFunc ) release_key_value, NULL );
1176 g_list_free( st_settings->private->content );
1177 st_settings->private->content = new_content;
1179 g_debug( "%s: releasing modifs", thisfn );
1180 g_list_foreach( modifs, ( GFunc ) release_key_value, NULL );
1181 g_list_free( modifs );
1184 static void
1185 on_key_changed_final_handler( NASettings *settings, gchar *group, gchar *key, NABoxed *new_value, gboolean mandatory )
1187 g_debug( "na_settings_on_key_changed_final_handler: group=%s, key=%s", group, key );
1188 na_boxed_dump( new_value );
1191 static KeyValue *
1192 peek_key_value_from_content( GList *content, const gchar *group, const gchar *key )
1194 KeyValue *value, *found;
1195 GList *ic;
1197 found = NULL;
1198 for( ic = content ; ic && !found ; ic = ic->next ){
1199 value = ( KeyValue * ) ic->data;
1200 if( !strcmp( value->group, group ) && !strcmp( value->def->key, key )){
1201 found = value;
1205 return( found );
1208 /* group may be NULL
1210 static KeyValue *
1211 read_key_value( const gchar *group, const gchar *key, gboolean *found, gboolean *mandatory )
1213 KeyDef *key_def;
1214 gboolean has_entry;
1215 KeyValue *key_value;
1217 key_value = NULL;
1218 if( found ){
1219 *found = FALSE;
1221 if( mandatory ){
1222 *mandatory = FALSE;
1225 settings_new();
1226 key_def = get_key_def( key );
1228 if( key_def ){
1229 has_entry = FALSE;
1230 key_value = read_key_value_from_key_file( st_settings->private->mandatory->key_file, group ? group : key_def->group, key, key_def );
1231 if( key_value ){
1232 has_entry = TRUE;
1233 if( found ){
1234 *found = TRUE;
1236 if( mandatory ){
1237 *mandatory = TRUE;
1240 if( !has_entry ){
1241 key_value = read_key_value_from_key_file( st_settings->private->user->key_file, group ? group : key_def->group, key, key_def );
1242 if( key_value ){
1243 has_entry = TRUE;
1244 if( found ){
1245 *found = TRUE;
1251 return( key_value );
1254 static KeyValue *
1255 read_key_value_from_key_file( GKeyFile *key_file, const gchar *group, const gchar *key, const KeyDef *key_def )
1257 static const gchar *thisfn = "na_settings_read_key_value_from_key_file";
1258 KeyValue *value;
1259 gchar *str;
1260 GError *error;
1262 g_debug( "%s: group=%s, key=%s", thisfn, group, key );
1264 value = NULL;
1265 error = NULL;
1267 switch( key_def->type ){
1269 case NA_DATA_TYPE_STRING:
1270 case NA_DATA_TYPE_STRING_LIST:
1271 case NA_DATA_TYPE_UINT:
1272 case NA_DATA_TYPE_UINT_LIST:
1273 case NA_DATA_TYPE_BOOLEAN:
1274 str = g_key_file_get_string( key_file, group, key, &error );
1275 if( error ){
1276 if( error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND && error->code != G_KEY_FILE_ERROR_GROUP_NOT_FOUND ){
1277 g_warning( "%s: %s", thisfn, error->message );
1279 g_error_free( error );
1281 /* key exists, but may be empty */
1282 } else {
1283 value = g_new0( KeyValue, 1 );
1284 value->group = g_strdup( group );
1285 value->def = key_def;
1286 switch( key_def->type ){
1287 case NA_DATA_TYPE_STRING:
1288 case NA_DATA_TYPE_UINT:
1289 case NA_DATA_TYPE_BOOLEAN:
1290 case NA_DATA_TYPE_STRING_LIST:
1291 case NA_DATA_TYPE_UINT_LIST:
1292 value->boxed = na_boxed_new_from_string( key_def->type, str );
1293 break;
1296 g_free( str );
1297 break;
1299 default:
1300 g_warning( "%s: unmanaged boxed type: %d", thisfn, key_def->type );
1301 break;
1304 return( value );
1308 * called from instance_finalize
1309 * release the list of registered consumers
1311 static void
1312 release_consumer( Consumer *consumer )
1314 g_free( consumer->monitored_key );
1315 g_free( consumer );
1319 * called from instance_dispose
1320 * release the opened and monitored GKeyFiles
1322 static void
1323 release_key_file( KeyFile *key_file )
1325 g_key_file_free( key_file->key_file );
1326 if( key_file->monitor ){
1327 if( key_file->handler ){
1328 g_signal_handler_disconnect( key_file->monitor, key_file->handler );
1330 g_file_monitor_cancel( key_file->monitor );
1331 g_object_unref( key_file->monitor );
1333 g_free( key_file->fname );
1334 g_free( key_file );
1338 * called from instance_finalize
1339 * release a KeyValue struct
1341 static void
1342 release_key_value( KeyValue *value )
1344 g_free(( gpointer ) value->group );
1345 g_object_unref( value->boxed );
1346 g_free( value );
1349 static gboolean
1350 set_key_value( const gchar *group, const gchar *key, const gchar *string )
1352 static const gchar *thisfn = "na_settings_set_key_value";
1353 KeyDef *key_def;
1354 const gchar *wgroup;
1355 gboolean ok;
1356 GError *error;
1358 ok = FALSE;
1359 settings_new();
1361 wgroup = group;
1362 if( !wgroup ){
1363 key_def = get_key_def( key );
1364 if( key_def ){
1365 wgroup = key_def->group;
1368 if( wgroup ){
1369 ok = TRUE;
1371 if( string ){
1372 g_key_file_set_string( st_settings->private->user->key_file, wgroup, key, string );
1374 } else {
1375 error = NULL;
1376 ok = g_key_file_remove_key( st_settings->private->user->key_file, wgroup, key, &error );
1377 if( error ){
1378 g_warning( "%s: g_key_file_remove_key: %s", thisfn, error->message );
1379 g_error_free( error );
1383 ok &= write_user_key_file();
1386 return( ok );
1389 static gboolean
1390 write_user_key_file( void )
1392 static const gchar *thisfn = "na_settings_write_user_key_file";
1393 gchar *data;
1394 GFile *file;
1395 GFileOutputStream *stream;
1396 GError *error;
1397 gsize length;
1399 error = NULL;
1400 settings_new();
1401 data = g_key_file_to_data( st_settings->private->user->key_file, &length, NULL );
1402 file = g_file_new_for_path( st_settings->private->user->fname );
1404 stream = g_file_replace( file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error );
1405 if( error ){
1406 g_warning( "%s: g_file_replace: %s", thisfn, error->message );
1407 g_error_free( error );
1408 if( stream ){
1409 g_object_unref( stream );
1411 g_object_unref( file );
1412 g_free( data );
1413 return( FALSE );
1416 g_output_stream_write( G_OUTPUT_STREAM( stream ), data, length, NULL, &error );
1417 if( error ){
1418 g_warning( "%s: g_output_stream_write: %s", thisfn, error->message );
1419 g_error_free( error );
1420 g_object_unref( stream );
1421 g_object_unref( file );
1422 g_free( data );
1423 return( FALSE );
1426 g_output_stream_close( G_OUTPUT_STREAM( stream ), NULL, &error );
1427 if( error ){
1428 g_warning( "%s: g_output_stream_close: %s", thisfn, error->message );
1429 g_error_free( error );
1430 g_object_unref( stream );
1431 g_object_unref( file );
1432 g_free( data );
1433 return( FALSE );
1436 g_object_unref( stream );
1437 g_object_unref( file );
1438 g_free( data );
1440 return( TRUE );