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 EMPTY >
104 * <!ATTLIST range min CDATA #REQUIRED
105 * max CDATA #REQUIRED >
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 /* where the signals go... */
150 GMainContext *main_context;
152 GSettingsBackend *backend;
153 GSettingsSchema *schema;
158 GDelayedSettingsBackend *delayed;
173 SIGNAL_WRITABLE_CHANGE_EVENT,
174 SIGNAL_WRITABLE_CHANGED,
180 static guint g_settings_signals[N_SIGNALS];
182 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
185 g_settings_real_change_event (GSettings *settings,
192 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
194 for (i = 0; i < n_keys; i++)
195 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
196 keys[i], g_quark_to_string (keys[i]));
202 g_settings_real_writable_change_event (GSettings *settings,
205 const GQuark *keys = &key;
210 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
212 for (i = 0; i < n_keys; i++)
214 const gchar *string = g_quark_to_string (keys[i]);
216 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
218 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
226 settings_backend_changed (GSettingsBackend *backend,
231 GSettings *settings = G_SETTINGS (target);
232 gboolean ignore_this;
235 g_assert (settings->priv->backend == backend);
237 for (i = 0; key[i] == settings->priv->path[i]; i++);
239 if (settings->priv->path[i] == '\0' &&
240 g_settings_schema_has_key (settings->priv->schema, key + i))
244 quark = g_quark_from_string (key + i);
245 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
246 0, &quark, 1, &ignore_this);
251 settings_backend_path_changed (GSettingsBackend *backend,
256 GSettings *settings = G_SETTINGS (target);
257 gboolean ignore_this;
259 g_assert (settings->priv->backend == backend);
261 if (g_str_has_prefix (settings->priv->path, path))
262 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
263 0, NULL, 0, &ignore_this);
267 settings_backend_keys_changed (GSettingsBackend *backend,
270 const gchar * const *items,
273 GSettings *settings = G_SETTINGS (target);
274 gboolean ignore_this;
277 g_assert (settings->priv->backend == backend);
279 for (i = 0; settings->priv->path[i] &&
280 settings->priv->path[i] == path[i]; i++);
287 for (j = 0; items[j]; j++)
289 const gchar *item = items[j];
292 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
294 if (settings->priv->path[i + k] == '\0' &&
295 g_settings_schema_has_key (settings->priv->schema, item + k))
296 quarks[l++] = g_quark_from_string (item + k);
298 /* "256 quarks ought to be enough for anybody!"
299 * If this bites you, I'm sorry. Please file a bug.
305 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
306 0, quarks, l, &ignore_this);
311 settings_backend_writable_changed (GSettingsBackend *backend,
315 GSettings *settings = G_SETTINGS (target);
316 gboolean ignore_this;
319 g_assert (settings->priv->backend == backend);
321 for (i = 0; key[i] == settings->priv->path[i]; i++);
323 if (settings->priv->path[i] == '\0' &&
324 g_settings_schema_has_key (settings->priv->schema, key + i))
325 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
326 0, g_quark_from_string (key + i), &ignore_this);
330 settings_backend_path_writable_changed (GSettingsBackend *backend,
334 GSettings *settings = G_SETTINGS (target);
335 gboolean ignore_this;
337 g_assert (settings->priv->backend == backend);
339 if (g_str_has_prefix (settings->priv->path, path))
340 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
341 0, (GQuark) 0, &ignore_this);
345 g_settings_constructed (GObject *object)
347 GSettings *settings = G_SETTINGS (object);
348 const gchar *schema_path;
350 settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
351 schema_path = g_settings_schema_get_path (settings->priv->schema);
353 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
354 g_error ("settings object created with schema '%s' and path '%s', but "
355 "path '%s' is specified by schema",
356 settings->priv->schema_name, settings->priv->path, schema_path);
358 if (settings->priv->path == NULL)
360 if (schema_path == NULL)
361 g_error ("attempting to create schema '%s' without a path",
362 settings->priv->schema_name);
364 settings->priv->path = g_strdup (schema_path);
367 settings->priv->backend = g_settings_backend_get_with_context (settings->priv->context);
368 g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
369 settings->priv->main_context,
370 settings_backend_changed,
371 settings_backend_path_changed,
372 settings_backend_keys_changed,
373 settings_backend_writable_changed,
374 settings_backend_path_writable_changed);
375 g_settings_backend_subscribe (settings->priv->backend,
376 settings->priv->path);
380 g_settings_init (GSettings *settings)
382 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
386 settings->priv->main_context = g_main_context_get_thread_default ();
388 if (settings->priv->main_context == NULL)
389 settings->priv->main_context = g_main_context_default ();
391 g_main_context_ref (settings->priv->main_context);
396 * @settings: a #GSettings object
398 * Changes the #GSettings object into 'delay-apply' mode. In this
399 * mode, changes to @settings are not immediately propagated to the
400 * backend, but kept locally until g_settings_apply() is called.
405 g_settings_delay (GSettings *settings)
407 g_return_if_fail (G_IS_SETTINGS (settings));
409 if (settings->priv->delayed)
412 settings->priv->delayed =
413 g_delayed_settings_backend_new (settings->priv->backend, settings);
414 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
415 g_object_unref (settings->priv->backend);
417 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
418 g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
419 settings->priv->main_context,
420 settings_backend_changed,
421 settings_backend_path_changed,
422 settings_backend_keys_changed,
423 settings_backend_writable_changed,
424 settings_backend_path_writable_changed);
429 * @settings: a #GSettings instance
431 * Applies any changes that have been made to the settings. This
432 * function does nothing unless @settings is in 'delay-apply' mode;
433 * see g_settings_set_delay_apply(). In the normal case settings are
434 * always applied immediately.
437 g_settings_apply (GSettings *settings)
439 if (settings->priv->delayed)
441 GDelayedSettingsBackend *delayed;
443 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
444 g_delayed_settings_backend_apply (delayed);
450 * @settings: a #GSettings instance
452 * Reverts all non-applied changes to the settings. This function
453 * does nothing unless @settings is in 'delay-apply' mode; see
454 * g_settings_set_delay_apply(). In the normal case settings are
455 * always applied immediately.
457 * Change notifications will be emitted for affected keys.
460 g_settings_revert (GSettings *settings)
462 if (settings->priv->delayed)
464 GDelayedSettingsBackend *delayed;
466 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
467 g_delayed_settings_backend_revert (delayed);
472 g_settings_set_property (GObject *object,
477 GSettings *settings = G_SETTINGS (object);
482 g_assert (settings->priv->schema_name == NULL);
483 settings->priv->schema_name = g_value_dup_string (value);
487 settings->priv->path = g_value_dup_string (value);
491 settings->priv->context = g_value_dup_string (value);
495 g_assert_not_reached ();
500 * g_settings_get_has_unapplied:
501 * @settings: a #GSettings object
502 * @returns: %TRUE if @settings has unapplied changes
504 * Returns whether the #GSettings object has any unapplied
505 * changes. This can only be the case if it is in 'delayed-apply' mode.
510 g_settings_get_has_unapplied (GSettings *settings)
512 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
514 return settings->priv->delayed &&
515 g_delayed_settings_backend_get_has_unapplied (
516 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
520 g_settings_get_property (GObject *object,
525 GSettings *settings = G_SETTINGS (object);
530 g_value_set_object (value, settings->priv->schema);
533 case PROP_HAS_UNAPPLIED:
534 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
538 g_assert_not_reached ();
543 g_settings_finalize (GObject *object)
545 GSettings *settings = G_SETTINGS (object);
547 g_settings_backend_unsubscribe (settings->priv->backend,
548 settings->priv->path);
549 g_main_context_unref (settings->priv->main_context);
550 g_object_unref (settings->priv->backend);
551 g_object_unref (settings->priv->schema);
552 g_free (settings->priv->schema_name);
553 g_free (settings->priv->path);
557 g_settings_class_init (GSettingsClass *class)
559 GObjectClass *object_class = G_OBJECT_CLASS (class);
561 class->writable_change_event = g_settings_real_writable_change_event;
562 class->change_event = g_settings_real_change_event;
564 object_class->set_property = g_settings_set_property;
565 object_class->get_property = g_settings_get_property;
566 object_class->constructed = g_settings_constructed;
567 object_class->finalize = g_settings_finalize;
569 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
572 * GSettings::changed:
573 * @settings: the object on which the signal was emitted
574 * @key: the name of the key that changed
576 * The "changed" signal is emitted when a key has potentially changed.
577 * You should call one of the g_settings_get() calls to check the new
580 * This signal supports detailed connections. You can connect to the
581 * detailed signal "changed::x" in order to only receive callbacks
582 * when key "x" changes.
584 g_settings_signals[SIGNAL_CHANGED] =
585 g_signal_new ("changed", G_TYPE_SETTINGS,
586 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
587 G_STRUCT_OFFSET (GSettingsClass, changed),
588 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
589 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
592 * GSettings::change-event:
593 * @settings: the object on which the signal was emitted
594 * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
595 * @n_keys: the length of the @keys array, or 0
596 * @returns: %TRUE to stop other handlers from being invoked for the
597 * event. FALSE to propagate the event further.
599 * The "change-event" signal is emitted once per change event that
600 * affects this settings object. You should connect to this signal
601 * only if you are interested in viewing groups of changes before they
602 * are split out into multiple emissions of the "changed" signal.
603 * For most use cases it is more appropriate to use the "changed" signal.
605 * In the event that the change event applies to one or more specified
606 * keys, @keys will be an array of #GQuark of length @n_keys. In the
607 * event that the change event applies to the #GSettings object as a
608 * whole (ie: potentially every key has been changed) then @keys will
609 * be %NULL and @n_keys will be 0.
611 * The default handler for this signal invokes the "changed" signal
612 * for each affected key. If any other connected handler returns
613 * %TRUE then this default functionality will be supressed.
615 g_settings_signals[SIGNAL_CHANGE_EVENT] =
616 g_signal_new ("change-event", G_TYPE_SETTINGS,
618 G_STRUCT_OFFSET (GSettingsClass, change_event),
619 g_signal_accumulator_true_handled, NULL,
620 _gio_marshal_BOOL__POINTER_INT,
621 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
624 * GSettings::writable-changed:
625 * @settings: the object on which the signal was emitted
628 * The "writable-changed" signal is emitted when the writability of a
629 * key has potentially changed. You should call
630 * g_settings_is_writable() in order to determine the new status.
632 * This signal supports detailed connections. You can connect to the
633 * detailed signal "writable-changed::x" in order to only receive
634 * callbacks when the writability of "x" changes.
636 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
637 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
638 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
639 G_STRUCT_OFFSET (GSettingsClass, changed),
640 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
641 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
644 * GSettings::writable-change-event:
645 * @settings: the object on which the signal was emitted
646 * @key: the quark of the key, or 0
647 * @returns: %TRUE to stop other handlers from being invoked for the
648 * event. FALSE to propagate the event further.
650 * The "writable-change-event" signal is emitted once per writability
651 * change event that affects this settings object. You should connect
652 * to this signal if you are interested in viewing groups of changes
653 * before they are split out into multiple emissions of the
654 * "writable-changed" signal. For most use cases it is more
655 * appropriate to use the "writable-changed" signal.
657 * In the event that the writability change applies only to a single
658 * key, @key will be set to the #GQuark for that key. In the event
659 * that the writability change affects the entire settings object,
662 * The default handler for this signal invokes the "writable-changed"
663 * and "changed" signals for each affected key. This is done because
664 * changes in writability might also imply changes in value (if for
665 * example, a new mandatory setting is introduced). If any other
666 * connected handler returns %TRUE then this default functionality
669 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
670 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
672 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
673 g_signal_accumulator_true_handled, NULL,
674 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
679 * The name of the context that the settings are stored in.
681 g_object_class_install_property (object_class, PROP_CONTEXT,
682 g_param_spec_string ("context",
684 P_("The name of the context for this settings object"),
685 "", G_PARAM_CONSTRUCT_ONLY |
686 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
691 * The name of the schema that describes the types of keys
692 * for this #GSettings object.
694 g_object_class_install_property (object_class, PROP_SCHEMA,
695 g_param_spec_string ("schema",
697 P_("The name of the schema for this settings object"),
699 G_PARAM_CONSTRUCT_ONLY |
700 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
705 * The path within the backend where the settings are stored.
707 g_object_class_install_property (object_class, PROP_PATH,
708 g_param_spec_string ("path",
710 P_("The path within the backend where the settings are"),
712 G_PARAM_CONSTRUCT_ONLY |
713 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
716 * GSettings:has-unapplied:
718 * If this property is %TRUE, the #GSettings object has outstanding
719 * changes that will be applied when g_settings_apply() is called.
721 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
722 g_param_spec_boolean ("has-unapplied",
723 P_("Has unapplied changes"),
724 P_("TRUE if there are outstanding changes to apply()"),
726 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
731 * g_settings_get_value:
732 * @settings: a #GSettings object
733 * @key: the key to get the value for
734 * @returns: a new #GVariant
736 * Gets the value that is stored in @settings for @key.
738 * It is a programmer error to give a @key that isn't valid for
744 g_settings_get_value (GSettings *settings,
747 const gchar *unparsed = NULL;
748 GVariant *value, *options;
749 const GVariantType *type;
750 gchar lc_char = '\0';
754 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
756 sval = g_settings_schema_get_value (settings->priv->schema, key, &options);
758 if G_UNLIKELY (sval == NULL)
759 g_error ("schema '%s' does not contain a key named '%s'",
760 settings->priv->schema_name, key);
762 path = g_strconcat (settings->priv->path, key, NULL);
763 type = g_variant_get_type (sval);
764 value = g_settings_backend_read (settings->priv->backend, path, type, FALSE);
771 GVariant *option_value;
773 g_variant_iter_init (&iter, options);
774 while (g_variant_iter_loop (&iter, "{&sv}", &option, &option_value))
776 if (strcmp (option, "l10n") == 0)
777 g_variant_get (option_value, "(y&s)", &lc_char, &unparsed);
779 g_warning ("unknown schema extension '%s'", option);
783 if (value && !g_variant_is_of_type (value, type))
785 g_variant_unref (value);
789 if (value == NULL && lc_char != '\0')
790 /* we will need to translate the schema default value */
792 const gchar *translated;
793 GError *error = NULL;
797 domain = g_settings_schema_get_gettext_domain (settings->priv->schema);
800 lc_category = LC_TIME;
802 lc_category = LC_MESSAGES;
804 translated = dcgettext (domain, unparsed, lc_category);
806 if (translated != unparsed)
807 /* it was translated, so we need to re-parse it */
809 value = g_variant_parse (g_variant_get_type (sval),
810 translated, NULL, NULL, &error);
814 g_warning ("Failed to parse translated string `%s' for "
815 "key `%s' in schema `%s': %s", unparsed, key,
816 settings->priv->schema_name, error->message);
817 g_warning ("Using untranslated default instead.");
818 g_error_free (error);
824 /* either translation failed or there was none to do.
825 * use the pre-compiled default.
827 value = g_variant_ref (sval);
829 g_variant_unref (sval);
835 * g_settings_set_value:
836 * @settings: a #GSettings object
837 * @key: the name of the key to set
838 * @value: a #GVariant of the correct type
839 * @returns: %TRUE if setting the key succeeded,
840 * %FALSE if the key was not writable
842 * Sets @key in @settings to @value.
844 * It is a programmer error to give a @key that isn't valid for
845 * @settings. It is a programmer error to give a @value of the
848 * If @value is floating then this function consumes the reference.
853 g_settings_set_value (GSettings *settings,
857 gboolean correct_type;
862 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
864 sval = g_settings_schema_get_value (settings->priv->schema, key, NULL);
865 correct_type = g_variant_is_of_type (value, g_variant_get_type (sval));
866 g_variant_unref (sval);
868 g_return_val_if_fail (correct_type, FALSE);
870 path = g_strconcat (settings->priv->path, key, NULL);
871 result = g_settings_backend_write (settings->priv->backend,
880 * @settings: a #GSettings object
881 * @key: the key to get the value for
882 * @format: a #GVariant format string
883 * @...: arguments as per @format
885 * Gets the value that is stored at @key in @settings.
887 * A convenience function that combines g_settings_get_value() with
890 * It is a programmer error to pass a @key that isn't valid for
891 * @settings or a @format_string that doesn't match the type of @key according
892 * to the schema of @settings.
897 g_settings_get (GSettings *settings,
905 value = g_settings_get_value (settings, key);
907 va_start (ap, format);
908 g_variant_get_va (value, format, NULL, &ap);
911 g_variant_unref (value);
916 * @settings: a #GSettings object
917 * @key: the name of the key to set
918 * @format: a #GVariant format string
919 * @...: arguments as per @format
920 * @returns: %TRUE if setting the key succeeded,
921 * %FALSE if the key was not writable
923 * Sets @key in @settings to @value.
925 * A convenience function that combines g_settings_set_value() with
928 * It is a programmer error to pass a @key that isn't valid for
929 * @settings or a @format that doesn't match the type of @key according
930 * to the schema of @settings.
935 g_settings_set (GSettings *settings,
943 va_start (ap, format);
944 value = g_variant_new_va (format, NULL, &ap);
947 return g_settings_set_value (settings, key, value);
951 * g_settings_is_writable:
952 * @settings: a #GSettings object
953 * @name: the name of a key
954 * @returns: %TRUE if the key @name is writable
956 * Finds out if a key can be written or not
961 g_settings_is_writable (GSettings *settings,
967 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
969 path = g_strconcat (settings->priv->path, name, NULL);
970 writable = g_settings_backend_get_writable (settings->priv->backend, path);
977 * g_settings_get_child:
978 * @settings: a #GSettings object
979 * @name: the name of the 'child' schema
980 * @returns: a 'child' settings object
982 * Creates a 'child' settings object which has a base path of
983 * <replaceable>base-path</replaceable>/@name", where
984 * <replaceable>base-path</replaceable> is the base path of @settings.
986 * The schema for the child settings object must have been declared
987 * in the schema of @settings using a <tag class="starttag">child</tag> element.
992 g_settings_get_child (GSettings *settings,
995 GVariant *child_schema;
1000 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1002 child_name = g_strconcat (name, "/", NULL);
1003 child_schema = g_settings_schema_get_value (settings->priv->schema,
1005 if (child_schema == NULL ||
1006 !g_variant_is_of_type (child_schema, G_VARIANT_TYPE_STRING))
1007 g_error ("Schema '%s' has no child '%s'",
1008 settings->priv->schema_name, name);
1010 child_path = g_strconcat (settings->priv->path, child_name, NULL);
1011 child = g_object_new (G_TYPE_SETTINGS,
1012 "schema", g_variant_get_string (child_schema, NULL),
1015 g_variant_unref (child_schema);
1016 g_free (child_path);
1017 g_free (child_name);
1024 * @schema: the name of the schema
1025 * @returns: a new #GSettings object
1027 * Creates a new #GSettings object with a given schema.
1029 * Signals on the newly created #GSettings object will be dispatched
1030 * via the thread-default #GMainContext in effect at the time of the
1031 * call to g_settings_new(). The new #GSettings will hold a reference
1032 * on the context. See g_main_context_push_thread_default().
1037 g_settings_new (const gchar *schema)
1039 return g_object_new (G_TYPE_SETTINGS,
1045 * g_settings_new_with_path:
1046 * @schema: the name of the schema
1047 * @path: the path to use
1048 * @returns: a new #GSettings object
1050 * Creates a new #GSettings object with a given schema and path.
1052 * You only need to do this if you want to directly create a settings
1053 * object with a schema that doesn't have a specified path of its own.
1054 * That's quite rare.
1056 * It is a programmer error to call this function for a schema that
1057 * has an explicitly specified path.
1062 g_settings_new_with_path (const gchar *schema,
1065 return g_object_new (G_TYPE_SETTINGS,
1072 * g_settings_new_with_context:
1073 * @schema: the name of the schema
1074 * @context: the context to use
1075 * @returns: a new #GSettings object
1077 * Creates a new #GSettings object with a given schema and context.
1079 * Creating settings objects with a context allow accessing settings
1080 * from a database other than the usual one. For example, it may make
1081 * sense to specify "defaults" in order to get a settings object that
1082 * modifies the system default settings instead of the settings for this
1085 * It is a programmer error to call this function for an unsupported
1086 * context. Use g_settings_supports_context() to determine if a context
1087 * is supported if you are unsure.
1092 g_settings_new_with_context (const gchar *schema,
1093 const gchar *context)
1095 return g_object_new (G_TYPE_SETTINGS,
1102 * g_settings_new_with_context_and_path:
1103 * @schema: the name of the schema
1104 * @path: the path to use
1105 * @returns: a new #GSettings object
1107 * Creates a new #GSettings object with a given schema, context and
1110 * This is a mix of g_settings_new_with_context() and
1111 * g_settings_new_with_path().
1116 g_settings_new_with_context_and_path (const gchar *schema,
1117 const gchar *context,
1120 return g_object_new (G_TYPE_SETTINGS,
1129 GSettings *settings;
1132 GSettingsBindGetMapping get_mapping;
1133 GSettingsBindSetMapping set_mapping;
1135 GDestroyNotify destroy;
1137 guint writable_handler_id;
1138 guint property_handler_id;
1139 const GParamSpec *property;
1140 guint key_handler_id;
1144 /* prevent recursion */
1149 g_settings_binding_free (gpointer data)
1151 GSettingsBinding *binding = data;
1153 g_assert (!binding->running);
1155 if (binding->writable_handler_id)
1156 g_signal_handler_disconnect (binding->settings,
1157 binding->writable_handler_id);
1159 if (binding->key_handler_id)
1160 g_signal_handler_disconnect (binding->settings,
1161 binding->key_handler_id);
1163 if (g_signal_handler_is_connected (binding->object,
1164 binding->property_handler_id))
1165 g_signal_handler_disconnect (binding->object,
1166 binding->property_handler_id);
1168 g_variant_type_free (binding->type);
1169 g_object_unref (binding->settings);
1171 if (binding->destroy)
1172 binding->destroy (binding->user_data);
1174 g_slice_free (GSettingsBinding, binding);
1178 g_settings_binding_quark (const char *property)
1183 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
1184 quark = g_quark_from_string (tmp);
1191 g_settings_binding_key_changed (GSettings *settings,
1195 GSettingsBinding *binding = user_data;
1196 GValue value = { 0, };
1199 g_assert (settings == binding->settings);
1200 g_assert (key == binding->key);
1202 if (binding->running)
1205 binding->running = TRUE;
1207 g_value_init (&value, binding->property->value_type);
1208 variant = g_settings_get_value (settings, key);
1209 if (binding->get_mapping (&value, variant, binding->user_data))
1210 g_object_set_property (binding->object,
1211 binding->property->name,
1213 g_value_unset (&value);
1215 binding->running = FALSE;
1219 g_settings_binding_property_changed (GObject *object,
1220 const GParamSpec *pspec,
1223 GSettingsBinding *binding = user_data;
1224 GValue value = { 0, };
1227 g_assert (object == binding->object);
1228 g_assert (pspec == binding->property);
1230 if (binding->running)
1233 binding->running = TRUE;
1235 g_value_init (&value, pspec->value_type);
1236 g_object_get_property (object, pspec->name, &value);
1237 if ((variant = binding->set_mapping (&value, binding->type,
1238 binding->user_data)))
1240 g_settings_set_value (binding->settings,
1242 g_variant_ref_sink (variant));
1243 g_variant_unref (variant);
1245 g_value_unset (&value);
1247 binding->running = FALSE;
1252 * @settings: a #GSettings object
1253 * @key: the key to bind
1254 * @object: a #GObject
1255 * @property: the name of the property to bind
1256 * @flags: flags for the binding
1258 * Create a binding between the @key in the @settings object
1259 * and the property @property of @object.
1261 * The binding uses the default GIO mapping functions to map
1262 * between the settings and property values. These functions
1263 * handle booleans, numeric types and string types in a
1264 * straightforward way. Use g_settings_bind_with_mapping() if
1265 * you need a custom mapping, or map between types that are not
1266 * supported by the default mapping functions.
1268 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
1269 * function also establishes a binding between the writability of
1270 * @key and the "sensitive" property of @object (if @object has
1271 * a boolean property by that name). See g_settings_bind_writable()
1272 * for more details about writable bindings.
1274 * Note that the lifecycle of the binding is tied to the object,
1275 * and that you can have only one binding per object property.
1276 * If you bind the same property twice on the same object, the second
1277 * binding overrides the first one.
1282 g_settings_bind (GSettings *settings,
1285 const gchar *property,
1286 GSettingsBindFlags flags)
1288 g_settings_bind_with_mapping (settings, key, object, property,
1289 flags, NULL, NULL, NULL, NULL);
1293 * g_settings_bind_with_mapping:
1294 * @settings: a #GSettings object
1295 * @key: the key to bind
1296 * @object: a #GObject
1297 * @property: the name of the property to bind
1298 * @flags: flags for the binding
1299 * @get_mapping: a function that gets called to convert values
1300 * from @settings to @object, or %NULL to use the default GIO mapping
1301 * @set_mapping: a function that gets called to convert values
1302 * from @object to @settings, or %NULL to use the default GIO mapping
1303 * @user_data: data that gets passed to @get_mapping and @set_mapping
1304 * @destroy: #GDestroyNotify function for @user_data
1306 * Create a binding between the @key in the @settings object
1307 * and the property @property of @object.
1309 * The binding uses the provided mapping functions to map between
1310 * settings and property values.
1312 * Note that the lifecycle of the binding is tied to the object,
1313 * and that you can have only one binding per object property.
1314 * If you bind the same property twice on the same object, the second
1315 * binding overrides the first one.
1320 g_settings_bind_with_mapping (GSettings *settings,
1323 const gchar *property,
1324 GSettingsBindFlags flags,
1325 GSettingsBindGetMapping get_mapping,
1326 GSettingsBindSetMapping set_mapping,
1328 GDestroyNotify destroy)
1330 GSettingsBinding *binding;
1331 GObjectClass *objectclass;
1332 gchar *detailed_signal;
1333 GQuark binding_quark;
1335 g_return_if_fail (G_IS_SETTINGS (settings));
1337 objectclass = G_OBJECT_GET_CLASS (object);
1339 binding = g_slice_new0 (GSettingsBinding);
1340 binding->settings = g_object_ref (settings);
1341 binding->object = object;
1342 binding->key = g_intern_string (key);
1343 binding->property = g_object_class_find_property (objectclass, property);
1344 binding->user_data = user_data;
1345 binding->destroy = destroy;
1346 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
1347 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
1349 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
1350 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
1352 if (binding->property == NULL)
1354 g_critical ("g_settings_bind: no property '%s' on class '%s'",
1355 property, G_OBJECT_TYPE_NAME (object));
1359 if ((flags & G_SETTINGS_BIND_GET) && (binding->property->flags & G_PARAM_WRITABLE) == 0)
1361 g_critical ("g_settings_bind: property '%s' on class '%s' is not writable",
1362 property, G_OBJECT_TYPE_NAME (object));
1365 if ((flags & G_SETTINGS_BIND_SET) && (binding->property->flags & G_PARAM_READABLE) == 0)
1367 g_critical ("g_settings_bind: property '%s' on class '%s' is not readable",
1368 property, G_OBJECT_TYPE_NAME (object));
1375 value = g_settings_schema_get_value (settings->priv->schema, key, NULL);
1376 binding->type = g_variant_type_copy (g_variant_get_type (value));
1378 g_variant_unref (value);
1381 if (binding->type == NULL)
1383 g_critical ("g_settings_bind: no key '%s' on schema '%s'",
1384 key, settings->priv->schema_name);
1388 if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
1389 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
1390 !g_settings_mapping_is_compatible (binding->property->value_type,
1393 g_critical ("g_settings_bind: property '%s' on class '%s' has type"
1394 "'%s' which is not compatible with type '%s' of key '%s'"
1395 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
1396 g_type_name (binding->property->value_type),
1397 g_variant_type_dup_string (binding->type), key,
1398 settings->priv->schema_name);
1402 if ((flags & G_SETTINGS_BIND_SET) &&
1403 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
1405 GParamSpec *sensitive;
1407 sensitive = g_object_class_find_property (objectclass, "sensitive");
1409 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
1410 (sensitive->flags & G_PARAM_WRITABLE))
1411 g_settings_bind_writable (settings, binding->key,
1412 object, "sensitive", FALSE);
1415 if (flags & G_SETTINGS_BIND_SET)
1417 detailed_signal = g_strdup_printf ("notify::%s", property);
1418 binding->property_handler_id =
1419 g_signal_connect (object, detailed_signal,
1420 G_CALLBACK (g_settings_binding_property_changed),
1422 g_free (detailed_signal);
1424 if (~flags & G_SETTINGS_BIND_GET)
1425 g_settings_binding_property_changed (object,
1430 if (flags & G_SETTINGS_BIND_GET)
1432 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
1434 detailed_signal = g_strdup_printf ("changed::%s", key);
1435 binding->key_handler_id =
1436 g_signal_connect (settings, detailed_signal,
1437 G_CALLBACK (g_settings_binding_key_changed),
1439 g_free (detailed_signal);
1442 g_settings_binding_key_changed (settings, binding->key, binding);
1445 binding_quark = g_settings_binding_quark (property);
1446 g_object_set_qdata_full (object, binding_quark,
1447 binding, g_settings_binding_free);
1452 GSettings *settings;
1455 const gchar *property;
1458 } GSettingsWritableBinding;
1461 g_settings_writable_binding_free (gpointer data)
1463 GSettingsWritableBinding *binding = data;
1465 g_signal_handler_disconnect (binding->settings, binding->handler_id);
1466 g_object_unref (binding->settings);
1467 g_slice_free (GSettingsWritableBinding, binding);
1471 g_settings_binding_writable_changed (GSettings *settings,
1475 GSettingsWritableBinding *binding = user_data;
1478 g_assert (settings == binding->settings);
1479 g_assert (key == binding->key);
1481 writable = g_settings_is_writable (settings, key);
1483 if (binding->inverted)
1484 writable = !writable;
1486 g_object_set (binding->object, binding->property, writable, NULL);
1490 * g_settings_bind_writable:
1491 * @settings: a #GSettings object
1492 * @key: the key to bind
1493 * @object: a #GObject
1494 * @property: the name of a boolean property to bind
1495 * @inverted: whether to 'invert' the value
1497 * Create a binding between the writability of @key in the
1498 * @settings object and the property @property of @object.
1499 * The property must be boolean; "sensitive" or "visible"
1500 * properties of widgets are the most likely candidates.
1502 * Writable bindings are always uni-directional; changes of the
1503 * writability of the setting will be propagated to the object
1504 * property, not the other way.
1506 * When the @inverted argument is %TRUE, the binding inverts the
1507 * value as it passes from the setting to the object, i.e. @property
1508 * will be set to %TRUE if the key is <emphasis>not</emphasis>
1511 * Note that the lifecycle of the binding is tied to the object,
1512 * and that you can have only one binding per object property.
1513 * If you bind the same property twice on the same object, the second
1514 * binding overrides the first one.
1519 g_settings_bind_writable (GSettings *settings,
1522 const gchar *property,
1525 GSettingsWritableBinding *binding;
1526 gchar *detailed_signal;
1529 g_return_if_fail (G_IS_SETTINGS (settings));
1531 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
1534 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
1535 property, G_OBJECT_TYPE_NAME (object));
1538 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
1540 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
1541 property, G_OBJECT_TYPE_NAME (object));
1545 binding = g_slice_new (GSettingsWritableBinding);
1546 binding->settings = g_object_ref (settings);
1547 binding->object = object;
1548 binding->key = g_intern_string (key);
1549 binding->property = g_intern_string (property);
1550 binding->inverted = inverted;
1552 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
1553 binding->handler_id =
1554 g_signal_connect (settings, detailed_signal,
1555 G_CALLBACK (g_settings_binding_writable_changed),
1557 g_free (detailed_signal);
1559 g_object_set_qdata_full (object, g_settings_binding_quark (property),
1560 binding, g_settings_writable_binding_free);
1562 g_settings_binding_writable_changed (settings, binding->key, binding);
1566 * g_settings_unbind:
1567 * @object: the object
1568 * @property: the property whose binding is removed
1570 * Removes an existing binding for @property on @object.
1572 * Note that bindings are automatically removed when the
1573 * object is finalized, so it is rarely necessary to call this
1579 g_settings_unbind (gpointer object,
1580 const gchar *property)
1582 GQuark binding_quark;
1584 binding_quark = g_settings_binding_quark (property);
1585 g_object_set_qdata (object, binding_quark, NULL);
1589 * g_settings_get_string:
1590 * @settings: a #GSettings object
1591 * @key: the key to get the value for
1592 * @returns: a newly-allocated string
1594 * Gets the value that is stored at @key in @settings.
1596 * A convenience variant of g_settings_get() for strings.
1598 * It is a programmer error to pass a @key that isn't valid for
1599 * @settings or is not of type string.
1604 g_settings_get_string (GSettings *settings,
1610 value = g_settings_get_value (settings, key);
1611 result = g_variant_dup_string (value, NULL);
1612 g_variant_unref (value);
1618 * g_settings_set_string:
1619 * @settings: a #GSettings object
1620 * @key: the name of the key to set
1621 * @value: the value to set it to
1622 * @returns: %TRUE if setting the key succeeded,
1623 * %FALSE if the key was not writable
1625 * Sets @key in @settings to @value.
1627 * A convenience variant of g_settings_set() for strings.
1629 * It is a programmer error to pass a @key that isn't valid for
1630 * @settings or is not of type string.
1635 g_settings_set_string (GSettings *settings,
1639 return g_settings_set_value (settings, key, g_variant_new_string (value));
1643 * g_settings_get_int:
1644 * @settings: a #GSettings object
1645 * @key: the key to get the value for
1646 * @returns: an integer
1648 * Gets the value that is stored at @key in @settings.
1650 * A convenience variant of g_settings_get() for 32-bit integers.
1652 * It is a programmer error to pass a @key that isn't valid for
1653 * @settings or is not of type int32.
1658 g_settings_get_int (GSettings *settings,
1664 value = g_settings_get_value (settings, key);
1665 result = g_variant_get_int32 (value);
1666 g_variant_unref (value);
1672 * g_settings_set_int:
1673 * @settings: a #GSettings object
1674 * @key: the name of the key to set
1675 * @value: the value to set it to
1676 * @returns: %TRUE if setting the key succeeded,
1677 * %FALSE if the key was not writable
1679 * Sets @key in @settings to @value.
1681 * A convenience variant of g_settings_set() for 32-bit integers.
1683 * It is a programmer error to pass a @key that isn't valid for
1684 * @settings or is not of type int32.
1689 g_settings_set_int (GSettings *settings,
1693 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1697 * g_settings_get_double:
1698 * @settings: a #GSettings object
1699 * @key: the key to get the value for
1700 * @returns: a double
1702 * Gets the value that is stored at @key in @settings.
1704 * A convenience variant of g_settings_get() for doubles.
1706 * It is a programmer error to pass a @key that isn't valid for
1707 * @settings or is not of type double.
1712 g_settings_get_double (GSettings *settings,
1718 value = g_settings_get_value (settings, key);
1719 result = g_variant_get_double (value);
1720 g_variant_unref (value);
1726 * g_settings_set_double:
1727 * @settings: a #GSettings object
1728 * @key: the name of the key to set
1729 * @value: the value to set it to
1730 * @returns: %TRUE if setting the key succeeded,
1731 * %FALSE if the key was not writable
1733 * Sets @key in @settings to @value.
1735 * A convenience variant of g_settings_set() for doubles.
1737 * It is a programmer error to pass a @key that isn't valid for
1738 * @settings or is not of type double.
1743 g_settings_set_double (GSettings *settings,
1747 return g_settings_set_value (settings, key, g_variant_new_double (value));
1751 * g_settings_get_boolean:
1752 * @settings: a #GSettings object
1753 * @key: the key to get the value for
1754 * @returns: a boolean
1756 * Gets the value that is stored at @key in @settings.
1758 * A convenience variant of g_settings_get() for booleans.
1760 * It is a programmer error to pass a @key that isn't valid for
1761 * @settings or is not of type boolean.
1766 g_settings_get_boolean (GSettings *settings,
1772 value = g_settings_get_value (settings, key);
1773 result = g_variant_get_boolean (value);
1774 g_variant_unref (value);
1780 * g_settings_set_boolean:
1781 * @settings: a #GSettings object
1782 * @key: the name of the key to set
1783 * @value: the value to set it to
1784 * @returns: %TRUE if setting the key succeeded,
1785 * %FALSE if the key was not writable
1787 * Sets @key in @settings to @value.
1789 * A convenience variant of g_settings_set() for booleans.
1791 * It is a programmer error to pass a @key that isn't valid for
1792 * @settings or is not of type boolean.
1797 g_settings_set_boolean (GSettings *settings,
1801 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1805 * g_settings_get_strv:
1806 * @settings: a #GSettings object
1807 * @key: the key to get the value for
1808 * @length: return location for the length of the result, or %NULL
1809 * @returns: a newly-allocated, %NULL-terminated array of strings
1811 * Gets the value that is stored at @key in @settings.
1813 * A convenience variant of g_settings_get() for string arrays.
1815 * It is a programmer error to pass a @key that isn't valid for
1816 * @settings or is not of type 'string array'.
1821 g_settings_get_strv (GSettings *settings,
1828 value = g_settings_get_value (settings, key);
1829 result = g_variant_dup_strv (value, length);
1830 g_variant_unref (value);
1836 * g_settings_set_strv:
1837 * @settings: a #GSettings object
1838 * @key: the name of the key to set
1839 * @value: the value to set it to
1840 * @length: the length of the @value array, or -1
1841 * @returns: %TRUE if setting the key succeeded,
1842 * %FALSE if the key was not writable
1844 * Sets @key in @settings to @value.
1846 * A convenience variant of g_settings_set() for string arrays.
1848 * It is a programmer error to pass a @key that isn't valid for
1849 * @settings or is not of type 'string array'.
1854 g_settings_set_strv (GSettings *settings,
1856 const gchar * const *value,
1859 return g_settings_set_value (settings, key, g_variant_new_strv (value, length));
1862 #define __G_SETTINGS_C__
1863 #include "gioaliasdef.c"