Coverage Report

Created: 2025-06-13 06:21

/src/glib/gio/gsettingsschema.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright © 2010 Codethink Limited
3
 * Copyright © 2011 Canonical Limited
4
 *
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "config.h"
22
23
#include "glib-private.h"
24
#include "gsettingsschema-internal.h"
25
#include "gsettings.h"
26
27
#include "gvdb/gvdb-reader.h"
28
#include "strinfo.c"
29
30
#include <glibintl.h>
31
#include <locale.h>
32
#include <string.h>
33
#include <stdlib.h>
34
35
#ifdef HAVE_XLOCALE_H
36
/* Needed on macOS and FreeBSD for uselocale() */
37
#include <xlocale.h>
38
#endif
39
40
/**
41
 * GSettingsSchema:
42
 *
43
 * The [struct@Gio.SettingsSchemaSource] and `GSettingsSchema` APIs provide a
44
 * mechanism for advanced control over the loading of schemas and a
45
 * mechanism for introspecting their content.
46
 *
47
 * Plugin loading systems that wish to provide plugins a way to access
48
 * settings face the problem of how to make the schemas for these
49
 * settings visible to GSettings.  Typically, a plugin will want to ship
50
 * the schema along with itself and it won't be installed into the
51
 * standard system directories for schemas.
52
 *
53
 * [struct@Gio.SettingsSchemaSource] provides a mechanism for dealing with this
54
 * by allowing the creation of a new ‘schema source’ from which schemas can
55
 * be acquired.  This schema source can then become part of the metadata
56
 * associated with the plugin and queried whenever the plugin requires
57
 * access to some settings.
58
 *
59
 * Consider the following example:
60
 *
61
 * ```c
62
 * typedef struct
63
 * {
64
 *    …
65
 *    GSettingsSchemaSource *schema_source;
66
 *    …
67
 * } Plugin;
68
 *
69
 * Plugin *
70
 * initialise_plugin (const gchar *dir)
71
 * {
72
 *   Plugin *plugin;
73
 *
74
 *   …
75
 *
76
 *   plugin->schema_source =
77
 *     g_settings_schema_source_new_from_directory (dir,
78
 *       g_settings_schema_source_get_default (), FALSE, NULL);
79
 *
80
 *   …
81
 *
82
 *   return plugin;
83
 * }
84
 *
85
 * …
86
 *
87
 * GSettings *
88
 * plugin_get_settings (Plugin      *plugin,
89
 *                      const gchar *schema_id)
90
 * {
91
 *   GSettingsSchema *schema;
92
 *
93
 *   if (schema_id == NULL)
94
 *     schema_id = plugin->identifier;
95
 *
96
 *   schema = g_settings_schema_source_lookup (plugin->schema_source,
97
 *                                             schema_id, FALSE);
98
 *
99
 *   if (schema == NULL)
100
 *     {
101
 *       … disable the plugin or abort, etc …
102
 *     }
103
 *
104
 *   return g_settings_new_full (schema, NULL, NULL);
105
 * }
106
 * ```
107
 *
108
 * The code above shows how hooks should be added to the code that
109
 * initialises (or enables) the plugin to create the schema source and
110
 * how an API can be added to the plugin system to provide a convenient
111
 * way for the plugin to access its settings, using the schemas that it
112
 * ships.
113
 *
114
 * From the standpoint of the plugin, it would need to ensure that it
115
 * ships a gschemas.compiled file as part of itself, and then simply do
116
 * the following:
117
 *
118
 * ```c
119
 * {
120
 *   GSettings *settings;
121
 *   gint some_value;
122
 *
123
 *   settings = plugin_get_settings (self, NULL);
124
 *   some_value = g_settings_get_int (settings, "some-value");
125
 *   …
126
 * }
127
 * ```
128
 *
129
 * It's also possible that the plugin system expects the schema source
130
 * files (ie: `.gschema.xml` files) instead of a `gschemas.compiled` file.
131
 * In that case, the plugin loading system must compile the schemas for
132
 * itself before attempting to create the settings source.
133
 *
134
 * Since: 2.32
135
 **/
136
137
/**
138
 * GSettingsSchemaKey:
139
 *
140
 * #GSettingsSchemaKey is an opaque data structure and can only be accessed
141
 * using the following functions.
142
 **/
143
144
struct _GSettingsSchema
145
{
146
  GSettingsSchemaSource *source;
147
  const gchar *gettext_domain;
148
  const gchar *path;
149
  GQuark *items;
150
  gint n_items;
151
  GvdbTable *table;
152
  gchar *id;
153
154
  GSettingsSchema *extends;
155
156
  gint ref_count;
157
};
158
159
/**
160
 * G_TYPE_SETTINGS_SCHEMA_SOURCE:
161
 *
162
 * A boxed #GType corresponding to #GSettingsSchemaSource.
163
 *
164
 * Since: 2.32
165
 **/
166
G_DEFINE_BOXED_TYPE (GSettingsSchemaSource, g_settings_schema_source, g_settings_schema_source_ref, g_settings_schema_source_unref)
167
168
/**
169
 * G_TYPE_SETTINGS_SCHEMA:
170
 *
171
 * A boxed #GType corresponding to #GSettingsSchema.
172
 *
173
 * Since: 2.32
174
 **/
175
G_DEFINE_BOXED_TYPE (GSettingsSchema, g_settings_schema, g_settings_schema_ref, g_settings_schema_unref)
176
177
/**
178
 * GSettingsSchemaSource:
179
 *
180
 * This is an opaque structure type.  You may not access it directly.
181
 *
182
 * Since: 2.32
183
 **/
184
struct _GSettingsSchemaSource
185
{
186
  GSettingsSchemaSource *parent;
187
  gchar *directory;
188
  GvdbTable *table;
189
  GHashTable **text_tables;
190
191
  gint ref_count;
192
};
193
194
static GSettingsSchemaSource *schema_sources;
195
196
/**
197
 * g_settings_schema_source_ref:
198
 * @source: a #GSettingsSchemaSource
199
 *
200
 * Increase the reference count of @source, returning a new reference.
201
 *
202
 * Returns: (transfer full) (not nullable): a new reference to @source
203
 *
204
 * Since: 2.32
205
 **/
206
GSettingsSchemaSource *
207
g_settings_schema_source_ref (GSettingsSchemaSource *source)
208
0
{
209
0
  g_atomic_int_inc (&source->ref_count);
210
211
0
  return source;
212
0
}
213
214
/**
215
 * g_settings_schema_source_unref:
216
 * @source: a #GSettingsSchemaSource
217
 *
218
 * Decrease the reference count of @source, possibly freeing it.
219
 *
220
 * Since: 2.32
221
 **/
222
void
223
g_settings_schema_source_unref (GSettingsSchemaSource *source)
224
0
{
225
0
  if (g_atomic_int_dec_and_test (&source->ref_count))
226
0
    {
227
0
      if (source == schema_sources)
228
0
        g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
229
230
0
      if (source->parent)
231
0
        g_settings_schema_source_unref (source->parent);
232
0
      gvdb_table_free (source->table);
233
0
      g_free (source->directory);
234
235
0
      if (source->text_tables)
236
0
        {
237
0
          g_hash_table_unref (source->text_tables[0]);
238
0
          g_hash_table_unref (source->text_tables[1]);
239
0
          g_free (source->text_tables);
240
0
        }
241
242
0
      g_slice_free (GSettingsSchemaSource, source);
243
0
    }
244
0
}
245
246
/**
247
 * g_settings_schema_source_new_from_directory:
248
 * @directory: (type filename): the filename of a directory
249
 * @parent: (nullable): a #GSettingsSchemaSource, or %NULL
250
 * @trusted: %TRUE, if the directory is trusted
251
 * @error: a pointer to a #GError pointer set to %NULL, or %NULL
252
 *
253
 * Attempts to create a new schema source corresponding to the contents
254
 * of the given directory.
255
 *
256
 * This function is not required for normal uses of #GSettings but it
257
 * may be useful to authors of plugin management systems.
258
 *
259
 * The directory should contain a file called `gschemas.compiled` as
260
 * produced by the [glib-compile-schemas][glib-compile-schemas] tool.
261
 *
262
 * If @trusted is %TRUE then `gschemas.compiled` is trusted not to be
263
 * corrupted. This assumption has a performance advantage, but can result
264
 * in crashes or inconsistent behaviour in the case of a corrupted file.
265
 * Generally, you should set @trusted to %TRUE for files installed by the
266
 * system and to %FALSE for files in the home directory.
267
 *
268
 * In either case, an empty file or some types of corruption in the file will
269
 * result in %G_FILE_ERROR_INVAL being returned.
270
 *
271
 * If @parent is non-%NULL then there are two effects.
272
 *
273
 * First, if g_settings_schema_source_lookup() is called with the
274
 * @recursive flag set to %TRUE and the schema can not be found in the
275
 * source, the lookup will recurse to the parent.
276
 *
277
 * Second, any references to other schemas specified within this
278
 * source (ie: `child` or `extends`) references may be resolved
279
 * from the @parent.
280
 *
281
 * For this second reason, except in very unusual situations, the
282
 * @parent should probably be given as the default schema source, as
283
 * returned by g_settings_schema_source_get_default().
284
 *
285
 * Since: 2.32
286
 **/
