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