0.9.4claws42
[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, plugin_type;
96         gint ok;
97
98         g_return_val_if_fail(filename != NULL, -1);
99         g_return_val_if_fail(error != NULL, -1);
100         
101         plugin = g_new0(Plugin, 1);
102         if (plugin == NULL) {
103                 *error = g_strdup(_("Failed to allocate memory for Plugin"));
104                 return -1;
105         }
106
107         plugin->module = g_module_open(filename, 0);
108         if (plugin->module == NULL) {
109                 *error = g_strdup(g_module_error());
110                 g_free(plugin);
111                 return -1;
112         }
113
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);
120                 g_free(plugin);
121                 return -1;
122         }
123
124         if ((ok = plugin_init(error)) < 0) {
125                 g_module_close(plugin->module);
126                 g_free(plugin);
127                 return ok;
128         }
129
130         plugin->name = plugin_name;
131         plugin->desc = plugin_desc;
132         plugin->type = plugin_type;
133         plugin->filename = g_strdup(filename);
134
135         plugins = g_slist_append(plugins, plugin);
136
137         debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
138
139         return 0;
140 }
141
142 void plugin_unload(Plugin *plugin)
143 {
144         void (*plugin_done) (void);
145
146         if (g_module_symbol(plugin->module, "plugin_done", (gpointer *)&plugin_done)) {
147                 plugin_done();
148         }
149
150         g_module_close(plugin->module);
151         plugins = g_slist_remove(plugins, plugin);
152         g_free(plugin->filename);
153         g_free(plugin);
154 }
155
156 void plugin_load_all(const gchar *type)
157 {
158         gchar *rcpath;
159         gchar buf[BUFFSIZE];
160         PrefFile *pfile;
161         gchar *error, *block;
162
163         plugin_types = g_slist_append(plugin_types, g_strdup(type));
164
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)) {
169                 g_free(rcpath);
170                 return;
171         }
172         g_free(block);
173
174         while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
175                 if (buf[0] == '[')
176                         break;
177
178                 g_strstrip(buf);
179                 if ((buf[0] != '\0') && (plugin_load(buf, &error) < 0)) {
180                         g_warning("plugin loading error: %s\n", error);
181                         g_free(error);
182                 }                                                       
183         }
184         prefs_file_close(pfile);
185
186         g_free(rcpath);
187 }
188
189 void plugin_unload_all(const gchar *type)
190 {
191         GSList *list, *cur;
192
193         list = g_slist_copy(plugins);
194         list = g_slist_reverse(list);
195
196         for(cur = list; cur != NULL; cur = g_slist_next(cur)) {
197                 Plugin *plugin = (Plugin *) cur->data;
198                 
199                 if (!strcmp(type, plugin->type()))
200                         plugin_unload(plugin);
201         }
202         g_slist_free(list);
203
204         cur = g_slist_find_custom(plugin_types, type, list_find_by_string);
205         if (cur) {
206                 g_free(cur->data);
207                 g_slist_remove(plugin_types, cur);
208         }
209 }
210
211 GSList *plugin_get_list(void)
212 {
213         return g_slist_copy(plugins);
214 }
215
216 const gchar *plugin_get_name(Plugin *plugin)
217 {
218         return plugin->name();
219 }
220
221 const gchar *plugin_get_desc(Plugin *plugin)
222 {
223         return plugin->desc();
224 }