4d9926aa3fcd3f2d62726ea9cfc0e4aa70325850
[claws.git] / src / plugins / spamassassin / spamassassin_gtk.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and 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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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
43 struct SpamAssassinPage
44 {
45         PrefsPage page;
46
47         GtkWidget *enable_sa_checkbtn;
48         GtkWidget *transport_optmenu;
49         GtkWidget *transport_label;
50         GtkWidget *username;
51         GtkWidget *hostname;
52         GtkWidget *colon;
53         GtkWidget *port;
54         GtkWidget *socket;
55         GtkWidget *process_emails;
56         GtkWidget *receive_spam;
57         GtkWidget *save_folder;
58         GtkWidget *save_folder_select;
59         GtkWidget *max_size;
60         GtkWidget *timeout;
61
62         SpamAssassinTransport   trans;
63 };
64
65 struct Transport
66 {
67         gchar                   *name;
68         SpamAssassinTransport    transport;
69         guint                    page;
70         guint                    pageflags;
71 };
72
73 enum {
74         PAGE_DISABLED = 0,
75         PAGE_NETWORK  = 1,
76         PAGE_UNIX     = 2,
77 };
78
79 enum {
80         NETWORK_HOSTNAME = 1,
81 };
82
83 struct Transport transports[] = {
84         /*{ N_("Disabled"),     SPAMASSASSIN_DISABLED,                  PAGE_DISABLED, 0 },*/
85         { N_("Localhost"),      SPAMASSASSIN_TRANSPORT_LOCALHOST,       PAGE_NETWORK, 0 },
86         { N_("TCP"),            SPAMASSASSIN_TRANSPORT_TCP,             PAGE_NETWORK, NETWORK_HOSTNAME },
87         { N_("Unix Socket"),    SPAMASSASSIN_TRANSPORT_UNIX,            PAGE_UNIX,    0 },
88 };
89
90 static void foldersel_cb(GtkWidget *widget, gpointer data)
91 {
92         struct SpamAssassinPage *page = (struct SpamAssassinPage *) data;
93         FolderItem *item;
94         gchar *item_id;
95         gint newpos = 0;
96         
97         item = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL);
98         if (item && (item_id = folder_item_get_identifier(item)) != NULL) {
99                 gtk_editable_delete_text(GTK_EDITABLE(page->save_folder), 0, -1);
100                 gtk_editable_insert_text(GTK_EDITABLE(page->save_folder), item_id, strlen(item_id), &newpos);
101                 g_free(item_id);
102         }
103 }
104
105 static void show_transport(struct SpamAssassinPage *page, struct Transport *transport)
106 {
107         page->trans = transport->transport;
108
109         switch (transport->page) {
110                 /*
111         case PAGE_DISABLED:
112                 gtk_widget_show(page->hostname);
113                 gtk_widget_show(page->colon);
114                 gtk_widget_show(page->port);
115                 gtk_widget_hide(page->socket);
116                 gtk_widget_set_sensitive(page->username, FALSE);
117                 gtk_widget_set_sensitive(page->hostname, FALSE);
118                 gtk_widget_set_sensitive(page->colon, FALSE);
119                 gtk_widget_set_sensitive(page->port, FALSE);
120                 gtk_widget_set_sensitive(page->max_size, FALSE);
121                 gtk_widget_set_sensitive(page->timeout, FALSE);
122                 gtk_widget_set_sensitive(page->process_emails, FALSE);
123                 gtk_widget_set_sensitive(page->receive_spam, FALSE);
124                 gtk_widget_set_sensitive(page->save_folder, FALSE);
125                 gtk_widget_set_sensitive(page->save_folder_select, FALSE);
126                 break;
127                 */
128         case PAGE_UNIX:
129                 gtk_widget_hide(page->hostname);
130                 gtk_widget_hide(page->colon);
131                 gtk_widget_hide(page->port);
132                 gtk_widget_show(page->socket);
133                 gtk_widget_set_sensitive(page->username, TRUE);
134                 gtk_widget_set_sensitive(page->socket, TRUE);
135                 gtk_widget_set_sensitive(page->max_size, TRUE);
136                 gtk_widget_set_sensitive(page->timeout, TRUE);
137                 gtk_widget_set_sensitive(page->process_emails, TRUE);
138                 gtk_widget_set_sensitive(page->receive_spam, TRUE);
139                 gtk_widget_set_sensitive(page->save_folder, TRUE);
140                 gtk_widget_set_sensitive(page->save_folder_select, TRUE);
141                 break;
142         case PAGE_NETWORK:
143                 gtk_widget_show(page->hostname);
144                 gtk_widget_show(page->colon);
145                 gtk_widget_show(page->port);
146                 gtk_widget_hide(page->socket);
147                 gtk_widget_set_sensitive(page->username, TRUE);
148                 gtk_widget_set_sensitive(page->max_size, TRUE);
149                 gtk_widget_set_sensitive(page->timeout, TRUE);
150                 gtk_widget_set_sensitive(page->process_emails, TRUE);
151                 gtk_widget_set_sensitive(page->receive_spam, TRUE);
152                 gtk_widget_set_sensitive(page->save_folder, TRUE);
153                 gtk_widget_set_sensitive(page->save_folder_select, TRUE);
154                 if (transport->pageflags & NETWORK_HOSTNAME) {
155                         gtk_widget_set_sensitive(page->hostname, TRUE);
156                         gtk_widget_set_sensitive(page->colon, TRUE);
157                         gtk_widget_set_sensitive(page->port, TRUE);
158                 } else {
159                         gtk_widget_set_sensitive(page->hostname, FALSE);
160                         gtk_widget_set_sensitive(page->colon, FALSE);
161                         gtk_widget_set_sensitive(page->port, TRUE);
162                 }
163                 break;
164         default:
165                 break;
166         }
167 }
168
169 static void transport_sel_cb(GtkMenuItem *menuitem, gpointer data)
170 {
171         struct SpamAssassinPage *page = (struct SpamAssassinPage *) data;
172         struct Transport *transport;
173
174         transport = (struct Transport *) g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID);
175         show_transport(page, transport);
176 }
177
178 static void spamassassin_create_widget_func(PrefsPage * _page,
179                                             GtkWindow * window,
180                                             gpointer data)
181 {
182         struct SpamAssassinPage *page = (struct SpamAssassinPage *) _page;
183         SpamAssassinConfig *config;
184         guint i, active;
185
186         GtkWidget *vbox1, *vbox2;
187         GtkWidget *frame_transport, *table_transport, *vbox_transport;
188         GtkWidget *hbox_spamd, *hbox_max_size, *hbox_timeout;
189         GtkWidget *hbox_process_emails, *hbox_save_spam;
190
191         GtkWidget *enable_sa_checkbtn;
192
193         GtkWidget *transport_label;
194         GtkWidget *transport_optmenu;
195         GtkWidget *transport_menu;
196
197         GtkWidget *user_label;
198         GtkWidget *user_entry;
199
200         GtkWidget *spamd_label;
201         GtkWidget *spamd_hostname_entry;
202         GtkWidget *spamd_colon_label;
203         GtkObject *spamd_port_spinbtn_adj;
204         GtkWidget *spamd_port_spinbtn;
205         GtkWidget *spamd_socket_entry;
206
207         GtkWidget *max_size_label;
208         GtkObject *max_size_spinbtn_adj;
209         GtkWidget *max_size_spinbtn;
210         GtkWidget *max_size_kb_label;
211
212         GtkWidget *timeout_label;
213         GtkObject *timeout_spinbtn_adj;
214         GtkWidget *timeout_spinbtn;
215         GtkWidget *timeout_seconds_label;
216
217         GtkWidget *process_emails_checkbtn;
218
219         GtkWidget *save_spam_checkbtn;
220         GtkWidget *save_spam_folder_entry;
221         GtkWidget *save_spam_folder_select;
222
223         GtkTooltips *tooltips;
224
225         tooltips = gtk_tooltips_new();
226
227         vbox1 = gtk_vbox_new (FALSE, VSPACING);
228         gtk_widget_show (vbox1);
229         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
230
231         vbox2 = gtk_vbox_new (FALSE, 4);
232         gtk_widget_show (vbox2);
233         gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
234
235         enable_sa_checkbtn = gtk_check_button_new_with_label(_("Enable SpamAssassin plugin"));
236         gtk_widget_show(enable_sa_checkbtn);
237         gtk_box_pack_start(GTK_BOX(vbox2), enable_sa_checkbtn, TRUE, TRUE, 0);
238
239         PACK_FRAME(vbox2, frame_transport, _("Transport"));
240         vbox_transport = gtk_vbox_new (FALSE, VSPACING_NARROW);
241         gtk_widget_show (vbox_transport);
242         gtk_container_add (GTK_CONTAINER (frame_transport), vbox_transport);
243         gtk_container_set_border_width (GTK_CONTAINER (vbox_transport), 8);
244
245         table_transport = gtk_table_new (3, 3, FALSE);
246         gtk_widget_show (table_transport);
247         gtk_box_pack_start(GTK_BOX(vbox_transport), table_transport, TRUE, TRUE, 0);
248         gtk_table_set_row_spacings (GTK_TABLE (table_transport), 4);
249         gtk_table_set_col_spacings (GTK_TABLE (table_transport), 8);
250
251         transport_label = gtk_label_new(_("Type of transport"));
252         gtk_widget_show(transport_label);
253         gtk_table_attach (GTK_TABLE (table_transport), transport_label, 0, 1, 0, 1,
254                         (GtkAttachOptions) (GTK_FILL),
255                         (GtkAttachOptions) (0), 0, 0);
256         gtk_label_set_justify(GTK_LABEL(transport_label), GTK_JUSTIFY_RIGHT);
257         gtk_misc_set_alignment(GTK_MISC(transport_label), 1, 0.5);
258
259         transport_optmenu = gtk_option_menu_new();
260         gtk_widget_show(transport_optmenu);
261
262         gtk_table_attach (GTK_TABLE (table_transport), transport_optmenu, 1, 2, 0, 1,
263                         (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
264                         (GtkAttachOptions) (0), 0, 0);
265         transport_menu = gtk_menu_new();
266
267         user_label = gtk_label_new(_("User"));
268         gtk_widget_show(user_label);
269         gtk_table_attach (GTK_TABLE (table_transport), user_label, 0, 1, 1, 2,
270                         (GtkAttachOptions) (GTK_FILL),
271                         (GtkAttachOptions) (0), 0, 0);
272         gtk_label_set_justify(GTK_LABEL(user_label), GTK_JUSTIFY_RIGHT);
273         gtk_misc_set_alignment(GTK_MISC(user_label), 1, 0.5);
274
275         user_entry = gtk_entry_new();
276         gtk_widget_show(user_entry);
277         gtk_table_attach (GTK_TABLE (table_transport), user_entry, 1, 2, 1, 2,
278                         (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
279                         (GtkAttachOptions) (0), 0, 0);
280         gtk_tooltips_set_tip(tooltips, user_entry, _("User to use with spamd server"),
281                         NULL);
282
283         spamd_label = gtk_label_new(_("spamd"));
284         gtk_widget_show(spamd_label);
285         gtk_table_attach (GTK_TABLE (table_transport), spamd_label, 0, 1, 2, 3,
286                         (GtkAttachOptions) (GTK_FILL),
287                         (GtkAttachOptions) (0), 0, 0);
288         gtk_label_set_justify(GTK_LABEL(spamd_label), GTK_JUSTIFY_RIGHT);
289         gtk_misc_set_alignment(GTK_MISC(spamd_label), 1, 0.5);
290
291         hbox_spamd = gtk_hbox_new(FALSE, 8);
292         gtk_widget_show(hbox_spamd);
293         gtk_table_attach (GTK_TABLE (table_transport), hbox_spamd, 1, 2, 2, 3,
294                         (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
295                         (GtkAttachOptions) (0), 0, 0);
296
297         spamd_hostname_entry = gtk_entry_new();
298         gtk_widget_show(spamd_hostname_entry);
299         gtk_box_pack_start(GTK_BOX(hbox_spamd), spamd_hostname_entry, TRUE, TRUE, 0);
300         gtk_tooltips_set_tip(tooltips, spamd_hostname_entry,
301                         _("Hostname or IP address of spamd server"), NULL);
302
303         spamd_colon_label = gtk_label_new(":");
304         gtk_widget_show(spamd_colon_label);
305         gtk_box_pack_start(GTK_BOX(hbox_spamd), spamd_colon_label, FALSE, FALSE, 0);
306
307         spamd_port_spinbtn_adj = gtk_adjustment_new(783, 1, 65535, 1, 10, 10);
308         spamd_port_spinbtn = gtk_spin_button_new(GTK_ADJUSTMENT(spamd_port_spinbtn_adj), 1, 0);
309         gtk_widget_show(spamd_port_spinbtn);
310         gtk_box_pack_start(GTK_BOX(hbox_spamd), spamd_port_spinbtn, FALSE, FALSE, 0);
311         gtk_tooltips_set_tip(tooltips, spamd_port_spinbtn,
312                         _("Port of spamd server"), NULL);
313         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(spamd_port_spinbtn), TRUE);
314
315         spamd_socket_entry = gtk_entry_new();
316         gtk_widget_show(spamd_socket_entry);
317         gtk_box_pack_start(GTK_BOX(hbox_spamd), spamd_socket_entry, TRUE, TRUE, 0);
318         gtk_tooltips_set_tip(tooltips, spamd_socket_entry, _("Path of Unix socket"),
319                         NULL);
320
321         hbox_max_size = gtk_hbox_new(FALSE, 8);
322         gtk_widget_show(hbox_max_size);
323         gtk_box_pack_start (GTK_BOX (vbox2), hbox_max_size, TRUE, TRUE, 0);
324
325         max_size_label = gtk_label_new(_("Maximum size"));
326         gtk_widget_show(max_size_label);
327         gtk_box_pack_start(GTK_BOX(hbox_max_size), max_size_label, FALSE, FALSE, 0);
328
329         max_size_spinbtn_adj = gtk_adjustment_new(250, 0, 10000, 10, 10, 10);
330         max_size_spinbtn = gtk_spin_button_new(GTK_ADJUSTMENT(max_size_spinbtn_adj), 1, 0);
331         gtk_widget_show(max_size_spinbtn);
332         gtk_box_pack_start(GTK_BOX(hbox_max_size), max_size_spinbtn, FALSE, FALSE, 0);
333         gtk_tooltips_set_tip(tooltips, max_size_spinbtn,
334                         _("Messages larger than this will not be checked"), NULL);
335         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(max_size_spinbtn), TRUE);
336
337         max_size_kb_label = gtk_label_new(_("kB"));
338         gtk_widget_show(max_size_kb_label);
339         gtk_box_pack_start(GTK_BOX(hbox_max_size), max_size_kb_label, FALSE, FALSE, 0);
340
341         hbox_timeout = gtk_hbox_new(FALSE, 8);
342         gtk_widget_show(hbox_timeout);
343         gtk_box_pack_start (GTK_BOX (vbox2), hbox_timeout, TRUE, TRUE, 0);
344
345         timeout_label = gtk_label_new(_("Timeout"));
346         gtk_widget_show(timeout_label);
347         gtk_box_pack_start(GTK_BOX(hbox_timeout), timeout_label, FALSE, FALSE, 0);
348
349         timeout_spinbtn_adj = gtk_adjustment_new(60, 0, 10000, 10, 10, 10);
350         timeout_spinbtn = gtk_spin_button_new(GTK_ADJUSTMENT(timeout_spinbtn_adj), 1, 0);
351         gtk_widget_show(timeout_spinbtn);
352         gtk_box_pack_start(GTK_BOX(hbox_timeout), timeout_spinbtn, FALSE, FALSE, 0);
353         gtk_tooltips_set_tip(tooltips, timeout_spinbtn,
354                         _("Maximum time allowed for checking. If the check takes longer "
355                                 "it will be aborted."),
356                         NULL);
357         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(timeout_spinbtn), TRUE);
358
359         timeout_seconds_label = gtk_label_new(_("seconds"));
360         gtk_widget_show(timeout_seconds_label);
361         gtk_box_pack_start(GTK_BOX(hbox_timeout), timeout_seconds_label, FALSE, FALSE, 0);
362
363         hbox_process_emails = gtk_hbox_new(FALSE, 8);
364         gtk_widget_show(hbox_process_emails);
365         gtk_box_pack_start (GTK_BOX (vbox2), hbox_process_emails, TRUE, TRUE, 0);
366
367         process_emails_checkbtn = gtk_check_button_new_with_label(
368                         _("Process messages on receiving"));
369         gtk_widget_show(process_emails_checkbtn);
370         gtk_box_pack_start(GTK_BOX(hbox_process_emails), process_emails_checkbtn, TRUE, TRUE, 0);
371
372         hbox_save_spam = gtk_hbox_new(FALSE, 8);
373         gtk_widget_show(hbox_save_spam);
374         gtk_box_pack_start (GTK_BOX (vbox2), hbox_save_spam, TRUE, TRUE, 0);
375
376         save_spam_checkbtn = gtk_check_button_new_with_label(_("Save spam in"));
377         gtk_widget_show(save_spam_checkbtn);
378         gtk_box_pack_start(GTK_BOX(hbox_save_spam), save_spam_checkbtn, FALSE, FALSE, 0);
379
380         save_spam_folder_entry = gtk_entry_new();
381         gtk_widget_show (save_spam_folder_entry);
382         gtk_box_pack_start (GTK_BOX (hbox_save_spam), save_spam_folder_entry, TRUE, TRUE, 0);
383         gtk_tooltips_set_tip(tooltips, save_spam_folder_entry,
384                         _("Folder for storing identified spam. Leave empty to use the default trash folder"),
385                         NULL);
386
387         save_spam_folder_select = gtkut_get_browse_directory_btn(_("_Browse"));
388         gtk_widget_show (save_spam_folder_select);
389         gtk_box_pack_start (GTK_BOX (hbox_save_spam), save_spam_folder_select, FALSE, FALSE, 0);
390         gtk_tooltips_set_tip(tooltips, save_spam_folder_select,
391                         _("Click this button to select a folder for storing spam"),
392                         NULL);
393
394         SET_TOGGLE_SENSITIVITY(enable_sa_checkbtn, frame_transport);
395         SET_TOGGLE_SENSITIVITY(enable_sa_checkbtn, hbox_max_size);
396         SET_TOGGLE_SENSITIVITY(enable_sa_checkbtn, hbox_timeout);
397         SET_TOGGLE_SENSITIVITY(enable_sa_checkbtn, hbox_save_spam);
398         SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, save_spam_folder_entry);
399         SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, save_spam_folder_select);
400         SET_TOGGLE_SENSITIVITY(enable_sa_checkbtn, hbox_process_emails);
401
402         config = spamassassin_get_config();
403
404         g_signal_connect(G_OBJECT(save_spam_folder_select), "clicked",
405                         G_CALLBACK(foldersel_cb), page);
406
407         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_sa_checkbtn), config->enable);
408         if (config->username != NULL)
409                 gtk_entry_set_text(GTK_ENTRY(user_entry), config->username);
410         if (config->hostname != NULL)
411                 gtk_entry_set_text(GTK_ENTRY(spamd_hostname_entry), config->hostname);
412         if (config->socket != NULL)
413                 gtk_entry_set_text(GTK_ENTRY(spamd_socket_entry), config->socket);
414         gtk_spin_button_set_value(GTK_SPIN_BUTTON(spamd_port_spinbtn), (float) config->port);
415         gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_size_spinbtn), (float) config->max_size);
416         gtk_spin_button_set_value(GTK_SPIN_BUTTON(timeout_spinbtn), (float) config->timeout);
417         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(process_emails_checkbtn), config->process_emails);
418         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(save_spam_checkbtn), config->receive_spam);
419         if (config->save_folder != NULL)
420                 gtk_entry_set_text(GTK_ENTRY(save_spam_folder_entry), config->save_folder);
421
422         page->enable_sa_checkbtn = enable_sa_checkbtn;
423         page->transport_label = transport_label;
424         page->transport_optmenu = transport_optmenu;
425         page->username = user_entry;
426         page->hostname = spamd_hostname_entry;
427         page->colon = spamd_colon_label;
428         page->port = spamd_port_spinbtn;
429         page->socket = spamd_socket_entry;
430         page->max_size = max_size_spinbtn;
431         page->timeout = timeout_spinbtn;
432         page->process_emails = process_emails_checkbtn;
433         page->receive_spam = save_spam_checkbtn;
434         page->save_folder = save_spam_folder_entry;
435         page->save_folder_select = save_spam_folder_select;
436
437         active = 0;
438         for (i = 0; i < (sizeof(transports) / sizeof(struct Transport)); i++) {
439                 GtkWidget *menuitem;
440
441                 menuitem = gtk_menu_item_new_with_label(gettext(transports[i].name));
442                 g_object_set_data(G_OBJECT(menuitem), MENU_VAL_ID, &transports[i]);
443                 g_signal_connect(G_OBJECT(menuitem), "activate",
444                                 G_CALLBACK(transport_sel_cb), page);
445                 gtk_widget_show(menuitem);
446                 gtk_menu_append(GTK_MENU(transport_menu), menuitem);
447
448                 if (config->transport == transports[i].transport) {
449                         show_transport(page, &transports[i]);
450                         active = i;
451                 } else if (config->transport == SPAMASSASSIN_DISABLED 
452                         && transports[i].transport == SPAMASSASSIN_TRANSPORT_LOCALHOST) {
453                         show_transport(page, &transports[i]);
454                         active = i;
455                         /* and disable via new way */
456                         config->enable = FALSE;
457                         gtk_toggle_button_set_active(
458                                 GTK_TOGGLE_BUTTON(enable_sa_checkbtn), 
459                                 config->enable);
460                 }
461         }
462         gtk_option_menu_set_menu(GTK_OPTION_MENU(transport_optmenu), transport_menu);
463         gtk_option_menu_set_history(GTK_OPTION_MENU(transport_optmenu), active);
464
465         page->page.widget = vbox1;
466 }
467
468 static void spamassassin_destroy_widget_func(PrefsPage *_page)
469 {
470         debug_print("Destroying SpamAssassin widget\n");
471 }
472
473 static void spamassassin_save_func(PrefsPage *_page)
474 {
475         struct SpamAssassinPage *page = (struct SpamAssassinPage *) _page;
476         SpamAssassinConfig *config;
477
478         debug_print("Saving SpamAssassin Page\n");
479
480         config = spamassassin_get_config();
481
482         /* enable */
483         config->enable = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->enable_sa_checkbtn));
484         config->transport = page->trans;
485
486         /* username */
487         g_free(config->username);
488         config->username = gtk_editable_get_chars(GTK_EDITABLE(page->username), 0, -1);
489         spamassassin_check_username();
490
491         /* hostname */
492         g_free(config->hostname);
493         config->hostname = gtk_editable_get_chars(GTK_EDITABLE(page->hostname), 0, -1);
494
495         /* port */
496         config->port = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->port));
497
498         /* hostname */
499         g_free(config->socket);
500         config->socket = gtk_editable_get_chars(GTK_EDITABLE(page->socket), 0, -1);
501
502         /* process_emails */
503         config->process_emails = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->process_emails));
504
505         /* receive_spam */
506         config->receive_spam = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->receive_spam));
507
508         /* save_folder */
509         g_free(config->save_folder);
510         config->save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1);
511
512         /* max_size */
513         config->max_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->max_size));
514
515         /* timeout */
516         config->timeout = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->timeout));
517
518         if (config->process_emails) {
519                 spamassassin_register_hook();
520         } else {
521                 spamassassin_unregister_hook();
522         }
523
524         if (!config->enable) {
525                 procmsg_unregister_spam_learner(spamassassin_learn);
526                 procmsg_spam_set_folder(NULL);
527         } else {
528                 if (config->transport == SPAMASSASSIN_TRANSPORT_TCP)
529                         debug_print("enabling learner with a remote spamassassin server requires spamc/spamd 3.1.x\n");
530                 procmsg_register_spam_learner(spamassassin_learn);
531                 procmsg_spam_set_folder(config->save_folder);
532         }
533
534         spamassassin_save_config();
535 }
536
537 static void gtk_message_callback(gchar *message)
538 {
539         statusbar_print_all(message);
540 }
541
542 static struct SpamAssassinPage spamassassin_page;
543
544 gint spamassassin_gtk_init(void)
545 {
546         static gchar *path[3];
547
548         path[0] = _("Plugins");
549         path[1] = _("SpamAssassin");
550         path[2] = NULL;
551
552         spamassassin_page.page.path = path;
553         spamassassin_page.page.create_widget = spamassassin_create_widget_func;
554         spamassassin_page.page.destroy_widget = spamassassin_destroy_widget_func;
555         spamassassin_page.page.save_page = spamassassin_save_func;
556         spamassassin_page.page.weight = 35.0;
557
558         prefs_gtk_register_page((PrefsPage *) &spamassassin_page);
559         spamassassin_set_message_callback(gtk_message_callback);
560
561         debug_print("SpamAssassin GTK plugin loaded\n");
562         return 0;       
563 }
564
565 void spamassassin_gtk_done(void)
566 {
567         prefs_gtk_unregister_page((PrefsPage *) &spamassassin_page);
568 }