10cb9d4c9b2d20e9845f5d850c178c08e74ef7ff
[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 <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 };
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 static gint list_find_by_plugin_filename(const Plugin *plugin, const gchar *filename)
52 {
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);
56         
57         return strcmp(filename, plugin->filename);
58 }
59
60 void plugin_save_list(void)
61 {
62         gchar *rcpath, *block;
63         PrefFile *pfile;
64         GSList *type_cur, *plugin_cur;
65         Plugin *plugin;
66
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");
73                         g_free(rcpath);
74                         return;
75                 }
76                 g_free(block);
77
78                 for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
79                         plugin = (Plugin *) plugin_cur->data;
80                         
81                         if (!strcmp(plugin->type(), type_cur->data))
82                                 fprintf(pfile->fp, "%s\n", plugin->filename);
83                 }
84                 fprintf(pfile->fp, "\n");
85
86                 if (prefs_file_close(pfile) < 0)
87                         g_warning("failed to write plugin list\n");
88
89                 g_free(rcpath); 
90         }
91 }
92
93 /**
94  * Loads a plugin
95  *
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
99  */
100 gint plugin_load(const gchar *filename, gchar **error)
101 {
102         Plugin *plugin;
103         gint (*plugin_init) (gchar **error);
104         gpointer plugin_name, plugin_desc;
105         const gchar *(*plugin_type)(void);
106         gint ok;
107
108         g_return_val_if_fail(filename != NULL, -1);
109         g_return_val_if_fail(error != NULL, -1);
110
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"));
115                 return -1;                
116         }                               
117         
118         plugin = g_new0(Plugin, 1);
119         if (plugin == NULL) {
120                 *error = g_strdup(_("Failed to allocate memory for Plugin"));
121                 return -1;
122         }
123
124         plugin->module = g_module_open(filename, 0);
125         if (plugin->module == NULL) {
126                 *error = g_strdup(g_module_error());
127                 g_free(plugin);
128                 return -1;
129         }
130
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);
137                 g_free(plugin);
138                 return -1;
139         }
140         
141         if (!strcmp(plugin_type(), "GTK")) {
142                 *error = g_strdup(_("This module is for Sylpheed-Claws GTK1."));
143                 g_module_close(plugin->module);
144                 g_free(plugin);
145                 return -1;
146         }
147
148         if ((ok = plugin_init(error)) < 0) {
149                 g_module_close(plugin->module);
150                 g_free(plugin);
151                 return ok;
152         }
153
154         plugin->name = plugin_name;
155         plugin->desc = plugin_desc;
156         plugin->type = plugin_type;
157         plugin->filename = g_strdup(filename);
158
159         plugins = g_slist_append(plugins, plugin);
160
161         debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
162
163         return 0;
164 }
165
166 void plugin_unload(Plugin *plugin)
167 {
168         void (*plugin_done) (void);
169
170         if (g_module_symbol(plugin->module, "plugin_done", (gpointer *)&plugin_done)) {
171                 plugin_done();
172         }
173
174         g_module_close(plugin->module);
175         plugins = g_slist_remove(plugins, plugin);
176         g_free(plugin->filename);
177         g_free(plugin);
178 }
179
180 void plugin_load_all(const gchar *type)
181 {
182         gchar *rcpath;
183         gchar buf[BUFFSIZE];
184         PrefFile *pfile;
185         gchar *error, *block;
186
187         plugin_types = g_slist_append(plugin_types, g_strdup(type));
188
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)) {
193                 g_free(rcpath);
194                 return;
195         }
196         g_free(block);
197
198         while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
199                 if (buf[0] == '[')
200                         break;
201
202                 g_strstrip(buf);
203                 if ((buf[0] != '\0') && (plugin_load(buf, &error) < 0)) {
204                         g_warning("plugin loading error: %s\n", error);
205                         g_free(error);
206                 }                                                       
207         }
208         prefs_file_close(pfile);
209
210         g_free(rcpath);
211 }
212
213 void plugin_unload_all(const gchar *type)
214 {
215         GSList *list, *cur;
216
217         list = g_slist_copy(plugins);
218         list = g_slist_reverse(list);
219
220         for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
221                 Plugin *plugin = (Plugin *) cur->data;
222                 
223                 if (!strcmp(type, plugin->type()))
224                         plugin_unload(plugin);
225         }
226         g_slist_free(list);
227
228         cur = g_slist_find_custom(plugin_types, (gpointer) type, list_find_by_string);
229         if (cur) {
230                 g_free(cur->data);
231                 g_slist_remove(plugin_types, cur);
232         }
233 }
234
235 GSList *plugin_get_list(void)
236 {
237         return g_slist_copy(plugins);
238 }
239
240 const gchar *plugin_get_name(Plugin *plugin)
241 {
242         return plugin->name();
243 }
244
245 const gchar *plugin_get_desc(Plugin *plugin)
246 {
247         return plugin->desc();
248 }