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