2008-05-07 [colin] 3.4.0cvs29
[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                 gtk_entry_set_text(GTK_ENTRY(GTK_BIN(whitelist_ab_folder_combo)->child),
304                                 config->whitelist_ab_folder);
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(page->whitelist_ab_folder_combo)->child), 0, -1);
374
375         /* learn_from_whitelist_chkbtn */
376         config->learn_from_whitelist = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->learn_from_whitelist_chkbtn));
377
378         /* bogopath */
379         g_free(config->bogopath);
380         config->bogopath = gtk_editable_get_chars(GTK_EDITABLE(page->bogopath), 0, -1);
381
382         /* max_size */
383         config->max_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->max_size));
384
385         /* mark_as_read */
386         config->mark_as_read = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->mark_as_read));
387
388         if (config->process_emails) {
389                 bogofilter_register_hook();
390         } else {
391                 bogofilter_unregister_hook();
392         }
393
394         procmsg_register_spam_learner(bogofilter_learn);
395         procmsg_spam_set_folder(config->save_folder, bogofilter_get_spam_folder);
396
397         bogofilter_save_config();
398 }
399
400 typedef struct _BogoCbData {
401         gchar *message;
402         gint total;
403         gint done;
404 } BogoCbData;
405
406 static gboolean gtk_message_callback(gpointer data)
407 {
408         BogoCbData *cbdata = (BogoCbData *)data;
409
410         if (cbdata->message)
411                 statusbar_print_all("%s", cbdata->message);
412         else if (cbdata->total == 0) {
413                 statusbar_pop_all();
414         }
415         if (cbdata->total && cbdata->done)
416                 statusbar_progress_all(cbdata->done, cbdata->total, 10);
417         else
418                 statusbar_progress_all(0,0,0);
419         g_free(cbdata->message);
420         g_free(cbdata);
421         GTK_EVENTS_FLUSH();
422         return FALSE;
423 }
424
425 static void gtk_safe_message_callback(gchar *message, gint total, gint done, gboolean thread_safe)
426 {
427         BogoCbData *cbdata = g_new0(BogoCbData, 1);
428         if (message)
429                 cbdata->message = g_strdup(message);
430         cbdata->total = total;
431         cbdata->done = done;
432         if (thread_safe)
433                 g_timeout_add(0, gtk_message_callback, cbdata);
434         else
435                 gtk_message_callback(cbdata);
436 }
437
438 static struct BogofilterPage bogofilter_page;
439
440 gint bogofilter_gtk_init(void)
441 {
442         static gchar *path[3];
443
444         path[0] = _("Plugins");
445         path[1] = _("Bogofilter");
446         path[2] = NULL;
447
448         bogofilter_page.page.path = path;
449         bogofilter_page.page.create_widget = bogofilter_create_widget_func;
450         bogofilter_page.page.destroy_widget = bogofilter_destroy_widget_func;
451         bogofilter_page.page.save_page = bogofilter_save_func;
452         bogofilter_page.page.weight = 35.0;
453
454         prefs_gtk_register_page((PrefsPage *) &bogofilter_page);
455         bogofilter_set_message_callback(gtk_safe_message_callback);
456
457         debug_print("Bogofilter GTK plugin loaded\n");
458         return 0;       
459 }
460
461 void bogofilter_gtk_done(void)
462 {
463         prefs_gtk_unregister_page((PrefsPage *) &bogofilter_page);
464 }