0.8.8claws2
[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 <glib.h>
23 #include <gmodule.h>
24
25 #include "intl.h"
26 #include "defs.h"
27 #include "utils.h"
28 #include "plugin.h"
29 #include "prefs.h"
30
31 struct _Plugin
32 {
33         gchar   *filename;
34         gchar   *name;
35         GModule *module;
36         gchar   *desc;
37 };
38
39 /**
40  * List of all loaded plugins
41  */
42 GSList *plugins = NULL;
43
44 void plugin_save_list()
45 {
46         gchar *rcpath;
47         PrefFile *pfile;
48         GSList *cur;
49         Plugin *plugin;
50
51         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "pluginsrc", NULL);
52         if ((pfile = prefs_write_open(rcpath)) == NULL) {
53                 g_warning("failed to write plugin list\n");
54                 g_free(rcpath);
55                 return;
56         }
57
58         for (cur = plugins; cur != NULL; cur = g_slist_next(cur)) {
59                 plugin = (Plugin *)cur->data;
60                         
61                 fprintf(pfile->fp, "%s\n", plugin->filename);
62         }
63
64         if (prefs_write_close(pfile) < 0)
65                 g_warning("failed to write plugin list\n");
66
67         g_free(rcpath); 
68 }
69
70 /**
71  * Loads a plugin
72  *
73  * \param filename The filename of the plugin to load
74  * \param error The location where an error string can be stored
75  * \return 0 on success, -1 otherwise
76  */
77 gint plugin_load(const gchar *filename, gchar **error)
78 {
79         Plugin *plugin;
80         gint (*plugin_init) (gchar **error);
81         gint ok;
82
83         g_return_val_if_fail(filename != NULL, -1);
84         g_return_val_if_fail(error != NULL, -1);
85         
86         plugin = g_new0(Plugin, 1);
87         if (plugin == NULL) {
88                 *error = g_strdup(_("Failed to allocate memory for Plugin"));
89                 return -1;
90         }
91
92         plugin->module = g_module_open(filename, 0);
93         if (plugin->module == NULL) {
94                 *error = g_strdup(g_module_error());
95                 g_free(plugin);
96                 return -1;
97         }
98
99         if (!g_module_symbol(plugin->module, "plugin_name", (gpointer *)&plugin->name) ||
100             !g_module_symbol(plugin->module, "plugin_desc", (gpointer *)&plugin->desc) ||
101             !g_module_symbol(plugin->module, "plugin_init", (gpointer *)&plugin_init)) {
102                 *error = g_strdup(g_module_error());
103                 g_module_close(plugin->module);
104                 g_free(plugin);
105                 return -1;
106         }
107
108         if ((ok = plugin_init(error)) < 0) {
109                 g_module_close(plugin->module);
110                 g_free(plugin);
111                 return ok;
112         }
113
114         plugins = g_slist_append(plugins, plugin);
115
116         return 0;
117 }
118
119 void plugin_unload(Plugin *plugin)
120 {
121         void (*plugin_done) ();
122
123         if (g_module_symbol(plugin->module, "plugin_done", (gpointer *)&plugin_done)) {
124                 plugin_done();
125         }
126
127         g_module_close(plugin->module);
128         g_free(plugin);
129 }
130
131 void plugin_load_all()
132 {
133         gchar *rcpath;
134         gchar buf[BUFFSIZE];
135         FILE *fp;
136
137         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "pluginsrc", NULL);       
138         if ((fp = fopen(rcpath, "rb")) != NULL) {
139                 gchar *error;
140
141                 while(fgets(buf, sizeof(buf), fp) != NULL) {
142                         g_strstrip(buf);
143                         
144                         if (plugin_load(buf, &error) < 0) {
145                                 debug_print("plugin loading error: %s\n", error);
146                                 g_free(error);
147                         }
148                 }
149
150                 fclose(fp);
151         }
152
153         g_free(rcpath);
154 }
155
156 void plugin_unload_all()
157 {
158         GSList *cur;
159
160         for (cur = plugins; cur != NULL; cur = g_slist_next(cur)) {
161                 plugin_unload((Plugin *)cur->data);
162         }
163         g_slist_free(plugins);
164         plugins = NULL;
165 }
166
167 GSList *plugin_get_list()
168 {
169         return g_slist_copy(plugins);
170 }
171
172 const gchar *plugin_get_name(Plugin *plugin)
173 {
174         return plugin->name;
175 }
176
177 const gchar *plugin_get_desc(Plugin *plugin)
178 {
179         return plugin->desc;
180 }