2 * Copyright © 2009, 2010 Codethink Limited
3 * Copyright © 2010 Red Hat, Inc.
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.
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.
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.
20 * Authors: Ryan Lortie <desrt@desrt.ca>
21 * Matthias Clasen <mclasen@redhat.com>
26 #include "gsettingsbackendinternal.h"
27 #include "gnullsettingsbackend.h"
28 #include "giomodule-priv.h"
29 #include "gio-marshal.h"
38 G_DEFINE_ABSTRACT_TYPE (GSettingsBackend, g_settings_backend, G_TYPE_OBJECT)
40 typedef struct _GSettingsBackendWatch GSettingsBackendWatch;
42 struct _GSettingsBackendPrivate
44 GSettingsBackendWatch *watches;
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
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.
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.
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.
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
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>
89 struct _GSettingsBackendWatch
91 GSettingsBackendChangedFunc changed;
92 GSettingsBackendPathChangedFunc path_changed;
93 GSettingsBackendKeysChangedFunc keys_changed;
94 GSettingsBackendWritableChangedFunc writable_changed;
95 GSettingsBackendPathWritableChangedFunc path_writable_changed;
98 GSettingsBackendWatch *next;
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,
110 GSettingsBackendWatch *watch;
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;
120 watch->next = backend->priv->watches;
121 backend->priv->watches = watch;
125 g_settings_backend_unwatch (GSettingsBackend *backend,
128 GSettingsBackendWatch **ptr;
130 for (ptr = &backend->priv->watches; *ptr; ptr = &(*ptr)->next)
131 if ((*ptr)->user_data == user_data)
133 GSettingsBackendWatch *tmp = *ptr;
136 g_slice_free (GSettingsBackendWatch, tmp);
141 g_assert_not_reached ();
145 is_key (const gchar *key)
150 g_return_val_if_fail (key != NULL, FALSE);
151 g_return_val_if_fail (key[0] == '/', FALSE);
153 for (i = 1; key[i]; i++)
154 g_return_val_if_fail (key[i] != '/' || key[i + 1] != '/', FALSE);
158 g_return_val_if_fail (key[length - 1] != '/', FALSE);
164 is_path (const gchar *path)
169 g_return_val_if_fail (path != NULL, FALSE);
170 g_return_val_if_fail (path[0] == '/', FALSE);
172 for (i = 1; path[i]; i++)
173 g_return_val_if_fail (path[i] != '/' || path[i + 1] != '/', FALSE);
177 g_return_val_if_fail (path[length - 1] == '/', FALSE);
183 * g_settings_backend_changed:
184 * @backend: a #GSettingsBackend implementation
185 * @key: the name of the key
186 * @origin_tag: the origin tag
188 * Signals that a single key has possibly changed. Backend
189 * implementations should call this if a key has possibly changed its
192 * @key must be a valid key (ie: starting with a slash, not containing
193 * '//', and not ending with a slash).
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.
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()).
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.
214 g_settings_backend_changed (GSettingsBackend *backend,
218 GSettingsBackendWatch *watch;
220 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
221 g_return_if_fail (is_key (key));
223 for (watch = backend->priv->watches; watch; watch = watch->next)
224 watch->changed (backend, key, origin_tag, watch->user_data);
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
234 * Signals that a list of keys have possibly changed. Backend
235 * implementations should call this if keys have possibly changed their
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 '//').
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
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).
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.
259 g_settings_backend_keys_changed (GSettingsBackend *backend,
261 gchar const * const *items,
264 GSettingsBackendWatch *watch;
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);
270 for (watch = backend->priv->watches; watch; watch = watch->next)
271 watch->keys_changed (backend, path, items, origin_tag, watch->user_data);
275 * g_settings_backend_path_changed:
276 * @backend: a #GSettingsBackend implementation
277 * @path: the path containing the changes
278 * @origin_tag: the origin tag
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.
284 * @path must be a valid path (ie: starting and ending with a slash and
285 * not containing '//').
287 * The meaning of this signal is that any of the key which has a name
288 * starting with @path may have changed.
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
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.
305 g_settings_backend_path_changed (GSettingsBackend *backend,
309 GSettingsBackendWatch *watch;
311 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
312 g_return_if_fail (is_path (path));
314 for (watch = backend->priv->watches; watch; watch = watch->next)
315 watch->path_changed (backend, path, origin_tag, watch->user_data);
319 * g_settings_backend_writable_changed:
320 * @backend: a #GSettingsBackend implementation
321 * @key: the name of the key
323 * Signals that the writability of a single key has possibly changed.
325 * Since GSettings performs no locking operations for itself, this call
326 * will always be made in response to external events.
331 g_settings_backend_writable_changed (GSettingsBackend *backend,
334 GSettingsBackendWatch *watch;
336 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
337 g_return_if_fail (is_key (key));
339 for (watch = backend->priv->watches; watch; watch = watch->next)
340 watch->writable_changed (backend, key, watch->user_data);
344 * g_settings_backend_path_writable_changed:
345 * @backend: a #GSettingsBackend implementation
346 * @path: the name of the path
348 * Signals that the writability of all keys below a given path may have
351 * Since GSettings performs no locking operations for itself, this call
352 * will always be made in response to external events.
357 g_settings_backend_path_writable_changed (GSettingsBackend *backend,
360 GSettingsBackendWatch *watch;
362 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
363 g_return_if_fail (is_path (path));
365 for (watch = backend->priv->watches; watch; watch = watch->next)
366 watch->path_writable_changed (backend, path, watch->user_data);
377 tree_get_keys (gpointer key,
381 GetKeysState *state = user_data;
382 const gchar *skey = key;
385 g_return_val_if_fail (is_key (key), TRUE);
387 /* calculate longest common prefix */
388 if (state->prefix == NULL)
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;
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.
404 for (i = 0; state->prefix[i] == skey[i]; i++);
406 /* check if we need to shorten the prefix */
407 if (state->prefix[i] != '\0')
409 /* find the nearest '/', terminate after it */
410 while (state->prefix[i - 1] != '/')
413 state->prefix[i] = '\0';
414 state->prefix_len = i;
419 /* save the entire item into the array.
420 * the prefixes will be removed later.
422 *state->items++ = key;
428 * g_settings_backend_changed_tree:
429 * @backend: a #GSettingsBackend implementation
430 * @tree: a #GTree containing the changes
431 * @origin_tag: the origin tag
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().
440 g_settings_backend_changed_tree (GSettingsBackend *backend,
444 GSettingsBackendWatch *watch;
445 GetKeysState state = { 0, };
448 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
450 list = g_new (gchar *, g_tree_nnodes (tree) + 1);
453 g_tree_foreach (tree, tree_get_keys, &state);
454 g_return_if_fail (list + g_tree_nnodes (tree) == state.items);
457 while (state.items-- != list)
458 *state.items += state.prefix_len;
460 for (watch = backend->priv->watches; watch; watch = watch->next)
461 watch->keys_changed (backend, state.prefix,
462 (const gchar * const *) list,
463 origin_tag, watch->user_data);
466 g_free (state.prefix);
470 * g_settings_backend_read:
471 * @backend: a #GSettingsBackend implementation
472 * @key: the key to read
473 * @expected_type: a #GVariantType hint
474 * @returns: the value that was read, or %NULL
476 * Reads a key. This call will never block.
478 * If the key exists, the value associated with it will be returned.
479 * If the key does not exist, %NULL will be returned.
481 * If @expected_type is given, it serves as a type hint to the backend.
482 * If you expect a key of a certain type then you should give
483 * @expected_type to increase your chances of getting it. Some backends
484 * may ignore this argument and return values of a different type; it is
485 * mostly used by backends that don't store strong type information.
488 g_settings_backend_read (GSettingsBackend *backend,
490 const GVariantType *expected_type)
492 return G_SETTINGS_BACKEND_GET_CLASS (backend)
493 ->read (backend, key, expected_type);
497 * g_settings_backend_write:
498 * @backend: a #GSettingsBackend implementation
499 * @key: the name of the key
500 * @value: a #GVariant value to write to this key
501 * @origin_tag: the origin tag
502 * @returns: %TRUE if the write succeeded, %FALSE if the key was not writable
504 * Writes exactly one key.
506 * This call does not fail. During this call a
507 * #GSettingsBackend::changed signal will be emitted if the value of the
508 * key has changed. The updated key value will be visible to any signal
511 * One possible method that an implementation might deal with failures is
512 * to emit a second "changed" signal (either during this call, or later)
513 * to indicate that the affected keys have suddenly "changed back" to their
517 g_settings_backend_write (GSettingsBackend *backend,
522 return G_SETTINGS_BACKEND_GET_CLASS (backend)
523 ->write (backend, key, value, origin_tag);
527 * g_settings_backend_write_keys:
528 * @backend: a #GSettingsBackend implementation
529 * @values: a #GTree containing key-value pairs to write
530 * @origin_tag: the origin tag
532 * Writes one or more keys. This call will never block.
534 * The key of each item in the tree is the key name to write to and the
535 * value is a #GVariant to write. The proper type of #GTree for this
536 * call can be created with g_settings_backend_create_tree(). This call
537 * might take a reference to the tree; you must not modified the #GTree
538 * after passing it to this call.
540 * This call does not fail. During this call a #GSettingsBackend::changed
541 * signal will be emitted if any keys have been changed. The new values of
542 * all updated keys will be visible to any signal callbacks.
544 * One possible method that an implementation might deal with failures is
545 * to emit a second "changed" signal (either during this call, or later)
546 * to indicate that the affected keys have suddenly "changed back" to their
550 g_settings_backend_write_keys (GSettingsBackend *backend,
554 return G_SETTINGS_BACKEND_GET_CLASS (backend)
555 ->write_keys (backend, tree, origin_tag);
559 * g_settings_backend_reset:
560 * @backend: a #GSettingsBackend implementation
561 * @key: the name of a key
562 * @origin_tag: the origin tag
564 * "Resets" the named key to its "default" value (ie: after system-wide
565 * defaults, mandatory keys, etc. have been taken into account) or possibly
569 g_settings_backend_reset (GSettingsBackend *backend,
573 G_SETTINGS_BACKEND_GET_CLASS (backend)
574 ->reset (backend, key, origin_tag);
578 * g_settings_backend_reset_path:
579 * @backend: a #GSettingsBackend implementation
580 * @name: the name of a key or path
581 * @origin_tag: the origin tag
583 * "Resets" the named path. This means that every key under the path is
587 g_settings_backend_reset_path (GSettingsBackend *backend,
591 G_SETTINGS_BACKEND_GET_CLASS (backend)
592 ->reset_path (backend, path, origin_tag);
596 * g_settings_backend_get_writable:
597 * @backend: a #GSettingsBackend implementation
598 * @key: the name of a key
599 * @returns: %TRUE if the key is writable
601 * Finds out if a key is available for writing to. This is the
602 * interface through which 'lockdown' is implemented. Locked down
603 * keys will have %FALSE returned by this call.
605 * You should not write to locked-down keys, but if you do, the
606 * implementation will deal with it.
609 g_settings_backend_get_writable (GSettingsBackend *backend,
612 return G_SETTINGS_BACKEND_GET_CLASS (backend)
613 ->get_writable (backend, key);
617 * g_settings_backend_unsubscribe:
618 * @backend: a #GSettingsBackend
619 * @name: a key or path to subscribe to
621 * Reverses the effect of a previous call to
622 * g_settings_backend_subscribe().
625 g_settings_backend_unsubscribe (GSettingsBackend *backend,
628 G_SETTINGS_BACKEND_GET_CLASS (backend)
629 ->unsubscribe (backend, name);
633 * g_settings_backend_subscribe:
634 * @backend: a #GSettingsBackend
635 * @name: a key or path to subscribe to
637 * Requests that change signals be emitted for events on @name.
640 g_settings_backend_subscribe (GSettingsBackend *backend,
643 G_SETTINGS_BACKEND_GET_CLASS (backend)
644 ->subscribe (backend, name);
648 g_settings_backend_set_property (GObject *object,
653 GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
658 backend->priv->context = g_value_dup_string (value);
662 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
668 g_settings_backend_get_property (GObject *object,
673 GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
678 g_value_set_string (value, backend->priv->context);
682 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
688 g_settings_backend_finalize (GObject *object)
690 GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
692 g_free (backend->priv->context);
694 G_OBJECT_CLASS (g_settings_backend_parent_class)->finalize (object);
698 ignore_subscription (GSettingsBackend *backend,
704 g_settings_backend_init (GSettingsBackend *backend)
706 backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend,
707 G_TYPE_SETTINGS_BACKEND,
708 GSettingsBackendPrivate);
712 g_settings_backend_class_init (GSettingsBackendClass *class)
714 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
716 class->subscribe = ignore_subscription;
717 class->unsubscribe = ignore_subscription;
719 gobject_class->get_property = g_settings_backend_get_property;
720 gobject_class->set_property = g_settings_backend_set_property;
721 gobject_class->finalize = g_settings_backend_finalize;
723 g_type_class_add_private (class, sizeof (GSettingsBackendPrivate));
726 * GSettingsBackend:context:
728 * The "context" property gives a hint to the backend as to
729 * what storage to use. It is up to the implementation to make
730 * use of this information.
732 * E.g. DConf supports "user", "system", "defaults" and "login"
735 * If your backend supports different contexts, you should also
736 * provide an implementation of the supports_context() class
737 * function in #GSettingsBackendClass.
739 g_object_class_install_property (gobject_class, PROP_CONTEXT,
740 g_param_spec_string ("context", P_("Context"),
741 P_("An identifier to decide which storage to use"),
742 "", G_PARAM_READWRITE |
743 G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
748 * g_settings_backend_create_tree:
749 * @returns: a new #GTree
751 * This is a convenience function for creating a tree that is compatible
752 * with g_settings_backend_write(). It merely calls g_tree_new_full()
753 * with strcmp(), g_free() and g_variant_unref().
756 g_settings_backend_create_tree (void)
758 return g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
759 g_free, (GDestroyNotify) g_variant_unref);
764 get_default_backend (const gchar *context)
766 GIOExtension *extension = NULL;
767 GIOExtensionPoint *point;
772 _g_io_modules_ensure_loaded ();
774 point = g_io_extension_point_lookup (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME);
776 if ((env = getenv ("GSETTINGS_BACKEND")))
778 extension = g_io_extension_point_get_extension_by_name (point, env);
780 if (extension == NULL)
781 g_warning ("Can't find GSettings backend '%s' given in "
782 "GSETTINGS_BACKEND environment variable", env);
785 if (extension == NULL)
787 extensions = g_io_extension_point_get_extensions (point);
789 if (extensions == NULL)
790 g_error ("No GSettingsBackend implementations exist.");
792 extension = extensions->data;
795 if (context[0] != '\0') /* (context != "") */
797 GSettingsBackendClass *backend_class;
800 class = g_io_extension_ref_class (extension);
801 backend_class = G_SETTINGS_BACKEND_CLASS (class);
803 if (backend_class->supports_context == NULL ||
804 !backend_class->supports_context (context))
806 g_type_class_unref (class);
810 g_type_class_unref (class);
813 type = g_io_extension_get_type (extension);
815 return g_object_new (type, "context", context, NULL);
818 static GHashTable *g_settings_backends;
821 * g_settings_backend_get_with_context:
822 * @context: a context that might be used by the backend to determine
823 * which storage to use, or %NULL to use the default storage
824 * @returns: the default #GSettingsBackend
826 * Returns the default #GSettingsBackend. It is possible to override
827 * the default by setting the <envar>GSETTINGS_BACKEND</envar>
828 * environment variable to the name of a settings backend.
830 * The @context parameter can be used to indicate that a different
831 * than the default storage is desired. E.g. the DConf backend lets
832 * you use "user", "system", "defaults" and "login" as contexts.
834 * If @context is not supported by the implementation, this function
835 * returns an instance of the #GSettingsMemoryBackend.
836 * See g_settings_backend_supports_context(),
838 * The user does not own the return value and it must not be freed.
841 g_settings_backend_get_with_context (const gchar *context)
843 GSettingsBackend *backend;
845 g_return_val_if_fail (context != NULL, NULL);
847 _g_io_modules_ensure_extension_points_registered ();
849 if (g_settings_backends == NULL)
850 g_settings_backends = g_hash_table_new (g_str_hash, g_str_equal);
852 backend = g_hash_table_lookup (g_settings_backends, context);
856 backend = get_default_backend (context);
859 backend = g_null_settings_backend_new ();
861 g_hash_table_insert (g_settings_backends, g_strdup (context), backend);
864 return g_object_ref (backend);
868 * g_settings_backend_supports_context:
869 * @context: a context string that might be passed to
870 * g_settings_backend_new_with_context()
871 * @returns: #TRUE if @context is supported
873 * Determines if the given context is supported by the default
874 * GSettingsBackend implementation.
877 g_settings_backend_supports_context (const gchar *context)
879 GSettingsBackend *backend;
881 g_return_val_if_fail (context != NULL, FALSE);
883 backend = get_default_backend (context);
887 g_object_unref (backend);
895 * g_settings_backend_setup:
896 * @context: a context string (not %NULL or "")
897 * @backend: a #GSettingsBackend
899 * Sets up @backend for use with #GSettings.
901 * If you create a #GSettings with its context property set to @context
902 * then it will use the backend given to this function. See
903 * g_settings_new_with_context().
905 * The backend must be set up before any settings objects are created
906 * for the named context.
908 * It is not possible to specify a backend for the default context.
910 * This function takes a reference on @backend and never releases it.
915 g_settings_backend_setup (const gchar *context,
916 GSettingsBackend *backend)
918 g_return_if_fail (context[0] != '\0');
919 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
921 if (g_settings_backends == NULL)
922 g_settings_backends = g_hash_table_new (g_str_hash, g_str_equal);
924 if (g_hash_table_lookup (g_settings_backends, context))
925 g_error ("A GSettingsBackend already exists for context '%s'", context);
927 g_hash_table_insert (g_settings_backends,
929 g_object_ref (backend));
932 #define __G_SETTINGS_BACKEND_C__
933 #include "gioaliasdef.c"