2 * Copyright © 2009, 2010 Codethink Limited
3 * Copyright © 2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the licence, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Ryan Lortie <desrt@desrt.ca>
21 * Matthias Clasen <mclasen@redhat.com>
26 #include "gsettingsbackendinternal.h"
27 #include "gnullsettingsbackend.h"
28 #include "gsimplepermission.h"
29 #include "giomodule-priv.h"
30 #include "gio-marshal.h"
39 G_DEFINE_ABSTRACT_TYPE (GSettingsBackend, g_settings_backend, G_TYPE_OBJECT)
41 typedef struct _GSettingsBackendClosure GSettingsBackendClosure;
42 typedef struct _GSettingsBackendWatch GSettingsBackendWatch;
44 struct _GSettingsBackendPrivate
46 GSettingsBackendWatch *watches;
51 * SECTION:gsettingsbackend
52 * @title: GSettingsBackend
53 * @short_description: an interface for settings backend implementations
54 * @include: gio/gsettingsbackend.h
55 * @see_also: #GSettings, #GIOExtensionPoint
57 * The #GSettingsBackend interface defines a generic interface for
58 * non-strictly-typed data that is stored in a hierarchy. To implement
59 * an alternative storage backend for #GSettings, you need to implement
60 * the #GSettingsBackend interface and then make it implement the
61 * extension point #G_SETTINGS_BACKEND_EXTENSION_POINT_NAME.
63 * The interface defines methods for reading and writing values, a
64 * method for determining if writing of certain values will fail
65 * (lockdown) and a change notification mechanism.
67 * The semantics of the interface are very precisely defined and
68 * implementations must carefully adhere to the expectations of
69 * callers that are documented on each of the interface methods.
71 * Some of the GSettingsBackend functions accept or return a #GTree.
72 * These trees always have strings as keys and #GVariant as values.
73 * g_settings_backend_create_tree() is a convenience function to create
77 * The #GSettingsBackend API is exported to allow third-party
78 * implementations, but does not carry the same stability guarantees
79 * as the public GIO API. For this reason, you have to define the
80 * C preprocessor symbol #G_SETTINGS_ENABLE_BACKEND before including
81 * <filename>gio/gsettingsbackend.h</filename>
86 is_key (const gchar *key)
91 g_return_val_if_fail (key != NULL, FALSE);
92 g_return_val_if_fail (key[0] == '/', FALSE);
94 for (i = 1; key[i]; i++)
95 g_return_val_if_fail (key[i] != '/' || key[i + 1] != '/', FALSE);
99 g_return_val_if_fail (key[length - 1] != '/', FALSE);
105 is_path (const gchar *path)
110 g_return_val_if_fail (path != NULL, FALSE);
111 g_return_val_if_fail (path[0] == '/', FALSE);
113 for (i = 1; path[i]; i++)
114 g_return_val_if_fail (path[i] != '/' || path[i + 1] != '/', FALSE);
118 g_return_val_if_fail (path[length - 1] == '/', FALSE);
124 g_settings_backend_get_active_context (void)
126 GMainContext *context;
129 if ((source = g_main_current_source ()))
130 context = g_source_get_context (source);
134 context = g_main_context_get_thread_default ();
137 context = g_main_context_default ();
143 struct _GSettingsBackendWatch
146 GMainContext *context;
147 GSettingsBackendChangedFunc changed;
148 GSettingsBackendPathChangedFunc path_changed;
149 GSettingsBackendKeysChangedFunc keys_changed;
150 GSettingsBackendWritableChangedFunc writable_changed;
151 GSettingsBackendPathWritableChangedFunc path_writable_changed;
153 GSettingsBackendWatch *next;
156 struct _GSettingsBackendClosure
158 void (*function) (GSettingsBackend *backend,
164 GSettingsBackend *backend;
168 GBoxedFreeFunc data1_free;
173 g_settings_backend_watch_weak_notify (gpointer data,
174 GObject *where_the_object_was)
176 GSettingsBackend *backend = data;
177 GSettingsBackendWatch **ptr;
179 /* search and remove */
180 g_static_mutex_lock (&backend->priv->lock);
181 for (ptr = &backend->priv->watches; *ptr; ptr = &(*ptr)->next)
182 if ((*ptr)->target == where_the_object_was)
184 GSettingsBackendWatch *tmp = *ptr;
187 g_slice_free (GSettingsBackendWatch, tmp);
189 g_static_mutex_unlock (&backend->priv->lock);
193 /* we didn't find it. that shouldn't happen. */
194 g_assert_not_reached ();
198 * g_settings_backend_watch:
199 * @backend: a #GSettingsBackend
200 * @target: the GObject (typically GSettings instance) to call back to
201 * @context: a #GMainContext, or %NULL
204 * Registers a new watch on a #GSettingsBackend.
206 * note: %NULL @context does not mean "default main context" but rather,
207 * "it is okay to dispatch in any context". If the default main context
208 * is specifically desired then it must be given.
210 * note also: if you want to get meaningful values for the @origin_tag
211 * that appears as an argument to some of the callbacks, you *must* have
212 * @context as %NULL. Otherwise, you are subject to cross-thread
213 * dispatching and whatever owned @origin_tag at the time that the event
214 * occured may no longer own it. This is a problem if you consider that
215 * you may now be the new owner of that address and mistakenly think
216 * that the event in question originated from yourself.
218 * tl;dr: If you give a non-%NULL @context then you must ignore the
219 * value of @origin_tag given to any callbacks.
222 g_settings_backend_watch (GSettingsBackend *backend,
224 GMainContext *context,
225 GSettingsBackendChangedFunc changed,
226 GSettingsBackendPathChangedFunc path_changed,
227 GSettingsBackendKeysChangedFunc keys_changed,
228 GSettingsBackendWritableChangedFunc writable_changed,
229 GSettingsBackendPathWritableChangedFunc path_writable_changed)
231 GSettingsBackendWatch *watch;
233 /* For purposes of discussion, we assume that our target is a
234 * GSettings instance.
236 * Our strategy to defend against the final reference dropping on the
237 * GSettings object in a thread other than the one that is doing the
238 * dispatching is as follows:
240 * 1) hold a GObject reference on the GSettings during an outstanding
241 * dispatch. This ensures that the delivery is always possible.
243 * 2) hold a weak reference on the GSettings at other times. This
244 * allows us to receive early notification of pending destruction
245 * of the object. At this point, it is still safe to obtain a
246 * reference on the GObject to keep it alive, so #1 will work up
247 * to that point. After that point, we'll have been able to drop
248 * the watch from the list.
250 * Note, in particular, that it's not possible to simply have an
251 * "unwatch" function that gets called from the finalize function of
252 * the GSettings instance because, by that point it is no longer
253 * possible to keep the object alive using g_object_ref() and we would
254 * have no way of knowing this.
256 * Note also that we do not need to hold a reference on the main
257 * context here since the GSettings instance does that for us and we
258 * will receive the weak notify long before it is dropped. We don't
259 * even need to hold it during dispatches because our reference on the
260 * GSettings will prevent the finalize from running and dropping the
261 * ref on the context.
263 * All access to the list holds a mutex. We have some strategies to
264 * avoid some of the pain that would be associated with that.
267 watch = g_slice_new (GSettingsBackendWatch);
268 watch->context = context;
269 watch->target = target;
270 g_object_weak_ref (target, g_settings_backend_watch_weak_notify, backend);
272 watch->changed = changed;
273 watch->path_changed = path_changed;
274 watch->keys_changed = keys_changed;
275 watch->writable_changed = writable_changed;
276 watch->path_writable_changed = path_writable_changed;
278 /* linked list prepend */
279 g_static_mutex_lock (&backend->priv->lock);
280 watch->next = backend->priv->watches;
281 backend->priv->watches = watch;
282 g_static_mutex_unlock (&backend->priv->lock);
286 g_settings_backend_unwatch (GSettingsBackend *backend,
289 /* Our caller surely owns a reference on 'target', so the order of
290 * these two calls is unimportant.
292 g_object_weak_unref (target, g_settings_backend_watch_weak_notify, backend);
293 g_settings_backend_watch_weak_notify (backend, target);
297 g_settings_backend_invoke_closure (gpointer user_data)
299 GSettingsBackendClosure *closure = user_data;
301 closure->function (closure->backend, closure->target, closure->name,
302 closure->data1, closure->data2);
304 closure->data1_free (closure->data1);
305 g_object_unref (closure->backend);
306 g_object_unref (closure->target);
307 g_free (closure->name);
309 g_slice_free (GSettingsBackendClosure, closure);
315 pointer_id (gpointer a)
321 pointer_ignore (gpointer a)
326 g_settings_backend_dispatch_signal (GSettingsBackend *backend,
327 gsize function_offset,
330 GBoxedCopyFunc data1_copy,
331 GBoxedFreeFunc data1_free,
334 GMainContext *context, *here_and_now;
335 GSettingsBackendWatch *watch;
337 /* We need to hold the mutex here (to prevent a node from being
338 * deleted as we are traversing the list). Since we should not
339 * re-enter user code while holding this mutex, we create a
340 * one-time-use GMainContext and populate it with the events that we
341 * would have called directly. We dispatch these events after
342 * releasing the lock. Note that the GObject reference is acquired on
343 * the target while holding the mutex and the mutex needs to be held
344 * as part of the destruction of any GSettings instance (via the weak
345 * reference handling). This is the key to the safety of the whole
349 if (data1_copy == NULL)
350 data1_copy = pointer_id;
352 if (data1_free == NULL)
353 data1_free = pointer_ignore;
355 context = g_settings_backend_get_active_context ();
356 here_and_now = g_main_context_new ();
358 /* traverse the (immutable while holding lock) list */
359 g_static_mutex_lock (&backend->priv->lock);
360 for (watch = backend->priv->watches; watch; watch = watch->next)
362 GSettingsBackendClosure *closure;
365 closure = g_slice_new (GSettingsBackendClosure);
366 closure->backend = g_object_ref (backend);
367 closure->target = g_object_ref (watch->target);
368 closure->function = G_STRUCT_MEMBER (void *, watch, function_offset);
369 closure->name = g_strdup (name);
370 closure->data1 = data1_copy (data1);
371 closure->data1_free = data1_free;
372 closure->data2 = data2;
374 source = g_idle_source_new ();
375 g_source_set_priority (source, G_PRIORITY_DEFAULT);
376 g_source_set_callback (source,
377 g_settings_backend_invoke_closure,
380 if (watch->context && watch->context != context)
381 g_source_attach (source, watch->context);
383 g_source_attach (source, here_and_now);
385 g_source_unref (source);
387 g_static_mutex_unlock (&backend->priv->lock);
389 while (g_main_context_iteration (here_and_now, FALSE));
390 g_main_context_unref (here_and_now);
394 * g_settings_backend_changed:
395 * @backend: a #GSettingsBackend implementation
396 * @key: the name of the key
397 * @origin_tag: the origin tag
399 * Signals that a single key has possibly changed. Backend
400 * implementations should call this if a key has possibly changed its
403 * @key must be a valid key (ie: starting with a slash, not containing
404 * '//', and not ending with a slash).
406 * The implementation must call this function during any call to
407 * g_settings_backend_write(), before the call returns (except in the
408 * case that no keys are actually changed and it cares to detect this
409 * fact). It may not rely on the existence of a mainloop for
410 * dispatching the signal later.
412 * The implementation may call this function at any other time it likes
413 * in response to other events (such as changes occuring outside of the
414 * program). These calls may originate from a mainloop or may originate
415 * in response to any other action (including from calls to
416 * g_settings_backend_write()).
418 * In the case that this call is in response to a call to
419 * g_settings_backend_write() then @origin_tag must be set to the same
420 * value that was passed to that call.
425 g_settings_backend_changed (GSettingsBackend *backend,
429 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
430 g_return_if_fail (is_key (key));
432 g_settings_backend_dispatch_signal (backend,
433 G_STRUCT_OFFSET (GSettingsBackendWatch,
435 key, origin_tag, NULL, NULL, NULL);
439 * g_settings_backend_keys_changed:
440 * @backend: a #GSettingsBackend implementation
441 * @path: the path containing the changes
442 * @items: the %NULL-terminated list of changed keys
443 * @origin_tag: the origin tag
445 * Signals that a list of keys have possibly changed. Backend
446 * implementations should call this if keys have possibly changed their
449 * @path must be a valid path (ie: starting and ending with a slash and
450 * not containing '//'). Each string in @items must form a valid key
451 * name when @path is prefixed to it (ie: each item must not start or
452 * end with '/' and must not contain '//').
454 * The meaning of this signal is that any of the key names resulting
455 * from the contatenation of @path with each item in @items may have
458 * The same rules for when notifications must occur apply as per
459 * g_settings_backend_changed(). These two calls can be used
460 * interchangeably if exactly one item has changed (although in that
461 * case g_settings_backend_changed() is definitely preferred).
463 * For efficiency reasons, the implementation should strive for @path to
464 * be as long as possible (ie: the longest common prefix of all of the
465 * keys that were changed) but this is not strictly required.
470 g_settings_backend_keys_changed (GSettingsBackend *backend,
472 gchar const * const *items,
475 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
476 g_return_if_fail (is_path (path));
478 /* XXX: should do stricter checking (ie: inspect each item) */
479 g_return_if_fail (items != NULL);
481 g_settings_backend_dispatch_signal (backend,
482 G_STRUCT_OFFSET (GSettingsBackendWatch,
484 path, (gpointer) items,
485 (GBoxedCopyFunc) g_strdupv,
486 (GBoxedFreeFunc) g_strfreev,
491 * g_settings_backend_path_changed:
492 * @backend: a #GSettingsBackend implementation
493 * @path: the path containing the changes
494 * @origin_tag: the origin tag
496 * Signals that all keys below a given path may have possibly changed.
497 * Backend implementations should call this if an entire path of keys
498 * have possibly changed their values.
500 * @path must be a valid path (ie: starting and ending with a slash and
501 * not containing '//').
503 * The meaning of this signal is that any of the key which has a name
504 * starting with @path may have changed.
506 * The same rules for when notifications must occur apply as per
507 * g_settings_backend_changed(). This call might be an appropriate
508 * reasponse to a 'reset' call but implementations are also free to
509 * explicitly list the keys that were affected by that call if they can
512 * For efficiency reasons, the implementation should strive for @path to
513 * be as long as possible (ie: the longest common prefix of all of the
514 * keys that were changed) but this is not strictly required. As an
515 * example, if this function is called with the path of "/" then every
516 * single key in the application will be notified of a possible change.
521 g_settings_backend_path_changed (GSettingsBackend *backend,
525 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
526 g_return_if_fail (is_path (path));
528 g_settings_backend_dispatch_signal (backend,
529 G_STRUCT_OFFSET (GSettingsBackendWatch,
531 path, origin_tag, NULL, NULL, NULL);
535 * g_settings_backend_writable_changed:
536 * @backend: a #GSettingsBackend implementation
537 * @key: the name of the key
539 * Signals that the writability of a single key has possibly changed.
541 * Since GSettings performs no locking operations for itself, this call
542 * will always be made in response to external events.
547 g_settings_backend_writable_changed (GSettingsBackend *backend,
550 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
551 g_return_if_fail (is_key (key));
553 g_settings_backend_dispatch_signal (backend,
554 G_STRUCT_OFFSET (GSettingsBackendWatch,
556 key, NULL, NULL, NULL, NULL);
560 * g_settings_backend_path_writable_changed:
561 * @backend: a #GSettingsBackend implementation
562 * @path: the name of the path
564 * Signals that the writability of all keys below a given path may have
567 * Since GSettings performs no locking operations for itself, this call
568 * will always be made in response to external events.
573 g_settings_backend_path_writable_changed (GSettingsBackend *backend,
576 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
577 g_return_if_fail (is_path (path));
579 g_settings_backend_dispatch_signal (backend,
580 G_STRUCT_OFFSET (GSettingsBackendWatch,
581 path_writable_changed),
582 path, NULL, NULL, NULL, NULL);
594 g_settings_backend_flatten_one (gpointer key,
598 FlattenState *state = user_data;
599 const gchar *skey = key;
602 g_return_val_if_fail (is_key (key), TRUE);
604 /* calculate longest common prefix */
605 if (state->prefix == NULL)
609 /* first key? just take the prefix up to the last '/' */
610 state->prefix = g_strdup (skey);
611 last_byte = strrchr (state->prefix, '/') + 1;
612 state->prefix_len = last_byte - state->prefix;
617 /* find the first character that does not match. we will
618 * definitely find one because the prefix ends in '/' and the key
619 * does not. also: no two keys in the tree are the same.
621 for (i = 0; state->prefix[i] == skey[i]; i++);
623 /* check if we need to shorten the prefix */
624 if (state->prefix[i] != '\0')
626 /* find the nearest '/', terminate after it */
627 while (state->prefix[i - 1] != '/')
630 state->prefix[i] = '\0';
631 state->prefix_len = i;
636 /* save the entire item into the array.
637 * the prefixes will be removed later.
639 *state->keys++ = key;
642 *state->values++ = value;
648 * g_settings_backend_flatten_tree:
649 * @tree: a #GTree containing the changes
650 * @path: the location to save the path
651 * @keys: the location to save the relative keys
652 * @values: the location to save the values, or %NULL
654 * Calculate the longest common prefix of all keys in a tree and write
655 * out an array of the key names relative to that prefix and,
656 * optionally, the value to store at each of those keys.
658 * You must free the value returned in @path, @keys and @values using
659 * g_free(). You should not attempt to free or unref the contents of
665 g_settings_backend_flatten_tree (GTree *tree,
670 FlattenState state = { 0, };
673 nnodes = g_tree_nnodes (tree);
675 *keys = state.keys = g_new (const gchar *, nnodes + 1);
676 state.keys[nnodes] = NULL;
680 *values = state.values = g_new (GVariant *, nnodes + 1);
681 state.values[nnodes] = NULL;
684 g_tree_foreach (tree, g_settings_backend_flatten_one, &state);
685 g_return_if_fail (*keys + nnodes == state.keys);
687 *path = state.prefix;
689 *--state.keys += state.prefix_len;
693 * g_settings_backend_changed_tree:
694 * @backend: a #GSettingsBackend implementation
695 * @tree: a #GTree containing the changes
696 * @origin_tag: the origin tag
698 * This call is a convenience wrapper. It gets the list of changes from
699 * @tree, computes the longest common prefix and calls
700 * g_settings_backend_changed().
705 g_settings_backend_changed_tree (GSettingsBackend *backend,
709 GSettingsBackendWatch *watch;
713 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
715 g_settings_backend_flatten_tree (tree, &path, &keys, NULL);
717 for (watch = backend->priv->watches; watch; watch = watch->next)
718 watch->keys_changed (backend, watch->target, path, keys, origin_tag);
725 * g_settings_backend_read:
726 * @backend: a #GSettingsBackend implementation
727 * @key: the key to read
728 * @expected_type: a #GVariantType
729 * @default_value: if the default value should be returned
730 * @returns: the value that was read, or %NULL
732 * Reads a key. This call will never block.
734 * If the key exists, the value associated with it will be returned.
735 * If the key does not exist, %NULL will be returned.
737 * The returned value will be of the type given in @expected_type. If
738 * the backend stored a value of a different type then %NULL will be
741 * If @default_value is %TRUE then this gets the default value from the
742 * backend (ie: the one that the backend would contain if
743 * g_settings_reset() were called).
746 g_settings_backend_read (GSettingsBackend *backend,
748 const GVariantType *expected_type,
749 gboolean default_value)
753 value = G_SETTINGS_BACKEND_GET_CLASS (backend)
754 ->read (backend, key, expected_type, default_value);
756 if G_UNLIKELY (value && !g_variant_is_of_type (value, expected_type))
758 g_variant_unref (value);
766 * g_settings_backend_write:
767 * @backend: a #GSettingsBackend implementation
768 * @key: the name of the key
769 * @value: a #GVariant value to write to this key
770 * @origin_tag: the origin tag
771 * @returns: %TRUE if the write succeeded, %FALSE if the key was not writable
773 * Writes exactly one key.
775 * This call does not fail. During this call a
776 * #GSettingsBackend::changed signal will be emitted if the value of the
777 * key has changed. The updated key value will be visible to any signal
780 * One possible method that an implementation might deal with failures is
781 * to emit a second "changed" signal (either during this call, or later)
782 * to indicate that the affected keys have suddenly "changed back" to their
786 g_settings_backend_write (GSettingsBackend *backend,
791 return G_SETTINGS_BACKEND_GET_CLASS (backend)
792 ->write (backend, key, value, origin_tag);
796 * g_settings_backend_write_keys:
797 * @backend: a #GSettingsBackend implementation
798 * @values: a #GTree containing key-value pairs to write
799 * @origin_tag: the origin tag
801 * Writes one or more keys. This call will never block.
803 * The key of each item in the tree is the key name to write to and the
804 * value is a #GVariant to write. The proper type of #GTree for this
805 * call can be created with g_settings_backend_create_tree(). This call
806 * might take a reference to the tree; you must not modified the #GTree
807 * after passing it to this call.
809 * This call does not fail. During this call a #GSettingsBackend::changed
810 * signal will be emitted if any keys have been changed. The new values of
811 * all updated keys will be visible to any signal callbacks.
813 * One possible method that an implementation might deal with failures is
814 * to emit a second "changed" signal (either during this call, or later)
815 * to indicate that the affected keys have suddenly "changed back" to their
819 g_settings_backend_write_keys (GSettingsBackend *backend,
823 return G_SETTINGS_BACKEND_GET_CLASS (backend)
824 ->write_keys (backend, tree, origin_tag);
828 * g_settings_backend_reset:
829 * @backend: a #GSettingsBackend implementation
830 * @key: the name of a key
831 * @origin_tag: the origin tag
833 * "Resets" the named key to its "default" value (ie: after system-wide
834 * defaults, mandatory keys, etc. have been taken into account) or possibly
838 g_settings_backend_reset (GSettingsBackend *backend,
842 G_SETTINGS_BACKEND_GET_CLASS (backend)
843 ->reset (backend, key, origin_tag);
847 * g_settings_backend_reset_path:
848 * @backend: a #GSettingsBackend implementation
849 * @name: the name of a key or path
850 * @origin_tag: the origin tag
852 * "Resets" the named path. This means that every key under the path is
856 g_settings_backend_reset_path (GSettingsBackend *backend,
860 G_SETTINGS_BACKEND_GET_CLASS (backend)
861 ->reset_path (backend, path, origin_tag);
865 * g_settings_backend_get_writable:
866 * @backend: a #GSettingsBackend implementation
867 * @key: the name of a key
868 * @returns: %TRUE if the key is writable
870 * Finds out if a key is available for writing to. This is the
871 * interface through which 'lockdown' is implemented. Locked down
872 * keys will have %FALSE returned by this call.
874 * You should not write to locked-down keys, but if you do, the
875 * implementation will deal with it.
878 g_settings_backend_get_writable (GSettingsBackend *backend,
881 return G_SETTINGS_BACKEND_GET_CLASS (backend)
882 ->get_writable (backend, key);
886 * g_settings_backend_unsubscribe:
887 * @backend: a #GSettingsBackend
888 * @name: a key or path to subscribe to
890 * Reverses the effect of a previous call to
891 * g_settings_backend_subscribe().
894 g_settings_backend_unsubscribe (GSettingsBackend *backend,
897 G_SETTINGS_BACKEND_GET_CLASS (backend)
898 ->unsubscribe (backend, name);
902 * g_settings_backend_subscribe:
903 * @backend: a #GSettingsBackend
904 * @name: a key or path to subscribe to
906 * Requests that change signals be emitted for events on @name.
909 g_settings_backend_subscribe (GSettingsBackend *backend,
912 G_SETTINGS_BACKEND_GET_CLASS (backend)
913 ->subscribe (backend, name);
917 g_settings_backend_finalize (GObject *object)
919 GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
921 g_static_mutex_unlock (&backend->priv->lock);
923 G_OBJECT_CLASS (g_settings_backend_parent_class)
928 ignore_subscription (GSettingsBackend *backend,
934 g_settings_backend_init (GSettingsBackend *backend)
936 backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend,
937 G_TYPE_SETTINGS_BACKEND,
938 GSettingsBackendPrivate);
939 g_static_mutex_init (&backend->priv->lock);
943 g_settings_backend_class_init (GSettingsBackendClass *class)
945 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
947 class->subscribe = ignore_subscription;
948 class->unsubscribe = ignore_subscription;
950 gobject_class->finalize = g_settings_backend_finalize;
952 g_type_class_add_private (class, sizeof (GSettingsBackendPrivate));
956 * g_settings_backend_create_tree:
957 * @returns: a new #GTree
959 * This is a convenience function for creating a tree that is compatible
960 * with g_settings_backend_write(). It merely calls g_tree_new_full()
961 * with strcmp(), g_free() and g_variant_unref().
964 g_settings_backend_create_tree (void)
966 return g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
967 g_free, (GDestroyNotify) g_variant_unref);
971 * g_settings_backend_get_default:
972 * @returns: the default #GSettingsBackend
974 * Returns the default #GSettingsBackend. It is possible to override
975 * the default by setting the <envar>GSETTINGS_BACKEND</envar>
976 * environment variable to the name of a settings backend.
978 * The user gets a reference to the backend.
981 g_settings_backend_get_default (void)
983 static gsize backend;
985 if (g_once_init_enter (&backend))
987 GSettingsBackend *instance;
988 GIOExtensionPoint *point;
989 GIOExtension *extension;
990 GType extension_type;
994 _g_io_modules_ensure_loaded ();
996 point = g_io_extension_point_lookup (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME);
999 if ((env = getenv ("GSETTINGS_BACKEND")))
1001 extension = g_io_extension_point_get_extension_by_name (point, env);
1003 if (extension == NULL)
1004 g_warning ("Can't find GSettings backend '%s' given in "
1005 "GSETTINGS_BACKEND environment variable", env);
1008 if (extension == NULL)
1010 extensions = g_io_extension_point_get_extensions (point);
1012 if (extensions == NULL)
1013 g_error ("No GSettingsBackend implementations exist.");
1015 extension = extensions->data;
1018 extension_type = g_io_extension_get_type (extension);
1019 instance = g_object_new (extension_type, NULL);
1021 g_once_init_leave (&backend, (gsize) instance);
1024 return g_object_ref ((void *) backend);
1028 * g_settings_backend_get_permission:
1029 * @backend: a #GSettingsBackend
1031 * @returns: a non-%NULL #GPermission
1033 * Gets the permission object associated with writing to keys below
1034 * @path on @backend.
1036 * If this is not implemented in the backend, then a %TRUE
1037 * #GSimplePermission is returned.
1040 g_settings_backend_get_permission (GSettingsBackend *backend,
1043 GSettingsBackendClass *class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1045 if (class->get_permission)
1046 return class->get_permission (backend, path);
1048 return g_simple_permission_new (TRUE);
1052 * g_settings_backend_sync_default:
1054 * Syncs the default backend.
1057 g_settings_backend_sync_default (void)
1059 GSettingsBackendClass *class;
1060 GSettingsBackend *backend;
1062 backend = g_settings_backend_get_default ();
1063 class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1066 class->sync (backend);
1069 #define __G_SETTINGS_BACKEND_C__
1070 #include "gioaliasdef.c"