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