0.8.8claws30
[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         GModule *module;
35         gchar   *(*name) ();
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, COMMON_RC, NULL);
52         if ((pfile = prefs_write_open(rcpath)) == NULL ||
53             (prefs_set_block_label(pfile, "Plugins") < 0)) {
54                 g_warning("failed to write plugin list\n");
55                 g_free(rcpath);
56                 return;
57         }
58
59         for (cur = plugins; cur != NULL; cur = g_slist_next(cur)) {
60                 plugin = (Plugin *)cur->data;
61                         
62                 fprintf(pfile->fp, "%s\n", plugin->filename);
63         }
64
65         if (prefs_file_close(pfile) < 0)
66                 g_warning("failed to write plugin list\n");
67
68         g_free(rcpath); 
69 }
70
71 /**
72  * Loads a plugin
73  *
74  * \param filename The filename of the plugin to load
75  * \param error The location where an error string can be stored
76  * \return 0 on success, -1 otherwise
77  */
78 gint plugin_load(const gchar *filename, gchar **error)
79 {
80         Plugin *plugin;
81         gint (*plugin_init) (gchar **error);
82         gchar *plugin_name, *plugin_desc;
83         gint ok;
84
85         g_return_val_if_fail(filename != NULL, -1);
86         g_return_val_if_fail(error != NULL, -1);
87         
88         plugin = g_new0(Plugin, 1);
89         if (plugin == NULL) {
90                 *error = g_strdup(_("Failed to allocate memory for Plugin"));
91                 return -1;
92         }
93
94         plugin->module = g_module_open(filename, 0);
95         if (plugin->module == NULL) {
96                 *error = g_strdup(g_module_error());
97                 g_free(plugin);
98                 return -1;
99         }
100
101         if (!g_module_symbol(plugin->module, "plugin_name", (gpointer *)&plugin_name) ||
102             !g_module_symbol(plugin->module, "plugin_desc", (gpointer *)&plugin_desc) ||
103             !g_module_symbol(plugin->module, "plugin_init", (gpointer *)&plugin_init)) {
104                 *error = g_strdup(g_module_error());
105                 g_module_close(plugin->module);
106                 g_free(plugin);
107                 return -1;
108         }
109
110         if ((ok = plugin_init(error)) < 0) {
111                 g_module_close(plugin->module);
112                 g_free(plugin);
113                 return ok;
114         }
115
116         plugin->name = plugin_name;
117         plugin->desc = plugin_desc;
118         plugin->filename = g_strdup(filename);
119
120         plugins = g_slist_append(plugins, plugin);
121
122         debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
123
124         return 0;
125 }
126
127 void plugin_unload(Plugin *plugin)
128 {
129         void (*plugin_done) ();
130
131         if (g_module_symbol(plugin->module, "plugin_done", (gpointer *)&plugin_done)) {
132                 plugin_done();
133         }
134
135         g_module_close(plugin->module);
136         g_free(plugin);
137 }
138
139 void plugin_load_all()
140 {
141         gchar *rcpath;
142         gchar buf[BUFFSIZE];
143         PrefFile *pfile;
144         gchar *error;
145
146         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL); 
147         if ((pfile = prefs_read_open(rcpath)) == NULL ||
148             (prefs_set_block_label(pfile, "Plugins") < 0)) {
149                 g_free(rcpath);
150                 return;
151         }
152
153         while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
154                 if (buf[0] == '[')
155                         break;
156
157                 g_strstrip(buf);
158                 if ((buf[0] != '\0') && (plugin_load(buf, &error) < 0)) {
159                         g_warning("plugin loading error: %s\n", error);
160                         g_free(error);
161                 }                                                       
162         }
163         prefs_file_close(pfile);
164
165         g_free(rcpath);
166 }
167
168 void plugin_unload_all()
169 {
170         GSList *cur;
171
172         for (cur = plugins; cur != NULL; cur = g_slist_next(cur)) {
173                 plugin_unload((Plugin *)cur->data);
174         }
175         g_slist_free(plugins);
176         plugins = NULL;
177 }
178
179 GSList *plugin_get_list()
180 {
181         return g_slist_copy(plugins);
182 }
183
184 const gchar *plugin_get_name(Plugin *plugin)
185 {
186         return plugin->name();
187 }
188
189 const gchar *plugin_get_desc(Plugin *plugin)
190 {
191         return plugin->desc();
192 }