[Kazehakase-cvs] kazehakase-svn [2952] * src/kz-module.{c, h}, src/kz-module-impl.h: added generic module

Back to archive index

svnno****@sourc***** svnno****@sourc*****
Thu Mar 22 14:57:26 JST 2007


Revision: 2952
          http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=kazehakase&view=rev&rev=2952
Author:   kous
Date:     2007-03-22 14:57:26 +0900 (Thu, 22 Mar 2007)

Log Message:
-----------
* src/kz-module.{c,h}, src/kz-module-impl.h: added generic module
  interface.
* src/kz-embed.{c,h}, module/embed/gecko/kz-gecko-embed.cpp:
  supported KzModule.
* module/embed/gecko/Makefile.am: removed lib prefix.
* src/Makefile.am: removed kz-embed-impl.h and added kz-module*.[ch].
* src/main.c: called kz_embed_exit().

Modified Paths:
--------------
    kazehakase/trunk/ChangeLog
    kazehakase/trunk/module/embed/gecko/Makefile.am
    kazehakase/trunk/module/embed/gecko/kz-gecko-embed.cpp
    kazehakase/trunk/src/Makefile.am
    kazehakase/trunk/src/kz-embed.c
    kazehakase/trunk/src/kz-embed.h
    kazehakase/trunk/src/main.c

Added Paths:
-----------
    kazehakase/trunk/src/kz-module-impl.h
    kazehakase/trunk/src/kz-module.c
    kazehakase/trunk/src/kz-module.h

Removed Paths:
-------------
    kazehakase/trunk/src/kz-embed-module.c

Modified: kazehakase/trunk/ChangeLog
===================================================================
--- kazehakase/trunk/ChangeLog	2007-03-22 05:41:09 UTC (rev 2951)
+++ kazehakase/trunk/ChangeLog	2007-03-22 05:57:26 UTC (rev 2952)
@@ -1,3 +1,17 @@
+2007-03-22  Kouhei Sutou  <kou****@cozmi*****>
+
+	* src/kz-module.{c,h}, src/kz-module-impl.h: added generic module
+	interface.
+
+	* src/kz-embed.{c,h}, module/embed/gecko/kz-gecko-embed.cpp:
+	supported KzModule.
+
+	* module/embed/gecko/Makefile.am: removed lib prefix.
+
+	* src/Makefile.am: removed kz-embed-impl.h and added kz-module*.[ch].
+
+	* src/main.c: called kz_embed_exit().
+
 2007-03-22  Hiroyuki Ikezoe  <poinc****@ikezo*****>
 
 	* src/Makefile.am: Add LDFLAGS=-R$(MOZILLA_HOME). It's tentavie.

Modified: kazehakase/trunk/module/embed/gecko/Makefile.am
===================================================================
--- kazehakase/trunk/module/embed/gecko/Makefile.am	2007-03-22 05:41:09 UTC (rev 2951)
+++ kazehakase/trunk/module/embed/gecko/Makefile.am	2007-03-22 05:57:26 UTC (rev 2952)
@@ -1,6 +1,6 @@
 CLEANFILES = *~ *.bak
 
-embed_LTLIBRARIES = libgecko.la
+embed_LTLIBRARIES = gecko.la
 
 INCLUDES = \
 	$(MOZILLA_COMPONENT_CFLAGS) \
@@ -89,7 +89,7 @@
 	kz-mozthumbnailprogress.cpp kz-mozthumbnailprogress.h 
 endif
 
-libgecko_la_SOURCES = \
+gecko_la_SOURCES = \
 	kz-gecko-embed.cpp \
 	kz-gecko-single.cpp kz-gecko-single.h \
 	GtkNSSDialogs.cpp GtkNSSDialogs.h \
@@ -110,7 +110,7 @@
 	$(LIBGSTHUMBNAIL_SOURCES) \
 	MozillaPrivate.cpp MozillaPrivate.h
 
