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



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

    ephy-net-monitor: port to GDBus
    
    Also sporting some improvements:
    - Getting a proper initial state from NM when starting up.
    - Making sure is_active() always query NM for the current state.
    - Consider the new NM states: anything equal or better than LAN is considered
      online. Can be detailed in a further patch with proper UI.
    
    Bug #624421

 src/ephy-net-monitor.c |  300 ++++++++++++++---------------------------------
 src/ephy-net-monitor.h |    2 +-
 src/ephy-shell.c       |    4 +-
 3 files changed, 94 insertions(+), 212 deletions(-)
---
diff --git a/src/ephy-net-monitor.c b/src/ephy-net-monitor.c
index 3c970c1..5273517 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,27 +22,26 @@
 
 #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>
+#include <gio/gio.h>
 
-typedef enum 
+#define EPHY_NET_MONITOR_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_NET_MONITOR, EphyNetMonitorPrivate))
+
+typedef enum
 {
-	NETWORK_UP,
-	NETWORK_DOWN
+	NETWORK_DOWN,
+	NETWORK_UP
 } NetworkStatus;
 
-#define EPHY_NET_MONITOR_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_NET_MONITOR, EphyNetMonitorPrivate))
-
 struct _EphyNetMonitorPrivate
 {
-	DBusConnection *bus;
-	guint active : 1;
+	GDBusProxy *proxy;
+	guint notify_id;
 	NetworkStatus status;
 };
 
@@ -54,236 +54,95 @@ enum
 G_DEFINE_TYPE (EphyNetMonitor, ephy_net_monitor, G_TYPE_OBJECT)
 
 static void
-ephy_net_monitor_set_net_status (EphyNetMonitor *monitor,
-				 NetworkStatus status)
+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;
+	guint state;
 
-	LOG ("EphyNetMonitor turning Epiphany to %s mode",
-	     status != NETWORK_DOWN ? "online" : "offline");
+	if (g_strcmp0 ("StateChanged", signal_name) != 0) return;
 
-	priv->status = status;
+	g_variant_get (parameters, "(u)", &state);
 
-	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");
+	priv->status = (state == NM_STATE_CONNECTED) ? NETWORK_UP : NETWORK_DOWN;
 
-	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)
-{
-	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;
-	}
+	LOG ("EphyNetMonitor turning Epiphany to %s mode",
+	     (priv->status == NETWORK_UP) ? "online" : "offline");
 
-	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);
+	g_object_notify (G_OBJECT (monitor), "network-status");
 }
 
-/* Filters all the messages from Network Manager */
-static DBusHandlerResult
-filter_func (DBusConnection *connection,
-	     DBusMessage *message,
-	     void *user_data)
+static NetworkStatus
+get_status_from_nm (EphyNetMonitor *monitor)
 {
-	EphyNetMonitor *monitor;
-
-	g_return_val_if_fail (user_data != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+	GVariant *state;
+	NMState nm_state;
+	NetworkStatus status;
+	GError *error = NULL;
 
-	monitor = EPHY_NET_MONITOR (user_data);
+	state = g_dbus_proxy_call_sync (monitor->priv->proxy, "state",
+					NULL,
+					G_DBUS_CALL_FLAGS_NONE, -1,
+					NULL, &error);
 
-	if (dbus_message_is_signal (message,
-				    NM_DBUS_INTERFACE,
-				    "StateChange"))
+	if (error != NULL)
 	{
-		LOG ("EphyNetMonitor catches StateChange signal");
-
-		ephy_net_monitor_check_network (monitor);
-
-		return DBUS_HANDLER_RESULT_HANDLED;
+		g_warning ("Failed to get state: %s\n", error->message);
+		g_error_free (error);
+		return NETWORK_DOWN;
 	}
 
-	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-}
-
-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");
-
-	dbus = ephy_dbus_get_default ();
+	g_variant_get (state, "(u)", &nm_state);
+	g_variant_unref (state);
 
-	g_connection = ephy_dbus_get_bus (dbus, EPHY_DBUS_SYSTEM);
-	if (g_connection == NULL) return;
-	
-	priv->bus = dbus_g_connection_get_connection (g_connection);
+	/* FIXME: we can use the new states to know when there's network but no
+	 * internet, but meanwhile we'll do with anything with a minimum of
+	 * LAN. A proper UI for detailed states is needed. */
+	status = (nm_state >= NM_STATE_CONNECTED_LOCAL)
+			? NETWORK_UP : NETWORK_DOWN;
 
-	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");
-	}
+	return status;
 }
 
 static void
