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