-libgecko_la_LIBADD = \
+gecko_la_LIBADD = \
 	$(MOZILLA_COMPONENT_LIBS) \
 	$(GTK_LIBS)
 
@@ -121,7 +121,7 @@
 	-export-dynamic $(no_undefined)		\
 	 $(LIBTOOL_EXPORT_OPTIONS)
 
-libgecko_la_CPPFLAGS = \
+gecko_la_CPPFLAGS = \
 	-DLOCALEDIR=\""$(localedir)"\" \
 	-DSYSCONFDIR=\""$(sysconfdir)"\" \
 	-DDATADIR=\""$(datadir)"\" \

Modified: kazehakase/trunk/module/embed/gecko/kz-gecko-embed.cpp
===================================================================
--- kazehakase/trunk/module/embed/gecko/kz-gecko-embed.cpp	2007-03-22 05:41:09 UTC (rev 2951)
+++ kazehakase/trunk/module/embed/gecko/kz-gecko-embed.cpp	2007-03-22 05:57:26 UTC (rev 2952)
@@ -18,7 +18,7 @@
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 //
 
-#include "kz-embed-module-impl.h"
+#include "kz-module-impl.h"
 
 #include "kz-embed-prefs.h"
 #include "kz-gecko-single.h"
@@ -462,22 +462,27 @@
 }
 
 G_MODULE_EXPORT void
-KZ_EMBED_MODULE_IMPL_INIT (GTypeModule *module, KzApp *kz_app)
+KZ_MODULE_IMPL_INIT (GTypeModule *module)
 {
 	gecko_single = kz_gecko_single_new(kz_app);
 	kz_gecko_embed_register_type(module);
 }
 
 G_MODULE_EXPORT void
-KZ_EMBED_MODULE_IMPL_EXIT (void)
+KZ_MODULE_IMPL_EXIT (void)
 {
 	g_object_unref(gecko_single);
 }
 
-G_MODULE_EXPORT GtkWidget *
-KZ_EMBED_MODULE_IMPL_CREATE (void)
+G_MODULE_EXPORT GObject *
+KZ_MODULE_IMPL_INSTANTIATE (const gchar *first_property,
+			    va_list      var_args)
 {
-	return kz_gecko_embed_new();
+	KzGeckoEmbed *kzembed;
+
+	kzembed = KZ_GECKO_EMBED(g_object_new(KZ_TYPE_GECKO_EMBED,
+					      first_property, var_args));
+	return G_OBJECT(kzembed);
 }
 
 GType

Modified: kazehakase/trunk/src/Makefile.am
===================================================================
--- kazehakase/trunk/src/Makefile.am	2007-03-22 05:41:09 UTC (rev 2951)
+++ kazehakase/trunk/src/Makefile.am	2007-03-22 05:57:26 UTC (rev 2952)
@@ -51,9 +51,10 @@
 libkazehakase_public_h_sources = \
 	kazehakase.h \
 	kz-app.h \
+	kz-module.h \
+	kz-module-impl.h \
 	kz-embed.h \
 	kz-embed-event.h \
-	kz-embed-module-impl.h \
 	kz-embed-prefs.h \
 	kz-gesture.h \
 	kz-tab-label.h \
@@ -93,8 +94,8 @@
 
 libkazehakase_la_SOURCES = \
 	kz-app.c \
+	kz-module.c \
 	kz-embed.c \
-	kz-embed-module.c \
 	kz-embed-event.c \
 	kz-embed-prefs.c \
 	kz-gesture.c \