287
GSettingsSchemaSource *
288
g_settings_schema_source_new_from_directory (const gchar            *directory,
289
                                             GSettingsSchemaSource  *parent,
290
                                             gboolean                trusted,
291
                                             GError                **error)
292
0
{
293
0
  GSettingsSchemaSource *source;
294
0
  GvdbTable *table;
295
0
  gchar *filename;
296
297
0
  filename = g_build_filename (directory, "gschemas.compiled", NULL);
298
0
  table = gvdb_table_new (filename, trusted, error);
299
0
  g_free (filename);
300
301
0
  if (table == NULL)
302
0
    return NULL;
303
304
0
  source = g_slice_new (GSettingsSchemaSource);
305
0
  source->directory = g_strdup (directory);
306
0
  source->parent = parent ? g_settings_schema_source_ref (parent) : NULL;
307
0
  source->text_tables = NULL;
308
0
  source->table = table;
309
0
  source->ref_count = 1;
310
311
0
  return source;
312
0
}
313
314
static void
315
try_prepend_dir (const gchar *directory)
316
0
{
317
0
  GSettingsSchemaSource *source;
318
319
0
  source = g_settings_schema_source_new_from_directory (directory, schema_sources, TRUE, NULL);
320
321
  /* If we successfully created it then prepend it to the global list */
322
0
  if (source != NULL)
323
0
    schema_sources = source;
324
0
}
325
326
static void
327
try_prepend_data_dir (const gchar *directory)
328
0
{
329
0
  gchar *dirname = g_build_filename (directory, "glib-2.0", "schemas", NULL);
330
0
  try_prepend_dir (dirname);
331
0
  g_free (dirname);
332
0
}
333
334
static void
335
initialise_schema_sources (void)
336
0
{
337
0
  static gsize initialised;
338
339
  /* need a separate variable because 'schema_sources' may legitimately
340
   * be null if we have zero valid schema sources
341
   */
342
0
  if G_UNLIKELY (g_once_init_enter (&initialised))
343
0
    {
344
0
      gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
345
0
      const gchar * const *dirs;
346
0
      const gchar *path;
347
0
      gchar **extra_schema_dirs;
348
0
      gint i;
349
350
      /* iterate in reverse: count up, then count down */
351
0
      dirs = g_get_system_data_dirs ();
352
0
      for (i = 0; dirs[i]; i++);
353
354
0
      while (i--)
355
0
        try_prepend_data_dir (dirs[i]);
356
357
0
      try_prepend_data_dir (g_get_user_data_dir ());
358
359
      /* Disallow loading extra schemas if running as setuid, as that could
360
       * allow reading privileged files. */
361
0
      if (!is_setuid && (path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
362
0
        {
363
0
          extra_schema_dirs = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 0);
364
0
          for (i = 0; extra_schema_dirs[i]; i++);
365
366
0
          while (i--)
367
0
            try_prepend_dir (extra_schema_dirs[i]);
368
369
0
          g_strfreev (extra_schema_dirs);
370
0
        }
371
372
0
      g_once_init_leave (&initialised, TRUE);
373
0
    }
374
0
}
375
376
/**
377
 * g_settings_schema_source_get_default:
378
 *
379
 * Gets the default system schema source.
380
 *
381
 * This function is not required for normal uses of #GSettings but it
382
 * may be useful to authors of plugin management systems or to those who
383
 * want to introspect the content of schemas.
384
 *
385
 * If no schemas are installed, %NULL will be returned.
386
 *
387
 * The returned source may actually consist of multiple schema sources
388
 * from different directories, depending on which directories were given
389
 * in `XDG_DATA_DIRS` and `GSETTINGS_SCHEMA_DIR`. For this reason, all
390
 * lookups performed against the default source should probably be done
391
 * recursively.
392
 *
393
 * Returns: (transfer none) (nullable): the default schema source
394
 *
395
 * Since: 2.32
396
 **/
397
GSettingsSchemaSource *
398
g_settings_schema_source_get_default (void)
399
0
{
400
0
  initialise_schema_sources ();
401
402
0
  return schema_sources;
403
0
}
404
405
/**
406
 * g_settings_schema_source_lookup:
407
 * @source: a #GSettingsSchemaSource
408
 * @schema_id: a schema ID
409
 * @recursive: %TRUE if the lookup should be recursive
410
 *
411
 * Looks up a schema with the identifier @schema_id in @source.
412
 *
413
 * This function is not required for normal uses of #GSettings but it
414
 * may be useful to authors of plugin management systems or to those who
415
 * want to introspect the content of schemas.
416
 *
417
 * If the schema isn't found directly in @source and @recursive is %TRUE
418
 * then the parent sources will also be checked.
419
 *
420
 * If the schema isn't found, %NULL is returned.
421
 *
422
 * Returns: (nullable) (transfer full): a new #GSettingsSchema
423
 *
424
 * Since: 2.32
425
 **/
426
GSettingsSchema *
427
g_settings_schema_source_lookup (GSettingsSchemaSource *source,
428
                                 const gchar           *schema_id,
429
                                 gboolean               recursive)
430
0
{
431
0
  GSettingsSchema *schema;
432
0
  GvdbTable *table;
433
0
  const gchar *extends;
434
435
0
  g_return_val_if_fail (source != NULL, NULL);
436
0
  g_return_val_if_fail (schema_id != NULL, NULL);
437
438
0
  table = gvdb_table_get_table (source->table, schema_id);
439
440
0
  if (table == NULL && recursive)
441
0
    for (source = source->parent; source; source = source->parent)
442
0
      if ((table = gvdb_table_get_table (source->table, schema_id)))
443
0
        break;
444
445
0
  if (table == NULL)
446
0
    return NULL;
447
448
0
  schema = g_slice_new0 (GSettingsSchema);
449
0
  schema->source = g_settings_schema_source_ref (source);
450
0
  schema->ref_count = 1;
451
0
  schema->id = g_strdup (schema_id);
452
0
  schema->table = table;
453
0
  schema->path = g_settings_schema_get_string (schema, ".path");
454
0
  schema->gettext_domain = g_settings_schema_get_string (schema, ".gettext-domain");
455
456
0
  if (schema->gettext_domain)
457
0
    bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
458
459
0
  extends = g_settings_schema_get_string (schema, ".extends");
460
0
  if (extends)
461
0
    {
462
0
      schema->extends = g_settings_schema_source_lookup (source, extends, TRUE);
463
0
      if (schema->extends == NULL)
464
0
        g_warning ("Schema '%s' extends schema '%s' but we could not find it", schema_id, extends);
465
0
    }
466
467
0
  return schema;
468
0
}
469
470
typedef struct
471
{
472
  GHashTable *summaries;
473
  GHashTable *descriptions;
474
  GSList     *gettext_domain;
475
  GSList     *schema_id;
476
  GSList     *key_name;
477
  GString    *string;
478
} TextTableParseInfo;
479
480
static const gchar *
481
get_attribute_value (GSList *list)
482
0
{
483
0
  GSList *node;
484
485
0
  for (node = list; node; node = node->next)
486
0
    if (node->data)
487
0
      return node->data;
488
489
0
  return NULL;
490
0
}
491
492
static void
493
pop_attribute_value (GSList **list)
494
0
{
495
0
  gchar *top;
496
497
0
  top = (*list)->data;
498
0
  *list = g_slist_remove (*list, top);
499
500
0
  g_free (top);
501
0
}
502
503
static void
504
push_attribute_value (GSList      **list,
505
                      const gchar  *value)
506
0
{
507
0
  *list = g_slist_prepend (*list, g_strdup (value));
508
0
}
509
510
static void
511
start_element (GMarkupParseContext  *context,
512
               const gchar          *element_name,
513
               const gchar         **attribute_names,
514
               const gchar         **attribute_values,
515
               gpointer              user_data,
516
               GError              **error)
517
0
{
518
0
  TextTableParseInfo *info = user_data;
519
0
  const gchar *gettext_domain = NULL;
520
0
  const gchar *schema_id = NULL;
521
0
  const gchar *key_name = NULL;
522
0
  gint i;
523
524
0
  for (i = 0; attribute_names[i]; i++)
525
0
    {
526
0
      if (g_str_equal (attribute_names[i], "gettext-domain"))
527
0
        gettext_domain = attribute_values[i];
528
0
      else if (g_str_equal (attribute_names[i], "id"))
529
0
        schema_id = attribute_values[i];
530
0
      else if (g_str_equal (attribute_names[i], "name"))
531
0
        key_name = attribute_values[i];
532
0
    }
533
534
0
  push_attribute_value (&info->gettext_domain, gettext_domain);
535
0
  push_attribute_value (&info->schema_id, schema_id);
536
0
  push_attribute_value (&info->key_name, key_name);
537
538
0
  if (info->string)
539
0
    {
540
0
      g_string_free (info->string, TRUE);
541
0
      info->string = NULL;
542
0
    }
543
544
0
  if (g_str_equal (element_name, "summary") || g_str_equal (element_name, "description"))
545
0
    info->string = g_string_new (NULL);
546
0
}
547
548
static gchar *
549
normalise_whitespace (const gchar *orig)
550
0
{
551
  /* We normalise by the same rules as in intltool:
552
   *
553
   *   sub cleanup {
554
   *       s/^\s+//;
555
   *       s/\s+$//;
556
   *       s/\s+/ /g;
557
   *       return $_;
558
   *   }
559
   *
560
   *   $message = join "\n\n", map &cleanup, split/\n\s*\n+/, $message;
561
   *
562
   * Where \s is an ascii space character.
563
   *
564
   * We aim for ease of implementation over efficiency -- this code is
565
   * not run in normal applications.
566
   */
567
0
  static GRegex *cleanup[3];
568
0
  static GRegex *splitter;
569
0
  gchar **lines;
570
0
  gchar *result;
571
0
  gint i;
572
573
0
  if (g_once_init_enter_pointer (&splitter))
574
0
    {
575
0
      GRegex *s;
576
577
0
      cleanup[0] = g_regex_new ("^\\s+", G_REGEX_DEFAULT,
578
0
                                G_REGEX_MATCH_DEFAULT, NULL);
579
0
      cleanup[1] = g_regex_new ("\\s+$", G_REGEX_DEFAULT,
580
0
                                G_REGEX_MATCH_DEFAULT, NULL);
581
0
      cleanup[2] = g_regex_new ("\\s+", G_REGEX_DEFAULT,
582
0
                                G_REGEX_MATCH_DEFAULT, NULL);
583
0
      s = g_regex_new ("\\n\\s*\\n+", G_REGEX_DEFAULT,
584
0
                       G_REGEX_MATCH_DEFAULT, NULL);
585
586
0
      g_once_init_leave_pointer (&splitter, s);
587
0
    }
588
589
0
  lines = g_regex_split (splitter, orig, 0);
590
0
  for (i = 0; lines[i]; i++)
591
0
    {
592
0
      gchar *a, *b, *c;
593
594
0
      a = g_regex_replace_literal (cleanup[0], lines[i], -1, 0, "", 0, 0);
595
0
      b = g_regex_replace_literal (cleanup[1], a, -1, 0, "", 0, 0);
596
0
      c = g_regex_replace_literal (cleanup[2], b, -1, 0, " ", 0, 0);
597
0
      g_free (lines[i]);
598
0
      g_free (a);
599
0
      g_free (b);
600
0
      lines[i] = c;
601
0
    }
602
603
0
  result = g_strjoinv ("\n\n", lines);
604
0
  g_strfreev (lines);
605
606
0
  return result;
607
0
}
608
609
static void
610
end_element (GMarkupParseContext *context,
611
             const gchar *element_name,
612
             gpointer user_data,
613
             GError **error)
614
0
{
615
0
  TextTableParseInfo *info = user_data;
616
617
0
  pop_attribute_value (&info->gettext_domain);
618
0
  pop_attribute_value (&info->schema_id);
619
0
  pop_attribute_value (&info->key_name);
620
621
0
  if (info->string)
622
0
    {
623
0
      GHashTable *source_table = NULL;
624
0
      const gchar *gettext_domain;
625
0
      const gchar *schema_id;
626
0
      const gchar *key_name;
627
628
0
      gettext_domain = get_attribute_value (info->gettext_domain);
629
0
      schema_id = get_attribute_value (info->schema_id);
630
0
      key_name = get_attribute_value (info->key_name);
631
632
0
      if (g_str_equal (element_name, "summary"))
633
0
        source_table = info->summaries;
634
0
      else if (g_str_equal (element_name, "description"))
635
0
        source_table = info->descriptions;
636
637
0
      if (source_table && schema_id && key_name)
638
0
        {
639
0
          GHashTable *schema_table;
640
0
          gchar *normalised;
641
642
0
          schema_table = g_hash_table_lookup (source_table, schema_id);
643
644
0
          if (schema_table == NULL)
645
0
            {
646
0
              schema_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
647
0
              g_hash_table_insert (source_table, g_strdup (schema_id), schema_table);
648
0
            }
649
650
0
          normalised = normalise_whitespace (info->string->str);
651
652
0
          if (gettext_domain && normalised[0])
653
0
            {
654
0
              gchar *translated;
655
656
0
              translated = g_strdup (g_dgettext (gettext_domain, normalised));
657
0
              g_free (normalised);
658
0
              normalised = translated;
659
0
            }
660
661
0
          g_hash_table_insert (schema_table, g_strdup (key_name), normalised);
662
0
        }
663
664
0
      g_string_free (info->string, TRUE);
665
0
      info->string = NULL;
666
0
    }
667
0
}
668
669
static void
670
text (GMarkupParseContext  *context,
671
      const gchar          *text,
672
      gsize                 text_len,
673
      gpointer              user_data,
674
      GError              **error)
675
0
{
676
0
  TextTableParseInfo *info = user_data;
677
678
0
  if (info->string)
679
0
    g_string_append_len (info->string, text, text_len);
680
0
}
681
682
static void
683
parse_into_text_tables (const gchar *directory,
684
                        GHashTable  *summaries,
685
                        GHashTable  *descriptions)
686
0
{
687
0
  GMarkupParser parser = { start_element, end_element, text, NULL, NULL };
688
0
  TextTableParseInfo info = { summaries, descriptions, NULL, NULL, NULL, NULL };
689
0
  const gchar *basename;
690
0
  GDir *dir;
691
692
0
  dir = g_dir_open (directory, 0, NULL);
693
0
  while ((basename = g_dir_read_name (dir)))
694
0
    {
695
0
      gchar *filename;
696
0
      gchar *contents;
697
0
      gsize size;
698
699
0
      filename = g_build_filename (directory, basename, NULL);
700
0
      if (g_file_get_contents (filename, &contents, &size, NULL))
701
0
        {
702
0
          GMarkupParseContext *context;
703
704
0
          context = g_markup_parse_context_new (&parser, G_MARKUP_TREAT_CDATA_AS_TEXT, &info, NULL);
705
          /* Ignore errors here, this is best effort only. */
706
0
          if (g_markup_parse_context_parse (context, contents, size, NULL))
707
0
            (void) g_markup_parse_context_end_parse (context, NULL);
708
0
          g_markup_parse_context_free (context);
709
710
          /* Clean up dangling stuff in case there was an error. */
711
0
          g_slist_free_full (info.gettext_domain, g_free);
712
0
          g_slist_free_full (info.schema_id, g_free);
713
0
          g_slist_free_full (info.key_name, g_free);
714
715
0
          info.gettext_domain = NULL;
716
0
          info.schema_id = NULL;
717
0
          info.key_name = NULL;
718
719
0
          if (info.string)
720
0
            {
721
0
              g_string_free (info.string, TRUE);
722
0
              info.string = NULL;
723
0
            }
724
725
0
          g_free (contents);
726
0
        }
727
728
0
      g_free (filename);
729
0
    }
730
  
731
0
  g_dir_close (dir);
732
0
}
733
734
static GHashTable **
735
g_settings_schema_source_get_text_tables (GSettingsSchemaSource *source)
736
0
{
737
0
  if (g_once_init_enter_pointer (&source->text_tables))
738
0
    {
739
0
      GHashTable **text_tables;
740
741
0
      text_tables = g_new (GHashTable *, 2);
742
0
      text_tables[0] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_unref);
743
0
      text_tables[1] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_unref);
744
745
0
      if (source->directory)
746
0
        parse_into_text_tables (source->directory, text_tables[0], text_tables[1]);
747
748
0
      g_once_init_leave_pointer (&source->text_tables, text_tables);
749
0
    }
