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.
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)
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
;
60 NASettingsPrivate
*private;
64 typedef struct _NASettingsClassPrivate NASettingsClassPrivate
;
69 NASettingsClassPrivate
*private;
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.
87 GFileMonitor
*monitor
;
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.
104 gchar
*monitored_key
;
110 /* private instance data
112 struct _NASettingsPrivate
{
113 gboolean dispose_has_run
;
121 #define GROUP_NACT "nact"
122 #define GROUP_RUNTIME "runtime"
128 const gchar
*default_value
;
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" },
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.
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 );
243 settings_get_type( void )
245 static GType object_type
= 0;
248 object_type
= register_type();
251 return( object_type
);
255 register_type( void )
257 static const gchar
*thisfn
= "na_settings_register_type";
260 static GTypeInfo info
= {
261 sizeof( NASettingsClass
),
262 ( GBaseInitFunc
) NULL
,
263 ( GBaseFinalizeFunc
) NULL
,
264 ( GClassInitFunc
) class_init
,
267 sizeof( NASettings
),
269 ( GInstanceInitFunc
) instance_init
272 g_debug( "%s", thisfn
);
274 type
= g_type_register_static( G_TYPE_OBJECT
, "NASettings", &info
, 0 );
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,
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
,
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
,
323 G_TYPE_STRING
, G_TYPE_STRING
, G_TYPE_POINTER
, G_TYPE_BOOLEAN
);
327 instance_init( GTypeInstance
*instance
, gpointer klass
)
329 static const gchar
*thisfn
= "na_settings_instance_init";
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;
354 instance_dispose( GObject
*object
)
356 static const gchar
*thisfn
= "na_settings_instance_dispose";
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
);
379 instance_finalize( GObject
*object
)
381 static const gchar
*thisfn
= "na_settings_instance_finalize";
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
);
407 * Returns: a new #NASettings object which should be g_object_unref()
413 static const gchar
*thisfn
= "na_settings_new";
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
);
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
);
429 st_settings
->private->content
= content_load();
437 na_settings_free( void )
440 g_object_unref( st_settings
);
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.
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
;
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.
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.
513 na_settings_get_boolean_ex( const gchar
*group
, const gchar
*key
, gboolean
*found
, gboolean
*mandatory
)
520 key_value
= read_key_value( group
, key
, found
, mandatory
);
523 value
= na_boxed_get_boolean( key_value
->boxed
);
524 release_key_value( key_value
);
527 key_def
= get_key_def( key
);
529 value
= ( key_def
->default_value
? ( strcasecmp( key_def
->default_value
, "true" ) == 0 || atoi( key_def
->default_value
) != 0 ) : FALSE
);
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.
556 na_settings_get_string( const gchar
*key
, gboolean
*found
, gboolean
*mandatory
)
563 key_value
= read_key_value( NULL
, key
, found
, mandatory
);
566 value
= na_boxed_get_string( key_value
->boxed
);
567 release_key_value( key_value
);
570 key_def
= get_key_def( key
);
571 if( key_def
&& key_def
->default_value
){
572 value
= g_strdup( key_def
->default_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.
599 na_settings_get_string_list( const gchar
*key
, gboolean
*found
, gboolean
*mandatory
)
606 key_value
= read_key_value( NULL
, key
, found
, mandatory
);
609 value
= na_boxed_get_string_list( key_value
->boxed
);
610 release_key_value( key_value
);
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
));
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.
641 na_settings_get_uint( const gchar
*key
, gboolean
*found
, gboolean
*mandatory
)
648 key_value
= read_key_value( NULL
, key
, found
, mandatory
);
651 value
= na_boxed_get_uint( key_value
->boxed
);
652 release_key_value( key_value
);
655 key_def
= get_key_def( key
);
656 if( key_def
&& key_def
->default_value
){
657 value
= atoi( key_def
->default_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.
684 na_settings_get_uint_list( const gchar
*key
, gboolean
*found
, gboolean
*mandatory
)
691 key_value
= read_key_value( NULL
, key
, found
, mandatory
);
694 value
= na_boxed_get_uint_list( key_value
->boxed
);
695 release_key_value( key_value
);
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
)));
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.
722 na_settings_set_boolean( const gchar
*key
, gboolean value
)
727 string
= g_strdup_printf( "%s", value
? "true" : "false" );
728 ok
= set_key_value( NULL
, key
, string
);
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.
747 na_settings_set_boolean_ex( const gchar
*group
, const gchar
*key
, gboolean value
)
752 string
= g_strdup_printf( "%s", value
? "true" : "false" );
753 ok
= set_key_value( group
, key
, string
);
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.
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.
794 na_settings_set_string_list( const gchar
*key
, const GSList
*value
)
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
);
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.
825 na_settings_set_uint( const gchar
*key
, guint value
)
830 string
= g_strdup_printf( "%u", value
);
831 ok
= set_key_value( NULL
, key
, string
);
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.
852 na_settings_set_uint_list( const gchar
*key
, const GList
*value
)
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
);
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.
881 na_settings_get_groups( void )
889 array
= g_key_file_get_groups( st_settings
->private->mandatory
->key_file
, NULL
);
891 groups
= na_core_utils_slist_from_array(( const gchar
** ) array
);
895 array
= g_key_file_get_groups( st_settings
->private->user
->key_file
, NULL
);
897 groups
= g_slist_concat( groups
, na_core_utils_slist_from_array(( const gchar
** ) array
));
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
917 content_diff( GList
*old
, GList
*new )
919 GList
*diffs
, *io
, *in
;
920 KeyValue
*kold
, *knew
, *kdiff
;
925 for( io
= old
; io
; io
= io
->next
){
926 kold
= ( KeyValue
* ) io
->data
;
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
){
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
);
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
;
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
){
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
);
977 /* load the content of the two configuration files (actually of _the_ configuration)
978 * taking care of not overriding mandatory preferences with user ones
986 content
= content_load_keys( NULL
, st_settings
->private->mandatory
, TRUE
);
987 content
= content_load_keys( content
, st_settings
->private->user
, FALSE
);
993 content_load_keys( GList
*content
, KeyFile
*key_file
, gboolean mandatory
)
995 static const gchar
*thisfn
= "na_settings_content_load_keys";
997 gchar
**groups
, **ig
;
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
);
1011 groups
= g_key_file_get_groups( key_file
->key_file
, NULL
);
1014 keys
= g_key_file_get_keys( key_file
->key_file
, *ig
, NULL
, NULL
);
1017 key_def
= get_key_def( *ik
);
1019 key_value
= peek_key_value_from_content( content
, *ig
, *ik
);
1021 key_value
= read_key_value_from_key_file( key_file
->key_file
, *ig
, *ik
, key_def
);
1023 key_value
->mandatory
= mandatory
;
1024 content
= g_list_prepend( content
, key_value
);
1033 g_strfreev( groups
);
1040 get_key_def( const gchar
*key
)
1042 static const gchar
*thisfn
= "na_settings_get_key_def";
1043 KeyDef
*found
= NULL
;
1046 idef
= ( KeyDef
* ) st_def_keys
;
1047 while( idef
->key
&& !found
){
1048 if( !strcmp( idef
->key
, key
)){
1054 g_warning( "%s: no KeyDef found for key=%s", thisfn
, key
);
1061 * called from na_settings_new
1062 * allocate and load the key files for global and user preferences
1065 key_file_new( const gchar
*dir
)
1067 static const gchar
*thisfn
= "na_settings_key_file_new";
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
);
1078 file
= g_file_new_for_path( key_file
->fname
);
1079 key_file
->monitor
= g_file_monitor_file( file
, 0, NULL
, &error
);
1081 g_warning( "%s: %s: %s", thisfn
, key_file
->fname
, error
->message
);
1082 g_error_free( error
);
1085 key_file
->handler
= g_signal_connect( key_file
->monitor
, "changed", ( GCallback
) on_keyfile_changed
, NULL
);
1087 g_object_unref( 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
1101 on_keyfile_changed( GFileMonitor
*monitor
,
1102 GFile
*file
, GFile
*other_file
, GFileMonitorEvent event_type
)
1105 na_timeout_event( &st_settings
->private->timeout
);
1109 on_keyfile_changed_timeout( void )
1111 static const gchar
*thisfn
= "na_settings_on_keyfile_changed_timeout";
1115 const KeyValue
*changed
;
1116 const Consumer
*consumer
;
1117 gchar
*group_prefix
, *key
;
1118 #ifdef NA_MAINTAINER_MODE
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
);
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
;
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
)(
1160 na_boxed_get_pointer( changed
->boxed
),
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
);
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
);
1192 peek_key_value_from_content( GList
*content
, const gchar
*group
, const gchar
*key
)
1194 KeyValue
*value
, *found
;
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
)){
1208 /* group may be NULL
1211 read_key_value( const gchar
*group
, const gchar
*key
, gboolean
*found
, gboolean
*mandatory
)
1215 KeyValue
*key_value
;
1226 key_def
= get_key_def( key
);
1230 key_value
= read_key_value_from_key_file( st_settings
->private->mandatory
->key_file
, group
? group
: key_def
->group
, key
, key_def
);
1241 key_value
= read_key_value_from_key_file( st_settings
->private->user
->key_file
, group
? group
: key_def
->group
, key
, key_def
);
1251 return( key_value
);
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";
1262 g_debug( "%s: group=%s, key=%s", thisfn
, group
, key
);
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
);
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 */
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
);
1300 g_warning( "%s: unmanaged boxed type: %d", thisfn
, key_def
->type
);
1308 * called from instance_finalize
1309 * release the list of registered consumers
1312 release_consumer( Consumer
*consumer
)
1314 g_free( consumer
->monitored_key
);
1319 * called from instance_dispose
1320 * release the opened and monitored GKeyFiles
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
);
1338 * called from instance_finalize
1339 * release a KeyValue struct
1342 release_key_value( KeyValue
*value
)
1344 g_free(( gpointer
) value
->group
);
1345 g_object_unref( value
->boxed
);
1350 set_key_value( const gchar
*group
, const gchar
*key
, const gchar
*string
)
1352 static const gchar
*thisfn
= "na_settings_set_key_value";
1354 const gchar
*wgroup
;
1363 key_def
= get_key_def( key
);
1365 wgroup
= key_def
->group
;
1372 g_key_file_set_string( st_settings
->private->user
->key_file
, wgroup
, key
, string
);
1376 ok
= g_key_file_remove_key( st_settings
->private->user
->key_file
, wgroup
, key
, &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();
1390 write_user_key_file( void )
1392 static const gchar
*thisfn
= "na_settings_write_user_key_file";
1395 GFileOutputStream
*stream
;
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
);
1406 g_warning( "%s: g_file_replace: %s", thisfn
, error
->message
);
1407 g_error_free( error
);
1409 g_object_unref( stream
);
1411 g_object_unref( file
);
1416 g_output_stream_write( G_OUTPUT_STREAM( stream
), data
, length
, NULL
, &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
);
1426 g_output_stream_close( G_OUTPUT_STREAM( stream
), NULL
, &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
);
1436 g_object_unref( stream
);
1437 g_object_unref( file
);