2007-06-10 [wwp] 2.9.2cvs51
[claws.git] / src / gtk / pluginwindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 "defs.h"
32 #include "plugin.h"
33
34 #include "filesel.h"
35 #include "alertpanel.h"
36 #include "prefs_common.h"
37 #include "../inc.h"
38 #include "manual.h"
39 #include "manage_window.h"
40
41 enum {
42         PLUGINWINDOW_NAME,              /*<! plugin name */
43         PLUGINWINDOW_DATA,              /*<! Plugin pointer */
44         PLUGINWINDOW_STYLE,             /*<! italic if error */
45         N_PLUGINWINDOW_COLUMNS
46 };
47
48 typedef struct _PluginWindow
49 {
50         GtkWidget *window;
51         GtkWidget *plugin_list_view;
52         GtkWidget *plugin_desc;
53         GtkWidget *unload_btn;
54
55         Plugin *selected_plugin;
56 } PluginWindow;
57
58 static GtkListStore* pluginwindow_create_data_store     (void);
59 static GtkWidget *pluginwindow_list_view_create         (PluginWindow *pluginwindow);
60 static void pluginwindow_create_list_view_columns       (GtkWidget *list_view);
61 static gboolean pluginwindow_selected                   (GtkTreeSelection *selector,
62                                                          GtkTreeModel *model, 
63                                                          GtkTreePath *path,
64                                                          gboolean currently_selected,
65                                                          gpointer data);
66
67 static void close_cb(GtkButton *button, PluginWindow *pluginwindow)
68 {
69         gtk_widget_destroy(pluginwindow->window);
70         g_free(pluginwindow);
71         plugin_save_list();
72         inc_unlock();
73 }
74
75 static gint pluginwindow_delete_cb(GtkWidget *widget, GdkEventAny *event,
76                                   PluginWindow *pluginwindow)
77 {
78         close_cb(NULL,pluginwindow);
79         return TRUE;
80 }
81
82 static void set_plugin_list(PluginWindow *pluginwindow)
83 {
84         GSList *plugins, *cur, *unloaded;
85         const gchar *text;
86         GtkListStore *store;
87         GtkTreeIter iter;
88         GtkTextBuffer *textbuf;
89         GtkTextIter start_iter, end_iter;
90         GtkTreeSelection *selection;
91
92         plugins = plugin_get_list();
93         unloaded = plugin_get_unloaded_list();
94
95         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
96                                 (pluginwindow->plugin_list_view)));
97         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store),
98                                              0, GTK_SORT_ASCENDING);
99         gtk_list_store_clear(store);
100         
101         textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pluginwindow->plugin_desc));
102         gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(pluginwindow->plugin_desc), FALSE);
103         gtk_text_view_set_editable(GTK_TEXT_VIEW(pluginwindow->plugin_desc), FALSE);
104         gtk_text_buffer_get_start_iter(textbuf, &start_iter);
105         gtk_text_buffer_get_end_iter(textbuf, &end_iter);
106         gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
107         gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);
108
109         for(cur = plugins; cur != NULL; cur = g_slist_next(cur)) {
110                 Plugin *plugin = (Plugin *) cur->data;
111
112                 gtk_list_store_append(store, &iter);
113                 text = plugin_get_name(plugin); 
114                 gtk_list_store_set(store, &iter,
115                                    PLUGINWINDOW_NAME, text,
116                                    PLUGINWINDOW_DATA, plugin,
117                                    PLUGINWINDOW_STYLE, PANGO_STYLE_NORMAL,
118                                    -1);
119         }
120
121         for(cur = unloaded; cur != NULL; cur = g_slist_next(cur)) {
122                 Plugin *plugin = (Plugin *) cur->data;
123
124                 gtk_list_store_append(store, &iter);
125                 text = plugin_get_name(plugin);
126                 gtk_list_store_set(store, &iter,
127                                    PLUGINWINDOW_NAME, text,
128                                    PLUGINWINDOW_DATA, plugin,
129                                    PLUGINWINDOW_STYLE, PANGO_STYLE_ITALIC,
130                                    -1);
131         }
132
133         if (pluginwindow->selected_plugin == NULL) { 
134                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW
135                                 (pluginwindow->plugin_list_view));
136                 gtk_tree_selection_unselect_all(selection);                             
137         }               
138         g_slist_free(plugins);
139 }
140
141 static void select_row_cb(Plugin *plugin, PluginWindow *pluginwindow)
142 {
143         GtkTextView *plugin_desc = GTK_TEXT_VIEW(pluginwindow->plugin_desc);
144         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(plugin_desc);
145         GtkTextIter start_iter, end_iter;
146         gchar *text;
147
148         pluginwindow->selected_plugin = plugin;
149
150         if (pluginwindow->selected_plugin != NULL) {
151                 const gchar *desc = plugin_get_desc(plugin);
152                 const gchar *err = plugin_get_error(plugin);
153                 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
154                 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
155                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
156                 
157                 if (err == NULL)
158                         text = g_strconcat(desc, _("\n\nVersion: "),
159                                    plugin_get_version(plugin), "\n", NULL);
160                 else
161                         text = g_strconcat(_("Error: "),
162                                    err, "\n", _("Plugin is not functional."), 
163                                    "\n\n", desc, _("\n\nVersion: "),
164                                    plugin_get_version(plugin), "\n", NULL);
165                 gtk_text_buffer_insert(textbuf, &start_iter, text, strlen(text));
166                 g_free(text);
167                 gtk_widget_set_sensitive(pluginwindow->unload_btn, TRUE);
168         } else {
169                 gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);
170         }
171 }
172
173 static void unselect_row_cb(Plugin *plugin, PluginWindow *pluginwindow)
174 {
175         gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);      
176 }
177
178 static void unload_cb(GtkButton *button, PluginWindow *pluginwindow)
179 {
180         Plugin *plugin = pluginwindow->selected_plugin;
181
182         g_return_if_fail(plugin != NULL);
183         plugin_unload(plugin);
184         pluginwindow->selected_plugin = NULL;
185         set_plugin_list(pluginwindow);
186 }
187
188 static void load_cb(GtkButton *button, PluginWindow *pluginwindow)
189 {
190         GList *file_list;
191
192         file_list = filesel_select_multiple_files_open_with_filter(
193                         _("Select the Plugins to load"), get_plugin_dir(), 
194                         "*." G_MODULE_SUFFIX);
195
196         if (file_list) {
197                 GList *tmp;
198
199                 for ( tmp = file_list; tmp; tmp = tmp->next) {
200                         gchar *file, *error = NULL;
201
202                         file = (gchar *) tmp->data;
203                         if (!file) continue;
204                         plugin_load(file, &error);
205                         if (error != NULL) {
206                                 gchar *basename = g_path_get_basename(file);
207                                 alertpanel_error(
208                                 _("The following error occured while loading %s :\n\n%s\n"),
209                                 basename, error);
210                                 g_free(basename);
211                                 g_free(error);
212                         }
213
214                         /* FIXME: globally or atom-ly : ? */
215                         set_plugin_list(pluginwindow);
216                         g_free(file);
217                 }
218
219                 g_list_free(file_list);
220         }               
221 }
222
223 static gboolean pluginwindow_key_pressed(GtkWidget *widget, GdkEventKey *event,
224                                      PluginWindow *pluginwindow)
225 {
226         if (event) {
227                 switch (event->keyval) {
228                         case GDK_Escape : 
229                         case GDK_Return : 
230                         case GDK_KP_Enter :
231                                 close_cb(NULL, pluginwindow);
232                                 break;
233                         case GDK_Insert : 
234                         case GDK_KP_Insert :
235                         case GDK_KP_Add : 
236                         case GDK_plus :
237                                 load_cb(NULL, pluginwindow);
238                                 break;
239                         case GDK_Delete : 
240                         case GDK_KP_Delete :
241                         case GDK_KP_Subtract : 
242                         case GDK_minus :
243                                 unload_cb(NULL, pluginwindow);
244                                 break;
245                         default :
246                                 break;
247                 }
248         }
249         return FALSE;
250 }
251
252 /*!
253  *\brief        Save Gtk object size to prefs dataset
254  */
255 static void pluginwindow_size_allocate_cb(GtkWidget *widget,
256                                          GtkAllocation *allocation)
257 {
258         g_return_if_fail(allocation != NULL);
259
260         prefs_common.pluginswin_width = allocation->width;
261         prefs_common.pluginswin_height = allocation->height;
262 }
263
264
265 void pluginwindow_create()
266 {
267         PluginWindow *pluginwindow;
268         GtkWidget *window;
269         GtkWidget *vbox1;
270         GtkWidget *hbox2;
271         GtkWidget *scrolledwindow2;
272         GtkWidget *plugin_list_view;
273         GtkWidget *vbox2;
274         GtkWidget *frame2;
275         GtkWidget *label13;
276         GtkWidget *scrolledwindow3;
277         GtkWidget *plugin_desc;
278         GtkWidget *hbuttonbox1;
279         GtkWidget *help_btn;
280         GtkWidget *load_btn;
281         GtkWidget *unload_btn;
282         GtkWidget *close_btn;
283         GtkWidget *get_more_btn;
284         GtkWidget *desc_lbl;
285         static GdkGeometry geometry;
286         GtkTooltips *tooltips;
287         
288         debug_print("Creating plugins window...\n");
289
290         pluginwindow = g_new0(PluginWindow, 1);
291
292         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "pluginwindow");
293         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
294         gtk_window_set_title(GTK_WINDOW(window), _("Plugins"));
295         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
296         manage_window_set_transient(GTK_WINDOW(window));
297
298         vbox1 = gtk_vbox_new(FALSE, 4);
299         gtk_widget_show(vbox1);
300         gtk_container_add(GTK_CONTAINER(window), vbox1);
301         gtk_box_set_homogeneous(GTK_BOX(vbox1), FALSE);
302         gtk_widget_realize(window);
303
304         hbox2 = gtk_hbox_new(FALSE, 8);
305         gtk_widget_show(hbox2);
306         gtk_box_pack_start(GTK_BOX(vbox1), hbox2, TRUE, TRUE, 0);
307
308         scrolledwindow2 = gtk_scrolled_window_new(NULL, NULL);
309         gtk_widget_show(scrolledwindow2);
310         gtk_box_pack_start(GTK_BOX(hbox2), scrolledwindow2, FALSE, FALSE, 0);
311         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
312                                        (scrolledwindow2), GTK_POLICY_NEVER,
313                                        GTK_POLICY_AUTOMATIC);
314
315         plugin_list_view = pluginwindow_list_view_create(pluginwindow);
316         gtk_widget_show(plugin_list_view);
317         gtk_container_add(GTK_CONTAINER(scrolledwindow2), plugin_list_view);
318         gtk_widget_grab_focus(GTK_WIDGET(plugin_list_view));
319
320         vbox2 = gtk_vbox_new(FALSE, 0);
321         gtk_widget_show(vbox2);
322         gtk_box_pack_start(GTK_BOX(hbox2), vbox2, TRUE, TRUE, 0);
323
324         frame2 = gtk_frame_new(NULL);
325         gtk_widget_show(frame2);
326         gtk_box_pack_start(GTK_BOX(vbox2), frame2, FALSE, TRUE, 0);
327
328         label13 = gtk_label_new(_("Description"));
329         gtk_widget_show(label13);
330         gtk_container_add(GTK_CONTAINER(frame2), label13);
331         gtk_misc_set_alignment(GTK_MISC(label13), 0, 0.5);
332         gtk_misc_set_padding(GTK_MISC(label13), 2, 2);
333
334         scrolledwindow3 = gtk_scrolled_window_new(NULL, NULL);
335         gtk_widget_show(scrolledwindow3);
336         gtk_box_pack_start(GTK_BOX(vbox2), scrolledwindow3, TRUE, TRUE, 0);
337         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
338                                        (scrolledwindow3), GTK_POLICY_NEVER,
339                                        GTK_POLICY_ALWAYS);
340
341         plugin_desc = gtk_text_view_new();
342         gtk_widget_show(plugin_desc);
343         gtk_container_add(GTK_CONTAINER(scrolledwindow3), plugin_desc);
344
345         desc_lbl = gtk_label_new(_("More plugins are available from the "
346                                    "Claws Mail website."));
347         gtk_misc_set_alignment(GTK_MISC(desc_lbl), 0, 0.5);
348         gtk_widget_show(desc_lbl);
349         gtk_box_pack_start(GTK_BOX(vbox1), desc_lbl, FALSE, FALSE, 0);
350
351         get_more_btn = gtkut_get_link_btn(window, PLUGINS_URI, _("Get more..."));
352         gtk_misc_set_alignment(GTK_MISC(GTK_BIN(get_more_btn)->child), 0, 0.5);
353         gtk_widget_show(get_more_btn);
354         gtk_box_pack_start(GTK_BOX(vbox1), get_more_btn, FALSE, FALSE, 0);
355
356         gtkut_stock_button_set_create_with_help(&hbuttonbox1, &help_btn,
357                         &load_btn, _("Load Plugin..."),
358                         &unload_btn, _("Unload Plugin"),
359                         &close_btn, GTK_STOCK_CLOSE);
360         gtk_box_set_spacing(GTK_BOX(hbuttonbox1), 6);
361         gtk_widget_show(hbuttonbox1);
362         gtk_box_pack_end(GTK_BOX(vbox1), hbuttonbox1, FALSE, FALSE, 0);
363
364         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(plugin_desc), GTK_WRAP_WORD);
365         gtk_widget_set_sensitive(GTK_WIDGET(unload_btn), FALSE);
366
367         g_signal_connect(G_OBJECT(help_btn), "clicked",
368                          G_CALLBACK(manual_open_with_anchor_cb),
369                          MANUAL_ANCHOR_PLUGINS);
370         g_signal_connect(G_OBJECT(load_btn), "clicked",
371                          G_CALLBACK(load_cb), pluginwindow);
372         g_signal_connect(G_OBJECT(unload_btn), "clicked",
373                          G_CALLBACK(unload_cb), pluginwindow);
374         g_signal_connect(G_OBJECT(close_btn), "clicked",
375                          G_CALLBACK(close_cb), pluginwindow);
376         g_signal_connect(G_OBJECT(window), "size_allocate",
377                          G_CALLBACK(pluginwindow_size_allocate_cb), NULL);
378         g_signal_connect(G_OBJECT(window), "key_press_event",
379                            G_CALLBACK(pluginwindow_key_pressed), pluginwindow);
380         g_signal_connect(G_OBJECT(window), "delete_event",
381                          G_CALLBACK(pluginwindow_delete_cb), pluginwindow);
382
383         tooltips = gtk_tooltips_new();
384
385         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips),
386                         load_btn,
387                         _("Click here to load one or more plugins"), NULL);
388
389         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips),
390                         unload_btn,
391                         _("Unload the selected plugin"), NULL);
392
393         pluginwindow->window = window;
394         pluginwindow->plugin_list_view = plugin_list_view;
395         pluginwindow->plugin_desc = plugin_desc;
396         pluginwindow->unload_btn = unload_btn;
397         pluginwindow->selected_plugin = NULL;
398
399         set_plugin_list(pluginwindow);
400
401         inc_lock();
402
403         if (!geometry.min_height) {
404                 geometry.min_width = 480;
405                 geometry.min_height = 300;
406         }
407
408         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
409                                       GDK_HINT_MIN_SIZE);
410         gtk_widget_set_size_request(window, prefs_common.pluginswin_width,
411                                     prefs_common.pluginswin_height);
412
413         gtk_widget_show(window);
414 }
415
416 static GtkListStore* pluginwindow_create_data_store(void)
417 {
418         return gtk_list_store_new(N_PLUGINWINDOW_COLUMNS,
419                                   G_TYPE_STRING,        
420                                   G_TYPE_POINTER,
421                                   PANGO_TYPE_STYLE,
422                                   -1);
423 }
424
425 static GtkWidget *pluginwindow_list_view_create(PluginWindow *pluginwindow)
426 {
427         GtkTreeView *list_view;
428         GtkTreeSelection *selector;
429         GtkTreeModel *model;
430
431         model = GTK_TREE_MODEL(pluginwindow_create_data_store());
432         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
433         g_object_unref(model);  
434
435         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
436         gtk_tree_view_set_search_column (list_view, 0);
437
438         selector = gtk_tree_view_get_selection(list_view);
439         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
440         gtk_tree_selection_set_select_function(selector, pluginwindow_selected,
441                                                pluginwindow, NULL);
442
443         /* create the columns */
444         pluginwindow_create_list_view_columns(GTK_WIDGET(list_view));
445
446         return GTK_WIDGET(list_view);
447 }
448
449 static void pluginwindow_create_list_view_columns(GtkWidget *list_view)
450 {
451         GtkTreeViewColumn *column;
452         GtkCellRenderer *renderer;
453
454         renderer = gtk_cell_renderer_text_new();
455         column = gtk_tree_view_column_new_with_attributes
456                 (_("Loaded plugins"),
457                  renderer,
458                  "text", PLUGINWINDOW_NAME,
459                  "style", PLUGINWINDOW_STYLE,
460                  NULL);
461         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
462 }
463
464 static gboolean pluginwindow_selected(GtkTreeSelection *selector,
465                                       GtkTreeModel *model, 
466                                       GtkTreePath *path,
467                                       gboolean currently_selected,
468                                       gpointer data)
469 {
470         GtkTreeIter iter;
471         Plugin *plugin;
472         
473         if (!gtk_tree_model_get_iter(model, &iter, path))
474                 return TRUE;
475
476         gtk_tree_model_get(model, &iter, 
477                            PLUGINWINDOW_DATA, &plugin,
478                            -1);
479
480         if (currently_selected) 
481                 unselect_row_cb(plugin, data);
482         else
483                 select_row_cb(plugin, data);
484
485         return TRUE;
486 }
487