2005-02-10 [paul] 1.0.1cvs3.2
[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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
30 #include "common/sylpheed.h"
31 #include "common/version.h"
32 #include "plugin.h"
33 #include "common/utils.h"
34 #include "prefs.h"
35 #include "folder.h"
36 #include "prefs_gtk.h"
37 #include "foldersel.h"
38 #include "spamassassin.h"
39 #include "statusbar.h"
40 #include "menu.h"
41
42 struct SpamAssassinPage
43 {
44         PrefsPage page;
45         
46         GtkWidget *transport;
47         GtkWidget *transport_notebook;
48         GtkWidget *hostname;
49         GtkWidget *colon;
50         GtkWidget *port;
51         GtkWidget *socket;
52         GtkWidget *receive_spam;
53         GtkWidget *save_folder;
54         GtkWidget *max_size;
55         GtkWidget *timeout;
56
57         SpamAssassinTransport   trans;
58 };
59
60 struct Transport
61 {
62         gchar                   *name;
63         SpamAssassinTransport    transport;
64         guint                    page;
65         guint                    pageflags;
66 };
67
68 enum {
69         PAGE_NETWORK = 0,
70         PAGE_UNIX    = 1,
71 };
72
73 enum {
74         NETWORK_HOSTNAME = 1,
75 };
76
77 struct Transport transports[] = {
78         { N_("Disabled"),       SPAMASSASSIN_DISABLED,                  PAGE_NETWORK, 0 },
79         { N_("Localhost"),      SPAMASSASSIN_TRANSPORT_LOCALHOST,       PAGE_NETWORK, 0 },
80         { N_("TCP"),            SPAMASSASSIN_TRANSPORT_TCP,             PAGE_NETWORK, NETWORK_HOSTNAME },
81         { N_("Unix Socket"),    SPAMASSASSIN_TRANSPORT_UNIX,            PAGE_UNIX,    0 },
82 };
83
84 static void foldersel_cb(GtkWidget *widget, gpointer data)
85 {
86         struct SpamAssassinPage *page = (struct SpamAssassinPage *) data;
87         FolderItem *item;
88         gchar *item_id;
89         gint newpos = 0;
90         
91         item = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL);
92         if (item && (item_id = folder_item_get_identifier(item)) != NULL) {
93                 gtk_editable_delete_text(GTK_EDITABLE(page->save_folder), 0, -1);
94                 gtk_editable_insert_text(GTK_EDITABLE(page->save_folder), item_id, strlen(item_id), &newpos);
95                 g_free(item_id);
96         }
97 }
98
99 static void show_transport(struct SpamAssassinPage *page, struct Transport *transport)
100 {
101         page->trans = transport->transport;
102
103         switch (transport->page) {
104         case PAGE_NETWORK:
105                 if (transport->pageflags & NETWORK_HOSTNAME) {
106                         gtk_widget_show(page->hostname);
107                         gtk_widget_show(page->colon);
108                 } else {
109                         gtk_widget_hide(page->hostname);
110                         gtk_widget_hide(page->colon);
111                 }
112                 break;
113         default:
114                 break;
115         }
116         gtk_notebook_set_current_page(GTK_NOTEBOOK(page->transport_notebook), transport->page);
117 }
118
119 static void transport_sel_cb(GtkMenuItem *menuitem, gpointer data)
120 {
121         struct SpamAssassinPage *page = (struct SpamAssassinPage *) data;
122         struct Transport *transport;
123
124         transport = (struct Transport *) g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID);
125         show_transport(page, transport);
126 }
127
128 static void spamassassin_create_widget_func(PrefsPage * _page,
129                                             GtkWindow * window,
130                                             gpointer data)
131 {
132         struct SpamAssassinPage *page = (struct SpamAssassinPage *) _page;
133         SpamAssassinConfig *config;
134         guint i, active;
135
136         /*
137          * BEGIN GLADE CODE
138          * DO NOT EDIT
139          */
140         GtkWidget *table;
141         GtkWidget *label3;
142         GtkWidget *label4;
143         GtkWidget *hbox4;
144         GtkWidget *transport;
145         GtkWidget *transport_menu;
146         GtkWidget *transport_notebook;
147         GtkWidget *hbox1;
148         GtkWidget *hostname;
149         GtkWidget *colon;
150         GtkObject *port_adj;
151         GtkWidget *port;
152         GtkWidget *socket;
153         GtkWidget *label15;
154         GtkWidget *hbox6;
155         GtkObject *timeout_adj;
156         GtkWidget *timeout;
157         GtkWidget *label16;
158         GtkWidget *label9;
159         GtkWidget *receive_spam;
160         GtkWidget *hbox3;
161         GtkObject *max_size_adj;
162         GtkWidget *max_size;
163         GtkWidget *label11;
164         GtkWidget *label8;
165         GtkWidget *save_folder;
166         GtkWidget *button4;
167         GtkWidget *label6;
168         GtkTooltips *tooltips;
169
170         tooltips = gtk_tooltips_new();
171
172         table = gtk_table_new(6, 3, FALSE);
173         gtk_widget_show(table);
174         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
175         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
176         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
177
178         label3 = gtk_label_new(_("Transport"));
179         gtk_widget_show(label3);
180         gtk_table_attach(GTK_TABLE(table), label3, 0, 1, 0, 1,
181                          (GtkAttachOptions) (GTK_FILL),
182                          (GtkAttachOptions) (0), 0, 0);
183         gtk_misc_set_alignment(GTK_MISC(label3), 0, 0.5);
184
185         label4 = gtk_label_new(_("spamd "));
186         gtk_widget_show(label4);
187         gtk_table_attach(GTK_TABLE(table), label4, 0, 1, 1, 2,
188                          (GtkAttachOptions) (GTK_FILL),
189                          (GtkAttachOptions) (0), 0, 0);
190         gtk_misc_set_alignment(GTK_MISC(label4), 0, 0.5);
191
192         hbox4 = gtk_hbox_new(FALSE, 0);
193         gtk_widget_show(hbox4);
194         gtk_table_attach(GTK_TABLE(table), hbox4, 1, 2, 0, 1,
195                          (GtkAttachOptions) (GTK_FILL),
196                          (GtkAttachOptions) (GTK_FILL), 0, 0);
197
198         transport = gtk_option_menu_new();
199         gtk_widget_show(transport);
200         gtk_box_pack_start(GTK_BOX(hbox4), transport, FALSE, FALSE, 0);
201         transport_menu = gtk_menu_new();
202
203         transport_notebook = gtk_notebook_new();
204         gtk_widget_show(transport_notebook);
205         gtk_table_attach(GTK_TABLE(table), transport_notebook, 1, 2, 1, 2,
206                          (GtkAttachOptions) (GTK_FILL),
207                          (GtkAttachOptions) (GTK_FILL), 0, 0);
208         GTK_WIDGET_UNSET_FLAGS(transport_notebook, GTK_CAN_FOCUS);
209         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(transport_notebook),
210                                    FALSE);
211         gtk_notebook_set_show_border(GTK_NOTEBOOK(transport_notebook),
212                                      FALSE);
213
214         hbox1 = gtk_hbox_new(FALSE, 0);
215         gtk_widget_show(hbox1);
216         gtk_container_add(GTK_CONTAINER(transport_notebook), hbox1);
217
218         hostname = gtk_entry_new();
219         gtk_widget_show(hostname);
220         gtk_box_pack_start(GTK_BOX(hbox1), hostname, TRUE, TRUE, 0);
221         gtk_tooltips_set_tip(tooltips, hostname,
222                              _("Hostname or IP address of spamd server"),
223                              NULL);
224
225         colon = gtk_label_new(_(":"));
226         gtk_widget_show(colon);
227         gtk_box_pack_start(GTK_BOX(hbox1), colon, FALSE, FALSE, 0);
228         gtk_misc_set_padding(GTK_MISC(colon), 8, 0);
229
230         port_adj = gtk_adjustment_new(783, 1, 65535, 1, 10, 10);
231         port = gtk_spin_button_new(GTK_ADJUSTMENT(port_adj), 1, 0);
232         gtk_widget_show(port);
233         gtk_box_pack_end(GTK_BOX(hbox1), port, FALSE, TRUE, 0);
234         gtk_widget_set_usize(port, 64, -2);
235         gtk_tooltips_set_tip(tooltips, port, _("Port of spamd server"),
236                              NULL);
237         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(port), TRUE);
238
239         socket = gtk_entry_new();
240         gtk_widget_show(socket);
241         gtk_container_add(GTK_CONTAINER(transport_notebook), socket);
242         gtk_tooltips_set_tip(tooltips, socket, _("Path of Unix socket"),
243                              NULL);
244
245         label15 = gtk_label_new(_("Timeout"));
246         gtk_widget_show(label15);
247         gtk_table_attach(GTK_TABLE(table), label15, 0, 1, 5, 6,
248                          (GtkAttachOptions) (GTK_FILL),
249                          (GtkAttachOptions) (0), 0, 0);
250         gtk_misc_set_alignment(GTK_MISC(label15), 0, 0.5);
251
252         hbox6 = gtk_hbox_new(FALSE, 0);
253         gtk_widget_show(hbox6);
254         gtk_table_attach(GTK_TABLE(table), hbox6, 1, 2, 5, 6,
255                          (GtkAttachOptions) (GTK_FILL),
256                          (GtkAttachOptions) (GTK_FILL), 0, 0);
257
258         timeout_adj = gtk_adjustment_new(60, 0, 10000, 10, 10, 10);
259         timeout = gtk_spin_button_new(GTK_ADJUSTMENT(timeout_adj), 1, 0);
260         gtk_widget_show(timeout);
261         gtk_box_pack_end(GTK_BOX(hbox6), timeout, FALSE, TRUE, 0);
262         gtk_widget_set_usize(timeout, 64, -2);
263         gtk_tooltips_set_tip(tooltips, timeout,
264                              _
265                              ("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."),
266                              NULL);
267         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(timeout), TRUE);
268
269         label16 = gtk_label_new(_("s"));
270         gtk_widget_show(label16);
271         gtk_table_attach(GTK_TABLE(table), label16, 2, 3, 5, 6,
272                          (GtkAttachOptions) (GTK_FILL),
273                          (GtkAttachOptions) (0), 0, 0);
274         gtk_misc_set_alignment(GTK_MISC(label16), 0, 0.5);
275
276         label9 = gtk_label_new(_("Save Spam"));
277         gtk_widget_show(label9);
278         gtk_table_attach(GTK_TABLE(table), label9, 0, 1, 2, 3,
279                          (GtkAttachOptions) (GTK_FILL),
280                          (GtkAttachOptions) (0), 0, 0);
281         gtk_misc_set_alignment(GTK_MISC(label9), 0, 0.5);
282
283         receive_spam = gtk_check_button_new_with_label("");
284         gtk_widget_show(receive_spam);
285         gtk_table_attach(GTK_TABLE(table), receive_spam, 1, 2, 2, 3,
286                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
287                          (GtkAttachOptions) (0), 0, 0);
288         gtk_tooltips_set_tip(tooltips, receive_spam,
289                              _
290                              ("Save mails that where identified as spam to a folder"),
291                              NULL);
292
293         hbox3 = gtk_hbox_new(FALSE, 0);
294         gtk_widget_show(hbox3);
295         gtk_table_attach(GTK_TABLE(table), hbox3, 1, 2, 4, 5,
296                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
297                          (GtkAttachOptions) (GTK_FILL), 0, 0);
298
299         max_size_adj = gtk_adjustment_new(250, 0, 10000, 10, 10, 10);
300         max_size = gtk_spin_button_new(GTK_ADJUSTMENT(max_size_adj), 1, 0);
301         gtk_widget_show(max_size);
302         gtk_box_pack_end(GTK_BOX(hbox3), max_size, FALSE, TRUE, 0);
303         gtk_widget_set_usize(max_size, 64, -2);
304         gtk_tooltips_set_tip(tooltips, max_size,
305                              _
306                              ("Maximum size a message is allowed to have to be checked"),
307                              NULL);
308         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(max_size), TRUE);
309
310         label11 = gtk_label_new(_("kB"));
311         gtk_widget_show(label11);
312         gtk_table_attach(GTK_TABLE(table), label11, 2, 3, 4, 5,
313                          (GtkAttachOptions) (GTK_FILL),
314                          (GtkAttachOptions) (0), 0, 0);
315         gtk_misc_set_alignment(GTK_MISC(label11), 0, 0.5);
316
317         label8 = gtk_label_new(_("Save Folder"));
318         gtk_widget_show(label8);
319         gtk_table_attach(GTK_TABLE(table), label8, 0, 1, 3, 4,
320                          (GtkAttachOptions) (GTK_FILL),
321                          (GtkAttachOptions) (0), 0, 0);
322         gtk_label_set_justify(GTK_LABEL(label8), GTK_JUSTIFY_LEFT);
323         gtk_misc_set_alignment(GTK_MISC(label8), 0, 0.5);
324
325         save_folder = gtk_entry_new();
326         gtk_widget_show(save_folder);
327         gtk_table_attach(GTK_TABLE(table), save_folder, 1, 2, 3, 4,
328                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
329                          (GtkAttachOptions) (0), 0, 0);
330         gtk_tooltips_set_tip(tooltips, save_folder,
331                              _
332                              ("Folder that will be used to save spam. Leave empty to use the default trash folder"),
333                              NULL);
334
335         button4 = gtk_button_new_with_label(_("..."));
336         gtk_widget_show(button4);
337         gtk_table_attach(GTK_TABLE(table), button4, 2, 3, 3, 4,
338                          (GtkAttachOptions) (GTK_SHRINK | GTK_FILL),
339                          (GtkAttachOptions) (0), 0, 0);
340
341         label6 = gtk_label_new(_("Maximum Size"));
342         gtk_widget_show(label6);
343         gtk_table_attach(GTK_TABLE(table), label6, 0, 1, 4, 5,
344                          (GtkAttachOptions) (GTK_FILL),
345                          (GtkAttachOptions) (0), 0, 0);
346         gtk_misc_set_alignment(GTK_MISC(label6), 0, 0.5);
347         /*
348          * END GLADE CODE
349          */
350
351         config = spamassassin_get_config();
352
353         g_signal_connect(G_OBJECT(button4), "released", 
354                          G_CALLBACK(foldersel_cb), page);
355
356         if (config->hostname != NULL)
357                 gtk_entry_set_text(GTK_ENTRY(hostname), config->hostname);
358         gtk_spin_button_set_value(GTK_SPIN_BUTTON(port), (float) config->port);
359         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(receive_spam), config->receive_spam);
360         if (config->save_folder != NULL)
361                 gtk_entry_set_text(GTK_ENTRY(save_folder), config->save_folder);
362         gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_size), (float) config->max_size);
363         gtk_spin_button_set_value(GTK_SPIN_BUTTON(timeout), (float) config->timeout);
364         
365         page->transport = transport;
366         page->transport_notebook = transport_notebook;
367         page->hostname = hostname;
368         page->colon = colon;
369         page->port = port;
370         page->socket = socket;
371         page->receive_spam = receive_spam;
372         page->save_folder = save_folder;
373         page->max_size = max_size;
374         page->timeout = timeout;
375
376         active = 0;
377         for (i = 0; i < (sizeof(transports) / sizeof(struct Transport)); i++) {
378                 GtkWidget *menuitem;
379
380                 menuitem = gtk_menu_item_new_with_label(gettext(transports[i].name));
381                 g_object_set_data(G_OBJECT(menuitem), MENU_VAL_ID, &transports[i]);
382                 g_signal_connect(G_OBJECT(menuitem), "activate",
383                                  G_CALLBACK(transport_sel_cb), page);
384                 gtk_widget_show(menuitem);
385                 gtk_menu_append(GTK_MENU(transport_menu), menuitem);
386
387                 if (config->transport == transports[i].transport) {
388                         show_transport(page, &transports[i]);
389                         active = i;
390                 }
391         }
392         gtk_option_menu_set_menu(GTK_OPTION_MENU(transport), transport_menu);
393         gtk_option_menu_set_history(GTK_OPTION_MENU(transport), active);
394
395         page->page.widget = table;
396 }
397
398 static void spamassassin_destroy_widget_func(PrefsPage *_page)
399 {
400         debug_print("Destroying SpamAssassin widget\n");
401 }
402
403 static void spamassassin_save_func(PrefsPage *_page)
404 {
405         struct SpamAssassinPage *page = (struct SpamAssassinPage *) _page;
406         SpamAssassinConfig *config;
407
408         debug_print("Saving SpamAssassin Page\n");
409
410         config = spamassassin_get_config();
411
412         /* enable */
413         config->transport = page->trans;
414
415         /* hostname */
416         g_free(config->hostname);
417         config->hostname = gtk_editable_get_chars(GTK_EDITABLE(page->hostname), 0, -1);
418
419         /* port */
420         config->port = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->port));
421
422         /* hostname */
423         g_free(config->socket);
424         config->socket = gtk_editable_get_chars(GTK_EDITABLE(page->socket), 0, -1);
425
426         /* receive_spam */
427         config->receive_spam = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->receive_spam));
428
429         /* save_folder */
430         g_free(config->save_folder);
431         config->save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1);
432
433         /* max_size */
434         config->max_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->max_size));
435
436         /* timeout */
437         config->timeout = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->timeout));
438
439         spamassassin_save_config();
440 }
441
442 static void gtk_message_callback(gchar *message)
443 {
444         statusbar_print_all(message);
445 }
446
447 static struct SpamAssassinPage spamassassin_page;
448
449 gint plugin_init(gchar **error)
450 {
451         static gchar *path[3];
452
453         if ((sylpheed_get_version() > VERSION_NUMERIC)) {
454                 *error = g_strdup("Your sylpheed version is newer than the version the plugin was built with");
455                 return -1;
456         }
457
458         if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
459                 *error = g_strdup("Your sylpheed version is too old");
460                 return -1;
461         }
462     
463         path[0] = _("Filtering");
464         path[1] = _("SpamAssassin");
465         path[2] = NULL;
466
467         spamassassin_page.page.path = path;
468         spamassassin_page.page.create_widget = spamassassin_create_widget_func;
469         spamassassin_page.page.destroy_widget = spamassassin_destroy_widget_func;
470         spamassassin_page.page.save_page = spamassassin_save_func;
471         spamassassin_page.page.weight = 35.0;
472
473         prefs_gtk_register_page((PrefsPage *) &spamassassin_page);
474         spamassassin_set_message_callback(gtk_message_callback);
475
476         debug_print("SpamAssassin GTK plugin loaded\n");
477         return 0;       
478 }
479
480 void plugin_done(void)
481 {
482         spamassassin_set_message_callback(NULL);
483         prefs_gtk_unregister_page((PrefsPage *) &spamassassin_page);
484
485         debug_print("SpamAssassin GTK plugin unloaded\n");
486 }
487
488 const gchar *plugin_name(void)
489 {
490         return _("SpamAssassin GTK");
491 }
492
493 const gchar *plugin_desc(void)
494 {
495         return _("This plugin provides a Preferences page for the SpamAssassin "
496                  "plugin.\n"
497                  "\n"
498                  "You will find the options in the Other Preferences window "
499                  "under Filtering/SpamAssassin.\n"
500                  "\n"
501                  "With this plugin you can enable the filtering, change the "
502                  "SpamAssassin server host and port, set the maximum size of "
503                  "messages to be checked, (if the message is larger it will "
504                  "not be checked), configure whether spam mail should be received "
505                  "(default: Yes) and select the folder where spam mail will be "
506                  "saved.\n");
507 }
508
509 const gchar *plugin_type(void)
510 {
511         return "GTK2";
512 }