2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <glib/gi18n.h>
40 const gchar *(*name) (void);
41 const gchar *(*desc) (void);
42 const gchar *(*version) (void);
43 const gchar *(*type) (void);
44 const gchar *(*licence) (void);
50 * List of all loaded plugins
52 GSList *plugins = NULL;
53 GSList *plugin_types = NULL;
56 * List of plugins unloaded for some fixable reason
58 static GSList *unloaded_plugins = NULL;
60 static gint list_find_by_string(gconstpointer data, gconstpointer str)
62 return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE;
65 static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *filename)
67 /* FIXME: There is a problem in case of symlinks or when a
68 user tries to load a plugin with the same name from a
69 different directory. I think it would be better to compare
70 only the basename of the filename here (case-insensitive on
72 g_return_val_if_fail(plugin, 1);
73 g_return_val_if_fail(plugin->filename, 1);
74 g_return_val_if_fail(filename, 1);
75 return strcmp(filename, plugin->filename);
78 void plugin_save_list(void)
80 gchar *rcpath, *block;
82 GSList *type_cur, *plugin_cur;
85 for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) {
86 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
88 block = g_strconcat("PluginsWin32_", type_cur->data, NULL);
90 block = g_strconcat("Plugins_", type_cur->data, NULL);
92 if ((pfile = prefs_write_open(rcpath)) == NULL ||
93 (prefs_set_block_label(pfile, block) < 0)) {
94 g_warning("failed to write plugin list\n");
100 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
101 plugin = (Plugin *) plugin_cur->data;
103 if (!strcmp(plugin->type(), type_cur->data))
104 fprintf(pfile->fp, "%s\n", plugin->filename);
106 for (plugin_cur = unloaded_plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
107 plugin = (Plugin *) plugin_cur->data;
109 if (!strcmp(plugin->type(), type_cur->data))
110 fprintf(pfile->fp, "%s\n", plugin->filename);
112 fprintf(pfile->fp, "\n");
114 if (prefs_file_close(pfile) < 0)
115 g_warning("failed to write plugin list\n");
121 static gboolean plugin_is_loaded(const gchar *filename)
123 return (g_slist_find_custom(plugins, filename,
124 (GCompareFunc)list_find_by_plugin_filename) != NULL);
127 static Plugin *plugin_get_by_filename(const gchar *filename)
129 GSList *cur = plugins;
130 for(; cur; cur = cur->next) {
131 Plugin *p = (Plugin *)cur->data;
132 if (!strcmp(p->filename, filename)) {
140 * Loads a plugin dependancies
142 * Plugin dependancies are, optionnaly, listed in a file in
143 * get_plugin_dir()/$pluginname.deps.
144 * \param filename The filename of the plugin for which we have to load deps
145 * \param error The location where an error string can be stored
146 * \return 0 on success, -1 otherwise
148 static gint plugin_load_deps(const gchar *filename, gchar **error)
151 gchar *deps_file = NULL;
156 tmp = g_strdup(filename);
157 if( (p = strrchr(tmp, '.')) )
159 deps_file = g_strconcat(tmp, ".deps", NULL);
162 fp = g_fopen(deps_file, "rb");
168 while (fgets(buf, sizeof(buf), fp) != NULL) {
169 Plugin *dep_plugin = NULL;
171 buf[strlen(buf)-1]='\0'; /* chop off \n */
172 path = g_strconcat(get_plugin_dir(), buf,
173 ".", G_MODULE_SUFFIX, NULL);
174 if ((dep_plugin = plugin_get_by_filename(path)) == NULL) {
175 debug_print("trying to load %s\n", path);
176 dep_plugin = plugin_load(path, error);
177 if (dep_plugin == NULL) {
183 if (!g_slist_find_custom(dep_plugin->rdeps,
184 (gpointer) filename, list_find_by_string)) {
185 debug_print("adding %s to %s rdeps\n",
187 dep_plugin->filename);
189 g_slist_append(dep_plugin->rdeps,
197 static void plugin_unload_rdeps(Plugin *plugin)
199 GSList *cur = plugin->rdeps;
200 debug_print("removing %s rdeps\n", plugin->filename);
202 gchar *file = (gchar *)cur->data;
203 Plugin *rdep_plugin = file?plugin_get_by_filename(file):NULL;
204 debug_print(" rdep %s: %p\n", file, rdep_plugin);
206 plugin_unload(rdep_plugin);
211 g_slist_free(plugin->rdeps);
212 plugin->rdeps = NULL;
215 static void plugin_remove_from_unloaded_list (const gchar *filename)
217 GSList *item = g_slist_find_custom(unloaded_plugins,
218 (gpointer) filename, (GCompareFunc)list_find_by_plugin_filename);
219 Plugin *unloaded_plugin = item ? ((Plugin *)item->data):NULL;
220 if (unloaded_plugin != NULL) {
221 debug_print("removing %s from unloaded list\n", unloaded_plugin->filename);
222 unloaded_plugins = g_slist_remove(unloaded_plugins, unloaded_plugin);
223 g_module_close(unloaded_plugin->module);
224 g_free(unloaded_plugin->filename);
225 g_free(unloaded_plugin->error);
226 g_free(unloaded_plugin);
233 * \param filename The filename of the plugin to load
234 * \param error The location where an error string can be stored
235 * \return the plugin on success, NULL otherwise
237 Plugin *plugin_load(const gchar *filename, gchar **error)
240 gint (*plugin_init) (gchar **error);
241 gpointer plugin_name, plugin_desc, plugin_version;
242 const gchar *(*plugin_type)(void);
243 const gchar *(*plugin_licence)(void);
246 g_return_val_if_fail(filename != NULL, NULL);
247 g_return_val_if_fail(error != NULL, NULL);
249 /* check duplicate plugin path name */
250 if (plugin_is_loaded(filename)) {
251 *error = g_strdup(_("Plugin already loaded"));
255 plugin_remove_from_unloaded_list(filename);
257 if (plugin_load_deps(filename, error) < 0)
259 plugin = g_new0(Plugin, 1);
260 if (plugin == NULL) {
261 *error = g_strdup(_("Failed to allocate memory for Plugin"));
265 debug_print("trying to load `%s'\n", filename);
266 plugin->module = g_module_open(filename, 0);
267 if (plugin->module == NULL) {
268 *error = g_strdup(g_module_error());
273 if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
274 !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
275 !g_module_symbol(plugin->module, "plugin_version", &plugin_version) ||
276 !g_module_symbol(plugin->module, "plugin_type", (gpointer)&plugin_type) ||
277 !g_module_symbol(plugin->module, "plugin_licence", (gpointer)&plugin_licence) ||
278 !g_module_symbol(plugin->module, "plugin_init", (gpointer)&plugin_init)) {
279 *error = g_strdup(g_module_error());
280 g_module_close(plugin->module);
285 if (strcmp(plugin_licence(), "GPL")
286 && strncmp(plugin_licence(), "GPL-compatible", strlen("GPL-compatible"))) {
287 *error = g_strdup(_("This module is not licenced under a GPL compatible licence."));
288 g_module_close(plugin->module);
293 if (!strcmp(plugin_type(), "GTK")) {
294 *error = g_strdup(_("This module is for Sylpheed-Claws GTK1."));
295 g_module_close(plugin->module);
300 plugin->name = plugin_name;
301 plugin->desc = plugin_desc;
302 plugin->version = plugin_version;
303 plugin->type = plugin_type;
304 plugin->licence = plugin_licence;
305 plugin->filename = g_strdup(filename);
306 plugin->error = NULL;
307 if ((ok = plugin_init(error)) < 0) {
309 plugin->error = g_strdup(*error);
310 unloaded_plugins = g_slist_append(unloaded_plugins, plugin);
314 plugins = g_slist_append(plugins, plugin);
316 debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
321 void plugin_unload(Plugin *plugin)
323 void (*plugin_done) (void);
325 plugin_unload_rdeps(plugin);
328 plugin_remove_from_unloaded_list(plugin->filename);
331 if (g_module_symbol(plugin->module, "plugin_done", (gpointer) &plugin_done)) {
335 g_module_close(plugin->module);
336 plugins = g_slist_remove(plugins, plugin);
337 g_free(plugin->filename);
341 void plugin_load_all(const gchar *type)
346 gchar *error = NULL, *block;
348 plugin_types = g_slist_append(plugin_types, g_strdup(type));
350 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
352 block = g_strconcat("PluginsWin32_", type, NULL);
354 block = g_strconcat("Plugins_", type, NULL);
356 if ((pfile = prefs_read_open(rcpath)) == NULL ||
357 (prefs_set_block_label(pfile, block) < 0)) {
363 while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
368 if ((buf[0] != '\0') && (plugin_load(buf, &error) == NULL)) {
369 g_warning("plugin loading error: %s\n", error);
373 prefs_file_close(pfile);
378 void plugin_unload_all(const gchar *type)
382 list = g_slist_copy(plugins);
383 list = g_slist_reverse(list);
385 for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
386 Plugin *plugin = (Plugin *) cur->data;
388 if (!strcmp(type, plugin->type()))
389 plugin_unload(plugin);
393 cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
396 plugin_types = g_slist_remove(plugin_types, cur);
401 /* Load those plugins we always want to use. No error output; just
403 void plugin_load_standard_plugins (void)
405 static const char *names[] = {
413 gchar *error, *filename;
415 for (i=0; names[i]; i++) {
416 /* Simple hack to check whether the plugin has already
417 * been loaded but checking only for the basename. */
418 GSList *cur = plugins;
419 for(; cur; cur = cur->next) {
420 Plugin *p = (Plugin *)cur->data;
421 if (strstr(p->filename, names[i]))
424 if (!cur) { /* Not yet loaded. */
425 /* FIXME: get_plugin_dir () returns with a trailing
426 * (back)slash; this should be fixed so that we can use
427 * g_module_build_path here. */
428 filename = g_strconcat (get_plugin_dir(),
431 plugin_load(filename, &error);
439 GSList *plugin_get_list(void)
441 return g_slist_copy(plugins);
444 GSList *plugin_get_unloaded_list(void)
446 return g_slist_copy(unloaded_plugins);
449 const gchar *plugin_get_name(Plugin *plugin)
451 return plugin->name();
454 const gchar *plugin_get_desc(Plugin *plugin)
456 return plugin->desc();
459 const gchar *plugin_get_version(Plugin *plugin)
461 return plugin->version();
464 const gchar *plugin_get_error(Plugin *plugin)
466 return plugin->error;