[gnome-bluetooth] Add the start of a plugin system



commit 2eb44814bc7992595d667ea81c3bcc6e79dd4cea
Author: Bastien Nocera <hadess hadess net>
Date:   Thu May 28 15:49:09 2009 +0100

    Add the start of a plugin system
    
    This would be used in the wizard and the preferences to
    set up additional services with some device types.
---
 common/Makefile.am                |   18 ++++---
 common/bluetooth-plugin-manager.c |  100 +++++++++++++++++++++++++++++++++++++
 common/bluetooth-plugin-manager.h |   34 +++++++++++++
 common/bluetooth-plugin.h         |   35 +++++++++++++
 common/plugins/Makefile.am        |   17 ++++++
 common/plugins/test.c             |   50 ++++++++++++++++++
 configure.ac                      |    6 ++
 7 files changed, 253 insertions(+), 7 deletions(-)

diff --git a/common/Makefile.am b/common/Makefile.am
index dc11b75..1f82ed3 100644
--- a/common/Makefile.am
+++ b/common/Makefile.am
@@ -1,12 +1,15 @@
+SUBDIRS = plugins
 
 noinst_LTLIBRARIES = libcommon.la
 lib_LTLIBRARIES = libgnome-bluetooth.la
 
-libcommon_la_SOURCES = \
-		bluetooth-client.h bluetooth-client.c \
-		bluetooth-agent.h bluetooth-agent.c \
-		bluetooth-killswitch.h bluetooth-killswitch.c \
-		obex-agent.h obex-agent.c \
+libcommon_la_SOURCES =						\
+		bluetooth-client.h bluetooth-client.c		\
+		bluetooth-agent.h bluetooth-agent.c		\
+		bluetooth-killswitch.h bluetooth-killswitch.c	\
+		obex-agent.h obex-agent.c			\
+		bluetooth-plugin-manager.c			\
+		bluetooth-plugin-manager.h			\
 		bluetooth-enums.h
 libcommon_la_LIBADD = $(COMMON_LIBS)
 libcommon_la_LDFLAGS = -no-undefined $(AM_LDFLAGS)
@@ -32,9 +35,10 @@ gnomebluetoothdir = $(pkgincludedir)
 gnomebluetooth_HEADERS =				\
 	bluetooth-chooser.h				\
 	bluetooth-chooser-button.h			\
-	bluetooth-enums.h
+	bluetooth-enums.h				\
+	bluetooth-plugin.h
 
-AM_CFLAGS = -I$(srcdir) $(LIBGNOMEBT_CFLAGS) $(COMMON_CFLAGS) $(WARN_CFLAGS) $(DISABLE_DEPRECATED)
+AM_CFLAGS = -I$(srcdir) $(LIBGNOMEBT_CFLAGS) $(COMMON_CFLAGS) $(WARN_CFLAGS) $(DISABLE_DEPRECATED) -DPLUGINDIR=\"$(libdir)/gnome-bluetooth/plugins\"
 
 BUILT_SOURCES = marshal.h marshal.c \
 		bluetooth-client-glue.h \
