summaryrefslogtreecommitdiff
path: root/bin/dconf.vala
blob: 9704d4abec2cdc1b456f64da4a54591d3234912f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * Copyright © 2010, 2011 Codethink Limited
 * Copyright © 2011 Canonical Limited
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the licence, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

void show_help (bool requested, string? command) {
	var str = new StringBuilder ();
	string? description = null;
	string? synopsis = null;

	switch (command) {
		case null:
			break;

		case "help":
			description = "Print help";
			synopsis = " COMMAND ";
			break;

		case "read":
			description = "Read the value of a key.  -d to read default values.";
			synopsis = " [-d] KEY ";
			break;

		case "list":
			description = "List the sub-keys and sub-dirs of a dir";
			synopsis = " DIR ";
			break;

		case "list-locks":
			description = "List the locks under a dir";
			synopsis = " DIR ";
			break;

		case "write":
			description = "Write a new value to a key";
			synopsis = " KEY VALUE ";
			break;

		case "reset":
			description = "Reset a key or dir.  -f is required for dirs.";
			synopsis = " [-f] PATH ";
			break;

		case "compile":
			description = "Compile a binary database from keyfiles";
			synopsis = " OUTPUT KEYFILEDIR ";
			break;

		case "update":
			description = "Update the system dconf databases";
			synopsis = "";
			break;

		case "watch":
			description = "Watch a path for key changes";
			synopsis = " PATH ";
			break;

		case "dump":
			description = "Dump an entire subpath to stdout";
			synopsis = " DIR ";
			break;

		case "load":
			description = "Populate a subpath from stdin";
			synopsis = " DIR ";
			break;

		default:
			str.append_printf ("Unknown command '%s'\n\n", command);
			command = null;
			break;
	}

	if (command == null) {
		str.append (
"""Usage:
  dconf COMMAND [ARGS...]

Commands:
  help              Show this information
  read              Read the value of a key
  list              List the contents of a dir
  write             Change the value of a key
  reset             Reset the value of a key or dir
  compile           Compile a binary database from keyfiles
  update            Update the system databases
  watch             Watch a path for changes
  dump              Dump an entire subpath to stdout
  load              Populate a subpath from stdin

Use 'dconf help COMMAND' to get detailed help.

""");
	} else {
		str.append ("Usage:\n");
		str.append_printf ("  dconf %s%s\n\n", command, synopsis);
		str.append_printf ("%s\n\n", description);

		if (synopsis != "") {
			str.append ("Arguments:\n");

			if (" COMMAND " in synopsis) {
				str.append ("  COMMAND     The (optional) command to explain\n");
			}

			if (" PATH " in synopsis) {
				str.append ("  PATH        Either a KEY or DIR\n");
			}

			if (" PATH " in synopsis || " KEY " in synopsis) {
				str.append ("  KEY         A key path (starting, but not ending with '/')\n");
			}

			if (" PATH " in synopsis || " DIR " in synopsis) {
				str.append ("  DIR         A directory path (starting and ending with '/')\n");
			}

			if (" VALUE " in synopsis) {
				str.append ("  VALUE       The value to write (in GVariant format)\n");
			}

			if (" OUTPUT " in synopsis) {
				str.append ("  OUTPUT      The filename of the (binary) output\n");
			}

			if (" KEYFILEDIR " in synopsis) {
				str.append ("  KEYFILEDIR  The path to the .d directory containing keyfiles\n");
			}
		}

		str.append ("\n");
	}

	if (requested) {
		print ("%s", str.str);
	} else {
		printerr ("%s", str.str);
	}
}

void dconf_help (string[] args) throws Error {
	show_help (true, args[2]);
}

void dconf_read (string?[] args) throws Error {
	var client = new DConf.Client ();
	var flags = DConf.ReadFlags.NONE;
	var index = 2;

	if (args[index] == "-d") {
		flags = DConf.ReadFlags.DEFAULT_VALUE;
		index++;
	}

	var key = args[index];

	DConf.verify_key (key);

	var result = client.read_full (key, flags, null);

	if (result != null) {
		print ("%s\n", result.print (true));
	}
}

void dconf_list (string?[] args) throws Error {
	var client = new DConf.Client ();
	var dir = args[2];

	DConf.verify_dir (dir);

	foreach (var item in client.list (dir)) {
		print ("%s\n", item);
	}
}

