2006-08-27 [colin] 2.4.0cvs100
[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 Sylpheed-Claws 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(
164                         _("Bogofilter call: "));
165         gtk_widget_show(bogopath_label);
166         gtk_box_pack_start(GTK_BOX(hbox_bogopath), bogopath_label, FALSE, FALSE, 0);
167
168         bogopath_entry = gtk_entry_new();
169         gtk_widget_show(bogopath_entry);
170         gtk_box_pack_start(GTK_BOX(hbox_bogopath), bogopath_entry, FALSE, FALSE, 0);
171
172         SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, save_spam_folder_entry);
173         SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, save_spam_folder_select);
174
175         config = bogofilter_get_config();
176
177         g_signal_connect(G_OBJECT(save_spam_folder_select), "clicked",
178                         G_CALLBACK(foldersel_cb), page);
179
180         gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_size_spinbtn), (float) config->max_size);
181         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(process_emails_checkbtn), config->process_emails);
182         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(save_spam_checkbtn), config->receive_spam);
183         if (config->save_folder != NULL)
184                 gtk_entry_set_text(GTK_ENTRY(save_spam_folder_entry), config->save_folder);
185         if (config->bogopath != NULL)
186                 gtk_entry_set_text(GTK_ENTRY(bogopath_entry), config->bogopath);
187
188         page->max_size = max_size_spinbtn;
189         page->process_emails = process_emails_checkbtn;
190         page->receive_spam = save_spam_checkbtn;
191         page->save_folder = save_spam_folder_entry;
192         page->save_folder_select = save_spam_folder_select;
193         page->bogopath = bogopath_entry;
194
195         page->page.widget = vbox1;
196 }
197
198 static void bogofilter_destroy_widget_func(PrefsPage *_page)
199 {
200         debug_print("Destroying Bogofilter widget\n");
201 }
202
203 static void bogofilter_save_func(PrefsPage *_page)
204 {
205         struct BogofilterPage *page = (struct BogofilterPage *) _page;
206         BogofilterConfig *config;
207
208         debug_print("Saving Bogofilter Page\n");
209
210         config = bogofilter_get_config();
211
212         /* process_emails */
213         config->process_emails = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->process_emails));
214
215         /* receive_spam */
216         config->receive_spam = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->receive_spam));
217
218         /* save_folder */
219         g_free(config->save_folder);
220         config->save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1);
221
222         /* bogopath */
223         g_free(config->bogopath);
224         config->bogopath = gtk_editable_get_chars(GTK_EDITABLE(page->bogopath), 0, -1);
225
226         /* max_size */
227         config->max_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->max_size));
228
229         if (config->process_emails) {
230                 bogofilter_register_hook();
231         } else {
232                 bogofilter_unregister_hook();
233         }
234
235         procmsg_register_spam_learner(bogofilter_learn);
236         procmsg_spam_set_folder(config->save_folder);
237
238         bogofilter_save_config();
239 }
240
241 static void gtk_message_callback(gchar *message, gint total, gint done)
242 {
243         if (message)
244                 statusbar_print_all(message);
245         else if (total == 0) {
246                 statusbar_pop_all();
247         }
248         if (total && done)
249                 statusbar_progress_all(done, total, 10);
250         else
251                 statusbar_progress_all(0,0,0);
252         GTK_EVENTS_FLUSH();
253 }
254
255 static struct BogofilterPage bogofilter_page;
256
257 gint bogofilter_gtk_init(void)
258 {
259         static gchar *path[3];
260
261         path[0] = _("Plugins");
262         path[1] = _("Bogofilter");
263         path[2] = NULL;
264
265         bogofilter_page.page.path = path;
266         bogofilter_page.page.create_widget = bogofilter_create_widget_func;
267         bogofilter_page.page.destroy_widget = bogofilter_destroy_widget_func;
268         bogofilter_page.page.save_page = bogofilter_save_func;
269         bogofilter_page.page.weight = 35.0;
270
271         prefs_gtk_register_page((PrefsPage *) &bogofilter_page);
272         bogofilter_set_message_callback(gtk_message_callback);
273
274         debug_print("Bogofilter GTK plugin loaded\n");
275         return 0;       
276 }
277
278 void bogofilter_gtk_done(void)
279 {
280         prefs_gtk_unregister_page((PrefsPage *) &bogofilter_page);
281 }