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