[dconf] more client library API, exposed by cmdline tool



commit 3750e8bc5d0474226053ebffec8c92328d6ea416
Author: Ryan Lortie <desrt desrt ca>
Date:   Sun May 23 20:09:22 2010 -0400

    more client library API, exposed by cmdline tool

 bin/dconf.c           |  193 +++++++++++++++++++++++++++++++++++++++++++------
 client/dconf-client.c |   43 +++++++++--
 engine/dconf-engine.c |   35 +++++++++-
 engine/dconf-engine.h |   10 +--
 4 files changed, 243 insertions(+), 38 deletions(-)
---
diff --git a/bin/dconf.c b/bin/dconf.c
index 3a5e230..0f81d77 100644
--- a/bin/dconf.c
+++ b/bin/dconf.c
@@ -1,45 +1,194 @@
 #include <dconf.h>
 
-int
-main (int argc, char **argv)
+const gchar *
+shift (int *argc, char ***argv)
 {
-  DConfClient *client;
+  if (argc == 0)
+    return NULL;
 
-  g_type_init ();
+  (*argc)--;
+  return *(*argv)++;
+}
 
-  client = dconf_client_new (NULL, NULL, NULL, NULL);
+const gchar *
+peek (int argc, char **argv)
+{
+  if (argc == 0)
+    return NULL;
+
+  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);
+}
+
+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;
+    }
 
-  if (g_strcmp0 (argv[1], "get") == 0)
+  return TRUE;
+}
+
+static gboolean
+do_sync_command (DConfClient  *client,
+                 int           argc,
+                 char        **argv,
+                 GError      **error)
+{
+  const gchar *cmd;
+
+  cmd = shift (&argc, &argv);
+
+  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, argv[2], DCONF_READ_NORMAL);
+      value = dconf_client_read (client, key, DCONF_READ_NORMAL);
 
       if (value == NULL)
-        g_print ("(null)\n");
-      else
-        {
-          gchar *printed;
-          printed = g_variant_print (value, TRUE);
-          g_print ("%s\n", printed);
-          g_variant_unref (value);
-          g_free (printed);
-        }
+        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 (argv[1], "set") == 0)
+  else if (g_strcmp0 (cmd, "write") == 0)
     {
-      GError *error = NULL;
+      const gchar *key, *strval;
       GVariant *value;
 
-      value = g_variant_parse (NULL, argv[3], NULL, NULL, &error);
+      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)
-        g_error ("%s\n", error->message);
+        return FALSE;
 
-      if (!dconf_client_write (client, argv[2], value, NULL, NULL, &error))
-        g_error ("%s\n", error->message);
+      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);
+    }
+
+  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);
+    }
+
+  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);
+    }
+}
+
+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, NULL, NULL, NULL);
+
+  if (!do_sync_command (client, argc, argv, &error))
+    g_error ("%s\n", error->message);
+
   return 0;
 }
diff --git a/client/dconf-client.c b/client/dconf-client.c
index 76025cb..ca5e5af 100644
--- a/client/dconf-client.c
+++ b/client/dconf-client.c
@@ -318,6 +318,42 @@ dconf_client_write_finish (DConfClient   *client,
 }
 
 
+gchar **
+dconf_client_list (DConfClient    *client,
+                   const gchar    *prefix,
+                   DConfResetList *resets)
+{
+  return dconf_engine_list (client->engine, prefix, resets);
+}
+
+gboolean
+dconf_client_set_locked (DConfClient   *client,
+                         const gchar   *path,
+                         gboolean       locked,
+                         GCancellable  *cancellable,
+                         GError       **error)
+{
+  DConfEngineMessage dcem;
+
+  dconf_engine_set_locked (client->engine, &dcem, path, locked);
+
+  return dconf_client_call_sync (client, &dcem, NULL, cancellable, error);
+}
+
+gboolean
+dconf_client_is_writable (DConfClient  *client,
+                          const gchar  *path,
+                          GError      **error)
+{
+  DConfEngineMessage dcem;
+
+  if (!dconf_engine_is_writable (client->engine, &dcem, path, error))
+    return FALSE;
+
+  return dconf_client_call_sync (client, &dcem, NULL, NULL, error);
+}
+
+
 
 #if 0
 
