Branch data Line data Source code
1 : : /*
2 : : * Copyright © 2010 Codethink Limited
3 : : *
4 : : * SPDX-License-Identifier: LGPL-2.1-or-later
5 : : *
6 : : * This library is free software; you can redistribute it and/or
7 : : * modify it under the terms of the GNU Lesser General Public
8 : : * License as published by the Free Software Foundation; either
9 : : * version 2.1 of the License, or (at your option) any later version.
10 : : *
11 : : * This library is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : * Lesser General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU Lesser General Public
17 : : * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 : : *
19 : : * Author: Ryan Lortie <desrt@desrt.ca>
20 : : */
21 : :
22 : : #include "config.h"
23 : :
24 : : #include <gio/gio.h>
25 : : #include <gi18n.h>
26 : : #include <locale.h>
27 : : #include <string.h>
28 : : #include <stdlib.h>
29 : :
30 : : #include "glib/glib-private.h"
31 : :
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;
38 : :
39 : : static gboolean
40 : 0 : is_relocatable_schema (GSettingsSchema *schema)
41 : : {
42 : 0 : return g_settings_schema_get_path (schema) == NULL;
43 : : }
44 : :
45 : : static gboolean
46 : 0 : check_relocatable_schema (GSettingsSchema *schema,
47 : : const gchar *schema_id)
48 : : {
49 : 0 : if (schema == NULL)
50 : : {
51 : 0 : g_printerr (_("No such schema “%s”\n"), schema_id);
52 : 0 : return FALSE;
53 : : }
54 : :
55 : 0 : if (!is_relocatable_schema (schema))
56 : : {
57 : 0 : g_printerr (_("Schema “%s” is not relocatable "
58 : : "(path must not be specified)\n"),
59 : 0 : schema_id);
60 : 0 : return FALSE;
61 : : }
62 : :
63 : 0 : return TRUE;
64 : 0 : }
65 : :
66 : : static gboolean
67 : 0 : check_schema (GSettingsSchema *schema,
68 : : const gchar *schema_id)
69 : : {
70 : 0 : if (schema == NULL)
71 : : {
72 : 0 : g_printerr (_("No such schema “%s”\n"), schema_id);
73 : 0 : return FALSE;
74 : : }
75 : :
76 : 0 : if (is_relocatable_schema (schema))
77 : : {
78 : 0 : g_printerr (_("Schema “%s” is relocatable "
79 : : "(path must be specified)\n"),
80 : 0 : schema_id);
81 : 0 : return FALSE;
82 : : }
83 : :
84 : 0 : return TRUE;
85 : 0 : }
86 : :
87 : : static gboolean
88 : 0 : check_path (const gchar *path)
89 : : {
90 : 0 : if (path[0] == '\0')
91 : : {
92 : 0 : g_printerr (_("Empty path given.\n"));
93 : 0 : return FALSE;
94 : : }
95 : :
96 : 0 : if (path[0] != '/')
97 : : {
98 : 0 : g_printerr (_("Path must begin with a slash (/)\n"));
99 : 0 : return FALSE;
100 : : }
101 : :
102 : 0 : if (!g_str_has_suffix (path, "/"))
103 : : {
104 : 0 : g_printerr (_("Path must end with a slash (/)\n"));
105 : 0 : return FALSE;
106 : : }
107 : :
108 : 0 : if (strstr (path, "//"))
109 : : {
110 : 0 : g_printerr (_("Path must not contain two adjacent slashes (//)\n"));
111 : 0 : return FALSE;
112 : : }
113 : :
114 : 0 : return TRUE;
115 : 0 : }
116 : :
117 : : static int
118 : 0 : qsort_cmp (const void *a,
119 : : const void *b)
120 : : {
121 : 0 : return g_strcmp0 (*(gchar* const*)a, *(gchar* const*)b);
122 : : }
123 : :
124 : : static void
125 : 0 : output_list (gchar **list)
126 : : {
127 : : gint i;
128 : :
129 : 0 : qsort (list, g_strv_length (list), sizeof (gchar*), qsort_cmp);
130 : 0 : for (i = 0; list[i]; i++)
131 : 0 : g_print ("%s\n", list[i]);
132 : 0 : }
133 : :
134 : : static void
135 : 0 : gsettings_print_version (void)
136 : : {
137 : 0 : g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version,
138 : 0 : glib_micro_version);
139 : 0 : }
140 : :
141 : : static void
142 : 0 : gsettings_list_schemas (void)
143 : : {
144 : : gchar **schemas;
145 : :
146 : 0 : g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
147 : 0 : output_list (schemas);
148 : 0 : g_strfreev (schemas);
149 : 0 : }
150 : :
151 : : static void
152 : 0 : gsettings_list_schemas_with_paths (void)
153 : : {
154 : : gchar **schemas;
155 : : gsize i;
156 : :
157 : 0 : g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
158 : :
159 : 0 : for (i = 0; schemas[i] != NULL; i++)
160 : : {
161 : : GSettingsSchema *schema;
162 : : gchar *schema_name;
163 : : const gchar *schema_path;
164 : :
165 : 0 : schema_name = g_steal_pointer (&schemas[i]);
166 : :
167 : 0 : schema = g_settings_schema_source_lookup (global_schema_source, schema_name, TRUE);
168 : 0 : schema_path = g_settings_schema_get_path (schema);
169 : :
170 : 0 : schemas[i] = g_strconcat (schema_name, " ", schema_path, NULL);
171 : :
172 : 0 : g_settings_schema_unref (schema);
173 : 0 : g_free (schema_name);
174 : 0 : }
175 : :
176 : 0 : output_list (schemas);
177 : 0 : g_strfreev (schemas);
178 : 0 : }
179 : :
180 : : static void
181 : 0 : gsettings_list_relocatable_schemas (void)
182 : : {
183 : : gchar **schemas;
184 : :
185 : 0 : g_settings_schema_source_list_schemas (global_schema_source, TRUE, NULL, &schemas);
186 : 0 : output_list (schemas);
187 : 0 : g_strfreev (schemas);
188 : 0 : }
189 : :
190 : : static void
191 : 0 : gsettings_list_keys (void)
192 : : {
193 : : gchar **keys;
194 : :
195 : 0 : keys = g_settings_schema_list_keys (global_schema);
196 : 0 : output_list (keys);
197 : 0 : g_strfreev (keys);
198 : 0 : }
199 : :
200 : : static void
201 : 0 : gsettings_list_children (void)
202 : : {
203 : : gchar **children;
204 : 0 : gsize max = 0;
205 : : gint i;
206 : :
207 : 0 : children = g_settings_list_children (global_settings);
208 : 0 : qsort (children, g_strv_length (children), sizeof (gchar*), qsort_cmp);
209 : 0 : for (i = 0; children[i]; i++)
210 : : {
211 : 0 : gsize len = strlen (children[i]);
212 : 0 : if (len > max)
213 : 0 : max = len;
214 : 0 : }
215 : :
216 : 0 : for (i = 0; children[i]; i++)
217 : : {
218 : : GSettings *child;
219 : : GSettingsSchema *schema;
220 : : gchar *path;
221 : :
222 : 0 : child = g_settings_get_child (global_settings, children[i]);
223 : 0 : g_object_get (child,
224 : : "settings-schema", &schema,
225 : : "path", &path,
226 : : NULL);
227 : :
228 : 0 : if (g_settings_schema_get_path (schema) != NULL)
229 : 0 : g_print ("%-*s %s\n", (int) MIN (max, G_MAXINT), children[i],
230 : 0 : g_settings_schema_get_id (schema));
231 : : else
232 : 0 : g_print ("%-*s %s:%s\n", (int) MIN (max, G_MAXINT), children[i],
233 : 0 : g_settings_schema_get_id (schema), path);
234 : :
235 : 0 : g_object_unref (child);
236 : 0 : g_settings_schema_unref (schema);
237 : 0 : g_free (path);
238 : 0 : }
239 : :
240 : 0 : g_strfreev (children);
241 : 0 : }
242 : :
243 : : static void
244 : 0 : enumerate (GSettings *settings)
245 : : {
246 : : gchar **keys;
247 : : GSettingsSchema *schema;
248 : : gint i;
249 : :
250 : 0 : g_object_get (settings, "settings-schema", &schema, NULL);
251 : :
252 : 0 : keys = g_settings_schema_list_keys (schema);
253 : 0 : qsort (keys, g_strv_length (keys), sizeof (gchar*), qsort_cmp);
254 : 0 : for (i = 0; keys[i]; i++)
255 : : {
256 : : GVariant *value;
257 : : gchar *printed;
258 : :
259 : 0 : value = g_settings_get_value (settings, keys[i]);
260 : 0 : printed = g_variant_print (value, TRUE);
261 : 0 : g_print ("%s %s %s\n", g_settings_schema_get_id (schema), keys[i], printed);
262 : 0 : g_variant_unref (value);
263 : 0 : g_free (printed);
264 : 0 : }
265 : :
266 : 0 : g_settings_schema_unref (schema);
267 : 0 : g_strfreev (keys);
268 : 0 : }
269 : :
270 : : static void
271 : 0 : list_recursively (GSettings *settings)
272 : : {
273 : : gchar **children;
274 : : gint i;
275 : :
276 : 0 : enumerate (settings);
277 : 0 : children = g_settings_list_children (settings);
278 : 0 : qsort (children, g_strv_length (children), sizeof (gchar*), qsort_cmp);
279 : 0 : for (i = 0; children[i]; i++)
280 : : {
281 : 0 : gboolean will_see_elsewhere = FALSE;
282 : : GSettings *child;
283 : :
284 : 0 : child = g_settings_get_child (settings, children[i]);
285 : :
286 : 0 : if (global_settings == NULL)
287 : : {
288 : : /* we're listing all non-relocatable settings objects from the
289 : : * top-level, so if this one is non-relocatable, don't recurse,
290 : : * because we will pick it up later on.
291 : : */
292 : :
293 : : GSettingsSchema *child_schema;
294 : :
295 : 0 : g_object_get (child, "settings-schema", &child_schema, NULL);
296 : 0 : will_see_elsewhere = !is_relocatable_schema (child_schema);
297 : 0 : g_settings_schema_unref (child_schema);
298 : 0 : }
299 : :
300 : 0 : if (!will_see_elsewhere)
301 : 0 : list_recursively (child);
302 : :
303 : 0 : g_object_unref (child);
304 : 0 : }
305 : :
306 : 0 : g_strfreev (children);
307 : 0 : }
308 : :
309 : : static void
310 : 0 : gsettings_list_recursively (void)
311 : : {
312 : 0 : if (global_settings)
313 : : {
314 : 0 : list_recursively (global_settings);
315 : 0 : }
316 : : else
317 : : {
318 : : gchar **schemas;
319 : : gint i;
320 : :
321 : 0 : g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
322 : 0 : qsort (schemas, g_strv_length (schemas), sizeof (gchar*), qsort_cmp);
323 : :
324 : 0 : for (i = 0; schemas[i]; i++)
325 : : {
326 : : GSettings *settings;
327 : : GSettingsSchema *schema;
328 : :
329 : 0 : schema = g_settings_schema_source_lookup (global_schema_source, schemas[i], FALSE);
330 : 0 : if (!schema)
331 : 0 : continue;
332 : 0 : settings = g_settings_new_full (schema, NULL, NULL);
333 : 0 : list_recursively (settings);
334 : 0 : g_object_unref (settings);
335 : 0 : g_settings_schema_unref (schema);
336 : 0 : }
337 : :
338 : 0 : g_strfreev (schemas);
339 : : }
340 : 0 : }
341 : :
342 : : static void
343 : 0 : gsettings_description (void)
344 : : {
345 : : const gchar *description;
346 : 0 : description = g_settings_schema_key_get_description (global_schema_key);
347 : 0 : if (description == NULL)
348 : 0 : description = g_settings_schema_key_get_summary (global_schema_key);
349 : 0 : g_print ("%s\n", description);
350 : 0 : }
351 : :
352 : : static void
353 : 0 : gsettings_range (void)
354 : : {
355 : : GVariant *range, *detail;
356 : : const gchar *type;
357 : :
358 : 0 : range = g_settings_schema_key_get_range (global_schema_key);
359 : 0 : g_variant_get (range, "(&sv)", &type, &detail);
360 : :
361 : 0 : if (strcmp (type, "type") == 0)
362 : 0 : g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
363 : :
364 : 0 : else if (strcmp (type, "range") == 0)
365 : : {
366 : : GVariant *min, *max;
367 : : gchar *smin, *smax;
368 : :
369 : 0 : g_variant_get (detail, "(**)", &min, &max);
370 : 0 : smin = g_variant_print (min, FALSE);
371 : 0 : smax = g_variant_print (max, FALSE);
372 : :
373 : 0 : g_print ("range %s %s %s\n",
374 : 0 : g_variant_get_type_string (min), smin, smax);
375 : 0 : g_variant_unref (min);
376 : 0 : g_variant_unref (max);
377 : 0 : g_free (smin);
378 : 0 : g_free (smax);
379 : 0 : }
380 : :
381 : 0 : else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
382 : : {
383 : : GVariantIter iter;
384 : : GVariant *item;
385 : :
386 : 0 : g_print ("%s\n", type);
387 : :
388 : 0 : g_variant_iter_init (&iter, detail);
389 : 0 : while (g_variant_iter_loop (&iter, "*", &item))
390 : : {
391 : : gchar *printed;
392 : :
393 : 0 : printed = g_variant_print (item, FALSE);
394 : 0 : g_print ("%s\n", printed);
395 : 0 : g_free (printed);
396 : : }
397 : 0 : }
398 : :
399 : 0 : g_variant_unref (detail);
400 : 0 : g_variant_unref (range);
401 : 0 : }
402 : :
403 : : static void
404 : 0 : gsettings_get (void)
405 : : {
406 : : GVariant *value;
407 : : gchar *printed;
408 : :
409 : 0 : value = g_settings_get_value (global_settings, global_key);
410 : 0 : printed = g_variant_print (value, TRUE);
411 : 0 : g_print ("%s\n", printed);
412 : 0 : g_variant_unref (value);
413 : 0 : g_free (printed);
414 : 0 : }
415 : :
416 : : static void
417 : 0 : gsettings_reset (void)
418 : : {
419 : 0 : g_settings_reset (global_settings, global_key);
420 : 0 : g_settings_sync ();
421 : 0 : }
422 : :
423 : : static void
424 : 0 : reset_all_keys (GSettings *settings)
425 : : {
426 : : GSettingsSchema *schema;
427 : : gchar **keys;
428 : : gint i;
429 : :
430 : 0 : g_object_get (settings, "settings-schema", &schema, NULL);
431 : :
432 : 0 : keys = g_settings_schema_list_keys (schema);
433 : 0 : for (i = 0; keys[i]; i++)
434 : : {
435 : 0 : g_settings_reset (settings, keys[i]);
436 : 0 : }
437 : :
438 : 0 : g_settings_schema_unref (schema);
439 : 0 : g_strfreev (keys);
440 : 0 : }
441 : :
442 : : static void
443 : 0 : gsettings_reset_recursively (void)
444 : : {
445 : : gchar **children;
446 : : gint i;
447 : :
448 : 0 : g_settings_delay (global_settings);
449 : :
450 : 0 : reset_all_keys (global_settings);
451 : 0 : children = g_settings_list_children (global_settings);
452 : 0 : for (i = 0; children[i]; i++)
453 : : {
454 : : GSettings *child;
455 : 0 : child = g_settings_get_child (global_settings, children[i]);
456 : :
457 : 0 : reset_all_keys (child);
458 : :
459 : 0 : g_object_unref (child);
460 : 0 : }
461 : :
462 : 0 : g_strfreev (children);
463 : :
464 : 0 : g_settings_apply (global_settings);
465 : 0 : g_settings_sync ();
466 : 0 : }
467 : :
468 : : static void
469 : 0 : gsettings_writable (void)
470 : : {
471 : 0 : g_print ("%s\n",
472 : 0 : g_settings_is_writable (global_settings, global_key) ?
473 : : "true" : "false");
474 : 0 : }
475 : :
476 : : static void
477 : 0 : value_changed (GSettings *settings,
478 : : const gchar *key,
479 : : gpointer user_data)
480 : : {
481 : : GVariant *value;
482 : : gchar *printed;
483 : :
484 : 0 : value = g_settings_get_value (settings, key);
485 : 0 : printed = g_variant_print (value, TRUE);
486 : 0 : g_print ("%s: %s\n", key, printed);
487 : 0 : g_variant_unref (value);
488 : 0 : g_free (printed);
489 : 0 : }
490 : :
491 : : static void
492 : 0 : gsettings_monitor (void)
493 : : {
494 : 0 : if (global_key)
495 : : {
496 : : gchar *name;
497 : :
498 : 0 : name = g_strdup_printf ("changed::%s", global_key);
499 : 0 : g_signal_connect (global_settings, name, G_CALLBACK (value_changed), NULL);
500 : 0 : }
501 : : else
502 : 0 : g_signal_connect (global_settings, "changed", G_CALLBACK (value_changed), NULL);
503 : :
504 : 0 : for (;;)
505 : 0 : g_main_context_iteration (NULL, TRUE);
506 : : }
507 : :
508 : : static void
509 : 0 : gsettings_set (void)
510 : : {
511 : : const GVariantType *type;
512 : 0 : GError *error = NULL;
513 : : GVariant *new;
514 : :
515 : 0 : type = g_settings_schema_key_get_value_type (global_schema_key);
516 : :
517 : 0 : new = g_variant_parse (type, global_value, NULL, NULL, &error);
518 : :
519 : : /* If that didn't work and the type is string then we should assume
520 : : * that the user is just trying to set a string directly and forgot
521 : : * the quotes (or had them consumed by the shell).
522 : : *
523 : : * If the user started with a quote then we assume that some deeper
524 : : * problem is at play and we want the failure in that case.
525 : : *
526 : : * Consider:
527 : : *
528 : : * gsettings set x.y.z key "'i don't expect this to work'"
529 : : *
530 : : * Note that we should not just add quotes and try parsing again, but
531 : : * rather assume that the user is providing us with a bare string.
532 : : * Assume we added single quotes, then consider this case:
533 : : *
534 : : * gsettings set x.y.z key "i'd expect this to work"
535 : : *
536 : : * A similar example could be given for double quotes.
537 : : *
538 : : * Avoid that whole mess by just using g_variant_new_string().
539 : : */
540 : 0 : if (new == NULL &&
541 : 0 : g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
542 : 0 : global_value[0] != '\'' && global_value[0] != '"')
543 : : {
544 : 0 : g_clear_error (&error);
545 : 0 : new = g_variant_new_string (global_value);
546 : 0 : g_variant_ref_sink (new);
547 : 0 : }
548 : :
549 : 0 : if (new == NULL)
550 : : {
551 : : gchar *context;
552 : :
553 : 0 : context = g_variant_parse_error_print_context (error, global_value);
554 : 0 : g_printerr ("%s", context);
555 : 0 : g_free (context);
556 : 0 : exit (1);
557 : : }
558 : :
559 : 0 : if (!g_settings_schema_key_range_check (global_schema_key, new))
560 : : {
561 : 0 : g_printerr (_("The provided value is outside of the valid range\n"));
562 : 0 : g_variant_unref (new);
563 : 0 : exit (1);
564 : : }
565 : :
566 : 0 : if (!g_settings_set_value (global_settings, global_key, new))
567 : : {
568 : 0 : g_printerr (_("The key is not writable\n"));
569 : 0 : g_variant_unref (new);
570 : 0 : exit (1);
571 : : }
572 : :
573 : 0 : g_settings_sync ();
574 : :
575 : 0 : g_variant_unref (new);
576 : 0 : }
577 : :
578 : : static int
579 : 0 : gsettings_help (gboolean requested,
580 : : const gchar *command)
581 : : {
582 : 0 : const gchar *description = NULL;
583 : 0 : const gchar *synopsis = NULL;
584 : : GString *string;
585 : :
586 : 0 : string = g_string_new (NULL);
587 : :
588 : 0 : if (command == NULL)
589 : : ;
590 : :
591 : 0 : else if (strcmp (command, "help") == 0)
592 : : {
593 : 0 : description = _("Print help");
594 : 0 : synopsis = "[COMMAND]";
595 : 0 : }
596 : :
597 : 0 : else if (strcmp (command, "--version") == 0)
598 : : {
599 : 0 : description = _("Print version information and exit");
600 : 0 : synopsis = "";
601 : 0 : }
602 : :
603 : 0 : else if (strcmp (command, "list-schemas") == 0)
604 : : {
605 : 0 : description = _("List the installed (non-relocatable) schemas");
606 : 0 : synopsis = "[--print-paths]";
607 : 0 : }
608 : :
609 : 0 : else if (strcmp (command, "list-relocatable-schemas") == 0)
610 : : {
611 : 0 : description = _("List the installed relocatable schemas");
612 : 0 : synopsis = "";
613 : 0 : }
614 : :
615 : 0 : else if (strcmp (command, "list-keys") == 0)
616 : : {
617 : 0 : description = _("List the keys in SCHEMA");
618 : 0 : synopsis = N_("SCHEMA[:PATH]");
619 : 0 : }
620 : :
621 : 0 : else if (strcmp (command, "list-children") == 0)
622 : : {
623 : 0 : description = _("List the children of SCHEMA");
624 : 0 : synopsis = N_("SCHEMA[:PATH]");
625 : 0 : }
626 : :
627 : 0 : else if (strcmp (command, "list-recursively") == 0)
628 : : {
629 : 0 : description = _("List keys and values, recursively\n"
630 : : "If no SCHEMA is given, list all keys\n");
631 : 0 : synopsis = N_("[SCHEMA[:PATH]]");
632 : 0 : }
633 : :
634 : 0 : else if (strcmp (command, "get") == 0)
635 : : {
636 : 0 : description = _("Get the value of KEY");
637 : 0 : synopsis = N_("SCHEMA[:PATH] KEY");
638 : 0 : }
639 : :
640 : 0 : else if (strcmp (command, "range") == 0)
641 : : {
642 : 0 : description = _("Query the range of valid values for KEY");
643 : 0 : synopsis = N_("SCHEMA[:PATH] KEY");
644 : 0 : }
645 : :
646 : 0 : else if (strcmp (command, "describe") == 0)
647 : : {
648 : 0 : description = _("Query the description for KEY");
649 : 0 : synopsis = N_("SCHEMA[:PATH] KEY");
650 : 0 : }
651 : :
652 : 0 : else if (strcmp (command, "set") == 0)
653 : : {
654 : 0 : description = _("Set the value of KEY to VALUE");
655 : 0 : synopsis = N_("SCHEMA[:PATH] KEY VALUE");
656 : 0 : }
657 : :
658 : 0 : else if (strcmp (command, "reset") == 0)
659 : : {
660 : 0 : description = _("Reset KEY to its default value");
661 : 0 : synopsis = N_("SCHEMA[:PATH] KEY");
662 : 0 : }
663 : :
664 : 0 : else if (strcmp (command, "reset-recursively") == 0)
665 : : {
666 : 0 : description = _("Reset all keys in SCHEMA to their defaults");
667 : 0 : synopsis = N_("SCHEMA[:PATH]");
668 : 0 : }
669 : :
670 : 0 : else if (strcmp (command, "writable") == 0)
671 : : {
672 : 0 : description = _("Check if KEY is writable");
673 : 0 : synopsis = N_("SCHEMA[:PATH] KEY");
674 : 0 : }
675 : :
676 : 0 : else if (strcmp (command, "monitor") == 0)
677 : : {
678 : 0 : description = _("Monitor KEY for changes.\n"
679 : : "If no KEY is specified, monitor all keys in SCHEMA.\n"
680 : : "Use ^C to stop monitoring.\n");
681 : 0 : synopsis = N_("SCHEMA[:PATH] [KEY]");
682 : 0 : }
683 : : else
684 : : {
685 : 0 : g_string_printf (string, _("Unknown command %s\n\n"), command);
686 : 0 : requested = FALSE;
687 : 0 : command = NULL;
688 : : }
689 : :
690 : 0 : if (command == NULL)
691 : : {
692 : 0 : g_string_append (string,
693 : 0 : _("Usage:\n"
694 : : " gsettings --version\n"
695 : : " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS…]\n"
696 : : "\n"
697 : : "Commands:\n"
698 : : " help Show this information\n"
699 : : " list-schemas List installed schemas\n"
700 : : " list-relocatable-schemas List relocatable schemas\n"
701 : : " list-keys List keys in a schema\n"
702 : : " list-children List children of a schema\n"
703 : : " list-recursively List keys and values, recursively\n"
704 : : " range Queries the range of a key\n"
705 : : " describe Queries the description of a key\n"
706 : : " get Get the value of a key\n"
707 : : " set Set the value of a key\n"
708 : : " reset Reset the value of a key\n"
709 : : " reset-recursively Reset all values in a given schema\n"
710 : : " writable Check if a key is writable\n"
711 : : " monitor Watch for changes\n"
712 : : "\n"
713 : : "Use “gsettings help COMMAND” to get detailed help.\n\n"));
714 : 0 : }
715 : : else
716 : : {
717 : 0 : g_string_append_printf (string, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
718 : 0 : command, synopsis[0] ? _(synopsis) : "", description);
719 : :
720 : 0 : g_string_append (string, _("Arguments:\n"));
721 : :
722 : 0 : g_string_append (string,
723 : 0 : _(" SCHEMADIR A directory to search for additional schemas\n"));
724 : :
725 : 0 : if (strstr (synopsis, "[COMMAND]"))
726 : 0 : g_string_append (string,
727 : 0 : _(" COMMAND The (optional) command to explain\n"));
728 : :
729 : 0 : else if (strstr (synopsis, "SCHEMA"))
730 : 0 : g_string_append (string,
731 : 0 : _(" SCHEMA The name of the schema\n"
732 : : " PATH The path, for relocatable schemas\n"));
733 : :
734 : 0 : if (strstr (synopsis, "[KEY]"))
735 : 0 : g_string_append (string,
736 : 0 : _(" KEY The (optional) key within the schema\n"));
737 : :
738 : 0 : else if (strstr (synopsis, "KEY"))
739 : 0 : g_string_append (string,
740 : 0 : _(" KEY The key within the schema\n"));
741 : :
742 : 0 : if (strstr (synopsis, "VALUE"))
743 : 0 : g_string_append (string,
744 : 0 : _(" VALUE The value to set\n"));
745 : :
746 : 0 : g_string_append (string, "\n");
747 : : }
748 : :
749 : 0 : if (requested)
750 : 0 : g_print ("%s", string->str);
751 : : else
752 : 0 : g_printerr ("%s\n", string->str);
753 : :
754 : 0 : g_string_free (string, TRUE);
755 : :
756 : 0 : return requested ? 0 : 1;
757 : : }
758 : :
759 : :
760 : : int
761 : 0 : main (int argc, char **argv)
762 : : {
763 : : void (* function) (void);
764 : : gboolean need_settings, skip_third_arg_test;
765 : :
766 : : #ifdef G_OS_WIN32
767 : : gchar *tmp;
768 : : #endif
769 : :
770 : 0 : setlocale (LC_ALL, GLIB_DEFAULT_LOCALE);
771 : 0 : textdomain (GETTEXT_PACKAGE);
772 : :
773 : : #ifdef G_OS_WIN32
774 : 0 : tmp = _glib_get_locale_dir ();
775 : 0 : bindtextdomain (GETTEXT_PACKAGE, tmp);
776 : 0 : g_free (tmp);
777 : : #else
778 : 0 : bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
779 : : #endif
780 : :
781 : : #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
782 : 0 : bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
783 : : #endif
784 : :
785 : 0 : if (argc < 2)
786 : 0 : return gsettings_help (FALSE, NULL);
787 : :
788 : 0 : global_schema_source = g_settings_schema_source_get_default ();
789 : :
790 : 0 : if (argc > 3 && g_str_equal (argv[1], "--schemadir"))
791 : 0 : {
792 : 0 : GSettingsSchemaSource *parent = global_schema_source;
793 : 0 : GError *error = NULL;
794 : :
795 : 0 : global_schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error);
796 : :
797 : 0 : if (global_schema_source == NULL)
798 : : {
799 : 0 : g_printerr (_("Could not load schemas from %s: %s\n"), argv[2], error->message);
800 : 0 : g_clear_error (&error);
801 : :
802 : 0 : return 1;
803 : : }
804 : :
805 : : /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
806 : 0 : argv = argv + 2;
807 : 0 : argc -= 2;
808 : 0 : }
809 : 0 : else if (global_schema_source == NULL)
810 : : {
811 : 0 : g_printerr (_("No schemas installed\n"));
812 : 0 : return 1;
813 : : }
814 : : else
815 : 0 : g_settings_schema_source_ref (global_schema_source);
816 : :
817 : 0 : need_settings = TRUE;
818 : 0 : skip_third_arg_test = FALSE;
819 : :
820 : 0 : if (strcmp (argv[1], "help") == 0)
821 : 0 : return gsettings_help (TRUE, argv[2]);
822 : :
823 : 0 : else if (argc == 2 && strcmp (argv[1], "--version") == 0)
824 : 0 : function = gsettings_print_version;
825 : :
826 : 0 : else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
827 : 0 : function = gsettings_list_schemas;
828 : :
829 : 0 : else if (argc == 3 && strcmp (argv[1], "list-schemas") == 0
830 : 0 : && strcmp (argv[2], "--print-paths") == 0)
831 : : {
832 : 0 : skip_third_arg_test = TRUE;
833 : 0 : function = gsettings_list_schemas_with_paths;
834 : 0 : }
835 : :
836 : 0 : else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
837 : 0 : function = gsettings_list_relocatable_schemas;
838 : :
839 : 0 : else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
840 : : {
841 : 0 : need_settings = FALSE;
842 : 0 : function = gsettings_list_keys;
843 : 0 : }
844 : :
845 : 0 : else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
846 : 0 : function = gsettings_list_children;
847 : :
848 : 0 : else if ((argc == 2 || argc == 3) && strcmp (argv[1], "list-recursively") == 0)
849 : 0 : function = gsettings_list_recursively;
850 : :
851 : 0 : else if (argc == 4 && strcmp (argv[1], "describe") == 0)
852 : : {
853 : 0 : need_settings = FALSE;
854 : 0 : function = gsettings_description;
855 : 0 : }
856 : :
857 : 0 : else if (argc == 4 && strcmp (argv[1], "range") == 0)
858 : : {
859 : 0 : need_settings = FALSE;
860 : 0 : function = gsettings_range;
861 : 0 : }
862 : :
863 : 0 : else if (argc == 4 && strcmp (argv[1], "get") == 0)
864 : 0 : function = gsettings_get;
865 : :
866 : 0 : else if (argc == 5 && strcmp (argv[1], "set") == 0)
867 : 0 : function = gsettings_set;
868 : :
869 : 0 : else if (argc == 4 && strcmp (argv[1], "reset") == 0)
870 : 0 : function = gsettings_reset;
871 : :
872 : 0 : else if (argc == 3 && strcmp (argv[1], "reset-recursively") == 0)
873 : 0 : function = gsettings_reset_recursively;
874 : :
875 : 0 : else if (argc == 4 && strcmp (argv[1], "writable") == 0)
876 : 0 : function = gsettings_writable;
877 : :
878 : 0 : else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
879 : 0 : function = gsettings_monitor;
880 : :
881 : : else
882 : 0 : return gsettings_help (FALSE, argv[1]);
883 : :
884 : 0 : if (argc > 2 && !skip_third_arg_test)
885 : : {
886 : : gchar **parts;
887 : :
888 : 0 : if (argv[2][0] == '\0')
889 : : {
890 : 0 : g_printerr (_("Empty schema name given\n"));
891 : 0 : return 1;
892 : : }
893 : :
894 : 0 : parts = g_strsplit (argv[2], ":", 2);
895 : :
896 : 0 : global_schema = g_settings_schema_source_lookup (global_schema_source, parts[0], TRUE);
897 : :
898 : 0 : if (need_settings)
899 : : {
900 : 0 : if (parts[1])
901 : : {
902 : 0 : if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
903 : : {
904 : 0 : g_strfreev (parts);
905 : 0 : return 1;
906 : : }
907 : :
908 : 0 : global_settings = g_settings_new_full (global_schema, NULL, parts[1]);
909 : 0 : }
910 : : else
911 : : {
912 : 0 : if (!check_schema (global_schema, parts[0]))
913 : : {
914 : 0 : g_strfreev (parts);
915 : 0 : return 1;
916 : : }
917 : :
918 : 0 : global_settings = g_settings_new_full (global_schema, NULL, NULL);
919 : : }
920 : 0 : }
921 : : else
922 : : {
923 : : /* If the user has given a path then we enforce that we have a
924 : : * relocatable schema, but if they didn't give a path then it
925 : : * doesn't matter what type of schema we have (since it's
926 : : * reasonable to ask for introspection information on a
927 : : * relocatable schema without having to give the path).
928 : : */
929 : 0 : if (parts[1])
930 : : {
931 : 0 : if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
932 : : {
933 : 0 : g_strfreev (parts);
934 : 0 : return 1;
935 : : }
936 : 0 : }
937 : : else
938 : : {
939 : 0 : if (global_schema == NULL)
940 : : {
941 : 0 : g_printerr (_("No such schema “%s”\n"), parts[0]);
942 : 0 : g_strfreev (parts);
943 : 0 : return 1;
944 : : }
945 : : }
946 : : }
947 : :
948 : 0 : g_strfreev (parts);
949 : 0 : }
950 : :
951 : 0 : if (argc > 3)
952 : : {
953 : 0 : if (!g_settings_schema_has_key (global_schema, argv[3]))
954 : : {
955 : 0 : g_printerr (_("No such key “%s”\n"), argv[3]);
956 : 0 : return 1;
957 : : }
958 : :
959 : 0 : global_key = argv[3];
960 : 0 : global_schema_key = g_settings_schema_get_key (global_schema, global_key);
961 : 0 : }
962 : :
963 : 0 : if (argc > 4)
964 : 0 : global_value = argv[4];
965 : :
966 : 0 : (* function) ();
967 : :
968 : :
969 : 0 : g_clear_pointer (&global_schema_source, g_settings_schema_source_unref);
970 : 0 : g_clear_pointer (&global_schema_key, g_settings_schema_key_unref);
971 : 0 : g_clear_pointer (&global_schema, g_settings_schema_unref);
972 : 0 : g_clear_object (&global_settings);
973 : :
974 : 0 : return 0;
975 : 0 : }
|