1 /* Copyright (c) 2008, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "modest-module.h"
39 G_DEFINE_TYPE (ModestModule, modest_module, G_TYPE_TYPE_MODULE);
42 static gboolean modest_module_load (GTypeModule *gmodule);
43 static void modest_module_unload (GTypeModule *gmodule);
44 static void modest_module_init (ModestModule *module);
45 static void modest_module_finalize (GObject *object);
46 static void modest_module_class_init (ModestModuleClass *class);
48 typedef GType (*ModestModuleRegisterFunc) (GTypeModule *);
50 typedef struct _ModestModulePrivate ModestModulePrivate;
51 struct _ModestModulePrivate {
57 #define MODEST_MODULE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
62 modest_module_load (GTypeModule *g_type_module)
64 ModestModule *module = MODEST_MODULE (g_type_module);
65 ModestModulePrivate *priv = MODEST_MODULE_GET_PRIVATE (module);
66 ModestModuleRegisterFunc register_func;
68 /* We don't do lazy linking to fail just if we cannot link all the symbols available */
69 priv->g_module = g_module_open (priv->path, 0);
71 if (priv->g_module == NULL) {
72 g_warning ("%s", g_module_error());
76 /* Get the register function */
77 if (!g_module_symbol (priv->g_module, "register_modest_plugin", (void *) ®ister_func)) {
78 g_warning ("%s", g_module_error());
79 g_module_close (priv->g_module);
84 if (register_func == NULL) {
85 g_warning ("register_modest_plugin shouldn't be NULL");
86 g_module_close (priv->g_module);
91 /* call the register function to initialize the module */
92 priv->type = register_func (g_type_module);
93 if (priv->type == 0) {
94 g_warning ("%s is not a modest plugin", priv->path);
102 modest_module_unload (GTypeModule *gmodule)
104 ModestModule *module = MODEST_MODULE (gmodule);
105 ModestModulePrivate *priv = MODEST_MODULE_GET_PRIVATE (module);
107 g_module_close (priv->g_module);
109 priv->g_module = NULL;
114 modest_module_new_object (ModestModule *module)
116 ModestModulePrivate *priv = MODEST_MODULE_GET_PRIVATE (module);
118 if (priv->type == 0) {
122 return g_object_new (priv->type, NULL);
126 modest_module_init (ModestModule *module)
131 modest_module_finalize (GObject *object)
133 ModestModule *module = MODEST_MODULE (object);
134 ModestModulePrivate *priv = MODEST_MODULE_GET_PRIVATE (module);
138 G_OBJECT_CLASS (modest_module_parent_class)->finalize (object);
142 modest_module_class_init (ModestModuleClass *class)
144 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
145 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
147 gobject_class->finalize = modest_module_finalize;
149 module_class->load = modest_module_load;
150 module_class->unload = modest_module_unload;
152 g_type_class_add_private (gobject_class, sizeof(ModestModulePrivate));
156 modest_module_new (const gchar *path)
158 ModestModule *module;
159 ModestModulePrivate *priv;
161 if (path == NULL || path[0] == '\0') {
165 module = g_object_new (MODEST_TYPE_MODULE, NULL);
166 priv = MODEST_MODULE_GET_PRIVATE (module);
168 g_type_module_set_name (G_TYPE_MODULE (module), path);
170 priv->path = g_strdup (path);