2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
29 #include "glib/glib-private.h"
32 static GSettingsSchemaSource
*global_schema_source
;
33 static GSettings
*global_settings
;
34 static GSettingsSchema
*global_schema
;
35 static GSettingsSchemaKey
*global_schema_key
;
36 const gchar
*global_key
;
37 const gchar
*global_value
;
40 is_relocatable_schema (GSettingsSchema
*schema
)
42 return g_settings_schema_get_path (schema
) == NULL
;
46 check_relocatable_schema (GSettingsSchema
*schema
,
47 const gchar
*schema_id
)
51 g_printerr (_("No such schema '%s'\n"), schema_id
);
55 if (!is_relocatable_schema (schema
))
57 g_printerr (_("Schema '%s' is not relocatable "
58 "(path must not be specified)\n"),
67 check_schema (GSettingsSchema
*schema
,
68 const gchar
*schema_id
)
72 g_printerr (_("No such schema '%s'\n"), schema_id
);
76 if (is_relocatable_schema (schema
))
78 g_printerr (_("Schema '%s' is relocatable "
79 "(path must be specified)\n"),
88 check_path (const gchar
*path
)
92 g_printerr (_("Empty path given.\n"));
98 g_printerr (_("Path must begin with a slash (/)\n"));
102 if (!g_str_has_suffix (path
, "/"))
104 g_printerr (_("Path must end with a slash (/)\n"));
108 if (strstr (path
, "//"))
110 g_printerr (_("Path must not contain two adjacent slashes (//)\n"));
118 output_list (gchar
**list
)
122 for (i
= 0; list
[i
]; i
++)
123 g_print ("%s\n", list
[i
]);
127 gsettings_print_version (void)
129 g_print ("%d.%d.%d\n", glib_major_version
, glib_minor_version
,
134 gsettings_list_schemas (void)
138 g_settings_schema_source_list_schemas (global_schema_source
, TRUE
, &schemas
, NULL
);
139 output_list (schemas
);
140 g_strfreev (schemas
);
144 gsettings_list_relocatable_schemas (void)
148 g_settings_schema_source_list_schemas (global_schema_source
, TRUE
, NULL
, &schemas
);
149 output_list (schemas
);
150 g_strfreev (schemas
);
154 gsettings_list_keys (void)
158 keys
= g_settings_list_keys (global_settings
);
164 gsettings_list_children (void)
170 children
= g_settings_list_children (global_settings
);
171 for (i
= 0; children
[i
]; i
++)
172 if (strlen (children
[i
]) > max
)
173 max
= strlen (children
[i
]);
175 for (i
= 0; children
[i
]; i
++)
178 GSettingsSchema
*schema
;
181 child
= g_settings_get_child (global_settings
, children
[i
]);
183 "settings-schema", &schema
,
187 if (g_settings_schema_get_path (schema
) != NULL
)
188 g_print ("%-*s %s\n", max
, children
[i
], g_settings_schema_get_id (schema
));
190 g_print ("%-*s %s:%s\n", max
, children
[i
], g_settings_schema_get_id (schema
), path
);
192 g_object_unref (child
);
193 g_settings_schema_unref (schema
);
197 g_strfreev (children
);
201 enumerate (GSettings
*settings
)
207 g_object_get (settings
, "schema-id", &schema
, NULL
);
209 keys
= g_settings_list_keys (settings
);
210 for (i
= 0; keys
[i
]; i
++)
215 value
= g_settings_get_value (settings
, keys
[i
]);
216 printed
= g_variant_print (value
, TRUE
);
217 g_print ("%s %s %s\n", schema
, keys
[i
], printed
);
218 g_variant_unref (value
);
227 list_recursively (GSettings
*settings
)
232 enumerate (settings
);
233 children
= g_settings_list_children (settings
);
234 for (i
= 0; children
[i
]; i
++)
238 child
= g_settings_get_child (settings
, children
[i
]);
239 list_recursively (child
);
240 g_object_unref (child
);
243 g_strfreev (children
);
247 gsettings_list_recursively (void)
251 list_recursively (global_settings
);
258 g_settings_schema_source_list_schemas (global_schema_source
, TRUE
, &schemas
, NULL
);
260 for (i
= 0; schemas
[i
]; i
++)
264 settings
= g_settings_new (schemas
[i
]);
265 list_recursively (settings
);
266 g_object_unref (settings
);
269 g_strfreev (schemas
);
274 gsettings_range (void)
276 GVariant
*range
, *detail
;
279 range
= g_settings_schema_key_get_range (global_schema_key
);
280 g_variant_get (range
, "(&sv)", &type
, &detail
);
282 if (strcmp (type
, "type") == 0)
283 g_print ("type %s\n", g_variant_get_type_string (detail
) + 1);
285 else if (strcmp (type
, "range") == 0)
290 g_variant_get (detail
, "(**)", &min
, &max
);
291 smin
= g_variant_print (min
, FALSE
);
292 smax
= g_variant_print (max
, FALSE
);
294 g_print ("range %s %s %s\n",
295 g_variant_get_type_string (min
), smin
, smax
);
296 g_variant_unref (min
);
297 g_variant_unref (max
);
302 else if (strcmp (type
, "enum") == 0 || strcmp (type
, "flags") == 0)
307 g_print ("%s\n", type
);
309 g_variant_iter_init (&iter
, detail
);
310 while (g_variant_iter_loop (&iter
, "*", &item
))
314 printed
= g_variant_print (item
, FALSE
);
315 g_print ("%s\n", printed
);
320 g_variant_unref (detail
);
321 g_variant_unref (range
);
330 value
= g_settings_get_value (global_settings
, global_key
);
331 printed
= g_variant_print (value
, TRUE
);
332 g_print ("%s\n", printed
);
333 g_variant_unref (value
);
338 gsettings_reset (void)
340 g_settings_reset (global_settings
, global_key
);
345 reset_all_keys (GSettings
*settings
)
350 keys
= g_settings_list_keys (settings
);
351 for (i
= 0; keys
[i
]; i
++)
353 g_settings_reset (settings
, keys
[i
]);
360 gsettings_reset_recursively (void)
365 g_settings_delay (global_settings
);
367 reset_all_keys (global_settings
);
368 children
= g_settings_list_children (global_settings
);
369 for (i
= 0; children
[i
]; i
++)
372 child
= g_settings_get_child (global_settings
, children
[i
]);
374 reset_all_keys (child
);
376 g_object_unref (child
);
379 g_strfreev (children
);
381 g_settings_apply (global_settings
);
386 gsettings_writable (void)
389 g_settings_is_writable (global_settings
, global_key
) ?
394 value_changed (GSettings
*settings
,
401 value
= g_settings_get_value (settings
, key
);
402 printed
= g_variant_print (value
, TRUE
);
403 g_print ("%s: %s\n", key
, printed
);
404 g_variant_unref (value
);
409 gsettings_monitor (void)
417 name
= g_strdup_printf ("changed::%s", global_key
);
418 g_signal_connect (global_settings
, name
, G_CALLBACK (value_changed
), NULL
);
421 g_signal_connect (global_settings
, "changed", G_CALLBACK (value_changed
), NULL
);
423 /* We have to read a value from GSettings before we start receiving
426 * If the schema has zero keys then we won't be displaying any
427 * notifications anyway.
429 keys
= g_settings_list_keys (global_settings
);
431 g_variant_unref (g_settings_get_value (global_settings
, keys
[0]));
435 g_main_context_iteration (NULL
, TRUE
);
441 const GVariantType
*type
;
442 GError
*error
= NULL
;
444 gchar
*freeme
= NULL
;
446 type
= g_settings_schema_key_get_value_type (global_schema_key
);
448 new = g_variant_parse (type
, global_value
, NULL
, NULL
, &error
);
450 /* If that didn't work and the type is string then we should assume
451 * that the user is just trying to set a string directly and forgot
452 * the quotes (or had them consumed by the shell).
454 * If the user started with a quote then we assume that some deeper
455 * problem is at play and we want the failure in that case.
459 * gsettings set x.y.z key "'i don't expect this to work'"
461 * Note that we should not just add quotes and try parsing again, but
462 * rather assume that the user is providing us with a bare string.
463 * Assume we added single quotes, then consider this case:
465 * gsettings set x.y.z key "i'd expect this to work"
467 * A similar example could be given for double quotes.
469 * Avoid that whole mess by just using g_variant_new_string().
472 g_variant_type_equal (type
, G_VARIANT_TYPE_STRING
) &&
473 global_value
[0] != '\'' && global_value
[0] != '"')
475 g_clear_error (&error
);
476 new = g_variant_new_string (global_value
);
483 context
= g_variant_parse_error_print_context (error
, global_value
);
484 g_printerr ("%s", context
);
488 if (!g_settings_schema_key_range_check (global_schema_key
, new))
490 g_printerr (_("The provided value is outside of the valid range\n"));
491 g_variant_unref (new);
495 if (!g_settings_set_value (global_settings
, global_key
, new))
497 g_printerr (_("The key is not writable\n"));
507 gsettings_help (gboolean requested
,
508 const gchar
*command
)
510 const gchar
*description
;
511 const gchar
*synopsis
;
514 string
= g_string_new (NULL
);
519 else if (strcmp (command
, "help") == 0)
521 description
= _("Print help");
522 synopsis
= "[COMMAND]";
525 else if (strcmp (command
, "--version") == 0)
527 description
= _("Print version information and exit");
531 else if (strcmp (command
, "list-schemas") == 0)
533 description
= _("List the installed (non-relocatable) schemas");
537 else if (strcmp (command
, "list-relocatable-schemas") == 0)
539 description
= _("List the installed relocatable schemas");
543 else if (strcmp (command
, "list-keys") == 0)
545 description
= _("List the keys in SCHEMA");
546 synopsis
= N_("SCHEMA[:PATH]");
549 else if (strcmp (command
, "list-children") == 0)
551 description
= _("List the children of SCHEMA");
552 synopsis
= N_("SCHEMA[:PATH]");
555 else if (strcmp (command
, "list-recursively") == 0)
557 description
= _("List keys and values, recursively\n"
558 "If no SCHEMA is given, list all keys\n");
559 synopsis
= N_("[SCHEMA[:PATH]]");
562 else if (strcmp (command
, "get") == 0)
564 description
= _("Get the value of KEY");
565 synopsis
= N_("SCHEMA[:PATH] KEY");
568 else if (strcmp (command
, "range") == 0)
570 description
= _("Query the range of valid values for KEY");
571 synopsis
= N_("SCHEMA[:PATH] KEY");
574 else if (strcmp (command
, "set") == 0)
576 description
= _("Set the value of KEY to VALUE");
577 synopsis
= N_("SCHEMA[:PATH] KEY VALUE");
580 else if (strcmp (command
, "reset") == 0)
582 description
= _("Reset KEY to its default value");
583 synopsis
= N_("SCHEMA[:PATH] KEY");
586 else if (strcmp (command
, "reset-recursively") == 0)
588 description
= _("Reset all keys in SCHEMA to their defaults");
589 synopsis
= N_("SCHEMA[:PATH]");
592 else if (strcmp (command
, "writable") == 0)
594 description
= _("Check if KEY is writable");
595 synopsis
= N_("SCHEMA[:PATH] KEY");
598 else if (strcmp (command
, "monitor") == 0)
600 description
= _("Monitor KEY for changes.\n"
601 "If no KEY is specified, monitor all keys in SCHEMA.\n"
602 "Use ^C to stop monitoring.\n");
603 synopsis
= N_("SCHEMA[:PATH] [KEY]");
607 g_string_printf (string
, _("Unknown command %s\n\n"), command
);
614 g_string_append (string
,
616 " gsettings --version\n"
617 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS...]\n"
620 " help Show this information\n"
621 " list-schemas List installed schemas\n"
622 " list-relocatable-schemas List relocatable schemas\n"
623 " list-keys List keys in a schema\n"
624 " list-children List children of a schema\n"
625 " list-recursively List keys and values, recursively\n"
626 " range Queries the range of a key\n"
627 " get Get the value of a key\n"
628 " set Set the value of a key\n"
629 " reset Reset the value of a key\n"
630 " reset-recursively Reset all values in a given schema\n"
631 " writable Check if a key is writable\n"
632 " monitor Watch for changes\n"
634 "Use 'gsettings help COMMAND' to get detailed help.\n\n"));
638 g_string_append_printf (string
, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
639 command
, synopsis
[0] ? _(synopsis
) : "", description
);
641 g_string_append (string
, _("Arguments:\n"));
643 g_string_append (string
,
644 _(" SCHEMADIR A directory to search for additional schemas\n"));
646 if (strstr (synopsis
, "[COMMAND]"))
647 g_string_append (string
,
648 _(" COMMAND The (optional) command to explain\n"));
650 else if (strstr (synopsis
, "SCHEMA"))
651 g_string_append (string
,
652 _(" SCHEMA The name of the schema\n"
653 " PATH The path, for relocatable schemas\n"));
655 if (strstr (synopsis
, "[KEY]"))
656 g_string_append (string
,
657 _(" KEY The (optional) key within the schema\n"));
659 else if (strstr (synopsis
, "KEY"))
660 g_string_append (string
,
661 _(" KEY The key within the schema\n"));
663 if (strstr (synopsis
, "VALUE"))
664 g_string_append (string
,
665 _(" VALUE The value to set\n"));
667 g_string_append (string
, "\n");
671 g_print ("%s", string
->str
);
673 g_printerr ("%s\n", string
->str
);
675 g_string_free (string
, TRUE
);
677 return requested
? 0 : 1;
682 main (int argc
, char **argv
)
684 void (* function
) (void);
690 setlocale (LC_ALL
, "");
691 textdomain (GETTEXT_PACKAGE
);
694 tmp
= _glib_get_locale_dir ();
695 bindtextdomain (GETTEXT_PACKAGE
, tmp
);
698 bindtextdomain (GETTEXT_PACKAGE
, GLIB_LOCALE_DIR
);
701 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
702 bind_textdomain_codeset (GETTEXT_PACKAGE
, "UTF-8");
706 return gsettings_help (FALSE
, NULL
);
708 global_schema_source
= g_settings_schema_source_ref (g_settings_schema_source_get_default ());
710 if (argc
> 3 && g_str_equal (argv
[1], "--schemadir"))
712 GSettingsSchemaSource
*parent
= global_schema_source
;
713 GError
*error
= NULL
;
715 global_schema_source
= g_settings_schema_source_new_from_directory (argv
[2], parent
, FALSE
, &error
);
716 g_settings_schema_source_unref (parent
);
718 if (global_schema_source
== NULL
)
720 g_printerr (_("Could not load schemas from %s: %s\n"), argv
[2], error
->message
);
721 g_clear_error (&error
);
726 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
731 if (strcmp (argv
[1], "help") == 0)
732 return gsettings_help (TRUE
, argv
[2]);
734 else if (argc
== 2 && strcmp (argv
[1], "--version") == 0)
735 function
= gsettings_print_version
;
737 else if (argc
== 2 && strcmp (argv
[1], "list-schemas") == 0)
738 function
= gsettings_list_schemas
;
740 else if (argc
== 2 && strcmp (argv
[1], "list-relocatable-schemas") == 0)
741 function
= gsettings_list_relocatable_schemas
;
743 else if (argc
== 3 && strcmp (argv
[1], "list-keys") == 0)
744 function
= gsettings_list_keys
;
746 else if (argc
== 3 && strcmp (argv
[1], "list-children") == 0)
747 function
= gsettings_list_children
;
749 else if ((argc
== 2 || argc
== 3) && strcmp (argv
[1], "list-recursively") == 0)
750 function
= gsettings_list_recursively
;
752 else if (argc
== 4 && strcmp (argv
[1], "range") == 0)
753 function
= gsettings_range
;
755 else if (argc
== 4 && strcmp (argv
[1], "get") == 0)
756 function
= gsettings_get
;
758 else if (argc
== 5 && strcmp (argv
[1], "set") == 0)
759 function
= gsettings_set
;
761 else if (argc
== 4 && strcmp (argv
[1], "reset") == 0)
762 function
= gsettings_reset
;
764 else if (argc
== 3 && strcmp (argv
[1], "reset-recursively") == 0)
765 function
= gsettings_reset_recursively
;
767 else if (argc
== 4 && strcmp (argv
[1], "writable") == 0)
768 function
= gsettings_writable
;
770 else if ((argc
== 3 || argc
== 4) && strcmp (argv
[1], "monitor") == 0)
771 function
= gsettings_monitor
;
774 return gsettings_help (FALSE
, argv
[1]);
780 if (argv
[2][0] == '\0')
782 g_printerr (_("Empty schema name given\n"));
786 parts
= g_strsplit (argv
[2], ":", 2);
788 global_schema
= g_settings_schema_source_lookup (global_schema_source
, parts
[0], TRUE
);
791 if (!check_relocatable_schema (global_schema
, parts
[0]) || !check_path (parts
[1]))
794 global_settings
= g_settings_new_full (global_schema
, NULL
, parts
[1]);
798 if (!check_schema (global_schema
, parts
[0]))
801 global_settings
= g_settings_new_full (global_schema
, NULL
, NULL
);
809 if (!g_settings_schema_has_key (global_schema
, argv
[3]))
811 g_printerr (_("No such key '%s'\n"), argv
[3]);
815 global_key
= argv
[3];
816 global_schema_key
= g_settings_schema_get_key (global_schema
, global_key
);
820 global_value
= argv
[4];
825 g_clear_pointer (&global_schema_source
, g_settings_schema_source_unref
);
826 g_clear_pointer (&global_schema_key
, g_settings_schema_key_unref
);
827 g_clear_pointer (&global_schema
, g_settings_schema_unref
);
828 g_clear_object (&global_settings
);