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