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