diff options
author | Ryan Lortie <desrt@desrt.ca> | 2010-07-18 23:23:54 -0400 |
---|---|---|
committer | Ryan Lortie <desrt@desrt.ca> | 2010-07-18 23:23:54 -0400 |
commit | 67cb6fa67a608ba5b778e6e0bba9578a0a6ab075 (patch) | |
tree | a168ebcf2cf2291a903ae77c9af1c88c2bca1966 /bin | |
parent | 4c375030eb6254b757b6fa1bba6dbd0c5d69774f (diff) | |
download | dconf-67cb6fa67a608ba5b778e6e0bba9578a0a6ab075.tar.gz |
assorted client API cleanups, vala port of 'dconf'
Diffstat (limited to 'bin')
-rw-r--r-- | bin/.gitignore | 2 | ||||
-rw-r--r-- | bin/Makefile.am | 4 | ||||
-rw-r--r-- | bin/dconf.c | 242 | ||||
-rw-r--r-- | bin/dconf.vala | 56 |
4 files changed, 61 insertions, 243 deletions
diff --git a/bin/.gitignore b/bin/.gitignore index 08c4537..b9cdeb5 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -1,4 +1,4 @@ dconf dconf-update -dconf-update.c +*.c *.stamp diff --git a/bin/Makefile.am b/bin/Makefile.am index 5544f82..2f77617 100644 --- a/bin/Makefile.am +++ b/bin/Makefile.am @@ -8,3 +8,7 @@ dconf_LDADD = ../client/libdconf.la $(gio_LIBS) dconf_update_VALAFLAGS = --pkg=posix --pkg=gio-2.0 dconf_update_LDADD = $(gio_LIBS) dconf_update_SOURCES = dconf-update.vala ../gvdb/gvdb-builder.c gvdb.vapi fixes.vapi + +dconf_VALAFLAGS = --pkg=gio-2.0 ../client/dconf.vapi +dconf_LDADD = $(gio_LIBS) ../client/libdconf.la +dconf_SOURCES = dconf.vala diff --git a/bin/dconf.c b/bin/dconf.c deleted file mode 100644 index 5702d9b..0000000 --- a/bin/dconf.c +++ /dev/null @@ -1,242 +0,0 @@ -#include <dconf.h> - -static const gchar * -shift (int *argc, char ***argv) -{ - if (argc == 0) - return NULL; - - (*argc)--; - return *(*argv)++; -} - -static gboolean -grab_args (int argc, - char **argv, - const gchar *description, - GError **error, - gint num, - ...) -{ - va_list ap; - - if (argc != num) - { - g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, - "require exactly %d arguments: %s", num, description); - return FALSE; - } - - va_start (ap, num); - while (num--) - *va_arg (ap, gchar **) = *argv++; - va_end (ap); - - return TRUE; -} - -static gboolean -ensure (const gchar *type, - const gchar *string, - gboolean (*checker) (const gchar *string), - GError **error) -{ - if (!checker (string)) - { - g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, - "'%s' is not a dconf %s", string, type); - return FALSE; - } - - return TRUE; -} - -static void -write_done (GObject *object, - GAsyncResult *result, - gpointer user_data) -{ - GError *error = NULL; - - if (!dconf_client_write_finish (DCONF_CLIENT (object), result, NULL, &error)) - g_error ("%s\n", error->message); - - g_print ("done\n"); -} - -static void -do_async_command (DConfClient *client, - int argc, - char **argv) -{ - const gchar *cmd; - - cmd = shift (&argc, &argv); - - if (g_strcmp0 (cmd, "write") == 0) - { - const gchar *key, *strval; - GVariant *value; - - if (!grab_args (argc, argv, "key and value", NULL, 2, &key, &strval)) - g_assert_not_reached (); - - if (!ensure ("key", key, dconf_is_key, NULL)) - g_assert_not_reached (); - - value = g_variant_parse (NULL, strval, NULL, NULL, NULL); - - g_assert (value != NULL); - - return dconf_client_write_async (client, key, value, - NULL, write_done, NULL); - } -} - -static gboolean -do_sync_command (DConfClient *client, - int argc, - char **argv, - GError **error) -{ - const gchar *cmd; - - cmd = shift (&argc, &argv); - - if (g_strcmp0 (cmd, "async") == 0) - { - do_async_command (client, argc, argv); - g_main_loop_run (g_main_loop_new (NULL, FALSE)); - return TRUE; - } - - else if (g_strcmp0 (cmd, "read") == 0) - { - const gchar *key; - GVariant *value; - gchar *printed; - - if (!grab_args (argc, argv, "key", error, 1, &key)) - return FALSE; - - if (!ensure ("key", key, dconf_is_key, error)) - return FALSE; - - value = dconf_client_read (client, key); - - if (value == NULL) - return TRUE; - - printed = g_variant_print (value, TRUE); - g_print ("%s\n", printed); - g_variant_unref (value); - g_free (printed); - - return TRUE; - } - - else if (g_strcmp0 (cmd, "write") == 0) - { - const gchar *key, *strval; - GVariant *value; - - if (!grab_args (argc, argv, "key and value", error, 2, &key, &strval)) - return FALSE; - - if (!ensure ("key", key, dconf_is_key, error)) - return FALSE; - - value = g_variant_parse (NULL, strval, NULL, NULL, error); - - if (value == NULL) - return FALSE; - - return dconf_client_write (client, key, value, NULL, NULL, error); - } - - else if (g_strcmp0 (cmd, "write-many") == 0) - { - g_assert_not_reached (); - } - - else if (g_strcmp0 (cmd, "list") == 0) - { - const gchar *dir; - gchar **list; - - if (!grab_args (argc, argv, "dir", error, 1, &dir)) - return FALSE; - - if (!ensure ("dir", dir, dconf_is_dir, error)) - return FALSE; - - list = dconf_client_list (client, dir, NULL); - - while (*list) - g_print ("%s\n", *list++); - - return TRUE; - } - - else if (g_strcmp0 (cmd, "lock") == 0) - { - const gchar *path; - - if (!grab_args (argc, argv, "path", error, 1, &path)) - return FALSE; - - if (!ensure ("path", path, dconf_is_path, error)) - return FALSE; - - return dconf_client_set_locked (client, path, TRUE, NULL, NULL); - } - - else if (g_strcmp0 (cmd, "unlock") == 0) - { - const gchar *path; - - if (!grab_args (argc, argv, "path", error, 1, &path)) - return FALSE; - - if (!ensure ("path", path, dconf_is_path, error)) - return FALSE; - - return dconf_client_set_locked (client, path, FALSE, NULL, NULL); - } - - else if (g_strcmp0 (cmd, "is-writable") == 0) - { - const gchar *path; - - if (!grab_args (argc, argv, "path", error, 1, &path)) - return FALSE; - - if (!ensure ("path", path, dconf_is_path, error)) - return FALSE; - - return dconf_client_is_writable (client, path, error); - } - - else - { - g_set_error (error, 0, 0, "unknown command"); - return FALSE; - } -} - -int -main (int argc, char **argv) -{ - GError *error = NULL; - DConfClient *client; - - g_type_init (); - g_set_prgname (shift (&argc, &argv)); - - client = dconf_client_new (NULL, FALSE, NULL, NULL, NULL); - - if (!do_sync_command (client, argc, argv, &error)) - g_error ("%s\n", error->message); - - return 0; -} diff --git a/bin/dconf.vala b/bin/dconf.vala new file mode 100644 index 0000000..6276ba7 --- /dev/null +++ b/bin/dconf.vala @@ -0,0 +1,56 @@ + + +void do_read (DConf.Client client, string key) throws Error { + DConf.verify_key (key); + + var result = client.read (key); + if (result != null) { + stdout.puts (result.print (true)); + stdout.putc ('\n'); + } +} + +void do_list (DConf.Client client, string dir) throws Error { + DConf.verify_dir (dir); + + foreach (var item in client.list (dir)) { + stdout.puts (item); + stdout.putc ('\n'); + } +} + +void do_write (DConf.Client client, string key, string val) throws Error { + DConf.verify_key (key); + + client.write (key, Variant.parse (null, val)); +} + +void main (string[] args) { + try { + var client = new DConf.Client (); + + Environment.set_prgname (args[0]); + + switch (args[1]) { + case "read": + do_read (client, args[2]); + break; + + case "list": + do_list (client, args[2]); + break; + + case "write": + do_write (client, args[2], args[3]); + break; + + default: + error ("unknown command"); + break; + } + } catch (Error e) { + stderr.printf ("error: %s\n", e.message); + } +} + +// vim:noet sw=4 ts=4 |