-ephy_net_monitor_detach_from_dbus (EphyNetMonitor *monitor)
+ephy_net_monitor_startup (EphyNetMonitor *monitor)
 {
-	EphyNetMonitorPrivate *priv = monitor->priv;
-	
-	LOG ("EphyNetMonitor is trying to detach from SYSTEM bus");
+	GDBusProxy *proxy;
+	GError *error = NULL;
 
-	if (priv->bus != NULL)
-	{
-		dbus_connection_remove_filter (priv->bus, 
-					       filter_func, 
-					       monitor);
-		priv->bus = NULL;
-	}
-}
+	LOG ("EphyNetMonitor starting up");
 
+	proxy = g_dbus_proxy_new_for_bus_sync
+				(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, &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");
+		g_signal_connect (proxy, "g-signal",
+				  G_CALLBACK (state_changed), monitor);
+		monitor->priv->proxy = proxy;
 
-		ephy_net_monitor_attach_to_dbus (monitor);
+		monitor->priv->status = get_status_from_nm (monitor);
+		g_object_notify (G_OBJECT (monitor), "network-status");
 	}
-}
 
-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");
-}
-
-static void
 ephy_net_monitor_init (EphyNetMonitor *monitor)
 {
 	EphyNetMonitorPrivate *priv;
@@ -294,6 +153,8 @@ ephy_net_monitor_init (EphyNetMonitor *monitor)
 
 	priv->status = NETWORK_UP;
 
+	priv->proxy = NULL;
+
 	ephy_net_monitor_startup (monitor);
 }
 
@@ -301,10 +162,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);
 }
@@ -320,7 +188,7 @@ ephy_net_monitor_get_property (GObject *object,
 	switch (prop_id)
 	{
 		case PROP_NETWORK_STATUS:
-			g_value_set_boolean (value, ephy_net_monitor_get_net_status (monitor));
+			g_value_set_boolean (value, ephy_net_monitor_is_active (monitor));
 			break;
 	}
 }
@@ -358,8 +226,15 @@ ephy_net_monitor_new (void)
 	return g_object_new (EPHY_TYPE_NET_MONITOR, NULL);
 }
 
+/**
+ * ephy_net_monitor_is_active:
+ * @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_is_active (EphyNetMonitor *monitor)
 {
 	EphyNetMonitorPrivate *priv;
 	gboolean managed;
@@ -370,5 +245,10 @@ 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;
+	priv->status = get_status_from_nm (monitor);
+
+	LOG ("managed: %d | priv->status == NETWORK_UP: %d",
+			managed, (priv->status == NETWORK_UP));
+
+	return (managed || (priv->status == NETWORK_UP));
 }
diff --git a/src/ephy-net-monitor.h b/src/ephy-net-monitor.h
index 9255e8f..4684c47 100644
--- a/src/ephy-net-monitor.h
+++ b/src/ephy-net-monitor.h
@@ -59,7 +59,7 @@ GType	 	ephy_net_monitor_get_type		(void);
 
 EphyNetMonitor *ephy_net_monitor_new			(void);
 
-gboolean	ephy_net_monitor_get_net_status	(EphyNetMonitor *monitor);
+gboolean	ephy_net_monitor_is_active		(EphyNetMonitor *monitor);
 
 G_END_DECLS
 
diff --git a/src/ephy-shell.c b/src/ephy-shell.c
index ef45fb4..a5da8b5 100644
--- a/src/ephy-shell.c
+++ b/src/ephy-shell.c
@@ -159,11 +159,13 @@ ephy_shell_sync_network_status (EphyNetMonitor *net_monitor,
 	EphyEmbedSingle *single;
 	gboolean net_status;
 
+	LOG ("sync-status: %d", ephy_net_monitor_is_active (net_monitor));
+
 	if (!priv->embed_single_connected) return;
 
 	single = EPHY_EMBED_SINGLE (ephy_embed_shell_get_embed_single (EPHY_EMBED_SHELL (shell)));
 
-	net_status = ephy_net_monitor_get_net_status (net_monitor);
+	net_status = ephy_net_monitor_is_active (net_monitor);
 	ephy_embed_single_set_network_status (single, net_status);
 }
 



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