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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <glib/gi18n.h>
35 const gchar *(*name) (void);
36 const gchar *(*desc) (void);
37 const gchar *(*type) (void);
42 * List of all loaded plugins
44 GSList *plugins = NULL;
45 GSList *plugin_types = NULL;
47 static gint list_find_by_string(gconstpointer data, gconstpointer str)
49 return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE;
52 static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *filename)
54 g_return_val_if_fail(plugin, 1);
55 g_return_val_if_fail(plugin->filename, 1);
56 g_return_val_if_fail(filename, 1);
57 return strcmp(filename, plugin->filename);
60 void plugin_save_list(void)
62 gchar *rcpath, *block;
64 GSList *type_cur, *plugin_cur;
67 for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) {
68 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
69 block = g_strconcat("Plugins_", type_cur->data, NULL);
70 if ((pfile = prefs_write_open(rcpath)) == NULL ||
71 (prefs_set_block_label(pfile, block) < 0)) {
72 g_warning("failed to write plugin list\n");
78 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
79 plugin = (Plugin *) plugin_cur->data;
81 if (!strcmp(plugin->type(), type_cur->data))
82 fprintf(pfile->fp, "%s\n", plugin->filename);
84 fprintf(pfile->fp, "\n");
86 if (prefs_file_close(pfile) < 0)
87 g_warning("failed to write plugin list\n");
93 static gboolean plugin_is_loaded(const gchar *filename)
95 return (g_slist_find_custom(plugins, filename,
96 (GCompareFunc)list_find_by_plugin_filename) != NULL);
99 static Plugin *plugin_get_by_filename(const gchar *filename)
101 GSList *cur = plugins;
102 for(; cur; cur = cur->next) {
103 Plugin *p = (Plugin *)cur->data;
104 if (!strcmp(p->filename, filename)) {
112 * Loads a plugin dependancies
114 * Plugin dependancies are, optionnaly, listed in a file in
115 * PLUGINDIR/$pluginname.deps.
116 * \param filename The filename of the plugin for which we have to load deps
117 * \param error The location where an error string can be stored
118 * \return 0 on success, -1 otherwise
120 static gint plugin_load_deps(const gchar *filename, gchar **error)
122 gchar *tmp = g_strdup(filename);
123 gchar *deps_file = NULL;
127 *strrchr(tmp, '.') = '\0';
128 deps_file = g_strconcat(tmp, ".deps", NULL);
131 fp = g_fopen(deps_file, "rb");
137 while (fgets(buf, sizeof(buf), fp) != NULL) {
138 Plugin *dep_plugin = NULL;
140 buf[strlen(buf)-1]='\0'; /* chop off \n */
141 path = g_strconcat(PLUGINDIR, buf,
142 ".", G_MODULE_SUFFIX, NULL);
143 if ((dep_plugin = plugin_get_by_filename(path)) == NULL) {
144 debug_print("trying to load %s\n", path);
145 dep_plugin = plugin_load(path, error);
146 if (dep_plugin == NULL) {
151 if (!g_slist_find_custom(dep_plugin->rdeps,
152 (gpointer) filename, list_find_by_string)) {
153 debug_print("adding %s to %s rdeps\n",
155 dep_plugin->filename);
157 g_slist_append(dep_plugin->rdeps,
165 static void plugin_unload_rdeps(Plugin *plugin)
167 GSList *cur = plugin->rdeps;
168 debug_print("removing %s rdeps\n", plugin->filename);
170 gchar *file = (gchar *)cur->data;
171 Plugin *rdep_plugin = file?plugin_get_by_filename(file):NULL;
172 debug_print(" rdep %s: %p\n", file, rdep_plugin);
174 plugin_unload(rdep_plugin);
179 g_slist_free(plugin->rdeps);
180 plugin->rdeps = NULL;
185 * \param filename The filename of the plugin to load
186 * \param error The location where an error string can be stored
187 * \return the plugin on success, NULL otherwise
189 Plugin *plugin_load(const gchar *filename, gchar **error)
192 gint (*plugin_init) (gchar **error);
193 gpointer plugin_name, plugin_desc;
194 const gchar *(*plugin_type)(void);
197 g_return_val_if_fail(filename != NULL, NULL);
198 g_return_val_if_fail(error != NULL, NULL);
200 /* check duplicate plugin path name */
201 if (plugin_is_loaded(filename)) {
202 *error = g_strdup(_("Plugin already loaded"));
206 if (plugin_load_deps(filename, error) < 0)
208 plugin = g_new0(Plugin, 1);
209 if (plugin == NULL) {
210 *error = g_strdup(_("Failed to allocate memory for Plugin"));
214 plugin->module = g_module_open(filename, 0);
215 if (plugin->module == NULL) {
216 *error = g_strdup(g_module_error());
221 if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
222 !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
223 !g_module_symbol(plugin->module, "plugin_type", (gpointer *) &plugin_type) ||
224 !g_module_symbol(plugin->module, "plugin_init", (gpointer *) &plugin_init)) {
225 *error = g_strdup(g_module_error());
226 g_module_close(plugin->module);
231 if (!strcmp(plugin_type(), "GTK")) {
232 *error = g_strdup(_("This module is for Sylpheed-Claws GTK1."));
233 g_module_close(plugin->module);
238 if ((ok = plugin_init(error)) < 0) {
239 g_module_close(plugin->module);
244 plugin->name = plugin_name;
245 plugin->desc = plugin_desc;
246 plugin->type = plugin_type;
247 plugin->filename = g_strdup(filename);
249 plugins = g_slist_append(plugins, plugin);
251 debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
256 void plugin_unload(Plugin *plugin)
258 void (*plugin_done) (void);
260 plugin_unload_rdeps(plugin);
262 if (g_module_symbol(plugin->module, "plugin_done", (gpointer *)&plugin_done)) {
266 g_module_close(plugin->module);
267 plugins = g_slist_remove(plugins, plugin);
268 g_free(plugin->filename);
272 void plugin_load_all(const gchar *type)
277 gchar *error, *block;
279 plugin_types = g_slist_append(plugin_types, g_strdup(type));
281 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
282 block = g_strconcat("Plugins_", type, NULL);
283 if ((pfile = prefs_read_open(rcpath)) == NULL ||
284 (prefs_set_block_label(pfile, block) < 0)) {
290 while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
295 if ((buf[0] != '\0') && (plugin_load(buf, &error) == NULL)) {
296 g_warning("plugin loading error: %s\n", error);
300 prefs_file_close(pfile);
305 void plugin_unload_all(const gchar *type)
309 list = g_slist_copy(plugins);
310 list = g_slist_reverse(list);
312 for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
313 Plugin *plugin = (Plugin *) cur->data;
315 if (!strcmp(type, plugin->type()))
316 plugin_unload(plugin);
320 cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
323 g_slist_remove(plugin_types, cur);
327 GSList *plugin_get_list(void)
329 return g_slist_copy(plugins);
332 const gchar *plugin_get_name(Plugin *plugin)
334 return plugin->name();
337 const gchar *plugin_get_desc(Plugin *plugin)
339 return plugin->desc();