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;
49 * SECTION:gsettingsbackend
50 * @title: GSettingsBackend
51 * @short_description: Interface for settings backend implementations
52 * @include: gio/gsettingsbackend.h
53 * @see_also: #GSettings, #GIOExtensionPoint
55 * The #GSettingsBackend interface defines a generic interface for
56 * non-strictly-typed data that is stored in a hierarchy. To implement
57 * an alternative storage backend for #GSettings, you need to implement
58 * the #GSettingsBackend interface and then make it implement the
59 * extension point #G_SETTINGS_BACKEND_EXTENSION_POINT_NAME.
61 * The interface defines methods for reading and writing values, a
62 * method for determining if writing of certain values will fail
63 * (lockdown) and a change notification mechanism.
65 * The semantics of the interface are very precisely defined and
66 * implementations must carefully adhere to the expectations of
67 * callers that are documented on each of the interface methods.
69 * Some of the GSettingsBackend functions accept or return a #GTree.
70 * These trees always have strings as keys and #GVariant as values.
71 * g_settings_backend_create_tree() is a convenience function to create
75 * The #GSettingsBackend API is exported to allow third-party
76 * implementations, but does not carry the same stability guarantees
77 * as the public GIO API. For this reason, you have to define the
78 * C preprocessor symbol #G_SETTINGS_ENABLE_BACKEND before including
79 * <filename>gio/gsettingsbackend.h</filename>
84 is_key (const gchar *key)
89 g_return_val_if_fail (key != NULL, FALSE);
90 g_return_val_if_fail (key[0] == '/', FALSE);
92 for (i = 1; key[i]; i++)
93 g_return_val_if_fail (key[i] != '/' || key[i + 1] != '/', FALSE);
97 g_return_val_if_fail (key[length - 1] != '/', FALSE);
103 is_path (const gchar *path)
108 g_return_val_if_fail (path != NULL, FALSE);
109 g_return_val_if_fail (path[0] == '/', FALSE);
111 for (i = 1; path[i]; i++)
112 g_return_val_if_fail (path[i] != '/' || path[i + 1] != '/', FALSE);
116 g_return_val_if_fail (path[length - 1] == '/', FALSE);
121 struct _GSettingsBackendWatch
124 const GSettingsListenerVTable *vtable;
125 GMainContext *context;
126 GSettingsBackendWatch *next;
129 struct _GSettingsBackendClosure
131 void (*function) (GObject *target,
132 GSettingsBackend *backend,
137 GSettingsBackend *backend;
141 GBoxedFreeFunc data1_free;
146 g_settings_backend_watch_weak_notify (gpointer data,
147 GObject *where_the_object_was)
149 GSettingsBackend *backend = data;
150 GSettingsBackendWatch **ptr;
152 /* search and remove */
153 g_static_mutex_lock (&backend->priv->lock);
154 for (ptr = &backend->priv->watches; *ptr; ptr = &(*ptr)->next)
155 if ((*ptr)->target == where_the_object_was)
157 GSettingsBackendWatch *tmp = *ptr;
160 g_slice_free (GSettingsBackendWatch, tmp);
162 g_static_mutex_unlock (&backend->priv->lock);
166 /* we didn't find it. that shouldn't happen. */
167 g_assert_not_reached ();
171 * g_settings_backend_watch:
172 * @backend: a #GSettingsBackend
173 * @target: the GObject (typically GSettings instance) to call back to
174 * @context: a #GMainContext, or %NULL
177 * Registers a new watch on a #GSettingsBackend.
179 * note: %NULL @context does not mean "default main context" but rather,
180 * "it is okay to dispatch in any context". If the default main context
181 * is specifically desired then it must be given.
183 * note also: if you want to get meaningful values for the @origin_tag
184 * that appears as an argument to some of the callbacks, you *must* have
185 * @context as %NULL. Otherwise, you are subject to cross-thread
186 * dispatching and whatever owned @origin_tag at the time that the event
187 * occured may no longer own it. This is a problem if you consider that
188 * you may now be the new owner of that address and mistakenly think
189 * that the event in question originated from yourself.
191 * tl;dr: If you give a non-%NULL @context then you must ignore the
192 * value of @origin_tag given to any callbacks.
195 g_settings_backend_watch (GSettingsBackend *backend,
196 const GSettingsListenerVTable *vtable,
198 GMainContext *context)
200 GSettingsBackendWatch *watch;
202 /* For purposes of discussion, we assume that our target is a
203 * GSettings instance.
205 * Our strategy to defend against the final reference dropping on the
206 * GSettings object in a thread other than the one that is doing the
207 * dispatching is as follows:
209 * 1) hold a GObject reference on the GSettings during an outstanding
210 * dispatch. This ensures that the delivery is always possible.
212 * 2) hold a weak reference on the GSettings at other times. This
213 * allows us to receive early notification of pending destruction
214 * of the object. At this point, it is still safe to obtain a
215 * reference on the GObject to keep it alive, so #1 will work up
216 * to that point. After that point, we'll have been able to drop
217 * the watch from the list.
219 * Note, in particular, that it's not possible to simply have an
220 * "unwatch" function that gets called from the finalize function of
221 * the GSettings instance because, by that point it is no longer
222 * possible to keep the object alive using g_object_ref() and we would
223 * have no way of knowing this.
225 * Note also that we do not need to hold a reference on the main
226 * context here since the GSettings instance does that for us and we
227 * will receive the weak notify long before it is dropped. We don't
228 * even need to hold it during dispatches because our reference on the
229 * GSettings will prevent the finalize from running and dropping the
230 * ref on the context.
232 * All access to the list holds a mutex. We have some strategies to
233 * avoid some of the pain that would be associated with that.
236 watch = g_slice_new (GSettingsBackendWatch);
237 watch->context = context;
238 watch->vtable = vtable;
239 watch->target = target;
240 g_object_weak_ref (target, g_settings_backend_watch_weak_notify, backend);
242 /* linked list prepend */
243 g_static_mutex_lock (&backend->priv->lock);
244 watch->next = backend->priv->watches;
245 backend->priv->watches = watch;
246 g_static_mutex_unlock (&backend->priv->lock);
250 g_settings_backend_unwatch (GSettingsBackend *backend,
253 /* Our caller surely owns a reference on 'target', so the order of
254 * these two calls is unimportant.
256 g_object_weak_unref (target, g_settings_backend_watch_weak_notify, backend);
257 g_settings_backend_watch_weak_notify (backend, target);
261 g_settings_backend_invoke_closure (gpointer user_data)
263 GSettingsBackendClosure *closure = user_data;
265 closure->function (closure->target, closure->backend, closure->name,
266 closure->data1, closure->data2);
268 closure->data1_free (closure->data1);
269 g_object_unref (closure->backend);
270 g_object_unref (closure->target);
271 g_free (closure->name);
273 g_slice_free (GSettingsBackendClosure, closure);
279 pointer_id (gpointer a)
285 pointer_ignore (gpointer a)
290 g_settings_backend_dispatch_signal (GSettingsBackend *backend,
291 gsize function_offset,
294 GBoxedCopyFunc data1_copy,
295 GBoxedFreeFunc data1_free,
298 GSettingsBackendWatch *suffix, *watch, *next;
300 if (data1_copy == NULL)
301 data1_copy = pointer_id;
303 if (data1_free == NULL)
304 data1_free = pointer_ignore;
306 /* We're in a little bit of a tricky situation here. We need to hold
307 * a lock while traversing the list, but we don't want to hold the
308 * lock while calling back into user code.
310 * Since we're not holding the lock while we call user code, we can't
311 * render the list immutable. We can, however, store a pointer to a
312 * given suffix of the list and render that suffix immutable.
314 * Adds will never modify the suffix since adds always come in the
315 * form of prepends. We can also prevent removes from modifying the
316 * suffix since removes only happen in response to the last reference
317 * count dropping -- so just add a reference to everything in the
320 g_static_mutex_lock (&backend->priv->lock);
321 suffix = backend->priv->watches;
322 for (watch = suffix; watch; watch = watch->next)
323 g_object_ref (watch->target);
324 g_static_mutex_unlock (&backend->priv->lock);
326 /* The suffix is now immutable, so this is safe. */
327 for (watch = suffix; watch; watch = next)
329 GSettingsBackendClosure *closure;
331 closure = g_slice_new (GSettingsBackendClosure);
332 closure->backend = g_object_ref (backend);
333 closure->target = watch->target; /* we took our ref above */
334 closure->function = G_STRUCT_MEMBER (void *, watch->vtable,
336 closure->name = g_strdup (name);
337 closure->data1 = data1_copy (data1);
338 closure->data1_free = data1_free;
339 closure->data2 = data2;
341 /* we do this here because 'watch' may not live to the end of this
342 * iteration of the loop (since we may unref the target below).
347 g_main_context_invoke (watch->context,
348 g_settings_backend_invoke_closure,
351 g_settings_backend_invoke_closure (closure);
356 * g_settings_backend_changed:
357 * @backend: a #GSettingsBackend implementation
358 * @key: the name of the key
359 * @origin_tag: the origin tag
361 * Signals that a single key has possibly changed. Backend
362 * implementations should call this if a key has possibly changed its
365 * @key must be a valid key (ie starting with a slash, not containing
366 * '//', and not ending with a slash).
368 * The implementation must call this function during any call to
369 * g_settings_backend_write(), before the call returns (except in the
370 * case that no keys are actually changed and it cares to detect this
371 * fact). It may not rely on the existence of a mainloop for
372 * dispatching the signal later.
374 * The implementation may call this function at any other time it likes
375 * in response to other events (such as changes occuring outside of the
376 * program). These calls may originate from a mainloop or may originate
377 * in response to any other action (including from calls to
378 * g_settings_backend_write()).
380 * In the case that this call is in response to a call to
381 * g_settings_backend_write() then @origin_tag must be set to the same
382 * value that was passed to that call.
387 g_settings_backend_changed (GSettingsBackend *backend,
391 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
392 g_return_if_fail (is_key (key));
394 g_settings_backend_dispatch_signal (backend,
395 G_STRUCT_OFFSET (GSettingsListenerVTable,
397 key, origin_tag, NULL, NULL, NULL);
401 * g_settings_backend_keys_changed:
402 * @backend: a #GSettingsBackend implementation
403 * @path: the path containing the changes
404 * @items: the %NULL-terminated list of changed keys
405 * @origin_tag: the origin tag
407 * Signals that a list of keys have possibly changed. Backend
408 * implementations should call this if keys have possibly changed their
411 * @path must be a valid path (ie starting and ending with a slash and
412 * not containing '//'). Each string in @items must form a valid key
413 * name when @path is prefixed to it (ie: each item must not start or
414 * end with '/' and must not contain '//').
416 * The meaning of this signal is that any of the key names resulting
417 * from the contatenation of @path with each item in @items may have
420 * The same rules for when notifications must occur apply as per
421 * g_settings_backend_changed(). These two calls can be used
422 * interchangeably if exactly one item has changed (although in that
423 * case g_settings_backend_changed() is definitely preferred).
425 * For efficiency reasons, the implementation should strive for @path to
426 * be as long as possible (ie: the longest common prefix of all of the
427 * keys that were changed) but this is not strictly required.
432 g_settings_backend_keys_changed (GSettingsBackend *backend,
434 gchar const * const *items,
437 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
438 g_return_if_fail (is_path (path));
440 /* XXX: should do stricter checking (ie: inspect each item) */
441 g_return_if_fail (items != NULL);
443 g_settings_backend_dispatch_signal (backend,
444 G_STRUCT_OFFSET (GSettingsListenerVTable,
446 path, (gpointer) items,
447 (GBoxedCopyFunc) g_strdupv,
448 (GBoxedFreeFunc) g_strfreev,
453 * g_settings_backend_path_changed:
454 * @backend: a #GSettingsBackend implementation
455 * @path: the path containing the changes
456 * @origin_tag: the origin tag
458 * Signals that all keys below a given path may have possibly changed.
459 * Backend implementations should call this if an entire path of keys
460 * have possibly changed their values.
462 * @path must be a valid path (ie starting and ending with a slash and
463 * not containing '//').
465 * The meaning of this signal is that any of the key which has a name
466 * starting with @path may have changed.
468 * The same rules for when notifications must occur apply as per
469 * g_settings_backend_changed(). This call might be an appropriate
470 * reasponse to a 'reset' call but implementations are also free to
471 * explicitly list the keys that were affected by that call if they can
474 * For efficiency reasons, the implementation should strive for @path to
475 * be as long as possible (ie: the longest common prefix of all of the
476 * keys that were changed) but this is not strictly required. As an
477 * example, if this function is called with the path of "/" then every
478 * single key in the application will be notified of a possible change.
483 g_settings_backend_path_changed (GSettingsBackend *backend,
487 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
488 g_return_if_fail (is_path (path));
490 g_settings_backend_dispatch_signal (backend,
491 G_STRUCT_OFFSET (GSettingsListenerVTable,
493 path, origin_tag, NULL, NULL, NULL);
497 * g_settings_backend_writable_changed:
498 * @backend: a #GSettingsBackend implementation
499 * @key: the name of the key
501 * Signals that the writability of a single key has possibly changed.
503 * Since GSettings performs no locking operations for itself, this call
504 * will always be made in response to external events.
509 g_settings_backend_writable_changed (GSettingsBackend *backend,
512 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
513 g_return_if_fail (is_key (key));
515 g_settings_backend_dispatch_signal (backend,
516 G_STRUCT_OFFSET (GSettingsListenerVTable,
518 key, NULL, NULL, NULL, NULL);
522 * g_settings_backend_path_writable_changed:
523 * @backend: a #GSettingsBackend implementation
524 * @path: the name of the path
526 * Signals that the writability of all keys below a given path may have
529 * Since GSettings performs no locking operations for itself, this call
530 * will always be made in response to external events.
535 g_settings_backend_path_writable_changed (GSettingsBackend *backend,
538 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
539 g_return_if_fail (is_path (path));
541 g_settings_backend_dispatch_signal (backend,
542 G_STRUCT_OFFSET (GSettingsListenerVTable,
543 path_writable_changed),
544 path, NULL, NULL, NULL, NULL);
556 g_settings_backend_flatten_one (gpointer key,
560 FlattenState *state = user_data;
561 const gchar *skey = key;
564 g_return_val_if_fail (is_key (key), TRUE);
566 /* calculate longest common prefix */
567 if (state->prefix == NULL)
571 /* first key? just take the prefix up to the last '/' */
572 state->prefix = g_strdup (skey);
573 last_byte = strrchr (state->prefix, '/') + 1;
574 state->prefix_len = last_byte - state->prefix;
579 /* find the first character that does not match. we will
580 * definitely find one because the prefix ends in '/' and the key
581 * does not. also: no two keys in the tree are the same.
583 for (i = 0; state->prefix[i] == skey[i]; i++);
585 /* check if we need to shorten the prefix */
586 if (state->prefix[i] != '\0')
588 /* find the nearest '/', terminate after it */
589 while (state->prefix[i - 1] != '/')
592 state->prefix[i] = '\0';
593 state->prefix_len = i;
598 /* save the entire item into the array.
599 * the prefixes will be removed later.
601 *state->keys++ = key;
604 *state->values++ = value;
610 * g_settings_backend_flatten_tree:
611 * @tree: a #GTree containing the changes
612 * @path: the location to save the path
613 * @keys: the location to save the relative keys
614 * @values: the location to save the values, or %NULL
616 * Calculate the longest common prefix of all keys in a tree and write
617 * out an array of the key names relative to that prefix and,
618 * optionally, the value to store at each of those keys.
620 * You must free the value returned in @path, @keys and @values using
621 * g_free(). You should not attempt to free or unref the contents of
627 g_settings_backend_flatten_tree (GTree *tree,
632 FlattenState state = { 0, };
635 nnodes = g_tree_nnodes (tree);
637 *keys = state.keys = g_new (const gchar *, nnodes + 1);
638 state.keys[nnodes] = NULL;
642 *values = state.values = g_new (GVariant *, nnodes + 1);
643 state.values[nnodes] = NULL;
646 g_tree_foreach (tree, g_settings_backend_flatten_one, &state);
647 g_return_if_fail (*keys + nnodes == state.keys);
649 *path = state.prefix;
651 *--state.keys += state.prefix_len;
655 * g_settings_backend_changed_tree:
656 * @backend: a #GSettingsBackend implementation
657 * @tree: a #GTree containing the changes
658 * @origin_tag: the origin tag
660 * This call is a convenience wrapper. It gets the list of changes from
661 * @tree, computes the longest common prefix and calls
662 * g_settings_backend_changed().
667 g_settings_backend_changed_tree (GSettingsBackend *backend,
671 GSettingsBackendWatch *watch;
675 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
677 g_settings_backend_flatten_tree (tree, &path, &keys, NULL);
684 g_print ("changed_tree(): prefix %s\n", path);
685 for (i = 0; keys[i]; i++)
686 g_print (" %s\n", keys[i]);
691 for (watch = backend->priv->watches; watch; watch = watch->next)
692 watch->vtable->keys_changed (watch->target, backend,
693 path, keys, origin_tag);
700 * g_settings_backend_read:
701 * @backend: a #GSettingsBackend implementation
702 * @key: the key to read
703 * @expected_type: a #GVariantType
704 * @default_value: if the default value should be returned
705 * @returns: the value that was read, or %NULL
707 * Reads a key. This call will never block.
709 * If the key exists, the value associated with it will be returned.
710 * If the key does not exist, %NULL will be returned.
712 * The returned value will be of the type given in @expected_type. If
713 * the backend stored a value of a different type then %NULL will be
716 * If @default_value is %TRUE then this gets the default value from the
717 * backend (ie: the one that the backend would contain if
718 * g_settings_reset() were called).
721 g_settings_backend_read (GSettingsBackend *backend,
723 const GVariantType *expected_type,
724 gboolean default_value)
728 value = G_SETTINGS_BACKEND_GET_CLASS (backend)
729 ->read (backend, key, expected_type, default_value);
731 if G_UNLIKELY (value && !g_variant_is_of_type (value, expected_type))
733 g_variant_unref (value);
741 * g_settings_backend_write:
742 * @backend: a #GSettingsBackend implementation
743 * @key: the name of the key
744 * @value: a #GVariant value to write to this key
745 * @origin_tag: the origin tag
746 * @returns: %TRUE if the write succeeded, %FALSE if the key was not writable
748 * Writes exactly one key.
750 * This call does not fail. During this call a
751 * #GSettingsBackend::changed signal will be emitted if the value of the
752 * key has changed. The updated key value will be visible to any signal
755 * One possible method that an implementation might deal with failures is
756 * to emit a second "changed" signal (either during this call, or later)
757 * to indicate that the affected keys have suddenly "changed back" to their
761 g_settings_backend_write (GSettingsBackend *backend,
766 return G_SETTINGS_BACKEND_GET_CLASS (backend)
767 ->write (backend, key, value, origin_tag);
771 * g_settings_backend_write_keys:
772 * @backend: a #GSettingsBackend implementation
773 * @values: a #GTree containing key-value pairs to write
774 * @origin_tag: the origin tag
776 * Writes one or more keys. This call will never block.
778 * The key of each item in the tree is the key name to write to and the
779 * value is a #GVariant to write. The proper type of #GTree for this
780 * call can be created with g_settings_backend_create_tree(). This call
781 * might take a reference to the tree; you must not modified the #GTree
782 * after passing it to this call.
784 * This call does not fail. During this call a #GSettingsBackend::changed
785 * signal will be emitted if any keys have been changed. The new values of
786 * all updated keys will be visible to any signal callbacks.
788 * One possible method that an implementation might deal with failures is
789 * to emit a second "changed" signal (either during this call, or later)
790 * to indicate that the affected keys have suddenly "changed back" to their
794 g_settings_backend_write_tree (GSettingsBackend *backend,
798 return G_SETTINGS_BACKEND_GET_CLASS (backend)
799 ->write_tree (backend, tree, origin_tag);
803 * g_settings_backend_reset:
804 * @backend: a #GSettingsBackend implementation
805 * @key: the name of a key
806 * @origin_tag: the origin tag
808 * "Resets" the named key to its "default" value (ie: after system-wide
809 * defaults, mandatory keys, etc. have been taken into account) or possibly
813 g_settings_backend_reset (GSettingsBackend *backend,
817 G_SETTINGS_BACKEND_GET_CLASS (backend)
818 ->reset (backend, key, origin_tag);
822 * g_settings_backend_get_writable:
823 * @backend: a #GSettingsBackend implementation
824 * @key: the name of a key
825 * @returns: %TRUE if the key is writable
827 * Finds out if a key is available for writing to. This is the
828 * interface through which 'lockdown' is implemented. Locked down
829 * keys will have %FALSE returned by this call.
831 * You should not write to locked-down keys, but if you do, the
832 * implementation will deal with it.
835 g_settings_backend_get_writable (GSettingsBackend *backend,
838 return G_SETTINGS_BACKEND_GET_CLASS (backend)
839 ->get_writable (backend, key);
843 * g_settings_backend_unsubscribe:
844 * @backend: a #GSettingsBackend
845 * @name: a key or path to subscribe to
847 * Reverses the effect of a previous call to
848 * g_settings_backend_subscribe().
851 g_settings_backend_unsubscribe (GSettingsBackend *backend,
854 G_SETTINGS_BACKEND_GET_CLASS (backend)
855 ->unsubscribe (backend, name);
859 * g_settings_backend_subscribe:
860 * @backend: a #GSettingsBackend
861 * @name: a key or path to subscribe to
863 * Requests that change signals be emitted for events on @name.
866 g_settings_backend_subscribe (GSettingsBackend *backend,
869 G_SETTINGS_BACKEND_GET_CLASS (backend)
870 ->subscribe (backend, name);
874 g_settings_backend_finalize (GObject *object)
876 GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
878 g_static_mutex_unlock (&backend->priv->lock);
880 G_OBJECT_CLASS (g_settings_backend_parent_class)
885 ignore_subscription (GSettingsBackend *backend,
891 g_settings_backend_init (GSettingsBackend *backend)
893 backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend,
894 G_TYPE_SETTINGS_BACKEND,
895 GSettingsBackendPrivate);
896 g_static_mutex_init (&backend->priv->lock);
900 g_settings_backend_class_init (GSettingsBackendClass *class)
902 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
904 class->subscribe = ignore_subscription;
905 class->unsubscribe = ignore_subscription;
907 gobject_class->finalize = g_settings_backend_finalize;
909 g_type_class_add_private (class, sizeof (GSettingsBackendPrivate));
913 * g_settings_backend_create_tree:
914 * @returns: a new #GTree
916 * This is a convenience function for creating a tree that is compatible
917 * with g_settings_backend_write(). It merely calls g_tree_new_full()
918 * with strcmp(), g_free() and g_variant_unref().
921 g_settings_backend_create_tree (void)
923 return g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
924 g_free, (GDestroyNotify) g_variant_unref);
928 * g_settings_backend_get_default:
929 * @returns: the default #GSettingsBackend
931 * Returns the default #GSettingsBackend. It is possible to override
932 * the default by setting the <envar>GSETTINGS_BACKEND</envar>
933 * environment variable to the name of a settings backend.
935 * The user gets a reference to the backend.
940 g_settings_backend_get_default (void)
942 static gsize backend;
944 if (g_once_init_enter (&backend))
946 GSettingsBackend *instance;
947 GIOExtensionPoint *point;
948 GIOExtension *extension;
949 GType extension_type;
953 _g_io_modules_ensure_loaded ();
955 point = g_io_extension_point_lookup (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME);
958 if ((env = getenv ("GSETTINGS_BACKEND")))
960 extension = g_io_extension_point_get_extension_by_name (point, env);
962 if (extension == NULL)
963 g_warning ("Can't find GSettings backend '%s' given in "
964 "GSETTINGS_BACKEND environment variable", env);
967 if (extension == NULL)
969 extensions = g_io_extension_point_get_extensions (point);
971 if (extensions == NULL)
972 g_error ("No GSettingsBackend implementations exist.");
974 extension = extensions->data;
976 if (strcmp (g_io_extension_get_name (extension), "memory") == 0)
977 g_message ("Using the 'memory' GSettings backend. Your settings "
978 "will not be saved or shared with other applications.");
981 extension_type = g_io_extension_get_type (extension);
982 instance = g_object_new (extension_type, NULL);
984 g_once_init_leave (&backend, (gsize) instance);
987 return g_object_ref ((void *) backend);
991 * g_settings_backend_get_permission:
992 * @backend: a #GSettingsBackend
994 * @returns: a non-%NULL #GPermission. Free with g_object_unref()
996 * Gets the permission object associated with writing to keys below
999 * If this is not implemented in the backend, then a %TRUE
1000 * #GSimplePermission is returned.
1003 g_settings_backend_get_permission (GSettingsBackend *backend,
1006 GSettingsBackendClass *class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1008 if (class->get_permission)
1009 return class->get_permission (backend, path);
1011 return g_simple_permission_new (TRUE);
1015 * g_settings_backend_sync_default:
1017 * Syncs the default backend.
1020 g_settings_backend_sync_default (void)
1022 GSettingsBackendClass *class;
1023 GSettingsBackend *backend;
1025 backend = g_settings_backend_get_default ();
1026 class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1029 class->sync (backend);