[gconf-editor] policykit: port to GDBus



commit d91a2f465eaa22a0f47c0770d6295f5162d59f45
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Mon Mar 7 12:16:09 2011 -0500

    policykit: port to GDBus

 configure.in          |    3 +-
 src/gconf-policykit.c |  193 ++++++++++++++++++++++++-------------------------
 2 files changed, 95 insertions(+), 101 deletions(-)
---
diff --git a/configure.in b/configure.in
index 287f40a..94b9b14 100644
--- a/configure.in
+++ b/configure.in
@@ -31,8 +31,7 @@ GTK_REQUIRED=3.0.0
 
 PKG_CHECK_MODULES(GCONF_EDITOR,
 		  gconf-2.0 >= 2.9.2
-		  gtk+-3.0 >= GTK_REQUIRED
-		  dbus-glib-1)
+		  gtk+-3.0 >= GTK_REQUIRED)
 
 AC_PATH_PROG(GLIB_GENMARSHAL, glib-genmarshal)
 
diff --git a/src/gconf-policykit.c b/src/gconf-policykit.c
index 1f57a56..d24e4c4 100644
--- a/src/gconf-policykit.c
+++ b/src/gconf-policykit.c
@@ -32,51 +32,35 @@
 #include <unistd.h>
 #include <string.h>
 #include <sys/wait.h>
-
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus-glib-lowlevel.h>
+#include <gio/gio.h>
 
 #include "gconf-policykit.h"
 
 #define CACHE_VALIDITY_SEC 10
 
-static DBusGConnection *
-get_system_bus (void)
-{
-        GError          *error;
-        static DBusGConnection *bus = NULL;
-
-        if (bus == NULL) {
-                error = NULL;
-                bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
-                if (bus == NULL) {
-                        g_warning ("Couldn't connect to system bus: %s",
-                                   error->message);
-                        g_error_free (error);
-                }
-        }
-
-        return bus;
-}
-
 static guint
 can_set (const gchar *key, gboolean mandatory)
 {
-        DBusGConnection *bus;
-        DBusGProxy *proxy = NULL;
+        GDBusConnection *bus = NULL;
+        GDBusProxy *proxy = NULL;
 	const gchar *keys[2];
 	const gchar *func;
 	GError *error = NULL;
-        guint res = 0;
+        GVariant *res;
+        guint retval = 0;
 
-        bus = get_system_bus ();
+        bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM,
+                              NULL, NULL);
         if (bus == NULL)
                 goto out;
 
-        proxy = dbus_g_proxy_new_for_name (bus,
-                                           "org.gnome.GConf.Defaults",
-                                           "/",
-                                           "org.gnome.GConf.Defaults");
+        proxy = g_dbus_proxy_new_sync (bus,
+                                       G_DBUS_PROXY_FLAGS_NONE,
+                                       NULL,
+                                       "org.gnome.GConf.Defaults",
+                                       "/",
+                                       "org.gnome.GConf.Defaults",
+                                       NULL, NULL);
         if (proxy == NULL)
                 goto out;
 
@@ -84,21 +68,26 @@ can_set (const gchar *key, gboolean mandatory)
 	keys[1] = NULL;
 	func = mandatory ? "CanSetMandatory" : "CanSetSystem";
 
-        if (!dbus_g_proxy_call (proxy, func,
-                                &error,
-                                G_TYPE_STRV, keys,
-                                G_TYPE_INVALID,
-				G_TYPE_UINT, &res,
-                                G_TYPE_INVALID)) {
+        res = g_dbus_proxy_call_sync (proxy, func,
+                                      g_variant_new ("(^as)", keys),
+                                      G_DBUS_CALL_FLAGS_NONE,
+                                      -1, NULL, &error);
+
+        if (error != NULL) {
     		g_warning ("error calling %s: %s\n", func, error->message);
     		g_error_free (error);
-  	}
+  	} else {
+                g_variant_get (res, "(u)", &retval);
+        }
 
 out:
+        if (bus)
+                g_object_unref (bus);
+
 	if (proxy)
 		g_object_unref (proxy);
 
-        return res;
+        return retval;
 }
 
 typedef struct
@@ -176,30 +165,6 @@ typedef struct {
         GDestroyNotify  notify;
 } GConfPKCallbackData;
 
-static gpointer
-_gconf_pk_data_ref (gpointer d)
-{
-        GConfPKCallbackData *data = d;
-
-        data->ref_count++;
-
-        return data;
-}
-
-static void
-_gconf_pk_data_unref (gpointer d)
-{
-        GConfPKCallbackData *data = d;
-
-        data->ref_count--;
-        if (data->ref_count == 0) {
-                if (data->notify)
-                        data->notify (data->data);
-                g_free (data->key);
-                g_slice_free (GConfPKCallbackData, data);
-        }
-}
-
 static void set_key_async (GConfPKCallbackData *data);
 
 static void
@@ -207,21 +172,22 @@ gconf_pk_update_can_set_cache (const gchar *key,
                                gboolean     mandatory);
 
 static void
