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