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>
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.
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.
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.
28 #include <glib/gi18n.h>
30 #include <gtk/gtkutils.h>
32 #include "common/claws.h"
33 #include "common/version.h"
35 #include "common/utils.h"
38 #include "prefs_gtk.h"
39 #include "foldersel.h"
40 #include "statusbar.h"
41 #include "bogofilter.h"
48 GtkWidget *process_emails;
49 GtkWidget *receive_spam;
50 GtkWidget *save_folder;
51 GtkWidget *save_folder_select;
56 static void foldersel_cb(GtkWidget *widget, gpointer data)
58 struct BogofilterPage *page = (struct BogofilterPage *) data;
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);
71 static void bogofilter_create_widget_func(PrefsPage * _page,
75 struct BogofilterPage *page = (struct BogofilterPage *) _page;
76 BogofilterConfig *config;
78 GtkWidget *vbox1, *vbox2;
79 GtkWidget *hbox_max_size;
80 GtkWidget *hbox_process_emails, *hbox_save_spam, *hbox_bogopath;
82 GtkWidget *max_size_label;
83 GtkObject *max_size_spinbtn_adj;
84 GtkWidget *max_size_spinbtn;
85 GtkWidget *max_size_kb_label;
87 GtkWidget *process_emails_checkbtn;
89 GtkWidget *save_spam_checkbtn;
90 GtkWidget *save_spam_folder_entry;
91 GtkWidget *save_spam_folder_select;
93 GtkWidget *bogopath_label;
94 GtkWidget *bogopath_entry;
96 GtkTooltips *tooltips;
98 tooltips = gtk_tooltips_new();
100 vbox1 = gtk_vbox_new (FALSE, VSPACING);
101 gtk_widget_show (vbox1);
102 gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
104 vbox2 = gtk_vbox_new (FALSE, 4);
105 gtk_widget_show (vbox2);
106 gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
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);
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);
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);
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);
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);
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);
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);
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);
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"),
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"),
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);
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);
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"),
174 SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, save_spam_folder_entry);
175 SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, save_spam_folder_select);
177 config = bogofilter_get_config();
179 g_signal_connect(G_OBJECT(save_spam_folder_select), "clicked",
180 G_CALLBACK(foldersel_cb), page);
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);
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;
197 page->page.widget = vbox1;
200 static void bogofilter_destroy_widget_func(PrefsPage *_page)
202 debug_print("Destroying Bogofilter widget\n");
205 static void bogofilter_save_func(PrefsPage *_page)
207 struct BogofilterPage *page = (struct BogofilterPage *) _page;
208 BogofilterConfig *config;
210 debug_print("Saving Bogofilter Page\n");
212 config = bogofilter_get_config();
215 config->process_emails = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->process_emails));
218 config->receive_spam = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->receive_spam));
221 g_free(config->save_folder);
222 config->save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1);
225 g_free(config->bogopath);
226 config->bogopath = gtk_editable_get_chars(GTK_EDITABLE(page->bogopath), 0, -1);
229 config->max_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->max_size));
231 if (config->process_emails) {
232 bogofilter_register_hook();
234 bogofilter_unregister_hook();
237 procmsg_register_spam_learner(bogofilter_learn);
238 procmsg_spam_set_folder(config->save_folder);
240 bogofilter_save_config();
243 typedef struct _BogoCbData {
249 static gboolean gtk_message_callback(gpointer data)
251 BogoCbData *cbdata = (BogoCbData *)data;
254 statusbar_print_all(cbdata->message);
255 else if (cbdata->total == 0) {
258 if (cbdata->total && cbdata->done)
259 statusbar_progress_all(cbdata->done, cbdata->total, 10);
261 statusbar_progress_all(0,0,0);
262 g_free(cbdata->message);
268 static void gtk_safe_message_callback(gchar *message, gint total, gint done, gboolean thread_safe)
270 BogoCbData *cbdata = g_new0(BogoCbData, 1);
272 cbdata->message = g_strdup(message);
273 cbdata->total = total;
276 g_timeout_add(0, gtk_message_callback, cbdata);
278 gtk_message_callback(cbdata);
281 static struct BogofilterPage bogofilter_page;
283 gint bogofilter_gtk_init(void)
285 static gchar *path[3];
287 path[0] = _("Plugins");
288 path[1] = _("Bogofilter");
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;
297 prefs_gtk_register_page((PrefsPage *) &bogofilter_page);
298 bogofilter_set_message_callback(gtk_safe_message_callback);
300 debug_print("Bogofilter GTK plugin loaded\n");
304 void bogofilter_gtk_done(void)
306 prefs_gtk_unregister_page((PrefsPage *) &bogofilter_page);