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