[epiphany/gdbus: 2/6] ephy-net-monitor: port to GDBus



commit c1328391d9f3c0f8525e9966a97b24a65c337ac5
Author: Diego Escalante Urrelo <descalante igalia com>
Date:   Thu Jul 15 01:25:27 2010 -0500

    ephy-net-monitor: port to GDBus
    
    Bug #624421

 src/ephy-net-monitor.c |  275 ++++++++++-------------------------------------
 1 files changed, 59 insertions(+), 216 deletions(-)
---
diff --git a/src/ephy-net-monitor.c b/src/ephy-net-monitor.c
index 3c970c1..a89bf36 100644
--- a/src/ephy-net-monitor.c
+++ b/src/ephy-net-monitor.c
@@ -1,5 +1,6 @@
 /*
  *  Copyright © 2005, 2006 Jean-François Rameau
+ *  Copyright © 2010 Igalia S.L.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -21,28 +22,22 @@
 
 #include "ephy-net-monitor.h"
 
-#include "ephy-dbus.h"
 #include "ephy-debug.h"
 #include "ephy-settings.h"
 #include "ephy-prefs.h"
 
 #include <NetworkManager.h>
 
-#include <gmodule.h>
-
-typedef enum 
-{
-	NETWORK_UP,
-	NETWORK_DOWN
-} NetworkStatus;
+#include <gio/gio.h>
 
 #define EPHY_NET_MONITOR_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_NET_MONITOR, EphyNetMonitorPrivate))
 
 struct _EphyNetMonitorPrivate
 {
-	DBusConnection *bus;
+	GDBusProxy *proxy;
+	guint notify_id;
 	guint active : 1;
-	NetworkStatus status;
+	gboolean status;
 };
 
 enum
@@ -54,233 +49,65 @@ enum
 G_DEFINE_TYPE (EphyNetMonitor, ephy_net_monitor, G_TYPE_OBJECT)
 
 static void
-ephy_net_monitor_set_net_status (EphyNetMonitor *monitor,
-				 NetworkStatus status)
-{
-	EphyNetMonitorPrivate *priv = monitor->priv;
-
-	LOG ("EphyNetMonitor turning Epiphany to %s mode",
-	     status != NETWORK_DOWN ? "online" : "offline");
-
-	priv->status = status;
-
-	g_object_notify (G_OBJECT (monitor), "network-status");
-}
-
-static void 
-ephy_net_monitor_dbus_notify (DBusPendingCall *pending,
-                    	      EphyNetMonitor *monitor) 
-{
-	DBusMessage* msg = dbus_pending_call_steal_reply (pending);
-	
-	LOG ("EphyNetMonitor getting response from dbus");
-
-	if (!msg) return;
-
-	if (dbus_message_get_type(msg) == DBUS_MESSAGE_TYPE_METHOD_RETURN) 
-	{
-		dbus_uint32_t result;
-
-		if (dbus_message_get_args (msg, NULL, DBUS_TYPE_UINT32, &result,
-					  DBUS_TYPE_INVALID)) 
-		{
-			NetworkStatus net_status;
-
-			net_status = result == NM_STATE_CONNECTED ? NETWORK_UP : NETWORK_DOWN;
-
-			LOG ("EphyNetMonitor guesses the network is %s", 
-					net_status != NETWORK_DOWN ? "up" : "down");
-
-			ephy_net_monitor_set_net_status (monitor, net_status);
-		}
-		dbus_message_unref (msg);
-	}
-}
-
-/* This is the heart of Net Monitor monitor */
-/* ATM, if there is an active device, we say that network is up: that's all ! */
-static void
-ephy_net_monitor_check_network (EphyNetMonitor *monitor)
+state_changed (GDBusProxy *proxy,
+               const gchar *sender_name,
+               const gchar *signal_name,
+               GVariant *parameters,
+               gpointer user_data)
 {
+	EphyNetMonitor *monitor = EPHY_NET_MONITOR (user_data);
 	EphyNetMonitorPrivate *priv = monitor->priv;
-	DBusMessage *message;
-	DBusPendingCall* reply;
-
-	g_return_if_fail (priv != NULL && priv->bus != NULL);
-
-	LOG ("EphyNetMonitor checking network");
-
-	/* ask to Network Manager if there is at least one active device */
-	message = dbus_message_new_method_call (NM_DBUS_SERVICE, 
-						NM_DBUS_PATH, 
-						NM_DBUS_INTERFACE, 
-						"state");
-
-	if (message == NULL)
-	{
-		g_warning ("Couldn't allocate the dbus message");
-		/* fallback: let's Epiphany roll */
-		return;
-	}
-
-	if (dbus_connection_send_with_reply (priv->bus, message, &reply, -1)) 
-	{
-		dbus_pending_call_set_notify (reply,
-					      (DBusPendingCallNotifyFunction)ephy_net_monitor_dbus_notify,
-					      monitor, NULL);
-		dbus_pending_call_unref (reply);
-	}
-	
-	dbus_message_unref (message);
-}
-
-/* Filters all the messages from Network Manager */
-static DBusHandlerResult
-filter_func (DBusConnection *connection,
-	     DBusMessage *message,
-	     void *user_data)
-{
-	EphyNetMonitor *monitor;
-
-	g_return_val_if_fail (user_data != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
-
-	monitor = EPHY_NET_MONITOR (user_data);
-
-	if (dbus_message_is_signal (message,
-				    NM_DBUS_INTERFACE,
-				    "StateChange"))
-	{
-		LOG ("EphyNetMonitor catches StateChange signal");
-
-		ephy_net_monitor_check_network (monitor);
+	guint state;
 
-		return DBUS_HANDLER_RESULT_HANDLED;
-	}
-
-	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-}
+	if (g_strcmp0 ("StateChanged", signal_name) != 0) return;
 
-static void
-ephy_net_monitor_attach_to_dbus (EphyNetMonitor *monitor)
-{
-	EphyNetMonitorPrivate *priv = monitor->priv;
-	DBusError error;
-	EphyDbus *dbus;
-	DBusGConnection *g_connection;
-	
-	LOG ("EphyNetMonitor is trying to attach to SYSTEM bus");
+	g_variant_get (parameters, "(u)", &state);
 
-	dbus = ephy_dbus_get_default ();
+	priv->status = (state == NM_STATE_CONNECTED);
 
-	g_connection = ephy_dbus_get_bus (dbus, EPHY_DBUS_SYSTEM);
-	if (g_connection == NULL) return;
-	
-	priv->bus = dbus_g_connection_get_connection (g_connection);
+	LOG ("EphyNetMonitor turning Epiphany to %s mode",
+	     priv->status ? "online" : "offline");
 
-	if (priv->bus != NULL)
-	{
-		dbus_connection_add_filter (priv->bus, 
-					    filter_func, 
-					    monitor,
-					    NULL);
-		dbus_error_init (&error);
-		dbus_bus_add_match (priv->bus, 
-				    "type='signal',interface='" NM_DBUS_INTERFACE "'", 
-				    &error);
-		if (dbus_error_is_set(&error)) 
-		{
-			g_warning("EphyNetMonitor cannot register signal handler: %s: %s", 
-				  error.name, error.message);
-			dbus_error_free(&error);
-		}
-		LOG ("EphyNetMonitor attached to SYSTEM bus");
-	}
+	g_object_notify (G_OBJECT (monitor), "network-status");
 }
 
 static void
-ephy_net_monitor_detach_from_dbus (EphyNetMonitor *monitor)
+proxy_created (GObject *source,
+               GAsyncResult *result,
+               gpointer user_data)
 {
-	EphyNetMonitorPrivate *priv = monitor->priv;
-	
-	LOG ("EphyNetMonitor is trying to detach from SYSTEM bus");
-
-	if (priv->bus != NULL)
-	{
-		dbus_connection_remove_filter (priv->bus, 
-					       filter_func, 
-					       monitor);
-		priv->bus = NULL;
-	}
-}
+	EphyNetMonitor *monitor = EPHY_NET_MONITOR (user_data);
+	GError *error = NULL;
+	GDBusProxy *proxy;
 
+	proxy = g_dbus_proxy_new_for_bus_finish (result, &error);
 
-static void
-connect_to_system_bus_cb (EphyDbus *dbus,
-			  EphyDbusBus kind,
-			  EphyNetMonitor *monitor)
-{
-	if (kind == EPHY_DBUS_SYSTEM)
+	if (proxy != NULL)
 	{
-		LOG ("EphyNetMonitor connecting to SYSTEM bus");
-
-		ephy_net_monitor_attach_to_dbus (monitor);
+		g_signal_connect (proxy, "g-signal",
+				  G_CALLBACK (state_changed), monitor);
+		monitor->priv->proxy = proxy;
 	}
-}
 
-static void
-disconnect_from_system_bus_cb (EphyDbus *dbus,
-			       EphyDbusBus kind,
-			       EphyNetMonitor *monitor)
-{
-	if (kind == EPHY_DBUS_SYSTEM)
+	if (error != NULL)
 	{
-		LOG ("EphyNetMonitor disconnected from SYSTEM bus");
-
-		/* no bus anymore */
-		ephy_net_monitor_detach_from_dbus (monitor);
+		g_warning ("Failed to create proxy: %s\n", error->message);
+		g_error_free (error);
 	}
 }
 
 static void
 ephy_net_monitor_startup (EphyNetMonitor *monitor)
 {
-	EphyDbus *dbus;
-
-	dbus = ephy_dbus_get_default ();
-
 	LOG ("EphyNetMonitor starting up");
 
-	ephy_net_monitor_attach_to_dbus (monitor);
-
-	if (monitor->priv->bus != NULL)
-       	{
-		/* DBUS may disconnect us at any time. So listen carefully to it */
-		g_signal_connect (dbus, "connected",  
-				  G_CALLBACK (connect_to_system_bus_cb), monitor);
-		g_signal_connect (dbus, "disconnected",  
-				  G_CALLBACK (disconnect_from_system_bus_cb), monitor);
-
-		ephy_net_monitor_check_network (monitor);
-	}
-}
-
-static void
-ephy_net_monitor_shutdown (EphyNetMonitor *monitor)
-{
-	EphyNetMonitorPrivate *priv = monitor->priv;
-	EphyDbus *dbus;
-
-	dbus = ephy_dbus_get_default ();
-			
-	ephy_net_monitor_detach_from_dbus (monitor);
-	g_signal_handlers_disconnect_by_func
-		(dbus, G_CALLBACK (connect_to_system_bus_cb), monitor);
-	g_signal_handlers_disconnect_by_func
-		(dbus,G_CALLBACK (disconnect_from_system_bus_cb), monitor);
-		
-	priv->bus = NULL;
-
-	LOG ("EphyNetMonitor shutdown");
+	g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
+				  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
+				   G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
+				  NULL,
+				  NM_DBUS_SERVICE, NM_DBUS_PATH,
+				  NM_DBUS_INTERFACE,
+				  NULL, proxy_created, monitor);
 }
 
 static void