Deleted: kazehakase/trunk/src/kz-embed-module.c
===================================================================
--- kazehakase/trunk/src/kz-embed-module.c	2007-03-22 05:41:09 UTC (rev 2951)
+++ kazehakase/trunk/src/kz-embed-module.c	2007-03-22 05:57:26 UTC (rev 2952)
@@ -1,186 +0,0 @@
-/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
-
-/*
- *  Copyright (C) 2007 Hiroyuki Ikezoe
- *
- *  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
- *  the Free Software Foundation; either version 2, or (at your option)
- *  any later version.
- *
- *  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 General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#include <glib/gi18n.h>
-#include <gmodule.h>
-
-#include "kazehakase.h"
-#include "glib-utils.h"
-#include "kz-embed-module-impl.h"
-
-typedef struct _KzEmbedModule KzEmbedModule;
-typedef struct _KzEmbedModuleClass KzEmbedModuleClass;
-
-struct _KzEmbedModule
-{
-	GTypeModule  object;
-	GModule *library;
-
-	gchar *path;
-
-	KzEmbedModuleInitFunc init;
-	KzEmbedModuleExitFunc exit;
-	KzEmbedModuleCreateFunc create;
-};
-
-struct _KzEmbedModuleClass
-{
-	GTypeModuleClass parent_class;
-};
-
-GType kz_embed_module_get_type (void) G_GNUC_CONST;
-
-G_DEFINE_TYPE (KzEmbedModule, kz_embed_module, G_TYPE_TYPE_MODULE)
-
-#define KZ_TYPE_EMBED_MODULE      (kz_embed_module_get_type ())
-#define KZ_EMBED_MODULE(module)   (G_TYPE_CHECK_INSTANCE_CAST ((module), KZ_TYPE_EMBED_MODULE, KzEmbedModule))
-
-static GSList *loaded_embed;
-
-static gboolean
-kz_embed_module_load (GTypeModule *module)
-{
-	KzEmbedModule *s_module = KZ_EMBED_MODULE(module);
-	gpointer initp, exitp, createp;
-
-	s_module->library = g_module_open(s_module->path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
-	if (!s_module->library)
-	{
-		g_warning(g_module_error());
-		return FALSE;
-	}
-
-	/* exact symbols from the lib */
-	if (!g_module_symbol(s_module->library,
-			     G_STRINGIFY(KZ_EMBED_MODULE_IMPL_INIT),
-			     &initp) ||
-	    !g_module_symbol(s_module->library,
-			     G_STRINGIFY(KZ_EMBED_MODULE_IMPL_EXIT),
-			     &exitp) ||
-	    !g_module_symbol(s_module->library,
-			     G_STRINGIFY(KZ_EMBED_MODULE_IMPL_CREATE),
-			     &createp))
-	{
-		g_warning(g_module_error());
-		g_module_close(s_module->library);
-
-		return FALSE;
-	}
-
-	s_module->init   = initp;
-	s_module->exit   = exitp;
-	s_module->create = createp;
-
-	s_module->init(module, kz_app);
-
-	return TRUE;
-}
-
-static void
-kz_embed_module_unload (GTypeModule *module)
-{
-	KzEmbedModule *s_module = KZ_EMBED_MODULE(module);
-
-	s_module->exit();
-
-	g_module_close(s_module->library);
-	s_module->library = NULL;
-
-	s_module->init   = NULL;
-	s_module->exit   = NULL;
-	s_module->create = NULL;
-}
-
-static void
-kz_embed_module_finalize (GObject *object)
-{
-	KzEmbedModule *module = KZ_EMBED_MODULE(object);
-
-	g_free(module->path);
-
-	G_OBJECT_CLASS(kz_embed_module_parent_class)->finalize(object);
-}
-static void
-kz_embed_module_class_init (KzEmbedModuleClass *klass)
-{
-	GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS(klass);
-	GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
-
-	module_class->load   = kz_embed_module_load;
-	module_class->unload = kz_embed_module_unload;
-
-	gobject_class->finalize = kz_embed_module_finalize;
-}
-
-static void
-kz_embed_module_init (KzEmbedModule *module)
-{
-}
-
-static GtkWidget *
-kz_embed_module_create (KzEmbedModule *module, const gchar *url)
-{
-	GtkWidget *embed = NULL;
-	if (g_type_module_use(G_TYPE_MODULE(module)))
-	{
-		embed = module->create(url);
-		g_type_module_unuse(G_TYPE_MODULE(module));
-		return embed;
-	}
-	return embed;
-}
-
-GtkWidget *
-kz_embed_new (const gchar *name, const gchar *url)
-{
-	GSList *l;
-	gchar *module_path;
-	KzEmbedModule *module;
-	GtkWidget *embed = NULL;
-
-	for (l = loaded_embed; l; l = l->next)
-	{
-		module = l->data;
-
-		if (strcmp(G_TYPE_MODULE(module)->name, name) == 0)
-			return kz_embed_module_create(module, url);
-	}
-
-	if (g_module_supported())
-	{
-		module_path = g_module_build_path(KZ_EMBED_MODULEDIR, name);
-
-		if (module_path)
-		{
-			module = g_object_new(KZ_TYPE_EMBED_MODULE, NULL);
-
-			g_type_module_set_name(G_TYPE_MODULE(module), name);
-			module->path = g_strdup(module_path);
-
-			loaded_embed = g_slist_prepend(loaded_embed, module);
-
-			embed = kz_embed_module_create(module, url);
-			g_free(module_path);
-		}
-	}
-
-	return embed;
-}
-

