1faf4b8fc1bfed2a324ba26750a58a2cafa788cc
[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         gint destlen;
619         gint prefixlen;
620         gint oldpathlen;
621
622         g_return_if_fail(old_path != NULL);
623         g_return_if_fail(new_path != NULL);
624
625         oldpathlen = strlen(old_path);
626
627         for (cur = global_processing; cur != NULL; cur = cur->next) {
628                 FilteringProp   *filtering = (FilteringProp *)cur->data;
629                 FilteringAction *action = filtering->action;
630
631                 if (!action->destination) continue;
632
633                 destlen = strlen(action->destination);
634
635                 if (destlen > oldpathlen) {
636                         prefixlen = destlen - oldpathlen;
637                         suffix = action->destination + prefixlen;
638
639                         if (!strncmp(old_path, suffix, oldpathlen)) {
640                                 prefix = g_malloc0(prefixlen + 1);
641                                 strncpy2(prefix, action->destination, prefixlen);
642
643                                 base = suffix + oldpathlen;
644                                 while (*base == G_DIR_SEPARATOR) base++;
645                                 if (*base == '\0')
646                                         dest_path = g_strconcat(prefix,
647                                                                 G_DIR_SEPARATOR_S,
648                                                                 new_path, NULL);
649                                 else
650                                         dest_path = g_strconcat(prefix,
651                                                                 G_DIR_SEPARATOR_S,
652                                                                 new_path,
653                                                                 G_DIR_SEPARATOR_S,
654                                                                 base, NULL);
655
656                                 g_free(prefix);
657                                 g_free(action->destination);
658                                 action->destination = dest_path;
659                         }
660                 }
661         }
662
663         prefs_matcher_write_config();
664 }
665
666 void prefs_filtering_delete_path(const gchar *path)
667 {
668         GSList *cur;
669         GSList *next;
670         gchar *suffix;
671         gint destlen;
672         gint prefixlen;
673         gint pathlen;
674
675         g_return_if_fail(path != NULL);
676
677         pathlen = strlen(path);
678         for (cur = global_processing; cur != NULL; cur = next) {
679                 FilteringProp *filtering = (FilteringProp *)cur->data;
680                 FilteringAction *action = filtering->action;
681                 next = cur->next;
682
683                 if (!action->destination) continue;
684
685                 destlen = strlen(action->destination);
686
687                 if (destlen > pathlen) {
688                         prefixlen = destlen - pathlen;
689                         suffix = action->destination + prefixlen;
690
691                         if (suffix && !strncmp(path, suffix, pathlen)) {
692                                 filteringprop_free(filtering);
693                                 global_processing = 
694                                         g_slist_remove(global_processing, filtering);
695                         }
696                 }
697         }
698
699         prefs_matcher_write_config();
700 }
701
702 static void prefs_filtering_set_dialog(const gchar *header, const gchar *key)
703 {
704         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
705         GSList *cur;
706         GSList * prefs_filtering;
707         gchar *cond_str[1];
708         gint row;
709         
710         gtk_clist_freeze(clist);
711         gtk_clist_clear(clist);
712
713         cond_str[0] = _("(New)");
714         row = gtk_clist_append(clist, cond_str);
715         gtk_clist_set_row_data(clist, row, NULL);
716
717         if (cur_item == NULL)
718                 prefs_filtering = global_processing;
719         else
720                 prefs_filtering = cur_item->prefs->processing;
721
722         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur)) {
723                 FilteringProp * prop = (FilteringProp *) cur->data;
724
725                 cond_str[0] = filteringprop_to_string(prop);
726                 subst_char(cond_str[0], '\t', ':');
727                 row = gtk_clist_append(clist, cond_str);
728                 gtk_clist_set_row_data(clist, row, prop);
729
730                 g_free(cond_str[0]);
731         }
732
733         prefs_filtering_update_hscrollbar();
734         gtk_clist_thaw(clist);
735
736         prefs_filtering_reset_dialog();
737
738         if (header && key) {
739                 gchar *match_str = g_strconcat(header, " ",
740                         get_matchparser_tab_str(MATCHTYPE_MATCHCASE),
741                         " \"", key, "\"", NULL);
742                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), match_str);
743                 g_free(match_str);
744         }
745 }
746
747 static void prefs_filtering_reset_dialog(void)
748 {
749         gtk_list_select_item(GTK_LIST(filtering.action_list), 0);
750         gtk_list_select_item(GTK_LIST(filtering.account_list), 0);
751         gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), "");
752         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), "");
753 }
754
755 static void prefs_filtering_set_list(void)
756 {
757         gint row = 1;
758         FilteringProp *prop;
759         GSList * cur;
760         gchar * filtering_str;
761         GSList * prefs_filtering;
762
763         if (cur_item == NULL)
764                 prefs_filtering = global_processing;
765         else
766                 prefs_filtering = cur_item->prefs->processing;
767
768         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
769                 filteringprop_free((FilteringProp *) cur->data);
770         g_slist_free(prefs_filtering);
771         prefs_filtering = NULL;
772
773         while (gtk_clist_get_text(GTK_CLIST(filtering.cond_clist),
774                                   row, 0, &filtering_str)) {
775                 if (strcmp(filtering_str, _("(New)")) != 0) {
776                         /* tmp = filtering_str; */
777                         prop = matcher_parser_get_filtering(filtering_str);
778                         if (prop != NULL)
779                                 prefs_filtering =
780                                         g_slist_append(prefs_filtering, prop);
781                 }
782                 row++;
783         }
784
785         if (cur_item == NULL)
786                 global_processing = prefs_filtering;
787         else
788                 cur_item->prefs->processing = prefs_filtering;
789 }
790
791 static gint prefs_filtering_clist_set_row(gint row, FilteringProp * prop)
792 {
793         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
794         gchar * str;
795         gchar *cond_str[1];
796
797         if (prop == NULL) {
798                 cond_str[0] = _("(New)");
799                 return gtk_clist_append(clist, cond_str);
800         }
801
802         str = filteringprop_to_string(prop);
803         if (str == NULL) {
804                 return -1;
805         }
806         cond_str[0] = str;
807
808         if (row < 0)
809                 row = gtk_clist_append(clist, cond_str);
810         else
811                 gtk_clist_set_text(clist, row, 0, cond_str[0]);
812         g_free(str);
813
814         return row;
815 }
816
817 static void prefs_filtering_condition_define_done(MatcherList * matchers)
818 {
819         gchar * str;
820
821         if (matchers == NULL)
822                 return;
823
824         str = matcherlist_to_string(matchers);
825
826         if (str != NULL) {
827                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), str);
828                 g_free(str);
829         }
830 }
831
832 static void prefs_filtering_condition_define(void)
833 {
834         gchar * cond_str;
835         MatcherList * matchers = NULL;
836
837         cond_str = gtk_entry_get_text(GTK_ENTRY(filtering.cond_entry));
838
839         if (*cond_str != '\0') {
840                 matchers = matcher_parser_get_cond(cond_str);
841                 if (matchers == NULL)
842                         alertpanel_error(_("Match string is not valid."));
843         }
844
845         prefs_matcher_open(matchers, prefs_filtering_condition_define_done);
846
847         if (matchers != NULL)
848                 matcherlist_free(matchers);
849 }
850
851
852 /* register / substitute delete buttons */
853
854
855 static FilteringProp * prefs_filtering_dialog_to_filtering(void)
856 {
857         MatcherList * cond;
858         gchar * cond_str;
859         FilteringProp * prop;
860         FilteringAction * action;
861         gint list_id;
862         gint action_id;
863         gint action_type;
864         gint account_id;
865         gchar * destination;
866         gint labelcolor = 0;
867         
868         cond_str = gtk_entry_get_text(GTK_ENTRY(filtering.cond_entry));
869         if (*cond_str == '\0') {
870                 alertpanel_error(_("Score is not set."));
871                 return NULL;
872         }
873
874         action_id = get_sel_from_list(GTK_LIST(filtering.action_list));
875         action_type = prefs_filtering_get_matching_from_action(action_id);
876         list_id = get_sel_from_list(GTK_LIST(filtering.account_list));
877         account_id = get_account_id_from_list_id(list_id);
878
879         switch (action_id) {
880         case ACTION_MOVE:
881         case ACTION_COPY:
882         case ACTION_FORWARD:
883         case ACTION_FORWARD_AS_ATTACHMENT:
884         case ACTION_REDIRECT:
885         case ACTION_EXECUTE:
886                 destination = gtk_entry_get_text(GTK_ENTRY(filtering.dest_entry));
887                 if (*destination == '\0') {
888                         alertpanel_error(_("Destination is not set."));
889                         return NULL;
890                 }
891                 break;
892         case ACTION_COLOR:
893                 labelcolor = colorlabel_get_color_menu_active_item(
894                         gtk_option_menu_get_menu(GTK_OPTION_MENU(filtering.color_optmenu)));
895                 destination = NULL;     
896                 break;
897         case ACTION_DELETE_ON_SERVER:
898                 destination = NULL;
899                 break;
900         default:
901                 destination = NULL;
902                 break;
903         }
904         
905         action = filteringaction_new(action_type, account_id, destination, labelcolor);
906
907         cond = matcher_parser_get_cond(cond_str);
908
909         if (cond == NULL) {
910                 alertpanel_error(_("Match string is not valid."));
911                 filteringaction_free(action);
912                 return NULL;
913         }
914
915         prop = filteringprop_new(cond, action);
916
917         return prop;
918 }
919
920 static void prefs_filtering_register_cb(void)
921 {
922         FilteringProp * prop;
923         
924         prop = prefs_filtering_dialog_to_filtering();
925         if (prop == NULL)
926                 return;
927         prefs_filtering_clist_set_row(-1, prop);
928
929         filteringprop_free(prop);
930         
931         prefs_filtering_update_hscrollbar();
932 }
933
934 static void prefs_filtering_substitute_cb(void)
935 {
936         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
937         gint row;
938         FilteringProp * prop;
939         
940         if (!clist->selection) return;
941
942         row = GPOINTER_TO_INT(clist->selection->data);
943         if (row == 0) return;
944
945         prop = prefs_filtering_dialog_to_filtering();
946         if (prop == NULL)
947                 return;
948         prefs_filtering_clist_set_row(row, prop);
949
950         filteringprop_free(prop);
951         
952         prefs_filtering_update_hscrollbar();
953 }
954
955 static void prefs_filtering_delete_cb(void)
956 {
957         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
958         gint row;
959
960         if (!clist->selection) return;
961         row = GPOINTER_TO_INT(clist->selection->data);
962         if (row == 0) return;
963
964         if (alertpanel(_("Delete rule"),
965                        _("Do you really want to delete this rule?"),
966                        _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
967                 return;
968
969         gtk_clist_remove(clist, row);
970
971         prefs_filtering_update_hscrollbar();
972 }
973
974 static void prefs_filtering_up(void)
975 {
976         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
977         gint row;
978
979         if (!clist->selection) return;
980
981         row = GPOINTER_TO_INT(clist->selection->data);
982         if (row > 1) {
983                 gtk_clist_row_move(clist, row, row - 1);
984                 if(gtk_clist_row_is_visible(clist, row - 1) != GTK_VISIBILITY_FULL) {
985                         gtk_clist_moveto(clist, row - 1, 0, 0, 0);
986                 } 
987         }
988 }
989
990 static void prefs_filtering_down(void)
991 {
992         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
993         gint row;
994
995         if (!clist->selection) return;
996
997         row = GPOINTER_TO_INT(clist->selection->data);
998         if (row > 0 && row < clist->rows - 1) {
999                 gtk_clist_row_move(clist, row, row + 1);
1000                 if(gtk_clist_row_is_visible(clist, row + 1) != GTK_VISIBILITY_FULL) {
1001                         gtk_clist_moveto(clist, row + 1, 0, 1, 0);
1002                 } 
1003         }
1004 }
1005
1006 static void prefs_filtering_select_set(FilteringProp * prop)
1007 {
1008         FilteringAction * action;
1009         gchar * matcher_str;
1010         gint list_id;
1011
1012         prefs_filtering_reset_dialog();
1013
1014         action = prop->action;
1015
1016         matcher_str = matcherlist_to_string(prop->matchers);
1017         if (matcher_str == NULL) {
1018                 filteringprop_free(prop);
1019                 return;
1020         }
1021
1022         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), matcher_str);
1023         
1024         if (action->destination)
1025                 gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), action->destination);
1026         else
1027                 gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), "");
1028
1029         switch(action->type) {
1030         case MATCHACTION_MOVE:
1031                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1032                                      ACTION_MOVE);
1033                 break;
1034         case MATCHACTION_COPY:
1035                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1036                                      ACTION_COPY);
1037                 break;
1038         case MATCHACTION_DELETE:
1039                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1040                                      ACTION_DELETE);
1041                 break;
1042         case MATCHACTION_MARK:
1043                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1044                                      ACTION_MARK);
1045                 break;
1046         case MATCHACTION_UNMARK:
1047                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1048                                      ACTION_UNMARK);
1049                 break;
1050         case MATCHACTION_MARK_AS_READ:
1051                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1052                                      ACTION_MARK_AS_READ);
1053                 break;
1054         case MATCHACTION_MARK_AS_UNREAD:
1055                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1056                                      ACTION_MARK_AS_UNREAD);
1057                 break;
1058         case MATCHACTION_FORWARD:
1059                 list_id = get_list_id_from_account_id(action->account_id);
1060                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1061                                      ACTION_FORWARD);
1062                 gtk_list_select_item(GTK_LIST(filtering.account_list),
1063                                      list_id);
1064                 break;
1065         case MATCHACTION_FORWARD_AS_ATTACHMENT:
1066                 list_id = get_list_id_from_account_id(action->account_id);
1067                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1068                                      ACTION_FORWARD_AS_ATTACHMENT);
1069                 gtk_list_select_item(GTK_LIST(filtering.account_list),
1070                                      list_id);
1071                 break;
1072         case MATCHACTION_REDIRECT:
1073                 list_id = get_list_id_from_account_id(action->account_id);
1074                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1075                                      ACTION_REDIRECT);
1076                 gtk_list_select_item(GTK_LIST(filtering.account_list),
1077                                      list_id);
1078                 break;
1079         case MATCHACTION_EXECUTE:
1080                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1081                                      ACTION_EXECUTE);
1082                 break;
1083         case MATCHACTION_COLOR:
1084                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1085                                      ACTION_COLOR);
1086                 gtk_option_menu_set_history(GTK_OPTION_MENU(filtering.color_optmenu), action->labelcolor);     
1087                 break;
1088         case MATCHACTION_DELETE_ON_SERVER:
1089                 gtk_list_select_item(GTK_LIST(filtering.action_list),
1090                                      ACTION_DELETE_ON_SERVER);
1091                 break;
1092         }
1093
1094         g_free(matcher_str);
1095 }
1096
1097 static void prefs_filtering_select(GtkCList *clist, gint row, gint column,
1098                                 GdkEvent *event)
1099 {
1100         FilteringProp * prop;
1101         gchar * filtering_str;
1102
1103         if (row == 0) {
1104                 prefs_filtering_reset_dialog();
1105                 return;
1106         }
1107
1108         if (!gtk_clist_get_text(GTK_CLIST(filtering.cond_clist),
1109                                 row, 0, &filtering_str))
1110                 return;
1111         
1112         prop = matcher_parser_get_filtering(filtering_str);
1113         if (prop == NULL)
1114                 return;
1115
1116         prefs_filtering_select_set(prop);
1117
1118         filteringprop_free(prop);
1119 }
1120
1121 static void prefs_filtering_select_dest(void)
1122 {
1123         FolderItem *dest;
1124         gchar * path;
1125
1126         dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL);
1127         if (!dest) return;
1128
1129         path = folder_item_get_identifier(dest);
1130
1131         gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), path);
1132         g_free(path);
1133 }
1134
1135 static void prefs_filtering_action_selection_changed(GtkList *list,
1136                                                      gpointer user_data)
1137 {
1138         gint value;
1139
1140         value = get_sel_from_list(GTK_LIST(filtering.action_list));
1141
1142         if (filtering.current_action != value) {
1143                 if (filtering.current_action == ACTION_FORWARD 
1144                 ||  filtering.current_action == ACTION_FORWARD_AS_ATTACHMENT) {
1145                         debug_print("unregistering address completion entry\n");
1146                         address_completion_unregister_entry(GTK_ENTRY(filtering.dest_entry));
1147                 }
1148                 if (value == ACTION_FORWARD || value == ACTION_FORWARD_AS_ATTACHMENT) {
1149                         debug_print("registering address completion entry\n");
1150                         address_completion_register_entry(GTK_ENTRY(filtering.dest_entry));
1151                 }
1152                 filtering.current_action = value;
1153         }
1154 }
1155
1156 static void prefs_filtering_action_select(GtkList *list,
1157                                           GtkWidget *widget,
1158                                           gpointer user_data)
1159 {
1160         gint value;
1161
1162         value = get_sel_from_list(GTK_LIST(filtering.action_list));
1163
1164         switch (value) {
1165         case ACTION_MOVE:
1166                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1167                 gtk_widget_show(filtering.dest_entry);
1168                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1169                 gtk_widget_show(filtering.dest_btn);
1170                 gtk_widget_show(filtering.dest_label);
1171                 gtk_widget_hide(filtering.exec_label);
1172                 gtk_widget_hide(filtering.exec_btn);
1173                 gtk_widget_hide(filtering.color_optmenu);
1174                 gtk_widget_hide(filtering.color_label);
1175                 break;
1176         case ACTION_COPY:
1177                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1178                 gtk_widget_show(filtering.dest_entry);
1179                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1180                 gtk_widget_show(filtering.dest_btn);
1181                 gtk_widget_show(filtering.dest_label);
1182                 gtk_widget_hide(filtering.exec_label);
1183                 gtk_widget_hide(filtering.exec_btn);
1184                 gtk_widget_hide(filtering.color_optmenu);
1185                 gtk_widget_hide(filtering.color_label);
1186                 break;
1187         case ACTION_DELETE:
1188                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1189                 gtk_widget_show(filtering.dest_entry);
1190                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1191                 gtk_widget_hide(filtering.dest_btn);
1192                 gtk_widget_show(filtering.dest_label);
1193                 gtk_widget_hide(filtering.exec_label);
1194                 gtk_widget_hide(filtering.exec_btn);
1195                 gtk_widget_hide(filtering.color_optmenu);
1196                 gtk_widget_hide(filtering.color_label);
1197                 break;
1198         case ACTION_MARK:
1199                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1200                 gtk_widget_show(filtering.dest_entry);
1201                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1202                 gtk_widget_hide(filtering.dest_btn);
1203                 gtk_widget_show(filtering.dest_label);
1204                 gtk_widget_hide(filtering.exec_label);
1205                 gtk_widget_hide(filtering.exec_btn);
1206                 gtk_widget_hide(filtering.color_optmenu);
1207                 gtk_widget_hide(filtering.color_label);
1208                 break;
1209         case ACTION_UNMARK:
1210                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1211                 gtk_widget_show(filtering.dest_entry);
1212                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1213                 gtk_widget_hide(filtering.dest_btn);
1214                 gtk_widget_show(filtering.dest_label);
1215                 gtk_widget_hide(filtering.exec_label);
1216                 gtk_widget_hide(filtering.exec_btn);
1217                 gtk_widget_hide(filtering.color_optmenu);
1218                 gtk_widget_hide(filtering.color_label);
1219                 break;
1220         case ACTION_MARK_AS_READ:
1221                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1222                 gtk_widget_show(filtering.dest_entry);
1223                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1224                 gtk_widget_hide(filtering.dest_btn);
1225                 gtk_widget_show(filtering.dest_label);
1226                 gtk_widget_hide(filtering.exec_label);
1227                 gtk_widget_hide(filtering.exec_btn);
1228                 gtk_widget_hide(filtering.color_optmenu);
1229                 gtk_widget_hide(filtering.color_label);
1230                 break;
1231         case ACTION_MARK_AS_UNREAD:
1232                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1233                 gtk_widget_show(filtering.dest_entry);
1234                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1235                 gtk_widget_hide(filtering.dest_btn);
1236                 gtk_widget_show(filtering.dest_label);
1237                 gtk_widget_hide(filtering.exec_label);
1238                 gtk_widget_hide(filtering.exec_btn);
1239                 gtk_widget_hide(filtering.color_optmenu);
1240                 gtk_widget_hide(filtering.color_label);
1241                 break;
1242         case ACTION_FORWARD:
1243                 gtk_widget_set_sensitive(filtering.account_combo, TRUE);
1244                 gtk_widget_show(filtering.dest_entry);
1245                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1246                 gtk_widget_hide(filtering.dest_btn);
1247                 gtk_widget_show(filtering.dest_label);
1248                 gtk_widget_hide(filtering.exec_label);
1249                 gtk_widget_hide(filtering.exec_btn);
1250                 gtk_widget_hide(filtering.color_optmenu);
1251                 gtk_widget_hide(filtering.color_label);
1252                 break;
1253         case ACTION_FORWARD_AS_ATTACHMENT:
1254                 gtk_widget_set_sensitive(filtering.account_combo, TRUE);
1255                 gtk_widget_show(filtering.dest_entry);
1256                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1257                 gtk_widget_hide(filtering.dest_btn);
1258                 gtk_widget_show(filtering.dest_label);
1259                 gtk_widget_hide(filtering.exec_label);
1260                 gtk_widget_hide(filtering.exec_btn);
1261                 gtk_widget_hide(filtering.color_optmenu);
1262                 gtk_widget_hide(filtering.color_label);
1263                 break;
1264         case ACTION_REDIRECT:
1265                 gtk_widget_set_sensitive(filtering.account_combo, TRUE);
1266                 gtk_widget_show(filtering.dest_entry);
1267                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1268                 gtk_widget_hide(filtering.dest_btn);
1269                 gtk_widget_show(filtering.dest_label);
1270                 gtk_widget_hide(filtering.exec_label);
1271                 gtk_widget_hide(filtering.exec_btn);
1272                 gtk_widget_hide(filtering.color_optmenu);
1273                 gtk_widget_hide(filtering.color_label);
1274                 break;
1275         case ACTION_EXECUTE:
1276                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1277                 gtk_widget_show(filtering.dest_entry);
1278                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1279                 gtk_widget_hide(filtering.dest_btn);
1280                 gtk_widget_hide(filtering.dest_label);
1281                 gtk_widget_show(filtering.exec_label);
1282                 gtk_widget_set_sensitive(filtering.exec_btn, TRUE);
1283                 gtk_widget_show(filtering.exec_btn);
1284                 gtk_widget_hide(filtering.color_optmenu);
1285                 gtk_widget_hide(filtering.color_label);
1286                 break;
1287         case ACTION_COLOR:
1288                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1289                 gtk_widget_hide(filtering.dest_entry);
1290                 gtk_widget_hide(filtering.dest_btn);
1291                 gtk_widget_hide(filtering.dest_label);
1292                 gtk_widget_hide(filtering.exec_label);
1293                 gtk_widget_show(filtering.exec_btn);
1294                 gtk_widget_set_sensitive(filtering.exec_btn, FALSE);
1295                 gtk_widget_show(filtering.color_optmenu);
1296                 gtk_widget_show(filtering.color_label);
1297                 break;
1298         case ACTION_DELETE_ON_SERVER:
1299                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1300                 gtk_widget_hide(filtering.dest_entry);
1301                 gtk_widget_hide(filtering.dest_btn);
1302                 gtk_widget_hide(filtering.dest_label);
1303                 gtk_widget_hide(filtering.exec_label);
1304                 gtk_widget_show(filtering.exec_btn);
1305                 gtk_widget_hide(filtering.exec_btn);
1306                 gtk_widget_hide(filtering.color_optmenu);
1307                 gtk_widget_hide(filtering.color_label);
1308                 break;
1309         }
1310 }
1311
1312 static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
1313                                  gpointer data)
1314 {
1315         prefs_filtering_cancel();
1316         return TRUE;
1317 }
1318
1319 static void prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
1320                                      gpointer data)
1321 {
1322         if (event && event->keyval == GDK_Escape)
1323                 prefs_filtering_cancel();
1324 }
1325
1326 static void prefs_filtering_ok(void)
1327 {
1328         prefs_filtering_set_list();
1329         prefs_matcher_write_config();
1330         prefs_filtering_close();
1331 }
1332
1333 static void prefs_filtering_cancel(void)
1334 {
1335         prefs_matcher_read_config();
1336         prefs_filtering_close();
1337 }