750
751
0
  return source->text_tables;
752
0
}
753
754
/**
755
 * g_settings_schema_source_list_schemas:
756
 * @source: a #GSettingsSchemaSource
757
 * @recursive: if we should recurse
758
 * @non_relocatable: (out) (transfer full) (array zero-terminated=1): the
759
 *   list of non-relocatable schemas, in no defined order
760
 * @relocatable: (out) (transfer full) (array zero-terminated=1): the list
761
 *   of relocatable schemas, in no defined order
762
 *
763
 * Lists the schemas in a given source.
764
 *
765
 * If @recursive is %TRUE then include parent sources.  If %FALSE then
766
 * only include the schemas from one source (ie: one directory).  You
767
 * probably want %TRUE.
768
 *
769
 * Non-relocatable schemas are those for which you can call
770
 * g_settings_new().  Relocatable schemas are those for which you must
771
 * use g_settings_new_with_path().
772
 *
773
 * Do not call this function from normal programs.  This is designed for
774
 * use by database editors, commandline tools, etc.
775
 *
776
 * Since: 2.40
777
 **/
778
void
779
g_settings_schema_source_list_schemas (GSettingsSchemaSource   *source,
780
                                       gboolean                 recursive,
781
                                       gchar                 ***non_relocatable,
782
                                       gchar                 ***relocatable)
