8ba5599e101f658b993c93649616787a38616bd3
[claws.git] / src / gtk / pluginwindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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 Plugin 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                                 alertpanel_error(
199                                 _("The following error occured while loading the plugin [%s] :\n%s\n"),
200                                 file, error);
201                                 g_free(error);
202                         }
203
204                         /* FIXME: globally or atom-ly : ? */
205                         set_plugin_list(pluginwindow);
206                         g_free(file);
207                 }
208
209                 g_list_free(file_list);
210         }               
211 }
212
213 static gboolean pluginwindow_key_pressed(GtkWidget *widget, GdkEventKey *event,
214                                      PluginWindow *pluginwindow)
215 {
216         if (event) {
217                 switch (event->keyval) {
218                         case GDK_Escape : 
219                         case GDK_Return : 
220                         case GDK_KP_Enter :
221                                 close_cb(NULL, pluginwindow);
222                                 break;
223                         case GDK_Insert : 
224                         case GDK_KP_Insert :
225                         case GDK_KP_Add : 
226                         case GDK_plus :
227                                 load_cb(NULL, pluginwindow);
228                                 break;
229                         case GDK_Delete : 
230                         case GDK_KP_Delete :
231                         case GDK_KP_Subtract : 
232                         case GDK_minus :
233                                 unload_cb(NULL, pluginwindow);
234                                 break;
235                         default :
236                                 break;
237                 }
238         }
239         return FALSE;
240 }
241
242 /*!
243  *\brief        Save Gtk object size to prefs dataset
244  */
245 static void pluginwindow_size_allocate_cb(GtkWidget *widget,
246                                          GtkAllocation *allocation)
247 {
248         g_return_if_fail(allocation != NULL);
249
250         prefs_common.pluginswin_width = allocation->width;
251         prefs_common.pluginswin_height = allocation->height;
252 }
253
254
255 void pluginwindow_create()
256 {
257         PluginWindow *pluginwindow;
258         GtkWidget *window;
259         GtkWidget *vbox1;
260         GtkWidget *hbox2;
261         GtkWidget *scrolledwindow2;
262         GtkWidget *plugin_list_view;
263         GtkWidget *vbox2;
264         GtkWidget *frame2;
265         GtkWidget *label13;
266         GtkWidget *scrolledwindow3;
267         GtkWidget *plugin_desc;
268         GtkWidget *hbuttonbox1, *hbox3;
269         GtkWidget *help_btn;
270         GtkWidget *load_btn;
271         GtkWidget *unload_btn;
272         GtkWidget *close_btn;
273         GtkWidget *get_more_btn;
274         GtkWidget *desc_lbl;
275         static GdkGeometry geometry;
276         
277         debug_print("Creating plugins window...\n");
278
279         pluginwindow = g_new0(PluginWindow, 1);
280
281         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
282         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
283         gtk_window_set_title(GTK_WINDOW(window), _("Plugins"));
284         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
285
286         vbox1 = gtk_vbox_new(FALSE, 4);
287         gtk_widget_show(vbox1);
288         gtk_container_add(GTK_CONTAINER(window), vbox1);
289         gtk_box_set_homogeneous(GTK_BOX(vbox1), FALSE);
290         gtk_widget_realize(window);
291
292         hbox2 = gtk_hbox_new(FALSE, 8);
293         gtk_widget_show(hbox2);
294         gtk_box_pack_start(GTK_BOX(vbox1), hbox2, TRUE, TRUE, 0);
295
296         scrolledwindow2 = gtk_scrolled_window_new(NULL, NULL);
297         gtk_widget_show(scrolledwindow2);
298         gtk_box_pack_start(GTK_BOX(hbox2), scrolledwindow2, FALSE, FALSE, 0);
299         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
300                                        (scrolledwindow2), GTK_POLICY_NEVER,
301                                        GTK_POLICY_AUTOMATIC);
302
303         plugin_list_view = pluginwindow_list_view_create(pluginwindow);
304         gtk_widget_show(plugin_list_view);
305         gtk_container_add(GTK_CONTAINER(scrolledwindow2), plugin_list_view);
306         gtk_widget_grab_focus(GTK_WIDGET(plugin_list_view));
307
308         vbox2 = gtk_vbox_new(FALSE, 0);
309         gtk_widget_show(vbox2);
310         gtk_box_pack_start(GTK_BOX(hbox2), vbox2, TRUE, TRUE, 0);
311
312         frame2 = gtk_frame_new(NULL);
313         gtk_widget_show(frame2);
314         gtk_box_pack_start(GTK_BOX(vbox2), frame2, FALSE, TRUE, 0);
315
316         label13 = gtk_label_new(_("Description"));
317         gtk_widget_show(label13);
318         gtk_container_add(GTK_CONTAINER(frame2), label13);
319         gtk_misc_set_alignment(GTK_MISC(label13), 0, 0.5);
320         gtk_misc_set_padding(GTK_MISC(label13), 2, 2);
321
322         scrolledwindow3 = gtk_scrolled_window_new(NULL, NULL);
323         gtk_widget_show(scrolledwindow3);
324         gtk_box_pack_start(GTK_BOX(vbox2), scrolledwindow3, TRUE, TRUE, 0);
325         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
326                                        (scrolledwindow3), GTK_POLICY_NEVER,
327                                        GTK_POLICY_ALWAYS);
328
329         plugin_desc = gtk_text_view_new();
330         gtk_widget_show(plugin_desc);
331         gtk_container_add(GTK_CONTAINER(scrolledwindow3), plugin_desc);
332
333         desc_lbl = gtk_label_new(_("More plugins are available from the "
334                                    "Sylpheed-Claws website."));
335         gtk_misc_set_alignment(GTK_MISC(desc_lbl), 0, 0.5);
336         gtk_widget_show(desc_lbl);
337         gtk_box_pack_start(GTK_BOX(vbox1), desc_lbl, FALSE, FALSE, 0);
338
339         get_more_btn = gtkut_get_link_btn(window, PLUGINS_URI, _("Get more..."));
340         gtk_misc_set_alignment(GTK_MISC(GTK_BIN(get_more_btn)->child), 0, 0.5);
341         gtk_widget_show(get_more_btn);
342         gtk_box_pack_start(GTK_BOX(vbox1), get_more_btn, FALSE, FALSE, 0);
343
344         hbox3 = gtk_hbox_new(FALSE, 0);
345         gtk_widget_show(hbox3);
346         hbuttonbox1 = gtk_hbutton_box_new();
347         gtk_widget_show(hbuttonbox1);
348
349         gtk_box_pack_start(GTK_BOX(hbox3), hbuttonbox1, FALSE, FALSE, 0);
350         gtk_box_pack_start(GTK_BOX(vbox1), hbox3, FALSE, FALSE, 0);
351
352         gtkut_stock_button_set_create_with_help(&hbuttonbox1, &help_btn,
353                         &load_btn, _("Load Plugin..."),
354                         &unload_btn, _("Unload Plugin"),
355                         &close_btn, GTK_STOCK_CLOSE);
356         gtk_box_set_spacing(GTK_BOX(hbuttonbox1), 6);
357         gtk_widget_show(hbuttonbox1);
358         gtk_box_pack_end (GTK_BOX (hbox3), hbuttonbox1, FALSE, FALSE, 0);
359
360         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(plugin_desc), GTK_WRAP_WORD);
361         gtk_widget_set_sensitive(GTK_WIDGET(unload_btn), FALSE);
362
363         g_signal_connect(G_OBJECT(help_btn), "clicked",
364                          G_CALLBACK(manual_open_with_anchor_cb),
365                          MANUAL_ANCHOR_PLUGINS);
366         g_signal_connect(G_OBJECT(load_btn), "clicked",
367                          G_CALLBACK(load_cb), pluginwindow);
368         g_signal_connect(G_OBJECT(unload_btn), "clicked",
369                          G_CALLBACK(unload_cb), pluginwindow);
370         g_signal_connect(G_OBJECT(close_btn), "clicked",
371                          G_CALLBACK(close_cb), pluginwindow);
372         g_signal_connect(G_OBJECT(window), "size_allocate",
373                          G_CALLBACK(pluginwindow_size_allocate_cb), NULL);
374         g_signal_connect(G_OBJECT(window), "key_press_event",
375                            G_CALLBACK(pluginwindow_key_pressed), pluginwindow);
376
377         pluginwindow->window = window;
378         pluginwindow->plugin_list_view = plugin_list_view;
379         pluginwindow->plugin_desc = plugin_desc;
380         pluginwindow->unload_btn = unload_btn;
381         pluginwindow->selected_plugin = NULL;
382
383         set_plugin_list(pluginwindow);
384
385         inc_lock();
386
387         if (!geometry.min_height) {
388                 geometry.min_width = 480;
389                 geometry.min_height = 300;
390         }
391
392         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
393                                       GDK_HINT_MIN_SIZE);
394         gtk_widget_set_size_request(window, prefs_common.pluginswin_width,
395                                     prefs_common.pluginswin_height);
396
397         gtk_widget_set_size_request(hbuttonbox1, -1, -1);
398
399         gtk_widget_show(window);
400 }
401
402 static GtkListStore* pluginwindow_create_data_store(void)
403 {
404         return gtk_list_store_new(N_PLUGINWINDOW_COLUMNS,
405                                   G_TYPE_STRING,        
406                                   G_TYPE_POINTER,
407                                   PANGO_TYPE_STYLE,
408                                   -1);
409 }
410
411 static GtkWidget *pluginwindow_list_view_create(PluginWindow *pluginwindow)
412 {
413         GtkTreeView *list_view;
414         GtkTreeSelection *selector;
415         GtkTreeModel *model;
416
417         model = GTK_TREE_MODEL(pluginwindow_create_data_store());
418         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
419         g_object_unref(model);  
420
421         gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
422         gtk_tree_view_set_search_column (list_view, 0);
423
424         selector = gtk_tree_view_get_selection(list_view);
425         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
426         gtk_tree_selection_set_select_function(selector, pluginwindow_selected,
427                                                pluginwindow, NULL);
428
429         /* create the columns */
430         pluginwindow_create_list_view_columns(GTK_WIDGET(list_view));
431
432         return GTK_WIDGET(list_view);
433 }
434
435 static void pluginwindow_create_list_view_columns(GtkWidget *list_view)
436 {
437         GtkTreeViewColumn *column;
438         GtkCellRenderer *renderer;
439
440         renderer = gtk_cell_renderer_text_new();
441         column = gtk_tree_view_column_new_with_attributes
442                 (_("Loaded plugins"),
443                  renderer,
444                  "text", PLUGINWINDOW_NAME,
445                  "style", PLUGINWINDOW_STYLE,
446                  NULL);
447         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
448 }
449
450 static gboolean pluginwindow_selected(GtkTreeSelection *selector,
451                                       GtkTreeModel *model, 
452                                       GtkTreePath *path,
453                                       gboolean currently_selected,
454                                       gpointer data)
455 {
456         GtkTreeIter iter;
457         Plugin *plugin;
458         
459         if (!gtk_tree_model_get_iter(model, &iter, path))
460                 return TRUE;
461
462         gtk_tree_model_get(model, &iter, 
463                            PLUGINWINDOW_DATA, &plugin,
464                            -1);
465
466         if (currently_selected) 
467                 unselect_row_cb(plugin, data);
468         else
469                 select_row_cb(plugin, data);
470
471         return TRUE;
472 }
473