GSettings tool: stop using deprecated APIs

Stop using the recently-deprecated GSettings APIs.

This fixes a number of bugs and also a net reduction of code.  In
particular, list-schemas will now work in context of a given --schemadir
argument.

https://bugzilla.gnome.org/show_bug.cgi?id=695558
This commit is contained in:
Ryan Lortie 2013-10-27 08:24:22 -07:00
parent 27840ede86
commit a1d8d962ae

View File

@ -31,16 +31,12 @@
#include "glib/glib-private.h" #include "glib/glib-private.h"
#endif #endif
static gboolean static GSettingsSchemaSource *global_schema_source;
contained (const gchar * const *items, static GSettings *global_settings;
const gchar *item) static GSettingsSchema *global_schema;
{ static GSettingsSchemaKey *global_schema_key;
while (*items) const gchar *global_key;
if (strcmp (*items++, item) == 0) const gchar *global_value;
return TRUE;
return FALSE;
}
static gboolean static gboolean
is_relocatable_schema (GSettingsSchema *schema) is_relocatable_schema (GSettingsSchema *schema)
@ -120,27 +116,8 @@ check_path (const gchar *path)
return TRUE; 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 static void
output_list (const gchar * const *list) output_list (gchar **list)
{ {
gint i; gint i;
@ -149,52 +126,50 @@ output_list (const gchar * const *list)
} }
static void static void
gsettings_print_version (GSettings *settings, gsettings_print_version (void)
const gchar *key,
const gchar *value)
{ {
g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version, g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version,
glib_micro_version); glib_micro_version);
} }
static void static void
gsettings_list_schemas (GSettings *settings, gsettings_list_schemas (void)
const gchar *key,
const gchar *value)
{ {
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 static void
gsettings_list_relocatable_schemas (GSettings *settings, gsettings_list_relocatable_schemas (void)
const gchar *key,
const gchar *value)
{ {
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 static void
gsettings_list_keys (GSettings *settings, gsettings_list_keys (void)
const gchar *key,
const gchar *value)
{ {
gchar **keys; gchar **keys;
keys = g_settings_list_keys (settings); keys = g_settings_list_keys (global_settings);
output_list ((const gchar **) keys); output_list (keys);
g_strfreev (keys); g_strfreev (keys);
} }
static void static void
gsettings_list_children (GSettings *settings, gsettings_list_children (void)
const gchar *key,
const gchar *value)
{ {
gchar **children; gchar **children;
gint max = 0; gint max = 0;
gint i; gint i;
children = g_settings_list_children (settings); children = g_settings_list_children (global_settings);
for (i = 0; children[i]; i++) for (i = 0; children[i]; i++)
if (strlen (children[i]) > max) if (strlen (children[i]) > max)
max = strlen (children[i]); max = strlen (children[i]);
@ -205,7 +180,7 @@ gsettings_list_children (GSettings *settings,
GSettingsSchema *schema; GSettingsSchema *schema;
gchar *path; gchar *path;
child = g_settings_get_child (settings, children[i]); child = g_settings_get_child (global_settings, children[i]);
g_object_get (child, g_object_get (child,
"settings-schema", &schema, "settings-schema", &schema,
"path", &path, "path", &path,
@ -251,53 +226,59 @@ enumerate (GSettings *settings)
} }
static void static void
gsettings_list_recursively (GSettings *settings, list_recursively (GSettings *settings)
const gchar *key,
const gchar *value)
{ {
if (settings) gchar **children;
gint i;
enumerate (settings);
children = g_settings_list_children (settings);
for (i = 0; children[i]; i++)
{ {
gchar **children; GSettings *child;
gint i;
enumerate (settings); child = g_settings_get_child (settings, children[i]);
children = g_settings_list_children (settings); list_recursively (child);
for (i = 0; children[i]; i++) g_object_unref (child);
{ }
GSettings *child;
child = g_settings_get_child (settings, children[i]); g_strfreev (children);
gsettings_list_recursively (child, NULL, NULL); }
g_object_unref (child);
}
g_strfreev (children); static void
gsettings_list_recursively (void)
{
if (global_settings)
{
list_recursively (global_settings);
} }
else else
{ {
const gchar * const *schemas; gchar **schemas;
gint i; 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++) for (i = 0; schemas[i]; i++)
{ {
GSettings *settings;
settings = g_settings_new (schemas[i]); settings = g_settings_new (schemas[i]);
gsettings_list_recursively (settings, NULL, NULL); list_recursively (settings);
g_object_unref (settings); g_object_unref (settings);
} }
g_strfreev (schemas);
} }
} }
static void static void
gsettings_range (GSettings *settings, gsettings_range (void)
const gchar *key,
const gchar *value)
{ {
GVariant *range, *detail; GVariant *range, *detail;
const gchar *type; 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); g_variant_get (range, "(&sv)", &type, &detail);
if (strcmp (type, "type") == 0) if (strcmp (type, "type") == 0)
@ -343,14 +324,12 @@ gsettings_range (GSettings *settings,
} }
static void static void
gsettings_get (GSettings *settings, gsettings_get (void)
const gchar *key,
const gchar *value_)
{ {
GVariant *value; GVariant *value;
gchar *printed; gchar *printed;
value = g_settings_get_value (settings, key); value = g_settings_get_value (global_settings, global_key);
printed = g_variant_print (value, TRUE); printed = g_variant_print (value, TRUE);
g_print ("%s\n", printed); g_print ("%s\n", printed);
g_variant_unref (value); g_variant_unref (value);
@ -358,16 +337,14 @@ gsettings_get (GSettings *settings,
} }
static void static void
gsettings_reset (GSettings *settings, gsettings_reset (void)
const gchar *key,
const gchar *value)
{ {
g_settings_reset (settings, key); g_settings_reset (global_settings, global_key);
g_settings_sync (); g_settings_sync ();
} }
static void static void
reset_all_keys (GSettings *settings) reset_all_keys (GSettings *settings)
{ {
gchar **keys; gchar **keys;
gint i; gint i;
@ -382,21 +359,19 @@ reset_all_keys (GSettings *settings)
} }
static void static void
gsettings_reset_recursively (GSettings *settings, gsettings_reset_recursively (void)
const gchar *key,
const gchar *value)
{ {
gchar **children; gchar **children;
gint i; gint i;
g_settings_delay (settings); g_settings_delay (global_settings);
reset_all_keys (settings); reset_all_keys (global_settings);
children = g_settings_list_children (settings); children = g_settings_list_children (global_settings);
for (i = 0; children[i]; i++) for (i = 0; children[i]; i++)
{ {
GSettings *child; GSettings *child;
child = g_settings_get_child (settings, children[i]); child = g_settings_get_child (global_settings, children[i]);
reset_all_keys (child); reset_all_keys (child);
@ -405,68 +380,58 @@ gsettings_reset_recursively (GSettings *settings,
g_strfreev (children); g_strfreev (children);
g_settings_apply (settings); g_settings_apply (global_settings);
g_settings_sync (); g_settings_sync ();
} }
static void static void
gsettings_writable (GSettings *settings, gsettings_writable (void)
const gchar *key,
const gchar *value)
{ {
g_print ("%s\n", g_print ("%s\n",
g_settings_is_writable (settings, key) ? g_settings_is_writable (global_settings, global_key) ?
"true" : "false"); "true" : "false");
} }
static void static void
value_changed (GSettings *settings, value_changed (void)
const gchar *key,
gpointer user_data)
{ {
GVariant *value; GVariant *value;
gchar *printed; gchar *printed;
value = g_settings_get_value (settings, key); value = g_settings_get_value (global_settings, global_key);
printed = g_variant_print (value, TRUE); printed = g_variant_print (value, TRUE);
g_print ("%s: %s\n", key, printed); g_print ("%s: %s\n", global_key, printed);
g_variant_unref (value); g_variant_unref (value);
g_free (printed); g_free (printed);
} }
static void static void
gsettings_monitor (GSettings *settings, gsettings_monitor (void)
const gchar *key,
const gchar *value)
{ {
if (key) if (global_key)
{ {
gchar *name; gchar *name;
name = g_strdup_printf ("changed::%s", key); name = g_strdup_printf ("changed::%s", global_key);
g_signal_connect (settings, name, G_CALLBACK (value_changed), NULL); g_signal_connect (global_settings, name, G_CALLBACK (value_changed), NULL);
} }
else 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)); g_main_loop_run (g_main_loop_new (NULL, FALSE));
} }
static void static void
gsettings_set (GSettings *settings, gsettings_set (void)
const gchar *key,
const gchar *value)
{ {
const GVariantType *type; const GVariantType *type;
GError *error = NULL; GError *error = NULL;
GVariant *existing;
GVariant *new; GVariant *new;
gchar *freeme = NULL; gchar *freeme = NULL;
existing = g_settings_get_value (settings, key); type = g_settings_schema_key_get_value_type (global_schema_key);
type = g_variant_get_type (existing);
new = g_variant_parse (type, value, NULL, NULL, &error); new = g_variant_parse (type, global_value, NULL, NULL, &error);
/* If that didn't work and the type is string then we should assume /* 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 * that the user is just trying to set a string directly and forgot
@ -491,29 +456,26 @@ gsettings_set (GSettings *settings,
*/ */
if (new == NULL && if (new == NULL &&
g_variant_type_equal (type, G_VARIANT_TYPE_STRING) && g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
value[0] != '\'' && value[0] != '"') global_value[0] != '\'' && global_value[0] != '"')
{ {
g_clear_error (&error); g_clear_error (&error);
new = g_variant_new_string (value); new = g_variant_new_string (global_value);
} }
/* we're done with 'type' now, so we can free 'existing' */
g_variant_unref (existing);
if (new == NULL) if (new == NULL)
{ {
g_printerr ("%s\n", error->message); g_printerr ("%s\n", error->message);
exit (1); 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_printerr (_("The provided value is outside of the valid range\n"));
g_variant_unref (new); g_variant_unref (new);
exit (1); exit (1);
} }
if (!g_settings_set_value (settings, key, new)) if (!g_settings_set_value (global_settings, global_key, new))
{ {
g_printerr (_("The key is not writable\n")); g_printerr (_("The key is not writable\n"));
exit (1); exit (1);
@ -702,11 +664,7 @@ gsettings_help (gboolean requested,
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
void (* function) (GSettings *, const gchar *, const gchar *); void (* function) (void);
GSettingsSchemaSource *schema_source;
GSettingsSchema *schema;
GSettings *settings;
const gchar *key;
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
gchar *tmp; gchar *tmp;
@ -730,17 +688,17 @@ main (int argc, char **argv)
if (argc < 2) if (argc < 2)
return gsettings_help (FALSE, NULL); return gsettings_help (FALSE, NULL);
schema_source = g_settings_schema_source_ref (g_settings_schema_source_get_default ()); global_schema_source = g_settings_schema_source_ref (g_settings_schema_source_get_default ());
if (argc > 3 && g_str_equal (argv[1], "--schemadir")) if (argc > 3 && g_str_equal (argv[1], "--schemadir"))
{ {
GSettingsSchemaSource *parent = schema_source; GSettingsSchemaSource *parent = global_schema_source;
GError *error = NULL; GError *error = NULL;
schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error); global_schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error);
g_settings_schema_source_unref (parent); g_settings_schema_source_unref (parent);
if (schema_source == NULL) if (global_schema_source == NULL)
{ {
g_printerr (_("Could not load schemas from %s: %s\n"), argv[2], error->message); g_printerr (_("Could not load schemas from %s: %s\n"), argv[2], error->message);
g_clear_error (&error); g_clear_error (&error);
@ -810,48 +768,47 @@ main (int argc, char **argv)
parts = g_strsplit (argv[2], ":", 2); parts = g_strsplit (argv[2], ":", 2);
schema = g_settings_schema_source_lookup (schema_source, parts[0], TRUE); global_schema = g_settings_schema_source_lookup (global_schema_source, parts[0], TRUE);
if (parts[1]) if (parts[1])
{ {
if (!check_relocatable_schema (schema, parts[0]) || !check_path (parts[1])) if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
return 1; return 1;
settings = g_settings_new_full (schema, NULL, parts[1]); global_settings = g_settings_new_full (global_schema, NULL, parts[1]);
} }
else else
{ {
if (!check_schema (schema, parts[0])) if (!check_schema (global_schema, parts[0]))
return 1; return 1;
settings = g_settings_new_full (schema, NULL, NULL); global_settings = g_settings_new_full (global_schema, NULL, NULL);
} }
g_strfreev (parts); g_strfreev (parts);
} }
else
{
settings = NULL;
schema = NULL;
}
if (argc > 3) if (argc > 3)
{ {
if (!check_key (settings, argv[3])) if (!g_settings_schema_has_key (global_schema, argv[3]))
return 1; {
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];
if (settings != NULL) (* function) ();
g_object_unref (settings);
if (schema != NULL)
g_settings_schema_unref (schema);
g_settings_schema_source_unref (schema_source);
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; return 0;
} }