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