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