Small documentation addition
[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 #ifdef DEBUG_CHANGES
718   {
719     gint i;
720
721     g_print ("----\n");
722     g_print ("changed_tree(): prefix %s\n", path);
723     for (i = 0; keys[i]; i++)
724       g_print ("  %s\n", keys[i]);
725     g_print ("----\n");
726   }
727 #endif
728
729   for (watch = backend->priv->watches; watch; watch = watch->next)
730     watch->keys_changed (backend, watch->target, path, keys, origin_tag);
731
732   g_free (path);
733   g_free (keys);
734 }
735
736 /*< private >
737  * g_settings_backend_read:
738  * @backend: a #GSettingsBackend implementation
739  * @key: the key to read
740  * @expected_type: a #GVariantType
741  * @default_value: if the default value should be returned
742  * @returns: the value that was read, or %NULL
743  *
744  * Reads a key. This call will never block.
745  *
746  * If the key exists, the value associated with it will be returned.
747  * If the key does not exist, %NULL will be returned.
748  *
749  * The returned value will be of the type given in @expected_type.  If
750  * the backend stored a value of a different type then %NULL will be
751  * returned.
752  *
753  * If @default_value is %TRUE then this gets the default value from the
754  * backend (ie: the one that the backend would contain if
755  * g_settings_reset() were called).
756  */
757 GVariant *
758 g_settings_backend_read (GSettingsBackend   *backend,
759                          const gchar        *key,
760                          const GVariantType *expected_type,
761                          gboolean            default_value)
762 {
763   GVariant *value;
764
765   value = G_SETTINGS_BACKEND_GET_CLASS (backend)
766     ->read (backend, key, expected_type, default_value);
767
768   if G_UNLIKELY (value && !g_variant_is_of_type (value, expected_type))
769     {
770       g_variant_unref (value);
771       value = NULL;
772     }
773
774   return value;
775 }
776
777 /*< private >
778  * g_settings_backend_write:
779  * @backend: a #GSettingsBackend implementation
780  * @key: the name of the key
781  * @value: a #GVariant value to write to this key
782  * @origin_tag: the origin tag
783  * @returns: %TRUE if the write succeeded, %FALSE if the key was not writable
784  *
785  * Writes exactly one key.
786  *
787  * This call does not fail.  During this call a
788  * #GSettingsBackend::changed signal will be emitted if the value of the
789  * key has changed.  The updated key value will be visible to any signal
790  * callbacks.
791  *
792  * One possible method that an implementation might deal with failures is
793  * to emit a second "changed" signal (either during this call, or later)
794  * to indicate that the affected keys have suddenly "changed back" to their
795  * old values.
796  */
797 gboolean
798 g_settings_backend_write (GSettingsBackend *backend,
799                           const gchar      *key,
800                           GVariant         *value,
801                           gpointer          origin_tag)
802 {
803   return G_SETTINGS_BACKEND_GET_CLASS (backend)
804     ->write (backend, key, value, origin_tag);
805 }
806
807 /*< private >
808  * g_settings_backend_write_keys:
809  * @backend: a #GSettingsBackend implementation
810  * @values: a #GTree containing key-value pairs to write
811  * @origin_tag: the origin tag
812  *
813  * Writes one or more keys.  This call will never block.
814  *
815  * The key of each item in the tree is the key name to write to and the
816  * value is a #GVariant to write.  The proper type of #GTree for this
817  * call can be created with g_settings_backend_create_tree().  This call
818  * might take a reference to the tree; you must not modified the #GTree
819  * after passing it to this call.
820  *
821  * This call does not fail.  During this call a #GSettingsBackend::changed
822  * signal will be emitted if any keys have been changed.  The new values of
823  * all updated keys will be visible to any signal callbacks.
824  *
825  * One possible method that an implementation might deal with failures is
826  * to emit a second "changed" signal (either during this call, or later)
827  * to indicate that the affected keys have suddenly "changed back" to their
828  * old values.
829  */
830 gboolean
831 g_settings_backend_write_keys (GSettingsBackend *backend,
832                                GTree            *tree,
833                                gpointer          origin_tag)
834 {
835   return G_SETTINGS_BACKEND_GET_CLASS (backend)
836     ->write_keys (backend, tree, origin_tag);
837 }
838
839 /*< private >
840  * g_settings_backend_reset:
841  * @backend: a #GSettingsBackend implementation
842  * @key: the name of a key
843  * @origin_tag: the origin tag
844  *
845  * "Resets" the named key to its "default" value (ie: after system-wide
846  * defaults, mandatory keys, etc. have been taken into account) or possibly
847  * unsets it.
848  */
849 void
850 g_settings_backend_reset (GSettingsBackend *backend,
851                           const gchar      *key,
852                           gpointer          origin_tag)
853 {
854   G_SETTINGS_BACKEND_GET_CLASS (backend)
855     ->reset (backend, key, origin_tag);
856 }
857
858 /*< private >
859  * g_settings_backend_reset_path:
860  * @backend: a #GSettingsBackend implementation
861  * @name: the name of a key or path
862  * @origin_tag: the origin tag
863  *
864  * "Resets" the named path.  This means that every key under the path is
865  * reset.
866  */
867 void
868 g_settings_backend_reset_path (GSettingsBackend *backend,
869                                const gchar      *path,
870                                gpointer          origin_tag)
871 {
872   G_SETTINGS_BACKEND_GET_CLASS (backend)
873     ->reset_path (backend, path, origin_tag);
874 }
875
876 /*< private >
877  * g_settings_backend_get_writable:
878  * @backend: a #GSettingsBackend implementation
879  * @key: the name of a key
880  * @returns: %TRUE if the key is writable
881  *
882  * Finds out if a key is available for writing to.  This is the
883  * interface through which 'lockdown' is implemented.  Locked down
884  * keys will have %FALSE returned by this call.
885  *
886  * You should not write to locked-down keys, but if you do, the
887  * implementation will deal with it.
888  */
889 gboolean
890 g_settings_backend_get_writable (GSettingsBackend *backend,
891                                  const gchar      *key)
892 {
893   return G_SETTINGS_BACKEND_GET_CLASS (backend)
894     ->get_writable (backend, key);
895 }
896
897 /*< private >
898  * g_settings_backend_unsubscribe:
899  * @backend: a #GSettingsBackend
900  * @name: a key or path to subscribe to
901  *
902  * Reverses the effect of a previous call to
903  * g_settings_backend_subscribe().
904  */
905 void
906 g_settings_backend_unsubscribe (GSettingsBackend *backend,
907                                 const char       *name)
908 {
909   G_SETTINGS_BACKEND_GET_CLASS (backend)
910     ->unsubscribe (backend, name);
911 }
912
913 /*< private >
914  * g_settings_backend_subscribe:
915  * @backend: a #GSettingsBackend
916  * @name: a key or path to subscribe to
917  *
918  * Requests that change signals be emitted for events on @name.
919  */
920 void
921 g_settings_backend_subscribe (GSettingsBackend *backend,
922                               const gchar      *name)
923 {
924   G_SETTINGS_BACKEND_GET_CLASS (backend)
925     ->subscribe (backend, name);
926 }
927
928 static void
929 g_settings_backend_finalize (GObject *object)
930 {
931   GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
932
933   g_static_mutex_unlock (&backend->priv->lock);
934
935   G_OBJECT_CLASS (g_settings_backend_parent_class)
936     ->finalize (object);
937 }
938
939 static void
940 ignore_subscription (GSettingsBackend *backend,
941                      const gchar      *key)
942 {
943 }
944
945 static void
946 g_settings_backend_init (GSettingsBackend *backend)
947 {
948   backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend,
949                                                G_TYPE_SETTINGS_BACKEND,
950                                                GSettingsBackendPrivate);
951   g_static_mutex_init (&backend->priv->lock);
952 }
953
954 static void
955 g_settings_backend_class_init (GSettingsBackendClass *class)
956 {
957   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
958
959   class->subscribe = ignore_subscription;
960   class->unsubscribe = ignore_subscription;
961
962   gobject_class->finalize = g_settings_backend_finalize;
963
964   g_type_class_add_private (class, sizeof (GSettingsBackendPrivate));
965 }
966
967 /*< private >
968  * g_settings_backend_create_tree:
969  * @returns: a new #GTree
970  *
971  * This is a convenience function for creating a tree that is compatible
972  * with g_settings_backend_write().  It merely calls g_tree_new_full()
973  * with strcmp(), g_free() and g_variant_unref().
974  */
975 GTree *
976 g_settings_backend_create_tree (void)
977 {
978   return g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
979                           g_free, (GDestroyNotify) g_variant_unref);
980 }
981
982 /*< private >
983  * g_settings_backend_get_default:
984  * @returns: the default #GSettingsBackend
985  *
986  * Returns the default #GSettingsBackend. It is possible to override
987  * the default by setting the <envar>GSETTINGS_BACKEND</envar>
988  * environment variable to the name of a settings backend.
989  *
990  * The user gets a reference to the backend.
991  */
992 GSettingsBackend *
993 g_settings_backend_get_default (void)
994 {
995   static gsize backend;
996
997   if (g_once_init_enter (&backend))
998     {
999       GSettingsBackend *instance;
1000       GIOExtensionPoint *point;
1001       GIOExtension *extension;
1002       GType extension_type;
1003       GList *extensions;
1004       const gchar *env;
1005
1006       _g_io_modules_ensure_loaded ();
1007
1008       point = g_io_extension_point_lookup (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME);
1009       extension = NULL;
1010
1011       if ((env = getenv ("GSETTINGS_BACKEND")))
1012         {
1013           extension = g_io_extension_point_get_extension_by_name (point, env);
1014
1015           if (extension == NULL)
1016             g_warning ("Can't find GSettings backend '%s' given in "
1017                        "GSETTINGS_BACKEND environment variable", env);
1018         }
1019
1020       if (extension == NULL)
1021         {
1022           extensions = g_io_extension_point_get_extensions (point);
1023
1024           if (extensions == NULL)
1025             g_error ("No GSettingsBackend implementations exist.");
1026
1027           extension = extensions->data;
1028         }
1029
1030       extension_type = g_io_extension_get_type (extension);
1031       instance = g_object_new (extension_type, NULL);
1032
1033       g_once_init_leave (&backend, (gsize) instance);
1034     }
1035
1036   return g_object_ref ((void *) backend);
1037 }
1038
1039 /*< private >
1040  * g_settings_backend_get_permission:
1041  * @backend: a #GSettingsBackend
1042  * @path: a path
1043  * @returns: a non-%NULL #GPermission. Free with g_object_unref()
1044  *
1045  * Gets the permission object associated with writing to keys below
1046  * @path on @backend.
1047  *
1048  * If this is not implemented in the backend, then a %TRUE
1049  * #GSimplePermission is returned.
1050  */
1051 GPermission *
1052 g_settings_backend_get_permission (GSettingsBackend *backend,
1053                                    const gchar      *path)
1054 {
1055   GSettingsBackendClass *class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1056
1057   if (class->get_permission)
1058     return class->get_permission (backend, path);
1059
1060   return g_simple_permission_new (TRUE);
1061 }
1062
1063 /*< private >
1064  * g_settings_backend_sync_default:
1065  *
1066  * Syncs the default backend.
1067  */
1068 void
1069 g_settings_backend_sync_default (void)
1070 {
1071   GSettingsBackendClass *class;
1072   GSettingsBackend *backend;
1073
1074   backend = g_settings_backend_get_default ();
1075   class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1076
1077   if (class->sync)
1078     class->sync (backend);
1079 }
1080
1081 #define __G_SETTINGS_BACKEND_C__
1082 #include "gioaliasdef.c"