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