Modified: kazehakase/trunk/src/kz-embed.c
===================================================================
--- kazehakase/trunk/src/kz-embed.c	2007-03-22 05:41:09 UTC (rev 2951)
+++ kazehakase/trunk/src/kz-embed.c	2007-03-22 05:57:26 UTC (rev 2952)
@@ -20,8 +20,67 @@
  */
 
 #include "kz-embed.h"
+#include "kz-module.h"
 #include "kz-marshalers.h"
 
+static GList *embeds = NULL;
+
+static const gchar *
+_kz_embed_module_dir (void)
+{
+	const gchar *base_dir;
+        base_dir = g_getenv("KZ_EMBED_MODULE_DIR");
+	if (base_dir)
+		return base_dir;
+        return KZ_EMBED_MODULEDIR;
+}
+
+void
+kz_embed_load (const gchar *base_dir)
+{
+	if (!base_dir)
+		base_dir = _kz_embed_module_dir();
+
+	embeds = g_list_concat(kz_module_load_modules(base_dir), embeds);
+}
+
+void
+kz_embed_unload (void)
+{
+	g_list_foreach(embeds, (GFunc)kz_module_unload, NULL);
+	g_list_free(embeds);
+	embeds = NULL;
+}
+
+void
+kz_embed_exit (void)
+{
+	g_list_foreach(embeds, (GFunc)kz_module_exit, NULL);
+}
+
+static GtkWidget *
+_kz_embed_new (const gchar *name, const gchar *first_property, ...)
+{
+	GObject *embed;
+	va_list var_args;
+
+	va_start(var_args, first_property);
+	embed = kz_module_instantiate(embeds, name, first_property, var_args);
+	va_end(var_args);
+
+	return GTK_WIDGET(embed);
+}
+
+GtkWidget *
+kz_embed_new (const gchar *name, const gchar *url)
+{
+	if (!kz_module_load_module(&embeds, _kz_embed_module_dir(), name))
+		return NULL;
+
+	return _kz_embed_new(name, NULL);
+}
+
+
 enum {
 	LINK_MESSAGE_SIGNAL,
 	JS_STATUS_SIGNAL,

Modified: kazehakase/trunk/src/kz-embed.h
===================================================================
--- kazehakase/trunk/src/kz-embed.h	2007-03-22 05:41:09 UTC (rev 2951)
+++ kazehakase/trunk/src/kz-embed.h	2007-03-22 05:57:26 UTC (rev 2952)
@@ -295,6 +295,10 @@
 
 GType		kz_embed_get_type		(void) G_GNUC_CONST;
 
+void            kz_embed_load			(const gchar   *base_dir);
+void            kz_embed_unload			(void);
+void            kz_embed_exit			(void);
+
 GtkWidget      *kz_embed_new			(const gchar   *name,
 						 const gchar   *url);
 void		kz_embed_load_url		(KzEmbed       *kzembed,

Added: kazehakase/trunk/src/kz-module-impl.h
===================================================================
--- kazehakase/trunk/src/kz-module-impl.h	2007-03-22 05:41:09 UTC (rev 2951)
+++ kazehakase/trunk/src/kz-module-impl.h	2007-03-22 05:57:26 UTC (rev 2952)
@@ -0,0 +1,48 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/*
+ *  Copyright (C) 2007 Kouhei Sutou <kou****@cozmi*****>
+ *
+ *  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
+ *  the Free Software Foundation; either version 2, or (at your option)
+ *  any later version.
+ *
+ *  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 General Public License for more details.
+ *
+ *  You should have received a copyED of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __KZ_MODULE_IMPL_H__
+#define __KZ_MODULE_IMPL_H__
+
+#include <glib-object.h>
+#include "kz-app.h"
+
+G_BEGIN_DECLS
+
+#include "kz-module.h"
+
+typedef void     (*KzModuleInitFunc)         (GTypeModule *module);
+typedef void     (*KzModuleExitFunc)         (void);
+typedef GObject *(*KzModuleInstantiateFunc)  (const gchar *first_property,
+					      va_list      var_args);
+
+#define KZ_MODULE_IMPL_INIT           kz_module_impl_init
+#define KZ_MODULE_IMPL_EXIT           kz_module_impl_exit
+#define KZ_MODULE_IMPL_INSTANTIATE    kz_module_impl_instantiate
+
+
+void     KZ_MODULE_IMPL_INIT           (GTypeModule  *module);
+void     KZ_MODULE_IMPL_EXIT           (void);
+GObject *KZ_MODULE_IMPL_INSTANTIATE    (const gchar *first_property,
+					va_list      var_args);
+
+G_END_DECLS
+
+#endif /* __KZ_MODULE_IMPL_H__ */

Added: kazehakase/trunk/src/kz-module.c
===================================================================
--- kazehakase/trunk/src/kz-module.c	2007-03-22 05:41:09 UTC (rev 2951)
+++ kazehakase/trunk/src/kz-module.c	2007-03-22 05:57:26 UTC (rev 2952)
@@ -0,0 +1,366 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/*
+ *  Copyright (C) 2007 Kouhei Sutou <kou****@cozmi*****>
+ *
+ *  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
+ *  the Free Software Foundation; either version 2, or (at your option)
+ *  any later version.
+ *
+ *  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 General Public License for more details.
+ *
+ *  You should have received a copyED of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif /* HAVE_CONFIG_H */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <gmodule.h>
+
+#include "kz-module.h"
+#include "kz-module-impl.h"
+
+#define KZ_MODULE_GET_PRIVATE(obj)					\
+	(G_TYPE_INSTANCE_GET_PRIVATE ((obj), KZ_TYPE_MODULE, KzModulePrivate))
+
+typedef struct _KzModulePrivate	KzModulePrivate;
+struct _KzModulePrivate
+{
+	GModule      *library;
+	gchar        *mod_path;
+	GList        *registered_types;
+	gboolean      exited;
+
+	KzModuleInitFunc         init;
+	KzModuleExitFunc         exit;
+	KzModuleInstantiateFunc  instantiate;
+};
+
+G_DEFINE_TYPE (KzModule, kz_module, G_TYPE_TYPE_MODULE)
+
+static void     finalize        (GObject     *object);
+static gboolean load            (GTypeModule *module);
+static void     unload          (GTypeModule *module);
+
+static void     _kz_module_show_error   (GModule     *module);
+static GModule *_kz_module_open         (const gchar *mod_path);
+static void     _kz_module_close        (GModule     *module);
+static gboolean _kz_module_load_func    (GModule     *module,
+                                         const gchar *func_name,
+                                         gpointer    *symbol);
+static gboolean _kz_module_match_name   (const gchar *mod_path,
+                                         const gchar *name);
+
+static void
+kz_module_class_init (KzModuleClass *klass)
+{
+	GObjectClass *gobject_class;
+	GTypeModuleClass *type_module_class;
+
+	gobject_class = G_OBJECT_CLASS(klass);
+	gobject_class->finalize     = finalize;
+
+	type_module_class = G_TYPE_MODULE_CLASS(klass);
+	type_module_class->load     = load;
+	type_module_class->unload   = unload;
+
+	g_type_class_add_private(gobject_class, sizeof(KzModulePrivate));
+}
+
+static void
+kz_module_init (KzModule *module)
+{
+	KzModulePrivate *priv = KZ_MODULE_GET_PRIVATE(module);
+
+	priv->library          = NULL;
+	priv->mod_path         = NULL;
+	priv->registered_types = NULL;
+	priv->exited           = FALSE;
+}
+
+static void
+finalize (GObject *object)
+{
+	KzModulePrivate *priv = KZ_MODULE_GET_PRIVATE(object);
+
+	g_free(priv->mod_path);
+	priv->mod_path = NULL;
+	g_list_free(priv->registered_types);
+	priv->registered_types = NULL;
+
+	G_OBJECT_CLASS(kz_module_parent_class)->finalize(object);
+}
+
+static gboolean
+load (GTypeModule *module)
+{
+	KzModulePrivate *priv = KZ_MODULE_GET_PRIVATE(module);
+
+	priv->library = _kz_module_open(priv->mod_path);
+	if (!priv->library)
+		return FALSE;
+
+	if (!_kz_module_load_func(priv->library,
+				  G_STRINGIFY(KZ_MODULE_IMPL_INIT),
+				  (gpointer)&priv->init) ||
+	    !_kz_module_load_func(priv->library,
+				  G_STRINGIFY(KZ_MODULE_IMPL_EXIT),
+				  (gpointer)&priv->exit) ||
+	    !_kz_module_load_func(priv->library,
+				  G_STRINGIFY(KZ_MODULE_IMPL_INSTANTIATE),
+				  (gpointer)&priv->instantiate)) {
+		_kz_module_close(priv->library);
+		priv->library = NULL;
+		return FALSE;
+	}
+
+	priv->exited = FALSE;
+
+	priv->init(module);
+
+	return TRUE;
+}
+
+static void
+unload (GTypeModule *module)
+{
+	KzModulePrivate *priv = KZ_MODULE_GET_PRIVATE(module);
+
+	if (!priv->exited)
+	{
+		priv->exit();
+		priv->exited = TRUE;
+	}
+
+	_kz_module_close(priv->library);
+	priv->library  = NULL;
+
+	priv->init = NULL;
+	priv->exit = NULL;
+	priv->instantiate = NULL;
+}
+
+
+static void
+_kz_module_show_error (GModule *module)
+{
+	gchar *message;
+
+	if (!g_module_error()) return;
+	message = g_locale_to_utf8(g_module_error(), -1, NULL, NULL, NULL);
+
+	if (module) {
+		gchar *name;
+		name = g_strdup(g_module_name(module));
+		g_warning("%s: %s", name, message);
+		g_free(name);
+	} else {
+		g_warning("%s", message);
+	}
+
+	g_free(message);
+}
+
+static KzModule *
+_kz_module_find (GList *modules, const gchar *name)
+{
+	GList *node;
+
+	for (node = modules; node; node = g_list_next(node)) {
+		KzModule *module = node->data;
+		KzModulePrivate *priv;
+
+		priv = KZ_MODULE_GET_PRIVATE(module);
+		if (_kz_module_match_name(priv->mod_path, name))
+			return module;
+	}
+
+	return NULL;
+}
+
+GObject *
+kz_module_instantiate (GList *modules, const gchar *name,
+		       const gchar *first_property, va_list var_args)
+{
+	GObject *object = NULL;
+	KzModule *module;
+
+	module = _kz_module_find(modules, name);
+	if (module)
+	{
+		KzModulePrivate *priv;
+
+		priv = KZ_MODULE_GET_PRIVATE(module);
+		if (g_type_module_use(G_TYPE_MODULE(module)))
+		{
+			object = priv->instantiate(first_property, var_args);
+			g_type_module_unuse(G_TYPE_MODULE(module));
+		}
+	}
+
+	return object;
+}
+
+static GModule *
+_kz_module_open (const gchar *mod_path)
+{
+	GModule *module;
+
+	module = g_module_open(mod_path, G_MODULE_BIND_LAZY);
+	if (!module) {
+		_kz_module_show_error(NULL);
+	}
+
+	return module;
+}
+
+static void
+_kz_module_close (GModule *module)
+{
+	if (module && g_module_close(module)) {
+		_kz_module_show_error(NULL);
+	}
+}
+
+static gchar *
+_kz_module_module_file_name (const gchar *name)
+{
+	return g_strconcat(name, "." G_MODULE_SUFFIX, NULL);
+}
+
+static gboolean
+_kz_module_load_func (GModule *module, const gchar *func_name,
+		      gpointer *symbol)
+{
+	g_return_val_if_fail(module, FALSE);
+
+	if (g_module_symbol(module, func_name, symbol)) {
+		return TRUE;
+	} else {
+		_kz_module_show_error(module);
+		return FALSE;
+	}
+}
+
+static KzModule *
+_kz_module_load (const gchar *base_dir, const gchar *name)
+{
+	gchar *mod_base_name, *mod_path;
+	KzModule *module = NULL;
+
+	mod_base_name = g_build_filename(base_dir, name, NULL);
+	if (g_str_has_suffix(mod_base_name, G_MODULE_SUFFIX)) {
+		mod_path = mod_base_name;
+	} else {
+		mod_path = _kz_module_module_file_name(mod_base_name);
+		g_free(mod_base_name);
+	}
+
+	if (g_file_test(mod_path, G_FILE_TEST_EXISTS)) {
+		KzModulePrivate *priv;
+		module = g_object_new(KZ_TYPE_MODULE, NULL);
+		priv = KZ_MODULE_GET_PRIVATE(module);
+		priv->mod_path = g_strdup(mod_path);
+		g_type_module_set_name(G_TYPE_MODULE(module), priv->mod_path);
+	}
+	g_free(mod_path);
+
+	return module;
+}
+
+gboolean
+kz_module_load_module (GList **modules, const gchar *base_dir, const gchar *name)
+{
+	KzModule *module;
+
+	if (_kz_module_find(*modules, name))
+		return TRUE;
+
+	module = _kz_module_load(base_dir, name);
+	if (module)
+		*modules = g_list_prepend(*modules, module);
+
+	return module != NULL;
+}
+
+GList *
+kz_module_load_modules (const gchar *base_dir)
+{
+	GDir *dir;
+	GList *modules = NULL;
+
+	dir = g_dir_open(base_dir, 0, NULL);
+	if (dir) {
+		const gchar *entry;
+
+		while ((entry = g_dir_read_name(dir))) {
+			KzModule *module;
+			module = _kz_module_load(base_dir, entry);
+			if (module)
+				modules = g_list_prepend(modules, module);
+		}
+
+		g_dir_close(dir);
+	}
+
+	return modules;
+}
+
+static gboolean
+_kz_module_match_name (const gchar *mod_path, const gchar *name)
+{
+	gboolean matched;
+	gchar *module_base_name, *normalized_matched_name;
+
+	module_base_name = g_path_get_basename(mod_path);
+	normalized_matched_name = _kz_module_module_file_name(name);
+
+	matched = (0 == strcmp(module_base_name, normalized_matched_name));
+
+	g_free(module_base_name);
+	g_free(normalized_matched_name);
+
+	return matched;
+}
+
+void
+kz_module_exit (KzModule *module)
+{
+	KzModulePrivate *priv;
+
+	g_return_if_fail(KZ_IS_MODULE(module));
+
+	priv = KZ_MODULE_GET_PRIVATE(module);
+	if (priv->exited)
+		return;
+
+	if (g_type_module_use(G_TYPE_MODULE(module))) {
+		priv->exit();
+		priv->exited = TRUE;
+		g_type_module_unuse(G_TYPE_MODULE(module));
+	}
+}
+
+void
+kz_module_unload (KzModule *module)
+{
+	GTypeModule *type_module;
+
+	g_return_if_fail(KZ_IS_MODULE(module));
+
+	type_module = G_TYPE_MODULE(module);
+	if (type_module->type_infos || type_module->interface_infos)
+		return;
+
+	g_object_unref(module);
+}

Added: kazehakase/trunk/src/kz-module.h
===================================================================
--- kazehakase/trunk/src/kz-module.h	2007-03-22 05:41:09 UTC (rev 2951)
+++ kazehakase/trunk/src/kz-module.h	2007-03-22 05:57:26 UTC (rev 2952)
@@ -0,0 +1,66 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/*
+ *  Copyright (C) 2007 Kouhei Sutou <kou****@cozmi*****>
+ *
+ *  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
+ *  the Free Software Foundation; either version 2, or (at your option)
+ *  any later version.
+ *
+ *  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 General Public License for more details.
+ *
+ *  You should have received a copyED of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __KZ_MODULE_H__
+#define __KZ_MODULE_H__
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define KZ_TYPE_MODULE            (kz_module_get_type ())
+#define KZ_MODULE(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), KZ_TYPE_MODULE, KzModule))
+#define KZ_MODULE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), KZ_TYPE_MODULE, KzModuleClass))
+#define KZ_IS_MODULE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), KZ_TYPE_MODULE))
+#define KZ_IS_MODULE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), KZ_TYPE_MODULE))
+#define KZ_MODULE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj), KZ_TYPE_MODULE, KzModuleClass))
+
+typedef struct _KzModule KzModule;
+typedef struct _KzModuleClass KzModuleClass;
+
+struct _KzModule
+{
+    GTypeModule object;
+};
+
+struct _KzModuleClass
+{
+    GTypeModuleClass parent_class;
+};
+
+GType     kz_module_get_type		(void) G_GNUC_CONST;
+
+
+gboolean  kz_module_load_module		(GList		**modules,
+					 const gchar	 *base_dir,
+					 const gchar	 *name);
+GList    *kz_module_load_modules	(const gchar *base_dir);
+
+GObject  *kz_module_instantiate		(GList       *modules,
+					 const gchar *name,
+					 const gchar *first_property,
+					 va_list      var_args);
+
+void      kz_module_exit		(KzModule *module);
+void      kz_module_unload		(KzModule *module);
+
+G_END_DECLS
+
+#endif /* __KZ_MODULE_H__ */

Modified: kazehakase/trunk/src/main.c
===================================================================
--- kazehakase/trunk/src/main.c	2007-03-22 05:41:09 UTC (rev 2951)
+++ kazehakase/trunk/src/main.c	2007-03-22 05:57:26 UTC (rev 2952)
@@ -508,6 +508,7 @@
 	gtk_widget_destroy(dupl_check_server);
 	dupl_check_server = NULL;
 
+	kz_embed_exit();
 	kz_ext_exit();
 
 	g_object_unref(kz_app);





More information about the Kazehakase-cvs mailing list
Back to archive index