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,
415 settings->priv->main_context);
416 g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
417 g_object_unref (settings->priv->backend);
419 settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
420 g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
421 settings->priv->main_context,
422 settings_backend_changed,
423 settings_backend_path_changed,
424 settings_backend_keys_changed,
425 settings_backend_writable_changed,
426 settings_backend_path_writable_changed);
431 * @settings: a #GSettings instance
433 * Applies any changes that have been made to the settings. This
434 * function does nothing unless @settings is in 'delay-apply' mode;
435 * see g_settings_set_delay_apply(). In the normal case settings are
436 * always applied immediately.
439 g_settings_apply (GSettings *settings)
441 if (settings->priv->delayed)
443 GDelayedSettingsBackend *delayed;
445 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
446 g_delayed_settings_backend_apply (delayed);
452 * @settings: a #GSettings instance
454 * Reverts all non-applied changes to the settings. This function
455 * does nothing unless @settings is in 'delay-apply' mode; see
456 * g_settings_set_delay_apply(). In the normal case settings are
457 * always applied immediately.
459 * Change notifications will be emitted for affected keys.
462 g_settings_revert (GSettings *settings)
464 if (settings->priv->delayed)
466 GDelayedSettingsBackend *delayed;
468 delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
469 g_delayed_settings_backend_revert (delayed);
474 g_settings_set_property (GObject *object,
479 GSettings *settings = G_SETTINGS (object);
484 g_assert (settings->priv->schema_name == NULL);
485 settings->priv->schema_name = g_value_dup_string (value);
489 settings->priv->path = g_value_dup_string (value);
493 settings->priv->context = g_value_dup_string (value);
497 g_assert_not_reached ();
502 * g_settings_get_has_unapplied:
503 * @settings: a #GSettings object
504 * @returns: %TRUE if @settings has unapplied changes
506 * Returns whether the #GSettings object has any unapplied
507 * changes. This can only be the case if it is in 'delayed-apply' mode.
512 g_settings_get_has_unapplied (GSettings *settings)
514 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
516 return settings->priv->delayed &&
517 g_delayed_settings_backend_get_has_unapplied (
518 G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
522 g_settings_get_property (GObject *object,
527 GSettings *settings = G_SETTINGS (object);
532 g_value_set_object (value, settings->priv->schema);
535 case PROP_HAS_UNAPPLIED:
536 g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
540 g_assert_not_reached ();
545 g_settings_finalize (GObject *object)
547 GSettings *settings = G_SETTINGS (object);
549 g_settings_backend_unsubscribe (settings->priv->backend,
550 settings->priv->path);
551 g_main_context_unref (settings->priv->main_context);
552 g_object_unref (settings->priv->backend);
553 g_object_unref (settings->priv->schema);
554 g_free (settings->priv->schema_name);
555 g_free (settings->priv->path);
559 g_settings_class_init (GSettingsClass *class)
561 GObjectClass *object_class = G_OBJECT_CLASS (class);
563 class->writable_change_event = g_settings_real_writable_change_event;
564 class->change_event = g_settings_real_change_event;
566 object_class->set_property = g_settings_set_property;
567 object_class->get_property = g_settings_get_property;
568 object_class->constructed = g_settings_constructed;
569 object_class->finalize = g_settings_finalize;
571 g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
574 * GSettings::changed:
575 * @settings: the object on which the signal was emitted
576 * @key: the name of the key that changed
578 * The "changed" signal is emitted when a key has potentially changed.
579 * You should call one of the g_settings_get() calls to check the new
582 * This signal supports detailed connections. You can connect to the
583 * detailed signal "changed::x" in order to only receive callbacks
584 * when key "x" changes.
586 g_settings_signals[SIGNAL_CHANGED] =
587 g_signal_new ("changed", G_TYPE_SETTINGS,
588 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
589 G_STRUCT_OFFSET (GSettingsClass, changed),
590 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
591 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
594 * GSettings::change-event:
595 * @settings: the object on which the signal was emitted
596 * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
597 * @n_keys: the length of the @keys array, or 0
598 * @returns: %TRUE to stop other handlers from being invoked for the
599 * event. FALSE to propagate the event further.
601 * The "change-event" signal is emitted once per change event that
602 * affects this settings object. You should connect to this signal
603 * only if you are interested in viewing groups of changes before they
604 * are split out into multiple emissions of the "changed" signal.
605 * For most use cases it is more appropriate to use the "changed" signal.
607 * In the event that the change event applies to one or more specified
608 * keys, @keys will be an array of #GQuark of length @n_keys. In the
609 * event that the change event applies to the #GSettings object as a
610 * whole (ie: potentially every key has been changed) then @keys will
611 * be %NULL and @n_keys will be 0.
613 * The default handler for this signal invokes the "changed" signal
614 * for each affected key. If any other connected handler returns
615 * %TRUE then this default functionality will be supressed.
617 g_settings_signals[SIGNAL_CHANGE_EVENT] =
618 g_signal_new ("change-event", G_TYPE_SETTINGS,
620 G_STRUCT_OFFSET (GSettingsClass, change_event),
621 g_signal_accumulator_true_handled, NULL,
622 _gio_marshal_BOOL__POINTER_INT,
623 G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
626 * GSettings::writable-changed:
627 * @settings: the object on which the signal was emitted
630 * The "writable-changed" signal is emitted when the writability of a
631 * key has potentially changed. You should call
632 * g_settings_is_writable() in order to determine the new status.
634 * This signal supports detailed connections. You can connect to the
635 * detailed signal "writable-changed::x" in order to only receive
636 * callbacks when the writability of "x" changes.
638 g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
639 g_signal_new ("writable-changed", G_TYPE_SETTINGS,
640 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
641 G_STRUCT_OFFSET (GSettingsClass, changed),
642 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
643 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
646 * GSettings::writable-change-event:
647 * @settings: the object on which the signal was emitted
648 * @key: the quark of the key, or 0
649 * @returns: %TRUE to stop other handlers from being invoked for the
650 * event. FALSE to propagate the event further.
652 * The "writable-change-event" signal is emitted once per writability
653 * change event that affects this settings object. You should connect
654 * to this signal if you are interested in viewing groups of changes
655 * before they are split out into multiple emissions of the
656 * "writable-changed" signal. For most use cases it is more
657 * appropriate to use the "writable-changed" signal.
659 * In the event that the writability change applies only to a single
660 * key, @key will be set to the #GQuark for that key. In the event
661 * that the writability change affects the entire settings object,
664 * The default handler for this signal invokes the "writable-changed"
665 * and "changed" signals for each affected key. This is done because
666 * changes in writability might also imply changes in value (if for
667 * example, a new mandatory setting is introduced). If any other
668 * connected handler returns %TRUE then this default functionality
671 g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
672 g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
674 G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
675 g_signal_accumulator_true_handled, NULL,
676 _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
681 * The name of the context that the settings are stored in.
683 g_object_class_install_property (object_class, PROP_CONTEXT,
684 g_param_spec_string ("context",
686 P_("The name of the context for this settings object"),
687 "", G_PARAM_CONSTRUCT_ONLY |
688 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
693 * The name of the schema that describes the types of keys
694 * for this #GSettings object.
696 g_object_class_install_property (object_class, PROP_SCHEMA,
697 g_param_spec_string ("schema",
699 P_("The name of the schema for this settings object"),
701 G_PARAM_CONSTRUCT_ONLY |
702 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
707 * The path within the backend where the settings are stored.
709 g_object_class_install_property (object_class, PROP_PATH,
710 g_param_spec_string ("path",
712 P_("The path within the backend where the settings are"),
714 G_PARAM_CONSTRUCT_ONLY |
715 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
718 * GSettings:has-unapplied:
720 * If this property is %TRUE, the #GSettings object has outstanding
721 * changes that will be applied when g_settings_apply() is called.
723 g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
724 g_param_spec_boolean ("has-unapplied",
725 P_("Has unapplied changes"),
726 P_("TRUE if there are outstanding changes to apply()"),
728 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
733 * g_settings_get_value:
734 * @settings: a #GSettings object
735 * @key: the key to get the value for
736 * @returns: a new #GVariant
738 * Gets the value that is stored in @settings for @key.
740 * It is a programmer error to give a @key that isn't valid for
746 g_settings_get_value (GSettings *settings,
749 const gchar *unparsed = NULL;
750 GVariant *value, *options;
751 const GVariantType *type;
752 gchar lc_char = '\0';
756 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
758 sval = g_settings_schema_get_value (settings->priv->schema, key, &options);
760 if G_UNLIKELY (sval == NULL)
761 g_error ("schema '%s' does not contain a key named '%s'",
762 settings->priv->schema_name, key);
764 path = g_strconcat (settings->priv->path, key, NULL);
765 type = g_variant_get_type (sval);
766 value = g_settings_backend_read (settings->priv->backend, path, type, FALSE);
773 GVariant *option_value;
775 g_variant_iter_init (&iter, options);
776 while (g_variant_iter_loop (&iter, "{&sv}", &option, &option_value))
778 if (strcmp (option, "l10n") == 0)
779 g_variant_get (option_value, "(y&s)", &lc_char, &unparsed);
781 g_warning ("unknown schema extension '%s'", option);
785 if (value && !g_variant_is_of_type (value, type))
787 g_variant_unref (value);
791 if (value == NULL && lc_char != '\0')
792 /* we will need to translate the schema default value */
794 const gchar *translated;
795 GError *error = NULL;
798 domain = g_settings_schema_get_gettext_domain (settings->priv->schema);
801 translated = g_dcgettext (domain, unparsed, LC_TIME);
803 translated = g_dgettext (domain, unparsed);
805 if (translated != unparsed)
806 /* it was translated, so we need to re-parse it */
808 value = g_variant_parse (g_variant_get_type (sval),
809 translated, NULL, NULL, &error);
813 g_warning ("Failed to parse translated string `%s' for "
814 "key `%s' in schema `%s': %s", unparsed, key,
815 settings->priv->schema_name, error->message);
816 g_warning ("Using untranslated default instead.");
817 g_error_free (error);
823 /* either translation failed or there was none to do.
824 * use the pre-compiled default.
826 value = g_variant_ref (sval);
828 g_variant_unref (sval);
834 * g_settings_set_value:
835 * @settings: a #GSettings object
836 * @key: the name of the key to set
837 * @value: a #GVariant of the correct type
838 * @returns: %TRUE if setting the key succeeded,
839 * %FALSE if the key was not writable
841 * Sets @key in @settings to @value.
843 * It is a programmer error to give a @key that isn't valid for
844 * @settings. It is a programmer error to give a @value of the
847 * If @value is floating then this function consumes the reference.
852 g_settings_set_value (GSettings *settings,
856 gboolean correct_type;
861 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
863 sval = g_settings_schema_get_value (settings->priv->schema, key, NULL);
864 correct_type = g_variant_is_of_type (value, g_variant_get_type (sval));
865 g_variant_unref (sval);
867 g_return_val_if_fail (correct_type, FALSE);
869 path = g_strconcat (settings->priv->path, key, NULL);
870 result = g_settings_backend_write (settings->priv->backend,
879 * @settings: a #GSettings object
880 * @key: the key to get the value for
881 * @format: a #GVariant format string
882 * @...: arguments as per @format
884 * Gets the value that is stored at @key in @settings.
886 * A convenience function that combines g_settings_get_value() with
889 * It is a programmer error to pass a @key that isn't valid for
890 * @settings or a @format_string that doesn't match the type of @key according
891 * to the schema of @settings.
896 g_settings_get (GSettings *settings,
904 value = g_settings_get_value (settings, key);
906 va_start (ap, format);
907 g_variant_get_va (value, format, NULL, &ap);
910 g_variant_unref (value);
915 * @settings: a #GSettings object
916 * @key: the name of the key to set
917 * @format: a #GVariant format string
918 * @...: arguments as per @format
919 * @returns: %TRUE if setting the key succeeded,
920 * %FALSE if the key was not writable
922 * Sets @key in @settings to @value.
924 * A convenience function that combines g_settings_set_value() with
927 * It is a programmer error to pass a @key that isn't valid for
928 * @settings or a @format that doesn't match the type of @key according
929 * to the schema of @settings.
934 g_settings_set (GSettings *settings,
942 va_start (ap, format);
943 value = g_variant_new_va (format, NULL, &ap);
946 return g_settings_set_value (settings, key, value);
950 * g_settings_is_writable:
951 * @settings: a #GSettings object
952 * @name: the name of a key
953 * @returns: %TRUE if the key @name is writable
955 * Finds out if a key can be written or not
960 g_settings_is_writable (GSettings *settings,
966 g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
968 path = g_strconcat (settings->priv->path, name, NULL);
969 writable = g_settings_backend_get_writable (settings->priv->backend, path);
976 * g_settings_get_child:
977 * @settings: a #GSettings object
978 * @name: the name of the 'child' schema
979 * @returns: a 'child' settings object
981 * Creates a 'child' settings object which has a base path of
982 * <replaceable>base-path</replaceable>/@name", where
983 * <replaceable>base-path</replaceable> is the base path of @settings.
985 * The schema for the child settings object must have been declared
986 * in the schema of @settings using a <tag class="starttag">child</tag> element.
991 g_settings_get_child (GSettings *settings,
994 const gchar *child_schema;
999 g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1001 child_name = g_strconcat (name, "/", NULL);
1002 child_schema = g_settings_schema_get_string (settings->priv->schema,
1004 if (child_schema == NULL)
1005 g_error ("Schema '%s' has no child '%s'",
1006 settings->priv->schema_name, name);
1008 child_path = g_strconcat (settings->priv->path, child_name, NULL);
1009 child = g_object_new (G_TYPE_SETTINGS,
1010 "schema", child_schema,
1013 g_free (child_path);
1014 g_free (child_name);
1021 * @schema: the name of the schema
1022 * @returns: a new #GSettings object
1024 * Creates a new #GSettings object with a given schema.
1026 * Signals on the newly created #GSettings object will be dispatched
1027 * via the thread-default #GMainContext in effect at the time of the
1028 * call to g_settings_new(). The new #GSettings will hold a reference
1029 * on the context. See g_main_context_push_thread_default().
1034 g_settings_new (const gchar *schema)
1036 return g_object_new (G_TYPE_SETTINGS,
1042 * g_settings_new_with_path:
1043 * @schema: the name of the schema
1044 * @path: the path to use
1045 * @returns: a new #GSettings object
1047 * Creates a new #GSettings object with a given schema and path.
1049 * You only need to do this if you want to directly create a settings
1050 * object with a schema that doesn't have a specified path of its own.
1051 * That's quite rare.
1053 * It is a programmer error to call this function for a schema that
1054 * has an explicitly specified path.
1059 g_settings_new_with_path (const gchar *schema,
1062 return g_object_new (G_TYPE_SETTINGS,
1069 * g_settings_new_with_context:
1070 * @schema: the name of the schema
1071 * @context: the context to use
1072 * @returns: a new #GSettings object
1074 * Creates a new #GSettings object with a given schema and context.
1076 * Creating settings objects with a context allow accessing settings
1077 * from a database other than the usual one. For example, it may make
1078 * sense to specify "defaults" in order to get a settings object that
1079 * modifies the system default settings instead of the settings for this
1082 * It is a programmer error to call this function for an unsupported
1083 * context. Use g_settings_supports_context() to determine if a context
1084 * is supported if you are unsure.
1089 g_settings_new_with_context (const gchar *schema,
1090 const gchar *context)
1092 return g_object_new (G_TYPE_SETTINGS,
1099 * g_settings_new_with_context_and_path:
1100 * @schema: the name of the schema
1101 * @path: the path to use
1102 * @returns: a new #GSettings object
1104 * Creates a new #GSettings object with a given schema, context and
1107 * This is a mix of g_settings_new_with_context() and
1108 * g_settings_new_with_path().
1113 g_settings_new_with_context_and_path (const gchar *schema,
1114 const gchar *context,
1117 return g_object_new (G_TYPE_SETTINGS,
1126 GSettings *settings;
1129 GSettingsBindGetMapping get_mapping;
1130 GSettingsBindSetMapping set_mapping;
1132 GDestroyNotify destroy;
1134 guint writable_handler_id;
1135 guint property_handler_id;
1136 const GParamSpec *property;
1137 guint key_handler_id;
1141 /* prevent recursion */
1146 g_settings_binding_free (gpointer data)
1148 GSettingsBinding *binding = data;
1150 g_assert (!binding->running);
1152 if (binding->writable_handler_id)
1153 g_signal_handler_disconnect (binding->settings,
1154 binding->writable_handler_id);
1156 if (binding->key_handler_id)
1157 g_signal_handler_disconnect (binding->settings,
1158 binding->key_handler_id);
1160 if (g_signal_handler_is_connected (binding->object,
1161 binding->property_handler_id))
1162 g_signal_handler_disconnect (binding->object,
1163 binding->property_handler_id);
1165 g_variant_type_free (binding->type);
1166 g_object_unref (binding->settings);
1168 if (binding->destroy)
1169 binding->destroy (binding->user_data);
1171 g_slice_free (GSettingsBinding, binding);
1175 g_settings_binding_quark (const char *property)
1180 tmp = g_strdup_printf ("gsettingsbinding-%s", property);
1181 quark = g_quark_from_string (tmp);
1188 g_settings_binding_key_changed (GSettings *settings,
1192 GSettingsBinding *binding = user_data;
1193 GValue value = { 0, };
1196 g_assert (settings == binding->settings);
1197 g_assert (key == binding->key);
1199 if (binding->running)
1202 binding->running = TRUE;
1204 g_value_init (&value, binding->property->value_type);
1205 variant = g_settings_get_value (settings, key);
1206 if (binding->get_mapping (&value, variant, binding->user_data))
1207 g_object_set_property (binding->object,
1208 binding->property->name,
1210 g_value_unset (&value);
1212 binding->running = FALSE;
1216 g_settings_binding_property_changed (GObject *object,
1217 const GParamSpec *pspec,
1220 GSettingsBinding *binding = user_data;
1221 GValue value = { 0, };
1224 g_assert (object == binding->object);
1225 g_assert (pspec == binding->property);
1227 if (binding->running)
1230 binding->running = TRUE;
1232 g_value_init (&value, pspec->value_type);
1233 g_object_get_property (object, pspec->name, &value);
1234 if ((variant = binding->set_mapping (&value, binding->type,
1235 binding->user_data)))
1237 g_settings_set_value (binding->settings,
1239 g_variant_ref_sink (variant));
1240 g_variant_unref (variant);
1242 g_value_unset (&value);
1244 binding->running = FALSE;
1249 * @settings: a #GSettings object
1250 * @key: the key to bind
1251 * @object: a #GObject
1252 * @property: the name of the property to bind
1253 * @flags: flags for the binding
1255 * Create a binding between the @key in the @settings object
1256 * and the property @property of @object.
1258 * The binding uses the default GIO mapping functions to map
1259 * between the settings and property values. These functions
1260 * handle booleans, numeric types and string types in a
1261 * straightforward way. Use g_settings_bind_with_mapping() if
1262 * you need a custom mapping, or map between types that are not
1263 * supported by the default mapping functions.
1265 * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
1266 * function also establishes a binding between the writability of
1267 * @key and the "sensitive" property of @object (if @object has
1268 * a boolean property by that name). See g_settings_bind_writable()
1269 * for more details about writable bindings.
1271 * Note that the lifecycle of the binding is tied to the object,
1272 * and that you can have only one binding per object property.
1273 * If you bind the same property twice on the same object, the second
1274 * binding overrides the first one.
1279 g_settings_bind (GSettings *settings,
1282 const gchar *property,
1283 GSettingsBindFlags flags)
1285 g_settings_bind_with_mapping (settings, key, object, property,
1286 flags, NULL, NULL, NULL, NULL);
1290 * g_settings_bind_with_mapping:
1291 * @settings: a #GSettings object
1292 * @key: the key to bind
1293 * @object: a #GObject
1294 * @property: the name of the property to bind
1295 * @flags: flags for the binding
1296 * @get_mapping: a function that gets called to convert values
1297 * from @settings to @object, or %NULL to use the default GIO mapping
1298 * @set_mapping: a function that gets called to convert values
1299 * from @object to @settings, or %NULL to use the default GIO mapping
1300 * @user_data: data that gets passed to @get_mapping and @set_mapping
1301 * @destroy: #GDestroyNotify function for @user_data
1303 * Create a binding between the @key in the @settings object
1304 * and the property @property of @object.
1306 * The binding uses the provided mapping functions to map between
1307 * settings and property values.
1309 * Note that the lifecycle of the binding is tied to the object,
1310 * and that you can have only one binding per object property.
1311 * If you bind the same property twice on the same object, the second
1312 * binding overrides the first one.
1317 g_settings_bind_with_mapping (GSettings *settings,
1320 const gchar *property,
1321 GSettingsBindFlags flags,
1322 GSettingsBindGetMapping get_mapping,
1323 GSettingsBindSetMapping set_mapping,
1325 GDestroyNotify destroy)
1327 GSettingsBinding *binding;
1328 GObjectClass *objectclass;
1329 gchar *detailed_signal;
1330 GQuark binding_quark;
1332 g_return_if_fail (G_IS_SETTINGS (settings));
1334 objectclass = G_OBJECT_GET_CLASS (object);
1336 binding = g_slice_new0 (GSettingsBinding);
1337 binding->settings = g_object_ref (settings);
1338 binding->object = object;
1339 binding->key = g_intern_string (key);
1340 binding->property = g_object_class_find_property (objectclass, property);
1341 binding->user_data = user_data;
1342 binding->destroy = destroy;
1343 binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
1344 binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
1346 if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
1347 flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
1349 if (binding->property == NULL)
1351 g_critical ("g_settings_bind: no property '%s' on class '%s'",
1352 property, G_OBJECT_TYPE_NAME (object));
1356 if ((flags & G_SETTINGS_BIND_GET) && (binding->property->flags & G_PARAM_WRITABLE) == 0)
1358 g_critical ("g_settings_bind: property '%s' on class '%s' is not writable",
1359 property, G_OBJECT_TYPE_NAME (object));
1362 if ((flags & G_SETTINGS_BIND_SET) && (binding->property->flags & G_PARAM_READABLE) == 0)
1364 g_critical ("g_settings_bind: property '%s' on class '%s' is not readable",
1365 property, G_OBJECT_TYPE_NAME (object));
1372 value = g_settings_schema_get_value (settings->priv->schema, key, NULL);
1373 binding->type = g_variant_type_copy (g_variant_get_type (value));
1375 g_variant_unref (value);
1378 if (binding->type == NULL)
1380 g_critical ("g_settings_bind: no key '%s' on schema '%s'",
1381 key, settings->priv->schema_name);
1385 if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
1386 (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
1387 !g_settings_mapping_is_compatible (binding->property->value_type,
1390 g_critical ("g_settings_bind: property '%s' on class '%s' has type"
1391 "'%s' which is not compatible with type '%s' of key '%s'"
1392 "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
1393 g_type_name (binding->property->value_type),
1394 g_variant_type_dup_string (binding->type), key,
1395 settings->priv->schema_name);
1399 if ((flags & G_SETTINGS_BIND_SET) &&
1400 (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
1402 GParamSpec *sensitive;
1404 sensitive = g_object_class_find_property (objectclass, "sensitive");
1406 if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
1407 (sensitive->flags & G_PARAM_WRITABLE))
1408 g_settings_bind_writable (settings, binding->key,
1409 object, "sensitive", FALSE);
1412 if (flags & G_SETTINGS_BIND_SET)
1414 detailed_signal = g_strdup_printf ("notify::%s", property);
1415 binding->property_handler_id =
1416 g_signal_connect (object, detailed_signal,
1417 G_CALLBACK (g_settings_binding_property_changed),
1419 g_free (detailed_signal);
1421 if (~flags & G_SETTINGS_BIND_GET)
1422 g_settings_binding_property_changed (object,
1427 if (flags & G_SETTINGS_BIND_GET)
1429 if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
1431 detailed_signal = g_strdup_printf ("changed::%s", key);
1432 binding->key_handler_id =
1433 g_signal_connect (settings, detailed_signal,
1434 G_CALLBACK (g_settings_binding_key_changed),
1436 g_free (detailed_signal);
1439 g_settings_binding_key_changed (settings, binding->key, binding);
1442 binding_quark = g_settings_binding_quark (property);
1443 g_object_set_qdata_full (object, binding_quark,
1444 binding, g_settings_binding_free);
1449 GSettings *settings;
1452 const gchar *property;
1455 } GSettingsWritableBinding;
1458 g_settings_writable_binding_free (gpointer data)
1460 GSettingsWritableBinding *binding = data;
1462 g_signal_handler_disconnect (binding->settings, binding->handler_id);
1463 g_object_unref (binding->settings);
1464 g_slice_free (GSettingsWritableBinding, binding);
1468 g_settings_binding_writable_changed (GSettings *settings,
1472 GSettingsWritableBinding *binding = user_data;
1475 g_assert (settings == binding->settings);
1476 g_assert (key == binding->key);
1478 writable = g_settings_is_writable (settings, key);
1480 if (binding->inverted)
1481 writable = !writable;
1483 g_object_set (binding->object, binding->property, writable, NULL);
1487 * g_settings_bind_writable:
1488 * @settings: a #GSettings object
1489 * @key: the key to bind
1490 * @object: a #GObject
1491 * @property: the name of a boolean property to bind
1492 * @inverted: whether to 'invert' the value
1494 * Create a binding between the writability of @key in the
1495 * @settings object and the property @property of @object.
1496 * The property must be boolean; "sensitive" or "visible"
1497 * properties of widgets are the most likely candidates.
1499 * Writable bindings are always uni-directional; changes of the
1500 * writability of the setting will be propagated to the object
1501 * property, not the other way.
1503 * When the @inverted argument is %TRUE, the binding inverts the
1504 * value as it passes from the setting to the object, i.e. @property
1505 * will be set to %TRUE if the key is <emphasis>not</emphasis>
1508 * Note that the lifecycle of the binding is tied to the object,
1509 * and that you can have only one binding per object property.
1510 * If you bind the same property twice on the same object, the second
1511 * binding overrides the first one.
1516 g_settings_bind_writable (GSettings *settings,
1519 const gchar *property,
1522 GSettingsWritableBinding *binding;
1523 gchar *detailed_signal;
1526 g_return_if_fail (G_IS_SETTINGS (settings));
1528 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
1531 g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
1532 property, G_OBJECT_TYPE_NAME (object));
1535 if ((pspec->flags & G_PARAM_WRITABLE) == 0)
1537 g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
1538 property, G_OBJECT_TYPE_NAME (object));
1542 binding = g_slice_new (GSettingsWritableBinding);
1543 binding->settings = g_object_ref (settings);
1544 binding->object = object;
1545 binding->key = g_intern_string (key);
1546 binding->property = g_intern_string (property);
1547 binding->inverted = inverted;
1549 detailed_signal = g_strdup_printf ("writable-changed::%s", key);
1550 binding->handler_id =
1551 g_signal_connect (settings, detailed_signal,
1552 G_CALLBACK (g_settings_binding_writable_changed),
1554 g_free (detailed_signal);
1556 g_object_set_qdata_full (object, g_settings_binding_quark (property),
1557 binding, g_settings_writable_binding_free);
1559 g_settings_binding_writable_changed (settings, binding->key, binding);
1563 * g_settings_unbind:
1564 * @object: the object
1565 * @property: the property whose binding is removed
1567 * Removes an existing binding for @property on @object.
1569 * Note that bindings are automatically removed when the
1570 * object is finalized, so it is rarely necessary to call this
1576 g_settings_unbind (gpointer object,
1577 const gchar *property)
1579 GQuark binding_quark;
1581 binding_quark = g_settings_binding_quark (property);
1582 g_object_set_qdata (object, binding_quark, NULL);
1586 * g_settings_get_string:
1587 * @settings: a #GSettings object
1588 * @key: the key to get the value for
1589 * @returns: a newly-allocated string
1591 * Gets the value that is stored at @key in @settings.
1593 * A convenience variant of g_settings_get() for strings.
1595 * It is a programmer error to pass a @key that isn't valid for
1596 * @settings or is not of type string.
1601 g_settings_get_string (GSettings *settings,
1607 value = g_settings_get_value (settings, key);
1608 result = g_variant_dup_string (value, NULL);
1609 g_variant_unref (value);
1615 * g_settings_set_string:
1616 * @settings: a #GSettings object
1617 * @key: the name of the key to set
1618 * @value: the value to set it to
1619 * @returns: %TRUE if setting the key succeeded,
1620 * %FALSE if the key was not writable
1622 * Sets @key in @settings to @value.
1624 * A convenience variant of g_settings_set() for strings.
1626 * It is a programmer error to pass a @key that isn't valid for
1627 * @settings or is not of type string.
1632 g_settings_set_string (GSettings *settings,
1636 return g_settings_set_value (settings, key, g_variant_new_string (value));
1640 * g_settings_get_int:
1641 * @settings: a #GSettings object
1642 * @key: the key to get the value for
1643 * @returns: an integer
1645 * Gets the value that is stored at @key in @settings.
1647 * A convenience variant of g_settings_get() for 32-bit integers.
1649 * It is a programmer error to pass a @key that isn't valid for
1650 * @settings or is not of type int32.
1655 g_settings_get_int (GSettings *settings,
1661 value = g_settings_get_value (settings, key);
1662 result = g_variant_get_int32 (value);
1663 g_variant_unref (value);
1669 * g_settings_set_int:
1670 * @settings: a #GSettings object
1671 * @key: the name of the key to set
1672 * @value: the value to set it to
1673 * @returns: %TRUE if setting the key succeeded,
1674 * %FALSE if the key was not writable
1676 * Sets @key in @settings to @value.
1678 * A convenience variant of g_settings_set() for 32-bit integers.
1680 * It is a programmer error to pass a @key that isn't valid for
1681 * @settings or is not of type int32.
1686 g_settings_set_int (GSettings *settings,
1690 return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1694 * g_settings_get_double:
1695 * @settings: a #GSettings object
1696 * @key: the key to get the value for
1697 * @returns: a double
1699 * Gets the value that is stored at @key in @settings.
1701 * A convenience variant of g_settings_get() for doubles.
1703 * It is a programmer error to pass a @key that isn't valid for
1704 * @settings or is not of type double.
1709 g_settings_get_double (GSettings *settings,
1715 value = g_settings_get_value (settings, key);
1716 result = g_variant_get_double (value);
1717 g_variant_unref (value);
1723 * g_settings_set_double:
1724 * @settings: a #GSettings object
1725 * @key: the name of the key to set
1726 * @value: the value to set it to
1727 * @returns: %TRUE if setting the key succeeded,
1728 * %FALSE if the key was not writable
1730 * Sets @key in @settings to @value.
1732 * A convenience variant of g_settings_set() for doubles.
1734 * It is a programmer error to pass a @key that isn't valid for
1735 * @settings or is not of type double.
1740 g_settings_set_double (GSettings *settings,
1744 return g_settings_set_value (settings, key, g_variant_new_double (value));
1748 * g_settings_get_boolean:
1749 * @settings: a #GSettings object
1750 * @key: the key to get the value for
1751 * @returns: a boolean
1753 * Gets the value that is stored at @key in @settings.
1755 * A convenience variant of g_settings_get() for booleans.
1757 * It is a programmer error to pass a @key that isn't valid for
1758 * @settings or is not of type boolean.
1763 g_settings_get_boolean (GSettings *settings,
1769 value = g_settings_get_value (settings, key);
1770 result = g_variant_get_boolean (value);
1771 g_variant_unref (value);
1777 * g_settings_set_boolean:
1778 * @settings: a #GSettings object
1779 * @key: the name of the key to set
1780 * @value: the value to set it to
1781 * @returns: %TRUE if setting the key succeeded,
1782 * %FALSE if the key was not writable
1784 * Sets @key in @settings to @value.
1786 * A convenience variant of g_settings_set() for booleans.
1788 * It is a programmer error to pass a @key that isn't valid for
1789 * @settings or is not of type boolean.
1794 g_settings_set_boolean (GSettings *settings,
1798 return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1802 * g_settings_get_strv:
1803 * @settings: a #GSettings object
1804 * @key: the key to get the value for
1805 * @returns: a newly-allocated, %NULL-terminated array of strings
1807 * Gets the value that is stored at @key in @settings.
1809 * A convenience variant of g_settings_get() for string arrays.
1811 * It is a programmer error to pass a @key that isn't valid for
1812 * @settings or is not of type 'string array'.
1817 g_settings_get_strv (GSettings *settings,
1823 value = g_settings_get_value (settings, key);
1824 result = g_variant_dup_strv (value, NULL);
1825 g_variant_unref (value);
1831 * g_settings_set_strv:
1832 * @settings: a #GSettings object
1833 * @key: the name of the key to set
1834 * @value: (allow-none): the value to set it to, or %NULL
1835 * @returns: %TRUE if setting the key succeeded,
1836 * %FALSE if the key was not writable
1838 * Sets @key in @settings to @value.
1840 * A convenience variant of g_settings_set() for string arrays. If
1841 * @value is %NULL, then @key is set to be the empty array.
1843 * It is a programmer error to pass a @key that isn't valid for
1844 * @settings or is not of type 'string array'.
1849 g_settings_set_strv (GSettings *settings,
1851 const gchar * const *value)
1856 array = g_variant_new_strv (value, -1);
1858 array = g_variant_new_strv (NULL, 0);
1860 return g_settings_set_value (settings, key, array);
1863 #define __G_SETTINGS_C__
1864 #include "gioaliasdef.c"