2005-12-02 [paul] 1.9.100cvs55
[claws.git] / src / plugins / clamav / clamav_plugin_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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <gtk/gtkutils.h>
30
31 #include "common/sylpheed.h"
32 #include "common/version.h"
33 #include "plugin.h"
34 #include "utils.h"
35 #include "prefs.h"
36 #include "folder.h"
37 #include "prefs_gtk.h"
38 #include "foldersel.h"
39 #include "clamav_plugin.h"
40 #include "statusbar.h"
41
42 struct ClamAvPage
43 {
44         PrefsPage page;
45         
46         GtkWidget *enable_clamav;
47         GtkWidget *enable_arc;
48         GtkWidget *max_size;
49         GtkWidget *recv_infected;
50         GtkWidget *save_folder;
51 };
52
53 static void foldersel_cb(GtkWidget *widget, gpointer data)
54 {
55         struct ClamAvPage *page = (struct ClamAvPage *) data;
56         FolderItem *item;
57         gchar *item_id;
58         gint newpos = 0;
59         
60         item = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL);
61         if (item && (item_id = folder_item_get_identifier(item)) != NULL) {
62                 gtk_editable_delete_text(GTK_EDITABLE(page->save_folder), 0, -1);
63                 gtk_editable_insert_text(GTK_EDITABLE(page->save_folder), item_id, strlen(item_id), &newpos);
64                 g_free(item_id);
65         }
66 }
67
68 static void clamav_create_widget_func(PrefsPage * _page, GtkWindow *window, gpointer data)
69 {
70         struct ClamAvPage *page = (struct ClamAvPage *) _page;
71         ClamAvConfig *config;
72          
73         GtkWidget *table;
74         GtkWidget *enable_clamav;
75         GtkWidget *label1;
76         GtkWidget *enable_arc;
77         GtkWidget *label2;
78         GtkWidget *label3;
79         GtkObject *max_size_adj;
80         GtkWidget *max_size;
81         GtkWidget *hbox1;
82         GtkWidget *hbox2;
83         GtkWidget *recv_infected;
84         GtkWidget *save_folder;
85         GtkWidget *save_folder_select;
86         GtkTooltips *save_folder_tip;
87
88         table = gtk_table_new (8, 3, FALSE);
89         gtk_widget_show(table);
90         gtk_container_set_border_width(GTK_CONTAINER(table), VBOX_BORDER);
91         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
92         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
93
94         enable_clamav = gtk_check_button_new_with_label(_("Enable virus scanning"));
95         gtk_widget_show (enable_clamav);
96         gtk_table_attach (GTK_TABLE (table), enable_clamav, 0, 1, 0, 1,
97                           (GtkAttachOptions) (GTK_FILL),
98                           (GtkAttachOptions) (0), 0, 0);
99
100         enable_arc = gtk_check_button_new_with_label(_("Scan archive contents"));
101         gtk_widget_show (enable_arc);
102         gtk_table_attach (GTK_TABLE (table), enable_arc, 0, 1, 1, 2,
103                           (GtkAttachOptions) (GTK_FILL),
104                           (GtkAttachOptions) (0), 0, 0);
105
106         hbox1 = gtk_hbox_new (FALSE, 8);
107         gtk_widget_show (hbox1);
108         gtk_table_attach (GTK_TABLE (table), hbox1, 0, 1, 2, 3,
109                           (GtkAttachOptions) (GTK_FILL),
110                           (GtkAttachOptions) (GTK_FILL), 0, 0);
111         SET_TOGGLE_SENSITIVITY (enable_arc, hbox1);
112
113         label1 = gtk_label_new(_("Maximum attachment size"));
114         gtk_widget_show (label1);
115         gtk_box_pack_start (GTK_BOX (hbox1), label1, FALSE, FALSE, 0);
116
117         max_size_adj = gtk_adjustment_new (1, 1, 1024, 1, 10, 10);
118         max_size = gtk_spin_button_new (GTK_ADJUSTMENT (max_size_adj), 1, 0);
119         gtk_widget_show (max_size);
120         gtk_box_pack_start (GTK_BOX (hbox1), max_size, FALSE, FALSE, 0);
121         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (max_size), TRUE);
122
123         label2 = gtk_label_new(_("MB"));
124         gtk_widget_show (label2);
125         gtk_box_pack_start (GTK_BOX (hbox1), label2, FALSE, FALSE, 0);
126
127         recv_infected = gtk_check_button_new_with_label(_("Save infected messages"));
128         gtk_widget_show (recv_infected);
129         gtk_table_attach (GTK_TABLE (table), recv_infected, 0, 1, 4, 5,
130                           (GtkAttachOptions) (GTK_FILL),
131                           (GtkAttachOptions) (0), 0, 0);
132
133         hbox2 = gtk_hbox_new (FALSE, 8);
134         gtk_widget_show (hbox2);
135         gtk_table_attach (GTK_TABLE (table), hbox2, 0, 1, 5, 6,
136                           (GtkAttachOptions) (GTK_FILL),
137                           (GtkAttachOptions) (GTK_FILL), 0, 0);
138         SET_TOGGLE_SENSITIVITY (recv_infected, hbox2);
139
140         label3 = gtk_label_new (_("Save folder"));
141         gtk_widget_show (label3);
142         gtk_box_pack_start (GTK_BOX (hbox2), label3, FALSE, FALSE, 0);
143
144         save_folder_tip = gtk_tooltips_new();
145         save_folder = gtk_entry_new ();
146         gtk_widget_show (save_folder);
147         gtk_box_pack_start (GTK_BOX (hbox2), save_folder, FALSE, FALSE, 0);
148         gtk_tooltips_set_tip(save_folder_tip, save_folder,
149                              _("Leave empty to use the default trash folder"),
150                              NULL);
151
152         save_folder_select = gtkut_get_browse_directory_btn(_("_Browse"));
153         gtk_widget_show (save_folder_select);
154         gtk_box_pack_start (GTK_BOX (hbox2), save_folder_select, FALSE, FALSE, 0);
155         gtk_tooltips_set_tip(save_folder_tip, save_folder_select,
156                              _("Leave empty to use the default trash folder"),
157                              NULL);
158
159         config = clamav_get_config();
160
161         g_signal_connect(G_OBJECT(save_folder_select), "released", 
162                          G_CALLBACK(foldersel_cb), page);
163
164         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_clamav), config->clamav_enable);
165         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_arc), config->clamav_enable_arc);
166         gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_size), (float) config->clamav_max_size);
167         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(recv_infected), config->clamav_recv_infected);
168         if (config->clamav_save_folder != NULL)
169                 gtk_entry_set_text(GTK_ENTRY(save_folder), config->clamav_save_folder);
170         
171         page->enable_clamav = enable_clamav;
172         page->enable_arc = enable_arc;
173         page->max_size = max_size;
174         page->recv_infected = recv_infected;
175         page->save_folder = save_folder;
176
177         page->page.widget = table;
178 }
179
180 static void clamav_destroy_widget_func(PrefsPage *_page)
181 {
182         debug_print("Destroying ClamAV widget\n");
183 }
184
185 static void clamav_save_func(PrefsPage *_page)
186 {
187         struct ClamAvPage *page = (struct ClamAvPage *) _page;
188         ClamAvConfig *config;
189
190         debug_print("Saving ClamAV Page\n");
191
192         config = clamav_get_config();
193
194         config->clamav_enable = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->enable_clamav));
195         config->clamav_enable_arc = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->enable_arc));
196
197         config->clamav_max_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->max_size));
198         config->clamav_recv_infected = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->recv_infected));
199         g_free(config->clamav_save_folder);
200         config->clamav_save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1);
201
202         clamav_save_config();
203 }
204
205 static struct ClamAvPage clamav_page;
206
207 static void gtk_message_callback(gchar *message)
208 {
209         statusbar_print_all(message);
210 }
211
212 gint clamav_gtk_init(void)
213 {
214         static gchar *path[3];
215
216         path[0] = _("Plugins");
217         path[1] = _("Clam AntiVirus");
218         path[2] = NULL;
219
220         clamav_page.page.path = path;
221         clamav_page.page.create_widget = clamav_create_widget_func;
222         clamav_page.page.destroy_widget = clamav_destroy_widget_func;
223         clamav_page.page.save_page = clamav_save_func;
224         clamav_page.page.weight = 35.0;
225         
226         prefs_gtk_register_page((PrefsPage *) &clamav_page);
227         clamav_set_message_callback(gtk_message_callback);
228
229         debug_print("ClamAV GTK plugin loaded\n");
230         return 0;       
231 }
232
233 void clamav_gtk_done(void)
234 {
235         prefs_gtk_unregister_page((PrefsPage *) &clamav_page);
236 }