2006-12-15 [paul] 2.6.1cvs36
[claws.git] / src / plugins / bogofilter / bogofilter_gtk.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail Team
4  * This file Copyright (C) 2006 Colin Leroy <colin@colino.net>
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
44 struct BogofilterPage
45 {
46         PrefsPage page;
47
48         GtkWidget *process_emails;
49         GtkWidget *receive_spam;
50         GtkWidget *save_folder;
51         GtkWidget *save_folder_select;
52         GtkWidget *insert_header;
53         GtkWidget *max_size;
54         GtkWidget *bogopath;
55 };
56
57 static void foldersel_cb(GtkWidget *widget, gpointer data)
58 {
59         struct BogofilterPage *page = (struct BogofilterPage *) data;
60         FolderItem *item;
61         gchar *item_id;
62         gint newpos = 0;
63         
64         item = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL);
65         if (item && (item_id = folder_item_get_identifier(item)) != NULL) {
66                 gtk_editable_delete_text(GTK_EDITABLE(page->save_folder), 0, -1);
67                 gtk_editable_insert_text(GTK_EDITABLE(page->save_folder), item_id, strlen(item_id), &newpos);
68                 g_free(item_id);
69         }
70 }
71
72 static void bogofilter_create_widget_func(PrefsPage * _page,
73                                             GtkWindow * window,
74                                             gpointer data)
75 {
76         struct BogofilterPage *page = (struct BogofilterPage *) _page;
77         BogofilterConfig *config;
78
79         GtkWidget *vbox1, *vbox2;
80         GtkWidget *hbox_max_size;
81         GtkWidget *hbox_process_emails, *hbox_save_spam, *hbox_bogopath;
82
83         GtkWidget *max_size_label;
84         GtkObject *max_size_spinbtn_adj;
85         GtkWidget *max_size_spinbtn;
86         GtkWidget *max_size_kb_label;
87
88         GtkWidget *process_emails_checkbtn;
89
90         GtkWidget *save_spam_checkbtn;
91         GtkWidget *save_spam_folder_entry;
92         GtkWidget *save_spam_folder_select;
93
94         GtkWidget *insert_header_chkbtn;
95         GtkWidget *bogopath_label;
96         GtkWidget *bogopath_entry;
97
98         GtkTooltips *tooltips;
99
100         tooltips = gtk_tooltips_new();
101
102         vbox1 = gtk_vbox_new (FALSE, VSPACING);
103         gtk_widget_show (vbox1);
104         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
105
106         vbox2 = gtk_vbox_new (FALSE, 4);
107         gtk_widget_show (vbox2);
108         gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
109
110         hbox_process_emails = gtk_hbox_new(FALSE, 8);
111         gtk_widget_show(hbox_process_emails);
112         gtk_box_pack_start (GTK_BOX (vbox2), hbox_process_emails, TRUE, TRUE, 0);
113
114         process_emails_checkbtn = gtk_check_button_new_with_label(
115                         _("Process messages on receiving"));
116         gtk_widget_show(process_emails_checkbtn);
117         gtk_box_pack_start(GTK_BOX(hbox_process_emails), process_emails_checkbtn, TRUE, TRUE, 0);
118
119         hbox_max_size = gtk_hbox_new(FALSE, 8);
120         gtk_widget_show(hbox_max_size);
121         gtk_box_pack_start (GTK_BOX (vbox2), hbox_max_size, TRUE, TRUE, 0);
122
123         max_size_label = gtk_label_new(_("Maximum size"));
124         gtk_widget_show(max_size_label);
125         gtk_box_pack_start(GTK_BOX(hbox_max_size), max_size_label, FALSE, FALSE, 0);
126
127         max_size_spinbtn_adj = gtk_adjustment_new(250, 0, 10000, 10, 10, 10);
128         max_size_spinbtn = gtk_spin_button_new(GTK_ADJUSTMENT(max_size_spinbtn_adj), 1, 0);
129         gtk_widget_show(max_size_spinbtn);
130         gtk_box_pack_start(GTK_BOX(hbox_max_size), max_size_spinbtn, FALSE, FALSE, 0);
131         gtk_tooltips_set_tip(tooltips, max_size_spinbtn,
132                         _("Messages larger than this will not be checked"), NULL);
133         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(max_size_spinbtn), TRUE);
134
135         max_size_kb_label = gtk_label_new(_("kB"));
136         gtk_widget_show(max_size_kb_label);
137         gtk_box_pack_start(GTK_BOX(hbox_max_size), max_size_kb_label, FALSE, FALSE, 0);
138
139         hbox_save_spam = gtk_hbox_new(FALSE, 8);
140         gtk_widget_show(hbox_save_spam);
141         gtk_box_pack_start (GTK_BOX (vbox2), hbox_save_spam, TRUE, TRUE, 0);
142
143         save_spam_checkbtn = gtk_check_button_new_with_label(_("Save spam in"));
144         gtk_widget_show(save_spam_checkbtn);
145         gtk_box_pack_start(GTK_BOX(hbox_save_spam), save_spam_checkbtn, FALSE, FALSE, 0);
146
147         save_spam_folder_entry = gtk_entry_new();
148         gtk_widget_show (save_spam_folder_entry);
149         gtk_box_pack_start (GTK_BOX (hbox_save_spam), save_spam_folder_entry, TRUE, TRUE, 0);
150         gtk_tooltips_set_tip(tooltips, save_spam_folder_entry,
151                         _("Folder for storing identified spam. Leave empty to use the default trash folder"),
152                         NULL);
153
154         save_spam_folder_select = gtkut_get_browse_directory_btn(_("_Browse"));
155         gtk_widget_show (save_spam_folder_select);
156         gtk_box_pack_start (GTK_BOX (hbox_save_spam), save_spam_folder_select, FALSE, FALSE, 0);
157         gtk_tooltips_set_tip(tooltips, save_spam_folder_select,
158                         _("Click this button to select a folder for storing spam"),
159                         NULL);
160
161         insert_header_chkbtn = gtk_check_button_new_with_label(_("Insert X-Bogosity header"));
162         gtk_widget_show(insert_header_chkbtn);
163         gtk_box_pack_start(GTK_BOX(vbox2), insert_header_chkbtn, FALSE, FALSE, 0);
164         gtk_tooltips_set_tip(tooltips, insert_header_chkbtn,
165                         _("Only done for mails in MH folders"),
166                         NULL);
167
168         hbox_bogopath = gtk_hbox_new(FALSE, 8);
169         gtk_widget_show(hbox_bogopath);
170         gtk_box_pack_start (GTK_BOX (vbox2), hbox_bogopath, FALSE, FALSE, 0);
171
172         bogopath_label = gtk_label_new(_("Bogofilter call"));
173         gtk_widget_show(bogopath_label);
174         gtk_box_pack_start(GTK_BOX(hbox_bogopath), bogopath_label, FALSE, FALSE, 0);
175
176         bogopath_entry = gtk_entry_new();
177         gtk_widget_show(bogopath_entry);
178         gtk_box_pack_start(GTK_BOX(hbox_bogopath), bogopath_entry, FALSE, FALSE, 0);
179         gtk_tooltips_set_tip(tooltips, bogopath_entry,
180                         _("Path to bogofilter executable"),
181                         NULL);
182
183         SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, save_spam_folder_entry);
184         SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, save_spam_folder_select);
185         SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, insert_header_chkbtn);
186
187         config = bogofilter_get_config();
188
189         g_signal_connect(G_OBJECT(save_spam_folder_select), "clicked",
190                         G_CALLBACK(foldersel_cb), page);
191
192         gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_size_spinbtn), (float) config->max_size);
193         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(process_emails_checkbtn), config->process_emails);
194         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(save_spam_checkbtn), config->receive_spam);
195         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(insert_header_chkbtn), config->insert_header);
196         if (config->save_folder != NULL)
197                 gtk_entry_set_text(GTK_ENTRY(save_spam_folder_entry), config->save_folder);
198         if (config->bogopath != NULL)
199                 gtk_entry_set_text(GTK_ENTRY(bogopath_entry), config->bogopath);
200
201         page->max_size = max_size_spinbtn;
202         page->process_emails = process_emails_checkbtn;
203         page->receive_spam = save_spam_checkbtn;
204         page->insert_header = insert_header_chkbtn;
205         page->save_folder = save_spam_folder_entry;
206         page->save_folder_select = save_spam_folder_select;
207         page->bogopath = bogopath_entry;
208
209         page->page.widget = vbox1;
210 }
211
212 static void bogofilter_destroy_widget_func(PrefsPage *_page)
213 {
214         debug_print("Destroying Bogofilter widget\n");
215 }
216
217 static void bogofilter_save_func(PrefsPage *_page)
218 {
219         struct BogofilterPage *page = (struct BogofilterPage *) _page;
220         BogofilterConfig *config;
221
222         debug_print("Saving Bogofilter Page\n");
223
224         config = bogofilter_get_config();
225
226         /* process_emails */
227         config->process_emails = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->process_emails));
228
229         /* receive_spam */
230         config->receive_spam = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->receive_spam));
231
232         /* save_folder */
233         g_free(config->save_folder);
234         config->save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1);
235
236         /* insert_header */
237         config->insert_header = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->insert_header));
238
239         /* bogopath */
240         g_free(config->bogopath);
241         config->bogopath = gtk_editable_get_chars(GTK_EDITABLE(page->bogopath), 0, -1);
242
243         /* max_size */
244         config->max_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->max_size));
245
246         if (config->process_emails) {
247                 bogofilter_register_hook();
248         } else {
249                 bogofilter_unregister_hook();
250         }
251
252         procmsg_register_spam_learner(bogofilter_learn);
253         procmsg_spam_set_folder(config->save_folder);
254
255         bogofilter_save_config();
256 }
257
258 typedef struct _BogoCbData {
259         gchar *message;
260         gint total;
261         gint done;
262 } BogoCbData;
263
264 static gboolean gtk_message_callback(gpointer data)
265 {
266         BogoCbData *cbdata = (BogoCbData *)data;
267
268         if (cbdata->message)
269                 statusbar_print_all(cbdata->message);
270         else if (cbdata->total == 0) {
271                 statusbar_pop_all();
272         }
273         if (cbdata->total && cbdata->done)
274                 statusbar_progress_all(cbdata->done, cbdata->total, 10);
275         else
276                 statusbar_progress_all(0,0,0);
277         g_free(cbdata->message);
278         g_free(cbdata);
279         GTK_EVENTS_FLUSH();
280         return FALSE;
281 }
282
283 static void gtk_safe_message_callback(gchar *message, gint total, gint done, gboolean thread_safe)
284 {
285         BogoCbData *cbdata = g_new0(BogoCbData, 1);
286         if (message)
287                 cbdata->message = g_strdup(message);
288         cbdata->total = total;
289         cbdata->done = done;
290         if (thread_safe)
291                 g_timeout_add(0, gtk_message_callback, cbdata);
292         else
293                 gtk_message_callback(cbdata);
294 }
295
296 static struct BogofilterPage bogofilter_page;
297
298 gint bogofilter_gtk_init(void)
299 {
300         static gchar *path[3];
301
302         path[0] = _("Plugins");
303         path[1] = _("Bogofilter");
304         path[2] = NULL;
305
306         bogofilter_page.page.path = path;
307         bogofilter_page.page.create_widget = bogofilter_create_widget_func;
308         bogofilter_page.page.destroy_widget = bogofilter_destroy_widget_func;
309         bogofilter_page.page.save_page = bogofilter_save_func;
310         bogofilter_page.page.weight = 35.0;
311
312         prefs_gtk_register_page((PrefsPage *) &bogofilter_page);
313         bogofilter_set_message_callback(gtk_safe_message_callback);
314
315         debug_print("Bogofilter GTK plugin loaded\n");
316         return 0;       
317 }
318
319 void bogofilter_gtk_done(void)
320 {
321         prefs_gtk_unregister_page((PrefsPage *) &bogofilter_page);
322 }