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