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 g_return_val_if_fail(plugin, 1);
68 g_return_val_if_fail(plugin->filename, 1);
69 g_return_val_if_fail(filename, 1);
70 return strcmp(filename, plugin->filename);
73 void plugin_save_list(void)
75 gchar *rcpath, *block;
77 GSList *type_cur, *plugin_cur;
80 for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) {
81 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
83 block = g_strconcat("PluginsWin32_", type_cur->data, NULL);
85 block = g_strconcat("Plugins_", type_cur->data, NULL);
87 if ((pfile = prefs_write_open(rcpath)) == NULL ||
88 (prefs_set_block_label(pfile, block) < 0)) {
89 g_warning("failed to write plugin list\n");
95 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
96 plugin = (Plugin *) plugin_cur->data;
98 if (!strcmp(plugin->type(), type_cur->data))
99 fprintf(pfile->fp, "%s\n", plugin->filename);
101 for (plugin_cur = unloaded_plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
102 plugin = (Plugin *) plugin_cur->data;
104 if (!strcmp(plugin->type(), type_cur->data))
105 fprintf(pfile->fp, "%s\n", plugin->filename);
107 fprintf(pfile->fp, "\n");
109 if (prefs_file_close(pfile) < 0)
110 g_warning("failed to write plugin list\n");
116 static gboolean plugin_is_loaded(const gchar *filename)
118 return (g_slist_find_custom(plugins, filename,
119 (GCompareFunc)list_find_by_plugin_filename) != NULL);
122 static Plugin *plugin_get_by_filename(const gchar *filename)
124 GSList *cur = plugins;
125 for(; cur; cur = cur->next) {
126 Plugin *p = (Plugin *)cur->data;
127 if (!strcmp(p->filename, filename)) {
135 * Loads a plugin dependancies
137 * Plugin dependancies are, optionnaly, listed in a file in
138 * get_plugin_dir()/$pluginname.deps.
139 * \param filename The filename of the plugin for which we have to load deps
140 * \param error The location where an error string can be stored
141 * \return 0 on success, -1 otherwise
143 static gint plugin_load_deps(const gchar *filename, gchar **error)
146 gchar *deps_file = NULL;
150 tmp = g_strdup(filename);
151 *strrchr(tmp, '.') = '\0';
152 deps_file = g_strconcat(tmp, ".deps", NULL);
155 fp = g_fopen(deps_file, "rb");
161 while (fgets(buf, sizeof(buf), fp) != NULL) {
162 Plugin *dep_plugin = NULL;
164 buf[strlen(buf)-1]='\0'; /* chop off \n */
165 path = g_strconcat(get_plugin_dir(), buf,
166 ".", G_MODULE_SUFFIX, NULL);
167 if ((dep_plugin = plugin_get_by_filename(path)) == NULL) {
168 debug_print("trying to load %s\n", path);
169 dep_plugin = plugin_load(path, error);
170 if (dep_plugin == NULL) {
176 if (!g_slist_find_custom(dep_plugin->rdeps,
177 (gpointer) filename, list_find_by_string)) {
178 debug_print("adding %s to %s rdeps\n",
180 dep_plugin->filename);
182 g_slist_append(dep_plugin->rdeps,
190 static void plugin_unload_rdeps(Plugin *plugin)
192 GSList *cur = plugin->rdeps;
193 debug_print("removing %s rdeps\n", plugin->filename);
195 gchar *file = (gchar *)cur->data;
196 Plugin *rdep_plugin = file?plugin_get_by_filename(file):NULL;
197 debug_print(" rdep %s: %p\n", file, rdep_plugin);
199 plugin_unload(rdep_plugin);
204 g_slist_free(plugin->rdeps);
205 plugin->rdeps = NULL;
208 static void plugin_remove_from_unloaded_list (const gchar *filename)
210 GSList *item = g_slist_find_custom(unloaded_plugins,
211 (gpointer) filename, (GCompareFunc)list_find_by_plugin_filename);
212 Plugin *unloaded_plugin = item ? ((Plugin *)item->data):NULL;
213 if (unloaded_plugin != NULL) {
214 debug_print("removing %s from unloaded list\n", unloaded_plugin->filename);
215 unloaded_plugins = g_slist_remove(unloaded_plugins, unloaded_plugin);
216 g_module_close(unloaded_plugin->module);
217 g_free(unloaded_plugin->filename);
218 g_free(unloaded_plugin->error);
219 g_free(unloaded_plugin);
226 * \param filename The filename of the plugin to load
227 * \param error The location where an error string can be stored
228 * \return the plugin on success, NULL otherwise
230 Plugin *plugin_load(const gchar *filename, gchar **error)
233 gint (*plugin_init) (gchar **error);
234 gpointer plugin_name, plugin_desc, plugin_version;
235 const gchar *(*plugin_type)(void);
236 const gchar *(*plugin_licence)(void);
239 g_return_val_if_fail(filename != NULL, NULL);
240 g_return_val_if_fail(error != NULL, NULL);
242 /* check duplicate plugin path name */
243 if (plugin_is_loaded(filename)) {
244 *error = g_strdup(_("Plugin already loaded"));
248 plugin_remove_from_unloaded_list(filename);
250 if (plugin_load_deps(filename, error) < 0)
252 plugin = g_new0(Plugin, 1);
253 if (plugin == NULL) {
254 *error = g_strdup(_("Failed to allocate memory for Plugin"));
258 debug_print("trying to load `%s'\n", filename);
259 plugin->module = g_module_open(filename, 0);
260 if (plugin->module == NULL) {
261 *error = g_strdup(g_module_error());
266 if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
267 !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
268 !g_module_symbol(plugin->module, "plugin_version", &plugin_version) ||
269 !g_module_symbol(plugin->module, "plugin_type", (gpointer)&plugin_type) ||
270 !g_module_symbol(plugin->module, "plugin_licence", (gpointer)&plugin_licence) ||
271 !g_module_symbol(plugin->module, "plugin_init", (gpointer)&plugin_init)) {
272 *error = g_strdup(g_module_error());
273 g_module_close(plugin->module);
278 if (strcmp(plugin_licence(), "GPL")
279 && strncmp(plugin_licence(), "GPL-compatible", strlen("GPL-compatible"))) {
280 *error = g_strdup(_("This module is not licenced under a GPL compatible licence."));
281 g_module_close(plugin->module);
286 if (!strcmp(plugin_type(), "GTK")) {
287 *error = g_strdup(_("This module is for Sylpheed-Claws GTK1."));
288 g_module_close(plugin->module);
293 plugin->name = plugin_name;
294 plugin->desc = plugin_desc;
295 plugin->version = plugin_version;
296 plugin->type = plugin_type;
297 plugin->licence = plugin_licence;
298 plugin->filename = g_strdup(filename);
299 plugin->error = NULL;
300 if ((ok = plugin_init(error)) < 0) {
302 plugin->error = g_strdup(*error);
303 unloaded_plugins = g_slist_append(unloaded_plugins, plugin);
307 plugins = g_slist_append(plugins, plugin);
309 debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
314 void plugin_unload(Plugin *plugin)
316 void (*plugin_done) (void);
318 plugin_unload_rdeps(plugin);
321 plugin_remove_from_unloaded_list(plugin->filename);
324 if (g_module_symbol(plugin->module, "plugin_done", (gpointer) &plugin_done)) {
328 g_module_close(plugin->module);
329 plugins = g_slist_remove(plugins, plugin);
330 g_free(plugin->filename);
334 void plugin_load_all(const gchar *type)
339 gchar *error, *block;
341 plugin_types = g_slist_append(plugin_types, g_strdup(type));
343 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
345 block = g_strconcat("PluginsWin32_", type, NULL);
347 block = g_strconcat("Plugins_", type, NULL);
349 if ((pfile = prefs_read_open(rcpath)) == NULL ||
350 (prefs_set_block_label(pfile, block) < 0)) {
356 while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
361 if ((buf[0] != '\0') && (plugin_load(buf, &error) == NULL)) {
362 g_warning("plugin loading error: %s\n", error);
366 prefs_file_close(pfile);
371 void plugin_unload_all(const gchar *type)
375 list = g_slist_copy(plugins);
376 list = g_slist_reverse(list);
378 for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
379 Plugin *plugin = (Plugin *) cur->data;
381 if (!strcmp(type, plugin->type()))
382 plugin_unload(plugin);
386 cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
389 plugin_types = g_slist_remove(plugin_types, cur);
393 GSList *plugin_get_list(void)
395 return g_slist_copy(plugins);
398 GSList *plugin_get_unloaded_list(void)
400 return g_slist_copy(unloaded_plugins);
403 const gchar *plugin_get_name(Plugin *plugin)
405 return plugin->name();
408 const gchar *plugin_get_desc(Plugin *plugin)
410 return plugin->desc();
413 const gchar *plugin_get_version(Plugin *plugin)
415 return plugin->version();
418 const gchar *plugin_get_error(Plugin *plugin)
420 return plugin->error;