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