GSettings: remove "context" from backend too.
[platform/upstream/glib.git] / gio / gsettingsbackend.c
1 /*
2  * Copyright © 2009, 2010 Codethink Limited
3  * Copyright © 2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Authors: Ryan Lortie <desrt@desrt.ca>
21  *          Matthias Clasen <mclasen@redhat.com>
22  */
23
24 #include "config.h"
25
26 #include "gsettingsbackendinternal.h"
27 #include "gnullsettingsbackend.h"
28 #include "gsimplepermission.h"
29 #include "giomodule-priv.h"
30 #include "gio-marshal.h"
31
32 #include <string.h>
33 #include <stdlib.h>
34 #include <glib.h>
35 #include <glibintl.h>
36
37 #include "gioalias.h"
38
39 G_DEFINE_ABSTRACT_TYPE (GSettingsBackend, g_settings_backend, G_TYPE_OBJECT)
40
41 typedef struct _GSettingsBackendClosure GSettingsBackendClosure;
42 typedef struct _GSettingsBackendWatch   GSettingsBackendWatch;
43
44 struct _GSettingsBackendPrivate
45 {
46   GSettingsBackendWatch *watches;
47   GStaticMutex lock;
48 };
49
50 /**
51  * SECTION:gsettingsbackend
52  * @title: GSettingsBackend
53  * @short_description: an interface for settings backend implementations
54  * @include: gio/gsettingsbackend.h
55  * @see_also: #GSettings, #GIOExtensionPoint
56  *
57  * The #GSettingsBackend interface defines a generic interface for
58  * non-strictly-typed data that is stored in a hierarchy. To implement
59  * an alternative storage backend for #GSettings, you need to implement
60  * the #GSettingsBackend interface and then make it implement the
61  * extension point #G_SETTINGS_BACKEND_EXTENSION_POINT_NAME.
62  *
63  * The interface defines methods for reading and writing values, a
64  * method for determining if writing of certain values will fail
65  * (lockdown) and a change notification mechanism.
66  *
67  * The semantics of the interface are very precisely defined and
68  * implementations must carefully adhere to the expectations of
69  * callers that are documented on each of the interface methods.
70  *
71  * Some of the GSettingsBackend functions accept or return a #GTree.
72  * These trees always have strings as keys and #GVariant as values.
73  * g_settings_backend_create_tree() is a convenience function to create
74  * suitable trees.
75  *
76  * <note><para>
77  * The #GSettingsBackend API is exported to allow third-party
78  * implementations, but does not carry the same stability guarantees
79  * as the public GIO API. For this reason, you have to define the
80  * C preprocessor symbol #G_SETTINGS_ENABLE_BACKEND before including
81  * <filename>gio/gsettingsbackend.h</filename>
82  * </para></note>
83  **/
84
85 static gboolean
86 is_key (const gchar *key)
87 {
88   gint length;
89   gint i;
90
91   g_return_val_if_fail (key != NULL, FALSE);
92   g_return_val_if_fail (key[0] == '/', FALSE);
93
94   for (i = 1; key[i]; i++)
95     g_return_val_if_fail (key[i] != '/' || key[i + 1] != '/', FALSE);
96
97   length = i;
98
99   g_return_val_if_fail (key[length - 1] != '/', FALSE);
100
101   return TRUE;
102 }
103
104 static gboolean
105 is_path (const gchar *path)
106 {
107   gint length;
108   gint i;
109
110   g_return_val_if_fail (path != NULL, FALSE);
111   g_return_val_if_fail (path[0] == '/', FALSE);
112
113   for (i = 1; path[i]; i++)
114     g_return_val_if_fail (path[i] != '/' || path[i + 1] != '/', FALSE);
115
116   length = i;
117
118   g_return_val_if_fail (path[length - 1] == '/', FALSE);
119
120   return TRUE;
121 }
122
123 GMainContext *
124 g_settings_backend_get_active_context (void)
125 {
126   GMainContext *context;
127   GSource *source;
128
129   if ((source = g_main_current_source ()))
130     context = g_source_get_context (source);
131
132   else
133     {
134       context = g_main_context_get_thread_default ();
135
136       if (context == NULL)
137         context = g_main_context_default ();
138     }
139
140   return context;
141 }
142
143 struct _GSettingsBackendWatch
144 {
145   GObject                                 *target;
146   GMainContext                            *context;
147   GSettingsBackendChangedFunc              changed;
148   GSettingsBackendPathChangedFunc          path_changed;
149   GSettingsBackendKeysChangedFunc          keys_changed;
150   GSettingsBackendWritableChangedFunc      writable_changed;
151   GSettingsBackendPathWritableChangedFunc  path_writable_changed;
152
153   GSettingsBackendWatch                   *next;
154 };
155
156 struct _GSettingsBackendClosure
157 {
158   void (*function) (GSettingsBackend *backend,
159                     GObject          *target,
160                     const gchar      *name,
161                     gpointer          data1,
162                     gpointer          data2);
163
164   GSettingsBackend *backend;
165   GObject          *target;
166   gchar            *name;
167   gpointer          data1;
168   GBoxedFreeFunc    data1_free;
169   gpointer          data2;
170 };
171
172 static void
173 g_settings_backend_watch_weak_notify (gpointer  data,
174                                       GObject  *where_the_object_was)
175 {
176   GSettingsBackend *backend = data;
177   GSettingsBackendWatch **ptr;
178
179   /* search and remove */
180   g_static_mutex_lock (&backend->priv->lock);
181   for (ptr = &backend->priv->watches; *ptr; ptr = &(*ptr)->next)
182     if ((*ptr)->target == where_the_object_was)
183       {
184         GSettingsBackendWatch *tmp = *ptr;
185
186         *ptr = tmp->next;
187         g_slice_free (GSettingsBackendWatch, tmp);
188
189         g_static_mutex_unlock (&backend->priv->lock);
190         return;
191       }
192
193   /* we didn't find it.  that shouldn't happen. */
194   g_assert_not_reached ();
195 }
196
197 /*< private >
198  * g_settings_backend_watch:
199  * @backend: a #GSettingsBackend
200  * @target: the GObject (typically GSettings instance) to call back to
201  * @context: a #GMainContext, or %NULL
202  * ...: callbacks...
203  *
204  * Registers a new watch on a #GSettingsBackend.
205  *
206  * note: %NULL @context does not mean "default main context" but rather,
207  * "it is okay to dispatch in any context".  If the default main context
208  * is specifically desired then it must be given.
209  *
210  * note also: if you want to get meaningful values for the @origin_tag
211  * that appears as an argument to some of the callbacks, you *must* have
212  * @context as %NULL.  Otherwise, you are subject to cross-thread
213  * dispatching and whatever owned @origin_tag at the time that the event
214  * occured may no longer own it.  This is a problem if you consider that
215  * you may now be the new owner of that address and mistakenly think
216  * that the event in question originated from yourself.
217  *
218  * tl;dr: If you give a non-%NULL @context then you must ignore the
219  * value of @origin_tag given to any callbacks.
220  **/
221 void
222 g_settings_backend_watch (GSettingsBackend                        *backend,
223                           GObject                                 *target,
224                           GMainContext                            *context,
225                           GSettingsBackendChangedFunc              changed,
226                           GSettingsBackendPathChangedFunc          path_changed,
227                           GSettingsBackendKeysChangedFunc          keys_changed,
228                           GSettingsBackendWritableChangedFunc      writable_changed,
229                           GSettingsBackendPathWritableChangedFunc  path_writable_changed)
230 {
231   GSettingsBackendWatch *watch;
232
233   /* For purposes of discussion, we assume that our target is a
234    * GSettings instance.
235    *
236    * Our strategy to defend against the final reference dropping on the
237    * GSettings object in a thread other than the one that is doing the
238    * dispatching is as follows:
239    *
240    *  1) hold a GObject reference on the GSettings during an outstanding
241    *     dispatch.  This ensures that the delivery is always possible.
242    *
243    *  2) hold a weak reference on the GSettings at other times.  This
244    *     allows us to receive early notification of pending destruction
245    *     of the object.  At this point, it is still safe to obtain a
246    *     reference on the GObject to keep it alive, so #1 will work up
247    *     to that point.  After that point, we'll have been able to drop
248    *     the watch from the list.
249    *
250    * Note, in particular, that it's not possible to simply have an
251    * "unwatch" function that gets called from the finalize function of
252    * the GSettings instance because, by that point it is no longer
253    * possible to keep the object alive using g_object_ref() and we would
254    * have no way of knowing this.
255    *
256    * Note also that we do not need to hold a reference on the main
257    * context here since the GSettings instance does that for us and we
258    * will receive the weak notify long before it is dropped.  We don't
259    * even need to hold it during dispatches because our reference on the
260    * GSettings will prevent the finalize from running and dropping the
261    * ref on the context.
262    *
263    * All access to the list holds a mutex.  We have some strategies to
264    * avoid some of the pain that would be associated with that.
265    */
266
267   watch = g_slice_new (GSettingsBackendWatch);
268   watch->context = context;
269   watch->target = target;
270   g_object_weak_ref (target, g_settings_backend_watch_weak_notify, backend);
271
272   watch->changed = changed;
273   watch->path_changed = path_changed;
274   watch->keys_changed = keys_changed;
275   watch->writable_changed = writable_changed;
276   watch->path_writable_changed = path_writable_changed;
277
278   /* linked list prepend */
279   g_static_mutex_lock (&backend->priv->lock);
280   watch->next = backend->priv->watches;
281   backend->priv->watches = watch;
282   g_static_mutex_unlock (&backend->priv->lock);
283 }
284
285 void
286 g_settings_backend_unwatch (GSettingsBackend *backend,
287                             GObject          *target)
288 {
289   /* Our caller surely owns a reference on 'target', so the order of
290    * these two calls is unimportant.
291    */
292   g_object_weak_unref (target, g_settings_backend_watch_weak_notify, backend);
293   g_settings_backend_watch_weak_notify (backend, target);
294 }
295
296 static gboolean
297 g_settings_backend_invoke_closure (gpointer user_data)
298 {
299   GSettingsBackendClosure *closure = user_data;
300
301   closure->function (closure->backend, closure->target, closure->name,
302                      closure->data1, closure->data2);
303
304   closure->data1_free (closure->data1);
305   g_object_unref (closure->backend);
306   g_object_unref (closure->target);
307   g_free (closure->name);
308
309   g_slice_free (GSettingsBackendClosure, closure);
310
311   return FALSE;
312 }
313
314 static gpointer
315 pointer_id (gpointer a)
316 {
317   return a;
318 }
319
320 static void
321 pointer_ignore (gpointer a)
322 {
323 }
324
325 static void
326 g_settings_backend_dispatch_signal (GSettingsBackend *backend,
327                                     gsize             function_offset,
328                                     const gchar      *name,
329                                     gpointer          data1,
330                                     GBoxedCopyFunc    data1_copy,
331                                     GBoxedFreeFunc    data1_free,
332                                     gpointer          data2)
333 {
334   GMainContext *context, *here_and_now;
335   GSettingsBackendWatch *watch;
336
337   /* We need to hold the mutex here (to prevent a node from being
338    * deleted as we are traversing the list).  Since we should not
339    * re-enter user code while holding this mutex, we create a
340    * one-time-use GMainContext and populate it with the events that we
341    * would have called directly.  We dispatch these events after
342    * releasing the lock.  Note that the GObject reference is acquired on
343    * the target while holding the mutex and the mutex needs to be held
344    * as part of the destruction of any GSettings instance (via the weak
345    * reference handling).  This is the key to the safety of the whole
346    * setup.
347    */
348
349   if (data1_copy == NULL)
350     data1_copy = pointer_id;
351
352   if (data1_free == NULL)
353     data1_free = pointer_ignore;
354
355   context = g_settings_backend_get_active_context ();
356   here_and_now = g_main_context_new ();
357
358   /* traverse the (immutable while holding lock) list */
359   g_static_mutex_lock (&backend->priv->lock);
360   for (watch = backend->priv->watches; watch; watch = watch->next)
361     {
362       GSettingsBackendClosure *closure;
363       GSource *source;
364
365       closure = g_slice_new (GSettingsBackendClosure);
366       closure->backend = g_object_ref (backend);
367       closure->target = g_object_ref (watch->target);
368       closure->function = G_STRUCT_MEMBER (void *, watch, function_offset);
369       closure->name = g_strdup (name);
370       closure->data1 = data1_copy (data1);
371       closure->data1_free = data1_free;
372       closure->data2 = data2;
373
374       source = g_idle_source_new ();
375       g_source_set_priority (source, G_PRIORITY_DEFAULT);
376       g_source_set_callback (source,
377                              g_settings_backend_invoke_closure,
378                              closure, NULL);
379
380       if (watch->context && watch->context != context)
381         g_source_attach (source, watch->context);
382       else
383         g_source_attach (source, here_and_now);
384
385       g_source_unref (source);
386     }
387   g_static_mutex_unlock (&backend->priv->lock);
388
389   while (g_main_context_iteration (here_and_now, FALSE));
390   g_main_context_unref (here_and_now);
391 }
392
393 /**
394  * g_settings_backend_changed:
395  * @backend: a #GSettingsBackend implementation
396  * @key: the name of the key
397  * @origin_tag: the origin tag
398  *
399  * Signals that a single key has possibly changed.  Backend
400  * implementations should call this if a key has possibly changed its
401  * value.
402  *
403  * @key must be a valid key (ie: starting with a slash, not containing
404  * '//', and not ending with a slash).
405  *
406  * The implementation must call this function during any call to
407  * g_settings_backend_write(), before the call returns (except in the
408  * case that no keys are actually changed and it cares to detect this
409  * fact).  It may not rely on the existence of a mainloop for
410  * dispatching the signal later.
411  *
412  * The implementation may call this function at any other time it likes
413  * in response to other events (such as changes occuring outside of the
414  * program).  These calls may originate from a mainloop or may originate
415  * in response to any other action (including from calls to
416  * g_settings_backend_write()).
417  *
418  * In the case that this call is in response to a call to
419  * g_settings_backend_write() then @origin_tag must be set to the same
420  * value that was passed to that call.
421  *
422  * Since: 2.26
423  **/
424 void
425 g_settings_backend_changed (GSettingsBackend *backend,
426                             const gchar      *key,
427                             gpointer          origin_tag)
428 {
429   g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
430   g_return_if_fail (is_key (key));
431
432   g_settings_backend_dispatch_signal (backend,
433                                       G_STRUCT_OFFSET (GSettingsBackendWatch,
434                                                        changed),
435                                       key, origin_tag, NULL, NULL, NULL);
436 }
437
438 /**
439  * g_settings_backend_keys_changed:
440  * @backend: a #GSettingsBackend implementation
441  * @path: the path containing the changes
442  * @items: the %NULL-terminated list of changed keys
443  * @origin_tag: the origin tag
444  *
445  * Signals that a list of keys have possibly changed.  Backend
446  * implementations should call this if keys have possibly changed their
447  * values.
448  *
449  * @path must be a valid path (ie: starting and ending with a slash and
450  * not containing '//').  Each string in @items must form a valid key
451  * name when @path is prefixed to it (ie: each item must not start or
452  * end with '/' and must not contain '//').
453  *
454  * The meaning of this signal is that any of the key names resulting
455  * from the contatenation of @path with each item in @items may have
456  * changed.
457  *
458  * The same rules for when notifications must occur apply as per
459  * g_settings_backend_changed().  These two calls can be used
460  * interchangeably if exactly one item has changed (although in that
461  * case g_settings_backend_changed() is definitely preferred).
462  *
463  * For efficiency reasons, the implementation should strive for @path to
464  * be as long as possible (ie: the longest common prefix of all of the
465  * keys that were changed) but this is not strictly required.
466  *
467  * Since: 2.26
468  */
469 void
470 g_settings_backend_keys_changed (GSettingsBackend    *backend,
471                                  const gchar         *path,
472                                  gchar const * const *items,
473                                  gpointer             origin_tag)
474 {
475   g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
476   g_return_if_fail (is_path (path));
477
478   /* XXX: should do stricter checking (ie: inspect each item) */
479   g_return_if_fail (items != NULL);
480
481   g_settings_backend_dispatch_signal (backend,
482                                       G_STRUCT_OFFSET (GSettingsBackendWatch,
483                                                        keys_changed),
484                                       path, (gpointer) items,
485                                       (GBoxedCopyFunc) g_strdupv,
486                                       (GBoxedFreeFunc) g_strfreev,
487                                       origin_tag);
488 }
489
490 /**
491  * g_settings_backend_path_changed:
492  * @backend: a #GSettingsBackend implementation
493  * @path: the path containing the changes
494  * @origin_tag: the origin tag
495  *
496  * Signals that all keys below a given path may have possibly changed.
497  * Backend implementations should call this if an entire path of keys
498  * have possibly changed their values.
499  *
500  * @path must be a valid path (ie: starting and ending with a slash and
501  * not containing '//').
502  *
503  * The meaning of this signal is that any of the key which has a name
504  * starting with @path may have changed.
505  *
506  * The same rules for when notifications must occur apply as per
507  * g_settings_backend_changed().  This call might be an appropriate
508  * reasponse to a 'reset' call but implementations are also free to
509  * explicitly list the keys that were affected by that call if they can
510  * easily do so.
511  *
512  * For efficiency reasons, the implementation should strive for @path to
513  * be as long as possible (ie: the longest common prefix of all of the
514  * keys that were changed) but this is not strictly required.  As an
515  * example, if this function is called with the path of "/" then every
516  * single key in the application will be notified of a possible change.
517  *
518  * Since: 2.26
519  */
520 void
521 g_settings_backend_path_changed (GSettingsBackend *backend,
522                                  const gchar      *path,
523                                  gpointer          origin_tag)
524 {
525   g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
526   g_return_if_fail (is_path (path));
527
528   g_settings_backend_dispatch_signal (backend,
529                                       G_STRUCT_OFFSET (GSettingsBackendWatch,
530                                                        path_changed),
531                                       path, origin_tag, NULL, NULL, NULL);
532 }
533
534 /**
535  * g_settings_backend_writable_changed:
536  * @backend: a #GSettingsBackend implementation
537  * @key: the name of the key
538  *
539  * Signals that the writability of a single key has possibly changed.
540  *
541  * Since GSettings performs no locking operations for itself, this call
542  * will always be made in response to external events.
543  *
544  * Since: 2.26
545  **/
546 void
547 g_settings_backend_writable_changed (GSettingsBackend *backend,
548                                      const gchar      *key)
549 {
550   g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
551   g_return_if_fail (is_key (key));
552
553   g_settings_backend_dispatch_signal (backend,
554                                       G_STRUCT_OFFSET (GSettingsBackendWatch,
555                                                        writable_changed),
556                                       key, NULL, NULL, NULL, NULL);
557 }
558
559 /**
560  * g_settings_backend_path_writable_changed:
561  * @backend: a #GSettingsBackend implementation
562  * @path: the name of the path
563  *
564  * Signals that the writability of all keys below a given path may have
565  * changed.
566  *
567  * Since GSettings performs no locking operations for itself, this call
568  * will always be made in response to external events.
569  *
570  * Since: 2.26
571  **/
572 void
573 g_settings_backend_path_writable_changed (GSettingsBackend *backend,
574                                           const gchar      *path)
575 {
576   g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
577   g_return_if_fail (is_path (path));
578
579   g_settings_backend_dispatch_signal (backend,
580                                       G_STRUCT_OFFSET (GSettingsBackendWatch,
581                                                        path_writable_changed),
582                                       path, NULL, NULL, NULL, NULL);
583 }
584
585 typedef struct
586 {
587   const gchar **keys;
588   GVariant **values;
589   gint prefix_len;
590   gchar *prefix;
591 } FlattenState;
592
593 static gboolean
594 g_settings_backend_flatten_one (gpointer key,
595                                 gpointer value,
596                                 gpointer user_data)
597 {
598   FlattenState *state = user_data;
599   const gchar *skey = key;
600   gint i;
601
602   g_return_val_if_fail (is_key (key), TRUE);
603
604   /* calculate longest common prefix */
605   if (state->prefix == NULL)
606     {
607       gchar *last_byte;
608
609       /* first key?  just take the prefix up to the last '/' */
610       state->prefix = g_strdup (skey);
611       last_byte = strrchr (state->prefix, '/') + 1;
612       state->prefix_len = last_byte - state->prefix;
613       *last_byte = '\0';
614     }
615   else
616     {
617       /* find the first character that does not match.  we will
618        * definitely find one because the prefix ends in '/' and the key
619        * does not.  also: no two keys in the tree are the same.
620        */
621       for (i = 0; state->prefix[i] == skey[i]; i++);
622
623       /* check if we need to shorten the prefix */
624       if (state->prefix[i] != '\0')
625         {
626           /* find the nearest '/', terminate after it */
627           while (state->prefix[i - 1] != '/')
628             i--;
629
630           state->prefix[i] = '\0';
631           state->prefix_len = i;
632         }
633     }
634
635
636   /* save the entire item into the array.
637    * the prefixes will be removed later.
638    */
639   *state->keys++ = key;
640
641   if (state->values)
642     *state->values++ = value;
643
644   return FALSE;
645 }
646
647 /**
648  * g_settings_backend_flatten_tree:
649  * @tree: a #GTree containing the changes
650  * @path: the location to save the path
651  * @keys: the location to save the relative keys
652  * @values: the location to save the values, or %NULL
653  *
654  * Calculate the longest common prefix of all keys in a tree and write
655  * out an array of the key names relative to that prefix and,
656  * optionally, the value to store at each of those keys.
657  *
658  * You must free the value returned in @path, @keys and @values using
659  * g_free().  You should not attempt to free or unref the contents of
660  * @keys or @values.
661  *
662  * Since: 2.26
663  **/
664 void
665 g_settings_backend_flatten_tree (GTree         *tree,
666                                  gchar        **path,
667                                  const gchar ***keys,
668                                  GVariant    ***values)
669 {
670   FlattenState state = { 0, };
671   gsize nnodes;
672
673   nnodes = g_tree_nnodes (tree);
674
675   *keys = state.keys = g_new (const gchar *, nnodes + 1);
676   state.keys[nnodes] = NULL;
677
678   if (values != NULL)
679     {
680       *values = state.values = g_new (GVariant *, nnodes + 1);
681       state.values[nnodes] = NULL;
682     }
683
684   g_tree_foreach (tree, g_settings_backend_flatten_one, &state);
685   g_return_if_fail (*keys + nnodes == state.keys);
686
687   *path = state.prefix;
688   while (nnodes--)
689     *--state.keys += state.prefix_len;
690 }
691
692 /**
693  * g_settings_backend_changed_tree:
694  * @backend: a #GSettingsBackend implementation
695  * @tree: a #GTree containing the changes
696  * @origin_tag: the origin tag
697  *
698  * This call is a convenience wrapper.  It gets the list of changes from
699  * @tree, computes the longest common prefix and calls
700  * g_settings_backend_changed().
701  *
702  * Since: 2.26
703  **/
704 void
705 g_settings_backend_changed_tree (GSettingsBackend *backend,
706                                  GTree            *tree,
707                                  gpointer          origin_tag)
708 {
709   GSettingsBackendWatch *watch;
710   const gchar **keys;
711   gchar *path;
712
713   g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
714
715   g_settings_backend_flatten_tree (tree, &path, &keys, NULL);
716
717   for (watch = backend->priv->watches; watch; watch = watch->next)
718     watch->keys_changed (backend, watch->target, path, keys, origin_tag);
719
720   g_free (path);
721   g_free (keys);
722 }
723
724 /*< private >
725  * g_settings_backend_read:
726  * @backend: a #GSettingsBackend implementation
727  * @key: the key to read
728  * @expected_type: a #GVariantType
729  * @default_value: if the default value should be returned
730  * @returns: the value that was read, or %NULL
731  *
732  * Reads a key. This call will never block.
733  *
734  * If the key exists, the value associated with it will be returned.
735  * If the key does not exist, %NULL will be returned.
736  *
737  * The returned value will be of the type given in @expected_type.  If
738  * the backend stored a value of a different type then %NULL will be
739  * returned.
740  *
741  * If @default_value is %TRUE then this gets the default value from the
742  * backend (ie: the one that the backend would contain if
743  * g_settings_reset() were called).
744  */
745 GVariant *
746 g_settings_backend_read (GSettingsBackend   *backend,
747                          const gchar        *key,
748                          const GVariantType *expected_type,
749                          gboolean            default_value)
750 {
751   GVariant *value;
752
753   value = G_SETTINGS_BACKEND_GET_CLASS (backend)
754     ->read (backend, key, expected_type, default_value);
755
756   if G_UNLIKELY (value && !g_variant_is_of_type (value, expected_type))
757     {
758       g_variant_unref (value);
759       value = NULL;
760     }
761
762   return value;
763 }
764
765 /*< private >
766  * g_settings_backend_write:
767  * @backend: a #GSettingsBackend implementation
768  * @key: the name of the key
769  * @value: a #GVariant value to write to this key
770  * @origin_tag: the origin tag
771  * @returns: %TRUE if the write succeeded, %FALSE if the key was not writable
772  *
773  * Writes exactly one key.
774  *
775  * This call does not fail.  During this call a
776  * #GSettingsBackend::changed signal will be emitted if the value of the
777  * key has changed.  The updated key value will be visible to any signal
778  * callbacks.
779  *
780  * One possible method that an implementation might deal with failures is
781  * to emit a second "changed" signal (either during this call, or later)
782  * to indicate that the affected keys have suddenly "changed back" to their
783  * old values.
784  */
785 gboolean
786 g_settings_backend_write (GSettingsBackend *backend,
787                           const gchar      *key,
788                           GVariant         *value,
789                           gpointer          origin_tag)
790 {
791   return G_SETTINGS_BACKEND_GET_CLASS (backend)
792     ->write (backend, key, value, origin_tag);
793 }
794
795 /*< private >
796  * g_settings_backend_write_keys:
797  * @backend: a #GSettingsBackend implementation
798  * @values: a #GTree containing key-value pairs to write
799  * @origin_tag: the origin tag
800  *
801  * Writes one or more keys.  This call will never block.
802  *
803  * The key of each item in the tree is the key name to write to and the
804  * value is a #GVariant to write.  The proper type of #GTree for this
805  * call can be created with g_settings_backend_create_tree().  This call
806  * might take a reference to the tree; you must not modified the #GTree
807  * after passing it to this call.
808  *
809  * This call does not fail.  During this call a #GSettingsBackend::changed
810  * signal will be emitted if any keys have been changed.  The new values of
811  * all updated keys will be visible to any signal callbacks.
812  *
813  * One possible method that an implementation might deal with failures is
814  * to emit a second "changed" signal (either during this call, or later)
815  * to indicate that the affected keys have suddenly "changed back" to their
816  * old values.
817  */
818 gboolean
819 g_settings_backend_write_keys (GSettingsBackend *backend,
820                                GTree            *tree,
821                                gpointer          origin_tag)
822 {
823   return G_SETTINGS_BACKEND_GET_CLASS (backend)
824     ->write_keys (backend, tree, origin_tag);
825 }
826
827 /*< private >
828  * g_settings_backend_reset:
829  * @backend: a #GSettingsBackend implementation
830  * @key: the name of a key
831  * @origin_tag: the origin tag
832  *
833  * "Resets" the named key to its "default" value (ie: after system-wide
834  * defaults, mandatory keys, etc. have been taken into account) or possibly
835  * unsets it.
836  */
837 void
838 g_settings_backend_reset (GSettingsBackend *backend,
839                           const gchar      *key,
840                           gpointer          origin_tag)
841 {
842   G_SETTINGS_BACKEND_GET_CLASS (backend)
843     ->reset (backend, key, origin_tag);
844 }
845
846 /*< private >
847  * g_settings_backend_reset_path:
848  * @backend: a #GSettingsBackend implementation
849  * @name: the name of a key or path
850  * @origin_tag: the origin tag
851  *
852  * "Resets" the named path.  This means that every key under the path is
853  * reset.
854  */
855 void
856 g_settings_backend_reset_path (GSettingsBackend *backend,
857                                const gchar      *path,
858                                gpointer          origin_tag)
859 {
860   G_SETTINGS_BACKEND_GET_CLASS (backend)
861     ->reset_path (backend, path, origin_tag);
862 }
863
864 /*< private >
865  * g_settings_backend_get_writable:
866  * @backend: a #GSettingsBackend implementation
867  * @key: the name of a key
868  * @returns: %TRUE if the key is writable
869  *
870  * Finds out if a key is available for writing to.  This is the
871  * interface through which 'lockdown' is implemented.  Locked down
872  * keys will have %FALSE returned by this call.
873  *
874  * You should not write to locked-down keys, but if you do, the
875  * implementation will deal with it.
876  */
877 gboolean
878 g_settings_backend_get_writable (GSettingsBackend *backend,
879                                  const gchar      *key)
880 {
881   return G_SETTINGS_BACKEND_GET_CLASS (backend)
882     ->get_writable (backend, key);
883 }
884
885 /*< private >
886  * g_settings_backend_unsubscribe:
887  * @backend: a #GSettingsBackend
888  * @name: a key or path to subscribe to
889  *
890  * Reverses the effect of a previous call to
891  * g_settings_backend_subscribe().
892  */
893 void
894 g_settings_backend_unsubscribe (GSettingsBackend *backend,
895                                 const char       *name)
896 {
897   G_SETTINGS_BACKEND_GET_CLASS (backend)
898     ->unsubscribe (backend, name);
899 }
900
901 /*< private >
902  * g_settings_backend_subscribe:
903  * @backend: a #GSettingsBackend
904  * @name: a key or path to subscribe to
905  *
906  * Requests that change signals be emitted for events on @name.
907  */
908 void
909 g_settings_backend_subscribe (GSettingsBackend *backend,
910                               const gchar      *name)
911 {
912   G_SETTINGS_BACKEND_GET_CLASS (backend)
913     ->subscribe (backend, name);
914 }
915
916 static void
917 g_settings_backend_finalize (GObject *object)
918 {
919   GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
920
921   g_static_mutex_unlock (&backend->priv->lock);
922
923   G_OBJECT_CLASS (g_settings_backend_parent_class)
924     ->finalize (object);
925 }
926
927 static void
928 ignore_subscription (GSettingsBackend *backend,
929                      const gchar      *key)
930 {
931 }
932
933 static void
934 g_settings_backend_init (GSettingsBackend *backend)
935 {
936   backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend,
937                                                G_TYPE_SETTINGS_BACKEND,
938                                                GSettingsBackendPrivate);
939   g_static_mutex_init (&backend->priv->lock);
940 }
941
942 static void
943 g_settings_backend_class_init (GSettingsBackendClass *class)
944 {
945   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
946
947   class->subscribe = ignore_subscription;
948   class->unsubscribe = ignore_subscription;
949
950   gobject_class->finalize = g_settings_backend_finalize;
951
952   g_type_class_add_private (class, sizeof (GSettingsBackendPrivate));
953 }
954
955 /*< private >
956  * g_settings_backend_create_tree:
957  * @returns: a new #GTree
958  *
959  * This is a convenience function for creating a tree that is compatible
960  * with g_settings_backend_write().  It merely calls g_tree_new_full()
961  * with strcmp(), g_free() and g_variant_unref().
962  */
963 GTree *
964 g_settings_backend_create_tree (void)
965 {
966   return g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
967                           g_free, (GDestroyNotify) g_variant_unref);
968 }
969
970 /*< private >
971  * g_settings_backend_get_default:
972  * @returns: the default #GSettingsBackend
973  *
974  * Returns the default #GSettingsBackend. It is possible to override
975  * the default by setting the <envar>GSETTINGS_BACKEND</envar>
976  * environment variable to the name of a settings backend.
977  *
978  * The user gets a reference to the backend.
979  */
980 GSettingsBackend *
981 g_settings_backend_get_default (void)
982 {
983   static gsize backend;
984
985   if (g_once_init_enter (&backend))
986     {
987       GSettingsBackend *instance;
988       GIOExtensionPoint *point;
989       GIOExtension *extension;
990       GType extension_type;
991       GList *extensions;
992       const gchar *env;
993
994       _g_io_modules_ensure_loaded ();
995
996       point = g_io_extension_point_lookup (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME);
997       extension = NULL;
998
999       if ((env = getenv ("GSETTINGS_BACKEND")))
1000         {
1001           extension = g_io_extension_point_get_extension_by_name (point, env);
1002
1003           if (extension == NULL)
1004             g_warning ("Can't find GSettings backend '%s' given in "
1005                        "GSETTINGS_BACKEND environment variable", env);
1006         }
1007
1008       if (extension == NULL)
1009         {
1010           extensions = g_io_extension_point_get_extensions (point);
1011
1012           if (extensions == NULL)
1013             g_error ("No GSettingsBackend implementations exist.");
1014
1015           extension = extensions->data;
1016         }
1017
1018       extension_type = g_io_extension_get_type (extension);
1019       instance = g_object_new (extension_type, NULL);
1020
1021       g_once_init_leave (&backend, (gsize) instance);
1022     }
1023
1024   return g_object_ref ((void *) backend);
1025 }
1026
1027 /*< private >
1028  * g_settings_backend_get_permission:
1029  * @backend: a #GSettingsBackend
1030  * @path: a path
1031  * @returns: a non-%NULL #GPermission
1032  *
1033  * Gets the permission object associated with writing to keys below
1034  * @path on @backend.
1035  *
1036  * If this is not implemented in the backend, then a %TRUE
1037  * #GSimplePermission is returned.
1038  */
1039 GPermission *
1040 g_settings_backend_get_permission (GSettingsBackend *backend,
1041                                    const gchar      *path)
1042 {
1043   GSettingsBackendClass *class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1044
1045   if (class->get_permission)
1046     return class->get_permission (backend, path);
1047
1048   return g_simple_permission_new (TRUE);
1049 }
1050
1051 /*< private >
1052  * g_settings_backend_sync_default:
1053  *
1054  * Syncs the default backend.
1055  */
1056 void
1057 g_settings_backend_sync_default (void)
1058 {
1059   GSettingsBackendClass *class;
1060   GSettingsBackend *backend;
1061
1062   backend = g_settings_backend_get_default ();
1063   class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1064
1065   if (class->sync)
1066     class->sync (backend);
1067 }
1068
1069 #define __G_SETTINGS_BACKEND_C__
1070 #include "gioaliasdef.c"