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