-set_key_notify (DBusGProxy     *proxy,
-                DBusGProxyCall *call,
-                void           *user_data)
+on_proxy_method_call (GObject *source,
+                      GAsyncResult *res,
+                      gpointer user_data)
 {
         GConfPKCallbackData *data = user_data;
         GError *error = NULL;
 
-        if (dbus_g_proxy_end_call (proxy, call, &error, G_TYPE_INVALID)) {
+        g_dbus_proxy_call_finish (G_DBUS_PROXY (source), res, &error);
+
+        if (error == NULL) {
 		gconf_pk_update_can_set_cache (data->key, data->mandatory);
                 if (data->callback)
                         data->callback (data->data, NULL);
-        }
-        else {
-                if (error->domain == DBUS_GERROR &&
-                    error->code == DBUS_GERROR_NO_REPLY) {
+        } else {
+                if (error->domain == G_DBUS_ERROR &&
+                    error->code == G_DBUS_ERROR_NO_REPLY) {
                         /* these errors happen because dbus doesn't
                          * use monotonic clocks
                          */
@@ -233,42 +199,75 @@ set_key_notify (DBusGProxy     *proxy,
                 else {
                         if (data->callback)
                                 data->callback (data->data, error);
-                        else
-                                g_error_free (error);
                 }
         }
 }
 
 static void
-set_key_async (GConfPKCallbackData *data)
+on_gconf_proxy_created (GObject *source,
+                        GAsyncResult *res,
+                        gpointer user_data)
 {
-        DBusGConnection *bus;
-        DBusGProxy      *proxy;
+        GConfPKCallbackData *data = user_data;
+        GDBusProxy *proxy;
 	const gchar     *call;
-        gchar           *keys[2] = { data->key, NULL };
+        gchar *keys[2] = { data->key, NULL };
+        gchar *dummy[2] = { NULL };
+        GError *error = NULL;
 
-        bus = get_system_bus ();
-        if (bus == NULL)
-                return;
+        proxy = g_dbus_proxy_new_finish (res, &error);
 
-        proxy = dbus_g_proxy_new_for_name (bus,
-                                           "org.gnome.GConf.Defaults",
-                                           "/",
-                                           "org.gnome.GConf.Defaults");
+        if (error != NULL) {
+                if (data->callback)
+                        data->callback (data->data, error);
+
+                return;
+        }
 
 	call = data->mandatory ? "SetMandatory" : "SetSystem";
-        dbus_g_proxy_begin_call_with_timeout (proxy,
-                                              call,
-                                              set_key_notify,
-                                              _gconf_pk_data_ref (data),
-                                              _gconf_pk_data_unref,
-                                              INT_MAX,
-                                              /* parameters: */
-                                              G_TYPE_STRV, keys,
-                                              G_TYPE_STRV, NULL,
-                                              G_TYPE_INVALID,
-                                              /* return values: */
-                                              G_TYPE_INVALID);
+        g_dbus_proxy_call (proxy,
+                           call, 
+                           g_variant_new ("(^as^as)",
+                                          keys, dummy),
+                           G_DBUS_CALL_FLAGS_NONE,
+                           -1, NULL,
+                           on_proxy_method_call, data);
+}
+
+static void
+on_system_bus_got (GObject *source,
+                   GAsyncResult *res,
+                   gpointer user_data)
+{
+        GConfPKCallbackData *data = user_data;
+        GDBusConnection *bus;
+        GError *error = NULL;
+
+        bus = g_bus_get_finish (res, &error);
+
+        if (error != NULL) {
+                if (data->callback)
+                        data->callback (data->data, error);
+
+                return;
+        }
+
+        g_dbus_proxy_new (bus,
+                          G_DBUS_PROXY_FLAGS_NONE,
+                          NULL,
+                          "org.gnome.GConf.Defaults",
+                          "/",
+                          "org.gnome.GConf.Defaults",
+                          NULL,
+                          on_gconf_proxy_created,
+                          data);
+}
+
+static void
+set_key_async (GConfPKCallbackData *data)
+{
+        g_bus_get (G_BUS_TYPE_SYSTEM,
+                   NULL, on_system_bus_got, data);
 }
 
 void
@@ -283,7 +282,6 @@ gconf_pk_set_default_async (const gchar    *key,
                 return;
 
         data = g_slice_new0 (GConfPKCallbackData);
-        data->ref_count = 1;
         data->mandatory = FALSE;
         data->key = g_strdup (key);
         data->callback = callback;
@@ -291,7 +289,6 @@ gconf_pk_set_default_async (const gchar    *key,
         data->notify = notify;
 
         set_key_async (data);
-        _gconf_pk_data_unref (data);
 }
 
 void
@@ -306,7 +303,6 @@ gconf_pk_set_mandatory_async (const gchar    *key,
                 return;
 
         data = g_slice_new0 (GConfPKCallbackData);
-        data->ref_count = 1;
         data->mandatory = TRUE;
         data->key = g_strdup (key);
         data->callback = callback;
@@ -314,5 +310,4 @@ gconf_pk_set_mandatory_async (const gchar    *key,
         data->notify = notify;
 
         set_key_async (data);
-        _gconf_pk_data_unref (data);
 }



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