2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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.
24 #include <glib/gi18n.h>
35 const gchar *(*name) (void);
36 const gchar *(*desc) (void);
37 const gchar *(*version) (void);
38 const gchar *(*type) (void);
39 const gchar *(*licence) (void);
44 * List of all loaded plugins
46 GSList *plugins = NULL;
47 GSList *plugin_types = NULL;
49 static gint list_find_by_string(gconstpointer data, gconstpointer str)
51 return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE;
54 static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *filename)
56 g_return_val_if_fail(plugin, 1);
57 g_return_val_if_fail(plugin->filename, 1);
58 g_return_val_if_fail(filename, 1);
59 return strcmp(filename, plugin->filename);
62 void plugin_save_list(void)
64 gchar *rcpath, *block;
66 GSList *type_cur, *plugin_cur;
69 for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) {
70 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
71 block = g_strconcat("Plugins_", type_cur->data, NULL);
72 if ((pfile = prefs_write_open(rcpath)) == NULL ||
73 (prefs_set_block_label(pfile, block) < 0)) {
74 g_warning("failed to write plugin list\n");
80 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
81 plugin = (Plugin *) plugin_cur->data;
83 if (!strcmp(plugin->type(), type_cur->data))
84 fprintf(pfile->fp, "%s\n", plugin->filename);
86 fprintf(pfile->fp, "\n");
88 if (prefs_file_close(pfile) < 0)
89 g_warning("failed to write plugin list\n");
95 static gboolean plugin_is_loaded(const gchar *filename)
97 return (g_slist_find_custom(plugins, filename,
98 (GCompareFunc)list_find_by_plugin_filename) != NULL);
101 static Plugin *plugin_get_by_filename(const gchar *filename)
103 GSList *cur = plugins;
104 for(; cur; cur = cur->next) {
105 Plugin *p = (Plugin *)cur->data;
106 if (!strcmp(p->filename, filename)) {
114 * Loads a plugin dependancies
116 * Plugin dependancies are, optionnaly, listed in a file in
117 * PLUGINDIR/$pluginname.deps.
118 * \param filename The filename of the plugin for which we have to load deps
119 * \param error The location where an error string can be stored
120 * \return 0 on success, -1 otherwise
122 static gint plugin_load_deps(const gchar *filename, gchar **error)
124 gchar *tmp = g_strdup(filename);
125 gchar *deps_file = NULL;
129 *strrchr(tmp, '.') = '\0';
130 deps_file = g_strconcat(tmp, ".deps", NULL);
133 fp = g_fopen(deps_file, "rb");
139 while (fgets(buf, sizeof(buf), fp) != NULL) {
140 Plugin *dep_plugin = NULL;
142 buf[strlen(buf)-1]='\0'; /* chop off \n */
143 path = g_strconcat(PLUGINDIR, buf,
144 ".", G_MODULE_SUFFIX, NULL);
145 if ((dep_plugin = plugin_get_by_filename(path)) == NULL) {
146 debug_print("trying to load %s\n", path);
147 dep_plugin = plugin_load(path, error);
148 if (dep_plugin == NULL) {
154 if (!g_slist_find_custom(dep_plugin->rdeps,
155 (gpointer) filename, list_find_by_string)) {
156 debug_print("adding %s to %s rdeps\n",
158 dep_plugin->filename);
160 g_slist_append(dep_plugin->rdeps,
168 static void plugin_unload_rdeps(Plugin *plugin)
170 GSList *cur = plugin->rdeps;
171 debug_print("removing %s rdeps\n", plugin->filename);
173 gchar *file = (gchar *)cur->data;
174 Plugin *rdep_plugin = file?plugin_get_by_filename(file):NULL;
175 debug_print(" rdep %s: %p\n", file, rdep_plugin);
177 plugin_unload(rdep_plugin);
182 g_slist_free(plugin->rdeps);
183 plugin->rdeps = NULL;
188 * \param filename The filename of the plugin to load
189 * \param error The location where an error string can be stored
190 * \return the plugin on success, NULL otherwise
192 Plugin *plugin_load(const gchar *filename, gchar **error)
195 gint (*plugin_init) (gchar **error);
196 gpointer plugin_name, plugin_desc, plugin_version;
197 const gchar *(*plugin_type)(void);
198 const gchar *(*plugin_licence)(void);
201 g_return_val_if_fail(filename != NULL, NULL);
202 g_return_val_if_fail(error != NULL, NULL);
204 /* check duplicate plugin path name */
205 if (plugin_is_loaded(filename)) {
206 *error = g_strdup(_("Plugin already loaded"));
210 if (plugin_load_deps(filename, error) < 0)
212 plugin = g_new0(Plugin, 1);
213 if (plugin == NULL) {
214 *error = g_strdup(_("Failed to allocate memory for Plugin"));
218 plugin->module = g_module_open(filename, 0);
219 if (plugin->module == NULL) {
220 *error = g_strdup(g_module_error());
225 if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
226 !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
227 !g_module_symbol(plugin->module, "plugin_version", &plugin_version) ||
228 !g_module_symbol(plugin->module, "plugin_type", (gpointer)&plugin_type) ||
229 !g_module_symbol(plugin->module, "plugin_licence", (gpointer)&plugin_licence) ||
230 !g_module_symbol(plugin->module, "plugin_init", (gpointer)&plugin_init)) {
231 *error = g_strdup(g_module_error());
232 g_module_close(plugin->module);
237 if (strcmp(plugin_licence(), "GPL")
238 && strncmp(plugin_licence(), "GPL-compatible", strlen("GPL-compatible"))) {
239 *error = g_strdup(_("This module is not licenced under a GPL compatible licence."));
240 g_module_close(plugin->module);
245 if (!strcmp(plugin_type(), "GTK")) {
246 *error = g_strdup(_("This module is for Sylpheed-Claws GTK1."));
247 g_module_close(plugin->module);
252 if ((ok = plugin_init(error)) < 0) {
253 g_module_close(plugin->module);
258 plugin->name = plugin_name;
259 plugin->desc = plugin_desc;
260 plugin->version = plugin_version;
261 plugin->type = plugin_type;
262 plugin->licence = plugin_licence;
263 plugin->filename = g_strdup(filename);
265 plugins = g_slist_append(plugins, plugin);
267 debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
272 void plugin_unload(Plugin *plugin)
274 void (*plugin_done) (void);
276 plugin_unload_rdeps(plugin);
278 if (g_module_symbol(plugin->module, "plugin_done", (gpointer) &plugin_done)) {
282 g_module_close(plugin->module);
283 plugins = g_slist_remove(plugins, plugin);
284 g_free(plugin->filename);
288 void plugin_load_all(const gchar *type)
293 gchar *error, *block;
295 plugin_types = g_slist_append(plugin_types, g_strdup(type));
297 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
298 block = g_strconcat("Plugins_", type, NULL);
299 if ((pfile = prefs_read_open(rcpath)) == NULL ||
300 (prefs_set_block_label(pfile, block) < 0)) {
306 while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
311 if ((buf[0] != '\0') && (plugin_load(buf, &error) == NULL)) {
312 g_warning("plugin loading error: %s\n", error);
316 prefs_file_close(pfile);
321 void plugin_unload_all(const gchar *type)
325 list = g_slist_copy(plugins);
326 list = g_slist_reverse(list);
328 for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
329 Plugin *plugin = (Plugin *) cur->data;
331 if (!strcmp(type, plugin->type()))
332 plugin_unload(plugin);
336 cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
339 g_slist_remove(plugin_types, cur);
343 GSList *plugin_get_list(void)
345 return g_slist_copy(plugins);
348 const gchar *plugin_get_name(Plugin *plugin)
350 return plugin->name();
353 const gchar *plugin_get_desc(Plugin *plugin)
355 return plugin->desc();
358 const gchar *plugin_get_version(Plugin *plugin)
360 return plugin->version();