f1c7b0a97f28cbb2c92a97e0265e7d86577b7f33
[claws.git] / src / prefs_filtering.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <gtk/gtkoptionmenu.h>
30 #include <gdk/gdkkeysyms.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35
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 #include "manual.h"
53 #include "combobox.h"
54
55 #include "matcher_parser.h"
56 #include "matcher.h"
57 #include "prefs_filtering_action.h"
58
59 enum {
60         PREFS_FILTERING_ENABLED,
61         PREFS_FILTERING_NAME,
62         PREFS_FILTERING_ACCOUNT_ID,
63         PREFS_FILTERING_ACCOUNT_NAME,
64         PREFS_FILTERING_RULE,
65         PREFS_FILTERING_PROP,
66         N_PREFS_FILTERING_COLUMNS
67 };
68
69 struct _Filtering {
70         GtkWidget *window;
71
72         GtkWidget *help_btn;
73         GtkWidget *ok_btn;
74         GtkWidget *name_entry;
75         GtkWidget *account_label;
76         GtkWidget *account_combobox;
77         GtkListStore *account_combobox_list;
78         GtkWidget *cond_entry;
79         GtkWidget *action_entry;
80
81         GtkWidget *cond_list_view;
82
83         GtkTreeViewColumn *account_name_column;
84 };
85
86 typedef struct _Filtering Filtering;
87
88 static Filtering  filtering;
89
90 static GSList ** p_processing_list = NULL;
91
92 /* widget creating functions */
93 static void prefs_filtering_create              (void);
94
95 static void prefs_filtering_set_dialog  (const gchar *header,
96                                          const gchar *key);
97 static void prefs_filtering_set_list    (void);
98
99 /* callback functions */
100 static void prefs_filtering_register_cb (void);
101 static void prefs_filtering_substitute_cb       (void);
102 static void prefs_filtering_delete_cb   (void);
103 static void prefs_filtering_top         (void);
104 static void prefs_filtering_up          (void);
105 static void prefs_filtering_down        (void);
106 static void prefs_filtering_bottom      (void);
107 static gint prefs_filtering_deleted     (GtkWidget      *widget,
108                                          GdkEventAny    *event,
109                                          gpointer        data);
110 static gboolean prefs_filtering_key_pressed(GtkWidget   *widget,
111                                          GdkEventKey    *event,
112                                          gpointer        data);
113 static void prefs_filtering_cancel      (void);
114 static void prefs_filtering_ok          (void);
115
116 static void prefs_filtering_condition_define    (void);
117 static void prefs_filtering_action_define(void);
118 static gint prefs_filtering_list_view_set_row   (gint row, FilteringProp * prop);
119                                           
120 static void prefs_filtering_reset_dialog        (void);
121 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data);
122 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data);
123
124 static void delete_path(GSList ** p_filters, const gchar * path);
125
126
127 static GtkListStore* prefs_filtering_create_data_store  (void);
128 static gint prefs_filtering_list_view_insert_rule       (GtkListStore *list_store,
129                                                          gint row,
130                                                          gboolean enabled,
131                                                          const gchar *name,
132                                                          gint account_id,
133                                                          const gchar *account_name,
134                                                          const gchar *rule, 
135                                                          gboolean prop);
136 static gchar *prefs_filtering_list_view_get_rule        (GtkWidget *list, 
137                                                          gint row);
138 static void prefs_filtering_list_view_get_rule_name     (GtkWidget *list, 
139                                                          gint row,
140                                                          gboolean *enabled,
141                                                          gchar **name,
142                                                          gint *account_id);
143
144 static GtkWidget *prefs_filtering_list_view_create      (void);
145 static void prefs_filtering_create_list_view_columns    (GtkWidget *list_view);
146 static gint prefs_filtering_get_selected_row            (GtkWidget *list_view);
147 static gboolean prefs_filtering_list_view_select_row    (GtkWidget *list, gint row);
148
149 static gboolean prefs_filtering_selected                (GtkTreeSelection *selector,
150                                                          GtkTreeModel *model, 
151                                                          GtkTreePath *path,
152                                                          gboolean currently_selected,
153                                                          gpointer data);
154
155 static void prefs_filtering_account_option_menu_populate(void);
156
157 static gulong signal_id = 0; /* filtering.help_btn clicked signal */
158
159 void prefs_filtering_open(GSList ** p_processing,
160                           const gchar *title,
161                           const gchar *help_url_anchor,
162                           const gchar *header,
163                           const gchar *key,
164                           gboolean per_account_filtering)
165 {
166         if (prefs_rc_is_readonly(FILTERING_RC))
167                 return;
168
169         inc_lock();
170
171         if (!filtering.window) {
172                 prefs_filtering_create();
173         } else {
174                 gtk_list_store_clear(filtering.account_combobox_list);
175                 prefs_filtering_account_option_menu_populate();
176         }
177
178         gtk_tree_view_column_set_visible(filtering.account_name_column,
179                                                                          per_account_filtering);
180
181         manage_window_set_transient(GTK_WINDOW(filtering.window));
182         gtk_widget_grab_focus(filtering.ok_btn);
183         
184         if (title != NULL)
185                 gtk_window_set_title(GTK_WINDOW(filtering.window), title);
186         else
187                 gtk_window_set_title (GTK_WINDOW(filtering.window),
188                                       _("Filtering/Processing configuration"));
189
190         if (help_url_anchor != NULL) {
191                 if (signal_id != 0) {
192                         g_signal_handler_disconnect(
193                                         G_OBJECT(filtering.help_btn),
194                                         signal_id);
195                 }
196
197                 signal_id = g_signal_connect(G_OBJECT(filtering.help_btn),
198                                 "clicked",
199                                 G_CALLBACK(manual_open_with_anchor_cb),
200                                 (gchar*)help_url_anchor);
201         }
202         else {
203                 gtk_widget_set_sensitive(filtering.help_btn, FALSE);
204         }
205
206         p_processing_list = p_processing;
207         
208         prefs_filtering_set_dialog(header, key);
209         if (per_account_filtering) {
210                 gtk_widget_show(filtering.account_label);
211                 gtk_widget_show(filtering.account_combobox);
212         } else {
213                 gtk_widget_hide(filtering.account_label);
214                 gtk_widget_hide(filtering.account_combobox);
215                 combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
216         }
217
218         gtk_widget_show(filtering.window);
219
220         start_address_completion(NULL);
221 }
222
223 static void prefs_filtering_size_allocate_cb(GtkWidget *widget,
224                                          GtkAllocation *allocation)
225 {
226         g_return_if_fail(allocation != NULL);
227
228         prefs_common.filteringwin_width = allocation->width;
229         prefs_common.filteringwin_height = allocation->height;
230 }
231
232 /* prefs_filtering_close() - just to have one common exit point */
233 static void prefs_filtering_close(void)
234 {
235         end_address_completion();
236         
237         gtk_widget_hide(filtering.window);
238         inc_unlock();
239 }
240
241 static void prefs_filtering_account_option_menu_populate(void)
242 {
243         GList *accounts = NULL;
244         GtkTreeIter iter;
245
246         accounts = account_get_list();
247
248         g_return_if_fail(accounts != NULL);
249
250         COMBOBOX_ADD(filtering.account_combobox_list, Q_("Filtering Account Menu|All"), 0);
251         for (; accounts != NULL; accounts = accounts->next) {
252                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
253
254                 COMBOBOX_ADD(filtering.account_combobox_list, ac->account_name, ac->account_id);
255         }
256 }
257
258 static GtkWidget *prefs_filtering_account_option_menu(Filtering *filtering)
259 {
260         GtkWidget *optmenu = NULL;
261         GtkWidget *optmenubox = NULL;
262         GtkListStore *menu = NULL;
263         
264         optmenubox = gtk_event_box_new();
265         optmenu = gtkut_sc_combobox_create(optmenubox, FALSE);
266         menu = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(optmenu)));
267
268         filtering->account_combobox = optmenu;
269         filtering->account_combobox_list = menu;
270
271         prefs_filtering_account_option_menu_populate();
272
273         return optmenubox;
274 }
275
276 static void prefs_filtering_create(void)
277 {
278         GtkWidget *window;
279         GtkWidget *vbox;
280         GtkWidget *help_btn;
281         GtkWidget *cancel_btn;
282         GtkWidget *ok_btn;
283         GtkWidget *confirm_area;
284
285         GtkWidget *vbox1;
286         GtkWidget *reg_hbox;
287         GtkWidget *arrow;
288         GtkWidget *btn_hbox;
289
290         GtkWidget *name_label;
291         GtkWidget *name_entry;
292         GtkWidget *account_label;
293         GtkWidget *account_opt_menu;
294         GtkWidget *cond_label;
295         GtkWidget *cond_entry;
296         GtkWidget *cond_btn;
297         GtkWidget *action_label;
298         GtkWidget *action_entry;
299         GtkWidget *action_btn;
300
301         GtkWidget *reg_btn;
302         GtkWidget *subst_btn;
303         GtkWidget *del_btn;
304
305         GtkWidget *cond_hbox;
306         GtkWidget *cond_scrolledwin;
307         GtkWidget *cond_list_view;
308
309         GtkWidget *btn_vbox;
310         GtkWidget *spc_vbox;
311         GtkWidget *top_btn;
312         GtkWidget *up_btn;
313         GtkWidget *down_btn;
314         GtkWidget *bottom_btn;
315         GtkWidget *table;
316         static GdkGeometry geometry;
317
318         debug_print("Creating filtering configuration window...\n");
319
320         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
321         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
322         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
323         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
324         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
325
326         vbox = gtk_vbox_new (FALSE, 6);
327         gtk_widget_show (vbox);
328         gtk_container_add (GTK_CONTAINER (window), vbox);
329
330         gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
331                         &cancel_btn, GTK_STOCK_CANCEL,
332                         &ok_btn, GTK_STOCK_OK,
333                         NULL, NULL);
334         gtk_widget_show (confirm_area);
335         gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
336         gtk_widget_grab_default (ok_btn);
337
338         gtk_window_set_title (GTK_WINDOW(window),
339                                     _("Filtering/Processing configuration"));
340
341         g_signal_connect(G_OBJECT(window), "delete_event",
342                          G_CALLBACK(prefs_filtering_deleted), NULL);
343         g_signal_connect(G_OBJECT(window), "size_allocate",
344                          G_CALLBACK(prefs_filtering_size_allocate_cb), NULL);
345         g_signal_connect(G_OBJECT(window), "key_press_event",
346                          G_CALLBACK(prefs_filtering_key_pressed), NULL);
347         MANAGE_WINDOW_SIGNALS_CONNECT (window);
348         g_signal_connect(G_OBJECT(ok_btn), "clicked",
349                          G_CALLBACK(prefs_filtering_ok), NULL);
350         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
351                          G_CALLBACK(prefs_filtering_cancel), NULL);
352
353         vbox1 = gtk_vbox_new (FALSE, VSPACING);
354         gtk_widget_show (vbox1);
355         gtk_box_pack_start (GTK_BOX (vbox), vbox1, FALSE, TRUE, 0);
356         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
357
358         table = gtk_table_new(3, 4, FALSE);
359         gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
360         gtk_table_set_col_spacings (GTK_TABLE (table), 4);
361         gtk_table_set_col_spacing (GTK_TABLE (table), 2, 0);
362         gtk_widget_show(table);
363         gtk_box_pack_start (GTK_BOX (vbox1), table, TRUE, TRUE, 0);
364
365         name_label = gtk_label_new (_("Name"));
366         gtk_widget_show (name_label);
367         gtk_misc_set_alignment (GTK_MISC (name_label), 1, 0.5);
368         gtk_table_attach (GTK_TABLE (table), name_label, 0, 1, 0, 1,
369                           (GtkAttachOptions) (GTK_FILL),
370                           (GtkAttachOptions) (0), 0, 0);
371
372         name_entry = gtk_entry_new ();
373         gtk_widget_show (name_entry);
374         gtk_table_attach (GTK_TABLE (table), name_entry, 1, 2, 0, 1,
375                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
376                           (GtkAttachOptions) (0), 0, 0);
377
378         account_label = gtk_label_new (_("Account"));
379         gtk_widget_show (account_label);
380         gtk_misc_set_alignment (GTK_MISC (account_label), 1, 0.5);
381         gtk_table_attach (GTK_TABLE (table), account_label, 0, 1, 1, 2,
382                           (GtkAttachOptions) (GTK_FILL),
383                           (GtkAttachOptions) (0), 0, 0);
384
385         account_opt_menu = prefs_filtering_account_option_menu(&filtering);
386         gtk_widget_show (account_opt_menu);
387         gtk_table_attach (GTK_TABLE (table), account_opt_menu, 1, 2, 1, 2,
388                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
389                           (GtkAttachOptions) (0), 0, 0);
390         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
391
392         cond_label = gtk_label_new (_("Condition"));
393         gtk_widget_show (cond_label);
394         gtk_misc_set_alignment (GTK_MISC (cond_label), 1, 0.5);
395         gtk_table_attach (GTK_TABLE (table), cond_label, 0, 1, 2, 3,
396                           (GtkAttachOptions) (GTK_FILL),
397                           (GtkAttachOptions) (0), 0, 0);
398
399         cond_entry = gtk_entry_new ();
400         gtk_widget_show (cond_entry);
401         gtk_table_attach (GTK_TABLE (table), cond_entry, 1, 2, 2, 3,
402                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
403                           (GtkAttachOptions) (0), 0, 0);
404
405         cond_btn = gtk_button_new_with_label (_(" Define... "));
406         gtk_widget_show (cond_btn);
407         gtk_table_attach (GTK_TABLE (table), cond_btn, 2, 3, 2, 3,
408                           (GtkAttachOptions) (GTK_FILL),
409                           (GtkAttachOptions) (0), 2, 2);
410         g_signal_connect(G_OBJECT (cond_btn), "clicked",
411                          G_CALLBACK(prefs_filtering_condition_define),
412                          NULL);
413
414         action_label = gtk_label_new (_("Action"));
415         gtk_widget_show (action_label);
416         gtk_misc_set_alignment (GTK_MISC (action_label), 1, 0.5);
417         gtk_table_attach (GTK_TABLE (table), action_label, 0, 1, 3, 4,
418                           (GtkAttachOptions) (GTK_FILL),
419                           (GtkAttachOptions) (0), 0, 0);
420
421         action_entry = gtk_entry_new ();
422         gtk_widget_show (action_entry);
423         gtk_table_attach (GTK_TABLE (table), action_entry, 1, 2, 3, 4,
424                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
425                           (GtkAttachOptions) (0), 0, 0);
426
427         action_btn = gtk_button_new_with_label (_(" Define... "));
428         gtk_widget_show (action_btn);
429         gtk_table_attach (GTK_TABLE (table), action_btn, 2, 3, 3, 4,
430                           (GtkAttachOptions) (GTK_FILL),
431                           (GtkAttachOptions) (0), 2, 2);
432         g_signal_connect(G_OBJECT (action_btn), "clicked",
433                          G_CALLBACK(prefs_filtering_action_define),
434                          NULL);
435                          
436         /* register / substitute / delete */
437         reg_hbox = gtk_hbox_new (FALSE, 4);
438         gtk_widget_show (reg_hbox);
439         gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
440
441         arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
442         gtk_widget_show (arrow);
443         gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
444         gtk_widget_set_size_request (arrow, -1, 16);
445
446         btn_hbox = gtk_hbox_new (TRUE, 4);
447         gtk_widget_show (btn_hbox);
448         gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
449
450         reg_btn = gtk_button_new_from_stock (GTK_STOCK_ADD);
451         gtk_widget_show (reg_btn);
452         gtk_box_pack_start (GTK_BOX (btn_hbox), reg_btn, FALSE, TRUE, 0);
453         g_signal_connect(G_OBJECT (reg_btn), "clicked",
454                          G_CALLBACK(prefs_filtering_register_cb), NULL);
455
456         subst_btn = gtkut_get_replace_btn (_("Replace"));
457         gtk_widget_show (subst_btn);
458         gtk_box_pack_start (GTK_BOX (btn_hbox), subst_btn, FALSE, TRUE, 0);
459         g_signal_connect(G_OBJECT (subst_btn), "clicked",
460                          G_CALLBACK(prefs_filtering_substitute_cb),
461                          NULL);
462
463         del_btn = gtk_button_new_from_stock (GTK_STOCK_DELETE);
464         gtk_widget_show (del_btn);
465         gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
466         g_signal_connect(G_OBJECT (del_btn), "clicked",
467                         G_CALLBACK(prefs_filtering_delete_cb), NULL);
468
469         cond_hbox = gtk_hbox_new (FALSE, 8);
470         gtk_widget_show (cond_hbox);
471         gtk_box_pack_start (GTK_BOX (vbox), cond_hbox, TRUE, TRUE, 0);
472
473         cond_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
474         gtk_widget_show (cond_scrolledwin);
475         gtk_widget_set_size_request (cond_scrolledwin, -1, 150);
476         gtk_box_pack_start (GTK_BOX (cond_hbox), cond_scrolledwin,
477                             TRUE, TRUE, 0);
478         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin),
479                                         GTK_POLICY_AUTOMATIC,
480                                         GTK_POLICY_AUTOMATIC);
481
482         cond_list_view = prefs_filtering_list_view_create();    
483         gtk_widget_show (cond_list_view);
484         gtk_container_add (GTK_CONTAINER (cond_scrolledwin), cond_list_view);
485
486         btn_vbox = gtk_vbox_new (FALSE, 8);
487         gtk_widget_show (btn_vbox);
488         gtk_box_pack_start (GTK_BOX (cond_hbox), btn_vbox, FALSE, FALSE, 0);
489
490         top_btn = gtk_button_new_from_stock (GTK_STOCK_GOTO_TOP);
491         gtk_widget_show (top_btn);
492         gtk_box_pack_start (GTK_BOX (btn_vbox), top_btn, FALSE, FALSE, 0);
493         g_signal_connect(G_OBJECT (top_btn), "clicked",
494                          G_CALLBACK(prefs_filtering_top), NULL);
495
496         PACK_VSPACER (btn_vbox, spc_vbox, VSPACING_NARROW_2);
497
498         up_btn = gtk_button_new_from_stock (GTK_STOCK_GO_UP);
499         gtk_widget_show (up_btn);
500         gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
501         g_signal_connect(G_OBJECT (up_btn), "clicked",
502                          G_CALLBACK(prefs_filtering_up), NULL);
503
504         down_btn = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN);
505         gtk_widget_show (down_btn);
506         gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
507         g_signal_connect(G_OBJECT (down_btn), "clicked",
508                          G_CALLBACK(prefs_filtering_down), NULL);
509
510         PACK_VSPACER (btn_vbox, spc_vbox, VSPACING_NARROW_2);
511
512         bottom_btn = gtk_button_new_from_stock (GTK_STOCK_GOTO_BOTTOM);
513         gtk_widget_show (bottom_btn);
514         gtk_box_pack_start (GTK_BOX (btn_vbox), bottom_btn, FALSE, FALSE, 0);
515         g_signal_connect(G_OBJECT (bottom_btn), "clicked",
516                          G_CALLBACK(prefs_filtering_bottom), NULL);
517
518         if (!geometry.min_height) {
519                 geometry.min_width = 500;
520                 geometry.min_height = 400;
521         }
522
523         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
524                                       GDK_HINT_MIN_SIZE);
525         gtk_widget_set_size_request(window, prefs_common.filteringwin_width,
526                                     prefs_common.filteringwin_height);
527
528         gtk_widget_show_all(window);
529
530         filtering.window    = window;
531         filtering.help_btn = help_btn;
532         filtering.ok_btn = ok_btn;
533
534         filtering.name_entry     = name_entry;
535         filtering.cond_entry     = cond_entry;
536         filtering.action_entry   = action_entry;
537         filtering.cond_list_view = cond_list_view;
538         filtering.account_label  = account_label;
539 }
540
541 static void rename_path(GSList * filters,
542                         const gchar * old_path, const gchar * new_path);
543
544 void prefs_filtering_rename_path(const gchar *old_path, const gchar *new_path)
545 {
546         GList * cur;
547         const gchar *paths[2] = {NULL, NULL};
548         paths[0] = old_path;
549         paths[1] = new_path;
550         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
551                 Folder *folder;
552                 folder = (Folder *) cur->data;
553                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
554                                 prefs_filtering_rename_path_func, paths);
555         }
556         
557         rename_path(pre_global_processing, old_path, new_path);
558         rename_path(post_global_processing, old_path, new_path);
559         rename_path(filtering_rules, old_path, new_path);
560         
561         prefs_matcher_write_config();
562 }
563
564 static void rename_path(GSList * filters,
565                         const gchar * old_path, const gchar * new_path)
566 {
567         gchar *base;
568         gchar *prefix;
569         gchar *suffix;
570         gchar *dest_path;
571         gchar *old_path_with_sep;
572         gint destlen;
573         gint prefixlen;
574         gint oldpathlen;
575         GSList * action_cur;
576         GSList * cur;
577
578         oldpathlen = strlen(old_path);
579         old_path_with_sep = g_strconcat(old_path,G_DIR_SEPARATOR_S,NULL);
580
581         for (cur = filters; cur != NULL; cur = cur->next) {
582                 FilteringProp   *filtering = (FilteringProp *)cur->data;
583                 
584                 for(action_cur = filtering->action_list ; action_cur != NULL ;
585                     action_cur = action_cur->next) {
586
587                         FilteringAction *action = action_cur->data;
588                         
589                         if (!action->destination) continue;
590                         
591                         destlen = strlen(action->destination);
592                         
593                         if (destlen > oldpathlen) {
594                                 prefixlen = destlen - oldpathlen;
595                                 suffix = action->destination + prefixlen;
596                                 
597                                 if (!strncmp(old_path, suffix, oldpathlen)) {
598                                         prefix = g_malloc0(prefixlen + 1);
599                                         strncpy2(prefix, action->destination, prefixlen);
600                                         
601                                         base = suffix + oldpathlen;
602                                         while (*base == G_DIR_SEPARATOR) base++;
603                                         if (*base == '\0')
604                                                 dest_path = g_strconcat(prefix,
605                                                     G_DIR_SEPARATOR_S,
606                                                     new_path, NULL);
607                                         else
608                                                 dest_path = g_strconcat(prefix,
609                                                     G_DIR_SEPARATOR_S,
610                                                     new_path,
611                                                     G_DIR_SEPARATOR_S,
612                                                     base, NULL);
613                                         
614                                         g_free(prefix);
615                                         g_free(action->destination);
616                                         action->destination = dest_path;
617                                 } else { /* for non-leaf folders */
618                                         /* compare with trailing slash */
619                                         if (!strncmp(old_path_with_sep, action->destination, oldpathlen+1)) {
620                                                 
621                                                 suffix = action->destination + oldpathlen + 1;
622                                                 dest_path = g_strconcat(new_path,
623                                                     G_DIR_SEPARATOR_S,
624                                                     suffix, NULL);
625                                                 g_free(action->destination);
626                                                 action->destination = dest_path;
627                                         }
628                                 }
629                         } else {
630                                 /* folder-moving a leaf */
631                                 if (!strcmp(old_path, action->destination)) {
632                                         dest_path = g_strdup(new_path);
633                                         g_free(action->destination);
634                                         action->destination = dest_path;
635                                 }
636                         }
637                 }
638         }
639 }
640
641 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
642 {
643         GSList *filters;
644         const gchar * old_path;
645         const gchar * new_path;
646         const gchar ** paths;
647         FolderItem *item;
648         
649         paths = data;
650         old_path = paths[0];
651         new_path = paths[1];
652
653         g_return_val_if_fail(old_path != NULL, FALSE);
654         g_return_val_if_fail(new_path != NULL, FALSE);
655         g_return_val_if_fail(node != NULL, FALSE);
656
657         item = node->data;
658         if (!item || !item->prefs)
659                 return FALSE;
660         filters = item->prefs->processing;
661
662         rename_path(filters, old_path, new_path);
663
664         return FALSE;
665 }
666
667 void prefs_filtering_delete_path(const gchar *path)
668 {
669         GList * cur;
670         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
671                 Folder *folder;
672                 folder = (Folder *) cur->data;
673                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
674                                 prefs_filtering_delete_path_func, (gchar *)path);
675         }
676         delete_path(&pre_global_processing, path);
677         delete_path(&post_global_processing, path);
678         delete_path(&filtering_rules, path);
679         
680         prefs_matcher_write_config();
681 }
682
683 static void delete_path(GSList ** p_filters, const gchar * path)
684 {
685         GSList * filters;
686         GSList * duplist;
687         gchar *suffix;
688         gint destlen;
689         gint prefixlen;
690         gint pathlen;
691         GSList * action_cur;
692         GSList * cur;
693         
694         filters = *p_filters;
695         pathlen = strlen(path);
696         duplist = g_slist_copy(filters);
697         for (cur = duplist ; cur != NULL; cur = g_slist_next(cur)) {
698                 FilteringProp *filtering = (FilteringProp *) cur->data;
699                 
700                 for(action_cur = filtering->action_list ; action_cur != NULL ;
701                     action_cur = action_cur->next) {
702                 
703                         FilteringAction *action;
704                         
705                         action = action_cur->data;
706                         
707                         if (!action->destination) continue;
708                         
709                         destlen = strlen(action->destination);
710                         
711                         if (destlen > pathlen) {
712                                 prefixlen = destlen - pathlen;
713                                 suffix = action->destination + prefixlen;
714                                 
715                                 if (suffix && !strncmp(path, suffix, pathlen)) {
716                                         filteringprop_free(filtering);
717                                         filters = g_slist_remove(filters, filtering);
718                                 }
719                         } else if (strcmp(action->destination, path) == 0) {
720                                 filteringprop_free(filtering);
721                                 filters = g_slist_remove(filters, filtering);
722                         }
723                 }
724         }                
725         g_slist_free(duplist);
726         
727         * p_filters = filters;
728 }
729
730 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data)
731 {
732         const gchar *path = data;
733         FolderItem *item;
734         GSList ** p_filters;
735         
736         g_return_val_if_fail(path != NULL, FALSE);
737         g_return_val_if_fail(node != NULL, FALSE);
738
739         item = node->data;
740         if (!item || !item->prefs)
741                 return FALSE;
742         p_filters = &item->prefs->processing;
743         
744         delete_path(p_filters, path);
745
746         return FALSE;
747 }
748
749 static void prefs_filtering_set_dialog(const gchar *header, const gchar *key)
750 {
751         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
752         GSList *cur;
753         GSList * prefs_filtering;
754         gchar *cond_str;
755         GtkListStore *list_store;
756         
757         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
758         gtk_list_store_clear(list_store);
759
760         /* add the place holder (New) at row 0 */
761         prefs_filtering_list_view_insert_rule(list_store, -1, 
762                                               FALSE,
763                                               _("(New)"),
764                                                   0,
765                                               _("(New)"),
766                                               _("(New)"),
767                                               FALSE);
768
769         prefs_filtering = *p_processing_list;
770
771         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur)) {
772                 FilteringProp * prop = (FilteringProp *) cur->data;
773                 gchar *account_name;
774
775                 if (prop->account_id > 0) {
776                         account_name = account_find_from_id(prop->account_id)->account_name;
777                 } else {
778                         account_name = (gchar *)Q_("Filtering Account Menu|All");
779                 }
780
781                 cond_str = filteringprop_to_string(prop);
782                 subst_char(cond_str, '\t', ':');
783
784                 prefs_filtering_list_view_insert_rule(list_store, -1, 
785                                                       prop->enabled,
786                                                       prop->name,
787                                                           prop->account_id,
788                                                           account_name,
789                                                       cond_str, TRUE);
790                 
791                 g_free(cond_str);
792         }
793
794         prefs_filtering_reset_dialog();
795
796         if (header && key) {
797                 gchar * quoted_key;
798                 gchar *match_str;
799
800                 quoted_key = matcher_quote_str(key);
801                 
802                 match_str = g_strconcat(header, " ", get_matchparser_tab_str(MATCHTYPE_MATCHCASE),
803                                         " \"", quoted_key, "\"", NULL);
804                 g_free(quoted_key);
805                 
806                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), match_str);
807                 g_free(match_str);
808         }
809 }
810
811 static void prefs_filtering_reset_dialog(void)
812 {
813         gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), "");
814         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
815         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), "");
816         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), "");
817 }
818
819 static void prefs_filtering_set_list(void)
820 {
821         gint row = 1;
822         FilteringProp *prop;
823         GSList * cur;
824         gchar * filtering_str;
825         GSList * prefs_filtering;
826
827         prefs_filtering = *p_processing_list;
828         for (cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
829                 filteringprop_free((FilteringProp *) cur->data);
830         g_slist_free(prefs_filtering);
831         prefs_filtering = NULL;
832         
833
834         while (NULL != (filtering_str = prefs_filtering_list_view_get_rule
835                                                 (filtering.cond_list_view, row))) {
836                 /* FIXME: this strcmp() is bogus: "(New)" should never
837                  * be inserted in the storage */
838                 if (strcmp(filtering_str, _("(New)")) != 0) {
839                         gboolean enabled;
840                         gchar *name;
841                         gint account_id = 0;
842
843                         prefs_filtering_list_view_get_rule_name(
844                                         filtering.cond_list_view, row,
845                                         &enabled, &name, &account_id);
846                         prop = matcher_parser_get_filtering(filtering_str);
847                         g_free(filtering_str);
848                         if (prop) {
849                                 prop->enabled = enabled;
850                                 prop->name = name;
851                                 prop->account_id = account_id;
852                                 prefs_filtering = 
853                                         g_slist_append(prefs_filtering, prop);
854                         }
855                 }
856                 
857                 row++;
858         }                               
859         
860         *p_processing_list = prefs_filtering;
861 }
862
863 static gint prefs_filtering_list_view_set_row(gint row, FilteringProp * prop)
864 {
865         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
866         gchar *str;
867         GtkListStore *list_store;
868         gchar *name = NULL;
869         gint account_id = 0;
870         gchar *account_name = (gchar *)Q_("Filtering Account Menu|All");
871         gboolean enabled = TRUE;
872
873         str = filteringprop_to_string(prop);
874         if (str == NULL)
875                 return -1;
876
877         if (prop) {
878                 if (prop->name)
879                         name = prop->name;
880                 account_id = prop->account_id;
881                 if (account_id > 0)
882                         account_name = account_find_from_id(account_id)->account_name;
883                 enabled = prop->enabled;
884         }
885
886         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
887
888         row = prefs_filtering_list_view_insert_rule(list_store, row,
889                                                     enabled,
890                                                     name,
891                                                     account_id,
892                                                         account_name,
893                                                     str,
894                                                     prop != NULL);
895
896         g_free(str);
897
898         return row;
899 }
900
901 static void prefs_filtering_condition_define_done(MatcherList * matchers)
902 {
903         gchar * str;
904
905         if (matchers == NULL)
906                 return;
907
908         str = matcherlist_to_string(matchers);
909
910         if (str != NULL) {
911                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), str);
912                 g_free(str);
913         }
914 }
915
916 static void prefs_filtering_condition_define(void)
917 {
918         gchar * cond_str;
919         MatcherList * matchers = NULL;
920
921         cond_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
922
923         if (*cond_str != '\0') {
924                 matchers = matcher_parser_get_cond(cond_str);
925                 if (matchers == NULL)
926                         alertpanel_error(_("Condition string is not valid."));
927         }
928         
929         g_free(cond_str);
930
931         prefs_matcher_open(matchers, prefs_filtering_condition_define_done);
932
933         if (matchers != NULL)
934                 matcherlist_free(matchers);
935 }
936
937 static void prefs_filtering_action_define_done(GSList * action_list)
938 {
939         gchar * str;
940
941         if (action_list == NULL)
942                 return;
943
944         str = filteringaction_list_to_string(action_list);
945
946         if (str != NULL) {
947                 gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), str);
948                 g_free(str);
949         }
950 }
951
952 static void prefs_filtering_action_define(void)
953 {
954         gchar * action_str;
955         GSList * action_list = NULL;
956
957         action_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
958
959         if (*action_str != '\0') {
960                 action_list = matcher_parser_get_action_list(action_str);
961                 if (action_list == NULL)
962                         alertpanel_error(_("Action string is not valid."));
963         }
964         
965         g_free(action_str);
966
967         prefs_filtering_action_open(action_list,
968             prefs_filtering_action_define_done);
969
970         if (action_list != NULL) {
971                 GSList * cur;
972                 for(cur = action_list ; cur != NULL ; cur = cur->next) {
973                         filteringaction_free(cur->data);
974                 }
975         }
976 }
977
978
979 /* register / substitute delete buttons */
980
981
982 static FilteringProp * prefs_filtering_dialog_to_filtering(gboolean alert)
983 {
984         MatcherList * cond;
985         gboolean enabled = TRUE;
986         gchar * name = NULL;
987         gint account_id = 0;
988         gchar * cond_str = NULL;
989         gchar * action_str = NULL;
990         FilteringProp * prop = NULL;
991         GSList * action_list;
992
993         name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
994         
995         account_id = combobox_get_active_data(GTK_COMBO_BOX(filtering.account_combobox));
996
997         cond_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
998         if (*cond_str == '\0') {
999                 if(alert == TRUE) alertpanel_error(_("Condition string is empty."));
1000                 goto fail;
1001         }
1002         
1003         action_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1004         if (*action_str == '\0') {
1005                 if(alert == TRUE) alertpanel_error(_("Action string is empty."));
1006                 goto fail;
1007         }
1008
1009         cond = matcher_parser_get_cond(cond_str);
1010
1011         if (cond == NULL) {
1012                 if(alert == TRUE) alertpanel_error(_("Condition string is not valid."));
1013                 goto fail;
1014         }
1015         
1016         action_list = matcher_parser_get_action_list(action_str);
1017         
1018
1019         if (action_list == NULL) {
1020                 if(alert == TRUE) alertpanel_error(_("Action string is not valid."));
1021                 goto fail;
1022         }
1023
1024         prop = filteringprop_new(enabled, name, account_id, cond, action_list);
1025
1026 fail:
1027         g_free(name);
1028         g_free(cond_str);
1029         g_free(action_str);
1030         return prop;
1031 }
1032
1033 static void prefs_filtering_register_cb(void)
1034 {
1035         FilteringProp * prop;
1036         
1037         prop = prefs_filtering_dialog_to_filtering(TRUE);
1038         if (prop == NULL)
1039                 return;
1040         prefs_filtering_list_view_set_row(-1, prop);
1041         
1042         filteringprop_free(prop);
1043
1044         prefs_filtering_reset_dialog();
1045 }
1046
1047 static void prefs_filtering_substitute_cb(void)
1048 {
1049         gint selected_row = prefs_filtering_get_selected_row
1050                 (filtering.cond_list_view);
1051         FilteringProp *prop;
1052         
1053         if (selected_row <= 0)
1054                 return;
1055
1056         prop = prefs_filtering_dialog_to_filtering(TRUE);
1057         if (prop == NULL) 
1058                 return;
1059         prefs_filtering_list_view_set_row(selected_row, prop);
1060
1061         filteringprop_free(prop);
1062
1063         prefs_filtering_reset_dialog();
1064 }
1065
1066 static void prefs_filtering_delete_cb(void)
1067 {
1068         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
1069         GtkTreeModel *model;
1070         GtkTreeIter iter;
1071         gint row;
1072         
1073         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1074         if (row <= 0) 
1075                 return; 
1076
1077         if (alertpanel(_("Delete rule"),
1078                        _("Do you really want to delete this rule?"),
1079                        GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
1080                 return;
1081
1082         model = gtk_tree_view_get_model(list_view);     
1083         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1084                 return;
1085
1086         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1087
1088         prefs_filtering_reset_dialog();
1089 }
1090
1091 static void prefs_filtering_top(void)
1092 {
1093         gint row;
1094         GtkTreeIter top, sel;
1095         GtkTreeModel *model;
1096
1097         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1098         if (row <= 1) 
1099                 return;
1100
1101         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));               
1102         
1103         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
1104         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1105                 return;
1106
1107         gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
1108         prefs_filtering_list_view_select_row(filtering.cond_list_view, 1);
1109 }
1110
1111 static void prefs_filtering_up(void)
1112 {
1113         gint row;
1114         GtkTreeIter top, sel;
1115         GtkTreeModel *model;
1116
1117         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1118         if (row <= 1) 
1119                 return;
1120                 
1121         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1122
1123         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
1124         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1125                 return;
1126
1127         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1128         prefs_filtering_list_view_select_row(filtering.cond_list_view, row - 1);
1129 }
1130
1131 static void prefs_filtering_down(void)
1132 {
1133         gint row, n_rows;
1134         GtkTreeIter top, sel;
1135         GtkTreeModel *model;
1136
1137         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1138         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1139         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1140         if (row < 1 || row >= n_rows - 1)
1141                 return;
1142
1143         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1144         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
1145                 return;
1146                         
1147         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1148         prefs_filtering_list_view_select_row(filtering.cond_list_view, row + 1);
1149 }
1150
1151 static void prefs_filtering_bottom(void)
1152 {
1153         gint row, n_rows;
1154         GtkTreeIter top, sel;
1155         GtkTreeModel *model;
1156
1157         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1158         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1159         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1160         if (row < 1 || row >= n_rows - 1)
1161                 return;
1162
1163         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1164         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
1165                 return;
1166
1167         gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);           
1168         prefs_filtering_list_view_select_row(filtering.cond_list_view, n_rows - 1);
1169 }
1170
1171 static void prefs_filtering_select_set(FilteringProp *prop)
1172 {
1173         gchar *matcher_str;
1174         gchar *action_str;
1175
1176         prefs_filtering_reset_dialog();
1177
1178         matcher_str = matcherlist_to_string(prop->matchers);
1179         if (matcher_str == NULL) {
1180                 return;
1181         }
1182         
1183         if (prop->name != NULL)
1184                 gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), prop->name);
1185
1186         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), prop->account_id);
1187                 
1188         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), matcher_str);
1189
1190         action_str = filteringaction_list_to_string(prop->action_list);
1191         if (matcher_str == NULL) {
1192                 return;
1193         }
1194         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), action_str);
1195
1196         g_free(action_str);
1197         g_free(matcher_str);
1198 }
1199
1200 static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
1201                                  gpointer data)
1202 {
1203         prefs_filtering_cancel();
1204         return TRUE;
1205 }
1206
1207 static gboolean prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
1208                                      gpointer data)
1209 {
1210         if (event && event->keyval == GDK_Escape) {
1211                 prefs_filtering_cancel();
1212                 return TRUE;                    
1213         }
1214         return FALSE;
1215 }
1216
1217 static void prefs_filtering_ok(void)
1218 {
1219         FilteringProp * prop;
1220         gchar * str;
1221         gchar * filtering_str;
1222         gint row = 1;
1223         AlertValue val;
1224         
1225         prop = prefs_filtering_dialog_to_filtering(FALSE);
1226         if (prop != NULL) {
1227                 str = filteringprop_to_string(prop);
1228
1229                 while (NULL != (filtering_str = (prefs_filtering_list_view_get_rule
1230                                                         (filtering.cond_list_view,
1231                                                          row)))) {
1232                         if (strcmp(filtering_str, str) == 0)
1233                                 break;
1234                         row++;
1235                         g_free(filtering_str);
1236                 }       
1237
1238                 if (!filtering_str) {
1239                         val = alertpanel(_("Entry not saved"),
1240                                  _("The entry was not saved. Close anyway?"),
1241                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1242                         if (G_ALERTDEFAULT != val) {
1243                                 g_free(filtering_str);
1244                                 g_free(str); /* fixed two leaks: huzzah! */
1245                                 filteringprop_free(prop);
1246                                 return;
1247                         }
1248                 }               
1249
1250                 g_free(filtering_str);
1251                 g_free(str);
1252                 filteringprop_free(prop); /* fixed a leak: huzzah! */
1253         } else {
1254                 gchar *name, *condition, *action;
1255                 name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
1256                 condition = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1257                 action = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1258                 if (strlen(name) || 
1259                     strlen(condition) || 
1260                     strlen(action)) {
1261                         val = alertpanel(_("Entry not saved"),
1262                                  _("The entry was not saved. Close anyway?"),
1263                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1264                         if (G_ALERTDEFAULT != val) {
1265                                 g_free(name);
1266                                 g_free(condition);
1267                                 g_free(action);
1268                                 return;
1269                         }
1270                 }
1271                 g_free(name);
1272                 g_free(condition);
1273                 g_free(action);
1274         }
1275         prefs_filtering_set_list();
1276         prefs_matcher_write_config();
1277         prefs_filtering_close();
1278 }
1279
1280 static void prefs_filtering_cancel(void)
1281 {
1282         prefs_matcher_read_config();
1283         prefs_filtering_close();
1284 }
1285
1286 static GtkListStore* prefs_filtering_create_data_store(void)
1287 {
1288         return gtk_list_store_new(N_PREFS_FILTERING_COLUMNS,
1289                                   G_TYPE_BOOLEAN,
1290                                   G_TYPE_STRING,
1291                                   G_TYPE_INT,
1292                                   G_TYPE_STRING,
1293                                   G_TYPE_STRING,
1294                                   G_TYPE_BOOLEAN,
1295                                  -1);
1296 }
1297
1298 /*!
1299  *\brief        Insert filtering rule into store. Note that we access the
1300  *              tree view / store by index, which is a bit suboptimal, but
1301  *              at least it made GTK 2 porting easier.
1302  *
1303  *\param        list_store Store to operate on
1304  *\param        row -1 to add a new rule to store, else change an existing
1305  *              row
1306  *\param        enabled TRUE if rule is enabled
1307  *\param        name The Name of rule
1308  *\param        account_id The account ID
1309  *\param        account_name The account name or All or (New)
1310  *\param        rule String representation of rule
1311  *\param        prop TRUE if valid filtering rule; if FALSE it's the first
1312  *              entry in the store ("(New)").
1313  *
1314  *\return       int Row of inserted / changed rule.
1315  */
1316 static gint prefs_filtering_list_view_insert_rule(GtkListStore *list_store,
1317                                                   gint row,
1318                                                   gboolean enabled,
1319                                                   const gchar *name,
1320                                                   gint account_id,
1321                                                   const gchar *account_name,
1322                                                   const gchar *rule,
1323                                                   gboolean prop) 
1324 {
1325         GtkTreeIter iter;
1326
1327         /* check if valid row at all */
1328         if (row >= 0) {
1329                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1330                                                    &iter, NULL, row))
1331                         row = -1;                                                  
1332         }
1333
1334         if (row < 0) {
1335                 /* append new */
1336                 gtk_list_store_append(list_store, &iter);
1337                 gtk_list_store_set(list_store, &iter, 
1338                                    PREFS_FILTERING_ENABLED, enabled,
1339                                    PREFS_FILTERING_NAME, name,
1340                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1341                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1342                                    PREFS_FILTERING_RULE, rule,
1343                                    PREFS_FILTERING_PROP, prop,
1344                                    -1);
1345                 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store),
1346                                                       NULL) - 1;
1347         } else {
1348                 /* change existing */
1349                 gtk_list_store_set(list_store, &iter, 
1350                                    PREFS_FILTERING_ENABLED, enabled,
1351                                    PREFS_FILTERING_NAME, name,
1352                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1353                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1354                                    PREFS_FILTERING_RULE, rule,
1355                                    -1);
1356                 return row;                                
1357         }
1358 }
1359
1360 /*!
1361  *\return       gchar * Rule at specified row - should be freed.
1362  */
1363 static gchar *prefs_filtering_list_view_get_rule(GtkWidget *list, gint row)
1364 {       
1365         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1366         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1367         GtkTreeIter iter;
1368         gchar *result;
1369
1370         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1371                 return NULL;
1372         
1373         gtk_tree_model_get(model, &iter, 
1374                            PREFS_FILTERING_RULE, &result,
1375                            -1);
1376         
1377         return result;
1378 }
1379
1380 static void prefs_filtering_list_view_get_rule_name(GtkWidget *list, gint row,
1381                                 gboolean *enabled, gchar **name, gint *account_id)
1382 {       
1383         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1384         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1385         GtkTreeIter iter;
1386
1387         *enabled = TRUE;
1388         *name = NULL;
1389
1390         if (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1391                 gtk_tree_model_get(model, &iter, 
1392                                    PREFS_FILTERING_ENABLED, enabled,
1393                                    PREFS_FILTERING_NAME, name,
1394                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1395                                    -1);
1396         }
1397 }
1398
1399 /*!
1400  *\brief        Create list view for filtering
1401  */
1402 static GtkWidget *prefs_filtering_list_view_create(void)
1403 {
1404         GtkTreeView *list_view;
1405         GtkTreeSelection *selector;
1406
1407         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL
1408                 (prefs_filtering_create_data_store())));
1409         
1410         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1411         gtk_tree_view_set_reorderable(list_view, TRUE);
1412         
1413         selector = gtk_tree_view_get_selection(list_view);
1414         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1415         gtk_tree_selection_set_select_function(selector, prefs_filtering_selected,
1416                                                NULL, NULL);
1417
1418         /* create the columns */
1419         prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view));
1420
1421         return GTK_WIDGET(list_view);
1422 }
1423
1424 static void prefs_filtering_enable_toggled(GtkCellRendererToggle *widget,
1425                 gchar *path,
1426                 GtkWidget *list_view)
1427 {
1428         GtkTreeIter iter;
1429         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1430         gboolean enabled = TRUE;
1431
1432         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1433                 return;
1434
1435         gtk_tree_model_get(model, &iter,
1436                            PREFS_FILTERING_ENABLED, &enabled,
1437                            -1);
1438
1439         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1440                            PREFS_FILTERING_ENABLED, !enabled,
1441                            -1);
1442 }
1443
1444 static void prefs_filtering_create_list_view_columns(GtkWidget *list_view)
1445 {
1446         GtkTreeViewColumn *column;
1447         GtkCellRenderer *renderer;
1448
1449         renderer = gtk_cell_renderer_toggle_new();
1450         g_object_set(renderer,
1451                      "radio", FALSE,
1452                      "activatable", TRUE,
1453                      NULL);
1454         column = gtk_tree_view_column_new_with_attributes
1455                 (_("Enable"), /* FIXME : Enable, Enabled, or 'E' ? */
1456                  renderer,
1457                  "active", PREFS_FILTERING_ENABLED,
1458                  NULL);
1459         gtk_tree_view_column_set_alignment (column, 0.5);
1460         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1461         g_signal_connect(G_OBJECT(renderer), "toggled",
1462                          G_CALLBACK(prefs_filtering_enable_toggled),
1463                          list_view);
1464
1465         renderer = gtk_cell_renderer_text_new();
1466         column = gtk_tree_view_column_new_with_attributes
1467                 (_("Name"),
1468                  renderer,
1469                  "text", PREFS_FILTERING_NAME,
1470                  NULL);
1471         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1472         gtk_tree_view_column_set_resizable(column, TRUE);
1473
1474         renderer = gtk_cell_renderer_text_new();
1475         column = gtk_tree_view_column_new_with_attributes
1476                 (_("Account"),
1477                  renderer,
1478                  "text", PREFS_FILTERING_ACCOUNT_NAME,
1479                  NULL);
1480         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1481         gtk_tree_view_column_set_resizable(column, TRUE);
1482
1483         filtering.account_name_column = column;
1484                 
1485         renderer = gtk_cell_renderer_text_new();
1486         column = gtk_tree_view_column_new_with_attributes
1487                 (_("Rule"),
1488                  renderer,
1489                  "text", PREFS_FILTERING_RULE,
1490                  NULL);
1491         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1492 }
1493
1494 static gboolean gtkut_tree_iter_comp(GtkTreeModel *model, 
1495                                      GtkTreeIter *iter1, 
1496                                      GtkTreeIter *iter2)
1497 {
1498         GtkTreePath *path1 = gtk_tree_model_get_path(model, iter1);
1499         GtkTreePath *path2 = gtk_tree_model_get_path(model, iter2);
1500         gboolean result;
1501
1502         result = gtk_tree_path_compare(path1, path2) == 0;
1503
1504         gtk_tree_path_free(path1);
1505         gtk_tree_path_free(path2);
1506         
1507         return result;
1508 }
1509
1510 /*!
1511  *\brief        Get selected row number.
1512  */
1513 static gint prefs_filtering_get_selected_row(GtkWidget *list_view)
1514 {
1515         GtkTreeView *view = GTK_TREE_VIEW(list_view);
1516         GtkTreeModel *model = gtk_tree_view_get_model(view);
1517         int n_rows = gtk_tree_model_iter_n_children(model, NULL);
1518         GtkTreeSelection *selection;
1519         GtkTreeIter iter;
1520         int row;
1521
1522         if (n_rows == 0) 
1523                 return -1;
1524         
1525         selection = gtk_tree_view_get_selection(view);
1526         if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1527                 return -1;
1528         
1529         /* get all iterators and compare them... */
1530         for (row = 0; row < n_rows; row++) {
1531                 GtkTreeIter itern;
1532
1533                 gtk_tree_model_iter_nth_child(model, &itern, NULL, row);
1534                 if (gtkut_tree_iter_comp(model, &iter, &itern))
1535                         return row;
1536         }
1537         
1538         return -1;
1539 }
1540
1541 static gboolean prefs_filtering_list_view_select_row(GtkWidget *list, gint row)
1542 {
1543         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1544         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
1545         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1546         GtkTreeIter iter;
1547         GtkTreePath *path;
1548
1549         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1550                 return FALSE;
1551         
1552         gtk_tree_selection_select_iter(selection, &iter);
1553
1554         path = gtk_tree_model_get_path(model, &iter);
1555         gtk_tree_view_set_cursor(list_view, path, NULL, FALSE);
1556         gtk_tree_path_free(path);
1557         
1558         return TRUE;
1559 }
1560
1561 /*!
1562  *\brief        Triggered when a row is selected
1563  */
1564 static gboolean prefs_filtering_selected(GtkTreeSelection *selector,
1565                                          GtkTreeModel *model, 
1566                                          GtkTreePath *path,
1567                                          gboolean currently_selected,
1568                                          gpointer data)
1569 {
1570         if (currently_selected)
1571                 return TRUE;
1572         else {          
1573                 gboolean has_prop  = FALSE;
1574                 GtkTreeIter iter;
1575
1576                 gtk_tree_model_get_iter(model, &iter, path);
1577                 gtk_tree_model_get(model, &iter,
1578                                    PREFS_FILTERING_PROP, &has_prop,
1579                                    -1);
1580
1581                 if (has_prop) {
1582                         FilteringProp *prop;
1583                         gchar *filtering_str = NULL;
1584                         gchar *name = NULL;
1585                         gint account_id = 0;
1586
1587                         gtk_tree_model_get(model, &iter,
1588                                            PREFS_FILTERING_RULE, &filtering_str,
1589                                            -1);
1590                         gtk_tree_model_get(model, &iter,
1591                                            PREFS_FILTERING_NAME, &name,
1592                                            -1);
1593                         gtk_tree_model_get(model, &iter,
1594                                            PREFS_FILTERING_ACCOUNT_ID, &account_id,
1595                                            -1);
1596
1597                         prop = matcher_parser_get_filtering(filtering_str);
1598                         if (prop) { 
1599                                 prop->name = g_strdup(name);
1600                                 prop->account_id = account_id;
1601                                 prefs_filtering_select_set(prop);
1602                                 filteringprop_free(prop);
1603                         }                               
1604                         g_free(name);
1605                         g_free(filtering_str);
1606                 } else
1607                         prefs_filtering_reset_dialog();
1608         }               
1609
1610         return TRUE;
1611 }
1612