783
0
{
784
0
  GHashTable *single, *reloc;
785
0
  GSettingsSchemaSource *s;
786
787
  /* We use hash tables to avoid duplicate listings for schemas that
788
   * appear in more than one file.
789
   */
790
0
  single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
791
0
  reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
792
793
0
  for (s = source; s; s = s->parent)
794
0
    {
795
0
      gchar **list;
796
0
      gint i;
797
798
0
      list = gvdb_table_list (s->table, "");
799
800
      /* empty schema cache file? */
801
0
      if (list == NULL)
802
0
        continue;
803
804
0
      for (i = 0; list[i]; i++)
805
0
        {
806
0
          if (!g_hash_table_contains (single, list[i]) &&
807
0
              !g_hash_table_contains (reloc, list[i]))
808
0
            {
809
0
              gchar *schema;
810
0
              GvdbTable *table;
811
812
0
              schema = g_strdup (list[i]);
813
814
0
              table = gvdb_table_get_table (s->table, list[i]);
815
0
              g_assert (table != NULL);
816
817
0
              if (gvdb_table_has_value (table, ".path"))
818
0
                g_hash_table_add (single, schema);
819
0
              else
820
0
                g_hash_table_add (reloc, schema);
821
822
0
              gvdb_table_free (table);
823
0
            }
824
0
        }
825
826
0
      g_strfreev (list);
827
828
      /* Only the first source if recursive not requested */
829
0
      if (!recursive)
830
0
        break;
831
0
    }
832
833
0
  if (non_relocatable)
834
0
    {
835
0
      *non_relocatable = (gchar **) g_hash_table_get_keys_as_array (single, NULL);
836
0
      g_hash_table_steal_all (single);
837
0
    }
838
839
0
  if (relocatable)
840
0
    {
841
0
      *relocatable = (gchar **) g_hash_table_get_keys_as_array (reloc, NULL);
842
0
      g_hash_table_steal_all (reloc);
843
0
    }
844
845
0
  g_hash_table_unref (single);
846
0
  g_hash_table_unref (reloc);
847
0
}
848
849
static gchar **non_relocatable_schema_list;
850
static gchar **relocatable_schema_list;
851
static gsize schema_lists_initialised;
852
853
static void
854
ensure_schema_lists (void)
855
0
{
856
0
  if (g_once_init_enter (&schema_lists_initialised))
857
0
    {
858
0
      initialise_schema_sources ();
859
860
0
      g_settings_schema_source_list_schemas (schema_sources, TRUE,
861
0
                                             &non_relocatable_schema_list,
862
0
                                             &relocatable_schema_list);
863
864
0
      g_once_init_leave (&schema_lists_initialised, TRUE);
865
0
    }
866
0
}
867
868
/**
869
 * g_settings_list_schemas:
870
 *
871
 * Deprecated.
872
 *
873
 * Returns: (element-type utf8) (transfer none) (not nullable): a list of
874
 *   #GSettings schemas that are available, in no defined order.  The list
875
 *   must not be modified or freed.
876
 *
877
 * Since: 2.26
878
 *
879
 * Deprecated: 2.40: Use g_settings_schema_source_list_schemas() instead.
880
 * If you used g_settings_list_schemas() to check for the presence of
881
 * a particular schema, use g_settings_schema_source_lookup() instead
882
 * of your whole loop.
883
 **/
884
const gchar * const *
885
g_settings_list_schemas (void)
886
0
{
887
0
  ensure_schema_lists ();
888
889
0
  return (const gchar **) non_relocatable_schema_list;
890
0
}
891
892
/**
893
 * g_settings_list_relocatable_schemas:
894
 *
895
 * Deprecated.
896
 *
897
 * Returns: (element-type utf8) (transfer none) (not nullable): a list of
898
 *   relocatable #GSettings schemas that are available, in no defined order.
899
 *   The list must not be modified or freed.
900
 *
901
 * Since: 2.28
902
 *
903
 * Deprecated: 2.40: Use g_settings_schema_source_list_schemas() instead
904
 **/
905
const gchar * const *
906
g_settings_list_relocatable_schemas (void)
907
0
{
908
0
  ensure_schema_lists ();
909
910
0
  return (const gchar **) relocatable_schema_list;
911
0
}
912
913
/**
914
 * g_settings_schema_ref:
915
 * @schema: a #GSettingsSchema
916
 *
917
 * Increase the reference count of @schema, returning a new reference.
918
 *
919
 * Returns: (transfer full) (not nullable): a new reference to @schema
920
 *
921
 * Since: 2.32
922
 **/
923
GSettingsSchema *
924
g_settings_schema_ref (GSettingsSchema *schema)
925
0
{
926
0
  g_atomic_int_inc (&schema->ref_count);
927
928
0
  return schema;
929
0
}
930
931
/**
932
 * g_settings_schema_unref:
933
 * @schema: a #GSettingsSchema
934
 *
935
 * Decrease the reference count of @schema, possibly freeing it.
936
 *
937
 * Since: 2.32
938
 **/
939
void
940
g_settings_schema_unref (GSettingsSchema *schema)
941
0
{
942
0
  if (g_atomic_int_dec_and_test (&schema->ref_count))
943
0
    {
944
0
      if (schema->extends)
945
0
        g_settings_schema_unref (schema->extends);
946
947
0
      g_settings_schema_source_unref (schema->source);
948
0
      gvdb_table_free (schema->table);
949
0
      g_free (schema->items);
950
0
      g_free (schema->id);
951
952
0
      g_slice_free (GSettingsSchema, schema);
953
0
    }
954
0
}
955
956
const gchar *
957
g_settings_schema_get_string (GSettingsSchema *schema,
958
                              const gchar     *key)
959
0
{
960
0
  const gchar *result = NULL;
961
0
  GVariant *value;
962
963
0
  if ((value = gvdb_table_get_raw_value (schema->table, key)))
964
0
    {
965
0
      result = g_variant_get_string (value, NULL);
966
0
      g_variant_unref (value);
967
0
    }
968
969
0
  return result;
970
0
}
971
972
GSettingsSchema *
973
g_settings_schema_get_child_schema (GSettingsSchema *schema,
974
                                    const gchar     *name)
