2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws Team
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.
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.
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.
25 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
34 #include "alertpanel.h"
35 #include "prefs_common.h"
39 PLUGINWINDOW_NAME, /*<! plugin name */
40 PLUGINWINDOW_DATA, /*<! Plugin pointer */
41 PLUGINWINDOW_STYLE, /*<! italic if error */
42 N_PLUGINWINDOW_COLUMNS
45 typedef struct _PluginWindow
48 GtkWidget *plugin_list_view;
49 GtkWidget *plugin_desc;
50 GtkWidget *unload_btn;
52 Plugin *selected_plugin;
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,
61 gboolean currently_selected,
64 static void close_cb(GtkButton *button, PluginWindow *pluginwindow)
66 gtk_widget_destroy(pluginwindow->window);
72 static void set_plugin_list(PluginWindow *pluginwindow)
74 GSList *plugins, *cur, *unloaded;
78 GtkTextBuffer *textbuf;
79 GtkTextIter start_iter, end_iter;
80 GtkTreeSelection *selection;
82 plugins = plugin_get_list();
83 unloaded = plugin_get_unloaded_list();
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);
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);
99 for(cur = plugins; cur != NULL; cur = g_slist_next(cur)) {
100 Plugin *plugin = (Plugin *) cur->data;
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,
111 for(cur = unloaded; cur != NULL; cur = g_slist_next(cur)) {
112 Plugin *plugin = (Plugin *) cur->data;
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,
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);
128 g_slist_free(plugins);
131 static void select_row_cb(Plugin *plugin, PluginWindow *pluginwindow)
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;
138 pluginwindow->selected_plugin = plugin;
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);
148 text = g_strconcat(desc, _("\n\nVersion: "),
149 plugin_get_version(plugin), "\n", NULL);
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));
157 gtk_widget_set_sensitive(pluginwindow->unload_btn, TRUE);
159 gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);
163 static void unselect_row_cb(Plugin *plugin, PluginWindow *pluginwindow)
165 gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);
168 static void unload_cb(GtkButton *button, PluginWindow *pluginwindow)
170 Plugin *plugin = pluginwindow->selected_plugin;
172 g_return_if_fail(plugin != NULL);
173 plugin_unload(plugin);
174 pluginwindow->selected_plugin = NULL;
175 set_plugin_list(pluginwindow);
178 static void load_cb(GtkButton *button, PluginWindow *pluginwindow)
182 file_list = filesel_select_multiple_files_open_with_filter(
183 _("Select Plugin to load"), get_plugin_dir(),
184 "*." G_MODULE_SUFFIX);
189 for ( tmp = file_list; tmp; tmp = tmp->next) {
190 gchar *file, *error = NULL;
192 file = (gchar *) tmp->data;
194 plugin_load(file, &error);
197 _("The following error occured while loading the plugin [%s] :\n%s\n"),
202 /* FIXME: globally or atom-ly : ? */
203 set_plugin_list(pluginwindow);
207 g_list_free(file_list);
211 static gboolean pluginwindow_key_pressed(GtkWidget *widget, GdkEventKey *event,
212 PluginWindow *pluginwindow)
215 switch (event->keyval) {
219 close_cb(NULL, pluginwindow);
225 load_cb(NULL, pluginwindow);
229 case GDK_KP_Subtract :
231 unload_cb(NULL, pluginwindow);
241 *\brief Save Gtk object size to prefs dataset
243 static void pluginwindow_size_allocate_cb(GtkWidget *widget,
244 GtkAllocation *allocation)
246 g_return_if_fail(allocation != NULL);
248 prefs_common.pluginswin_width = allocation->width;
249 prefs_common.pluginswin_height = allocation->height;
253 void pluginwindow_create()
255 PluginWindow *pluginwindow;
256 /* ---------------------- code made by glade ---------------------- */
260 GtkWidget *scrolledwindow2;
261 GtkWidget *plugin_list_view;
265 GtkWidget *scrolledwindow3;
266 GtkWidget *plugin_desc;
267 GtkWidget *hbuttonbox1;
269 GtkWidget *unload_btn;
270 GtkWidget *close_btn;
271 static GdkGeometry geometry;
273 debug_print("Creating plugins window...\n");
275 pluginwindow = g_new0(PluginWindow, 1);
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);
282 vbox1 = gtk_vbox_new(FALSE, 4);
283 gtk_widget_show(vbox1);
284 gtk_container_add(GTK_CONTAINER(window), vbox1);
286 hbox2 = gtk_hbox_new(FALSE, 8);
287 gtk_widget_show(hbox2);
288 gtk_box_pack_start(GTK_BOX(vbox1), hbox2, TRUE, TRUE, 0);
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);
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));
302 vbox2 = gtk_vbox_new(FALSE, 0);
303 gtk_widget_show(vbox2);
304 gtk_box_pack_start(GTK_BOX(hbox2), vbox2, TRUE, TRUE, 0);
306 frame2 = gtk_frame_new(NULL);
307 gtk_widget_show(frame2);
308 gtk_box_pack_start(GTK_BOX(vbox2), frame2, FALSE, TRUE, 0);
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);
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,
323 plugin_desc = gtk_text_view_new();
324 gtk_widget_show(plugin_desc);
325 gtk_container_add(GTK_CONTAINER(scrolledwindow3), plugin_desc);
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),
332 gtk_button_box_set_spacing(GTK_BUTTON_BOX(hbuttonbox1), 6);
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);
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);
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 /* ----------------------------------------------------------- */
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);
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);
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;
371 set_plugin_list(pluginwindow);
375 if (!geometry.min_height) {
376 geometry.min_width = 480;
377 geometry.min_height = 300;
380 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
382 gtk_widget_set_size_request(window, prefs_common.pluginswin_width,
383 prefs_common.pluginswin_height);
385 gtk_widget_show(window);
388 static GtkListStore* pluginwindow_create_data_store(void)
390 return gtk_list_store_new(N_PLUGINWINDOW_COLUMNS,
397 static GtkWidget *pluginwindow_list_view_create(PluginWindow *pluginwindow)
399 GtkTreeView *list_view;
400 GtkTreeSelection *selector;
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);
407 gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
408 gtk_tree_view_set_search_column (list_view, 0);
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,
415 /* create the columns */
416 pluginwindow_create_list_view_columns(GTK_WIDGET(list_view));
418 return GTK_WIDGET(list_view);
421 static void pluginwindow_create_list_view_columns(GtkWidget *list_view)
423 GtkTreeViewColumn *column;
424 GtkCellRenderer *renderer;
426 renderer = gtk_cell_renderer_text_new();
427 column = gtk_tree_view_column_new_with_attributes
430 "text", PLUGINWINDOW_NAME,
431 "style", PLUGINWINDOW_STYLE,
433 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
436 static gboolean pluginwindow_selected(GtkTreeSelection *selector,
439 gboolean currently_selected,
445 if (!gtk_tree_model_get_iter(model, &iter, path))
448 gtk_tree_model_get(model, &iter,
449 PLUGINWINDOW_DATA, &plugin,
452 if (currently_selected)
453 unselect_row_cb(plugin, data);
455 select_row_cb(plugin, data);