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