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 "gsimplepermission.h"
28 #include "giomodule-priv.h"
29 #include "gio-marshal.h"
37 G_DEFINE_ABSTRACT_TYPE (GSettingsBackend, g_settings_backend, G_TYPE_OBJECT)
39 typedef struct _GSettingsBackendClosure GSettingsBackendClosure;
40 typedef struct _GSettingsBackendWatch GSettingsBackendWatch;
42 struct _GSettingsBackendPrivate
44 GSettingsBackendWatch *watches;
48 /* For g_settings_backend_sync_default(), we only want to actually do
49 * the sync if the backend already exists. This avoids us creating an
50 * entire GSettingsBackend in order to call a do-nothing sync()
51 * operation on it. This variable lets us avoid that.
53 static gboolean g_settings_has_backend;
56 * SECTION:gsettingsbackend
57 * @title: GSettingsBackend
58 * @short_description: Interface for settings backend implementations
59 * @include: gio/gsettingsbackend.h
60 * @see_also: #GSettings, #GIOExtensionPoint
62 * The #GSettingsBackend interface defines a generic interface for
63 * non-strictly-typed data that is stored in a hierarchy. To implement
64 * an alternative storage backend for #GSettings, you need to implement
65 * the #GSettingsBackend interface and then make it implement the
66 * extension point #G_SETTINGS_BACKEND_EXTENSION_POINT_NAME.
68 * The interface defines methods for reading and writing values, a
69 * method for determining if writing of certain values will fail
70 * (lockdown) and a change notification mechanism.
72 * The semantics of the interface are very precisely defined and
73 * implementations must carefully adhere to the expectations of
74 * callers that are documented on each of the interface methods.
76 * Some of the GSettingsBackend functions accept or return a #GTree.
77 * These trees always have strings as keys and #GVariant as values.
78 * g_settings_backend_create_tree() is a convenience function to create
82 * The #GSettingsBackend API is exported to allow third-party
83 * implementations, but does not carry the same stability guarantees
84 * as the public GIO API. For this reason, you have to define the
85 * C preprocessor symbol #G_SETTINGS_ENABLE_BACKEND before including
86 * <filename>gio/gsettingsbackend.h</filename>
91 is_key (const gchar *key)
96 g_return_val_if_fail (key != NULL, FALSE);
97 g_return_val_if_fail (key[0] == '/', FALSE);
99 for (i = 1; key[i]; i++)
100 g_return_val_if_fail (key[i] != '/' || key[i + 1] != '/', FALSE);
104 g_return_val_if_fail (key[length - 1] != '/', FALSE);
110 is_path (const gchar *path)
115 g_return_val_if_fail (path != NULL, FALSE);
116 g_return_val_if_fail (path[0] == '/', FALSE);
118 for (i = 1; path[i]; i++)
119 g_return_val_if_fail (path[i] != '/' || path[i + 1] != '/', FALSE);
123 g_return_val_if_fail (path[length - 1] == '/', FALSE);
128 struct _GSettingsBackendWatch
131 const GSettingsListenerVTable *vtable;
132 GMainContext *context;
133 GSettingsBackendWatch *next;
136 struct _GSettingsBackendClosure
138 void (*function) (GObject *target,
139 GSettingsBackend *backend,
144 GSettingsBackend *backend;
148 GBoxedFreeFunc data1_free;
153 g_settings_backend_watch_weak_notify (gpointer data,
154 GObject *where_the_object_was)
156 GSettingsBackend *backend = data;
157 GSettingsBackendWatch **ptr;
159 /* search and remove */
160 g_static_mutex_lock (&backend->priv->lock);
161 for (ptr = &backend->priv->watches; *ptr; ptr = &(*ptr)->next)
162 if ((*ptr)->target == where_the_object_was)
164 GSettingsBackendWatch *tmp = *ptr;
167 g_slice_free (GSettingsBackendWatch, tmp);
169 g_static_mutex_unlock (&backend->priv->lock);
173 /* we didn't find it. that shouldn't happen. */
174 g_assert_not_reached ();
178 * g_settings_backend_watch:
179 * @backend: a #GSettingsBackend
180 * @target: the GObject (typically GSettings instance) to call back to
181 * @context: a #GMainContext, or %NULL
184 * Registers a new watch on a #GSettingsBackend.
186 * note: %NULL @context does not mean "default main context" but rather,
187 * "it is okay to dispatch in any context". If the default main context
188 * is specifically desired then it must be given.
190 * note also: if you want to get meaningful values for the @origin_tag
191 * that appears as an argument to some of the callbacks, you *must* have
192 * @context as %NULL. Otherwise, you are subject to cross-thread
193 * dispatching and whatever owned @origin_tag at the time that the event
194 * occured may no longer own it. This is a problem if you consider that
195 * you may now be the new owner of that address and mistakenly think
196 * that the event in question originated from yourself.
198 * tl;dr: If you give a non-%NULL @context then you must ignore the
199 * value of @origin_tag given to any callbacks.
202 g_settings_backend_watch (GSettingsBackend *backend,
203 const GSettingsListenerVTable *vtable,
205 GMainContext *context)
207 GSettingsBackendWatch *watch;
209 /* For purposes of discussion, we assume that our target is a
210 * GSettings instance.
212 * Our strategy to defend against the final reference dropping on the
213 * GSettings object in a thread other than the one that is doing the
214 * dispatching is as follows:
216 * 1) hold a GObject reference on the GSettings during an outstanding
217 * dispatch. This ensures that the delivery is always possible.
219 * 2) hold a weak reference on the GSettings at other times. This
220 * allows us to receive early notification of pending destruction
221 * of the object. At this point, it is still safe to obtain a
222 * reference on the GObject to keep it alive, so #1 will work up
223 * to that point. After that point, we'll have been able to drop
224 * the watch from the list.
226 * Note, in particular, that it's not possible to simply have an
227 * "unwatch" function that gets called from the finalize function of
228 * the GSettings instance because, by that point it is no longer
229 * possible to keep the object alive using g_object_ref() and we would
230 * have no way of knowing this.
232 * Note also that we do not need to hold a reference on the main
233 * context here since the GSettings instance does that for us and we
234 * will receive the weak notify long before it is dropped. We don't
235 * even need to hold it during dispatches because our reference on the
236 * GSettings will prevent the finalize from running and dropping the
237 * ref on the context.
239 * All access to the list holds a mutex. We have some strategies to
240 * avoid some of the pain that would be associated with that.
243 watch = g_slice_new (GSettingsBackendWatch);
244 watch->context = context;
245 watch->vtable = vtable;
246 watch->target = target;
247 g_object_weak_ref (target, g_settings_backend_watch_weak_notify, backend);
249 /* linked list prepend */
250 g_static_mutex_lock (&backend->priv->lock);
251 watch->next = backend->priv->watches;
252 backend->priv->watches = watch;
253 g_static_mutex_unlock (&backend->priv->lock);
257 g_settings_backend_unwatch (GSettingsBackend *backend,
260 /* Our caller surely owns a reference on 'target', so the order of
261 * these two calls is unimportant.
263 g_object_weak_unref (target, g_settings_backend_watch_weak_notify, backend);
264 g_settings_backend_watch_weak_notify (backend, target);
268 g_settings_backend_invoke_closure (gpointer user_data)
270 GSettingsBackendClosure *closure = user_data;
272 closure->function (closure->target, closure->backend, closure->name,
273 closure->data1, closure->data2);
275 closure->data1_free (closure->data1);
276 g_object_unref (closure->backend);
277 g_object_unref (closure->target);
278 g_free (closure->name);
280 g_slice_free (GSettingsBackendClosure, closure);
286 pointer_id (gpointer a)
292 pointer_ignore (gpointer a)
297 g_settings_backend_dispatch_signal (GSettingsBackend *backend,
298 gsize function_offset,
301 GBoxedCopyFunc data1_copy,
302 GBoxedFreeFunc data1_free,
305 GSettingsBackendWatch *suffix, *watch, *next;
307 if (data1_copy == NULL)
308 data1_copy = pointer_id;
310 if (data1_free == NULL)
311 data1_free = pointer_ignore;
313 /* We're in a little bit of a tricky situation here. We need to hold
314 * a lock while traversing the list, but we don't want to hold the
315 * lock while calling back into user code.
317 * Since we're not holding the lock while we call user code, we can't
318 * render the list immutable. We can, however, store a pointer to a
319 * given suffix of the list and render that suffix immutable.
321 * Adds will never modify the suffix since adds always come in the
322 * form of prepends. We can also prevent removes from modifying the
323 * suffix since removes only happen in response to the last reference
324 * count dropping -- so just add a reference to everything in the
327 g_static_mutex_lock (&backend->priv->lock);
328 suffix = backend->priv->watches;
329 for (watch = suffix; watch; watch = watch->next)
330 g_object_ref (watch->target);
331 g_static_mutex_unlock (&backend->priv->lock);
333 /* The suffix is now immutable, so this is safe. */
334 for (watch = suffix; watch; watch = next)
336 GSettingsBackendClosure *closure;
338 closure = g_slice_new (GSettingsBackendClosure);
339 closure->backend = g_object_ref (backend);
340 closure->target = watch->target; /* we took our ref above */
341 closure->function = G_STRUCT_MEMBER (void *, watch->vtable,
343 closure->name = g_strdup (name);
344 closure->data1 = data1_copy (data1);
345 closure->data1_free = data1_free;
346 closure->data2 = data2;
348 /* we do this here because 'watch' may not live to the end of this
349 * iteration of the loop (since we may unref the target below).
354 g_main_context_invoke (watch->context,
355 g_settings_backend_invoke_closure,
358 g_settings_backend_invoke_closure (closure);
363 * g_settings_backend_changed:
364 * @backend: a #GSettingsBackend implementation
365 * @key: the name of the key
366 * @origin_tag: the origin tag
368 * Signals that a single key has possibly changed. Backend
369 * implementations should call this if a key has possibly changed its
372 * @key must be a valid key (ie starting with a slash, not containing
373 * '//', and not ending with a slash).
375 * The implementation must call this function during any call to
376 * g_settings_backend_write(), before the call returns (except in the
377 * case that no keys are actually changed and it cares to detect this
378 * fact). It may not rely on the existence of a mainloop for
379 * dispatching the signal later.
381 * The implementation may call this function at any other time it likes
382 * in response to other events (such as changes occuring outside of the
383 * program). These calls may originate from a mainloop or may originate
384 * in response to any other action (including from calls to
385 * g_settings_backend_write()).
387 * In the case that this call is in response to a call to
388 * g_settings_backend_write() then @origin_tag must be set to the same
389 * value that was passed to that call.
394 g_settings_backend_changed (GSettingsBackend *backend,
398 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
399 g_return_if_fail (is_key (key));
401 g_settings_backend_dispatch_signal (backend,
402 G_STRUCT_OFFSET (GSettingsListenerVTable,
404 key, origin_tag, NULL, NULL, NULL);
408 * g_settings_backend_keys_changed:
409 * @backend: a #GSettingsBackend implementation
410 * @path: the path containing the changes
411 * @items: (array zero-terminated=1): the %NULL-terminated list of changed keys
412 * @origin_tag: the origin tag
414 * Signals that a list of keys have possibly changed. Backend
415 * implementations should call this if keys have possibly changed their
418 * @path must be a valid path (ie starting and ending with a slash and
419 * not containing '//'). Each string in @items must form a valid key
420 * name when @path is prefixed to it (ie: each item must not start or
421 * end with '/' and must not contain '//').
423 * The meaning of this signal is that any of the key names resulting
424 * from the contatenation of @path with each item in @items may have
427 * The same rules for when notifications must occur apply as per
428 * g_settings_backend_changed(). These two calls can be used
429 * interchangeably if exactly one item has changed (although in that
430 * case g_settings_backend_changed() is definitely preferred).
432 * For efficiency reasons, the implementation should strive for @path to
433 * be as long as possible (ie: the longest common prefix of all of the
434 * keys that were changed) but this is not strictly required.
439 g_settings_backend_keys_changed (GSettingsBackend *backend,
441 gchar const * const *items,
444 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
445 g_return_if_fail (is_path (path));
447 /* XXX: should do stricter checking (ie: inspect each item) */
448 g_return_if_fail (items != NULL);
450 g_settings_backend_dispatch_signal (backend,
451 G_STRUCT_OFFSET (GSettingsListenerVTable,
453 path, (gpointer) items,
454 (GBoxedCopyFunc) g_strdupv,
455 (GBoxedFreeFunc) g_strfreev,
460 * g_settings_backend_path_changed:
461 * @backend: a #GSettingsBackend implementation
462 * @path: the path containing the changes
463 * @origin_tag: the origin tag
465 * Signals that all keys below a given path may have possibly changed.
466 * Backend implementations should call this if an entire path of keys
467 * have possibly changed their values.
469 * @path must be a valid path (ie starting and ending with a slash and
470 * not containing '//').
472 * The meaning of this signal is that any of the key which has a name
473 * starting with @path may have changed.
475 * The same rules for when notifications must occur apply as per
476 * g_settings_backend_changed(). This call might be an appropriate
477 * reasponse to a 'reset' call but implementations are also free to
478 * explicitly list the keys that were affected by that call if they can
481 * For efficiency reasons, the implementation should strive for @path to
482 * be as long as possible (ie: the longest common prefix of all of the
483 * keys that were changed) but this is not strictly required. As an
484 * example, if this function is called with the path of "/" then every
485 * single key in the application will be notified of a possible change.
490 g_settings_backend_path_changed (GSettingsBackend *backend,
494 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
495 g_return_if_fail (is_path (path));
497 g_settings_backend_dispatch_signal (backend,
498 G_STRUCT_OFFSET (GSettingsListenerVTable,
500 path, origin_tag, NULL, NULL, NULL);
504 * g_settings_backend_writable_changed:
505 * @backend: a #GSettingsBackend implementation
506 * @key: the name of the key
508 * Signals that the writability of a single key has possibly changed.
510 * Since GSettings performs no locking operations for itself, this call
511 * will always be made in response to external events.
516 g_settings_backend_writable_changed (GSettingsBackend *backend,
519 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
520 g_return_if_fail (is_key (key));
522 g_settings_backend_dispatch_signal (backend,
523 G_STRUCT_OFFSET (GSettingsListenerVTable,
525 key, NULL, NULL, NULL, NULL);
529 * g_settings_backend_path_writable_changed:
530 * @backend: a #GSettingsBackend implementation
531 * @path: the name of the path
533 * Signals that the writability of all keys below a given path may have
536 * Since GSettings performs no locking operations for itself, this call
537 * will always be made in response to external events.
542 g_settings_backend_path_writable_changed (GSettingsBackend *backend,
545 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
546 g_return_if_fail (is_path (path));
548 g_settings_backend_dispatch_signal (backend,
549 G_STRUCT_OFFSET (GSettingsListenerVTable,
550 path_writable_changed),
551 path, NULL, NULL, NULL, NULL);
563 g_settings_backend_flatten_one (gpointer key,
567 FlattenState *state = user_data;
568 const gchar *skey = key;
571 g_return_val_if_fail (is_key (key), TRUE);
573 /* calculate longest common prefix */
574 if (state->prefix == NULL)
578 /* first key? just take the prefix up to the last '/' */
579 state->prefix = g_strdup (skey);
580 last_byte = strrchr (state->prefix, '/') + 1;
581 state->prefix_len = last_byte - state->prefix;
586 /* find the first character that does not match. we will
587 * definitely find one because the prefix ends in '/' and the key
588 * does not. also: no two keys in the tree are the same.
590 for (i = 0; state->prefix[i] == skey[i]; i++);
592 /* check if we need to shorten the prefix */
593 if (state->prefix[i] != '\0')
595 /* find the nearest '/', terminate after it */
596 while (state->prefix[i - 1] != '/')
599 state->prefix[i] = '\0';
600 state->prefix_len = i;
605 /* save the entire item into the array.
606 * the prefixes will be removed later.
608 *state->keys++ = key;
611 *state->values++ = value;
617 * g_settings_backend_flatten_tree:
618 * @tree: a #GTree containing the changes
619 * @path: (out): the location to save the path
620 * @keys: (out) (transfer container) (array zero-terminated=1): the
621 * location to save the relative keys
622 * @values: (out) (allow-none) (transfer container) (array zero-terminated=1):
623 * the location to save the values, or %NULL
625 * Calculate the longest common prefix of all keys in a tree and write
626 * out an array of the key names relative to that prefix and,
627 * optionally, the value to store at each of those keys.
629 * You must free the value returned in @path, @keys and @values using
630 * g_free(). You should not attempt to free or unref the contents of
636 g_settings_backend_flatten_tree (GTree *tree,
641 FlattenState state = { 0, };
644 nnodes = g_tree_nnodes (tree);
646 *keys = state.keys = g_new (const gchar *, nnodes + 1);
647 state.keys[nnodes] = NULL;
651 *values = state.values = g_new (GVariant *, nnodes + 1);
652 state.values[nnodes] = NULL;
655 g_tree_foreach (tree, g_settings_backend_flatten_one, &state);
656 g_return_if_fail (*keys + nnodes == state.keys);
658 *path = state.prefix;
660 *--state.keys += state.prefix_len;
664 * g_settings_backend_changed_tree:
665 * @backend: a #GSettingsBackend implementation
666 * @tree: a #GTree containing the changes
667 * @origin_tag: the origin tag
669 * This call is a convenience wrapper. It gets the list of changes from
670 * @tree, computes the longest common prefix and calls
671 * g_settings_backend_changed().
676 g_settings_backend_changed_tree (GSettingsBackend *backend,
680 GSettingsBackendWatch *watch;
684 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
686 g_settings_backend_flatten_tree (tree, &path, &keys, NULL);
693 g_print ("changed_tree(): prefix %s\n", path);
694 for (i = 0; keys[i]; i++)
695 g_print (" %s\n", keys[i]);
700 for (watch = backend->priv->watches; watch; watch = watch->next)
701 watch->vtable->keys_changed (watch->target, backend,
702 path, keys, origin_tag);
709 * g_settings_backend_read:
710 * @backend: a #GSettingsBackend implementation
711 * @key: the key to read
712 * @expected_type: a #GVariantType
713 * @default_value: if the default value should be returned
714 * @returns: the value that was read, or %NULL
716 * Reads a key. This call will never block.
718 * If the key exists, the value associated with it will be returned.
719 * If the key does not exist, %NULL will be returned.
721 * The returned value will be of the type given in @expected_type. If
722 * the backend stored a value of a different type then %NULL will be
725 * If @default_value is %TRUE then this gets the default value from the
726 * backend (ie: the one that the backend would contain if
727 * g_settings_reset() were called).
730 g_settings_backend_read (GSettingsBackend *backend,
732 const GVariantType *expected_type,
733 gboolean default_value)
737 value = G_SETTINGS_BACKEND_GET_CLASS (backend)
738 ->read (backend, key, expected_type, default_value);
740 if G_UNLIKELY (value && !g_variant_is_of_type (value, expected_type))
742 g_variant_unref (value);
750 * g_settings_backend_write:
751 * @backend: a #GSettingsBackend implementation
752 * @key: the name of the key
753 * @value: a #GVariant value to write to this key
754 * @origin_tag: the origin tag
755 * @returns: %TRUE if the write succeeded, %FALSE if the key was not writable
757 * Writes exactly one key.
759 * This call does not fail. During this call a
760 * #GSettingsBackend::changed signal will be emitted if the value of the
761 * key has changed. The updated key value will be visible to any signal
764 * One possible method that an implementation might deal with failures is
765 * to emit a second "changed" signal (either during this call, or later)
766 * to indicate that the affected keys have suddenly "changed back" to their
770 g_settings_backend_write (GSettingsBackend *backend,
775 return G_SETTINGS_BACKEND_GET_CLASS (backend)
776 ->write (backend, key, value, origin_tag);
780 * g_settings_backend_write_keys:
781 * @backend: a #GSettingsBackend implementation
782 * @values: a #GTree containing key-value pairs to write
783 * @origin_tag: the origin tag
785 * Writes one or more keys. This call will never block.
787 * The key of each item in the tree is the key name to write to and the
788 * value is a #GVariant to write. The proper type of #GTree for this
789 * call can be created with g_settings_backend_create_tree(). This call
790 * might take a reference to the tree; you must not modified the #GTree
791 * after passing it to this call.
793 * This call does not fail. During this call a #GSettingsBackend::changed
794 * signal will be emitted if any keys have been changed. The new values of
795 * all updated keys will be visible to any signal callbacks.
797 * One possible method that an implementation might deal with failures is
798 * to emit a second "changed" signal (either during this call, or later)
799 * to indicate that the affected keys have suddenly "changed back" to their
803 g_settings_backend_write_tree (GSettingsBackend *backend,
807 return G_SETTINGS_BACKEND_GET_CLASS (backend)
808 ->write_tree (backend, tree, origin_tag);
812 * g_settings_backend_reset:
813 * @backend: a #GSettingsBackend implementation
814 * @key: the name of a key
815 * @origin_tag: the origin tag
817 * "Resets" the named key to its "default" value (ie: after system-wide
818 * defaults, mandatory keys, etc. have been taken into account) or possibly
822 g_settings_backend_reset (GSettingsBackend *backend,
826 G_SETTINGS_BACKEND_GET_CLASS (backend)
827 ->reset (backend, key, origin_tag);
831 * g_settings_backend_get_writable:
832 * @backend: a #GSettingsBackend implementation
833 * @key: the name of a key
834 * @returns: %TRUE if the key is writable
836 * Finds out if a key is available for writing to. This is the
837 * interface through which 'lockdown' is implemented. Locked down
838 * keys will have %FALSE returned by this call.
840 * You should not write to locked-down keys, but if you do, the
841 * implementation will deal with it.
844 g_settings_backend_get_writable (GSettingsBackend *backend,
847 return G_SETTINGS_BACKEND_GET_CLASS (backend)
848 ->get_writable (backend, key);
852 * g_settings_backend_unsubscribe:
853 * @backend: a #GSettingsBackend
854 * @name: a key or path to subscribe to
856 * Reverses the effect of a previous call to
857 * g_settings_backend_subscribe().
860 g_settings_backend_unsubscribe (GSettingsBackend *backend,
863 G_SETTINGS_BACKEND_GET_CLASS (backend)
864 ->unsubscribe (backend, name);
868 * g_settings_backend_subscribe:
869 * @backend: a #GSettingsBackend
870 * @name: a key or path to subscribe to
872 * Requests that change signals be emitted for events on @name.
875 g_settings_backend_subscribe (GSettingsBackend *backend,
878 G_SETTINGS_BACKEND_GET_CLASS (backend)
879 ->subscribe (backend, name);
883 g_settings_backend_finalize (GObject *object)
885 GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
887 g_static_mutex_unlock (&backend->priv->lock);
889 G_OBJECT_CLASS (g_settings_backend_parent_class)
894 ignore_subscription (GSettingsBackend *backend,
900 g_settings_backend_init (GSettingsBackend *backend)
902 backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend,
903 G_TYPE_SETTINGS_BACKEND,
904 GSettingsBackendPrivate);
905 g_static_mutex_init (&backend->priv->lock);
909 g_settings_backend_class_init (GSettingsBackendClass *class)
911 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
913 class->subscribe = ignore_subscription;
914 class->unsubscribe = ignore_subscription;
916 gobject_class->finalize = g_settings_backend_finalize;
918 g_type_class_add_private (class, sizeof (GSettingsBackendPrivate));
922 g_settings_backend_variant_unref0 (gpointer data)
925 g_variant_unref (data);
929 * g_settings_backend_create_tree:
930 * @returns: a new #GTree
932 * This is a convenience function for creating a tree that is compatible
933 * with g_settings_backend_write(). It merely calls g_tree_new_full()
934 * with strcmp(), g_free() and g_variant_unref().
937 g_settings_backend_create_tree (void)
939 return g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
940 g_free, g_settings_backend_variant_unref0);
944 * g_settings_backend_get_default:
945 * @returns: (transfer full): the default #GSettingsBackend
947 * Returns the default #GSettingsBackend. It is possible to override
948 * the default by setting the <envar>GSETTINGS_BACKEND</envar>
949 * environment variable to the name of a settings backend.
951 * The user gets a reference to the backend.
956 g_settings_backend_get_default (void)
958 static gsize backend;
960 if (g_once_init_enter (&backend))
962 GSettingsBackend *instance;
963 GIOExtensionPoint *point;
964 GIOExtension *extension;
965 GType extension_type;
969 _g_io_modules_ensure_loaded ();
971 point = g_io_extension_point_lookup (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME);
974 if ((env = getenv ("GSETTINGS_BACKEND")))
976 extension = g_io_extension_point_get_extension_by_name (point, env);
978 if (extension == NULL)
979 g_warning ("Can't find GSettings backend '%s' given in "
980 "GSETTINGS_BACKEND environment variable", env);
983 if (extension == NULL)
985 extensions = g_io_extension_point_get_extensions (point);
987 if (extensions == NULL)
988 g_error ("No GSettingsBackend implementations exist.");
990 extension = extensions->data;
992 if (strcmp (g_io_extension_get_name (extension), "memory") == 0)
993 g_message ("Using the 'memory' GSettings backend. Your settings "
994 "will not be saved or shared with other applications.");
997 extension_type = g_io_extension_get_type (extension);
998 instance = g_object_new (extension_type, NULL);
999 g_settings_has_backend = TRUE;
1001 g_once_init_leave (&backend, (gsize) instance);
1004 return g_object_ref ((void *) backend);
1008 * g_settings_backend_get_permission:
1009 * @backend: a #GSettingsBackend
1011 * @returns: a non-%NULL #GPermission. Free with g_object_unref()
1013 * Gets the permission object associated with writing to keys below
1014 * @path on @backend.
1016 * If this is not implemented in the backend, then a %TRUE
1017 * #GSimplePermission is returned.
1020 g_settings_backend_get_permission (GSettingsBackend *backend,
1023 GSettingsBackendClass *class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1025 if (class->get_permission)
1026 return class->get_permission (backend, path);
1028 return g_simple_permission_new (TRUE);
1032 * g_settings_backend_sync_default:
1034 * Syncs the default backend.
1037 g_settings_backend_sync_default (void)
1039 if (g_settings_has_backend)
1041 GSettingsBackendClass *class;
1042 GSettingsBackend *backend;
1044 backend = g_settings_backend_get_default ();
1045 class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1048 class->sync (backend);