2 * Copyright © 2009, 2010 Codethink Limited
3 * Copyright © 2010 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
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"
36 typedef struct _GSettingsBackendClosure GSettingsBackendClosure;
37 typedef struct _GSettingsBackendWatch GSettingsBackendWatch;
39 struct _GSettingsBackendPrivate
41 GSettingsBackendWatch *watches;
45 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GSettingsBackend, g_settings_backend, G_TYPE_OBJECT)
47 /* For g_settings_backend_sync_default(), we only want to actually do
48 * the sync if the backend already exists. This avoids us creating an
49 * entire GSettingsBackend in order to call a do-nothing sync()
50 * operation on it. This variable lets us avoid that.
52 static gboolean g_settings_has_backend;
55 * SECTION:gsettingsbackend
56 * @title: GSettingsBackend
57 * @short_description: Interface for settings backend implementations
58 * @include: gio/gsettingsbackend.h
59 * @see_also: #GSettings, #GIOExtensionPoint
61 * The #GSettingsBackend interface defines a generic interface for
62 * non-strictly-typed data that is stored in a hierarchy. To implement
63 * an alternative storage backend for #GSettings, you need to implement
64 * the #GSettingsBackend interface and then make it implement the
65 * extension point %G_SETTINGS_BACKEND_EXTENSION_POINT_NAME.
67 * The interface defines methods for reading and writing values, a
68 * method for determining if writing of certain values will fail
69 * (lockdown) and a change notification mechanism.
71 * The semantics of the interface are very precisely defined and
72 * implementations must carefully adhere to the expectations of
73 * callers that are documented on each of the interface methods.
75 * Some of the #GSettingsBackend functions accept or return a #GTree.
76 * These trees always have strings as keys and #GVariant as values.
77 * g_settings_backend_create_tree() is a convenience function to create
80 * The #GSettingsBackend API is exported to allow third-party
81 * implementations, but does not carry the same stability guarantees
82 * as the public GIO API. For this reason, you have to define the
83 * C preprocessor symbol %G_SETTINGS_ENABLE_BACKEND before including
84 * `gio/gsettingsbackend.h`.
88 is_key (const gchar *key)
93 g_return_val_if_fail (key != NULL, FALSE);
94 g_return_val_if_fail (key[0] == '/', FALSE);
96 for (i = 1; key[i]; i++)
97 g_return_val_if_fail (key[i] != '/' || key[i + 1] != '/', FALSE);
101 g_return_val_if_fail (key[length - 1] != '/', FALSE);
107 is_path (const gchar *path)
112 g_return_val_if_fail (path != NULL, FALSE);
113 g_return_val_if_fail (path[0] == '/', FALSE);
115 for (i = 1; path[i]; i++)
116 g_return_val_if_fail (path[i] != '/' || path[i + 1] != '/', FALSE);
120 g_return_val_if_fail (path[length - 1] == '/', FALSE);
125 struct _GSettingsBackendWatch
127 /* Always access the target via the weak reference */
129 /* The pointer is only for comparison from the weak notify,
130 * at which point the target might already be close to
131 * destroyed. It's not safe to use it for anything anymore
134 const GSettingsListenerVTable *vtable;
135 GMainContext *context;
136 GSettingsBackendWatch *next;
139 struct _GSettingsBackendClosure
141 void (*function) (GObject *target,
142 GSettingsBackend *backend,
147 GMainContext *context;
149 GSettingsBackend *backend;
156 g_settings_backend_watch_weak_notify (gpointer data,
157 GObject *where_the_object_was)
159 GSettingsBackend *backend = data;
160 GSettingsBackendWatch **ptr;
162 /* search and remove */
163 g_mutex_lock (&backend->priv->lock);
164 for (ptr = &backend->priv->watches; *ptr; ptr = &(*ptr)->next)
165 if ((*ptr)->target_ptr == where_the_object_was)
167 GSettingsBackendWatch *tmp = *ptr;
170 g_weak_ref_clear (&tmp->target);
171 g_slice_free (GSettingsBackendWatch, tmp);
173 g_mutex_unlock (&backend->priv->lock);
177 /* we didn't find it. that shouldn't happen. */
178 g_assert_not_reached ();
182 * g_settings_backend_watch:
183 * @backend: a #GSettingsBackend
184 * @target: the GObject (typically GSettings instance) to call back to
185 * @context: (nullable): a #GMainContext, or %NULL
188 * Registers a new watch on a #GSettingsBackend.
190 * note: %NULL @context does not mean "default main context" but rather,
191 * "it is okay to dispatch in any context". If the default main context
192 * is specifically desired then it must be given.
194 * note also: if you want to get meaningful values for the @origin_tag
195 * that appears as an argument to some of the callbacks, you *must* have
196 * @context as %NULL. Otherwise, you are subject to cross-thread
197 * dispatching and whatever owned @origin_tag at the time that the event
198 * occurred may no longer own it. This is a problem if you consider that
199 * you may now be the new owner of that address and mistakenly think
200 * that the event in question originated from yourself.
202 * tl;dr: If you give a non-%NULL @context then you must ignore the
203 * value of @origin_tag given to any callbacks.
206 g_settings_backend_watch (GSettingsBackend *backend,
207 const GSettingsListenerVTable *vtable,
209 GMainContext *context)
211 GSettingsBackendWatch *watch;
213 /* For purposes of discussion, we assume that our target is a
214 * GSettings instance.
216 * Our strategy to defend against the final reference dropping on the
217 * GSettings object in a thread other than the one that is doing the
218 * dispatching is as follows:
220 * 1) hold a strong reference on the GSettings during an outstanding
221 * dispatch. This ensures that the delivery is always possible while
222 * the GSettings object is alive, and if this was the last reference
223 * then it will be dropped from the dispatch thread.
225 * 2) hold a weak reference on the GSettings at other times. This
226 * allows us to receive early notification of pending destruction
227 * of the object. At this point, it is still safe to obtain a
228 * reference on the GObject to keep it alive, so #1 will work up
229 * to that point. After that point, we'll have been able to drop
230 * the watch from the list.
232 * Note, in particular, that it's not possible to simply have an
233 * "unwatch" function that gets called from the finalize function of
234 * the GSettings instance because, by that point it is no longer
235 * possible to keep the object alive using g_object_ref() and we would
236 * have no way of knowing this.
238 * Note also that we need to hold a reference on the main context here
239 * since the GSettings instance may be finalized before the closure runs.
241 * All access to the list holds a mutex. We have some strategies to
242 * avoid some of the pain that would be associated with that.
245 watch = g_slice_new (GSettingsBackendWatch);
246 watch->context = context;
247 watch->vtable = vtable;
248 g_weak_ref_init (&watch->target, target);
249 watch->target_ptr = target;
250 g_object_weak_ref (target, g_settings_backend_watch_weak_notify, backend);
252 /* linked list prepend */
253 g_mutex_lock (&backend->priv->lock);
254 watch->next = backend->priv->watches;
255 backend->priv->watches = watch;
256 g_mutex_unlock (&backend->priv->lock);
260 g_settings_backend_unwatch (GSettingsBackend *backend,
263 /* Our caller surely owns a reference on 'target', so the order of
264 * these two calls is unimportant.
266 g_object_weak_unref (target, g_settings_backend_watch_weak_notify, backend);
267 g_settings_backend_watch_weak_notify (backend, target);
271 g_settings_backend_invoke_closure (gpointer user_data)
273 GSettingsBackendClosure *closure = user_data;
275 closure->function (closure->target, closure->backend, closure->name,
276 closure->origin_tag, closure->names);
278 if (closure->context)
279 g_main_context_unref (closure->context);
280 g_object_unref (closure->backend);
281 g_object_unref (closure->target);
282 g_strfreev (closure->names);
283 g_free (closure->name);
285 g_slice_free (GSettingsBackendClosure, closure);
291 g_settings_backend_dispatch_signal (GSettingsBackend *backend,
292 gsize function_offset,
295 const gchar * const *names)
297 GSettingsBackendWatch *watch;
298 GSList *closures = NULL;
300 /* We're in a little bit of a tricky situation here. We need to hold
301 * a lock while traversing the list, but we don't want to hold the
302 * lock while calling back into user code.
304 * We work around this by creating a bunch of GSettingsBackendClosure
305 * objects while holding the lock and dispatching them after. We
306 * never touch the list without holding the lock.
308 g_mutex_lock (&backend->priv->lock);
309 for (watch = backend->priv->watches; watch; watch = watch->next)
311 GSettingsBackendClosure *closure;
312 GObject *target = g_weak_ref_get (&watch->target);
314 /* If the target was destroyed in the meantime, just skip it here */
318 closure = g_slice_new (GSettingsBackendClosure);
319 closure->context = watch->context;
320 if (closure->context)
321 g_main_context_ref (closure->context);
322 closure->backend = g_object_ref (backend);
323 closure->target = g_steal_pointer (&target);
324 closure->function = G_STRUCT_MEMBER (void *, watch->vtable,
326 closure->name = g_strdup (name);
327 closure->origin_tag = origin_tag;
328 closure->names = g_strdupv ((gchar **) names);
330 closures = g_slist_prepend (closures, closure);
332 g_mutex_unlock (&backend->priv->lock);
336 GSettingsBackendClosure *closure = closures->data;
338 if (closure->context)
339 g_main_context_invoke (closure->context,
340 g_settings_backend_invoke_closure,
343 g_settings_backend_invoke_closure (closure);
345 closures = g_slist_delete_link (closures, closures);
350 * g_settings_backend_changed:
351 * @backend: a #GSettingsBackend implementation
352 * @key: the name of the key
353 * @origin_tag: the origin tag
355 * Signals that a single key has possibly changed. Backend
356 * implementations should call this if a key has possibly changed its
359 * @key must be a valid key (ie starting with a slash, not containing
360 * '//', and not ending with a slash).
362 * The implementation must call this function during any call to
363 * g_settings_backend_write(), before the call returns (except in the
364 * case that no keys are actually changed and it cares to detect this
365 * fact). It may not rely on the existence of a mainloop for
366 * dispatching the signal later.
368 * The implementation may call this function at any other time it likes
369 * in response to other events (such as changes occurring outside of the
370 * program). These calls may originate from a mainloop or may originate
371 * in response to any other action (including from calls to
372 * g_settings_backend_write()).
374 * In the case that this call is in response to a call to
375 * g_settings_backend_write() then @origin_tag must be set to the same
376 * value that was passed to that call.
381 g_settings_backend_changed (GSettingsBackend *backend,
385 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
386 g_return_if_fail (is_key (key));
388 g_settings_backend_dispatch_signal (backend,
389 G_STRUCT_OFFSET (GSettingsListenerVTable,
391 key, origin_tag, NULL);
395 * g_settings_backend_keys_changed:
396 * @backend: a #GSettingsBackend implementation
397 * @path: the path containing the changes
398 * @items: (array zero-terminated=1): the %NULL-terminated list of changed keys
399 * @origin_tag: the origin tag
401 * Signals that a list of keys have possibly changed. Backend
402 * implementations should call this if keys have possibly changed their
405 * @path must be a valid path (ie starting and ending with a slash and
406 * not containing '//'). Each string in @items must form a valid key
407 * name when @path is prefixed to it (ie: each item must not start or
408 * end with '/' and must not contain '//').
410 * The meaning of this signal is that any of the key names resulting
411 * from the contatenation of @path with each item in @items may have
414 * The same rules for when notifications must occur apply as per
415 * g_settings_backend_changed(). These two calls can be used
416 * interchangeably if exactly one item has changed (although in that
417 * case g_settings_backend_changed() is definitely preferred).
419 * For efficiency reasons, the implementation should strive for @path to
420 * be as long as possible (ie: the longest common prefix of all of the
421 * keys that were changed) but this is not strictly required.
426 g_settings_backend_keys_changed (GSettingsBackend *backend,
428 gchar const * const *items,
431 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
432 g_return_if_fail (is_path (path));
434 /* XXX: should do stricter checking (ie: inspect each item) */
435 g_return_if_fail (items != NULL);
437 g_settings_backend_dispatch_signal (backend,
438 G_STRUCT_OFFSET (GSettingsListenerVTable,
440 path, origin_tag, items);
444 * g_settings_backend_path_changed:
445 * @backend: a #GSettingsBackend implementation
446 * @path: the path containing the changes
447 * @origin_tag: the origin tag
449 * Signals that all keys below a given path may have possibly changed.
450 * Backend implementations should call this if an entire path of keys
451 * have possibly changed their values.
453 * @path must be a valid path (ie starting and ending with a slash and
454 * not containing '//').
456 * The meaning of this signal is that any of the key which has a name
457 * starting with @path may have changed.
459 * The same rules for when notifications must occur apply as per
460 * g_settings_backend_changed(). This call might be an appropriate
461 * reasponse to a 'reset' call but implementations are also free to
462 * explicitly list the keys that were affected by that call if they can
465 * For efficiency reasons, the implementation should strive for @path to
466 * be as long as possible (ie: the longest common prefix of all of the
467 * keys that were changed) but this is not strictly required. As an
468 * example, if this function is called with the path of "/" then every
469 * single key in the application will be notified of a possible change.
474 g_settings_backend_path_changed (GSettingsBackend *backend,
478 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
479 g_return_if_fail (is_path (path));
481 g_settings_backend_dispatch_signal (backend,
482 G_STRUCT_OFFSET (GSettingsListenerVTable,
484 path, origin_tag, NULL);
488 * g_settings_backend_writable_changed:
489 * @backend: a #GSettingsBackend implementation
490 * @key: the name of the key
492 * Signals that the writability of a single key has possibly changed.
494 * Since GSettings performs no locking operations for itself, this call
495 * will always be made in response to external events.
500 g_settings_backend_writable_changed (GSettingsBackend *backend,
503 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
504 g_return_if_fail (is_key (key));
506 g_settings_backend_dispatch_signal (backend,
507 G_STRUCT_OFFSET (GSettingsListenerVTable,
513 * g_settings_backend_path_writable_changed:
514 * @backend: a #GSettingsBackend implementation
515 * @path: the name of the path
517 * Signals that the writability of all keys below a given path may have
520 * Since GSettings performs no locking operations for itself, this call
521 * will always be made in response to external events.
526 g_settings_backend_path_writable_changed (GSettingsBackend *backend,
529 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
530 g_return_if_fail (is_path (path));
532 g_settings_backend_dispatch_signal (backend,
533 G_STRUCT_OFFSET (GSettingsListenerVTable,
534 path_writable_changed),
547 g_settings_backend_flatten_one (gpointer key,
551 FlattenState *state = user_data;
552 const gchar *skey = key;
555 g_return_val_if_fail (is_key (key), TRUE);
557 /* calculate longest common prefix */
558 if (state->prefix == NULL)
562 /* first key? just take the prefix up to the last '/' */
563 state->prefix = g_strdup (skey);
564 last_byte = strrchr (state->prefix, '/') + 1;
565 state->prefix_len = last_byte - state->prefix;
570 /* find the first character that does not match. we will
571 * definitely find one because the prefix ends in '/' and the key
572 * does not. also: no two keys in the tree are the same.
574 for (i = 0; state->prefix[i] == skey[i]; i++);
576 /* check if we need to shorten the prefix */
577 if (state->prefix[i] != '\0')
579 /* find the nearest '/', terminate after it */
580 while (state->prefix[i - 1] != '/')
583 state->prefix[i] = '\0';
584 state->prefix_len = i;
589 /* save the entire item into the array.
590 * the prefixes will be removed later.
592 *state->keys++ = key;
595 *state->values++ = value;
601 * g_settings_backend_flatten_tree:
602 * @tree: a #GTree containing the changes
603 * @path: (out): the location to save the path
604 * @keys: (out) (transfer container) (array zero-terminated=1): the
605 * location to save the relative keys
606 * @values: (out) (optional) (transfer container) (array zero-terminated=1):
607 * the location to save the values, or %NULL
609 * Calculate the longest common prefix of all keys in a tree and write
610 * out an array of the key names relative to that prefix and,
611 * optionally, the value to store at each of those keys.
613 * You must free the value returned in @path, @keys and @values using
614 * g_free(). You should not attempt to free or unref the contents of
620 g_settings_backend_flatten_tree (GTree *tree,
625 FlattenState state = { 0, };
628 nnodes = g_tree_nnodes (tree);
630 *keys = state.keys = g_new (const gchar *, nnodes + 1);
631 state.keys[nnodes] = NULL;
635 *values = state.values = g_new (GVariant *, nnodes + 1);
636 state.values[nnodes] = NULL;
639 g_tree_foreach (tree, g_settings_backend_flatten_one, &state);
640 g_return_if_fail (*keys + nnodes == state.keys);
642 *path = state.prefix;
644 *--state.keys += state.prefix_len;
648 * g_settings_backend_changed_tree:
649 * @backend: a #GSettingsBackend implementation
650 * @tree: a #GTree containing the changes
651 * @origin_tag: the origin tag
653 * This call is a convenience wrapper. It gets the list of changes from
654 * @tree, computes the longest common prefix and calls
655 * g_settings_backend_changed().
660 g_settings_backend_changed_tree (GSettingsBackend *backend,
667 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
669 g_settings_backend_flatten_tree (tree, &path, &keys, NULL);
676 g_print ("changed_tree(): prefix %s\n", path);
677 for (i = 0; keys[i]; i++)
678 g_print (" %s\n", keys[i]);
683 g_settings_backend_keys_changed (backend, path, keys, origin_tag);
689 * g_settings_backend_read:
690 * @backend: a #GSettingsBackend implementation
691 * @key: the key to read
692 * @expected_type: a #GVariantType
693 * @default_value: if the default value should be returned
695 * Reads a key. This call will never block.
697 * If the key exists, the value associated with it will be returned.
698 * If the key does not exist, %NULL will be returned.
700 * The returned value will be of the type given in @expected_type. If
701 * the backend stored a value of a different type then %NULL will be
704 * If @default_value is %TRUE then this gets the default value from the
705 * backend (ie: the one that the backend would contain if
706 * g_settings_reset() were called).
708 * Returns: (nullable) (transfer full): the value that was read, or %NULL
711 g_settings_backend_read (GSettingsBackend *backend,
713 const GVariantType *expected_type,
714 gboolean default_value)
718 value = G_SETTINGS_BACKEND_GET_CLASS (backend)
719 ->read (backend, key, expected_type, default_value);
722 value = g_variant_take_ref (value);
724 if G_UNLIKELY (value && !g_variant_is_of_type (value, expected_type))
726 g_variant_unref (value);
734 * g_settings_backend_read_user_value:
735 * @backend: a #GSettingsBackend implementation
736 * @key: the key to read
737 * @expected_type: a #GVariantType
739 * Reads the 'user value' of a key.
741 * This is the value of the key that the user has control over and has
742 * set for themselves. Put another way: if the user did not set the
743 * value for themselves, then this will return %NULL (even if the
744 * sysadmin has provided a default value).
746 * Returns: (nullable) (transfer full): the value that was read, or %NULL
749 g_settings_backend_read_user_value (GSettingsBackend *backend,
751 const GVariantType *expected_type)
755 value = G_SETTINGS_BACKEND_GET_CLASS (backend)
756 ->read_user_value (backend, key, expected_type);
759 value = g_variant_take_ref (value);
761 if G_UNLIKELY (value && !g_variant_is_of_type (value, expected_type))
763 g_variant_unref (value);
771 * g_settings_backend_write:
772 * @backend: a #GSettingsBackend implementation
773 * @key: the name of the key
774 * @value: a #GVariant value to write to this key
775 * @origin_tag: the origin tag
777 * Writes exactly one key.
779 * This call does not fail. During this call a
780 * #GSettingsBackend::changed signal will be emitted if the value of the
781 * key has changed. The updated key value will be visible to any signal
784 * One possible method that an implementation might deal with failures is
785 * to emit a second "changed" signal (either during this call, or later)
786 * to indicate that the affected keys have suddenly "changed back" to their
789 * If @value has a floating reference, it will be sunk.
791 * Returns: %TRUE if the write succeeded, %FALSE if the key was not writable
794 g_settings_backend_write (GSettingsBackend *backend,
801 g_variant_ref_sink (value);
802 success = G_SETTINGS_BACKEND_GET_CLASS (backend)
803 ->write (backend, key, value, origin_tag);
804 g_variant_unref (value);
810 * g_settings_backend_write_tree:
811 * @backend: a #GSettingsBackend implementation
812 * @tree: a #GTree containing key-value pairs to write
813 * @origin_tag: the origin tag
815 * Writes one or more keys. This call will never block.
817 * The key of each item in the tree is the key name to write to and the
818 * value is a #GVariant to write. The proper type of #GTree for this
819 * call can be created with g_settings_backend_create_tree(). This call
820 * might take a reference to the tree; you must not modified the #GTree
821 * after passing it to this call.
823 * This call does not fail. During this call a #GSettingsBackend::changed
824 * signal will be emitted if any keys have been changed. The new values of
825 * all updated keys will be visible to any signal callbacks.
827 * One possible method that an implementation might deal with failures is
828 * to emit a second "changed" signal (either during this call, or later)
829 * to indicate that the affected keys have suddenly "changed back" to their
833 g_settings_backend_write_tree (GSettingsBackend *backend,
837 return G_SETTINGS_BACKEND_GET_CLASS (backend)
838 ->write_tree (backend, tree, origin_tag);
842 * g_settings_backend_reset:
843 * @backend: a #GSettingsBackend implementation
844 * @key: the name of a key
845 * @origin_tag: the origin tag
847 * "Resets" the named key to its "default" value (ie: after system-wide
848 * defaults, mandatory keys, etc. have been taken into account) or possibly
852 g_settings_backend_reset (GSettingsBackend *backend,
856 G_SETTINGS_BACKEND_GET_CLASS (backend)
857 ->reset (backend, key, origin_tag);
861 * g_settings_backend_get_writable:
862 * @backend: a #GSettingsBackend implementation
863 * @key: the name of a key
865 * Finds out if a key is available for writing to. This is the
866 * interface through which 'lockdown' is implemented. Locked down
867 * keys will have %FALSE returned by this call.
869 * You should not write to locked-down keys, but if you do, the
870 * implementation will deal with it.
872 * Returns: %TRUE if the key is writable
875 g_settings_backend_get_writable (GSettingsBackend *backend,
878 return G_SETTINGS_BACKEND_GET_CLASS (backend)
879 ->get_writable (backend, key);
883 * g_settings_backend_unsubscribe:
884 * @backend: a #GSettingsBackend
885 * @name: a key or path to subscribe to
887 * Reverses the effect of a previous call to
888 * g_settings_backend_subscribe().
891 g_settings_backend_unsubscribe (GSettingsBackend *backend,
894 G_SETTINGS_BACKEND_GET_CLASS (backend)
895 ->unsubscribe (backend, name);
899 * g_settings_backend_subscribe:
900 * @backend: a #GSettingsBackend
901 * @name: a key or path to subscribe to
903 * Requests that change signals be emitted for events on @name.
906 g_settings_backend_subscribe (GSettingsBackend *backend,
909 G_SETTINGS_BACKEND_GET_CLASS (backend)
910 ->subscribe (backend, name);
914 g_settings_backend_finalize (GObject *object)
916 GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
918 g_mutex_clear (&backend->priv->lock);
920 G_OBJECT_CLASS (g_settings_backend_parent_class)
925 ignore_subscription (GSettingsBackend *backend,
931 g_settings_backend_real_read_user_value (GSettingsBackend *backend,
933 const GVariantType *expected_type)
935 return g_settings_backend_read (backend, key, expected_type, FALSE);
939 g_settings_backend_init (GSettingsBackend *backend)
941 backend->priv = g_settings_backend_get_instance_private (backend);
942 g_mutex_init (&backend->priv->lock);
946 g_settings_backend_class_init (GSettingsBackendClass *class)
948 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
950 class->subscribe = ignore_subscription;
951 class->unsubscribe = ignore_subscription;
953 class->read_user_value = g_settings_backend_real_read_user_value;
955 gobject_class->finalize = g_settings_backend_finalize;
959 g_settings_backend_variant_unref0 (gpointer data)
962 g_variant_unref (data);
966 * g_settings_backend_create_tree:
968 * This is a convenience function for creating a tree that is compatible
969 * with g_settings_backend_write(). It merely calls g_tree_new_full()
970 * with strcmp(), g_free() and g_variant_unref().
972 * Returns: a new #GTree
975 g_settings_backend_create_tree (void)
977 return g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
978 g_free, g_settings_backend_variant_unref0);
982 g_settings_backend_verify (gpointer impl)
984 GSettingsBackend *backend = impl;
986 if (strcmp (G_OBJECT_TYPE_NAME (backend), "GMemorySettingsBackend") == 0 &&
987 g_strcmp0 (g_getenv ("GSETTINGS_BACKEND"), "memory") != 0)
989 g_message ("Using the 'memory' GSettings backend. Your settings "
990 "will not be saved or shared with other applications.");
993 g_settings_has_backend = TRUE;
997 /* We need to cache the default #GSettingsBackend for the entire process
998 * lifetime, especially if the backend is #GMemorySettingsBackend: it needs to
999 * keep the in-memory settings around even while there are no #GSettings
1000 * instances alive. */
1001 static GSettingsBackend *settings_backend_default_singleton = NULL; /* (owned) (atomic) */
1004 * g_settings_backend_get_default:
1006 * Returns the default #GSettingsBackend. It is possible to override
1007 * the default by setting the `GSETTINGS_BACKEND` environment variable
1008 * to the name of a settings backend.
1010 * The user gets a reference to the backend.
1012 * Returns: (not nullable) (transfer full): the default #GSettingsBackend,
1013 * which will be a dummy (memory) settings backend if no other settings
1014 * backend is available.
1019 g_settings_backend_get_default (void)
1021 if (g_once_init_enter (&settings_backend_default_singleton))
1023 GSettingsBackend *singleton;
1025 singleton = _g_io_module_get_default (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
1026 "GSETTINGS_BACKEND",
1027 g_settings_backend_verify);
1029 g_once_init_leave (&settings_backend_default_singleton, singleton);
1032 return g_object_ref (settings_backend_default_singleton);
1036 * g_settings_backend_get_permission:
1037 * @backend: a #GSettingsBackend
1040 * Gets the permission object associated with writing to keys below
1041 * @path on @backend.
1043 * If this is not implemented in the backend, then a %TRUE
1044 * #GSimplePermission is returned.
1046 * Returns: (not nullable) (transfer full): a non-%NULL #GPermission.
1047 * Free with g_object_unref()
1050 g_settings_backend_get_permission (GSettingsBackend *backend,
1053 GSettingsBackendClass *class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1055 if (class->get_permission)
1056 return class->get_permission (backend, path);
1058 return g_simple_permission_new (TRUE);
1062 * g_settings_backend_sync_default:
1064 * Syncs the default backend.
1067 g_settings_backend_sync_default (void)
1069 if (g_settings_has_backend)
1071 GSettingsBackendClass *class;
1072 GSettingsBackend *backend;
1074 backend = g_settings_backend_get_default ();
1075 class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1078 class->sync (backend);
1080 g_object_unref (backend);