X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=gio%2Fgsettings-tool.c;h=cf3ef19137f1b8c4f969b7a80baa5a7e32fcea24;hb=2138deb07ebb7d7e541c0cd35b966e107d1bf800;hp=a5d43a473ce01f41b75ce470c1f9341fdb59a29d;hpb=ea57feff96f13bbd4d03a76040a4ddfad2677310;p=platform%2Fupstream%2Fglib.git diff --git a/gio/gsettings-tool.c b/gio/gsettings-tool.c index a5d43a4..cf3ef19 100644 --- a/gio/gsettings-tool.c +++ b/gio/gsettings-tool.c @@ -12,9 +12,7 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. + * License along with this library; if not, see . * * Author: Ryan Lortie */ @@ -27,61 +25,63 @@ #include #include -static gboolean -contained (const gchar * const *items, - const gchar *item) -{ - while (*items) - if (strcmp (*items++, item) == 0) - return TRUE; - - return FALSE; -} +#ifdef G_OS_WIN32 +#include "glib/glib-private.h" +#endif -static gboolean -is_schema (const gchar *schema) -{ - return contained (g_settings_list_schemas (), schema); -} +static GSettingsSchemaSource *global_schema_source; +static GSettings *global_settings; +static GSettingsSchema *global_schema; +static GSettingsSchemaKey *global_schema_key; +const gchar *global_key; +const gchar *global_value; 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 @@ -114,27 +114,8 @@ check_path (const gchar *path) return TRUE; } -static gboolean -check_key (GSettings *settings, - const gchar *key) -{ - gboolean good; - gchar **keys; - - keys = g_settings_list_keys (settings); - good = contained ((const gchar **) keys, key); - g_strfreev (keys); - - if (good) - return TRUE; - - g_printerr (_("No such key '%s'\n"), key); - - return FALSE; -} - static void -output_list (const gchar * const *list) +output_list (gchar **list) { gint i; @@ -143,43 +124,50 @@ output_list (const gchar * const *list) } static void -gsettings_list_schemas (GSettings *settings, - const gchar *key, - const gchar *value) +gsettings_print_version (void) +{ + g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version, + glib_micro_version); +} + +static void +gsettings_list_schemas (void) { - output_list (g_settings_list_schemas ()); + gchar **schemas; + + g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL); + output_list (schemas); + g_strfreev (schemas); } static void -gsettings_list_relocatable_schemas (GSettings *settings, - const gchar *key, - const gchar *value) +gsettings_list_relocatable_schemas (void) { - output_list (g_settings_list_relocatable_schemas ()); + gchar **schemas; + + g_settings_schema_source_list_schemas (global_schema_source, TRUE, NULL, &schemas); + output_list (schemas); + g_strfreev (schemas); } static void -gsettings_list_keys (GSettings *settings, - const gchar *key, - const gchar *value) +gsettings_list_keys (void) { gchar **keys; - keys = g_settings_list_keys (settings); - output_list ((const gchar **) keys); + keys = g_settings_list_keys (global_settings); + output_list (keys); g_strfreev (keys); } static void -gsettings_list_children (GSettings *settings, - const gchar *key, - const gchar *value) +gsettings_list_children (void) { gchar **children; gint max = 0; gint i; - children = g_settings_list_children (settings); + children = g_settings_list_children (global_settings); for (i = 0; children[i]; i++) if (strlen (children[i]) > max) max = strlen (children[i]); @@ -187,22 +175,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]); + child = g_settings_get_child (global_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); } @@ -216,7 +204,7 @@ enumerate (GSettings *settings) 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++) @@ -236,59 +224,59 @@ enumerate (GSettings *settings) } static void -gsettings_list_recursively (GSettings *settings, - const gchar *key, - const gchar *value) +list_recursively (GSettings *settings) { - if (settings) - { - gchar **children; - gint i; - - enumerate (settings); - children = g_settings_list_children (settings); - for (i = 0; children[i]; i++) - { - GSettings *child; - gchar *schema; + gchar **children; + gint i; - child = g_settings_get_child (settings, children[i]); - g_object_get (child, "schema", &schema, NULL); + enumerate (settings); + children = g_settings_list_children (settings); + for (i = 0; children[i]; i++) + { + GSettings *child; - if (is_schema (schema)) - enumerate (child); + child = g_settings_get_child (settings, children[i]); + list_recursively (child); + g_object_unref (child); + } - g_object_unref (child); - g_free (schema); - } + g_strfreev (children); +} - g_strfreev (children); +static void +gsettings_list_recursively (void) +{ + if (global_settings) + { + list_recursively (global_settings); } else { - const gchar * const *schemas; + gchar **schemas; gint i; - schemas = g_settings_list_schemas (); + g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL); for (i = 0; schemas[i]; i++) { + GSettings *settings; + settings = g_settings_new (schemas[i]); - enumerate (settings); + list_recursively (settings); g_object_unref (settings); } + + g_strfreev (schemas); } } static void -gsettings_range (GSettings *settings, - const gchar *key, - const gchar *value) +gsettings_range (void) { GVariant *range, *detail; const gchar *type; - range = g_settings_get_range (settings, key); + range = g_settings_schema_key_get_range (global_schema_key); g_variant_get (range, "(&sv)", &type, &detail); if (strcmp (type, "type") == 0) @@ -334,14 +322,12 @@ gsettings_range (GSettings *settings, } static void -gsettings_get (GSettings *settings, - const gchar *key, - const gchar *value_) +gsettings_get (void) { GVariant *value; gchar *printed; - value = g_settings_get_value (settings, key); + value = g_settings_get_value (global_settings, global_key); printed = g_variant_print (value, TRUE); g_print ("%s\n", printed); g_variant_unref (value); @@ -349,21 +335,58 @@ gsettings_get (GSettings *settings, } static void -gsettings_reset (GSettings *settings, - const gchar *key, - const gchar *value) +gsettings_reset (void) { - g_settings_reset (settings, key); + g_settings_reset (global_settings, global_key); g_settings_sync (); } static void -gsettings_writable (GSettings *settings, - const gchar *key, - const gchar *value) +reset_all_keys (GSettings *settings) +{ + gchar **keys; + gint i; + + keys = g_settings_list_keys (settings); + for (i = 0; keys[i]; i++) + { + g_settings_reset (settings, keys[i]); + } + + g_strfreev (keys); +} + +static void +gsettings_reset_recursively (void) +{ + gchar **children; + gint i; + + g_settings_delay (global_settings); + + reset_all_keys (global_settings); + children = g_settings_list_children (global_settings); + for (i = 0; children[i]; i++) + { + GSettings *child; + child = g_settings_get_child (global_settings, children[i]); + + reset_all_keys (child); + + g_object_unref (child); + } + + g_strfreev (children); + + g_settings_apply (global_settings); + g_settings_sync (); +} + +static void +gsettings_writable (void) { g_print ("%s\n", - g_settings_is_writable (settings, key) ? + g_settings_is_writable (global_settings, global_key) ? "true" : "false"); } @@ -383,82 +406,99 @@ value_changed (GSettings *settings, } static void -gsettings_monitor (GSettings *settings, - const gchar *key, - const gchar *value) +gsettings_monitor (void) { - if (key) + gchar **keys; + + if (global_key) { gchar *name; - name = g_strdup_printf ("changed::%s", key); - g_signal_connect (settings, name, G_CALLBACK (value_changed), NULL); + name = g_strdup_printf ("changed::%s", global_key); + g_signal_connect (global_settings, name, G_CALLBACK (value_changed), NULL); } else - g_signal_connect (settings, "changed", G_CALLBACK (value_changed), NULL); + g_signal_connect (global_settings, "changed", G_CALLBACK (value_changed), NULL); - g_main_loop_run (g_main_loop_new (NULL, FALSE)); + /* We have to read a value from GSettings before we start receiving + * signals... + * + * If the schema has zero keys then we won't be displaying any + * notifications anyway. + */ + keys = g_settings_list_keys (global_settings); + if (keys[0]) + g_variant_unref (g_settings_get_value (global_settings, keys[0])); + g_strfreev (keys); + + for (;;) + g_main_context_iteration (NULL, TRUE); } static void -gsettings_set (GSettings *settings, - const gchar *key, - const gchar *value) +gsettings_set (void) { const GVariantType *type; GError *error = NULL; - GVariant *existing; GVariant *new; - GVariant *stored; gchar *freeme = NULL; - existing = g_settings_get_value (settings, key); - type = g_variant_get_type (existing); - - 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. + type = g_settings_schema_key_get_value_type (global_schema_key); + + new = g_variant_parse (type, global_value, NULL, NULL, &error); + + /* 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) && + global_value[0] != '\'' && global_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 (global_value); } if (new == NULL) { - g_printerr ("%s\n", error->message); + gchar *context; + + context = g_variant_parse_error_print_context (error, global_value); + g_printerr ("%s", context); exit (1); } - if (!g_settings_range_check (settings, key, new)) + if (!g_settings_schema_key_range_check (global_schema_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 (global_settings, global_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); } @@ -482,6 +522,12 @@ gsettings_help (gboolean requested, 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"); @@ -537,6 +583,12 @@ gsettings_help (gboolean requested, synopsis = N_("SCHEMA[:PATH] KEY"); } + else if (strcmp (command, "reset-recursively") == 0) + { + description = _("Reset all keys in SCHEMA to their defaults"); + synopsis = N_("SCHEMA[:PATH]"); + } + else if (strcmp (command, "writable") == 0) { description = _("Check if KEY is writable"); @@ -561,7 +613,8 @@ gsettings_help (gboolean requested, { 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" @@ -574,6 +627,7 @@ gsettings_help (gboolean requested, " get Get the value of a key\n" " set Set the value of a key\n" " reset Reset the value of a key\n" + " reset-recursively Reset all values in a given schema\n" " writable Check if a key is writable\n" " monitor Watch for changes\n" "\n" @@ -581,36 +635,36 @@ 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, _("Arguments:\n")); - if (strstr (synopsis, "[COMMAND]")) - g_string_append (string, - _(" COMMAND The (optional) command to explain\n")); + g_string_append (string, + _(" SCHEMADIR A directory to search for additional 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, "[COMMAND]")) + g_string_append (string, + _(" COMMAND The (optional) command to explain\n")); - if (strstr (synopsis, "[KEY]")) - g_string_append (string, - _(" KEY The (optional) key within the schema\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, "KEY")) - g_string_append (string, - _(" KEY The key within the schema\n")); + if (strstr (synopsis, "[KEY]")) + g_string_append (string, + _(" KEY The (optional) key within the schema\n")); - if (strstr (synopsis, "VALUE")) - g_string_append (string, - _(" VALUE The value to set\n")); + else if (strstr (synopsis, "KEY")) + g_string_append (string, + _(" KEY The key within the schema\n")); - g_string_append (string, "\n"); - } + if (strstr (synopsis, "VALUE")) + g_string_append (string, + _(" VALUE The value to set\n")); + + g_string_append (string, "\n"); } if (requested) @@ -627,15 +681,17 @@ gsettings_help (gboolean requested, int main (int argc, char **argv) { - void (* function) (GSettings *, const gchar *, const gchar *); - GSettings *settings; - const gchar *key; + void (* function) (void); + +#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 @@ -649,9 +705,35 @@ main (int argc, char **argv) if (argc < 2) return gsettings_help (FALSE, NULL); - else if (strcmp (argv[1], "help") == 0) + global_schema_source = g_settings_schema_source_ref (g_settings_schema_source_get_default ()); + + if (argc > 3 && g_str_equal (argv[1], "--schemadir")) + { + GSettingsSchemaSource *parent = global_schema_source; + GError *error = NULL; + + global_schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error); + g_settings_schema_source_unref (parent); + + if (global_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; @@ -679,6 +761,9 @@ main (int argc, char **argv) else if (argc == 4 && strcmp (argv[1], "reset") == 0) function = gsettings_reset; + else if (argc == 3 && strcmp (argv[1], "reset-recursively") == 0) + function = gsettings_reset_recursively; + else if (argc == 4 && strcmp (argv[1], "writable") == 0) function = gsettings_writable; @@ -688,8 +773,6 @@ main (int argc, char **argv) else return gsettings_help (FALSE, argv[1]); - g_type_init (); - if (argc > 2) { gchar **parts; @@ -702,40 +785,47 @@ main (int argc, char **argv) parts = g_strsplit (argv[2], ":", 2); + global_schema = g_settings_schema_source_lookup (global_schema_source, parts[0], TRUE); if (parts[1]) { - if (!check_relocatable_schema (parts[0]) || !check_path (parts[1])) + if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1])) return 1; - settings = g_settings_new_with_path (parts[0], parts[1]); + global_settings = g_settings_new_full (global_schema, NULL, parts[1]); } else { - if (!check_schema (parts[0])) + if (!check_schema (global_schema, parts[0])) return 1; - settings = g_settings_new (parts[0]); + global_settings = g_settings_new_full (global_schema, NULL, NULL); } g_strfreev (parts); } - else - settings = NULL; if (argc > 3) { - if (!check_key (settings, argv[3])) - return 1; + if (!g_settings_schema_has_key (global_schema, argv[3])) + { + g_printerr (_("No such key '%s'\n"), argv[3]); + return 1; + } - key = argv[3]; + global_key = argv[3]; + global_schema_key = g_settings_schema_get_key (global_schema, global_key); } - else - key = NULL; - (* function) (settings, key, argc > 4 ? argv[4] : NULL); + if (argc > 4) + global_value = argv[4]; + + (* function) (); + - if (settings != NULL) - g_object_unref (settings); + g_clear_pointer (&global_schema_source, g_settings_schema_source_unref); + g_clear_pointer (&global_schema_key, g_settings_schema_key_unref); + g_clear_pointer (&global_schema, g_settings_schema_unref); + g_clear_object (&global_settings); return 0; }