GSettingsBackend API/ABI change
[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_path_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_path_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   const gchar **keys;
372   GVariant **values;
373   gint prefix_len;
374   gchar *prefix;
375 } FlattenState;
376
377 static gboolean
378 g_settings_backend_flatten_one (gpointer key,
379                                 gpointer value,
380                                 gpointer user_data)
381 {
382   FlattenState *state = user_data;
383   const gchar *skey = key;
384   gint i;
385
386   g_return_val_if_fail (is_key (key), TRUE);
387
388   /* calculate longest common prefix */
389   if (state->prefix == NULL)
390     {
391       gchar *last_byte;
392
393       /* first key?  just take the prefix up to the last '/' */
394       state->prefix = g_strdup (skey);
395       last_byte = strrchr (state->prefix, '/') + 1;
396       state->prefix_len = last_byte - state->prefix;
397       *last_byte = '\0';
398     }
399   else
400     {
401       /* find the first character that does not match.  we will
402        * definitely find one because the prefix ends in '/' and the key
403        * does not.  also: no two keys in the tree are the same.
404        */
405       for (i = 0; state->prefix[i] == skey[i]; i++);
406
407       /* check if we need to shorten the prefix */
408       if (state->prefix[i] != '\0')
409         {
410           /* find the nearest '/', terminate after it */
411           while (state->prefix[i - 1] != '/')
412             i--;
413
414           state->prefix[i] = '\0';
415           state->prefix_len = i;
416         }
417     }
418
419
420   /* save the entire item into the array.
421    * the prefixes will be removed later.
422    */
423   *state->keys++ = key;
424
425   if (state->values)
426     *state->values++ = value;
427
428   return FALSE;
429 }
430
431 /**
432  * g_settings_backend_flatten_tree:
433  * @tree: a #GTree containing the changes
434  * @path: the location to save the path
435  * @keys: the location to save the relative keys
436  * @values: the location to save the values, or %NULL
437  *
438  * Calculate the longest common prefix of all keys in a tree and write
439  * out an array of the key names relative to that prefix and,
440  * optionally, the value to store at each of those keys.
441  *
442  * You must free the value returned in @path, @keys and @values using
443  * g_free().  You should not attempt to free or unref the contents of
444  * @keys or @values.
445  *
446  * Since: 2.26
447  **/
448 void
449 g_settings_backend_flatten_tree (GTree         *tree,
450                                  gchar        **path,
451                                  const gchar ***keys,
452                                  GVariant    ***values)
453 {
454   FlattenState state = { 0, };
455   gsize nnodes;
456   gsize i;
457
458   nnodes = g_tree_nnodes (tree);
459
460   *keys = state.keys = g_new (const gchar *, nnodes + 1);
461   state.keys[nnodes] = NULL;
462
463   if (values != NULL)
464     {
465       *values = state.values = g_new (GVariant *, nnodes + 1);
466       state.values[nnodes] = NULL;
467     }
468
469   g_tree_foreach (tree, g_settings_backend_flatten_one, &state);
470   g_return_if_fail (*keys + nnodes == state.keys);
471
472   *path = state.prefix;
473   for (i = 0; i < nnodes; i++)
474     state.keys[i] += state.prefix_len;
475 }
476
477 /**
478  * g_settings_backend_changed_tree:
479  * @backend: a #GSettingsBackend implementation
480  * @tree: a #GTree containing the changes
481  * @origin_tag: the origin tag
482  *
483  * This call is a convenience wrapper.  It gets the list of changes from
484  * @tree, computes the longest common prefix and calls
485  * g_settings_backend_changed().
486  *
487  * Since: 2.26
488  **/
489 void
490 g_settings_backend_changed_tree (GSettingsBackend *backend,
491                                  GTree            *tree,
492                                  gpointer          origin_tag)
493 {
494   GSettingsBackendWatch *watch;
495   const gchar **keys;
496   gchar *path;
497
498   g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
499
500   g_settings_backend_flatten_tree (tree, &path, &keys, NULL);
501
502   for (watch = backend->priv->watches; watch; watch = watch->next)
503     watch->keys_changed (backend, path, keys, origin_tag, watch->user_data);
504
505   g_free (path);
506   g_free (keys);
507 }
508
509 /*< private >
510  * g_settings_backend_read:
511  * @backend: a #GSettingsBackend implementation
512  * @key: the key to read
513  * @expected_type: a #GVariantType hint
514  * @returns: the value that was read, or %NULL
515  *
516  * Reads a key. This call will never block.
517  *
518  * If the key exists, the value associated with it will be returned.
519  * If the key does not exist, %NULL will be returned.
520  *
521  * If @expected_type is given, it serves as a type hint to the backend.
522  * If you expect a key of a certain type then you should give
523  * @expected_type to increase your chances of getting it.  Some backends
524  * may ignore this argument and return values of a different type; it is
525  * mostly used by backends that don't store strong type information.
526  */
527 GVariant *
528 g_settings_backend_read (GSettingsBackend   *backend,
529                          const gchar        *key,
530                          const GVariantType *expected_type,
531                          gboolean            default_value)
532 {
533   return G_SETTINGS_BACKEND_GET_CLASS (backend)
534     ->read (backend, key, expected_type, default_value);
535 }
536
537 /*< private >
538  * g_settings_backend_write:
539  * @backend: a #GSettingsBackend implementation
540  * @key: the name of the key
541  * @value: a #GVariant value to write to this key
542  * @origin_tag: the origin tag
543  * @returns: %TRUE if the write succeeded, %FALSE if the key was not writable
544  *
545  * Writes exactly one key.
546  *
547  * This call does not fail.  During this call a
548  * #GSettingsBackend::changed signal will be emitted if the value of the
549  * key has changed.  The updated key value will be visible to any signal
550  * callbacks.
551  *
552  * One possible method that an implementation might deal with failures is
553  * to emit a second "changed" signal (either during this call, or later)
554  * to indicate that the affected keys have suddenly "changed back" to their
555  * old values.
556  */
557 gboolean
558 g_settings_backend_write (GSettingsBackend *backend,
559                           const gchar      *key,
560                           GVariant         *value,
561                           gpointer          origin_tag)
562 {
563   return G_SETTINGS_BACKEND_GET_CLASS (backend)
564     ->write (backend, key, value, origin_tag);
565 }
566
567 /*< private >
568  * g_settings_backend_write_keys:
569  * @backend: a #GSettingsBackend implementation
570  * @values: a #GTree containing key-value pairs to write
571  * @origin_tag: the origin tag
572  *
573  * Writes one or more keys.  This call will never block.
574  *
575  * The key of each item in the tree is the key name to write to and the
576  * value is a #GVariant to write.  The proper type of #GTree for this
577  * call can be created with g_settings_backend_create_tree().  This call
578  * might take a reference to the tree; you must not modified the #GTree
579  * after passing it to this call.
580  *
581  * This call does not fail.  During this call a #GSettingsBackend::changed
582  * signal will be emitted if any keys have been changed.  The new values of
583  * all updated keys will be visible to any signal callbacks.
584  *
585  * One possible method that an implementation might deal with failures is
586  * to emit a second "changed" signal (either during this call, or later)
587  * to indicate that the affected keys have suddenly "changed back" to their
588  * old values.
589  */
590 gboolean
591 g_settings_backend_write_keys (GSettingsBackend *backend,
592                                GTree            *tree,
593                                gpointer          origin_tag)
594 {
595   return G_SETTINGS_BACKEND_GET_CLASS (backend)
596     ->write_keys (backend, tree, origin_tag);
597 }
598
599 /*< private >
600  * g_settings_backend_reset:
601  * @backend: a #GSettingsBackend implementation
602  * @key: the name of a key
603  * @origin_tag: the origin tag
604  *
605  * "Resets" the named key to its "default" value (ie: after system-wide
606  * defaults, mandatory keys, etc. have been taken into account) or possibly
607  * unsets it.
608  */
609 void
610 g_settings_backend_reset (GSettingsBackend *backend,
611                           const gchar      *key,
612                           gpointer          origin_tag)
613 {
614   G_SETTINGS_BACKEND_GET_CLASS (backend)
615     ->reset (backend, key, origin_tag);
616 }
617
618 /*< private >
619  * g_settings_backend_reset_path:
620  * @backend: a #GSettingsBackend implementation
621  * @name: the name of a key or path
622  * @origin_tag: the origin tag
623  *
624  * "Resets" the named path.  This means that every key under the path is
625  * reset.
626  */
627 void
628 g_settings_backend_reset_path (GSettingsBackend *backend,
629                                const gchar      *path,
630                                gpointer          origin_tag)
631 {
632   G_SETTINGS_BACKEND_GET_CLASS (backend)
633     ->reset_path (backend, path, origin_tag);
634 }
635
636 /*< private >
637  * g_settings_backend_get_writable:
638  * @backend: a #GSettingsBackend implementation
639  * @key: the name of a key
640  * @returns: %TRUE if the key is writable
641  *
642  * Finds out if a key is available for writing to.  This is the
643  * interface through which 'lockdown' is implemented.  Locked down
644  * keys will have %FALSE returned by this call.
645  *
646  * You should not write to locked-down keys, but if you do, the
647  * implementation will deal with it.
648  */
649 gboolean
650 g_settings_backend_get_writable (GSettingsBackend *backend,
651                                  const gchar      *key)
652 {
653   return G_SETTINGS_BACKEND_GET_CLASS (backend)
654     ->get_writable (backend, key);
655 }
656
657 /*< private >
658  * g_settings_backend_unsubscribe:
659  * @backend: a #GSettingsBackend
660  * @name: a key or path to subscribe to
661  *
662  * Reverses the effect of a previous call to
663  * g_settings_backend_subscribe().
664  */
665 void
666 g_settings_backend_unsubscribe (GSettingsBackend *backend,
667                                 const char       *name)
668 {
669   G_SETTINGS_BACKEND_GET_CLASS (backend)
670     ->unsubscribe (backend, name);
671 }
672
673 /*< private >
674  * g_settings_backend_subscribe:
675  * @backend: a #GSettingsBackend
676  * @name: a key or path to subscribe to
677  *
678  * Requests that change signals be emitted for events on @name.
679  */
680 void
681 g_settings_backend_subscribe (GSettingsBackend *backend,
682                               const gchar      *name)
683 {
684   G_SETTINGS_BACKEND_GET_CLASS (backend)
685     ->subscribe (backend, name);
686 }
687
688 static void
689 g_settings_backend_set_property (GObject         *object,
690                                  guint            prop_id,
691                                  const GValue    *value,
692                                  GParamSpec      *pspec)
693 {
694   GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
695
696   switch (prop_id)
697     {
698     case PROP_CONTEXT:
699       backend->priv->context = g_value_dup_string (value);
700       break;
701
702     default:
703       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
704       break;
705     }
706 }
707
708 static void
709 g_settings_backend_get_property (GObject    *object,
710                                  guint       prop_id,
711                                  GValue     *value,
712                                  GParamSpec *pspec)
713 {
714   GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
715
716   switch (prop_id)
717     {
718     case PROP_CONTEXT:
719       g_value_set_string (value, backend->priv->context);
720       break;
721
722     default:
723       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
724       break;
725     }
726 }
727
728 static void
729 g_settings_backend_finalize (GObject *object)
730 {
731   GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
732
733   g_free (backend->priv->context);
734
735   G_OBJECT_CLASS (g_settings_backend_parent_class)->finalize (object);
736 }
737
738 static void
739 ignore_subscription (GSettingsBackend *backend,
740                      const gchar      *key)
741 {
742 }
743
744 static void
745 g_settings_backend_init (GSettingsBackend *backend)
746 {
747   backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend,
748                                                G_TYPE_SETTINGS_BACKEND,
749                                                GSettingsBackendPrivate);
750 }
751
752 static void
753 g_settings_backend_class_init (GSettingsBackendClass *class)
754 {
755   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
756
757   class->subscribe = ignore_subscription;
758   class->unsubscribe = ignore_subscription;
759
760   gobject_class->get_property = g_settings_backend_get_property;
761   gobject_class->set_property = g_settings_backend_set_property;
762   gobject_class->finalize = g_settings_backend_finalize;
763
764   g_type_class_add_private (class, sizeof (GSettingsBackendPrivate));
765
766   /**
767    * GSettingsBackend:context:
768    *
769    * The "context" property gives a hint to the backend as to
770    * what storage to use. It is up to the implementation to make
771    * use of this information.
772    *
773    * E.g. DConf supports "user", "system", "defaults" and "login"
774    * contexts.
775    *
776    * If your backend supports different contexts, you should also
777    * provide an implementation of the supports_context() class
778    * function in #GSettingsBackendClass.
779    */
780   g_object_class_install_property (gobject_class, PROP_CONTEXT,
781     g_param_spec_string ("context", P_("Context"),
782                          P_("An identifier to decide which storage to use"),
783                          "", G_PARAM_READWRITE |
784                          G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
785
786 }
787
788 /*< private >
789  * g_settings_backend_create_tree:
790  * @returns: a new #GTree
791  *
792  * This is a convenience function for creating a tree that is compatible
793  * with g_settings_backend_write().  It merely calls g_tree_new_full()
794  * with strcmp(), g_free() and g_variant_unref().
795  */
796 GTree *
797 g_settings_backend_create_tree (void)
798 {
799   return g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
800                           g_free, (GDestroyNotify) g_variant_unref);
801 }
802
803
804 static gpointer
805 get_default_backend (const gchar *context)
806 {
807   GIOExtension *extension = NULL;
808   GIOExtensionPoint *point;
809   GList *extensions;
810   const gchar *env;
811   GType type;
812
813   _g_io_modules_ensure_loaded ();
814
815   point = g_io_extension_point_lookup (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME);
816
817   if ((env = getenv ("GSETTINGS_BACKEND")))
818     {
819       extension = g_io_extension_point_get_extension_by_name (point, env);
820
821       if (extension == NULL)
822         g_warning ("Can't find GSettings backend '%s' given in "
823                    "GSETTINGS_BACKEND environment variable", env);
824     }
825
826   if (extension == NULL)
827     {
828       extensions = g_io_extension_point_get_extensions (point);
829
830       if (extensions == NULL)
831         g_error ("No GSettingsBackend implementations exist.");
832
833       extension = extensions->data;
834     }
835
836   if (context[0] != '\0') /* (context != "") */
837     {
838       GSettingsBackendClass *backend_class;
839       GTypeClass *class;
840
841       class = g_io_extension_ref_class (extension);
842       backend_class = G_SETTINGS_BACKEND_CLASS (class);
843
844       if (backend_class->supports_context == NULL ||
845           !backend_class->supports_context (context))
846         {
847           g_type_class_unref (class);
848           return NULL;
849         }
850
851       g_type_class_unref (class);
852     }
853
854   type = g_io_extension_get_type (extension);
855
856   return g_object_new (type, "context", context, NULL);
857 }
858
859 static GHashTable *g_settings_backends;
860
861 /*< private >
862  * g_settings_backend_get_with_context:
863  * @context: a context that might be used by the backend to determine
864  *     which storage to use, or %NULL to use the default storage
865  * @returns: the default #GSettingsBackend
866  *
867  * Returns the default #GSettingsBackend. It is possible to override
868  * the default by setting the <envar>GSETTINGS_BACKEND</envar>
869  * environment variable to the name of a settings backend.
870  *
871  * The @context parameter can be used to indicate that a different
872  * than the default storage is desired. E.g. the DConf backend lets
873  * you use "user", "system", "defaults" and "login" as contexts.
874  *
875  * If @context is not supported by the implementation, this function
876  * returns an instance of the #GSettingsMemoryBackend.
877  * See g_settings_backend_supports_context(),
878  *
879  * The user does not own the return value and it must not be freed.
880  */
881 GSettingsBackend *
882 g_settings_backend_get_with_context (const gchar *context)
883 {
884   GSettingsBackend *backend;
885
886   g_return_val_if_fail (context != NULL, NULL);
887
888   _g_io_modules_ensure_extension_points_registered ();
889
890   if (g_settings_backends == NULL)
891     g_settings_backends = g_hash_table_new (g_str_hash, g_str_equal);
892
893   backend = g_hash_table_lookup (g_settings_backends, context);
894
895   if (!backend)
896     {
897       backend = get_default_backend (context);
898
899       if (!backend)
900         backend = g_null_settings_backend_new ();
901
902       g_hash_table_insert (g_settings_backends, g_strdup (context), backend);
903     }
904
905   return g_object_ref (backend);
906 }
907
908 /*< private >
909  * g_settings_backend_supports_context:
910  * @context: a context string that might be passed to
911  *     g_settings_backend_new_with_context()
912  * @returns: #TRUE if @context is supported
913  *
914  * Determines if the given context is supported by the default
915  * GSettingsBackend implementation.
916  */
917 gboolean
918 g_settings_backend_supports_context (const gchar *context)
919 {
920   GSettingsBackend *backend;
921
922   g_return_val_if_fail (context != NULL, FALSE);
923
924   backend = get_default_backend (context);
925
926   if (backend)
927     {
928       g_object_unref (backend);
929       return TRUE;
930     }
931
932   return FALSE;
933 }
934
935 /**
936  * g_settings_backend_setup:
937  * @context: a context string (not %NULL or "")
938  * @backend: a #GSettingsBackend
939  *
940  * Sets up @backend for use with #GSettings.
941  *
942  * If you create a #GSettings with its context property set to @context
943  * then it will use the backend given to this function.  See
944  * g_settings_new_with_context().
945  *
946  * The backend must be set up before any settings objects are created
947  * for the named context.
948  *
949  * It is not possible to specify a backend for the default context.
950  *
951  * This function takes a reference on @backend and never releases it.
952  *
953  * Since: 2.26
954  **/
955 void
956 g_settings_backend_setup (const gchar      *context,
957                           GSettingsBackend *backend)
958 {
959   g_return_if_fail (context[0] != '\0');
960   g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
961
962   if (g_settings_backends == NULL)
963     g_settings_backends = g_hash_table_new (g_str_hash, g_str_equal);
964
965   if (g_hash_table_lookup (g_settings_backends, context))
966     g_error ("A GSettingsBackend already exists for context '%s'", context);
967
968   g_hash_table_insert (g_settings_backends,
969                        g_strdup (context),
970                        g_object_ref (backend));
971 }
972
973 #define __G_SETTINGS_BACKEND_C__
974 #include "gioaliasdef.c"