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