improve thread safety in GDelayedSettingsBackend
[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                           GObject             *target,
228                           const gchar         *key,
229                           gpointer             origin_tag)
230 {
231   GSettings *settings = G_SETTINGS (target);
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                                GObject          *target,
253                                const gchar      *path,
254                                gpointer          origin_tag)
255 {
256   GSettings *settings = G_SETTINGS (target);
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                                GObject             *target,
269                                const gchar         *path,
270                                const gchar * const *items,
271                                gpointer             origin_tag)
272 {
273   GSettings *settings = G_SETTINGS (target);
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                                    GObject          *target,
313                                    const gchar      *key)
314 {
315   GSettings *settings = G_SETTINGS (target);
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                                         GObject          *target,
332                                         const gchar      *path)
333 {
334   GSettings *settings = G_SETTINGS (target);
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, 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);
377 }
378
379 static void
380 g_settings_init (GSettings *settings)
381 {
382   settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
383                                                 G_TYPE_SETTINGS,
384                                                 GSettingsPrivate);
385
386   settings->priv->main_context = g_main_context_get_thread_default ();
387
388   if (settings->priv->main_context == NULL)
389     settings->priv->main_context = g_main_context_default ();
390
391   g_main_context_ref (settings->priv->main_context);
392 }
393
394 /**
395  * g_settings_delay:
396  * @settings: a #GSettings object
397  *
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.
401  *
402  * Since: 2.26
403  */
404 void
405 g_settings_delay (GSettings *settings)
406 {
407   g_return_if_fail (G_IS_SETTINGS (settings));
408
409   if (settings->priv->delayed)
410     return;
411
412   settings->priv->delayed =
413     g_delayed_settings_backend_new (settings->priv->backend,
414                                     settings,
415                                     settings->priv->main_context);
416   g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
417   g_object_unref (settings->priv->backend);
418
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);
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_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);
556 }
557
558 static void
559 g_settings_class_init (GSettingsClass *class)
560 {
561   GObjectClass *object_class = G_OBJECT_CLASS (class);
562
563   class->writable_change_event = g_settings_real_writable_change_event;
564   class->change_event = g_settings_real_change_event;
565
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;
570
571   g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
572
573   /**
574    * GSettings::changed:
575    * @settings: the object on which the signal was emitted
576    * @key: the name of the key that changed
577    *
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
580    * value.
581    *
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.
585    */
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);
592
593   /**
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.
600    *
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.
606    *
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.
612    *
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.
616    */
617   g_settings_signals[SIGNAL_CHANGE_EVENT] =
618     g_signal_new ("change-event", G_TYPE_SETTINGS,
619                   G_SIGNAL_RUN_LAST,
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);
624
625   /**
626    * GSettings::writable-changed:
627    * @settings: the object on which the signal was emitted
628    * @key: the key
629    *
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.
633    *
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.
637    */
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);
644
645   /**
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.
651    *
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.
658    *
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,
662    * @key will be 0.
663    *
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
669    * will be supressed.
670    */
671   g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
672     g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
673                   G_SIGNAL_RUN_LAST,
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);
677
678   /**
679    * GSettings:context:
680    *
681    * The name of the context that the settings are stored in.
682    */
683   g_object_class_install_property (object_class, PROP_CONTEXT,
684     g_param_spec_string ("context",
685                          P_("Context name"),
686                          P_("The name of the context for this settings object"),
687                          "", G_PARAM_CONSTRUCT_ONLY |
688                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
689
690   /**
691    * GSettings:schema:
692    *
693    * The name of the schema that describes the types of keys
694    * for this #GSettings object.
695    */
696   g_object_class_install_property (object_class, PROP_SCHEMA,
697     g_param_spec_string ("schema",
698                          P_("Schema name"),
699                          P_("The name of the schema for this settings object"),
700                          NULL,
701                          G_PARAM_CONSTRUCT_ONLY |
702                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
703
704    /**
705     * GSettings:path:
706     *
707     * The path within the backend where the settings are stored.
708     */
709    g_object_class_install_property (object_class, PROP_PATH,
710      g_param_spec_string ("path",
711                           P_("Base path"),
712                           P_("The path within the backend where the settings are"),
713                           NULL,
714                           G_PARAM_CONSTRUCT_ONLY |
715                           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
716
717    /**
718     * GSettings:has-unapplied:
719     *
720     * If this property is %TRUE, the #GSettings object has outstanding
721     * changes that will be applied when g_settings_apply() is called.
722     */
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()"),
727                            FALSE,
728                            G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
729
730 }
731
732 /**
733  * g_settings_get_value:
734  * @settings: a #GSettings object
735  * @key: the key to get the value for
736  * @returns: a new #GVariant
737  *
738  * Gets the value that is stored in @settings for @key.
739  *
740  * It is a programmer error to give a @key that isn't valid for
741  * @settings.
742  *
743  * Since: 2.26
744  */
745 GVariant *
746 g_settings_get_value (GSettings   *settings,
747                       const gchar *key)
748 {
749   const gchar *unparsed = NULL;
750   GVariant *value, *options;
751   const GVariantType *type;
752   gchar lc_char = '\0';
753   GVariant *sval;
754   gchar *path;
755
756   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
757
758   sval = g_settings_schema_get_value (settings->priv->schema, key, &options);
759
760   if G_UNLIKELY (sval == NULL)
761     g_error ("schema '%s' does not contain a key named '%s'",
762              settings->priv->schema_name, key);
763
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);
767   g_free (path);
768
769   if (options != NULL)
770     {
771       GVariantIter iter;
772       const gchar *option;
773       GVariant *option_value;
774
775       g_variant_iter_init (&iter, options);
776       while (g_variant_iter_loop (&iter, "{&sv}", &option, &option_value))
777         {
778           if (strcmp (option, "l10n") == 0)
779             g_variant_get (option_value, "(y&s)", &lc_char, &unparsed);
780           else
781             g_warning ("unknown schema extension '%s'", option);
782         }
783     }
784
785   if (value && !g_variant_is_of_type (value, type))
786     {
787       g_variant_unref (value);
788       value = NULL;
789     }
790
791   if (value == NULL && lc_char != '\0')
792   /* we will need to translate the schema default value */
793     {
794       const gchar *translated;
795       GError *error = NULL;
796       const gchar *domain;
797       gint lc_category;
798
799       domain = g_settings_schema_get_gettext_domain (settings->priv->schema);
800
801       if (lc_char == 't')
802         lc_category = LC_TIME;
803       else
804         lc_category = LC_MESSAGES;
805
806       translated = dcgettext (domain, unparsed, lc_category);
807
808       if (translated != unparsed)
809         /* it was translated, so we need to re-parse it */
810         {
811           value = g_variant_parse (g_variant_get_type (sval),
812                                    translated, NULL, NULL, &error);
813
814           if (value == NULL)
815             {
816               g_warning ("Failed to parse translated string `%s' for "
817                          "key `%s' in schema `%s': %s", unparsed, key,
818                          settings->priv->schema_name, error->message);
819               g_warning ("Using untranslated default instead.");
820               g_error_free (error);
821             }
822         }
823     }
824
825   if (value == NULL)
826     /* either translation failed or there was none to do.
827      * use the pre-compiled default.
828      */
829     value = g_variant_ref (sval);
830
831   g_variant_unref (sval);
832
833   return value;
834 }
835
836 /**
837  * g_settings_set_value:
838  * @settings: a #GSettings object
839  * @key: the name of the key to set
840  * @value: a #GVariant of the correct type
841  * @returns: %TRUE if setting the key succeeded,
842  *     %FALSE if the key was not writable
843  *
844  * Sets @key in @settings to @value.
845  *
846  * It is a programmer error to give a @key that isn't valid for
847  * @settings.  It is a programmer error to give a @value of the
848  * incorrect type.
849  *
850  * If @value is floating then this function consumes the reference.
851  *
852  * Since: 2.26
853  **/
854 gboolean
855 g_settings_set_value (GSettings   *settings,
856                       const gchar *key,
857                       GVariant    *value)
858 {
859   gboolean correct_type;
860   gboolean result;
861   GVariant *sval;
862   gchar *path;
863
864   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
865
866   sval = g_settings_schema_get_value (settings->priv->schema, key, NULL);
867   correct_type = g_variant_is_of_type (value, g_variant_get_type (sval));
868   g_variant_unref (sval);
869
870   g_return_val_if_fail (correct_type, FALSE);
871
872   path = g_strconcat (settings->priv->path, key, NULL);
873   result = g_settings_backend_write (settings->priv->backend,
874                                      path, value, NULL);
875   g_free (path);
876
877   return result;
878 }
879
880 /**
881  * g_settings_get:
882  * @settings: a #GSettings object
883  * @key: the key to get the value for
884  * @format: a #GVariant format string
885  * @...: arguments as per @format
886  *
887  * Gets the value that is stored at @key in @settings.
888  *
889  * A convenience function that combines g_settings_get_value() with
890  * g_variant_get().
891  *
892  * It is a programmer error to pass a @key that isn't valid for
893  * @settings or a @format_string that doesn't match the type of @key according
894  * to the schema of @settings.
895  *
896  * Since: 2.26
897  */
898 void
899 g_settings_get (GSettings   *settings,
900                 const gchar *key,
901                 const gchar *format,
902                 ...)
903 {
904   GVariant *value;
905   va_list ap;
906
907   value = g_settings_get_value (settings, key);
908
909   va_start (ap, format);
910   g_variant_get_va (value, format, NULL, &ap);
911   va_end (ap);
912
913   g_variant_unref (value);
914 }
915
916 /**
917  * g_settings_set:
918  * @settings: a #GSettings object
919  * @key: the name of the key to set
920  * @format: a #GVariant format string
921  * @...: arguments as per @format
922  * @returns: %TRUE if setting the key succeeded,
923  *     %FALSE if the key was not writable
924  *
925  * Sets @key in @settings to @value.
926  *
927  * A convenience function that combines g_settings_set_value() with
928  * g_variant_new().
929  *
930  * It is a programmer error to pass a @key that isn't valid for
931  * @settings or a @format that doesn't match the type of @key according
932  * to the schema of @settings.
933  *
934  * Since: 2.26
935  */
936 gboolean
937 g_settings_set (GSettings   *settings,
938                 const gchar *key,
939                 const gchar *format,
940                 ...)
941 {
942   GVariant *value;
943   va_list ap;
944
945   va_start (ap, format);
946   value = g_variant_new_va (format, NULL, &ap);
947   va_end (ap);
948
949   return g_settings_set_value (settings, key, value);
950 }
951
952 /**
953  * g_settings_is_writable:
954  * @settings: a #GSettings object
955  * @name: the name of a key
956  * @returns: %TRUE if the key @name is writable
957  *
958  * Finds out if a key can be written or not
959  *
960  * Since: 2.26
961  */
962 gboolean
963 g_settings_is_writable (GSettings   *settings,
964                         const gchar *name)
965 {
966   gboolean writable;
967   gchar *path;
968
969   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
970
971   path = g_strconcat (settings->priv->path, name, NULL);
972   writable = g_settings_backend_get_writable (settings->priv->backend, path);
973   g_free (path);
974
975   return writable;
976 }
977
978 /**
979  * g_settings_get_child:
980  * @settings: a #GSettings object
981  * @name: the name of the 'child' schema
982  * @returns: a 'child' settings object
983  *
984  * Creates a 'child' settings object which has a base path of
985  * <replaceable>base-path</replaceable>/@name", where
986  * <replaceable>base-path</replaceable> is the base path of @settings.
987  *
988  * The schema for the child settings object must have been declared
989  * in the schema of @settings using a <tag class="starttag">child</tag> element.
990  *
991  * Since: 2.26
992  */
993 GSettings *
994 g_settings_get_child (GSettings   *settings,
995                       const gchar *name)
996 {
997   GVariant *child_schema;
998   gchar *child_path;
999   gchar *child_name;
1000   GSettings *child;
1001
1002   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1003
1004   child_name = g_strconcat (name, "/", NULL);
1005   child_schema = g_settings_schema_get_value (settings->priv->schema,
1006                                               child_name, NULL);
1007   if (child_schema == NULL ||
1008       !g_variant_is_of_type (child_schema, G_VARIANT_TYPE_STRING))
1009     g_error ("Schema '%s' has no child '%s'",
1010              settings->priv->schema_name, name);
1011
1012   child_path = g_strconcat (settings->priv->path, child_name, NULL);
1013   child = g_object_new (G_TYPE_SETTINGS,
1014                         "schema", g_variant_get_string (child_schema, NULL),
1015                         "path", child_path,
1016                         NULL);
1017   g_variant_unref (child_schema);
1018   g_free (child_path);
1019   g_free (child_name);
1020
1021   return child;
1022 }
1023
1024 /**
1025  * g_settings_new:
1026  * @schema: the name of the schema
1027  * @returns: a new #GSettings object
1028  *
1029  * Creates a new #GSettings object with a given schema.
1030  *
1031  * Signals on the newly created #GSettings object will be dispatched
1032  * via the thread-default #GMainContext in effect at the time of the
1033  * call to g_settings_new().  The new #GSettings will hold a reference
1034  * on the context.  See g_main_context_push_thread_default().
1035  *
1036  * Since: 2.26
1037  */
1038 GSettings *
1039 g_settings_new (const gchar *schema)
1040 {
1041   return g_object_new (G_TYPE_SETTINGS,
1042                        "schema", schema,
1043                        NULL);
1044 }
1045
1046 /**
1047  * g_settings_new_with_path:
1048  * @schema: the name of the schema
1049  * @path: the path to use
1050  * @returns: a new #GSettings object
1051  *
1052  * Creates a new #GSettings object with a given schema and path.
1053  *
1054  * You only need to do this if you want to directly create a settings
1055  * object with a schema that doesn't have a specified path of its own.
1056  * That's quite rare.
1057  *
1058  * It is a programmer error to call this function for a schema that
1059  * has an explicitly specified path.
1060  *
1061  * Since: 2.26
1062  */
1063 GSettings *
1064 g_settings_new_with_path (const gchar *schema,
1065                           const gchar *path)
1066 {
1067   return g_object_new (G_TYPE_SETTINGS,
1068                        "schema", schema,
1069                        "path", path,
1070                        NULL);
1071 }
1072
1073 /**
1074  * g_settings_new_with_context:
1075  * @schema: the name of the schema
1076  * @context: the context to use
1077  * @returns: a new #GSettings object
1078  *
1079  * Creates a new #GSettings object with a given schema and context.
1080  *
1081  * Creating settings objects with a context allow accessing settings
1082  * from a database other than the usual one.  For example, it may make
1083  * sense to specify "defaults" in order to get a settings object that
1084  * modifies the system default settings instead of the settings for this
1085  * user.
1086  *
1087  * It is a programmer error to call this function for an unsupported
1088  * context.  Use g_settings_supports_context() to determine if a context
1089  * is supported if you are unsure.
1090  *
1091  * Since: 2.26
1092  */
1093 GSettings *
1094 g_settings_new_with_context (const gchar *schema,
1095                              const gchar *context)
1096 {
1097   return g_object_new (G_TYPE_SETTINGS,
1098                        "schema", schema,
1099                        "context", context,
1100                        NULL);
1101 }
1102
1103 /**
1104  * g_settings_new_with_context_and_path:
1105  * @schema: the name of the schema
1106  * @path: the path to use
1107  * @returns: a new #GSettings object
1108  *
1109  * Creates a new #GSettings object with a given schema, context and
1110  * path.
1111  *
1112  * This is a mix of g_settings_new_with_context() and
1113  * g_settings_new_with_path().
1114  *
1115  * Since: 2.26
1116  */
1117 GSettings *
1118 g_settings_new_with_context_and_path (const gchar *schema,
1119                                       const gchar *context,
1120                                       const gchar *path)
1121 {
1122   return g_object_new (G_TYPE_SETTINGS,
1123                        "schema", schema,
1124                         "context", context,
1125                         "path", path,
1126                         NULL);
1127 }
1128
1129 typedef struct
1130 {
1131   GSettings *settings;
1132   GObject *object;
1133
1134   GSettingsBindGetMapping get_mapping;
1135   GSettingsBindSetMapping set_mapping;
1136   gpointer user_data;
1137   GDestroyNotify destroy;
1138
1139   guint writable_handler_id;
1140   guint property_handler_id;
1141   const GParamSpec *property;
1142   guint key_handler_id;
1143   GVariantType *type;
1144   const gchar *key;
1145
1146   /* prevent recursion */
1147   gboolean running;
1148 } GSettingsBinding;
1149
1150 static void
1151 g_settings_binding_free (gpointer data)
1152 {
1153   GSettingsBinding *binding = data;
1154
1155   g_assert (!binding->running);
1156
1157   if (binding->writable_handler_id)
1158     g_signal_handler_disconnect (binding->settings,
1159                                  binding->writable_handler_id);
1160
1161   if (binding->key_handler_id)
1162     g_signal_handler_disconnect (binding->settings,
1163                                  binding->key_handler_id);
1164
1165   if (g_signal_handler_is_connected (binding->object,
1166                                      binding->property_handler_id))
1167   g_signal_handler_disconnect (binding->object,
1168                                binding->property_handler_id);
1169
1170   g_variant_type_free (binding->type);
1171   g_object_unref (binding->settings);
1172
1173   if (binding->destroy)
1174     binding->destroy (binding->user_data);
1175
1176   g_slice_free (GSettingsBinding, binding);
1177 }
1178
1179 static GQuark
1180 g_settings_binding_quark (const char *property)
1181 {
1182   GQuark quark;
1183   gchar *tmp;
1184
1185   tmp = g_strdup_printf ("gsettingsbinding-%s", property);
1186   quark = g_quark_from_string (tmp);
1187   g_free (tmp);
1188
1189   return quark;
1190 }
1191
1192 static void
1193 g_settings_binding_key_changed (GSettings   *settings,
1194                                 const gchar *key,
1195                                 gpointer     user_data)
1196 {
1197   GSettingsBinding *binding = user_data;
1198   GValue value = { 0, };
1199   GVariant *variant;
1200
1201   g_assert (settings == binding->settings);
1202   g_assert (key == binding->key);
1203
1204   if (binding->running)
1205     return;
1206
1207   binding->running = TRUE;
1208
1209   g_value_init (&value, binding->property->value_type);
1210   variant = g_settings_get_value (settings, key);
1211   if (binding->get_mapping (&value, variant, binding->user_data))
1212     g_object_set_property (binding->object,
1213                            binding->property->name,
1214                            &value);
1215   g_value_unset (&value);
1216
1217   binding->running = FALSE;
1218 }
1219
1220 static void
1221 g_settings_binding_property_changed (GObject          *object,
1222                                      const GParamSpec *pspec,
1223                                      gpointer          user_data)
1224 {
1225   GSettingsBinding *binding = user_data;
1226   GValue value = { 0, };
1227   GVariant *variant;
1228
1229   g_assert (object == binding->object);
1230   g_assert (pspec == binding->property);
1231
1232   if (binding->running)
1233     return;
1234
1235   binding->running = TRUE;
1236
1237   g_value_init (&value, pspec->value_type);
1238   g_object_get_property (object, pspec->name, &value);
1239   if ((variant = binding->set_mapping (&value, binding->type,
1240                                        binding->user_data)))
1241     {
1242       g_settings_set_value (binding->settings,
1243                             binding->key,
1244                             g_variant_ref_sink (variant));
1245       g_variant_unref (variant);
1246     }
1247   g_value_unset (&value);
1248
1249   binding->running = FALSE;
1250 }
1251
1252 /**
1253  * g_settings_bind:
1254  * @settings: a #GSettings object
1255  * @key: the key to bind
1256  * @object: a #GObject
1257  * @property: the name of the property to bind
1258  * @flags: flags for the binding
1259  *
1260  * Create a binding between the @key in the @settings object
1261  * and the property @property of @object.
1262  *
1263  * The binding uses the default GIO mapping functions to map
1264  * between the settings and property values. These functions
1265  * handle booleans, numeric types and string types in a
1266  * straightforward way. Use g_settings_bind_with_mapping() if
1267  * you need a custom mapping, or map between types that are not
1268  * supported by the default mapping functions.
1269  *
1270  * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
1271  * function also establishes a binding between the writability of
1272  * @key and the "sensitive" property of @object (if @object has
1273  * a boolean property by that name). See g_settings_bind_writable()
1274  * for more details about writable bindings.
1275  *
1276  * Note that the lifecycle of the binding is tied to the object,
1277  * and that you can have only one binding per object property.
1278  * If you bind the same property twice on the same object, the second
1279  * binding overrides the first one.
1280  *
1281  * Since: 2.26
1282  */
1283 void
1284 g_settings_bind (GSettings          *settings,
1285                  const gchar        *key,
1286                  gpointer            object,
1287                  const gchar        *property,
1288                  GSettingsBindFlags  flags)
1289 {
1290   g_settings_bind_with_mapping (settings, key, object, property,
1291                                 flags, NULL, NULL, NULL, NULL);
1292 }
1293
1294 /**
1295  * g_settings_bind_with_mapping:
1296  * @settings: a #GSettings object
1297  * @key: the key to bind
1298  * @object: a #GObject
1299  * @property: the name of the property to bind
1300  * @flags: flags for the binding
1301  * @get_mapping: a function that gets called to convert values
1302  *     from @settings to @object, or %NULL to use the default GIO mapping
1303  * @set_mapping: a function that gets called to convert values
1304  *     from @object to @settings, or %NULL to use the default GIO mapping
1305  * @user_data: data that gets passed to @get_mapping and @set_mapping
1306  * @destroy: #GDestroyNotify function for @user_data
1307  *
1308  * Create a binding between the @key in the @settings object
1309  * and the property @property of @object.
1310  *
1311  * The binding uses the provided mapping functions to map between
1312  * settings and property values.
1313  *
1314  * Note that the lifecycle of the binding is tied to the object,
1315  * and that you can have only one binding per object property.
1316  * If you bind the same property twice on the same object, the second
1317  * binding overrides the first one.
1318  *
1319  * Since: 2.26
1320  */
1321 void
1322 g_settings_bind_with_mapping (GSettings               *settings,
1323                               const gchar             *key,
1324                               gpointer                 object,
1325                               const gchar             *property,
1326                               GSettingsBindFlags       flags,
1327                               GSettingsBindGetMapping  get_mapping,
1328                               GSettingsBindSetMapping  set_mapping,
1329                               gpointer                 user_data,
1330                               GDestroyNotify           destroy)
1331 {
1332   GSettingsBinding *binding;
1333   GObjectClass *objectclass;
1334   gchar *detailed_signal;
1335   GQuark binding_quark;
1336
1337   g_return_if_fail (G_IS_SETTINGS (settings));
1338
1339   objectclass = G_OBJECT_GET_CLASS (object);
1340
1341   binding = g_slice_new0 (GSettingsBinding);
1342   binding->settings = g_object_ref (settings);
1343   binding->object = object;
1344   binding->key = g_intern_string (key);
1345   binding->property = g_object_class_find_property (objectclass, property);
1346   binding->user_data = user_data;
1347   binding->destroy = destroy;
1348   binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
1349   binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
1350
1351   if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
1352     flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
1353
1354   if (binding->property == NULL)
1355     {
1356       g_critical ("g_settings_bind: no property '%s' on class '%s'",
1357                   property, G_OBJECT_TYPE_NAME (object));
1358       return;
1359     }
1360
1361   if ((flags & G_SETTINGS_BIND_GET) && (binding->property->flags & G_PARAM_WRITABLE) == 0)
1362     {
1363       g_critical ("g_settings_bind: property '%s' on class '%s' is not writable",
1364                   property, G_OBJECT_TYPE_NAME (object));
1365       return;
1366     }
1367   if ((flags & G_SETTINGS_BIND_SET) && (binding->property->flags & G_PARAM_READABLE) == 0)
1368     {
1369       g_critical ("g_settings_bind: property '%s' on class '%s' is not readable",
1370                   property, G_OBJECT_TYPE_NAME (object));
1371       return;
1372     }
1373
1374   {
1375     GVariant *value;
1376
1377     value = g_settings_schema_get_value (settings->priv->schema, key, NULL);
1378     binding->type = g_variant_type_copy (g_variant_get_type (value));
1379     if (value)
1380       g_variant_unref (value);
1381   }
1382
1383   if (binding->type == NULL)
1384     {
1385       g_critical ("g_settings_bind: no key '%s' on schema '%s'",
1386                   key, settings->priv->schema_name);
1387       return;
1388     }
1389
1390   if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
1391        (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
1392       !g_settings_mapping_is_compatible (binding->property->value_type,
1393                                          binding->type))
1394     {
1395       g_critical ("g_settings_bind: property '%s' on class '%s' has type"
1396                   "'%s' which is not compatible with type '%s' of key '%s'"
1397                   "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
1398                   g_type_name (binding->property->value_type),
1399                   g_variant_type_dup_string (binding->type), key,
1400                   settings->priv->schema_name);
1401       return;
1402     }
1403
1404   if ((flags & G_SETTINGS_BIND_SET) &&
1405       (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
1406     {
1407       GParamSpec *sensitive;
1408
1409       sensitive = g_object_class_find_property (objectclass, "sensitive");
1410
1411       if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
1412           (sensitive->flags & G_PARAM_WRITABLE))
1413         g_settings_bind_writable (settings, binding->key,
1414                                   object, "sensitive", FALSE);
1415     }
1416
1417   if (flags & G_SETTINGS_BIND_SET)
1418     {
1419       detailed_signal = g_strdup_printf ("notify::%s", property);
1420       binding->property_handler_id =
1421         g_signal_connect (object, detailed_signal,
1422                           G_CALLBACK (g_settings_binding_property_changed),
1423                           binding);
1424       g_free (detailed_signal);
1425
1426       if (~flags & G_SETTINGS_BIND_GET)
1427         g_settings_binding_property_changed (object,
1428                                              binding->property,
1429                                              binding);
1430     }
1431
1432   if (flags & G_SETTINGS_BIND_GET)
1433     {
1434       if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
1435         {
1436           detailed_signal = g_strdup_printf ("changed::%s", key);
1437           binding->key_handler_id =
1438             g_signal_connect (settings, detailed_signal,
1439                               G_CALLBACK (g_settings_binding_key_changed),
1440                               binding);
1441           g_free (detailed_signal);
1442         }
1443
1444       g_settings_binding_key_changed (settings, binding->key, binding);
1445     }
1446
1447   binding_quark = g_settings_binding_quark (property);
1448   g_object_set_qdata_full (object, binding_quark,
1449                            binding, g_settings_binding_free);
1450 }
1451
1452 typedef struct
1453 {
1454   GSettings *settings;
1455   gpointer object;
1456   const gchar *key;
1457   const gchar *property;
1458   gboolean inverted;
1459   gulong handler_id;
1460 } GSettingsWritableBinding;
1461
1462 static void
1463 g_settings_writable_binding_free (gpointer data)
1464 {
1465   GSettingsWritableBinding *binding = data;
1466
1467   g_signal_handler_disconnect (binding->settings, binding->handler_id);
1468   g_object_unref (binding->settings);
1469   g_slice_free (GSettingsWritableBinding, binding);
1470 }
1471
1472 static void
1473 g_settings_binding_writable_changed (GSettings   *settings,
1474                                      const gchar *key,
1475                                      gpointer     user_data)
1476 {
1477   GSettingsWritableBinding *binding = user_data;
1478   gboolean writable;
1479
1480   g_assert (settings == binding->settings);
1481   g_assert (key == binding->key);
1482
1483   writable = g_settings_is_writable (settings, key);
1484
1485   if (binding->inverted)
1486     writable = !writable;
1487
1488   g_object_set (binding->object, binding->property, writable, NULL);
1489 }
1490
1491 /**
1492  * g_settings_bind_writable:
1493  * @settings: a #GSettings object
1494  * @key: the key to bind
1495  * @object: a #GObject
1496  * @property: the name of a boolean property to bind
1497  * @inverted: whether to 'invert' the value
1498  *
1499  * Create a binding between the writability of @key in the
1500  * @settings object and the property @property of @object.
1501  * The property must be boolean; "sensitive" or "visible"
1502  * properties of widgets are the most likely candidates.
1503  *
1504  * Writable bindings are always uni-directional; changes of the
1505  * writability of the setting will be propagated to the object
1506  * property, not the other way.
1507  *
1508  * When the @inverted argument is %TRUE, the binding inverts the
1509  * value as it passes from the setting to the object, i.e. @property
1510  * will be set to %TRUE if the key is <emphasis>not</emphasis>
1511  * writable.
1512  *
1513  * Note that the lifecycle of the binding is tied to the object,
1514  * and that you can have only one binding per object property.
1515  * If you bind the same property twice on the same object, the second
1516  * binding overrides the first one.
1517  *
1518  * Since: 2.26
1519  */
1520 void
1521 g_settings_bind_writable (GSettings   *settings,
1522                           const gchar *key,
1523                           gpointer     object,
1524                           const gchar *property,
1525                           gboolean     inverted)
1526 {
1527   GSettingsWritableBinding *binding;
1528   gchar *detailed_signal;
1529   GParamSpec *pspec;
1530
1531   g_return_if_fail (G_IS_SETTINGS (settings));
1532
1533   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
1534   if (pspec == NULL)
1535     {
1536       g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
1537                   property, G_OBJECT_TYPE_NAME (object));
1538       return;
1539     }
1540   if ((pspec->flags & G_PARAM_WRITABLE) == 0)
1541     {
1542       g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
1543                   property, G_OBJECT_TYPE_NAME (object));
1544       return;
1545     }
1546
1547   binding = g_slice_new (GSettingsWritableBinding);
1548   binding->settings = g_object_ref (settings);
1549   binding->object = object;
1550   binding->key = g_intern_string (key);
1551   binding->property = g_intern_string (property);
1552   binding->inverted = inverted;
1553
1554   detailed_signal = g_strdup_printf ("writable-changed::%s", key);
1555   binding->handler_id =
1556     g_signal_connect (settings, detailed_signal,
1557                       G_CALLBACK (g_settings_binding_writable_changed),
1558                       binding);
1559   g_free (detailed_signal);
1560
1561   g_object_set_qdata_full (object, g_settings_binding_quark (property),
1562                            binding, g_settings_writable_binding_free);
1563
1564   g_settings_binding_writable_changed (settings, binding->key, binding);
1565 }
1566
1567 /**
1568  * g_settings_unbind:
1569  * @object: the object
1570  * @property: the property whose binding is removed
1571  *
1572  * Removes an existing binding for @property on @object.
1573  *
1574  * Note that bindings are automatically removed when the
1575  * object is finalized, so it is rarely necessary to call this
1576  * function.
1577  *
1578  * Since: 2.26
1579  */
1580 void
1581 g_settings_unbind (gpointer     object,
1582                    const gchar *property)
1583 {
1584   GQuark binding_quark;
1585
1586   binding_quark = g_settings_binding_quark (property);
1587   g_object_set_qdata (object, binding_quark, NULL);
1588 }
1589
1590 /**
1591  * g_settings_get_string:
1592  * @settings: a #GSettings object
1593  * @key: the key to get the value for
1594  * @returns: a newly-allocated string
1595  *
1596  * Gets the value that is stored at @key in @settings.
1597  *
1598  * A convenience variant of g_settings_get() for strings.
1599  *
1600  * It is a programmer error to pass a @key that isn't valid for
1601  * @settings or is not of type string.
1602  *
1603  * Since: 2.26
1604  */
1605 gchar *
1606 g_settings_get_string (GSettings   *settings,
1607                        const gchar *key)
1608 {
1609   GVariant *value;
1610   gchar *result;
1611
1612   value = g_settings_get_value (settings, key);
1613   result = g_variant_dup_string (value, NULL);
1614   g_variant_unref (value);
1615
1616   return result;
1617 }
1618
1619 /**
1620  * g_settings_set_string:
1621  * @settings: a #GSettings object
1622  * @key: the name of the key to set
1623  * @value: the value to set it to
1624  * @returns: %TRUE if setting the key succeeded,
1625  *     %FALSE if the key was not writable
1626  *
1627  * Sets @key in @settings to @value.
1628  *
1629  * A convenience variant of g_settings_set() for strings.
1630  *
1631  * It is a programmer error to pass a @key that isn't valid for
1632  * @settings or is not of type string.
1633  *
1634  * Since: 2.26
1635  */
1636 gboolean
1637 g_settings_set_string (GSettings   *settings,
1638                        const gchar *key,
1639                        const gchar *value)
1640 {
1641   return g_settings_set_value (settings, key, g_variant_new_string (value));
1642 }
1643
1644 /**
1645  * g_settings_get_int:
1646  * @settings: a #GSettings object
1647  * @key: the key to get the value for
1648  * @returns: an integer
1649  *
1650  * Gets the value that is stored at @key in @settings.
1651  *
1652  * A convenience variant of g_settings_get() for 32-bit integers.
1653  *
1654  * It is a programmer error to pass a @key that isn't valid for
1655  * @settings or is not of type int32.
1656  *
1657  * Since: 2.26
1658  */
1659 gint
1660 g_settings_get_int (GSettings   *settings,
1661                     const gchar *key)
1662 {
1663   GVariant *value;
1664   gint result;
1665
1666   value = g_settings_get_value (settings, key);
1667   result = g_variant_get_int32 (value);
1668   g_variant_unref (value);
1669
1670   return result;
1671 }
1672
1673 /**
1674  * g_settings_set_int:
1675  * @settings: a #GSettings object
1676  * @key: the name of the key to set
1677  * @value: the value to set it to
1678  * @returns: %TRUE if setting the key succeeded,
1679  *     %FALSE if the key was not writable
1680  *
1681  * Sets @key in @settings to @value.
1682  *
1683  * A convenience variant of g_settings_set() for 32-bit integers.
1684  *
1685  * It is a programmer error to pass a @key that isn't valid for
1686  * @settings or is not of type int32.
1687  *
1688  * Since: 2.26
1689  */
1690 gboolean
1691 g_settings_set_int (GSettings   *settings,
1692                     const gchar *key,
1693                     gint         value)
1694 {
1695   return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1696 }
1697
1698 /**
1699  * g_settings_get_double:
1700  * @settings: a #GSettings object
1701  * @key: the key to get the value for
1702  * @returns: a double
1703  *
1704  * Gets the value that is stored at @key in @settings.
1705  *
1706  * A convenience variant of g_settings_get() for doubles.
1707  *
1708  * It is a programmer error to pass a @key that isn't valid for
1709  * @settings or is not of type double.
1710  *
1711  * Since: 2.26
1712  */
1713 gdouble
1714 g_settings_get_double (GSettings   *settings,
1715                        const gchar *key)
1716 {
1717   GVariant *value;
1718   gdouble result;
1719
1720   value = g_settings_get_value (settings, key);
1721   result = g_variant_get_double (value);
1722   g_variant_unref (value);
1723
1724   return result;
1725 }
1726
1727 /**
1728  * g_settings_set_double:
1729  * @settings: a #GSettings object
1730  * @key: the name of the key to set
1731  * @value: the value to set it to
1732  * @returns: %TRUE if setting the key succeeded,
1733  *     %FALSE if the key was not writable
1734  *
1735  * Sets @key in @settings to @value.
1736  *
1737  * A convenience variant of g_settings_set() for doubles.
1738  *
1739  * It is a programmer error to pass a @key that isn't valid for
1740  * @settings or is not of type double.
1741  *
1742  * Since: 2.26
1743  */
1744 gboolean
1745 g_settings_set_double (GSettings   *settings,
1746                        const gchar *key,
1747                        gdouble      value)
1748 {
1749   return g_settings_set_value (settings, key, g_variant_new_double (value));
1750 }
1751
1752 /**
1753  * g_settings_get_boolean:
1754  * @settings: a #GSettings object
1755  * @key: the key to get the value for
1756  * @returns: a boolean
1757  *
1758  * Gets the value that is stored at @key in @settings.
1759  *
1760  * A convenience variant of g_settings_get() for booleans.
1761  *
1762  * It is a programmer error to pass a @key that isn't valid for
1763  * @settings or is not of type boolean.
1764  *
1765  * Since: 2.26
1766  */
1767 gboolean
1768 g_settings_get_boolean (GSettings  *settings,
1769                        const gchar *key)
1770 {
1771   GVariant *value;
1772   gboolean result;
1773
1774   value = g_settings_get_value (settings, key);
1775   result = g_variant_get_boolean (value);
1776   g_variant_unref (value);
1777
1778   return result;
1779 }
1780
1781 /**
1782  * g_settings_set_boolean:
1783  * @settings: a #GSettings object
1784  * @key: the name of the key to set
1785  * @value: the value to set it to
1786  * @returns: %TRUE if setting the key succeeded,
1787  *     %FALSE if the key was not writable
1788  *
1789  * Sets @key in @settings to @value.
1790  *
1791  * A convenience variant of g_settings_set() for booleans.
1792  *
1793  * It is a programmer error to pass a @key that isn't valid for
1794  * @settings or is not of type boolean.
1795  *
1796  * Since: 2.26
1797  */
1798 gboolean
1799 g_settings_set_boolean (GSettings  *settings,
1800                        const gchar *key,
1801                        gboolean     value)
1802 {
1803   return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1804 }
1805
1806 /**
1807  * g_settings_get_strv:
1808  * @settings: a #GSettings object
1809  * @key: the key to get the value for
1810  * @length: return location for the length of the result, or %NULL
1811  * @returns: a newly-allocated, %NULL-terminated array of strings
1812  *
1813  * Gets the value that is stored at @key in @settings.
1814  *
1815  * A convenience variant of g_settings_get() for string arrays.
1816  *
1817  * It is a programmer error to pass a @key that isn't valid for
1818  * @settings or is not of type 'string array'.
1819  *
1820  * Since: 2.26
1821  */
1822 gchar **
1823 g_settings_get_strv (GSettings   *settings,
1824                      const gchar *key,
1825                      gsize       *length)
1826 {
1827   GVariant *value;
1828   gchar **result;
1829
1830   value = g_settings_get_value (settings, key);
1831   result = g_variant_dup_strv (value, length);
1832   g_variant_unref (value);
1833
1834   return result;
1835 }
1836
1837 /**
1838  * g_settings_set_strv:
1839  * @settings: a #GSettings object
1840  * @key: the name of the key to set
1841  * @value: the value to set it to
1842  * @length: the length of the @value array, or -1
1843  * @returns: %TRUE if setting the key succeeded,
1844  *     %FALSE if the key was not writable
1845  *
1846  * Sets @key in @settings to @value.
1847  *
1848  * A convenience variant of g_settings_set() for string arrays.
1849  *
1850  * It is a programmer error to pass a @key that isn't valid for
1851  * @settings or is not of type 'string array'.
1852  *
1853  * Since: 2.26
1854  */
1855 gboolean
1856 g_settings_set_strv (GSettings           *settings,
1857                      const gchar         *key,
1858                      const gchar * const *value,
1859                      gssize               length)
1860 {
1861   return g_settings_set_value (settings, key, g_variant_new_strv (value, length));
1862 }
1863
1864 #define __G_SETTINGS_C__
1865 #include "gioaliasdef.c"