Coverage Report

Created: 2025-07-11 07:03

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