@@ -292,7 +119,9 @@ ephy_net_monitor_init (EphyNetMonitor *monitor)
 
 	LOG ("EphyNetMonitor initialising");
 
-	priv->status = NETWORK_UP;
+	priv->status = TRUE;
+
+	priv->proxy = NULL;
 
 	ephy_net_monitor_startup (monitor);
 }
@@ -301,10 +130,17 @@ static void
 ephy_net_monitor_dispose (GObject *object)
 {
 	EphyNetMonitor *monitor = EPHY_NET_MONITOR (object);
+	EphyNetMonitorPrivate *priv;
+
+	priv = EPHY_NET_MONITOR_GET_PRIVATE (monitor);
 
 	LOG ("EphyNetMonitor finalising");
 
-	ephy_net_monitor_shutdown (monitor);
+	if (priv->proxy != NULL)
+	{
+		g_object_unref (priv->proxy);
+		priv->proxy = NULL;
+	}
 
 	G_OBJECT_CLASS (ephy_net_monitor_parent_class)->dispose (object);
 }
@@ -358,8 +194,15 @@ ephy_net_monitor_new (void)
 	return g_object_new (EPHY_TYPE_NET_MONITOR, NULL);
 }
 
+/**
+ * ephy_net_monitor_get_net_status:
+ * @monitor: an #EphyNetMonitor instance
+ *
+ * Returns: %TRUE if NetworkManager says we are online or the "managed-network"
+ * setting is active.
+ */
 gboolean
-ephy_net_monitor_get_net_status	(EphyNetMonitor *monitor)
+ephy_net_monitor_get_net_status (EphyNetMonitor *monitor)
 {
 	EphyNetMonitorPrivate *priv;
 	gboolean managed;
@@ -370,5 +213,5 @@ ephy_net_monitor_get_net_status	(EphyNetMonitor *monitor)
 	managed = g_settings_get_boolean (EPHY_SETTINGS_MAIN,
 					  EPHY_PREFS_MANAGED_NETWORK);
 
-	return !managed || priv->status != NETWORK_DOWN;
+	return managed || priv->status;
 }



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