void dconf_list_locks (string?[] args) throws Error {
	var client = new DConf.Client ();
	var dir = args[2];

	DConf.verify_dir (dir);

	foreach (var item in client.list_locks (dir)) {
		print ("%s\n", item);
	}
}

void dconf_write (string?[] args) throws Error {
	var client = new DConf.Client ();
	var key = args[2];
	var val = args[3];

	DConf.verify_key (key);

	client.write_sync (key, Variant.parse (null, val));
}

void dconf_reset (string?[] args) throws Error {
	var client = new DConf.Client ();
	bool force = false;
	var index = 2;

	if (args[index] == "-f") {
		force = true;
		index++;
	}

	var path = args[index];

	DConf.verify_path (path);

	if (DConf.is_dir (path) && !force) {
		throw new OptionError.FAILED ("-f must be given to (recursively) reset entire dirs");
	}

	client.write_sync (path, null);
}

void show_path (DConf.Client client, string path) {
	if (DConf.is_key (path)) {
		var value = client.read (path);

		print ("  %s\n", value != null ? value.print (true) : "unset");
	}
}

void watch_function (DConf.Client client, string path, string[] items, string? tag) {
	foreach (var item in items) {
		var full = path + item;
		print ("%s\n", full);
		show_path (client, full);
	}
	print ("\n");
}

void dconf_watch (string?[] args) throws Error {
	var client = new DConf.Client ();
	var path = args[2];

	DConf.verify_path (path);

	client.changed.connect (watch_function);
	client.watch_sync (path);

	new MainLoop (null, false).run ();
}

void dconf_blame (string?[] args) throws Error {
	var connection = Bus.get_sync (BusType.SESSION, null);
	var reply = connection.call_sync ("ca.desrt.dconf", "/ca/desrt/dconf", "ca.desrt.dconf.ServiceInfo", "Blame",
	                                  null, new VariantType ("(s)"), DBusCallFlags.NONE, -1, null);
	print ("%s", reply.get_child_value (0).get_string (null));
}

void dconf_complete (string[] args) throws Error {
	var suffix = args[2];
	var path = args[3];

	if (path == "") {
		print ("/\n");
		return;
	}

	if (path[0] == '/') {
		var client = new DConf.Client ();
		var last = 0;

		for (var i = 1; path[i] != '\0'; i++) {
			if (path[i] == '/') {
				last = i;
			}
		}

		var dir = path.substring (0, last + 1);
		foreach (var item in client.list (dir)) {
			var full_item = dir + item;

			if (full_item.has_prefix (path) && item.has_suffix (suffix)) {
				print ("%s%s\n", full_item, full_item.has_suffix ("/") ? "" : " ");
			}
		}
	}
}

delegate void Command (string[] args) throws Error;

struct CommandMapping {
	Command func;
	string name;

	public CommandMapping (string name, Command func) {
		this.name = name;
		this.func = func;
	}
}

int main (string[] args) {
	assert (args.length != 0);
	Environment.set_prgname (args[0]);

	Intl.setlocale (LocaleCategory.ALL, "");

	var map = new CommandMapping[] {
		CommandMapping ("help",       dconf_help),
		CommandMapping ("read",       dconf_read),
		CommandMapping ("list",       dconf_list),
		CommandMapping ("list-locks", dconf_list_locks),
		CommandMapping ("write",      dconf_write),
		CommandMapping ("reset",      dconf_reset),
		CommandMapping ("compile",    dconf_compile),
		CommandMapping ("update",     dconf_update),
		CommandMapping ("watch",      dconf_watch),
		CommandMapping ("dump",       dconf_dump),
		CommandMapping ("load",       dconf_load),
		CommandMapping ("blame",      dconf_blame),
		CommandMapping ("_complete",  dconf_complete)
	};

	try {
		if (args[1] == null) {
			throw new OptionError.FAILED ("no command specified");
		}

		foreach (var mapping in map) {
			if (mapping.name == args[1]) {
				mapping.func (args);
				return 0;
			}
		}

		throw new OptionError.FAILED ("unknown command %s", args[1]);
	} catch (Error e) {
		stderr.printf ("error: %s\n\n", e.message);
		show_help (false, args[1]);
		return 1;
	}
}