46f4887debd2d6f864d32fad36407dd670b4be5e
[claws.git] / src / gtk / pluginwindow.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 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include <gtk/gtk.h>
27
28 #include "intl.h"
29 #include "plugin.h"
30
31 #include "filesel.h"
32 #include "alertpanel.h"
33 #include "../inc.h"
34
35 typedef struct _PluginWindow
36 {
37         GtkWidget *window;
38         GtkWidget *plugin_list;
39         GtkWidget *plugin_desc;
40         GtkWidget *unload_btn;
41
42         Plugin *selected_plugin;
43 } PluginWindow;
44
45 static void close_cb(GtkButton *button, PluginWindow *pluginwindow)
46 {
47         gtk_widget_destroy(pluginwindow->window);
48         g_free(pluginwindow);
49         plugin_save_list();
50         inc_unlock();
51 }
52
53 static void set_plugin_list(PluginWindow *pluginwindow)
54 {
55         GSList *plugins, *cur;
56         gchar *text[1];
57         gint row;
58         GtkCList *clist = GTK_CLIST(pluginwindow->plugin_list);
59         GtkTextBuffer *textbuf;
60         GtkTextIter start_iter, end_iter;
61         
62         plugins = plugin_get_list();
63         gtk_clist_freeze(clist);
64         gtk_clist_clear(clist);
65         textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pluginwindow->plugin_desc));
66         gtk_text_buffer_get_start_iter(textbuf, &start_iter);
67         gtk_text_buffer_get_end_iter(textbuf, &end_iter);
68         gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
69         gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);
70         
71         for(cur = plugins; cur != NULL; cur = g_slist_next(cur)) {
72                 Plugin *plugin = (Plugin *) cur->data;
73                 
74                 text[0] = (gchar *) plugin_get_name(plugin);
75                 row = gtk_clist_append(clist, text);
76                 gtk_clist_set_row_data(clist, row, plugin);
77         }
78         gtk_clist_thaw(clist);
79 }
80
81 static void select_row_cb(GtkCList *clist, gint row, gint column,
82                           GdkEventButton *event, PluginWindow *pluginwindow)
83 {
84         Plugin *plugin;
85         GtkTextView *plugin_desc = GTK_TEXT_VIEW(pluginwindow->plugin_desc);
86         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(plugin_desc);
87         GtkTextIter start_iter, end_iter;
88         const gchar *text;
89         
90         plugin = (Plugin *) gtk_clist_get_row_data(clist, row);
91         pluginwindow->selected_plugin = plugin;
92
93         if (pluginwindow->selected_plugin != NULL) {
94                 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
95                 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
96                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
97                 text = plugin_get_desc(plugin);
98                 gtk_text_buffer_insert(textbuf, &start_iter, text, strlen(text));
99                 gtk_widget_set_sensitive(pluginwindow->unload_btn, TRUE);
100         } else {
101                 gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);
102         }
103 }
104
105 static void unselect_row_cb(GtkCList * clist, gint row, gint column,
106                             GdkEventButton * event, PluginWindow *pluginwindow)
107 {
108         gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);      
109 }
110
111 static void unload_cb(GtkButton *button, PluginWindow *pluginwindow)
112 {
113         Plugin *plugin = pluginwindow->selected_plugin;
114         
115         g_return_if_fail(plugin != NULL);
116         plugin_unload(plugin);
117         set_plugin_list(pluginwindow);
118 }
119
120 static void load_cb(GtkButton *button, PluginWindow *pluginwindow)
121 {
122         gchar *file, *error = NULL;
123
124         file = filesel_select_file_open(_("Select Plugin to load"), PLUGINDIR);
125         if (file == NULL)
126                 return;
127
128         plugin_load(file, &error);
129         if (error != NULL) {
130                 alertpanel_error("The following error occured while loading the plugin:\n%s\n", error);
131                 g_free(error);
132         }
133
134         set_plugin_list(pluginwindow);          
135 }
136
137 void pluginwindow_create()
138 {
139         PluginWindow *pluginwindow;
140         /* ---------------------- code made by glade ---------------------- */
141         GtkWidget *window;
142         GtkWidget *vbox1;
143         GtkWidget *hbox2;
144         GtkWidget *scrolledwindow2;
145         GtkWidget *plugin_list;
146         GtkWidget *label12;
147         GtkWidget *vbox2;
148         GtkWidget *frame2;
149         GtkWidget *label13;
150         GtkWidget *scrolledwindow3;
151         GtkWidget *plugin_desc;
152         GtkWidget *hbuttonbox1;
153         GtkWidget *load_btn;
154         GtkWidget *unload_btn;
155         GtkWidget *close_btn;
156
157         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
158         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
159         gtk_window_set_default_size(GTK_WINDOW(window), 480, 300);
160         gtk_window_set_title(GTK_WINDOW(window), _("Plugins"));
161         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
162
163         vbox1 = gtk_vbox_new(FALSE, 4);
164         gtk_widget_show(vbox1);
165         gtk_container_add(GTK_CONTAINER(window), vbox1);
166
167         hbox2 = gtk_hbox_new(FALSE, 8);
168         gtk_widget_show(hbox2);
169         gtk_box_pack_start(GTK_BOX(vbox1), hbox2, TRUE, TRUE, 0);
170
171         scrolledwindow2 = gtk_scrolled_window_new(NULL, NULL);
172         gtk_widget_show(scrolledwindow2);
173         gtk_box_pack_start(GTK_BOX(hbox2), scrolledwindow2, TRUE, TRUE, 0);
174         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
175                                        (scrolledwindow2), GTK_POLICY_NEVER,
176                                        GTK_POLICY_AUTOMATIC);
177
178         plugin_list = gtk_clist_new(1);
179         gtk_widget_show(plugin_list);
180         gtk_container_add(GTK_CONTAINER(scrolledwindow2), plugin_list);
181         gtk_clist_set_column_width(GTK_CLIST(plugin_list), 0, 80);
182         gtk_clist_column_titles_show(GTK_CLIST(plugin_list));
183
184         label12 = gtk_label_new(_("Plugins"));
185         gtk_widget_show(label12);
186         gtk_clist_set_column_widget(GTK_CLIST(plugin_list), 0, label12);
187         gtk_label_set_justify(GTK_LABEL(label12), GTK_JUSTIFY_LEFT);
188         gtk_misc_set_alignment(GTK_MISC(label12), 0, 0.5);
189
190         vbox2 = gtk_vbox_new(FALSE, 0);
191         gtk_widget_show(vbox2);
192         gtk_box_pack_start(GTK_BOX(hbox2), vbox2, TRUE, TRUE, 0);
193
194         frame2 = gtk_frame_new(NULL);
195         gtk_widget_show(frame2);
196         gtk_box_pack_start(GTK_BOX(vbox2), frame2, FALSE, TRUE, 0);
197
198         label13 = gtk_label_new(_("Description"));
199         gtk_widget_show(label13);
200         gtk_container_add(GTK_CONTAINER(frame2), label13);
201         gtk_misc_set_alignment(GTK_MISC(label13), 0, 0.5);
202         gtk_misc_set_padding(GTK_MISC(label13), 2, 2);
203
204         scrolledwindow3 = gtk_scrolled_window_new(NULL, NULL);
205         gtk_widget_show(scrolledwindow3);
206         gtk_box_pack_start(GTK_BOX(vbox2), scrolledwindow3, TRUE, TRUE, 0);
207         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
208                                        (scrolledwindow3), GTK_POLICY_NEVER,
209                                        GTK_POLICY_AUTOMATIC);
210
211         plugin_desc = gtk_text_view_new();
212         gtk_widget_show(plugin_desc);
213         gtk_container_add(GTK_CONTAINER(scrolledwindow3), plugin_desc);
214
215         hbuttonbox1 = gtk_hbutton_box_new();
216         gtk_widget_show(hbuttonbox1);
217         gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox1, FALSE, FALSE, 0);
218         gtk_button_box_set_layout(GTK_BUTTON_BOX(hbuttonbox1),
219                                   GTK_BUTTONBOX_END);
220         gtk_button_box_set_spacing(GTK_BUTTON_BOX(hbuttonbox1), 0);
221
222         load_btn = gtk_button_new_with_label(_("Load Plugin"));
223         gtk_widget_show(load_btn);
224         gtk_container_add(GTK_CONTAINER(hbuttonbox1), load_btn);
225         GTK_WIDGET_SET_FLAGS(load_btn, GTK_CAN_DEFAULT);
226
227         unload_btn = gtk_button_new_with_label(_("Unload Plugin"));
228         gtk_widget_show(unload_btn);
229         gtk_container_add(GTK_CONTAINER(hbuttonbox1), unload_btn);
230         GTK_WIDGET_SET_FLAGS(unload_btn, GTK_CAN_DEFAULT);
231
232         close_btn = gtk_button_new_with_label(_("Close"));
233         gtk_widget_show(close_btn);
234         gtk_container_add(GTK_CONTAINER(hbuttonbox1), close_btn);
235         GTK_WIDGET_SET_FLAGS(close_btn, GTK_CAN_DEFAULT);
236         /* ----------------------------------------------------------- */
237
238         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(plugin_desc), GTK_WRAP_WORD);
239         gtk_widget_set_sensitive(GTK_WIDGET(unload_btn), FALSE);
240
241         pluginwindow = g_new0(PluginWindow, 1);
242
243         g_signal_connect(G_OBJECT(load_btn), "released",
244                          G_CALLBACK(load_cb), pluginwindow);
245         g_signal_connect(G_OBJECT(unload_btn), "released",
246                          G_CALLBACK(unload_cb), pluginwindow);
247         g_signal_connect(G_OBJECT(close_btn), "released",
248                          G_CALLBACK(close_cb), pluginwindow);
249         g_signal_connect(G_OBJECT(plugin_list), "select-row",
250                          G_CALLBACK(select_row_cb), pluginwindow);
251         g_signal_connect(G_OBJECT(plugin_list), "unselect-row",
252                          G_CALLBACK(unselect_row_cb), pluginwindow);
253
254         pluginwindow->window = window;
255         pluginwindow->plugin_list = plugin_list;
256         pluginwindow->plugin_desc = plugin_desc;
257         pluginwindow->unload_btn = unload_btn;
258         pluginwindow->selected_plugin = NULL;
259
260         set_plugin_list(pluginwindow);
261
262         inc_lock();
263         gtk_widget_show(window);
264 }