9fd54e230e2211e2ef09f6ce5e78d0c9080c3100
[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         /* ---------------------- code made by glade ---------------------- */
259         GtkWidget *window;
260         GtkWidget *vbox1;
261         GtkWidget *hbox2;
262         GtkWidget *scrolledwindow2;
263         GtkWidget *plugin_list_view;
264         GtkWidget *vbox2;
265         GtkWidget *frame2;
266         GtkWidget *label13;
267         GtkWidget *scrolledwindow3;
268         GtkWidget *plugin_desc;
269         GtkWidget *hbuttonbox1, *hbox3;
270         GtkWidget *help_btn;
271         GtkWidget *load_btn;
272         GtkWidget *unload_btn;
273         GtkWidget *close_btn;
274         GtkWidget *get_more_btn;
275         GtkWidget *desc_lbl;
276         static GdkGeometry geometry;
277         
278         debug_print("Creating plugins window...\n");
279
280         pluginwindow = g_new0(PluginWindow, 1);
281
282         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
283         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
284         gtk_window_set_title(GTK_WINDOW(window), _("Plugins"));
285         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
286
287         vbox1 = gtk_vbox_new(FALSE, 4);
288         gtk_widget_show(vbox1);
289         gtk_container_add(GTK_CONTAINER(window), vbox1);
290         gtk_box_set_homogeneous(GTK_BOX(vbox1), FALSE);
291         gtk_widget_realize(window);
292
293         desc_lbl = gtk_label_new(_("Plugins are a way to greatly extend Sylpheed-Claws' capabilities."));
294         gtk_misc_set_alignment(GTK_MISC(desc_lbl), 0, 0.5);
295         gtk_widget_show(desc_lbl);
296         gtk_box_pack_start(GTK_BOX(vbox1), desc_lbl, FALSE, FALSE, 0);
297
298         get_more_btn = gtkut_get_link_btn(window, PLUGINS_URI, _("Get more..."));
299         gtk_misc_set_alignment(GTK_MISC(GTK_BIN(get_more_btn)->child), 0, 0.5);
300         gtk_widget_show(get_more_btn);
301         gtk_box_pack_start(GTK_BOX(vbox1), get_more_btn, FALSE, FALSE, 0);
302
303         hbox2 = gtk_hbox_new(FALSE, 8);
304         gtk_widget_show(hbox2);
305         gtk_box_pack_start(GTK_BOX(vbox1), hbox2, TRUE, TRUE, 0);
306
307         scrolledwindow2 = gtk_scrolled_window_new(NULL, NULL);
308         gtk_widget_show(scrolledwindow2);
309         gtk_box_pack_start(GTK_BOX(hbox2), scrolledwindow2, FALSE, FALSE, 0);
310         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
311                                        (scrolledwindow2), GTK_POLICY_NEVER,
312                                        GTK_POLICY_AUTOMATIC);
313
314         plugin_list_view = pluginwindow_list_view_create(pluginwindow);
315         gtk_widget_show(plugin_list_view);
316         gtk_container_add(GTK_CONTAINER(scrolledwindow2), plugin_list_view);
317         gtk_widget_grab_focus(GTK_WIDGET(plugin_list_view));
318
319         vbox2 = gtk_vbox_new(FALSE, 0);
320         gtk_widget_show(vbox2);
321         gtk_box_pack_start(GTK_BOX(hbox2), vbox2, TRUE, TRUE, 0);
322
323         frame2 = gtk_frame_new(NULL);
324         gtk_widget_show(frame2);
325         gtk_box_pack_start(GTK_BOX(vbox2), frame2, FALSE, TRUE, 0);
326
327         label13 = gtk_label_new(_("Description"));
328         gtk_widget_show(label13);
329         gtk_container_add(GTK_CONTAINER(frame2), label13);
330         gtk_misc_set_alignment(GTK_MISC(label13), 0, 0.5);
331         gtk_misc_set_padding(GTK_MISC(label13), 2, 2);
332
333         scrolledwindow3 = gtk_scrolled_window_new(NULL, NULL);
334         gtk_widget_show(scrolledwindow3);
335         gtk_box_pack_start(GTK_BOX(vbox2), scrolledwindow3, TRUE, TRUE, 0);
336         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
337                                        (scrolledwindow3), GTK_POLICY_NEVER,
338                                        GTK_POLICY_ALWAYS);
339
340         plugin_desc = gtk_text_view_new();
341         gtk_widget_show(plugin_desc);
342         gtk_container_add(GTK_CONTAINER(scrolledwindow3), plugin_desc);
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_button_box_set_spacing(GTK_BUTTON_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_show(window);
398 }
399
400 static GtkListStore* pluginwindow_create_data_store(void)
401 {
402         return gtk_list_store_new(N_PLUGINWINDOW_COLUMNS,
403                                   G_TYPE_STRING,        
404                                   G_TYPE_POINTER,
405                                   PANGO_TYPE_STYLE,
406                                   -1);
407 }
408
409 static GtkWidget *pluginwindow_list_view_create(PluginWindow *pluginwindow)
410 {
411         GtkTreeView *list_view;
412         GtkTreeSelection *selector;
413         GtkTreeModel *model;
414
415         model = GTK_TREE_MODEL(pluginwindow_create_data_store());
416         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
417         g_object_unref(model);  
418
419         gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
420         gtk_tree_view_set_search_column (list_view, 0);
421
422         selector = gtk_tree_view_get_selection(list_view);
423         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
424         gtk_tree_selection_set_select_function(selector, pluginwindow_selected,
425                                                pluginwindow, NULL);
426
427         /* create the columns */
428         pluginwindow_create_list_view_columns(GTK_WIDGET(list_view));
429
430         return GTK_WIDGET(list_view);
431 }
432
433 static void pluginwindow_create_list_view_columns(GtkWidget *list_view)
434 {
435         GtkTreeViewColumn *column;
436         GtkCellRenderer *renderer;
437
438         renderer = gtk_cell_renderer_text_new();
439         column = gtk_tree_view_column_new_with_attributes
440                 (_("Loaded plugins"),
441                  renderer,
442                  "text", PLUGINWINDOW_NAME,
443                  "style", PLUGINWINDOW_STYLE,
444                  NULL);
445         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
446 }
447
448 static gboolean pluginwindow_selected(GtkTreeSelection *selector,
449                                       GtkTreeModel *model, 
450                                       GtkTreePath *path,
451                                       gboolean currently_selected,
452                                       gpointer data)
453 {
454         GtkTreeIter iter;
455         Plugin *plugin;
456         
457         if (!gtk_tree_model_get_iter(model, &iter, path))
458                 return TRUE;
459
460         gtk_tree_model_get(model, &iter, 
461                            PLUGINWINDOW_DATA, &plugin,
462                            -1);
463
464         if (currently_selected) 
465                 unselect_row_cb(plugin, data);
466         else
467                 select_row_cb(plugin, data);
468
469         return TRUE;
470 }
471