2 * Copyright © 2010 Codethink Limited
3 * Copyright © 2011 Canonical Limited
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.1 of the License, or (at your option) any later version.
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.
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/>.
21 #include "gsettingsschema-internal.h"
22 #include "gsettings.h"
24 #include "gvdb/gvdb-reader.h"
33 * SECTION:gsettingsschema
34 * @short_description: Introspecting and controlling the loading
35 * of GSettings schemas
38 * The #GSettingsSchemaSource and #GSettingsSchema APIs provide a
39 * mechanism for advanced control over the loading of schemas and a
40 * mechanism for introspecting their content.
42 * Plugin loading systems that wish to provide plugins a way to access
43 * settings face the problem of how to make the schemas for these
44 * settings visible to GSettings. Typically, a plugin will want to ship
45 * the schema along with itself and it won't be installed into the
46 * standard system directories for schemas.
48 * #GSettingsSchemaSource provides a mechanism for dealing with this by
49 * allowing the creation of a new 'schema source' from which schemas can
50 * be acquired. This schema source can then become part of the metadata
51 * associated with the plugin and queried whenever the plugin requires
52 * access to some settings.
54 * Consider the following example:
56 * |[<!-- language="C" -->
60 * GSettingsSchemaSource *schema_source;
65 * initialise_plugin (const gchar *dir)
71 * plugin->schema_source =
72 * g_settings_schema_source_new_from_directory (dir,
73 * g_settings_schema_source_get_default (), FALSE, NULL);
83 * plugin_get_settings (Plugin *plugin,
84 * const gchar *schema_id)
86 * GSettingsSchema *schema;
88 * if (schema_id == NULL)
89 * schema_id = plugin->identifier;
91 * schema = g_settings_schema_source_lookup (plugin->schema_source,
96 * ... disable the plugin or abort, etc ...
99 * return g_settings_new_full (schema, NULL, NULL);
103 * The code above shows how hooks should be added to the code that
104 * initialises (or enables) the plugin to create the schema source and
105 * how an API can be added to the plugin system to provide a convenient
106 * way for the plugin to access its settings, using the schemas that it
109 * From the standpoint of the plugin, it would need to ensure that it
110 * ships a gschemas.compiled file as part of itself, and then simply do
113 * |[<!-- language="C" -->
115 * GSettings *settings;
118 * settings = plugin_get_settings (self, NULL);
119 * some_value = g_settings_get_int (settings, "some-value");
124 * It's also possible that the plugin system expects the schema source
125 * files (ie: .gschema.xml files) instead of a gschemas.compiled file.
126 * In that case, the plugin loading system must compile the schemas for
127 * itself before attempting to create the settings source.
133 * GSettingsSchemaKey:
135 * #GSettingsSchemaKey is an opaque data structure and can only be accessed
136 * using the following functions.
142 * This is an opaque structure type. You may not access it directly.
146 struct _GSettingsSchema
148 GSettingsSchemaSource
*source
;
149 const gchar
*gettext_domain
;
156 GSettingsSchema
*extends
;
162 * G_TYPE_SETTINGS_SCHEMA_SOURCE:
164 * A boxed #GType corresponding to #GSettingsSchemaSource.
168 G_DEFINE_BOXED_TYPE (GSettingsSchemaSource
, g_settings_schema_source
, g_settings_schema_source_ref
, g_settings_schema_source_unref
)
171 * G_TYPE_SETTINGS_SCHEMA:
173 * A boxed #GType corresponding to #GSettingsSchema.
177 G_DEFINE_BOXED_TYPE (GSettingsSchema
, g_settings_schema
, g_settings_schema_ref
, g_settings_schema_unref
)
180 * GSettingsSchemaSource:
182 * This is an opaque structure type. You may not access it directly.
186 struct _GSettingsSchemaSource
188 GSettingsSchemaSource
*parent
;
191 GHashTable
**text_tables
;
196 static GSettingsSchemaSource
*schema_sources
;
199 * g_settings_schema_source_ref:
200 * @source: a #GSettingsSchemaSource
202 * Increase the reference count of @source, returning a new reference.
204 * Returns: a new reference to @source
208 GSettingsSchemaSource
*
209 g_settings_schema_source_ref (GSettingsSchemaSource
*source
)
211 g_atomic_int_inc (&source
->ref_count
);
217 * g_settings_schema_source_unref:
218 * @source: a #GSettingsSchemaSource
220 * Decrease the reference count of @source, possibly freeing it.
225 g_settings_schema_source_unref (GSettingsSchemaSource
*source
)
227 if (g_atomic_int_dec_and_test (&source
->ref_count
))
229 if (source
== schema_sources
)
230 g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
233 g_settings_schema_source_unref (source
->parent
);
234 gvdb_table_unref (source
->table
);
235 g_free (source
->directory
);
237 if (source
->text_tables
)
239 g_hash_table_unref (source
->text_tables
[0]);
240 g_hash_table_unref (source
->text_tables
[1]);
241 g_free (source
->text_tables
);
244 g_slice_free (GSettingsSchemaSource
, source
);
249 * g_settings_schema_source_new_from_directory:
250 * @directory: (type filename): the filename of a directory
251 * @parent: (nullable): a #GSettingsSchemaSource, or %NULL
252 * @trusted: %TRUE, if the directory is trusted
253 * @error: a pointer to a #GError pointer set to %NULL, or %NULL
255 * Attempts to create a new schema source corresponding to the contents
256 * of the given directory.
258 * This function is not required for normal uses of #GSettings but it
259 * may be useful to authors of plugin management systems.
261 * The directory should contain a file called `gschemas.compiled` as
262 * produced by the [glib-compile-schemas][glib-compile-schemas] tool.
264 * If @trusted is %TRUE then `gschemas.compiled` is trusted not to be
265 * corrupted. This assumption has a performance advantage, but can result
266 * in crashes or inconsistent behaviour in the case of a corrupted file.
267 * Generally, you should set @trusted to %TRUE for files installed by the
268 * system and to %FALSE for files in the home directory.
270 * If @parent is non-%NULL then there are two effects.
272 * First, if g_settings_schema_source_lookup() is called with the
273 * @recursive flag set to %TRUE and the schema can not be found in the
274 * source, the lookup will recurse to the parent.
276 * Second, any references to other schemas specified within this
277 * source (ie: `child` or `extends`) references may be resolved
280 * For this second reason, except in very unusual situations, the
281 * @parent should probably be given as the default schema source, as
282 * returned by g_settings_schema_source_get_default().
286 GSettingsSchemaSource
*
287 g_settings_schema_source_new_from_directory (const gchar
*directory
,
288 GSettingsSchemaSource
*parent
,
292 GSettingsSchemaSource
*source
;
296 filename
= g_build_filename (directory
, "gschemas.compiled", NULL
);
297 table
= gvdb_table_new (filename
, trusted
, error
);
303 source
= g_slice_new (GSettingsSchemaSource
);
304 source
->directory
= g_strdup (directory
);
305 source
->parent
= parent
? g_settings_schema_source_ref (parent
) : NULL
;
306 source
->text_tables
= NULL
;
307 source
->table
= table
;
308 source
->ref_count
= 1;
314 try_prepend_dir (const gchar
*directory
)
316 GSettingsSchemaSource
*source
;
318 source
= g_settings_schema_source_new_from_directory (directory
, schema_sources
, TRUE
, NULL
);
320 /* If we successfully created it then prepend it to the global list */
322 schema_sources
= source
;
326 try_prepend_data_dir (const gchar
*directory
)
328 gchar
*dirname
= g_build_filename (directory
, "glib-2.0", "schemas", NULL
);
329 try_prepend_dir (dirname
);
334 initialise_schema_sources (void)
336 static gsize initialised
;
338 /* need a separate variable because 'schema_sources' may legitimately
339 * be null if we have zero valid schema sources
341 if G_UNLIKELY (g_once_init_enter (&initialised
))
343 const gchar
* const *dirs
;
347 /* iterate in reverse: count up, then count down */
348 dirs
= g_get_system_data_dirs ();
349 for (i
= 0; dirs
[i
]; i
++);
352 try_prepend_data_dir (dirs
[i
]);
354 try_prepend_data_dir (g_get_user_data_dir ());
356 if ((path
= g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL
)
357 try_prepend_dir (path
);
359 g_once_init_leave (&initialised
, TRUE
);
364 * g_settings_schema_source_get_default:
366 * Gets the default system schema source.
368 * This function is not required for normal uses of #GSettings but it
369 * may be useful to authors of plugin management systems or to those who
370 * want to introspect the content of schemas.
372 * If no schemas are installed, %NULL will be returned.
374 * The returned source may actually consist of multiple schema sources
375 * from different directories, depending on which directories were given
376 * in `XDG_DATA_DIRS` and `GSETTINGS_SCHEMA_DIR`. For this reason, all
377 * lookups performed against the default source should probably be done
380 * Returns: (transfer none) (nullable): the default schema source
384 GSettingsSchemaSource
*
385 g_settings_schema_source_get_default (void)
387 initialise_schema_sources ();
389 return schema_sources
;
393 * g_settings_schema_source_lookup:
394 * @source: a #GSettingsSchemaSource
395 * @schema_id: a schema ID
396 * @recursive: %TRUE if the lookup should be recursive
398 * Looks up a schema with the identifier @schema_id in @source.
400 * This function is not required for normal uses of #GSettings but it
401 * may be useful to authors of plugin management systems or to those who
402 * want to introspect the content of schemas.
404 * If the schema isn't found directly in @source and @recursive is %TRUE
405 * then the parent sources will also be checked.
407 * If the schema isn't found, %NULL is returned.
409 * Returns: (nullable) (transfer full): a new #GSettingsSchema
414 g_settings_schema_source_lookup (GSettingsSchemaSource
*source
,
415 const gchar
*schema_id
,
418 GSettingsSchema
*schema
;
420 const gchar
*extends
;
422 g_return_val_if_fail (source
!= NULL
, NULL
);
423 g_return_val_if_fail (schema_id
!= NULL
, NULL
);
425 table
= gvdb_table_get_table (source
->table
, schema_id
);
427 if (table
== NULL
&& recursive
)
428 for (source
= source
->parent
; source
; source
= source
->parent
)
429 if ((table
= gvdb_table_get_table (source
->table
, schema_id
)))
435 schema
= g_slice_new0 (GSettingsSchema
);
436 schema
->source
= g_settings_schema_source_ref (source
);
437 schema
->ref_count
= 1;
438 schema
->id
= g_strdup (schema_id
);
439 schema
->table
= table
;
440 schema
->path
= g_settings_schema_get_string (schema
, ".path");
441 schema
->gettext_domain
= g_settings_schema_get_string (schema
, ".gettext-domain");
443 if (schema
->gettext_domain
)
444 bind_textdomain_codeset (schema
->gettext_domain
, "UTF-8");
446 extends
= g_settings_schema_get_string (schema
, ".extends");
449 schema
->extends
= g_settings_schema_source_lookup (source
, extends
, TRUE
);
450 if (schema
->extends
== NULL
)
451 g_warning ("Schema '%s' extends schema '%s' but we could not find it", schema_id
, extends
);
459 GHashTable
*summaries
;
460 GHashTable
*descriptions
;
461 GSList
*gettext_domain
;
465 } TextTableParseInfo
;
468 get_attribute_value (GSList
*list
)
472 for (node
= list
; node
; node
= node
->next
)
480 pop_attribute_value (GSList
**list
)
485 *list
= g_slist_remove (*list
, top
);
491 push_attribute_value (GSList
**list
,
494 *list
= g_slist_prepend (*list
, g_strdup (value
));
498 start_element (GMarkupParseContext
*context
,
499 const gchar
*element_name
,
500 const gchar
**attribute_names
,
501 const gchar
**attribute_values
,
505 TextTableParseInfo
*info
= user_data
;
506 const gchar
*gettext_domain
= NULL
;
507 const gchar
*schema_id
= NULL
;
508 const gchar
*key_name
= NULL
;
511 for (i
= 0; attribute_names
[i
]; i
++)
513 if (g_str_equal (attribute_names
[i
], "gettext-domain"))
514 gettext_domain
= attribute_values
[i
];
515 else if (g_str_equal (attribute_names
[i
], "id"))
516 schema_id
= attribute_values
[i
];
517 else if (g_str_equal (attribute_names
[i
], "name"))
518 key_name
= attribute_values
[i
];
521 push_attribute_value (&info
->gettext_domain
, gettext_domain
);
522 push_attribute_value (&info
->schema_id
, schema_id
);
523 push_attribute_value (&info
->key_name
, key_name
);
527 g_string_free (info
->string
, TRUE
);
531 if (g_str_equal (element_name
, "summary") || g_str_equal (element_name
, "description"))
532 info
->string
= g_string_new (NULL
);
536 normalise_whitespace (const gchar
*orig
)
538 /* We normalise by the same rules as in intltool:
547 * $message = join "\n\n", map &cleanup, split/\n\s*\n+/, $message;
549 * Where \s is an ascii space character.
551 * We aim for ease of implementation over efficiency -- this code is
552 * not run in normal applications.
554 static GRegex
*cleanup
[3];
555 static GRegex
*splitter
;
560 if (g_once_init_enter (&splitter
))
564 cleanup
[0] = g_regex_new ("^\\s+", 0, 0, 0);
565 cleanup
[1] = g_regex_new ("\\s+$", 0, 0, 0);
566 cleanup
[2] = g_regex_new ("\\s+", 0, 0, 0);
567 s
= g_regex_new ("\\n\\s*\\n+", 0, 0, 0);
569 g_once_init_leave (&splitter
, s
);
572 lines
= g_regex_split (splitter
, orig
, 0);
573 for (i
= 0; lines
[i
]; i
++)
577 a
= g_regex_replace_literal (cleanup
[0], lines
[i
], -1, 0, "", 0, 0);
578 b
= g_regex_replace_literal (cleanup
[1], a
, -1, 0, "", 0, 0);
579 c
= g_regex_replace_literal (cleanup
[2], b
, -1, 0, " ", 0, 0);
586 result
= g_strjoinv ("\n\n", lines
);
593 end_element (GMarkupParseContext
*context
,
594 const gchar
*element_name
,
598 TextTableParseInfo
*info
= user_data
;
600 pop_attribute_value (&info
->gettext_domain
);
601 pop_attribute_value (&info
->schema_id
);
602 pop_attribute_value (&info
->key_name
);
606 GHashTable
*source_table
= NULL
;
607 const gchar
*gettext_domain
;
608 const gchar
*schema_id
;
609 const gchar
*key_name
;
611 gettext_domain
= get_attribute_value (info
->gettext_domain
);
612 schema_id
= get_attribute_value (info
->schema_id
);
613 key_name
= get_attribute_value (info
->key_name
);
615 if (g_str_equal (element_name
, "summary"))
616 source_table
= info
->summaries
;
617 else if (g_str_equal (element_name
, "description"))
618 source_table
= info
->descriptions
;
620 if (source_table
&& schema_id
&& key_name
)
622 GHashTable
*schema_table
;
625 schema_table
= g_hash_table_lookup (source_table
, schema_id
);
627 if (schema_table
== NULL
)
629 schema_table
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, g_free
);
630 g_hash_table_insert (source_table
, g_strdup (schema_id
), schema_table
);
633 normalised
= normalise_whitespace (info
->string
->str
);
635 if (gettext_domain
&& normalised
[0])
639 translated
= g_strdup (g_dgettext (gettext_domain
, normalised
));
641 normalised
= translated
;
644 g_hash_table_insert (schema_table
, g_strdup (key_name
), normalised
);
647 g_string_free (info
->string
, TRUE
);
653 text (GMarkupParseContext
*context
,
659 TextTableParseInfo
*info
= user_data
;
662 g_string_append_len (info
->string
, text
, text_len
);
666 parse_into_text_tables (const gchar
*directory
,
667 GHashTable
*summaries
,
668 GHashTable
*descriptions
)
670 GMarkupParser parser
= { start_element
, end_element
, text
};
671 TextTableParseInfo info
= { summaries
, descriptions
};
672 const gchar
*basename
;
675 dir
= g_dir_open (directory
, 0, NULL
);
676 while ((basename
= g_dir_read_name (dir
)))
682 filename
= g_build_filename (directory
, basename
, NULL
);
683 if (g_file_get_contents (filename
, &contents
, &size
, NULL
))
685 GMarkupParseContext
*context
;
687 context
= g_markup_parse_context_new (&parser
, G_MARKUP_TREAT_CDATA_AS_TEXT
, &info
, NULL
);
688 /* Ignore errors here, this is best effort only. */
689 if (g_markup_parse_context_parse (context
, contents
, size
, NULL
))
690 (void) g_markup_parse_context_end_parse (context
, NULL
);
691 g_markup_parse_context_free (context
);
693 /* Clean up dangling stuff in case there was an error. */
694 g_slist_free_full (info
.gettext_domain
, g_free
);
695 g_slist_free_full (info
.schema_id
, g_free
);
696 g_slist_free_full (info
.key_name
, g_free
);
698 info
.gettext_domain
= NULL
;
699 info
.schema_id
= NULL
;
700 info
.key_name
= NULL
;
704 g_string_free (info
.string
, TRUE
);
718 g_settings_schema_source_get_text_tables (GSettingsSchemaSource
*source
)
720 if (g_once_init_enter (&source
->text_tables
))
722 GHashTable
**text_tables
;
724 text_tables
= g_new (GHashTable
*, 2);
725 text_tables
[0] = g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
) g_hash_table_unref
);
726 text_tables
[1] = g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
) g_hash_table_unref
);
728 if (source
->directory
)
729 parse_into_text_tables (source
->directory
, text_tables
[0], text_tables
[1]);
731 g_once_init_leave (&source
->text_tables
, text_tables
);
734 return source
->text_tables
;
738 * g_settings_schema_source_list_schemas:
739 * @source: a #GSettingsSchemaSource
740 * @recursive: if we should recurse
741 * @non_relocatable: (out) (transfer full) (array zero-terminated=1): the
742 * list of non-relocatable schemas
743 * @relocatable: (out) (transfer full) (array zero-terminated=1): the list
744 * of relocatable schemas
746 * Lists the schemas in a given source.
748 * If @recursive is %TRUE then include parent sources. If %FALSE then
749 * only include the schemas from one source (ie: one directory). You
750 * probably want %TRUE.
752 * Non-relocatable schemas are those for which you can call
753 * g_settings_new(). Relocatable schemas are those for which you must
754 * use g_settings_new_with_path().
756 * Do not call this function from normal programs. This is designed for
757 * use by database editors, commandline tools, etc.
762 g_settings_schema_source_list_schemas (GSettingsSchemaSource
*source
,
764 gchar
***non_relocatable
,
765 gchar
***relocatable
)
767 GHashTable
*single
, *reloc
;
768 GSettingsSchemaSource
*s
;
770 /* We use hash tables to avoid duplicate listings for schemas that
771 * appear in more than one file.
773 single
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
774 reloc
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
776 for (s
= source
; s
; s
= s
->parent
)
781 list
= gvdb_table_list (s
->table
, "");
783 /* empty schema cache file? */
787 for (i
= 0; list
[i
]; i
++)
789 if (!g_hash_table_contains (single
, list
[i
]) &&
790 !g_hash_table_contains (reloc
, list
[i
]))
795 schema
= g_strdup (list
[i
]);
797 table
= gvdb_table_get_table (s
->table
, list
[i
]);
798 g_assert (table
!= NULL
);
800 if (gvdb_table_has_value (table
, ".path"))
801 g_hash_table_add (single
, schema
);
803 g_hash_table_add (reloc
, schema
);
805 gvdb_table_unref (table
);
811 /* Only the first source if recursive not requested */
818 *non_relocatable
= (gchar
**) g_hash_table_get_keys_as_array (single
, NULL
);
819 g_hash_table_steal_all (single
);
824 *relocatable
= (gchar
**) g_hash_table_get_keys_as_array (reloc
, NULL
);
825 g_hash_table_steal_all (reloc
);
828 g_hash_table_unref (single
);
829 g_hash_table_unref (reloc
);
832 static gchar
**non_relocatable_schema_list
;
833 static gchar
**relocatable_schema_list
;
834 static gsize schema_lists_initialised
;
837 ensure_schema_lists (void)
839 if (g_once_init_enter (&schema_lists_initialised
))
841 initialise_schema_sources ();
843 g_settings_schema_source_list_schemas (schema_sources
, TRUE
,
844 &non_relocatable_schema_list
,
845 &relocatable_schema_list
);
847 g_once_init_leave (&schema_lists_initialised
, TRUE
);
852 * g_settings_list_schemas:
856 * Returns: (element-type utf8) (transfer none): a list of #GSettings
857 * schemas that are available. The list must not be modified or
862 * Deprecated: 2.40: Use g_settings_schema_source_list_schemas() instead.
863 * If you used g_settings_list_schemas() to check for the presence of
864 * a particular schema, use g_settings_schema_source_lookup() instead
865 * of your whole loop.
867 const gchar
* const *
868 g_settings_list_schemas (void)
870 ensure_schema_lists ();
872 return (const gchar
**) non_relocatable_schema_list
;
876 * g_settings_list_relocatable_schemas:
880 * Returns: (element-type utf8) (transfer none): a list of relocatable
881 * #GSettings schemas that are available. The list must not be
886 * Deprecated: 2.40: Use g_settings_schema_source_list_schemas() instead
888 const gchar
* const *
889 g_settings_list_relocatable_schemas (void)
891 ensure_schema_lists ();
893 return (const gchar
**) relocatable_schema_list
;
897 * g_settings_schema_ref:
898 * @schema: a #GSettingsSchema
900 * Increase the reference count of @schema, returning a new reference.
902 * Returns: a new reference to @schema
907 g_settings_schema_ref (GSettingsSchema
*schema
)
909 g_atomic_int_inc (&schema
->ref_count
);
915 * g_settings_schema_unref:
916 * @schema: a #GSettingsSchema
918 * Decrease the reference count of @schema, possibly freeing it.
923 g_settings_schema_unref (GSettingsSchema
*schema
)
925 if (g_atomic_int_dec_and_test (&schema
->ref_count
))
928 g_settings_schema_unref (schema
->extends
);
930 g_settings_schema_source_unref (schema
->source
);
931 gvdb_table_unref (schema
->table
);
932 g_free (schema
->items
);
935 g_slice_free (GSettingsSchema
, schema
);
940 g_settings_schema_get_string (GSettingsSchema
*schema
,
943 const gchar
*result
= NULL
;
946 if ((value
= gvdb_table_get_raw_value (schema
->table
, key
)))
948 result
= g_variant_get_string (value
, NULL
);
949 g_variant_unref (value
);
956 g_settings_schema_get_value (GSettingsSchema
*schema
,
959 GSettingsSchema
*s
= schema
;
961 GVariant
*value
= NULL
;
963 g_return_val_if_fail (schema
!= NULL
, NULL
);
965 for (s
= schema
; s
; s
= s
->extends
)
966 if ((value
= gvdb_table_get_raw_value (s
->table
, key
)))
969 if G_UNLIKELY (value
== NULL
|| !g_variant_is_of_type (value
, G_VARIANT_TYPE_TUPLE
))
970 g_error ("Settings schema '%s' does not contain a key named '%s'", schema
->id
, key
);
972 iter
= g_variant_iter_new (value
);
973 g_variant_unref (value
);
979 * g_settings_schema_get_path:
980 * @schema: a #GSettingsSchema
982 * Gets the path associated with @schema, or %NULL.
984 * Schemas may be single-instance or relocatable. Single-instance
985 * schemas correspond to exactly one set of keys in the backend
986 * database: those located at the path returned by this function.
988 * Relocatable schemas can be referenced by other schemas and can
989 * threfore describe multiple sets of keys at different locations. For
990 * relocatable schemas, this function will return %NULL.
992 * Returns: (transfer none): the path of the schema, or %NULL
997 g_settings_schema_get_path (GSettingsSchema
*schema
)
1003 g_settings_schema_get_gettext_domain (GSettingsSchema
*schema
)
1005 return schema
->gettext_domain
;
1009 * g_settings_schema_has_key:
1010 * @schema: a #GSettingsSchema
1011 * @name: the name of a key
1013 * Checks if @schema has a key named @name.
1015 * Returns: %TRUE if such a key exists
1020 g_settings_schema_has_key (GSettingsSchema
*schema
,
1023 return gvdb_table_has_value (schema
->table
, key
);
1027 * g_settings_schema_list_children:
1028 * @schema: a #GSettingsSchema
1030 * Gets the list of children in @schema.
1032 * You should free the return value with g_strfreev() when you are done
1035 * Returns: (transfer full) (element-type utf8): a list of the children on @settings
1040 g_settings_schema_list_children (GSettingsSchema
*schema
)
1047 g_return_val_if_fail (schema
!= NULL
, NULL
);
1049 keys
= g_settings_schema_list (schema
, &n_keys
);
1050 strv
= g_new (gchar
*, n_keys
+ 1);
1051 for (i
= j
= 0; i
< n_keys
; i
++)
1053 const gchar
*key
= g_quark_to_string (keys
[i
]);
1055 if (g_str_has_suffix (key
, "/"))
1057 gint length
= strlen (key
);
1059 strv
[j
] = g_memdup (key
, length
);
1060 strv
[j
][length
- 1] = '\0';
1070 * g_settings_schema_list_keys:
1071 * @schema: a #GSettingsSchema
1073 * Introspects the list of keys on @schema.
1075 * You should probably not be calling this function from "normal" code
1076 * (since you should already know what keys are in your schema). This
1077 * function is intended for introspection reasons.
1079 * Returns: (transfer full) (element-type utf8): a list of the keys on
1085 g_settings_schema_list_keys (GSettingsSchema
*schema
)
1092 g_return_val_if_fail (schema
!= NULL
, NULL
);
1094 keys
= g_settings_schema_list (schema
, &n_keys
);
1095 strv
= g_new (gchar
*, n_keys
+ 1);
1096 for (i
= j
= 0; i
< n_keys
; i
++)
1098 const gchar
*key
= g_quark_to_string (keys
[i
]);
1100 if (!g_str_has_suffix (key
, "/"))
1101 strv
[j
++] = g_strdup (key
);
1109 g_settings_schema_list (GSettingsSchema
*schema
,
1112 if (schema
->items
== NULL
)
1115 GHashTableIter iter
;
1121 items
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
1123 for (s
= schema
; s
; s
= s
->extends
)
1127 list
= gvdb_table_list (s
->table
, "");
1131 for (i
= 0; list
[i
]; i
++)
1132 g_hash_table_add (items
, list
[i
]); /* transfer ownership */
1134 g_free (list
); /* free container only */
1138 /* Do a first pass to eliminate child items that do not map to
1139 * valid schemas (ie: ones that would crash us if we actually
1140 * tried to create them).
1142 g_hash_table_iter_init (&iter
, items
);
1143 while (g_hash_table_iter_next (&iter
, &name
, NULL
))
1144 if (g_str_has_suffix (name
, "/"))
1146 GSettingsSchemaSource
*source
;
1147 GVariant
*child_schema
;
1148 GvdbTable
*child_table
;
1150 child_schema
= gvdb_table_get_raw_value (schema
->table
, name
);
1156 for (source
= schema
->source
; source
; source
= source
->parent
)
1157 if ((child_table
= gvdb_table_get_table (source
->table
, g_variant_get_string (child_schema
, NULL
))))
1160 g_variant_unref (child_schema
);
1162 /* Schema is not found -> remove it from the list */
1163 if (child_table
== NULL
)
1165 g_hash_table_iter_remove (&iter
);
1169 /* Make sure the schema is relocatable or at the
1172 if (gvdb_table_has_value (child_table
, ".path"))
1178 path
= gvdb_table_get_raw_value (child_table
, ".path");
1179 expected
= g_strconcat (schema
->path
, name
, NULL
);
1180 same
= g_str_equal (expected
, g_variant_get_string (path
, NULL
));
1181 g_variant_unref (path
);
1184 /* Schema is non-relocatable and did not have the
1185 * expected path -> remove it from the list
1188 g_hash_table_iter_remove (&iter
);
1191 gvdb_table_unref (child_table
);
1194 /* Now create the list */
1195 len
= g_hash_table_size (items
);
1196 schema
->items
= g_new (GQuark
, len
);
1198 g_hash_table_iter_init (&iter
, items
);
1200 while (g_hash_table_iter_next (&iter
, &name
, NULL
))
1201 schema
->items
[i
++] = g_quark_from_string (name
);
1202 schema
->n_items
= i
;
1203 g_assert (i
== len
);
1205 g_hash_table_unref (items
);
1208 *n_items
= schema
->n_items
;
1209 return schema
->items
;
1213 * g_settings_schema_get_id:
1214 * @schema: a #GSettingsSchema
1216 * Get the ID of @schema.
1218 * Returns: (transfer none): the ID
1221 g_settings_schema_get_id (GSettingsSchema
*schema
)
1227 endian_fixup (GVariant
**value
)
1229 #if G_BYTE_ORDER == G_BIG_ENDIAN
1232 tmp
= g_variant_byteswap (*value
);
1233 g_variant_unref (*value
);
1239 g_settings_schema_key_init (GSettingsSchemaKey
*key
,
1240 GSettingsSchema
*schema
,
1247 memset (key
, 0, sizeof *key
);
1249 iter
= g_settings_schema_get_value (schema
, name
);
1251 key
->schema
= g_settings_schema_ref (schema
);
1252 key
->default_value
= g_variant_iter_next_value (iter
);
1253 endian_fixup (&key
->default_value
);
1254 key
->type
= g_variant_get_type (key
->default_value
);
1255 key
->name
= g_intern_string (name
);
1257 while (g_variant_iter_next (iter
, "(y*)", &code
, &data
))
1262 /* translation requested */
1263 g_variant_get (data
, "(y&s)", &key
->lc_char
, &key
->unparsed
);
1267 /* enumerated types... */
1268 key
->is_enum
= TRUE
;
1273 key
->is_flags
= TRUE
;
1277 /* ..., choices, aliases */
1278 key
->strinfo
= g_variant_get_fixed_array (data
, &key
->strinfo_length
, sizeof (guint32
));
1282 g_variant_get (data
, "(**)", &key
->minimum
, &key
->maximum
);
1283 endian_fixup (&key
->minimum
);
1284 endian_fixup (&key
->maximum
);
1288 g_variant_get (data
, "@a{sv}", &key
->desktop_overrides
);
1289 endian_fixup (&key
->desktop_overrides
);
1293 g_warning ("unknown schema extension '%c'", code
);
1297 g_variant_unref (data
);
1300 g_variant_iter_free (iter
);
1304 g_settings_schema_key_clear (GSettingsSchemaKey
*key
)
1307 g_variant_unref (key
->minimum
);
1310 g_variant_unref (key
->maximum
);
1312 if (key
->desktop_overrides
)
1313 g_variant_unref (key
->desktop_overrides
);
1315 g_variant_unref (key
->default_value
);
1317 g_settings_schema_unref (key
->schema
);
1321 g_settings_schema_key_type_check (GSettingsSchemaKey
*key
,
1324 g_return_val_if_fail (value
!= NULL
, FALSE
);
1326 return g_variant_is_of_type (value
, key
->type
);
1330 g_settings_schema_key_range_fixup (GSettingsSchemaKey
*key
,
1333 const gchar
*target
;
1335 if (g_settings_schema_key_range_check (key
, value
))
1336 return g_variant_ref (value
);
1338 if (key
->strinfo
== NULL
)
1341 if (g_variant_is_container (value
))
1343 GVariantBuilder builder
;
1347 g_variant_iter_init (&iter
, value
);
1348 g_variant_builder_init (&builder
, g_variant_get_type (value
));
1350 while ((child
= g_variant_iter_next_value (&iter
)))
1354 fixed
= g_settings_schema_key_range_fixup (key
, child
);
1355 g_variant_unref (child
);
1359 g_variant_builder_clear (&builder
);
1363 g_variant_builder_add_value (&builder
, fixed
);
1364 g_variant_unref (fixed
);
1367 return g_variant_ref_sink (g_variant_builder_end (&builder
));
1370 target
= strinfo_string_from_alias (key
->strinfo
, key
->strinfo_length
,
1371 g_variant_get_string (value
, NULL
));
1372 return target
? g_variant_ref_sink (g_variant_new_string (target
)) : NULL
;
1376 g_settings_schema_key_get_translated_default (GSettingsSchemaKey
*key
)
1378 const gchar
*translated
;
1379 GError
*error
= NULL
;
1380 const gchar
*domain
;
1383 domain
= g_settings_schema_get_gettext_domain (key
->schema
);
1385 if (key
->lc_char
== '\0')
1386 /* translation not requested for this key */
1389 if (key
->lc_char
== 't')
1390 translated
= g_dcgettext (domain
, key
->unparsed
, LC_TIME
);
1392 translated
= g_dgettext (domain
, key
->unparsed
);
1394 if (translated
== key
->unparsed
)
1395 /* the default value was not translated */
1398 /* try to parse the translation of the unparsed default */
1399 value
= g_variant_parse (key
->type
, translated
, NULL
, NULL
, &error
);
1403 g_warning ("Failed to parse translated string '%s' for "
1404 "key '%s' in schema '%s': %s", translated
, key
->name
,
1405 g_settings_schema_get_id (key
->schema
), error
->message
);
1406 g_warning ("Using untranslated default instead.");
1407 g_error_free (error
);
1410 else if (!g_settings_schema_key_range_check (key
, value
))
1412 g_warning ("Translated default '%s' for key '%s' in schema '%s' "
1413 "is outside of valid range", key
->unparsed
, key
->name
,
1414 g_settings_schema_get_id (key
->schema
));
1415 g_variant_unref (value
);
1423 g_settings_schema_key_get_per_desktop_default (GSettingsSchemaKey
*key
)
1425 static const gchar
* const *current_desktops
;
1426 GVariant
*value
= NULL
;
1429 if (!key
->desktop_overrides
)
1432 if (g_once_init_enter (¤t_desktops
))
1434 const gchar
*xdg_current_desktop
= g_getenv ("XDG_CURRENT_DESKTOP");
1437 if (xdg_current_desktop
!= NULL
&& xdg_current_desktop
[0] != '\0')
1438 tmp
= g_strsplit (xdg_current_desktop
, G_SEARCHPATH_SEPARATOR_S
, -1);
1440 tmp
= g_new0 (gchar
*, 0 + 1);
1442 g_once_init_leave (¤t_desktops
, (const gchar
**) tmp
);
1445 for (i
= 0; value
== NULL
&& current_desktops
[i
] != NULL
; i
++)
1446 value
= g_variant_lookup_value (key
->desktop_overrides
, current_desktops
[i
], NULL
);
1452 g_settings_schema_key_to_enum (GSettingsSchemaKey
*key
,
1458 it_worked
= strinfo_enum_from_string (key
->strinfo
, key
->strinfo_length
,
1459 g_variant_get_string (value
, NULL
),
1462 /* 'value' can only come from the backend after being filtered for validity,
1463 * from the translation after being filtered for validity, or from the schema
1464 * itself (which the schema compiler checks for validity). If this assertion
1465 * fails then it's really a bug in GSettings or the schema compiler...
1467 g_assert (it_worked
);
1473 g_settings_schema_key_from_enum (GSettingsSchemaKey
*key
,
1476 const gchar
*string
;
1478 string
= strinfo_string_from_enum (key
->strinfo
, key
->strinfo_length
, value
);
1483 return g_variant_new_string (string
);
1487 g_settings_schema_key_to_flags (GSettingsSchemaKey
*key
,
1495 g_variant_iter_init (&iter
, value
);
1496 while (g_variant_iter_next (&iter
, "&s", &flag
))
1501 it_worked
= strinfo_enum_from_string (key
->strinfo
, key
->strinfo_length
, flag
, &flag_value
);
1502 /* as in g_settings_to_enum() */
1503 g_assert (it_worked
);
1505 result
|= flag_value
;
1512 g_settings_schema_key_from_flags (GSettingsSchemaKey
*key
,
1515 GVariantBuilder builder
;
1518 g_variant_builder_init (&builder
, G_VARIANT_TYPE ("as"));
1520 for (i
= 0; i
< 32; i
++)
1521 if (value
& (1u << i
))
1523 const gchar
*string
;
1525 string
= strinfo_string_from_enum (key
->strinfo
, key
->strinfo_length
, 1u << i
);
1529 g_variant_builder_clear (&builder
);
1533 g_variant_builder_add (&builder
, "s", string
);
1536 return g_variant_builder_end (&builder
);
1539 G_DEFINE_BOXED_TYPE (GSettingsSchemaKey
, g_settings_schema_key
, g_settings_schema_key_ref
, g_settings_schema_key_unref
)
1542 * g_settings_schema_key_ref:
1543 * @key: a #GSettingsSchemaKey
1545 * Increase the reference count of @key, returning a new reference.
1547 * Returns: a new reference to @key
1551 GSettingsSchemaKey
*
1552 g_settings_schema_key_ref (GSettingsSchemaKey
*key
)
1554 g_return_val_if_fail (key
!= NULL
, NULL
);
1556 g_atomic_int_inc (&key
->ref_count
);
1562 * g_settings_schema_key_unref:
1563 * @key: a #GSettingsSchemaKey
1565 * Decrease the reference count of @key, possibly freeing it.
1570 g_settings_schema_key_unref (GSettingsSchemaKey
*key
)
1572 g_return_if_fail (key
!= NULL
);
1574 if (g_atomic_int_dec_and_test (&key
->ref_count
))
1576 g_settings_schema_key_clear (key
);
1578 g_slice_free (GSettingsSchemaKey
, key
);
1583 * g_settings_schema_get_key:
1584 * @schema: a #GSettingsSchema
1585 * @name: the name of a key
1587 * Gets the key named @name from @schema.
1589 * It is a programmer error to request a key that does not exist. See
1590 * g_settings_schema_list_keys().
1592 * Returns: (transfer full): the #GSettingsSchemaKey for @name
1596 GSettingsSchemaKey
*
1597 g_settings_schema_get_key (GSettingsSchema
*schema
,
1600 GSettingsSchemaKey
*key
;
1602 g_return_val_if_fail (schema
!= NULL
, NULL
);
1603 g_return_val_if_fail (name
!= NULL
, NULL
);
1605 key
= g_slice_new (GSettingsSchemaKey
);
1606 g_settings_schema_key_init (key
, schema
, name
);
1613 * g_settings_schema_key_get_name:
1614 * @key: a #GSettingsSchemaKey
1616 * Gets the name of @key.
1618 * Returns: the name of @key.
1623 g_settings_schema_key_get_name (GSettingsSchemaKey
*key
)
1625 g_return_val_if_fail (key
!= NULL
, NULL
);
1631 * g_settings_schema_key_get_summary:
1632 * @key: a #GSettingsSchemaKey
1634 * Gets the summary for @key.
1636 * If no summary has been provided in the schema for @key, returns
1639 * The summary is a short description of the purpose of the key; usually
1640 * one short sentence. Summaries can be translated and the value
1641 * returned from this function is is the current locale.
1643 * This function is slow. The summary and description information for
1644 * the schemas is not stored in the compiled schema database so this
1645 * function has to parse all of the source XML files in the schema
1648 * Returns: the summary for @key, or %NULL
1653 g_settings_schema_key_get_summary (GSettingsSchemaKey
*key
)
1655 GHashTable
**text_tables
;
1656 GHashTable
*summaries
;
1658 text_tables
= g_settings_schema_source_get_text_tables (key
->schema
->source
);
1659 summaries
= g_hash_table_lookup (text_tables
[0], key
->schema
->id
);
1661 return summaries
? g_hash_table_lookup (summaries
, key
->name
) : NULL
;
1665 * g_settings_schema_key_get_description:
1666 * @key: a #GSettingsSchemaKey
1668 * Gets the description for @key.
1670 * If no description has been provided in the schema for @key, returns
1673 * The description can be one sentence to several paragraphs in length.
1674 * Paragraphs are delimited with a double newline. Descriptions can be
1675 * translated and the value returned from this function is is the
1678 * This function is slow. The summary and description information for
1679 * the schemas is not stored in the compiled schema database so this
1680 * function has to parse all of the source XML files in the schema
1683 * Returns: the description for @key, or %NULL
1688 g_settings_schema_key_get_description (GSettingsSchemaKey
*key
)
1690 GHashTable
**text_tables
;
1691 GHashTable
*descriptions
;
1693 text_tables
= g_settings_schema_source_get_text_tables (key
->schema
->source
);
1694 descriptions
= g_hash_table_lookup (text_tables
[1], key
->schema
->id
);
1696 return descriptions
? g_hash_table_lookup (descriptions
, key
->name
) : NULL
;
1700 * g_settings_schema_key_get_value_type:
1701 * @key: a #GSettingsSchemaKey
1703 * Gets the #GVariantType of @key.
1705 * Returns: (transfer none): the type of @key
1709 const GVariantType
*
1710 g_settings_schema_key_get_value_type (GSettingsSchemaKey
*key
)
1712 g_return_val_if_fail (key
, NULL
);
1718 * g_settings_schema_key_get_default_value:
1719 * @key: a #GSettingsSchemaKey
1721 * Gets the default value for @key.
1723 * Note that this is the default value according to the schema. System
1724 * administrator defaults and lockdown are not visible via this API.
1726 * Returns: (transfer full): the default value for the key
1731 g_settings_schema_key_get_default_value (GSettingsSchemaKey
*key
)
1735 g_return_val_if_fail (key
, NULL
);
1737 value
= g_settings_schema_key_get_translated_default (key
);
1740 value
= g_settings_schema_key_get_per_desktop_default (key
);
1743 value
= g_variant_ref (key
->default_value
);
1749 * g_settings_schema_key_get_range:
1750 * @key: a #GSettingsSchemaKey
1752 * Queries the range of a key.
1754 * This function will return a #GVariant that fully describes the range
1755 * of values that are valid for @key.
1757 * The type of #GVariant returned is `(sv)`. The string describes
1758 * the type of range restriction in effect. The type and meaning of
1759 * the value contained in the variant depends on the string.
1761 * If the string is `'type'` then the variant contains an empty array.
1762 * The element type of that empty array is the expected type of value
1763 * and all values of that type are valid.
1765 * If the string is `'enum'` then the variant contains an array
1766 * enumerating the possible values. Each item in the array is
1767 * a possible valid value and no other values are valid.
1769 * If the string is `'flags'` then the variant contains an array. Each
1770 * item in the array is a value that may appear zero or one times in an
1771 * array to be used as the value for this key. For example, if the
1772 * variant contained the array `['x', 'y']` then the valid values for
1773 * the key would be `[]`, `['x']`, `['y']`, `['x', 'y']` and
1776 * Finally, if the string is `'range'` then the variant contains a pair
1777 * of like-typed values -- the minimum and maximum permissible values
1780 * This information should not be used by normal programs. It is
1781 * considered to be a hint for introspection purposes. Normal programs
1782 * should already know what is permitted by their own schema. The
1783 * format may change in any way in the future -- but particularly, new
1784 * forms may be added to the possibilities described above.
1786 * You should free the returned value with g_variant_unref() when it is
1789 * Returns: (transfer full): a #GVariant describing the range
1794 g_settings_schema_key_get_range (GSettingsSchemaKey
*key
)
1801 range
= g_variant_new ("(**)", key
->minimum
, key
->maximum
);
1804 else if (key
->strinfo
)
1806 range
= strinfo_enumerate (key
->strinfo
, key
->strinfo_length
);
1807 type
= key
->is_flags
? "flags" : "enum";
1811 range
= g_variant_new_array (key
->type
, NULL
, 0);
1815 return g_variant_ref_sink (g_variant_new ("(sv)", type
, range
));
1819 * g_settings_schema_key_range_check:
1820 * @key: a #GSettingsSchemaKey
1821 * @value: the value to check
1823 * Checks if the given @value is of the correct type and within the
1824 * permitted range for @key.
1826 * It is a programmer error if @value is not of the correct type -- you
1827 * must check for this first.
1829 * Returns: %TRUE if @value is valid for @key
1834 g_settings_schema_key_range_check (GSettingsSchemaKey
*key
,
1837 if (key
->minimum
== NULL
&& key
->strinfo
== NULL
)
1840 if (g_variant_is_container (value
))
1846 g_variant_iter_init (&iter
, value
);
1847 while (ok
&& (child
= g_variant_iter_next_value (&iter
)))
1849 ok
= g_settings_schema_key_range_check (key
, child
);
1850 g_variant_unref (child
);
1858 return g_variant_compare (key
->minimum
, value
) <= 0 &&
1859 g_variant_compare (value
, key
->maximum
) <= 0;
1862 return strinfo_is_string_valid (key
->strinfo
, key
->strinfo_length
,
1863 g_variant_get_string (value
, NULL
));