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>
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/sylpheed.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;
55 static void foldersel_cb(GtkWidget *widget, gpointer data)
57 struct BogofilterPage *page = (struct BogofilterPage *) data;
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);
70 static void bogofilter_create_widget_func(PrefsPage * _page,
74 struct BogofilterPage *page = (struct BogofilterPage *) _page;
75 BogofilterConfig *config;
77 GtkWidget *vbox1, *vbox2;
78 GtkWidget *hbox_max_size;
79 GtkWidget *hbox_process_emails, *hbox_save_spam;
81 GtkWidget *max_size_label;
82 GtkObject *max_size_spinbtn_adj;
83 GtkWidget *max_size_spinbtn;
84 GtkWidget *max_size_kb_label;
86 GtkWidget *process_emails_checkbtn;
88 GtkWidget *save_spam_checkbtn;
89 GtkWidget *save_spam_folder_entry;
90 GtkWidget *save_spam_folder_select;
92 GtkTooltips *tooltips;
94 tooltips = gtk_tooltips_new();
96 vbox1 = gtk_vbox_new (FALSE, VSPACING);
97 gtk_widget_show (vbox1);
98 gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
100 vbox2 = gtk_vbox_new (FALSE, 4);
101 gtk_widget_show (vbox2);
102 gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
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);
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);
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);
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);
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);
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);
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);
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);
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"),
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"),
155 SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, save_spam_folder_entry);
156 SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, save_spam_folder_select);
158 config = bogofilter_get_config();
160 g_signal_connect(G_OBJECT(save_spam_folder_select), "clicked",
161 G_CALLBACK(foldersel_cb), page);
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);
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;
175 page->page.widget = vbox1;
178 static void bogofilter_destroy_widget_func(PrefsPage *_page)
180 debug_print("Destroying Bogofilter widget\n");
183 static void bogofilter_save_func(PrefsPage *_page)
185 struct BogofilterPage *page = (struct BogofilterPage *) _page;
186 BogofilterConfig *config;
188 debug_print("Saving Bogofilter Page\n");
190 config = bogofilter_get_config();
193 config->process_emails = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->process_emails));
196 config->receive_spam = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->receive_spam));
199 g_free(config->save_folder);
200 config->save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1);
203 config->max_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->max_size));
205 if (config->process_emails) {
206 bogofilter_register_hook();
208 bogofilter_unregister_hook();
211 procmsg_register_spam_learner(bogofilter_learn);
212 procmsg_spam_set_folder(config->save_folder);
214 bogofilter_save_config();
217 static void gtk_message_callback(gchar *message, gint total, gint done)
220 statusbar_print_all(message);
221 else if (total == 0) {
225 statusbar_progress_all(done, total, 10);
227 statusbar_progress_all(0,0,0);
231 static struct BogofilterPage bogofilter_page;
233 gint bogofilter_gtk_init(void)
235 static gchar *path[3];
237 path[0] = _("Plugins");
238 path[1] = _("Bogofilter");
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;
247 prefs_gtk_register_page((PrefsPage *) &bogofilter_page);
248 bogofilter_set_message_callback(gtk_message_callback);
250 debug_print("Bogofilter GTK plugin loaded\n");
254 void bogofilter_gtk_done(void)
256 prefs_gtk_unregister_page((PrefsPage *) &bogofilter_page);