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 gschema-compile utility. The input is a schema description in
74 * an XML format that can be described by the following DTD:
76 * <!ELEMENT schemalist (schema*) >
77 * <!ATTLIST schemalist gettext-domain #IMPLIED >
79 * <!ELEMENT schema (key|child)* >
80 * <!ATTLIST schema id CDATA #REQUIRED
82 * gettext-domain CDATA #IMPLIED >
84 * <!ELEMENT key (default|summary?|description?|range?|choices?) >
85 * <!-- name can only contain lowercase letters, numbers and '-' -->
86 * <!-- type must be a GVariant type string -->
87 * <!ATTLIST key name CDATA #REQUIRED
88 * type CDATA #REQUIRED >
90 * <!-- the default value is specified a a serialized GVariant,
91 * i.e. you have to include the quotes when specifying a string -->
92 * <!ELEMENT default (#PCDATA) >
93 * <!-- the presence of the l10n attribute marks a default value for
94 * translation, its value is the gettext category to use -->
95 * <!-- if context is present, it specifies msgctxt to use -->
96 * <!ATTLIST default l10n (messages|time) #IMPLIED
97 * context CDATA #IMPLIED >
99 * <!ELEMENT summary (#PCDATA) >
100 * <!ELEMENT description (#PCDATA) >
102 * <!ELEMENT range (min,max) >
103 * <!ELEMENT min (#PCDATA) >
104 * <!ELEMENT max (#PCDATA) >
106 * <!ELEMENT choices (choice+) >
107 * <!ELEMENT choice (alias?) >
108 * <!ATTLIST choice value CDATA #REQUIRED >
109 * <!ELEMENT choice (alias?) >
110 * <!ELEMENT alias EMPTY >
111 * <!ATTLIST alias value CDATA #REQUIRED >
113 * <!ELEMENT child EMPTY >
114 * <!ATTLIST child name CDATA #REQUIRED
115 * schema CDATA #REQUIRED >
119 * At runtime, schemas are identified by their id (as specified
120 * in the <tag class="attribute">id</tag> attribute of the
121 * <tag class="starttag">schema</tag> element). The
122 * convention for schema ids is to use a dotted name, similar in
123 * style to a DBus bus name, e.g. "org.gnome.font-rendering".
126 * <title>Binding</title>
128 * A very convenient feature of GSettings lets you bind #GObject properties
129 * directly to settings, using g_settings_bind(). Once a GObject property
130 * has been bound to a setting, changes on either side are automatically
131 * propagated to the other side. GSettings handles details like
132 * mapping between GObject and GVariant types, and preventing infinite
136 * This makes it very easy to hook up a preferences dialog to the
137 * underlying settings. To make this even more convenient, GSettings
138 * looks for a boolean property with the name "sensitivity" and
139 * automatically binds it to the writability of the bound setting.
140 * If this 'magic' gets in the way, it can be suppressed with the
141 * #G_SETTINGS_BIND_NO_SENSITIVITY flag.
146 struct _GSettingsPrivate
148 GSettingsBackend *backend;
149 GSettingsSchema *schema;
154 GDelayedSettingsBackend *delayed;
169 SIGNAL_WRITABLE_CHANGE_EVENT,
170 SIGNAL_WRITABLE_CHANGED,
176 static guint g_settings_signals[N_SIGNALS];
178 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
181 g_settings_real_change_event (GSettings *settings,
188 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
190 for (i = 0; i < n_keys; i++)
191 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
192 keys[i], g_quark_to_string (keys[i]));
198 g_settings_real_writable_change_event (GSettings *settings,
201 const GQuark *keys = &key;
206 keys = g_settings_schema_list (settings->priv->schema, &n_keys);
208 for (i = 0; i < n_keys; i++)
210 const gchar *string = g_quark_to_string (keys[i]);
212 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
214 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
222 settings_backend_changed (GSettingsBackend *backend,
227 GSettings *settings = G_SETTINGS (user_data);
228 gboolean ignore_this;
231 g_assert (settings->priv->backend == backend);
233 for (i = 0; key[i] == settings->priv->path[i]; i++);
235 if (settings->priv->path[i] == '\0' &&
236 g_settings_schema_has_key (settings->priv->schema, key + i))
240 quark = g_quark_from_string (key + i);
241 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
242 0, &quark, 1, &ignore_this);
247 settings_backend_path_changed (GSettingsBackend *backend,
252 GSettings *settings = G_SETTINGS (user_data);
253 gboolean ignore_this;
255 g_assert (settings->priv->backend == backend);
257 if (g_str_has_prefix (settings->priv->path, path))
258 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
259 0, NULL, 0, &ignore_this);
263 settings_backend_keys_changed (GSettingsBackend *backend,
265 const gchar * const *items,
269 GSettings *settings = G_SETTINGS (user_data);
270 gboolean ignore_this;
273 g_assert (settings->priv->backend == backend);
275 for (i = 0; settings->priv->path[i] &&
276 settings->priv->path[i] == path[i]; i++);
283 for (j = 0; items[j]; j++)
285 const gchar *item = items[j];
288 for (k = 0; item[k] == settings->priv->path[i + k]; k++);
290 if (settings->priv->path[i + k] == '\0' &&
291 g_settings_schema_has_key (settings->priv->schema, item + k))
292 quarks[l++] = g_quark_from_string (item + k);
294 /* "256 quarks ought to be enough for anybody!"
295 * If this bites you, I'm sorry. Please file a bug.
301 g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
302 0, quarks, l, &ignore_this);
307 settings_backend_writable_changed (GSettingsBackend *backend,
311 GSettings *settings = G_SETTINGS (user_data);
312 gboolean ignore_this;
315 g_assert (settings->priv->backend == backend);
317 for (i = 0; key[i] == settings->priv->path[i]; i++);
319 if (settings->priv->path[i] == '\0' &&
320 g_settings_schema_has_key (settings->priv->schema, key + i))
321 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
322 0, g_quark_from_string (key + i), &ignore_this);
326 settings_backend_path_writable_changed (GSettingsBackend *backend,
330 GSettings *settings = G_SETTINGS (user_data);
331 gboolean ignore_this;
333 g_assert (settings->priv->backend == backend);
335 if (g_str_has_prefix (settings->priv->path, path))
336 g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
337 0, (GQuark) 0, &ignore_this);
341 g_settings_constructed (GObject *object)
343 GSettings *settings = G_SETTINGS (object);
344 const gchar *schema_path;
346 settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
347 schema_path = g_settings_schema_get_path (settings->priv->schema);
349 if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
350 g_error ("settings object created with schema '%s' and path '%s', but "
351 "path '%s' is specified by schema",
352 settings->priv->schema_name, settings->priv->path, schema_path);
354 if (settings->priv->path == NULL)
356 if (schema_path == NULL)
357 g_error ("attempting to create schema '%s' without a path",
358 settings->priv->schema_name);
360 settings->priv->path = g_strdup (schema_path);
363 settings->priv->backend = g_settings_backend_get_with_context (settings->priv->context);
364 g_settings_backend_watch (settings->priv->backend,
365 settings_backend_changed,
366 settings_backend_path_changed,
367 settings_backend_keys_changed,
368 settings_backend_writable_changed,
369 settings_backend_path_writable_changed,
371 g_settings_backend_subscribe (settings->priv->backend,
372 settings->priv->path);
376 g_settings_init (GSettings *settings)
378 settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
385 * @settings: a #GSettings object
387 * Changes the #GSettings object into 'delay-apply' mode. In this
388 * mode, changes to @settings are not immediately propagated to the
389 * backend, but kept locally until g_settings_apply() is called.
394 g_settings_delay (GSettings *settings)
396 if (settings->priv->delayed)
399 settings->priv->delayed =
400 g_delayed_settings_backend_new (settings->priv->backend, settings);
401 g_settings_backend_unwatch (settings->priv->backend, settings);
402 g_object_unref (settings->priv->backend);
404 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
405 g_settings_backend_watch (settings->priv->backend,
406 settings_backend_changed,
407 settings_backend_path_changed,
408 settings_backend_keys_changed,
409 settings_backend_writable_changed,
410 settings_backend_path_writable_changed,
416 * @settings: a #GSettings instance
418 * Applies any changes that have been made to the settings. This
419 * function does nothing unless @settings is in 'delay-apply' mode;
420 * see g_settings_set_delay_apply(). In the normal case settings are
421 * always applied immediately.
424 g_settings_apply (GSettings *settings)
426 if (settings->priv->delayed)
428 GDelayedSettingsBackend *delayed;
430 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
431 g_delayed_settings_backend_apply (delayed);
437 * @settings: a #GSettings instance
439 * Reverts all non-applied changes to the settings. This function
440 * does nothing unless @settings is in 'delay-apply' mode; see
441 * g_settings_set_delay_apply(). In the normal case settings are
442 * always applied immediately.
444 * Change notifications will be emitted for affected keys.
447 g_settings_revert (GSettings *settings)
449 if (settings->priv->delayed)
451 GDelayedSettingsBackend *delayed;
453 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
454 g_delayed_settings_backend_revert (delayed);
459 g_settings_set_property (GObject *object,
464 GSettings *settings = G_SETTINGS (object);
469 g_assert (settings->priv->schema_name == NULL);
470 settings->priv->schema_name = g_value_dup_string (value);
474 settings->priv->path = g_value_dup_string (value);
478 settings->priv->context = g_value_dup_string (value);
482 g_assert_not_reached ();
487 * g_settings_get_has_unapplied:
488 * @settings: a #GSettings object
489 * @returns: %TRUE if @settings has unapplied changes
491 * Returns whether the #GSettings object has any unapplied
492 * changes. This can only be the case if it is in 'delayed-apply' mode.
497 g_settings_get_has_unapplied (GSettings *settings)
499 return settings->priv->delayed &&
500 g_delayed_settings_backend_get_has_unapplied (
501 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
505 g_settings_get_property (GObject *object,
510 GSettings *settings = G_SETTINGS (object);
515 g_value_set_object (value, settings->priv->schema);
518 case PROP_HAS_UNAPPLIED:
519 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
523 g_assert_not_reached ();
528 g_settings_finalize (GObject *object)
530 GSettings *settings = G_SETTINGS (object);
532 g_settings_backend_unwatch (settings->priv->backend, settings);
533 g_settings_backend_unsubscribe (settings->priv->backend,
534 settings->priv->path);
535 g_object_unref (settings->priv->backend);
536 g_object_unref (settings->priv->schema);
537 g_free (settings->priv->schema_name);
538 g_free (settings->priv->path);
542 g_settings_class_init (GSettingsClass *class)
544 GObjectClass *object_class = G_OBJECT_CLASS (class);
546 class->writable_change_event = g_settings_real_writable_change_event;
547 class->change_event = g_settings_real_change_event;
549 object_class->set_property = g_settings_set_property;
550 object_class->get_property = g_settings_get_property;
551 object_class->constructed = g_settings_constructed;
552 object_class->finalize = g_settings_finalize;
554 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
557 * GSettings::changed:
558 * @settings: the object on which the signal was emitted
559 * @key: the name of the key that changed
561 * The "changed" signal is emitted when a key has potentially changed.
562 * You should call one of the g_settings_get() calls to check the new
565 * This signal supports detailed connections. You can connect to the
566 * detailed signal "changed::x" in order to only receive callbacks
567 * when key "x" changes.
569 g_settings_signals[SIGNAL_CHANGED] =
570 g_signal_new ("changed", G_TYPE_SETTINGS,
571 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
572 G_STRUCT_OFFSET (GSettingsClass, changed),
573 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
574 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
577 * GSettings::change-event:
578 * @settings: the object on which the signal was emitted
579 * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
580 * @n_keys: the length of the @keys array, or 0
581 * @returns: %TRUE to stop other handlers from being invoked for the
582 * event. FALSE to propagate the event further.
584 * The "change-event" signal is emitted once per change event that
585 * affects this settings object. You should connect to this signal
586 * only if you are interested in viewing groups of changes before they
587 * are split out into multiple emissions of the "changed" signal.
588 * For most use cases it is more appropriate to use the "changed" signal.
590 * In the event that the change event applies to one or more specified
591 * keys, @keys will be an array of #GQuark of length @n_keys. In the
592 * event that the change event applies to the #GSettings object as a
593 * whole (ie: potentially every key has been changed) then @keys will
594 * be %NULL and @n_keys will be 0.
596 * The default handler for this signal invokes the "changed" signal
597 * for each affected key. If any other connected handler returns
598 * %TRUE then this default functionality will be supressed.
600 g_settings_signals[SIGNAL_CHANGE_EVENT] =
601 g_signal_new ("change-event", G_TYPE_SETTINGS,
603 G_STRUCT_OFFSET (GSettingsClass, change_event),
604 g_signal_accumulator_true_handled, NULL,
605 _gio_marshal_BOOL__POINTER_INT,
606 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
609 * GSettings::writable-changed:
610 * @settings: the object on which the signal was emitted
613 * The "writable-changed" signal is emitted when the writability of a
614 * key has potentially changed. You should call
615 * g_settings_is_writable() in order to determine the new status.
617 * This signal supports detailed connections. You can connect to the
618 * detailed signal "writable-changed::x" in order to only receive
619 * callbacks when the writability of "x" changes.
621 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
622 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
623 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
624 G_STRUCT_OFFSET (GSettingsClass, changed),
625 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
626 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
629 * GSettings::writable-change-event:
630 * @settings: the object on which the signal was emitted
631 * @key: the quark of the key, or 0
632 * @returns: %TRUE to stop other handlers from being invoked for the
633 * event. FALSE to propagate the event further.
635 * The "writable-change-event" signal is emitted once per writability
636 * change event that affects this settings object. You should connect
637 * to this signal if you are interested in viewing groups of changes
638 * before they are split out into multiple emissions of the
639 * "writable-changed" signal. For most use cases it is more
640 * appropriate to use the "writable-changed" signal.
642 * In the event that the writability change applies only to a single
643 * key, @key will be set to the #GQuark for that key. In the event
644 * that the writability change affects the entire settings object,
647 * The default handler for this signal invokes the "writable-changed"
648 * and "changed" signals for each affected key. This is done because
649 * changes in writability might also imply changes in value (if for
650 * example, a new mandatory setting is introduced). If any other
651 * connected handler returns %TRUE then this default functionality
654 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
655 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
657 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
658 g_signal_accumulator_true_handled, NULL,
659 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
664 * The name of the context that the settings are stored in.
666 g_object_class_install_property (object_class, PROP_CONTEXT,
667 g_param_spec_string ("context",
669 P_("The name of the context for this settings object"),
670 "", G_PARAM_CONSTRUCT_ONLY |
671 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
676 * The name of the schema that describes the types of keys
677 * for this #GSettings object.
679 g_object_class_install_property (object_class, PROP_SCHEMA,
680 g_param_spec_string ("schema",
682 P_("The name of the schema for this settings object"),
684 G_PARAM_CONSTRUCT_ONLY |
685 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
690 * The path within the backend where the settings are stored.
692 g_object_class_install_property (object_class, PROP_PATH,
693 g_param_spec_string ("path",
695 P_("The path within the backend where the settings are"),
697 G_PARAM_CONSTRUCT_ONLY |
698 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
701 * GSettings:has-unapplied:
703 * If this property is %TRUE, the #GSettings object has outstanding
704 * changes that will be applied when g_settings_apply() is called.
706 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
707 g_param_spec_boolean ("has-unapplied",
708 P_("Has unapplied changes"),
709 P_("TRUE if there are outstanding changes to apply()"),
711 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
716 * g_settings_get_value:
717 * @settings: a #GSettings object
718 * @key: the key to get the value for
719 * @returns: a new #GVariant
721 * Gets the value that is stored in @settings for @key.
723 * It is a programmer error to give a @key that isn't valid for
729 g_settings_get_value (GSettings *settings,
732 const gchar *unparsed = NULL;
733 GVariant *value, *options;
734 const GVariantType *type;
735 gchar lc_char = '\0';
739 sval = g_settings_schema_get_value (settings->priv->schema, key, &options);
741 if G_UNLIKELY (sval == NULL)
742 g_error ("schema '%s' does not contain a key named '%s'",
743 settings->priv->schema_name, key);
745 path = g_strconcat (settings->priv->path, key, NULL);
746 type = g_variant_get_type (sval);
747 value = g_settings_backend_read (settings->priv->backend, path, type);
754 GVariant *option_value;
756 g_variant_iter_init (&iter, options);
757 while (g_variant_iter_loop (&iter, "{&sv}", &option, &option_value))
759 if (strcmp (option, "l10n") == 0)
760 g_variant_get (option_value, "(y&s)", &lc_char, &unparsed);
762 g_warning ("unknown schema extension '%s'", option);
766 if (value && !g_variant_is_of_type (value, type))
768 g_variant_unref (value);
772 if (value == NULL && lc_char != '\0')
773 /* we will need to translate the schema default value */
775 const gchar *translated;
776 GError *error = NULL;
780 domain = g_settings_schema_get_gettext_domain (settings->priv->schema);
783 lc_category = LC_TIME;
785 lc_category = LC_MESSAGES;
787 translated = dcgettext (domain, unparsed, lc_category);
789 if (translated != unparsed)
790 /* it was translated, so we need to re-parse it */
792 value = g_variant_parse (g_variant_get_type (sval),
793 translated, NULL, NULL, &error);
797 g_warning ("Failed to parse translated string `%s' for "
798 "key `%s' in schema `%s': %s", unparsed, key,
799 settings->priv->schema_name, error->message);
800 g_warning ("Using untranslated default instead.");
801 g_error_free (error);
807 /* either translation failed or there was none to do.
808 * use the pre-compiled default.
810 value = g_variant_ref (sval);
812 g_variant_unref (sval);
818 * g_settings_set_value:
819 * @settings: a #GSettings object
820 * @key: the name of the key to set
821 * @value: a #GVariant of the correct type
822 * @returns: %TRUE if setting the key succeeded,
823 * %FALSE if the key was not writable
825 * Sets @key in @settings to @value.
827 * It is a programmer error to give a @key that isn't valid for
828 * @settings. It is a programmer error to give a @value of the
831 * If @value is floating then this function consumes the reference.
836 g_settings_set_value (GSettings *settings,
840 gboolean correct_type;
845 sval = g_settings_schema_get_value (settings->priv->schema, key, NULL);
846 correct_type = g_variant_is_of_type (value, g_variant_get_type (sval));
847 g_variant_unref (sval);
849 g_return_val_if_fail (correct_type, FALSE);
851 path = g_strconcat (settings->priv->path, key, NULL);
852 result = g_settings_backend_write (settings->priv->backend,
861 * @settings: a #GSettings object
862 * @key: the key to get the value for
863 * @format: a #GVariant format string
864 * @...: arguments as per @format
866 * Gets the value that is stored at @key in @settings.
868 * A convenience function that combines g_settings_get_value() with
871 * It is a programmer error to pass a @key that isn't valid for
872 * @settings or a @format_string that doesn't match the type of @key according
873 * to the schema of @settings.
878 g_settings_get (GSettings *settings,
886 value = g_settings_get_value (settings, key);
888 va_start (ap, format);
889 g_variant_get_va (value, format, NULL, &ap);
892 g_variant_unref (value);
897 * @settings: a #GSettings object
898 * @key: the name of the key to set
899 * @format: a #GVariant format string
900 * @...: arguments as per @format
901 * @returns: %TRUE if setting the key succeeded,
902 * %FALSE if the key was not writable
904 * Sets @key in @settings to @value.
906 * A convenience function that combines g_settings_set_value() with
909 * It is a programmer error to pass a @key that isn't valid for
910 * @settings or a @format that doesn't match the type of @key according
911 * to the schema of @settings.
916 g_settings_set (GSettings *settings,
924 va_start (ap, format);
925 value = g_variant_new_va (format, NULL, &ap);
928 return g_settings_set_value (settings, key, value);
932 * g_settings_is_writable:
933 * @settings: a #GSettings object
934 * @name: the name of a key
935 * @returns: %TRUE if the key @name is writable
937 * Finds out if a key can be written or not
942 g_settings_is_writable (GSettings *settings,
948 path = g_strconcat (settings->priv->path, name, NULL);
949 writable = g_settings_backend_get_writable (settings->priv->backend, path);
956 * g_settings_get_child:
957 * @settings: a #GSettings object
958 * @name: the name of the 'child' schema
959 * @returns: a 'child' settings object
961 * Creates a 'child' settings object which has a base path of
962 * <replaceable>base-path</replaceable>/@name", where
963 * <replaceable>base-path</replaceable> is the base path of @settings.
965 * The schema for the child settings object must have been declared
966 * in the schema of @settings using a <tag class="starttag">child</tag> element.
971 g_settings_get_child (GSettings *settings,
974 GVariant *child_schema;
979 child_name = g_strconcat (name, "/", NULL);
980 child_schema = g_settings_schema_get_value (settings->priv->schema,
982 if (child_schema == NULL ||
983 !g_variant_is_of_type (child_schema, G_VARIANT_TYPE_STRING))
984 g_error ("Schema '%s' has no child '%s'",
985 settings->priv->schema_name, name);
987 child_path = g_strconcat (settings->priv->path, child_name, NULL);
988 child = g_object_new (G_TYPE_SETTINGS,
989 "schema", g_variant_get_string (child_schema, NULL),
992 g_variant_unref (child_schema);
1001 * @schema: the name of the schema
1002 * @returns: a new #GSettings object
1004 * Creates a new #GSettings object with a given schema.
1009 g_settings_new (const gchar *schema)
1011 return g_object_new (G_TYPE_SETTINGS,
1017 * g_settings_new_with_path:
1018 * @schema: the name of the schema
1019 * @path: the path to use
1020 * @returns: a new #GSettings object
1022 * Creates a new #GSettings object with a given schema and path.
1024 * You only need to do this if you want to directly create a settings
1025 * object with a schema that doesn't have a specified path of its own.
1026 * That's quite rare.
1028 * It is a programmer error to call this function for a schema that
1029 * has an explicitly specified path.
1034 g_settings_new_with_path (const gchar *schema,
1037 return g_object_new (G_TYPE_SETTINGS,
1044 * g_settings_new_with_context:
1045 * @schema: the name of the schema
1046 * @context: the context to use
1047 * @returns: a new #GSettings object
1049 * Creates a new #GSettings object with a given schema and context.
1051 * Creating settings objects with a context allow accessing settings
1052 * from a database other than the usual one. For example, it may make
1053 * sense to specify "defaults" in order to get a settings object that
1054 * modifies the system default settings instead of the settings for this
1057 * It is a programmer error to call this function for an unsupported
1058 * context. Use g_settings_supports_context() to determine if a context
1059 * is supported if you are unsure.
1064 g_settings_new_with_context (const gchar *schema,
1065 const gchar *context)
1067 return g_object_new (G_TYPE_SETTINGS,
1074 * g_settings_new_with_context_and_path:
1075 * @schema: the name of the schema
1076 * @path: the path to use
1077 * @returns: a new #GSettings object
1079 * Creates a new #GSettings object with a given schema, context and
1082 * This is a mix of g_settings_new_with_context() and
1083 * g_settings_new_with_path().
1088 g_settings_new_with_context_and_path (const gchar *schema,
1089 const gchar *context,
1092 return g_object_new (G_TYPE_SETTINGS,
1101 GSettings *settings;
1104 GSettingsBindGetMapping get_mapping;
1105 GSettingsBindSetMapping set_mapping;
1107 GDestroyNotify destroy;
1109 guint writable_handler_id;
1110 guint property_handler_id;
1111 const GParamSpec *property;
1112 guint key_handler_id;
1116 /* prevent recursion */
1121 g_settings_binding_free (gpointer data)
1123 GSettingsBinding *binding = data;
1125 g_assert (!binding->running);
1127 if (binding->writable_handler_id)
1128 g_signal_handler_disconnect (binding->settings,
1129 binding->writable_handler_id);
1131 if (binding->key_handler_id)
1132 g_signal_handler_disconnect (binding->settings,
1133 binding->key_handler_id);
1135 if (g_signal_handler_is_connected (binding->object,
1136 binding->property_handler_id))
1137 g_signal_handler_disconnect (binding->object,
1138 binding->property_handler_id);
1140 g_variant_type_free (binding->type);
1141 g_object_unref (binding->settings);
1143 if (binding->destroy)
1144 binding->destroy (binding->user_data);
1146 g_slice_free (GSettingsBinding, binding);
1150 g_settings_binding_quark (const char *property)
1155 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
1156 quark = g_quark_from_string (tmp);
1163 g_settings_binding_key_changed (GSettings *settings,
1167 GSettingsBinding *binding = user_data;
1168 GValue value = { 0, };
1171 g_assert (settings == binding->settings);
1172 g_assert (key == binding->key);
1174 if (binding->running)
1177 binding->running = TRUE;
1179 g_value_init (&value, binding->property->value_type);
1180 variant = g_settings_get_value (settings, key);
1181 if (binding->get_mapping (&value, variant, binding->user_data))
1182 g_object_set_property (binding->object,
1183 binding->property->name,
1185 g_value_unset (&value);
1187 binding->running = FALSE;
1191 g_settings_binding_property_changed (GObject *object,
1192 const GParamSpec *pspec,
1195 GSettingsBinding *binding = user_data;
1196 GValue value = { 0, };
1199 g_assert (object == binding->object);
1200 g_assert (pspec == binding->property);
1202 if (binding->running)
1205 binding->running = TRUE;
1207 g_value_init (&value, pspec->value_type);
1208 g_object_get_property (object, pspec->name, &value);
1209 if ((variant = binding->set_mapping (&value, binding->type,
1210 binding->user_data)))
1212 g_settings_set_value (binding->settings,
1214 g_variant_ref_sink (variant));
1215 g_variant_unref (variant);
1217 g_value_unset (&value);
1219 binding->running = FALSE;
1224 * @settings: a #GSettings object
1225 * @key: the key to bind
1226 * @object: a #GObject
1227 * @property: the name of the property to bind
1228 * @flags: flags for the binding
1230 * Create a binding between the @key in the @settings object
1231 * and the property @property of @object.
1233 * The binding uses the default GIO mapping functions to map
1234 * between the settings and property values. These functions
1235 * handle booleans, numeric types and string types in a
1236 * straightforward way. Use g_settings_bind_with_mapping()
1237 * if you need a custom mapping, or map between types that
1238 * are not supported by the default mapping functions.
1240 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
1241 * function also establishes a binding between the writability of
1242 * @key and the "sensitive" property of @object (if @object has
1243 * a boolean property by that name). See g_settings_bind_writable()
1244 * for more details about writable bindings.
1246 * Note that the lifecycle of the binding is tied to the object,
1247 * and that you can have only one binding per object property.
1248 * If you bind the same property twice on the same object, the second
1249 * binding overrides the first one.
1254 g_settings_bind (GSettings *settings,
1257 const gchar *property,
1258 GSettingsBindFlags flags)
1260 g_settings_bind_with_mapping (settings, key, object, property,
1261 flags, NULL, NULL, NULL, NULL);
1265 * g_settings_bind_with_mapping:
1266 * @settings: a #GSettings object
1267 * @key: the key to bind
1268 * @object: a #GObject
1269 * @property: the name of the property to bind
1270 * @flags: flags for the binding
1271 * @get_mapping: a function that gets called to convert values
1272 * from @settings to @object, or %NULL to use the default GIO mapping
1273 * @set_mapping: a function that gets called to convert values
1274 * from @object to @settings, or %NULL to use the default GIO mapping
1275 * @user_data: data that gets passed to @get_mapping and @set_mapping
1276 * @destroy: #GDestroyNotify function for @user_data
1278 * Create a binding between the @key in the @settings object
1279 * and the property @property of @object.
1281 * The binding uses the provided mapping functions to map between
1282 * settings and property values.
1284 * Note that the lifecycle of the binding is tied to the object,
1285 * and that you can have only one binding per object property.
1286 * If you bind the same property twice on the same object, the second
1287 * binding overrides the first one.
1292 g_settings_bind_with_mapping (GSettings *settings,
1295 const gchar *property,
1296 GSettingsBindFlags flags,
1297 GSettingsBindGetMapping get_mapping,
1298 GSettingsBindSetMapping set_mapping,
1300 GDestroyNotify destroy)
1302 GSettingsBinding *binding;
1303 GObjectClass *objectclass;
1304 gchar *detailed_signal;
1305 GQuark binding_quark;
1307 objectclass = G_OBJECT_GET_CLASS (object);
1309 binding = g_slice_new0 (GSettingsBinding);
1310 binding->settings = g_object_ref (settings);
1311 binding->object = object;
1312 binding->key = g_intern_string (key);
1313 binding->property = g_object_class_find_property (objectclass, property);
1314 binding->user_data = user_data;
1315 binding->destroy = destroy;
1316 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
1317 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
1319 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
1320 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
1322 if (binding->property == NULL)
1324 g_critical ("g_settings_bind: no property '%s' on class '%s'",
1325 property, G_OBJECT_TYPE_NAME (object));
1332 value = g_settings_schema_get_value (settings->priv->schema, key, NULL);
1333 binding->type = g_variant_type_copy (g_variant_get_type (value));
1335 g_variant_unref (value);
1338 if (binding->type == NULL)
1340 g_critical ("g_settings_bind: no key '%s' on schema '%s'",
1341 key, settings->priv->schema_name);
1345 if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
1346 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
1347 !g_settings_mapping_is_compatible (binding->property->value_type,
1350 g_critical ("g_settings_bind: property '%s' on class '%s' has type"
1351 "'%s' which is not compatible with type '%s' of key '%s'"
1352 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
1353 g_type_name (binding->property->value_type),
1354 g_variant_type_dup_string (binding->type), key,
1355 settings->priv->schema_name);
1359 if ((flags & G_SETTINGS_BIND_SET) &&
1360 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
1362 GParamSpec *sensitive;
1364 sensitive = g_object_class_find_property (objectclass, "sensitive");
1366 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN)
1367 g_settings_bind_writable (settings, binding->key,
1368 object, "sensitive", FALSE);
1371 if (flags & G_SETTINGS_BIND_SET)
1373 detailed_signal = g_strdup_printf ("notify::%s", property);
1374 binding->property_handler_id =
1375 g_signal_connect (object, detailed_signal,
1376 G_CALLBACK (g_settings_binding_property_changed),
1378 g_free (detailed_signal);
1380 if (~flags & G_SETTINGS_BIND_GET)
1381 g_settings_binding_property_changed (object,
1386 if (flags & G_SETTINGS_BIND_GET)
1388 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
1390 detailed_signal = g_strdup_printf ("changed::%s", key);
1391 binding->key_handler_id =
1392 g_signal_connect (settings, detailed_signal,
1393 G_CALLBACK (g_settings_binding_key_changed),
1395 g_free (detailed_signal);
1398 g_settings_binding_key_changed (settings, binding->key, binding);
1401 binding_quark = g_settings_binding_quark (property);
1402 g_object_set_qdata_full (object, binding_quark,
1403 binding, g_settings_binding_free);
1408 GSettings *settings;
1411 const gchar *property;
1414 } GSettingsWritableBinding;
1417 g_settings_writable_binding_free (gpointer data)
1419 GSettingsWritableBinding *binding = data;
1421 g_signal_handler_disconnect (binding->settings, binding->handler_id);
1422 g_object_unref (binding->settings);
1426 g_settings_binding_writable_changed (GSettings *settings,
1430 GSettingsWritableBinding *binding = user_data;
1433 g_assert (settings == binding->settings);
1434 g_assert (key == binding->key);
1436 writable = g_settings_is_writable (settings, key);
1438 if (binding->inverted)
1439 writable = !writable;
1441 g_object_set (binding->object, binding->property, writable, NULL);
1445 * g_settings_bind_writable:
1446 * @settings: a #GSettings object
1447 * @key: the key to bind
1448 * @object: a #GObject
1449 * @property: the name of a boolean property to bind
1450 * @inverted: whether to 'invert' the value
1452 * Create a binding between the writability of @key in the
1453 * @settings object and the property @property of @object.
1454 * The property must be boolean; "sensitive" or "visible"
1455 * properties of widgets are the most likely candidates.
1457 * Writable bindings are always uni-directional; changes of the
1458 * writability of the setting will be propagated to the object
1459 * property, not the other way.
1461 * When the @inverted argument is %TRUE, the binding inverts the
1462 * value as it passes from the setting to the object, i.e. @property
1463 * will be set to %TRUE if the key is <emphasis>not</emphasis>
1466 * Note that the lifecycle of the binding is tied to the object,
1467 * and that you can have only one binding per object property.
1468 * If you bind the same property twice on the same object, the second
1469 * binding overrides the first one.
1474 g_settings_bind_writable (GSettings *settings,
1477 const gchar *property,
1480 GSettingsWritableBinding *binding;
1481 gchar *detailed_signal;
1483 binding = g_slice_new (GSettingsWritableBinding);
1484 binding->settings = g_object_ref (settings);
1485 binding->object = object;
1486 binding->property = g_intern_string (property);
1487 binding->inverted = inverted;
1489 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
1490 binding->handler_id =
1491 g_signal_connect (settings, detailed_signal,
1492 G_CALLBACK (g_settings_binding_writable_changed),
1494 g_free (detailed_signal);
1496 g_object_set_qdata_full (object, g_settings_binding_quark (property),
1497 binding, g_settings_writable_binding_free);
1499 g_settings_binding_writable_changed (settings, binding->key, binding);
1503 * g_settings_unbind:
1504 * @object: the object
1505 * @property: the property whose binding is removed
1507 * Removes an existing binding for @property on @object.
1509 * Note that bindings are automatically removed when the
1510 * object is finalized, so it is rarely necessary to call this
1516 g_settings_unbind (gpointer object,
1517 const gchar *property)
1519 GQuark binding_quark;
1521 binding_quark = g_settings_binding_quark (property);
1522 g_object_set_qdata (object, binding_quark, NULL);
1526 * g_settings_get_string:
1527 * @settings: a #GSettings object
1528 * @key: the key to get the value for
1529 * @returns: a newly-allocated string
1531 * Gets the value that is stored at @key in @settings.
1533 * A convenience variant of g_settings_get() for strings.
1535 * It is a programmer error to pass a @key that isn't valid for
1536 * @settings or is not of type string.
1541 g_settings_get_string (GSettings *settings,
1547 value = g_settings_get_value (settings, key);
1548 result = g_variant_dup_string (value, NULL);
1549 g_variant_unref (value);
1555 * g_settings_set_string:
1556 * @settings: a #GSettings object
1557 * @key: the name of the key to set
1558 * @value: the value to set it to
1559 * @returns: %TRUE if setting the key succeeded,
1560 * %FALSE if the key was not writable
1562 * Sets @key in @settings to @value.
1564 * A convenience variant of g_settings_set() for strings.
1566 * It is a programmer error to pass a @key that isn't valid for
1567 * @settings or is not of type string.
1572 g_settings_set_string (GSettings *settings,
1576 return g_settings_set_value (settings, key, g_variant_new_string (value));
1580 * g_settings_get_int:
1581 * @settings: a #GSettings object
1582 * @key: the key to get the value for
1583 * @returns: an integer
1585 * Gets the value that is stored at @key in @settings.
1587 * A convenience variant of g_settings_get() for 32-bit integers.
1589 * It is a programmer error to pass a @key that isn't valid for
1590 * @settings or is not of type int32.
1595 g_settings_get_int (GSettings *settings,
1601 value = g_settings_get_value (settings, key);
1602 result = g_variant_get_int32 (value);
1603 g_variant_unref (value);
1609 * g_settings_set_int:
1610 * @settings: a #GSettings object
1611 * @key: the name of the key to set
1612 * @value: the value to set it to
1613 * @returns: %TRUE if setting the key succeeded,
1614 * %FALSE if the key was not writable
1616 * Sets @key in @settings to @value.
1618 * A convenience variant of g_settings_set() for 32-bit integers.
1620 * It is a programmer error to pass a @key that isn't valid for
1621 * @settings or is not of type int32.
1626 g_settings_set_int (GSettings *settings,
1630 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1634 * g_settings_get_double:
1635 * @settings: a #GSettings object
1636 * @key: the key to get the value for
1637 * @returns: a double
1639 * Gets the value that is stored at @key in @settings.
1641 * A convenience variant of g_settings_get() for doubles.
1643 * It is a programmer error to pass a @key that isn't valid for
1644 * @settings or is not of type double.
1649 g_settings_get_double (GSettings *settings,
1655 value = g_settings_get_value (settings, key);
1656 result = g_variant_get_double (value);
1657 g_variant_unref (value);
1663 * g_settings_set_double:
1664 * @settings: a #GSettings object
1665 * @key: the name of the key to set
1666 * @value: the value to set it to
1667 * @returns: %TRUE if setting the key succeeded,
1668 * %FALSE if the key was not writable
1670 * Sets @key in @settings to @value.
1672 * A convenience variant of g_settings_set() for doubles.
1674 * It is a programmer error to pass a @key that isn't valid for
1675 * @settings or is not of type double.
1680 g_settings_set_double (GSettings *settings,
1684 return g_settings_set_value (settings, key, g_variant_new_double (value));
1688 * g_settings_get_boolean:
1689 * @settings: a #GSettings object
1690 * @key: the key to get the value for
1691 * @returns: a boolean
1693 * Gets the value that is stored at @key in @settings.
1695 * A convenience variant of g_settings_get() for booleans.
1697 * It is a programmer error to pass a @key that isn't valid for
1698 * @settings or is not of type boolean.
1703 g_settings_get_boolean (GSettings *settings,
1709 value = g_settings_get_value (settings, key);
1710 result = g_variant_get_boolean (value);
1711 g_variant_unref (value);
1717 * g_settings_set_boolean:
1718 * @settings: a #GSettings object
1719 * @key: the name of the key to set
1720 * @value: the value to set it to
1721 * @returns: %TRUE if setting the key succeeded,
1722 * %FALSE if the key was not writable
1724 * Sets @key in @settings to @value.
1726 * A convenience variant of g_settings_set() for booleans.
1728 * It is a programmer error to pass a @key that isn't valid for
1729 * @settings or is not of type boolean.
1734 g_settings_set_boolean (GSettings *settings,
1738 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1742 * g_settings_get_strv:
1743 * @settings: a #GSettings object
1744 * @key: the key to get the value for
1745 * @returns: a newly-allocated, %NULL-terminated array of strings
1747 * Gets the value that is stored at @key in @settings.
1749 * A convenience variant of g_settings_get() for string arrays.
1751 * It is a programmer error to pass a @key that isn't valid for
1752 * @settings or is not of type 'string array'.
1757 g_settings_get_strv (GSettings *settings,
1764 value = g_settings_get_value (settings, key);
1765 result = g_variant_dup_strv (value, length);
1766 g_variant_unref (value);
1772 * g_settings_set_strv:
1773 * @settings: a #GSettings object
1774 * @key: the name of the key to set
1775 * @value: the value to set it to
1776 * @returns: %TRUE if setting the key succeeded,
1777 * %FALSE if the key was not writable
1779 * Sets @key in @settings to @value.
1781 * A convenience variant of g_settings_set() for string arrays.
1783 * It is a programmer error to pass a @key that isn't valid for
1784 * @settings or is not of type 'string array'.
1789 g_settings_set_strv (GSettings *settings,
1791 const gchar * const *value,
1794 return g_settings_set_value (settings, key, g_variant_new_strv (value, length));
1797 #define __G_SETTINGS_C__
1798 #include "gioaliasdef.c"