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