Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / gio / gsettingsbackend.c
blobedc4ff4d3af9df3085ec2fc716217786051893a8
1 /*
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>
22 #include "config.h"
24 #include "gsettingsbackendinternal.h"
25 #include "gsimplepermission.h"
26 #include "giomodule-priv.h"
28 #include <string.h>
29 #include <stdlib.h>
30 #include <glib.h>
31 #include <glibintl.h>
34 typedef struct _GSettingsBackendClosure GSettingsBackendClosure;
35 typedef struct _GSettingsBackendWatch GSettingsBackendWatch;
37 struct _GSettingsBackendPrivate
39 GSettingsBackendWatch *watches;
40 GMutex lock;
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;
52 /**
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
76 * suitable trees.
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`.
83 **/
85 static gboolean
86 is_key (const gchar *key)
88 gint length;
89 gint i;
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);
97 length = i;
99 g_return_val_if_fail (key[length - 1] != '/', FALSE);
101 return TRUE;
104 static gboolean
105 is_path (const gchar *path)
107 gint length;
108 gint i;
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);
116 length = i;
118 g_return_val_if_fail (path[length - 1] == '/', FALSE);
120 return TRUE;
123 struct _GSettingsBackendWatch
125 GObject *target;
126 const GSettingsListenerVTable *vtable;
127 GMainContext *context;
128 GSettingsBackendWatch *next;
131 struct _GSettingsBackendClosure
133 void (*function) (GObject *target,
134 GSettingsBackend *backend,
135 const gchar *name,
136 gpointer origin_tag,
137 gchar **names);
139 GMainContext *context;
140 GWeakRef *target_ref;
141 GSettingsBackend *backend;
142 gchar *name;
143 gpointer origin_tag;
144 gchar **names;
147 static void
148 g_settings_backend_watch_weak_notify (gpointer data,
149 GObject *where_the_object_was)
151 GSettingsBackend *backend = data;
152 GSettingsBackendWatch **ptr;
154 /* search and remove */
155 g_mutex_lock (&backend->priv->lock);
156 for (ptr = &backend->priv->watches; *ptr; ptr = &(*ptr)->next)
157 if ((*ptr)->target == where_the_object_was)
159 GSettingsBackendWatch *tmp = *ptr;
161 *ptr = tmp->next;
162 g_slice_free (GSettingsBackendWatch, tmp);
164 g_mutex_unlock (&backend->priv->lock);
165 return;
168 /* we didn't find it. that shouldn't happen. */
169 g_assert_not_reached ();
172 /*< private >
173 * g_settings_backend_watch:
174 * @backend: a #GSettingsBackend
175 * @target: the GObject (typically GSettings instance) to call back to
176 * @context: (nullable): a #GMainContext, or %NULL
177 * ...: callbacks...
179 * Registers a new watch on a #GSettingsBackend.
181 * note: %NULL @context does not mean "default main context" but rather,
182 * "it is okay to dispatch in any context". If the default main context
183 * is specifically desired then it must be given.
185 * note also: if you want to get meaningful values for the @origin_tag
186 * that appears as an argument to some of the callbacks, you *must* have
187 * @context as %NULL. Otherwise, you are subject to cross-thread
188 * dispatching and whatever owned @origin_tag at the time that the event
189 * occurred may no longer own it. This is a problem if you consider that
190 * you may now be the new owner of that address and mistakenly think
191 * that the event in question originated from yourself.
193 * tl;dr: If you give a non-%NULL @context then you must ignore the
194 * value of @origin_tag given to any callbacks.
196 void
197 g_settings_backend_watch (GSettingsBackend *backend,
198 const GSettingsListenerVTable *vtable,
199 GObject *target,
200 GMainContext *context)
202 GSettingsBackendWatch *watch;
204 /* For purposes of discussion, we assume that our target is a
205 * GSettings instance.
207 * Our strategy to defend against the final reference dropping on the
208 * GSettings object in a thread other than the one that is doing the
209 * dispatching is as follows:
211 * 1) hold a thread-safe GWeakRef on the GSettings during an outstanding
212 * dispatch. This ensures that the delivery is always possible while
213 * the GSettings object is alive.
215 * 2) hold a weak reference on the GSettings at other times. This
216 * allows us to receive early notification of pending destruction
217 * of the object. At this point, it is still safe to obtain a
218 * reference on the GObject to keep it alive, so #1 will work up
219 * to that point. After that point, we'll have been able to drop
220 * the watch from the list.
222 * Note, in particular, that it's not possible to simply have an
223 * "unwatch" function that gets called from the finalize function of
224 * the GSettings instance because, by that point it is no longer
225 * possible to keep the object alive using g_object_ref() and we would
226 * have no way of knowing this.
228 * Note also that we need to hold a reference on the main context here
229 * since the GSettings instance may be finalized before the closure runs.
231 * All access to the list holds a mutex. We have some strategies to
232 * avoid some of the pain that would be associated with that.
235 watch = g_slice_new (GSettingsBackendWatch);
236 watch->context = context;
237 watch->vtable = vtable;
238 watch->target = target;
239 g_object_weak_ref (target, g_settings_backend_watch_weak_notify, backend);
241 /* linked list prepend */
242 g_mutex_lock (&backend->priv->lock);
243 watch->next = backend->priv->watches;
244 backend->priv->watches = watch;
245 g_mutex_unlock (&backend->priv->lock);
248 void
249 g_settings_backend_unwatch (GSettingsBackend *backend,
250 GObject *target)
252 /* Our caller surely owns a reference on 'target', so the order of
253 * these two calls is unimportant.
255 g_object_weak_unref (target, g_settings_backend_watch_weak_notify, backend);
256 g_settings_backend_watch_weak_notify (backend, target);
259 static gboolean
260 g_settings_backend_invoke_closure (gpointer user_data)
262 GSettingsBackendClosure *closure = user_data;
263 GObject *target = g_weak_ref_get (closure->target_ref);
265 if (target)
267 closure->function (target, closure->backend, closure->name,
268 closure->origin_tag, closure->names);
269 g_object_unref (target);
272 if (closure->context)
273 g_main_context_unref (closure->context);
274 g_object_unref (closure->backend);
275 g_weak_ref_clear (closure->target_ref);
276 g_free (closure->target_ref);
277 g_strfreev (closure->names);
278 g_free (closure->name);
280 g_slice_free (GSettingsBackendClosure, closure);
282 return FALSE;
285 static void
286 g_settings_backend_dispatch_signal (GSettingsBackend *backend,
287 gsize function_offset,
288 const gchar *name,
289 gpointer origin_tag,
290 const gchar * const *names)
292 GSettingsBackendWatch *watch;
293 GSList *closures = NULL;
295 /* We're in a little bit of a tricky situation here. We need to hold
296 * a lock while traversing the list, but we don't want to hold the
297 * lock while calling back into user code.
299 * We work around this by creating a bunch of GSettingsBackendClosure
300 * objects while holding the lock and dispatching them after. We
301 * never touch the list without holding the lock.
303 g_mutex_lock (&backend->priv->lock);
304 for (watch = backend->priv->watches; watch; watch = watch->next)
306 GSettingsBackendClosure *closure;
308 closure = g_slice_new (GSettingsBackendClosure);
309 closure->context = watch->context;
310 if (closure->context)
311 g_main_context_ref (closure->context);
312 closure->backend = g_object_ref (backend);
313 closure->target_ref = g_new (GWeakRef, 1);
314 g_weak_ref_init (closure->target_ref, watch->target);
315 closure->function = G_STRUCT_MEMBER (void *, watch->vtable,
316 function_offset);
317 closure->name = g_strdup (name);
318 closure->origin_tag = origin_tag;
319 closure->names = g_strdupv ((gchar **) names);
321 closures = g_slist_prepend (closures, closure);
323 g_mutex_unlock (&backend->priv->lock);
325 while (closures)
327 GSettingsBackendClosure *closure = closures->data;
329 if (closure->context)
330 g_main_context_invoke (closure->context,
331 g_settings_backend_invoke_closure,
332 closure);
333 else
334 g_settings_backend_invoke_closure (closure);
336 closures = g_slist_delete_link (closures, closures);
341 * g_settings_backend_changed:
342 * @backend: a #GSettingsBackend implementation
343 * @key: the name of the key
344 * @origin_tag: the origin tag
346 * Signals that a single key has possibly changed. Backend
347 * implementations should call this if a key has possibly changed its
348 * value.
350 * @key must be a valid key (ie starting with a slash, not containing
351 * '//', and not ending with a slash).
353 * The implementation must call this function during any call to
354 * g_settings_backend_write(), before the call returns (except in the
355 * case that no keys are actually changed and it cares to detect this
356 * fact). It may not rely on the existence of a mainloop for
357 * dispatching the signal later.
359 * The implementation may call this function at any other time it likes
360 * in response to other events (such as changes occurring outside of the
361 * program). These calls may originate from a mainloop or may originate
362 * in response to any other action (including from calls to
363 * g_settings_backend_write()).
365 * In the case that this call is in response to a call to
366 * g_settings_backend_write() then @origin_tag must be set to the same
367 * value that was passed to that call.
369 * Since: 2.26
371 void
372 g_settings_backend_changed (GSettingsBackend *backend,
373 const gchar *key,
374 gpointer origin_tag)
376 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
377 g_return_if_fail (is_key (key));
379 g_settings_backend_dispatch_signal (backend,
380 G_STRUCT_OFFSET (GSettingsListenerVTable,
381 changed),
382 key, origin_tag, NULL);
386 * g_settings_backend_keys_changed:
387 * @backend: a #GSettingsBackend implementation
388 * @path: the path containing the changes
389 * @items: (array zero-terminated=1): the %NULL-terminated list of changed keys
390 * @origin_tag: the origin tag
392 * Signals that a list of keys have possibly changed. Backend
393 * implementations should call this if keys have possibly changed their
394 * values.
396 * @path must be a valid path (ie starting and ending with a slash and
397 * not containing '//'). Each string in @items must form a valid key
398 * name when @path is prefixed to it (ie: each item must not start or
399 * end with '/' and must not contain '//').
401 * The meaning of this signal is that any of the key names resulting
402 * from the contatenation of @path with each item in @items may have
403 * changed.
405 * The same rules for when notifications must occur apply as per
406 * g_settings_backend_changed(). These two calls can be used
407 * interchangeably if exactly one item has changed (although in that
408 * case g_settings_backend_changed() is definitely preferred).
410 * For efficiency reasons, the implementation should strive for @path to
411 * be as long as possible (ie: the longest common prefix of all of the
412 * keys that were changed) but this is not strictly required.
414 * Since: 2.26
416 void
417 g_settings_backend_keys_changed (GSettingsBackend *backend,
418 const gchar *path,
419 gchar const * const *items,
420 gpointer origin_tag)
422 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
423 g_return_if_fail (is_path (path));
425 /* XXX: should do stricter checking (ie: inspect each item) */
426 g_return_if_fail (items != NULL);
428 g_settings_backend_dispatch_signal (backend,
429 G_STRUCT_OFFSET (GSettingsListenerVTable,
430 keys_changed),
431 path, origin_tag, items);
435 * g_settings_backend_path_changed:
436 * @backend: a #GSettingsBackend implementation
437 * @path: the path containing the changes
438 * @origin_tag: the origin tag
440 * Signals that all keys below a given path may have possibly changed.
441 * Backend implementations should call this if an entire path of keys
442 * have possibly changed their values.
444 * @path must be a valid path (ie starting and ending with a slash and
445 * not containing '//').
447 * The meaning of this signal is that any of the key which has a name
448 * starting with @path may have changed.
450 * The same rules for when notifications must occur apply as per
451 * g_settings_backend_changed(). This call might be an appropriate
452 * reasponse to a 'reset' call but implementations are also free to
453 * explicitly list the keys that were affected by that call if they can
454 * easily do so.
456 * For efficiency reasons, the implementation should strive for @path to
457 * be as long as possible (ie: the longest common prefix of all of the
458 * keys that were changed) but this is not strictly required. As an
459 * example, if this function is called with the path of "/" then every
460 * single key in the application will be notified of a possible change.
462 * Since: 2.26
464 void
465 g_settings_backend_path_changed (GSettingsBackend *backend,
466 const gchar *path,
467 gpointer origin_tag)
469 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
470 g_return_if_fail (is_path (path));
472 g_settings_backend_dispatch_signal (backend,
473 G_STRUCT_OFFSET (GSettingsListenerVTable,
474 path_changed),
475 path, origin_tag, NULL);
479 * g_settings_backend_writable_changed:
480 * @backend: a #GSettingsBackend implementation
481 * @key: the name of the key
483 * Signals that the writability of a single key has possibly changed.
485 * Since GSettings performs no locking operations for itself, this call
486 * will always be made in response to external events.
488 * Since: 2.26
490 void
491 g_settings_backend_writable_changed (GSettingsBackend *backend,
492 const gchar *key)
494 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
495 g_return_if_fail (is_key (key));
497 g_settings_backend_dispatch_signal (backend,
498 G_STRUCT_OFFSET (GSettingsListenerVTable,
499 writable_changed),
500 key, NULL, NULL);
504 * g_settings_backend_path_writable_changed:
505 * @backend: a #GSettingsBackend implementation
506 * @path: the name of the path
508 * Signals that the writability of all keys below a given path may have
509 * changed.
511 * Since GSettings performs no locking operations for itself, this call
512 * will always be made in response to external events.
514 * Since: 2.26
516 void
517 g_settings_backend_path_writable_changed (GSettingsBackend *backend,
518 const gchar *path)
520 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
521 g_return_if_fail (is_path (path));
523 g_settings_backend_dispatch_signal (backend,
524 G_STRUCT_OFFSET (GSettingsListenerVTable,
525 path_writable_changed),
526 path, NULL, NULL);
529 typedef struct
531 const gchar **keys;
532 GVariant **values;
533 gint prefix_len;
534 gchar *prefix;
535 } FlattenState;
537 static gboolean
538 g_settings_backend_flatten_one (gpointer key,
539 gpointer value,
540 gpointer user_data)
542 FlattenState *state = user_data;
543 const gchar *skey = key;
544 gint i;
546 g_return_val_if_fail (is_key (key), TRUE);
548 /* calculate longest common prefix */
549 if (state->prefix == NULL)
551 gchar *last_byte;
553 /* first key? just take the prefix up to the last '/' */
554 state->prefix = g_strdup (skey);
555 last_byte = strrchr (state->prefix, '/') + 1;
556 state->prefix_len = last_byte - state->prefix;
557 *last_byte = '\0';
559 else
561 /* find the first character that does not match. we will
562 * definitely find one because the prefix ends in '/' and the key
563 * does not. also: no two keys in the tree are the same.
565 for (i = 0; state->prefix[i] == skey[i]; i++);
567 /* check if we need to shorten the prefix */
568 if (state->prefix[i] != '\0')
570 /* find the nearest '/', terminate after it */
571 while (state->prefix[i - 1] != '/')
572 i--;
574 state->prefix[i] = '\0';
575 state->prefix_len = i;
580 /* save the entire item into the array.
581 * the prefixes will be removed later.
583 *state->keys++ = key;
585 if (state->values)
586 *state->values++ = value;
588 return FALSE;
592 * g_settings_backend_flatten_tree:
593 * @tree: a #GTree containing the changes
594 * @path: (out): the location to save the path
595 * @keys: (out) (transfer container) (array zero-terminated=1): the
596 * location to save the relative keys
597 * @values: (out) (optional) (transfer container) (array zero-terminated=1):
598 * the location to save the values, or %NULL
600 * Calculate the longest common prefix of all keys in a tree and write
601 * out an array of the key names relative to that prefix and,
602 * optionally, the value to store at each of those keys.
604 * You must free the value returned in @path, @keys and @values using
605 * g_free(). You should not attempt to free or unref the contents of
606 * @keys or @values.
608 * Since: 2.26
610 void
611 g_settings_backend_flatten_tree (GTree *tree,
612 gchar **path,
613 const gchar ***keys,
614 GVariant ***values)
616 FlattenState state = { 0, };
617 gsize nnodes;
619 nnodes = g_tree_nnodes (tree);
621 *keys = state.keys = g_new (const gchar *, nnodes + 1);
622 state.keys[nnodes] = NULL;
624 if (values != NULL)
626 *values = state.values = g_new (GVariant *, nnodes + 1);
627 state.values[nnodes] = NULL;
630 g_tree_foreach (tree, g_settings_backend_flatten_one, &state);
631 g_return_if_fail (*keys + nnodes == state.keys);
633 *path = state.prefix;
634 while (nnodes--)
635 *--state.keys += state.prefix_len;
639 * g_settings_backend_changed_tree:
640 * @backend: a #GSettingsBackend implementation
641 * @tree: a #GTree containing the changes
642 * @origin_tag: the origin tag
644 * This call is a convenience wrapper. It gets the list of changes from
645 * @tree, computes the longest common prefix and calls
646 * g_settings_backend_changed().
648 * Since: 2.26
650 void
651 g_settings_backend_changed_tree (GSettingsBackend *backend,
652 GTree *tree,
653 gpointer origin_tag)
655 const gchar **keys;
656 gchar *path;
658 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
660 g_settings_backend_flatten_tree (tree, &path, &keys, NULL);
662 #ifdef DEBUG_CHANGES
664 gint i;
666 g_print ("----\n");
667 g_print ("changed_tree(): prefix %s\n", path);
668 for (i = 0; keys[i]; i++)
669 g_print (" %s\n", keys[i]);
670 g_print ("----\n");
672 #endif
674 g_settings_backend_keys_changed (backend, path, keys, origin_tag);
675 g_free (path);
676 g_free (keys);
679 /*< private >
680 * g_settings_backend_read:
681 * @backend: a #GSettingsBackend implementation
682 * @key: the key to read
683 * @expected_type: a #GVariantType
684 * @default_value: if the default value should be returned
686 * Reads a key. This call will never block.
688 * If the key exists, the value associated with it will be returned.
689 * If the key does not exist, %NULL will be returned.
691 * The returned value will be of the type given in @expected_type. If
692 * the backend stored a value of a different type then %NULL will be
693 * returned.
695 * If @default_value is %TRUE then this gets the default value from the
696 * backend (ie: the one that the backend would contain if
697 * g_settings_reset() were called).
699 * Returns: the value that was read, or %NULL
701 GVariant *
702 g_settings_backend_read (GSettingsBackend *backend,
703 const gchar *key,
704 const GVariantType *expected_type,
705 gboolean default_value)
707 GVariant *value;
709 value = G_SETTINGS_BACKEND_GET_CLASS (backend)
710 ->read (backend, key, expected_type, default_value);
712 if (value != NULL)
713 value = g_variant_take_ref (value);
715 if G_UNLIKELY (value && !g_variant_is_of_type (value, expected_type))
717 g_variant_unref (value);
718 value = NULL;
721 return value;
724 /*< private >
725 * g_settings_backend_read_user_value:
726 * @backend: a #GSettingsBackend implementation
727 * @key: the key to read
728 * @expected_type: a #GVariantType
730 * Reads the 'user value' of a key.
732 * This is the value of the key that the user has control over and has
733 * set for themselves. Put another way: if the user did not set the
734 * value for themselves, then this will return %NULL (even if the
735 * sysadmin has provided a default value).
737 * Returns: the value that was read, or %NULL
739 GVariant *
740 g_settings_backend_read_user_value (GSettingsBackend *backend,
741 const gchar *key,
742 const GVariantType *expected_type)
744 GVariant *value;
746 value = G_SETTINGS_BACKEND_GET_CLASS (backend)
747 ->read_user_value (backend, key, expected_type);
749 if (value != NULL)
750 value = g_variant_take_ref (value);
752 if G_UNLIKELY (value && !g_variant_is_of_type (value, expected_type))
754 g_variant_unref (value);
755 value = NULL;
758 return value;
761 /*< private >
762 * g_settings_backend_write:
763 * @backend: a #GSettingsBackend implementation
764 * @key: the name of the key
765 * @value: a #GVariant value to write to this key
766 * @origin_tag: the origin tag
768 * Writes exactly one key.
770 * This call does not fail. During this call a
771 * #GSettingsBackend::changed signal will be emitted if the value of the
772 * key has changed. The updated key value will be visible to any signal
773 * callbacks.
775 * One possible method that an implementation might deal with failures is
776 * to emit a second "changed" signal (either during this call, or later)
777 * to indicate that the affected keys have suddenly "changed back" to their
778 * old values.
780 * Returns: %TRUE if the write succeeded, %FALSE if the key was not writable
782 gboolean
783 g_settings_backend_write (GSettingsBackend *backend,
784 const gchar *key,
785 GVariant *value,
786 gpointer origin_tag)
788 gboolean success;
790 g_variant_ref_sink (value);
791 success = G_SETTINGS_BACKEND_GET_CLASS (backend)
792 ->write (backend, key, value, origin_tag);
793 g_variant_unref (value);
795 return success;
798 /*< private >
799 * g_settings_backend_write_tree:
800 * @backend: a #GSettingsBackend implementation
801 * @tree: a #GTree containing key-value pairs to write
802 * @origin_tag: the origin tag
804 * Writes one or more keys. This call will never block.
806 * The key of each item in the tree is the key name to write to and the
807 * value is a #GVariant to write. The proper type of #GTree for this
808 * call can be created with g_settings_backend_create_tree(). This call
809 * might take a reference to the tree; you must not modified the #GTree
810 * after passing it to this call.
812 * This call does not fail. During this call a #GSettingsBackend::changed
813 * signal will be emitted if any keys have been changed. The new values of
814 * all updated keys will be visible to any signal callbacks.
816 * One possible method that an implementation might deal with failures is
817 * to emit a second "changed" signal (either during this call, or later)
818 * to indicate that the affected keys have suddenly "changed back" to their
819 * old values.
821 gboolean
822 g_settings_backend_write_tree (GSettingsBackend *backend,
823 GTree *tree,
824 gpointer origin_tag)
826 return G_SETTINGS_BACKEND_GET_CLASS (backend)
827 ->write_tree (backend, tree, origin_tag);
830 /*< private >
831 * g_settings_backend_reset:
832 * @backend: a #GSettingsBackend implementation
833 * @key: the name of a key
834 * @origin_tag: the origin tag
836 * "Resets" the named key to its "default" value (ie: after system-wide
837 * defaults, mandatory keys, etc. have been taken into account) or possibly
838 * unsets it.
840 void
841 g_settings_backend_reset (GSettingsBackend *backend,
842 const gchar *key,
843 gpointer origin_tag)
845 G_SETTINGS_BACKEND_GET_CLASS (backend)
846 ->reset (backend, key, origin_tag);
849 /*< private >
850 * g_settings_backend_get_writable:
851 * @backend: a #GSettingsBackend implementation
852 * @key: the name of a key
854 * Finds out if a key is available for writing to. This is the
855 * interface through which 'lockdown' is implemented. Locked down
856 * keys will have %FALSE returned by this call.
858 * You should not write to locked-down keys, but if you do, the
859 * implementation will deal with it.
861 * Returns: %TRUE if the key is writable
863 gboolean
864 g_settings_backend_get_writable (GSettingsBackend *backend,
865 const gchar *key)
867 return G_SETTINGS_BACKEND_GET_CLASS (backend)
868 ->get_writable (backend, key);
871 /*< private >
872 * g_settings_backend_unsubscribe:
873 * @backend: a #GSettingsBackend
874 * @name: a key or path to subscribe to
876 * Reverses the effect of a previous call to
877 * g_settings_backend_subscribe().
879 void
880 g_settings_backend_unsubscribe (GSettingsBackend *backend,
881 const char *name)
883 G_SETTINGS_BACKEND_GET_CLASS (backend)
884 ->unsubscribe (backend, name);
887 /*< private >
888 * g_settings_backend_subscribe:
889 * @backend: a #GSettingsBackend
890 * @name: a key or path to subscribe to
892 * Requests that change signals be emitted for events on @name.
894 void
895 g_settings_backend_subscribe (GSettingsBackend *backend,
896 const gchar *name)
898 G_SETTINGS_BACKEND_GET_CLASS (backend)
899 ->subscribe (backend, name);
902 static void
903 g_settings_backend_finalize (GObject *object)
905 GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
907 g_mutex_clear (&backend->priv->lock);
909 G_OBJECT_CLASS (g_settings_backend_parent_class)
910 ->finalize (object);
913 static void
914 ignore_subscription (GSettingsBackend *backend,
915 const gchar *key)
919 static GVariant *
920 g_settings_backend_real_read_user_value (GSettingsBackend *backend,
921 const gchar *key,
922 const GVariantType *expected_type)
924 return g_settings_backend_read (backend, key, expected_type, FALSE);
927 static void
928 g_settings_backend_init (GSettingsBackend *backend)
930 backend->priv = g_settings_backend_get_instance_private (backend);
931 g_mutex_init (&backend->priv->lock);
934 static void
935 g_settings_backend_class_init (GSettingsBackendClass *class)
937 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
939 class->subscribe = ignore_subscription;
940 class->unsubscribe = ignore_subscription;
942 class->read_user_value = g_settings_backend_real_read_user_value;
944 gobject_class->finalize = g_settings_backend_finalize;
947 static void
948 g_settings_backend_variant_unref0 (gpointer data)
950 if (data != NULL)
951 g_variant_unref (data);
954 /*< private >
955 * g_settings_backend_create_tree:
957 * This is a convenience function for creating a tree that is compatible
958 * with g_settings_backend_write(). It merely calls g_tree_new_full()
959 * with strcmp(), g_free() and g_variant_unref().
961 * Returns: a new #GTree
963 GTree *
964 g_settings_backend_create_tree (void)
966 return g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
967 g_free, g_settings_backend_variant_unref0);
970 static gboolean
971 g_settings_backend_verify (gpointer impl)
973 GSettingsBackend *backend = impl;
975 if (strcmp (G_OBJECT_TYPE_NAME (backend), "GMemorySettingsBackend") == 0 &&
976 g_strcmp0 (g_getenv ("GSETTINGS_BACKEND"), "memory") != 0)
978 g_message ("Using the 'memory' GSettings backend. Your settings "
979 "will not be saved or shared with other applications.");
982 g_settings_has_backend = TRUE;
983 return TRUE;
987 * g_settings_backend_get_default:
989 * Returns the default #GSettingsBackend. It is possible to override
990 * the default by setting the `GSETTINGS_BACKEND` environment variable
991 * to the name of a settings backend.
993 * The user gets a reference to the backend.
995 * Returns: (transfer full): the default #GSettingsBackend
997 * Since: 2.28
999 GSettingsBackend *
1000 g_settings_backend_get_default (void)
1002 GSettingsBackend *backend;
1004 backend = _g_io_module_get_default (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
1005 "GSETTINGS_BACKEND",
1006 g_settings_backend_verify);
1007 return g_object_ref (backend);
1010 /*< private >
1011 * g_settings_backend_get_permission:
1012 * @backend: a #GSettingsBackend
1013 * @path: a path
1015 * Gets the permission object associated with writing to keys below
1016 * @path on @backend.
1018 * If this is not implemented in the backend, then a %TRUE
1019 * #GSimplePermission is returned.
1021 * Returns: a non-%NULL #GPermission. Free with g_object_unref()
1023 GPermission *
1024 g_settings_backend_get_permission (GSettingsBackend *backend,
1025 const gchar *path)
1027 GSettingsBackendClass *class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1029 if (class->get_permission)
1030 return class->get_permission (backend, path);
1032 return g_simple_permission_new (TRUE);
1035 /*< private >
1036 * g_settings_backend_sync_default:
1038 * Syncs the default backend.
1040 void
1041 g_settings_backend_sync_default (void)
1043 if (g_settings_has_backend)
1045 GSettingsBackendClass *class;
1046 GSettingsBackend *backend;
1048 backend = g_settings_backend_get_default ();
1049 class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1051 if (class->sync)
1052 class->sync (backend);