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