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