Add some more test about gdbus_error apis
[glib.git] / gio / gsettingsbackend.c
blobb1e6cff4c0799e987420d6f6197863e549292291
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 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>
24 #include "config.h"
26 #include "gsettingsbackendinternal.h"
27 #include "gnullsettingsbackend.h"
28 #include "gsimplepermission.h"
29 #include "giomodule-priv.h"
30 #include "gio-marshal.h"
32 #include <string.h>
33 #include <stdlib.h>
34 #include <glib.h>
35 #include <glibintl.h>
38 G_DEFINE_ABSTRACT_TYPE (GSettingsBackend, g_settings_backend, G_TYPE_OBJECT)
40 typedef struct _GSettingsBackendClosure GSettingsBackendClosure;
41 typedef struct _GSettingsBackendWatch GSettingsBackendWatch;
43 struct _GSettingsBackendPrivate
45 GSettingsBackendWatch *watches;
46 GStaticMutex lock;
49 /**
50 * SECTION:gsettingsbackend
51 * @title: GSettingsBackend
52 * @short_description: an interface for settings backend implementations
53 * @include: gio/gsettingsbackend.h
54 * @see_also: #GSettings, #GIOExtensionPoint
56 * The #GSettingsBackend interface defines a generic interface for
57 * non-strictly-typed data that is stored in a hierarchy. To implement
58 * an alternative storage backend for #GSettings, you need to implement
59 * the #GSettingsBackend interface and then make it implement the
60 * extension point #G_SETTINGS_BACKEND_EXTENSION_POINT_NAME.
62 * The interface defines methods for reading and writing values, a
63 * method for determining if writing of certain values will fail
64 * (lockdown) and a change notification mechanism.
66 * The semantics of the interface are very precisely defined and
67 * implementations must carefully adhere to the expectations of
68 * callers that are documented on each of the interface methods.
70 * Some of the GSettingsBackend functions accept or return a #GTree.
71 * These trees always have strings as keys and #GVariant as values.
72 * g_settings_backend_create_tree() is a convenience function to create
73 * suitable trees.
75 * <note><para>
76 * The #GSettingsBackend API is exported to allow third-party
77 * implementations, but does not carry the same stability guarantees
78 * as the public GIO API. For this reason, you have to define the
79 * C preprocessor symbol #G_SETTINGS_ENABLE_BACKEND before including
80 * <filename>gio/gsettingsbackend.h</filename>
81 * </para></note>
82 **/
84 static gboolean
85 is_key (const gchar *key)
87 gint length;
88 gint i;
90 g_return_val_if_fail (key != NULL, FALSE);
91 g_return_val_if_fail (key[0] == '/', FALSE);
93 for (i = 1; key[i]; i++)
94 g_return_val_if_fail (key[i] != '/' || key[i + 1] != '/', FALSE);
96 length = i;
98 g_return_val_if_fail (key[length - 1] != '/', FALSE);
100 return TRUE;
103 static gboolean
104 is_path (const gchar *path)
106 gint length;
107 gint i;
109 g_return_val_if_fail (path != NULL, FALSE);
110 g_return_val_if_fail (path[0] == '/', FALSE);
112 for (i = 1; path[i]; i++)
113 g_return_val_if_fail (path[i] != '/' || path[i + 1] != '/', FALSE);
115 length = i;
117 g_return_val_if_fail (path[length - 1] == '/', FALSE);
119 return TRUE;
122 GMainContext *
123 g_settings_backend_get_active_context (void)
125 GMainContext *context;
126 GSource *source;
128 if ((source = g_main_current_source ()))
129 context = g_source_get_context (source);
131 else
133 context = g_main_context_get_thread_default ();
135 if (context == NULL)
136 context = g_main_context_default ();
139 return context;
142 struct _GSettingsBackendWatch
144 GObject *target;
145 GMainContext *context;
146 GSettingsBackendChangedFunc changed;
147 GSettingsBackendPathChangedFunc path_changed;
148 GSettingsBackendKeysChangedFunc keys_changed;
149 GSettingsBackendWritableChangedFunc writable_changed;
150 GSettingsBackendPathWritableChangedFunc path_writable_changed;
152 GSettingsBackendWatch *next;
155 struct _GSettingsBackendClosure
157 void (*function) (GSettingsBackend *backend,
158 GObject *target,
159 const gchar *name,
160 gpointer data1,
161 gpointer data2);
163 GSettingsBackend *backend;
164 GObject *target;
165 gchar *name;
166 gpointer data1;
167 GBoxedFreeFunc data1_free;
168 gpointer data2;
171 static void
172 g_settings_backend_watch_weak_notify (gpointer data,
173 GObject *where_the_object_was)
175 GSettingsBackend *backend = data;
176 GSettingsBackendWatch **ptr;
178 /* search and remove */
179 g_static_mutex_lock (&backend->priv->lock);
180 for (ptr = &backend->priv->watches; *ptr; ptr = &(*ptr)->next)
181 if ((*ptr)->target == where_the_object_was)
183 GSettingsBackendWatch *tmp = *ptr;
185 *ptr = tmp->next;
186 g_slice_free (GSettingsBackendWatch, tmp);
188 g_static_mutex_unlock (&backend->priv->lock);
189 return;
192 /* we didn't find it. that shouldn't happen. */
193 g_assert_not_reached ();
196 /*< private >
197 * g_settings_backend_watch:
198 * @backend: a #GSettingsBackend
199 * @target: the GObject (typically GSettings instance) to call back to
200 * @context: a #GMainContext, or %NULL
201 * ...: callbacks...
203 * Registers a new watch on a #GSettingsBackend.
205 * note: %NULL @context does not mean "default main context" but rather,
206 * "it is okay to dispatch in any context". If the default main context
207 * is specifically desired then it must be given.
209 * note also: if you want to get meaningful values for the @origin_tag
210 * that appears as an argument to some of the callbacks, you *must* have
211 * @context as %NULL. Otherwise, you are subject to cross-thread
212 * dispatching and whatever owned @origin_tag at the time that the event
213 * occured may no longer own it. This is a problem if you consider that
214 * you may now be the new owner of that address and mistakenly think
215 * that the event in question originated from yourself.
217 * tl;dr: If you give a non-%NULL @context then you must ignore the
218 * value of @origin_tag given to any callbacks.
220 void
221 g_settings_backend_watch (GSettingsBackend *backend,
222 GObject *target,
223 GMainContext *context,
224 GSettingsBackendChangedFunc changed,
225 GSettingsBackendPathChangedFunc path_changed,
226 GSettingsBackendKeysChangedFunc keys_changed,
227 GSettingsBackendWritableChangedFunc writable_changed,
228 GSettingsBackendPathWritableChangedFunc path_writable_changed)
230 GSettingsBackendWatch *watch;
232 /* For purposes of discussion, we assume that our target is a
233 * GSettings instance.
235 * Our strategy to defend against the final reference dropping on the
236 * GSettings object in a thread other than the one that is doing the
237 * dispatching is as follows:
239 * 1) hold a GObject reference on the GSettings during an outstanding
240 * dispatch. This ensures that the delivery is always possible.
242 * 2) hold a weak reference on the GSettings at other times. This
243 * allows us to receive early notification of pending destruction
244 * of the object. At this point, it is still safe to obtain a
245 * reference on the GObject to keep it alive, so #1 will work up
246 * to that point. After that point, we'll have been able to drop
247 * the watch from the list.
249 * Note, in particular, that it's not possible to simply have an
250 * "unwatch" function that gets called from the finalize function of
251 * the GSettings instance because, by that point it is no longer
252 * possible to keep the object alive using g_object_ref() and we would
253 * have no way of knowing this.
255 * Note also that we do not need to hold a reference on the main
256 * context here since the GSettings instance does that for us and we
257 * will receive the weak notify long before it is dropped. We don't
258 * even need to hold it during dispatches because our reference on the
259 * GSettings will prevent the finalize from running and dropping the
260 * ref on the context.
262 * All access to the list holds a mutex. We have some strategies to
263 * avoid some of the pain that would be associated with that.
266 watch = g_slice_new (GSettingsBackendWatch);
267 watch->context = context;
268 watch->target = target;
269 g_object_weak_ref (target, g_settings_backend_watch_weak_notify, backend);
271 watch->changed = changed;
272 watch->path_changed = path_changed;
273 watch->keys_changed = keys_changed;
274 watch->writable_changed = writable_changed;
275 watch->path_writable_changed = path_writable_changed;
277 /* linked list prepend */
278 g_static_mutex_lock (&backend->priv->lock);
279 watch->next = backend->priv->watches;
280 backend->priv->watches = watch;
281 g_static_mutex_unlock (&backend->priv->lock);
284 void
285 g_settings_backend_unwatch (GSettingsBackend *backend,
286 GObject *target)
288 /* Our caller surely owns a reference on 'target', so the order of
289 * these two calls is unimportant.
291 g_object_weak_unref (target, g_settings_backend_watch_weak_notify, backend);
292 g_settings_backend_watch_weak_notify (backend, target);
295 static gboolean
296 g_settings_backend_invoke_closure (gpointer user_data)
298 GSettingsBackendClosure *closure = user_data;
300 closure->function (closure->backend, closure->target, closure->name,
301 closure->data1, closure->data2);
303 closure->data1_free (closure->data1);
304 g_object_unref (closure->backend);
305 g_object_unref (closure->target);
306 g_free (closure->name);
308 g_slice_free (GSettingsBackendClosure, closure);
310 return FALSE;
313 static gpointer
314 pointer_id (gpointer a)
316 return a;
319 static void
320 pointer_ignore (gpointer a)
324 static void
325 g_settings_backend_dispatch_signal (GSettingsBackend *backend,
326 gsize function_offset,
327 const gchar *name,
328 gpointer data1,
329 GBoxedCopyFunc data1_copy,
330 GBoxedFreeFunc data1_free,
331 gpointer data2)
333 GMainContext *context, *here_and_now;
334 GSettingsBackendWatch *watch;
336 /* We need to hold the mutex here (to prevent a node from being
337 * deleted as we are traversing the list). Since we should not
338 * re-enter user code while holding this mutex, we create a
339 * one-time-use GMainContext and populate it with the events that we
340 * would have called directly. We dispatch these events after
341 * releasing the lock. Note that the GObject reference is acquired on
342 * the target while holding the mutex and the mutex needs to be held
343 * as part of the destruction of any GSettings instance (via the weak
344 * reference handling). This is the key to the safety of the whole
345 * setup.
348 if (data1_copy == NULL)
349 data1_copy = pointer_id;
351 if (data1_free == NULL)
352 data1_free = pointer_ignore;
354 context = g_settings_backend_get_active_context ();
355 here_and_now = g_main_context_new ();
357 /* traverse the (immutable while holding lock) list */
358 g_static_mutex_lock (&backend->priv->lock);
359 for (watch = backend->priv->watches; watch; watch = watch->next)
361 GSettingsBackendClosure *closure;
362 GSource *source;
364 closure = g_slice_new (GSettingsBackendClosure);
365 closure->backend = g_object_ref (backend);
366 closure->target = g_object_ref (watch->target);
367 closure->function = G_STRUCT_MEMBER (void *, watch, function_offset);
368 closure->name = g_strdup (name);
369 closure->data1 = data1_copy (data1);
370 closure->data1_free = data1_free;
371 closure->data2 = data2;
373 source = g_idle_source_new ();
374 g_source_set_priority (source, G_PRIORITY_DEFAULT);
375 g_source_set_callback (source,
376 g_settings_backend_invoke_closure,
377 closure, NULL);
379 if (watch->context && watch->context != context)
380 g_source_attach (source, watch->context);
381 else
382 g_source_attach (source, here_and_now);
384 g_source_unref (source);
386 g_static_mutex_unlock (&backend->priv->lock);
388 while (g_main_context_iteration (here_and_now, FALSE));
389 g_main_context_unref (here_and_now);
393 * g_settings_backend_changed:
394 * @backend: a #GSettingsBackend implementation
395 * @key: the name of the key
396 * @origin_tag: the origin tag
398 * Signals that a single key has possibly changed. Backend
399 * implementations should call this if a key has possibly changed its
400 * value.
402 * @key must be a valid key (ie: starting with a slash, not containing
403 * '//', and not ending with a slash).
405 * The implementation must call this function during any call to
406 * g_settings_backend_write(), before the call returns (except in the
407 * case that no keys are actually changed and it cares to detect this
408 * fact). It may not rely on the existence of a mainloop for
409 * dispatching the signal later.
411 * The implementation may call this function at any other time it likes
412 * in response to other events (such as changes occuring outside of the
413 * program). These calls may originate from a mainloop or may originate
414 * in response to any other action (including from calls to
415 * g_settings_backend_write()).
417 * In the case that this call is in response to a call to
418 * g_settings_backend_write() then @origin_tag must be set to the same
419 * value that was passed to that call.
421 * Since: 2.26
423 void
424 g_settings_backend_changed (GSettingsBackend *backend,
425 const gchar *key,
426 gpointer origin_tag)
428 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
429 g_return_if_fail (is_key (key));
431 g_settings_backend_dispatch_signal (backend,
432 G_STRUCT_OFFSET (GSettingsBackendWatch,
433 changed),
434 key, origin_tag, NULL, NULL, NULL);
438 * g_settings_backend_keys_changed:
439 * @backend: a #GSettingsBackend implementation
440 * @path: the path containing the changes
441 * @items: the %NULL-terminated list of changed keys
442 * @origin_tag: the origin tag
444 * Signals that a list of keys have possibly changed. Backend
445 * implementations should call this if keys have possibly changed their
446 * values.
448 * @path must be a valid path (ie: starting and ending with a slash and
449 * not containing '//'). Each string in @items must form a valid key
450 * name when @path is prefixed to it (ie: each item must not start or
451 * end with '/' and must not contain '//').
453 * The meaning of this signal is that any of the key names resulting
454 * from the contatenation of @path with each item in @items may have
455 * changed.
457 * The same rules for when notifications must occur apply as per
458 * g_settings_backend_changed(). These two calls can be used
459 * interchangeably if exactly one item has changed (although in that
460 * case g_settings_backend_changed() is definitely preferred).
462 * For efficiency reasons, the implementation should strive for @path to
463 * be as long as possible (ie: the longest common prefix of all of the
464 * keys that were changed) but this is not strictly required.
466 * Since: 2.26
468 void
469 g_settings_backend_keys_changed (GSettingsBackend *backend,
470 const gchar *path,
471 gchar const * const *items,
472 gpointer origin_tag)
474 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
475 g_return_if_fail (is_path (path));
477 /* XXX: should do stricter checking (ie: inspect each item) */
478 g_return_if_fail (items != NULL);
480 g_settings_backend_dispatch_signal (backend,
481 G_STRUCT_OFFSET (GSettingsBackendWatch,
482 keys_changed),
483 path, (gpointer) items,
484 (GBoxedCopyFunc) g_strdupv,
485 (GBoxedFreeFunc) g_strfreev,
486 origin_tag);
490 * g_settings_backend_path_changed:
491 * @backend: a #GSettingsBackend implementation
492 * @path: the path containing the changes
493 * @origin_tag: the origin tag
495 * Signals that all keys below a given path may have possibly changed.
496 * Backend implementations should call this if an entire path of keys
497 * have possibly changed their values.
499 * @path must be a valid path (ie: starting and ending with a slash and
500 * not containing '//').
502 * The meaning of this signal is that any of the key which has a name
503 * starting with @path may have changed.
505 * The same rules for when notifications must occur apply as per
506 * g_settings_backend_changed(). This call might be an appropriate
507 * reasponse to a 'reset' call but implementations are also free to
508 * explicitly list the keys that were affected by that call if they can
509 * easily do so.
511 * For efficiency reasons, the implementation should strive for @path to
512 * be as long as possible (ie: the longest common prefix of all of the
513 * keys that were changed) but this is not strictly required. As an
514 * example, if this function is called with the path of "/" then every
515 * single key in the application will be notified of a possible change.
517 * Since: 2.26
519 void
520 g_settings_backend_path_changed (GSettingsBackend *backend,
521 const gchar *path,
522 gpointer origin_tag)
524 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
525 g_return_if_fail (is_path (path));
527 g_settings_backend_dispatch_signal (backend,
528 G_STRUCT_OFFSET (GSettingsBackendWatch,
529 path_changed),
530 path, origin_tag, NULL, NULL, NULL);
534 * g_settings_backend_writable_changed:
535 * @backend: a #GSettingsBackend implementation
536 * @key: the name of the key
538 * Signals that the writability of a single key has possibly changed.
540 * Since GSettings performs no locking operations for itself, this call
541 * will always be made in response to external events.
543 * Since: 2.26
545 void
546 g_settings_backend_writable_changed (GSettingsBackend *backend,
547 const gchar *key)
549 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
550 g_return_if_fail (is_key (key));
552 g_settings_backend_dispatch_signal (backend,
553 G_STRUCT_OFFSET (GSettingsBackendWatch,
554 writable_changed),
555 key, NULL, NULL, NULL, NULL);
559 * g_settings_backend_path_writable_changed:
560 * @backend: a #GSettingsBackend implementation
561 * @path: the name of the path
563 * Signals that the writability of all keys below a given path may have
564 * changed.
566 * Since GSettings performs no locking operations for itself, this call
567 * will always be made in response to external events.
569 * Since: 2.26
571 void
572 g_settings_backend_path_writable_changed (GSettingsBackend *backend,
573 const gchar *path)
575 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
576 g_return_if_fail (is_path (path));
578 g_settings_backend_dispatch_signal (backend,
579 G_STRUCT_OFFSET (GSettingsBackendWatch,
580 path_writable_changed),
581 path, NULL, NULL, NULL, NULL);
584 typedef struct
586 const gchar **keys;
587 GVariant **values;
588 gint prefix_len;
589 gchar *prefix;
590 } FlattenState;
592 static gboolean
593 g_settings_backend_flatten_one (gpointer key,
594 gpointer value,
595 gpointer user_data)
597 FlattenState *state = user_data;
598 const gchar *skey = key;
599 gint i;
601 g_return_val_if_fail (is_key (key), TRUE);
603 /* calculate longest common prefix */
604 if (state->prefix == NULL)
606 gchar *last_byte;
608 /* first key? just take the prefix up to the last '/' */
609 state->prefix = g_strdup (skey);
610 last_byte = strrchr (state->prefix, '/') + 1;
611 state->prefix_len = last_byte - state->prefix;
612 *last_byte = '\0';
614 else
616 /* find the first character that does not match. we will
617 * definitely find one because the prefix ends in '/' and the key
618 * does not. also: no two keys in the tree are the same.
620 for (i = 0; state->prefix[i] == skey[i]; i++);
622 /* check if we need to shorten the prefix */
623 if (state->prefix[i] != '\0')
625 /* find the nearest '/', terminate after it */
626 while (state->prefix[i - 1] != '/')
627 i--;
629 state->prefix[i] = '\0';
630 state->prefix_len = i;
635 /* save the entire item into the array.
636 * the prefixes will be removed later.
638 *state->keys++ = key;
640 if (state->values)
641 *state->values++ = value;
643 return FALSE;
647 * g_settings_backend_flatten_tree:
648 * @tree: a #GTree containing the changes
649 * @path: the location to save the path
650 * @keys: the location to save the relative keys
651 * @values: the location to save the values, or %NULL
653 * Calculate the longest common prefix of all keys in a tree and write
654 * out an array of the key names relative to that prefix and,
655 * optionally, the value to store at each of those keys.
657 * You must free the value returned in @path, @keys and @values using
658 * g_free(). You should not attempt to free or unref the contents of
659 * @keys or @values.
661 * Since: 2.26
663 void
664 g_settings_backend_flatten_tree (GTree *tree,
665 gchar **path,
666 const gchar ***keys,
667 GVariant ***values)
669 FlattenState state = { 0, };
670 gsize nnodes;
672 nnodes = g_tree_nnodes (tree);
674 *keys = state.keys = g_new (const gchar *, nnodes + 1);
675 state.keys[nnodes] = NULL;
677 if (values != NULL)
679 *values = state.values = g_new (GVariant *, nnodes + 1);
680 state.values[nnodes] = NULL;
683 g_tree_foreach (tree, g_settings_backend_flatten_one, &state);
684 g_return_if_fail (*keys + nnodes == state.keys);
686 *path = state.prefix;
687 while (nnodes--)
688 *--state.keys += state.prefix_len;
692 * g_settings_backend_changed_tree:
693 * @backend: a #GSettingsBackend implementation
694 * @tree: a #GTree containing the changes
695 * @origin_tag: the origin tag
697 * This call is a convenience wrapper. It gets the list of changes from
698 * @tree, computes the longest common prefix and calls
699 * g_settings_backend_changed().
701 * Since: 2.26
703 void
704 g_settings_backend_changed_tree (GSettingsBackend *backend,
705 GTree *tree,
706 gpointer origin_tag)
708 GSettingsBackendWatch *watch;
709 const gchar **keys;
710 gchar *path;
712 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
714 g_settings_backend_flatten_tree (tree, &path, &keys, NULL);
716 #ifdef DEBUG_CHANGES
718 gint i;
720 g_print ("----\n");
721 g_print ("changed_tree(): prefix %s\n", path);
722 for (i = 0; keys[i]; i++)
723 g_print (" %s\n", keys[i]);
724 g_print ("----\n");
726 #endif
728 for (watch = backend->priv->watches; watch; watch = watch->next)
729 watch->keys_changed (backend, watch->target, path, keys, origin_tag);
731 g_free (path);
732 g_free (keys);
735 /*< private >
736 * g_settings_backend_read:
737 * @backend: a #GSettingsBackend implementation
738 * @key: the key to read
739 * @expected_type: a #GVariantType
740 * @default_value: if the default value should be returned
741 * @returns: the value that was read, or %NULL
743 * Reads a key. This call will never block.
745 * If the key exists, the value associated with it will be returned.
746 * If the key does not exist, %NULL will be returned.
748 * The returned value will be of the type given in @expected_type. If
749 * the backend stored a value of a different type then %NULL will be
750 * returned.
752 * If @default_value is %TRUE then this gets the default value from the
753 * backend (ie: the one that the backend would contain if
754 * g_settings_reset() were called).
756 GVariant *
757 g_settings_backend_read (GSettingsBackend *backend,
758 const gchar *key,
759 const GVariantType *expected_type,
760 gboolean default_value)
762 GVariant *value;
764 value = G_SETTINGS_BACKEND_GET_CLASS (backend)
765 ->read (backend, key, expected_type, default_value);
767 if G_UNLIKELY (value && !g_variant_is_of_type (value, expected_type))
769 g_variant_unref (value);
770 value = NULL;
773 return value;
776 /*< private >
777 * g_settings_backend_write:
778 * @backend: a #GSettingsBackend implementation
779 * @key: the name of the key
780 * @value: a #GVariant value to write to this key
781 * @origin_tag: the origin tag
782 * @returns: %TRUE if the write succeeded, %FALSE if the key was not writable
784 * Writes exactly one key.
786 * This call does not fail. During this call a
787 * #GSettingsBackend::changed signal will be emitted if the value of the
788 * key has changed. The updated key value will be visible to any signal
789 * callbacks.
791 * One possible method that an implementation might deal with failures is
792 * to emit a second "changed" signal (either during this call, or later)
793 * to indicate that the affected keys have suddenly "changed back" to their
794 * old values.
796 gboolean
797 g_settings_backend_write (GSettingsBackend *backend,
798 const gchar *key,
799 GVariant *value,
800 gpointer origin_tag)
802 return G_SETTINGS_BACKEND_GET_CLASS (backend)
803 ->write (backend, key, value, origin_tag);
806 /*< private >
807 * g_settings_backend_write_keys:
808 * @backend: a #GSettingsBackend implementation
809 * @values: a #GTree containing key-value pairs to write
810 * @origin_tag: the origin tag
812 * Writes one or more keys. This call will never block.
814 * The key of each item in the tree is the key name to write to and the
815 * value is a #GVariant to write. The proper type of #GTree for this
816 * call can be created with g_settings_backend_create_tree(). This call
817 * might take a reference to the tree; you must not modified the #GTree
818 * after passing it to this call.
820 * This call does not fail. During this call a #GSettingsBackend::changed
821 * signal will be emitted if any keys have been changed. The new values of
822 * all updated keys will be visible to any signal callbacks.
824 * One possible method that an implementation might deal with failures is
825 * to emit a second "changed" signal (either during this call, or later)
826 * to indicate that the affected keys have suddenly "changed back" to their
827 * old values.
829 gboolean
830 g_settings_backend_write_tree (GSettingsBackend *backend,
831 GTree *tree,
832 gpointer origin_tag)
834 return G_SETTINGS_BACKEND_GET_CLASS (backend)
835 ->write_tree (backend, tree, origin_tag);
838 /*< private >
839 * g_settings_backend_reset:
840 * @backend: a #GSettingsBackend implementation
841 * @key: the name of a key
842 * @origin_tag: the origin tag
844 * "Resets" the named key to its "default" value (ie: after system-wide
845 * defaults, mandatory keys, etc. have been taken into account) or possibly
846 * unsets it.
848 void
849 g_settings_backend_reset (GSettingsBackend *backend,
850 const gchar *key,
851 gpointer origin_tag)
853 G_SETTINGS_BACKEND_GET_CLASS (backend)
854 ->reset (backend, key, origin_tag);
857 /*< private >
858 * g_settings_backend_get_writable:
859 * @backend: a #GSettingsBackend implementation
860 * @key: the name of a key
861 * @returns: %TRUE if the key is writable
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 gboolean
871 g_settings_backend_get_writable (GSettingsBackend *backend,
872 const gchar *key)
874 return G_SETTINGS_BACKEND_GET_CLASS (backend)
875 ->get_writable (backend, key);
878 /*< private >
879 * g_settings_backend_unsubscribe:
880 * @backend: a #GSettingsBackend
881 * @name: a key or path to subscribe to
883 * Reverses the effect of a previous call to
884 * g_settings_backend_subscribe().
886 void
887 g_settings_backend_unsubscribe (GSettingsBackend *backend,
888 const char *name)
890 G_SETTINGS_BACKEND_GET_CLASS (backend)
891 ->unsubscribe (backend, name);
894 /*< private >
895 * g_settings_backend_subscribe:
896 * @backend: a #GSettingsBackend
897 * @name: a key or path to subscribe to
899 * Requests that change signals be emitted for events on @name.
901 void
902 g_settings_backend_subscribe (GSettingsBackend *backend,
903 const gchar *name)
905 G_SETTINGS_BACKEND_GET_CLASS (backend)
906 ->subscribe (backend, name);
909 static void
910 g_settings_backend_finalize (GObject *object)
912 GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
914 g_static_mutex_unlock (&backend->priv->lock);
916 G_OBJECT_CLASS (g_settings_backend_parent_class)
917 ->finalize (object);
920 static void
921 ignore_subscription (GSettingsBackend *backend,
922 const gchar *key)
926 static void
927 g_settings_backend_init (GSettingsBackend *backend)
929 backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend,
930 G_TYPE_SETTINGS_BACKEND,
931 GSettingsBackendPrivate);
932 g_static_mutex_init (&backend->priv->lock);
935 static void
936 g_settings_backend_class_init (GSettingsBackendClass *class)
938 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
940 class->subscribe = ignore_subscription;
941 class->unsubscribe = ignore_subscription;
943 gobject_class->finalize = g_settings_backend_finalize;
945 g_type_class_add_private (class, sizeof (GSettingsBackendPrivate));
948 /*< private >
949 * g_settings_backend_create_tree:
950 * @returns: a new #GTree
952 * This is a convenience function for creating a tree that is compatible
953 * with g_settings_backend_write(). It merely calls g_tree_new_full()
954 * with strcmp(), g_free() and g_variant_unref().
956 GTree *
957 g_settings_backend_create_tree (void)
959 return g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
960 g_free, (GDestroyNotify) g_variant_unref);
963 /*< private >
964 * g_settings_backend_get_default:
965 * @returns: the default #GSettingsBackend
967 * Returns the default #GSettingsBackend. It is possible to override
968 * the default by setting the <envar>GSETTINGS_BACKEND</envar>
969 * environment variable to the name of a settings backend.
971 * The user gets a reference to the backend.
973 GSettingsBackend *
974 g_settings_backend_get_default (void)
976 static gsize backend;
978 if (g_once_init_enter (&backend))
980 GSettingsBackend *instance;
981 GIOExtensionPoint *point;
982 GIOExtension *extension;
983 GType extension_type;
984 GList *extensions;
985 const gchar *env;
987 _g_io_modules_ensure_loaded ();
989 point = g_io_extension_point_lookup (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME);
990 extension = NULL;
992 if ((env = getenv ("GSETTINGS_BACKEND")))
994 extension = g_io_extension_point_get_extension_by_name (point, env);
996 if (extension == NULL)
997 g_warning ("Can't find GSettings backend '%s' given in "
998 "GSETTINGS_BACKEND environment variable", env);
1001 if (extension == NULL)
1003 extensions = g_io_extension_point_get_extensions (point);
1005 if (extensions == NULL)
1006 g_error ("No GSettingsBackend implementations exist.");
1008 extension = extensions->data;
1010 if (strcmp (g_io_extension_get_name (extension), "memory") == 0)
1011 g_message ("Using the 'memory' GSettings backend. Your settings "
1012 "will not be saved or shared with other applications.");
1015 extension_type = g_io_extension_get_type (extension);
1016 instance = g_object_new (extension_type, NULL);
1018 g_once_init_leave (&backend, (gsize) instance);
1021 return g_object_ref ((void *) backend);
1024 /*< private >
1025 * g_settings_backend_get_permission:
1026 * @backend: a #GSettingsBackend
1027 * @path: a path
1028 * @returns: a non-%NULL #GPermission. Free with g_object_unref()
1030 * Gets the permission object associated with writing to keys below
1031 * @path on @backend.
1033 * If this is not implemented in the backend, then a %TRUE
1034 * #GSimplePermission is returned.
1036 GPermission *
1037 g_settings_backend_get_permission (GSettingsBackend *backend,
1038 const gchar *path)
1040 GSettingsBackendClass *class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1042 if (class->get_permission)
1043 return class->get_permission (backend, path);
1045 return g_simple_permission_new (TRUE);
1048 /*< private >
1049 * g_settings_backend_sync_default:
1051 * Syncs the default backend.
1053 void
1054 g_settings_backend_sync_default (void)
1056 GSettingsBackendClass *class;
1057 GSettingsBackend *backend;
1059 backend = g_settings_backend_get_default ();
1060 class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1062 if (class->sync)
1063 class->sync (backend);