[evolution/express] Convert NetworkManager integration to an EShell extension.



commit 74e48f9275442d9ef0f4a88d1888e0d8c1403948
Author: Matthew Barnes <mbarnes redhat com>
Date:   Sat Mar 20 11:38:33 2010 -0400

    Convert NetworkManager integration to an EShell extension.
    
    This demonstrates how to extend EShell without having to modify and
    recompile e-shell.c.  If NetworkManager integration is enabled, the
    extension is loaded automatically when the EShell is created.
    
    The same pattern can be applied to integrate other network monitoring
    software like ConnMan or Microsoft's Wireless Zero Configuration.

 configure.ac                                       |   15 +-
 modules/Makefile.am                                |   13 +-
 modules/network-manager/Makefile.am                |   22 ++
 .../network-manager/evolution-network-manager.c    |  245 ++++++++++++++++++++
 shell/Makefile.am                                  |   12 -
 shell/e-shell-nm.c                                 |  177 --------------
 shell/e-shell.c                                    |    8 -
 7 files changed, 285 insertions(+), 207 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index b1ba739..fc9ef87 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1133,18 +1133,14 @@ AC_ARG_ENABLE([nm],
 AC_MSG_CHECKING([if NetworkManager support is enabled])
 AC_MSG_RESULT([$enable_nm])
 if test "$enable_nm" = yes; then
-	PKG_CHECK_MODULES([NM], [NetworkManager >= nm_minimum_version],
-	[NM_SUPPORT="yes"],
-	[AC_MSG_ERROR([NetworkManager not found (or version < nm_minimum_version)!
-	If you want to disable NetworkManager, please append --disable-nm to configure!])])
-	AC_DEFINE(NM_SUPPORT, 1, [network manager available])
+	PKG_CHECK_MODULES([NM], [NetworkManager >= nm_minimum_version],,
+	[AC_MSG_ERROR([NetworkManager not found (or version < nm_minimum_version).
+	If you want to disable NetworkManager, please append --disable-nm to configure.])])
 	AC_SUBST(HAVE_NM)
 	AC_SUBST(NM_CFLAGS)
-else
-	NM_SUPPORT="no"
 fi
 
-AM_CONDITIONAL([NM_SUPPORT], [test "$NM_SUPPORT" = yes])
+AM_CONDITIONAL([ENABLE_NETWORK_MANAGER], [test "$enable_nm" = yes])
 
 dnl ******************************
 dnl Camel Flags
@@ -1693,6 +1689,7 @@ modules/Makefile
 modules/addressbook/Makefile
 modules/calendar/Makefile
 modules/mail/Makefile
+modules/network-manager/Makefile
 modules/plugin-lib/Makefile
 modules/plugin-mono/Makefile
 modules/plugin-python/Makefile
@@ -1763,7 +1760,7 @@ fi
 
 echo "
 	LDAP support:		$msg_ldap
-	NetworkManager:		$NM_SUPPORT
+	NetworkManager:		$enable_nm
 	Pilot conduits:		$msg_pilot
 	Libnotify:		$HAVE_LIBNOTIFY
 	Kerberos 5:		$msg_krb5
diff --git a/modules/Makefile.am b/modules/Makefile.am
index f792a4d..0b6ccf2 100644
--- a/modules/Makefile.am
+++ b/modules/Makefile.am
@@ -6,6 +6,17 @@ if ENABLE_PYTHON
 PYTHON_DIR = plugin-python
 endif
 
-SUBDIRS = addressbook calendar mail plugin-lib $(MONO_DIR) $(PYTHON_DIR)
+if ENABLE_NETWORK_MANAGER
+NETWORK_MANAGER_DIR = network-manager
+endif
+
+SUBDIRS = \
+	addressbook \
+	calendar \
+	mail \
+	plugin-lib \
+	$(MONO_DIR) \
+	$(PYTHON_DIR) \
+	$(NETWORK_MANAGER_DIR)
 
 -include $(top_srcdir)/git.mk
diff --git a/modules/network-manager/Makefile.am b/modules/network-manager/Makefile.am
new file mode 100644
index 0000000..24cd80a
--- /dev/null
+++ b/modules/network-manager/Makefile.am
@@ -0,0 +1,22 @@
+module_LTLIBRARIES = libevolution-module-network-manager.la
+
+libevolution_module_network_manager_la_CPPFLAGS =		\
+	$(AM_CPPFLAGS)						\
+	-I$(top_srcdir)						\
+	-DG_LOG_DOMAIN=\"evolution-network-manager\"		\
+	$(GNOME_PLATFORM_CFLAGS)				\
+	$(DBUS_GLIB_CFLAGS)					\
+	$(NM_CFLAGS)
+
+libevolution_module_network_manager_la_SOURCES =		\
+	evolution-network-manager.c
+
+libevolution_module_network_manager_la_LIBADD =			\
+	$(top_builddir)/shell/libeshell.la			\
+	$(GNOME_PLATFORM_LIBS)					\
+	$(DBUS_GLIB_LIBS)
+
+libevolution_module_network_manager_la_LDFLAGS =		\
+	-module -avoid-version $(NO_UNDEFINED)
+
+-include $(top_srcdir)/git.mk
diff --git a/modules/network-manager/evolution-network-manager.c b/modules/network-manager/evolution-network-manager.c
new file mode 100644
index 0000000..7dc110f
--- /dev/null
+++ b/modules/network-manager/evolution-network-manager.c
@@ -0,0 +1,245 @@
+/*
+ * evolution-network-manager.c
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+#include <NetworkManager/NetworkManager.h>
+
+#include <shell/e-shell.h>
+#include <e-util/e-extension.h>
+
+/* Standard GObject macros */
+#define E_TYPE_NETWORK_MANAGER \
+	(e_network_manager_get_type ())
+#define E_NETWORK_MANAGER(obj) \
+	(G_TYPE_CHECK_INSTANCE_CAST \
+	((obj), E_TYPE_NETWORK_MANAGER, ENetworkManager))
+
+typedef struct _ENetworkManager ENetworkManager;
+typedef struct _ENetworkManagerClass ENetworkManagerClass;
+
+struct _ENetworkManager {
+	EExtension parent;
+	DBusConnection *connection;
+};
+
+struct _ENetworkManagerClass {
+	EExtensionClass parent_class;
+};
+
+/* Module Entry Points */
+void e_module_load (GTypeModule *type_module);
+void e_module_unload (GTypeModule *type_module);
+
+/* Forward Declarations */
+GType e_network_manager_get_type (void);
+static gboolean network_manager_connect (ENetworkManager *extension);
+
+G_DEFINE_DYNAMIC_TYPE (ENetworkManager, e_network_manager, E_TYPE_EXTENSION)
+
+static EShell *
+network_manager_get_shell (ENetworkManager *extension)
+{
+	EExtensible *extensible;
+
+	extensible = e_extension_get_extensible (E_EXTENSION (extension));
+
+	return E_SHELL (extensible);
+}
+
+static DBusHandlerResult
+network_manager_monitor (DBusConnection *connection G_GNUC_UNUSED,
+                         DBusMessage *message,
+                         gpointer user_data)
+{
+	ENetworkManager *extension = user_data;
+	EShell *shell;
+	const gchar *path;
+	guint32 state;
+	DBusError error = DBUS_ERROR_INIT;
+
+	shell = network_manager_get_shell (extension);
+
+	path = dbus_message_get_path (message);
+
+	if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected") &&
+		g_strcmp0 (path, DBUS_PATH_LOCAL) == 0) {
+		dbus_connection_unref (extension->connection);
+		extension->connection = NULL;
+
+		g_timeout_add_seconds (
+			3, (GSourceFunc) network_manager_connect, extension);
+
+		return DBUS_HANDLER_RESULT_HANDLED;
+	}
+
+	if (!dbus_message_is_signal (message, NM_DBUS_INTERFACE, "StateChanged"))
+		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+	dbus_message_get_args (
+		message, &error,
+		DBUS_TYPE_UINT32, &state,
+		DBUS_TYPE_INVALID);
+
+	if (dbus_error_is_set (&error)) {
+		g_warning ("%s", error.message);
+		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+	}
+
+	switch (state) {
+		case NM_STATE_CONNECTED:
+			e_shell_set_network_available (shell, TRUE);
+			break;
+		case NM_STATE_ASLEEP:
+		case NM_STATE_DISCONNECTED:
+			e_shell_set_network_available (shell, FALSE);
+			break;
+		default:
+			break;
+	}
+
+	return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static void
+network_manager_check_initial_state (ENetworkManager *extension)
+{
+	EShell *shell;
+	DBusMessage *message = NULL;
+	DBusMessage *response = NULL;
+	guint32 state = -1;
+	DBusError error = DBUS_ERROR_INIT;
+
+	shell = network_manager_get_shell (extension);
+
+	message = dbus_message_new_method_call (
+		NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, "state");
+
+	/* XXX Assuming this should be safe to call synchronously. */
+	response = dbus_connection_send_with_reply_and_block (
+		extension->connection, message, 100, &error);
+
+	if (response != NULL) {
+		dbus_message_get_args (
+			response, &error, DBUS_TYPE_UINT32,
+			&state, DBUS_TYPE_INVALID);
+	} else {
+		g_warning ("%s", error.message);
+		dbus_error_free (&error);
+		return;
+	}
+
+	/* Update the state only in the absence of a network connection,
+	 * otherwise let the old state prevail. */
+	if (state == NM_STATE_ASLEEP || state == NM_STATE_DISCONNECTED)
+		e_shell_set_network_available (shell, FALSE);
+
+	dbus_message_unref (message);
+	dbus_message_unref (response);
+}
+
+static gboolean
+network_manager_connect (ENetworkManager *extension)
+{
+	DBusError error = DBUS_ERROR_INIT;
+
+	/* This is a timeout callback, so the return value denotes
+	 * whether to reschedule, not whether we're successful. */
+
+	if (extension->connection != NULL)
+		return FALSE;
+
+	extension->connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
+	if (extension->connection == NULL) {
+		g_warning ("%s", error.message);
+		dbus_error_free (&error);
+		return TRUE;
+	}
+
+	dbus_connection_setup_with_g_main (extension->connection, NULL);
+	dbus_connection_set_exit_on_disconnect (extension->connection, FALSE);
+
+	if (!dbus_connection_add_filter (
+		extension->connection,
+		network_manager_monitor, extension, NULL))
+		goto fail;
+
+	network_manager_check_initial_state (extension);
+
+	dbus_bus_add_match (
+		extension->connection,
+		"type='signal',"
+		"interface='" NM_DBUS_INTERFACE "',"
+		"sender='" NM_DBUS_SERVICE "',"
+		"path='" NM_DBUS_PATH "'",
+		&error);
+	if (dbus_error_is_set (&error)) {
+		g_warning ("%s", error.message);
+		dbus_error_free (&error);
+		goto fail;
+	}
+
+	return FALSE;
+
+fail:
+	dbus_connection_unref (extension->connection);
+	extension->connection = NULL;
+
+	return TRUE;
+}
+
+static void
+network_manager_constructed (GObject *object)
+{
+	network_manager_connect (E_NETWORK_MANAGER (object));
+}
+
+static void
+e_network_manager_class_init (ENetworkManagerClass *class)
+{
+	GObjectClass *object_class;
+	EExtensionClass *extension_class;
+
+	object_class = G_OBJECT_CLASS (class);
+	object_class->constructed = network_manager_constructed;
+
+	extension_class = E_EXTENSION_CLASS (class);
+	extension_class->extensible_type = E_TYPE_SHELL;
+}
+
+static void
+e_network_manager_class_finalize (ENetworkManagerClass *class)
+{
+}
+
+static void
+e_network_manager_init (ENetworkManager *extension)
+{
+}
+
+G_MODULE_EXPORT void
+e_module_load (GTypeModule *type_module)
+{
+	e_network_manager_register_type (type_module);
+}
+
+G_MODULE_EXPORT void
+e_module_unload (GTypeModule *type_module)
+{
+}
diff --git a/shell/Makefile.am b/shell/Makefile.am
index 26a54eb..7e8ec29 100644
--- a/shell/Makefile.am
+++ b/shell/Makefile.am
@@ -2,13 +2,6 @@ if ENABLE_TEST_COMPONENT
 SUBDIRS = . test
 endif
 
-if NM_SUPPORT
-NM_CPPFLAGS = \
-	$(DBUS_GLIB_CFLAGS) \
-	$(NM_CFLAGS)
-NM_SUPPORT_FILES = e-shell-nm.c
-endif
-
 # Executable
 bin_PROGRAMS = evolution
 
@@ -61,7 +54,6 @@ libeshell_la_CPPFLAGS =						\
 	$(SHELL_CFLAGS)
 
 libeshell_la_SOURCES =				\
-	$(NM_SUPPORT_FILES)			\
 	$(eshellinclude_HEADERS)		\
 	e-shell.c				\
 	e-shell-backend.c			\
@@ -92,10 +84,6 @@ libeshell_la_LIBADD =					\
 	$(GNOME_PLATFORM_LIBS)				\
 	$(SHELL_LIBS)
 
-if NM_SUPPORT
-libeshell_la_LIBADD += $(DBUS_GLIB_LIBS)
-endif
-
 # Evolution executable
 
 if HAVE_WINDRES
diff --git a/shell/e-shell.c b/shell/e-shell.c
index f428984..7b94f37 100644
--- a/shell/e-shell.c
+++ b/shell/e-shell.c
@@ -98,10 +98,6 @@ static GDebugKey debug_keys[] = {
 static gpointer default_shell;
 static guint signals[LAST_SIGNAL];
 
-#if defined(NM_SUPPORT) && NM_SUPPORT
-gboolean e_shell_dbus_initialize (EShell *shell);
-#endif
-
 G_DEFINE_TYPE_WITH_CODE (
 	EShell, e_shell, UNIQUE_TYPE_APP,
 	G_IMPLEMENT_INTERFACE (E_TYPE_EXTENSIBLE, NULL))
@@ -1087,10 +1083,6 @@ e_shell_init (EShell *shell)
 
 	g_object_ref_sink (shell->priv->preferences_window);
 
-#if defined(NM_SUPPORT) && NM_SUPPORT
-	e_shell_dbus_initialize (shell);
-#endif
-
 	/* Add our icon directory to the theme's search path
 	 * here instead of in main() so Anjal picks it up. */
 	icon_theme = gtk_icon_theme_get_default ();



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