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