2006-02-21 [wwp] 2.0.0cvs69
[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 "plugin.h"
32
33 #include "filesel.h"
34 #include "alertpanel.h"
35 #include "prefs_common.h"
36 #include "../inc.h"
37
38 enum {
39         PLUGINWINDOW_NAME,              /*<! plugin name */
40         PLUGINWINDOW_DATA,              /*<! Plugin pointer */
41         PLUGINWINDOW_STYLE,             /*<! italic if error */
42         N_PLUGINWINDOW_COLUMNS
43 };
44
45 typedef struct _PluginWindow
46 {
47         GtkWidget *window;
48         GtkWidget *plugin_list_view;
49         GtkWidget *plugin_desc;
50         GtkWidget *unload_btn;
51
52         Plugin *selected_plugin;
53 } PluginWindow;
54
55 static GtkListStore* pluginwindow_create_data_store     (void);
56 static GtkWidget *pluginwindow_list_view_create         (PluginWindow *pluginwindow);
57 static void pluginwindow_create_list_view_columns       (GtkWidget *list_view);
58 static gboolean pluginwindow_selected                   (GtkTreeSelection *selector,
59                                                          GtkTreeModel *model, 
60                                                          GtkTreePath *path,
61                                                          gboolean currently_selected,
62                                                          gpointer data);
63
64 static void close_cb(GtkButton *button, PluginWindow *pluginwindow)
65 {
66         gtk_widget_destroy(pluginwindow->window);
67         g_free(pluginwindow);
68         plugin_save_list();
69         inc_unlock();
70 }
71
72 static void set_plugin_list(PluginWindow *pluginwindow)
73 {
74         GSList *plugins, *cur, *unloaded;
75         const gchar *text;
76         GtkListStore *store;
77         GtkTreeIter iter;
78         GtkTextBuffer *textbuf;
79         GtkTextIter start_iter, end_iter;
80         GtkTreeSelection *selection;
81
82         plugins = plugin_get_list();
83         unloaded = plugin_get_unloaded_list();
84
85         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
86                                 (pluginwindow->plugin_list_view)));
87         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store),
88                                              0, GTK_SORT_ASCENDING);
89         gtk_list_store_clear(store);
90         
91         textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pluginwindow->plugin_desc));
92         gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(pluginwindow->plugin_desc), FALSE);
93         gtk_text_view_set_editable(GTK_TEXT_VIEW(pluginwindow->plugin_desc), FALSE);
94         gtk_text_buffer_get_start_iter(textbuf, &start_iter);
95         gtk_text_buffer_get_end_iter(textbuf, &end_iter);
96         gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
97         gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);
98
99         for(cur = plugins; cur != NULL; cur = g_slist_next(cur)) {
100                 Plugin *plugin = (Plugin *) cur->data;
101
102                 gtk_list_store_append(store, &iter);
103                 text = plugin_get_name(plugin); 
104                 gtk_list_store_set(store, &iter,
105                                    PLUGINWINDOW_NAME, text,
106                                    PLUGINWINDOW_DATA, plugin,
107                                    PLUGINWINDOW_STYLE, PANGO_STYLE_NORMAL,
108                                    -1);
109         }
110
111         for(cur = unloaded; cur != NULL; cur = g_slist_next(cur)) {
112                 Plugin *plugin = (Plugin *) cur->data;
113
114                 gtk_list_store_append(store, &iter);
115                 text = plugin_get_name(plugin);
116                 gtk_list_store_set(store, &iter,
117                                    PLUGINWINDOW_NAME, text,
118                                    PLUGINWINDOW_DATA, plugin,
119                                    PLUGINWINDOW_STYLE, PANGO_STYLE_ITALIC,
120                                    -1);
121         }
122
123         if (pluginwindow->selected_plugin == NULL) { 
124                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW
125                                 (pluginwindow->plugin_list_view));
126                 gtk_tree_selection_unselect_all(selection);                             
127         }               
128         g_slist_free(plugins);
129 }
130
131 static void select_row_cb(Plugin *plugin, PluginWindow *pluginwindow)
132 {
133         GtkTextView *plugin_desc = GTK_TEXT_VIEW(pluginwindow->plugin_desc);
134         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(plugin_desc);
135         GtkTextIter start_iter, end_iter;
136         gchar *text;
137
138         pluginwindow->selected_plugin = plugin;
139
140         if (pluginwindow->selected_plugin != NULL) {
141                 const gchar *desc = plugin_get_desc(plugin);
142                 const gchar *err = plugin_get_error(plugin);
143                 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
144                 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
145                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
146                 
147                 if (err == NULL)
148                         text = g_strconcat(desc, _("\n\nVersion: "),
149                                    plugin_get_version(plugin), "\n", NULL);
150                 else
151                         text = g_strconcat(_("Error: "),
152                                    err, "\n", _("Plugin is not functional."), 
153                                    "\n\n", desc, _("\n\nVersion: "),
154                                    plugin_get_version(plugin), "\n", NULL);
155                 gtk_text_buffer_insert(textbuf, &start_iter, text, strlen(text));
156                 g_free(text);
157                 gtk_widget_set_sensitive(pluginwindow->unload_btn, TRUE);
158         } else {
159                 gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);
160         }
161 }
162
163 static void unselect_row_cb(Plugin *plugin, PluginWindow *pluginwindow)
164 {
165         gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);      
166 }
167
168 static void unload_cb(GtkButton *button, PluginWindow *pluginwindow)
169 {
170         Plugin *plugin = pluginwindow->selected_plugin;
171
172         g_return_if_fail(plugin != NULL);
173         plugin_unload(plugin);
174         pluginwindow->selected_plugin = NULL;
175         set_plugin_list(pluginwindow);
176 }
177
178 static void load_cb(GtkButton *button, PluginWindow *pluginwindow)
179 {
180         GList *file_list;
181
182         file_list = filesel_select_multiple_files_open_with_filter(
183                         _("Select Plugin to load"), get_plugin_dir(), 
184                         "*." G_MODULE_SUFFIX);
185
186         if (file_list) {
187                 GList *tmp;
188
189                 for ( tmp = file_list; tmp; tmp = tmp->next) {
190                         gchar *file, *error = NULL;
191
192                         file = (gchar *) tmp->data;
193                         if (!file) continue;
194                         plugin_load(file, &error);
195                         if (error != NULL) {
196                                 alertpanel_error(
197                                 _("The following error occured while loading the plugin [%s] :\n%s\n"),
198                                 file, error);
199                                 g_free(error);
200                         }
201
202                         /* FIXME: globally or atom-ly : ? */
203                         set_plugin_list(pluginwindow);
204                         g_free(file);
205                 }
206
207                 g_list_free(file_list);
208         }               
209 }
210
211 static gboolean pluginwindow_key_pressed(GtkWidget *widget, GdkEventKey *event,
212                                      PluginWindow *pluginwindow)
213 {
214         if (event) {
215                 switch (event->keyval) {
216                         case GDK_Escape : 
217                         case GDK_Return : 
218                         case GDK_KP_Enter :
219                                 close_cb(NULL, pluginwindow);
220                                 break;
221                         case GDK_Insert : 
222                         case GDK_KP_Insert :
223                         case GDK_KP_Add : 
224                         case GDK_plus :
225                                 load_cb(NULL, pluginwindow);
226                                 break;
227                         case GDK_Delete : 
228                         case GDK_KP_Delete :
229                         case GDK_KP_Subtract : 
230                         case GDK_minus :
231                                 unload_cb(NULL, pluginwindow);
232                                 break;
233                         default :
234                                 break;
235                 }
236         }
237         return FALSE;
238 }
239
240 /*!
241  *\brief        Save Gtk object size to prefs dataset
242  */
243 static void pluginwindow_size_allocate_cb(GtkWidget *widget,
244                                          GtkAllocation *allocation)
245 {
246         g_return_if_fail(allocation != NULL);
247
248         prefs_common.pluginswin_width = allocation->width;
249         prefs_common.pluginswin_height = allocation->height;
250 }
251
252
253 void pluginwindow_create()
254 {
255         PluginWindow *pluginwindow;
256         /* ---------------------- code made by glade ---------------------- */
257         GtkWidget *window;
258         GtkWidget *vbox1;
259         GtkWidget *hbox2;
260         GtkWidget *scrolledwindow2;
261         GtkWidget *plugin_list_view;
262         GtkWidget *vbox2;
263         GtkWidget *frame2;
264         GtkWidget *label13;
265         GtkWidget *scrolledwindow3;
266         GtkWidget *plugin_desc;
267         GtkWidget *hbuttonbox1;
268         GtkWidget *load_btn;
269         GtkWidget *unload_btn;
270         GtkWidget *close_btn;
271         static GdkGeometry geometry;
272         
273         debug_print("Creating plugins window...\n");
274
275         pluginwindow = g_new0(PluginWindow, 1);
276
277         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
278         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
279         gtk_window_set_title(GTK_WINDOW(window), _("Plugins"));
280         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
281
282         vbox1 = gtk_vbox_new(FALSE, 4);
283         gtk_widget_show(vbox1);
284         gtk_container_add(GTK_CONTAINER(window), vbox1);
285
286         hbox2 = gtk_hbox_new(FALSE, 8);
287         gtk_widget_show(hbox2);
288         gtk_box_pack_start(GTK_BOX(vbox1), hbox2, TRUE, TRUE, 0);
289
290         scrolledwindow2 = gtk_scrolled_window_new(NULL, NULL);
291         gtk_widget_show(scrolledwindow2);
292         gtk_box_pack_start(GTK_BOX(hbox2), scrolledwindow2, FALSE, FALSE, 0);
293         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
294                                        (scrolledwindow2), GTK_POLICY_NEVER,
295                                        GTK_POLICY_AUTOMATIC);
296
297         plugin_list_view = pluginwindow_list_view_create(pluginwindow);
298         gtk_widget_show(plugin_list_view);
299         gtk_container_add(GTK_CONTAINER(scrolledwindow2), plugin_list_view);
300         gtk_widget_grab_focus(GTK_WIDGET(plugin_list_view));
301
302         vbox2 = gtk_vbox_new(FALSE, 0);
303         gtk_widget_show(vbox2);
304         gtk_box_pack_start(GTK_BOX(hbox2), vbox2, TRUE, TRUE, 0);
305
306         frame2 = gtk_frame_new(NULL);
307         gtk_widget_show(frame2);
308         gtk_box_pack_start(GTK_BOX(vbox2), frame2, FALSE, TRUE, 0);
309
310         label13 = gtk_label_new(_("Description"));
311         gtk_widget_show(label13);
312         gtk_container_add(GTK_CONTAINER(frame2), label13);
313         gtk_misc_set_alignment(GTK_MISC(label13), 0, 0.5);
314         gtk_misc_set_padding(GTK_MISC(label13), 2, 2);
315
316         scrolledwindow3 = gtk_scrolled_window_new(NULL, NULL);
317         gtk_widget_show(scrolledwindow3);
318         gtk_box_pack_start(GTK_BOX(vbox2), scrolledwindow3, TRUE, TRUE, 0);
319         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
320                                        (scrolledwindow3), GTK_POLICY_NEVER,
321                                        GTK_POLICY_ALWAYS);
322
323         plugin_desc = gtk_text_view_new();
324         gtk_widget_show(plugin_desc);
325         gtk_container_add(GTK_CONTAINER(scrolledwindow3), plugin_desc);
326
327         hbuttonbox1 = gtk_hbutton_box_new();
328         gtk_widget_show(hbuttonbox1);
329         gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox1, FALSE, FALSE, 0);
330         gtk_button_box_set_layout(GTK_BUTTON_BOX(hbuttonbox1),
331                                   GTK_BUTTONBOX_END);
332         gtk_button_box_set_spacing(GTK_BUTTON_BOX(hbuttonbox1), 6);
333
334         load_btn = gtk_button_new_with_label(_("Load Plugin..."));
335         gtk_widget_show(load_btn);
336         gtk_container_add(GTK_CONTAINER(hbuttonbox1), load_btn);
337         GTK_WIDGET_SET_FLAGS(load_btn, GTK_CAN_DEFAULT);
338
339         unload_btn = gtk_button_new_with_label(_("Unload Plugin"));
340         gtk_widget_show(unload_btn);
341         gtk_container_add(GTK_CONTAINER(hbuttonbox1), unload_btn);
342         GTK_WIDGET_SET_FLAGS(unload_btn, GTK_CAN_DEFAULT);
343
344         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
345         gtk_widget_show(close_btn);
346         gtk_container_add(GTK_CONTAINER(hbuttonbox1), close_btn);
347         GTK_WIDGET_SET_FLAGS(close_btn, GTK_CAN_DEFAULT);
348         /* ----------------------------------------------------------- */
349
350         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(plugin_desc), GTK_WRAP_WORD);
351         gtk_widget_set_sensitive(GTK_WIDGET(unload_btn), FALSE);
352
353
354         g_signal_connect(G_OBJECT(load_btn), "released",
355                          G_CALLBACK(load_cb), pluginwindow);
356         g_signal_connect(G_OBJECT(unload_btn), "released",
357                          G_CALLBACK(unload_cb), pluginwindow);
358         g_signal_connect(G_OBJECT(close_btn), "released",
359                          G_CALLBACK(close_cb), pluginwindow);
360         g_signal_connect(G_OBJECT(window), "size_allocate",
361                          G_CALLBACK(pluginwindow_size_allocate_cb), NULL);
362         g_signal_connect(G_OBJECT(window), "key_press_event",
363                            G_CALLBACK(pluginwindow_key_pressed), pluginwindow);
364
365         pluginwindow->window = window;
366         pluginwindow->plugin_list_view = plugin_list_view;
367         pluginwindow->plugin_desc = plugin_desc;
368         pluginwindow->unload_btn = unload_btn;
369         pluginwindow->selected_plugin = NULL;
370
371         set_plugin_list(pluginwindow);
372
373         inc_lock();
374
375         if (!geometry.min_height) {
376                 geometry.min_width = 480;
377                 geometry.min_height = 300;
378         }
379
380         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
381                                       GDK_HINT_MIN_SIZE);
382         gtk_widget_set_size_request(window, prefs_common.pluginswin_width,
383                                     prefs_common.pluginswin_height);
384
385         gtk_widget_show(window);
386 }
387
388 static GtkListStore* pluginwindow_create_data_store(void)
389 {
390         return gtk_list_store_new(N_PLUGINWINDOW_COLUMNS,
391                                   G_TYPE_STRING,        
392                                   G_TYPE_POINTER,
393                                   PANGO_TYPE_STYLE,
394                                   -1);
395 }
396
397 static GtkWidget *pluginwindow_list_view_create(PluginWindow *pluginwindow)
398 {
399         GtkTreeView *list_view;
400         GtkTreeSelection *selector;
401         GtkTreeModel *model;
402
403         model = GTK_TREE_MODEL(pluginwindow_create_data_store());
404         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
405         g_object_unref(model);  
406
407         gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
408         gtk_tree_view_set_search_column (list_view, 0);
409
410         selector = gtk_tree_view_get_selection(list_view);
411         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
412         gtk_tree_selection_set_select_function(selector, pluginwindow_selected,
413                                                pluginwindow, NULL);
414
415         /* create the columns */
416         pluginwindow_create_list_view_columns(GTK_WIDGET(list_view));
417
418         return GTK_WIDGET(list_view);
419 }
420
421 static void pluginwindow_create_list_view_columns(GtkWidget *list_view)
422 {
423         GtkTreeViewColumn *column;
424         GtkCellRenderer *renderer;
425
426         renderer = gtk_cell_renderer_text_new();
427         column = gtk_tree_view_column_new_with_attributes
428                 (_("Plugins"),
429                  renderer,
430                  "text", PLUGINWINDOW_NAME,
431                  "style", PLUGINWINDOW_STYLE,
432                  NULL);
433         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
434 }
435
436 static gboolean pluginwindow_selected(GtkTreeSelection *selector,
437                                       GtkTreeModel *model, 
438                                       GtkTreePath *path,
439                                       gboolean currently_selected,
440                                       gpointer data)
441 {
442         GtkTreeIter iter;
443         Plugin *plugin;
444         
445         if (!gtk_tree_model_get_iter(model, &iter, path))
446                 return TRUE;
447
448         gtk_tree_model_get(model, &iter, 
449                            PLUGINWINDOW_DATA, &plugin,
450                            -1);
451
452         if (currently_selected) 
453                 unselect_row_cb(plugin, data);
454         else
455                 select_row_cb(plugin, data);
456
457         return TRUE;
458 }
459