0.9.4claws16
[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_entry;
62
63         GtkWidget *cond_clist;
64 } filtering;
65
66 /* widget creating functions */
67 static void prefs_filtering_create              (void);
68
69 static void prefs_filtering_set_dialog  (const gchar *header,
70                                          const gchar *key);
71 static void prefs_filtering_set_list    (void);
72
73 /* callback functions */
74 static void prefs_filtering_register_cb (void);
75 static void prefs_filtering_substitute_cb       (void);
76 static void prefs_filtering_delete_cb   (void);
77 static void prefs_filtering_up          (void);
78 static void prefs_filtering_down        (void);
79 static void prefs_filtering_select      (GtkCList       *clist,
80                                          gint            row,
81                                          gint            column,
82                                          GdkEvent       *event);
83
84 static gint prefs_filtering_deleted     (GtkWidget      *widget,
85                                          GdkEventAny    *event,
86                                          gpointer        data);
87 static void prefs_filtering_key_pressed (GtkWidget      *widget,
88                                          GdkEventKey    *event,
89                                          gpointer        data);
90 static void prefs_filtering_cancel      (void);
91 static void prefs_filtering_ok          (void);
92
93 static void prefs_filtering_condition_define    (void);
94 static void prefs_filtering_action_define(void);
95 static gint prefs_filtering_clist_set_row       (gint row, FilteringProp * prop);
96 static void prefs_filtering_select_dest         (void);
97 static void prefs_filtering_action_select       (GtkList *list,
98                                                  GtkWidget *widget, 
99                                                  gpointer user_data);
100
101 static void prefs_filtering_action_selection_changed(GtkList *list,
102                                                      gpointer user_data);
103                                           
104 static void prefs_filtering_reset_dialog        (void);
105 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data);
106 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data);
107
108 static FolderItem * cur_item = NULL; /* folder (if dialog opened for processing) */
109
110 typedef enum Action_ {
111         ACTION_MOVE,
112         ACTION_COPY,
113         ACTION_DELETE,
114         ACTION_MARK,
115         ACTION_UNMARK,
116         ACTION_LOCK,
117         ACTION_UNLOCK,
118         ACTION_MARK_AS_READ,
119         ACTION_MARK_AS_UNREAD,
120         ACTION_FORWARD,
121         ACTION_FORWARD_AS_ATTACHMENT,
122         ACTION_REDIRECT,
123         ACTION_EXECUTE,
124         ACTION_COLOR,
125         /* add other action constants */
126 } Action;
127
128 static struct {
129         gchar *text;
130         Action action;
131 } action_text [] = {
132         { N_("Move"),                   ACTION_MOVE     },      
133         { N_("Copy"),                   ACTION_COPY     },
134         { N_("Delete"),                 ACTION_DELETE   },
135         { N_("Mark"),                   ACTION_MARK     },
136         { N_("Unmark"),                 ACTION_UNMARK   },
137         { N_("Lock"),                   ACTION_LOCK     },
138         { N_("Unlock"),                 ACTION_UNLOCK   },
139         { N_("Mark as read"),           ACTION_MARK_AS_READ },
140         { N_("Mark as unread"),         ACTION_MARK_AS_UNREAD },
141         { N_("Forward"),                ACTION_FORWARD  },
142         { N_("Forward as attachment"),  ACTION_FORWARD_AS_ATTACHMENT },
143         { N_("Redirect"),               ACTION_REDIRECT },
144         { N_("Execute"),                ACTION_EXECUTE  },
145         { N_("Color"),                  ACTION_COLOR    }
146 };
147
148 static const gchar *get_action_text(Action action)
149 {
150         int n;
151         for (n = 0; n < sizeof action_text / sizeof action_text[0]; n++)
152                 if (action_text[n].action == action)
153                         return action_text[n].text;
154         return "";                      
155 }
156
157 static gint get_sel_from_list(GtkList * list)
158 {
159         gint row = 0;
160         void * sel;
161         GList * child;
162
163         if (list->selection == NULL)
164                 return -1;
165
166         sel = list->selection->data;
167         for(child = list->children ; child != NULL ;
168             child = g_list_next(child)) {
169                 if (child->data == sel)
170                         return row;
171                 row ++;
172         }
173         
174         return row;
175 }
176
177 static gint get_account_id_from_list_id(gint list_id)
178 {
179         GList * accounts;
180
181         for (accounts = account_get_list() ; accounts != NULL;
182              accounts = accounts->next) {
183                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
184
185                 if (list_id == 0)
186                         return ac->account_id;
187                 list_id--;
188         }
189         return 0;
190 }
191
192 static gint get_list_id_from_account_id(gint account_id)
193 {
194         GList * accounts;
195         gint list_id = 0;
196
197         for (accounts = account_get_list() ; accounts != NULL;
198              accounts = accounts->next) {
199                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
200
201                 if (account_id == ac->account_id)
202                         return list_id;
203                 list_id++;
204         }
205         return 0;
206 }
207
208 static gint prefs_filtering_get_matching_from_action(Action action_id)
209 {
210         switch (action_id) {
211         case ACTION_MOVE:
212                 return MATCHACTION_MOVE;
213         case ACTION_COPY:
214                 return MATCHACTION_COPY;
215         case ACTION_DELETE:
216                 return MATCHACTION_DELETE;
217         case ACTION_MARK:
218                 return MATCHACTION_MARK;
219         case ACTION_UNMARK:
220                 return MATCHACTION_UNMARK;
221         case ACTION_LOCK:
222                 return MATCHACTION_LOCK;
223         case ACTION_UNLOCK:
224                 return MATCHACTION_UNLOCK;
225         case ACTION_MARK_AS_READ:
226                 return MATCHACTION_MARK_AS_READ;
227         case ACTION_MARK_AS_UNREAD:
228                 return MATCHACTION_MARK_AS_UNREAD;
229         case ACTION_FORWARD:
230                 return MATCHACTION_FORWARD;
231         case ACTION_FORWARD_AS_ATTACHMENT:
232                 return MATCHACTION_FORWARD_AS_ATTACHMENT;
233         case ACTION_REDIRECT:
234                 return MATCHACTION_REDIRECT;
235         case ACTION_EXECUTE:
236                 return MATCHACTION_EXECUTE;
237         case ACTION_COLOR:
238                 return MATCHACTION_COLOR;
239         default:
240                 return -1;
241         }
242 }
243
244 void prefs_filtering_open(FolderItem * item,
245                           const gchar *header,
246                           const gchar *key)
247 {
248         gchar *esckey;
249
250         if (prefs_rc_is_readonly(FILTERING_RC))
251                 return;
252
253         inc_lock();
254
255         if (!filtering.window) {
256                 prefs_filtering_create();
257         }
258
259         manage_window_set_transient(GTK_WINDOW(filtering.window));
260         gtk_widget_grab_focus(filtering.ok_btn);
261
262         cur_item = item;
263
264         esckey = matcher_escape_str(key);
265         prefs_filtering_set_dialog(header, esckey);
266         g_free(esckey);
267
268         gtk_widget_show(filtering.window);
269
270         start_address_completion();
271 }
272
273 /* prefs_filtering_close() - just to have one common exit point */
274 static void prefs_filtering_close(void)
275 {
276         end_address_completion();
277         
278         gtk_widget_hide(filtering.window);
279         inc_unlock();
280 }
281
282 static void prefs_filtering_create(void)
283 {
284         GtkWidget *window;
285         GtkWidget *vbox;
286         GtkWidget *ok_btn;
287         GtkWidget *cancel_btn;
288         GtkWidget *confirm_area;
289
290         GtkWidget *vbox1;
291         GtkWidget *hbox1;
292         GtkWidget *reg_hbox;
293         GtkWidget *arrow;
294         GtkWidget *btn_hbox;
295
296         GtkWidget *cond_label;
297         GtkWidget *cond_entry;
298         GtkWidget *cond_btn;
299         GtkWidget *action_label;
300         GtkWidget *action_entry;
301         GtkWidget *action_btn;
302
303         GtkWidget *reg_btn;
304         GtkWidget *subst_btn;
305         GtkWidget *del_btn;
306
307         GtkWidget *cond_hbox;
308         GtkWidget *cond_scrolledwin;
309         GtkWidget *cond_clist;
310
311         GtkWidget *btn_vbox;
312         GtkWidget *up_btn;
313         GtkWidget *down_btn;
314
315
316         GList *combo_items;
317         gint i;
318
319         GList *accounts;
320         GList * cur;
321
322         gchar *title[1];
323
324         debug_print("Creating filtering configuration window...\n");
325
326         window = gtk_window_new (GTK_WINDOW_DIALOG);
327         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
328         gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
329         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
330         gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, FALSE);
331
332         vbox = gtk_vbox_new (FALSE, 6);
333         gtk_widget_show (vbox);
334         gtk_container_add (GTK_CONTAINER (window), vbox);
335
336         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
337                                 &cancel_btn, _("Cancel"), NULL, NULL);
338         gtk_widget_show (confirm_area);
339         gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
340         gtk_widget_grab_default (ok_btn);
341
342         gtk_window_set_title (GTK_WINDOW(window),
343                                     _("Filtering/Processing configuration"));
344
345         gtk_signal_connect (GTK_OBJECT(window), "delete_event",
346                             GTK_SIGNAL_FUNC(prefs_filtering_deleted), NULL);
347         gtk_signal_connect (GTK_OBJECT(window), "key_press_event",
348                             GTK_SIGNAL_FUNC(prefs_filtering_key_pressed), NULL);
349         MANAGE_WINDOW_SIGNALS_CONNECT (window);
350         gtk_signal_connect (GTK_OBJECT(ok_btn), "clicked",
351                             GTK_SIGNAL_FUNC(prefs_filtering_ok), NULL);
352         gtk_signal_connect (GTK_OBJECT(cancel_btn), "clicked",
353                             GTK_SIGNAL_FUNC(prefs_filtering_cancel), NULL);
354
355         vbox1 = gtk_vbox_new (FALSE, VSPACING);
356         gtk_widget_show (vbox1);
357         gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
358         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
359
360         cond_label = gtk_label_new (_("Condition"));
361         gtk_widget_show (cond_label);
362         gtk_misc_set_alignment (GTK_MISC (cond_label), 0, 0.5);
363         gtk_box_pack_start (GTK_BOX (vbox1), cond_label, FALSE, FALSE, 0);
364
365         hbox1 = gtk_hbox_new (FALSE, VSPACING);
366         gtk_widget_show (hbox1);
367         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
368         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
369
370         cond_entry = gtk_entry_new ();
371         gtk_widget_show (cond_entry);
372         gtk_box_pack_start (GTK_BOX (hbox1), cond_entry, TRUE, TRUE, 0);
373
374         cond_btn = gtk_button_new_with_label (_("Define ..."));
375         gtk_widget_show (cond_btn);
376         gtk_box_pack_start (GTK_BOX (hbox1), cond_btn, FALSE, FALSE, 0);
377         gtk_signal_connect (GTK_OBJECT (cond_btn), "clicked",
378                             GTK_SIGNAL_FUNC (prefs_filtering_condition_define),
379                             NULL);
380
381         action_label = gtk_label_new (_("Action"));
382         gtk_widget_show (action_label);
383         gtk_misc_set_alignment (GTK_MISC (action_label), 0, 0.5);
384         gtk_box_pack_start (GTK_BOX (vbox1), action_label, FALSE, FALSE, 0);
385
386         hbox1 = gtk_hbox_new (FALSE, VSPACING);
387         gtk_widget_show (hbox1);
388         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
389         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
390
391         action_entry = gtk_entry_new ();
392         gtk_widget_show (action_entry);
393         gtk_box_pack_start (GTK_BOX (hbox1), action_entry, TRUE, TRUE, 0);
394
395         action_btn = gtk_button_new_with_label (_("Define ..."));
396         gtk_widget_show (action_btn);
397         gtk_box_pack_start (GTK_BOX (hbox1), action_btn, FALSE, FALSE, 0);
398         gtk_signal_connect (GTK_OBJECT (action_btn), "clicked",
399                             GTK_SIGNAL_FUNC (prefs_filtering_action_define),
400                             NULL);
401
402         /* register / substitute / delete */
403
404         reg_hbox = gtk_hbox_new (FALSE, 4);
405         gtk_widget_show (reg_hbox);
406         gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
407
408         arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
409         gtk_widget_show (arrow);
410         gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
411         gtk_widget_set_usize (arrow, -1, 16);
412
413         btn_hbox = gtk_hbox_new (TRUE, 4);
414         gtk_widget_show (btn_hbox);
415         gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
416
417         reg_btn = gtk_button_new_with_label (_("Add"));
418         gtk_widget_show (reg_btn);
419         gtk_box_pack_start (GTK_BOX (btn_hbox), reg_btn, FALSE, TRUE, 0);
420         gtk_signal_connect (GTK_OBJECT (reg_btn), "clicked",
421                             GTK_SIGNAL_FUNC (prefs_filtering_register_cb), NULL);
422
423         subst_btn = gtk_button_new_with_label (_("  Replace  "));
424         gtk_widget_show (subst_btn);
425         gtk_box_pack_start (GTK_BOX (btn_hbox), subst_btn, FALSE, TRUE, 0);
426         gtk_signal_connect (GTK_OBJECT (subst_btn), "clicked",
427                             GTK_SIGNAL_FUNC (prefs_filtering_substitute_cb),
428                             NULL);
429
430         del_btn = gtk_button_new_with_label (_("Delete"));
431         gtk_widget_show (del_btn);
432         gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
433         gtk_signal_connect (GTK_OBJECT (del_btn), "clicked",
434                             GTK_SIGNAL_FUNC (prefs_filtering_delete_cb), NULL);
435
436         cond_hbox = gtk_hbox_new (FALSE, 8);
437         gtk_widget_show (cond_hbox);
438         gtk_box_pack_start (GTK_BOX (vbox1), cond_hbox, TRUE, TRUE, 0);
439
440         cond_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
441         gtk_widget_show (cond_scrolledwin);
442         gtk_widget_set_usize (cond_scrolledwin, -1, 150);
443         gtk_box_pack_start (GTK_BOX (cond_hbox), cond_scrolledwin,
444                             TRUE, TRUE, 0);
445         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin),
446                                         GTK_POLICY_AUTOMATIC,
447                                         GTK_POLICY_AUTOMATIC);
448
449         title[0] = _("Current filtering/processing rules");
450         cond_clist = gtk_clist_new_with_titles(1, title);
451         gtk_widget_show (cond_clist);
452         gtk_container_add (GTK_CONTAINER (cond_scrolledwin), cond_clist);
453         gtk_clist_set_column_width (GTK_CLIST (cond_clist), 0, 80);
454         gtk_clist_set_selection_mode (GTK_CLIST (cond_clist),
455                                       GTK_SELECTION_BROWSE);
456         GTK_WIDGET_UNSET_FLAGS (GTK_CLIST (cond_clist)->column[0].button,
457                                 GTK_CAN_FOCUS);
458         gtk_signal_connect (GTK_OBJECT (cond_clist), "select_row",
459                             GTK_SIGNAL_FUNC (prefs_filtering_select), NULL);
460
461         btn_vbox = gtk_vbox_new (FALSE, 8);
462         gtk_widget_show (btn_vbox);
463         gtk_box_pack_start (GTK_BOX (cond_hbox), btn_vbox, FALSE, FALSE, 0);
464
465         up_btn = gtk_button_new_with_label (_("Up"));
466         gtk_widget_show (up_btn);
467         gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
468         gtk_signal_connect (GTK_OBJECT (up_btn), "clicked",
469                             GTK_SIGNAL_FUNC (prefs_filtering_up), NULL);
470
471         down_btn = gtk_button_new_with_label (_("Down"));
472         gtk_widget_show (down_btn);
473         gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
474         gtk_signal_connect (GTK_OBJECT (down_btn), "clicked",
475                             GTK_SIGNAL_FUNC (prefs_filtering_down), NULL);
476
477         gtk_widget_set_usize(window, 500, -1);
478
479         gtk_widget_show_all(window);
480
481         filtering.window    = window;
482         filtering.ok_btn = ok_btn;
483
484         filtering.cond_entry = cond_entry;
485         filtering.action_entry = action_entry;
486         filtering.cond_clist   = cond_clist;
487 }
488
489 static void prefs_filtering_update_hscrollbar(void)
490 {
491         gint optwidth = gtk_clist_optimal_column_width(GTK_CLIST(filtering.cond_clist), 0);
492         gtk_clist_set_column_width(GTK_CLIST(filtering.cond_clist), 0, optwidth);
493 }
494
495 void prefs_filtering_rename_path(const gchar *old_path, const gchar *new_path)
496 {
497         GList * cur;
498         gchar *paths[2] = {NULL, NULL};
499         paths[0] = (gchar*)old_path;
500         paths[1] = (gchar*)new_path;
501         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
502                 Folder *folder;
503                 folder = (Folder *) cur->data;
504                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
505                                 prefs_filtering_rename_path_func, paths);
506         }
507         prefs_filtering_rename_path_func(NULL, paths);
508 }
509
510 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
511 {
512         GSList *cur;
513         gchar *old_path, *new_path;
514         gchar *base;
515         gchar *prefix;
516         gchar *suffix;
517         gchar *dest_path;
518         gchar *old_path_with_sep;
519         gint destlen;
520         gint prefixlen;
521         gint oldpathlen;
522         FolderItem *item;
523         GSList * action_cur;
524
525         old_path = ((gchar **)data)[0];
526         new_path = ((gchar **)data)[1];
527
528         g_return_val_if_fail(old_path != NULL, FALSE);
529         g_return_val_if_fail(new_path != NULL, FALSE);
530
531         oldpathlen = strlen(old_path);
532         old_path_with_sep = g_strconcat(old_path,G_DIR_SEPARATOR_S,NULL);
533         if (node == NULL)
534                 cur = global_processing;
535         else {
536                 item = node->data;
537                 if (!item || !item->prefs) 
538                         return FALSE;
539                 cur = item->prefs->processing;
540         }
541
542
543         for (; cur != NULL; cur = cur->next) {
544                 FilteringProp   *filtering = (FilteringProp *)cur->data;
545                 
546                 for(action_cur = filtering->action_list ; action_cur != NULL ;
547                     action_cur = action_cur->next) {
548
549                         FilteringAction *action = action_cur->data;
550                         
551                         if (!action->destination) continue;
552                         
553                         destlen = strlen(action->destination);
554                         
555                         if (destlen > oldpathlen) {
556                                 prefixlen = destlen - oldpathlen;
557                                 suffix = action->destination + prefixlen;
558                                 
559                                 if (!strncmp(old_path, suffix, oldpathlen)) {
560                                         prefix = g_malloc0(prefixlen + 1);
561                                         strncpy2(prefix, action->destination, prefixlen);
562                                         
563                                         base = suffix + oldpathlen;
564                                         while (*base == G_DIR_SEPARATOR) base++;
565                                         if (*base == '\0')
566                                                 dest_path = g_strconcat(prefix,
567                                                     G_DIR_SEPARATOR_S,
568                                                     new_path, NULL);
569                                         else
570                                                 dest_path = g_strconcat(prefix,
571                                                     G_DIR_SEPARATOR_S,
572                                                     new_path,
573                                                     G_DIR_SEPARATOR_S,
574                                                     base, NULL);
575                                         
576                                         g_free(prefix);
577                                         g_free(action->destination);
578                                         action->destination = dest_path;
579                                 } else { /* for non-leaf folders */
580                                         /* compare with trailing slash */
581                                         if (!strncmp(old_path_with_sep, action->destination, oldpathlen+1)) {
582                                                 
583                                                 suffix = action->destination + oldpathlen + 1;
584                                                 dest_path = g_strconcat(new_path,
585                                                     G_DIR_SEPARATOR_S,
586                                                     suffix, NULL);
587                                                 g_free(action->destination);
588                                                 action->destination = dest_path;
589                                         }
590                                 }
591                         } else {
592                                 /* folder-moving a leaf */
593                                 if (!strcmp(old_path, action->destination)) {           
594                                         dest_path = g_strdup(new_path);
595                                         g_free(action->destination);
596                                         action->destination = dest_path;
597                                 }
598                         }
599                 }
600         }
601         
602         g_free(old_path_with_sep);
603         prefs_matcher_write_config();
604
605         return FALSE;
606 }
607
608 void prefs_filtering_delete_path(const gchar *path)
609 {
610         GList * cur;
611         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
612                 Folder *folder;
613                 folder = (Folder *) cur->data;
614                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
615                                 prefs_filtering_delete_path_func, (gchar *)path);
616         }
617         prefs_filtering_delete_path_func(NULL, (gchar *)path);
618 }
619
620 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data)
621 {
622         GSList *cur, *orig;
623         gchar *path = (gchar *)data;
624         gchar *suffix;
625         gint destlen;
626         gint prefixlen;
627         gint pathlen;
628         FolderItem *item;
629         GSList * action_cur;
630         
631         g_return_val_if_fail(path != NULL, FALSE);
632
633         pathlen = strlen(path);
634         if (node == NULL)
635                 cur = global_processing;
636         else {
637                 item = node->data;
638                 if (!item || !item->prefs)
639                         return FALSE;
640                 cur = item->prefs->processing;
641         }
642         orig = cur;
643         
644         for (; cur != NULL; cur = cur->next) {
645                 FilteringProp *filtering = (FilteringProp *)cur->data;
646                 
647                 for(action_cur = filtering->action_list ; action_cur != NULL ;
648                     action_cur = action_cur->next) {
649                 
650                         FilteringAction *action;
651                         
652                         action = action_cur->data;
653                         
654                         if (!action->destination) continue;
655                         
656                         destlen = strlen(action->destination);
657                         
658                         if (destlen > pathlen) {
659                                 prefixlen = destlen - pathlen;
660                                 suffix = action->destination + prefixlen;
661                                 
662                                 if (suffix && !strncmp(path, suffix, pathlen)) {
663                                         filteringprop_free(filtering);
664                                         orig = g_slist_remove(orig, filtering);
665                                 }
666                         } else if (strcmp(action->destination, path) == 0) {
667                                 filteringprop_free(filtering);
668                                 orig = g_slist_remove(orig, filtering);
669                                 
670                         }
671                 }
672         }                
673         
674         if (node == NULL)
675                 global_processing = orig;
676         else {
677                 item = node->data;
678                 if (!item || !item->prefs)
679                         return FALSE;
680                 item->prefs->processing = orig;
681         }
682         
683         prefs_matcher_write_config();
684
685         return FALSE;
686 }
687
688 static void prefs_filtering_set_dialog(const gchar *header, const gchar *key)
689 {
690         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
691         GSList *cur;
692         GSList * prefs_filtering;
693         gchar *cond_str[1];
694         gint row;
695         
696         gtk_clist_freeze(clist);
697         gtk_clist_clear(clist);
698
699         cond_str[0] = _("(New)");
700         row = gtk_clist_append(clist, cond_str);
701         gtk_clist_set_row_data(clist, row, NULL);
702
703         if (cur_item == NULL)
704                 prefs_filtering = global_processing;
705         else
706                 prefs_filtering = cur_item->prefs->processing;
707
708         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur)) {
709                 FilteringProp * prop = (FilteringProp *) cur->data;
710
711                 cond_str[0] = filteringprop_to_string(prop);
712                 subst_char(cond_str[0], '\t', ':');
713                 row = gtk_clist_append(clist, cond_str);
714                 gtk_clist_set_row_data(clist, row, prop);
715
716                 g_free(cond_str[0]);
717         }
718
719         prefs_filtering_update_hscrollbar();
720         gtk_clist_thaw(clist);
721
722         prefs_filtering_reset_dialog();
723
724         if (header && key) {
725                 gchar *match_str = g_strconcat(header, " ",
726                         get_matchparser_tab_str(MATCHTYPE_MATCHCASE),
727                         " \"", key, "\"", NULL);
728                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), match_str);
729                 g_free(match_str);
730         }
731 }
732
733 static void prefs_filtering_reset_dialog(void)
734 {
735         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), "");
736         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), "");
737 }
738
739 static void prefs_filtering_set_list(void)
740 {
741         gint row = 1;
742         FilteringProp *prop;
743         GSList * cur;
744         gchar * filtering_str;
745         GSList * prefs_filtering;
746
747         if (cur_item == NULL)
748                 prefs_filtering = global_processing;
749         else
750                 prefs_filtering = cur_item->prefs->processing;
751
752         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
753                 filteringprop_free((FilteringProp *) cur->data);
754         g_slist_free(prefs_filtering);
755         prefs_filtering = NULL;
756
757         while (gtk_clist_get_text(GTK_CLIST(filtering.cond_clist),
758                                   row, 0, &filtering_str)) {
759                 if (strcmp(filtering_str, _("(New)")) != 0) {
760                         prop = matcher_parser_get_filtering(filtering_str);
761                         if (prop != NULL)
762                                 prefs_filtering =
763                                         g_slist_append(prefs_filtering, prop);
764                 }
765                 row++;
766         }
767
768         if (cur_item == NULL)
769                 global_processing = prefs_filtering;
770         else
771                 cur_item->prefs->processing = prefs_filtering;
772 }
773
774 static gint prefs_filtering_clist_set_row(gint row, FilteringProp * prop)
775 {
776         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
777         gchar * str;
778         gchar *cond_str[1];
779
780         if (prop == NULL) {
781                 cond_str[0] = _("(New)");
782                 return gtk_clist_append(clist, cond_str);
783         }
784
785         str = filteringprop_to_string(prop);
786         if (str == NULL) {
787                 return -1;
788         }
789         cond_str[0] = str;
790
791         if (row < 0)
792                 row = gtk_clist_append(clist, cond_str);
793         else
794                 gtk_clist_set_text(clist, row, 0, cond_str[0]);
795         g_free(str);
796
797         return row;
798 }
799
800 static void prefs_filtering_condition_define_done(MatcherList * matchers)
801 {
802         gchar * str;
803
804         if (matchers == NULL)
805                 return;
806
807         str = matcherlist_to_string(matchers);
808
809         if (str != NULL) {
810                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), str);
811                 g_free(str);
812         }
813 }
814
815 static void prefs_filtering_condition_define(void)
816 {
817         gchar * cond_str;
818         MatcherList * matchers = NULL;
819
820         cond_str = gtk_entry_get_text(GTK_ENTRY(filtering.cond_entry));
821
822         if (*cond_str != '\0') {
823                 matchers = matcher_parser_get_cond(cond_str);
824                 if (matchers == NULL)
825                         alertpanel_error(_("Condition string is not valid."));
826         }
827
828         prefs_matcher_open(matchers, prefs_filtering_condition_define_done);
829
830         if (matchers != NULL)
831                 matcherlist_free(matchers);
832 }
833
834 static void prefs_filtering_action_define_done(GSList * action_list)
835 {
836         gchar * str;
837
838         if (action_list == NULL)
839                 return;
840
841         str = filteringaction_list_to_string(action_list);
842
843         if (str != NULL) {
844                 gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), str);
845                 g_free(str);
846         }
847 }
848
849 static void prefs_filtering_action_define(void)
850 {
851         gchar * action_str;
852         GSList * action_list = NULL;
853
854         action_str = gtk_entry_get_text(GTK_ENTRY(filtering.action_entry));
855
856         if (*action_str != '\0') {
857                 action_list = matcher_parser_get_action_list(action_str);
858                 if (action_list == NULL)
859                         alertpanel_error(_("Action string is not valid."));
860         }
861
862         prefs_filtering_action_open(action_list,
863             prefs_filtering_action_define_done);
864
865         if (action_list != NULL) {
866                 GSList * cur;
867                 for(cur = action_list ; cur != NULL ; cur = cur->next) {
868                         filteringaction_free(cur->data);
869                 }
870         }
871 }
872
873
874 /* register / substitute delete buttons */
875
876
877 static FilteringProp * prefs_filtering_dialog_to_filtering(gboolean alert)
878 {
879         MatcherList * cond;
880         gchar * cond_str;
881         gchar * action_str;
882         FilteringProp * prop;
883         FilteringAction * action;
884         gint list_id;
885         Action action_id;
886         gint action_type;
887         gint account_id;
888         gchar * destination;
889         gint labelcolor = 0;
890         GSList * action_list;
891
892         cond_str = gtk_entry_get_text(GTK_ENTRY(filtering.cond_entry));
893         if (*cond_str == '\0') {
894                 if(alert == TRUE) alertpanel_error(_("Condition string is empty."));
895                 return NULL;
896         }
897
898         action_str = gtk_entry_get_text(GTK_ENTRY(filtering.action_entry));
899         if (*action_str == '\0') {
900                 if(alert == TRUE) alertpanel_error(_("Action string is empty."));
901                 return NULL;
902         }
903
904         cond = matcher_parser_get_cond(cond_str);
905
906         if (cond == NULL) {
907                 if(alert == TRUE) alertpanel_error(_("Condition string is not valid."));
908                 return NULL;
909         }
910         
911         action_list = matcher_parser_get_action_list(action_str);
912
913         if (action_list == NULL) {
914                 if(alert == TRUE) alertpanel_error(_("Action string is not valid."));
915                 return NULL;
916         }
917
918         prop = filteringprop_new(cond, action_list);
919
920         return prop;
921 }
922
923 static void prefs_filtering_register_cb(void)
924 {
925         FilteringProp * prop;
926         
927         prop = prefs_filtering_dialog_to_filtering(TRUE);
928         if (prop == NULL)
929                 return;
930         prefs_filtering_clist_set_row(-1, prop);
931
932         filteringprop_free(prop);
933         
934         prefs_filtering_update_hscrollbar();
935 }
936
937 static void prefs_filtering_substitute_cb(void)
938 {
939         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
940         gint row;
941         FilteringProp * prop;
942         
943         if (!clist->selection) return;
944
945         row = GPOINTER_TO_INT(clist->selection->data);
946         if (row == 0) return;
947
948         prop = prefs_filtering_dialog_to_filtering(TRUE);
949         if (prop == NULL)
950                 return;
951         prefs_filtering_clist_set_row(row, prop);
952
953         filteringprop_free(prop);
954         
955         prefs_filtering_update_hscrollbar();
956 }
957
958 static void prefs_filtering_delete_cb(void)
959 {
960         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
961         gint row;
962
963         if (!clist->selection) return;
964         row = GPOINTER_TO_INT(clist->selection->data);
965         if (row == 0) return;
966
967         if (alertpanel(_("Delete rule"),
968                        _("Do you really want to delete this rule?"),
969                        _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
970                 return;
971
972         gtk_clist_remove(clist, row);
973
974         prefs_filtering_reset_dialog();
975
976         prefs_filtering_update_hscrollbar();
977 }
978
979 static void prefs_filtering_up(void)
980 {
981         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
982         gint row;
983
984         if (!clist->selection) return;
985
986         row = GPOINTER_TO_INT(clist->selection->data);
987         if (row > 1) {
988                 gtk_clist_row_move(clist, row, row - 1);
989                 if(gtk_clist_row_is_visible(clist, row - 1) != GTK_VISIBILITY_FULL) {
990                         gtk_clist_moveto(clist, row - 1, 0, 0, 0);
991                 } 
992         }
993 }
994
995 static void prefs_filtering_down(void)
996 {
997         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
998         gint row;
999
1000         if (!clist->selection) return;
1001
1002         row = GPOINTER_TO_INT(clist->selection->data);
1003         if (row > 0 && row < clist->rows - 1) {
1004                 gtk_clist_row_move(clist, row, row + 1);
1005                 if(gtk_clist_row_is_visible(clist, row + 1) != GTK_VISIBILITY_FULL) {
1006                         gtk_clist_moveto(clist, row + 1, 0, 1, 0);
1007                 } 
1008         }
1009 }
1010
1011 static void prefs_filtering_select_set(FilteringProp *prop)
1012 {
1013         FilteringAction *action;
1014         gchar *matcher_str;
1015         gint list_id;
1016         gchar *action_str;
1017
1018         prefs_filtering_reset_dialog();
1019
1020         matcher_str = matcherlist_to_string(prop->matchers);
1021         if (matcher_str == NULL) {
1022                 return;
1023         }
1024
1025         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), matcher_str);
1026
1027         action_str = filteringaction_list_to_string(prop->action_list);
1028         if (matcher_str == NULL) {
1029                 return;
1030         }
1031         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), action_str);
1032
1033         g_free(action_str);
1034         g_free(matcher_str);
1035 }
1036
1037 static void prefs_filtering_select(GtkCList *clist, gint row, gint column,
1038                                 GdkEvent *event)
1039 {
1040         FilteringProp * prop;
1041         gchar * filtering_str;
1042
1043         if (row == 0) {
1044                 prefs_filtering_reset_dialog();
1045                 return;
1046         }
1047
1048         if (!gtk_clist_get_text(GTK_CLIST(filtering.cond_clist),
1049                                 row, 0, &filtering_str))
1050                 return;
1051         
1052         prop = matcher_parser_get_filtering(filtering_str);
1053         if (prop == NULL)
1054                 return;
1055
1056         prefs_filtering_select_set(prop);
1057
1058         filteringprop_free(prop);
1059 }
1060
1061
1062 static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
1063                                  gpointer data)
1064 {
1065         prefs_filtering_cancel();
1066         return TRUE;
1067 }
1068
1069 static void prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
1070                                      gpointer data)
1071 {
1072         if (event && event->keyval == GDK_Escape)
1073                 prefs_filtering_cancel();
1074 }
1075
1076 static void prefs_filtering_ok(void)
1077 {
1078         FilteringProp * prop;
1079         gchar * str;
1080         gchar * filtering_str;
1081         gint row = 1;
1082         AlertValue val;
1083         
1084         prop = prefs_filtering_dialog_to_filtering(FALSE);
1085         if (prop != NULL) {
1086                 str = filteringprop_to_string(prop);
1087
1088                 while (gtk_clist_get_text(GTK_CLIST(filtering.cond_clist),
1089                                           row, 0, &filtering_str)) {
1090                         if (strcmp(filtering_str, str) == 0) break;
1091                         row++;
1092                 }
1093                 if (strcmp(filtering_str, str) != 0) {
1094                         val = alertpanel(_("Entry not saved"),
1095                                  _("The entry was not saved. Close anyway?"),
1096                                  _("Yes"), _("No"), NULL);
1097                         if (G_ALERTDEFAULT != val) {
1098                                 g_free(str);
1099                                 return;
1100                         }
1101                 }
1102                 g_free(str);
1103         }
1104         prefs_filtering_set_list();
1105         prefs_matcher_write_config();
1106         prefs_filtering_close();
1107 }
1108
1109 static void prefs_filtering_cancel(void)
1110 {
1111         prefs_matcher_read_config();
1112         prefs_filtering_close();
1113 }