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