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