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