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 "giomodule-priv.h"
28 #include "gio-marshal.h"
37 G_DEFINE_ABSTRACT_TYPE (GSettingsBackend, g_settings_backend, G_TYPE_OBJECT)
39 typedef struct _GSettingsBackendWatch GSettingsBackendWatch;
41 struct _GSettingsBackendPrivate
43 GSettingsBackendWatch *watches;
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
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.
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.
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.
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
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>
88 struct _GSettingsBackendWatch
90 GSettingsBackendChangedFunc changed;
91 GSettingsBackendPathChangedFunc path_changed;
92 GSettingsBackendKeysChangedFunc keys_changed;
93 GSettingsBackendWritableChangedFunc writable_changed;
94 GSettingsBackendPathWritableChangedFunc path_writable_changed;
97 GSettingsBackendWatch *next;
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,
109 GSettingsBackendWatch *watch;
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;
119 watch->next = backend->priv->watches;
120 backend->priv->watches = watch;
124 g_settings_backend_unwatch (GSettingsBackend *backend,
127 GSettingsBackendWatch **ptr;
129 for (ptr = &backend->priv->watches; *ptr; ptr = &(*ptr)->next)
130 if ((*ptr)->user_data == user_data)
132 GSettingsBackendWatch *tmp = *ptr;
135 g_slice_free (GSettingsBackendWatch, tmp);
140 g_assert_not_reached ();
144 is_key (const gchar *key)
149 g_return_val_if_fail (key != NULL, FALSE);
150 g_return_val_if_fail (key[0] == '/', FALSE);
152 for (i = 1; key[i]; i++)
153 g_return_val_if_fail (key[i] != '/' || key[i + 1] != '/', FALSE);
157 g_return_val_if_fail (key[length - 1] != '/', FALSE);
163 is_path (const gchar *path)
168 g_return_val_if_fail (path != NULL, FALSE);
169 g_return_val_if_fail (path[0] == '/', FALSE);
171 for (i = 1; path[i]; i++)
172 g_return_val_if_fail (path[i] != '/' || path[i + 1] != '/', FALSE);
176 g_return_val_if_fail (path[length - 1] == '/', FALSE);
182 * g_settings_backend_changed:
183 * @backend: a #GSettingsBackend implementation
184 * @key: the name of the key
185 * @origin_tag: the origin tag
187 * Signals that a single key has possibly changed. Backend
188 * implementations should call this if a key has possibly changed its
191 * @key must be a valid key (ie: starting with a slash, not containing
192 * '//', and not ending with a slash).
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.
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()).
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.
213 g_settings_backend_changed (GSettingsBackend *backend,
217 GSettingsBackendWatch *watch;
219 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
220 g_return_if_fail (is_key (key));
222 for (watch = backend->priv->watches; watch; watch = watch->next)
223 watch->changed (backend, key, origin_tag, watch->user_data);
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
233 * Signals that a list of keys have possibly changed. Backend
234 * implementations should call this if keys have possibly changed their
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 '//').
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
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).
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.
258 g_settings_backend_keys_changed (GSettingsBackend *backend,
260 gchar const * const *items,
263 GSettingsBackendWatch *watch;
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);
269 for (watch = backend->priv->watches; watch; watch = watch->next)
270 watch->keys_changed (backend, path, items, origin_tag, watch->user_data);
274 * g_settings_backend_keys_changed:
275 * @backend: a #GSettingsBackend implementation
276 * @path: the path containing the changes
277 * @origin_tag: the origin tag
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.
283 * @path must be a valid path (ie: starting and ending with a slash and
284 * not containing '//').
286 * The meaning of this signal is that any of the key which has a name
287 * starting with @path may have changed.
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
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.
304 g_settings_backend_path_changed (GSettingsBackend *backend,
308 GSettingsBackendWatch *watch;
310 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
311 g_return_if_fail (is_path (path));
313 for (watch = backend->priv->watches; watch; watch = watch->next)
314 watch->path_changed (backend, path, origin_tag, watch->user_data);
318 * g_settings_backend_writable_changed:
319 * @backend: a #GSettingsBackend implementation
320 * @key: the name of the key
322 * Signals that the writability of a single key has possibly changed.
324 * Since GSettings performs no locking operations for itself, this call
325 * will always be made in response to external events.
330 g_settings_backend_writable_changed (GSettingsBackend *backend,
333 GSettingsBackendWatch *watch;
335 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
336 g_return_if_fail (is_key (key));
338 for (watch = backend->priv->watches; watch; watch = watch->next)
339 watch->writable_changed (backend, key, watch->user_data);
343 * g_settings_backend_writable_changed:
344 * @backend: a #GSettingsBackend implementation
345 * @path: the name of the path
347 * Signals that the writability of all keys below a given path may have
350 * Since GSettings performs no locking operations for itself, this call
351 * will always be made in response to external events.
356 g_settings_backend_path_writable_changed (GSettingsBackend *backend,
359 GSettingsBackendWatch *watch;
361 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
362 g_return_if_fail (is_path (path));
364 for (watch = backend->priv->watches; watch; watch = watch->next)
365 watch->path_writable_changed (backend, path, watch->user_data);
376 tree_get_keys (gpointer key,
380 GetKeysState *state = user_data;
381 const gchar *skey = key;
384 g_return_val_if_fail (is_key (key), TRUE);
386 /* calculate longest common prefix */
387 if (state->prefix == NULL)
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;
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.
403 for (i = 0; state->prefix[i] == skey[i]; i++);
405 /* check if we need to shorten the prefix */
406 if (state->prefix[i] != '\0')
408 /* find the nearest '/', terminate after it */
409 while (state->prefix[i - 1] != '/')
412 state->prefix[i] = '\0';
413 state->prefix_len = i;
418 /* save the entire item into the array.
419 * the prefixes will be removed later.
421 *state->items++ = key;
427 * g_settings_backend_changed_tree:
428 * @backend: a #GSettingsBackend implementation
429 * @tree: a #GTree containing the changes
430 * @origin_tag: the origin tag
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().
439 g_settings_backend_changed_tree (GSettingsBackend *backend,
443 GSettingsBackendWatch *watch;
444 GetKeysState state = { 0, };
447 list = g_new (gchar *, g_tree_nnodes (tree) + 1);
450 g_tree_foreach (tree, tree_get_keys, &state);
451 g_return_if_fail (list + g_tree_nnodes (tree) == state.items);
454 while (state.items-- != list)
455 *state.items += state.prefix_len;
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);
463 g_free (state.prefix);
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
473 * Reads a key. This call will never block.
475 * If the key exists, the value associated with it will be returned.
476 * If the key does not exist, %NULL will be returned.
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.
485 g_settings_backend_read (GSettingsBackend *backend,
487 const GVariantType *expected_type)
489 return G_SETTINGS_BACKEND_GET_CLASS (backend)
490 ->read (backend, key, expected_type);
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
500 * Writes exactly one key.
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
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
513 g_settings_backend_write (GSettingsBackend *backend,
518 G_SETTINGS_BACKEND_GET_CLASS (backend)
519 ->write (backend, key, value, origin_tag);
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
528 * Writes one or more keys. This call will never block.
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.
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.
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
546 g_settings_backend_write_keys (GSettingsBackend *backend,
550 G_SETTINGS_BACKEND_GET_CLASS (backend)
551 ->write_keys (backend, tree, origin_tag);
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
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
565 * For paths, it means that every key under the path is reset.
568 g_settings_backend_reset (GSettingsBackend *backend,
572 G_SETTINGS_BACKEND_GET_CLASS (backend)
573 ->reset (backend, name, origin_tag);
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
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.
586 * You should not write to locked-down keys, but if you do, the
587 * implementation will deal with it.
590 g_settings_backend_get_writable (GSettingsBackend *backend,
593 return G_SETTINGS_BACKEND_GET_CLASS (backend)
594 ->get_writable (backend, name);
598 * g_settings_backend_unsubscribe:
599 * @backend: a #GSettingsBackend
600 * @name: a key or path to subscribe to
602 * Reverses the effect of a previous call to
603 * g_settings_backend_subscribe().
606 g_settings_backend_unsubscribe (GSettingsBackend *backend,
609 G_SETTINGS_BACKEND_GET_CLASS (backend)
610 ->unsubscribe (backend, name);
614 * g_settings_backend_subscribe:
615 * @backend: a #GSettingsBackend
616 * @name: a key or path to subscribe to
618 * Requests that change signals be emitted for events on @name.
621 g_settings_backend_subscribe (GSettingsBackend *backend,
624 G_SETTINGS_BACKEND_GET_CLASS (backend)
625 ->subscribe (backend, name);
629 g_settings_backend_set_property (GObject *object,
634 GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
639 backend->priv->context = g_value_dup_string (value);
643 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
649 g_settings_backend_get_property (GObject *object,
654 GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
659 g_value_set_string (value, backend->priv->context);
663 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
669 g_settings_backend_finalize (GObject *object)
671 GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
673 g_free (backend->priv->context);
675 G_OBJECT_CLASS (g_settings_backend_parent_class)->finalize (object);
679 ignore_subscription (GSettingsBackend *backend,
685 g_settings_backend_init (GSettingsBackend *backend)
687 backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend,
688 G_TYPE_SETTINGS_BACKEND,
689 GSettingsBackendPrivate);
693 g_settings_backend_class_init (GSettingsBackendClass *class)
695 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
697 class->subscribe = ignore_subscription;
698 class->unsubscribe = ignore_subscription;
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;
704 g_type_class_add_private (class, sizeof (GSettingsBackendPrivate));
707 * GSettingsBackend:context:
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.
713 * E.g. DConf supports "user", "system", "defaults" and "login"
716 * If your backend supports different contexts, you should also
717 * provide an implementation of the supports_context() class
718 * function in #GSettingsBackendClass.
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));
731 * g_settings_backend_create_tree:
732 * @returns: a new #GTree
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().
739 g_settings_backend_create_tree (void)
741 return g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
742 g_free, (GDestroyNotify) g_variant_unref);
747 get_default_backend (const gchar *context)
749 GIOExtension *extension = NULL;
750 GIOExtensionPoint *point;
755 _g_io_modules_ensure_loaded ();
757 point = g_io_extension_point_lookup (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME);
759 if ((env = getenv ("GSETTINGS_BACKEND")))
761 extension = g_io_extension_point_get_extension_by_name (point, env);
763 if (extension == NULL)
764 g_warning ("Can't find GSettings backend '%s' given in "
765 "GSETTINGS_BACKEND environment variable", env);
768 if (extension == NULL)
770 extensions = g_io_extension_point_get_extensions (point);
772 if (extensions == NULL)
773 g_error ("No GSettingsBackend implementations exist.");
775 extension = extensions->data;
780 GSettingsBackendClass *backend_class;
783 class = g_io_extension_ref_class (extension);
784 backend_class = G_SETTINGS_BACKEND_CLASS (class);
786 if (backend_class->supports_context != NULL &&
787 !backend_class->supports_context (context))
789 g_type_class_unref (class);
793 g_type_class_unref (class);
796 type = g_io_extension_get_type (extension);
798 return g_object_new (type, "context", context, NULL);
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
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.
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.
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(),
819 * The user does not own the return value and it must not be freed.
822 g_settings_backend_get_with_context (const gchar *context)
824 static GHashTable *backends;
825 GSettingsBackend *backend;
827 g_return_val_if_fail (context != NULL, NULL);
829 _g_io_modules_ensure_extension_points_registered ();
832 backends = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
834 backend = g_hash_table_lookup (backends, context);
838 backend = get_default_backend (context);
842 /* FIXME: create an instance of the const backend */
845 g_hash_table_insert (backends, g_strdup (context), backend);
848 return g_object_ref (backend);
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
857 * Determines if the given context is supported by the default
858 * GSettingsBackend implementation.
861 g_settings_backend_supports_context (const gchar *context)
863 GSettingsBackend *backend;
865 backend = get_default_backend (context);
869 g_object_unref (backend);
876 #define __G_SETTINGS_BACKEND_C__
877 #include "gioaliasdef.c"