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