diff libpurple/network.c @ 24243:785db7300ef2

Improve our NetworkManager support in a couple of ways: * We no longer show error minidialogs when the network is not available. * We no longer ask NetworkManager (over DBus) for the network state with every call to purple_network_is_available(). We now cache the result. * Be a little more robust when we're not sure of the connection status. Fixes #7323
author Richard Laager <rlaager@wiktel.com>
date Sun, 26 Oct 2008 00:29:27 +0000
parents 91169093449d
children a6742d9eadf3 b38cbefca6ad
line wrap: on
line diff
--- a/libpurple/network.c	Sat Oct 25 19:03:42 2008 +0000
+++ b/libpurple/network.c	Sun Oct 26 00:29:27 2008 +0000
@@ -66,6 +66,8 @@
 static DBusGConnection *nm_conn = NULL;
 static DBusGProxy *nm_proxy = NULL;
 static DBusGProxy *dbus_proxy = NULL;
+static NMState nm_state = NM_STATE_UNKNOWN;
+static gboolean have_nm_state = FALSE;
 
 #elif defined _WIN32
 static int current_network_count;
@@ -596,13 +598,15 @@
 purple_network_is_available(void)
 {
 #ifdef HAVE_NETWORKMANAGER
-	NMState state = nm_get_network_state();
-	if (state == NM_STATE_UNKNOWN)
+	if (!have_nm_state)
 	{
-		purple_debug_warning("network", "NetworkManager not active. Assuming connection exists.\n");
-		return TRUE;
+		have_nm_state = TRUE;
+		nm_state = nm_get_network_state();
+		if (nm_state == NM_STATE_UNKNOWN)
+			purple_debug_warning("network", "NetworkManager not active. Assuming connection exists.\n");
 	}
-	else if (state == NM_STATE_CONNECTED)
+
+	if (nm_state == NM_STATE_UNKNOWN || nm_state == NM_STATE_CONNECTED)
 		return TRUE;
 
 	return FALSE;
@@ -618,9 +622,12 @@
 static void
 nm_update_state(NMState state)
 {
-	static NMState prev = NM_STATE_UNKNOWN;
+	NMState prev = nm_state;
 	PurpleConnectionUiOps *ui_ops = purple_connections_get_ui_ops();
 
+	have_nm_state = TRUE;
+	nm_state = state;
+
 	purple_signal_emit(purple_network_get_handle(), "network-configuration-changed", NULL);
 
 	switch(state)
@@ -630,16 +637,14 @@
 			res_init();
 			if (ui_ops != NULL && ui_ops->network_connected != NULL)
 				ui_ops->network_connected();
-			prev = state;
 			break;
 		case NM_STATE_ASLEEP:
 		case NM_STATE_CONNECTING:
 		case NM_STATE_DISCONNECTED:
-			if (prev != NM_STATE_CONNECTED)
+			if (prev != NM_STATE_CONNECTED && prev != NM_STATE_UNKNOWN)
 				break;
 			if (ui_ops != NULL && ui_ops->network_disconnected != NULL)
 				ui_ops->network_disconnected();
-			prev = state;
 			break;
 		case NM_STATE_UNKNOWN:
 		default:
@@ -664,7 +669,6 @@
 		return NM_STATE_UNKNOWN;
 
 	if (!dbus_g_proxy_call(nm_proxy, "state", &err, G_TYPE_INVALID, G_TYPE_UINT, &state, G_TYPE_INVALID)) {
-		/* XXX: Print an error? */
 		g_error_free(err);
 		return NM_STATE_UNKNOWN;
 	}