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