[epiphany/gdbus: 4/6] ephy-dbus: half-port to gdbus



commit ce0f5b497534263fae0d9379257d6a37219634d6
Author: Diego Escalante Urrelo <descalante igalia com>
Date:   Sat May 7 18:37:25 2011 -0500

    ephy-dbus: half-port to gdbus

 src/ephy-dbus.c |  199 ++++++++++++++++++++++++++++++++-----------------------
 src/ephy-dbus.h |    5 +-
 2 files changed, 118 insertions(+), 86 deletions(-)
---
diff --git a/src/ephy-dbus.c b/src/ephy-dbus.c
index a00c81d..c2ee0dc 100644
--- a/src/ephy-dbus.c
+++ b/src/ephy-dbus.c
@@ -25,9 +25,9 @@
 #include "ephy-marshal.h"
 #include "ephy-debug.h"
 #include "ephy-activation.h"
-#include "ephy-dbus-server-bindings.h"
 
 #include <string.h>
+#include <gio/gio.h>
 #include <dbus/dbus-glib-bindings.h>
 
 /* dbus 0.6 API change */
@@ -51,12 +51,16 @@
 
 struct _EphyDbusPrivate
 {
-	DBusGConnection *session_bus;
-	DBusGConnection *system_bus;
+	GDBusConnection *session_bus;
+	GDBusConnection *system_bus;
 	guint session_reconnect_timeout_id;
 	guint system_reconnect_timeout_id;
 	guint is_session_service_owner : 1;
 	guint register_name : 1;
+
+	guint owner_id;
+	guint session_bus_filter_id;
+	guint system_bus_filter_id;
 };
 
 enum
@@ -77,15 +81,6 @@ static EphyDbus *ephy_dbus_instance;
 static guint signals[LAST_SIGNAL];
 GQuark ephy_dbus_error_quark;
 
-/* Filter signals form session bus */
-static DBusHandlerResult session_filter_func (DBusConnection *connection,
-				              DBusMessage *message,
-				              void *user_data);
-/* Filter signals from system bus */
-static DBusHandlerResult system_filter_func (DBusConnection *connection,
-				             DBusMessage *message,
-				             void *user_data);
-
 /* Both  connect to their respective bus */
 static gboolean ephy_dbus_connect_to_session_bus (EphyDbus*, GError**);
 static gboolean ephy_dbus_connect_to_system_bus  (EphyDbus*, GError**);
@@ -126,21 +121,21 @@ ephy_dbus_connect_to_system_bus_cb (gpointer user_data)
 	return FALSE;
 }
 
-static DBusHandlerResult
-session_filter_func (DBusConnection *connection,
-	     	     DBusMessage *message,
-	     	     void *user_data)
+static GDBusMessage*
+session_filter_func (GDBusConnection *connection,
+	     	     GDBusMessage *message,
+		     gboolean incoming,
+	     	     gpointer user_data)
 {
 	EphyDbus *ephy_dbus = EPHY_DBUS (user_data);
 	EphyDbusPrivate *priv = ephy_dbus->priv;
 
-	if (dbus_message_is_signal (message,
-				    DBUS_INTERFACE_LOCAL,
-				    "Disconnected"))
+	if (g_dbus_message_get_message_type (message) == G_DBUS_MESSAGE_TYPE_SIGNAL &&
+	    g_strcmp0 (g_dbus_message_get_member (message), "Disconnected"))
 	{
 		LOG ("EphyDbus disconnected from session bus");
 
-		dbus_g_connection_unref (priv->session_bus);
+		g_object_unref (priv->session_bus);
 		priv->session_bus = NULL;
 
 		g_signal_emit (ephy_dbus, signals[DISCONNECTED], 0, EPHY_DBUS_SESSION);
@@ -151,29 +146,31 @@ session_filter_func (DBusConnection *connection,
 				       (GSourceFunc) ephy_dbus_connect_to_session_bus_cb,
 				       ephy_dbus);
 
-		return DBUS_HANDLER_RESULT_HANDLED;
+		g_object_unref (message);
+		message = NULL;
+		return message;
 	}
 
-	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+	return message;
 }
 
-static DBusHandlerResult
-system_filter_func (DBusConnection *connection,
-	     	    DBusMessage *message,
-	     	    void *user_data)
+static GDBusMessage*
+system_filter_func (GDBusConnection *connection,
+	     	    GDBusMessage *message,
+		    gboolean incoming,
+	     	    gpointer user_data)
 {
 	EphyDbus *ephy_dbus = EPHY_DBUS (user_data);
 	EphyDbusPrivate *priv = ephy_dbus->priv;
 
 	LOG ("EphyDbus filtering message from system bus");
 
-	if (dbus_message_is_signal (message,
-				    DBUS_INTERFACE_LOCAL,
-				    "Disconnected"))
+	if (g_dbus_message_get_message_type (message) == G_DBUS_MESSAGE_TYPE_SIGNAL &&
+	    g_strcmp0 (g_dbus_message_get_member (message), "Disconnected"))
 	{
 		LOG ("EphyDbus disconnected from system bus");
 
-		dbus_g_connection_unref (priv->system_bus);
+		g_object_unref (priv->system_bus);
 		priv->system_bus = NULL;
 
 		g_signal_emit (ephy_dbus, signals[DISCONNECTED], 0, EPHY_DBUS_SYSTEM);
@@ -184,10 +181,12 @@ system_filter_func (DBusConnection *connection,
 				       (GSourceFunc) ephy_dbus_connect_to_system_bus_cb,
 				       ephy_dbus);
 
-		return DBUS_HANDLER_RESULT_HANDLED;
+		g_object_unref (message);
+		message = NULL;
+		return message;
 	}
 
-	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+	return message;
 }
 
 static gboolean
