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