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 of the licence, 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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include "gsettingsschema-internal.h"
24 #include "gsettings.h"
26 #include "gvdb/gvdb-reader.h"
34 * SECTION:gsettingsschema
35 * @short_description: Introspecting and controlling the loading of
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:
60 * GSettingsSchemaSource *schema_source;
65 * initialise_plugin (const gchar *dir)
71 * plugin->schema_source =
72 * g_settings_new_schema_source_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
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.
135 * This is an opaque structure type. You may not access it directly.
139 struct _GSettingsSchema
141 GSettingsSchemaSource
*source
;
142 const gchar
*gettext_domain
;
149 GSettingsSchema
*extends
;
155 * G_TYPE_SETTINGS_SCHEMA_SOURCE:
157 * A boxed #GType corresponding to #GSettingsSchemaSource.
161 G_DEFINE_BOXED_TYPE (GSettingsSchemaSource
, g_settings_schema_source
, g_settings_schema_source_ref
, g_settings_schema_source_unref
)
164 * G_TYPE_SETTINGS_SCHEMA:
166 * A boxed #GType corresponding to #GSettingsSchema.
170 G_DEFINE_BOXED_TYPE (GSettingsSchema
, g_settings_schema
, g_settings_schema_ref
, g_settings_schema_unref
)
173 * GSettingsSchemaSource:
175 * This is an opaque structure type. You may not access it directly.
179 struct _GSettingsSchemaSource
181 GSettingsSchemaSource
*parent
;
184 GHashTable
**text_tables
;
189 static GSettingsSchemaSource
*schema_sources
;
192 * g_settings_schema_source_ref:
193 * @source: a #GSettingsSchemaSource
195 * Increase the reference count of @source, returning a new reference.
197 * Returns: a new reference to @source
201 GSettingsSchemaSource
*
202 g_settings_schema_source_ref (GSettingsSchemaSource
*source
)
204 g_atomic_int_inc (&source
->ref_count
);
210 * g_settings_schema_source_unref:
211 * @source: a #GSettingsSchemaSource
213 * Decrease the reference count of @source, possibly freeing it.
218 g_settings_schema_source_unref (GSettingsSchemaSource
*source
)
220 if (g_atomic_int_dec_and_test (&source
->ref_count
))
222 if (source
== schema_sources
)
223 g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
226 g_settings_schema_source_unref (source
->parent
);
227 gvdb_table_unref (source
->table
);
228 g_free (source
->directory
);
230 if (source
->text_tables
)
232 g_hash_table_unref (source
->text_tables
[0]);
233 g_hash_table_unref (source
->text_tables
[1]);
234 g_free (source
->text_tables
);
237 g_slice_free (GSettingsSchemaSource
, source
);
242 * g_settings_schema_source_new_from_directory:
243 * @directory: the filename of a directory
244 * @parent: (allow-none): a #GSettingsSchemaSource, or %NULL
245 * @trusted: %TRUE, if the directory is trusted
246 * @error: a pointer to a #GError pointer set to %NULL, or %NULL
248 * Attempts to create a new schema source corresponding to the contents
249 * of the given directory.
251 * This function is not required for normal uses of #GSettings but it
252 * may be useful to authors of plugin management systems.
254 * The directory should contain a file called
255 * <filename>gschemas.compiled</filename> as produced by
256 * <command>glib-compile-schemas</command>.
258 * If @trusted is %TRUE then <filename>gschemas.compiled</filename> is
259 * trusted not to be corrupted. This assumption has a performance
260 * advantage, but can result in crashes or inconsistent behaviour in the
261 * case of a corrupted file. Generally, you should set @trusted to
262 * %TRUE for files installed by the system and to %FALSE for files in
263 * the home directory.
265 * If @parent is non-%NULL then there are two effects.
267 * First, if g_settings_schema_source_lookup() is called with the
268 * @recursive flag set to %TRUE and the schema can not be found in the
269 * source, the lookup will recurse to the parent.
271 * Second, any references to other schemas specified within this
272 * source (ie: <literal>child</literal> or <literal>extends</literal>)
273 * references may be resolved from the @parent.
275 * For this second reason, except in very unusual situations, the
276 * @parent should probably be given as the default schema source, as
277 * returned by g_settings_schema_source_get_default().
281 GSettingsSchemaSource
*
282 g_settings_schema_source_new_from_directory (const gchar
*directory
,
283 GSettingsSchemaSource
*parent
,
287 GSettingsSchemaSource
*source
;
291 filename
= g_build_filename (directory
, "gschemas.compiled", NULL
);
292 table
= gvdb_table_new (filename
, trusted
, error
);
298 source
= g_slice_new (GSettingsSchemaSource
);
299 source
->directory
= g_strdup (directory
);
300 source
->parent
= parent
? g_settings_schema_source_ref (parent
) : NULL
;
301 source
->text_tables
= NULL
;
302 source
->table
= table
;
303 source
->ref_count
= 1;
309 try_prepend_dir (const gchar
*directory
)
311 GSettingsSchemaSource
*source
;
313 source
= g_settings_schema_source_new_from_directory (directory
, schema_sources
, TRUE
, NULL
);
315 /* If we successfully created it then prepend it to the global list */
317 schema_sources
= source
;
321 initialise_schema_sources (void)
323 static gsize initialised
;
325 /* need a separate variable because 'schema_sources' may legitimately
326 * be null if we have zero valid schema sources
328 if G_UNLIKELY (g_once_init_enter (&initialised
))
330 const gchar
* const *dirs
;
334 /* iterate in reverse: count up, then count down */
335 dirs
= g_get_system_data_dirs ();
336 for (i
= 0; dirs
[i
]; i
++);
342 dirname
= g_build_filename (dirs
[i
], "glib-2.0", "schemas", NULL
);
343 try_prepend_dir (dirname
);
347 if ((path
= g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL
)
348 try_prepend_dir (path
);
350 g_once_init_leave (&initialised
, TRUE
);
355 * g_settings_schema_source_get_default:
357 * Gets the default system schema source.
359 * This function is not required for normal uses of #GSettings but it
360 * may be useful to authors of plugin management systems or to those who
361 * want to introspect the content of schemas.
363 * If no schemas are installed, %NULL will be returned.
365 * The returned source may actually consist of multiple schema sources
366 * from different directories, depending on which directories were given
367 * in <envar>XDG_DATA_DIRS</envar> and
368 * <envar>GSETTINGS_SCHEMA_DIR</envar>. For this reason, all lookups
369 * performed against the default source should probably be done
372 * Returns: (transfer none): the default schema source
376 GSettingsSchemaSource
*
377 g_settings_schema_source_get_default (void)
379 initialise_schema_sources ();
381 return schema_sources
;
385 * g_settings_schema_source_lookup:
386 * @source: a #GSettingsSchemaSource
387 * @schema_id: a schema ID
388 * @recursive: %TRUE if the lookup should be recursive
390 * Looks up a schema with the identifier @schema_id in @source.
392 * This function is not required for normal uses of #GSettings but it
393 * may be useful to authors of plugin management systems or to those who
394 * want to introspect the content of schemas.
396 * If the schema isn't found directly in @source and @recursive is %TRUE
397 * then the parent sources will also be checked.
399 * If the schema isn't found, %NULL is returned.
401 * Returns: (transfer full): a new #GSettingsSchema
406 g_settings_schema_source_lookup (GSettingsSchemaSource
*source
,
407 const gchar
*schema_id
,
410 GSettingsSchema
*schema
;
412 const gchar
*extends
;
414 g_return_val_if_fail (source
!= NULL
, NULL
);
415 g_return_val_if_fail (schema_id
!= NULL
, NULL
);
417 table
= gvdb_table_get_table (source
->table
, schema_id
);
419 if (table
== NULL
&& recursive
)
420 for (source
= source
->parent
; source
; source
= source
->parent
)
421 if ((table
= gvdb_table_get_table (source
->table
, schema_id
)))
427 schema
= g_slice_new0 (GSettingsSchema
);
428 schema
->source
= g_settings_schema_source_ref (source
);
429 schema
->ref_count
= 1;
430 schema
->id
= g_strdup (schema_id
);
431 schema
->table
= table
;
432 schema
->path
= g_settings_schema_get_string (schema
, ".path");
433 schema
->gettext_domain
= g_settings_schema_get_string (schema
, ".gettext-domain");
435 if (schema
->gettext_domain
)
436 bind_textdomain_codeset (schema
->gettext_domain
, "UTF-8");
438 extends
= g_settings_schema_get_string (schema
, ".extends");
441 schema
->extends
= g_settings_schema_source_lookup (source
, extends
, TRUE
);
442 if (schema
->extends
== NULL
)
443 g_warning ("Schema '%s' extends schema '%s' but we could not find it", schema_id
, extends
);
451 GHashTable
*summaries
;
452 GHashTable
*descriptions
;
453 GSList
*gettext_domain
;
457 } TextTableParseInfo
;
460 get_attribute_value (GSList
*list
)
464 for (node
= list
; node
; node
= node
->next
)
472 pop_attribute_value (GSList
**list
)
477 *list
= g_slist_remove (*list
, top
);
483 push_attribute_value (GSList
**list
,
486 *list
= g_slist_prepend (*list
, g_strdup (value
));
490 start_element (GMarkupParseContext
*context
,
491 const gchar
*element_name
,
492 const gchar
**attribute_names
,
493 const gchar
**attribute_values
,
497 TextTableParseInfo
*info
= user_data
;
498 const gchar
*gettext_domain
= NULL
;
499 const gchar
*schema_id
= NULL
;
500 const gchar
*key_name
= NULL
;
503 for (i
= 0; attribute_names
[i
]; i
++)
505 if (g_str_equal (attribute_names
[i
], "gettext-domain"))
506 gettext_domain
= attribute_values
[i
];
507 else if (g_str_equal (attribute_names
[i
], "id"))
508 schema_id
= attribute_values
[i
];
509 else if (g_str_equal (attribute_names
[i
], "name"))
510 key_name
= attribute_values
[i
];
513 push_attribute_value (&info
->gettext_domain
, gettext_domain
);
514 push_attribute_value (&info
->schema_id
, schema_id
);
515 push_attribute_value (&info
->key_name
, key_name
);
519 g_string_free (info
->string
, TRUE
);
523 if (g_str_equal (element_name
, "summary") || g_str_equal (element_name
, "description"))
524 info
->string
= g_string_new (NULL
);
528 normalise_whitespace (const gchar
*orig
)
530 /* We normalise by the same rules as in intltool:
539 * $message = join "\n\n", map &cleanup, split/\n\s*\n+/, $message;
541 * Where \s is an ascii space character.
543 * We aim for ease of implementation over efficiency -- this code is
544 * not run in normal applications.
546 static GRegex
*cleanup
[3];
547 static GRegex
*splitter
;
552 if (g_once_init_enter (&splitter
))
556 cleanup
[0] = g_regex_new ("^\\s+", 0, 0, 0);
557 cleanup
[1] = g_regex_new ("\\s+$", 0, 0, 0);
558 cleanup
[2] = g_regex_new ("\\s+", 0, 0, 0);
559 s
= g_regex_new ("\\n\\s*\\n+", 0, 0, 0);
561 g_once_init_leave (&splitter
, s
);
564 lines
= g_regex_split (splitter
, orig
, 0);
565 for (i
= 0; lines
[i
]; i
++)
569 a
= g_regex_replace_literal (cleanup
[0], lines
[i
], -1, 0, "", 0, 0);
570 b
= g_regex_replace_literal (cleanup
[1], a
, -1, 0, "", 0, 0);
571 c
= g_regex_replace_literal (cleanup
[2], b
, -1, 0, " ", 0, 0);
578 result
= g_strjoinv ("\n\n", lines
);
585 end_element (GMarkupParseContext
*context
,
586 const gchar
*element_name
,
590 TextTableParseInfo
*info
= user_data
;
592 pop_attribute_value (&info
->gettext_domain
);
593 pop_attribute_value (&info
->schema_id
);
594 pop_attribute_value (&info
->key_name
);
598 GHashTable
*source_table
= NULL
;
599 const gchar
*gettext_domain
;
600 const gchar
*schema_id
;
601 const gchar
*key_name
;
603 gettext_domain
= get_attribute_value (info
->gettext_domain
);
604 schema_id
= get_attribute_value (info
->schema_id
);
605 key_name
= get_attribute_value (info
->key_name
);
607 if (g_str_equal (element_name
, "summary"))
608 source_table
= info
->summaries
;
609 else if (g_str_equal (element_name
, "description"))
610 source_table
= info
->descriptions
;
612 if (source_table
&& schema_id
&& key_name
)
614 GHashTable
*schema_table
;
617 schema_table
= g_hash_table_lookup (source_table
, schema_id
);
619 if (schema_table
== NULL
)
621 schema_table
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, g_free
);
622 g_hash_table_insert (source_table
, g_strdup (schema_id
), schema_table
);
625 normalised
= normalise_whitespace (info
->string
->str
);
631 translated
= g_strdup (g_dgettext (gettext_domain
, normalised
));
633 normalised
= translated
;
636 g_hash_table_insert (schema_table
, g_strdup (key_name
), normalised
);
639 g_string_free (info
->string
, TRUE
);
645 text (GMarkupParseContext
*context
,
651 TextTableParseInfo
*info
= user_data
;
654 g_string_append_len (info
->string
, text
, text_len
);
658 parse_into_text_tables (const gchar
*directory
,
659 GHashTable
*summaries
,
660 GHashTable
*descriptions
)
662 GMarkupParser parser
= { start_element
, end_element
, text
};
663 TextTableParseInfo info
= { summaries
, descriptions
};
664 const gchar
*basename
;
667 dir
= g_dir_open (directory
, 0, NULL
);
668 while ((basename
= g_dir_read_name (dir
)))
674 filename
= g_build_filename (directory
, basename
, NULL
);
675 if (g_file_get_contents (filename
, &contents
, &size
, NULL
))
677 GMarkupParseContext
*context
;
679 context
= g_markup_parse_context_new (&parser
, G_MARKUP_TREAT_CDATA_AS_TEXT
, &info
, NULL
);
680 if (g_markup_parse_context_parse (context
, contents
, size
, NULL
))
681 g_markup_parse_context_end_parse (context
, NULL
);
682 g_markup_parse_context_free (context
);
684 /* Clean up dangling stuff in case there was an error. */
685 g_slist_free_full (info
.gettext_domain
, g_free
);
686 g_slist_free_full (info
.schema_id
, g_free
);
687 g_slist_free_full (info
.key_name
, g_free
);
689 info
.gettext_domain
= NULL
;
690 info
.schema_id
= NULL
;
691 info
.key_name
= NULL
;
695 g_string_free (info
.string
, TRUE
);
707 g_settings_schema_source_get_text_tables (GSettingsSchemaSource
*source
)
709 if (g_once_init_enter (&source
->text_tables
))
711 GHashTable
**text_tables
;
713 text_tables
= g_new (GHashTable
*, 2);
714 text_tables
[0] = g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
) g_hash_table_unref
);
715 text_tables
[1] = g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
) g_hash_table_unref
);
717 if (source
->directory
)
718 parse_into_text_tables (source
->directory
, text_tables
[0], text_tables
[1]);
720 g_once_init_leave (&source
->text_tables
, text_tables
);
723 return source
->text_tables
;
727 * g_settings_schema_source_list_schemas:
728 * @source: a #GSettingsSchemaSource
729 * @recursive: if we should recurse
730 * @non_relocatable_schemas: (out) (transfer full): the list of
731 * non-relocatable schemas
732 * @relocatable_schemas: (out) (transfer full): the list of relocatable
735 * Lists the schemas in a given source.
737 * If @recursive is %TRUE then include parent sources. If %FALSE then
738 * only include the schemas from one source (ie: one directory). You
739 * probably want %TRUE.
741 * Non-relocatable schemas are those for which you can call
742 * g_settings_new(). Relocatable schemas are those for which you must
743 * use g_settings_new_with_path().
745 * Do not call this function from normal programs. This is designed for
746 * use by database editors, commandline tools, etc.
751 g_settings_schema_source_list_schemas (GSettingsSchemaSource
*source
,
753 gchar
***non_relocatable_schemas
,
754 gchar
***relocatable_schemas
)
756 GHashTable
*single
, *reloc
;
757 GSettingsSchemaSource
*s
;
759 /* We use hash tables to avoid duplicate listings for schemas that
760 * appear in more than one file.
762 single
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
763 reloc
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
765 for (s
= source
; s
; s
= s
->parent
)
770 list
= gvdb_table_list (s
->table
, "");
772 /* empty schema cache file? */
776 for (i
= 0; list
[i
]; i
++)
778 if (!g_hash_table_lookup (single
, list
[i
]) &&
779 !g_hash_table_lookup (reloc
, list
[i
]))
783 table
= gvdb_table_get_table (s
->table
, list
[i
]);
784 g_assert (table
!= NULL
);
786 if (gvdb_table_has_value (table
, ".path"))
787 g_hash_table_insert (single
, g_strdup (list
[i
]), NULL
);
789 g_hash_table_insert (reloc
, g_strdup (list
[i
]), NULL
);
791 gvdb_table_unref (table
);
797 /* Only the first source if recursive not requested */
802 if (non_relocatable_schemas
)
804 *non_relocatable_schemas
= (gchar
**) g_hash_table_get_keys_as_array (single
, NULL
);
805 g_hash_table_steal_all (single
);
808 if (relocatable_schemas
)
810 *relocatable_schemas
= (gchar
**) g_hash_table_get_keys_as_array (reloc
, NULL
);
811 g_hash_table_steal_all (reloc
);
814 g_hash_table_unref (single
);
815 g_hash_table_unref (reloc
);
818 static gchar
**non_relocatable_schema_list
;
819 static gchar
**relocatable_schema_list
;
820 static gsize schema_lists_initialised
;
823 ensure_schema_lists (void)
825 if (g_once_init_enter (&schema_lists_initialised
))
827 initialise_schema_sources ();
829 g_settings_schema_source_list_schemas (schema_sources
, TRUE
,
830 &non_relocatable_schema_list
,
831 &relocatable_schema_list
);
833 g_once_init_leave (&schema_lists_initialised
, TRUE
);
838 * g_settings_list_schemas:
840 * Returns: (element-type utf8) (transfer none): a list of #GSettings
841 * schemas that are available. The list must not be modified or
846 * Deprecated:2.40: Use g_settings_schema_source_list_schemas() instead
848 const gchar
* const *
849 g_settings_list_schemas (void)
851 ensure_schema_lists ();
853 return (const gchar
**) non_relocatable_schema_list
;
857 * g_settings_list_relocatable_schemas:
859 * Returns: (element-type utf8) (transfer none): a list of relocatable
860 * #GSettings schemas that are available. The list must not be
865 * Deprecated:2.40: Use g_settings_schema_source_list_schemas() instead
867 const gchar
* const *
868 g_settings_list_relocatable_schemas (void)
870 ensure_schema_lists ();
872 return (const gchar
**) relocatable_schema_list
;
876 * g_settings_schema_ref:
877 * @schema: a #GSettingsSchema
879 * Increase the reference count of @schema, returning a new reference.
881 * Returns: a new reference to @schema
886 g_settings_schema_ref (GSettingsSchema
*schema
)
888 g_atomic_int_inc (&schema
->ref_count
);
894 * g_settings_schema_unref:
895 * @schema: a #GSettingsSchema
897 * Decrease the reference count of @schema, possibly freeing it.
902 g_settings_schema_unref (GSettingsSchema
*schema
)
904 if (g_atomic_int_dec_and_test (&schema
->ref_count
))
907 g_settings_schema_unref (schema
->extends
);
909 g_settings_schema_source_unref (schema
->source
);
910 gvdb_table_unref (schema
->table
);
911 g_free (schema
->items
);
914 g_slice_free (GSettingsSchema
, schema
);
919 g_settings_schema_get_string (GSettingsSchema
*schema
,
922 const gchar
*result
= NULL
;
925 if ((value
= gvdb_table_get_raw_value (schema
->table
, key
)))
927 result
= g_variant_get_string (value
, NULL
);
928 g_variant_unref (value
);
935 g_settings_schema_get_value (GSettingsSchema
*schema
,
938 GSettingsSchema
*s
= schema
;
942 g_return_val_if_fail (schema
!= NULL
, NULL
);
944 for (s
= schema
; s
; s
= schema
->extends
)
945 if ((value
= gvdb_table_get_raw_value (schema
->table
, key
)))
948 if G_UNLIKELY (value
== NULL
|| !g_variant_is_of_type (value
, G_VARIANT_TYPE_TUPLE
))
949 g_error ("Settings schema '%s' does not contain a key named '%s'", schema
->id
, key
);
951 iter
= g_variant_iter_new (value
);
952 g_variant_unref (value
);
958 * g_settings_schema_get_path:
959 * @schema: a #GSettingsSchema
961 * Gets the path associated with @schema, or %NULL.
963 * Schemas may be single-instance or relocatable. Single-instance
964 * schemas correspond to exactly one set of keys in the backend
965 * database: those located at the path returned by this function.
967 * Relocatable schemas can be referenced by other schemas and can
968 * threfore describe multiple sets of keys at different locations. For
969 * relocatable schemas, this function will return %NULL.
971 * Returns: (transfer none): the path of the schema, or %NULL
976 g_settings_schema_get_path (GSettingsSchema
*schema
)
982 g_settings_schema_get_gettext_domain (GSettingsSchema
*schema
)
984 return schema
->gettext_domain
;
988 g_settings_schema_has_key (GSettingsSchema
*schema
,
991 return gvdb_table_has_value (schema
->table
, key
);
995 g_settings_schema_list (GSettingsSchema
*schema
,
998 if (schema
->items
== NULL
)
1001 GHashTableIter iter
;
1007 items
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
1009 for (s
= schema
; s
; s
= s
->extends
)
1013 list
= gvdb_table_list (s
->table
, "");
1017 for (i
= 0; list
[i
]; i
++)
1018 g_hash_table_add (items
, list
[i
]); /* transfer ownership */
1020 g_free (list
); /* free container only */
1024 /* Do a first pass to eliminate child items that do not map to
1025 * valid schemas (ie: ones that would crash us if we actually
1026 * tried to create them).
1028 g_hash_table_iter_init (&iter
, items
);
1029 while (g_hash_table_iter_next (&iter
, &name
, NULL
))
1030 if (g_str_has_suffix (name
, "/"))
1032 GSettingsSchemaSource
*source
;
1033 GVariant
*child_schema
;
1034 GvdbTable
*child_table
;
1036 child_schema
= gvdb_table_get_raw_value (schema
->table
, name
);
1042 for (source
= schema_sources
; source
; source
= source
->parent
)
1043 if ((child_table
= gvdb_table_get_table (source
->table
, g_variant_get_string (child_schema
, NULL
))))
1046 g_variant_unref (child_schema
);
1048 /* Schema is not found -> remove it from the list */
1049 if (child_table
== NULL
)
1051 g_hash_table_iter_remove (&iter
);
1055 /* Make sure the schema is relocatable or at the
1058 if (gvdb_table_has_value (child_table
, ".path"))
1064 path
= gvdb_table_get_raw_value (child_table
, ".path");
1065 expected
= g_strconcat (schema
->path
, name
, NULL
);
1066 same
= g_str_equal (expected
, g_variant_get_string (path
, NULL
));
1067 g_variant_unref (path
);
1070 /* Schema is non-relocatable and did not have the
1071 * expected path -> remove it from the list
1074 g_hash_table_iter_remove (&iter
);
1077 gvdb_table_unref (child_table
);
1080 /* Now create the list */
1081 len
= g_hash_table_size (items
);
1082 schema
->items
= g_new (GQuark
, len
);
1084 g_hash_table_iter_init (&iter
, items
);
1086 while (g_hash_table_iter_next (&iter
, &name
, NULL
))
1087 schema
->items
[i
++] = g_quark_from_string (name
);
1088 schema
->n_items
= i
;
1089 g_assert (i
== len
);
1091 g_hash_table_unref (items
);
1094 *n_items
= schema
->n_items
;
1095 return schema
->items
;
1099 * g_settings_schema_get_id:
1100 * @schema: a #GSettingsSchema
1102 * Get the ID of @schema.
1104 * Returns: (transfer none): the ID
1107 g_settings_schema_get_id (GSettingsSchema
*schema
)
1113 endian_fixup (GVariant
**value
)
1115 #if G_BYTE_ORDER == G_BIG_ENDIAN
1118 tmp
= g_variant_byteswap (*value
);
1119 g_variant_unref (*value
);
1125 g_settings_schema_key_init (GSettingsSchemaKey
*key
,
1126 GSettingsSchema
*schema
,
1133 memset (key
, 0, sizeof *key
);
1135 iter
= g_settings_schema_get_value (schema
, name
);
1137 key
->schema
= g_settings_schema_ref (schema
);
1138 key
->default_value
= g_variant_iter_next_value (iter
);
1139 endian_fixup (&key
->default_value
);
1140 key
->type
= g_variant_get_type (key
->default_value
);
1141 key
->name
= g_intern_string (name
);
1143 while (g_variant_iter_next (iter
, "(y*)", &code
, &data
))
1148 /* translation requested */
1149 g_variant_get (data
, "(y&s)", &key
->lc_char
, &key
->unparsed
);
1153 /* enumerated types... */
1154 key
->is_enum
= TRUE
;
1159 key
->is_flags
= TRUE
;
1163 /* ..., choices, aliases */
1164 key
->strinfo
= g_variant_get_fixed_array (data
, &key
->strinfo_length
, sizeof (guint32
));
1168 g_variant_get (data
, "(**)", &key
->minimum
, &key
->maximum
);
1169 endian_fixup (&key
->minimum
);
1170 endian_fixup (&key
->maximum
);
1174 g_warning ("unknown schema extension '%c'", code
);
1178 g_variant_unref (data
);
1181 g_variant_iter_free (iter
);
1185 g_settings_schema_key_clear (GSettingsSchemaKey
*key
)
1188 g_variant_unref (key
->minimum
);
1191 g_variant_unref (key
->maximum
);
1193 g_variant_unref (key
->default_value
);
1195 g_settings_schema_unref (key
->schema
);
1199 g_settings_schema_key_type_check (GSettingsSchemaKey
*key
,
1202 g_return_val_if_fail (value
!= NULL
, FALSE
);
1204 return g_variant_is_of_type (value
, key
->type
);
1208 g_settings_schema_key_range_check (GSettingsSchemaKey
*key
,
1211 if (key
->minimum
== NULL
&& key
->strinfo
== NULL
)
1214 if (g_variant_is_container (value
))
1220 g_variant_iter_init (&iter
, value
);
1221 while (ok
&& (child
= g_variant_iter_next_value (&iter
)))
1223 ok
= g_settings_schema_key_range_check (key
, child
);
1224 g_variant_unref (child
);
1232 return g_variant_compare (key
->minimum
, value
) <= 0 &&
1233 g_variant_compare (value
, key
->maximum
) <= 0;
1236 return strinfo_is_string_valid (key
->strinfo
, key
->strinfo_length
,
1237 g_variant_get_string (value
, NULL
));
1241 g_settings_schema_key_range_fixup (GSettingsSchemaKey
*key
,
1244 const gchar
*target
;
1246 if (g_settings_schema_key_range_check (key
, value
))
1247 return g_variant_ref (value
);
1249 if (key
->strinfo
== NULL
)
1252 if (g_variant_is_container (value
))
1254 GVariantBuilder builder
;
1258 g_variant_iter_init (&iter
, value
);
1259 g_variant_builder_init (&builder
, g_variant_get_type (value
));
1261 while ((child
= g_variant_iter_next_value (&iter
)))
1265 fixed
= g_settings_schema_key_range_fixup (key
, child
);
1266 g_variant_unref (child
);
1270 g_variant_builder_clear (&builder
);
1274 g_variant_builder_add_value (&builder
, fixed
);
1275 g_variant_unref (fixed
);
1278 return g_variant_ref_sink (g_variant_builder_end (&builder
));
1281 target
= strinfo_string_from_alias (key
->strinfo
, key
->strinfo_length
,
1282 g_variant_get_string (value
, NULL
));
1283 return target
? g_variant_ref_sink (g_variant_new_string (target
)) : NULL
;
1288 g_settings_schema_key_get_translated_default (GSettingsSchemaKey
*key
)
1290 const gchar
*translated
;
1291 GError
*error
= NULL
;
1292 const gchar
*domain
;
1295 domain
= g_settings_schema_get_gettext_domain (key
->schema
);
1297 if (key
->lc_char
== '\0')
1298 /* translation not requested for this key */
1301 if (key
->lc_char
== 't')
1302 translated
= g_dcgettext (domain
, key
->unparsed
, LC_TIME
);
1304 translated
= g_dgettext (domain
, key
->unparsed
);
1306 if (translated
== key
->unparsed
)
1307 /* the default value was not translated */
1310 /* try to parse the translation of the unparsed default */
1311 value
= g_variant_parse (key
->type
, translated
, NULL
, NULL
, &error
);
1315 g_warning ("Failed to parse translated string '%s' for "
1316 "key '%s' in schema '%s': %s", key
->unparsed
, key
->name
,
1317 g_settings_schema_get_id (key
->schema
), error
->message
);
1318 g_warning ("Using untranslated default instead.");
1319 g_error_free (error
);
1322 else if (!g_settings_schema_key_range_check (key
, value
))
1324 g_warning ("Translated default '%s' for key '%s' in schema '%s' "
1325 "is outside of valid range", key
->unparsed
, key
->name
,
1326 g_settings_schema_get_id (key
->schema
));
1327 g_variant_unref (value
);
1335 g_settings_schema_key_to_enum (GSettingsSchemaKey
*key
,
1341 it_worked
= strinfo_enum_from_string (key
->strinfo
, key
->strinfo_length
,
1342 g_variant_get_string (value
, NULL
),
1345 /* 'value' can only come from the backend after being filtered for validity,
1346 * from the translation after being filtered for validity, or from the schema
1347 * itself (which the schema compiler checks for validity). If this assertion
1348 * fails then it's really a bug in GSettings or the schema compiler...
1350 g_assert (it_worked
);
1356 g_settings_schema_key_from_enum (GSettingsSchemaKey
*key
,
1359 const gchar
*string
;
1361 string
= strinfo_string_from_enum (key
->strinfo
, key
->strinfo_length
, value
);
1366 return g_variant_new_string (string
);
1370 g_settings_schema_key_to_flags (GSettingsSchemaKey
*key
,
1378 g_variant_iter_init (&iter
, value
);
1379 while (g_variant_iter_next (&iter
, "&s", &flag
))
1384 it_worked
= strinfo_enum_from_string (key
->strinfo
, key
->strinfo_length
, flag
, &flag_value
);
1385 /* as in g_settings_to_enum() */
1386 g_assert (it_worked
);
1388 result
|= flag_value
;
1395 g_settings_schema_key_from_flags (GSettingsSchemaKey
*key
,
1398 GVariantBuilder builder
;
1401 g_variant_builder_init (&builder
, G_VARIANT_TYPE ("as"));
1403 for (i
= 0; i
< 32; i
++)
1404 if (value
& (1u << i
))
1406 const gchar
*string
;
1408 string
= strinfo_string_from_enum (key
->strinfo
, key
->strinfo_length
, 1u << i
);
1412 g_variant_builder_clear (&builder
);
1416 g_variant_builder_add (&builder
, "s", string
);
1419 return g_variant_builder_end (&builder
);
1422 G_DEFINE_BOXED_TYPE (GSettingsSchemaKey
, g_settings_schema_key
, g_settings_schema_key_ref
, g_settings_schema_key_unref
)
1425 * g_settings_schema_key_ref:
1426 * @key: a #GSettingsSchemaKey
1428 * Increase the reference count of @key, returning a new reference.
1430 * Returns: a new reference to @key
1434 GSettingsSchemaKey
*
1435 g_settings_schema_key_ref (GSettingsSchemaKey
*key
)
1437 g_return_val_if_fail (key
!= NULL
, NULL
);
1439 g_atomic_int_inc (&key
->ref_count
);
1445 * g_settings_schema_key_unref:
1446 * @key: a #GSettingsSchemaKey
1448 * Decrease the reference count of @key, possibly freeing it.
1453 g_settings_schema_key_unref (GSettingsSchemaKey
*key
)
1455 g_return_if_fail (key
!= NULL
);
1457 if (g_atomic_int_dec_and_test (&key
->ref_count
))
1459 g_settings_schema_key_clear (key
);
1461 g_slice_free (GSettingsSchemaKey
, key
);
1466 * g_settings_schema_get_key:
1467 * @schema: a #GSettingsSchema
1468 * @name: the name of a key
1470 * Gets the key named @name from @schema.
1472 * It is a programmer error to request a key that does not exist. See
1473 * g_settings_schema_list_keys().
1475 * Returns: (transfer full): the #GSettingsSchemaKey for @name
1479 GSettingsSchemaKey
*
1480 g_settings_schema_get_key (GSettingsSchema
*schema
,
1483 GSettingsSchemaKey
*key
;
1485 g_return_val_if_fail (schema
!= NULL
, NULL
);
1486 g_return_val_if_fail (name
!= NULL
, NULL
);
1488 key
= g_slice_new (GSettingsSchemaKey
);
1489 g_settings_schema_key_init (key
, schema
, name
);
1496 * g_settings_schema_key_get_summary:
1497 * @key: a #GSettingsSchemaKey
1499 * Gets the summary for @key.
1501 * If no summary has been provided in the schema for @key, returns
1504 * The summary is a short description of the purpose of the key; usually
1505 * one short sentence. Summaries can be translated and the value
1506 * returned from this function is is the current locale.
1508 * This function is slow. The summary and description information for
1509 * the schemas is not stored in the compiled schema database so this
1510 * function has to parse all of the source XML files in the schema
1513 * Returns: the summary for @key, or %NULL
1518 g_settings_schema_key_get_summary (GSettingsSchemaKey
*key
)
1520 GHashTable
**text_tables
;
1521 GHashTable
*summaries
;
1523 text_tables
= g_settings_schema_source_get_text_tables (key
->schema
->source
);
1524 summaries
= g_hash_table_lookup (text_tables
[0], key
->schema
->id
);
1526 return summaries
? g_hash_table_lookup (summaries
, key
->name
) : NULL
;
1530 * g_settings_schema_key_get_description:
1531 * @key: a #GSettingsSchemaKey
1533 * Gets the description for @key.
1535 * If no description has been provided in the schema for @key, returns
1538 * The description can be one sentence to several paragraphs in length.
1539 * Paragraphs are delimited with a double newline. Descriptions can be
1540 * translated and the value returned from this function is is the
1543 * This function is slow. The summary and description information for
1544 * the schemas is not stored in the compiled schema database so this
1545 * function has to parse all of the source XML files in the schema
1548 * Returns: the description for @key, or %NULL
1553 g_settings_schema_key_get_description (GSettingsSchemaKey
*key
)
1555 GHashTable
**text_tables
;
1556 GHashTable
*descriptions
;
1558 text_tables
= g_settings_schema_source_get_text_tables (key
->schema
->source
);
1559 descriptions
= g_hash_table_lookup (text_tables
[1], key
->schema
->id
);
1561 return descriptions
? g_hash_table_lookup (descriptions
, key
->name
) : NULL
;