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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
31 #include "glib/glib-private.h"
35 contained (const gchar
* const *items
,
39 if (strcmp (*items
++, item
) == 0)
46 is_relocatable_schema (GSettingsSchema
*schema
)
48 return g_settings_schema_get_path (schema
) == NULL
;
52 check_relocatable_schema (GSettingsSchema
*schema
,
53 const gchar
*schema_id
)
57 g_printerr (_("No such schema '%s'\n"), schema_id
);
61 if (!is_relocatable_schema (schema
))
63 g_printerr (_("Schema '%s' is not relocatable "
64 "(path must not be specified)\n"),
73 check_schema (GSettingsSchema
*schema
,
74 const gchar
*schema_id
)
78 g_printerr (_("No such schema '%s'\n"), schema_id
);
82 if (is_relocatable_schema (schema
))
84 g_printerr (_("Schema '%s' is relocatable "
85 "(path must be specified)\n"),
94 check_path (const gchar
*path
)
98 g_printerr (_("Empty path given.\n"));
104 g_printerr (_("Path must begin with a slash (/)\n"));
108 if (!g_str_has_suffix (path
, "/"))
110 g_printerr (_("Path must end with a slash (/)\n"));
114 if (strstr (path
, "//"))
116 g_printerr (_("Path must not contain two adjacent slashes (//)\n"));
124 check_key (GSettings
*settings
,
130 keys
= g_settings_list_keys (settings
);
131 good
= contained ((const gchar
**) keys
, key
);
137 g_printerr (_("No such key '%s'\n"), key
);
143 output_list (const gchar
* const *list
)
147 for (i
= 0; list
[i
]; i
++)
148 g_print ("%s\n", list
[i
]);
152 gsettings_print_version (GSettings
*settings
,
156 g_print ("%d.%d.%d\n", glib_major_version
, glib_minor_version
,
161 gsettings_list_schemas (GSettings
*settings
,
165 output_list (g_settings_list_schemas ());
169 gsettings_list_relocatable_schemas (GSettings
*settings
,
173 output_list (g_settings_list_relocatable_schemas ());
177 gsettings_list_keys (GSettings
*settings
,
183 keys
= g_settings_list_keys (settings
);
184 output_list ((const gchar
**) keys
);
189 gsettings_list_children (GSettings
*settings
,
197 children
= g_settings_list_children (settings
);
198 for (i
= 0; children
[i
]; i
++)
199 if (strlen (children
[i
]) > max
)
200 max
= strlen (children
[i
]);
202 for (i
= 0; children
[i
]; i
++)
205 GSettingsSchema
*schema
;
208 child
= g_settings_get_child (settings
, children
[i
]);
210 "settings-schema", &schema
,
214 if (g_settings_schema_get_path (schema
) != NULL
)
215 g_print ("%-*s %s\n", max
, children
[i
], g_settings_schema_get_id (schema
));
217 g_print ("%-*s %s:%s\n", max
, children
[i
], g_settings_schema_get_id (schema
), path
);
219 g_object_unref (child
);
220 g_settings_schema_unref (schema
);
224 g_strfreev (children
);
228 enumerate (GSettings
*settings
)
234 g_object_get (settings
, "schema-id", &schema
, NULL
);
236 keys
= g_settings_list_keys (settings
);
237 for (i
= 0; keys
[i
]; i
++)
242 value
= g_settings_get_value (settings
, keys
[i
]);
243 printed
= g_variant_print (value
, TRUE
);
244 g_print ("%s %s %s\n", schema
, keys
[i
], printed
);
245 g_variant_unref (value
);
254 gsettings_list_recursively (GSettings
*settings
,
263 enumerate (settings
);
264 children
= g_settings_list_children (settings
);
265 for (i
= 0; children
[i
]; i
++)
269 child
= g_settings_get_child (settings
, children
[i
]);
270 gsettings_list_recursively (child
, NULL
, NULL
);
271 g_object_unref (child
);
274 g_strfreev (children
);
278 const gchar
* const *schemas
;
281 schemas
= g_settings_list_schemas ();
283 for (i
= 0; schemas
[i
]; i
++)
285 settings
= g_settings_new (schemas
[i
]);
286 gsettings_list_recursively (settings
, NULL
, NULL
);
287 g_object_unref (settings
);
293 gsettings_range (GSettings
*settings
,
297 GVariant
*range
, *detail
;
300 range
= g_settings_get_range (settings
, key
);
301 g_variant_get (range
, "(&sv)", &type
, &detail
);
303 if (strcmp (type
, "type") == 0)
304 g_print ("type %s\n", g_variant_get_type_string (detail
) + 1);
306 else if (strcmp (type
, "range") == 0)
311 g_variant_get (detail
, "(**)", &min
, &max
);
312 smin
= g_variant_print (min
, FALSE
);
313 smax
= g_variant_print (max
, FALSE
);
315 g_print ("range %s %s %s\n",
316 g_variant_get_type_string (min
), smin
, smax
);
317 g_variant_unref (min
);
318 g_variant_unref (max
);
323 else if (strcmp (type
, "enum") == 0 || strcmp (type
, "flags") == 0)
328 g_print ("%s\n", type
);
330 g_variant_iter_init (&iter
, detail
);
331 while (g_variant_iter_loop (&iter
, "*", &item
))
335 printed
= g_variant_print (item
, FALSE
);
336 g_print ("%s\n", printed
);
341 g_variant_unref (detail
);
342 g_variant_unref (range
);
346 gsettings_get (GSettings
*settings
,
353 value
= g_settings_get_value (settings
, key
);
354 printed
= g_variant_print (value
, TRUE
);
355 g_print ("%s\n", printed
);
356 g_variant_unref (value
);
361 gsettings_reset (GSettings
*settings
,
365 g_settings_reset (settings
, key
);
370 reset_all_keys (GSettings
*settings
)
375 keys
= g_settings_list_keys (settings
);
376 for (i
= 0; keys
[i
]; i
++)
378 g_settings_reset (settings
, keys
[i
]);
385 gsettings_reset_recursively (GSettings
*settings
,
392 g_settings_delay (settings
);
394 reset_all_keys (settings
);
395 children
= g_settings_list_children (settings
);
396 for (i
= 0; children
[i
]; i
++)
399 child
= g_settings_get_child (settings
, children
[i
]);
401 reset_all_keys (child
);
403 g_object_unref (child
);
406 g_strfreev (children
);
408 g_settings_apply (settings
);
413 gsettings_writable (GSettings
*settings
,
418 g_settings_is_writable (settings
, key
) ?
423 value_changed (GSettings
*settings
,
430 value
= g_settings_get_value (settings
, key
);
431 printed
= g_variant_print (value
, TRUE
);
432 g_print ("%s: %s\n", key
, printed
);
433 g_variant_unref (value
);
438 gsettings_monitor (GSettings
*settings
,
446 name
= g_strdup_printf ("changed::%s", key
);
447 g_signal_connect (settings
, name
, G_CALLBACK (value_changed
), NULL
);
450 g_signal_connect (settings
, "changed", G_CALLBACK (value_changed
), NULL
);
452 g_main_loop_run (g_main_loop_new (NULL
, FALSE
));
456 gsettings_set (GSettings
*settings
,
460 const GVariantType
*type
;
461 GError
*error
= NULL
;
464 gchar
*freeme
= NULL
;
466 existing
= g_settings_get_value (settings
, key
);
467 type
= g_variant_get_type (existing
);
469 new = g_variant_parse (type
, value
, NULL
, NULL
, &error
);
471 /* If that didn't work and the type is string then we should assume
472 * that the user is just trying to set a string directly and forgot
473 * the quotes (or had them consumed by the shell).
475 * If the user started with a quote then we assume that some deeper
476 * problem is at play and we want the failure in that case.
480 * gsettings set x.y.z key "'i don't expect this to work'"
482 * Note that we should not just add quotes and try parsing again, but
483 * rather assume that the user is providing us with a bare string.
484 * Assume we added single quotes, then consider this case:
486 * gsettings set x.y.z key "i'd expect this to work"
488 * A similar example could be given for double quotes.
490 * Avoid that whole mess by just using g_variant_new_string().
493 g_variant_type_equal (type
, G_VARIANT_TYPE_STRING
) &&
494 value
[0] != '\'' && value
[0] != '"')
496 g_clear_error (&error
);
497 new = g_variant_new_string (value
);
500 /* we're done with 'type' now, so we can free 'existing' */
501 g_variant_unref (existing
);
505 g_printerr ("%s\n", error
->message
);
509 if (!g_settings_range_check (settings
, key
, new))
511 g_printerr (_("The provided value is outside of the valid range\n"));
512 g_variant_unref (new);
516 if (!g_settings_set_value (settings
, key
, new))
518 g_printerr (_("The key is not writable\n"));
528 gsettings_help (gboolean requested
,
529 const gchar
*command
)
531 const gchar
*description
;
532 const gchar
*synopsis
;
535 string
= g_string_new (NULL
);
540 else if (strcmp (command
, "help") == 0)
542 description
= _("Print help");
543 synopsis
= "[COMMAND]";
546 else if (strcmp (command
, "--version") == 0)
548 description
= _("Print version information and exit");
552 else if (strcmp (command
, "list-schemas") == 0)
554 description
= _("List the installed (non-relocatable) schemas");
558 else if (strcmp (command
, "list-relocatable-schemas") == 0)
560 description
= _("List the installed relocatable schemas");
564 else if (strcmp (command
, "list-keys") == 0)
566 description
= _("List the keys in SCHEMA");
567 synopsis
= N_("SCHEMA[:PATH]");
570 else if (strcmp (command
, "list-children") == 0)
572 description
= _("List the children of SCHEMA");
573 synopsis
= N_("SCHEMA[:PATH]");
576 else if (strcmp (command
, "list-recursively") == 0)
578 description
= _("List keys and values, recursively\n"
579 "If no SCHEMA is given, list all keys\n");
580 synopsis
= N_("[SCHEMA[:PATH]]");
583 else if (strcmp (command
, "get") == 0)
585 description
= _("Get the value of KEY");
586 synopsis
= N_("SCHEMA[:PATH] KEY");
589 else if (strcmp (command
, "range") == 0)
591 description
= _("Query the range of valid values for KEY");
592 synopsis
= N_("SCHEMA[:PATH] KEY");
595 else if (strcmp (command
, "set") == 0)
597 description
= _("Set the value of KEY to VALUE");
598 synopsis
= N_("SCHEMA[:PATH] KEY VALUE");
601 else if (strcmp (command
, "reset") == 0)
603 description
= _("Reset KEY to its default value");
604 synopsis
= N_("SCHEMA[:PATH] KEY");
607 else if (strcmp (command
, "reset-recursively") == 0)
609 description
= _("Reset all keys in SCHEMA to their defaults");
610 synopsis
= N_("SCHEMA[:PATH]");
613 else if (strcmp (command
, "writable") == 0)
615 description
= _("Check if KEY is writable");
616 synopsis
= N_("SCHEMA[:PATH] KEY");
619 else if (strcmp (command
, "monitor") == 0)
621 description
= _("Monitor KEY for changes.\n"
622 "If no KEY is specified, monitor all keys in SCHEMA.\n"
623 "Use ^C to stop monitoring.\n");
624 synopsis
= N_("SCHEMA[:PATH] [KEY]");
628 g_string_printf (string
, _("Unknown command %s\n\n"), command
);
635 g_string_append (string
,
637 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS...]\n"
640 " help Show this information\n"
641 " list-schemas List installed schemas\n"
642 " list-relocatable-schemas List relocatable schemas\n"
643 " list-keys List keys in a schema\n"
644 " list-children List children of a schema\n"
645 " list-recursively List keys and values, recursively\n"
646 " range Queries the range of a key\n"
647 " get Get the value of a key\n"
648 " set Set the value of a key\n"
649 " reset Reset the value of a key\n"
650 " reset-recursively Reset all values in a given schema\n"
651 " writable Check if a key is writable\n"
652 " monitor Watch for changes\n"
654 "Use 'gsettings help COMMAND' to get detailed help.\n\n"));
658 g_string_append_printf (string
, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
659 command
, synopsis
[0] ? _(synopsis
) : "", description
);
661 g_string_append (string
, _("Arguments:\n"));
663 g_string_append (string
,
664 _(" SCHEMADIR A directory to search for additional schemas\n"));
666 if (strstr (synopsis
, "[COMMAND]"))
667 g_string_append (string
,
668 _(" COMMAND The (optional) command to explain\n"));
670 else if (strstr (synopsis
, "SCHEMA"))
671 g_string_append (string
,
672 _(" SCHEMA The name of the schema\n"
673 " PATH The path, for relocatable schemas\n"));
675 if (strstr (synopsis
, "[KEY]"))
676 g_string_append (string
,
677 _(" KEY The (optional) key within the schema\n"));
679 else if (strstr (synopsis
, "KEY"))
680 g_string_append (string
,
681 _(" KEY The key within the schema\n"));
683 if (strstr (synopsis
, "VALUE"))
684 g_string_append (string
,
685 _(" VALUE The value to set\n"));
687 g_string_append (string
, "\n");
691 g_print ("%s", string
->str
);
693 g_printerr ("%s\n", string
->str
);
695 g_string_free (string
, TRUE
);
697 return requested
? 0 : 1;
702 main (int argc
, char **argv
)
704 void (* function
) (GSettings
*, const gchar
*, const gchar
*);
705 GSettingsSchemaSource
*schema_source
;
706 GSettingsSchema
*schema
;
714 setlocale (LC_ALL
, "");
715 textdomain (GETTEXT_PACKAGE
);
718 tmp
= _glib_get_locale_dir ();
719 bindtextdomain (GETTEXT_PACKAGE
, tmp
);
722 bindtextdomain (GETTEXT_PACKAGE
, GLIB_LOCALE_DIR
);
725 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
726 bind_textdomain_codeset (GETTEXT_PACKAGE
, "UTF-8");
730 return gsettings_help (FALSE
, NULL
);
732 schema_source
= g_settings_schema_source_ref (g_settings_schema_source_get_default ());
734 if (argc
> 3 && g_str_equal (argv
[1], "--schemadir"))
736 GSettingsSchemaSource
*parent
= schema_source
;
737 GError
*error
= NULL
;
739 schema_source
= g_settings_schema_source_new_from_directory (argv
[2], parent
, FALSE
, &error
);
740 g_settings_schema_source_unref (parent
);
742 if (schema_source
== NULL
)
744 g_printerr (_("Could not load schemas from %s: %s\n"), argv
[2], error
->message
);
745 g_clear_error (&error
);
750 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
755 if (strcmp (argv
[1], "help") == 0)
756 return gsettings_help (TRUE
, argv
[2]);
758 else if (argc
== 2 && strcmp (argv
[1], "--version") == 0)
759 function
= gsettings_print_version
;
761 else if (argc
== 2 && strcmp (argv
[1], "list-schemas") == 0)
762 function
= gsettings_list_schemas
;
764 else if (argc
== 2 && strcmp (argv
[1], "list-relocatable-schemas") == 0)
765 function
= gsettings_list_relocatable_schemas
;
767 else if (argc
== 3 && strcmp (argv
[1], "list-keys") == 0)
768 function
= gsettings_list_keys
;
770 else if (argc
== 3 && strcmp (argv
[1], "list-children") == 0)
771 function
= gsettings_list_children
;
773 else if ((argc
== 2 || argc
== 3) && strcmp (argv
[1], "list-recursively") == 0)
774 function
= gsettings_list_recursively
;
776 else if (argc
== 4 && strcmp (argv
[1], "range") == 0)
777 function
= gsettings_range
;
779 else if (argc
== 4 && strcmp (argv
[1], "get") == 0)
780 function
= gsettings_get
;
782 else if (argc
== 5 && strcmp (argv
[1], "set") == 0)
783 function
= gsettings_set
;
785 else if (argc
== 4 && strcmp (argv
[1], "reset") == 0)
786 function
= gsettings_reset
;
788 else if (argc
== 3 && strcmp (argv
[1], "reset-recursively") == 0)
789 function
= gsettings_reset_recursively
;
791 else if (argc
== 4 && strcmp (argv
[1], "writable") == 0)
792 function
= gsettings_writable
;
794 else if ((argc
== 3 || argc
== 4) && strcmp (argv
[1], "monitor") == 0)
795 function
= gsettings_monitor
;
798 return gsettings_help (FALSE
, argv
[1]);
804 if (argv
[2][0] == '\0')
806 g_printerr (_("Empty schema name given\n"));
810 parts
= g_strsplit (argv
[2], ":", 2);
812 schema
= g_settings_schema_source_lookup (schema_source
, parts
[0], TRUE
);
815 if (!check_relocatable_schema (schema
, parts
[0]) || !check_path (parts
[1]))
818 settings
= g_settings_new_full (schema
, NULL
, parts
[1]);
822 if (!check_schema (schema
, parts
[0]))
825 settings
= g_settings_new_full (schema
, NULL
, NULL
);
838 if (!check_key (settings
, argv
[3]))
846 (* function
) (settings
, key
, argc
> 4 ? argv
[4] : NULL
);
848 if (settings
!= NULL
)
849 g_object_unref (settings
);
851 g_settings_schema_unref (schema
);
853 g_settings_schema_source_unref (schema_source
);