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