2 * Copyright © 2009, 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
27 #include "gsettings.h"
29 #include "gdelayedsettingsbackend.h"
30 #include "gsettingsbackendinternal.h"
31 #include "gsettings-mapping.h"
32 #include "gio-marshal.h"
33 #include "gsettingsschema.h"
41 * @short_description: a high-level API for application settings
43 * The #GSettings class provides a convenient API for storing and retrieving
44 * application settings.
46 * When creating a GSettings instance, you have to specify a schema
47 * that describes the keys in your settings and their types and default
48 * values, as well as some other information.
50 * Normally, a schema has as fixed path that determines where the settings
51 * are stored in the conceptual global tree of settings. However, schemas
52 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
53 * useful e.g. when the schema describes an 'account', and you want to be
54 * able to store a arbitrary number of accounts.
56 * Unlike other configuration systems (like GConf), GSettings does not
57 * restrict keys to basic types like strings and numbers. GSettings stores
58 * values as #GVariant, and allows any #GVariantType for keys. Key names
59 * are restricted to lowercase characters, numbers and '-'. Furthermore,
60 * the names must begin with a lowercase character, must not end
61 * with a '-', and must not contain consecutive dashes. Key names can
62 * be up to 32 characters long.
64 * Similar to GConf, the default values in GSettings schemas can be
65 * localized, but the localized values are stored in gettext catalogs
66 * and looked up with the domain that is specified in the
67 * <tag class="attribute">gettext-domain</tag> attribute of the
68 * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
69 * elements and the category that is specified in the l10n attribute of the
70 * <tag class="starttag">key</tag> element.
72 * GSettings uses schemas in a compact binary form that is created
73 * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
74 * utility. The input is a schema description in an XML format that can be
75 * described by the following DTD:
77 * <!ELEMENT schemalist (schema*) >
78 * <!ATTLIST schemalist gettext-domain #IMPLIED >
80 * <!ELEMENT schema (key|child)* >
81 * <!ATTLIST schema id CDATA #REQUIRED
83 * gettext-domain CDATA #IMPLIED >
85 * <!ELEMENT key (default|summary?|description?|range?|choices?) >
86 * <!-- name can only contain lowercase letters, numbers and '-' -->
87 * <!-- type must be a GVariant type string -->
88 * <!ATTLIST key name CDATA #REQUIRED
89 * type CDATA #REQUIRED >
91 * <!-- the default value is specified a a serialized GVariant,
92 * i.e. you have to include the quotes when specifying a string -->
93 * <!ELEMENT default (#PCDATA) >
94 * <!-- the presence of the l10n attribute marks a default value for
95 * translation, its value is the gettext category to use -->
96 * <!-- if context is present, it specifies msgctxt to use -->
97 * <!ATTLIST default l10n (messages|time) #IMPLIED
98 * context CDATA #IMPLIED >
100 * <!ELEMENT summary (#PCDATA) >
101 * <!ELEMENT description (#PCDATA) >
103 * <!ELEMENT range (min,max) >
104 * <!ELEMENT min (#PCDATA) >
105 * <!ELEMENT max (#PCDATA) >
107 * <!ELEMENT choices (choice+) >
108 * <!ELEMENT choice (alias?) >
109 * <!ATTLIST choice value CDATA #REQUIRED >
110 * <!ELEMENT choice (alias?) >
111 * <!ELEMENT alias EMPTY >
112 * <!ATTLIST alias value CDATA #REQUIRED >
114 * <!ELEMENT child EMPTY >
115 * <!ATTLIST child name CDATA #REQUIRED
116 * schema CDATA #REQUIRED >
120 * At runtime, schemas are identified by their id (as specified
121 * in the <tag class="attribute">id</tag> attribute of the
122 * <tag class="starttag">schema</tag> element). The
123 * convention for schema ids is to use a dotted name, similar in
124 * style to a DBus bus name, e.g. "org.gnome.font-rendering".
127 * <title>Binding</title>
129 * A very convenient feature of GSettings lets you bind #GObject properties
130 * directly to settings, using g_settings_bind(). Once a GObject property
131 * has been bound to a setting, changes on either side are automatically
132 * propagated to the other side. GSettings handles details like
133 * mapping between GObject and GVariant types, and preventing infinite
137 * This makes it very easy to hook up a preferences dialog to the
138 * underlying settings. To make this even more convenient, GSettings
139 * looks for a boolean property with the name "sensitivity" and
140 * automatically binds it to the writability of the bound setting.
141 * If this 'magic' gets in the way, it can be suppressed with the
142 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
147 struct _GSettingsPrivate
149 GSettingsBackend *backend;
150 GSettingsSchema *schema;
155 GDelayedSettingsBackend *delayed;
170 SIGNAL_WRITABLE_CHANGE_EVENT,
171 SIGNAL_WRITABLE_CHANGED,
177 static guint g_settings_signals[N_SIGNALS];
179 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
182 g_settings_real_change_event (GSettings *settings,
189 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
191 for (i = 0; i < n_keys; i++)
192 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
193 keys[i], g_quark_to_string (keys[i]));
199 g_settings_real_writable_change_event (GSettings *settings,
202 const GQuark *keys = &key;
207 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
209 for (i = 0; i < n_keys; i++)
211 const gchar *string = g_quark_to_string (keys[i]);
213 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
215 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
223 settings_backend_changed (GSettingsBackend *backend,
228 GSettings *settings = G_SETTINGS (user_data);
229 gboolean ignore_this;
232 g_assert (settings->priv->backend == backend);
234 for (i = 0; key[i] == settings->priv->path[i]; i++);
236 if (settings->priv->path[i] == '\0' &&
237 g_settings_schema_has_key (settings->priv->schema, key + i))
241 quark = g_quark_from_string (key + i);
242 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
243 0, &quark, 1, &ignore_this);
248 settings_backend_path_changed (GSettingsBackend *backend,
253 GSettings *settings = G_SETTINGS (user_data);
254 gboolean ignore_this;
256 g_assert (settings->priv->backend == backend);
258 if (g_str_has_prefix (settings->priv->path, path))
259 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
260 0, NULL, 0, &ignore_this);
264 settings_backend_keys_changed (GSettingsBackend *backend,
266 const gchar * const *items,
270 GSettings *settings = G_SETTINGS (user_data);
271 gboolean ignore_this;
274 g_assert (settings->priv->backend == backend);
276 for (i = 0; settings->priv->path[i] &&
277 settings->priv->path[i] == path[i]; i++);
284 for (j = 0; items[j]; j++)
286 const gchar *item = items[j];
289 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
291 if (settings->priv->path[i + k] == '\0' &&
292 g_settings_schema_has_key (settings->priv->schema, item + k))
293 quarks[l++] = g_quark_from_string (item + k);
295 /* "256 quarks ought to be enough for anybody!"
296 * If this bites you, I'm sorry. Please file a bug.
302 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
303 0, quarks, l, &ignore_this);
308 settings_backend_writable_changed (GSettingsBackend *backend,
312 GSettings *settings = G_SETTINGS (user_data);
313 gboolean ignore_this;
316 g_assert (settings->priv->backend == backend);
318 for (i = 0; key[i] == settings->priv->path[i]; i++);
320 if (settings->priv->path[i] == '\0' &&
321 g_settings_schema_has_key (settings->priv->schema, key + i))
322 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
323 0, g_quark_from_string (key + i), &ignore_this);
327 settings_backend_path_writable_changed (GSettingsBackend *backend,
331 GSettings *settings = G_SETTINGS (user_data);
332 gboolean ignore_this;
334 g_assert (settings->priv->backend == backend);
336 if (g_str_has_prefix (settings->priv->path, path))
337 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
338 0, (GQuark) 0, &ignore_this);
342 g_settings_constructed (GObject *object)
344 GSettings *settings = G_SETTINGS (object);
345 const gchar *schema_path;
347 settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
348 schema_path = g_settings_schema_get_path (settings->priv->schema);
350 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
351 g_error ("settings object created with schema '%s' and path '%s', but "
352 "path '%s' is specified by schema",
353 settings->priv->schema_name, settings->priv->path, schema_path);
355 if (settings->priv->path == NULL)
357 if (schema_path == NULL)
358 g_error ("attempting to create schema '%s' without a path",
359 settings->priv->schema_name);
361 settings->priv->path = g_strdup (schema_path);
364 settings->priv->backend = g_settings_backend_get_with_context (settings->priv->context);
365 g_settings_backend_watch (settings->priv->backend,
366 settings_backend_changed,
367 settings_backend_path_changed,
368 settings_backend_keys_changed,
369 settings_backend_writable_changed,
370 settings_backend_path_writable_changed,
372 g_settings_backend_subscribe (settings->priv->backend,
373 settings->priv->path);
377 g_settings_init (GSettings *settings)
379 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
386 * @settings: a #GSettings object
388 * Changes the #GSettings object into 'delay-apply' mode. In this
389 * mode, changes to @settings are not immediately propagated to the
390 * backend, but kept locally until g_settings_apply() is called.
395 g_settings_delay (GSettings *settings)
397 g_return_if_fail (G_IS_SETTINGS (settings));
399 if (settings->priv->delayed)
402 settings->priv->delayed =
403 g_delayed_settings_backend_new (settings->priv->backend, settings);
404 g_settings_backend_unwatch (settings->priv->backend, settings);
405 g_object_unref (settings->priv->backend);
407 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
408 g_settings_backend_watch (settings->priv->backend,
409 settings_backend_changed,
410 settings_backend_path_changed,
411 settings_backend_keys_changed,
412 settings_backend_writable_changed,
413 settings_backend_path_writable_changed,
419 * @settings: a #GSettings instance
421 * Applies any changes that have been made to the settings. This
422 * function does nothing unless @settings is in 'delay-apply' mode;
423 * see g_settings_set_delay_apply(). In the normal case settings are
424 * always applied immediately.
427 g_settings_apply (GSettings *settings)
429 if (settings->priv->delayed)
431 GDelayedSettingsBackend *delayed;
433 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
434 g_delayed_settings_backend_apply (delayed);
440 * @settings: a #GSettings instance
442 * Reverts all non-applied changes to the settings. This function
443 * does nothing unless @settings is in 'delay-apply' mode; see
444 * g_settings_set_delay_apply(). In the normal case settings are
445 * always applied immediately.
447 * Change notifications will be emitted for affected keys.
450 g_settings_revert (GSettings *settings)
452 if (settings->priv->delayed)
454 GDelayedSettingsBackend *delayed;
456 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
457 g_delayed_settings_backend_revert (delayed);
462 g_settings_set_property (GObject *object,
467 GSettings *settings = G_SETTINGS (object);
472 g_assert (settings->priv->schema_name == NULL);
473 settings->priv->schema_name = g_value_dup_string (value);
477 settings->priv->path = g_value_dup_string (value);
481 settings->priv->context = g_value_dup_string (value);
485 g_assert_not_reached ();
490 * g_settings_get_has_unapplied:
491 * @settings: a #GSettings object
492 * @returns: %TRUE if @settings has unapplied changes
494 * Returns whether the #GSettings object has any unapplied
495 * changes. This can only be the case if it is in 'delayed-apply' mode.
500 g_settings_get_has_unapplied (GSettings *settings)
502 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
504 return settings->priv->delayed &&
505 g_delayed_settings_backend_get_has_unapplied (
506 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
510 g_settings_get_property (GObject *object,
515 GSettings *settings = G_SETTINGS (object);
520 g_value_set_object (value, settings->priv->schema);
523 case PROP_HAS_UNAPPLIED:
524 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
528 g_assert_not_reached ();
533 g_settings_finalize (GObject *object)
535 GSettings *settings = G_SETTINGS (object);
537 g_settings_backend_unwatch (settings->priv->backend, settings);
538 g_settings_backend_unsubscribe (settings->priv->backend,
539 settings->priv->path);
540 g_object_unref (settings->priv->backend);
541 g_object_unref (settings->priv->schema);
542 g_free (settings->priv->schema_name);
543 g_free (settings->priv->path);
547 g_settings_class_init (GSettingsClass *class)
549 GObjectClass *object_class = G_OBJECT_CLASS (class);
551 class->writable_change_event = g_settings_real_writable_change_event;
552 class->change_event = g_settings_real_change_event;
554 object_class->set_property = g_settings_set_property;
555 object_class->get_property = g_settings_get_property;
556 object_class->constructed = g_settings_constructed;
557 object_class->finalize = g_settings_finalize;
559 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
562 * GSettings::changed:
563 * @settings: the object on which the signal was emitted
564 * @key: the name of the key that changed
566 * The "changed" signal is emitted when a key has potentially changed.
567 * You should call one of the g_settings_get() calls to check the new
570 * This signal supports detailed connections. You can connect to the
571 * detailed signal "changed::x" in order to only receive callbacks
572 * when key "x" changes.
574 g_settings_signals[SIGNAL_CHANGED] =
575 g_signal_new ("changed", G_TYPE_SETTINGS,
576 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
577 G_STRUCT_OFFSET (GSettingsClass, changed),
578 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
579 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
582 * GSettings::change-event:
583 * @settings: the object on which the signal was emitted
584 * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
585 * @n_keys: the length of the @keys array, or 0
586 * @returns: %TRUE to stop other handlers from being invoked for the
587 * event. FALSE to propagate the event further.
589 * The "change-event" signal is emitted once per change event that
590 * affects this settings object. You should connect to this signal
591 * only if you are interested in viewing groups of changes before they
592 * are split out into multiple emissions of the "changed" signal.
593 * For most use cases it is more appropriate to use the "changed" signal.
595 * In the event that the change event applies to one or more specified
596 * keys, @keys will be an array of #GQuark of length @n_keys. In the
597 * event that the change event applies to the #GSettings object as a
598 * whole (ie: potentially every key has been changed) then @keys will
599 * be %NULL and @n_keys will be 0.
601 * The default handler for this signal invokes the "changed" signal
602 * for each affected key. If any other connected handler returns
603 * %TRUE then this default functionality will be supressed.
605 g_settings_signals[SIGNAL_CHANGE_EVENT] =
606 g_signal_new ("change-event", G_TYPE_SETTINGS,
608 G_STRUCT_OFFSET (GSettingsClass, change_event),
609 g_signal_accumulator_true_handled, NULL,
610 _gio_marshal_BOOL__POINTER_INT,
611 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
614 * GSettings::writable-changed:
615 * @settings: the object on which the signal was emitted
618 * The "writable-changed" signal is emitted when the writability of a
619 * key has potentially changed. You should call
620 * g_settings_is_writable() in order to determine the new status.
622 * This signal supports detailed connections. You can connect to the
623 * detailed signal "writable-changed::x" in order to only receive
624 * callbacks when the writability of "x" changes.
626 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
627 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
628 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
629 G_STRUCT_OFFSET (GSettingsClass, changed),
630 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
631 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
634 * GSettings::writable-change-event:
635 * @settings: the object on which the signal was emitted
636 * @key: the quark of the key, or 0
637 * @returns: %TRUE to stop other handlers from being invoked for the
638 * event. FALSE to propagate the event further.
640 * The "writable-change-event" signal is emitted once per writability
641 * change event that affects this settings object. You should connect
642 * to this signal if you are interested in viewing groups of changes
643 * before they are split out into multiple emissions of the
644 * "writable-changed" signal. For most use cases it is more
645 * appropriate to use the "writable-changed" signal.
647 * In the event that the writability change applies only to a single
648 * key, @key will be set to the #GQuark for that key. In the event
649 * that the writability change affects the entire settings object,
652 * The default handler for this signal invokes the "writable-changed"
653 * and "changed" signals for each affected key. This is done because
654 * changes in writability might also imply changes in value (if for
655 * example, a new mandatory setting is introduced). If any other
656 * connected handler returns %TRUE then this default functionality
659 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
660 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
662 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
663 g_signal_accumulator_true_handled, NULL,
664 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
669 * The name of the context that the settings are stored in.
671 g_object_class_install_property (object_class, PROP_CONTEXT,
672 g_param_spec_string ("context",
674 P_("The name of the context for this settings object"),
675 "", G_PARAM_CONSTRUCT_ONLY |
676 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
681 * The name of the schema that describes the types of keys
682 * for this #GSettings object.
684 g_object_class_install_property (object_class, PROP_SCHEMA,
685 g_param_spec_string ("schema",
687 P_("The name of the schema for this settings object"),
689 G_PARAM_CONSTRUCT_ONLY |
690 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
695 * The path within the backend where the settings are stored.
697 g_object_class_install_property (object_class, PROP_PATH,
698 g_param_spec_string ("path",
700 P_("The path within the backend where the settings are"),
702 G_PARAM_CONSTRUCT_ONLY |
703 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
706 * GSettings:has-unapplied:
708 * If this property is %TRUE, the #GSettings object has outstanding
709 * changes that will be applied when g_settings_apply() is called.
711 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
712 g_param_spec_boolean ("has-unapplied",
713 P_("Has unapplied changes"),
714 P_("TRUE if there are outstanding changes to apply()"),
716 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
721 * g_settings_get_value:
722 * @settings: a #GSettings object
723 * @key: the key to get the value for
724 * @returns: a new #GVariant
726 * Gets the value that is stored in @settings for @key.
728 * It is a programmer error to give a @key that isn't valid for
734 g_settings_get_value (GSettings *settings,
737 const gchar *unparsed = NULL;
738 GVariant *value, *options;
739 const GVariantType *type;
740 gchar lc_char = '\0';
744 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
746 sval = g_settings_schema_get_value (settings->priv->schema, key, &options);
748 if G_UNLIKELY (sval == NULL)
749 g_error ("schema '%s' does not contain a key named '%s'",
750 settings->priv->schema_name, key);
752 path = g_strconcat (settings->priv->path, key, NULL);
753 type = g_variant_get_type (sval);
754 value = g_settings_backend_read (settings->priv->backend, path, type);
761 GVariant *option_value;
763 g_variant_iter_init (&iter, options);
764 while (g_variant_iter_loop (&iter, "{&sv}", &option, &option_value))
766 if (strcmp (option, "l10n") == 0)
767 g_variant_get (option_value, "(y&s)", &lc_char, &unparsed);
769 g_warning ("unknown schema extension '%s'", option);
773 if (value && !g_variant_is_of_type (value, type))
775 g_variant_unref (value);
779 if (value == NULL && lc_char != '\0')
780 /* we will need to translate the schema default value */
782 const gchar *translated;
783 GError *error = NULL;
787 domain = g_settings_schema_get_gettext_domain (settings->priv->schema);
790 lc_category = LC_TIME;
792 lc_category = LC_MESSAGES;
794 translated = dcgettext (domain, unparsed, lc_category);
796 if (translated != unparsed)
797 /* it was translated, so we need to re-parse it */
799 value = g_variant_parse (g_variant_get_type (sval),
800 translated, NULL, NULL, &error);
804 g_warning ("Failed to parse translated string `%s' for "
805 "key `%s' in schema `%s': %s", unparsed, key,
806 settings->priv->schema_name, error->message);
807 g_warning ("Using untranslated default instead.");
808 g_error_free (error);
814 /* either translation failed or there was none to do.
815 * use the pre-compiled default.
817 value = g_variant_ref (sval);
819 g_variant_unref (sval);
825 * g_settings_set_value:
826 * @settings: a #GSettings object
827 * @key: the name of the key to set
828 * @value: a #GVariant of the correct type
829 * @returns: %TRUE if setting the key succeeded,
830 * %FALSE if the key was not writable
832 * Sets @key in @settings to @value.
834 * It is a programmer error to give a @key that isn't valid for
835 * @settings. It is a programmer error to give a @value of the
838 * If @value is floating then this function consumes the reference.
843 g_settings_set_value (GSettings *settings,
847 gboolean correct_type;
852 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
854 sval = g_settings_schema_get_value (settings->priv->schema, key, NULL);
855 correct_type = g_variant_is_of_type (value, g_variant_get_type (sval));
856 g_variant_unref (sval);
858 g_return_val_if_fail (correct_type, FALSE);
860 path = g_strconcat (settings->priv->path, key, NULL);
861 result = g_settings_backend_write (settings->priv->backend,
870 * @settings: a #GSettings object
871 * @key: the key to get the value for
872 * @format: a #GVariant format string
873 * @...: arguments as per @format
875 * Gets the value that is stored at @key in @settings.
877 * A convenience function that combines g_settings_get_value() with
880 * It is a programmer error to pass a @key that isn't valid for
881 * @settings or a @format_string that doesn't match the type of @key according
882 * to the schema of @settings.
887 g_settings_get (GSettings *settings,
895 value = g_settings_get_value (settings, key);
897 va_start (ap, format);
898 g_variant_get_va (value, format, NULL, &ap);
901 g_variant_unref (value);
906 * @settings: a #GSettings object
907 * @key: the name of the key to set
908 * @format: a #GVariant format string
909 * @...: arguments as per @format
910 * @returns: %TRUE if setting the key succeeded,
911 * %FALSE if the key was not writable
913 * Sets @key in @settings to @value.
915 * A convenience function that combines g_settings_set_value() with
918 * It is a programmer error to pass a @key that isn't valid for
919 * @settings or a @format that doesn't match the type of @key according
920 * to the schema of @settings.
925 g_settings_set (GSettings *settings,
933 va_start (ap, format);
934 value = g_variant_new_va (format, NULL, &ap);
937 return g_settings_set_value (settings, key, value);
941 * g_settings_is_writable:
942 * @settings: a #GSettings object
943 * @name: the name of a key
944 * @returns: %TRUE if the key @name is writable
946 * Finds out if a key can be written or not
951 g_settings_is_writable (GSettings *settings,
957 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
959 path = g_strconcat (settings->priv->path, name, NULL);
960 writable = g_settings_backend_get_writable (settings->priv->backend, path);
967 * g_settings_get_child:
968 * @settings: a #GSettings object
969 * @name: the name of the 'child' schema
970 * @returns: a 'child' settings object
972 * Creates a 'child' settings object which has a base path of
973 * <replaceable>base-path</replaceable>/@name", where
974 * <replaceable>base-path</replaceable> is the base path of @settings.
976 * The schema for the child settings object must have been declared
977 * in the schema of @settings using a <tag class="starttag">child</tag> element.
982 g_settings_get_child (GSettings *settings,
985 GVariant *child_schema;
990 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
992 child_name = g_strconcat (name, "/", NULL);
993 child_schema = g_settings_schema_get_value (settings->priv->schema,
995 if (child_schema == NULL ||
996 !g_variant_is_of_type (child_schema, G_VARIANT_TYPE_STRING))
997 g_error ("Schema '%s' has no child '%s'",
998 settings->priv->schema_name, name);
1000 child_path = g_strconcat (settings->priv->path, child_name, NULL);
1001 child = g_object_new (G_TYPE_SETTINGS,
1002 "schema", g_variant_get_string (child_schema, NULL),
1005 g_variant_unref (child_schema);
1006 g_free (child_path);
1007 g_free (child_name);
1014 * @schema: the name of the schema
1015 * @returns: a new #GSettings object
1017 * Creates a new #GSettings object with a given schema.
1022 g_settings_new (const gchar *schema)
1024 return g_object_new (G_TYPE_SETTINGS,
1030 * g_settings_new_with_path:
1031 * @schema: the name of the schema
1032 * @path: the path to use
1033 * @returns: a new #GSettings object
1035 * Creates a new #GSettings object with a given schema and path.
1037 * You only need to do this if you want to directly create a settings
1038 * object with a schema that doesn't have a specified path of its own.
1039 * That's quite rare.
1041 * It is a programmer error to call this function for a schema that
1042 * has an explicitly specified path.
1047 g_settings_new_with_path (const gchar *schema,
1050 return g_object_new (G_TYPE_SETTINGS,
1057 * g_settings_new_with_context:
1058 * @schema: the name of the schema
1059 * @context: the context to use
1060 * @returns: a new #GSettings object
1062 * Creates a new #GSettings object with a given schema and context.
1064 * Creating settings objects with a context allow accessing settings
1065 * from a database other than the usual one. For example, it may make
1066 * sense to specify "defaults" in order to get a settings object that
1067 * modifies the system default settings instead of the settings for this
1070 * It is a programmer error to call this function for an unsupported
1071 * context. Use g_settings_supports_context() to determine if a context
1072 * is supported if you are unsure.
1077 g_settings_new_with_context (const gchar *schema,
1078 const gchar *context)
1080 return g_object_new (G_TYPE_SETTINGS,
1087 * g_settings_new_with_context_and_path:
1088 * @schema: the name of the schema
1089 * @path: the path to use
1090 * @returns: a new #GSettings object
1092 * Creates a new #GSettings object with a given schema, context and
1095 * This is a mix of g_settings_new_with_context() and
1096 * g_settings_new_with_path().
1101 g_settings_new_with_context_and_path (const gchar *schema,
1102 const gchar *context,
1105 return g_object_new (G_TYPE_SETTINGS,
1114 GSettings *settings;
1117 GSettingsBindGetMapping get_mapping;
1118 GSettingsBindSetMapping set_mapping;
1120 GDestroyNotify destroy;
1122 guint writable_handler_id;
1123 guint property_handler_id;
1124 const GParamSpec *property;
1125 guint key_handler_id;
1129 /* prevent recursion */
1134 g_settings_binding_free (gpointer data)
1136 GSettingsBinding *binding = data;
1138 g_assert (!binding->running);
1140 if (binding->writable_handler_id)
1141 g_signal_handler_disconnect (binding->settings,
1142 binding->writable_handler_id);
1144 if (binding->key_handler_id)
1145 g_signal_handler_disconnect (binding->settings,
1146 binding->key_handler_id);
1148 if (g_signal_handler_is_connected (binding->object,
1149 binding->property_handler_id))
1150 g_signal_handler_disconnect (binding->object,
1151 binding->property_handler_id);
1153 g_variant_type_free (binding->type);
1154 g_object_unref (binding->settings);
1156 if (binding->destroy)
1157 binding->destroy (binding->user_data);
1159 g_slice_free (GSettingsBinding, binding);
1163 g_settings_binding_quark (const char *property)
1168 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
1169 quark = g_quark_from_string (tmp);
1176 g_settings_binding_key_changed (GSettings *settings,
1180 GSettingsBinding *binding = user_data;
1181 GValue value = { 0, };
1184 g_assert (settings == binding->settings);
1185 g_assert (key == binding->key);
1187 if (binding->running)
1190 binding->running = TRUE;
1192 g_value_init (&value, binding->property->value_type);
1193 variant = g_settings_get_value (settings, key);
1194 if (binding->get_mapping (&value, variant, binding->user_data))
1195 g_object_set_property (binding->object,
1196 binding->property->name,
1198 g_value_unset (&value);
1200 binding->running = FALSE;
1204 g_settings_binding_property_changed (GObject *object,
1205 const GParamSpec *pspec,
1208 GSettingsBinding *binding = user_data;
1209 GValue value = { 0, };
1212 g_assert (object == binding->object);
1213 g_assert (pspec == binding->property);
1215 if (binding->running)
1218 binding->running = TRUE;
1220 g_value_init (&value, pspec->value_type);
1221 g_object_get_property (object, pspec->name, &value);
1222 if ((variant = binding->set_mapping (&value, binding->type,
1223 binding->user_data)))
1225 g_settings_set_value (binding->settings,
1227 g_variant_ref_sink (variant));
1228 g_variant_unref (variant);
1230 g_value_unset (&value);
1232 binding->running = FALSE;
1237 * @settings: a #GSettings object
1238 * @key: the key to bind
1239 * @object: a #GObject
1240 * @property: the name of the property to bind
1241 * @flags: flags for the binding
1243 * Create a binding between the @key in the @settings object
1244 * and the property @property of @object.
1246 * The binding uses the default GIO mapping functions to map
1247 * between the settings and property values. These functions
1248 * handle booleans, numeric types and string types in a
1249 * straightforward way. Use g_settings_bind_with_mapping() if
1250 * you need a custom mapping, or map between types that are not
1251 * supported by the default mapping functions.
1253 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
1254 * function also establishes a binding between the writability of
1255 * @key and the "sensitive" property of @object (if @object has
1256 * a boolean property by that name). See g_settings_bind_writable()
1257 * for more details about writable bindings.
1259 * Note that the lifecycle of the binding is tied to the object,
1260 * and that you can have only one binding per object property.
1261 * If you bind the same property twice on the same object, the second
1262 * binding overrides the first one.
1267 g_settings_bind (GSettings *settings,
1270 const gchar *property,
1271 GSettingsBindFlags flags)
1273 g_settings_bind_with_mapping (settings, key, object, property,
1274 flags, NULL, NULL, NULL, NULL);
1278 * g_settings_bind_with_mapping:
1279 * @settings: a #GSettings object
1280 * @key: the key to bind
1281 * @object: a #GObject
1282 * @property: the name of the property to bind
1283 * @flags: flags for the binding
1284 * @get_mapping: a function that gets called to convert values
1285 * from @settings to @object, or %NULL to use the default GIO mapping
1286 * @set_mapping: a function that gets called to convert values
1287 * from @object to @settings, or %NULL to use the default GIO mapping
1288 * @user_data: data that gets passed to @get_mapping and @set_mapping
1289 * @destroy: #GDestroyNotify function for @user_data
1291 * Create a binding between the @key in the @settings object
1292 * and the property @property of @object.
1294 * The binding uses the provided mapping functions to map between
1295 * settings and property values.
1297 * Note that the lifecycle of the binding is tied to the object,
1298 * and that you can have only one binding per object property.
1299 * If you bind the same property twice on the same object, the second
1300 * binding overrides the first one.
1305 g_settings_bind_with_mapping (GSettings *settings,
1308 const gchar *property,
1309 GSettingsBindFlags flags,
1310 GSettingsBindGetMapping get_mapping,
1311 GSettingsBindSetMapping set_mapping,
1313 GDestroyNotify destroy)
1315 GSettingsBinding *binding;
1316 GObjectClass *objectclass;
1317 gchar *detailed_signal;
1318 GQuark binding_quark;
1320 g_return_if_fail (G_IS_SETTINGS (settings));
1322 objectclass = G_OBJECT_GET_CLASS (object);
1324 binding = g_slice_new0 (GSettingsBinding);
1325 binding->settings = g_object_ref (settings);
1326 binding->object = object;
1327 binding->key = g_intern_string (key);
1328 binding->property = g_object_class_find_property (objectclass, property);
1329 binding->user_data = user_data;
1330 binding->destroy = destroy;
1331 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
1332 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
1334 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
1335 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
1337 if (binding->property == NULL)
1339 g_critical ("g_settings_bind: no property '%s' on class '%s'",
1340 property, G_OBJECT_TYPE_NAME (object));
1344 if ((flags & G_SETTINGS_BIND_GET) && (binding->property->flags & G_PARAM_WRITABLE) == 0)
1346 g_critical ("g_settings_bind: property '%s' on class '%s' is not writable",
1347 property, G_OBJECT_TYPE_NAME (object));
1350 if ((flags & G_SETTINGS_BIND_SET) && (binding->property->flags & G_PARAM_READABLE) == 0)
1352 g_critical ("g_settings_bind: property '%s' on class '%s' is not readable",
1353 property, G_OBJECT_TYPE_NAME (object));
1360 value = g_settings_schema_get_value (settings->priv->schema, key, NULL);
1361 binding->type = g_variant_type_copy (g_variant_get_type (value));
1363 g_variant_unref (value);
1366 if (binding->type == NULL)
1368 g_critical ("g_settings_bind: no key '%s' on schema '%s'",
1369 key, settings->priv->schema_name);
1373 if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
1374 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
1375 !g_settings_mapping_is_compatible (binding->property->value_type,
1378 g_critical ("g_settings_bind: property '%s' on class '%s' has type"
1379 "'%s' which is not compatible with type '%s' of key '%s'"
1380 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
1381 g_type_name (binding->property->value_type),
1382 g_variant_type_dup_string (binding->type), key,
1383 settings->priv->schema_name);
1387 if ((flags & G_SETTINGS_BIND_SET) &&
1388 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
1390 GParamSpec *sensitive;
1392 sensitive = g_object_class_find_property (objectclass, "sensitive");
1394 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
1395 (sensitive->flags & G_PARAM_WRITABLE))
1396 g_settings_bind_writable (settings, binding->key,
1397 object, "sensitive", FALSE);
1400 if (flags & G_SETTINGS_BIND_SET)
1402 detailed_signal = g_strdup_printf ("notify::%s", property);
1403 binding->property_handler_id =
1404 g_signal_connect (object, detailed_signal,
1405 G_CALLBACK (g_settings_binding_property_changed),
1407 g_free (detailed_signal);
1409 if (~flags & G_SETTINGS_BIND_GET)
1410 g_settings_binding_property_changed (object,
1415 if (flags & G_SETTINGS_BIND_GET)
1417 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
1419 detailed_signal = g_strdup_printf ("changed::%s", key);
1420 binding->key_handler_id =
1421 g_signal_connect (settings, detailed_signal,
1422 G_CALLBACK (g_settings_binding_key_changed),
1424 g_free (detailed_signal);
1427 g_settings_binding_key_changed (settings, binding->key, binding);
1430 binding_quark = g_settings_binding_quark (property);
1431 g_object_set_qdata_full (object, binding_quark,
1432 binding, g_settings_binding_free);
1437 GSettings *settings;
1440 const gchar *property;
1443 } GSettingsWritableBinding;
1446 g_settings_writable_binding_free (gpointer data)
1448 GSettingsWritableBinding *binding = data;
1450 g_signal_handler_disconnect (binding->settings, binding->handler_id);
1451 g_object_unref (binding->settings);
1455 g_settings_binding_writable_changed (GSettings *settings,
1459 GSettingsWritableBinding *binding = user_data;
1462 g_assert (settings == binding->settings);
1463 g_assert (key == binding->key);
1465 writable = g_settings_is_writable (settings, key);
1467 if (binding->inverted)
1468 writable = !writable;
1470 g_object_set (binding->object, binding->property, writable, NULL);
1474 * g_settings_bind_writable:
1475 * @settings: a #GSettings object
1476 * @key: the key to bind
1477 * @object: a #GObject
1478 * @property: the name of a boolean property to bind
1479 * @inverted: whether to 'invert' the value
1481 * Create a binding between the writability of @key in the
1482 * @settings object and the property @property of @object.
1483 * The property must be boolean; "sensitive" or "visible"
1484 * properties of widgets are the most likely candidates.
1486 * Writable bindings are always uni-directional; changes of the
1487 * writability of the setting will be propagated to the object
1488 * property, not the other way.
1490 * When the @inverted argument is %TRUE, the binding inverts the
1491 * value as it passes from the setting to the object, i.e. @property
1492 * will be set to %TRUE if the key is <emphasis>not</emphasis>
1495 * Note that the lifecycle of the binding is tied to the object,
1496 * and that you can have only one binding per object property.
1497 * If you bind the same property twice on the same object, the second
1498 * binding overrides the first one.
1503 g_settings_bind_writable (GSettings *settings,
1506 const gchar *property,
1509 GSettingsWritableBinding *binding;
1510 gchar *detailed_signal;
1513 g_return_if_fail (G_IS_SETTINGS (settings));
1515 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
1518 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
1519 property, G_OBJECT_TYPE_NAME (object));
1522 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
1524 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
1525 property, G_OBJECT_TYPE_NAME (object));
1529 binding = g_slice_new (GSettingsWritableBinding);
1530 binding->settings = g_object_ref (settings);
1531 binding->object = object;
1532 binding->property = g_intern_string (property);
1533 binding->inverted = inverted;
1535 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
1536 binding->handler_id =
1537 g_signal_connect (settings, detailed_signal,
1538 G_CALLBACK (g_settings_binding_writable_changed),
1540 g_free (detailed_signal);
1542 g_object_set_qdata_full (object, g_settings_binding_quark (property),
1543 binding, g_settings_writable_binding_free);
1545 g_settings_binding_writable_changed (settings, binding->key, binding);
1549 * g_settings_unbind:
1550 * @object: the object
1551 * @property: the property whose binding is removed
1553 * Removes an existing binding for @property on @object.
1555 * Note that bindings are automatically removed when the
1556 * object is finalized, so it is rarely necessary to call this
1562 g_settings_unbind (gpointer object,
1563 const gchar *property)
1565 GQuark binding_quark;
1567 binding_quark = g_settings_binding_quark (property);
1568 g_object_set_qdata (object, binding_quark, NULL);
1572 * g_settings_get_string:
1573 * @settings: a #GSettings object
1574 * @key: the key to get the value for
1575 * @returns: a newly-allocated string
1577 * Gets the value that is stored at @key in @settings.
1579 * A convenience variant of g_settings_get() for strings.
1581 * It is a programmer error to pass a @key that isn't valid for
1582 * @settings or is not of type string.
1587 g_settings_get_string (GSettings *settings,
1593 value = g_settings_get_value (settings, key);
1594 result = g_variant_dup_string (value, NULL);
1595 g_variant_unref (value);
1601 * g_settings_set_string:
1602 * @settings: a #GSettings object
1603 * @key: the name of the key to set
1604 * @value: the value to set it to
1605 * @returns: %TRUE if setting the key succeeded,
1606 * %FALSE if the key was not writable
1608 * Sets @key in @settings to @value.
1610 * A convenience variant of g_settings_set() for strings.
1612 * It is a programmer error to pass a @key that isn't valid for
1613 * @settings or is not of type string.
1618 g_settings_set_string (GSettings *settings,
1622 return g_settings_set_value (settings, key, g_variant_new_string (value));
1626 * g_settings_get_int:
1627 * @settings: a #GSettings object
1628 * @key: the key to get the value for
1629 * @returns: an integer
1631 * Gets the value that is stored at @key in @settings.
1633 * A convenience variant of g_settings_get() for 32-bit integers.
1635 * It is a programmer error to pass a @key that isn't valid for
1636 * @settings or is not of type int32.
1641 g_settings_get_int (GSettings *settings,
1647 value = g_settings_get_value (settings, key);
1648 result = g_variant_get_int32 (value);
1649 g_variant_unref (value);
1655 * g_settings_set_int:
1656 * @settings: a #GSettings object
1657 * @key: the name of the key to set
1658 * @value: the value to set it to
1659 * @returns: %TRUE if setting the key succeeded,
1660 * %FALSE if the key was not writable
1662 * Sets @key in @settings to @value.
1664 * A convenience variant of g_settings_set() for 32-bit integers.
1666 * It is a programmer error to pass a @key that isn't valid for
1667 * @settings or is not of type int32.
1672 g_settings_set_int (GSettings *settings,
1676 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1680 * g_settings_get_double:
1681 * @settings: a #GSettings object
1682 * @key: the key to get the value for
1683 * @returns: a double
1685 * Gets the value that is stored at @key in @settings.
1687 * A convenience variant of g_settings_get() for doubles.
1689 * It is a programmer error to pass a @key that isn't valid for
1690 * @settings or is not of type double.
1695 g_settings_get_double (GSettings *settings,
1701 value = g_settings_get_value (settings, key);
1702 result = g_variant_get_double (value);
1703 g_variant_unref (value);
1709 * g_settings_set_double:
1710 * @settings: a #GSettings object
1711 * @key: the name of the key to set
1712 * @value: the value to set it to
1713 * @returns: %TRUE if setting the key succeeded,
1714 * %FALSE if the key was not writable
1716 * Sets @key in @settings to @value.
1718 * A convenience variant of g_settings_set() for doubles.
1720 * It is a programmer error to pass a @key that isn't valid for
1721 * @settings or is not of type double.
1726 g_settings_set_double (GSettings *settings,
1730 return g_settings_set_value (settings, key, g_variant_new_double (value));
1734 * g_settings_get_boolean:
1735 * @settings: a #GSettings object
1736 * @key: the key to get the value for
1737 * @returns: a boolean
1739 * Gets the value that is stored at @key in @settings.
1741 * A convenience variant of g_settings_get() for booleans.
1743 * It is a programmer error to pass a @key that isn't valid for
1744 * @settings or is not of type boolean.
1749 g_settings_get_boolean (GSettings *settings,
1755 value = g_settings_get_value (settings, key);
1756 result = g_variant_get_boolean (value);
1757 g_variant_unref (value);
1763 * g_settings_set_boolean:
1764 * @settings: a #GSettings object
1765 * @key: the name of the key to set
1766 * @value: the value to set it to
1767 * @returns: %TRUE if setting the key succeeded,
1768 * %FALSE if the key was not writable
1770 * Sets @key in @settings to @value.
1772 * A convenience variant of g_settings_set() for booleans.
1774 * It is a programmer error to pass a @key that isn't valid for
1775 * @settings or is not of type boolean.
1780 g_settings_set_boolean (GSettings *settings,
1784 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1788 * g_settings_get_strv:
1789 * @settings: a #GSettings object
1790 * @key: the key to get the value for
1791 * @returns: a newly-allocated, %NULL-terminated array of strings
1793 * Gets the value that is stored at @key in @settings.
1795 * A convenience variant of g_settings_get() for string arrays.
1797 * It is a programmer error to pass a @key that isn't valid for
1798 * @settings or is not of type 'string array'.
1803 g_settings_get_strv (GSettings *settings,
1810 value = g_settings_get_value (settings, key);
1811 result = g_variant_dup_strv (value, length);
1812 g_variant_unref (value);
1818 * g_settings_set_strv:
1819 * @settings: a #GSettings object
1820 * @key: the name of the key to set
1821 * @value: the value to set it to
1822 * @returns: %TRUE if setting the key succeeded,
1823 * %FALSE if the key was not writable
1825 * Sets @key in @settings to @value.
1827 * A convenience variant of g_settings_set() for string arrays.
1829 * It is a programmer error to pass a @key that isn't valid for
1830 * @settings or is not of type 'string array'.
1835 g_settings_set_strv (GSettings *settings,
1837 const gchar * const *value,
1840 return g_settings_set_value (settings, key, g_variant_new_strv (value, length));
1843 #define __G_SETTINGS_C__
1844 #include "gioaliasdef.c"