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.
35 gchar *(*name) (void);
36 gchar *(*desc) (void);
37 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 void plugin_save_list(void)
53 gchar *rcpath, *block;
55 GSList *type_cur, *plugin_cur;
58 for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) {
59 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
60 block = g_strconcat("Plugins_", type_cur->data, NULL);
61 if ((pfile = prefs_write_open(rcpath)) == NULL ||
62 (prefs_set_block_label(pfile, block) < 0)) {
63 g_warning("failed to write plugin list\n");
69 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
70 plugin = (Plugin *) plugin_cur->data;
72 if (!strcmp(plugin->type(), type_cur->data))
73 fprintf(pfile->fp, "%s\n", plugin->filename);
75 fprintf(pfile->fp, "\n");
77 if (prefs_file_close(pfile) < 0)
78 g_warning("failed to write plugin list\n");
87 * \param filename The filename of the plugin to load
88 * \param error The location where an error string can be stored
89 * \return 0 on success, -1 otherwise
91 gint plugin_load(const gchar *filename, gchar **error)
94 gint (*plugin_init) (gchar **error);
95 gpointer plugin_name, plugin_desc, plugin_type;
98 g_return_val_if_fail(filename != NULL, -1);
99 g_return_val_if_fail(error != NULL, -1);
101 plugin = g_new0(Plugin, 1);
102 if (plugin == NULL) {
103 *error = g_strdup(_("Failed to allocate memory for Plugin"));
107 plugin->module = g_module_open(filename, 0);
108 if (plugin->module == NULL) {
109 *error = g_strdup(g_module_error());
114 if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
115 !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
116 !g_module_symbol(plugin->module, "plugin_type", &plugin_type) ||
117 !g_module_symbol(plugin->module, "plugin_init", (gpointer *) &plugin_init)) {
118 *error = g_strdup(g_module_error());
119 g_module_close(plugin->module);
124 if ((ok = plugin_init(error)) < 0) {
125 g_module_close(plugin->module);
130 plugin->name = plugin_name;
131 plugin->desc = plugin_desc;
132 plugin->type = plugin_type;
133 plugin->filename = g_strdup(filename);
135 plugins = g_slist_append(plugins, plugin);
137 debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
142 void plugin_unload(Plugin *plugin)
144 void (*plugin_done) (void);
146 if (g_module_symbol(plugin->module, "plugin_done", (gpointer *)&plugin_done)) {
150 g_module_close(plugin->module);
151 plugins = g_slist_remove(plugins, plugin);
152 g_free(plugin->filename);
156 void plugin_load_all(const gchar *type)
161 gchar *error, *block;
163 plugin_types = g_slist_append(plugin_types, g_strdup(type));
165 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
166 block = g_strconcat("Plugins_", type, NULL);
167 if ((pfile = prefs_read_open(rcpath)) == NULL ||
168 (prefs_set_block_label(pfile, block) < 0)) {
174 while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
179 if ((buf[0] != '\0') && (plugin_load(buf, &error) < 0)) {
180 g_warning("plugin loading error: %s\n", error);
184 prefs_file_close(pfile);
189 void plugin_unload_all(const gchar *type)
193 list = g_slist_copy(plugins);
194 list = g_slist_reverse(list);
196 for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
197 Plugin *plugin = (Plugin *) cur->data;
199 if (!strcmp(type, plugin->type()))
200 plugin_unload(plugin);
204 cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
207 g_slist_remove(plugin_types, cur);
211 GSList *plugin_get_list(void)
213 return g_slist_copy(plugins);
216 const gchar *plugin_get_name(Plugin *plugin)
218 return plugin->name();
221 const gchar *plugin_get_desc(Plugin *plugin)
223 return plugin->desc();