diff --git a/common/bluetooth-plugin-manager.c b/common/bluetooth-plugin-manager.c
new file mode 100644
index 0000000..765e420
--- /dev/null
+++ b/common/bluetooth-plugin-manager.c
@@ -0,0 +1,100 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2009  Bastien Nocera <hadess hadess net>
+ *
+ *
+ *  This library 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.1 of the License, or (at your option) any later version.
+ *
+ *  This library 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 this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <gtk/gtk.h>
+#include <bluetooth-plugin.h>
+#include "bluetooth-plugin-manager.h"
+
+#define UNINSTALLED_PLUGINDIR "../common/plugins"
+
+GList *plugin_list = NULL;
+
+static void
+bluetooth_plugin_dir_process (const char *plugindir)
+{
+	GDir *dir;
+	const char *item;
+	GbtPlugin *p = NULL;
+	GError *err = NULL;
+
+	dir = g_dir_open (plugindir, 0, &err);
+
+	if (dir == NULL) {
+		g_warning ("Can't open the plugins dir: %s", err ? err->message : "No reason");
+		if (err)
+			g_error_free (err);
+	} else {
+		while ((item = g_dir_read_name(dir))) {
+			if (g_str_has_suffix (item, SOEXT)) {
+				char *module_path;
+
+				p = g_new0(GbtPlugin, 1); 
+				module_path = g_module_build_path (plugindir, item);
+				p->module = g_module_open (module_path, G_MODULE_BIND_LAZY);
+				if (!p->module) {
+					g_warning ("error opening %s: %s", module_path, g_module_error ());
+					g_free (module_path);
+					continue;
+				}
+				g_free (module_path);
+
+				plugin_list = g_list_append (plugin_list, p); 
+			}
+		}
+		g_dir_close (dir);
+	}
+}
+
+gboolean
+bluetooth_plugin_manager_init (void)
+{
+	if (g_file_test (UNINSTALLED_PLUGINDIR, G_FILE_TEST_IS_DIR) != FALSE) {
+		/* Try to load the local plugins */
+		bluetooth_plugin_dir_process ("../common/plugins/.libs/");
+	}
+
+	if (g_list_length (plugin_list) == 0)
+		bluetooth_plugin_dir_process (PLUGINDIR);
+
+	return g_list_length (plugin_list) != 0;
+}
+
+void
+bluetooth_plugin_manager_cleanup (void)
+{
+	GList *l;
+
+	for (l = plugin_list; l != NULL; l = l->next) {
+		GbtPlugin *p = l->data;
+
+		g_module_close (p->module);
+		g_free (p);
+	}
+	g_list_free (plugin_list);
+	plugin_list = NULL;
+}
+
diff --git a/common/bluetooth-plugin-manager.h b/common/bluetooth-plugin-manager.h
new file mode 100644
index 0000000..2c02c66
--- /dev/null
+++ b/common/bluetooth-plugin-manager.h
@@ -0,0 +1,34 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2009  Bastien Nocera <hadess hadess net>
+ *
+ *
+ *  This library 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.1 of the License, or (at your option) any later version.
+ *
+ *  This library 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 this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __BLUETOOTH_PLUGIN_MANAGER_H
+#define __BLUETOOTH_PLUGIN_MANAGER_H
+
+G_BEGIN_DECLS
+
+gboolean bluetooth_plugin_manager_init (void);
+void bluetooth_plugin_manager_cleanup (void);
+
+G_END_DECLS
+
+#endif /* __BLUETOOTH_PLUGIN_MANAGER_H */
diff --git a/common/bluetooth-plugin.h b/common/bluetooth-plugin.h
new file mode 100644
index 0000000..6028ccf
--- /dev/null
+++ b/common/bluetooth-plugin.h
@@ -0,0 +1,35 @@
+#ifndef _GNOME_BLUETOOTH_PLUGIN_H_
+#define _GNOME_BLUETOOTH_PLUGIN_H_
+
+#include <gmodule.h>
+#include <gtk/gtk.h>
+
+typedef struct _GbtPluginInfo GbtPluginInfo;
+typedef struct _GbtPlugin GbtPlugin;
+
+struct _GbtPluginInfo 
+{
+	const char *id;
+	gboolean (* has_config_widget) (const char *bdaddr, const char **uuids);
+	GtkWidget * (* get_config_widgets) (const char *bdaddr, const char **uuids);
+};
+
+struct _GbtPlugin
+{
+	GModule *module;
+	GbtPluginInfo *info;
+};
+
+#define GBT_INIT_PLUGIN(plugininfo)					\
+	gboolean gbt_init_plugin (GbtPlugin *plugin);			\
+	G_MODULE_EXPORT gboolean					\
+	gbt_init_plugin (GbtPlugin *plugin) {				\
+		plugin->info = &(plugininfo);				\
+		return TRUE;						\
+	}
+
+#define SOEXT           ("." G_MODULE_SUFFIX)
+#define SOEXT_LEN       (strlen (SOEXT))
+
+#endif /* _GNOME_BLUETOOTH_PLUGIN_H_ */
+
diff --git a/common/plugins/Makefile.am b/common/plugins/Makefile.am
new file mode 100644
index 0000000..4a62d43
--- /dev/null
+++ b/common/plugins/Makefile.am
@@ -0,0 +1,17 @@
+plugindir = $(libdir)/gnome-bluetooth/plugins
+
+INCLUDES =                                      \
+        -DDATADIR=\"$(datadir)\"                \
+        -DICONDIR=\"$(icondir)\"                \
+	-DLOCALEDIR="\"$(datadir)/locale\""	\
+        -I$(top_srcdir)/common			\
+        -I$(top_builddir)			\
+	$(PLUGINS_CFLAGS)			\
+	$(WARN_CFLAGS)
+
+plugin_LTLIBRARIES = libgbttest.la
+
+libgbttest_la_SOURCES = test.c
+libgbttest_la_LDFLAGS = -module -avoid-version
+libgbttest_la_LIBADD = $(PLUGINS_LIBS)
+
diff --git a/common/plugins/test.c b/common/plugins/test.c
new file mode 100644
index 0000000..a71235d
--- /dev/null
+++ b/common/plugins/test.c
@@ -0,0 +1,50 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2009  Bastien Nocera <hadess hadess net>
+ *
+ *
+ *  This library 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.1 of the License, or (at your option) any later version.
+ *
+ *  This library 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 this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <gtk/gtk.h>
+#include <bluetooth-plugin.h>
+
+static gboolean
+has_config_widget (const char *bdaddr, const char **uuids)
+{
+	return FALSE;
+}
+
+static GtkWidget *
+get_config_widgets (const char *bdaddr, const char **uuids)
+{
+	return NULL;
+}
+
+static GbtPluginInfo plugin_info = {
+	"test-plugin",
+	has_config_widget,
+	get_config_widgets
+};
+
+GBT_INIT_PLUGIN(plugin_info)
+
diff --git a/configure.ac b/configure.ac
index 286df1c..ad63df9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -106,9 +106,14 @@ PKG_CHECK_MODULES(LIBGNOMEBT,
 dnl Requires for the private library
 PKG_CHECK_MODULES(COMMON,
 		  dbus-glib-1 >= $DBUS_GLIB_REQUIRED
+		  gmodule-2.0
 		  gtk+-2.0
 		  hal >= $HAL_REQUIRED)
 
+dnl Requires for the plugins
+PKG_CHECK_MODULES(PLUGINS,
+		  gtk+-2.0)
+
 DBUS_BINDING_TOOL="dbus-binding-tool"
 AC_SUBST(DBUS_BINDING_TOOL)
 
@@ -127,6 +132,7 @@ AC_OUTPUT(Makefile
 	  gnome-bluetooth-1.0.pc
 	  icons/Makefile
 	  common/Makefile
+	  common/plugins/Makefile
 	  applet/Makefile
 	  applet/bluetooth-applet.desktop.in
 	  properties/Makefile



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