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