@@ -325,13 +361,6 @@ GVariant *              dconf_client_read                               (DConfCl
                                                                          const gchar          *key,
                                                                          DConfReadType         type);
 
-gchar **                dconf_client_list                               (DConfClient          *client,
-                                                                         const gchar          *prefix,
-                                                                         DConfResetList       *resets);
-
-gboolean                dconf_client_is_writable                        (DConfClient          *client,
-                                                                         const gchar          *prefix,
-                                                                         GError              **error);
 
 gboolean                dconf_client_write_many                         (DConfClient          *client,
                                                                          const gchar          *prefix,
diff --git a/engine/dconf-engine.c b/engine/dconf-engine.c
index bbc1e84..062b77e 100644
--- a/engine/dconf-engine.c
+++ b/engine/dconf-engine.c
@@ -156,7 +156,8 @@ dconf_engine_write_many (DConfEngine          *engine,
                          DConfEngineMessage   *dcem,
                          const gchar          *prefix,
                          const gchar * const  *keys,
-                         GVariant            **values)
+                         GVariant            **values,
+                         GError              **error)
 {
   GVariantBuilder builder;
   gsize i;
@@ -171,3 +172,35 @@ dconf_engine_write_many (DConfEngine          *engine,
 
   return TRUE;
 }
+
+void
+dconf_engine_set_locked (DConfEngine        *engine,
+                         DConfEngineMessage *dcem,
+                         const gchar        *path,
+                         gboolean            locked)
+{
+  dconf_engine_dcem (engine, dcem, "SetLocked", "(sb)", path, locked);
+}
+
+gchar **
+dconf_engine_list (DConfEngine    *engine,
+                   const gchar    *dir,
+                   DConfResetList *resets)
+{
+  GvdbTable *table;
+  gchar *filename;
+  gchar **list;
+
+  /* not yet supported */
+  g_assert (resets == NULL);
+
+  filename = g_build_filename (g_get_user_config_dir (), "dconf", NULL);
+  table = gvdb_table_new (filename, FALSE, NULL);
+  g_free (filename);
+
+  list = gvdb_table_list (table, dir);
+
+  gvdb_table_unref (table);
+
+  return list;
+}
diff --git a/engine/dconf-engine.h b/engine/dconf-engine.h
index 785fced..c16af84 100644
--- a/engine/dconf-engine.h
+++ b/engine/dconf-engine.h
@@ -2,10 +2,10 @@
 #define _dconf_engine_h_
 
 #include <dconf-readtype.h>
+#include <dconf-resetlist.h>
 #include <glib.h>
 
 typedef struct _DConfEngine DConfEngine;
-typedef struct _DConfEngineResetList DConfEngineResetList;
 
 typedef struct
 {
@@ -32,7 +32,7 @@ GVariant *              dconf_engine_read                               (DConfEn
                                                                          DConfReadType            type);
 gchar **                dconf_engine_list                               (DConfEngine             *engine,
                                                                          const gchar             *path,
-                                                                         DConfEngineResetList    *resets);
+                                                                         DConfResetList          *resets);
 
 void                    dconf_engine_get_service_info                   (DConfEngine             *engine,
                                                                          const gchar            **bus_type,
@@ -62,10 +62,4 @@ void                    dconf_engine_decode_notify                      (DConfEn
                                                                          const gchar           ***keys,
                                                                          GVariant                *body);
 
-void                    dconf_engine_reset_list_init                    (DConfEngineResetList    *resets,
-                                                                         const gchar * const     *list);
-void                    dconf_engine_reset_list_add                     (DConfEngineResetList    *resets,
-                                                                         const gchar             *item);
-void                    dconf_engine_reset_list_clear                   (DConfEngineResetList    *resets);
-
 #endif /* _dconf_engine_h_ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]