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