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