0.9.4claws58
[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 *enable;
45         GtkWidget *hostname;
46         GtkWidget *port;
47         GtkWidget *receive_spam;
48         GtkWidget *save_folder;
49         GtkWidget *max_size;
50         GtkWidget *timeout
51 };
52
53 static void foldersel_cb(GtkWidget *widget, gpointer data)
54 {
55         struct SpamAssassinPage *page = (struct SpamAssassinPage *) data;
56         FolderItem *item;
57         gchar *item_id;
58         gint newpos = 0;
59         
60         item = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL);
61         if (item && (item_id = folder_item_get_identifier(item)) != NULL) {
62                 gtk_editable_delete_text(GTK_EDITABLE(page->save_folder), 0, -1);
63                 gtk_editable_insert_text(GTK_EDITABLE(page->save_folder), item_id, strlen(item_id), &newpos);
64                 g_free(item_id);
65         }
66 }
67
68 static void spamassassin_create_widget_func(PrefsPage * _page,
69                                             GtkWindow * window,
70                                             gpointer data)
71 {
72         struct SpamAssassinPage *page = (struct SpamAssassinPage *) _page;
73         SpamAssassinConfig *config;
74
75         /*
76          * BEGIN GLADE CODE
77          * DO NOT EDIT
78          */
79         GtkWidget *table1;
80         GtkWidget *label3;
81         GtkWidget *label4;
82         GtkWidget *hbox1;
83         GtkWidget *hostname;
84         GtkWidget *label5;
85         GtkObject *port_adj;
86         GtkWidget *port;
87         GtkWidget *enable;
88         GtkWidget *label9;
89         GtkWidget *receive_spam;
90         GtkWidget *label8;
91         GtkWidget *save_folder;
92         GtkWidget *button4;
93         GtkWidget *label6;
94         GtkWidget *hbox3;
95         GtkObject *max_size_adj;
96         GtkWidget *max_size;
97         GtkWidget *label11;
98         GtkWidget *label12;
99         GtkWidget *label13;
100         GtkWidget *hbox4;
101         GtkObject *timeout_adj;
102         GtkWidget *timeout;
103         GtkTooltips *tooltips;
104
105         tooltips = gtk_tooltips_new();
106
107         table1 = gtk_table_new(6, 3, FALSE);
108         gtk_widget_show(table1);
109         gtk_container_set_border_width(GTK_CONTAINER(table1), 8);
110         gtk_table_set_row_spacings(GTK_TABLE(table1), 4);
111         gtk_table_set_col_spacings(GTK_TABLE(table1), 8);
112
113         label3 = gtk_label_new(_("Enable"));
114         gtk_widget_show(label3);
115         gtk_table_attach(GTK_TABLE(table1), label3, 0, 1, 0, 1,
116                          (GtkAttachOptions) (GTK_FILL),
117                          (GtkAttachOptions) (0), 0, 0);
118         gtk_misc_set_alignment(GTK_MISC(label3), 0, 0.5);
119
120         label4 = gtk_label_new(_("spamd "));
121         gtk_widget_show(label4);
122         gtk_table_attach(GTK_TABLE(table1), label4, 0, 1, 1, 2,
123                          (GtkAttachOptions) (GTK_FILL),
124                          (GtkAttachOptions) (0), 0, 0);
125         gtk_misc_set_alignment(GTK_MISC(label4), 0, 0.5);
126
127         hbox1 = gtk_hbox_new(FALSE, 0);
128         gtk_widget_show(hbox1);
129         gtk_table_attach(GTK_TABLE(table1), hbox1, 1, 2, 1, 2,
130                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
131                          (GtkAttachOptions) (GTK_FILL), 0, 0);
132
133         hostname = gtk_entry_new();
134         gtk_widget_show(hostname);
135         gtk_box_pack_start(GTK_BOX(hbox1), hostname, TRUE, TRUE, 0);
136         gtk_tooltips_set_tip(tooltips, hostname,
137                              _("Hostname or IP address of spamd server"),
138                              NULL);
139
140         label5 = gtk_label_new(_(":"));
141         gtk_widget_show(label5);
142         gtk_box_pack_start(GTK_BOX(hbox1), label5, FALSE, FALSE, 0);
143         gtk_misc_set_padding(GTK_MISC(label5), 8, 0);
144
145         port_adj = gtk_adjustment_new(783, 1, 65535, 1, 10, 10);
146         port = gtk_spin_button_new(GTK_ADJUSTMENT(port_adj), 1, 0);
147         gtk_widget_show(port);
148         gtk_box_pack_end(GTK_BOX(hbox1), port, FALSE, TRUE, 0);
149         gtk_widget_set_usize(port, 64, -2);
150         gtk_tooltips_set_tip(tooltips, port, _("Port of spamd server"),
151                              NULL);
152         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(port), TRUE);
153
154         enable = gtk_check_button_new_with_label("");
155         gtk_widget_show(enable);
156         gtk_table_attach(GTK_TABLE(table1), enable, 1, 2, 0, 1,
157                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
158                          (GtkAttachOptions) (0), 0, 0);
159         gtk_tooltips_set_tip(tooltips, enable,
160                              _("Enable SpamAssassin filtering"), NULL);
161
162         label9 = gtk_label_new(_("Save Spam"));
163         gtk_widget_show(label9);
164         gtk_table_attach(GTK_TABLE(table1), label9, 0, 1, 2, 3,
165                          (GtkAttachOptions) (GTK_FILL),
166                          (GtkAttachOptions) (0), 0, 0);
167         gtk_misc_set_alignment(GTK_MISC(label9), 0, 0.5);
168
169         receive_spam = gtk_check_button_new_with_label("");
170         gtk_widget_show(receive_spam);
171         gtk_table_attach(GTK_TABLE(table1), receive_spam, 1, 2, 2, 3,
172                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
173                          (GtkAttachOptions) (0), 0, 0);
174         gtk_tooltips_set_tip(tooltips, receive_spam,
175                              _
176                              ("Save mails that where identified as spam to a folder"),
177                              NULL);
178
179         label8 = gtk_label_new(_("Save Folder"));
180         gtk_widget_show(label8);
181         gtk_table_attach(GTK_TABLE(table1), label8, 0, 1, 3, 4,
182                          (GtkAttachOptions) (GTK_FILL),
183                          (GtkAttachOptions) (0), 0, 0);
184         gtk_label_set_justify(GTK_LABEL(label8), GTK_JUSTIFY_LEFT);
185         gtk_misc_set_alignment(GTK_MISC(label8), 0, 0.5);
186
187         save_folder = gtk_entry_new();
188         gtk_widget_show(save_folder);
189         gtk_table_attach(GTK_TABLE(table1), save_folder, 1, 2, 3, 4,
190                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
191                          (GtkAttachOptions) (0), 0, 0);
192         gtk_tooltips_set_tip(tooltips, save_folder,
193                              _
194                              ("Folder that will be used to save spam. Leave empty to use the default trash folder"),
195                              NULL);
196
197         button4 = gtk_button_new_with_label(_("..."));
198         gtk_widget_show(button4);
199         gtk_table_attach(GTK_TABLE(table1), button4, 2, 3, 3, 4,
200                          (GtkAttachOptions) (GTK_SHRINK | GTK_FILL),
201                          (GtkAttachOptions) (0), 0, 0);
202
203         label6 = gtk_label_new(_("Maximum Size"));
204         gtk_widget_show(label6);
205         gtk_table_attach(GTK_TABLE(table1), label6, 0, 1, 4, 5,
206                          (GtkAttachOptions) (GTK_FILL),
207                          (GtkAttachOptions) (0), 0, 0);
208         gtk_misc_set_alignment(GTK_MISC(label6), 0, 0.5);
209
210         hbox3 = gtk_hbox_new(FALSE, 0);
211         gtk_widget_show(hbox3);
212         gtk_table_attach(GTK_TABLE(table1), hbox3, 1, 2, 4, 5,
213                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
214                          (GtkAttachOptions) (GTK_FILL), 0, 0);
215
216         max_size_adj = gtk_adjustment_new(250, 0, 10000, 10, 10, 10);
217         max_size = gtk_spin_button_new(GTK_ADJUSTMENT(max_size_adj), 1, 0);
218         gtk_widget_show(max_size);
219         gtk_box_pack_end(GTK_BOX(hbox3), max_size, FALSE, TRUE, 0);
220         gtk_widget_set_usize(max_size, 64, -2);
221         gtk_tooltips_set_tip(tooltips, max_size,
222                              _
223                              ("Maximum size a message is allowed to have to be checked"),
224                              NULL);
225         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(max_size), TRUE);
226
227         label11 = gtk_label_new(_("kB"));
228         gtk_widget_show(label11);
229         gtk_table_attach(GTK_TABLE(table1), label11, 2, 3, 4, 5,
230                          (GtkAttachOptions) (GTK_FILL),
231                          (GtkAttachOptions) (0), 0, 0);
232         gtk_misc_set_alignment(GTK_MISC(label11), 0, 0.5);
233
234         label12 = gtk_label_new(_("Timeout"));
235         gtk_widget_show(label12);
236         gtk_table_attach(GTK_TABLE(table1), label12, 0, 1, 5, 6,
237                          (GtkAttachOptions) (GTK_FILL),
238                          (GtkAttachOptions) (0), 0, 0);
239         gtk_misc_set_alignment(GTK_MISC(label12), 0, 0.5);
240
241         label13 = gtk_label_new(_("s"));
242         gtk_widget_show(label13);
243         gtk_table_attach(GTK_TABLE(table1), label13, 2, 3, 5, 6,
244                          (GtkAttachOptions) (GTK_FILL),
245                          (GtkAttachOptions) (0), 0, 0);
246         gtk_misc_set_alignment(GTK_MISC(label13), 0, 0.5);
247
248         hbox4 = gtk_hbox_new(FALSE, 0);
249         gtk_widget_show(hbox4);
250         gtk_table_attach(GTK_TABLE(table1), hbox4, 1, 2, 5, 6,
251                          (GtkAttachOptions) (GTK_FILL),
252                          (GtkAttachOptions) (GTK_FILL), 0, 0);
253
254         timeout_adj = gtk_adjustment_new(30, 5, 300, 1, 1, 1);
255         timeout = gtk_spin_button_new(GTK_ADJUSTMENT(timeout_adj), 1, 0);
256         gtk_widget_show(timeout);
257         gtk_box_pack_end(GTK_BOX(hbox4), timeout, FALSE, TRUE, 0);
258         gtk_widget_set_usize(timeout, 64, -2);
259         gtk_tooltips_set_tip(tooltips, timeout,
260                              _
261                              ("Maximum time allowed for the spam check. After the time the check will be aborted and the message delivered as none spam."),
262                              NULL);
263         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(timeout), TRUE);
264         /*
265          * END GLADE CODE
266          */
267
268         config = spamassassin_get_config();
269
270         gtk_signal_connect(GTK_OBJECT(button4), "released", GTK_SIGNAL_FUNC(foldersel_cb), page);
271
272         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable), config->enable);
273         if (config->hostname != NULL)
274                 gtk_entry_set_text(GTK_ENTRY(hostname), config->hostname);
275         gtk_spin_button_set_value(GTK_SPIN_BUTTON(port), (float) config->port);
276         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(receive_spam), config->receive_spam);
277         if (config->save_folder != NULL)
278                 gtk_entry_set_text(GTK_ENTRY(save_folder), config->save_folder);
279         gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_size), (float) config->max_size);
280         gtk_spin_button_set_value(GTK_SPIN_BUTTON(timeout), (float) config->timeout);
281         
282         page->enable = enable;
283         page->hostname = hostname;
284         page->port = port;
285         page->receive_spam = receive_spam;
286         page->save_folder = save_folder;
287         page->max_size = max_size;
288         page->timeout = timeout;
289
290         page->page.widget = table1;
291 }
292
293 static void spamassassin_destroy_widget_func(PrefsPage *_page)
294 {
295         debug_print("Destroying SpamAssassin widget\n");
296 }
297
298 static void spamassassin_save_func(PrefsPage *_page)
299 {
300         struct SpamAssassinPage *page = (struct SpamAssassinPage *) _page;
301         SpamAssassinConfig *config;
302
303         debug_print("Saving SpamAssassin Page\n");
304
305         config = spamassassin_get_config();
306
307         /* enable */
308         config->enable = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->enable));
309
310         /* hostname */
311         g_free(config->hostname);
312         config->hostname = gtk_editable_get_chars(GTK_EDITABLE(page->hostname), 0, -1);
313
314         /* port */
315         config->port = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->port));
316
317         /* receive_spam */
318         config->receive_spam = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->receive_spam));
319
320         /* save_folder */
321         g_free(config->save_folder);
322         config->save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1);
323
324         /* max_size */
325         config->max_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->max_size));
326
327         /* timeout */
328         config->timeout = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->timeout));
329
330         spamassassin_save_config();
331 }
332
333 static struct SpamAssassinPage spamassassin_page;
334
335 gint plugin_init(gchar **error)
336 {
337         if ((sylpheed_get_version() > VERSION_NUMERIC)) {
338                 *error = g_strdup("Your sylpheed version is newer than the version the plugin was built with");
339                 return -1;
340         }
341
342         if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
343                 *error = g_strdup("Your sylpheed version is too old");
344                 return -1;
345         }
346
347         spamassassin_page.page.path = _("Filtering/SpamAssassin");
348         spamassassin_page.page.create_widget = spamassassin_create_widget_func;
349         spamassassin_page.page.destroy_widget = spamassassin_destroy_widget_func;
350         spamassassin_page.page.save_page = spamassassin_save_func;
351         spamassassin_page.page.weight = 35.0;
352
353         prefs_gtk_register_page((PrefsPage *) &spamassassin_page);
354
355         debug_print("SpamAssassin GTK plugin loaded\n");
356         return 0;       
357 }
358
359 void plugin_done(void)
360 {
361         prefs_gtk_unregister_page((PrefsPage *) &spamassassin_page);
362
363         debug_print("SpamAssassin GTK plugin unloaded\n");
364 }
365
366 const gchar *plugin_name(void)
367 {
368         return _("SpamAssassin GTK");
369 }
370
371 const gchar *plugin_desc(void)
372 {
373         return _("This plugin provides a Preferences page for the SpamAssassin "
374                  "plugin.\n"
375                  "\n"
376                  "You will find the options in the Other Preferences window "
377                  "under Filtering/SpamAssassin.\n"
378                  "\n"
379                  "With this plugin you can enable the filtering, change the "
380                  "SpamAssassin server host and port, set the maximum size of "
381                  "messages to be checked, (if the message is larger it will "
382                  "not be checked), configure whether spam mail should be received "
383                  "(default: Yes) and select the folder where spam mail will be "
384                  "saved.\n");
385 }
386
387 const gchar *plugin_type(void)
388 {
389         return "GTK";
390 }