#include <string.h>
#include <stdlib.h>
+#ifdef G_OS_WIN32
+#include "glib/glib-private.h"
+#endif
+
static gboolean
contained (const gchar * const *items,
const gchar *item)
}
static gboolean
-is_schema (const gchar *schema)
-{
- return contained (g_settings_list_schemas (), schema);
-}
-
-static gboolean
-is_relocatable_schema (const gchar *schema)
+is_relocatable_schema (GSettingsSchema *schema)
{
- return contained (g_settings_list_relocatable_schemas (), schema);
+ return g_settings_schema_get_path (schema) == NULL;
}
static gboolean
-check_relocatable_schema (const gchar *schema)
+check_relocatable_schema (GSettingsSchema *schema,
+ const gchar *schema_id)
{
- if (is_relocatable_schema (schema))
- return TRUE;
-
- if (is_schema (schema))
- g_printerr (_("Schema '%s' is not relocatable "
- "(path must not be specified)\n"),
- schema);
+ if (schema == NULL)
+ {
+ g_printerr (_("No such schema '%s'\n"), schema_id);
+ return FALSE;
+ }
- else
- g_printerr (_("No such schema '%s'\n"), schema);
+ if (!is_relocatable_schema (schema))
+ {
+ g_printerr (_("Schema '%s' is not relocatable "
+ "(path must not be specified)\n"),
+ schema_id);
+ return FALSE;
+ }
- 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);
+ {
+ g_printerr (_("Schema '%s' is relocatable "
+ "(path must be specified)\n"),
+ schema_id);
+ return FALSE;
+ }
- return FALSE;
+ return TRUE;
}
static gboolean
}
static void
+gsettings_print_version (GSettings *settings,
+ const gchar *key,
+ const gchar *value)
+{
+ g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version,
+ glib_micro_version);
+}
+
+static void
gsettings_list_schemas (GSettings *settings,
const gchar *key,
const gchar *value)
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);
}
gchar *schema;
gint i;
- g_object_get (settings, "schema", &schema, NULL);
+ g_object_get (settings, "schema-id", &schema, NULL);
keys = g_settings_list_keys (settings);
for (i = 0; keys[i]; i++)
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))
- enumerate (child);
-
+ gsettings_list_recursively (child, NULL, NULL);
g_object_unref (child);
- g_free (schema);
}
g_strfreev (children);
for (i = 0; schemas[i]; i++)
{
settings = g_settings_new (schemas[i]);
- enumerate (settings);
+ gsettings_list_recursively (settings, NULL, NULL);
g_object_unref (settings);
}
}
static void
gsettings_reset_recursively (GSettings *settings,
- const gchar *key,
- const gchar *value)
+ const gchar *key,
+ const gchar *value)
{
gchar **children;
gint i;
g_settings_delay (settings);
-
+
reset_all_keys (settings);
children = g_settings_list_children (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);
-
+
+ reset_all_keys (child);
+
g_object_unref (child);
- g_free (schema);
}
-
+
g_strfreev (children);
-
+
g_settings_apply (settings);
g_settings_sync ();
}
GError *error = NULL;
GVariant *existing;
GVariant *new;
- GVariant *stored;
gchar *freeme = NULL;
existing = g_settings_get_value (settings, key);
new = g_variant_parse (type, value, NULL, NULL, &error);
- /* A common error is to specify a string with single quotes
- * (or use completion for that), and forget that the shell
- * will eat one level of quoting, resulting in 'unknown keyword'
- * error from the gvariant parser.
- * To handle this case, try to parse again with an extra level
- * of quotes.
+ /* If that didn't work and the type is string then we should assume
+ * that the user is just trying to set a string directly and forgot
+ * the quotes (or had them consumed by the shell).
+ *
+ * If the user started with a quote then we assume that some deeper
+ * problem is at play and we want the failure in that case.
+ *
+ * Consider:
+ *
+ * gsettings set x.y.z key "'i don't expect this to work'"
+ *
+ * Note that we should not just add quotes and try parsing again, but
+ * rather assume that the user is providing us with a bare string.
+ * Assume we added single quotes, then consider this case:
+ *
+ * gsettings set x.y.z key "i'd expect this to work"
+ *
+ * A similar example could be given for double quotes.
+ *
+ * Avoid that whole mess by just using g_variant_new_string().
*/
if (new == NULL &&
- g_error_matches (error, G_VARIANT_PARSE_ERROR,
- G_VARIANT_PARSE_ERROR_UNKNOWN_KEYWORD))
+ g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
+ value[0] != '\'' && value[0] != '"')
{
- value = freeme = g_strdup_printf ("\"%s\"", value);
- new = g_variant_parse (type, value, NULL, NULL, NULL);
- if (new != NULL)
- g_clear_error (&error);
+ g_clear_error (&error);
+ new = g_variant_new_string (value);
}
+ /* we're done with 'type' now, so we can free 'existing' */
+ g_variant_unref (existing);
+
if (new == NULL)
{
g_printerr ("%s\n", error->message);
if (!g_settings_range_check (settings, key, new))
{
g_printerr (_("The provided value is outside of the valid range\n"));
+ g_variant_unref (new);
exit (1);
}
- g_settings_set_value (settings, key, new);
- g_settings_sync ();
-
- stored = g_settings_get_value (settings, key);
- if (g_variant_equal (stored, existing))
+ if (!g_settings_set_value (settings, key, new))
{
- g_printerr (_("Failed to set value\n"));
+ g_printerr (_("The key is not writable\n"));
exit (1);
}
- g_variant_unref (stored);
- g_variant_unref (existing);
- g_variant_unref (new);
+ g_settings_sync ();
g_free (freeme);
}
synopsis = "[COMMAND]";
}
+ else if (strcmp (command, "--version") == 0)
+ {
+ description = _("Print version information and exit");
+ synopsis = "";
+ }
+
else if (strcmp (command, "list-schemas") == 0)
{
description = _("List the installed (non-relocatable) schemas");
{
g_string_append (string,
_("Usage:\n"
- " gsettings COMMAND [ARGS...]\n"
+ " gsettings --version\n"
+ " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS...]\n"
"\n"
"Commands:\n"
" help Show this information\n"
}
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, _("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"));
+ if (strstr (synopsis, "[COMMAND]"))
+ g_string_append (string,
+ _(" COMMAND The (optional) command to explain\n"));
- else if (strstr (synopsis, "SCHEMA"))
- g_string_append (string,
- _(" SCHEMA The name of the schema\n"
- " PATH The path, for relocatable schemas\n"));
+ else if (strstr (synopsis, "SCHEMA"))
+ g_string_append (string,
+ _(" SCHEMA The name of the schema\n"
+ " PATH The path, for relocatable schemas\n"));
- if (strstr (synopsis, "[KEY]"))
- g_string_append (string,
- _(" KEY The (optional) key within the schema\n"));
+ if (strstr (synopsis, "[KEY]"))
+ g_string_append (string,
+ _(" KEY The (optional) key within the schema\n"));
- else if (strstr (synopsis, "KEY"))
- g_string_append (string,
- _(" KEY The key within the schema\n"));
+ else if (strstr (synopsis, "KEY"))
+ g_string_append (string,
+ _(" KEY The key within the schema\n"));
- if (strstr (synopsis, "VALUE"))
- g_string_append (string,
- _(" VALUE The value to set\n"));
+ if (strstr (synopsis, "VALUE"))
+ g_string_append (string,
+ _(" VALUE The value to set\n"));
- g_string_append (string, "\n");
- }
+ g_string_append (string, "\n");
}
if (requested)
main (int argc, char **argv)
{
void (* function) (GSettings *, const gchar *, const gchar *);
+ GSettingsSchemaSource *schema_source;
+ GSettingsSchema *schema;
GSettings *settings;
const gchar *key;
+#ifdef G_OS_WIN32
+ gchar *tmp;
+#endif
+
setlocale (LC_ALL, "");
textdomain (GETTEXT_PACKAGE);
#ifdef G_OS_WIN32
- gchar *tmp = _glib_get_locale_dir ();
+ tmp = _glib_get_locale_dir ();
bindtextdomain (GETTEXT_PACKAGE, tmp);
g_free (tmp);
#else
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], "--version") == 0)
+ function = gsettings_print_version;
+
else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
function = gsettings_list_schemas;
else
return gsettings_help (FALSE, argv[1]);
- g_type_init ();
-
if (argc > 2)
{
gchar **parts;
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;
+ {
+ settings = NULL;
+ schema = NULL;
+ }
if (argc > 3)
{
if (settings != NULL)
g_object_unref (settings);
+ if (schema != NULL)
+ g_settings_schema_unref (schema);
+
+ g_settings_schema_source_unref (schema_source);
return 0;
}