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