975
0
{
976
0
  const gchar *child_id;
977
0
  gchar *child_name;
978
979
0
  child_name = g_strconcat (name, "/", NULL);
980
0
  child_id = g_settings_schema_get_string (schema, child_name);
981
982
0
  g_free (child_name);
983
984
0
  if (child_id == NULL)
985
0
    return NULL;
986
987
0
  return g_settings_schema_source_lookup (schema->source, child_id, TRUE);
988
0
}
989
990
GVariantIter *
991
g_settings_schema_get_value (GSettingsSchema *schema,
992
                             const gchar     *key)
993
0
{
994
0
  GSettingsSchema *s = schema;
995
0
  GVariantIter *iter;
996
0
  GVariant *value = NULL;
997
998
0
  g_return_val_if_fail (schema != NULL, NULL);
999
1000
0
  for (s = schema; s; s = s->extends)
1001
0
    if ((value = gvdb_table_get_raw_value (s->table, key)))
1002
0
      break;
1003
1004
0
  if G_UNLIKELY (value == NULL || !g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE))
1005
0
    g_error ("Settings schema '%s' does not contain a key named '%s'", schema->id, key);
1006
1007
0
  iter = g_variant_iter_new (value);
1008
0
  g_variant_unref (value);
1009
1010
0
  return iter;
1011
0
}
1012
1013
/**
1014
 * g_settings_schema_get_path:
1015
 * @schema: a #GSettingsSchema
1016
 *
1017
 * Gets the path associated with @schema, or %NULL.
1018
 *
1019
 * Schemas may be single-instance or relocatable.  Single-instance
1020
 * schemas correspond to exactly one set of keys in the backend
1021
 * database: those located at the path returned by this function.
1022
 *
1023
 * Relocatable schemas can be referenced by other schemas and can
1024
 * therefore describe multiple sets of keys at different locations.  For
1025
 * relocatable schemas, this function will return %NULL.
1026
 *
1027
 * Returns: (nullable) (transfer none): the path of the schema, or %NULL
1028
 *
1029
 * Since: 2.32
1030
 **/
1031
const gchar *
1032
g_settings_schema_get_path (GSettingsSchema *schema)
1033
0
{
1034
0
  return schema->path;
1035
0
}
1036
1037
const gchar *
1038
g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
1039
0
{
1040
0
  return schema->gettext_domain;
1041
0
}
1042
1043
/**
1044
 * g_settings_schema_has_key:
1045
 * @schema: a #GSettingsSchema
1046
 * @name: the name of a key
1047
 *
1048
 * Checks if @schema has a key named @name.
1049
 *
1050
 * Returns: %TRUE if such a key exists
1051
 *
1052
 * Since: 2.40
1053
 **/
1054
gboolean
1055
g_settings_schema_has_key (GSettingsSchema *schema,
1056
                           const gchar     *key)
1057
0
{
1058
0
  GSettingsSchema *s;
1059
1060
0
  if (gvdb_table_has_value (schema->table, key))
1061
0
    return TRUE;
1062
1063
0
  for (s = schema; s; s = s->extends)
1064
0
    if (gvdb_table_has_value (s->table, key))
1065
0
      return TRUE;
1066
1067
0
  return FALSE;
1068
0
}
1069
1070
/**
1071
 * g_settings_schema_list_children:
1072
 * @schema: a #GSettingsSchema
1073
 *
1074
 * Gets the list of children in @schema.
1075
 *
1076
 * You should free the return value with g_strfreev() when you are done
1077
 * with it.
1078
 *
1079
 * Returns: (not nullable) (transfer full) (element-type utf8): a list of
1080
 *    the children on @settings, in no defined order
1081
 *
1082
 * Since: 2.44
1083
 */
1084
gchar **
1085
g_settings_schema_list_children (GSettingsSchema *schema)
1086
0
{
1087
0
  const GQuark *keys;
1088
0
  gchar **strv;
1089
0
  gint n_keys;
1090
0
  gint i, j;
1091
1092
0
  g_return_val_if_fail (schema != NULL, NULL);
1093
1094
0
  keys = g_settings_schema_list (schema, &n_keys);
1095
0
  strv = g_new (gchar *, n_keys + 1);
1096
0
  for (i = j = 0; i < n_keys; i++)
1097
0
    {
1098
0
      const gchar *key = g_quark_to_string (keys[i]);
1099
1100
0
      if (g_str_has_suffix (key, "/"))
1101
0
        {
1102
0
          gsize length = strlen (key);
1103
1104
0
          strv[j] = g_memdup2 (key, length);
1105
0
          strv[j][length - 1] = '\0';
1106
0
          j++;
1107
0
        }
1108
0
    }
1109
0
  strv[j] = NULL;
1110
1111
0
  return strv;
1112
0
}
1113
1114
/**
1115
 * g_settings_schema_list_keys:
1116
 * @schema: a #GSettingsSchema
1117
 *
1118
 * Introspects the list of keys on @schema.
1119
 *
1120
 * You should probably not be calling this function from "normal" code
1121
 * (since you should already know what keys are in your schema).  This
1122
 * function is intended for introspection reasons.
1123
 *
1124
 * Returns: (not nullable) (transfer full) (element-type utf8): a list
1125
 *   of the keys on @schema, in no defined order
1126
 *
1127
 * Since: 2.46
1128
 */
1129
gchar **
1130
g_settings_schema_list_keys (GSettingsSchema *schema)
1131
0
{
1132
0
  const GQuark *keys;
1133
0
  gchar **strv;
1134
0
  gint n_keys;
1135
0
  gint i, j;
1136
1137
0
  g_return_val_if_fail (schema != NULL, NULL);
1138
1139
0
  keys = g_settings_schema_list (schema, &n_keys);
1140
0
  strv = g_new (gchar *, n_keys + 1);
1141
0
  for (i = j = 0; i < n_keys; i++)
1142
0
    {
1143
0
      const gchar *key = g_quark_to_string (keys[i]);
1144
1145
0
      if (!g_str_has_suffix (key, "/"))
1146
0
        strv[j++] = g_strdup (key);
1147
0
    }
1148
0
  strv[j] = NULL;
1149
1150
0
  return strv;
1151
0
}
1152
1153
const GQuark *
1154
g_settings_schema_list (GSettingsSchema *schema,
1155
                        gint            *n_items)
1156
0
{
1157
0
  if (schema->items == NULL)
1158
0
    {
1159
0
      GSettingsSchema *s;
1160
0
      GHashTableIter iter;
1161
0
      GHashTable *items;
1162
0
      gpointer name;
1163
0
      gint len;
1164
0
      gint i;
1165
1166
0
      items = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1167
1168
0
      for (s = schema; s; s = s->extends)
1169
0
        {
1170
0
          gchar **list;
1171
1172
0
          list = gvdb_table_list (s->table, "");
1173
1174
0
          if (list)
1175
0
            {
1176
0
              for (i = 0; list[i]; i++)
1177
0
                g_hash_table_add (items, list[i]); /* transfer ownership */
1178
1179
0
              g_free (list); /* free container only */
1180
0
            }
1181
0
        }
1182
1183
      /* Do a first pass to eliminate child items that do not map to
1184
       * valid schemas (ie: ones that would crash us if we actually
1185
       * tried to create them).
1186
       */
1187
0
      g_hash_table_iter_init (&iter, items);
1188
0
      while (g_hash_table_iter_next (&iter, &name, NULL))
1189
0
        if (g_str_has_suffix (name, "/"))
1190
0
          {
1191
0
            GSettingsSchemaSource *source;
1192
0
            GVariant *child_schema;
1193
0
            GvdbTable *child_table;
1194
1195
0
            child_schema = gvdb_table_get_raw_value (schema->table, name);
1196
0
            if (!child_schema)
1197
0
              continue;
1198
1199
0
            child_table = NULL;
1200
1201
0
            for (source = schema->source; source; source = source->parent)
1202
0
              if ((child_table = gvdb_table_get_table (source->table, g_variant_get_string (child_schema, NULL))))
1203
0
                break;
1204
1205
0
            g_variant_unref (child_schema);
1206
1207
            /* Schema is not found -> remove it from the list */
1208
0
            if (child_table == NULL)
1209
0
              {
1210
0
                g_hash_table_iter_remove (&iter);
1211
0
                continue;
1212
0
              }
1213
1214
            /* Make sure the schema is relocatable or at the
1215
             * expected path
1216
             */
1217
0
            if (gvdb_table_has_value (child_table, ".path"))
1218
0
              {
1219
0
                GVariant *path;
1220
0
                gchar *expected;
1221
0
                gboolean same;
1222
1223
0
                path = gvdb_table_get_raw_value (child_table, ".path");
1224
0
                expected = g_strconcat (schema->path, name, NULL);
1225
0
                same = g_str_equal (expected, g_variant_get_string (path, NULL));
1226
0
                g_variant_unref (path);
1227
0
                g_free (expected);
1228
1229
                /* Schema is non-relocatable and did not have the
1230
                 * expected path -> remove it from the list
1231
                 */
1232
0
                if (!same)
1233
0
                  g_hash_table_iter_remove (&iter);
1234
0
              }
1235
1236
0
            gvdb_table_free (child_table);
1237
0
          }
1238
1239
      /* Now create the list */
1240
0
      len = g_hash_table_size (items);
1241
0
      schema->items = g_new (GQuark, len);
1242
0
      i = 0;
1243
0
      g_hash_table_iter_init (&iter, items);
1244
1245
0
      while (g_hash_table_iter_next (&iter, &name, NULL))
1246
0
        schema->items[i++] = g_quark_from_string (name);
1247
0
      schema->n_items = i;
1248
0
      g_assert (i == len);
1249
1250
0
      g_hash_table_unref (items);
1251
0
    }
1252
1253
0
  *n_items = schema->n_items;
1254
0
  return schema->items;
1255
0
}
1256
1257
/**
1258
 * g_settings_schema_get_id:
1259
 * @schema: a #GSettingsSchema
1260
 *
1261
 * Get the ID of @schema.
1262
 *
1263
 * Returns: (not nullable) (transfer none): the ID
1264
 **/
