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