2004-12-31 [paul] 0.9.13cvs23.1
[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 enum {
37         PLUGINWINDOW_NAME,              /*<! plugin name */
38         PLUGINWINDOW_DATA,              /*<! Plugin pointer */
39         N_PLUGINWINDOW_COLUMNS
40 };
41
42 typedef struct _PluginWindow
43 {
44         GtkWidget *window;
45         GtkWidget *plugin_list_view;
46         GtkWidget *plugin_desc;
47         GtkWidget *unload_btn;
48
49         Plugin *selected_plugin;
50 } PluginWindow;
51
52 static GtkListStore* pluginwindow_create_data_store     (void);
53 static GtkWidget *pluginwindow_list_view_create         (PluginWindow *pluginwindow);
54 static void pluginwindow_create_list_view_columns       (GtkWidget *list_view);
55 static gboolean pluginwindow_selected                   (GtkTreeSelection *selector,
56                                                          GtkTreeModel *model, 
57                                                          GtkTreePath *path,
58                                                          gboolean currently_selected,
59                                                          gpointer data);
60
61 static void close_cb(GtkButton *button, PluginWindow *pluginwindow)
62 {
63         gtk_widget_destroy(pluginwindow->window);
64         g_free(pluginwindow);
65         plugin_save_list();
66         inc_unlock();
67 }
68
69 static void set_plugin_list(PluginWindow *pluginwindow)
70 {
71         GSList *plugins, *cur;
72         const gchar *text;
73         gint row;
74         GtkListStore *store;
75         GtkTreeIter iter;
76         GtkTextBuffer *textbuf;
77         GtkTextIter start_iter, end_iter;
78         GtkTreeSelection *selection;
79
80         plugins = plugin_get_list();
81
82         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
83                                 (pluginwindow->plugin_list_view)));
84         gtk_list_store_clear(store);
85         
86         textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pluginwindow->plugin_desc));
87         gtk_text_buffer_get_start_iter(textbuf, &start_iter);
88         gtk_text_buffer_get_end_iter(textbuf, &end_iter);
89         gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
90         gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);
91
92         for(cur = plugins; cur != NULL; cur = g_slist_next(cur)) {
93                 Plugin *plugin = (Plugin *) cur->data;
94
95                 gtk_list_store_append(store, &iter);
96                 text = plugin_get_name(plugin); 
97                 gtk_list_store_set(store, &iter,
98                                    PLUGINWINDOW_NAME, text,
99                                    PLUGINWINDOW_DATA, plugin,
100                                    -1);
101         }
102
103         if (pluginwindow->selected_plugin == NULL) { 
104                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW
105                                 (pluginwindow->plugin_list_view));
106                 gtk_tree_selection_unselect_all(selection);                             
107         }               
108         g_slist_free(plugins);
109 }
110
111 static void select_row_cb(Plugin *plugin, PluginWindow *pluginwindow)
112 {
113         GtkTextView *plugin_desc = GTK_TEXT_VIEW(pluginwindow->plugin_desc);
114         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(plugin_desc);
115         GtkTextIter start_iter, end_iter;
116         const gchar *text;
117
118         pluginwindow->selected_plugin = plugin;
119
120         if (pluginwindow->selected_plugin != NULL) {
121                 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
122                 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
123                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
124                 text = plugin_get_desc(plugin);
125                 gtk_text_buffer_insert(textbuf, &start_iter, text, strlen(text));
126                 gtk_widget_set_sensitive(pluginwindow->unload_btn, TRUE);
127         } else {
128                 gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);
129         }
130 }
131
132 static void unselect_row_cb(Plugin *plugin, PluginWindow *pluginwindow)
133 {
134         gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);      
135 }
136
137 static void unload_cb(GtkButton *button, PluginWindow *pluginwindow)
138 {
139         Plugin *plugin = pluginwindow->selected_plugin;
140
141         g_return_if_fail(plugin != NULL);
142         plugin_unload(plugin);
143         pluginwindow->selected_plugin = NULL;
144         set_plugin_list(pluginwindow);
145 }
146
147 static void load_cb(GtkButton *button, PluginWindow *pluginwindow)
148 {
149         gchar *file, *error = NULL;
150
151         file = filesel_select_file_open(_("Select Plugin to load"), PLUGINDIR);
152         if (file == NULL)
153                 return;
154
155         plugin_load(file, &error);
156         if (error != NULL) {
157                 alertpanel_error("The following error occured while loading the plugin:\n%s\n", error);
158                 g_free(error);
159         }
160
161         set_plugin_list(pluginwindow);
162         g_free(file);
163 }
164
165 static gboolean pluginwindow_key_pressed(GtkWidget *widget, GdkEventKey *event,
166                                      PluginWindow *pluginwindow)
167 {
168         if (event) {
169                 switch (event->keyval) {
170                         case GDK_Escape : 
171                         case GDK_Return : 
172                         case GDK_KP_Enter :
173                                 close_cb(NULL, pluginwindow);
174                                 break;
175                         case GDK_Insert : 
176                         case GDK_KP_Insert :
177                         case GDK_KP_Add : 
178                         case GDK_plus :
179                                 load_cb(NULL, pluginwindow);
180                                 break;
181                         case GDK_Delete : 
182                         case GDK_KP_Delete :
183                         case GDK_KP_Subtract : 
184                         case GDK_minus :
185                                 unload_cb(NULL, pluginwindow);
186                                 break;
187                         default :
188                                 break;
189                 }
190         }
191         return FALSE;
192 }
193
194 void pluginwindow_create()
195 {
196         PluginWindow *pluginwindow;
197         /* ---------------------- code made by glade ---------------------- */
198         GtkWidget *window;
199         GtkWidget *vbox1;
200         GtkWidget *hbox2;
201         GtkWidget *scrolledwindow2;
202         GtkWidget *plugin_list_view;
203         GtkWidget *vbox2;
204         GtkWidget *frame2;
205         GtkWidget *label13;
206         GtkWidget *scrolledwindow3;
207         GtkWidget *plugin_desc;
208         GtkWidget *hbuttonbox1;
209         GtkWidget *load_btn;
210         GtkWidget *unload_btn;
211         GtkWidget *close_btn;
212         
213         pluginwindow = g_new0(PluginWindow, 1);
214
215         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
216         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
217         gtk_window_set_default_size(GTK_WINDOW(window), 480, 300);
218         gtk_window_set_title(GTK_WINDOW(window), _("Plugins"));
219         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
220
221         vbox1 = gtk_vbox_new(FALSE, 4);
222         gtk_widget_show(vbox1);
223         gtk_container_add(GTK_CONTAINER(window), vbox1);
224
225         hbox2 = gtk_hbox_new(FALSE, 8);
226         gtk_widget_show(hbox2);
227         gtk_box_pack_start(GTK_BOX(vbox1), hbox2, TRUE, TRUE, 0);
228
229         scrolledwindow2 = gtk_scrolled_window_new(NULL, NULL);
230         gtk_widget_show(scrolledwindow2);
231         gtk_box_pack_start(GTK_BOX(hbox2), scrolledwindow2, FALSE, FALSE, 0);
232         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
233                                        (scrolledwindow2), GTK_POLICY_NEVER,
234                                        GTK_POLICY_AUTOMATIC);
235
236         plugin_list_view = pluginwindow_list_view_create(pluginwindow);
237         gtk_widget_show(plugin_list_view);
238         gtk_container_add(GTK_CONTAINER(scrolledwindow2), plugin_list_view);
239         gtk_widget_grab_focus(GTK_WIDGET(plugin_list_view));
240
241         vbox2 = gtk_vbox_new(FALSE, 0);
242         gtk_widget_show(vbox2);
243         gtk_box_pack_start(GTK_BOX(hbox2), vbox2, TRUE, TRUE, 0);
244
245         frame2 = gtk_frame_new(NULL);
246         gtk_widget_show(frame2);
247         gtk_box_pack_start(GTK_BOX(vbox2), frame2, FALSE, TRUE, 0);
248
249         label13 = gtk_label_new(_("Description"));
250         gtk_widget_show(label13);
251         gtk_container_add(GTK_CONTAINER(frame2), label13);
252         gtk_misc_set_alignment(GTK_MISC(label13), 0, 0.5);
253         gtk_misc_set_padding(GTK_MISC(label13), 2, 2);
254
255         scrolledwindow3 = gtk_scrolled_window_new(NULL, NULL);
256         gtk_widget_show(scrolledwindow3);
257         gtk_box_pack_start(GTK_BOX(vbox2), scrolledwindow3, TRUE, TRUE, 0);
258         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
259                                        (scrolledwindow3), GTK_POLICY_NEVER,
260                                        GTK_POLICY_ALWAYS);
261
262         plugin_desc = gtk_text_view_new();
263         gtk_widget_show(plugin_desc);
264         gtk_container_add(GTK_CONTAINER(scrolledwindow3), plugin_desc);
265
266         hbuttonbox1 = gtk_hbutton_box_new();
267         gtk_widget_show(hbuttonbox1);
268         gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox1, FALSE, FALSE, 0);
269         gtk_button_box_set_layout(GTK_BUTTON_BOX(hbuttonbox1),
270                                   GTK_BUTTONBOX_END);
271         gtk_button_box_set_spacing(GTK_BUTTON_BOX(hbuttonbox1), 0);
272
273         load_btn = gtk_button_new_with_label(_("Load Plugin"));
274         gtk_widget_show(load_btn);
275         gtk_container_add(GTK_CONTAINER(hbuttonbox1), load_btn);
276         GTK_WIDGET_SET_FLAGS(load_btn, GTK_CAN_DEFAULT);
277
278         unload_btn = gtk_button_new_with_label(_("Unload Plugin"));
279         gtk_widget_show(unload_btn);
280         gtk_container_add(GTK_CONTAINER(hbuttonbox1), unload_btn);
281         GTK_WIDGET_SET_FLAGS(unload_btn, GTK_CAN_DEFAULT);
282
283         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
284         gtk_widget_show(close_btn);
285         gtk_container_add(GTK_CONTAINER(hbuttonbox1), close_btn);
286         GTK_WIDGET_SET_FLAGS(close_btn, GTK_CAN_DEFAULT);
287         /* ----------------------------------------------------------- */
288
289         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(plugin_desc), GTK_WRAP_WORD);
290         gtk_widget_set_sensitive(GTK_WIDGET(unload_btn), FALSE);
291
292
293         g_signal_connect(G_OBJECT(load_btn), "released",
294                          G_CALLBACK(load_cb), pluginwindow);
295         g_signal_connect(G_OBJECT(unload_btn), "released",
296                          G_CALLBACK(unload_cb), pluginwindow);
297         g_signal_connect(G_OBJECT(close_btn), "released",
298                          G_CALLBACK(close_cb), pluginwindow);
299         g_signal_connect(G_OBJECT(window), "key_press_event",
300                            G_CALLBACK(pluginwindow_key_pressed), pluginwindow);
301
302         pluginwindow->window = window;
303         pluginwindow->plugin_list_view = plugin_list_view;
304         pluginwindow->plugin_desc = plugin_desc;
305         pluginwindow->unload_btn = unload_btn;
306         pluginwindow->selected_plugin = NULL;
307
308         set_plugin_list(pluginwindow);
309
310         inc_lock();
311         gtk_widget_show(window);
312 }
313
314 static GtkListStore* pluginwindow_create_data_store(void)
315 {
316         return gtk_list_store_new(N_PLUGINWINDOW_COLUMNS,
317                                   G_TYPE_STRING,        
318                                   G_TYPE_POINTER,
319                                   -1);
320 }
321
322 static GtkWidget *pluginwindow_list_view_create(PluginWindow *pluginwindow)
323 {
324         GtkTreeView *list_view;
325         GtkTreeSelection *selector;
326         GtkTreeModel *model;
327
328         model = GTK_TREE_MODEL(pluginwindow_create_data_store());
329         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
330         g_object_unref(model);  
331         
332         gtk_tree_view_set_rules_hint(list_view, TRUE);
333         
334         selector = gtk_tree_view_get_selection(list_view);
335         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
336         gtk_tree_selection_set_select_function(selector, pluginwindow_selected,
337                                                pluginwindow, NULL);
338
339         /* create the columns */
340         pluginwindow_create_list_view_columns(GTK_WIDGET(list_view));
341
342         return GTK_WIDGET(list_view);
343 }
344
345 static void pluginwindow_create_list_view_columns(GtkWidget *list_view)
346 {
347         GtkTreeViewColumn *column;
348         GtkCellRenderer *renderer;
349
350         renderer = gtk_cell_renderer_text_new();
351         column = gtk_tree_view_column_new_with_attributes
352                 (_("Plugins"),
353                  renderer,
354                  "text", PLUGINWINDOW_NAME,
355                  NULL);
356         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
357 }
358
359 static gboolean pluginwindow_selected(GtkTreeSelection *selector,
360                                       GtkTreeModel *model, 
361                                       GtkTreePath *path,
362                                       gboolean currently_selected,
363                                       gpointer data)
364 {
365         GtkTreeIter iter;
366         Plugin *plugin;
367         
368         if (!gtk_tree_model_get_iter(model, &iter, path))
369                 return TRUE;
370
371         gtk_tree_model_get(model, &iter, 
372                            PLUGINWINDOW_DATA, &plugin,
373                            -1);
374
375         if (currently_selected) 
376                 unselect_row_cb(plugin, data);
377         else
378                 select_row_cb(plugin, data);
379
380         return TRUE;
381 }
382