Merge remote branch 'gvdb/master'
[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 "giomodule-priv.h"
29 #include "gio-marshal.h"
30
31 #include <string.h>
32 #include <stdlib.h>
33 #include <glib.h>
34 #include <glibintl.h>
35
36 #include "gioalias.h"
37
38 G_DEFINE_ABSTRACT_TYPE (GSettingsBackend, g_settings_backend, G_TYPE_OBJECT)
39
40 typedef struct _GSettingsBackendWatch GSettingsBackendWatch;
41
42 struct _GSettingsBackendPrivate
43 {
44   GSettingsBackendWatch *watches;
45   gchar *context;
46 };
47
48 enum
49 {
50   PROP_0,
51   PROP_CONTEXT
52 };
53
54 /**
55  * SECTION:gsettingsbackend
56  * @title: GSettingsBackend
57  * @short_description: an interface for settings backend implementations
58  * @include: gio/gsettingsbackend.h
59  * @see_also: #GSettings, #GIOExtensionPoint
60  *
61  * The #GSettingsBackend interface defines a generic interface for
62  * non-strictly-typed data that is stored in a hierarchy. To implement
63  * an alternative storage backend for #GSettings, you need to implement
64  * the #GSettingsBackend interface and then make it implement the
65  * extension point #G_SETTINGS_BACKEND_EXTENSION_POINT_NAME.
66  *
67  * The interface defines methods for reading and writing values, a
68  * method for determining if writing of certain values will fail
69  * (lockdown) and a change notification mechanism.
70  *
71  * The semantics of the interface are very precisely defined and
72  * implementations must carefully adhere to the expectations of
73  * callers that are documented on each of the interface methods.
74  *
75  * Some of the GSettingsBackend functions accept or return a #GTree.
76  * These trees always have strings as keys and #GVariant as values.
77  * g_settings_backend_create_tree() is a convenience function to create
78  * suitable trees.
79  *
80  * <note><para>
81  * The #GSettingsBackend API is exported to allow third-party
82  * implementations, but does not carry the same stability guarantees
83  * as the public GIO API. For this reason, you have to define the
84  * C preprocessor symbol #G_SETTINGS_ENABLE_BACKEND before including
85  * <filename>gio/gsettingsbackend.h</filename>
86  * </para></note>
87  **/
88
89 struct _GSettingsBackendWatch
90 {
91   GSettingsBackendChangedFunc              changed;
92   GSettingsBackendPathChangedFunc          path_changed;
93   GSettingsBackendKeysChangedFunc          keys_changed;
94   GSettingsBackendWritableChangedFunc      writable_changed;
95   GSettingsBackendPathWritableChangedFunc  path_writable_changed;
96   gpointer                                 user_data;
97
98   GSettingsBackendWatch                   *next;
99 };
100
101 void
102 g_settings_backend_watch (GSettingsBackend                        *backend,
103                           GSettingsBackendChangedFunc              changed,
104                           GSettingsBackendPathChangedFunc          path_changed,
105                           GSettingsBackendKeysChangedFunc          keys_changed,
106                           GSettingsBackendWritableChangedFunc      writable_changed,
107                           GSettingsBackendPathWritableChangedFunc  path_writable_changed,
108                           gpointer                                 user_data)
109 {
110   GSettingsBackendWatch *watch;
111
112   watch = g_slice_new (GSettingsBackendWatch);
113   watch->changed = changed;
114   watch->path_changed = path_changed;
115   watch->keys_changed = keys_changed;
116   watch->writable_changed = writable_changed;
117   watch->path_writable_changed = path_writable_changed;
118   watch->user_data = user_data;
119
120   watch->next = backend->priv->watches;
121   backend->priv->watches = watch;
122 }
123
124 void
125 g_settings_backend_unwatch (GSettingsBackend *backend,
126                             gpointer          user_data)
127 {
128   GSettingsBackendWatch **ptr;
129
130   for (ptr = &backend->priv->watches; *ptr; ptr = &(*ptr)->next)
131     if ((*ptr)->user_data == user_data)
132       {
133         GSettingsBackendWatch *tmp = *ptr;
134
135         *ptr = tmp->next;
136         g_slice_free (GSettingsBackendWatch, tmp);
137
138         return;
139       }
140
141   g_assert_not_reached ();
142 }
143
144 static gboolean
145 is_key (const gchar *key)
146 {
147   gint length;
148   gint i;
149
150   g_return_val_if_fail (key != NULL, FALSE);
151   g_return_val_if_fail (key[0] == '/', FALSE);
152
153   for (i = 1; key[i]; i++)
154     g_return_val_if_fail (key[i] != '/' || key[i + 1] != '/', FALSE);
155
156   length = i;
157
158   g_return_val_if_fail (key[length - 1] != '/', FALSE);
159
160   return TRUE;
161 }
162
163 static gboolean
164 is_path (const gchar *path)
165 {
166   gint length;
167   gint i;
168
169   g_return_val_if_fail (path != NULL, FALSE);
170   g_return_val_if_fail (path[0] == '/', FALSE);
171
172   for (i = 1; path[i]; i++)
173     g_return_val_if_fail (path[i] != '/' || path[i + 1] != '/', FALSE);
174
175   length = i;
176
177   g_return_val_if_fail (path[length - 1] == '/', FALSE);
178
179   return TRUE;
180 }
181
182 /**
183  * g_settings_backend_changed:
184  * @backend: a #GSettingsBackend implementation
185  * @key: the name of the key
186  * @origin_tag: the origin tag
187  *
188  * Signals that a single key has possibly changed.  Backend
189  * implementations should call this if a key has possibly changed its
190  * value.
191  *
192  * @key must be a valid key (ie: starting with a slash, not containing
193  * '//', and not ending with a slash).
194  *
195  * The implementation must call this function during any call to
196  * g_settings_backend_write(), before the call returns (except in the
197  * case that no keys are actually changed and it cares to detect this
198  * fact).  It may not rely on the existence of a mainloop for
199  * dispatching the signal later.
200  *
201  * The implementation may call this function at any other time it likes
202  * in response to other events (such as changes occuring outside of the
203  * program).  These calls may originate from a mainloop or may originate
204  * in response to any other action (including from calls to
205  * g_settings_backend_write()).
206  *
207  * In the case that this call is in response to a call to
208  * g_settings_backend_write() then @origin_tag must be set to the same
209  * value that was passed to that call.
210  *
211  * Since: 2.26
212  **/
213 void
214 g_settings_backend_changed (GSettingsBackend *backend,
215                             const gchar      *key,
216                             gpointer          origin_tag)
217 {
218   GSettingsBackendWatch *watch;
219
220   g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
221   g_return_if_fail (is_key (key));
222
223   for (watch = backend->priv->watches; watch; watch = watch->next)
224     watch->changed (backend, key, origin_tag, watch->user_data);
225 }
226
227 /**
228  * g_settings_backend_keys_changed:
229  * @backend: a #GSettingsBackend implementation
230  * @path: the path containing the changes
231  * @items: the %NULL-terminated list of changed keys
232  * @origin_tag: the origin tag
233  *
234  * Signals that a list of keys have possibly changed.  Backend
235  * implementations should call this if keys have possibly changed their
236  * values.
237  *
238  * @path must be a valid path (ie: starting and ending with a slash and
239  * not containing '//').  Each string in @items must form a valid key
240  * name when @path is prefixed to it (ie: each item must not start or
241  * end with '/' and must not contain '//').
242  *
243  * The meaning of this signal is that any of the key names resulting
244  * from the contatenation of @path with each item in @items may have
245  * changed.
246  *
247  * The same rules for when notifications must occur apply as per
248  * g_settings_backend_changed().  These two calls can be used
249  * interchangeably if exactly one item has changed (although in that
250  * case g_settings_backend_changed() is definitely preferred).
251  *
252  * For efficiency reasons, the implementation should strive for @path to
253  * be as long as possible (ie: the longest common prefix of all of the
254  * keys that were changed) but this is not strictly required.
255  *
256  * Since: 2.26
257  */
258 void
259 g_settings_backend_keys_changed (GSettingsBackend    *backend,
260                                  const gchar         *path,
261                                  gchar const * const *items,
262                                  gpointer             origin_tag)
263 {
264   GSettingsBackendWatch *watch;
265
266   g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
267   g_return_if_fail (path[0] == '\0' || is_path (path));
268   g_return_if_fail (items != NULL);
269
270   for (watch = backend->priv->watches; watch; watch = watch->next)
271     watch->keys_changed (backend, path, items, origin_tag, watch->user_data);
272 }
273
274 /**
275  * g_settings_backend_keys_changed:
276  * @backend: a #GSettingsBackend implementation
277  * @path: the path containing the changes
278  * @origin_tag: the origin tag
279  *
280  * Signals that all keys below a given path may have possibly changed.
281  * Backend implementations should call this if an entire path of keys
282  * have possibly changed their values.
283  *
284  * @path must be a valid path (ie: starting and ending with a slash and
285  * not containing '//').
286  *
287  * The meaning of this signal is that any of the key which has a name
288  * starting with @path may have changed.
289  *
290  * The same rules for when notifications must occur apply as per
291  * g_settings_backend_changed().  This call might be an appropriate
292  * reasponse to a 'reset' call but implementations are also free to
293  * explicitly list the keys that were affected by that call if they can
294  * easily do so.
295  *
296  * For efficiency reasons, the implementation should strive for @path to
297  * be as long as possible (ie: the longest common prefix of all of the
298  * keys that were changed) but this is not strictly required.  As an
299  * example, if this function is called with the path of "/" then every
300  * single key in the application will be notified of a possible change.
301  *
302  * Since: 2.26
303  */
304 void
305 g_settings_backend_path_changed (GSettingsBackend *backend,
306                                  const gchar      *path,
307                                  gpointer          origin_tag)
308 {
309   GSettingsBackendWatch *watch;
310
311   g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
312   g_return_if_fail (is_path (path));
313
314   for (watch = backend->priv->watches; watch; watch = watch->next)
315     watch->path_changed (backend, path, origin_tag, watch->user_data);
316 }
317
318 /**
319  * g_settings_backend_writable_changed:
320  * @backend: a #GSettingsBackend implementation
321  * @key: the name of the key
322  *
323  * Signals that the writability of a single key has possibly changed.
324  *
325  * Since GSettings performs no locking operations for itself, this call
326  * will always be made in response to external events.
327  *
328  * Since: 2.26
329  **/
330 void
331 g_settings_backend_writable_changed (GSettingsBackend *backend,
332                                      const gchar      *key)
333 {
334   GSettingsBackendWatch *watch;
335
336   g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
337   g_return_if_fail (is_key (key));
338
339   for (watch = backend->priv->watches; watch; watch = watch->next)
340     watch->writable_changed (backend, key, watch->user_data);
341 }
342
343 /**
344  * g_settings_backend_writable_changed:
345  * @backend: a #GSettingsBackend implementation
346  * @path: the name of the path
347  *
348  * Signals that the writability of all keys below a given path may have
349  * changed.
350  *
351  * Since GSettings performs no locking operations for itself, this call
352  * will always be made in response to external events.
353  *
354  * Since: 2.26
355  **/
356 void
357 g_settings_backend_path_writable_changed (GSettingsBackend *backend,
358                                           const gchar      *path)
359 {
360   GSettingsBackendWatch *watch;
361
362   g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
363   g_return_if_fail (is_path (path));
364
365   for (watch = backend->priv->watches; watch; watch = watch->next)
366     watch->path_writable_changed (backend, path, watch->user_data);
367 }
368
369 typedef struct
370 {
371   gint prefix_len;
372   gchar *prefix;
373   gchar **items;
374 } GetKeysState;
375
376 static gboolean
377 tree_get_keys (gpointer key,
378                gpointer value,
379                gpointer user_data)
380 {
381   GetKeysState *state = user_data;
382   const gchar *skey = key;
383   gint i;
384
385   g_return_val_if_fail (is_key (key), TRUE);
386
387   /* calculate longest common prefix */
388   if (state->prefix == NULL)
389     {
390       gchar *last_byte;
391
392       /* first key?  just take the prefix up to the last '/' */
393       state->prefix = g_strdup (skey);
394       last_byte = strrchr (state->prefix, '/') + 1;
395       state->prefix_len = last_byte - state->prefix;
396       *last_byte = '\0';
397     }
398   else
399     {
400       /* find the first character that does not match.  we will
401        * definitely find one because the prefix ends in '/' and the key
402        * does not.  also: no two keys in the tree are the same.
403        */
404       for (i = 0; state->prefix[i] == skey[i]; i++);
405
406       /* check if we need to shorten the prefix */
407       if (state->prefix[i] != '\0')
408         {
409           /* find the nearest '/', terminate after it */
410           while (state->prefix[i - 1] != '/')
411             i--;
412
413           state->prefix[i] = '\0';
414           state->prefix_len = i;
415         }
416     }
417
418
419   /* save the entire item into the array.
420    * the prefixes will be removed later.
421    */
422   *state->items++ = key;
423
424   return FALSE;
425 }
426
427 /**
428  * g_settings_backend_changed_tree:
429  * @backend: a #GSettingsBackend implementation
430  * @tree: a #GTree containing the changes
431  * @origin_tag: the origin tag
432  *
433  * This call is a convenience wrapper.  It gets the list of changes from
434  * @tree, computes the longest common prefix and calls
435  * g_settings_backend_changed().
436  *
437  * Since: 2.26
438  **/
439 void
440 g_settings_backend_changed_tree (GSettingsBackend *backend,
441                                  GTree            *tree,
442                                  gpointer          origin_tag)
443 {
444   GSettingsBackendWatch *watch;
445   GetKeysState state = { 0, };
446   gchar **list;
447
448   list = g_new (gchar *, g_tree_nnodes (tree) + 1);
449   state.items = list;
450
451   g_tree_foreach (tree, tree_get_keys, &state);
452   g_return_if_fail (list + g_tree_nnodes (tree) == state.items);
453   *state.items = NULL;
454
455   while (state.items-- != list)
456     *state.items += state.prefix_len;
457
458   for (watch = backend->priv->watches; watch; watch = watch->next)
459     watch->keys_changed (backend, state.prefix,
460                          (const gchar * const *) list,
461                          origin_tag, watch->user_data);
462
463   g_free (list);
464   g_free (state.prefix);
465 }
466
467 /*< private >
468  * g_settings_backend_read:
469  * @backend: a #GSettingsBackend implementation
470  * @key: the key to read
471  * @expected_type: a #GVariantType hint
472  * @returns: the value that was read, or %NULL
473  *
474  * Reads a key. This call will never block.
475  *
476  * If the key exists, the value associated with it will be returned.
477  * If the key does not exist, %NULL will be returned.
478  *
479  * If @expected_type is given, it serves as a type hint to the backend.
480  * If you expect a key of a certain type then you should give
481  * @expected_type to increase your chances of getting it.  Some backends
482  * may ignore this argument and return values of a different type; it is
483  * mostly used by backends that don't store strong type information.
484  */
485 GVariant *
486 g_settings_backend_read (GSettingsBackend   *backend,
487                          const gchar        *key,
488                          const GVariantType *expected_type)
489 {
490   return G_SETTINGS_BACKEND_GET_CLASS (backend)
491     ->read (backend, key, expected_type);
492 }
493
494 /*< private >
495  * g_settings_backend_write:
496  * @backend: a #GSettingsBackend implementation
497  * @key: the name of the key
498  * @value: a #GVariant value to write to this key
499  * @origin_tag: the origin tag
500  *
501  * Writes exactly one key.
502  *
503  * This call does not fail.  During this call a
504  * #GSettingsBackend::changed signal will be emitted if the value of the
505  * key has changed.  The updated key value will be visible to any signal
506  * callbacks.
507  *
508  * One possible method that an implementation might deal with failures is
509  * to emit a second "changed" signal (either during this call, or later)
510  * to indicate that the affected keys have suddenly "changed back" to their
511  * old values.
512  */
513 gboolean
514 g_settings_backend_write (GSettingsBackend *backend,
515                           const gchar      *key,
516                           GVariant         *value,
517                           gpointer          origin_tag)
518 {
519   return G_SETTINGS_BACKEND_GET_CLASS (backend)
520     ->write (backend, key, value, origin_tag);
521 }
522
523 /*< private >
524  * g_settings_backend_write_keys:
525  * @backend: a #GSettingsBackend implementation
526  * @values: a #GTree containing key-value pairs to write
527  * @origin_tag: the origin tag
528  *
529  * Writes one or more keys.  This call will never block.
530  *
531  * The key of each item in the tree is the key name to write to and the
532  * value is a #GVariant to write.  The proper type of #GTree for this
533  * call can be created with g_settings_backend_create_tree().  This call
534  * might take a reference to the tree; you must not modified the #GTree
535  * after passing it to this call.
536  *
537  * This call does not fail.  During this call a #GSettingsBackend::changed
538  * signal will be emitted if any keys have been changed.  The new values of
539  * all updated keys will be visible to any signal callbacks.
540  *
541  * One possible method that an implementation might deal with failures is
542  * to emit a second "changed" signal (either during this call, or later)
543  * to indicate that the affected keys have suddenly "changed back" to their
544  * old values.
545  */
546 gboolean
547 g_settings_backend_write_keys (GSettingsBackend *backend,
548                                GTree            *tree,
549                                gpointer          origin_tag)
550 {
551   return G_SETTINGS_BACKEND_GET_CLASS (backend)
552     ->write_keys (backend, tree, origin_tag);
553 }
554
555 /*< private >
556  * g_settings_backend_reset:
557  * @backend: a #GSettingsBackend implementation
558  * @key: the name of a key
559  * @origin_tag: the origin tag
560  *
561  * "Resets" the named key to its "default" value (ie: after system-wide
562  * defaults, mandatory keys, etc. have been taken into account) or possibly
563  * unsets it.
564  */
565 void
566 g_settings_backend_reset (GSettingsBackend *backend,
567                           const gchar      *key,
568                           gpointer          origin_tag)
569 {
570   G_SETTINGS_BACKEND_GET_CLASS (backend)
571     ->reset (backend, key, origin_tag);
572 }
573
574 /*< private >
575  * g_settings_backend_reset_path:
576  * @backend: a #GSettingsBackend implementation
577  * @name: the name of a key or path
578  * @origin_tag: the origin tag
579  *
580  * "Resets" the named path.  This means that every key under the path is
581  * reset.
582  */
583 void
584 g_settings_backend_reset_path (GSettingsBackend *backend,
585                                const gchar      *path,
586                                gpointer          origin_tag)
587 {
588   G_SETTINGS_BACKEND_GET_CLASS (backend)
589     ->reset_path (backend, path, origin_tag);
590 }
591
592 /*< private >
593  * g_settings_backend_get_writable:
594  * @backend: a #GSettingsBackend implementation
595  * @key: the name of a key
596  * @returns: %TRUE if the key is writable
597  *
598  * Finds out if a key is available for writing to.  This is the
599  * interface through which 'lockdown' is implemented.  Locked down
600  * keys will have %FALSE returned by this call.
601  *
602  * You should not write to locked-down keys, but if you do, the
603  * implementation will deal with it.
604  */
605 gboolean
606 g_settings_backend_get_writable (GSettingsBackend *backend,
607                                  const gchar      *key)
608 {
609   return G_SETTINGS_BACKEND_GET_CLASS (backend)
610     ->get_writable (backend, key);
611 }
612
613 /*< private >
614  * g_settings_backend_unsubscribe:
615  * @backend: a #GSettingsBackend
616  * @name: a key or path to subscribe to
617  *
618  * Reverses the effect of a previous call to
619  * g_settings_backend_subscribe().
620  */
621 void
622 g_settings_backend_unsubscribe (GSettingsBackend *backend,
623                                 const char       *name)
624 {
625   G_SETTINGS_BACKEND_GET_CLASS (backend)
626     ->unsubscribe (backend, name);
627 }
628
629 /*< private >
630  * g_settings_backend_subscribe:
631  * @backend: a #GSettingsBackend
632  * @name: a key or path to subscribe to
633  *
634  * Requests that change signals be emitted for events on @name.
635  */
636 void
637 g_settings_backend_subscribe (GSettingsBackend *backend,
638                               const gchar      *name)
639 {
640   G_SETTINGS_BACKEND_GET_CLASS (backend)
641     ->subscribe (backend, name);
642 }
643
644 static void
645 g_settings_backend_set_property (GObject         *object,
646                                  guint            prop_id,
647                                  const GValue    *value,
648                                  GParamSpec      *pspec)
649 {
650   GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
651
652   switch (prop_id)
653     {
654     case PROP_CONTEXT:
655       backend->priv->context = g_value_dup_string (value);
656       break;
657
658     default:
659       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
660       break;
661     }
662 }
663
664 static void
665 g_settings_backend_get_property (GObject    *object,
666                                  guint       prop_id,
667                                  GValue     *value,
668                                  GParamSpec *pspec)
669 {
670   GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
671
672   switch (prop_id)
673     {
674     case PROP_CONTEXT:
675       g_value_set_string (value, backend->priv->context);
676       break;
677
678     default:
679       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
680       break;
681     }
682 }
683
684 static void
685 g_settings_backend_finalize (GObject *object)
686 {
687   GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
688
689   g_free (backend->priv->context);
690
691   G_OBJECT_CLASS (g_settings_backend_parent_class)->finalize (object);
692 }
693
694 static void
695 ignore_subscription (GSettingsBackend *backend,
696                      const gchar      *key)
697 {
698 }
699
700 static void
701 g_settings_backend_init (GSettingsBackend *backend)
702 {
703   backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend,
704                                                G_TYPE_SETTINGS_BACKEND,
705                                                GSettingsBackendPrivate);
706 }
707
708 static void
709 g_settings_backend_class_init (GSettingsBackendClass *class)
710 {
711   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
712
713   class->subscribe = ignore_subscription;
714   class->unsubscribe = ignore_subscription;
715
716   gobject_class->get_property = g_settings_backend_get_property;
717   gobject_class->set_property = g_settings_backend_set_property;
718   gobject_class->finalize = g_settings_backend_finalize;
719
720   g_type_class_add_private (class, sizeof (GSettingsBackendPrivate));
721
722   /**
723    * GSettingsBackend:context:
724    *
725    * The "context" property gives a hint to the backend as to
726    * what storage to use. It is up to the implementation to make
727    * use of this information.
728    *
729    * E.g. DConf supports "user", "system", "defaults" and "login"
730    * contexts.
731    *
732    * If your backend supports different contexts, you should also
733    * provide an implementation of the supports_context() class
734    * function in #GSettingsBackendClass.
735    *
736    * Since: 2.26
737    */
738   g_object_class_install_property (gobject_class, PROP_CONTEXT,
739     g_param_spec_string ("context", P_("Context"),
740                          P_("An identifier to decide which storage to use"),
741                          "", G_PARAM_READWRITE |
742                          G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
743
744 }
745
746 /*< private >
747  * g_settings_backend_create_tree:
748  * @returns: a new #GTree
749  *
750  * This is a convenience function for creating a tree that is compatible
751  * with g_settings_backend_write().  It merely calls g_tree_new_full()
752  * with strcmp(), g_free() and g_variant_unref().
753  */
754 GTree *
755 g_settings_backend_create_tree (void)
756 {
757   return g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
758                           g_free, (GDestroyNotify) g_variant_unref);
759 }
760
761
762 static gpointer
763 get_default_backend (const gchar *context)
764 {
765   GIOExtension *extension = NULL;
766   GIOExtensionPoint *point;
767   GList *extensions;
768   const gchar *env;
769   GType type;
770
771   _g_io_modules_ensure_loaded ();
772
773   point = g_io_extension_point_lookup (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME);
774
775   if ((env = getenv ("GSETTINGS_BACKEND")))
776     {
777       extension = g_io_extension_point_get_extension_by_name (point, env);
778
779       if (extension == NULL)
780         g_warning ("Can't find GSettings backend '%s' given in "
781                    "GSETTINGS_BACKEND environment variable", env);
782     }
783
784   if (extension == NULL)
785     {
786       extensions = g_io_extension_point_get_extensions (point);
787
788       if (extensions == NULL)
789         g_error ("No GSettingsBackend implementations exist.");
790
791       extension = extensions->data;
792     }
793
794   if (context[0] != '\0') /* (context != "") */
795     {
796       GSettingsBackendClass *backend_class;
797       GTypeClass *class;
798
799       class = g_io_extension_ref_class (extension);
800       backend_class = G_SETTINGS_BACKEND_CLASS (class);
801
802       if (backend_class->supports_context == NULL ||
803           !backend_class->supports_context (context))
804         {
805           g_type_class_unref (class);
806           return NULL;
807         }
808
809       g_type_class_unref (class);
810     }
811
812   type = g_io_extension_get_type (extension);
813
814   return g_object_new (type, "context", context, NULL);
815 }
816
817 /*< private >
818  * g_settings_backend_get_with_context:
819  * @context: a context that might be used by the backend to determine
820  *     which storage to use, or %NULL to use the default storage
821  * @returns: the default #GSettingsBackend
822  *
823  * Returns the default #GSettingsBackend. It is possible to override
824  * the default by setting the <envar>GSETTINGS_BACKEND</envar>
825  * environment variable to the name of a settings backend.
826  *
827  * The @context parameter can be used to indicate that a different
828  * than the default storage is desired. E.g. the DConf backend lets
829  * you use "user", "system", "defaults" and "login" as contexts.
830  *
831  * If @context is not supported by the implementation, this function
832  * returns an instance of the #GSettingsMemoryBackend.
833  * See g_settings_backend_supports_context(),
834  *
835  * The user does not own the return value and it must not be freed.
836  */
837 GSettingsBackend *
838 g_settings_backend_get_with_context (const gchar *context)
839 {
840   static GHashTable *backends;
841   GSettingsBackend *backend;
842
843   g_return_val_if_fail (context != NULL, NULL);
844
845   _g_io_modules_ensure_extension_points_registered ();
846
847   if (!backends)
848     backends = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
849
850   backend = g_hash_table_lookup (backends, context);
851
852   if (!backend)
853     {
854       backend = get_default_backend (context);
855
856       if (!backend)
857         backend = g_null_settings_backend_new ();
858
859       g_hash_table_insert (backends, g_strdup (context), backend);
860     }
861
862   return g_object_ref (backend);
863 }
864
865 /*< private >
866  * g_settings_backend_supports_context:
867  * @context: a context string that might be passed to
868  *     g_settings_backend_new_with_context()
869  * @returns: #TRUE if @context is supported
870  *
871  * Determines if the given context is supported by the default
872  * GSettingsBackend implementation.
873  */
874 gboolean
875 g_settings_backend_supports_context (const gchar *context)
876 {
877   GSettingsBackend *backend;
878
879   g_return_val_if_fail (context != NULL, NULL);
880
881   backend = get_default_backend (context);
882
883   if (backend)
884     {
885       g_object_unref (backend);
886       return TRUE;
887     }
888
889   return FALSE;
890 }
891
892 #define __G_SETTINGS_BACKEND_C__
893 #include "gioaliasdef.c"