1 /*
2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2015 Hiroyuki Yamamoto and the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.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 #include "manual.h"
39 #include "manage_window.h"
40
41 enum {
42 PLUGINWINDOW_NAME, /*<! plugin name */
43 PLUGINWINDOW_DATA, /*<! Plugin pointer */
44 PLUGINWINDOW_STYLE, /*<! italic if error */
45 N_PLUGINWINDOW_COLUMNS
46 };
47
48 typedef struct _PluginWindow
49 {
50 GtkWidget *window;
51 GtkWidget *plugin_list_view;
52 GtkWidget *plugin_desc;
53 GtkWidget *unload_btn;
54
55 Plugin *selected_plugin;
56
57 gboolean loading;
58 } PluginWindow;
59
60 static GtkListStore* pluginwindow_create_data_store (void);
61 static GtkWidget *pluginwindow_list_view_create (PluginWindow *pluginwindow);
62 static void pluginwindow_create_list_view_columns (GtkWidget *list_view);
63 static gboolean pluginwindow_selected (GtkTreeSelection *selector,
64 GtkTreeModel *model,
65 GtkTreePath *path,
66 gboolean currently_selected,
67 gpointer data);
68
close_cb(GtkButton * button,PluginWindow * pluginwindow)69 static void close_cb(GtkButton *button, PluginWindow *pluginwindow)
70 {
71 if (pluginwindow->loading)
72 return;
73 gtk_widget_destroy(pluginwindow->window);
74 g_free(pluginwindow);
75 plugin_save_list();
76 inc_unlock();
77 }
78
pluginwindow_delete_cb(GtkWidget * widget,GdkEventAny * event,PluginWindow * pluginwindow)79 static gint pluginwindow_delete_cb(GtkWidget *widget, GdkEventAny *event,
80 PluginWindow *pluginwindow)
81 {
82 if (pluginwindow->loading)
83 return FALSE;
84 close_cb(NULL,pluginwindow);
85 return TRUE;
86 }
87
set_plugin_list(PluginWindow * pluginwindow)88 static void set_plugin_list(PluginWindow *pluginwindow)
89 {
90 GSList *plugins, *cur, *unloaded;
91 const gchar *text;
92 GtkListStore *store;
93 GtkTreeIter iter;
94 GtkTextBuffer *textbuf;
95 GtkTextIter start_iter, end_iter;
96 GtkTreeSelection *selection;
97
98 plugins = plugin_get_list();
99 unloaded = plugin_get_unloaded_list();
100
101 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
102 (pluginwindow->plugin_list_view)));
103 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store),
104 0, GTK_SORT_ASCENDING);
105 gtk_list_store_clear(store);
106
107 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pluginwindow->plugin_desc));
108 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(pluginwindow->plugin_desc), FALSE);
109 gtk_text_view_set_editable(GTK_TEXT_VIEW(pluginwindow->plugin_desc), FALSE);
110 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
111 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
112 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
113 gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);
114
115 for(cur = plugins; cur != NULL; cur = g_slist_next(cur)) {
116 Plugin *plugin = (Plugin *) cur->data;
117
118 gtk_list_store_append(store, &iter);
119 text = plugin_get_name(plugin);
120 gtk_list_store_set(store, &iter,
121 PLUGINWINDOW_NAME, text,
122 PLUGINWINDOW_DATA, plugin,
123 PLUGINWINDOW_STYLE, PANGO_STYLE_NORMAL,
124 -1);
125 }
126
127 for(cur = unloaded; cur != NULL; cur = g_slist_next(cur)) {
128 Plugin *plugin = (Plugin *) cur->data;
129
130 gtk_list_store_append(store, &iter);
131 text = plugin_get_name(plugin);
132 gtk_list_store_set(store, &iter,
133 PLUGINWINDOW_NAME, text,
134 PLUGINWINDOW_DATA, plugin,
135 PLUGINWINDOW_STYLE, PANGO_STYLE_ITALIC,
136 -1);
137 }
138
139 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pluginwindow->plugin_list_view));
140 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
141 gtk_tree_selection_select_iter(selection, &iter);
142
143 g_slist_free(plugins);
144 }
145
select_row_cb(Plugin * plugin,PluginWindow * pluginwindow)146 static void select_row_cb(Plugin *plugin, PluginWindow *pluginwindow)
147 {
148 GtkTextView *plugin_desc = GTK_TEXT_VIEW(pluginwindow->plugin_desc);
149 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(plugin_desc);
150 GtkTextIter start_iter, end_iter;
151 gchar *text;
152
153 pluginwindow->selected_plugin = plugin;
154
155 if (pluginwindow->selected_plugin != NULL) {
156 const gchar *desc = plugin_get_desc(plugin);
157 const gchar *err = plugin_get_error(plugin);
158 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
159 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
160 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
161
162 if (err == NULL)
163 text = g_strconcat(desc, _("\n\nVersion: "),
164 plugin_get_version(plugin), "\n", NULL);
165 else
166 text = g_strconcat(_("Error: "),
167 err, "\n", _("Plugin is not functional."),
168 "\n\n", desc, _("\n\nVersion: "),
169 plugin_get_version(plugin), "\n", NULL);
170 gtk_text_buffer_insert(textbuf, &start_iter, text, strlen(text));
171 g_free(text);
172 gtk_widget_set_sensitive(pluginwindow->unload_btn, TRUE);
173 } else {
174 gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);
175 }
176 }
177
unselect_row_cb(Plugin * plugin,PluginWindow * pluginwindow)178 static void unselect_row_cb(Plugin *plugin, PluginWindow *pluginwindow)
179 {
180 gtk_widget_set_sensitive(pluginwindow->unload_btn, FALSE);
181 }
182
unload_cb(GtkButton * button,PluginWindow * pluginwindow)183 static void unload_cb(GtkButton *button, PluginWindow *pluginwindow)
184 {
185 Plugin *plugin = pluginwindow->selected_plugin;
186
187 cm_return_if_fail(plugin != NULL);
188 pluginwindow->loading = TRUE;
189 plugin_unload(plugin);
190 pluginwindow->loading = FALSE;
191 pluginwindow->selected_plugin = NULL;
192 set_plugin_list(pluginwindow);
193 }
194
load_cb(GtkButton * button,PluginWindow * pluginwindow)195 static void load_cb(GtkButton *button, PluginWindow *pluginwindow)
196 {
197 GList *file_list;
198
199 file_list = filesel_select_multiple_files_open_with_filter(
200 _("Select the Plugins to load"), get_plugin_dir(),
201 "*." G_MODULE_SUFFIX);
202
203 if (file_list) {
204 GList *tmp;
205 pluginwindow->loading = TRUE;
206 for ( tmp = file_list; tmp; tmp = tmp->next) {
207 gchar *file, *error = NULL;
208
209 file = (gchar *) tmp->data;
210 if (!file) continue;
211 plugin_load(file, &error);
212 if (error != NULL) {
213 gchar *basename = g_path_get_basename(file);
214 alertpanel_error(
215 _("The following error occurred while loading %s:\n\n%s\n"),
216 basename, error);
217 g_free(basename);
218 g_free(error);
219 }
220
221 /* FIXME: globally or atom-ly : ? */
222 set_plugin_list(pluginwindow);
223 g_free(file);
224 }
225 pluginwindow->loading = FALSE;
226 g_list_free(file_list);
227 }
228 }
229
pluginwindow_key_pressed(GtkWidget * widget,GdkEventKey * event,PluginWindow * pluginwindow)230 static gboolean pluginwindow_key_pressed(GtkWidget *widget, GdkEventKey *event,
231 PluginWindow *pluginwindow)
232 {
233 if (event) {
234 switch (event->keyval) {
235 case GDK_KEY_Escape :
236 case GDK_KEY_Return :
237 case GDK_KEY_KP_Enter :
238 close_cb(NULL, pluginwindow);
239 break;
240 case GDK_KEY_Insert :
241 case GDK_KEY_KP_Insert :
242 case GDK_KEY_KP_Add :
243 case GDK_KEY_plus :
244 load_cb(NULL, pluginwindow);
245 break;
246 case GDK_KEY_Delete :
247 case GDK_KEY_KP_Delete :
248 case GDK_KEY_KP_Subtract :
249 case GDK_KEY_minus :
250 unload_cb(NULL, pluginwindow);
251 break;
252 default :
253 break;
254 }
255 }
256 return FALSE;
257 }
258
259 /*!
260 *\brief Save Gtk object size to prefs dataset
261 */
pluginwindow_size_allocate_cb(GtkWidget * widget,GtkAllocation * allocation)262 static void pluginwindow_size_allocate_cb(GtkWidget *widget,
263 GtkAllocation *allocation)
264 {
265 cm_return_if_fail(allocation != NULL);
266
267 prefs_common.pluginswin_width = allocation->width;
268 prefs_common.pluginswin_height = allocation->height;
269 }
270
271
pluginwindow_create()272 void pluginwindow_create()
273 {
274 PluginWindow *pluginwindow;
275 GtkWidget *window;
276 GtkWidget *vbox1;
277 GtkWidget *hbox2;
278 GtkWidget *scrolledwindow2;
279 GtkWidget *plugin_list_view;
280 GtkWidget *vbox2;
281 GtkWidget *frame2;
282 GtkWidget *label13;
283 GtkWidget *scrolledwindow3;
284 GtkWidget *plugin_desc;
285 GtkWidget *hbuttonbox1;
286 GtkWidget *hbuttonbox2;
287 GtkWidget *help_btn;
288 GtkWidget *load_btn;
289 GtkWidget *unload_btn;
290 GtkWidget *close_btn;
291 gchar *markup, *span;
292 GtkWidget *desc_lbl;
293 GtkWidget *vbox3;
294 GtkWidget *hbox_info;
295 static GdkGeometry geometry;
296
297 debug_print("Creating plugins window...\n");
298
299 pluginwindow = g_new0(PluginWindow, 1);
300
301 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "pluginwindow");
302 gtk_container_set_border_width(GTK_CONTAINER(window), 8);
303 gtk_window_set_title(GTK_WINDOW(window), _("Plugins"));
304 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
305 gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
306 manage_window_set_transient(GTK_WINDOW(window));
307
308 vbox1 = gtk_vbox_new(FALSE, 4);
309 gtk_widget_show(vbox1);
310 gtk_container_add(GTK_CONTAINER(window), vbox1);
311 gtk_box_set_homogeneous(GTK_BOX(vbox1), FALSE);
312 gtk_widget_realize(window);
313
314 hbox2 = gtk_hbox_new(FALSE, 8);
315 gtk_widget_show(hbox2);
316 gtk_box_pack_start(GTK_BOX(vbox1), hbox2, TRUE, TRUE, 0);
317
318 vbox3 = gtk_vbox_new(FALSE, 4);
319 gtk_widget_show(vbox3);
320 gtk_box_pack_start(GTK_BOX(hbox2), vbox3, FALSE, FALSE, 0);
321
322 scrolledwindow2 = gtk_scrolled_window_new(NULL, NULL);
323 gtk_widget_show(scrolledwindow2);
324 gtk_box_pack_start(GTK_BOX(vbox3), scrolledwindow2, TRUE, TRUE, 0);
325 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow2),
326 GTK_SHADOW_ETCHED_IN);
327 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
328 (scrolledwindow2), GTK_POLICY_NEVER,
329 GTK_POLICY_AUTOMATIC);
330
331 plugin_list_view = pluginwindow_list_view_create(pluginwindow);
332 gtk_widget_show(plugin_list_view);
333 gtk_container_add(GTK_CONTAINER(scrolledwindow2), plugin_list_view);
334 gtk_widget_grab_focus(GTK_WIDGET(plugin_list_view));
335
336 gtkut_stock_button_set_create(&hbuttonbox1,
337 &load_btn, _("_Load..."),
338 &unload_btn, _("_Unload"),
339 NULL, NULL);
340 gtk_widget_show(hbuttonbox1);
341 gtk_box_pack_start(GTK_BOX(vbox3), hbuttonbox1, FALSE, FALSE, 0);
342
343 vbox2 = gtk_vbox_new(FALSE, 0);
344 gtk_widget_show(vbox2);
345 gtk_box_pack_start(GTK_BOX(hbox2), vbox2, TRUE, TRUE, 0);
346
347 frame2 = gtk_frame_new(NULL);
348 gtk_frame_set_shadow_type(GTK_FRAME(frame2), GTK_SHADOW_OUT);
349 gtk_widget_show(frame2);
350 gtk_box_pack_start(GTK_BOX(vbox2), frame2, FALSE, TRUE, 0);
351
352 label13 = gtk_label_new(_("Description"));
353 gtk_widget_show(label13);
354 gtk_container_add(GTK_CONTAINER(frame2), label13);
355 gtk_misc_set_alignment(GTK_MISC(label13), 0, 0.5);
356 gtk_misc_set_padding(GTK_MISC(label13), 2, 2);
357
358 scrolledwindow3 = gtk_scrolled_window_new(NULL, NULL);
359 gtk_widget_show(scrolledwindow3);
360 gtk_box_pack_start(GTK_BOX(vbox2), scrolledwindow3, TRUE, TRUE, 0);
361 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow3),
362 GTK_SHADOW_ETCHED_IN);
363 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
364 (scrolledwindow3), GTK_POLICY_NEVER,
365 GTK_POLICY_ALWAYS);
366
367 plugin_desc = gtk_text_view_new();
368 gtk_widget_show(plugin_desc);
369 gtk_container_add(GTK_CONTAINER(scrolledwindow3), plugin_desc);
370
371 hbox_info = gtk_hbox_new(FALSE, 5);
372 gtk_widget_show(hbox_info);
373
374 desc_lbl = gtk_label_new("");
375 span = g_strdup_printf("<a href=\"%s\"><span underline=\"none\">", PLUGINS_URI);
376 markup = g_strdup_printf(_("For more information about plugins see the "
377 "%sClaws Mail website%s."), span, "</span></a>");
378 gtk_label_set_markup(GTK_LABEL(desc_lbl), markup);
379 g_free(markup);
380 g_free(span);
381 gtk_misc_set_alignment(GTK_MISC(desc_lbl), 0, 0.5);
382 gtk_widget_show(desc_lbl);
383 gtk_box_pack_start(GTK_BOX(hbox_info), desc_lbl, FALSE, FALSE, 0);
384
385 gtk_box_pack_start(GTK_BOX(hbox_info), gtk_label_new(""), TRUE, TRUE, 0);
386 gtk_box_pack_start(GTK_BOX(vbox1), hbox_info, FALSE, FALSE, 0);
387
388 gtkut_stock_button_set_create_with_help(&hbuttonbox2, &help_btn,
389 &close_btn, GTK_STOCK_CLOSE,
390 NULL, NULL, NULL, NULL);
391
392 gtk_box_set_spacing(GTK_BOX(hbuttonbox2), 6);
393 gtk_widget_show(hbuttonbox2);
394 gtk_box_pack_end(GTK_BOX(vbox1), hbuttonbox2, FALSE, FALSE, 0);
395
396 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(plugin_desc), GTK_WRAP_WORD);
397 gtk_widget_set_sensitive(GTK_WIDGET(unload_btn), FALSE);
398
399 g_signal_connect(G_OBJECT(help_btn), "clicked",
400 G_CALLBACK(manual_open_with_anchor_cb),
401 MANUAL_ANCHOR_PLUGINS);
402 g_signal_connect(G_OBJECT(load_btn), "clicked",
403 G_CALLBACK(load_cb), pluginwindow);
404 g_signal_connect(G_OBJECT(unload_btn), "clicked",
405 G_CALLBACK(unload_cb), pluginwindow);
406 g_signal_connect(G_OBJECT(close_btn), "clicked",
407 G_CALLBACK(close_cb), pluginwindow);
408 g_signal_connect(G_OBJECT(window), "size_allocate",
409 G_CALLBACK(pluginwindow_size_allocate_cb), NULL);
410 g_signal_connect(G_OBJECT(window), "key_press_event",
411 G_CALLBACK(pluginwindow_key_pressed), pluginwindow);
412 g_signal_connect(G_OBJECT(window), "delete_event",
413 G_CALLBACK(pluginwindow_delete_cb), pluginwindow);
414 MANAGE_WINDOW_SIGNALS_CONNECT(window);
415
416 CLAWS_SET_TIP(load_btn,
417 _("Click here to load one or more plugins"));
418
419 CLAWS_SET_TIP(unload_btn,
420 _("Unload the selected plugin"));
421
422 pluginwindow->window = window;
423 pluginwindow->plugin_list_view = plugin_list_view;
424 pluginwindow->plugin_desc = plugin_desc;
425 pluginwindow->unload_btn = unload_btn;
426 pluginwindow->selected_plugin = NULL;
427
428 set_plugin_list(pluginwindow);
429
430 inc_lock();
431
432 if (!geometry.min_height) {
433 geometry.min_width = -1;
434 geometry.min_height = 300;
435 }
436
437 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
438 GDK_HINT_MIN_SIZE);
439 gtk_window_set_default_size(GTK_WINDOW(window), prefs_common.pluginswin_width,
440 prefs_common.pluginswin_height);
441
442 gtk_widget_show(window);
443 }
444
pluginwindow_create_data_store(void)445 static GtkListStore* pluginwindow_create_data_store(void)
446 {
447 return gtk_list_store_new(N_PLUGINWINDOW_COLUMNS,
448 G_TYPE_STRING,
449 G_TYPE_POINTER,
450 PANGO_TYPE_STYLE,
451 -1);
452 }
453
pluginwindow_list_view_create(PluginWindow * pluginwindow)454 static GtkWidget *pluginwindow_list_view_create(PluginWindow *pluginwindow)
455 {
456 GtkTreeView *list_view;
457 GtkTreeSelection *selector;
458 GtkTreeModel *model;
459
460 model = GTK_TREE_MODEL(pluginwindow_create_data_store());
461 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
462 g_object_unref(model);
463
464 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
465 gtk_tree_view_set_search_column (list_view, 0);
466
467 selector = gtk_tree_view_get_selection(list_view);
468 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
469 gtk_tree_selection_set_select_function(selector, pluginwindow_selected,
470 pluginwindow, NULL);
471
472 /* create the columns */
473 pluginwindow_create_list_view_columns(GTK_WIDGET(list_view));
474
475 return GTK_WIDGET(list_view);
476 }
477
pluginwindow_create_list_view_columns(GtkWidget * list_view)478 static void pluginwindow_create_list_view_columns(GtkWidget *list_view)
479 {
480 GtkTreeViewColumn *column;
481 GtkCellRenderer *renderer;
482
483 renderer = gtk_cell_renderer_text_new();
484 column = gtk_tree_view_column_new_with_attributes
485 (_("Loaded plugins"),
486 renderer,
487 "text", PLUGINWINDOW_NAME,
488 "style", PLUGINWINDOW_STYLE,
489 NULL);
490 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
491 }
492
pluginwindow_selected(GtkTreeSelection * selector,GtkTreeModel * model,GtkTreePath * path,gboolean currently_selected,gpointer data)493 static gboolean pluginwindow_selected(GtkTreeSelection *selector,
494 GtkTreeModel *model,
495 GtkTreePath *path,
496 gboolean currently_selected,
497 gpointer data)
498 {
499 GtkTreeIter iter;
500 Plugin *plugin;
501
502 if (!gtk_tree_model_get_iter(model, &iter, path))
503 return TRUE;
504
505 gtk_tree_model_get(model, &iter,
506 PLUGINWINDOW_DATA, &plugin,
507 -1);
508
509 if (currently_selected)
510 unselect_row_cb(plugin, data);
511 else
512 select_row_cb(plugin, data);
513
514 return TRUE;
515 }
516
517