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