1265
const gchar *
1266
g_settings_schema_get_id (GSettingsSchema *schema)
1267
0
{
1268
0
  return schema->id;
1269
0
}
1270
1271
static inline void
1272
endian_fixup (GVariant **value)
1273
0
{
1274
#if G_BYTE_ORDER == G_BIG_ENDIAN
1275
  GVariant *tmp;
1276
1277
  tmp = g_variant_byteswap (*value);
1278
  g_variant_unref (*value);
1279
  *value = tmp;
1280
#endif
1281
0
}
1282
1283
void
1284
g_settings_schema_key_init (GSettingsSchemaKey *key,
1285
                            GSettingsSchema    *schema,
1286
                            const gchar        *name)
1287
0
{
1288
0
  GVariantIter *iter;
1289
0
  GVariant *data;
1290
0
  guchar code;
1291
1292
0
  memset (key, 0, sizeof *key);
1293
1294
0
  iter = g_settings_schema_get_value (schema, name);
1295
1296
0
  key->schema = g_settings_schema_ref (schema);
1297
0
  key->default_value = g_variant_iter_next_value (iter);
1298
0
  endian_fixup (&key->default_value);
1299
0
  key->type = g_variant_get_type (key->default_value);
1300
0
  key->name = g_intern_string (name);
1301
1302
0
  while (g_variant_iter_next (iter, "(y*)", &code, &data))
1303
0
    {
1304
0
      switch (code)
1305
0
        {
1306
0
        case 'l':
1307
          /* translation requested */
1308
0
          g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
1309
0
          break;
1310
1311
0
        case 'e':
1312
          /* enumerated types... */
1313
0
          key->is_enum = TRUE;
1314
0
          goto choice;
1315
1316
0
        case 'f':
1317
          /* flags... */
1318
0
          key->is_flags = TRUE;
1319
0
          goto choice;
1320
1321
0
        choice: case 'c':
1322
          /* ..., choices, aliases */
1323
0
          key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
1324
0
          break;
1325
1326
0
        case 'r':
1327
0
          g_variant_get (data, "(**)", &key->minimum, &key->maximum);
1328
0
          endian_fixup (&key->minimum);
1329
0
          endian_fixup (&key->maximum);
1330
0
          break;
1331
1332
0
        case 'd':
1333
0
          g_variant_get (data, "@a{sv}", &key->desktop_overrides);
1334
0
          endian_fixup (&key->desktop_overrides);
1335
0
          break;
1336
1337
0
        default:
1338
0
          g_warning ("unknown schema extension '%c'", code);
1339
0
          break;
1340
0
        }
1341
1342
0
      g_variant_unref (data);
1343
0
    }
1344
1345
0
  g_variant_iter_free (iter);
1346
0
}
1347
1348
void
1349
g_settings_schema_key_clear (GSettingsSchemaKey *key)
1350
0
{
1351
0
  if (key->minimum)
1352
0
    g_variant_unref (key->minimum);
1353
1354
0
  if (key->maximum)
1355
0
    g_variant_unref (key->maximum);
1356
1357
0
  if (key->desktop_overrides)
1358
0
    g_variant_unref (key->desktop_overrides);
1359
1360
0
  g_variant_unref (key->default_value);
1361
1362
0
  g_settings_schema_unref (key->schema);
1363
0
}
1364
1365
gboolean
1366
g_settings_schema_key_type_check (GSettingsSchemaKey *key,
1367
                                  GVariant           *value)
1368
0
{
1369
0
  g_return_val_if_fail (value != NULL, FALSE);
1370
1371
0
  return g_variant_is_of_type (value, key->type);
1372
0
}
1373
1374
GVariant *
1375
g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
1376
                                   GVariant           *value)
1377
0
{
1378
0
  const gchar *target;
1379
1380
0
  if (g_settings_schema_key_range_check (key, value))
1381
0
    return g_variant_ref (value);
1382
1383
0
  if (key->strinfo == NULL)
1384
0
    return NULL;
1385
1386
0
  if (g_variant_is_container (value))
1387
0
    {
1388
0
      GVariantBuilder builder;
1389
0
      GVariantIter iter;
1390
0
      GVariant *child;
1391
1392
0
      g_variant_iter_init (&iter, value);
1393
0
      g_variant_builder_init_static (&builder, g_variant_get_type (value));
1394
1395
0
      while ((child = g_variant_iter_next_value (&iter)))
1396
0
        {
1397
0
          GVariant *fixed;
1398
1399
0
          fixed = g_settings_schema_key_range_fixup (key, child);
1400
0
          g_variant_unref (child);
1401
1402
0
          if (fixed == NULL)
1403
0
            {
1404
0
              g_variant_builder_clear (&builder);
1405
0
              return NULL;
1406
0
            }
1407
1408
0
          g_variant_builder_add_value (&builder, fixed);
1409
0
          g_variant_unref (fixed);
1410
0
        }
1411
1412
0
      return g_variant_ref_sink (g_variant_builder_end (&builder));
1413
0
    }
1414
1415
0
  target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
1416
0
                                      g_variant_get_string (value, NULL));
1417
0
  return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1418
