0.8.5claws179
[claws.git] / src / prefs_filtering.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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 #include <gtk/gtkoptionmenu.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #include "intl.h"
36 #include "main.h"
37 #include "prefs.h"
38 #include "prefs_matcher.h"
39 #include "prefs_filtering.h"
40 #include "prefs_common.h"
41 #include "mainwindow.h"
42 #include "foldersel.h"
43 #include "manage_window.h"
44 #include "inc.h"
45 #include "utils.h"
46 #include "gtkutils.h"
47 #include "alertpanel.h"
48 #include "folder.h"
49 #include "filtering.h"
50 #include "addr_compl.h"
51 #include "colorlabel.h"
52
53 #include "matcher_parser.h"
54 #include "matcher.h"
55
56 static struct Filtering {
57         GtkWidget *window;
58
59         GtkWidget *ok_btn;
60         GtkWidget *cond_entry;
61         GtkWidget *action_list;
62         GtkWidget *action_combo;
63         GtkWidget *account_list;
64         GtkWidget *account_combo;
65         GtkWidget *dest_entry;
66         GtkWidget *dest_btn;
67         GtkWidget *dest_label;
68         GtkWidget *exec_label;
69         GtkWidget *exec_btn;
70
71         GtkWidget *color_label;
72         GtkWidget *color_optmenu;
73
74         GtkWidget *cond_clist;
75
76         /* need this to make address completion entry work */
77         gint current_action;
78 } filtering;
79
80 /* widget creating functions */
81 static void prefs_filtering_create              (void);
82
83 static void prefs_filtering_set_dialog  (const gchar *header,
84                                          const gchar *key);
85 static void prefs_filtering_set_list    (void);
86
87 /* callback functions */
88 /* static void prefs_filtering_select_dest_cb   (void); */
89 static void prefs_filtering_register_cb (void);
90 static void prefs_filtering_substitute_cb       (void);
91 static void prefs_filtering_delete_cb   (void);
92 static void prefs_filtering_up          (void);
93 static void prefs_filtering_down                (void);
94 static void prefs_filtering_select              (GtkCList       *clist,
95                                          gint            row,
96                                          gint            column,
97                                          GdkEvent       *event);
98
99 static gint prefs_filtering_deleted     (GtkWidget      *widget,
100                                          GdkEventAny    *event,
101                                          gpointer        data);
102 static void prefs_filtering_key_pressed (GtkWidget      *widget,
103                                          GdkEventKey    *event,
104                                          gpointer        data);
105 static void prefs_filtering_cancel              (void);
106 static void prefs_filtering_ok          (void);
107
108 static void prefs_filtering_condition_define    (void);
109 static gint prefs_filtering_clist_set_row(gint row, FilteringProp * prop);
110 static void prefs_filtering_select_dest(void);
111 static void prefs_filtering_action_select(GtkList *list,
112                                           GtkWidget *widget, 
113                                           gpointer user_data);
114 static void prefs_filtering_action_selection_changed(GtkList *list,
115                                                      gpointer user_data);
116                                           
117 static void prefs_filtering_reset_dialog(void);
118 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data);
119 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data);
120
121 static FolderItem * cur_item = NULL;
122
123 enum {
124         ACTION_MOVE = 0,
125         ACTION_COPY = 1,
126         ACTION_DELETE = 2,
127         ACTION_MARK = 3,
128         ACTION_UNMARK = 4,
129         ACTION_MARK_AS_READ = 5,
130         ACTION_MARK_AS_UNREAD = 6,
131         ACTION_FORWARD = 7,
132         ACTION_FORWARD_AS_ATTACHMENT = 8,
133         ACTION_REDIRECT = 9,
134         ACTION_EXECUTE = 10,
135         ACTION_COLOR = 11,
136         ACTION_DELETE_ON_SERVER = 12,
137 };
138
139 static gint get_sel_from_list(GtkList * list)
140 {
141         gint row = 0;
142         void * sel;
143         GList * child;
144
145         if (list->selection == NULL)
146                 return -1;
147
148         sel = list->selection->data;
149         for(child = list->children ; child != NULL ;
150             child = g_list_next(child)) {
151                 if (child->data == sel)
152                         return row;
153                 row ++;
154         }
155         
156         return row;
157 }
158
159 static gint get_account_id_from_list_id(gint list_id)
160 {
161         GList * accounts;
162
163         for (accounts = account_get_list() ; accounts != NULL;
164              accounts = accounts->next) {
165                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
166
167                 if (list_id == 0)
168                         return ac->account_id;
169                 list_id--;
170         }
171         return 0;
172 }
173
174 static gint get_list_id_from_account_id(gint account_id)
175 {
176         GList * accounts;
177         gint list_id = 0;
178
179         for (accounts = account_get_list() ; accounts != NULL;
180              accounts = accounts->next) {
181                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
182
183                 if (account_id == ac->account_id)
184                         return list_id;
185                 list_id++;
186         }
187         return 0;
188 }
189
190 static gint prefs_filtering_get_matching_from_action(gint action_id)
191 {
192         switch(action_id) {
193         case ACTION_MOVE:
194                 return MATCHACTION_MOVE;
195         case ACTION_COPY:
196                 return MATCHACTION_COPY;
197         case ACTION_DELETE:
198                 return MATCHACTION_DELETE;
199         case ACTION_MARK:
200                 return MATCHACTION_MARK;
201         case ACTION_UNMARK:
202                 return MATCHACTION_UNMARK;
203         case ACTION_MARK_AS_READ:
204                 return MATCHACTION_MARK_AS_READ;
205         case ACTION_MARK_AS_UNREAD:
206                 return MATCHACTION_MARK_AS_UNREAD;
207         case ACTION_FORWARD:
208                 return MATCHACTION_FORWARD;
209         case ACTION_FORWARD_AS_ATTACHMENT:
210                 return MATCHACTION_FORWARD_AS_ATTACHMENT;
211         case ACTION_REDIRECT:
212                 return MATCHACTION_REDIRECT;
213         case ACTION_EXECUTE:
214                 return MATCHACTION_EXECUTE;
215         case ACTION_COLOR:
216                 return MATCHACTION_COLOR;
217         case ACTION_DELETE_ON_SERVER:
218                 return MATCHACTION_DELETE_ON_SERVER;
219         default:
220                 return -1;
221         }
222 }
223
224 static gchar * action_text [] = {
225         N_("Move"),     
226         N_("Copy"), 
227         N_("Delete"),
228         N_("Mark"), 
229         N_("Unmark"), 
230         N_("Mark as read"), 
231         N_("Mark as unread"),
232         N_("Forward"), 
233         N_("Forward as attachment"), 
234         N_("Redirect"), 
235         N_("Execute"),
236         N_("Color"),
237         N_("Delete on Server")
238 };
239
240 void prefs_filtering_open(FolderItem * item,
241                           const gchar *header,
242                           const gchar *key)
243 {
244         gchar *esckey;
245
246         if (prefs_rc_is_readonly(FILTERING_RC))
247                 return;
248
249         inc_lock();
250
251         if (!filtering.window) {
252                 prefs_filtering_create();
253         }
254
255         manage_window_set_transient(GTK_WINDOW(filtering.window));
256         gtk_widget_grab_focus(filtering.ok_btn);
257
258         cur_item = item;
259
260         esckey = matcher_escape_str(key);
261         prefs_filtering_set_dialog(header, esckey);
262         g_free(esckey);
263
264         gtk_widget_show(filtering.window);
265
266         start_address_completion();
267 }
268
269 /* prefs_filtering_close() - just to have one common exit point */
270 static void prefs_filtering_close(void)
271 {
272         end_address_completion();
273         
274         gtk_widget_hide(filtering.window);
275         inc_unlock();
276 }
277
278 static void prefs_filtering_create(void)
279 {
280         GtkWidget *window;
281         GtkWidget *vbox;
282         GtkWidget *ok_btn;
283         GtkWidget *cancel_btn;
284         GtkWidget *confirm_area;
285
286         GtkWidget *vbox1;
287         GtkWidget *hbox1;
288         GtkWidget *reg_hbox;
289         GtkWidget *arrow;
290         GtkWidget *btn_hbox;
291
292         GtkWidget *cond_label;
293         GtkWidget *cond_entry;
294         GtkWidget *cond_btn;
295         GtkWidget *action_label;
296         GtkWidget *action_list;
297         GtkWidget *action_combo;
298         GtkWidget *account_label;
299         GtkWidget *account_list;
300         GtkWidget *account_combo;
301         GtkWidget *dest_label;
302         GtkWidget *exec_label;
303         GtkWidget *dest_entry;
304         GtkWidget *dest_btn;
305         GtkWidget *exec_btn;
306         GtkWidget *color_label;
307         GtkWidget *color_optmenu;
308
309         GtkWidget *reg_btn;
310         GtkWidget *subst_btn;
311         GtkWidget *del_btn;
312
313         GtkWidget *cond_hbox;
314         GtkWidget *cond_scrolledwin;
315         GtkWidget *cond_clist;
316
317         GtkWidget *btn_vbox;
318         GtkWidget *up_btn;
319         GtkWidget *down_btn;
320
321
322         GList *combo_items;
323         gint i;
324
325         GList *accounts;
326         GList * cur;
327
328         gchar *title[1];
329
330         debug_print("Creating filtering setting window...\n");
331
332         window = gtk_window_new (GTK_WINDOW_DIALOG);
333         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
334         gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
335         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
336         gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, FALSE);
337
338         vbox = gtk_vbox_new (FALSE, 6);
339         gtk_widget_show (vbox);
340         gtk_container_add (GTK_CONTAINER (window), vbox);
341
342         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
343                                 &cancel_btn, _("Cancel"), NULL, NULL);
344         gtk_widget_show (confirm_area);
345         gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
346         gtk_widget_grab_default (ok_btn);
347
348         gtk_window_set_title (GTK_WINDOW(window),
349                               _("Filtering setting"));
350         gtk_signal_connect (GTK_OBJECT(window), "delete_event",
351                             GTK_SIGNAL_FUNC(prefs_filtering_deleted), NULL);
352         gtk_signal_connect (GTK_OBJECT(window), "key_press_event",
353                             GTK_SIGNAL_FUNC(prefs_filtering_key_pressed), NULL);
354         MANAGE_WINDOW_SIGNALS_CONNECT (window);
355         gtk_signal_connect (GTK_OBJECT(ok_btn), "clicked",
356                             GTK_SIGNAL_FUNC(prefs_filtering_ok), NULL);
357         gtk_signal_connect (GTK_OBJECT(cancel_btn), "clicked",
358                             GTK_SIGNAL_FUNC(prefs_filtering_cancel), NULL);
359
360         vbox1 = gtk_vbox_new (FALSE, VSPACING);
361         gtk_widget_show (vbox1);
362         gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
363         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
364
365         cond_label = gtk_label_new (_("Condition"));
366         gtk_widget_show (cond_label);
367         gtk_misc_set_alignment (GTK_MISC (cond_label), 0, 0.5);
368         gtk_box_pack_start (GTK_BOX (vbox1), cond_label, FALSE, FALSE, 0);
369
370         hbox1 = gtk_hbox_new (FALSE, VSPACING);
371         gtk_widget_show (vbox1);
372         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
373         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
374
375         cond_entry = gtk_entry_new ();
376         gtk_widget_show (cond_entry);
377         gtk_box_pack_start (GTK_BOX (hbox1), cond_entry, TRUE, TRUE, 0);
378
379         cond_btn = gtk_button_new_with_label (_("Define ..."));
380         gtk_widget_show (cond_btn);
381         gtk_box_pack_start (GTK_BOX (hbox1), cond_btn, FALSE, FALSE, 0);
382         gtk_signal_connect (GTK_OBJECT (cond_btn), "clicked",
383                             GTK_SIGNAL_FUNC (prefs_filtering_condition_define),
384                             NULL);
385
386         hbox1 = gtk_hbox_new (FALSE, VSPACING);
387         gtk_widget_show (vbox1);
388         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
389         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
390
391         action_label = gtk_label_new (_("Action"));
392         gtk_widget_show (action_label);
393         gtk_misc_set_alignment (GTK_MISC (action_label), 0, 0.5);
394         gtk_box_pack_start (GTK_BOX (hbox1), action_label, FALSE, FALSE, 0);
395
396         action_combo = gtk_combo_new ();
397         gtk_widget_show (action_combo);
398         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(action_combo)->entry),
399                                FALSE);
400
401         combo_items = NULL;
402
403         for(i = 0 ; i < (gint) (sizeof(action_text) / sizeof(gchar *)) ;
404             i++) {
405                 combo_items = g_list_append(combo_items,
406                                             (gpointer) _(action_text[i]));
407         }
408         gtk_combo_set_popdown_strings(GTK_COMBO(action_combo), combo_items);
409
410         g_list_free(combo_items);
411
412         gtk_box_pack_start (GTK_BOX (hbox1), action_combo,
413                             TRUE, TRUE, 0);
414         action_list = GTK_COMBO(action_combo)->list;
415         gtk_signal_connect (GTK_OBJECT (action_list), "select-child",
416                             GTK_SIGNAL_FUNC (prefs_filtering_action_select),
417                             NULL);
418
419         gtk_signal_connect(GTK_OBJECT(action_list), "selection-changed",
420                            GTK_SIGNAL_FUNC(prefs_filtering_action_selection_changed),
421                            NULL);
422
423         /* accounts */
424
425         hbox1 = gtk_hbox_new (FALSE, VSPACING);
426         gtk_widget_show (vbox1);
427         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
428         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
429
430         account_label = gtk_label_new (_("Account"));
431         gtk_widget_show (account_label);
432         gtk_misc_set_alignment (GTK_MISC (account_label), 0, 0.5);
433         gtk_box_pack_start (GTK_BOX (hbox1), account_label, FALSE, FALSE, 0);
434
435         account_combo = gtk_combo_new ();
436         gtk_widget_show (account_combo);
437
438         combo_items = NULL;
439         for (accounts = account_get_list() ; accounts != NULL;
440              accounts = accounts->next) {
441                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
442                 gchar *name;
443
444                 name = g_strdup_printf("%s <%s> (%s)",
445                                        ac->name, ac->address,
446                                        ac->account_name);
447                 combo_items = g_list_append(combo_items, (gpointer) name);
448         }
449
450         gtk_combo_set_popdown_strings(GTK_COMBO(account_combo), combo_items);
451
452         for(cur = g_list_first(combo_items) ; cur != NULL ;
453             cur = g_list_next(cur))
454                 g_free(cur->data);
455         g_list_free(combo_items);
456
457         gtk_box_pack_start (GTK_BOX (hbox1), account_combo,
458                             TRUE, TRUE, 0);
459         account_list = GTK_COMBO(account_combo)->list;
460         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(account_combo)->entry),
461                                FALSE);
462
463         /* destination */
464
465         hbox1 = gtk_hbox_new (FALSE, VSPACING);
466         gtk_widget_show (vbox1);
467         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
468         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
469
470         dest_label = gtk_label_new (_("Destination"));
471         gtk_widget_show (dest_label);
472         gtk_misc_set_alignment (GTK_MISC (dest_label), 0, 0.5);
473         gtk_box_pack_start (GTK_BOX (hbox1), dest_label, FALSE, FALSE, 0);
474
475         exec_label = gtk_label_new (_("Execute"));
476         gtk_widget_show (exec_label);
477         gtk_misc_set_alignment (GTK_MISC (exec_label), 0, 0.5);
478         gtk_box_pack_start (GTK_BOX (hbox1), exec_label, FALSE, FALSE, 0);
479         
480         color_label = gtk_label_new (_("Color"));
481         gtk_widget_show(color_label);
482         gtk_misc_set_alignment(GTK_MISC(color_label), 0, 0.5);
483         gtk_box_pack_start(GTK_BOX(hbox1), color_label, FALSE, FALSE, 0);
484
485         dest_entry = gtk_entry_new ();
486         gtk_widget_show (dest_entry);
487         gtk_box_pack_start (GTK_BOX (hbox1), dest_entry, TRUE, TRUE, 0);
488         
489         color_optmenu = gtk_option_menu_new();
490         gtk_option_menu_set_menu(GTK_OPTION_MENU(color_optmenu),
491                                  colorlabel_create_color_menu());
492         gtk_box_pack_start(GTK_BOX(hbox1), color_optmenu, TRUE, TRUE, 0);
493
494         dest_btn = gtk_button_new_with_label (_("Select ..."));
495         gtk_widget_show (dest_btn);
496         gtk_box_pack_start (GTK_BOX (hbox1), dest_btn, FALSE, FALSE, 0);
497         gtk_signal_connect (GTK_OBJECT (dest_btn), "clicked",
498                             GTK_SIGNAL_FUNC (prefs_filtering_select_dest),
499                             NULL);
500
501         exec_btn = gtk_button_new_with_label (_("Info ..."));
502         gtk_widget_show (exec_btn);
503         gtk_box_pack_start (GTK_BOX (hbox1), exec_btn, FALSE, FALSE, 0);
504         gtk_signal_connect (GTK_OBJECT (exec_btn), "clicked",
505                             GTK_SIGNAL_FUNC (prefs_matcher_exec_info),
506                             NULL);
507
508         /* register / substitute / delete */
509
510         reg_hbox = gtk_hbox_new (FALSE, 4);
511         gtk_widget_show (reg_hbox);
512         gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
513
514         arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
515         gtk_widget_show (arrow);
516         gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
517         gtk_widget_set_usize (arrow, -1, 16);
518
519         btn_hbox = gtk_hbox_new (TRUE, 4);
520         gtk_widget_show (btn_hbox);
521         gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
522
523         reg_btn = gtk_button_new_with_label (_("Register"));
524         gtk_widget_show (reg_btn);
525         gtk_box_pack_start (GTK_BOX (btn_hbox), reg_btn, FALSE, TRUE, 0);
526         gtk_signal_connect (GTK_OBJECT (reg_btn), "clicked",
527                             GTK_SIGNAL_FUNC (prefs_filtering_register_cb), NULL);
528
529         subst_btn = gtk_button_new_with_label (_(" Substitute "));
530         gtk_widget_show (subst_btn);
531         gtk_box_pack_start (GTK_BOX (btn_hbox), subst_btn, FALSE, TRUE, 0);
532         gtk_signal_connect (GTK_OBJECT (subst_btn), "clicked",
533                             GTK_SIGNAL_FUNC (prefs_filtering_substitute_cb),
534                             NULL);
535
536         del_btn = gtk_button_new_with_label (_("Delete"));
537         gtk_widget_show (del_btn);
538         gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
539         gtk_signal_connect (GTK_OBJECT (del_btn), "clicked",
540                             GTK_SIGNAL_FUNC (prefs_filtering_delete_cb), NULL);
541
542         cond_hbox = gtk_hbox_new (FALSE, 8);
543         gtk_widget_show (cond_hbox);
544         gtk_box_pack_start (GTK_BOX (vbox1), cond_hbox, TRUE, TRUE, 0);
545
546         cond_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
547         gtk_widget_show (cond_scrolledwin);
548         gtk_widget_set_usize (cond_scrolledwin, -1, 150);
549         gtk_box_pack_start (GTK_BOX (cond_hbox), cond_scrolledwin,
550                             TRUE, TRUE, 0);
551         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin),
552                                         GTK_POLICY_AUTOMATIC,
553                                         GTK_POLICY_AUTOMATIC);
554
555         title[0] = _("Registered rules");
556         cond_clist = gtk_clist_new_with_titles(1, title);
557         gtk_widget_show (cond_clist);
558         gtk_container_add (GTK_CONTAINER (cond_scrolledwin), cond_clist);
559         gtk_clist_set_column_width (GTK_CLIST (cond_clist), 0, 80);
560         gtk_clist_set_selection_mode (GTK_CLIST (cond_clist),
561                                       GTK_SELECTION_BROWSE);
562         GTK_WIDGET_UNSET_FLAGS (GTK_CLIST (cond_clist)->column[0].button,
563                                 GTK_CAN_FOCUS);
564         gtk_signal_connect (GTK_OBJECT (cond_clist), "select_row",
565                             GTK_SIGNAL_FUNC (prefs_filtering_select), NULL);
566
567         btn_vbox = gtk_vbox_new (FALSE, 8);
568         gtk_widget_show (btn_vbox);
569         gtk_box_pack_start (GTK_BOX (cond_hbox), btn_vbox, FALSE, FALSE, 0);
570
571         up_btn = gtk_button_new_with_label (_("Up"));
572         gtk_widget_show (up_btn);
573         gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
574         gtk_signal_connect (GTK_OBJECT (up_btn), "clicked",
575                             GTK_SIGNAL_FUNC (prefs_filtering_up), NULL);
576
577         down_btn = gtk_button_new_with_label (_("Down"));
578         gtk_widget_show (down_btn);
579         gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
580         gtk_signal_connect (GTK_OBJECT (down_btn), "clicked",
581                             GTK_SIGNAL_FUNC (prefs_filtering_down), NULL);
582
583         gtk_widget_set_usize(window, 500, -1);
584
585         gtk_widget_show_all(window);
586
587         filtering.window    = window;
588         filtering.ok_btn = ok_btn;
589
590         filtering.cond_entry = cond_entry;
591         filtering.action_list = action_list;
592         filtering.action_combo = action_combo;
593         filtering.account_list = account_list;
594         filtering.account_combo = account_combo;
595         filtering.dest_entry = dest_entry;
596         filtering.dest_btn = dest_btn;
597         filtering.dest_label = dest_label;
598         filtering.exec_label = exec_label;
599         filtering.exec_btn = exec_btn;
600
601         filtering.cond_clist   = cond_clist;
602
603         filtering.color_label   = color_label;
604         filtering.color_optmenu = color_optmenu;
605 }
606
607 static void prefs_filtering_update_hscrollbar(void)
608 {
609         gint optwidth = gtk_clist_optimal_column_width(GTK_CLIST(filtering.cond_clist), 0);
610         gtk_clist_set_column_width(GTK_CLIST(filtering.cond_clist), 0, optwidth);
611 }
612
613 void prefs_filtering_rename_path(const gchar *old_path, const gchar *new_path)
614 {
615         GList * cur;
616         gchar *paths[2] = {NULL, NULL};
617         paths[0] = (gchar*)old_path;
618         paths[1] = (gchar*)new_path;
619         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
620                 Folder *folder;
621                 folder = (Folder *) cur->data;
622                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
623                                 prefs_filtering_rename_path_func, paths);
624         }
625         prefs_filtering_rename_path_func(NULL, paths);
626 }
627
628 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
629 {
630         GSList *cur;
631         gchar *old_path, *new_path;
632         gchar *base;
633         gchar *prefix;
634         gchar *suffix;
635         gchar *dest_path;
636         gchar *old_path_with_sep;
637         gint destlen;
638         gint prefixlen;
639         gint oldpathlen;
640         FolderItem *item;
641
642         old_path = ((gchar **)data)[0];
643         new_path = ((gchar **)data)[1];
644
645         g_return_val_if_fail(old_path != NULL, FALSE);
646         g_return_val_if_fail(new_path != NULL, FALSE);
647
648         oldpathlen = strlen(old_path);
649         old_path_with_sep = g_strconcat(old_path,G_DIR_SEPARATOR_S,NULL);
650         if (node == NULL)
651                 cur = global_processing;
652         else {
653                 item = node->data;
654                 if (!item || !item->prefs) 
655                         return FALSE;
656                 cur = item->prefs->processing;
657         }
658
659         for (; cur != NULL; cur = cur->next) {
660                 FilteringProp   *filtering = (FilteringProp *)cur->data;
661                 FilteringAction *action = filtering->action;
662
663                 if (!action->destination) continue;
664
665                 destlen = strlen(action->destination);
666
667                 if (destlen > oldpathlen) {
668                         prefixlen = destlen - oldpathlen;
669                         suffix = action->destination + prefixlen;
670
671                         if (!strncmp(old_path, suffix, oldpathlen)) {
672                                 prefix = g_malloc0(prefixlen + 1);
673                                 strncpy2(prefix, action->destination, prefixlen);
674
675                                 base = suffix + oldpathlen;
676                                 while (*base == G_DIR_SEPARATOR) base++;
677                                 if (*base == '\0')
678                                         dest_path = g_strconcat(prefix,
679                                                                 G_DIR_SEPARATOR_S,
680                                                                 new_path, NULL);
681                                 else
682                                         dest_path = g_strconcat(prefix,
683                                                                 G_DIR_SEPARATOR_S,
684                                                                 new_path,
685                                                                 G_DIR_SEPARATOR_S,
686                                                                 base, NULL);
687
688                                 g_free(prefix);
689                                 g_free(action->destination);
690                                 action->destination = dest_path;
691                         } else { /* for non-leaf folders */
692                                 /* compare with trailing slash */
693                                 if (!strncmp(old_path_with_sep, action->destination, oldpathlen+1)) {
694                                         
695                                         suffix = action->destination + oldpathlen + 1;
696                                         dest_path = g_strconcat(new_path,
697                                                                 G_DIR_SEPARATOR_S,
698                                                                 suffix, NULL);
699                                         g_free(action->destination);
700                                         action->destination = dest_path;
701                                 }
702                         }
703                 } else {
704                         /* folder-moving a leaf */
705                         if (!strcmp(old_path, action->destination)) {           
706                                 dest_path = g_strdup(new_path);
707                                 g_free(action->destination);
708                                 action->destination = dest_path;
709                         }
710                 }
711         }
712         g_free(old_path_with_sep);
713         prefs_matcher_write_config();
714
715         return FALSE;
716 }
717
718 void prefs_filtering_delete_path(const gchar *path)
719 {
720         GList * cur;
721         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
722                 Folder *folder;
723                 folder = (Folder *) cur->data;
724                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
725                                 prefs_filtering_delete_path_func, (gchar *)path);
726         }
727         prefs_filtering_delete_path_func(NULL, (gchar *)path);
728 }
729
730 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data)
731 {
732         GSList *cur, *orig;
733         GSList *next;
734         gchar *path = (gchar *)data;
735         gchar *suffix;
736         gint destlen;
737         gint prefixlen;
738         gint pathlen;
739         FolderItem *item;
740         
741         g_return_val_if_fail(path != NULL, FALSE);
742
743         pathlen = strlen(path);
744         if (node == NULL)
745                 cur = global_processing;
746         else {
747                 item = node->data;
748                 if (!item || !item->prefs)
749                         return FALSE;
750                 cur = item->prefs->processing;
751         }
752         orig = cur;
753         
754         for (; cur != NULL; cur = cur->next) {
755                 FilteringProp *filtering = (FilteringProp *)cur->data;
756                 FilteringAction *action;
757                 if (!cur->data)
758                         break;
759                 
760                 action = filtering->action;
761                 next = cur->next;
762
763                 if (!action->destination) continue;
764
765                 destlen = strlen(action->destination);
766
767                 if (destlen > pathlen) {
768                         prefixlen = destlen - pathlen;
769                         suffix = action->destination + prefixlen;
770
771                         if (suffix && !strncmp(path, suffix, pathlen)) {
772                                 filteringprop_free(filtering);
773                                 global_processing = 
774                                         g_slist_remove(global_processing, filtering);
775                         }
776                 } else if (strcmp(action->destination, path) == 0) {
777                         filteringprop_free(filtering);
778                         orig = 
779                                 g_slist_remove(orig, filtering);
780
781                 }
782         }
783
784         prefs_matcher_write_config();
785
786         return FALSE;
787 }
788
789 static void prefs_filtering_set_dialog(const gchar *header, const gchar *key)
790 {
791         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
792         GSList *cur;
793         GSList * prefs_filtering;
794         gchar *cond_str[1];
795         gint row;
796         
797         gtk_clist_freeze(clist);
798         gtk_clist_clear(clist);
799
800         cond_str[0] = _("(New)");
801         row = gtk_clist_append(clist, cond_str);
802         gtk_clist_set_row_data(clist, row, NULL);
803
804         if (cur_item == NULL)
805                 prefs_filtering = global_processing;
806         else
807                 prefs_filtering = cur_item->prefs->processing;
808
809         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur)) {
810                 FilteringProp * prop = (FilteringProp *) cur->data;
811
812                 cond_str[0] = filteringprop_to_string(prop);
813                 subst_char(cond_str[0], '\t', ':');
814                 row = gtk_clist_append(clist, cond_str);
815                 gtk_clist_set_row_data(clist, row, prop);
816
817                 g_free(cond_str[0]);
818         }
819
820         prefs_filtering_update_hscrollbar();
821         gtk_clist_thaw(clist);
822
823         prefs_filtering_reset_dialog();
824
825         if (header && key) {
826                 gchar *match_str = g_strconcat(header, " ",
827                         get_matchparser_tab_str(MATCHTYPE_MATCHCASE),
828                         " \"", key, "\"", NULL);
829                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), match_str);
830                 g_free(match_str);
831         }
832 }
833
834 static void prefs_filtering_reset_dialog(void)
835 {
836         gtk_list_select_item(GTK_LIST(filtering.action_list), 0);
837         gtk_list_select_item(GTK_LIST(filtering.account_list), 0);
838         gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), "");
839         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), "");
840 }
841
842 static void prefs_filtering_set_list(void)
843 {
844         gint row = 1;
845         FilteringProp *prop;
846         GSList * cur;
847         gchar * filtering_str;
848         GSList * prefs_filtering;
849
850         if (cur_item == NULL)
851                 prefs_filtering = global_processing;
852         else
853                 prefs_filtering = cur_item->prefs->processing;
854
855         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
856                 filteringprop_free((FilteringProp *) cur->data);
857         g_slist_free(prefs_filtering);
858         prefs_filtering = NULL;
859
860         while (gtk_clist_get_text(GTK_CLIST(filtering.cond_clist),
861                                   row, 0, &filtering_str)) {
862                 if (strcmp(filtering_str, _("(New)")) != 0) {
863                         /* tmp = filtering_str; */
864                         prop = matcher_parser_get_filtering(filtering_str);
865                         if (prop != NULL)
866                                 prefs_filtering =
867                                         g_slist_append(prefs_filtering, prop);
868                 }
869                 row++;
870         }
871
872         if (cur_item == NULL)
873                 global_processing = prefs_filtering;
874         else
875                 cur_item->prefs->processing = prefs_filtering;
876 }
877
878 static gint prefs_filtering_clist_set_row(gint row, FilteringProp * prop)
879 {
880         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
881         gchar * str;
882         gchar *cond_str[1];
883
884         if (prop == NULL) {
885                 cond_str[0] = _("(New)");
886                 return gtk_clist_append(clist, cond_str);
887         }
888
889         str = filteringprop_to_string(prop);
890         if (str == NULL) {
891                 return -1;
892         }
893         cond_str[0] = str;
894
895         if (row < 0)
896                 row = gtk_clist_append(clist, cond_str);
897         else
898                 gtk_clist_set_text(clist, row, 0, cond_str[0]);
899         g_free(str);
900
901         return row;
902 }
903
904 static void prefs_filtering_condition_define_done(MatcherList * matchers)
905 {
906         gchar * str;
907
908         if (matchers == NULL)
909                 return;
910
911         str = matcherlist_to_string(matchers);
912
913         if (str != NULL) {
914                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), str);
915                 g_free(str);
916         }
917 }
918
919 static void prefs_filtering_condition_define(void)
920 {
921         gchar * cond_str;
922         MatcherList * matchers = NULL;
923
924         cond_str = gtk_entry_get_text(GTK_ENTRY(filtering.cond_entry));
925
926         if (*cond_str != '\0') {
927                 matchers = matcher_parser_get_cond(cond_str);
928                 if (matchers == NULL)
929                         alertpanel_error(_("Match string is not valid."));
930         }
931
932         prefs_matcher_open(matchers, prefs_filtering_condition_define_done);
933
934         if (matchers != NULL)
935                 matcherlist_free(matchers);
936 }
937
938
939 /* register / substitute delete buttons */
940
941
942 static FilteringProp * prefs_filtering_dialog_to_filtering(void)
943 {
944         MatcherList * cond;
945         gchar * cond_str;
946         FilteringProp * prop;
947         FilteringAction * action;
948         gint list_id;
949         gint action_id;
950         gint action_type;
951         gint account_id;
952         gchar * destination;
953         gint labelcolor = 0;
954         
955         cond_str = gtk_entry_get_text(GTK_ENTRY(filtering.cond_entry));
956         if (*cond_str == '\0') {
957                 alertpanel_error(_("Score is not set."));
958                 return NULL;
959         }
960
961         action_id = get_sel_from_list(GTK_LIST(filtering.action_list));
962         action_type = prefs_filtering_get_matching_from_action(action_id);
963         list_id = get_sel_from_list(GTK_LIST(filtering.account_list));
964         account_id = get_account_id_from_list_id(list_id);
965
966         switch (action_id) {
967         case ACTION_MOVE:
968         case ACTION_COPY:
969         case ACTION_FORWARD:
970         case ACTION_FORWARD_AS_ATTACHMENT:
971         case ACTION_REDIRECT:
972         case ACTION_EXECUTE:
973                 destination = gtk_entry_get_text(GTK_ENTRY(filtering.dest_entry));
974                 if (*destination == '\0') {
975                         alertpanel_error(_("Destination is not set."));
976                         return NULL;
977                 }
978                 break;
979         case ACTION_COLOR:
980                 labelcolor = colorlabel_get_color_menu_active_item(
981                         gtk_option_menu_get_menu(GTK_OPTION_MENU(filtering.color_optmenu)));
982                 destination = NULL;     
983                 break;
984         case ACTION_DELETE_ON_SERVER:
985                 destination = NULL;
986                 break;
987         default:
988                 destination = NULL;
989                 break;
990         }
991         
992         action = filteringaction_new(action_type, account_id, destination, labelcolor);
993
994         cond = matcher_parser_get_cond(cond_str);
995
996         if (cond == NULL) {
997                 alertpanel_error(_("Match string is not valid."));
998                 filteringaction_free(action);
999                 return NULL;
1000         }
1001
1002         prop = filteringprop_new(cond, action);
1003
1004         return prop;
1005 }
1006
1007 static void prefs_filtering_register_cb(void)
1008 {
1009         FilteringProp * prop;
1010         
1011         prop = prefs_filtering_dialog_to_filtering();
1012         if (prop == NULL)
1013                 return;
1014         prefs_filtering_clist_set_row(-1, prop);
1015
1016         filteringprop_free(prop);
1017         
1018         prefs_filtering_update_hscrollbar();
1019 }
1020
1021 static void prefs_filtering_substitute_cb(void)
1022 {
1023         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
1024         gint row;
1025         FilteringProp * prop;
1026         
1027         if (!clist->selection) return;
1028
1029         row = GPOINTER_TO_INT(clist->selection->data);
1030         if (row == 0) return;
1031
1032         prop = prefs_filtering_dialog_to_filtering();
1033         if (prop == NULL)
1034                 return;
1035         prefs_filtering_clist_set_row(row, prop);
1036
1037         filteringprop_free(prop);
1038         
1039         prefs_filtering_update_hscrollbar();
1040 }
1041
1042 static void prefs_filtering_delete_cb(void)
1043 {
1044         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
1045         gint row;
1046
1047         if (!clist->selection) return;
1048         row = GPOINTER_TO_INT(clist->selection->data);
1049         if (row == 0) return;
1050
1051         if (alertpanel(_("Delete rule"),
1052                        _("Do you really want to delete this rule?"),
1053                        _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
1054                 return;
1055
1056         gtk_clist_remove(clist, row);
1057
1058         prefs_filtering_update_hscrollbar();
1059 }
1060
1061 static void prefs_filtering_up(void)
1062 {
1063         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
1064         gint row;
1065
1066         if (!clist->selection) return;
1067
1068         row = GPOINTER_TO_INT(clist->selection->data);
1069         if (row > 1) {
1070                 gtk_clist_row_move(clist, row, row - 1);
1071                 if(gtk_clist_row_is_visible(clist, row - 1) != GTK_VISIBILITY_FULL) {
1072                         gtk_clist_moveto(clist, row - 1, 0, 0, 0);
1073                 } 
1074         }
1075 }
1076
1077 static void prefs_filtering_down(void)
1078 {
1079         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
1080         gint row;
1081
1082         if (!clist->selection) return;
1083
1084         row = GPOINTER_TO_INT(clist->selection->data);
1085         if (row > 0 && row < clist->rows - 1) {
1086                 gtk_clist_row_move(clist, row, row + 1);
1087                 if(gtk_clist_row_is_visible(clist, row + 1) != GTK_VISIBILITY_FULL) {
1088                         gtk_clist_moveto(clist, row + 1, 0, 1, 0);
1089                 } 
1090         }
1091 }
1092
1093 static void prefs_filtering_select_set(FilteringProp * prop)
1094 {
1095         FilteringAction * action;
1096         gchar * matcher_str;
1097         gint list_id;
1098
1099         prefs_filtering_reset_dialog();
1100
1101         action = prop->action;
1102
1103         matcher_str = matcherlist_to_string(prop->matchers);
1104         if (matcher_str == NULL) {
1105                 filteringprop_free(prop);
1106                 return;
1107         }
1108
1109         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), matcher_str);
1110         
1111         if (action->destination)
1112                 gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), action->destination);
1113         else
1114                 gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), "");
1115
1116         switch(action->type) {
1117         case MATCHACTION_MOVE:
1118                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1119                                      ACTION_MOVE);
1120                 break;
1121         case MATCHACTION_COPY:
1122                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1123                                      ACTION_COPY);
1124                 break;
1125         case MATCHACTION_DELETE:
1126                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1127                                      ACTION_DELETE);
1128                 break;
1129         case MATCHACTION_MARK:
1130                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1131                                      ACTION_MARK);
1132                 break;
1133         case MATCHACTION_UNMARK:
1134                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1135                                      ACTION_UNMARK);
1136                 break;
1137         case MATCHACTION_MARK_AS_READ:
1138                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1139                                      ACTION_MARK_AS_READ);
1140                 break;
1141         case MATCHACTION_MARK_AS_UNREAD:
1142                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1143                                      ACTION_MARK_AS_UNREAD);
1144                 break;
1145         case MATCHACTION_FORWARD:
1146                 list_id = get_list_id_from_account_id(action->account_id);
1147                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1148                                      ACTION_FORWARD);
1149                 gtk_list_select_item(GTK_LIST(filtering.account_list),
1150                                      list_id);
1151                 break;
1152         case MATCHACTION_FORWARD_AS_ATTACHMENT:
1153                 list_id = get_list_id_from_account_id(action->account_id);
1154                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1155                                      ACTION_FORWARD_AS_ATTACHMENT);
1156                 gtk_list_select_item(GTK_LIST(filtering.account_list),
1157                                      list_id);
1158                 break;
1159         case MATCHACTION_REDIRECT:
1160                 list_id = get_list_id_from_account_id(action->account_id);
1161                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1162                                      ACTION_REDIRECT);
1163                 gtk_list_select_item(GTK_LIST(filtering.account_list),
1164                                      list_id);
1165                 break;
1166         case MATCHACTION_EXECUTE:
1167                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1168                                      ACTION_EXECUTE);
1169                 break;
1170         case MATCHACTION_COLOR:
1171                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1172                                      ACTION_COLOR);
1173                 gtk_option_menu_set_history(GTK_OPTION_MENU(filtering.color_optmenu), action->labelcolor);     
1174                 break;
1175         case MATCHACTION_DELETE_ON_SERVER:
1176                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1177                                      ACTION_DELETE_ON_SERVER);
1178                 break;
1179         }
1180
1181         g_free(matcher_str);
1182 }
1183
1184 static void prefs_filtering_select(GtkCList *clist, gint row, gint column,
1185                                 GdkEvent *event)
1186 {
1187         FilteringProp * prop;
1188         gchar * filtering_str;
1189
1190         if (row == 0) {
1191                 prefs_filtering_reset_dialog();
1192                 return;
1193         }
1194
1195         if (!gtk_clist_get_text(GTK_CLIST(filtering.cond_clist),
1196                                 row, 0, &filtering_str))
1197                 return;
1198         
1199         prop = matcher_parser_get_filtering(filtering_str);
1200         if (prop == NULL)
1201                 return;
1202
1203         prefs_filtering_select_set(prop);
1204
1205         filteringprop_free(prop);
1206 }
1207
1208 static void prefs_filtering_select_dest(void)
1209 {
1210         FolderItem *dest;
1211         gchar * path;
1212
1213         dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL);
1214         if (!dest) return;
1215
1216         path = folder_item_get_identifier(dest);
1217
1218         gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), path);
1219         g_free(path);
1220 }
1221
1222 static void prefs_filtering_action_selection_changed(GtkList *list,
1223                                                      gpointer user_data)
1224 {
1225         gint value;
1226
1227         value = get_sel_from_list(GTK_LIST(filtering.action_list));
1228
1229         if (filtering.current_action != value) {
1230                 if (filtering.current_action == ACTION_FORWARD 
1231                 ||  filtering.current_action == ACTION_FORWARD_AS_ATTACHMENT) {
1232                         debug_print("unregistering address completion entry\n");
1233                         address_completion_unregister_entry(GTK_ENTRY(filtering.dest_entry));
1234                 }
1235                 if (value == ACTION_FORWARD || value == ACTION_FORWARD_AS_ATTACHMENT) {
1236                         debug_print("registering address completion entry\n");
1237                         address_completion_register_entry(GTK_ENTRY(filtering.dest_entry));
1238                 }
1239                 filtering.current_action = value;
1240         }
1241 }
1242
1243 static void prefs_filtering_action_select(GtkList *list,
1244                                           GtkWidget *widget,
1245                                           gpointer user_data)
1246 {
1247         gint value;
1248
1249         value = get_sel_from_list(GTK_LIST(filtering.action_list));
1250
1251         switch (value) {
1252         case ACTION_MOVE:
1253                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1254                 gtk_widget_show(filtering.dest_entry);
1255                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1256                 gtk_widget_show(filtering.dest_btn);
1257                 gtk_widget_show(filtering.dest_label);
1258                 gtk_widget_hide(filtering.exec_label);
1259                 gtk_widget_hide(filtering.exec_btn);
1260                 gtk_widget_hide(filtering.color_optmenu);
1261                 gtk_widget_hide(filtering.color_label);
1262                 break;
1263         case ACTION_COPY:
1264                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1265                 gtk_widget_show(filtering.dest_entry);
1266                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1267                 gtk_widget_show(filtering.dest_btn);
1268                 gtk_widget_show(filtering.dest_label);
1269                 gtk_widget_hide(filtering.exec_label);
1270                 gtk_widget_hide(filtering.exec_btn);
1271                 gtk_widget_hide(filtering.color_optmenu);
1272                 gtk_widget_hide(filtering.color_label);
1273                 break;
1274         case ACTION_DELETE:
1275                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1276                 gtk_widget_show(filtering.dest_entry);
1277                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1278                 gtk_widget_hide(filtering.dest_btn);
1279                 gtk_widget_show(filtering.dest_label);
1280                 gtk_widget_hide(filtering.exec_label);
1281                 gtk_widget_hide(filtering.exec_btn);
1282                 gtk_widget_hide(filtering.color_optmenu);
1283                 gtk_widget_hide(filtering.color_label);
1284                 break;
1285         case ACTION_MARK:
1286                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1287                 gtk_widget_show(filtering.dest_entry);
1288                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1289                 gtk_widget_hide(filtering.dest_btn);
1290                 gtk_widget_show(filtering.dest_label);
1291                 gtk_widget_hide(filtering.exec_label);
1292                 gtk_widget_hide(filtering.exec_btn);
1293                 gtk_widget_hide(filtering.color_optmenu);
1294                 gtk_widget_hide(filtering.color_label);
1295                 break;
1296         case ACTION_UNMARK:
1297                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1298                 gtk_widget_show(filtering.dest_entry);
1299                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1300                 gtk_widget_hide(filtering.dest_btn);
1301                 gtk_widget_show(filtering.dest_label);
1302                 gtk_widget_hide(filtering.exec_label);
1303                 gtk_widget_hide(filtering.exec_btn);
1304                 gtk_widget_hide(filtering.color_optmenu);
1305                 gtk_widget_hide(filtering.color_label);
1306                 break;
1307         case ACTION_MARK_AS_READ:
1308                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1309                 gtk_widget_show(filtering.dest_entry);
1310                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1311                 gtk_widget_hide(filtering.dest_btn);
1312                 gtk_widget_show(filtering.dest_label);
1313                 gtk_widget_hide(filtering.exec_label);
1314                 gtk_widget_hide(filtering.exec_btn);
1315                 gtk_widget_hide(filtering.color_optmenu);
1316                 gtk_widget_hide(filtering.color_label);
1317                 break;
1318         case ACTION_MARK_AS_UNREAD:
1319                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1320                 gtk_widget_show(filtering.dest_entry);
1321                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1322                 gtk_widget_hide(filtering.dest_btn);
1323                 gtk_widget_show(filtering.dest_label);
1324                 gtk_widget_hide(filtering.exec_label);
1325                 gtk_widget_hide(filtering.exec_btn);
1326                 gtk_widget_hide(filtering.color_optmenu);
1327                 gtk_widget_hide(filtering.color_label);
1328                 break;
1329         case ACTION_FORWARD:
1330                 gtk_widget_set_sensitive(filtering.account_combo, TRUE);
1331                 gtk_widget_show(filtering.dest_entry);
1332                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1333                 gtk_widget_hide(filtering.dest_btn);
1334                 gtk_widget_show(filtering.dest_label);
1335                 gtk_widget_hide(filtering.exec_label);
1336                 gtk_widget_hide(filtering.exec_btn);
1337                 gtk_widget_hide(filtering.color_optmenu);
1338                 gtk_widget_hide(filtering.color_label);
1339                 break;
1340         case ACTION_FORWARD_AS_ATTACHMENT:
1341                 gtk_widget_set_sensitive(filtering.account_combo, TRUE);
1342                 gtk_widget_show(filtering.dest_entry);
1343                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1344                 gtk_widget_hide(filtering.dest_btn);
1345                 gtk_widget_show(filtering.dest_label);
1346                 gtk_widget_hide(filtering.exec_label);
1347                 gtk_widget_hide(filtering.exec_btn);
1348                 gtk_widget_hide(filtering.color_optmenu);
1349                 gtk_widget_hide(filtering.color_label);
1350                 break;
1351         case ACTION_REDIRECT:
1352                 gtk_widget_set_sensitive(filtering.account_combo, TRUE);
1353                 gtk_widget_show(filtering.dest_entry);
1354                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1355                 gtk_widget_hide(filtering.dest_btn);
1356                 gtk_widget_show(filtering.dest_label);
1357                 gtk_widget_hide(filtering.exec_label);
1358                 gtk_widget_hide(filtering.exec_btn);
1359                 gtk_widget_hide(filtering.color_optmenu);
1360                 gtk_widget_hide(filtering.color_label);
1361                 break;
1362         case ACTION_EXECUTE:
1363                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1364                 gtk_widget_show(filtering.dest_entry);
1365                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1366                 gtk_widget_hide(filtering.dest_btn);
1367                 gtk_widget_hide(filtering.dest_label);
1368                 gtk_widget_show(filtering.exec_label);
1369                 gtk_widget_set_sensitive(filtering.exec_btn, TRUE);
1370                 gtk_widget_show(filtering.exec_btn);
1371                 gtk_widget_hide(filtering.color_optmenu);
1372                 gtk_widget_hide(filtering.color_label);
1373                 break;
1374         case ACTION_COLOR:
1375                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1376                 gtk_widget_hide(filtering.dest_entry);
1377                 gtk_widget_hide(filtering.dest_btn);
1378                 gtk_widget_hide(filtering.dest_label);
1379                 gtk_widget_hide(filtering.exec_label);
1380                 gtk_widget_show(filtering.exec_btn);
1381                 gtk_widget_set_sensitive(filtering.exec_btn, FALSE);
1382                 gtk_widget_show(filtering.color_optmenu);
1383                 gtk_widget_show(filtering.color_label);
1384                 break;
1385         case ACTION_DELETE_ON_SERVER:
1386                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1387                 gtk_widget_hide(filtering.dest_entry);
1388                 gtk_widget_hide(filtering.dest_btn);
1389                 gtk_widget_hide(filtering.dest_label);
1390                 gtk_widget_hide(filtering.exec_label);
1391                 gtk_widget_show(filtering.exec_btn);
1392                 gtk_widget_hide(filtering.exec_btn);
1393                 gtk_widget_hide(filtering.color_optmenu);
1394                 gtk_widget_hide(filtering.color_label);
1395                 break;
1396         }
1397 }
1398
1399 static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
1400                                  gpointer data)
1401 {
1402         prefs_filtering_cancel();
1403         return TRUE;
1404 }
1405
1406 static void prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
1407                                      gpointer data)
1408 {
1409         if (event && event->keyval == GDK_Escape)
1410                 prefs_filtering_cancel();
1411 }
1412
1413 static void prefs_filtering_ok(void)
1414 {
1415         prefs_filtering_set_list();
1416         prefs_matcher_write_config();
1417         prefs_filtering_close();
1418 }
1419
1420 static void prefs_filtering_cancel(void)
1421 {
1422         prefs_matcher_read_config();
1423         prefs_filtering_close();
1424 }