651d91de4f77ca09b6c63eda4a44ec800ab7d127
[claws.git] / src / plugins / spamassassin / spamassassin_gtk.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 "prefswindow.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 {
67         struct SpamAssassinPage *page = (struct SpamAssassinPage *) _page;
68
69         /* ------------------ code made by glade -------------------- */
70         GtkWidget *table1;
71         GtkWidget *label3;
72         GtkWidget *label4;
73         GtkWidget *label6;
74         GtkWidget *label8;
75         GtkWidget *label9;
76         GtkWidget *hbox1;
77         GtkWidget *hostname;
78         GtkWidget *label5;
79         GtkObject *port_adj;
80         GtkWidget *port;
81         GtkWidget *enable;
82         GtkWidget *receive_spam;
83         GtkWidget *label10;
84         GtkObject *max_size_adj;
85         GtkWidget *max_size;
86         GtkWidget *save_folder;
87         GtkWidget *button4;
88         GtkWidget *label11;
89
90         table1 = gtk_table_new(6, 3, FALSE);
91         gtk_widget_show(table1);
92         gtk_table_set_row_spacings(GTK_TABLE(table1), 4);
93         gtk_table_set_col_spacings(GTK_TABLE(table1), 8);
94
95         label3 = gtk_label_new(_("Enable SpamAssassin Filtering"));
96         gtk_widget_show(label3);
97         gtk_table_attach(GTK_TABLE(table1), label3, 0, 1, 0, 1,
98                          (GtkAttachOptions) (GTK_FILL),
99                          (GtkAttachOptions) (0), 0, 0);
100         gtk_misc_set_alignment(GTK_MISC(label3), 0, 0.5);
101
102         label4 = gtk_label_new(_("SpamAssassin Server (spamd)"));
103         gtk_widget_show(label4);
104         gtk_table_attach(GTK_TABLE(table1), label4, 0, 1, 1, 2,
105                          (GtkAttachOptions) (GTK_FILL),
106                          (GtkAttachOptions) (0), 0, 0);
107         gtk_misc_set_alignment(GTK_MISC(label4), 0, 0.5);
108
109         label6 = gtk_label_new(_("Maximum Message Size"));
110         gtk_widget_show(label6);
111         gtk_table_attach(GTK_TABLE(table1), label6, 0, 1, 2, 3,
112                          (GtkAttachOptions) (GTK_FILL),
113                          (GtkAttachOptions) (0), 0, 0);
114         gtk_misc_set_alignment(GTK_MISC(label6), 0, 0.5);
115
116         label8 = gtk_label_new(_("Folder for saved Spam"));
117         gtk_widget_show(label8);
118         gtk_table_attach(GTK_TABLE(table1), label8, 0, 1, 4, 5,
119                          (GtkAttachOptions) (GTK_FILL),
120                          (GtkAttachOptions) (0), 0, 0);
121         gtk_label_set_justify(GTK_LABEL(label8), GTK_JUSTIFY_LEFT);
122         gtk_misc_set_alignment(GTK_MISC(label8), 0, 0.5);
123
124         label9 = gtk_label_new(_("Receive Spam"));
125         gtk_widget_show(label9);
126         gtk_table_attach(GTK_TABLE(table1), label9, 0, 1, 3, 4,
127                          (GtkAttachOptions) (GTK_FILL),
128                          (GtkAttachOptions) (0), 0, 0);
129         gtk_misc_set_alignment(GTK_MISC(label9), 0, 0.5);
130
131         hbox1 = gtk_hbox_new(FALSE, 0);
132         gtk_widget_show(hbox1);
133         gtk_table_attach(GTK_TABLE(table1), hbox1, 1, 2, 1, 2,
134                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
135                          (GtkAttachOptions) (GTK_FILL), 0, 0);
136
137         hostname = gtk_entry_new();
138         gtk_widget_show(hostname);
139         gtk_box_pack_start(GTK_BOX(hbox1), hostname, TRUE, TRUE, 0);
140
141         label5 = gtk_label_new(_(":"));
142         gtk_widget_show(label5);
143         gtk_box_pack_start(GTK_BOX(hbox1), label5, FALSE, FALSE, 0);
144         gtk_misc_set_padding(GTK_MISC(label5), 8, 0);
145
146         port_adj = gtk_adjustment_new(783, 1, 65535, 1, 10, 10);
147         port =
148             gtk_spin_button_new(GTK_ADJUSTMENT(port_adj), 1, 0);
149         gtk_widget_show(port);
150         gtk_box_pack_start(GTK_BOX(hbox1), port, FALSE, TRUE, 0);
151         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(port), TRUE);
152
153         enable = gtk_check_button_new_with_label("");
154         gtk_widget_show(enable);
155         gtk_table_attach(GTK_TABLE(table1), enable, 1, 2, 0, 1,
156                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
157                          (GtkAttachOptions) (0), 0, 0);
158
159         receive_spam = gtk_check_button_new_with_label("");
160         gtk_widget_show(receive_spam);
161         gtk_table_attach(GTK_TABLE(table1), receive_spam, 1, 2, 3, 4,
162                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
163                          (GtkAttachOptions) (0), 0, 0);
164
165         label10 =
166             gtk_label_new(_
167                           ("Leave empty to use the default trash folder"));
168         gtk_widget_show(label10);
169         gtk_table_attach(GTK_TABLE(table1), label10, 1, 2, 5, 6,
170                          (GtkAttachOptions) (GTK_FILL),
171                          (GtkAttachOptions) (0), 0, 0);
172         gtk_label_set_justify(GTK_LABEL(label10), GTK_JUSTIFY_RIGHT);
173         gtk_misc_set_alignment(GTK_MISC(label10), 1, 0.5);
174
175         max_size_adj = gtk_adjustment_new(250, 0, 10000, 10, 10, 10);
176         max_size = gtk_spin_button_new(GTK_ADJUSTMENT(max_size_adj), 1, 0);
177         gtk_widget_show(max_size);
178         gtk_table_attach(GTK_TABLE(table1), max_size, 1, 2, 2, 3,
179                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
180                          (GtkAttachOptions) (0), 0, 0);
181
182         save_folder = gtk_entry_new();
183         gtk_widget_show(save_folder);
184         gtk_table_attach(GTK_TABLE(table1), save_folder, 1, 2, 4, 5,
185                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
186                          (GtkAttachOptions) (0), 0, 0);
187
188         button4 = gtk_button_new_with_label(_("..."));
189         gtk_widget_show(button4);
190         gtk_table_attach(GTK_TABLE(table1), button4, 2, 3, 4, 5,
191                          (GtkAttachOptions) (GTK_SHRINK | GTK_FILL),
192                          (GtkAttachOptions) (0), 0, 0);
193
194         label11 = gtk_label_new(_("kB"));
195         gtk_widget_show(label11);
196         gtk_table_attach(GTK_TABLE(table1), label11, 2, 3, 2, 3,
197                          (GtkAttachOptions) (GTK_FILL),
198                          (GtkAttachOptions) (0), 0, 0);
199         gtk_misc_set_alignment(GTK_MISC(label11), 0, 0.5);
200         /* --------------------------------------------------------- */
201
202         gtk_widget_set_usize(GTK_WIDGET(port), 64, -1);
203         gtk_signal_connect(GTK_OBJECT(button4), "released", GTK_SIGNAL_FUNC(foldersel_cb), page);
204
205         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable), spamassassin_enable);
206         gtk_entry_set_text(GTK_ENTRY(hostname), spamassassin_hostname);
207         gtk_spin_button_set_value(GTK_SPIN_BUTTON(port), (float) spamassassin_port);
208         gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_size), (float) spamassassin_max_size);
209         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(receive_spam), spamassassin_receive_spam);
210         gtk_entry_set_text(GTK_ENTRY(save_folder), spamassassin_save_folder);
211         
212         page->enable = enable;
213         page->hostname = hostname;
214         page->port = port;
215         page->max_size = max_size;
216         page->receive_spam = receive_spam;
217         page->save_folder = save_folder;
218
219         page->page.widget = table1;
220 }
221
222 static void spamassassin_destroy_widget_func(PrefsPage *_page)
223 {
224         debug_print("Destroying SpamAssassin widget\n");
225 }
226
227 static void spamassassin_save_func(PrefsPage *_page)
228 {
229         struct SpamAssassinPage *page = (struct SpamAssassinPage *) _page;
230         gchar *tmp;
231
232         debug_print("Saving SpamAssassin Page\n");
233
234         spamassassin_enable = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->enable));
235         if (spamassassin_hostname)
236                 g_free(spamassassin_hostname);
237         spamassassin_hostname = gtk_editable_get_chars(GTK_EDITABLE(page->hostname), 0, -1);
238         tmp = gtk_editable_get_chars(GTK_EDITABLE(page->port), 0, -1);
239         spamassassin_port = atoi(tmp);
240         g_free(tmp);
241         spamassassin_max_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->max_size));
242         spamassassin_receive_spam = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->receive_spam));
243         if (spamassassin_save_folder)
244                 g_free(spamassassin_save_folder);
245         spamassassin_save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1);
246
247         spamassassin_save_config();
248 }
249
250 static void spamassassin_destroy_func(PrefsPage *_page)
251 {
252         debug_print("Destroying SpamAssassin Page\n");
253         g_free(_page);
254 }
255
256 gint plugin_init(gchar **error)
257 {
258         struct SpamAssassinPage *page;
259
260         page = g_new0(struct SpamAssassinPage, 1);
261         page->page.path = _("Filtering/SpamAssassin");
262         page->page.create_widget = spamassassin_create_widget_func;
263         page->page.destroy_widget = spamassassin_destroy_widget_func;
264         page->page.save_page = spamassassin_save_func;
265         page->page.destroy_page = spamassassin_destroy_func;
266         prefswindow_register_new_page((PrefsPage *) page);
267
268         debug_print("SpamAssassin GTK plugin loaded\n");
269
270         return 0;       
271 }
272
273 void plugin_done()
274 {
275         debug_print("SpamAssassin GTK plugin unloaded\n");
276 }
277
278 const gchar *plugin_name()
279 {
280         return "SpamAssassin GTK";
281 }
282
283 const gchar *plugin_desc()
284 {
285         return "";
286 }