0
}
1419
1420
GVariant *
1421
g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
1422
0
{
1423
0
  const gchar *translated = NULL;
1424
0
  GError *error = NULL;
1425
0
  const gchar *domain;
1426
0
#ifdef HAVE_USELOCALE
1427
0
  const gchar *lc_time;
1428
0
  locale_t old_locale;
1429
0
  locale_t locale;
1430
0
#endif
1431
0
  GVariant *value;
1432
1433
0
  domain = g_settings_schema_get_gettext_domain (key->schema);
1434
1435
0
  if (key->lc_char == '\0')
1436
    /* translation not requested for this key */
1437
0
    return NULL;
1438
1439
0
#ifdef HAVE_USELOCALE
1440
0
  if (key->lc_char == 't')
1441
0
    {
1442
0
      lc_time = setlocale (LC_TIME, NULL);
1443
0
      if (lc_time)
1444
0
        {
1445
0
          locale = newlocale (LC_MESSAGES_MASK, lc_time, (locale_t) 0);
1446
0
          if (locale != (locale_t) 0)
1447
0
            {
1448
0
              old_locale = uselocale (locale);
1449
0
              translated = g_dgettext (domain, key->unparsed);
1450
0
              uselocale (old_locale);
1451
0
              freelocale (locale);
1452
0
            }
1453
0
        }
1454
0
    }
1455
0
#endif
1456
1457
0
  if (translated == NULL)
1458
0
    translated = g_dgettext (domain, key->unparsed);
1459
1460
0
  if (translated == key->unparsed)
1461
    /* the default value was not translated */
1462
0
    return NULL;
1463
1464
  /* try to parse the translation of the unparsed default */
1465
0
  value = g_variant_parse (key->type, translated, NULL, NULL, &error);
1466
1467
0
  if (value == NULL)
1468
0
    {
1469
0
      g_warning ("Failed to parse translated string '%s' for "
1470
0
                 "key '%s' in schema '%s': %s", translated, key->name,
1471
0
                 g_settings_schema_get_id (key->schema), error->message);
1472
0
      g_warning ("Using untranslated default instead.");
1473
0
      g_error_free (error);
1474
0
    }
1475
1476
0
  else if (!g_settings_schema_key_range_check (key, value))
1477
0
    {
1478
0
      g_warning ("Translated default '%s' for key '%s' in schema '%s' "
1479
0
                 "is outside of valid range", key->unparsed, key->name,
1480
0
                 g_settings_schema_get_id (key->schema));
1481
0
      g_variant_unref (value);
1482
0
      value = NULL;
1483
0
    }
1484
1485
0
  return value;
1486
0
}
1487
1488
GVariant *
1489
g_settings_schema_key_get_per_desktop_default (GSettingsSchemaKey *key)
1490
0
{
1491
0
  static const gchar * const *current_desktops;
1492
0
  GVariant *value = NULL;
1493
0
  gint i;
1494
1495
0
  if (!key->desktop_overrides)
1496
0
    return NULL;
1497
1498
0
  if (g_once_init_enter_pointer (&current_desktops))
1499
0
    {
1500
0
      const gchar *xdg_current_desktop = g_getenv ("XDG_CURRENT_DESKTOP");
1501
0
      gchar **tmp;
1502
1503
0
      if (xdg_current_desktop != NULL && xdg_current_desktop[0] != '\0')
1504
0
        tmp = g_strsplit (xdg_current_desktop, G_SEARCHPATH_SEPARATOR_S, -1);
1505
0
      else
1506
0
        tmp = g_new0 (gchar *, 0 + 1);
1507
1508
0
      g_once_init_leave_pointer (&current_desktops, (const gchar **) tmp);
1509
0
    }
1510
1511
0
  for (i = 0; value == NULL && current_desktops[i] != NULL; i++)
1512
0
    value = g_variant_lookup_value (key->desktop_overrides, current_desktops[i], NULL);
1513
1514
0
  return value;
1515
0
}
1516
1517
gint
1518
g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
1519
                               GVariant           *value)
1520
0
{
1521
0
  gboolean it_worked G_GNUC_UNUSED  /* when compiling with G_DISABLE_ASSERT */;
1522
0
  guint result;
1523
1524
0
  it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
1525
0
                                        g_variant_get_string (value, NULL),
1526
0
                                        &result);
1527
1528
  /* 'value' can only come from the backend after being filtered for validity,
1529
   * from the translation after being filtered for validity, or from the schema
1530
   * itself (which the schema compiler checks for validity).  If this assertion
1531
   * fails then it's really a bug in GSettings or the schema compiler...
1532
   */
1533
0
  g_assert (it_worked);
1534
1535
0
  return result;
1536
0
}
1537
1538
/* Returns a new floating #GVariant. */
1539
GVariant *
1540
g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
1541
                                 gint                value)
1542
0
{
1543
0
  const gchar *string;
1544
1545
0
  string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
1546
1547
0
  if (string == NULL)
1548
0
    return NULL;
1549
1550
0
  return g_variant_new_string (string);
1551
0
}
1552
1553
guint
1554
g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
1555
                                GVariant           *value)
1556
0
{
1557
0
  GVariantIter iter;
1558
0
  const gchar *flag;
1559
0
  guint result;
1560
1561
0
  result = 0;
1562
0
  g_variant_iter_init (&iter, value);
1563
0
  while (g_variant_iter_next (&iter, "&s", &flag))
1564
0
    {
1565
0
      gboolean it_worked G_GNUC_UNUSED  /* when compiling with G_DISABLE_ASSERT */;
1566
0
      guint flag_value;
1567
1568
0
      it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
1569
      /* as in g_settings_to_enum() */
1570
0
      g_assert (it_worked);
1571
1572
0
      result |= flag_value;
1573
0
    }
1574
1575
0
  return result;
1576
0
}
1577
1578
/* Returns a new floating #GVariant. */
1579
GVariant *
1580
g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
1581
                                  guint               value)
1582
0
{
1583
0
  GVariantBuilder builder;
1584
0
  gint i;
1585
1586
0
  g_variant_builder_init_static (&builder, G_VARIANT_TYPE ("as"));
1587
1588
0
  for (i = 0; i < 32; i++)
1589
0
    if (value & (1u << i))
1590
0
      {
1591
0
        const gchar *string;
1592
1593
0
        string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
1594
1595
0
        if (string == NULL)
1596
0
          {
1597
0
            g_variant_builder_clear (&builder);
1598
0
            return NULL;
1599
0
          }
1600
1601
0
        g_variant_builder_add (&builder, "s", string);
1602
0
      }
1603
1604
0
  return g_variant_builder_end (&builder);
1605
0
}
1606
1607
G_DEFINE_BOXED_TYPE (GSettingsSchemaKey, g_settings_schema_key, g_settings_schema_key_ref, g_settings_schema_key_unref)
1608
1609
/**
1610
 * g_settings_schema_key_ref:
1611
 * @key: a #GSettingsSchemaKey
1612
 *
1613
 * Increase the reference count of @key, returning a new reference.
1614
 *
1615
 * Returns: (not nullable) (transfer full): a new reference to @key
1616
 *
1617
 * Since: 2.40
1618
 **/
1619
GSettingsSchemaKey *
1620
g_settings_schema_key_ref (GSettingsSchemaKey *key)
1621
0
{
1622
0
  g_return_val_if_fail (key != NULL, NULL);
1623
1624
0
  g_atomic_int_inc (&key->ref_count);
1625
1626
0
  return key;
1627
0
}
1628
1629
/**
1630
 * g_settings_schema_key_unref:
1631
 * @key: a #GSettingsSchemaKey
1632
 *
1633
 * Decrease the reference count of @key, possibly freeing it.
1634
 *
1635
 * Since: 2.40
1636
 **/
1637
void
1638
g_settings_schema_key_unref (GSettingsSchemaKey *key)
1639
0
{
1640
0
  g_return_if_fail (key != NULL);
1641
1642
0
  if (g_atomic_int_dec_and_test (&key->ref_count))
1643
0
    {
1644
0
      g_settings_schema_key_clear (key);
1645
1646
0
      g_slice_free (GSettingsSchemaKey, key);
1647
0
    }
1648
0
}
1649
1650
/**
1651
 * g_settings_schema_get_key:
1652
 * @schema: a #GSettingsSchema
1653
 * @name: the name of a key
1654
 *
1655
 * Gets the key named @name from @schema.
1656
 *
1657
 * It is a programmer error to request a key that does not exist.  See
1658
 * g_settings_schema_list_keys().
1659
 *
1660
 * Returns: (not nullable) (transfer full): the #GSettingsSchemaKey for @name
1661
 *
1662
 * Since: 2.40
1663
 **/
1664
GSettingsSchemaKey *
1665
g_settings_schema_get_key (GSettingsSchema *schema,
1666
                           const gchar     *name)
1667
0
{
1668
0
  GSettingsSchemaKey *key;
1669
1670
0
  g_return_val_if_fail (schema != NULL, NULL);
1671
0
  g_return_val_if_fail (name != NULL, NULL);
1672
1673
0
  key = g_slice_new (GSettingsSchemaKey);
1674
0
  g_settings_schema_key_init (key, schema, name);
1675
0
  key->ref_count = 1;
1676
1677
0
  return key;
1678
0
}
1679
1680
/**
1681
 * g_settings_schema_key_get_name:
1682
 * @key: a #GSettingsSchemaKey
1683
 *
1684
 * Gets the name of @key.
1685
 *
1686
 * Returns: (not nullable) (transfer none): the name of @key.
1687
 *
1688
 * Since: 2.44
1689
 */
1690
const gchar *
1691
g_settings_schema_key_get_name (GSettingsSchemaKey *key)
1692
0
{
1693
0
  g_return_val_if_fail (key != NULL, NULL);
1694
1695
0
  return key->name;
1696
0
}
1697
1698
/**
1699
 * g_settings_schema_key_get_summary:
1700
 * @key: a #GSettingsSchemaKey
1701
 *
1702
 * Gets the summary for @key.
1703
 *
1704
 * If no summary has been provided in the schema for @key, returns
1705
 * %NULL.
1706
 *
1707
 * The summary is a short description of the purpose of the key; usually
1708
 * one short sentence.  Summaries can be translated and the value
1709
 * returned from this function is is the current locale.
1710
 *
1711
 * This function is slow.  The summary and description information for
1712
 * the schemas is not stored in the compiled schema database so this
1713
 * function has to parse all of the source XML files in the schema
1714
 * directory.
1715
 *
1716
 * Returns: (nullable) (transfer none): the summary for @key, or %NULL
1717
 *
1718
 * Since: 2.34
1719
 **/
