2005-12-12 [paul] 1.9.100cvs83
[claws.git] / src / gtk / pluginwindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 = plugin_get_desc(plugin);
130                 gtk_text_buffer_insert(textbuf, &start_iter, text, strlen(text));
131                 gtk_widget_set_sensitive(pluginwindow->unload_btn, TRUE);
132         } else {
133                 gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);
134         }
135 }
136
137 static void unselect_row_cb(Plugin *plugin, PluginWindow *pluginwindow)
138 {
139         gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);      
140 }
141
142 static void unload_cb(GtkButton *button, PluginWindow *pluginwindow)
143 {
144         Plugin *plugin = pluginwindow->selected_plugin;
145
146         g_return_if_fail(plugin != NULL);
147         plugin_unload(plugin);
148         pluginwindow->selected_plugin = NULL;
149         set_plugin_list(pluginwindow);
150 }
151
152 static void load_cb(GtkButton *button, PluginWindow *pluginwindow)
153 {
154         gchar *file, *error = NULL;
155
156         file = filesel_select_file_open_with_filter(_("Select Plugin to load"), 
157                                                     PLUGINDIR, 
158                                                     "*." G_MODULE_SUFFIX);
159         if (file == NULL)
160                 return;
161
162         plugin_load(file, &error);
163         if (error != NULL) {
164                 alertpanel_error("The following error occured while loading the plugin:\n%s\n", error);
165                 g_free(error);
166         }
167
168         set_plugin_list(pluginwindow);
169         g_free(file);
170 }
171
172 static gboolean pluginwindow_key_pressed(GtkWidget *widget, GdkEventKey *event,
173                                      PluginWindow *pluginwindow)
174 {
175         if (event) {
176                 switch (event->keyval) {
177                         case GDK_Escape : 
178                         case GDK_Return : 
179                         case GDK_KP_Enter :
180                                 close_cb(NULL, pluginwindow);
181                                 break;
182                         case GDK_Insert : 
183                         case GDK_KP_Insert :
184                         case GDK_KP_Add : 
185                         case GDK_plus :
186                                 load_cb(NULL, pluginwindow);
187                                 break;
188                         case GDK_Delete : 
189                         case GDK_KP_Delete :
190                         case GDK_KP_Subtract : 
191                         case GDK_minus :
192                                 unload_cb(NULL, pluginwindow);
193                                 break;
194                         default :
195                                 break;
196                 }
197         }
198         return FALSE;
199 }
200
201 /*!
202  *\brief        Save Gtk object size to prefs dataset
203  */
204 static void pluginwindow_size_allocate_cb(GtkWidget *widget,
205                                          GtkAllocation *allocation)
206 {
207         g_return_if_fail(allocation != NULL);
208
209         prefs_common.pluginswin_width = allocation->width;
210         prefs_common.pluginswin_height = allocation->height;
211 }
212
213
214 void pluginwindow_create()
215 {
216         PluginWindow *pluginwindow;
217         /* ---------------------- code made by glade ---------------------- */
218         GtkWidget *window;
219         GtkWidget *vbox1;
220         GtkWidget *hbox2;
221         GtkWidget *scrolledwindow2;
222         GtkWidget *plugin_list_view;
223         GtkWidget *vbox2;
224         GtkWidget *frame2;
225         GtkWidget *label13;
226         GtkWidget *scrolledwindow3;
227         GtkWidget *plugin_desc;
228         GtkWidget *hbuttonbox1;
229         GtkWidget *load_btn;
230         GtkWidget *unload_btn;
231         GtkWidget *close_btn;
232         static GdkGeometry geometry;
233         
234         debug_print("Creating plugins window...\n");
235
236         pluginwindow = g_new0(PluginWindow, 1);
237
238         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
239         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
240         gtk_window_set_title(GTK_WINDOW(window), _("Plugins"));
241         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
242
243         vbox1 = gtk_vbox_new(FALSE, 4);
244         gtk_widget_show(vbox1);
245         gtk_container_add(GTK_CONTAINER(window), vbox1);
246
247         hbox2 = gtk_hbox_new(FALSE, 8);
248         gtk_widget_show(hbox2);
249         gtk_box_pack_start(GTK_BOX(vbox1), hbox2, TRUE, TRUE, 0);
250
251         scrolledwindow2 = gtk_scrolled_window_new(NULL, NULL);
252         gtk_widget_show(scrolledwindow2);
253         gtk_box_pack_start(GTK_BOX(hbox2), scrolledwindow2, FALSE, FALSE, 0);
254         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
255                                        (scrolledwindow2), GTK_POLICY_NEVER,
256                                        GTK_POLICY_AUTOMATIC);
257
258         plugin_list_view = pluginwindow_list_view_create(pluginwindow);
259         gtk_widget_show(plugin_list_view);
260         gtk_container_add(GTK_CONTAINER(scrolledwindow2), plugin_list_view);
261         gtk_widget_grab_focus(GTK_WIDGET(plugin_list_view));
262
263         vbox2 = gtk_vbox_new(FALSE, 0);
264         gtk_widget_show(vbox2);
265         gtk_box_pack_start(GTK_BOX(hbox2), vbox2, TRUE, TRUE, 0);
266
267         frame2 = gtk_frame_new(NULL);
268         gtk_widget_show(frame2);
269         gtk_box_pack_start(GTK_BOX(vbox2), frame2, FALSE, TRUE, 0);
270
271         label13 = gtk_label_new(_("Description"));
272         gtk_widget_show(label13);
273         gtk_container_add(GTK_CONTAINER(frame2), label13);
274         gtk_misc_set_alignment(GTK_MISC(label13), 0, 0.5);
275         gtk_misc_set_padding(GTK_MISC(label13), 2, 2);
276
277         scrolledwindow3 = gtk_scrolled_window_new(NULL, NULL);
278         gtk_widget_show(scrolledwindow3);
279         gtk_box_pack_start(GTK_BOX(vbox2), scrolledwindow3, TRUE, TRUE, 0);
280         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
281                                        (scrolledwindow3), GTK_POLICY_NEVER,
282                                        GTK_POLICY_ALWAYS);
283
284         plugin_desc = gtk_text_view_new();
285         gtk_widget_show(plugin_desc);
286         gtk_container_add(GTK_CONTAINER(scrolledwindow3), plugin_desc);
287
288         hbuttonbox1 = gtk_hbutton_box_new();
289         gtk_widget_show(hbuttonbox1);
290         gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox1, FALSE, FALSE, 0);
291         gtk_button_box_set_layout(GTK_BUTTON_BOX(hbuttonbox1),
292                                   GTK_BUTTONBOX_END);
293         gtk_button_box_set_spacing(GTK_BUTTON_BOX(hbuttonbox1), 6);
294
295         load_btn = gtk_button_new_with_label(_("Load Plugin..."));
296         gtk_widget_show(load_btn);
297         gtk_container_add(GTK_CONTAINER(hbuttonbox1), load_btn);
298         GTK_WIDGET_SET_FLAGS(load_btn, GTK_CAN_DEFAULT);
299
300         unload_btn = gtk_button_new_with_label(_("Unload Plugin"));
301         gtk_widget_show(unload_btn);
302         gtk_container_add(GTK_CONTAINER(hbuttonbox1), unload_btn);
303         GTK_WIDGET_SET_FLAGS(unload_btn, GTK_CAN_DEFAULT);
304
305         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
306         gtk_widget_show(close_btn);
307         gtk_container_add(GTK_CONTAINER(hbuttonbox1), close_btn);
308         GTK_WIDGET_SET_FLAGS(close_btn, GTK_CAN_DEFAULT);
309         /* ----------------------------------------------------------- */
310
311         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(plugin_desc), GTK_WRAP_WORD);
312         gtk_widget_set_sensitive(GTK_WIDGET(unload_btn), FALSE);
313
314
315         g_signal_connect(G_OBJECT(load_btn), "released",
316                          G_CALLBACK(load_cb), pluginwindow);
317         g_signal_connect(G_OBJECT(unload_btn), "released",
318                          G_CALLBACK(unload_cb), pluginwindow);
319         g_signal_connect(G_OBJECT(close_btn), "released",
320                          G_CALLBACK(close_cb), pluginwindow);
321         g_signal_connect(G_OBJECT(window), "size_allocate",
322                          G_CALLBACK(pluginwindow_size_allocate_cb), NULL);
323         g_signal_connect(G_OBJECT(window), "key_press_event",
324                            G_CALLBACK(pluginwindow_key_pressed), pluginwindow);
325
326         pluginwindow->window = window;
327         pluginwindow->plugin_list_view = plugin_list_view;
328         pluginwindow->plugin_desc = plugin_desc;
329         pluginwindow->unload_btn = unload_btn;
330         pluginwindow->selected_plugin = NULL;
331
332         set_plugin_list(pluginwindow);
333
334         inc_lock();
335
336         if (!geometry.min_height) {
337                 geometry.min_width = 480;
338                 geometry.min_height = 300;
339         }
340
341         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
342                                       GDK_HINT_MIN_SIZE);
343         gtk_widget_set_size_request(window, prefs_common.pluginswin_width,
344                                     prefs_common.pluginswin_height);
345
346         gtk_widget_show(window);
347 }
348
349 static GtkListStore* pluginwindow_create_data_store(void)
350 {
351         return gtk_list_store_new(N_PLUGINWINDOW_COLUMNS,
352                                   G_TYPE_STRING,        
353                                   G_TYPE_POINTER,
354                                   -1);
355 }
356
357 static GtkWidget *pluginwindow_list_view_create(PluginWindow *pluginwindow)
358 {
359         GtkTreeView *list_view;
360         GtkTreeSelection *selector;
361         GtkTreeModel *model;
362
363         model = GTK_TREE_MODEL(pluginwindow_create_data_store());
364         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
365         g_object_unref(model);  
366         
367         gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
368         
369         selector = gtk_tree_view_get_selection(list_view);
370         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
371         gtk_tree_selection_set_select_function(selector, pluginwindow_selected,
372                                                pluginwindow, NULL);
373
374         /* create the columns */
375         pluginwindow_create_list_view_columns(GTK_WIDGET(list_view));
376
377         return GTK_WIDGET(list_view);
378 }
379
380 static void pluginwindow_create_list_view_columns(GtkWidget *list_view)
381 {
382         GtkTreeViewColumn *column;
383         GtkCellRenderer *renderer;
384
385         renderer = gtk_cell_renderer_text_new();
386         column = gtk_tree_view_column_new_with_attributes
387                 (_("Plugins"),
388                  renderer,
389                  "text", PLUGINWINDOW_NAME,
390                  NULL);
391         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
392 }
393
394 static gboolean pluginwindow_selected(GtkTreeSelection *selector,
395                                       GtkTreeModel *model, 
396                                       GtkTreePath *path,
397                                       gboolean currently_selected,
398                                       gpointer data)
399 {
400         GtkTreeIter iter;
401         Plugin *plugin;
402         
403         if (!gtk_tree_model_get_iter(model, &iter, path))
404                 return TRUE;
405
406         gtk_tree_model_get(model, &iter, 
407                            PLUGINWINDOW_DATA, &plugin,
408                            -1);
409
410         if (currently_selected) 
411                 unselect_row_cb(plugin, data);
412         else
413                 select_row_cb(plugin, data);
414
415         return TRUE;
416 }
417