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