53b0153fda9584e4f6b5c95c86a2622de2cee4da
[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 } 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 occurred 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 *hbuttonbox2;
280         GtkWidget *help_btn;
281         GtkWidget *load_btn;
282         GtkWidget *unload_btn;
283         GtkWidget *close_btn;
284         GtkWidget *get_more_btn;
285         GtkWidget *desc_lbl;
286         GtkWidget *vbox3;
287         GtkWidget *hbox_info;
288         static GdkGeometry geometry;
289         GtkTooltips *tooltips;
290         
291         debug_print("Creating plugins window...\n");
292
293         pluginwindow = g_new0(PluginWindow, 1);
294
295         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "pluginwindow");
296         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
297         gtk_window_set_title(GTK_WINDOW(window), _("Plugins"));
298         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
299         manage_window_set_transient(GTK_WINDOW(window));
300
301         vbox1 = gtk_vbox_new(FALSE, 4);
302         gtk_widget_show(vbox1);
303         gtk_container_add(GTK_CONTAINER(window), vbox1);
304         gtk_box_set_homogeneous(GTK_BOX(vbox1), FALSE);
305         gtk_widget_realize(window);
306
307         hbox2 = gtk_hbox_new(FALSE, 8);
308         gtk_widget_show(hbox2);
309         gtk_box_pack_start(GTK_BOX(vbox1), hbox2, TRUE, TRUE, 0);
310
311         vbox3 = gtk_vbox_new(FALSE, 4);
312         gtk_widget_show(vbox3);
313         gtk_box_pack_start(GTK_BOX(hbox2), vbox3, FALSE, FALSE, 0);
314
315         scrolledwindow2 = gtk_scrolled_window_new(NULL, NULL);
316         gtk_widget_show(scrolledwindow2);
317         gtk_box_pack_start(GTK_BOX(vbox3), scrolledwindow2, TRUE, TRUE, 0);
318         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow2),
319                                         GTK_SHADOW_ETCHED_IN);
320         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
321                                        (scrolledwindow2), GTK_POLICY_NEVER,
322                                        GTK_POLICY_AUTOMATIC);
323
324         plugin_list_view = pluginwindow_list_view_create(pluginwindow);
325         gtk_widget_show(plugin_list_view);
326         gtk_container_add(GTK_CONTAINER(scrolledwindow2), plugin_list_view);
327         gtk_widget_grab_focus(GTK_WIDGET(plugin_list_view));
328
329         gtkut_stock_button_set_create(&hbuttonbox1,
330                                 &load_btn, _("Load..."),
331                                 &unload_btn, _("Unload"),
332                                 NULL, NULL);
333         gtk_widget_show(hbuttonbox1);
334         gtk_box_pack_start(GTK_BOX(vbox3), hbuttonbox1, FALSE, FALSE, 0);
335         
336         vbox2 = gtk_vbox_new(FALSE, 0);
337         gtk_widget_show(vbox2);
338         gtk_box_pack_start(GTK_BOX(hbox2), vbox2, TRUE, TRUE, 0);
339
340         frame2 = gtk_frame_new(NULL);
341         gtk_widget_show(frame2);
342         gtk_box_pack_start(GTK_BOX(vbox2), frame2, FALSE, TRUE, 0);
343
344         label13 = gtk_label_new(_("Description"));
345         gtk_widget_show(label13);
346         gtk_container_add(GTK_CONTAINER(frame2), label13);
347         gtk_misc_set_alignment(GTK_MISC(label13), 0, 0.5);
348         gtk_misc_set_padding(GTK_MISC(label13), 2, 2);
349
350         scrolledwindow3 = gtk_scrolled_window_new(NULL, NULL);
351         gtk_widget_show(scrolledwindow3);
352         gtk_box_pack_start(GTK_BOX(vbox2), scrolledwindow3, TRUE, TRUE, 0);
353         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow3),
354                                         GTK_SHADOW_ETCHED_IN);
355         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
356                                        (scrolledwindow3), GTK_POLICY_NEVER,
357                                        GTK_POLICY_ALWAYS);
358
359         plugin_desc = gtk_text_view_new();
360         gtk_widget_show(plugin_desc);
361         gtk_container_add(GTK_CONTAINER(scrolledwindow3), plugin_desc);
362
363         hbox_info = gtk_hbox_new(FALSE, 5);
364         gtk_widget_show(hbox_info);
365         
366         desc_lbl = gtk_label_new(_("More plugins are available from the "
367                                    "Claws Mail website."));
368         gtk_misc_set_alignment(GTK_MISC(desc_lbl), 0, 0.5);
369         gtk_widget_show(desc_lbl);
370         gtk_box_pack_start(GTK_BOX(hbox_info), desc_lbl, FALSE, FALSE, 0);
371
372         get_more_btn = gtkut_get_link_btn(window, PLUGINS_URI, _("Get more..."));
373         gtk_misc_set_alignment(GTK_MISC(GTK_BIN(get_more_btn)->child), 0, 0.5);
374         gtk_widget_show(get_more_btn);
375         gtk_box_pack_start(GTK_BOX(hbox_info), get_more_btn, FALSE, FALSE, 0);
376         gtk_box_pack_start(GTK_BOX(hbox_info), gtk_label_new(""), TRUE, TRUE, 0);
377         gtk_box_pack_start(GTK_BOX(vbox1), hbox_info, FALSE, FALSE, 0);
378
379         gtkut_stock_button_set_create_with_help(&hbuttonbox2, &help_btn,
380                         &close_btn, GTK_STOCK_CLOSE,
381                         NULL, NULL, NULL, NULL);
382
383         gtk_box_set_spacing(GTK_BOX(hbuttonbox2), 6);
384         gtk_widget_show(hbuttonbox2);
385         gtk_box_pack_end(GTK_BOX(vbox1), hbuttonbox2, FALSE, FALSE, 0);
386
387         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(plugin_desc), GTK_WRAP_WORD);
388         gtk_widget_set_sensitive(GTK_WIDGET(unload_btn), FALSE);
389
390         g_signal_connect(G_OBJECT(help_btn), "clicked",
391                          G_CALLBACK(manual_open_with_anchor_cb),
392                          MANUAL_ANCHOR_PLUGINS);
393         g_signal_connect(G_OBJECT(load_btn), "clicked",
394                          G_CALLBACK(load_cb), pluginwindow);
395         g_signal_connect(G_OBJECT(unload_btn), "clicked",
396                          G_CALLBACK(unload_cb), pluginwindow);
397         g_signal_connect(G_OBJECT(close_btn), "clicked",
398                          G_CALLBACK(close_cb), pluginwindow);
399         g_signal_connect(G_OBJECT(window), "size_allocate",
400                          G_CALLBACK(pluginwindow_size_allocate_cb), NULL);
401         g_signal_connect(G_OBJECT(window), "key_press_event",
402                            G_CALLBACK(pluginwindow_key_pressed), pluginwindow);
403         g_signal_connect(G_OBJECT(window), "delete_event",
404                          G_CALLBACK(pluginwindow_delete_cb), pluginwindow);
405
406         tooltips = gtk_tooltips_new();
407
408         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips),
409                         load_btn,
410                         _("Click here to load one or more plugins"), NULL);
411
412         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips),
413                         unload_btn,
414                         _("Unload the selected plugin"), NULL);
415
416         pluginwindow->window = window;
417         pluginwindow->plugin_list_view = plugin_list_view;
418         pluginwindow->plugin_desc = plugin_desc;
419         pluginwindow->unload_btn = unload_btn;
420         pluginwindow->selected_plugin = NULL;
421
422         set_plugin_list(pluginwindow);
423
424         inc_lock();
425
426         if (!geometry.min_height) {
427                 geometry.min_width = -1;
428                 geometry.min_height = 300;
429         }
430
431         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
432                                       GDK_HINT_MIN_SIZE);
433         gtk_window_set_default_size(GTK_WINDOW(window), prefs_common.pluginswin_width,
434                                     prefs_common.pluginswin_height);
435
436         gtk_widget_show(window);
437 }
438
439 static GtkListStore* pluginwindow_create_data_store(void)
440 {
441         return gtk_list_store_new(N_PLUGINWINDOW_COLUMNS,
442                                   G_TYPE_STRING,        
443                                   G_TYPE_POINTER,
444                                   PANGO_TYPE_STYLE,
445                                   -1);
446 }
447
448 static GtkWidget *pluginwindow_list_view_create(PluginWindow *pluginwindow)
449 {
450         GtkTreeView *list_view;
451         GtkTreeSelection *selector;
452         GtkTreeModel *model;
453
454         model = GTK_TREE_MODEL(pluginwindow_create_data_store());
455         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
456         g_object_unref(model);  
457
458         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
459         gtk_tree_view_set_search_column (list_view, 0);
460
461         selector = gtk_tree_view_get_selection(list_view);
462         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
463         gtk_tree_selection_set_select_function(selector, pluginwindow_selected,
464                                                pluginwindow, NULL);
465
466         /* create the columns */
467         pluginwindow_create_list_view_columns(GTK_WIDGET(list_view));
468
469         return GTK_WIDGET(list_view);
470 }
471
472 static void pluginwindow_create_list_view_columns(GtkWidget *list_view)
473 {
474         GtkTreeViewColumn *column;
475         GtkCellRenderer *renderer;
476
477         renderer = gtk_cell_renderer_text_new();
478         column = gtk_tree_view_column_new_with_attributes
479                 (_("Loaded plugins"),
480                  renderer,
481                  "text", PLUGINWINDOW_NAME,
482                  "style", PLUGINWINDOW_STYLE,
483                  NULL);
484         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
485 }
486
487 static gboolean pluginwindow_selected(GtkTreeSelection *selector,
488                                       GtkTreeModel *model, 
489                                       GtkTreePath *path,
490                                       gboolean currently_selected,
491                                       gpointer data)
492 {
493         GtkTreeIter iter;
494         Plugin *plugin;
495         
496         if (!gtk_tree_model_get_iter(model, &iter, path))
497                 return TRUE;
498
499         gtk_tree_model_get(model, &iter, 
500                            PLUGINWINDOW_DATA, &plugin,
501                            -1);
502
503         if (currently_selected) 
504                 unselect_row_cb(plugin, data);
505         else
506                 select_row_cb(plugin, data);
507
508         return TRUE;
509 }
510