@@ -198,64 +197,77 @@ ephy_dbus_connect_to_system_bus (EphyDbus *ephy_dbus,
 
 	LOG ("EphyDbus connecting to system DBUS");
 
-	priv->system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, error);
+	priv->system_bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
 	if (priv->system_bus == NULL)
 	{
 		g_warning ("Unable to connect to system bus: %s", error ? (*error)->message : "");
 		return FALSE;
 	}
 
-	if (dbus_g_connection_get_connection (priv->system_bus) == NULL)
-	{
-		g_warning ("DBus connection is null");
-		g_set_error (error,
-			     EPHY_DBUS_ERROR_QUARK,
-			     0,
-			     "DBus connection is NULL");
-		return FALSE;
-	}
-
+#if 0
 	dbus_connection_set_exit_on_disconnect 
 		(dbus_g_connection_get_connection (priv->system_bus),
 		 FALSE);
+#endif
 
-	dbus_connection_add_filter
-		(dbus_g_connection_get_connection (priv->system_bus),
-		 system_filter_func, ephy_dbus, NULL);
+	priv->system_bus_filter_id = g_dbus_connection_add_filter
+		(priv->system_bus, system_filter_func, ephy_dbus, NULL);
 
 	g_signal_emit (ephy_dbus, signals[CONNECTED], 0, EPHY_DBUS_SYSTEM);
 
 	return TRUE;
 }
 
+static void
+bus_acquired_cb (GDBusConnection *connection,
+		 const gchar     *name,
+		 gpointer         user_data)
+{
+}
+
+static void
+name_acquired_cb (GDBusConnection *connection,
+		  const gchar     *name,
+		  gpointer         user_data)
+{
+}
+
+static void
+name_lost_cb (GDBusConnection *connection,
+	      const gchar     *name,
+	      gpointer         user_data)
+{
+}
+
 static gboolean
 ephy_dbus_connect_to_session_bus (EphyDbus *ephy_dbus,
 				  GError **error)
 {
 	EphyDbusPrivate *priv = ephy_dbus->priv;
-	DBusGProxy *proxy;
-	guint request_ret;
+	GDBusProxy *proxy;
 	
 	LOG ("EphyDbus connecting to session DBUS");
 
 	/* Init the DBus connection */
-	priv->session_bus = dbus_g_bus_get (DBUS_BUS_SESSION, error);
+	priv->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
 	if (priv->session_bus == NULL)
 	{
 		g_warning("Unable to connect to session bus: %s", error && *error ? (*error)->message : "");
 		return FALSE;
 	}
 
+#if 0
 	dbus_connection_set_exit_on_disconnect 
 		(dbus_g_connection_get_connection (priv->session_bus),
 		 FALSE);
+#endif
 
-	dbus_connection_add_filter
-		(dbus_g_connection_get_connection (priv->session_bus),
-		 session_filter_func, ephy_dbus, NULL);
+	priv->session_bus_filter_id = g_dbus_connection_add_filter
+		(priv->session_bus, session_filter_func, ephy_dbus, NULL);
 
 	if (priv->register_name == FALSE) return TRUE;
 
+#if 0
 	dbus_g_object_type_install_info (EPHY_TYPE_DBUS,
 					 &dbus_glib_ephy_activation_object_info);
 
@@ -263,23 +275,41 @@ ephy_dbus_connect_to_session_bus (EphyDbus *ephy_dbus,
 	dbus_g_connection_register_g_object (priv->session_bus,
 					     DBUS_EPHY_PATH,
 					     G_OBJECT (ephy_dbus));
+#endif
 
-	/* Register the service name, the constant here are defined in dbus-glib-bindings.h */
-	proxy = dbus_g_proxy_new_for_name (priv->session_bus,
-					   DBUS_SERVICE_DBUS,
-					   DBUS_PATH_DBUS,
-					   DBUS_INTERFACE_DBUS);
-
-	if (!org_freedesktop_DBus_request_name (proxy,
-						DBUS_EPHY_SERVICE,
-						DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT |
-						DBUS_NAME_FLAG_DO_NOT_QUEUE,
-						&request_ret, error))
-	{
-		/* We have a BIG problem! */
-		g_warning ("RequestName failed: %s\n", error ? (*error)->message : "");
-		return FALSE;
-	}
+	/* Register the service name, the constant here are defined in
+	 * dbus-glib-bindings.h */
+	proxy = g_dbus_proxy_new_for_bus_sync
+				(G_BUS_TYPE_SESSION,
+				 G_DBUS_PROXY_FLAGS_NONE,
+				 NULL,
+				 DBUS_SERVICE_DBUS,
+				 DBUS_PATH_DBUS,
+				 DBUS_INTERFACE_DBUS,
+				 NULL, NULL);
+
+	GVariant *ret;
+	ret = g_dbus_proxy_call_sync
+			  (proxy,
+			   "RequestName",
+			   g_variant_new ("(su)",
+				   	  DBUS_EPHY_SERVICE,
+					  DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT),
+			   G_DBUS_CALL_FLAGS_NONE, -1, NULL,
+			   NULL);
+
+	guint request_ret;
+	request_ret = g_variant_get_uint32 (ret);
+	g_variant_unref (ret);
+
+#if 0
+	priv->owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
+					 DBUS_EPHY_SERVICE,
+					 G_BUS_NAME_OWNER_FLAGS_NONE,
+					 bus_acquired_cb, name_acquired_cb,
+					 name_lost_cb,
+					 ephy_dbus, NULL);
+#endif
 
 	if (request_ret == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER ||
 	    request_ret == DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER)
@@ -322,19 +352,17 @@ ephy_dbus_shutdown (EphyDbus *dbus)
 
 	if (priv->session_bus)
 	{
-		dbus_connection_remove_filter
-			(dbus_g_connection_get_connection (priv->session_bus),
-			 session_filter_func, dbus);
-		dbus_g_connection_unref (priv->session_bus);
+		g_dbus_connection_remove_filter
+			(priv->session_bus, priv->session_bus_filter_id);
+		g_object_unref (priv->session_bus);
 		priv->session_bus = NULL;
 	}
 
         if (priv->system_bus)
 	{
-		dbus_connection_remove_filter
-			(dbus_g_connection_get_connection (priv->system_bus),
-			 system_filter_func, dbus);
-		dbus_g_connection_unref (priv->system_bus);
+		g_dbus_connection_remove_filter
+			(priv->system_bus, priv->system_bus_filter_id);
+		g_object_unref (priv->system_bus);
 		priv->system_bus = NULL;
 	}
 }
