Allow custom window title for the folder selection dialog.
[claws.git] / src / plugins / bsfilter / bsfilter_gtk.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2009 Colin Leroy <colin@colino.net> and 
4  * the Claws Mail team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #include "claws-features.h"
24 #endif
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28
29 #include "defs.h"
30
31 #include <glib.h>
32 #include <gtk/gtk.h>
33 #include "gtk/gtkutils.h"
34
35 #include "common/claws.h"
36 #include "common/version.h"
37 #include "plugin.h"
38 #include "common/utils.h"
39 #include "prefs.h"
40 #include "folder.h"
41 #include "prefs_gtk.h"
42 #include "foldersel.h"
43 #include "statusbar.h"
44 #include "bsfilter.h"
45 #include "menu.h"
46 #include "addressbook.h"
47 #include "combobox.h"
48
49 struct BsfilterPage
50 {
51         PrefsPage page;
52
53         GtkWidget *process_emails;
54         GtkWidget *receive_spam;
55         GtkWidget *save_folder;
56         GtkWidget *save_folder_select;
57         GtkWidget *max_size;
58         GtkWidget *bspath;
59         GtkWidget *whitelist_ab;
60         GtkWidget *whitelist_ab_folder_combo;
61         GtkWidget *learn_from_whitelist_chkbtn;
62         GtkWidget *mark_as_read;
63 };
64
65 static void foldersel_cb(GtkWidget *widget, gpointer data)
66 {
67         GtkWidget *entry = (GtkWidget *) data;
68         FolderItem *item;
69         gchar *item_id;
70         gint newpos = 0;
71         
72         item = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL, FALSE, NULL);
73         if (item && (item_id = folder_item_get_identifier(item)) != NULL) {
74                 gtk_editable_delete_text(GTK_EDITABLE(entry), 0, -1);
75                 gtk_editable_insert_text(GTK_EDITABLE(entry), item_id, strlen(item_id), &newpos);
76                 g_free(item_id);
77         }
78 }
79
80 #ifndef USE_ALT_ADDRBOOK
81 static void bsfilter_whitelist_ab_select_cb(GtkWidget *widget, gpointer data)
82 {
83         struct BsfilterPage *page = (struct BsfilterPage *) data;
84         const gchar *folderpath = NULL;
85         gchar *new_path = NULL;
86
87         folderpath = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((page->whitelist_ab_folder_combo)))));
88         new_path = addressbook_folder_selection(folderpath);
89         if (new_path) {
90                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((page->whitelist_ab_folder_combo)))), new_path);
91                 g_free(new_path);
92         } 
93 }
94 #endif
95
96 static void bsfilter_create_widget_func(PrefsPage * _page,
97                                             GtkWindow * window,
98                                             gpointer data)
99 {
100         struct BsfilterPage *page = (struct BsfilterPage *) _page;
101         BsfilterConfig *config;
102
103         GtkWidget *vbox1, *vbox2;
104         GtkWidget *hbox_max_size;
105         GtkWidget *hbox_process_emails, *hbox_save_spam;
106         GtkWidget *hbox_bspath, *hbox_whitelist;
107         GtkWidget *hbox_mark_as_read;
108
109         GtkWidget *max_size_label;
110         GtkObject *max_size_spinbtn_adj;
111         GtkWidget *max_size_spinbtn;
112         GtkWidget *max_size_kb_label;
113
114         GtkWidget *process_emails_checkbtn;
115
116         GtkWidget *save_spam_checkbtn;
117         GtkWidget *save_spam_folder_entry;
118         GtkWidget *save_spam_folder_select;
119
120         GtkWidget *whitelist_ab_checkbtn;
121         GtkWidget *learn_from_whitelist_chkbtn;
122         GtkWidget *bspath_label;
123         GtkWidget *bspath_entry;
124
125         GtkWidget *mark_as_read_checkbtn;
126
127         GtkWidget *whitelist_ab_folder_combo;
128         GtkWidget *whitelist_ab_select_btn;
129
130         vbox1 = gtk_vbox_new (FALSE, VSPACING);
131         gtk_widget_show (vbox1);
132         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
133
134         vbox2 = gtk_vbox_new (FALSE, 4);
135         gtk_widget_show (vbox2);
136         gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
137
138         hbox_process_emails = gtk_hbox_new(FALSE, 8);
139         gtk_widget_show(hbox_process_emails);
140         gtk_box_pack_start (GTK_BOX (vbox2), hbox_process_emails, TRUE, TRUE, 0);
141
142         process_emails_checkbtn = gtk_check_button_new_with_label(
143                         _("Process messages on receiving"));
144         gtk_widget_show(process_emails_checkbtn);
145         gtk_box_pack_start(GTK_BOX(hbox_process_emails), process_emails_checkbtn, TRUE, TRUE, 0);
146
147         hbox_max_size = gtk_hbox_new(FALSE, 8);
148         gtk_widget_show(hbox_max_size);
149         gtk_box_pack_start (GTK_BOX (vbox2), hbox_max_size, TRUE, TRUE, 0);
150
151         max_size_label = gtk_label_new(_("Maximum size"));
152         gtk_widget_show(max_size_label);
153         gtk_box_pack_start(GTK_BOX(hbox_max_size), max_size_label, FALSE, FALSE, 0);
154
155         max_size_spinbtn_adj = gtk_adjustment_new(250, 0, 10000, 10, 10, 0);
156         max_size_spinbtn = gtk_spin_button_new(GTK_ADJUSTMENT(max_size_spinbtn_adj), 1, 0);
157         gtk_widget_show(max_size_spinbtn);
158         gtk_box_pack_start(GTK_BOX(hbox_max_size), max_size_spinbtn, FALSE, FALSE, 0);
159         CLAWS_SET_TIP(max_size_spinbtn,
160                         _("Messages larger than this will not be checked"));
161         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(max_size_spinbtn), TRUE);
162
163         max_size_kb_label = gtk_label_new(_("KB"));
164         gtk_widget_show(max_size_kb_label);
165         gtk_box_pack_start(GTK_BOX(hbox_max_size), max_size_kb_label, FALSE, FALSE, 0);
166
167         hbox_save_spam = gtk_hbox_new(FALSE, 8);
168         gtk_widget_show(hbox_save_spam);
169         gtk_box_pack_start (GTK_BOX (vbox2), hbox_save_spam, TRUE, TRUE, 0);
170
171         save_spam_checkbtn = gtk_check_button_new_with_label(_("Save spam in"));
172         gtk_widget_show(save_spam_checkbtn);
173         gtk_box_pack_start(GTK_BOX(hbox_save_spam), save_spam_checkbtn, FALSE, FALSE, 0);
174
175         save_spam_folder_entry = gtk_entry_new();
176         gtk_widget_show (save_spam_folder_entry);
177         gtk_box_pack_start (GTK_BOX (hbox_save_spam), save_spam_folder_entry, TRUE, TRUE, 0);
178         CLAWS_SET_TIP(save_spam_folder_entry,
179                         _("Folder for storing identified spam. Leave empty to use the trash folder."));
180
181         save_spam_folder_select = gtkut_get_browse_directory_btn(_("_Browse"));
182         gtk_widget_show (save_spam_folder_select);
183         gtk_box_pack_start (GTK_BOX (hbox_save_spam), save_spam_folder_select, FALSE, FALSE, 0);
184         CLAWS_SET_TIP(save_spam_folder_select,
185                         _("Click this button to select a folder for storing spam"));
186
187         hbox_whitelist = gtk_hbox_new(FALSE, 8);
188         gtk_widget_show(hbox_whitelist);
189         gtk_box_pack_start (GTK_BOX (vbox2), hbox_whitelist, TRUE, TRUE, 0);
190
191         whitelist_ab_checkbtn = gtk_check_button_new_with_label(_("Whitelist senders found in address book/folder"));
192         gtk_widget_show(whitelist_ab_checkbtn);
193         gtk_box_pack_start(GTK_BOX(hbox_whitelist), whitelist_ab_checkbtn, FALSE, FALSE, 0);
194         CLAWS_SET_TIP(whitelist_ab_checkbtn,
195                         _("Messages coming from your address book contacts will be received in the normal folder even if detected as spam"));
196
197         whitelist_ab_folder_combo = combobox_text_new(TRUE, _("Any"), NULL);
198         gtk_widget_set_size_request(whitelist_ab_folder_combo, 100, -1);
199         gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_folder_combo, TRUE, TRUE, 0);
200
201         whitelist_ab_select_btn = gtk_button_new_with_label(_("Select..."));
202         gtk_widget_show (whitelist_ab_select_btn);
203         gtk_box_pack_start (GTK_BOX (hbox_whitelist), whitelist_ab_select_btn, FALSE, FALSE, 0);
204         CLAWS_SET_TIP(whitelist_ab_select_btn,
205                         _("Click this button to select a book or folder in the address book"));
206
207         learn_from_whitelist_chkbtn = gtk_check_button_new_with_label(_("Learn whitelisted emails as ham"));
208         CLAWS_SET_TIP(learn_from_whitelist_chkbtn,
209                         _("If Bsfilter thought an email was spam or unsure, but it was whitelisted, "
210                           "learn it as ham."));
211         gtk_widget_show(learn_from_whitelist_chkbtn);
212         gtk_box_pack_start (GTK_BOX (vbox2), learn_from_whitelist_chkbtn, TRUE, TRUE, 0);
213
214         hbox_bspath = gtk_hbox_new(FALSE, 8);
215         gtk_widget_show(hbox_bspath);
216         gtk_box_pack_start (GTK_BOX (vbox2), hbox_bspath, FALSE, FALSE, 0);
217
218         bspath_label = gtk_label_new(_("Bsfilter call"));
219         gtk_widget_show(bspath_label);
220         gtk_box_pack_start(GTK_BOX(hbox_bspath), bspath_label, FALSE, FALSE, 0);
221
222         bspath_entry = gtk_entry_new();
223         gtk_widget_show(bspath_entry);
224         gtk_box_pack_start(GTK_BOX(hbox_bspath), bspath_entry, FALSE, FALSE, 0);
225         CLAWS_SET_TIP(bspath_entry,
226                         _("Path to bsfilter executable"));
227
228         hbox_mark_as_read = gtk_hbox_new(FALSE, 8);
229         gtk_widget_show(hbox_mark_as_read);
230         gtk_box_pack_start (GTK_BOX (vbox2), hbox_mark_as_read, TRUE, TRUE, 0);
231
232         mark_as_read_checkbtn = gtk_check_button_new_with_label(_("Mark spam as read"));
233         gtk_widget_show(mark_as_read_checkbtn);
234         gtk_box_pack_start(GTK_BOX(hbox_mark_as_read), mark_as_read_checkbtn, FALSE, FALSE, 0);
235
236         SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, save_spam_folder_entry);
237         SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, save_spam_folder_select);
238         SET_TOGGLE_SENSITIVITY(whitelist_ab_checkbtn, whitelist_ab_folder_combo);
239         SET_TOGGLE_SENSITIVITY(whitelist_ab_checkbtn, whitelist_ab_select_btn);
240         SET_TOGGLE_SENSITIVITY(whitelist_ab_checkbtn, learn_from_whitelist_chkbtn);
241         SET_TOGGLE_SENSITIVITY(save_spam_checkbtn, mark_as_read_checkbtn);
242
243         config = bsfilter_get_config();
244
245         g_signal_connect(G_OBJECT(save_spam_folder_select), "clicked",
246                         G_CALLBACK(foldersel_cb), save_spam_folder_entry);
247 #ifndef USE_ALT_ADDRBOOK
248         g_signal_connect(G_OBJECT (whitelist_ab_select_btn), "clicked",
249                          G_CALLBACK(bsfilter_whitelist_ab_select_cb), page);
250 #else
251         gtk_widget_set_sensitive(whitelist_ab_select_btn, FALSE);
252 #endif
253
254         gtk_spin_button_set_value(GTK_SPIN_BUTTON(max_size_spinbtn), (float) config->max_size);
255         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(process_emails_checkbtn), config->process_emails);
256         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(save_spam_checkbtn), config->receive_spam);
257         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(whitelist_ab_checkbtn), config->whitelist_ab);
258         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(learn_from_whitelist_chkbtn), config->learn_from_whitelist);
259         if (config->whitelist_ab_folder != NULL) {
260                 /* translate "Any" (stored UNtranslated) */
261                 if (strcasecmp(config->whitelist_ab_folder, "Any") == 0)
262                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
263                                         config->whitelist_ab_folder);
264                 else
265                 /* backward compatibility (when translated "Any" was stored) */
266                 if (g_utf8_collate(config->whitelist_ab_folder, _("Any")) == 0)
267                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
268                                         config->whitelist_ab_folder);
269                 else
270                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((whitelist_ab_folder_combo)))),
271                                         config->whitelist_ab_folder);
272         }
273         if (config->save_folder != NULL)
274                 gtk_entry_set_text(GTK_ENTRY(save_spam_folder_entry), config->save_folder);
275         if (config->bspath != NULL)
276                 gtk_entry_set_text(GTK_ENTRY(bspath_entry), config->bspath);
277         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mark_as_read_checkbtn), config->mark_as_read);
278
279         page->max_size = max_size_spinbtn;
280         page->process_emails = process_emails_checkbtn;
281
282         page->receive_spam = save_spam_checkbtn;
283         page->save_folder = save_spam_folder_entry;
284         page->save_folder_select = save_spam_folder_select;
285
286         page->whitelist_ab = whitelist_ab_checkbtn;
287         page->whitelist_ab_folder_combo = whitelist_ab_folder_combo;
288         page->learn_from_whitelist_chkbtn = learn_from_whitelist_chkbtn;
289         page->bspath = bspath_entry;
290
291         page->mark_as_read = mark_as_read_checkbtn;
292
293         page->page.widget = vbox1;
294 }
295
296 static void bsfilter_destroy_widget_func(PrefsPage *_page)
297 {
298         debug_print("Destroying Bsfilter widget\n");
299 }
300
301 static void bsfilter_save_func(PrefsPage *_page)
302 {
303         struct BsfilterPage *page = (struct BsfilterPage *) _page;
304         BsfilterConfig *config;
305
306         debug_print("Saving Bsfilter Page\n");
307
308         config = bsfilter_get_config();
309
310         /* process_emails */
311         config->process_emails = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->process_emails));
312
313         /* receive_spam */
314         config->receive_spam = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->receive_spam));
315
316         /* save_folder */
317         g_free(config->save_folder);
318         config->save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1);
319
320         /* whitelist_ab */
321         config->whitelist_ab = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->whitelist_ab));
322         g_free(config->whitelist_ab_folder);
323         config->whitelist_ab_folder = gtk_editable_get_chars(
324                                 GTK_EDITABLE(gtk_bin_get_child(GTK_BIN((page->whitelist_ab_folder_combo)))), 0, -1);
325         /* store UNtranslated "Any" */
326         if (g_utf8_collate(config->whitelist_ab_folder, _("Any")) == 0) {
327                 g_free(config->whitelist_ab_folder);
328                 config->whitelist_ab_folder = g_strdup("Any");
329         }
330
331         /* learn_from_whitelist_chkbtn */
332         config->learn_from_whitelist = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->learn_from_whitelist_chkbtn));
333
334         /* bspath */
335         g_free(config->bspath);
336         config->bspath = gtk_editable_get_chars(GTK_EDITABLE(page->bspath), 0, -1);
337
338         /* max_size */
339         config->max_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->max_size));
340
341         /* mark_as_read */
342         config->mark_as_read = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->mark_as_read));
343
344         if (config->process_emails) {
345                 bsfilter_register_hook();
346         } else {
347                 bsfilter_unregister_hook();
348         }
349
350         procmsg_register_spam_learner(bsfilter_learn);
351         procmsg_spam_set_folder(config->save_folder, bsfilter_get_spam_folder);
352
353         bsfilter_save_config();
354 }
355
356 typedef struct _BsCbData {
357         gchar *message;
358         gint total;
359         gint done;
360 } BsCbData;
361
362 static gboolean gtk_message_callback(gpointer data)
363 {
364         BsCbData *cbdata = (BsCbData *)data;
365
366         if (cbdata->message)
367                 statusbar_print_all("%s", cbdata->message);
368         else if (cbdata->total == 0) {
369                 statusbar_pop_all();
370         }
371         if (cbdata->total && cbdata->done)
372                 statusbar_progress_all(cbdata->done, cbdata->total, 10);
373         else
374                 statusbar_progress_all(0,0,0);
375         g_free(cbdata->message);
376         g_free(cbdata);
377         GTK_EVENTS_FLUSH();
378         return FALSE;
379 }
380
381 static void gtk_safe_message_callback(gchar *message, gint total, gint done, gboolean thread_safe)
382 {
383         BsCbData *cbdata = g_new0(BsCbData, 1);
384         if (message)
385                 cbdata->message = g_strdup(message);
386         cbdata->total = total;
387         cbdata->done = done;
388         if (thread_safe)
389                 g_timeout_add(0, gtk_message_callback, cbdata);
390         else
391                 gtk_message_callback(cbdata);
392 }
393
394 static struct BsfilterPage bsfilter_page;
395
396 gint bsfilter_gtk_init(void)
397 {
398         static gchar *path[3];
399
400         path[0] = _("Plugins");
401         path[1] = _("Bsfilter");
402         path[2] = NULL;
403
404         bsfilter_page.page.path = path;
405         bsfilter_page.page.create_widget = bsfilter_create_widget_func;
406         bsfilter_page.page.destroy_widget = bsfilter_destroy_widget_func;
407         bsfilter_page.page.save_page = bsfilter_save_func;
408         bsfilter_page.page.weight = 35.0;
409
410         prefs_gtk_register_page((PrefsPage *) &bsfilter_page);
411         bsfilter_set_message_callback(gtk_safe_message_callback);
412
413         debug_print("Bsfilter GTK plugin loaded\n");
414         return 0;       
415 }
416
417 void bsfilter_gtk_done(void)
418 {
419         prefs_gtk_unregister_page((PrefsPage *) &bsfilter_page);
420 }