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