gsettings-tool: allow specifying custom schema dirs

It happens that one wants to customize settings for plugins or
shell extensions, that installing schemas in nonstandard locations.
This patch adds the --schemadir option to gsettings, and ensure
that the appropriate schema is found.

https://bugzilla.gnome.org/show_bug.cgi?id=666415
This commit is contained in:
Giovanni Campagna 2011-12-17 20:08:50 +01:00 committed by Ryan Lortie
parent f59acb55c1
commit 466432830c
2 changed files with 135 additions and 89 deletions

View File

@ -5,44 +5,61 @@
####################################################################################################
__gsettings() {
local choices
local choices coffset schemadir
case "${COMP_CWORD}" in
if [ ${COMP_CWORD} -gt 2 ]; then
if [ ${COMP_WORDS[1]} = --schemadir ]; then
# this complexity is needed to perform correct tilde expansion
schemadir=$(eval "echo --schemadir ${COMP_WORDS[2]}")
coffset=2
else
coffset=0
fi
else
coffset=0
fi
case "$((${COMP_CWORD}-$coffset))" in
1)
choices=$'help \nlist-schemas\nlist-relocatable-schemas\nlist-keys \nlist-children \nlist-recursively \nget \nrange \nset \nreset \nwritable \nmonitor'
choices=$'--schemadir\nhelp \nlist-schemas\nlist-relocatable-schemas\nlist-keys \nlist-children \nlist-recursively \nget \nrange \nset \nreset \nwritable \nmonitor'
;;
2)
case "${COMP_WORDS[1]}" in
case "${COMP_WORDS[$(($coffset+1))]}" in
--schemadir)
COMPREPLY=($(compgen -o dirnames -- ${COMP_WORDS[${COMP_CWORD}]}))
return 0
;;
help)
choices=$'list-schemas\nlist-relocatable-schemas\nlist-keys\nlist-children\nlist-recursively\nget\nrange\nset\nreset\nwritable\nmonitor'
;;
list-keys|list-children|list-recursively)
choices="$(gsettings list-schemas)"$'\n'"$(gsettings list-relocatable-schemas | sed -e 's.$.:/.')"
choices="$(gsettings $schemadir list-schemas)"$'\n'"$(gsettings $schemadir list-relocatable-schemas | sed -e 's.$.:/.')"
;;
get|range|set|reset|writable|monitor)
choices="$(gsettings list-schemas | sed -e 's.$. .')"$'\n'"$(gsettings list-relocatable-schemas | sed -e 's.$.:/.')"
choices="$(gsettings $schemadir list-schemas | sed -e 's.$. .')"$'\n'"$(gsettings $schemadir list-relocatable-schemas | sed -e 's.$.:/.')"
;;
esac
;;
3)
case "${COMP_WORDS[1]}" in
case "${COMP_WORDS[$(($coffset+1))]}" in
set)
choices="$(gsettings list-keys ${COMP_WORDS[2]} 2> /dev/null | sed -e 's.$. .')"
choices="$(gsettings $schemadir list-keys ${COMP_WORDS[$(($coffset+2))]} 2> /dev/null | sed -e 's.$. .')"
;;
get|range|reset|writable|monitor)
choices="$(gsettings list-keys ${COMP_WORDS[2]} 2> /dev/null)"
choices="$(gsettings $schemadir list-keys ${COMP_WORDS[$(($coffset+2))]} 2> /dev/null)"
;;
esac
;;
4)
case "${COMP_WORDS[1]}" in
case "${COMP_WORDS[$(($coffset+2))]}" in
set)
range=($(gsettings range ${COMP_WORDS[2]} ${COMP_WORDS[3]} 2> /dev/null))
range=($(gsettings $schemadir range ${COMP_WORDS[$(($coffset+2))]} ${COMP_WORDS[$(($coffset+3))]} 2> /dev/null))
case "${range[0]}" in
enum)
unset range[0]
@ -59,7 +76,7 @@ __gsettings() {
esac
local IFS=$'\n'
COMPREPLY=($(compgen -W "${choices}" "${COMP_WORDS[$COMP_CWORD]}"))
COMPREPLY=($(compgen -W "${choices}" -- "${COMP_WORDS[${COMP_CWORD}]}"))
}
####################################################################################################

View File