1720
const gchar *
1721
g_settings_schema_key_get_summary (GSettingsSchemaKey *key)
1722
0
{
1723
0
  GHashTable **text_tables;
1724
0
  GHashTable *summaries;
1725
1726
0
  text_tables = g_settings_schema_source_get_text_tables (key->schema->source);
1727
0
  summaries = g_hash_table_lookup (text_tables[0], key->schema->id);
1728
1729
0
  return summaries ? g_hash_table_lookup (summaries, key->name) : NULL;
1730
0
}
1731
1732
/**
1733
 * g_settings_schema_key_get_description:
1734
 * @key: a #GSettingsSchemaKey
1735
 *
1736
 * Gets the description for @key.
1737
 *
1738
 * If no description has been provided in the schema for @key, returns
1739
 * %NULL.
1740
 *
1741
 * The description can be one sentence to several paragraphs in length.
1742
 * Paragraphs are delimited with a double newline.  Descriptions can be
1743
 * translated and the value returned from this function is is the
1744
 * current locale.
1745
 *
1746
 * This function is slow.  The summary and description information for
1747
 * the schemas is not stored in the compiled schema database so this
1748
 * function has to parse all of the source XML files in the schema
1749
 * directory.
1750
 *
1751
 * Returns: (nullable) (transfer none): the description for @key, or %NULL
1752
 *
1753
 * Since: 2.34
1754
 **/
1755
const gchar *
1756
g_settings_schema_key_get_description (GSettingsSchemaKey *key)
1757
0
{
1758
0
  GHashTable **text_tables;
1759
0
  GHashTable *descriptions;
1760
1761
0
  text_tables = g_settings_schema_source_get_text_tables (key->schema->source);
1762
0
  descriptions = g_hash_table_lookup (text_tables[1], key->schema->id);
1763
1764
0
  return descriptions ? g_hash_table_lookup (descriptions, key->name) : NULL;
1765
0
}
1766
1767
/**
1768
 * g_settings_schema_key_get_value_type:
1769
 * @key: a #GSettingsSchemaKey
1770
 *
1771
 * Gets the #GVariantType of @key.
1772
 *
1773
 * Returns: (not nullable) (transfer none): the type of @key
1774
 *
1775
 * Since: 2.40
1776
 **/
1777
const GVariantType *
1778
g_settings_schema_key_get_value_type (GSettingsSchemaKey *key)
1779
0
{
1780
0
  g_return_val_if_fail (key, NULL);
1781
1782
0
  return key->type;
1783
0
}
1784
1785
/**
1786
 * g_settings_schema_key_get_default_value:
1787
 * @key: a #GSettingsSchemaKey
1788
 *
1789
 * Gets the default value for @key.
1790
 *
1791
 * Note that this is the default value according to the schema.  System
1792
 * administrator defaults and lockdown are not visible via this API.
1793
 *
1794
 * Returns: (not nullable) (transfer full): the default value for the key
1795
 *
1796
 * Since: 2.40
1797
 **/
1798
GVariant *
1799
g_settings_schema_key_get_default_value (GSettingsSchemaKey *key)
1800
0
{
1801
0
  GVariant *value;
1802
1803
0
  g_return_val_if_fail (key, NULL);
1804
1805
0
  value = g_settings_schema_key_get_translated_default (key);
1806
1807
0
  if (!value)
1808
0
    value = g_settings_schema_key_get_per_desktop_default (key);
1809
1810
0
  if (!value)
1811
0
    value = g_variant_ref (key->default_value);
1812
1813
0
  return value;
1814
0
}
1815
1816
/**
1817
 * g_settings_schema_key_get_range:
1818
 * @key: a #GSettingsSchemaKey
1819
 *
1820
 * Queries the range of a key.
1821
 *
1822
 * This function will return a #GVariant that fully describes the range
1823
 * of values that are valid for @key.
1824
 *
1825
 * The type of #GVariant returned is `(sv)`. The string describes
1826
 * the type of range restriction in effect. The type and meaning of
1827
 * the value contained in the variant depends on the string.
1828
 *
1829
 * If the string is `'type'` then the variant contains an empty array.
1830
 * The element type of that empty array is the expected type of value
1831
 * and all values of that type are valid.
1832
 *
1833
 * If the string is `'enum'` then the variant contains an array
1834
 * enumerating the possible values. Each item in the array is
1835
 * a possible valid value and no other values are valid.
1836
 *
1837
 * If the string is `'flags'` then the variant contains an array. Each
1838
 * item in the array is a value that may appear zero or one times in an
1839
 * array to be used as the value for this key. For example, if the
1840
 * variant contained the array `['x', 'y']` then the valid values for
1841
 * the key would be `[]`, `['x']`, `['y']`, `['x', 'y']` and
1842
 * `['y', 'x']`.
1843
 *
1844
 * Finally, if the string is `'range'` then the variant contains a pair
1845
 * of like-typed values -- the minimum and maximum permissible values
1846
 * for this key.
1847
 *
1848
 * This information should not be used by normal programs.  It is
1849
 * considered to be a hint for introspection purposes.  Normal programs
1850
 * should already know what is permitted by their own schema.  The
1851
 * format may change in any way in the future -- but particularly, new
1852
 * forms may be added to the possibilities described above.
1853
 *
1854
 * You should free the returned value with g_variant_unref() when it is
1855
 * no longer needed.
1856
 *
1857
 * Returns: (not nullable) (transfer full): a #GVariant describing the range
1858
 *
1859
 * Since: 2.40
1860
 **/
1861
GVariant *
1862
g_settings_schema_key_get_range (GSettingsSchemaKey *key)
1863
0
{
1864
0
  const gchar *type;
1865
0
  GVariant *range;
1866
1867
0
  if (key->minimum)
1868
0
    {
1869
0
      range = g_variant_new ("(**)", key->minimum, key->maximum);
1870
0
      type = "range";
1871
0
    }
1872
0
  else if (key->strinfo)
1873
0
    {
1874
0
      range = strinfo_enumerate (key->strinfo, key->strinfo_length);
1875
0
      type = key->is_flags ? "flags" : "enum";
1876
0
    }
1877
0
  else
1878
0
    {
1879
0
      range = g_variant_new_array (key->type, NULL, 0);
1880
0
      type = "type";
1881
0
    }
1882
1883
0
  return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
1884
0
}
1885
1886
/**
1887
 * g_settings_schema_key_range_check:
1888
 * @key: a #GSettingsSchemaKey
1889
 * @value: the value to check
1890
 *
1891
 * Checks if the given @value is within the
1892
 * permitted range for @key.
1893
 *
1894
 * It is a programmer error if @value is not of the correct type — you
1895
 * must check for this first.
1896
 *
1897
 * Returns: %TRUE if @value is valid for @key
1898
 *
1899
 * Since: 2.40
1900
 **/
1901
gboolean
1902
g_settings_schema_key_range_check (GSettingsSchemaKey *key,
1903
                                   GVariant           *value)
1904
0
{
1905
0
  if (key->minimum == NULL && key->strinfo == NULL)
1906
0
    return TRUE;
1907
1908
0
  if (g_variant_is_container (value))
1909
0
    {
1910
0
      gboolean ok = TRUE;
1911
0
      GVariantIter iter;
1912
0
      GVariant *child;
1913
1914
0
      g_variant_iter_init (&iter, value);
1915
0
      while (ok && (child = g_variant_iter_next_value (&iter)))
1916
0
        {
1917
0
          ok = g_settings_schema_key_range_check (key, child);
1918
0
          g_variant_unref (child);
1919
0
        }
1920
1921
0
      return ok;
1922
0
    }
1923
1924
0
  if (key->minimum)
1925
0
    {
1926
0
      return g_variant_compare (key->minimum, value) <= 0 &&
1927
0
             g_variant_compare (value, key->maximum) <= 0;
1928
0
    }
1929
1930
0
  return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
1931
0
                                  g_variant_get_string (value, NULL));
1932
0
}