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