2006-02-27 [wwp] 2.0.0cvs86
[claws.git] / src / plugins / spamassassin / spamassassin_gtk.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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/sylpheed.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 *transport;
48 /*      GtkWidget *transport_notebook;*/
49         GtkWidget *username;
50         GtkWidget *hostname;
51         GtkWidget *colon;
52         GtkWidget *port;
53         GtkWidget *socket;
54         GtkWidget *receive_spam;
55         GtkWidget *save_folder;
56         GtkWidget *save_folder_select;
57         GtkWidget *max_size;
58         GtkWidget *timeout;
59
60         SpamAssassinTransport   trans;
61 };
62
63 struct Transport
64 {
65         gchar                   *name;
66         SpamAssassinTransport    transport;
67         guint                    page;
68         guint                    pageflags;
69 };
70
71 enum {
72         PAGE_DISABLED = 0,
73         PAGE_NETWORK  = 1,
74         PAGE_UNIX     = 2,
75 };
76
77 enum {
78         NETWORK_HOSTNAME = 1,
79 };
80
81 struct Transport transports[] = {
82         { N_("Disabled"),       SPAMASSASSIN_DISABLED,                  PAGE_DISABLED, 0 },
83         { N_("Localhost"),      SPAMASSASSIN_TRANSPORT_LOCALHOST,       PAGE_NETWORK, 0 },
84         { N_("TCP"),            SPAMASSASSIN_TRANSPORT_TCP,             PAGE_NETWORK, NETWORK_HOSTNAME },
85         { N_("Unix Socket"),    SPAMASSASSIN_TRANSPORT_UNIX,            PAGE_UNIX,    0 },
86 };
87
88 static void foldersel_cb(GtkWidget *widget, gpointer data)
89 {
90         struct SpamAssassinPage *page = (struct SpamAssassinPage *) data;
91         FolderItem *item;
92         gchar *item_id;
93         gint newpos = 0;
94         
95         item = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL);
96         if (item && (item_id = folder_item_get_identifier(item)) != NULL) {
97                 gtk_editable_delete_text(GTK_EDITABLE(page->save_folder), 0, -1);
98                 gtk_editable_insert_text(GTK_EDITABLE(page->save_folder), item_id, strlen(item_id), &newpos);
99                 g_free(item_id);
100         }
101 }
102
103 static void show_transport(struct SpamAssassinPage *page, struct Transport *transport)
104 {
105         page->trans = transport->transport;
106
107         switch (transport->page) {
108         case PAGE_DISABLED:
109                 gtk_widget_show(page->hostname);
110                 gtk_widget_show(page->colon);
111                 gtk_widget_show(page->port);
112                 gtk_widget_hide(page->socket);
113                 gtk_widget_set_sensitive(page->username, FALSE);
114                 gtk_widget_set_sensitive(page->hostname, FALSE);
115                 gtk_widget_set_sensitive(page->colon, FALSE);
116                 gtk_widget_set_sensitive(page->port, FALSE);
117                 gtk_widget_set_sensitive(page->max_size, FALSE);
118                 gtk_widget_set_sensitive(page->timeout, FALSE);
119                 gtk_widget_set_sensitive(page->receive_spam, FALSE);
120                 gtk_widget_set_sensitive(page->save_folder, FALSE);
121                 gtk_widget_set_sensitive(page->save_folder_select, FALSE);
122                 break;
123         case PAGE_UNIX:
124                 gtk_widget_hide(page->hostname);
125                 gtk_widget_hide(page->colon);
126                 gtk_widget_hide(page->port);
127                 gtk_widget_show(page->socket);
128                 gtk_widget_set_sensitive(page->username, TRUE);
129                 gtk_widget_set_sensitive(page->socket, TRUE);
130                 gtk_widget_set_sensitive(page->max_size, TRUE);
131                 gtk_widget_set_sensitive(page->timeout, TRUE);
132                 gtk_widget_set_sensitive(page->receive_spam, TRUE);
133                 gtk_widget_set_sensitive(page->save_folder, TRUE);
134                 gtk_widget_set_sensitive(page->save_folder_select, TRUE);
135                 break;
136         case PAGE_NETWORK:
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, TRUE);
142                 gtk_widget_set_sensitive(page->max_size, TRUE);
143                 gtk_widget_set_sensitive(page->timeout, TRUE);
144                 gtk_widget_set_sensitive(page->receive_spam, TRUE);
145                 gtk_widget_set_sensitive(page->save_folder, TRUE);
146                 gtk_widget_set_sensitive(page->save_folder_select, TRUE);
147                 if (transport->pageflags & NETWORK_HOSTNAME) {
148                         gtk_widget_set_sensitive(page->hostname, TRUE);
149                         gtk_widget_set_sensitive(page->colon, TRUE);
150                         gtk_widget_set_sensitive(page->port, TRUE);
151                 } else {
152                         gtk_widget_set_sensitive(page->hostname, FALSE);
153                         gtk_widget_set_sensitive(page->colon, FALSE);
154                         gtk_widget_set_sensitive(page->port, TRUE);
155                 }
156                 break;
157         default:
158                 break;
159         }
160 /*      gtk_notebook_set_current_page(GTK_NOTEBOOK(page->transport_notebook), transport->page);*/
161 }
162
163 static void transport_sel_cb(GtkMenuItem *menuitem, gpointer data)
164 {
165         struct SpamAssassinPage *page = (struct SpamAssassinPage *) data;
166         struct Transport *transport;
167
168         transport = (struct Transport *) g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID);
169         show_transport(page, transport);
170 }
171
172 static void spamassassin_create_widget_func(PrefsPage * _page,
173                                             GtkWindow * window,
174                                             gpointer data)
175 {
176         struct SpamAssassinPage *page = (struct SpamAssassinPage *) _page;
177         SpamAssassinConfig *config;
178         guint i, active;
179
180         GtkWidget *table;
181         GtkWidget *label2;
182         GtkWidget *label3;
183         GtkWidget *label4;
184         GtkWidget *hbox1;
185         GtkWidget *hbox4;
186         GtkWidget *transport;
187         GtkWidget *transport_menu;
188 /*      GtkWidget *transport_notebook;*/
189         GtkWidget *spamd_hbox;
190         GtkWidget *username;
191         GtkWidget *hostname;
192         GtkWidget *colon;
193         GtkObject *port_adj;
194         GtkWidget *port;
195         GtkWidget *socket;
196         GtkWidget *label15;
197         GtkWidget *hbox6;
198         GtkObject *timeout_adj;
199         GtkWidget *timeout;
200         GtkWidget *label16;
201         GtkWidget *receive_spam;
202         GtkWidget *hbox3;
203         GtkObject *max_size_adj;
204         GtkWidget *max_size;
205         GtkWidget *label11;
206         GtkWidget *hbox2;
207         GtkWidget *save_folder;
208         GtkWidget *save_folder_select;
209         GtkWidget *label6;
210         GtkTooltips *tooltips;
211
212         tooltips = gtk_tooltips_new();
213
214         table = gtk_table_new(9, 3, FALSE);
215         gtk_widget_show(table);
216         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
217         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
218         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
219
220         hbox4 = gtk_hbox_new(FALSE, 8);
221         gtk_widget_show(hbox4);
222         gtk_table_attach(GTK_TABLE(table), hbox4, 0, 1, 0, 1,
223                          (GtkAttachOptions) (GTK_FILL),
224                          (GtkAttachOptions) (GTK_FILL), 0, 0);
225
226         label3 = gtk_label_new(_("Transport"));
227         gtk_widget_show(label3);
228         gtk_box_pack_start(GTK_BOX(hbox4), label3, FALSE, FALSE, 0);
229
230         transport = gtk_option_menu_new();
231         gtk_widget_show(transport);
232         gtk_box_pack_end(GTK_BOX(hbox4), transport, FALSE, FALSE, 0);
233         transport_menu = gtk_menu_new();
234
235 /*      transport_notebook = gtk_notebook_new();
236         gtk_widget_show(transport_notebook);
237         gtk_table_attach(GTK_TABLE(table), transport_notebook, 1, 2, 1, 2,
238                          (GtkAttachOptions) (GTK_FILL),
239                          (GtkAttachOptions) (GTK_FILL), 0, 0);
240         GTK_WIDGET_UNSET_FLAGS(transport_notebook, GTK_CAN_FOCUS);
241         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(transport_notebook),
242                                    FALSE);
243         gtk_notebook_set_show_border(GTK_NOTEBOOK(transport_notebook),
244                                      FALSE);
245 */
246
247         hbox1 = gtk_hbox_new(FALSE, 8);
248         gtk_widget_show(hbox1);
249         gtk_table_attach(GTK_TABLE(table), hbox1, 0, 1, 1, 2,
250                          (GtkAttachOptions) (GTK_FILL),
251                          (GtkAttachOptions) (GTK_FILL), 0, 0);
252
253         label2 = gtk_label_new(_("User"));
254         gtk_widget_show(label2);
255         gtk_box_pack_start(GTK_BOX(hbox1), label2, FALSE, FALSE, 0);
256
257         username = gtk_entry_new();
258         gtk_widget_show(username);
259         gtk_box_pack_end(GTK_BOX(hbox1), username, FALSE, FALSE, 0);
260
261         spamd_hbox = gtk_hbox_new (FALSE, 8);
262         gtk_widget_show (spamd_hbox);
263         gtk_table_attach (GTK_TABLE (table), spamd_hbox, 0, 1, 2, 3,
264                           (GtkAttachOptions) (GTK_FILL),
265                           (GtkAttachOptions) (GTK_FILL), 0, 0);
266
267         label4 = gtk_label_new(_("spamd"));
268         gtk_widget_show(label4);
269         gtk_box_pack_start(GTK_BOX(spamd_hbox), label4, FALSE, FALSE, 0);
270
271         port_adj = gtk_adjustment_new(783, 1, 65535, 1, 10, 10);
272         port = gtk_spin_button_new(GTK_ADJUSTMENT(port_adj), 1, 0);
273         gtk_widget_show(port);
274         gtk_box_pack_end(GTK_BOX(spamd_hbox), port, FALSE, FALSE, 0);
275         gtk_tooltips_set_tip(tooltips, port, _("Port of spamd server"),
276                              NULL);
277         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(port), TRUE);
278
279         colon = gtk_label_new(_(":"));
280         gtk_widget_show(colon);
281         gtk_box_pack_end(GTK_BOX(spamd_hbox), colon, FALSE, FALSE, 0);
282
283         hostname = gtk_entry_new();
284         gtk_widget_show(hostname);
285         gtk_box_pack_end(GTK_BOX(spamd_hbox), hostname, FALSE, FALSE, 0);
286         gtk_tooltips_set_tip(tooltips, hostname,
287                              _("Hostname or IP address of spamd server"),
288                              NULL);
289
290         socket = gtk_entry_new();
291         gtk_widget_show(socket);
292         gtk_box_pack_end(GTK_BOX(spamd_hbox), socket, FALSE, FALSE, 0);
293         gtk_tooltips_set_tip(tooltips, socket, _("Path of Unix socket"),
294                              NULL);
295
296         hbox3 = gtk_hbox_new (FALSE, 8);
297         gtk_widget_show (hbox3);
298         gtk_table_attach (GTK_TABLE (table), hbox3, 0, 1, 3, 4,
299                           (GtkAttachOptions) (GTK_FILL),
300                           (GtkAttachOptions) (GTK_FILL), 0, 0);
301
302         label6 = gtk_label_new(_("Maximum Size"));
303         gtk_widget_show(label6);
304         gtk_box_pack_start(GTK_BOX(hbox3), label6, FALSE, FALSE, 0);
305
306         max_size_adj = gtk_adjustment_new(250, 0, 10000, 10, 10, 10);
307         max_size = gtk_spin_button_new(GTK_ADJUSTMENT(max_size_adj), 1, 0);
308         gtk_widget_show(max_size);
309         gtk_box_pack_end(GTK_BOX(hbox3), max_size, FALSE, FALSE, 0);
310         gtk_tooltips_set_tip(tooltips, max_size,
311                         _("Maximum size a message is allowed to have to be checked"),
312                         NULL);
313         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(max_size), TRUE);
314
315         label11 = gtk_label_new(_("kB"));
316         gtk_widget_show(label11);
317         gtk_table_attach(GTK_TABLE(table), label11, 1, 2, 3, 4,
318                          (GtkAttachOptions) (GTK_FILL),
319                          (GtkAttachOptions) (GTK_FILL), 0, 0);
320
321         hbox6 = gtk_hbox_new(FALSE, 8);
322         gtk_widget_show(hbox6);
323         gtk_table_attach(GTK_TABLE(table), hbox6, 0, 1, 4, 5,
324                          (GtkAttachOptions) (GTK_FILL),
325                          (GtkAttachOptions) (GTK_FILL), 0, 0);
326
327         label15 = gtk_label_new(_("Timeout"));
328         gtk_widget_show(label15);
329         gtk_box_pack_start(GTK_BOX(hbox6), label15, FALSE, FALSE, 0);
330
331         timeout_adj = gtk_adjustment_new(60, 0, 10000, 10, 10, 10);
332         timeout = gtk_spin_button_new(GTK_ADJUSTMENT(timeout_adj), 1, 0);
333         gtk_widget_show(timeout);
334         gtk_box_pack_end(GTK_BOX(hbox6), timeout, FALSE, FALSE, 0);
335         gtk_tooltips_set_tip(tooltips, timeout,
336                 _("Time that is allowed for checking. If the check takes longer the check will be aborted and the message will be handled as not spam."),
337                 NULL);
338         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(timeout), TRUE);
339
340         label16 = gtk_label_new(_("s"));
341         gtk_widget_show(label16);
342         gtk_table_attach(GTK_TABLE(table), label16, 1, 2, 4, 5,
343                          (GtkAttachOptions) (GTK_FILL),
344                          (GtkAttachOptions) (GTK_FILL), 0, 0);
345
346         receive_spam = gtk_check_button_new_with_label(_("Save Spam"));
347         gtk_widget_show(receive_spam);
348         gtk_table_attach(GTK_TABLE(table), receive_spam, 0, 1, 5, 6,
349                          (GtkAttachOptions) (GTK_FILL),
350                          (GtkAttachOptions) (0), 0, 0);
351         gtk_tooltips_set_tip(tooltips, receive_spam,
352                              _("Save mails that where identified as spam"),
353                              NULL);
354
355         hbox2 = gtk_hbox_new (FALSE, 8);
356         gtk_widget_show (hbox2);
357         gtk_table_attach (GTK_TABLE (table), hbox2, 0, 1, 6, 7,
358                           (GtkAttachOptions) (GTK_FILL),
359                           (GtkAttachOptions) (GTK_FILL), 0, 0);
360         SET_TOGGLE_SENSITIVITY (receive_spam, hbox2);
361
362         label3 = gtk_label_new (_("Save folder"));
363         gtk_widget_show (label3);
364         gtk_box_pack_start (GTK_BOX (hbox2), label3, FALSE, FALSE, 0);
365
366         tooltips = gtk_tooltips_new();
367         save_folder = gtk_entry_new ();
368         gtk_widget_show (save_folder);
369         gtk_box_pack_start (GTK_BOX (hbox2), save_folder, TRUE, FALSE, 0);
370         gtk_tooltips_set_tip(tooltips, save_folder,
371                              _("Leave empty to use the default trash folder"),
372                              NULL);
373
374         save_folder_select = gtkut_get_browse_directory_btn(_("_Browse"));
375         gtk_widget_show (save_folder_select);
376         gtk_box_pack_end (GTK_BOX (hbox2), save_folder_select, FALSE, FALSE, 0);
377         gtk_tooltips_set_tip(tooltips, save_folder_select,
378                              _("Leave empty to use the default trash folder"),
379                              NULL);
380
381         config = spamassassin_get_config();
382
383         g_signal_connect(G_OBJECT(save_folder_select), "released",
384                          G_CALLBACK(foldersel_cb), page);
385
386         if (config->username != NULL)
387                 gtk_entry_set_text(GTK_ENTRY(username), config->username);
388         if (config->hostname != NULL)
389                 gtk_entry_set_text(GTK_ENTRY(hostname), config->hostname);
390         gtk_spin_button_set_value(GTK_SPIN_BUTTON(port), (float) config->port);
391         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(receive_spam), config->receive_spam);
392         if (config->save_folder != NULL)
393                 gtk_entry_set_text(GTK_ENTRY(save_folder), config->save_folder);
394         gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_size), (float) config->max_size);
395         gtk_spin_button_set_value(GTK_SPIN_BUTTON(timeout), (float) config->timeout);
396         
397         page->transport = transport;
398 /*      page->transport_notebook = transport_notebook;*/
399         page->username = username;
400         page->hostname = hostname;
401         page->colon = colon;
402         page->port = port;
403         page->socket = socket;
404         page->receive_spam = receive_spam;
405         page->save_folder = save_folder;
406         page->save_folder_select = save_folder_select;
407         page->max_size = max_size;
408         page->timeout = timeout;
409
410         active = 0;
411         for (i = 0; i < (sizeof(transports) / sizeof(struct Transport)); i++) {
412                 GtkWidget *menuitem;
413
414                 menuitem = gtk_menu_item_new_with_label(gettext(transports[i].name));
415                 g_object_set_data(G_OBJECT(menuitem), MENU_VAL_ID, &transports[i]);
416                 g_signal_connect(G_OBJECT(menuitem), "activate",
417                                  G_CALLBACK(transport_sel_cb), page);
418                 gtk_widget_show(menuitem);
419                 gtk_menu_append(GTK_MENU(transport_menu), menuitem);
420
421                 if (config->transport == transports[i].transport) {
422                         show_transport(page, &transports[i]);
423                         active = i;
424                 }
425         }
426         gtk_option_menu_set_menu(GTK_OPTION_MENU(transport), transport_menu);
427         gtk_option_menu_set_history(GTK_OPTION_MENU(transport), active);
428
429         page->page.widget = table;
430 }
431
432 static void spamassassin_destroy_widget_func(PrefsPage *_page)
433 {
434         debug_print("Destroying SpamAssassin widget\n");
435 }
436
437 static void spamassassin_save_func(PrefsPage *_page)
438 {
439         struct SpamAssassinPage *page = (struct SpamAssassinPage *) _page;
440         SpamAssassinConfig *config;
441
442         debug_print("Saving SpamAssassin Page\n");
443
444         config = spamassassin_get_config();
445
446         /* enable */
447         config->transport = page->trans;
448
449         /* username */
450         g_free(config->username);
451         config->username = gtk_editable_get_chars(GTK_EDITABLE(page->username), 0, -1);
452         spamassassin_check_username();
453
454         /* hostname */
455         g_free(config->hostname);
456         config->hostname = gtk_editable_get_chars(GTK_EDITABLE(page->hostname), 0, -1);
457
458         /* port */
459         config->port = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->port));
460
461         /* hostname */
462         g_free(config->socket);
463         config->socket = gtk_editable_get_chars(GTK_EDITABLE(page->socket), 0, -1);
464
465         /* receive_spam */
466         config->receive_spam = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->receive_spam));
467
468         /* save_folder */
469         g_free(config->save_folder);
470         config->save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1);
471
472         /* max_size */
473         config->max_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->max_size));
474
475         /* timeout */
476         config->timeout = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->timeout));
477
478         if (config->transport == SPAMASSASSIN_DISABLED) {
479                 procmsg_unregister_spam_learner(spamassassin_learn);
480                 procmsg_spam_set_folder(NULL);
481         } else {
482                 if (config->transport == SPAMASSASSIN_TRANSPORT_TCP)
483                         debug_print("enabling learner with a remote spamassassin server requires spamc/spamd 3.1.x\n");
484                 procmsg_register_spam_learner(spamassassin_learn);
485                 procmsg_spam_set_folder(config->save_folder);
486         }
487         spamassassin_save_config();
488 }
489
490 static void gtk_message_callback(gchar *message)
491 {
492         statusbar_print_all(message);
493 }
494
495 static struct SpamAssassinPage spamassassin_page;
496
497 gint spamassassin_gtk_init(void)
498 {
499         static gchar *path[3];
500
501         path[0] = _("Plugins");
502         path[1] = _("SpamAssassin");
503         path[2] = NULL;
504
505         spamassassin_page.page.path = path;
506         spamassassin_page.page.create_widget = spamassassin_create_widget_func;
507         spamassassin_page.page.destroy_widget = spamassassin_destroy_widget_func;
508         spamassassin_page.page.save_page = spamassassin_save_func;
509         spamassassin_page.page.weight = 35.0;
510
511         prefs_gtk_register_page((PrefsPage *) &spamassassin_page);
512         spamassassin_set_message_callback(gtk_message_callback);
513
514         debug_print("SpamAssassin GTK plugin loaded\n");
515         return 0;       
516 }
517
518 void spamassassin_gtk_done(void)
519 {
520         prefs_gtk_unregister_page((PrefsPage *) &spamassassin_page);
521 }