2005-02-03 [colin] 1.0.0cvs26
[claws.git] / src / common / plugin.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto and the Sylpheed-Claws Team
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include <stdio.h>
21
22 #include "defs.h"
23 #include <glib.h>
24 #include <gmodule.h>
25
26 #include "intl.h"
27 #include "utils.h"
28 #include "plugin.h"
29 #include "prefs.h"
30
31 struct _Plugin
32 {
33         gchar   *filename;
34         GModule *module;
35         gchar   *(*name) (void);
36         gchar   *(*desc) (void);
37         gchar   *(*type) (void);
38 };
39
40 /**
41  * List of all loaded plugins
42  */
43 GSList *plugins = NULL;
44 GSList *plugin_types = NULL;
45
46 static gint list_find_by_string(gconstpointer data, gconstpointer str)
47 {
48         return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE;
49 }
50
51 void plugin_save_list(void)
52 {
53         gchar *rcpath, *block;
54         PrefFile *pfile;
55         GSList *type_cur, *plugin_cur;
56         Plugin *plugin;
57
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");
64                         g_free(rcpath);
65                         return;
66                 }
67                 g_free(block);
68
69                 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
70                         plugin = (Plugin *) plugin_cur->data;
71                         
72                         if (!strcmp(plugin->type(), type_cur->data))
73                                 fprintf(pfile->fp, "%s\n", plugin->filename);
74                 }
75                 fprintf(pfile->fp, "\n");
76
77                 if (prefs_file_close(pfile) < 0)
78                         g_warning("failed to write plugin list\n");
79
80                 g_free(rcpath); 
81         }
82 }
83
84 /**
85  * Loads a plugin
86  *
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
90  */
91 gint plugin_load(const gchar *filename, gchar **error)
92 {
93         Plugin *plugin;
94         gint (*plugin_init) (gchar **error);
95         gpointer plugin_name, plugin_desc;
96         const gchar *(*plugin_type)(void);
97         gint ok;
98
99         g_return_val_if_fail(filename != NULL, -1);
100         g_return_val_if_fail(error != NULL, -1);
101         
102         plugin = g_new0(Plugin, 1);
103         if (plugin == NULL) {
104                 *error = g_strdup(_("Failed to allocate memory for Plugin"));
105                 return -1;
106         }
107
108         plugin->module = g_module_open(filename, 0);
109         if (plugin->module == NULL) {
110                 *error = g_strdup(g_module_error());
111                 g_free(plugin);
112                 return -1;
113         }
114
115         if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
116             !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
117             !g_module_symbol(plugin->module, "plugin_type", (gpointer *) &plugin_type) ||
118             !g_module_symbol(plugin->module, "plugin_init", (gpointer *) &plugin_init)) {
119                 *error = g_strdup(g_module_error());
120                 g_module_close(plugin->module);
121                 g_free(plugin);
122                 return -1;
123         }
124         
125         if (!strcmp(plugin_type(), "GTK2")) {
126                 *error = g_strdup(_("This module is for Sylpheed-Claws GTK2."));
127                 g_module_close(plugin->module);
128                 g_free(plugin);
129                 return -1;
130         }
131
132         if ((ok = plugin_init(error)) < 0) {
133                 g_module_close(plugin->module);
134                 g_free(plugin);
135                 return ok;
136         }
137
138         plugin->name = plugin_name;
139         plugin->desc = plugin_desc;
140         plugin->type = plugin_type;
141         plugin->filename = g_strdup(filename);
142
143         plugins = g_slist_append(plugins, plugin);
144
145         debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
146
147         return 0;
148 }
149
150 void plugin_unload(Plugin *plugin)
151 {
152         void (*plugin_done) (void);
153
154         if (g_module_symbol(plugin->module, "plugin_done", (gpointer *)&plugin_done)) {
155                 plugin_done();
156         }
157
158         g_module_close(plugin->module);
159         plugins = g_slist_remove(plugins, plugin);
160         g_free(plugin->filename);
161         g_free(plugin);
162 }
163
164 void plugin_load_all(const gchar *type)
165 {
166         gchar *rcpath;
167         gchar buf[BUFFSIZE];
168         PrefFile *pfile;
169         gchar *error, *block;
170
171         plugin_types = g_slist_append(plugin_types, g_strdup(type));
172
173         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL); 
174         block = g_strconcat("Plugins_", type, NULL);
175         if ((pfile = prefs_read_open(rcpath)) == NULL ||
176             (prefs_set_block_label(pfile, block) < 0)) {
177                 g_free(rcpath);
178                 return;
179         }
180         g_free(block);
181
182         while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
183                 if (buf[0] == '[')
184                         break;
185
186                 g_strstrip(buf);
187                 if ((buf[0] != '\0') && (plugin_load(buf, &error) < 0)) {
188                         g_warning("plugin loading error: %s\n", error);
189                         g_free(error);
190                 }                                                       
191         }
192         prefs_file_close(pfile);
193
194         g_free(rcpath);
195 }
196
197 void plugin_unload_all(const gchar *type)
198 {
199         GSList *list, *cur;
200
201         list = g_slist_copy(plugins);
202         list = g_slist_reverse(list);
203
204         for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
205                 Plugin *plugin = (Plugin *) cur->data;
206                 
207                 if (!strcmp(type, plugin->type()))
208                         plugin_unload(plugin);
209         }
210         g_slist_free(list);
211
212         cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
213         if (cur) {
214                 g_free(cur->data);
215                 g_slist_remove(plugin_types, cur);
216         }
217 }
218
219 GSList *plugin_get_list(void)
220 {
221         return g_slist_copy(plugins);
222 }
223
224 const gchar *plugin_get_name(Plugin *plugin)
225 {
226         return plugin->name();
227 }
228
229 const gchar *plugin_get_desc(Plugin *plugin)
230 {
231         return plugin->desc();
232 }