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.1 of the License, 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, see <http://www.gnu.org/licenses/>.
18 * Authors: Ryan Lortie <desrt@desrt.ca>
19 * Matthias Clasen <mclasen@redhat.com>
24 #include "gsettingsbackendinternal.h"
25 #include "gsimplepermission.h"
26 #include "giomodule-priv.h"
34 typedef struct _GSettingsBackendClosure GSettingsBackendClosure;
35 typedef struct _GSettingsBackendWatch GSettingsBackendWatch;
37 struct _GSettingsBackendPrivate
39 GSettingsBackendWatch *watches;
43 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GSettingsBackend, g_settings_backend, G_TYPE_OBJECT)
45 /* For g_settings_backend_sync_default(), we only want to actually do
46 * the sync if the backend already exists. This avoids us creating an
47 * entire GSettingsBackend in order to call a do-nothing sync()
48 * operation on it. This variable lets us avoid that.
50 static gboolean g_settings_has_backend;
53 * SECTION:gsettingsbackend
54 * @title: GSettingsBackend
55 * @short_description: Interface for settings backend implementations
56 * @include: gio/gsettingsbackend.h
57 * @see_also: #GSettings, #GIOExtensionPoint
59 * The #GSettingsBackend interface defines a generic interface for
60 * non-strictly-typed data that is stored in a hierarchy. To implement
61 * an alternative storage backend for #GSettings, you need to implement
62 * the #GSettingsBackend interface and then make it implement the
63 * extension point #G_SETTINGS_BACKEND_EXTENSION_POINT_NAME.
65 * The interface defines methods for reading and writing values, a
66 * method for determining if writing of certain values will fail
67 * (lockdown) and a change notification mechanism.
69 * The semantics of the interface are very precisely defined and
70 * implementations must carefully adhere to the expectations of
71 * callers that are documented on each of the interface methods.
73 * Some of the #GSettingsBackend functions accept or return a #GTree.
74 * These trees always have strings as keys and #GVariant as values.
75 * g_settings_backend_create_tree() is a convenience function to create
78 * The #GSettingsBackend API is exported to allow third-party
79 * implementations, but does not carry the same stability guarantees
80 * as the public GIO API. For this reason, you have to define the
81 * C preprocessor symbol %G_SETTINGS_ENABLE_BACKEND before including
82 * `gio/gsettingsbackend.h`.
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);
123 struct _GSettingsBackendWatch
125 /* Always access the target via the weak reference */
127 /* The pointer is only for comparison from the weak notify,
128 * at which point the target might already be close to
129 * destroyed. It's not safe to use it for anything anymore
132 const GSettingsListenerVTable *vtable;
133 GMainContext *context;
134 GSettingsBackendWatch *next;
137 struct _GSettingsBackendClosure
139 void (*function) (GObject *target,
140 GSettingsBackend *backend,
145 GMainContext *context;
147 GSettingsBackend *backend;
154 g_settings_backend_watch_weak_notify (gpointer data,
155 GObject *where_the_object_was)
157 GSettingsBackend *backend = data;
158 GSettingsBackendWatch **ptr;
160 /* search and remove */
161 g_mutex_lock (&backend->priv->lock);
162 for (ptr = &backend->priv->watches; *ptr; ptr = &(*ptr)->next)
163 if ((*ptr)->target_ptr == where_the_object_was)
165 GSettingsBackendWatch *tmp = *ptr;
168 g_weak_ref_clear (&tmp->target);
169 g_slice_free (GSettingsBackendWatch, tmp);
171 g_mutex_unlock (&backend->priv->lock);
175 /* we didn't find it. that shouldn't happen. */
176 g_assert_not_reached ();
180 * g_settings_backend_watch:
181 * @backend: a #GSettingsBackend
182 * @target: the GObject (typically GSettings instance) to call back to
183 * @context: (nullable): a #GMainContext, or %NULL
186 * Registers a new watch on a #GSettingsBackend.
188 * note: %NULL @context does not mean "default main context" but rather,
189 * "it is okay to dispatch in any context". If the default main context
190 * is specifically desired then it must be given.
192 * note also: if you want to get meaningful values for the @origin_tag
193 * that appears as an argument to some of the callbacks, you *must* have
194 * @context as %NULL. Otherwise, you are subject to cross-thread
195 * dispatching and whatever owned @origin_tag at the time that the event
196 * occurred may no longer own it. This is a problem if you consider that
197 * you may now be the new owner of that address and mistakenly think
198 * that the event in question originated from yourself.
200 * tl;dr: If you give a non-%NULL @context then you must ignore the
201 * value of @origin_tag given to any callbacks.
204 g_settings_backend_watch (GSettingsBackend *backend,
205 const GSettingsListenerVTable *vtable,
207 GMainContext *context)
209 GSettingsBackendWatch *watch;
211 /* For purposes of discussion, we assume that our target is a
212 * GSettings instance.
214 * Our strategy to defend against the final reference dropping on the
215 * GSettings object in a thread other than the one that is doing the
216 * dispatching is as follows:
218 * 1) hold a strong reference on the GSettings during an outstanding
219 * dispatch. This ensures that the delivery is always possible while
220 * the GSettings object is alive, and if this was the last reference
221 * then it will be dropped from the dispatch thread.
223 * 2) hold a weak reference on the GSettings at other times. This
224 * allows us to receive early notification of pending destruction
225 * of the object. At this point, it is still safe to obtain a
226 * reference on the GObject to keep it alive, so #1 will work up
227 * to that point. After that point, we'll have been able to drop
228 * the watch from the list.
230 * Note, in particular, that it's not possible to simply have an
231 * "unwatch" function that gets called from the finalize function of
232 * the GSettings instance because, by that point it is no longer
233 * possible to keep the object alive using g_object_ref() and we would
234 * have no way of knowing this.
236 * Note also that we need to hold a reference on the main context here
237 * since the GSettings instance may be finalized before the closure runs.
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 g_weak_ref_init (&watch->target, target);
247 watch->target_ptr = target;
248 g_object_weak_ref (target, g_settings_backend_watch_weak_notify, backend);
250 /* linked list prepend */
251 g_mutex_lock (&backend->priv->lock);
252 watch->next = backend->priv->watches;
253 backend->priv->watches = watch;
254 g_mutex_unlock (&backend->priv->lock);
258 g_settings_backend_unwatch (GSettingsBackend *backend,
261 /* Our caller surely owns a reference on 'target', so the order of
262 * these two calls is unimportant.
264 g_object_weak_unref (target, g_settings_backend_watch_weak_notify, backend);
265 g_settings_backend_watch_weak_notify (backend, target);
269 g_settings_backend_invoke_closure (gpointer user_data)
271 GSettingsBackendClosure *closure = user_data;
273 closure->function (closure->target, closure->backend, closure->name,
274 closure->origin_tag, closure->names);
276 if (closure->context)
277 g_main_context_unref (closure->context);
278 g_object_unref (closure->backend);
279 g_object_unref (closure->target);
280 g_strfreev (closure->names);
281 g_free (closure->name);
283 g_slice_free (GSettingsBackendClosure, closure);
289 g_settings_backend_dispatch_signal (GSettingsBackend *backend,
290 gsize function_offset,
293 const gchar * const *names)
295 GSettingsBackendWatch *watch;
296 GSList *closures = NULL;
298 /* We're in a little bit of a tricky situation here. We need to hold
299 * a lock while traversing the list, but we don't want to hold the
300 * lock while calling back into user code.
302 * We work around this by creating a bunch of GSettingsBackendClosure
303 * objects while holding the lock and dispatching them after. We
304 * never touch the list without holding the lock.
306 g_mutex_lock (&backend->priv->lock);
307 for (watch = backend->priv->watches; watch; watch = watch->next)
309 GSettingsBackendClosure *closure;
310 GObject *target = g_weak_ref_get (&watch->target);
312 /* If the target was destroyed in the meantime, just skip it here */
316 closure = g_slice_new (GSettingsBackendClosure);
317 closure->context = watch->context;
318 if (closure->context)
319 g_main_context_ref (closure->context);
320 closure->backend = g_object_ref (backend);
321 closure->target = g_steal_pointer (&target);
322 closure->function = G_STRUCT_MEMBER (void *, watch->vtable,
324 closure->name = g_strdup (name);
325 closure->origin_tag = origin_tag;
326 closure->names = g_strdupv ((gchar **) names);
328 closures = g_slist_prepend (closures, closure);
330 g_mutex_unlock (&backend->priv->lock);
334 GSettingsBackendClosure *closure = closures->data;
336 if (closure->context)
337 g_main_context_invoke (closure->context,
338 g_settings_backend_invoke_closure,
341 g_settings_backend_invoke_closure (closure);
343 closures = g_slist_delete_link (closures, closures);
348 * g_settings_backend_changed:
349 * @backend: a #GSettingsBackend implementation
350 * @key: the name of the key
351 * @origin_tag: the origin tag
353 * Signals that a single key has possibly changed. Backend
354 * implementations should call this if a key has possibly changed its
357 * @key must be a valid key (ie starting with a slash, not containing
358 * '//', and not ending with a slash).
360 * The implementation must call this function during any call to
361 * g_settings_backend_write(), before the call returns (except in the
362 * case that no keys are actually changed and it cares to detect this
363 * fact). It may not rely on the existence of a mainloop for
364 * dispatching the signal later.
366 * The implementation may call this function at any other time it likes
367 * in response to other events (such as changes occurring outside of the
368 * program). These calls may originate from a mainloop or may originate
369 * in response to any other action (including from calls to
370 * g_settings_backend_write()).
372 * In the case that this call is in response to a call to
373 * g_settings_backend_write() then @origin_tag must be set to the same
374 * value that was passed to that call.
379 g_settings_backend_changed (GSettingsBackend *backend,
383 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
384 g_return_if_fail (is_key (key));
386 g_settings_backend_dispatch_signal (backend,
387 G_STRUCT_OFFSET (GSettingsListenerVTable,
389 key, origin_tag, NULL);
393 * g_settings_backend_keys_changed:
394 * @backend: a #GSettingsBackend implementation
395 * @path: the path containing the changes
396 * @items: (array zero-terminated=1): the %NULL-terminated list of changed keys
397 * @origin_tag: the origin tag
399 * Signals that a list of keys have possibly changed. Backend
400 * implementations should call this if keys have possibly changed their
403 * @path must be a valid path (ie starting and ending with a slash and
404 * not containing '//'). Each string in @items must form a valid key
405 * name when @path is prefixed to it (ie: each item must not start or
406 * end with '/' and must not contain '//').
408 * The meaning of this signal is that any of the key names resulting
409 * from the contatenation of @path with each item in @items may have
412 * The same rules for when notifications must occur apply as per
413 * g_settings_backend_changed(). These two calls can be used
414 * interchangeably if exactly one item has changed (although in that
415 * case g_settings_backend_changed() is definitely preferred).
417 * For efficiency reasons, the implementation should strive for @path to
418 * be as long as possible (ie: the longest common prefix of all of the
419 * keys that were changed) but this is not strictly required.
424 g_settings_backend_keys_changed (GSettingsBackend *backend,
426 gchar const * const *items,
429 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
430 g_return_if_fail (is_path (path));
432 /* XXX: should do stricter checking (ie: inspect each item) */
433 g_return_if_fail (items != NULL);
435 g_settings_backend_dispatch_signal (backend,
436 G_STRUCT_OFFSET (GSettingsListenerVTable,
438 path, origin_tag, items);
442 * g_settings_backend_path_changed:
443 * @backend: a #GSettingsBackend implementation
444 * @path: the path containing the changes
445 * @origin_tag: the origin tag
447 * Signals that all keys below a given path may have possibly changed.
448 * Backend implementations should call this if an entire path of keys
449 * have possibly changed their values.
451 * @path must be a valid path (ie starting and ending with a slash and
452 * not containing '//').
454 * The meaning of this signal is that any of the key which has a name
455 * starting with @path may have changed.
457 * The same rules for when notifications must occur apply as per
458 * g_settings_backend_changed(). This call might be an appropriate
459 * reasponse to a 'reset' call but implementations are also free to
460 * explicitly list the keys that were affected by that call if they can
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. As an
466 * example, if this function is called with the path of "/" then every
467 * single key in the application will be notified of a possible change.
472 g_settings_backend_path_changed (GSettingsBackend *backend,
476 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
477 g_return_if_fail (is_path (path));
479 g_settings_backend_dispatch_signal (backend,
480 G_STRUCT_OFFSET (GSettingsListenerVTable,
482 path, origin_tag, NULL);
486 * g_settings_backend_writable_changed:
487 * @backend: a #GSettingsBackend implementation
488 * @key: the name of the key
490 * Signals that the writability of a single key has possibly changed.
492 * Since GSettings performs no locking operations for itself, this call
493 * will always be made in response to external events.
498 g_settings_backend_writable_changed (GSettingsBackend *backend,
501 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
502 g_return_if_fail (is_key (key));
504 g_settings_backend_dispatch_signal (backend,
505 G_STRUCT_OFFSET (GSettingsListenerVTable,
511 * g_settings_backend_path_writable_changed:
512 * @backend: a #GSettingsBackend implementation
513 * @path: the name of the path
515 * Signals that the writability of all keys below a given path may have
518 * Since GSettings performs no locking operations for itself, this call
519 * will always be made in response to external events.
524 g_settings_backend_path_writable_changed (GSettingsBackend *backend,
527 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
528 g_return_if_fail (is_path (path));
530 g_settings_backend_dispatch_signal (backend,
531 G_STRUCT_OFFSET (GSettingsListenerVTable,
532 path_writable_changed),
545 g_settings_backend_flatten_one (gpointer key,
549 FlattenState *state = user_data;
550 const gchar *skey = key;
553 g_return_val_if_fail (is_key (key), TRUE);
555 /* calculate longest common prefix */
556 if (state->prefix == NULL)
560 /* first key? just take the prefix up to the last '/' */
561 state->prefix = g_strdup (skey);
562 last_byte = strrchr (state->prefix, '/') + 1;
563 state->prefix_len = last_byte - state->prefix;
568 /* find the first character that does not match. we will
569 * definitely find one because the prefix ends in '/' and the key
570 * does not. also: no two keys in the tree are the same.
572 for (i = 0; state->prefix[i] == skey[i]; i++);
574 /* check if we need to shorten the prefix */
575 if (state->prefix[i] != '\0')
577 /* find the nearest '/', terminate after it */
578 while (state->prefix[i - 1] != '/')
581 state->prefix[i] = '\0';
582 state->prefix_len = i;
587 /* save the entire item into the array.
588 * the prefixes will be removed later.
590 *state->keys++ = key;
593 *state->values++ = value;
599 * g_settings_backend_flatten_tree:
600 * @tree: a #GTree containing the changes
601 * @path: (out): the location to save the path
602 * @keys: (out) (transfer container) (array zero-terminated=1): the
603 * location to save the relative keys
604 * @values: (out) (optional) (transfer container) (array zero-terminated=1):
605 * the location to save the values, or %NULL
607 * Calculate the longest common prefix of all keys in a tree and write
608 * out an array of the key names relative to that prefix and,
609 * optionally, the value to store at each of those keys.
611 * You must free the value returned in @path, @keys and @values using
612 * g_free(). You should not attempt to free or unref the contents of
618 g_settings_backend_flatten_tree (GTree *tree,
623 FlattenState state = { 0, };
626 nnodes = g_tree_nnodes (tree);
628 *keys = state.keys = g_new (const gchar *, nnodes + 1);
629 state.keys[nnodes] = NULL;
633 *values = state.values = g_new (GVariant *, nnodes + 1);
634 state.values[nnodes] = NULL;
637 g_tree_foreach (tree, g_settings_backend_flatten_one, &state);
638 g_return_if_fail (*keys + nnodes == state.keys);
640 *path = state.prefix;
642 *--state.keys += state.prefix_len;
646 * g_settings_backend_changed_tree:
647 * @backend: a #GSettingsBackend implementation
648 * @tree: a #GTree containing the changes
649 * @origin_tag: the origin tag
651 * This call is a convenience wrapper. It gets the list of changes from
652 * @tree, computes the longest common prefix and calls
653 * g_settings_backend_changed().
658 g_settings_backend_changed_tree (GSettingsBackend *backend,
665 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
667 g_settings_backend_flatten_tree (tree, &path, &keys, NULL);
674 g_print ("changed_tree(): prefix %s\n", path);
675 for (i = 0; keys[i]; i++)
676 g_print (" %s\n", keys[i]);
681 g_settings_backend_keys_changed (backend, path, keys, origin_tag);
687 * g_settings_backend_read:
688 * @backend: a #GSettingsBackend implementation
689 * @key: the key to read
690 * @expected_type: a #GVariantType
691 * @default_value: if the default value should be returned
693 * Reads a key. This call will never block.
695 * If the key exists, the value associated with it will be returned.
696 * If the key does not exist, %NULL will be returned.
698 * The returned value will be of the type given in @expected_type. If
699 * the backend stored a value of a different type then %NULL will be
702 * If @default_value is %TRUE then this gets the default value from the
703 * backend (ie: the one that the backend would contain if
704 * g_settings_reset() were called).
706 * Returns: the value that was read, or %NULL
709 g_settings_backend_read (GSettingsBackend *backend,
711 const GVariantType *expected_type,
712 gboolean default_value)
716 value = G_SETTINGS_BACKEND_GET_CLASS (backend)
717 ->read (backend, key, expected_type, default_value);
720 value = g_variant_take_ref (value);
722 if G_UNLIKELY (value && !g_variant_is_of_type (value, expected_type))
724 g_variant_unref (value);
732 * g_settings_backend_read_user_value:
733 * @backend: a #GSettingsBackend implementation
734 * @key: the key to read
735 * @expected_type: a #GVariantType
737 * Reads the 'user value' of a key.
739 * This is the value of the key that the user has control over and has
740 * set for themselves. Put another way: if the user did not set the
741 * value for themselves, then this will return %NULL (even if the
742 * sysadmin has provided a default value).
744 * Returns: the value that was read, or %NULL
747 g_settings_backend_read_user_value (GSettingsBackend *backend,
749 const GVariantType *expected_type)
753 value = G_SETTINGS_BACKEND_GET_CLASS (backend)
754 ->read_user_value (backend, key, expected_type);
757 value = g_variant_take_ref (value);
759 if G_UNLIKELY (value && !g_variant_is_of_type (value, expected_type))
761 g_variant_unref (value);
769 * g_settings_backend_write:
770 * @backend: a #GSettingsBackend implementation
771 * @key: the name of the key
772 * @value: a #GVariant value to write to this key
773 * @origin_tag: the origin tag
775 * Writes exactly one key.
777 * This call does not fail. During this call a
778 * #GSettingsBackend::changed signal will be emitted if the value of the
779 * key has changed. The updated key value will be visible to any signal
782 * One possible method that an implementation might deal with failures is
783 * to emit a second "changed" signal (either during this call, or later)
784 * to indicate that the affected keys have suddenly "changed back" to their
787 * If @value has a floating reference, it will be sunk.
789 * Returns: %TRUE if the write succeeded, %FALSE if the key was not writable
792 g_settings_backend_write (GSettingsBackend *backend,
799 g_variant_ref_sink (value);
800 success = G_SETTINGS_BACKEND_GET_CLASS (backend)
801 ->write (backend, key, value, origin_tag);
802 g_variant_unref (value);
808 * g_settings_backend_write_tree:
809 * @backend: a #GSettingsBackend implementation
810 * @tree: a #GTree containing key-value pairs to write
811 * @origin_tag: the origin tag
813 * Writes one or more keys. This call will never block.
815 * The key of each item in the tree is the key name to write to and the
816 * value is a #GVariant to write. The proper type of #GTree for this
817 * call can be created with g_settings_backend_create_tree(). This call
818 * might take a reference to the tree; you must not modified the #GTree
819 * after passing it to this call.
821 * This call does not fail. During this call a #GSettingsBackend::changed
822 * signal will be emitted if any keys have been changed. The new values of
823 * all updated keys will be visible to any signal callbacks.
825 * One possible method that an implementation might deal with failures is
826 * to emit a second "changed" signal (either during this call, or later)
827 * to indicate that the affected keys have suddenly "changed back" to their
831 g_settings_backend_write_tree (GSettingsBackend *backend,
835 return G_SETTINGS_BACKEND_GET_CLASS (backend)
836 ->write_tree (backend, tree, origin_tag);
840 * g_settings_backend_reset:
841 * @backend: a #GSettingsBackend implementation
842 * @key: the name of a key
843 * @origin_tag: the origin tag
845 * "Resets" the named key to its "default" value (ie: after system-wide
846 * defaults, mandatory keys, etc. have been taken into account) or possibly
850 g_settings_backend_reset (GSettingsBackend *backend,
854 G_SETTINGS_BACKEND_GET_CLASS (backend)
855 ->reset (backend, key, origin_tag);
859 * g_settings_backend_get_writable:
860 * @backend: a #GSettingsBackend implementation
861 * @key: the name of a key
863 * Finds out if a key is available for writing to. This is the
864 * interface through which 'lockdown' is implemented. Locked down
865 * keys will have %FALSE returned by this call.
867 * You should not write to locked-down keys, but if you do, the
868 * implementation will deal with it.
870 * Returns: %TRUE if the key is writable
873 g_settings_backend_get_writable (GSettingsBackend *backend,
876 return G_SETTINGS_BACKEND_GET_CLASS (backend)
877 ->get_writable (backend, key);
881 * g_settings_backend_unsubscribe:
882 * @backend: a #GSettingsBackend
883 * @name: a key or path to subscribe to
885 * Reverses the effect of a previous call to
886 * g_settings_backend_subscribe().
889 g_settings_backend_unsubscribe (GSettingsBackend *backend,
892 G_SETTINGS_BACKEND_GET_CLASS (backend)
893 ->unsubscribe (backend, name);
897 * g_settings_backend_subscribe:
898 * @backend: a #GSettingsBackend
899 * @name: a key or path to subscribe to
901 * Requests that change signals be emitted for events on @name.
904 g_settings_backend_subscribe (GSettingsBackend *backend,
907 G_SETTINGS_BACKEND_GET_CLASS (backend)
908 ->subscribe (backend, name);
912 g_settings_backend_finalize (GObject *object)
914 GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
916 g_mutex_clear (&backend->priv->lock);
918 G_OBJECT_CLASS (g_settings_backend_parent_class)
923 ignore_subscription (GSettingsBackend *backend,
929 g_settings_backend_real_read_user_value (GSettingsBackend *backend,
931 const GVariantType *expected_type)
933 return g_settings_backend_read (backend, key, expected_type, FALSE);
937 g_settings_backend_init (GSettingsBackend *backend)
939 backend->priv = g_settings_backend_get_instance_private (backend);
940 g_mutex_init (&backend->priv->lock);
944 g_settings_backend_class_init (GSettingsBackendClass *class)
946 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
948 class->subscribe = ignore_subscription;
949 class->unsubscribe = ignore_subscription;
951 class->read_user_value = g_settings_backend_real_read_user_value;
953 gobject_class->finalize = g_settings_backend_finalize;
957 g_settings_backend_variant_unref0 (gpointer data)
960 g_variant_unref (data);
964 * g_settings_backend_create_tree:
966 * This is a convenience function for creating a tree that is compatible
967 * with g_settings_backend_write(). It merely calls g_tree_new_full()
968 * with strcmp(), g_free() and g_variant_unref().
970 * Returns: a new #GTree
973 g_settings_backend_create_tree (void)
975 return g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
976 g_free, g_settings_backend_variant_unref0);
980 g_settings_backend_verify (gpointer impl)
982 GSettingsBackend *backend = impl;
984 if (strcmp (G_OBJECT_TYPE_NAME (backend), "GMemorySettingsBackend") == 0 &&
985 g_strcmp0 (g_getenv ("GSETTINGS_BACKEND"), "memory") != 0)
987 g_message ("Using the 'memory' GSettings backend. Your settings "
988 "will not be saved or shared with other applications.");
991 g_settings_has_backend = TRUE;
996 * g_settings_backend_get_default:
998 * Returns the default #GSettingsBackend. It is possible to override
999 * the default by setting the `GSETTINGS_BACKEND` environment variable
1000 * to the name of a settings backend.
1002 * The user gets a reference to the backend.
1004 * Returns: (transfer full): the default #GSettingsBackend
1009 g_settings_backend_get_default (void)
1011 GSettingsBackend *backend;
1013 backend = _g_io_module_get_default (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
1014 "GSETTINGS_BACKEND",
1015 g_settings_backend_verify);
1016 return g_object_ref (backend);
1020 * g_settings_backend_get_permission:
1021 * @backend: a #GSettingsBackend
1024 * Gets the permission object associated with writing to keys below
1025 * @path on @backend.
1027 * If this is not implemented in the backend, then a %TRUE
1028 * #GSimplePermission is returned.
1030 * Returns: a non-%NULL #GPermission. Free with g_object_unref()
1033 g_settings_backend_get_permission (GSettingsBackend *backend,
1036 GSettingsBackendClass *class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1038 if (class->get_permission)
1039 return class->get_permission (backend, path);
1041 return g_simple_permission_new (TRUE);
1045 * g_settings_backend_sync_default:
1047 * Syncs the default backend.
1050 g_settings_backend_sync_default (void)
1052 if (g_settings_has_backend)
1054 GSettingsBackendClass *class;
1055 GSettingsBackend *backend;
1057 backend = g_settings_backend_get_default ();
1058 class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1061 class->sync (backend);
1063 g_object_unref (backend);