*** empty log message ***
[rhythmbox.git] / plugins / rb-module.c
bloba5000362415577ff684eff68c1c3139b292b3eb9
1 /*
2 * heavily based on code from Gedit, which is based on code from Ephiphany
4 * Copyright (C) 2003 Marco Pesenti Gritti
5 * Copyright (C) 2003, 2004 Christian Persch
6 * Copyright (C) 2005 - Paolo Maggi
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301 USA.
24 #include "config.h"
26 #include "rb-module.h"
27 #include "rb-debug.h"
29 #include <gmodule.h>
31 typedef struct _RBModuleClass RBModuleClass;
33 struct _RBModuleClass
35 GTypeModuleClass parent_class;
38 struct _RBModule
40 GTypeModule parent_instance;
42 GModule *library;
44 gchar *path;
45 gchar *name;
46 GType type;
49 typedef GType (*RBModuleRegisterFunc) (GTypeModule *);
51 static void rb_module_init (RBModule *action);
52 static void rb_module_class_init (RBModuleClass *class);
54 static GObjectClass *parent_class = NULL;
56 G_DEFINE_TYPE (RBModule, rb_module, G_TYPE_TYPE_MODULE)
58 static gboolean
59 rb_module_load (GTypeModule *gmodule)
61 RBModule *module = RB_MODULE (gmodule);
62 RBModuleRegisterFunc register_func;
64 rb_debug ("Loading %s", module->path);
66 module->library = g_module_open (module->path, 0);
68 if (module->library == NULL) {
69 g_warning (g_module_error());
71 return FALSE;
74 /* extract symbols from the lib */
75 if (!g_module_symbol (module->library, "register_rb_plugin", (void *)&register_func)) {
76 g_warning (g_module_error ());
77 g_module_close (module->library);
78 return FALSE;
81 g_assert (register_func);
83 module->type = register_func (gmodule);
84 if (module->type == 0) {
85 g_warning ("Invalid rb plugin contained by module %s", module->path);
86 return FALSE;
89 return TRUE;
92 static void
93 rb_module_unload (GTypeModule *gmodule)
95 RBModule *module = RB_MODULE (gmodule);
97 rb_debug ("Unloading %s", module->path);
99 g_module_close (module->library);
101 module->library = NULL;
102 module->type = 0;
105 const gchar *
106 rb_module_get_path (RBModule *module)
108 g_return_val_if_fail (RB_IS_MODULE (module), NULL);
110 return module->path;
113 GObject *
114 rb_module_new_object (RBModule *module)
116 GObject *obj;
117 rb_debug ("Creating object of type %s", g_type_name (module->type));
119 if (module->type == 0) {
120 return NULL;
123 obj = g_object_new (module->type,
124 "name", module->name,
125 NULL);
126 return obj;
129 static void
130 rb_module_init (RBModule *module)
132 rb_debug ("RBModule %p initialising", module);
135 static void
136 rb_module_finalize (GObject *object)
138 RBModule *module = RB_MODULE (object);
140 rb_debug ("GeditModule %p finalising", module);
142 g_free (module->path);
144 G_OBJECT_CLASS (parent_class)->finalize (object);
147 static void
148 rb_module_class_init (RBModuleClass *class)
150 GObjectClass *object_class = G_OBJECT_CLASS (class);
151 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
153 parent_class = (GObjectClass *) g_type_class_peek_parent (class);
155 object_class->finalize = rb_module_finalize;
157 module_class->load = rb_module_load;
158 module_class->unload = rb_module_unload;
161 RBModule *
162 rb_module_new (const gchar *path, const char *module)
164 RBModule *result;
166 if (path == NULL || path[0] == '\0') {
167 return NULL;
170 result = g_object_new (RB_TYPE_MODULE, NULL);
172 g_type_module_set_name (G_TYPE_MODULE (result), path);
173 result->path = g_strdup (path);
174 result->name = g_strdup (module);
176 return result;