Bogofilter: In addition to deleting or saving a spam message, allow third option...
[claws.git] / src / plugins / bogofilter / bogofilter_gtk.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Colin Leroy <colin@colino.net> and 
4  * the Claws Mail team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #include "claws-features.h"
24 #endif
25
26 #include "defs.h"
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31 #include "gtk/gtkutils.h"
32
33 #include "common/claws.h"
34 #include "common/version.h"
35 #include "plugin.h"
36 #include "common/utils.h"
37 #include "prefs.h"
38 #include "folder.h"
39 #include "prefs_gtk.h"
40 #include "foldersel.h"
41 #include "statusbar.h"
42 #include "bogofilter.h"
43 #include "menu.h"
44 #include "addressbook.h"
45 #include "combobox.h"
46
47 struct BogofilterPage
48 {
49         PrefsPage page;
50
51         GtkWidget *process_emails;
52         GtkWidget *receive_spam;
53         GtkWidget *save_folder;
54         GtkWidget *save_folder_select;
55         GtkWidget *save_unsure;
56         GtkWidget *save_unsure_folder;
57         GtkWidget *save_unsure_folder_select;
58         GtkWidget *insert_header;
59         GtkWidget *max_size;
60         GtkWidget *bogopath;
61         GtkWidget *whitelist_ab;
62         GtkWidget *whitelist_ab_folder_combo;
63         GtkWidget *learn_from_whitelist_chkbtn;
64         GtkWidget *mark_as_read;
65 };
66
67 static void foldersel_cb(GtkWidget *widget, gpointer data)
68 {
69         GtkWidget *entry = (GtkWidget *) data;
70         FolderItem *item;
71         gchar *item_id;
72         gint newpos = 0;
73         
74         item = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL, FALSE);
75         if (item && (item_id = folder_item_get_identifier(item)) != NULL) {
76                 gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
77                 gtk_editable_insert_text(GTK_EDITABLE(entry), item_id, strlen(item_id), &newpos);
78                 g_free(item_id);
79         }
80 }
81
82 static void spam_handle_combobox_callback(GtkWidget *widget, gpointer user_data)
83 {
84         GtkWidget *hbox = GTK_WIDGET(user_data);
85
86         if (gtk_combo_box_get_active(GTK_COMBO_BOX(widget)) == 1) {
87                 gtk_widget_set_sensitive(hbox, TRUE);
88         } else {
89                 gtk_widget_set_sensitive(hbox, FALSE);
90         }
91 }
92
93 #ifndef USE_NEW_ADDRBOOK
94 static void bogofilter_whitelist_ab_select_cb(GtkWidget *widget, gpointer data)
95 {
96         struct BogofilterPage *page = (struct BogofilterPage *) data;
97         const gchar *folderpath = NULL;
98         gchar *new_path = NULL;
99
100         folderpath = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((page->whitelist_ab_folder_combo)))));
101         new_path = addressbook_folder_selection(folderpath);
102         if (new_path) {
103                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((page->whitelist_ab_folder_combo)))), new_path);
104                 g_free(new_path);
105         } 
106 }
107 #endif
108
109 static void bogofilter_create_widget_func(PrefsPage * _page,
110                                             GtkWindow * window,
111                                             gpointer data)
112 {
113         struct BogofilterPage *page = (struct BogofilterPage *) _page;
114         BogofilterConfig *config;
115
116         GtkWidget *vbox1, *vbox2;
117         GtkWidget *hbox_max_size;
118         GtkWidget *hbox_process_emails, *hbox_save_spam, *hbox_save_unsure;
119         GtkWidget *hbox_bogopath, *hbox_whitelist;
120         GtkWidget *hbox_mark_as_read;
121
122         GtkWidget *max_size_label;
123         GtkAdjustment *max_size_spinbtn_adj;
124         GtkWidget *max_size_spinbtn;
125         GtkWidget *max_size_kb_label;
126
127         GtkWidget *process_emails_checkbtn;
128
129         GtkWidget *spam_handle_label;
130         GtkWidget *spam_handle_combobox;
131         GtkWidget *save_spam_folder_entry;
132         GtkWidget *save_spam_folder_select;
133
134         GtkWidget *save_unsure_checkbtn;
135         GtkWidget *save_unsure_folder_entry;
136         GtkWidget *save_unsure_folder_select;
137
138         GtkWidget *insert_header_checkbtn;
139         GtkWidget *whitelist_ab_checkbtn;
140         GtkWidget *learn_from_whitelist_chkbtn;
141         GtkWidget *bogopath_label;
142         GtkWidget *bogopath_entry;
143
144         GtkWidget *mark_as_read_checkbtn;
145
146         GtkWidget *whitelist_ab_folder_combo;
147         GtkWidget *whitelist_ab_select_btn;
148
149         vbox1 = gtk_vbox_new (FALSE, VSPACING);
150         gtk_widget_show (vbox1);
151         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
152
153         vbox2 = gtk_vbox_new (FALSE, 4);
154         gtk_widget_show (vbox2);
155         gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
156
157         hbox_process_emails = gtk_hbox_new(FALSE, 8);
158         gtk_widget_show(hbox_process_emails);
159         gtk_box_pack_start (GTK_BOX (vbox2), hbox_process_emails, TRUE, TRUE, 0);
160
161         process_emails_checkbtn = gtk_check_button_new_with_label(
162                         _("Process messages on receiving"));
163         gtk_widget_show(process_emails_checkbtn);
164         gtk_box_pack_start(GTK_BOX(hbox_process_emails), process_emails_checkbtn, TRUE, TRUE, 0);
165
166         hbox_max_size = gtk_hbox_new(FALSE, 8);
167         gtk_widget_show(hbox_max_size);
168         gtk_box_pack_start (GTK_BOX (vbox2), hbox_max_size, TRUE, TRUE, 0);
169
170         max_size_label = gtk_label_new(_("Maximum size"));
171         gtk_widget_show(max_size_label);
172         gtk_box_pack_start(GTK_BOX(hbox_max_size), max_size_label, FALSE, FALSE, 0);
173
174         max_size_spinbtn_adj = GTK_ADJUSTMENT(gtk_adjustment_new(250, 0, 10000, 10, 10, 0));
175         max_size_spinbtn = gtk_spin_button_new(GTK_ADJUSTMENT(max_size_spinbtn_adj), 1, 0);
176         gtk_widget_show(max_size_spinbtn);
177         gtk_box_pack_start(GTK_BOX(hbox_max_size), max_size_spinbtn, FALSE, FALSE, 0);
178         CLAWS_SET_TIP(max_size_spinbtn,
179                         _("Messages larger than this will not be checked"));
180         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(max_size_spinbtn), TRUE);
181
182         max_size_kb_label = gtk_label_new(_("KB"));
183         gtk_widget_show(max_size_kb_label);
184         gtk_box_pack_start(GTK_BOX(hbox_max_size), max_size_kb_label, FALSE, FALSE, 0);
185
186         hbox_save_spam = gtk_hbox_new(FALSE, 8);
187         gtk_widget_show(hbox_save_spam);
188         gtk_box_pack_start (GTK_BOX (vbox2), hbox_save_spam, TRUE, TRUE, 0);
189
190 #if !GTK_CHECK_VERSION(2, 24, 0)
191         spam_handle_combobox = gtk_combo_box_new_text();
192 #else
193         spam_handle_combobox = gtk_combo_box_text_new();
194 #endif
195
196 #if !GTK_CHECK_VERSION(2, 24, 0)
197         gtk_combo_box_append_text(GTK_COMBO_BOX(spam_handle_combobox),
198 #else
199         gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(spam_handle_combobox),
200 #endif
201                         _("Delete spam"));
202
203 #if !GTK_CHECK_VERSION(2, 24, 0)
204         gtk_combo_box_append_text(GTK_COMBO_BOX(spam_handle_combobox),
205 #else
206         gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(spam_handle_combobox),
207 #endif
208                         _("Save spam in..."));
209
210 #if !GTK_CHECK_VERSION(2, 24, 0)
211         gtk_combo_box_append_text(GTK_COMBO_BOX(spam_handle_combobox),
212 #else
213         gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(spam_handle_combobox),
214 #endif
215                         _("Only mark as spam"));
216
217         gtk_widget_show(spam_handle_combobox);
218         gtk_box_pack_start(GTK_BOX(hbox_save_spam), spam_handle_combobox, FALSE, FALSE, 0);
219
220         GtkWidget *hbox = gtk_hbox_new(FALSE, 8);
221         gtk_widget_show(hbox);
222         gtk_box_pack_start (GTK_BOX (hbox_save_spam), hbox, TRUE, TRUE, 0);
223
224         save_spam_folder_entry = gtk_entry_new();
225         gtk_widget_show (save_spam_folder_entry);
226         gtk_box_pack_start (GTK_BOX (hbox), save_spam_folder_entry, TRUE, TRUE, 0);
227         CLAWS_SET_TIP(save_spam_folder_entry,
228                         _("Folder for storing identified spam. Leave empty to use the trash folder."));
229
230         save_spam_folder_select = gtkut_get_browse_directory_btn(_("_Browse"));
231         gtk_widget_show (save_spam_folder_select);
232         gtk_box_pack_start (GTK_BOX (hbox), save_spam_folder_select, FALSE, FALSE, 0);
233         CLAWS_SET_TIP(save_spam_folder_select,
234                         _("Click this button to select a folder for storing spam"));
235
236         g_signal_connect(G_OBJECT(spam_handle_combobox), "changed",
237                         G_CALLBACK(spam_handle_combobox_callback), (gpointer)hbox);
238
239         hbox_save_unsure = gtk_hbox_new(FALSE, 8);
240         gtk_widget_show(hbox_save_unsure);
241         gtk_box_pack_start (GTK_BOX (vbox2), hbox_save_unsure, TRUE, TRUE, 0);
242
243         save_unsure_checkbtn = gtk_check_button_new_with_label(_("When unsure, move to"));
244         gtk_widget_show(save_unsure_checkbtn);
245         gtk_box_pack_start(GTK_BOX(hbox_save_unsure), save_unsure_checkbtn, FALSE, FALSE, 0);
246
247         save_unsure_folder_entry = gtk_entry_new();
248         gtk_widget_show (save_unsure_folder_entry);
249         gtk_box_pack_start (GTK_BOX (hbox_save_unsure), save_unsure_folder_entry, TRUE, TRUE, 0);
250         CLAWS_SET_TIP(save_unsure_folder_entry,
251                         _("Folder for storing mail for which spam status is Unsure. Leave empty to use the Inbox folder."));
252
253         save_unsure_folder_select = gtkut_get_browse_directory_btn(_("_Browse"));
254         gtk_widget_show (save_unsure_folder_select);
255         gtk_box_pack_start (GTK_BOX (hbox_save_unsure), save_unsure_folder_select, FALSE, FALSE, 0);
256         CLAWS_SET_TIP(save_unsure_folder_select,
257                         _("Click this button to select a folder for storing Unsure mails."));
258
259         insert_header_checkbtn = gtk_check_button_new_with_label(_("Insert X-Bogosity header"));
260         gtk_widget_show(insert_header_checkbtn);
261         gtk_box_pack_start(GTK_BOX(vbox2), insert_header_checkbtn, FALSE, FALSE, 0);
262         CLAWS_SET_TIP(insert_header_checkbtn,
263                         _("Only done for messages in MH folders"));
264
265         hbox_whitelist = gtk_hbox_new(FALSE, 8);
266         gtk_widget_show(hbox_whitelist);
267         gtk_box_pack_start (GTK_BOX (vbox2), hbox_whitelist, TRUE, TRUE, 0);
268
269         whitelist_ab_checkbtn = gtk_check_button_new_with_label(_("Whitelist senders found in address book/folder"));
270         gtk_widget_show(whitelist_ab_checkbtn);
271         gtk_box_pack_start(GTK_BOX(hbox_whitelist), whitelist_ab_checkbtn, FALSE, FALSE, 0);
272         CLAWS_SET_TIP(whitelist_ab_checkbtn,
273                         _("Messages coming from your address book contacts will be received in the normal folder even if detected as spam"));
274
275         whitelist_ab_folder_combo = combobox_text_new(TRUE, _("Any"), NULL);
276         gtk_widget_set_size_request(whitelist_ab_folder_combo, 100, -1);
277         gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_folder_combo, TRUE, TRUE, 0);
278
279         whitelist_ab_select_btn = gtk_button_new_with_label(_("Select..."));
280         gtk_widget_show (whitelist_ab_select_btn);
281         gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_select_btn, FALSE, FALSE, 0);
282         CLAWS_SET_TIP(whitelist_ab_select_btn,
283                         _("Click this button to select a book or folder in the address book"));
284
285         learn_from_whitelist_chkbtn = gtk_check_button_new_with_label(_("Learn whitelisted emails as ham"));
286         CLAWS_SET_TIP(learn_from_whitelist_chkbtn,
287                         _("If Bogofilter thought an email was spam or unsure, but it was whitelisted, "
288                           "learn it as ham."));
289         gtk_widget_show(learn_from_whitelist_chkbtn);
290         gtk_box_pack_start (GTK_BOX (vbox2), learn_from_whitelist_chkbtn, TRUE, TRUE, 0);
291
292         hbox_bogopath = gtk_hbox_new(FALSE, 8);
293         gtk_widget_show(hbox_bogopath);
294         gtk_box_pack_start (GTK_BOX (vbox2), hbox_bogopath, FALSE, FALSE, 0);
295
296         bogopath_label = gtk_label_new(_("Bogofilter call"));
297         gtk_widget_show(bogopath_label);
298         gtk_box_pack_start(GTK_BOX(hbox_bogopath), bogopath_label, FALSE, FALSE, 0);
299
300         bogopath_entry = gtk_entry_new();
301         gtk_widget_show(bogopath_entry);
302         gtk_box_pack_start(GTK_BOX(hbox_bogopath), bogopath_entry, FALSE, FALSE, 0);
303         CLAWS_SET_TIP(bogopath_entry,
304                         _("Path to bogofilter executable"));
305
306         hbox_mark_as_read = gtk_hbox_new(FALSE, 8);
307         gtk_widget_show(hbox_mark_as_read);
308         gtk_box_pack_start (GTK_BOX (vbox2), hbox_mark_as_read, TRUE, TRUE, 0);
309
310         mark_as_read_checkbtn = gtk_check_button_new_with_label(_("Mark spam as read"));
311         gtk_widget_show(mark_as_read_checkbtn);
312         gtk_box_pack_start(GTK_BOX(hbox_mark_as_read), mark_as_read_checkbtn, FALSE, FALSE, 0);
313
314         SET_TOGGLE_SENSITIVITY(save_unsure_checkbtn, save_unsure_folder_entry);
315         SET_TOGGLE_SENSITIVITY(save_unsure_checkbtn, save_unsure_folder_select);
316         SET_TOGGLE_SENSITIVITY(whitelist_ab_checkbtn, whitelist_ab_folder_combo);
317 #ifndef USE_NEW_ADDRBOOK
318         SET_TOGGLE_SENSITIVITY(whitelist_ab_checkbtn, whitelist_ab_select_btn);
319 #endif
320         SET_TOGGLE_SENSITIVITY(whitelist_ab_checkbtn, learn_from_whitelist_chkbtn);
321
322         config = bogofilter_get_config();
323
324         g_signal_connect(G_OBJECT(save_spam_folder_select), "clicked",
325                         G_CALLBACK(foldersel_cb), save_spam_folder_entry);
326         g_signal_connect(G_OBJECT(save_unsure_folder_select), "clicked",
327                         G_CALLBACK(foldersel_cb), save_unsure_folder_entry);
328 #ifndef USE_NEW_ADDRBOOK
329         g_signal_connect(G_OBJECT (whitelist_ab_select_btn), "clicked",
330                          G_CALLBACK(bogofilter_whitelist_ab_select_cb), page);
331 #else
332         gtk_widget_set_sensitive(GTK_WIDGET(whitelist_ab_select_btn), FALSE);
333 #endif
334         gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_size_spinbtn), (float) config->max_size);
335         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(process_emails_checkbtn), config->process_emails);
336         gtk_combo_box_set_active(GTK_COMBO_BOX(spam_handle_combobox), config->receive_spam);
337         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(save_unsure_checkbtn), config->save_unsure);
338         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(insert_header_checkbtn), config->insert_header);
339         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(whitelist_ab_checkbtn), config->whitelist_ab);
340         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(learn_from_whitelist_chkbtn), config->learn_from_whitelist);
341         if (config->whitelist_ab_folder != NULL) {
342                 /* translate "Any" (stored UNtranslated) */
343                 if (strcasecmp(config->whitelist_ab_folder, "Any") == 0)
344                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
345                                         config->whitelist_ab_folder);
346                 else
347                 /* backward compatibility (when translated "Any" was stored) */
348                 if (g_utf8_collate(config->whitelist_ab_folder, _("Any")) == 0)
349                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
350                                         config->whitelist_ab_folder);
351                 else
352                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
353                                         config->whitelist_ab_folder);
354         }
355         if (config->save_folder != NULL)
356                 gtk_entry_set_text(GTK_ENTRY(save_spam_folder_entry), config->save_folder);
357         if (config->save_unsure_folder != NULL)
358                 gtk_entry_set_text(GTK_ENTRY(save_unsure_folder_entry), config->save_unsure_folder);
359         if (config->bogopath != NULL)
360                 gtk_entry_set_text(GTK_ENTRY(bogopath_entry), config->bogopath);
361         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mark_as_read_checkbtn), config->mark_as_read);
362
363         page->max_size = max_size_spinbtn;
364         page->process_emails = process_emails_checkbtn;
365
366         page->receive_spam = spam_handle_combobox;
367         page->save_folder = save_spam_folder_entry;
368         page->save_folder_select = save_spam_folder_select;
369
370         page->save_unsure = save_unsure_checkbtn;
371         page->save_unsure_folder = save_unsure_folder_entry;
372         page->save_unsure_folder_select = save_unsure_folder_select;
373
374         page->insert_header = insert_header_checkbtn;
375         page->whitelist_ab = whitelist_ab_checkbtn;
376         page->whitelist_ab_folder_combo = whitelist_ab_folder_combo;
377         page->learn_from_whitelist_chkbtn = learn_from_whitelist_chkbtn;
378         page->bogopath = bogopath_entry;
379
380         page->mark_as_read = mark_as_read_checkbtn;
381
382         page->page.widget = vbox1;
383 }
384
385 static void bogofilter_destroy_widget_func(PrefsPage *_page)
386 {
387         debug_print("Destroying Bogofilter widget\n");
388 }
389
390 static void bogofilter_save_func(PrefsPage *_page)
391 {
392         struct BogofilterPage *page = (struct BogofilterPage *) _page;
393         BogofilterConfig *config;
394
395         debug_print("Saving Bogofilter Page\n");
396
397         config = bogofilter_get_config();
398
399         /* process_emails */
400         config->process_emails = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->process_emails));
401
402         /* receive_spam */
403         config->receive_spam = gtk_combo_box_get_active(GTK_COMBO_BOX(page->receive_spam));
404
405         /* save_folder */
406         g_free(config->save_folder);
407         config->save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1);
408
409         /* save_unsure */
410         config->save_unsure = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->save_unsure));
411
412         /* save_unsure_folder */
413         g_free(config->save_unsure_folder);
414         config->save_unsure_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_unsure_folder), 0, -1);
415
416         /* insert_header */
417         config->insert_header = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->insert_header));
418
419         /* whitelist_ab */
420         config->whitelist_ab = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->whitelist_ab));
421         g_free(config->whitelist_ab_folder);
422         config->whitelist_ab_folder = gtk_editable_get_chars(
423                                 GTK_EDITABLE(gtk_bin_get_child(GTK_BIN((page->whitelist_ab_folder_combo)))), 0, -1);
424         /* store UNtranslated "Any" */
425         if (g_utf8_collate(config->whitelist_ab_folder, _("Any")) == 0) {
426                 g_free(config->whitelist_ab_folder);
427                 config->whitelist_ab_folder = g_strdup("Any");
428         }
429
430         /* learn_from_whitelist_chkbtn */
431         config->learn_from_whitelist = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->learn_from_whitelist_chkbtn));
432
433         /* bogopath */
434         g_free(config->bogopath);
435         config->bogopath = gtk_editable_get_chars(GTK_EDITABLE(page->bogopath), 0, -1);
436
437         /* max_size */
438         config->max_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->max_size));
439
440         /* mark_as_read */
441         config->mark_as_read = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->mark_as_read));
442
443         if (config->process_emails) {
444                 bogofilter_register_hook();
445         } else {
446                 bogofilter_unregister_hook();
447         }
448
449         procmsg_register_spam_learner(bogofilter_learn);
450         procmsg_spam_set_folder(config->save_folder, bogofilter_get_spam_folder);
451
452         bogofilter_save_config();
453 }
454
455 typedef struct _BogoCbData {
456         gchar *message;
457         gint total;
458         gint done;
459 } BogoCbData;
460
461 static gboolean gtk_message_callback(gpointer data)
462 {
463         BogoCbData *cbdata = (BogoCbData *)data;
464
465         if (cbdata->message)
466                 statusbar_print_all("%s", cbdata->message);
467         else if (cbdata->total == 0) {
468                 statusbar_pop_all();
469         }
470         if (cbdata->total && cbdata->done)
471                 statusbar_progress_all(cbdata->done, cbdata->total, 10);
472         else
473                 statusbar_progress_all(0,0,0);
474         g_free(cbdata->message);
475         g_free(cbdata);
476         GTK_EVENTS_FLUSH();
477         return FALSE;
478 }
479
480 static void gtk_safe_message_callback(gchar *message, gint total, gint done, gboolean thread_safe)
481 {
482         BogoCbData *cbdata = g_new0(BogoCbData, 1);
483         if (message)
484                 cbdata->message = g_strdup(message);
485         cbdata->total = total;
486         cbdata->done = done;
487         if (thread_safe)
488                 g_timeout_add(0, gtk_message_callback, cbdata);
489         else
490                 gtk_message_callback(cbdata);
491 }
492
493 static struct BogofilterPage bogofilter_page;
494
495 gint bogofilter_gtk_init(void)
496 {
497         static gchar *path[3];
498
499         path[0] = _("Plugins");
500         path[1] = _("Bogofilter");
501         path[2] = NULL;
502
503         bogofilter_page.page.path = path;
504         bogofilter_page.page.create_widget = bogofilter_create_widget_func;
505         bogofilter_page.page.destroy_widget = bogofilter_destroy_widget_func;
506         bogofilter_page.page.save_page = bogofilter_save_func;
507         bogofilter_page.page.weight = 35.0;
508
509         prefs_gtk_register_page((PrefsPage *) &bogofilter_page);
510         bogofilter_set_message_callback(gtk_safe_message_callback);
511
512         debug_print("Bogofilter GTK plugin loaded\n");
513         return 0;       
514 }
515
516 void bogofilter_gtk_done(void)
517 {
518         prefs_gtk_unregister_page((PrefsPage *) &bogofilter_page);
519 }