2007-02-03 [colin] 2.7.2cvs17
[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 2 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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
45 struct BogofilterPage
46 {
47         PrefsPage page;
48
49         GtkWidget *process_emails;
50         GtkWidget *receive_spam;
51         GtkWidget *save_folder;
52         GtkWidget *save_folder_select;
53         GtkWidget *save_unsure;
54         GtkWidget *save_unsure_folder;
55         GtkWidget *save_unsure_folder_select;
56         GtkWidget *insert_header;
57         GtkWidget *max_size;
58         GtkWidget *bogopath;
59         GtkWidget *whitelist_ab;
60         GtkWidget *whitelist_ab_folder_combo;
61 };
62
63 /*!
64  *\brief        Preset addressbook book/folder items
65  */
66 static const gchar *whitelist_ab_folder_text [] = {
67         N_("Any")
68 };
69
70 static void foldersel_cb(GtkWidget *widget, gpointer data)
71 {
72         GtkWidget *entry = (GtkWidget *) data;
73         FolderItem *item;
74         gchar *item_id;
75         gint newpos = 0;
76         
77         item = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL);
78         if (item && (item_id = folder_item_get_identifier(item)) != NULL) {
79                 gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
80                 gtk_editable_insert_text(GTK_EDITABLE(entry), item_id, strlen(item_id), &newpos);
81                 g_free(item_id);
82         }
83 }
84
85 static void bogofilter_whitelist_ab_select_cb(GtkWidget *widget, gpointer data)
86 {
87         struct BogofilterPage *page = (struct BogofilterPage *) data;
88         gchar *folderpath = NULL;
89         gboolean ret = FALSE;
90
91         folderpath = (gchar *) gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(page->whitelist_ab_folder_combo)->entry));
92         ret = addressbook_folder_selection(&folderpath);
93         if ( ret != FALSE && folderpath != NULL)
94                 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(page->whitelist_ab_folder_combo)->entry), folderpath);
95 }
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
109         GtkWidget *max_size_label;
110         GtkObject *max_size_spinbtn_adj;
111         GtkWidget *max_size_spinbtn;
112         GtkWidget *max_size_kb_label;
113
114         GtkWidget *process_emails_checkbtn;
115
116         GtkWidget *save_spam_checkbtn;
117         GtkWidget *save_spam_folder_entry;
118         GtkWidget *save_spam_folder_select;
119
120         GtkWidget *save_unsure_checkbtn;
121         GtkWidget *save_unsure_folder_entry;
122         GtkWidget *save_unsure_folder_select;
123
124         GtkWidget *insert_header_chkbtn;
125         GtkWidget *whitelist_ab_chkbtn;
126         GtkWidget *bogopath_label;
127         GtkWidget *bogopath_entry;
128
129         GtkTooltips *tooltips;
130
131         GtkWidget *whitelist_ab_folder_combo;
132         GtkWidget *whitelist_ab_select_btn;
133         GList *combo_items;
134         gint i;
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 in"));
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_chkbtn = gtk_check_button_new_with_label(_("Insert X-Bogosity header"));
220         gtk_widget_show(insert_header_chkbtn);
221         gtk_box_pack_start(GTK_BOX(vbox2), insert_header_chkbtn, FALSE, FALSE, 0);
222         gtk_tooltips_set_tip(tooltips, insert_header_chkbtn,
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_chkbtn = gtk_check_button_new_with_label(_("Whitelist senders present in addressbook/folder"));
231         gtk_widget_show(whitelist_ab_chkbtn);
232         gtk_box_pack_start(GTK_BOX(hbox_whitelist), whitelist_ab_chkbtn, FALSE, FALSE, 0);
233         gtk_tooltips_set_tip(tooltips, whitelist_ab_chkbtn,
234                         _("Messages coming from your addressbook contacts will be received in the normal folder even if detected as spam"), NULL);
235
236         whitelist_ab_folder_combo = gtk_combo_new();
237         gtk_widget_show(whitelist_ab_folder_combo);
238         gtk_widget_set_size_request(whitelist_ab_folder_combo, 100, -1);
239         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(whitelist_ab_folder_combo)->entry),
240                                TRUE);
241
242         combo_items = NULL;
243         for (i = 0; i < (gint) (sizeof(whitelist_ab_folder_text) / sizeof(gchar *)); i++) {
244                 combo_items = g_list_append(combo_items,
245                                             (gpointer) _(whitelist_ab_folder_text[i]));
246         }
247         gtk_combo_set_popdown_strings(GTK_COMBO(whitelist_ab_folder_combo), combo_items);
248         g_list_free(combo_items);
249
250         gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_folder_combo, TRUE, TRUE, 0);
251
252         whitelist_ab_select_btn = gtk_button_new_with_label(_(" Select... "));
253         gtk_widget_show (whitelist_ab_select_btn);
254         gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_select_btn, FALSE, FALSE, 0);
255         gtk_tooltips_set_tip(tooltips, whitelist_ab_select_btn,
256                         _("Click this button to select a book or folder in the addressbook"),
257                         NULL);
258
259         hbox_bogopath = gtk_hbox_new(FALSE, 8);
260         gtk_widget_show(hbox_bogopath);
261         gtk_box_pack_start (GTK_BOX (vbox2), hbox_bogopath, FALSE, FALSE, 0);
262
263         bogopath_label = gtk_label_new(_("Bogofilter call"));
264         gtk_widget_show(bogopath_label);
265         gtk_box_pack_start(GTK_BOX(hbox_bogopath), bogopath_label, FALSE, FALSE, 0);
266
267         bogopath_entry = gtk_entry_new();
268         gtk_widget_show(bogopath_entry);
269         gtk_box_pack_start(GTK_BOX(hbox_bogopath), bogopath_entry, FALSE, FALSE, 0);
270         gtk_tooltips_set_tip(tooltips, bogopath_entry,
271                         _("Path to bogofilter executable"),
272                         NULL);
273
274         SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, save_spam_folder_entry);
275         SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, save_spam_folder_select);
276         SET_TOGGLE_SENSITIVITY(save_unsure_checkbtn, save_unsure_folder_entry);
277         SET_TOGGLE_SENSITIVITY(save_unsure_checkbtn, save_unsure_folder_select);
278         SET_TOGGLE_SENSITIVITY(whitelist_ab_chkbtn, whitelist_ab_folder_combo);
279         SET_TOGGLE_SENSITIVITY(whitelist_ab_chkbtn, whitelist_ab_select_btn);
280
281         config = bogofilter_get_config();
282
283         g_signal_connect(G_OBJECT(save_spam_folder_select), "clicked",
284                         G_CALLBACK(foldersel_cb), save_spam_folder_entry);
285         g_signal_connect(G_OBJECT(save_unsure_folder_select), "clicked",
286                         G_CALLBACK(foldersel_cb), save_unsure_folder_entry);
287         g_signal_connect(G_OBJECT (whitelist_ab_select_btn), "clicked",
288                          G_CALLBACK(bogofilter_whitelist_ab_select_cb), page);
289
290         gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_size_spinbtn), (float) config->max_size);
291         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(process_emails_checkbtn), config->process_emails);
292         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(save_spam_checkbtn), config->receive_spam);
293         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(save_unsure_checkbtn), config->save_unsure);
294         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(insert_header_chkbtn), config->insert_header);
295         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(whitelist_ab_chkbtn), config->whitelist_ab);
296         if (config->whitelist_ab_folder != NULL)
297                 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(whitelist_ab_folder_combo)->entry),
298                                 config->whitelist_ab_folder);
299         if (config->save_folder != NULL)
300                 gtk_entry_set_text(GTK_ENTRY(save_spam_folder_entry), config->save_folder);
301         if (config->save_unsure_folder != NULL)
302                 gtk_entry_set_text(GTK_ENTRY(save_unsure_folder_entry), config->save_unsure_folder);
303         if (config->bogopath != NULL)
304                 gtk_entry_set_text(GTK_ENTRY(bogopath_entry), config->bogopath);
305
306         page->max_size = max_size_spinbtn;
307         page->process_emails = process_emails_checkbtn;
308
309         page->receive_spam = save_spam_checkbtn;
310         page->save_folder = save_spam_folder_entry;
311         page->save_folder_select = save_spam_folder_select;
312
313         page->save_unsure = save_unsure_checkbtn;
314         page->save_unsure_folder = save_unsure_folder_entry;
315         page->save_unsure_folder_select = save_unsure_folder_select;
316
317         page->insert_header = insert_header_chkbtn;
318         page->whitelist_ab = whitelist_ab_chkbtn;
319         page->whitelist_ab_folder_combo = whitelist_ab_folder_combo;
320         page->bogopath = bogopath_entry;
321
322         page->page.widget = vbox1;
323 }
324
325 static void bogofilter_destroy_widget_func(PrefsPage *_page)
326 {
327         debug_print("Destroying Bogofilter widget\n");
328 }
329
330 static void bogofilter_save_func(PrefsPage *_page)
331 {
332         struct BogofilterPage *page = (struct BogofilterPage *) _page;
333         BogofilterConfig *config;
334
335         debug_print("Saving Bogofilter Page\n");
336
337         config = bogofilter_get_config();
338
339         /* process_emails */
340         config->process_emails = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->process_emails));
341
342         /* receive_spam */
343         config->receive_spam = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->receive_spam));
344
345         /* save_folder */
346         g_free(config->save_folder);
347         config->save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1);
348
349         /* save_unsure */
350         config->save_unsure = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->save_unsure));
351
352         /* save_unsure_folder */
353         g_free(config->save_unsure_folder);
354         config->save_unsure_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_unsure_folder), 0, -1);
355
356         /* insert_header */
357         config->insert_header = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->insert_header));
358
359         /* whitelist_ab */
360         config->whitelist_ab = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->whitelist_ab));
361         g_free(config->whitelist_ab_folder);
362         config->whitelist_ab_folder = gtk_editable_get_chars(
363                                 GTK_EDITABLE(GTK_COMBO(page->whitelist_ab_folder_combo)->entry), 0, -1);
364
365         /* bogopath */
366         g_free(config->bogopath);
367         config->bogopath = gtk_editable_get_chars(GTK_EDITABLE(page->bogopath), 0, -1);
368
369         /* max_size */
370         config->max_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->max_size));
371
372         if (config->process_emails) {
373                 bogofilter_register_hook();
374         } else {
375                 bogofilter_unregister_hook();
376         }
377
378         procmsg_register_spam_learner(bogofilter_learn);
379         procmsg_spam_set_folder(config->save_folder, bogofilter_get_spam_folder);
380
381         bogofilter_save_config();
382 }
383
384 typedef struct _BogoCbData {
385         gchar *message;
386         gint total;
387         gint done;
388 } BogoCbData;
389
390 static gboolean gtk_message_callback(gpointer data)
391 {
392         BogoCbData *cbdata = (BogoCbData *)data;
393
394         if (cbdata->message)
395                 statusbar_print_all(cbdata->message);
396         else if (cbdata->total == 0) {
397                 statusbar_pop_all();
398         }
399         if (cbdata->total && cbdata->done)
400                 statusbar_progress_all(cbdata->done, cbdata->total, 10);
401         else
402                 statusbar_progress_all(0,0,0);
403         g_free(cbdata->message);
404         g_free(cbdata);
405         GTK_EVENTS_FLUSH();
406         return FALSE;
407 }
408
409 static void gtk_safe_message_callback(gchar *message, gint total, gint done, gboolean thread_safe)
410 {
411         BogoCbData *cbdata = g_new0(BogoCbData, 1);
412         if (message)
413                 cbdata->message = g_strdup(message);
414         cbdata->total = total;
415         cbdata->done = done;
416         if (thread_safe)
417                 g_timeout_add(0, gtk_message_callback, cbdata);
418         else
419                 gtk_message_callback(cbdata);
420 }
421
422 static struct BogofilterPage bogofilter_page;
423
424 gint bogofilter_gtk_init(void)
425 {
426         static gchar *path[3];
427
428         path[0] = _("Plugins");
429         path[1] = _("Bogofilter");
430         path[2] = NULL;
431
432         bogofilter_page.page.path = path;
433         bogofilter_page.page.create_widget = bogofilter_create_widget_func;
434         bogofilter_page.page.destroy_widget = bogofilter_destroy_widget_func;
435         bogofilter_page.page.save_page = bogofilter_save_func;
436         bogofilter_page.page.weight = 35.0;
437
438         prefs_gtk_register_page((PrefsPage *) &bogofilter_page);
439         bogofilter_set_message_callback(gtk_safe_message_callback);
440
441         debug_print("Bogofilter GTK plugin loaded\n");
442         return 0;       
443 }
444
445 void bogofilter_gtk_done(void)
446 {
447         prefs_gtk_unregister_page((PrefsPage *) &bogofilter_page);
448 }