GSettings: support emitting signals in threads
[platform/upstream/glib.git] / gio / gsettings.c
1 /*
2  * Copyright © 2009, 2010 Codethink Limited
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Author: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23 #include <glib.h>
24 #include <glibintl.h>
25 #include <locale.h>
26
27 #include "gsettings.h"
28
29 #include "gdelayedsettingsbackend.h"
30 #include "gsettingsbackendinternal.h"
31 #include "gsettings-mapping.h"
32 #include "gio-marshal.h"
33 #include "gsettingsschema.h"
34
35 #include <string.h>
36
37 #include "gioalias.h"
38
39 /**
40  * SECTION:gsettings
41  * @short_description: a high-level API for application settings
42  *
43  * The #GSettings class provides a convenient API for storing and retrieving
44  * application settings.
45  *
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.
49  *
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.
55  *
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.
63  *
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.
71  *
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:
76  * |[<![CDATA[
77  * <!ELEMENT schemalist (schema*) >
78  * <!ATTLIST schemalist gettext-domain #IMPLIED >
79  *
80  * <!ELEMENT schema (key|child)* >
81  * <!ATTLIST schema id             CDATA #REQUIRED
82  *                  path           CDATA #IMPLIED
83  *                  gettext-domain CDATA #IMPLIED >
84  *
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 >
90  *
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 >
99  *
100  * <!ELEMENT summary (#PCDATA) >
101  * <!ELEMENT description (#PCDATA) >
102  *
103  * <!ELEMENT range EMPTY >
104  * <!ATTLIST range min CDATA #REQUIRED
105  *                 max CDATA #REQUIRED >
106  *
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 >
113  *
114  * <!ELEMENT child EMPTY >
115  * <!ATTLIST child name  CDATA #REQUIRED
116  *                 schema CDATA #REQUIRED >
117  * ]]>
118  * ]|
119  *
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".
125  *
126  * <refsect2>
127  *  <title>Binding</title>
128  *   <para>
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
134  *    cycles.
135  *   </para>
136  *   <para>
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.
143  *   </para>
144  *  </refsect2>
145  */
146
147 struct _GSettingsPrivate
148 {
149   /* where the signals go... */
150   GMainContext *main_context;
151
152   GSettingsBackend *backend;
153   GSettingsSchema *schema;
154   gchar *schema_name;
155   gchar *context;
156   gchar *path;
157
158   GDelayedSettingsBackend *delayed;
159 };
160
161 enum
162 {
163   PROP_0,
164   PROP_BACKEND,
165   PROP_SCHEMA,
166   PROP_CONTEXT,
167   PROP_PATH,
168   PROP_HAS_UNAPPLIED,
169 };
170
171 enum
172 {
173   SIGNAL_WRITABLE_CHANGE_EVENT,
174   SIGNAL_WRITABLE_CHANGED,
175   SIGNAL_CHANGE_EVENT,
176   SIGNAL_CHANGED,
177   N_SIGNALS
178 };
179
180 static guint g_settings_signals[N_SIGNALS];
181
182 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
183
184 static gboolean
185 g_settings_real_change_event (GSettings    *settings,
186                               const GQuark *keys,
187                               gint          n_keys)
188 {
189   gint i;
190
191   if (keys == NULL)
192     keys = g_settings_schema_list (settings->priv->schema, &n_keys);
193
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]));
197
198   return FALSE;
199 }
200
201 static gboolean
202 g_settings_real_writable_change_event (GSettings *settings,
203                                        GQuark     key)
204 {
205   const GQuark *keys = &key;
206   gint n_keys = 1;
207   gint i;
208
209   if (key == 0)
210     keys = g_settings_schema_list (settings->priv->schema, &n_keys);
211
212   for (i = 0; i < n_keys; i++)
213     {
214       const gchar *string = g_quark_to_string (keys[i]);
215
216       g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
217                      keys[i], string);
218       g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
219                      keys[i], string);
220     }
221
222   return FALSE;
223 }
224
225 static void
226 settings_backend_changed (GSettingsBackend    *backend,
227                           const gchar         *key,
228                           gpointer             origin_tag,
229                           gpointer             user_data)
230 {
231   GSettings *settings = G_SETTINGS (user_data);
232   gboolean ignore_this;
233   gint i;
234
235   g_assert (settings->priv->backend == backend);
236
237   for (i = 0; key[i] == settings->priv->path[i]; i++);
238
239   if (settings->priv->path[i] == '\0' &&
240       g_settings_schema_has_key (settings->priv->schema, key + i))
241     {
242       GQuark quark;
243
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);
247     }
248 }
249
250 static void
251 settings_backend_path_changed (GSettingsBackend *backend,
252                                const gchar      *path,
253                                gpointer          origin_tag,
254                                gpointer          user_data)
255 {
256   GSettings *settings = G_SETTINGS (user_data);
257   gboolean ignore_this;
258
259   g_assert (settings->priv->backend == backend);
260
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);
264 }
265
266 static void
267 settings_backend_keys_changed (GSettingsBackend    *backend,
268                                const gchar         *path,
269                                const gchar * const *items,
270                                gpointer             origin_tag,
271                                gpointer             user_data)
272 {
273   GSettings *settings = G_SETTINGS (user_data);
274   gboolean ignore_this;
275   gint i;
276
277   g_assert (settings->priv->backend == backend);
278
279   for (i = 0; settings->priv->path[i] &&
280               settings->priv->path[i] == path[i]; i++);
281
282   if (path[i] == '\0')
283     {
284       GQuark quarks[256];
285       gint j, l = 0;
286
287       for (j = 0; items[j]; j++)
288          {
289            const gchar *item = items[j];
290            gint k;
291
292            for (k = 0; item[k] == settings->priv->path[i + k]; k++);
293
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);
297
298            /* "256 quarks ought to be enough for anybody!"
299             * If this bites you, I'm sorry.  Please file a bug.
300             */
301            g_assert (l < 256);
302          }
303
304       if (l > 0)
305         g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
306                        0, quarks, l, &ignore_this);
307     }
308 }
309
310 static void
311 settings_backend_writable_changed (GSettingsBackend *backend,
312                                    const gchar      *key,
313                                    gpointer          user_data)
314 {
315   GSettings *settings = G_SETTINGS (user_data);
316   gboolean ignore_this;
317   gint i;
318
319   g_assert (settings->priv->backend == backend);
320
321   for (i = 0; key[i] == settings->priv->path[i]; i++);
322
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);
327 }
328
329 static void
330 settings_backend_path_writable_changed (GSettingsBackend *backend,
331                                         const gchar      *path,
332                                         gpointer          user_data)
333 {
334   GSettings *settings = G_SETTINGS (user_data);
335   gboolean ignore_this;
336
337   g_assert (settings->priv->backend == backend);
338
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);
342 }
343
344 static void
345 g_settings_constructed (GObject *object)
346 {
347   GSettings *settings = G_SETTINGS (object);
348   const gchar *schema_path;
349
350   settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
351   schema_path = g_settings_schema_get_path (settings->priv->schema);
352
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);
357
358   if (settings->priv->path == NULL)
359     {
360       if (schema_path == NULL)
361         g_error ("attempting to create schema '%s' without a path",
362                  settings->priv->schema_name);
363
364       settings->priv->path = g_strdup (schema_path);
365     }
366
367   settings->priv->backend = g_settings_backend_get_with_context (settings->priv->context);
368   g_settings_backend_watch (settings->priv->backend,
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                             settings);
376   g_settings_backend_subscribe (settings->priv->backend,
377                                 settings->priv->path);
378 }
379
380 static void
381 g_settings_init (GSettings *settings)
382 {
383   settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
384                                                 G_TYPE_SETTINGS,
385                                                 GSettingsPrivate);
386
387   settings->priv->main_context = g_main_context_get_thread_default ();
388
389   if (settings->priv->main_context == NULL)
390     settings->priv->main_context = g_main_context_default ();
391
392   g_main_context_ref (settings->priv->main_context);
393 }
394
395 /**
396  * g_settings_delay:
397  * @settings: a #GSettings object
398  *
399  * Changes the #GSettings object into 'delay-apply' mode. In this
400  * mode, changes to @settings are not immediately propagated to the
401  * backend, but kept locally until g_settings_apply() is called.
402  *
403  * Since: 2.26
404  */
405 void
406 g_settings_delay (GSettings *settings)
407 {
408   g_return_if_fail (G_IS_SETTINGS (settings));
409
410   if (settings->priv->delayed)
411     return;
412
413   settings->priv->delayed =
414     g_delayed_settings_backend_new (settings->priv->backend, settings);
415   g_settings_backend_unwatch (settings->priv->backend, settings);
416   g_object_unref (settings->priv->backend);
417
418   settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
419   g_settings_backend_watch (settings->priv->backend,
420                             settings->priv->main_context,
421                             settings_backend_changed,
422                             settings_backend_path_changed,
423                             settings_backend_keys_changed,
424                             settings_backend_writable_changed,
425                             settings_backend_path_writable_changed,
426                             settings);
427 }
428
429 /**
430  * g_settings_apply:
431  * @settings: a #GSettings instance
432  *
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.
437  **/
438 void
439 g_settings_apply (GSettings *settings)
440 {
441   if (settings->priv->delayed)
442     {
443       GDelayedSettingsBackend *delayed;
444
445       delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
446       g_delayed_settings_backend_apply (delayed);
447     }
448 }
449
450 /**
451  * g_settings_revert:
452  * @settings: a #GSettings instance
453  *
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.
458  *
459  * Change notifications will be emitted for affected keys.
460  **/
461 void
462 g_settings_revert (GSettings *settings)
463 {
464   if (settings->priv->delayed)
465     {
466       GDelayedSettingsBackend *delayed;
467
468       delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
469       g_delayed_settings_backend_revert (delayed);
470     }
471 }
472
473 static void
474 g_settings_set_property (GObject      *object,
475                          guint         prop_id,
476                          const GValue *value,
477                          GParamSpec   *pspec)
478 {
479   GSettings *settings = G_SETTINGS (object);
480
481   switch (prop_id)
482     {
483     case PROP_SCHEMA:
484       g_assert (settings->priv->schema_name == NULL);
485       settings->priv->schema_name = g_value_dup_string (value);
486       break;
487
488     case PROP_PATH:
489       settings->priv->path = g_value_dup_string (value);
490       break;
491
492     case PROP_CONTEXT:
493       settings->priv->context = g_value_dup_string (value);
494       break;
495
496     default:
497       g_assert_not_reached ();
498     }
499 }
500
501 /**
502  * g_settings_get_has_unapplied:
503  * @settings: a #GSettings object
504  * @returns: %TRUE if @settings has unapplied changes
505  *
506  * Returns whether the #GSettings object has any unapplied
507  * changes.  This can only be the case if it is in 'delayed-apply' mode.
508  *
509  * Since: 2.26
510  */
511 gboolean
512 g_settings_get_has_unapplied (GSettings *settings)
513 {
514   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
515
516   return settings->priv->delayed &&
517          g_delayed_settings_backend_get_has_unapplied (
518            G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
519 }
520
521 static void
522 g_settings_get_property (GObject    *object,
523                          guint       prop_id,
524                          GValue     *value,
525                          GParamSpec *pspec)
526 {
527   GSettings *settings = G_SETTINGS (object);
528
529   switch (prop_id)
530     {
531      case PROP_SCHEMA:
532       g_value_set_object (value, settings->priv->schema);
533       break;
534
535      case PROP_HAS_UNAPPLIED:
536       g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
537       break;
538
539      default:
540       g_assert_not_reached ();
541     }
542 }
543
544 static void
545 g_settings_finalize (GObject *object)
546 {
547   GSettings *settings = G_SETTINGS (object);
548
549   g_settings_backend_unwatch (settings->priv->backend, settings);
550   g_settings_backend_unsubscribe (settings->priv->backend,
551                                   settings->priv->path);
552   g_main_context_unref (settings->priv->main_context);
553   g_object_unref (settings->priv->backend);
554   g_object_unref (settings->priv->schema);
555   g_free (settings->priv->schema_name);
556   g_free (settings->priv->path);
557 }
558
559 static void
560 g_settings_class_init (GSettingsClass *class)
561 {
562   GObjectClass *object_class = G_OBJECT_CLASS (class);
563
564   class->writable_change_event = g_settings_real_writable_change_event;
565   class->change_event = g_settings_real_change_event;
566
567   object_class->set_property = g_settings_set_property;
568   object_class->get_property = g_settings_get_property;
569   object_class->constructed = g_settings_constructed;
570   object_class->finalize = g_settings_finalize;
571
572   g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
573
574   /**
575    * GSettings::changed:
576    * @settings: the object on which the signal was emitted
577    * @key: the name of the key that changed
578    *
579    * The "changed" signal is emitted when a key has potentially changed.
580    * You should call one of the g_settings_get() calls to check the new
581    * value.
582    *
583    * This signal supports detailed connections.  You can connect to the
584    * detailed signal "changed::x" in order to only receive callbacks
585    * when key "x" changes.
586    */
587   g_settings_signals[SIGNAL_CHANGED] =
588     g_signal_new ("changed", G_TYPE_SETTINGS,
589                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
590                   G_STRUCT_OFFSET (GSettingsClass, changed),
591                   NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
592                   1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
593
594   /**
595    * GSettings::change-event:
596    * @settings: the object on which the signal was emitted
597    * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
598    * @n_keys: the length of the @keys array, or 0
599    * @returns: %TRUE to stop other handlers from being invoked for the
600    *           event. FALSE to propagate the event further.
601    *
602    * The "change-event" signal is emitted once per change event that
603    * affects this settings object.  You should connect to this signal
604    * only if you are interested in viewing groups of changes before they
605    * are split out into multiple emissions of the "changed" signal.
606    * For most use cases it is more appropriate to use the "changed" signal.
607    *
608    * In the event that the change event applies to one or more specified
609    * keys, @keys will be an array of #GQuark of length @n_keys.  In the
610    * event that the change event applies to the #GSettings object as a
611    * whole (ie: potentially every key has been changed) then @keys will
612    * be %NULL and @n_keys will be 0.
613    *
614    * The default handler for this signal invokes the "changed" signal
615    * for each affected key.  If any other connected handler returns
616    * %TRUE then this default functionality will be supressed.
617    */
618   g_settings_signals[SIGNAL_CHANGE_EVENT] =
619     g_signal_new ("change-event", G_TYPE_SETTINGS,
620                   G_SIGNAL_RUN_LAST,
621                   G_STRUCT_OFFSET (GSettingsClass, change_event),
622                   g_signal_accumulator_true_handled, NULL,
623                   _gio_marshal_BOOL__POINTER_INT,
624                   G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
625
626   /**
627    * GSettings::writable-changed:
628    * @settings: the object on which the signal was emitted
629    * @key: the key
630    *
631    * The "writable-changed" signal is emitted when the writability of a
632    * key has potentially changed.  You should call
633    * g_settings_is_writable() in order to determine the new status.
634    *
635    * This signal supports detailed connections.  You can connect to the
636    * detailed signal "writable-changed::x" in order to only receive
637    * callbacks when the writability of "x" changes.
638    */
639   g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
640     g_signal_new ("writable-changed", G_TYPE_SETTINGS,
641                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
642                   G_STRUCT_OFFSET (GSettingsClass, changed),
643                   NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
644                   1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
645
646   /**
647    * GSettings::writable-change-event:
648    * @settings: the object on which the signal was emitted
649    * @key: the quark of the key, or 0
650    * @returns: %TRUE to stop other handlers from being invoked for the
651    *           event. FALSE to propagate the event further.
652    *
653    * The "writable-change-event" signal is emitted once per writability
654    * change event that affects this settings object.  You should connect
655    * to this signal if you are interested in viewing groups of changes
656    * before they are split out into multiple emissions of the
657    * "writable-changed" signal.  For most use cases it is more
658    * appropriate to use the "writable-changed" signal.
659    *
660    * In the event that the writability change applies only to a single
661    * key, @key will be set to the #GQuark for that key.  In the event
662    * that the writability change affects the entire settings object,
663    * @key will be 0.
664    *
665    * The default handler for this signal invokes the "writable-changed"
666    * and "changed" signals for each affected key.  This is done because
667    * changes in writability might also imply changes in value (if for
668    * example, a new mandatory setting is introduced).  If any other
669    * connected handler returns %TRUE then this default functionality
670    * will be supressed.
671    */
672   g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
673     g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
674                   G_SIGNAL_RUN_LAST,
675                   G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
676                   g_signal_accumulator_true_handled, NULL,
677                   _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
678
679   /**
680    * GSettings:context:
681    *
682    * The name of the context that the settings are stored in.
683    */
684   g_object_class_install_property (object_class, PROP_CONTEXT,
685     g_param_spec_string ("context",
686                          P_("Context name"),
687                          P_("The name of the context for this settings object"),
688                          "", G_PARAM_CONSTRUCT_ONLY |
689                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
690
691   /**
692    * GSettings:schema:
693    *
694    * The name of the schema that describes the types of keys
695    * for this #GSettings object.
696    */
697   g_object_class_install_property (object_class, PROP_SCHEMA,
698     g_param_spec_string ("schema",
699                          P_("Schema name"),
700                          P_("The name of the schema for this settings object"),
701                          NULL,
702                          G_PARAM_CONSTRUCT_ONLY |
703                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
704
705    /**
706     * GSettings:path:
707     *
708     * The path within the backend where the settings are stored.
709     */
710    g_object_class_install_property (object_class, PROP_PATH,
711      g_param_spec_string ("path",
712                           P_("Base path"),
713                           P_("The path within the backend where the settings are"),
714                           NULL,
715                           G_PARAM_CONSTRUCT_ONLY |
716                           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
717
718    /**
719     * GSettings:has-unapplied:
720     *
721     * If this property is %TRUE, the #GSettings object has outstanding
722     * changes that will be applied when g_settings_apply() is called.
723     */
724    g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
725      g_param_spec_boolean ("has-unapplied",
726                            P_("Has unapplied changes"),
727                            P_("TRUE if there are outstanding changes to apply()"),
728                            FALSE,
729                            G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
730
731 }
732
733 /**
734  * g_settings_get_value:
735  * @settings: a #GSettings object
736  * @key: the key to get the value for
737  * @returns: a new #GVariant
738  *
739  * Gets the value that is stored in @settings for @key.
740  *
741  * It is a programmer error to give a @key that isn't valid for
742  * @settings.
743  *
744  * Since: 2.26
745  */
746 GVariant *
747 g_settings_get_value (GSettings   *settings,
748                       const gchar *key)
749 {
750   const gchar *unparsed = NULL;
751   GVariant *value, *options;
752   const GVariantType *type;
753   gchar lc_char = '\0';
754   GVariant *sval;
755   gchar *path;
756
757   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
758
759   sval = g_settings_schema_get_value (settings->priv->schema, key, &options);
760
761   if G_UNLIKELY (sval == NULL)
762     g_error ("schema '%s' does not contain a key named '%s'",
763              settings->priv->schema_name, key);
764
765   path = g_strconcat (settings->priv->path, key, NULL);
766   type = g_variant_get_type (sval);
767   value = g_settings_backend_read (settings->priv->backend, path, type, FALSE);
768   g_free (path);
769
770   if (options != NULL)
771     {
772       GVariantIter iter;
773       const gchar *option;
774       GVariant *option_value;
775
776       g_variant_iter_init (&iter, options);
777       while (g_variant_iter_loop (&iter, "{&sv}", &option, &option_value))
778         {
779           if (strcmp (option, "l10n") == 0)
780             g_variant_get (option_value, "(y&s)", &lc_char, &unparsed);
781           else
782             g_warning ("unknown schema extension '%s'", option);
783         }
784     }
785
786   if (value && !g_variant_is_of_type (value, type))
787     {
788       g_variant_unref (value);
789       value = NULL;
790     }
791
792   if (value == NULL && lc_char != '\0')
793   /* we will need to translate the schema default value */
794     {
795       const gchar *translated;
796       GError *error = NULL;
797       const gchar *domain;
798       gint lc_category;
799
800       domain = g_settings_schema_get_gettext_domain (settings->priv->schema);
801
802       if (lc_char == 't')
803         lc_category = LC_TIME;
804       else
805         lc_category = LC_MESSAGES;
806
807       translated = dcgettext (domain, unparsed, lc_category);
808
809       if (translated != unparsed)
810         /* it was translated, so we need to re-parse it */
811         {
812           value = g_variant_parse (g_variant_get_type (sval),
813                                    translated, NULL, NULL, &error);
814
815           if (value == NULL)
816             {
817               g_warning ("Failed to parse translated string `%s' for "
818                          "key `%s' in schema `%s': %s", unparsed, key,
819                          settings->priv->schema_name, error->message);
820               g_warning ("Using untranslated default instead.");
821               g_error_free (error);
822             }
823         }
824     }
825
826   if (value == NULL)
827     /* either translation failed or there was none to do.
828      * use the pre-compiled default.
829      */
830     value = g_variant_ref (sval);
831
832   g_variant_unref (sval);
833
834   return value;
835 }
836
837 /**
838  * g_settings_set_value:
839  * @settings: a #GSettings object
840  * @key: the name of the key to set
841  * @value: a #GVariant of the correct type
842  * @returns: %TRUE if setting the key succeeded,
843  *     %FALSE if the key was not writable
844  *
845  * Sets @key in @settings to @value.
846  *
847  * It is a programmer error to give a @key that isn't valid for
848  * @settings.  It is a programmer error to give a @value of the
849  * incorrect type.
850  *
851  * If @value is floating then this function consumes the reference.
852  *
853  * Since: 2.26
854  **/
855 gboolean
856 g_settings_set_value (GSettings   *settings,
857                       const gchar *key,
858                       GVariant    *value)
859 {
860   gboolean correct_type;
861   gboolean result;
862   GVariant *sval;
863   gchar *path;
864
865   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
866
867   sval = g_settings_schema_get_value (settings->priv->schema, key, NULL);
868   correct_type = g_variant_is_of_type (value, g_variant_get_type (sval));
869   g_variant_unref (sval);
870
871   g_return_val_if_fail (correct_type, FALSE);
872
873   path = g_strconcat (settings->priv->path, key, NULL);
874   result = g_settings_backend_write (settings->priv->backend,
875                                      path, value, NULL);
876   g_free (path);
877
878   return result;
879 }
880
881 /**
882  * g_settings_get:
883  * @settings: a #GSettings object
884  * @key: the key to get the value for
885  * @format: a #GVariant format string
886  * @...: arguments as per @format
887  *
888  * Gets the value that is stored at @key in @settings.
889  *
890  * A convenience function that combines g_settings_get_value() with
891  * g_variant_get().
892  *
893  * It is a programmer error to pass a @key that isn't valid for
894  * @settings or a @format_string that doesn't match the type of @key according
895  * to the schema of @settings.
896  *
897  * Since: 2.26
898  */
899 void
900 g_settings_get (GSettings   *settings,
901                 const gchar *key,
902                 const gchar *format,
903                 ...)
904 {
905   GVariant *value;
906   va_list ap;
907
908   value = g_settings_get_value (settings, key);
909
910   va_start (ap, format);
911   g_variant_get_va (value, format, NULL, &ap);
912   va_end (ap);
913
914   g_variant_unref (value);
915 }
916
917 /**
918  * g_settings_set:
919  * @settings: a #GSettings object
920  * @key: the name of the key to set
921  * @format: a #GVariant format string
922  * @...: arguments as per @format
923  * @returns: %TRUE if setting the key succeeded,
924  *     %FALSE if the key was not writable
925  *
926  * Sets @key in @settings to @value.
927  *
928  * A convenience function that combines g_settings_set_value() with
929  * g_variant_new().
930  *
931  * It is a programmer error to pass a @key that isn't valid for
932  * @settings or a @format that doesn't match the type of @key according
933  * to the schema of @settings.
934  *
935  * Since: 2.26
936  */
937 gboolean
938 g_settings_set (GSettings   *settings,
939                 const gchar *key,
940                 const gchar *format,
941                 ...)
942 {
943   GVariant *value;
944   va_list ap;
945
946   va_start (ap, format);
947   value = g_variant_new_va (format, NULL, &ap);
948   va_end (ap);
949
950   return g_settings_set_value (settings, key, value);
951 }
952
953 /**
954  * g_settings_is_writable:
955  * @settings: a #GSettings object
956  * @name: the name of a key
957  * @returns: %TRUE if the key @name is writable
958  *
959  * Finds out if a key can be written or not
960  *
961  * Since: 2.26
962  */
963 gboolean
964 g_settings_is_writable (GSettings   *settings,
965                         const gchar *name)
966 {
967   gboolean writable;
968   gchar *path;
969
970   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
971
972   path = g_strconcat (settings->priv->path, name, NULL);
973   writable = g_settings_backend_get_writable (settings->priv->backend, path);
974   g_free (path);
975
976   return writable;
977 }
978
979 /**
980  * g_settings_get_child:
981  * @settings: a #GSettings object
982  * @name: the name of the 'child' schema
983  * @returns: a 'child' settings object
984  *
985  * Creates a 'child' settings object which has a base path of
986  * <replaceable>base-path</replaceable>/@name", where
987  * <replaceable>base-path</replaceable> is the base path of @settings.
988  *
989  * The schema for the child settings object must have been declared
990  * in the schema of @settings using a <tag class="starttag">child</tag> element.
991  *
992  * Since: 2.26
993  */
994 GSettings *
995 g_settings_get_child (GSettings   *settings,
996                       const gchar *name)
997 {
998   GVariant *child_schema;
999   gchar *child_path;
1000   gchar *child_name;
1001   GSettings *child;
1002
1003   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1004
1005   child_name = g_strconcat (name, "/", NULL);
1006   child_schema = g_settings_schema_get_value (settings->priv->schema,
1007                                               child_name, NULL);
1008   if (child_schema == NULL ||
1009       !g_variant_is_of_type (child_schema, G_VARIANT_TYPE_STRING))
1010     g_error ("Schema '%s' has no child '%s'",
1011              settings->priv->schema_name, name);
1012
1013   child_path = g_strconcat (settings->priv->path, child_name, NULL);
1014   child = g_object_new (G_TYPE_SETTINGS,
1015                         "schema", g_variant_get_string (child_schema, NULL),
1016                         "path", child_path,
1017                         NULL);
1018   g_variant_unref (child_schema);
1019   g_free (child_path);
1020   g_free (child_name);
1021
1022   return child;
1023 }
1024
1025 /**
1026  * g_settings_new:
1027  * @schema: the name of the schema
1028  * @returns: a new #GSettings object
1029  *
1030  * Creates a new #GSettings object with a given schema.
1031  *
1032  * Signals on the newly created #GSettings object will be dispatched
1033  * via the thread-default #GMainContext in effect at the time of the
1034  * call to g_settings_new().  The new #GSettings will hold a reference
1035  * on the context.  See g_main_context_push_thread_default().
1036  *
1037  * Since: 2.26
1038  */
1039 GSettings *
1040 g_settings_new (const gchar *schema)
1041 {
1042   return g_object_new (G_TYPE_SETTINGS,
1043                        "schema", schema,
1044                        NULL);
1045 }
1046
1047 /**
1048  * g_settings_new_with_path:
1049  * @schema: the name of the schema
1050  * @path: the path to use
1051  * @returns: a new #GSettings object
1052  *
1053  * Creates a new #GSettings object with a given schema and path.
1054  *
1055  * You only need to do this if you want to directly create a settings
1056  * object with a schema that doesn't have a specified path of its own.
1057  * That's quite rare.
1058  *
1059  * It is a programmer error to call this function for a schema that
1060  * has an explicitly specified path.
1061  *
1062  * Since: 2.26
1063  */
1064 GSettings *
1065 g_settings_new_with_path (const gchar *schema,
1066                           const gchar *path)
1067 {
1068   return g_object_new (G_TYPE_SETTINGS,
1069                        "schema", schema,
1070                        "path", path,
1071                        NULL);
1072 }
1073
1074 /**
1075  * g_settings_new_with_context:
1076  * @schema: the name of the schema
1077  * @context: the context to use
1078  * @returns: a new #GSettings object
1079  *
1080  * Creates a new #GSettings object with a given schema and context.
1081  *
1082  * Creating settings objects with a context allow accessing settings
1083  * from a database other than the usual one.  For example, it may make
1084  * sense to specify "defaults" in order to get a settings object that
1085  * modifies the system default settings instead of the settings for this
1086  * user.
1087  *
1088  * It is a programmer error to call this function for an unsupported
1089  * context.  Use g_settings_supports_context() to determine if a context
1090  * is supported if you are unsure.
1091  *
1092  * Since: 2.26
1093  */
1094 GSettings *
1095 g_settings_new_with_context (const gchar *schema,
1096                              const gchar *context)
1097 {
1098   return g_object_new (G_TYPE_SETTINGS,
1099                        "schema", schema,
1100                        "context", context,
1101                        NULL);
1102 }
1103
1104 /**
1105  * g_settings_new_with_context_and_path:
1106  * @schema: the name of the schema
1107  * @path: the path to use
1108  * @returns: a new #GSettings object
1109  *
1110  * Creates a new #GSettings object with a given schema, context and
1111  * path.
1112  *
1113  * This is a mix of g_settings_new_with_context() and
1114  * g_settings_new_with_path().
1115  *
1116  * Since: 2.26
1117  */
1118 GSettings *
1119 g_settings_new_with_context_and_path (const gchar *schema,
1120                                       const gchar *context,
1121                                       const gchar *path)
1122 {
1123   return g_object_new (G_TYPE_SETTINGS,
1124                        "schema", schema,
1125                         "context", context,
1126                         "path", path,
1127                         NULL);
1128 }
1129
1130 typedef struct
1131 {
1132   GSettings *settings;
1133   GObject *object;
1134
1135   GSettingsBindGetMapping get_mapping;
1136   GSettingsBindSetMapping set_mapping;
1137   gpointer user_data;
1138   GDestroyNotify destroy;
1139
1140   guint writable_handler_id;
1141   guint property_handler_id;
1142   const GParamSpec *property;
1143   guint key_handler_id;
1144   GVariantType *type;
1145   const gchar *key;
1146
1147   /* prevent recursion */
1148   gboolean running;
1149 } GSettingsBinding;
1150
1151 static void
1152 g_settings_binding_free (gpointer data)
1153 {
1154   GSettingsBinding *binding = data;
1155
1156   g_assert (!binding->running);
1157
1158   if (binding->writable_handler_id)
1159     g_signal_handler_disconnect (binding->settings,
1160                                  binding->writable_handler_id);
1161
1162   if (binding->key_handler_id)
1163     g_signal_handler_disconnect (binding->settings,
1164                                  binding->key_handler_id);
1165
1166   if (g_signal_handler_is_connected (binding->object,
1167                                      binding->property_handler_id))
1168   g_signal_handler_disconnect (binding->object,
1169                                binding->property_handler_id);
1170
1171   g_variant_type_free (binding->type);
1172   g_object_unref (binding->settings);
1173
1174   if (binding->destroy)
1175     binding->destroy (binding->user_data);
1176
1177   g_slice_free (GSettingsBinding, binding);
1178 }
1179
1180 static GQuark
1181 g_settings_binding_quark (const char *property)
1182 {
1183   GQuark quark;
1184   gchar *tmp;
1185
1186   tmp = g_strdup_printf ("gsettingsbinding-%s", property);
1187   quark = g_quark_from_string (tmp);
1188   g_free (tmp);
1189
1190   return quark;
1191 }
1192
1193 static void
1194 g_settings_binding_key_changed (GSettings   *settings,
1195                                 const gchar *key,
1196                                 gpointer     user_data)
1197 {
1198   GSettingsBinding *binding = user_data;
1199   GValue value = { 0, };
1200   GVariant *variant;
1201
1202   g_assert (settings == binding->settings);
1203   g_assert (key == binding->key);
1204
1205   if (binding->running)
1206     return;
1207
1208   binding->running = TRUE;
1209
1210   g_value_init (&value, binding->property->value_type);
1211   variant = g_settings_get_value (settings, key);
1212   if (binding->get_mapping (&value, variant, binding->user_data))
1213     g_object_set_property (binding->object,
1214                            binding->property->name,
1215                            &value);
1216   g_value_unset (&value);
1217
1218   binding->running = FALSE;
1219 }
1220
1221 static void
1222 g_settings_binding_property_changed (GObject          *object,
1223                                      const GParamSpec *pspec,
1224                                      gpointer          user_data)
1225 {
1226   GSettingsBinding *binding = user_data;
1227   GValue value = { 0, };
1228   GVariant *variant;
1229
1230   g_assert (object == binding->object);
1231   g_assert (pspec == binding->property);
1232
1233   if (binding->running)
1234     return;
1235
1236   binding->running = TRUE;
1237
1238   g_value_init (&value, pspec->value_type);
1239   g_object_get_property (object, pspec->name, &value);
1240   if ((variant = binding->set_mapping (&value, binding->type,
1241                                        binding->user_data)))
1242     {
1243       g_settings_set_value (binding->settings,
1244                             binding->key,
1245                             g_variant_ref_sink (variant));
1246       g_variant_unref (variant);
1247     }
1248   g_value_unset (&value);
1249
1250   binding->running = FALSE;
1251 }
1252
1253 /**
1254  * g_settings_bind:
1255  * @settings: a #GSettings object
1256  * @key: the key to bind
1257  * @object: a #GObject
1258  * @property: the name of the property to bind
1259  * @flags: flags for the binding
1260  *
1261  * Create a binding between the @key in the @settings object
1262  * and the property @property of @object.
1263  *
1264  * The binding uses the default GIO mapping functions to map
1265  * between the settings and property values. These functions
1266  * handle booleans, numeric types and string types in a
1267  * straightforward way. Use g_settings_bind_with_mapping() if
1268  * you need a custom mapping, or map between types that are not
1269  * supported by the default mapping functions.
1270  *
1271  * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
1272  * function also establishes a binding between the writability of
1273  * @key and the "sensitive" property of @object (if @object has
1274  * a boolean property by that name). See g_settings_bind_writable()
1275  * for more details about writable bindings.
1276  *
1277  * Note that the lifecycle of the binding is tied to the object,
1278  * and that you can have only one binding per object property.
1279  * If you bind the same property twice on the same object, the second
1280  * binding overrides the first one.
1281  *
1282  * Since: 2.26
1283  */
1284 void
1285 g_settings_bind (GSettings          *settings,
1286                  const gchar        *key,
1287                  gpointer            object,
1288                  const gchar        *property,
1289                  GSettingsBindFlags  flags)
1290 {
1291   g_settings_bind_with_mapping (settings, key, object, property,
1292                                 flags, NULL, NULL, NULL, NULL);
1293 }
1294
1295 /**
1296  * g_settings_bind_with_mapping:
1297  * @settings: a #GSettings object
1298  * @key: the key to bind
1299  * @object: a #GObject
1300  * @property: the name of the property to bind
1301  * @flags: flags for the binding
1302  * @get_mapping: a function that gets called to convert values
1303  *     from @settings to @object, or %NULL to use the default GIO mapping
1304  * @set_mapping: a function that gets called to convert values
1305  *     from @object to @settings, or %NULL to use the default GIO mapping
1306  * @user_data: data that gets passed to @get_mapping and @set_mapping
1307  * @destroy: #GDestroyNotify function for @user_data
1308  *
1309  * Create a binding between the @key in the @settings object
1310  * and the property @property of @object.
1311  *
1312  * The binding uses the provided mapping functions to map between
1313  * settings and property values.
1314  *
1315  * Note that the lifecycle of the binding is tied to the object,
1316  * and that you can have only one binding per object property.
1317  * If you bind the same property twice on the same object, the second
1318  * binding overrides the first one.
1319  *
1320  * Since: 2.26
1321  */
1322 void
1323 g_settings_bind_with_mapping (GSettings               *settings,
1324                               const gchar             *key,
1325                               gpointer                 object,
1326                               const gchar             *property,
1327                               GSettingsBindFlags       flags,
1328                               GSettingsBindGetMapping  get_mapping,
1329                               GSettingsBindSetMapping  set_mapping,
1330                               gpointer                 user_data,
1331                               GDestroyNotify           destroy)
1332 {
1333   GSettingsBinding *binding;
1334   GObjectClass *objectclass;
1335   gchar *detailed_signal;
1336   GQuark binding_quark;
1337
1338   g_return_if_fail (G_IS_SETTINGS (settings));
1339
1340   objectclass = G_OBJECT_GET_CLASS (object);
1341
1342   binding = g_slice_new0 (GSettingsBinding);
1343   binding->settings = g_object_ref (settings);
1344   binding->object = object;
1345   binding->key = g_intern_string (key);
1346   binding->property = g_object_class_find_property (objectclass, property);
1347   binding->user_data = user_data;
1348   binding->destroy = destroy;
1349   binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
1350   binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
1351
1352   if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
1353     flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
1354
1355   if (binding->property == NULL)
1356     {
1357       g_critical ("g_settings_bind: no property '%s' on class '%s'",
1358                   property, G_OBJECT_TYPE_NAME (object));
1359       return;
1360     }
1361
1362   if ((flags & G_SETTINGS_BIND_GET) && (binding->property->flags & G_PARAM_WRITABLE) == 0)
1363     {
1364       g_critical ("g_settings_bind: property '%s' on class '%s' is not writable",
1365                   property, G_OBJECT_TYPE_NAME (object));
1366       return;
1367     }
1368   if ((flags & G_SETTINGS_BIND_SET) && (binding->property->flags & G_PARAM_READABLE) == 0)
1369     {
1370       g_critical ("g_settings_bind: property '%s' on class '%s' is not readable",
1371                   property, G_OBJECT_TYPE_NAME (object));
1372       return;
1373     }
1374
1375   {
1376     GVariant *value;
1377
1378     value = g_settings_schema_get_value (settings->priv->schema, key, NULL);
1379     binding->type = g_variant_type_copy (g_variant_get_type (value));
1380     if (value)
1381       g_variant_unref (value);
1382   }
1383
1384   if (binding->type == NULL)
1385     {
1386       g_critical ("g_settings_bind: no key '%s' on schema '%s'",
1387                   key, settings->priv->schema_name);
1388       return;
1389     }
1390
1391   if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
1392        (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
1393       !g_settings_mapping_is_compatible (binding->property->value_type,
1394                                          binding->type))
1395     {
1396       g_critical ("g_settings_bind: property '%s' on class '%s' has type"
1397                   "'%s' which is not compatible with type '%s' of key '%s'"
1398                   "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
1399                   g_type_name (binding->property->value_type),
1400                   g_variant_type_dup_string (binding->type), key,
1401                   settings->priv->schema_name);
1402       return;
1403     }
1404
1405   if ((flags & G_SETTINGS_BIND_SET) &&
1406       (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
1407     {
1408       GParamSpec *sensitive;
1409
1410       sensitive = g_object_class_find_property (objectclass, "sensitive");
1411
1412       if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
1413           (sensitive->flags & G_PARAM_WRITABLE))
1414         g_settings_bind_writable (settings, binding->key,
1415                                   object, "sensitive", FALSE);
1416     }
1417
1418   if (flags & G_SETTINGS_BIND_SET)
1419     {
1420       detailed_signal = g_strdup_printf ("notify::%s", property);
1421       binding->property_handler_id =
1422         g_signal_connect (object, detailed_signal,
1423                           G_CALLBACK (g_settings_binding_property_changed),
1424                           binding);
1425       g_free (detailed_signal);
1426
1427       if (~flags & G_SETTINGS_BIND_GET)
1428         g_settings_binding_property_changed (object,
1429                                              binding->property,
1430                                              binding);
1431     }
1432
1433   if (flags & G_SETTINGS_BIND_GET)
1434     {
1435       if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
1436         {
1437           detailed_signal = g_strdup_printf ("changed::%s", key);
1438           binding->key_handler_id =
1439             g_signal_connect (settings, detailed_signal,
1440                               G_CALLBACK (g_settings_binding_key_changed),
1441                               binding);
1442           g_free (detailed_signal);
1443         }
1444
1445       g_settings_binding_key_changed (settings, binding->key, binding);
1446     }
1447
1448   binding_quark = g_settings_binding_quark (property);
1449   g_object_set_qdata_full (object, binding_quark,
1450                            binding, g_settings_binding_free);
1451 }
1452
1453 typedef struct
1454 {
1455   GSettings *settings;
1456   gpointer object;
1457   const gchar *key;
1458   const gchar *property;
1459   gboolean inverted;
1460   gulong handler_id;
1461 } GSettingsWritableBinding;
1462
1463 static void
1464 g_settings_writable_binding_free (gpointer data)
1465 {
1466   GSettingsWritableBinding *binding = data;
1467
1468   g_signal_handler_disconnect (binding->settings, binding->handler_id);
1469   g_object_unref (binding->settings);
1470   g_slice_free (GSettingsWritableBinding, binding);
1471 }
1472
1473 static void
1474 g_settings_binding_writable_changed (GSettings   *settings,
1475                                      const gchar *key,
1476                                      gpointer     user_data)
1477 {
1478   GSettingsWritableBinding *binding = user_data;
1479   gboolean writable;
1480
1481   g_assert (settings == binding->settings);
1482   g_assert (key == binding->key);
1483
1484   writable = g_settings_is_writable (settings, key);
1485
1486   if (binding->inverted)
1487     writable = !writable;
1488
1489   g_object_set (binding->object, binding->property, writable, NULL);
1490 }
1491
1492 /**
1493  * g_settings_bind_writable:
1494  * @settings: a #GSettings object
1495  * @key: the key to bind
1496  * @object: a #GObject
1497  * @property: the name of a boolean property to bind
1498  * @inverted: whether to 'invert' the value
1499  *
1500  * Create a binding between the writability of @key in the
1501  * @settings object and the property @property of @object.
1502  * The property must be boolean; "sensitive" or "visible"
1503  * properties of widgets are the most likely candidates.
1504  *
1505  * Writable bindings are always uni-directional; changes of the
1506  * writability of the setting will be propagated to the object
1507  * property, not the other way.
1508  *
1509  * When the @inverted argument is %TRUE, the binding inverts the
1510  * value as it passes from the setting to the object, i.e. @property
1511  * will be set to %TRUE if the key is <emphasis>not</emphasis>
1512  * writable.
1513  *
1514  * Note that the lifecycle of the binding is tied to the object,
1515  * and that you can have only one binding per object property.
1516  * If you bind the same property twice on the same object, the second
1517  * binding overrides the first one.
1518  *
1519  * Since: 2.26
1520  */
1521 void
1522 g_settings_bind_writable (GSettings   *settings,
1523                           const gchar *key,
1524                           gpointer     object,
1525                           const gchar *property,
1526                           gboolean     inverted)
1527 {
1528   GSettingsWritableBinding *binding;
1529   gchar *detailed_signal;
1530   GParamSpec *pspec;
1531
1532   g_return_if_fail (G_IS_SETTINGS (settings));
1533
1534   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
1535   if (pspec == NULL)
1536     {
1537       g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
1538                   property, G_OBJECT_TYPE_NAME (object));
1539       return;
1540     }
1541   if ((pspec->flags & G_PARAM_WRITABLE) == 0)
1542     {
1543       g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
1544                   property, G_OBJECT_TYPE_NAME (object));
1545       return;
1546     }
1547
1548   binding = g_slice_new (GSettingsWritableBinding);
1549   binding->settings = g_object_ref (settings);
1550   binding->object = object;
1551   binding->key = g_intern_string (key);
1552   binding->property = g_intern_string (property);
1553   binding->inverted = inverted;
1554
1555   detailed_signal = g_strdup_printf ("writable-changed::%s", key);
1556   binding->handler_id =
1557     g_signal_connect (settings, detailed_signal,
1558                       G_CALLBACK (g_settings_binding_writable_changed),
1559                       binding);
1560   g_free (detailed_signal);
1561
1562   g_object_set_qdata_full (object, g_settings_binding_quark (property),
1563                            binding, g_settings_writable_binding_free);
1564
1565   g_settings_binding_writable_changed (settings, binding->key, binding);
1566 }
1567
1568 /**
1569  * g_settings_unbind:
1570  * @object: the object
1571  * @property: the property whose binding is removed
1572  *
1573  * Removes an existing binding for @property on @object.
1574  *
1575  * Note that bindings are automatically removed when the
1576  * object is finalized, so it is rarely necessary to call this
1577  * function.
1578  *
1579  * Since: 2.26
1580  */
1581 void
1582 g_settings_unbind (gpointer     object,
1583                    const gchar *property)
1584 {
1585   GQuark binding_quark;
1586
1587   binding_quark = g_settings_binding_quark (property);
1588   g_object_set_qdata (object, binding_quark, NULL);
1589 }
1590
1591 /**
1592  * g_settings_get_string:
1593  * @settings: a #GSettings object
1594  * @key: the key to get the value for
1595  * @returns: a newly-allocated string
1596  *
1597  * Gets the value that is stored at @key in @settings.
1598  *
1599  * A convenience variant of g_settings_get() for strings.
1600  *
1601  * It is a programmer error to pass a @key that isn't valid for
1602  * @settings or is not of type string.
1603  *
1604  * Since: 2.26
1605  */
1606 gchar *
1607 g_settings_get_string (GSettings   *settings,
1608                        const gchar *key)
1609 {
1610   GVariant *value;
1611   gchar *result;
1612
1613   value = g_settings_get_value (settings, key);
1614   result = g_variant_dup_string (value, NULL);
1615   g_variant_unref (value);
1616
1617   return result;
1618 }
1619
1620 /**
1621  * g_settings_set_string:
1622  * @settings: a #GSettings object
1623  * @key: the name of the key to set
1624  * @value: the value to set it to
1625  * @returns: %TRUE if setting the key succeeded,
1626  *     %FALSE if the key was not writable
1627  *
1628  * Sets @key in @settings to @value.
1629  *
1630  * A convenience variant of g_settings_set() for strings.
1631  *
1632  * It is a programmer error to pass a @key that isn't valid for
1633  * @settings or is not of type string.
1634  *
1635  * Since: 2.26
1636  */
1637 gboolean
1638 g_settings_set_string (GSettings   *settings,
1639                        const gchar *key,
1640                        const gchar *value)
1641 {
1642   return g_settings_set_value (settings, key, g_variant_new_string (value));
1643 }
1644
1645 /**
1646  * g_settings_get_int:
1647  * @settings: a #GSettings object
1648  * @key: the key to get the value for
1649  * @returns: an integer
1650  *
1651  * Gets the value that is stored at @key in @settings.
1652  *
1653  * A convenience variant of g_settings_get() for 32-bit integers.
1654  *
1655  * It is a programmer error to pass a @key that isn't valid for
1656  * @settings or is not of type int32.
1657  *
1658  * Since: 2.26
1659  */
1660 gint
1661 g_settings_get_int (GSettings   *settings,
1662                     const gchar *key)
1663 {
1664   GVariant *value;
1665   gint result;
1666
1667   value = g_settings_get_value (settings, key);
1668   result = g_variant_get_int32 (value);
1669   g_variant_unref (value);
1670
1671   return result;
1672 }
1673
1674 /**
1675  * g_settings_set_int:
1676  * @settings: a #GSettings object
1677  * @key: the name of the key to set
1678  * @value: the value to set it to
1679  * @returns: %TRUE if setting the key succeeded,
1680  *     %FALSE if the key was not writable
1681  *
1682  * Sets @key in @settings to @value.
1683  *
1684  * A convenience variant of g_settings_set() for 32-bit integers.
1685  *
1686  * It is a programmer error to pass a @key that isn't valid for
1687  * @settings or is not of type int32.
1688  *
1689  * Since: 2.26
1690  */
1691 gboolean
1692 g_settings_set_int (GSettings   *settings,
1693                     const gchar *key,
1694                     gint         value)
1695 {
1696   return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1697 }
1698
1699 /**
1700  * g_settings_get_double:
1701  * @settings: a #GSettings object
1702  * @key: the key to get the value for
1703  * @returns: a double
1704  *
1705  * Gets the value that is stored at @key in @settings.
1706  *
1707  * A convenience variant of g_settings_get() for doubles.
1708  *
1709  * It is a programmer error to pass a @key that isn't valid for
1710  * @settings or is not of type double.
1711  *
1712  * Since: 2.26
1713  */
1714 gdouble
1715 g_settings_get_double (GSettings   *settings,
1716                        const gchar *key)
1717 {
1718   GVariant *value;
1719   gdouble result;
1720
1721   value = g_settings_get_value (settings, key);
1722   result = g_variant_get_double (value);
1723   g_variant_unref (value);
1724
1725   return result;
1726 }
1727
1728 /**
1729  * g_settings_set_double:
1730  * @settings: a #GSettings object
1731  * @key: the name of the key to set
1732  * @value: the value to set it to
1733  * @returns: %TRUE if setting the key succeeded,
1734  *     %FALSE if the key was not writable
1735  *
1736  * Sets @key in @settings to @value.
1737  *
1738  * A convenience variant of g_settings_set() for doubles.
1739  *
1740  * It is a programmer error to pass a @key that isn't valid for
1741  * @settings or is not of type double.
1742  *
1743  * Since: 2.26
1744  */
1745 gboolean
1746 g_settings_set_double (GSettings   *settings,
1747                        const gchar *key,
1748                        gdouble      value)
1749 {
1750   return g_settings_set_value (settings, key, g_variant_new_double (value));
1751 }
1752
1753 /**
1754  * g_settings_get_boolean:
1755  * @settings: a #GSettings object
1756  * @key: the key to get the value for
1757  * @returns: a boolean
1758  *
1759  * Gets the value that is stored at @key in @settings.
1760  *
1761  * A convenience variant of g_settings_get() for booleans.
1762  *
1763  * It is a programmer error to pass a @key that isn't valid for
1764  * @settings or is not of type boolean.
1765  *
1766  * Since: 2.26
1767  */
1768 gboolean
1769 g_settings_get_boolean (GSettings  *settings,
1770                        const gchar *key)
1771 {
1772   GVariant *value;
1773   gboolean result;
1774
1775   value = g_settings_get_value (settings, key);
1776   result = g_variant_get_boolean (value);
1777   g_variant_unref (value);
1778
1779   return result;
1780 }
1781
1782 /**
1783  * g_settings_set_boolean:
1784  * @settings: a #GSettings object
1785  * @key: the name of the key to set
1786  * @value: the value to set it to
1787  * @returns: %TRUE if setting the key succeeded,
1788  *     %FALSE if the key was not writable
1789  *
1790  * Sets @key in @settings to @value.
1791  *
1792  * A convenience variant of g_settings_set() for booleans.
1793  *
1794  * It is a programmer error to pass a @key that isn't valid for
1795  * @settings or is not of type boolean.
1796  *
1797  * Since: 2.26
1798  */
1799 gboolean
1800 g_settings_set_boolean (GSettings  *settings,
1801                        const gchar *key,
1802                        gboolean     value)
1803 {
1804   return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1805 }
1806
1807 /**
1808  * g_settings_get_strv:
1809  * @settings: a #GSettings object
1810  * @key: the key to get the value for
1811  * @length: return location for the length of the result, or %NULL
1812  * @returns: a newly-allocated, %NULL-terminated array of strings
1813  *
1814  * Gets the value that is stored at @key in @settings.
1815  *
1816  * A convenience variant of g_settings_get() for string arrays.
1817  *
1818  * It is a programmer error to pass a @key that isn't valid for
1819  * @settings or is not of type 'string array'.
1820  *
1821  * Since: 2.26
1822  */
1823 gchar **
1824 g_settings_get_strv (GSettings   *settings,
1825                      const gchar *key,
1826                      gsize       *length)
1827 {
1828   GVariant *value;
1829   gchar **result;
1830
1831   value = g_settings_get_value (settings, key);
1832   result = g_variant_dup_strv (value, length);
1833   g_variant_unref (value);
1834
1835   return result;
1836 }
1837
1838 /**
1839  * g_settings_set_strv:
1840  * @settings: a #GSettings object
1841  * @key: the name of the key to set
1842  * @value: the value to set it to
1843  * @length: the length of the @value array, or -1
1844  * @returns: %TRUE if setting the key succeeded,
1845  *     %FALSE if the key was not writable
1846  *
1847  * Sets @key in @settings to @value.
1848  *
1849  * A convenience variant of g_settings_set() for string arrays.
1850  *
1851  * It is a programmer error to pass a @key that isn't valid for
1852  * @settings or is not of type 'string array'.
1853  *
1854  * Since: 2.26
1855  */
1856 gboolean
1857 g_settings_set_strv (GSettings           *settings,
1858                      const gchar         *key,
1859                      const gchar * const *value,
1860                      gssize               length)
1861 {
1862   return g_settings_set_value (settings, key, g_variant_new_strv (value, length));
1863 }
1864
1865 #define __G_SETTINGS_C__
1866 #include "gioaliasdef.c"