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