93709edc2f056895ecc33b03b07ff756d00bcdf1
[claws.git] / src / plugins / spamassassin / spamassassin_gtk.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 the Claws Mail Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include "gtk/gtkutils.h"
30
31 #include "common/claws.h"
32 #include "common/version.h"
33 #include "plugin.h"
34 #include "common/utils.h"
35 #include "prefs.h"
36 #include "folder.h"
37 #include "prefs_gtk.h"
38 #include "foldersel.h"
39 #include "spamassassin.h"
40 #include "statusbar.h"
41 #include "menu.h"
42 #include "addressbook.h"
43 #include "combobox.h"
44
45 struct SpamAssassinPage
46 {
47         PrefsPage page;
48
49         GtkWidget *enable_sa_checkbtn;
50         GtkWidget *transport_optmenu;
51         GtkWidget *transport_label;
52         GtkWidget *username;
53         GtkWidget *hostname;
54         GtkWidget *colon;
55         GtkWidget *port;
56         GtkWidget *socket;
57         GtkWidget *process_emails;
58         GtkWidget *receive_spam;
59         GtkWidget *save_folder;
60         GtkWidget *save_folder_select;
61         GtkWidget *max_size;
62         GtkWidget *timeout;
63         GtkWidget *mark_as_read;
64         GtkWidget *whitelist_ab;
65         GtkWidget *whitelist_ab_folder_combo;
66
67         SpamAssassinTransport   trans;
68 };
69
70 struct Transport
71 {
72         gchar                   *name;
73         SpamAssassinTransport    transport;
74         guint                    page;
75         guint                    pageflags;
76 };
77
78 enum {
79         PAGE_DISABLED = 0,
80         PAGE_NETWORK  = 1,
81         PAGE_UNIX     = 2,
82 };
83
84 enum {
85         NETWORK_HOSTNAME = 1,
86 };
87
88 struct Transport transports[] = {
89         /*{ N_("Disabled"),     SPAMASSASSIN_DISABLED,                  PAGE_DISABLED, 0 },*/
90         { N_("Localhost"),      SPAMASSASSIN_TRANSPORT_LOCALHOST,       PAGE_NETWORK, 0 },
91         { N_("TCP"),            SPAMASSASSIN_TRANSPORT_TCP,             PAGE_NETWORK, NETWORK_HOSTNAME },
92         { N_("Unix Socket"),    SPAMASSASSIN_TRANSPORT_UNIX,            PAGE_UNIX,    0 },
93 };
94
95 static void spamassassin_whitelist_ab_select_cb(GtkWidget *widget, gpointer data)
96 {
97         struct SpamAssassinPage *page = (struct SpamAssassinPage *) data;
98         const gchar *folderpath = NULL;
99         gchar *new_path = NULL;
100
101         folderpath = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((page->whitelist_ab_folder_combo)))));
102         new_path = addressbook_folder_selection(folderpath);
103         if (new_path) {
104                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((page->whitelist_ab_folder_combo)))), new_path);
105                 g_free(new_path);
106         } 
107 }
108
109 static void foldersel_cb(GtkWidget *widget, gpointer data)
110 {
111         struct SpamAssassinPage *page = (struct SpamAssassinPage *) data;
112         FolderItem *item;
113         gchar *item_id;
114         gint newpos = 0;
115         
116         item = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL, FALSE);
117         if (item && (item_id = folder_item_get_identifier(item)) != NULL) {
118                 gtk_editable_delete_text(GTK_EDITABLE(page->save_folder), 0, -1);
119                 gtk_editable_insert_text(GTK_EDITABLE(page->save_folder), item_id, strlen(item_id), &newpos);
120                 g_free(item_id);
121         }
122 }
123
124 static void show_transport(struct SpamAssassinPage *page, struct Transport *transport)
125 {
126         page->trans = transport->transport;
127
128         switch (transport->page) {
129                 /*
130         case PAGE_DISABLED:
131                 gtk_widget_show(page->hostname);
132                 gtk_widget_show(page->colon);
133                 gtk_widget_show(page->port);
134                 gtk_widget_hide(page->socket);
135                 gtk_widget_set_sensitive(page->username, FALSE);
136                 gtk_widget_set_sensitive(page->hostname, FALSE);
137                 gtk_widget_set_sensitive(page->colon, FALSE);
138                 gtk_widget_set_sensitive(page->port, FALSE);
139                 gtk_widget_set_sensitive(page->max_size, FALSE);
140                 gtk_widget_set_sensitive(page->timeout, FALSE);
141                 gtk_widget_set_sensitive(page->process_emails, FALSE);
142                 gtk_widget_set_sensitive(page->receive_spam, FALSE);
143                 gtk_widget_set_sensitive(page->save_folder, FALSE);
144                 gtk_widget_set_sensitive(page->save_folder_select, FALSE);
145                 break;
146                 */
147         case PAGE_UNIX:
148                 gtk_widget_hide(page->hostname);
149                 gtk_widget_hide(page->colon);
150                 gtk_widget_hide(page->port);
151                 gtk_widget_show(page->socket);
152                 gtk_widget_set_sensitive(page->username, TRUE);
153                 gtk_widget_set_sensitive(page->socket, TRUE);
154                 gtk_widget_set_sensitive(page->max_size, TRUE);
155                 gtk_widget_set_sensitive(page->timeout, TRUE);
156                 gtk_widget_set_sensitive(page->process_emails, TRUE);
157                 gtk_widget_set_sensitive(page->receive_spam, TRUE);
158                 gtk_widget_set_sensitive(page->save_folder, TRUE);
159                 gtk_widget_set_sensitive(page->save_folder_select, TRUE);
160                 break;
161         case PAGE_NETWORK:
162                 gtk_widget_show(page->hostname);
163                 gtk_widget_show(page->colon);
164                 gtk_widget_show(page->port);
165                 gtk_widget_hide(page->socket);
166                 gtk_widget_set_sensitive(page->username, TRUE);
167                 gtk_widget_set_sensitive(page->max_size, TRUE);
168                 gtk_widget_set_sensitive(page->timeout, TRUE);
169                 gtk_widget_set_sensitive(page->process_emails, TRUE);
170                 gtk_widget_set_sensitive(page->receive_spam, TRUE);
171                 gtk_widget_set_sensitive(page->save_folder, TRUE);
172                 gtk_widget_set_sensitive(page->save_folder_select, TRUE);
173                 if (transport->pageflags & NETWORK_HOSTNAME) {
174                         gtk_widget_set_sensitive(page->hostname, TRUE);
175                         gtk_widget_set_sensitive(page->colon, TRUE);
176                         gtk_widget_set_sensitive(page->port, TRUE);
177                 } else {
178                         gtk_widget_set_sensitive(page->hostname, FALSE);
179                         gtk_widget_set_sensitive(page->colon, FALSE);
180                         gtk_widget_set_sensitive(page->port, TRUE);
181                 }
182                 break;
183         default:
184                 break;
185         }
186 }
187
188 static void transport_sel_cb(GtkMenuItem *menuitem, gpointer data)
189 {
190         struct SpamAssassinPage *page = (struct SpamAssassinPage *) data;
191         struct Transport *transport;
192         GtkTreeModel *model;
193         GtkTreeIter iter;
194         
195         cm_return_if_fail(gtk_combo_box_get_active_iter(
196                         GTK_COMBO_BOX(page->transport_optmenu), &iter));
197         
198         model = gtk_combo_box_get_model(GTK_COMBO_BOX(page->transport_optmenu));                        
199         gtk_tree_model_get(model, &iter, 1, &transport, -1);
200
201         show_transport(page, transport);
202 }
203
204 static void spamassassin_create_widget_func(PrefsPage * _page,
205                                             GtkWindow * window,
206                                             gpointer data)
207 {
208         struct SpamAssassinPage *page = (struct SpamAssassinPage *) _page;
209         SpamAssassinConfig *config;
210         guint i, active;
211
212         GtkWidget *vbox1, *vbox2;
213         GtkWidget *frame_transport, *table_transport, *vbox_transport;
214         GtkWidget *hbox_spamd, *hbox_max_size, *hbox_timeout;
215         GtkWidget *hbox_process_emails, *hbox_save_spam;
216         GtkWidget *hbox_mark_as_read, *hbox_whitelist;
217         GtkWidget *whitelist_ab_checkbtn;
218         GtkWidget *whitelist_ab_folder_combo;
219         GtkWidget *whitelist_ab_select_btn;
220
221         GtkWidget *enable_sa_checkbtn;
222
223         GtkWidget *transport_label;
224         GtkWidget *transport_optmenu;
225
226         GtkWidget *user_label;
227         GtkWidget *user_entry;
228
229         GtkWidget *spamd_label;
230         GtkWidget *spamd_hostname_entry;
231         GtkWidget *spamd_colon_label;
232         GtkAdjustment *spamd_port_spinbtn_adj;
233         GtkWidget *spamd_port_spinbtn;
234         GtkWidget *spamd_socket_entry;
235
236         GtkWidget *max_size_label;
237         GtkAdjustment *max_size_spinbtn_adj;
238         GtkWidget *max_size_spinbtn;
239         GtkWidget *max_size_kb_label;
240
241         GtkWidget *timeout_label;
242         GtkAdjustment *timeout_spinbtn_adj;
243         GtkWidget *timeout_spinbtn;
244         GtkWidget *timeout_seconds_label;
245
246         GtkWidget *process_emails_checkbtn;
247
248         GtkWidget *save_spam_checkbtn;
249         GtkWidget *save_spam_folder_entry;
250         GtkWidget *save_spam_folder_select;
251
252         GtkWidget *mark_as_read_checkbtn;
253
254         GtkListStore *store;
255         GtkCellRenderer *renderer;
256         GtkTreeIter iter;
257
258         vbox1 = gtk_vbox_new (FALSE, VSPACING);
259         gtk_widget_show (vbox1);
260         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
261
262         vbox2 = gtk_vbox_new (FALSE, 4);
263         gtk_widget_show (vbox2);
264         gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
265
266         enable_sa_checkbtn = gtk_check_button_new_with_label(_("Enable SpamAssassin plugin"));
267         gtk_widget_show(enable_sa_checkbtn);
268         gtk_box_pack_start(GTK_BOX(vbox2), enable_sa_checkbtn, TRUE, TRUE, 0);
269
270         vbox_transport = gtkut_get_options_frame(vbox2, &frame_transport, _("Transport"));
271
272         table_transport = gtk_table_new (3, 3, FALSE);
273         gtk_widget_show (table_transport);
274         gtk_box_pack_start(GTK_BOX(vbox_transport), table_transport, TRUE, TRUE, 0);
275         gtk_table_set_row_spacings (GTK_TABLE (table_transport), 4);
276         gtk_table_set_col_spacings (GTK_TABLE (table_transport), 8);
277
278         transport_label = gtk_label_new(_("Type of transport"));
279         gtk_widget_show(transport_label);
280         gtk_table_attach (GTK_TABLE (table_transport), transport_label, 0, 1, 0, 1,
281                         (GtkAttachOptions) (GTK_FILL),
282                         (GtkAttachOptions) (0), 0, 0);
283         gtk_label_set_justify(GTK_LABEL(transport_label), GTK_JUSTIFY_RIGHT);
284         gtk_misc_set_alignment(GTK_MISC(transport_label), 1, 0.5);
285
286         store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
287         transport_optmenu = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
288         renderer = gtk_cell_renderer_text_new();
289         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(transport_optmenu),
290                                    renderer, TRUE);
291         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(transport_optmenu),
292                                        renderer, "text", 0, NULL);
293         gtk_widget_show(transport_optmenu);
294
295         gtk_table_attach (GTK_TABLE (table_transport), transport_optmenu, 1, 2, 0, 1,
296                         (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
297                         (GtkAttachOptions) (0), 0, 0);
298
299         user_label = gtk_label_new(_("User"));
300         gtk_widget_show(user_label);
301         gtk_table_attach (GTK_TABLE (table_transport), user_label, 0, 1, 1, 2,
302                         (GtkAttachOptions) (GTK_FILL),
303                         (GtkAttachOptions) (0), 0, 0);
304         gtk_label_set_justify(GTK_LABEL(user_label), GTK_JUSTIFY_RIGHT);
305         gtk_misc_set_alignment(GTK_MISC(user_label), 1, 0.5);
306
307         user_entry = gtk_entry_new();
308         gtk_widget_show(user_entry);
309         gtk_table_attach (GTK_TABLE (table_transport), user_entry, 1, 2, 1, 2,
310                         (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
311                         (GtkAttachOptions) (0), 0, 0);
312         CLAWS_SET_TIP(user_entry, _("User to use with spamd server"));
313
314         spamd_label = gtk_label_new(_("spamd"));
315         gtk_widget_show(spamd_label);
316         gtk_table_attach (GTK_TABLE (table_transport), spamd_label, 0, 1, 2, 3,
317                         (GtkAttachOptions) (GTK_FILL),
318                         (GtkAttachOptions) (0), 0, 0);
319         gtk_label_set_justify(GTK_LABEL(spamd_label), GTK_JUSTIFY_RIGHT);
320         gtk_misc_set_alignment(GTK_MISC(spamd_label), 1, 0.5);
321
322         hbox_spamd = gtk_hbox_new(FALSE, 8);
323         gtk_widget_show(hbox_spamd);
324         gtk_table_attach (GTK_TABLE (table_transport), hbox_spamd, 1, 2, 2, 3,
325                         (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
326                         (GtkAttachOptions) (0), 0, 0);
327
328         spamd_hostname_entry = gtk_entry_new();
329         gtk_widget_show(spamd_hostname_entry);
330         gtk_box_pack_start(GTK_BOX(hbox_spamd), spamd_hostname_entry, TRUE, TRUE, 0);
331         CLAWS_SET_TIP(spamd_hostname_entry,
332                         _("Hostname or IP address of spamd server"));
333
334         spamd_colon_label = gtk_label_new(":");
335         gtk_widget_show(spamd_colon_label);
336         gtk_box_pack_start(GTK_BOX(hbox_spamd), spamd_colon_label, FALSE, FALSE, 0);
337
338         spamd_port_spinbtn_adj = gtk_adjustment_new(783, 1, 65535, 1, 10, 0);
339         spamd_port_spinbtn = gtk_spin_button_new(GTK_ADJUSTMENT(spamd_port_spinbtn_adj), 1, 0);
340         gtk_widget_show(spamd_port_spinbtn);
341         gtk_box_pack_start(GTK_BOX(hbox_spamd), spamd_port_spinbtn, FALSE, FALSE, 0);
342         CLAWS_SET_TIP(spamd_port_spinbtn,
343                         _("Port of spamd server"));
344         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(spamd_port_spinbtn), TRUE);
345
346         spamd_socket_entry = gtk_entry_new();
347         gtk_widget_show(spamd_socket_entry);
348         gtk_box_pack_start(GTK_BOX(hbox_spamd), spamd_socket_entry, TRUE, TRUE, 0);
349         CLAWS_SET_TIP(spamd_socket_entry, _("Path of Unix socket"));
350
351         hbox_max_size = gtk_hbox_new(FALSE, 8);
352         gtk_widget_show(hbox_max_size);
353         gtk_box_pack_start (GTK_BOX (vbox2), hbox_max_size, TRUE, TRUE, 0);
354
355         max_size_label = gtk_label_new(_("Maximum size"));
356         gtk_widget_show(max_size_label);
357         gtk_box_pack_start(GTK_BOX(hbox_max_size), max_size_label, FALSE, FALSE, 0);
358
359         max_size_spinbtn_adj = gtk_adjustment_new(250, 0, 10000, 10, 10, 0);
360         max_size_spinbtn = gtk_spin_button_new(GTK_ADJUSTMENT(max_size_spinbtn_adj), 1, 0);
361         gtk_widget_show(max_size_spinbtn);
362         gtk_box_pack_start(GTK_BOX(hbox_max_size), max_size_spinbtn, FALSE, FALSE, 0);
363         CLAWS_SET_TIP(max_size_spinbtn,
364                         _("Messages larger than this will not be checked"));
365         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(max_size_spinbtn), TRUE);
366
367         max_size_kb_label = gtk_label_new(_("KB"));
368         gtk_widget_show(max_size_kb_label);
369         gtk_box_pack_start(GTK_BOX(hbox_max_size), max_size_kb_label, FALSE, FALSE, 0);
370
371         hbox_timeout = gtk_hbox_new(FALSE, 8);
372         gtk_widget_show(hbox_timeout);
373         gtk_box_pack_start (GTK_BOX (vbox2), hbox_timeout, TRUE, TRUE, 0);
374
375         timeout_label = gtk_label_new(_("Timeout"));
376         gtk_widget_show(timeout_label);
377         gtk_box_pack_start(GTK_BOX(hbox_timeout), timeout_label, FALSE, FALSE, 0);
378
379         timeout_spinbtn_adj = gtk_adjustment_new(60, 0, 10000, 10, 10, 0);
380         timeout_spinbtn = gtk_spin_button_new(GTK_ADJUSTMENT(timeout_spinbtn_adj), 1, 0);
381         gtk_widget_show(timeout_spinbtn);
382         gtk_box_pack_start(GTK_BOX(hbox_timeout), timeout_spinbtn, FALSE, FALSE, 0);
383         CLAWS_SET_TIP(timeout_spinbtn,
384                         _("Maximum time allowed for checking. If the check takes longer "
385                                 "it will be aborted."));
386         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(timeout_spinbtn), TRUE);
387
388         timeout_seconds_label = gtk_label_new(_("seconds"));
389         gtk_widget_show(timeout_seconds_label);
390         gtk_box_pack_start(GTK_BOX(hbox_timeout), timeout_seconds_label, FALSE, FALSE, 0);
391
392         hbox_process_emails = gtk_hbox_new(FALSE, 8);
393         gtk_widget_show(hbox_process_emails);
394         gtk_box_pack_start (GTK_BOX (vbox2), hbox_process_emails, TRUE, TRUE, 0);
395
396         process_emails_checkbtn = gtk_check_button_new_with_label(
397                         _("Process messages on receiving"));
398         gtk_widget_show(process_emails_checkbtn);
399         gtk_box_pack_start(GTK_BOX(hbox_process_emails), process_emails_checkbtn, TRUE, TRUE, 0);
400
401         hbox_save_spam = gtk_hbox_new(FALSE, 8);
402         gtk_widget_show(hbox_save_spam);
403         gtk_box_pack_start (GTK_BOX (vbox2), hbox_save_spam, TRUE, TRUE, 0);
404
405         save_spam_checkbtn = gtk_check_button_new_with_label(_("Save spam in"));
406         gtk_widget_show(save_spam_checkbtn);
407         gtk_box_pack_start(GTK_BOX(hbox_save_spam), save_spam_checkbtn, FALSE, FALSE, 0);
408
409         save_spam_folder_entry = gtk_entry_new();
410         gtk_widget_show (save_spam_folder_entry);
411         gtk_box_pack_start (GTK_BOX (hbox_save_spam), save_spam_folder_entry, TRUE, TRUE, 0);
412         CLAWS_SET_TIP(save_spam_folder_entry,
413                         _("Folder for storing identified spam. Leave empty to use the trash folder."));
414
415         save_spam_folder_select = gtkut_get_browse_directory_btn(_("_Browse"));
416         gtk_widget_show (save_spam_folder_select);
417         gtk_box_pack_start (GTK_BOX (hbox_save_spam), save_spam_folder_select, FALSE, FALSE, 0);
418         CLAWS_SET_TIP(save_spam_folder_select,
419                         _("Click this button to select a folder for storing spam"));
420
421         hbox_mark_as_read = gtk_hbox_new(FALSE, 8);
422         gtk_widget_show(hbox_mark_as_read);
423         gtk_box_pack_start (GTK_BOX (vbox2), hbox_mark_as_read, TRUE, TRUE, 0);
424
425         mark_as_read_checkbtn = gtk_check_button_new_with_label(
426                         _("Mark spam as read"));
427         gtk_widget_show(mark_as_read_checkbtn);
428         gtk_box_pack_start(GTK_BOX(hbox_mark_as_read), mark_as_read_checkbtn, TRUE, TRUE, 0);
429
430         hbox_whitelist = gtk_hbox_new(FALSE, 8);
431         gtk_widget_show(hbox_whitelist);
432         gtk_box_pack_start (GTK_BOX (vbox2), hbox_whitelist, TRUE, TRUE, 0);
433
434         whitelist_ab_checkbtn = gtk_check_button_new_with_label(_("Whitelist senders found in address book/folder"));
435         gtk_widget_show(whitelist_ab_checkbtn);
436         gtk_box_pack_start(GTK_BOX(hbox_whitelist), whitelist_ab_checkbtn, FALSE, FALSE, 0);
437         CLAWS_SET_TIP(whitelist_ab_checkbtn,
438                         _("Messages coming from your address book contacts will be received in the normal folder even if detected as spam"));
439
440         whitelist_ab_folder_combo = combobox_text_new(TRUE, _("Any"), NULL);
441         gtk_widget_set_size_request(whitelist_ab_folder_combo, 100, -1);
442         gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_folder_combo, TRUE, TRUE, 0);
443
444         whitelist_ab_select_btn = gtk_button_new_with_label(_("Select ..."));
445         gtk_widget_show (whitelist_ab_select_btn);
446         gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_select_btn, FALSE, FALSE, 0);
447         CLAWS_SET_TIP(whitelist_ab_select_btn,
448                         _("Click this button to select a book or folder in the address book"));
449
450         SET_TOGGLE_SENSITIVITY(enable_sa_checkbtn, frame_transport);
451         SET_TOGGLE_SENSITIVITY(enable_sa_checkbtn, hbox_max_size);
452         SET_TOGGLE_SENSITIVITY(enable_sa_checkbtn, hbox_timeout);
453         SET_TOGGLE_SENSITIVITY(enable_sa_checkbtn, hbox_save_spam);
454         SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, save_spam_folder_entry);
455         SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, save_spam_folder_select);
456         SET_TOGGLE_SENSITIVITY(enable_sa_checkbtn, hbox_process_emails);
457         SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, mark_as_read_checkbtn);
458         SET_TOGGLE_SENSITIVITY(whitelist_ab_checkbtn, whitelist_ab_folder_combo);
459         SET_TOGGLE_SENSITIVITY(whitelist_ab_checkbtn, whitelist_ab_select_btn);
460
461         config = spamassassin_get_config();
462
463         g_signal_connect(G_OBJECT(save_spam_folder_select), "clicked",
464                         G_CALLBACK(foldersel_cb), page);
465         g_signal_connect(G_OBJECT (whitelist_ab_select_btn), "clicked",
466                          G_CALLBACK(spamassassin_whitelist_ab_select_cb), page);
467
468         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_sa_checkbtn), config->enable);
469         if (config->username != NULL)
470                 gtk_entry_set_text(GTK_ENTRY(user_entry), config->username);
471         if (config->hostname != NULL)
472                 gtk_entry_set_text(GTK_ENTRY(spamd_hostname_entry), config->hostname);
473         if (config->socket != NULL)
474                 gtk_entry_set_text(GTK_ENTRY(spamd_socket_entry), config->socket);
475         if (config->whitelist_ab_folder != NULL) {
476                 /* translate "Any" (stored UNtranslated) */
477                 if (strcasecmp(config->whitelist_ab_folder, "Any") == 0)
478                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
479                                         config->whitelist_ab_folder);
480                 else
481                 /* backward compatibility (when translated "Any" was stored) */
482                 if (g_utf8_collate(config->whitelist_ab_folder, _("Any")) == 0)
483                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
484                                         config->whitelist_ab_folder);
485                 else
486                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
487                                         config->whitelist_ab_folder);
488         }
489         gtk_spin_button_set_value(GTK_SPIN_BUTTON(spamd_port_spinbtn), (float) config->port);
490         gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_size_spinbtn), (float) config->max_size);
491         gtk_spin_button_set_value(GTK_SPIN_BUTTON(timeout_spinbtn), (float) config->timeout);
492         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(process_emails_checkbtn), config->process_emails);
493         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(save_spam_checkbtn), config->receive_spam);
494         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(whitelist_ab_checkbtn), config->whitelist_ab);
495         if (config->save_folder != NULL)
496                 gtk_entry_set_text(GTK_ENTRY(save_spam_folder_entry), config->save_folder);
497         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mark_as_read_checkbtn), config->mark_as_read);
498
499         page->enable_sa_checkbtn = enable_sa_checkbtn;
500         page->transport_label = transport_label;
501         page->transport_optmenu = transport_optmenu;
502         page->username = user_entry;
503         page->hostname = spamd_hostname_entry;
504         page->colon = spamd_colon_label;
505         page->port = spamd_port_spinbtn;
506         page->socket = spamd_socket_entry;
507         page->max_size = max_size_spinbtn;
508         page->timeout = timeout_spinbtn;
509         page->process_emails = process_emails_checkbtn;
510         page->receive_spam = save_spam_checkbtn;
511         page->save_folder = save_spam_folder_entry;
512         page->save_folder_select = save_spam_folder_select;
513         page->mark_as_read = mark_as_read_checkbtn;
514         page->whitelist_ab = whitelist_ab_checkbtn;
515         page->whitelist_ab_folder_combo = whitelist_ab_folder_combo;
516
517         active = 0;
518         for (i = 0; i < (sizeof(transports) / sizeof(struct Transport)); i++) {
519                 
520                 gtk_list_store_append(store, &iter);
521                 gtk_list_store_set(store, &iter,
522                                    0, gettext(transports[i].name),
523                                    1, &transports[i], -1);
524
525                 if (config->transport == transports[i].transport) {
526                         show_transport(page, &transports[i]);
527                         active = i;
528                 } else if (config->transport == SPAMASSASSIN_DISABLED 
529                         && transports[i].transport == SPAMASSASSIN_TRANSPORT_LOCALHOST) {
530                         show_transport(page, &transports[i]);
531                         active = i;
532                         /* and disable via new way */
533                         config->enable = FALSE;
534                         gtk_toggle_button_set_active(
535                                 GTK_TOGGLE_BUTTON(enable_sa_checkbtn), 
536                                 config->enable);
537                 }
538         }
539         gtk_combo_box_set_active(GTK_COMBO_BOX(transport_optmenu), active);
540         g_signal_connect(G_OBJECT(transport_optmenu), "changed",
541                          G_CALLBACK(transport_sel_cb), page);
542
543         page->page.widget = vbox1;
544 }
545
546 static void spamassassin_destroy_widget_func(PrefsPage *_page)
547 {
548         debug_print("Destroying SpamAssassin widget\n");
549 }
550
551 static void spamassassin_save_func(PrefsPage *_page)
552 {
553         struct SpamAssassinPage *page = (struct SpamAssassinPage *) _page;
554         SpamAssassinConfig *config;
555
556         debug_print("Saving SpamAssassin Page\n");
557
558         config = spamassassin_get_config();
559
560         /* enable */
561         config->enable = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->enable_sa_checkbtn));
562         config->transport = page->trans;
563
564         /* username */
565         g_free(config->username);
566         config->username = gtk_editable_get_chars(GTK_EDITABLE(page->username), 0, -1);
567         spamassassin_check_username();
568
569         /* hostname */
570         g_free(config->hostname);
571         config->hostname = gtk_editable_get_chars(GTK_EDITABLE(page->hostname), 0, -1);
572
573         /* port */
574         config->port = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->port));
575
576         /* hostname */
577         g_free(config->socket);
578         config->socket = gtk_editable_get_chars(GTK_EDITABLE(page->socket), 0, -1);
579
580         /* process_emails */
581         config->process_emails = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->process_emails));
582
583         /* receive_spam */
584         config->receive_spam = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->receive_spam));
585
586         /* save_folder */
587         g_free(config->save_folder);
588         config->save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1);
589
590         /* max_size */
591         config->max_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->max_size));
592
593         /* timeout */
594         config->timeout = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->timeout));
595
596         /* mark_as_read */
597         config->mark_as_read = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->mark_as_read));
598
599         /* whitelist_ab */
600         config->whitelist_ab = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->whitelist_ab));
601         g_free(config->whitelist_ab_folder);
602         config->whitelist_ab_folder = gtk_editable_get_chars(
603                                 GTK_EDITABLE(gtk_bin_get_child(GTK_BIN((page->whitelist_ab_folder_combo)))), 0, -1);
604         /* store UNtranslated "Any" */
605         if (g_utf8_collate(config->whitelist_ab_folder, _("Any")) == 0) {
606                 g_free(config->whitelist_ab_folder);
607                 config->whitelist_ab_folder = g_strdup("Any");
608         }
609
610         if (config->process_emails) {
611                 spamassassin_register_hook();
612         } else {
613                 spamassassin_unregister_hook();
614         }
615
616         if (!config->enable) {
617                 procmsg_unregister_spam_learner(spamassassin_learn);
618                 procmsg_spam_set_folder(NULL, NULL);
619         } else {
620                 if (config->transport == SPAMASSASSIN_TRANSPORT_TCP)
621                         debug_print("enabling learner with a remote spamassassin server requires spamc/spamd 3.1.x\n");
622                 procmsg_register_spam_learner(spamassassin_learn);
623                 procmsg_spam_set_folder(config->save_folder, spamassassin_get_spam_folder);
624         }
625
626         spamassassin_save_config();
627 }
628
629 static void gtk_message_callback(gchar *message)
630 {
631         statusbar_print_all("%s", message);
632 }
633
634 static struct SpamAssassinPage spamassassin_page;
635
636 gint spamassassin_gtk_init(void)
637 {
638         static gchar *path[3];
639
640         path[0] = _("Plugins");
641         path[1] = _("SpamAssassin");
642         path[2] = NULL;
643
644         spamassassin_page.page.path = path;
645         spamassassin_page.page.create_widget = spamassassin_create_widget_func;
646         spamassassin_page.page.destroy_widget = spamassassin_destroy_widget_func;
647         spamassassin_page.page.save_page = spamassassin_save_func;
648         spamassassin_page.page.weight = 35.0;
649
650         prefs_gtk_register_page((PrefsPage *) &spamassassin_page);
651         spamassassin_set_message_callback(gtk_message_callback);
652
653         debug_print("SpamAssassin GTK plugin loaded\n");
654         return 0;       
655 }
656
657 void spamassassin_gtk_done(void)
658 {
659         prefs_gtk_unregister_page((PrefsPage *) &spamassassin_page);
660 }