2005-12-13 [colin] 1.9.100cvs86
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <stdio.h>
21
22 #include "defs.h"
23 #include <glib.h>
24 #include <glib/gi18n.h>
25 #include <gmodule.h>
26
27 #include "utils.h"
28 #include "plugin.h"
29 #include "prefs.h"
30
31 struct _Plugin
32 {
33         gchar   *filename;
34         GModule *module;
35         const gchar *(*name) (void);
36         const gchar *(*desc) (void);
37         const gchar *(*type) (void);
38         const gchar *(*licence) (void);
39         GSList *rdeps;
40 };
41
42 /**
43  * List of all loaded plugins
44  */
45 GSList *plugins = NULL;
46 GSList *plugin_types = NULL;
47
48 static gint list_find_by_string(gconstpointer data, gconstpointer str)
49 {
50         return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE;
51 }
52
53 static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *filename)
54 {
55         g_return_val_if_fail(plugin, 1);
56         g_return_val_if_fail(plugin->filename, 1);
57         g_return_val_if_fail(filename, 1);
58         return strcmp(filename, plugin->filename);
59 }
60
61 void plugin_save_list(void)
62 {
63         gchar *rcpath, *block;
64         PrefFile *pfile;
65         GSList *type_cur, *plugin_cur;
66         Plugin *plugin;
67
68         for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) {
69                 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
70                 block = g_strconcat("Plugins_", type_cur->data, NULL);
71                 if ((pfile = prefs_write_open(rcpath)) == NULL ||
72                     (prefs_set_block_label(pfile, block) < 0)) {
73                         g_warning("failed to write plugin list\n");
74                         g_free(rcpath);
75                         return;
76                 }
77                 g_free(block);
78
79                 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
80                         plugin = (Plugin *) plugin_cur->data;
81                         
82                         if (!strcmp(plugin->type(), type_cur->data))
83                                 fprintf(pfile->fp, "%s\n", plugin->filename);
84                 }
85                 fprintf(pfile->fp, "\n");
86
87                 if (prefs_file_close(pfile) < 0)
88                         g_warning("failed to write plugin list\n");
89
90                 g_free(rcpath); 
91         }
92 }
93
94 static gboolean plugin_is_loaded(const gchar *filename)
95 {
96         return (g_slist_find_custom(plugins, filename, 
97                   (GCompareFunc)list_find_by_plugin_filename) != NULL);
98 }
99
100 static Plugin *plugin_get_by_filename(const gchar *filename)
101 {
102         GSList *cur = plugins;
103         for(; cur; cur = cur->next) {
104                 Plugin *p = (Plugin *)cur->data;
105                 if (!strcmp(p->filename, filename)) {
106                         return p;
107                 } 
108         }
109         return NULL;
110 }
111
112 /** 
113  * Loads a plugin dependancies
114  * 
115  * Plugin dependancies are, optionnaly, listed in a file in
116  * PLUGINDIR/$pluginname.deps.
117  * \param filename The filename of the plugin for which we have to load deps
118  * \param error The location where an error string can be stored
119  * \return 0 on success, -1 otherwise
120  */
121 static gint plugin_load_deps(const gchar *filename, gchar **error)
122 {
123         gchar *tmp = g_strdup(filename);
124         gchar *deps_file = NULL;
125         FILE *fp = NULL;
126         gchar buf[BUFFSIZE];
127         
128         *strrchr(tmp, '.') = '\0';
129         deps_file = g_strconcat(tmp, ".deps", NULL);
130         g_free(tmp);
131         
132         fp = g_fopen(deps_file, "rb");
133         g_free(deps_file);
134         
135         if (!fp)
136                 return 0;
137         
138         while (fgets(buf, sizeof(buf), fp) != NULL) {
139                 Plugin *dep_plugin = NULL;
140                 gchar *path = NULL;
141                 buf[strlen(buf)-1]='\0'; /* chop off \n */
142                 path = g_strconcat(PLUGINDIR, buf,
143                                 ".", G_MODULE_SUFFIX, NULL);
144                 if ((dep_plugin = plugin_get_by_filename(path)) == NULL) {
145                         debug_print("trying to load %s\n", path);
146                         dep_plugin = plugin_load(path, error);
147                         if (dep_plugin == NULL) {
148                                 g_free(path);
149                                 return -1;
150                         }
151                 }
152                 g_free(path);
153                 if (!g_slist_find_custom(dep_plugin->rdeps, 
154                                 (gpointer) filename, list_find_by_string)) {
155                         debug_print("adding %s to %s rdeps\n",
156                                 filename,
157                                 dep_plugin->filename);
158                         dep_plugin->rdeps = 
159                                 g_slist_append(dep_plugin->rdeps, 
160                                         g_strdup(filename));
161                 }
162         }
163         fclose(fp);
164         return 0;
165 }
166
167 static void plugin_unload_rdeps(Plugin *plugin)
168 {
169         GSList *cur = plugin->rdeps;
170         debug_print("removing %s rdeps\n", plugin->filename);
171         while (cur) {
172                 gchar *file = (gchar *)cur->data;
173                 Plugin *rdep_plugin = file?plugin_get_by_filename(file):NULL;
174                 debug_print(" rdep %s: %p\n", file, rdep_plugin);
175                 if (rdep_plugin) {
176                         plugin_unload(rdep_plugin);
177                 }
178                 g_free(file);
179                 cur = cur->next;
180         }
181         g_slist_free(plugin->rdeps);
182         plugin->rdeps = NULL;
183 }
184 /**
185  * Loads a plugin
186  *
187  * \param filename The filename of the plugin to load
188  * \param error The location where an error string can be stored
189  * \return the plugin on success, NULL otherwise
190  */
191 Plugin *plugin_load(const gchar *filename, gchar **error)
192 {
193         Plugin *plugin;
194         gint (*plugin_init) (gchar **error);
195         gpointer plugin_name, plugin_desc;
196         const gchar *(*plugin_type)(void);
197         const gchar *(*plugin_licence)(void);
198         gint ok;
199
200         g_return_val_if_fail(filename != NULL, NULL);
201         g_return_val_if_fail(error != NULL, NULL);
202
203         /* check duplicate plugin path name */
204         if (plugin_is_loaded(filename)) {
205                 *error = g_strdup(_("Plugin already loaded"));
206                 return NULL;                
207         }                               
208         
209         if (plugin_load_deps(filename, error) < 0)
210                 return NULL;
211         plugin = g_new0(Plugin, 1);
212         if (plugin == NULL) {
213                 *error = g_strdup(_("Failed to allocate memory for Plugin"));
214                 return NULL;
215         }
216
217         plugin->module = g_module_open(filename, 0);
218         if (plugin->module == NULL) {
219                 *error = g_strdup(g_module_error());
220                 g_free(plugin);
221                 return NULL;
222         }
223
224         if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) ||
225             !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) ||
226             !g_module_symbol(plugin->module, "plugin_type", (gpointer)&plugin_type) ||
227             !g_module_symbol(plugin->module, "plugin_licence", (gpointer)&plugin_licence) ||
228             !g_module_symbol(plugin->module, "plugin_init", (gpointer)&plugin_init)) {
229                 *error = g_strdup(g_module_error());
230                 g_module_close(plugin->module);
231                 g_free(plugin);
232                 return NULL;
233         }
234         
235         if (strcmp(plugin_licence(), "GPL")
236         &&  strncmp(plugin_licence(), "GPL-compatible", strlen("GPL-compatible"))) {
237                 *error = g_strdup(_("This module is not licenced under a GPL compatible licence."));
238                 g_module_close(plugin->module);
239                 g_free(plugin);
240                 return NULL;
241         }
242
243         if (!strcmp(plugin_type(), "GTK")) {
244                 *error = g_strdup(_("This module is for Sylpheed-Claws GTK1."));
245                 g_module_close(plugin->module);
246                 g_free(plugin);
247                 return NULL;
248         }
249
250         if ((ok = plugin_init(error)) < 0) {
251                 g_module_close(plugin->module);
252                 g_free(plugin);
253                 return NULL;
254         }
255
256         plugin->name = plugin_name;
257         plugin->desc = plugin_desc;
258         plugin->type = plugin_type;
259         plugin->licence = plugin_licence;
260         plugin->filename = g_strdup(filename);
261
262         plugins = g_slist_append(plugins, plugin);
263
264         debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
265
266         return plugin;
267 }
268
269 void plugin_unload(Plugin *plugin)
270 {
271         void (*plugin_done) (void);
272
273         plugin_unload_rdeps(plugin);
274
275         if (g_module_symbol(plugin->module, "plugin_done", (gpointer) &plugin_done)) {
276                 plugin_done();
277         }
278
279         g_module_close(plugin->module);
280         plugins = g_slist_remove(plugins, plugin);
281         g_free(plugin->filename);
282         g_free(plugin);
283 }
284
285 void plugin_load_all(const gchar *type)
286 {
287         gchar *rcpath;
288         gchar buf[BUFFSIZE];
289         PrefFile *pfile;
290         gchar *error, *block;
291
292         plugin_types = g_slist_append(plugin_types, g_strdup(type));
293
294         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL); 
295         block = g_strconcat("Plugins_", type, NULL);
296         if ((pfile = prefs_read_open(rcpath)) == NULL ||
297             (prefs_set_block_label(pfile, block) < 0)) {
298                 g_free(rcpath);
299                 return;
300         }
301         g_free(block);
302
303         while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
304                 if (buf[0] == '[')
305                         break;
306
307                 g_strstrip(buf);
308                 if ((buf[0] != '\0') && (plugin_load(buf, &error) == NULL)) {
309                         g_warning("plugin loading error: %s\n", error);
310                         g_free(error);
311                 }                                                       
312         }
313         prefs_file_close(pfile);
314
315         g_free(rcpath);
316 }
317
318 void plugin_unload_all(const gchar *type)
319 {
320         GSList *list, *cur;
321
322         list = g_slist_copy(plugins);
323         list = g_slist_reverse(list);
324
325         for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
326                 Plugin *plugin = (Plugin *) cur->data;
327                 
328                 if (!strcmp(type, plugin->type()))
329                         plugin_unload(plugin);
330         }
331         g_slist_free(list);
332
333         cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
334         if (cur) {
335                 g_free(cur->data);
336                 g_slist_remove(plugin_types, cur);
337         }
338 }
339
340 GSList *plugin_get_list(void)
341 {
342         return g_slist_copy(plugins);
343 }
344
345 const gchar *plugin_get_name(Plugin *plugin)
346 {
347         return plugin->name();
348 }
349
350 const gchar *plugin_get_desc(Plugin *plugin)
351 {
352         return plugin->desc();
353 }