2006-03-03 [colin] 2.0.0cvs104
[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-2006 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 *vbox1, *vbox2;
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         GtkTooltips *recv_infected_tip;
85         GtkWidget *save_folder;
86         GtkWidget *save_folder_select;
87         GtkTooltips *save_folder_tip;
88
89         vbox1 = gtk_vbox_new (FALSE, VSPACING);
90         gtk_widget_show (vbox1);
91         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
92
93         vbox2 = gtk_vbox_new (FALSE, 0);
94         gtk_widget_show (vbox2);
95         gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
96
97         PACK_CHECK_BUTTON (vbox2, enable_clamav, _("Enable virus scanning"));
98         PACK_CHECK_BUTTON (vbox2, enable_arc, _("Scan archive contents"));
99
100         hbox1 = gtk_hbox_new (FALSE, 8);
101         gtk_widget_show (hbox1);
102         gtk_box_pack_start (GTK_BOX (vbox2), hbox1, FALSE, FALSE, 0);
103         SET_TOGGLE_SENSITIVITY (enable_arc, hbox1);
104
105         label1 = gtk_label_new(_("Maximum attachment size"));
106         gtk_widget_show (label1);
107         gtk_box_pack_start (GTK_BOX (hbox1), label1, FALSE, FALSE, 0);
108
109         max_size_adj = gtk_adjustment_new (1, 1, 1024, 1, 10, 10);
110         max_size = gtk_spin_button_new (GTK_ADJUSTMENT (max_size_adj), 1, 0);
111         gtk_widget_show (max_size);
112         gtk_box_pack_start (GTK_BOX (hbox1), max_size, FALSE, FALSE, 0);
113         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (max_size), TRUE);
114
115         label2 = gtk_label_new(_("MB"));
116         gtk_widget_show (label2);
117         gtk_box_pack_start (GTK_BOX (hbox1), label2, FALSE, FALSE, 0);
118
119         recv_infected_tip = gtk_tooltips_new();
120         recv_infected = gtk_check_button_new_with_label(_("Save infected messages"));
121         gtk_widget_show (recv_infected);
122         gtk_box_pack_start (GTK_BOX (vbox2), recv_infected, FALSE, FALSE, 0);
123         gtk_tooltips_set_tip(recv_infected_tip, recv_infected,
124                              _("Save mails that contain viruses"),
125                              NULL);
126
127         hbox2 = gtk_hbox_new (FALSE, 8);
128         gtk_widget_show (hbox2);
129         gtk_box_pack_start (GTK_BOX (vbox2), hbox2, TRUE, TRUE, 0);
130         SET_TOGGLE_SENSITIVITY (recv_infected, hbox2);
131
132         label3 = gtk_label_new (_("Save folder"));
133         gtk_widget_show (label3);
134         gtk_box_pack_start (GTK_BOX (hbox2), label3, FALSE, FALSE, 0);
135
136         save_folder_tip = gtk_tooltips_new();
137         save_folder = gtk_entry_new ();
138         gtk_widget_show (save_folder);
139         gtk_box_pack_start (GTK_BOX (hbox2), save_folder, TRUE, TRUE, 0);
140         gtk_tooltips_set_tip(save_folder_tip, save_folder,
141                              _("Leave empty to use the default trash folder"),
142                              NULL);
143
144         save_folder_select = gtkut_get_browse_directory_btn(_("_Browse"));
145         gtk_widget_show (save_folder_select);
146         gtk_box_pack_start (GTK_BOX (hbox2), save_folder_select, FALSE, FALSE, 0);
147         gtk_tooltips_set_tip(save_folder_tip, save_folder_select,
148                              _("Leave empty to use the default trash folder"),
149                              NULL);
150
151         config = clamav_get_config();
152
153         g_signal_connect(G_OBJECT(save_folder_select), "released", 
154                          G_CALLBACK(foldersel_cb), page);
155
156         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_clamav), config->clamav_enable);
157         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_arc), config->clamav_enable_arc);
158         gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_size), (float) config->clamav_max_size);
159         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(recv_infected), config->clamav_recv_infected);
160         if (config->clamav_save_folder != NULL)
161                 gtk_entry_set_text(GTK_ENTRY(save_folder), config->clamav_save_folder);
162         
163         page->enable_clamav = enable_clamav;
164         page->enable_arc = enable_arc;
165         page->max_size = max_size;
166         page->recv_infected = recv_infected;
167         page->save_folder = save_folder;
168
169         page->page.widget = vbox1;
170 }
171
172 static void clamav_destroy_widget_func(PrefsPage *_page)
173 {
174         debug_print("Destroying ClamAV widget\n");
175 }
176
177 static void clamav_save_func(PrefsPage *_page)
178 {
179         struct ClamAvPage *page = (struct ClamAvPage *) _page;
180         ClamAvConfig *config;
181
182         debug_print("Saving ClamAV Page\n");
183
184         config = clamav_get_config();
185
186         config->clamav_enable = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->enable_clamav));
187         config->clamav_enable_arc = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->enable_arc));
188
189         config->clamav_max_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->max_size));
190         config->clamav_recv_infected = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->recv_infected));
191         g_free(config->clamav_save_folder);
192         config->clamav_save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1);
193
194         clamav_save_config();
195 }
196
197 static struct ClamAvPage clamav_page;
198
199 static void gtk_message_callback(gchar *message)
200 {
201         statusbar_print_all(message);
202 }
203
204 gint clamav_gtk_init(void)
205 {
206         static gchar *path[3];
207
208         path[0] = _("Plugins");
209         path[1] = _("Clam AntiVirus");
210         path[2] = NULL;
211
212         clamav_page.page.path = path;
213         clamav_page.page.create_widget = clamav_create_widget_func;
214         clamav_page.page.destroy_widget = clamav_destroy_widget_func;
215         clamav_page.page.save_page = clamav_save_func;
216         clamav_page.page.weight = 35.0;
217         
218         prefs_gtk_register_page((PrefsPage *) &clamav_page);
219         clamav_set_message_callback(gtk_message_callback);
220
221         debug_print("ClamAV GTK plugin loaded\n");
222         return 0;       
223 }
224
225 void clamav_gtk_done(void)
226 {
227         prefs_gtk_unregister_page((PrefsPage *) &clamav_page);
228 }