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