@@ -452,12 +480,12 @@ ephy_dbus_get_default (void)
  * Returns: the #DBusGConnection for the @kind DBUS, or %NULL
  * if a connection could not be established.
  */
-DBusGConnection *
+GDBusConnection *
 ephy_dbus_get_bus (EphyDbus *dbus,
 		   EphyDbusBus kind)
 {
 	EphyDbusPrivate *priv = dbus->priv;
-	DBusGConnection *bus = NULL;
+	GDBusConnection *bus = NULL;
 
 	g_return_val_if_fail (EPHY_IS_DBUS (dbus), NULL);
 
@@ -488,11 +516,11 @@ ephy_dbus_get_bus (EphyDbus *dbus,
 	return bus;
 }
 
-DBusGProxy *
+GDBusProxy *
 ephy_dbus_get_proxy (EphyDbus *dbus,
 		     EphyDbusBus kind)
 {
-	DBusGConnection *bus = NULL;
+	GDBusConnection *bus = NULL;
 
 	g_return_val_if_fail (EPHY_IS_DBUS (dbus), NULL);
 	
@@ -505,10 +533,13 @@ ephy_dbus_get_proxy (EphyDbus *dbus,
 		return NULL;
 	}
 
-	return dbus_g_proxy_new_for_name (bus,
-					  DBUS_EPHY_SERVICE,
-					  DBUS_EPHY_PATH,
-					  DBUS_EPHY_INTERFACE);
+	return g_dbus_proxy_new_sync (bus,
+				      G_DBUS_PROXY_FLAGS_NONE,
+				      NULL,
+				      DBUS_EPHY_SERVICE,
+				      DBUS_EPHY_PATH,
+				      DBUS_EPHY_INTERFACE,
+				      NULL, NULL);
 }
 
 /* private API */
diff --git a/src/ephy-dbus.h b/src/ephy-dbus.h
index c24544a..68b13a9 100644
--- a/src/ephy-dbus.h
+++ b/src/ephy-dbus.h
@@ -28,6 +28,7 @@
 
 /* Yes, we know that DBUS API isn't stable yet */
 #define DBUS_API_SUBJECT_TO_CHANGE
+#include <gio/gio.h>
 #include <dbus/dbus-glib.h>
 #include <dbus/dbus-glib-lowlevel.h>
 
@@ -76,10 +77,10 @@ GType		ephy_dbus_get_type	(void);
 
 EphyDbus       *ephy_dbus_get_default	(void);
 
-DBusGConnection *ephy_dbus_get_bus	(EphyDbus *dbus,
+GDBusConnection *ephy_dbus_get_bus	(EphyDbus *dbus,
 					 EphyDbusBus kind);
 
-DBusGProxy	*ephy_dbus_get_proxy	(EphyDbus *dbus,
+GDBusProxy	*ephy_dbus_get_proxy	(EphyDbus *dbus,
 					 EphyDbusBus kind);
 
 /* private */



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