0.9.4claws65
[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 void 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 FolderItem * cur_item = NULL; /* folder (if dialog opened for processing) */
105
106 typedef enum Action_ {
107         ACTION_MOVE,
108         ACTION_COPY,
109         ACTION_DELETE,
110         ACTION_MARK,
111         ACTION_UNMARK,
112         ACTION_LOCK,
113         ACTION_UNLOCK,
114         ACTION_MARK_AS_READ,
115         ACTION_MARK_AS_UNREAD,
116         ACTION_FORWARD,
117         ACTION_FORWARD_AS_ATTACHMENT,
118         ACTION_REDIRECT,
119         ACTION_EXECUTE,
120         ACTION_COLOR,
121         /* add other action constants */
122 } Action;
123
124 void prefs_filtering_open(FolderItem * item,
125                           const gchar *header,
126                           const gchar *key)
127 {
128         gchar *esckey;
129
130         if (prefs_rc_is_readonly(FILTERING_RC))
131                 return;
132
133         inc_lock();
134
135         if (!filtering.window) {
136                 prefs_filtering_create();
137         }
138
139         manage_window_set_transient(GTK_WINDOW(filtering.window));
140         gtk_widget_grab_focus(filtering.ok_btn);
141
142         cur_item = item;
143
144         esckey = matcher_escape_str(key);
145         prefs_filtering_set_dialog(header, esckey);
146         g_free(esckey);
147
148         gtk_widget_show(filtering.window);
149
150         start_address_completion();
151 }
152
153 /* prefs_filtering_close() - just to have one common exit point */
154 static void prefs_filtering_close(void)
155 {
156         end_address_completion();
157         
158         gtk_widget_hide(filtering.window);
159         inc_unlock();
160 }
161
162 static void prefs_filtering_create(void)
163 {
164         GtkWidget *window;
165         GtkWidget *vbox;
166         GtkWidget *ok_btn;
167         GtkWidget *cancel_btn;
168         GtkWidget *confirm_area;
169
170         GtkWidget *vbox1;
171         GtkWidget *hbox1;
172         GtkWidget *reg_hbox;
173         GtkWidget *arrow;
174         GtkWidget *btn_hbox;
175
176         GtkWidget *cond_label;
177         GtkWidget *cond_entry;
178         GtkWidget *cond_btn;
179         GtkWidget *action_label;
180         GtkWidget *action_entry;
181         GtkWidget *action_btn;
182
183         GtkWidget *reg_btn;
184         GtkWidget *subst_btn;
185         GtkWidget *del_btn;
186
187         GtkWidget *cond_hbox;
188         GtkWidget *cond_scrolledwin;
189         GtkWidget *cond_clist;
190
191         GtkWidget *btn_vbox;
192         GtkWidget *spc_vbox;
193         GtkWidget *top_btn;
194         GtkWidget *up_btn;
195         GtkWidget *down_btn;
196         GtkWidget *bottom_btn;
197
198         gchar *title[1];
199
200         debug_print("Creating filtering configuration window...\n");
201
202         window = gtk_window_new (GTK_WINDOW_DIALOG);
203         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
204         gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
205         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
206         gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, FALSE);
207
208         vbox = gtk_vbox_new (FALSE, 6);
209         gtk_widget_show (vbox);
210         gtk_container_add (GTK_CONTAINER (window), vbox);
211
212         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
213                                 &cancel_btn, _("Cancel"), NULL, NULL);
214         gtk_widget_show (confirm_area);
215         gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
216         gtk_widget_grab_default (ok_btn);
217
218         gtk_window_set_title (GTK_WINDOW(window),
219                                     _("Filtering/Processing configuration"));
220
221         gtk_signal_connect (GTK_OBJECT(window), "delete_event",
222                             GTK_SIGNAL_FUNC(prefs_filtering_deleted), NULL);
223         gtk_signal_connect (GTK_OBJECT(window), "key_press_event",
224                             GTK_SIGNAL_FUNC(prefs_filtering_key_pressed), NULL);
225         MANAGE_WINDOW_SIGNALS_CONNECT (window);
226         gtk_signal_connect (GTK_OBJECT(ok_btn), "clicked",
227                             GTK_SIGNAL_FUNC(prefs_filtering_ok), NULL);
228         gtk_signal_connect (GTK_OBJECT(cancel_btn), "clicked",
229                             GTK_SIGNAL_FUNC(prefs_filtering_cancel), NULL);
230
231         vbox1 = gtk_vbox_new (FALSE, VSPACING);
232         gtk_widget_show (vbox1);
233         gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
234         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
235
236         cond_label = gtk_label_new (_("Condition"));
237         gtk_widget_show (cond_label);
238         gtk_misc_set_alignment (GTK_MISC (cond_label), 0, 0.5);
239         gtk_box_pack_start (GTK_BOX (vbox1), cond_label, FALSE, FALSE, 0);
240
241         hbox1 = gtk_hbox_new (FALSE, VSPACING);
242         gtk_widget_show (hbox1);
243         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
244         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
245
246         cond_entry = gtk_entry_new ();
247         gtk_widget_show (cond_entry);
248         gtk_box_pack_start (GTK_BOX (hbox1), cond_entry, TRUE, TRUE, 0);
249
250         cond_btn = gtk_button_new_with_label (_("Define ..."));
251         gtk_widget_show (cond_btn);
252         gtk_box_pack_start (GTK_BOX (hbox1), cond_btn, FALSE, FALSE, 0);
253         gtk_signal_connect (GTK_OBJECT (cond_btn), "clicked",
254                             GTK_SIGNAL_FUNC (prefs_filtering_condition_define),
255                             NULL);
256
257         action_label = gtk_label_new (_("Action"));
258         gtk_widget_show (action_label);
259         gtk_misc_set_alignment (GTK_MISC (action_label), 0, 0.5);
260         gtk_box_pack_start (GTK_BOX (vbox1), action_label, FALSE, FALSE, 0);
261
262         hbox1 = gtk_hbox_new (FALSE, VSPACING);
263         gtk_widget_show (hbox1);
264         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
265         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
266
267         action_entry = gtk_entry_new ();
268         gtk_widget_show (action_entry);
269         gtk_box_pack_start (GTK_BOX (hbox1), action_entry, TRUE, TRUE, 0);
270
271         action_btn = gtk_button_new_with_label (_("Define ..."));
272         gtk_widget_show (action_btn);
273         gtk_box_pack_start (GTK_BOX (hbox1), action_btn, FALSE, FALSE, 0);
274         gtk_signal_connect (GTK_OBJECT (action_btn), "clicked",
275                             GTK_SIGNAL_FUNC (prefs_filtering_action_define),
276                             NULL);
277
278         /* register / substitute / delete */
279
280         reg_hbox = gtk_hbox_new (FALSE, 4);
281         gtk_widget_show (reg_hbox);
282         gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
283
284         arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
285         gtk_widget_show (arrow);
286         gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
287         gtk_widget_set_usize (arrow, -1, 16);
288
289         btn_hbox = gtk_hbox_new (TRUE, 4);
290         gtk_widget_show (btn_hbox);
291         gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
292
293         reg_btn = gtk_button_new_with_label (_("Add"));
294         gtk_widget_show (reg_btn);
295         gtk_box_pack_start (GTK_BOX (btn_hbox), reg_btn, FALSE, TRUE, 0);
296         gtk_signal_connect (GTK_OBJECT (reg_btn), "clicked",
297                             GTK_SIGNAL_FUNC (prefs_filtering_register_cb), NULL);
298
299         subst_btn = gtk_button_new_with_label (_("  Replace  "));
300         gtk_widget_show (subst_btn);
301         gtk_box_pack_start (GTK_BOX (btn_hbox), subst_btn, FALSE, TRUE, 0);
302         gtk_signal_connect (GTK_OBJECT (subst_btn), "clicked",
303                             GTK_SIGNAL_FUNC (prefs_filtering_substitute_cb),
304                             NULL);
305
306         del_btn = gtk_button_new_with_label (_("Delete"));
307         gtk_widget_show (del_btn);
308         gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
309         gtk_signal_connect (GTK_OBJECT (del_btn), "clicked",
310                             GTK_SIGNAL_FUNC (prefs_filtering_delete_cb), NULL);
311
312         cond_hbox = gtk_hbox_new (FALSE, 8);
313         gtk_widget_show (cond_hbox);
314         gtk_box_pack_start (GTK_BOX (vbox1), cond_hbox, TRUE, TRUE, 0);
315
316         cond_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
317         gtk_widget_show (cond_scrolledwin);
318         gtk_widget_set_usize (cond_scrolledwin, -1, 150);
319         gtk_box_pack_start (GTK_BOX (cond_hbox), cond_scrolledwin,
320                             TRUE, TRUE, 0);
321         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin),
322                                         GTK_POLICY_AUTOMATIC,
323                                         GTK_POLICY_AUTOMATIC);
324
325         title[0] = _("Current filtering/processing rules");
326         cond_clist = gtk_clist_new_with_titles(1, title);
327         gtk_widget_show (cond_clist);
328         gtk_container_add (GTK_CONTAINER (cond_scrolledwin), cond_clist);
329         gtk_clist_set_column_width (GTK_CLIST (cond_clist), 0, 80);
330         gtk_clist_set_selection_mode (GTK_CLIST (cond_clist),
331                                       GTK_SELECTION_BROWSE);
332         GTK_WIDGET_UNSET_FLAGS (GTK_CLIST (cond_clist)->column[0].button,
333                                 GTK_CAN_FOCUS);
334         gtk_signal_connect (GTK_OBJECT (cond_clist), "select_row",
335                             GTK_SIGNAL_FUNC (prefs_filtering_select), NULL);
336
337         btn_vbox = gtk_vbox_new (FALSE, 8);
338         gtk_widget_show (btn_vbox);
339         gtk_box_pack_start (GTK_BOX (cond_hbox), btn_vbox, FALSE, FALSE, 0);
340
341         top_btn = gtk_button_new_with_label (_("Top"));
342         gtk_widget_show (top_btn);
343         gtk_box_pack_start (GTK_BOX (btn_vbox), top_btn, FALSE, FALSE, 0);
344         gtk_signal_connect (GTK_OBJECT (top_btn), "clicked",
345                             GTK_SIGNAL_FUNC (prefs_filtering_top), NULL);
346
347         PACK_VSPACER (btn_vbox, spc_vbox, VSPACING_NARROW_2);
348
349         up_btn = gtk_button_new_with_label (_("Up"));
350         gtk_widget_show (up_btn);
351         gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
352         gtk_signal_connect (GTK_OBJECT (up_btn), "clicked",
353                             GTK_SIGNAL_FUNC (prefs_filtering_up), NULL);
354
355         down_btn = gtk_button_new_with_label (_("Down"));
356         gtk_widget_show (down_btn);
357         gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
358         gtk_signal_connect (GTK_OBJECT (down_btn), "clicked",
359                             GTK_SIGNAL_FUNC (prefs_filtering_down), NULL);
360
361         PACK_VSPACER (btn_vbox, spc_vbox, VSPACING_NARROW_2);
362
363         bottom_btn = gtk_button_new_with_label (_("Bottom"));
364         gtk_widget_show (bottom_btn);
365         gtk_box_pack_start (GTK_BOX (btn_vbox), bottom_btn, FALSE, FALSE, 0);
366         gtk_signal_connect (GTK_OBJECT (bottom_btn), "clicked",
367                             GTK_SIGNAL_FUNC (prefs_filtering_bottom), NULL);
368
369         gtk_widget_set_usize(window, 500, -1);
370
371         gtk_widget_show_all(window);
372
373         filtering.window    = window;
374         filtering.ok_btn = ok_btn;
375
376         filtering.cond_entry = cond_entry;
377         filtering.action_entry = action_entry;
378         filtering.cond_clist   = cond_clist;
379 }
380
381 static void prefs_filtering_update_hscrollbar(void)
382 {
383         gint optwidth = gtk_clist_optimal_column_width(GTK_CLIST(filtering.cond_clist), 0);
384         gtk_clist_set_column_width(GTK_CLIST(filtering.cond_clist), 0, optwidth);
385 }
386
387 void prefs_filtering_rename_path(const gchar *old_path, const gchar *new_path)
388 {
389         GList * cur;
390         gchar *paths[2] = {NULL, NULL};
391         paths[0] = (gchar*)old_path;
392         paths[1] = (gchar*)new_path;
393         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
394                 Folder *folder;
395                 folder = (Folder *) cur->data;
396                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
397                                 prefs_filtering_rename_path_func, paths);
398         }
399         prefs_filtering_rename_path_func(NULL, paths);
400 }
401
402 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
403 {
404         GSList *cur;
405         gchar *old_path, *new_path;
406         gchar *base;
407         gchar *prefix;
408         gchar *suffix;
409         gchar *dest_path;
410         gchar *old_path_with_sep;
411         gint destlen;
412         gint prefixlen;
413         gint oldpathlen;
414         FolderItem *item;
415         GSList * action_cur;
416
417         old_path = ((gchar **)data)[0];
418         new_path = ((gchar **)data)[1];
419
420         g_return_val_if_fail(old_path != NULL, FALSE);
421         g_return_val_if_fail(new_path != NULL, FALSE);
422
423         oldpathlen = strlen(old_path);
424         old_path_with_sep = g_strconcat(old_path,G_DIR_SEPARATOR_S,NULL);
425         if (node == NULL)
426                 cur = global_processing;
427         else {
428                 item = node->data;
429                 if (!item || !item->prefs) 
430                         return FALSE;
431                 cur = item->prefs->processing;
432         }
433
434
435         for (; cur != NULL; cur = cur->next) {
436                 FilteringProp   *filtering = (FilteringProp *)cur->data;
437                 
438                 for(action_cur = filtering->action_list ; action_cur != NULL ;
439                     action_cur = action_cur->next) {
440
441                         FilteringAction *action = action_cur->data;
442                         
443                         if (!action->destination) continue;
444                         
445                         destlen = strlen(action->destination);
446                         
447                         if (destlen > oldpathlen) {
448                                 prefixlen = destlen - oldpathlen;
449                                 suffix = action->destination + prefixlen;
450                                 
451                                 if (!strncmp(old_path, suffix, oldpathlen)) {
452                                         prefix = g_malloc0(prefixlen + 1);
453                                         strncpy2(prefix, action->destination, prefixlen);
454                                         
455                                         base = suffix + oldpathlen;
456                                         while (*base == G_DIR_SEPARATOR) base++;
457                                         if (*base == '\0')
458                                                 dest_path = g_strconcat(prefix,
459                                                     G_DIR_SEPARATOR_S,
460                                                     new_path, NULL);
461                                         else
462                                                 dest_path = g_strconcat(prefix,
463                                                     G_DIR_SEPARATOR_S,
464                                                     new_path,
465                                                     G_DIR_SEPARATOR_S,
466                                                     base, NULL);
467                                         
468                                         g_free(prefix);
469                                         g_free(action->destination);
470                                         action->destination = dest_path;
471                                 } else { /* for non-leaf folders */
472                                         /* compare with trailing slash */
473                                         if (!strncmp(old_path_with_sep, action->destination, oldpathlen+1)) {
474                                                 
475                                                 suffix = action->destination + oldpathlen + 1;
476                                                 dest_path = g_strconcat(new_path,
477                                                     G_DIR_SEPARATOR_S,
478                                                     suffix, NULL);
479                                                 g_free(action->destination);
480                                                 action->destination = dest_path;
481                                         }
482                                 }
483                         } else {
484                                 /* folder-moving a leaf */
485                                 if (!strcmp(old_path, action->destination)) {           
486                                         dest_path = g_strdup(new_path);
487                                         g_free(action->destination);
488                                         action->destination = dest_path;
489                                 }
490                         }
491                 }
492         }
493         
494         g_free(old_path_with_sep);
495         prefs_matcher_write_config();
496
497         return FALSE;
498 }
499
500 void prefs_filtering_delete_path(const gchar *path)
501 {
502         GList * cur;
503         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
504                 Folder *folder;
505                 folder = (Folder *) cur->data;
506                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
507                                 prefs_filtering_delete_path_func, (gchar *)path);
508         }
509         prefs_filtering_delete_path_func(NULL, (gchar *)path);
510 }
511
512 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data)
513 {
514         GSList *cur, *filters, *duplist;
515         gchar *path = (gchar *) data;
516         gchar *suffix;
517         gint destlen;
518         gint prefixlen;
519         gint pathlen;
520         FolderItem *item;
521         GSList * action_cur;
522         
523         g_return_val_if_fail(path != NULL, FALSE);
524
525         pathlen = strlen(path);
526         if (node == NULL)
527                 filters = global_processing;
528         else {
529                 item = node->data;
530                 if (!item || !item->prefs)
531                         return FALSE;
532                 filters = item->prefs->processing;
533         }
534
535         duplist = g_slist_copy(filters);
536         for (cur = duplist ; cur != NULL; cur = g_slist_next(cur)) {
537                 FilteringProp *filtering = (FilteringProp *) cur->data;
538                 
539                 for(action_cur = filtering->action_list ; action_cur != NULL ;
540                     action_cur = action_cur->next) {
541                 
542                         FilteringAction *action;
543                         
544                         action = action_cur->data;
545                         
546                         if (!action->destination) continue;
547                         
548                         destlen = strlen(action->destination);
549                         
550                         if (destlen > pathlen) {
551                                 prefixlen = destlen - pathlen;
552                                 suffix = action->destination + prefixlen;
553                                 
554                                 if (suffix && !strncmp(path, suffix, pathlen)) {
555                                         filteringprop_free(filtering);
556                                         filters = g_slist_remove(filters, filtering);
557                                 }
558                         } else if (strcmp(action->destination, path) == 0) {
559                                 filteringprop_free(filtering);
560                                 filters = g_slist_remove(filters, filtering);
561                         }
562                 }
563         }                
564         g_slist_free(duplist);
565
566         if (node == NULL)
567                 global_processing = filters;
568         else {
569                 item = node->data;
570                 if (!item || !item->prefs)
571                         return FALSE;
572                 item->prefs->processing = filters;
573         }
574         
575         prefs_matcher_write_config();
576
577         return FALSE;
578 }
579
580 static void prefs_filtering_set_dialog(const gchar *header, const gchar *key)
581 {
582         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
583         GSList *cur;
584         GSList * prefs_filtering;
585         gchar *cond_str[1];
586         gint row;
587         
588         gtk_clist_freeze(clist);
589         gtk_clist_clear(clist);
590
591         cond_str[0] = _("(New)");
592         row = gtk_clist_append(clist, cond_str);
593         gtk_clist_set_row_data(clist, row, NULL);
594
595         if (cur_item == NULL)
596                 prefs_filtering = global_processing;
597         else
598                 prefs_filtering = cur_item->prefs->processing;
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 *match_str = g_strconcat(header, " ",
618                         get_matchparser_tab_str(MATCHTYPE_MATCHCASE),
619                         " \"", key, "\"", NULL);
620                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), match_str);
621                 g_free(match_str);
622         }
623 }
624
625 static void prefs_filtering_reset_dialog(void)
626 {
627         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), "");
628         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), "");
629 }
630
631 static void prefs_filtering_set_list(void)
632 {
633         gint row = 1;
634         FilteringProp *prop;
635         GSList * cur;
636         gchar * filtering_str;
637         GSList * prefs_filtering;
638
639         if (cur_item == NULL)
640                 prefs_filtering = global_processing;
641         else
642                 prefs_filtering = cur_item->prefs->processing;
643
644         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
645                 filteringprop_free((FilteringProp *) cur->data);
646         g_slist_free(prefs_filtering);
647         prefs_filtering = NULL;
648
649         while (gtk_clist_get_text(GTK_CLIST(filtering.cond_clist),
650                                   row, 0, &filtering_str)) {
651                 if (strcmp(filtering_str, _("(New)")) != 0) {
652                         prop = matcher_parser_get_filtering(filtering_str);
653                         if (prop != NULL)
654                                 prefs_filtering =
655                                         g_slist_append(prefs_filtering, prop);
656                 }
657                 row++;
658         }
659
660         if (cur_item == NULL)
661                 global_processing = prefs_filtering;
662         else
663                 cur_item->prefs->processing = 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 void prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
975                                      gpointer data)
976 {
977         if (event && event->keyval == GDK_Escape)
978                 prefs_filtering_cancel();
979 }
980
981 static void prefs_filtering_ok(void)
982 {
983         FilteringProp * prop;
984         gchar * str;
985         gchar * filtering_str;
986         gint row = 1;
987         AlertValue val;
988         
989         prop = prefs_filtering_dialog_to_filtering(FALSE);
990         if (prop != NULL) {
991                 str = filteringprop_to_string(prop);
992
993                 while (gtk_clist_get_text(GTK_CLIST(filtering.cond_clist),
994                                           row, 0, &filtering_str)) {
995                         if (strcmp(filtering_str, str) == 0) break;
996                         row++;
997                 }
998                 if (strcmp(filtering_str, str) != 0) {
999                         val = alertpanel(_("Entry not saved"),
1000                                  _("The entry was not saved. Close anyway?"),
1001                                  _("Yes"), _("No"), NULL);
1002                         if (G_ALERTDEFAULT != val) {
1003                                 g_free(str);
1004                                 return;
1005                         }
1006                 }
1007                 g_free(str);
1008         }
1009         prefs_filtering_set_list();
1010         prefs_matcher_write_config();
1011         prefs_filtering_close();
1012 }
1013
1014 static void prefs_filtering_cancel(void)
1015 {
1016         prefs_matcher_read_config();
1017         prefs_filtering_close();
1018 }