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