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