@ -39,49 +39,51 @@ contained (const gchar * const *items,
}
static gboolean
is_schema (const gchar *schema)
is_relocatable_schema (GSettingsSchema *schema)
{
return contained (g_settings_list_schemas (), schema);
return g_settings_schema_get_path (schema) == NULL;
}
static gboolean
is_relocatable_schema (const gchar *schema)
check_relocatable_schema (GSettingsSchema *schema,
const gchar *schema_id)
{
return contained (g_settings_list_relocatable_schemas (), schema);
}
if (schema == NULL)
{
g_printerr (_("No such schema '%s'\n"), schema_id);
return FALSE;
}
static gboolean
check_relocatable_schema (const gchar *schema)
{
if (is_relocatable_schema (schema))
return TRUE;
if (is_schema (schema))
if (!is_relocatable_schema (schema))
{
g_printerr (_("Schema '%s' is not relocatable "
"(path must not be specified)\n"),
schema);
else
g_printerr (_("No such schema '%s'\n"), schema);
schema_id);
return FALSE;
}
return TRUE;
}
static gboolean
check_schema (const gchar *schema)
check_schema (GSettingsSchema *schema,
const gchar *schema_id)
{
if (is_schema (schema))
return TRUE;
if (schema == NULL)
{
g_printerr (_("No such schema '%s'\n"), schema_id);
return FALSE;
}
if (is_relocatable_schema (schema))
{
g_printerr (_("Schema '%s' is relocatable "
"(path must be specified)\n"),
schema);
else
g_printerr (_("No such schema '%s'\n"), schema);
schema_id);
return FALSE;
}
return TRUE;
}
static gboolean
@ -187,22 +189,22 @@ gsettings_list_children (GSettings *settings,
for (i = 0; children[i]; i++)
{
GSettings *child;
gchar *schema;
GSettingsSchema *schema;
gchar *path;
child = g_settings_get_child (settings, children[i]);
g_object_get (child,
"schema", &schema,
"settings-schema", &schema,
"path", &path,
NULL);
if (is_schema (schema))
g_print ("%-*s %s\n", max, children[i], schema);
if (g_settings_schema_get_path (schema) != NULL)
g_print ("%-*s %s\n", max, children[i], g_settings_schema_get_id (schema));
else
g_print ("%-*s %s:%s\n", max, children[i], schema, path);
g_print ("%-*s %s:%s\n", max, children[i], g_settings_schema_get_id (schema), path);
g_object_unref (child);
g_free (schema);
g_settings_schema_unref (schema);
g_free (path);
}
@ -250,16 +252,15 @@ gsettings_list_recursively (GSettings *settings,
for (i = 0; children[i]; i++)
{
GSettings *child;
gchar *schema;
GSettingsSchema *schema;
child = g_settings_get_child (settings, children[i]);
g_object_get (child, "schema", &schema, NULL);
g_object_get (child, "settings-schema", &schema, NULL);
if (is_schema (schema))
enumerate (child);
g_object_unref (child);
g_free (schema);
g_settings_schema_unref (schema);
}
g_strfreev (children);
@ -387,16 +388,11 @@ gsettings_reset_recursively (GSettings *settings,
for (i = 0; children[i]; i++)
{
GSettings *child;
gchar *schema;
child = g_settings_get_child (settings, children[i]);
g_object_get (child, "schema", &schema, NULL);
if (is_schema (schema))
reset_all_keys (child);
g_object_unref (child);
g_free (schema);
}
g_strfreev (children);
@ -619,7 +615,7 @@ gsettings_help (gboolean requested,
{
g_string_append (string,
_("Usage:\n"
" gsettings COMMAND [ARGS...]\n"
" gsettings [--schemadir SCHEMADIR] COMMAND [ARGS...]\n"
"\n"
"Commands:\n"
" help Show this information\n"
@ -640,13 +636,14 @@ gsettings_help (gboolean requested,
}
else
{
g_string_append_printf (string, _("Usage:\n gsettings %s %s\n\n%s\n\n"),
g_string_append_printf (string, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
command, synopsis[0] ? _(synopsis) : "", description);
if (synopsis[0])
{
g_string_append (string, _("Arguments:\n"));
g_string_append (string,
_(" SCHEMADIR A directory to search for additional schemas\n"));
if (strstr (synopsis, "[COMMAND]"))
g_string_append (string,
_(" COMMAND The (optional) command to explain\n"));
@ -670,7 +667,6 @@ gsettings_help (gboolean requested,
g_string_append (string, "\n");
}
}
if (requested)
g_print ("%s", string->str);
@ -687,6 +683,8 @@ int
main (int argc, char **argv)
{
void (* function) (GSettings *, const gchar *, const gchar *);
GSettingsSchemaSource *schema_source;
GSettingsSchema *schema;
GSettings *settings;
const gchar *key;
@ -713,7 +711,30 @@ main (int argc, char **argv)
if (argc < 2)
return gsettings_help (FALSE, NULL);
else if (strcmp (argv[1], "help") == 0)
schema_source = g_settings_schema_source_ref (g_settings_schema_source_get_default ());
if (argc > 3 && g_str_equal (argv[1], "--schemadir"))
{
GSettingsSchemaSource *parent = schema_source;
GError *error = NULL;
schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error);
g_settings_schema_source_unref (parent);
if (schema_source == NULL)
{
g_printerr ("Could not load schemas from %s: %s\n", argv[2], error->message);
g_clear_error (&error);
return 1;
}
/* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
argv = argv + 2;
argc -= 2;
}
if (strcmp (argv[1], "help") == 0)
return gsettings_help (TRUE, argv[2]);
else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
@ -769,25 +790,29 @@ main (int argc, char **argv)
parts = g_strsplit (argv[2], ":", 2);
schema = g_settings_schema_source_lookup (schema_source, parts[0], TRUE);
if (parts[1])
{
if (!check_relocatable_schema (parts[0]) || !check_path (parts[1]))
if (!check_relocatable_schema (schema, parts[0]) || !check_path (parts[1]))
return 1;
settings = g_settings_new_with_path (parts[0], parts[1]);
settings = g_settings_new_full (schema, NULL, parts[1]);
}
else
{
if (!check_schema (parts[0]))
if (!check_schema (schema, parts[0]))
return 1;
settings = g_settings_new (parts[0]);
settings = g_settings_new_full (schema, NULL, NULL);
}
g_strfreev (parts);
}
else
{
settings = NULL;
schema = NULL;
}
if (argc > 3)
{
@ -803,6 +828,10 @@ main (int argc, char **argv)
if (settings != NULL)
g_object_unref (settings);
if (schema != NULL)
g_settings_schema_unref (schema);
g_settings_schema_source_unref (schema_source);
return 0;
}