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);
41 * List of all loaded plugins
43 GSList *plugins = NULL;
44 GSList *plugin_types = NULL;
46 static gint list_find_by_string(gconstpointer data, gconstpointer str)
48 return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE;
51 static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *filename)
53 g_return_val_if_fail(plugin, 1);
54 g_return_val_if_fail(plugin->filename, 1);
55 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");
96 * \param filename The filename of the plugin to load
97 * \param error The location where an error string can be stored
98 * \return 0 on success, -1 otherwise
100 gint plugin_load(const gchar *filename, gchar **error)
103 gint (*plugin_init) (gchar **error);
104 gpointer plugin_name, plugin_desc;
105 const gchar *(*plugin_type)(void);
108 g_return_val_if_fail(filename != NULL, -1);
109 g_return_val_if_fail(error != NULL, -1);
111 /* check duplicate plugin path name */
112 if (g_slist_find_custom(plugins, filename,
113 (GCompareFunc)list_find_by_plugin_filename)) {
114 *error = g_strdup(_("Plugin already loaded"));
118 plugin = g_new0(Plugin, 1);
119 if (plugin == NULL) {
120 *error = g_strdup(_("Failed to allocate memory for Plugin"));
124 plugin->module = g_module_open(filename, 0);
125 if (plugin->module == NULL) {
126 *error = g_strdup(g_module_error());
131 if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
132 !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
133 !g_module_symbol(plugin->module, "plugin_type", (gpointer *) &plugin_type) ||
134 !g_module_symbol(plugin->module, "plugin_init", (gpointer *) &plugin_init)) {
135 *error = g_strdup(g_module_error());
136 g_module_close(plugin->module);
141 if (!strcmp(plugin_type(), "GTK")) {
142 *error = g_strdup(_("This module is for Sylpheed-Claws GTK1."));
143 g_module_close(plugin->module);
148 if ((ok = plugin_init(error)) < 0) {
149 g_module_close(plugin->module);
154 plugin->name = plugin_name;
155 plugin->desc = plugin_desc;
156 plugin->type = plugin_type;
157 plugin->filename = g_strdup(filename);
159 plugins = g_slist_append(plugins, plugin);
161 debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
166 void plugin_unload(Plugin *plugin)
168 void (*plugin_done) (void);
170 if (g_module_symbol(plugin->module, "plugin_done", (gpointer *)&plugin_done)) {
174 g_module_close(plugin->module);
175 plugins = g_slist_remove(plugins, plugin);
176 g_free(plugin->filename);
180 void plugin_load_all(const gchar *type)
185 gchar *error, *block;
187 plugin_types = g_slist_append(plugin_types, g_strdup(type));
189 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
190 block = g_strconcat("Plugins_", type, NULL);
191 if ((pfile = prefs_read_open(rcpath)) == NULL ||
192 (prefs_set_block_label(pfile, block) < 0)) {
198 while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
203 if ((buf[0] != '\0') && (plugin_load(buf, &error) < 0)) {
204 g_warning("plugin loading error: %s\n", error);
208 prefs_file_close(pfile);
213 void plugin_unload_all(const gchar *type)
217 list = g_slist_copy(plugins);
218 list = g_slist_reverse(list);
220 for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
221 Plugin *plugin = (Plugin *) cur->data;
223 if (!strcmp(type, plugin->type()))
224 plugin_unload(plugin);
228 cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
231 g_slist_remove(plugin_types, cur);
235 GSList *plugin_get_list(void)
237 return g_slist_copy(plugins);
240 const gchar *plugin_get_name(Plugin *plugin)
242 return plugin->name();
245 const gchar *plugin_get_desc(Plugin *plugin)
247 return plugin->desc();