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