343d130e10b708cde8014c81572b5491345172ce
[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
40 enum {
41         PLUGINWINDOW_NAME,              /*<! plugin name */
42         PLUGINWINDOW_DATA,              /*<! Plugin pointer */
43         PLUGINWINDOW_STYLE,             /*<! italic if error */
44         N_PLUGINWINDOW_COLUMNS
45 };
46
47 typedef struct _PluginWindow
48 {
49         GtkWidget *window;
50         GtkWidget *plugin_list_view;
51         GtkWidget *plugin_desc;
52         GtkWidget *unload_btn;
53
54         Plugin *selected_plugin;
55 } PluginWindow;
56
57 static GtkListStore* pluginwindow_create_data_store     (void);
58 static GtkWidget *pluginwindow_list_view_create         (PluginWindow *pluginwindow);
59 static void pluginwindow_create_list_view_columns       (GtkWidget *list_view);
60 static gboolean pluginwindow_selected                   (GtkTreeSelection *selector,
61                                                          GtkTreeModel *model, 
62                                                          GtkTreePath *path,
63                                                          gboolean currently_selected,
64                                                          gpointer data);
65
66 static void close_cb(GtkButton *button, PluginWindow *pluginwindow)
67 {
68         gtk_widget_destroy(pluginwindow->window);
69         g_free(pluginwindow);
70         plugin_save_list();
71         inc_unlock();
72 }
73
74 static void set_plugin_list(PluginWindow *pluginwindow)
75 {
76         GSList *plugins, *cur, *unloaded;
77         const gchar *text;
78         GtkListStore *store;
79         GtkTreeIter iter;
80         GtkTextBuffer *textbuf;
81         GtkTextIter start_iter, end_iter;
82         GtkTreeSelection *selection;
83
84         plugins = plugin_get_list();
85         unloaded = plugin_get_unloaded_list();
86
87         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
88                                 (pluginwindow->plugin_list_view)));
89         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store),
90                                              0, GTK_SORT_ASCENDING);
91         gtk_list_store_clear(store);
92         
93         textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pluginwindow->plugin_desc));
94         gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(pluginwindow->plugin_desc), FALSE);
95         gtk_text_view_set_editable(GTK_TEXT_VIEW(pluginwindow->plugin_desc), FALSE);
96         gtk_text_buffer_get_start_iter(textbuf, &start_iter);
97         gtk_text_buffer_get_end_iter(textbuf, &end_iter);
98         gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
99         gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);
100
101         for(cur = plugins; cur != NULL; cur = g_slist_next(cur)) {
102                 Plugin *plugin = (Plugin *) cur->data;
103
104                 gtk_list_store_append(store, &iter);
105                 text = plugin_get_name(plugin); 
106                 gtk_list_store_set(store, &iter,
107                                    PLUGINWINDOW_NAME, text,
108                                    PLUGINWINDOW_DATA, plugin,
109                                    PLUGINWINDOW_STYLE, PANGO_STYLE_NORMAL,
110                                    -1);
111         }
112
113         for(cur = unloaded; cur != NULL; cur = g_slist_next(cur)) {
114                 Plugin *plugin = (Plugin *) cur->data;
115
116                 gtk_list_store_append(store, &iter);
117                 text = plugin_get_name(plugin);
118                 gtk_list_store_set(store, &iter,
119                                    PLUGINWINDOW_NAME, text,
120                                    PLUGINWINDOW_DATA, plugin,
121                                    PLUGINWINDOW_STYLE, PANGO_STYLE_ITALIC,
122                                    -1);
123         }
124
125         if (pluginwindow->selected_plugin == NULL) { 
126                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW
127                                 (pluginwindow->plugin_list_view));
128                 gtk_tree_selection_unselect_all(selection);                             
129         }               
130         g_slist_free(plugins);
131 }
132
133 static void select_row_cb(Plugin *plugin, PluginWindow *pluginwindow)
134 {
135         GtkTextView *plugin_desc = GTK_TEXT_VIEW(pluginwindow->plugin_desc);
136         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(plugin_desc);
137         GtkTextIter start_iter, end_iter;
138         gchar *text;
139
140         pluginwindow->selected_plugin = plugin;
141
142         if (pluginwindow->selected_plugin != NULL) {
143                 const gchar *desc = plugin_get_desc(plugin);
144                 const gchar *err = plugin_get_error(plugin);
145                 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
146                 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
147                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
148                 
149                 if (err == NULL)
150                         text = g_strconcat(desc, _("\n\nVersion: "),
151                                    plugin_get_version(plugin), "\n", NULL);
152                 else
153                         text = g_strconcat(_("Error: "),
154                                    err, "\n", _("Plugin is not functional."), 
155                                    "\n\n", desc, _("\n\nVersion: "),
156                                    plugin_get_version(plugin), "\n", NULL);
157                 gtk_text_buffer_insert(textbuf, &start_iter, text, strlen(text));
158                 g_free(text);
159                 gtk_widget_set_sensitive(pluginwindow->unload_btn, TRUE);
160         } else {
161                 gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);
162         }
163 }
164
165 static void unselect_row_cb(Plugin *plugin, PluginWindow *pluginwindow)
166 {
167         gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);      
168 }
169
170 static void unload_cb(GtkButton *button, PluginWindow *pluginwindow)
171 {
172         Plugin *plugin = pluginwindow->selected_plugin;
173
174         g_return_if_fail(plugin != NULL);
175         plugin_unload(plugin);
176         pluginwindow->selected_plugin = NULL;
177         set_plugin_list(pluginwindow);
178 }
179
180 static void load_cb(GtkButton *button, PluginWindow *pluginwindow)
181 {
182         GList *file_list;
183
184         file_list = filesel_select_multiple_files_open_with_filter(
185                         _("Select the Plugins to load"), get_plugin_dir(), 
186                         "*." G_MODULE_SUFFIX);
187
188         if (file_list) {
189                 GList *tmp;
190
191                 for ( tmp = file_list; tmp; tmp = tmp->next) {
192                         gchar *file, *error = NULL;
193
194                         file = (gchar *) tmp->data;
195                         if (!file) continue;
196                         plugin_load(file, &error);
197                         if (error != NULL) {
198                                 gchar *basename = g_path_get_basename(file);
199                                 alertpanel_error(
200                                 _("The following error occured while loading %s :\n\n%s\n"),
201                                 basename, error);
202                                 g_free(basename);
203                                 g_free(error);
204                         }
205
206                         /* FIXME: globally or atom-ly : ? */
207                         set_plugin_list(pluginwindow);
208                         g_free(file);
209                 }
210
211                 g_list_free(file_list);
212         }               
213 }
214
215 static gboolean pluginwindow_key_pressed(GtkWidget *widget, GdkEventKey *event,
216                                      PluginWindow *pluginwindow)
217 {
218         if (event) {
219                 switch (event->keyval) {
220                         case GDK_Escape : 
221                         case GDK_Return : 
222                         case GDK_KP_Enter :
223                                 close_cb(NULL, pluginwindow);
224                                 break;
225                         case GDK_Insert : 
226                         case GDK_KP_Insert :
227                         case GDK_KP_Add : 
228                         case GDK_plus :
229                                 load_cb(NULL, pluginwindow);
230                                 break;
231                         case GDK_Delete : 
232                         case GDK_KP_Delete :
233                         case GDK_KP_Subtract : 
234                         case GDK_minus :
235                                 unload_cb(NULL, pluginwindow);
236                                 break;
237                         default :
238                                 break;
239                 }
240         }
241         return FALSE;
242 }
243
244 /*!
245  *\brief        Save Gtk object size to prefs dataset
246  */
247 static void pluginwindow_size_allocate_cb(GtkWidget *widget,
248                                          GtkAllocation *allocation)
249 {
250         g_return_if_fail(allocation != NULL);
251
252         prefs_common.pluginswin_width = allocation->width;
253         prefs_common.pluginswin_height = allocation->height;
254 }
255
256
257 void pluginwindow_create()
258 {
259         PluginWindow *pluginwindow;
260         GtkWidget *window;
261         GtkWidget *vbox1;
262         GtkWidget *hbox2;
263         GtkWidget *scrolledwindow2;
264         GtkWidget *plugin_list_view;
265         GtkWidget *vbox2;
266         GtkWidget *frame2;
267         GtkWidget *label13;
268         GtkWidget *scrolledwindow3;
269         GtkWidget *plugin_desc;
270         GtkWidget *hbuttonbox1, *hbox3;
271         GtkWidget *help_btn;
272         GtkWidget *load_btn;
273         GtkWidget *unload_btn;
274         GtkWidget *close_btn;
275         GtkWidget *get_more_btn;
276         GtkWidget *desc_lbl;
277         static GdkGeometry geometry;
278         GtkTooltips *tooltips;
279         
280         debug_print("Creating plugins window...\n");
281
282         pluginwindow = g_new0(PluginWindow, 1);
283
284         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
285         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
286         gtk_window_set_title(GTK_WINDOW(window), _("Plugins"));
287         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
288         manage_window_set_transient(window);
289
290         vbox1 = gtk_vbox_new(FALSE, 4);
291         gtk_widget_show(vbox1);
292         gtk_container_add(GTK_CONTAINER(window), vbox1);
293         gtk_box_set_homogeneous(GTK_BOX(vbox1), FALSE);
294         gtk_widget_realize(window);
295
296         hbox2 = gtk_hbox_new(FALSE, 8);
297         gtk_widget_show(hbox2);
298         gtk_box_pack_start(GTK_BOX(vbox1), hbox2, TRUE, TRUE, 0);
299
300         scrolledwindow2 = gtk_scrolled_window_new(NULL, NULL);
301         gtk_widget_show(scrolledwindow2);
302         gtk_box_pack_start(GTK_BOX(hbox2), scrolledwindow2, FALSE, FALSE, 0);
303         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
304                                        (scrolledwindow2), GTK_POLICY_NEVER,
305                                        GTK_POLICY_AUTOMATIC);
306
307         plugin_list_view = pluginwindow_list_view_create(pluginwindow);
308         gtk_widget_show(plugin_list_view);
309         gtk_container_add(GTK_CONTAINER(scrolledwindow2), plugin_list_view);
310         gtk_widget_grab_focus(GTK_WIDGET(plugin_list_view));
311
312         vbox2 = gtk_vbox_new(FALSE, 0);
313         gtk_widget_show(vbox2);
314         gtk_box_pack_start(GTK_BOX(hbox2), vbox2, TRUE, TRUE, 0);
315
316         frame2 = gtk_frame_new(NULL);
317         gtk_widget_show(frame2);
318         gtk_box_pack_start(GTK_BOX(vbox2), frame2, FALSE, TRUE, 0);
319
320         label13 = gtk_label_new(_("Description"));
321         gtk_widget_show(label13);
322         gtk_container_add(GTK_CONTAINER(frame2), label13);
323         gtk_misc_set_alignment(GTK_MISC(label13), 0, 0.5);
324         gtk_misc_set_padding(GTK_MISC(label13), 2, 2);
325
326         scrolledwindow3 = gtk_scrolled_window_new(NULL, NULL);
327         gtk_widget_show(scrolledwindow3);
328         gtk_box_pack_start(GTK_BOX(vbox2), scrolledwindow3, TRUE, TRUE, 0);
329         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
330                                        (scrolledwindow3), GTK_POLICY_NEVER,
331                                        GTK_POLICY_ALWAYS);
332
333         plugin_desc = gtk_text_view_new();
334         gtk_widget_show(plugin_desc);
335         gtk_container_add(GTK_CONTAINER(scrolledwindow3), plugin_desc);
336
337         desc_lbl = gtk_label_new(_("More plugins are available from the "
338                                    "Claws Mail website."));
339         gtk_misc_set_alignment(GTK_MISC(desc_lbl), 0, 0.5);
340         gtk_widget_show(desc_lbl);
341         gtk_box_pack_start(GTK_BOX(vbox1), desc_lbl, FALSE, FALSE, 0);
342
343         get_more_btn = gtkut_get_link_btn(window, PLUGINS_URI, _("Get more..."));
344         gtk_misc_set_alignment(GTK_MISC(GTK_BIN(get_more_btn)->child), 0, 0.5);
345         gtk_widget_show(get_more_btn);
346         gtk_box_pack_start(GTK_BOX(vbox1), get_more_btn, FALSE, FALSE, 0);
347
348         hbox3 = gtk_hbox_new(FALSE, 0);
349         gtk_widget_show(hbox3);
350         hbuttonbox1 = gtk_hbutton_box_new();
351         gtk_widget_show(hbuttonbox1);
352
353         gtk_box_pack_start(GTK_BOX(hbox3), hbuttonbox1, FALSE, FALSE, 0);
354         gtk_box_pack_start(GTK_BOX(vbox1), hbox3, 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 (hbox3), hbuttonbox1, TRUE, TRUE, 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
381         tooltips = gtk_tooltips_new();
382
383         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips),
384                         load_btn,
385                         _("Click here to load one or more plugins"), NULL);
386
387         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips),
388                         unload_btn,
389                         _("Unload the selected plugin"), NULL);
390
391         pluginwindow->window = window;
392         pluginwindow->plugin_list_view = plugin_list_view;
393         pluginwindow->plugin_desc = plugin_desc;
394         pluginwindow->unload_btn = unload_btn;
395         pluginwindow->selected_plugin = NULL;
396
397         set_plugin_list(pluginwindow);
398
399         inc_lock();
400
401         if (!geometry.min_height) {
402                 geometry.min_width = 480;
403                 geometry.min_height = 300;
404         }
405
406         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
407                                       GDK_HINT_MIN_SIZE);
408         gtk_widget_set_size_request(window, prefs_common.pluginswin_width,
409                                     prefs_common.pluginswin_height);
410
411         gtk_widget_set_size_request(hbuttonbox1, -1, -1);
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