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