2006-09-09 [wwp] 2.4.0cvs165
[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 = NULL;
774
775                 if (prop->account_id > 0) {
776                         PrefsAccount *ac_prefs = account_find_from_id(prop->account_id);
777
778                         if (ac_prefs)
779                                 account_name = ac_prefs->account_name;
780                 }
781                 if (account_name == NULL)
782                         account_name = (gchar *)Q_("Filtering Account Menu|All");
783
784                 cond_str = filteringprop_to_string(prop);
785                 subst_char(cond_str, '\t', ':');
786
787                 prefs_filtering_list_view_insert_rule(list_store, -1, 
788                                                       prop->enabled,
789                                                       prop->name,
790                                                           prop->account_id,
791                                                           account_name,
792                                                       cond_str, TRUE);
793                 
794                 g_free(cond_str);
795         }
796
797         prefs_filtering_reset_dialog();
798
799         if (header && key) {
800                 gchar * quoted_key;
801                 gchar *match_str;
802
803                 quoted_key = matcher_quote_str(key);
804                 
805                 match_str = g_strconcat(header, " ", get_matchparser_tab_str(MATCHTYPE_MATCHCASE),
806                                         " \"", quoted_key, "\"", NULL);
807                 g_free(quoted_key);
808                 
809                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), match_str);
810                 g_free(match_str);
811         }
812 }
813
814 static void prefs_filtering_reset_dialog(void)
815 {
816         gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), "");
817         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
818         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), "");
819         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), "");
820 }
821
822 static void prefs_filtering_set_list(void)
823 {
824         gint row = 1;
825         FilteringProp *prop;
826         GSList * cur;
827         gchar * filtering_str;
828         GSList * prefs_filtering;
829
830         prefs_filtering = *p_processing_list;
831         for (cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
832                 filteringprop_free((FilteringProp *) cur->data);
833         g_slist_free(prefs_filtering);
834         prefs_filtering = NULL;
835         
836
837         while (NULL != (filtering_str = prefs_filtering_list_view_get_rule
838                                                 (filtering.cond_list_view, row))) {
839                 /* FIXME: this strcmp() is bogus: "(New)" should never
840                  * be inserted in the storage */
841                 if (strcmp(filtering_str, _("(New)")) != 0) {
842                         gboolean enabled;
843                         gchar *name;
844                         gint account_id = 0;
845
846                         prefs_filtering_list_view_get_rule_name(
847                                         filtering.cond_list_view, row,
848                                         &enabled, &name, &account_id);
849                         prop = matcher_parser_get_filtering(filtering_str);
850                         g_free(filtering_str);
851                         if (prop) {
852                                 prop->enabled = enabled;
853                                 prop->name = name;
854                                 prop->account_id = account_id;
855                                 prefs_filtering = 
856                                         g_slist_append(prefs_filtering, prop);
857                         }
858                 }
859                 
860                 row++;
861         }                               
862         
863         *p_processing_list = prefs_filtering;
864 }
865
866 static gint prefs_filtering_list_view_set_row(gint row, FilteringProp * prop)
867 {
868         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
869         gchar *str;
870         GtkListStore *list_store;
871         gchar *name = NULL;
872         gint account_id = 0;
873         gchar *account_name = (gchar *)Q_("Filtering Account Menu|All");
874         gboolean enabled = TRUE;
875
876         str = filteringprop_to_string(prop);
877         if (str == NULL)
878                 return -1;
879
880         if (prop) {
881                 if (prop->name)
882                         name = prop->name;
883                 account_id = prop->account_id;
884                 if (account_id > 0)
885                         account_name = account_find_from_id(account_id)->account_name;
886                 enabled = prop->enabled;
887         }
888
889         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
890
891         row = prefs_filtering_list_view_insert_rule(list_store, row,
892                                                     enabled,
893                                                     name,
894                                                     account_id,
895                                                         account_name,
896                                                     str,
897                                                     prop != NULL);
898
899         g_free(str);
900
901         return row;
902 }
903
904 static void prefs_filtering_condition_define_done(MatcherList * matchers)
905 {
906         gchar * str;
907
908         if (matchers == NULL)
909                 return;
910
911         str = matcherlist_to_string(matchers);
912
913         if (str != NULL) {
914                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), str);
915                 g_free(str);
916         }
917 }
918
919 static void prefs_filtering_condition_define(void)
920 {
921         gchar * cond_str;
922         MatcherList * matchers = NULL;
923
924         cond_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
925
926         if (*cond_str != '\0') {
927                 matchers = matcher_parser_get_cond(cond_str);
928                 if (matchers == NULL)
929                         alertpanel_error(_("Condition string is not valid."));
930         }
931         
932         g_free(cond_str);
933
934         prefs_matcher_open(matchers, prefs_filtering_condition_define_done);
935
936         if (matchers != NULL)
937                 matcherlist_free(matchers);
938 }
939
940 static void prefs_filtering_action_define_done(GSList * action_list)
941 {
942         gchar * str;
943
944         if (action_list == NULL)
945                 return;
946
947         str = filteringaction_list_to_string(action_list);
948
949         if (str != NULL) {
950                 gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), str);
951                 g_free(str);
952         }
953 }
954
955 static void prefs_filtering_action_define(void)
956 {
957         gchar * action_str;
958         GSList * action_list = NULL;
959
960         action_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
961
962         if (*action_str != '\0') {
963                 action_list = matcher_parser_get_action_list(action_str);
964                 if (action_list == NULL)
965                         alertpanel_error(_("Action string is not valid."));
966         }
967         
968         g_free(action_str);
969
970         prefs_filtering_action_open(action_list,
971             prefs_filtering_action_define_done);
972
973         if (action_list != NULL) {
974                 GSList * cur;
975                 for(cur = action_list ; cur != NULL ; cur = cur->next) {
976                         filteringaction_free(cur->data);
977                 }
978         }
979 }
980
981
982 /* register / substitute delete buttons */
983
984
985 static FilteringProp * prefs_filtering_dialog_to_filtering(gboolean alert)
986 {
987         MatcherList * cond;
988         gboolean enabled = TRUE;
989         gchar * name = NULL;
990         gint account_id = 0;
991         gchar * cond_str = NULL;
992         gchar * action_str = NULL;
993         FilteringProp * prop = NULL;
994         GSList * action_list;
995
996         name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
997         
998         account_id = combobox_get_active_data(GTK_COMBO_BOX(filtering.account_combobox));
999
1000         cond_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1001         if (*cond_str == '\0') {
1002                 if(alert == TRUE) alertpanel_error(_("Condition string is empty."));
1003                 goto fail;
1004         }
1005         
1006         action_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1007         if (*action_str == '\0') {
1008                 if(alert == TRUE) alertpanel_error(_("Action string is empty."));
1009                 goto fail;
1010         }
1011
1012         cond = matcher_parser_get_cond(cond_str);
1013
1014         if (cond == NULL) {
1015                 if(alert == TRUE) alertpanel_error(_("Condition string is not valid."));
1016                 goto fail;
1017         }
1018         
1019         action_list = matcher_parser_get_action_list(action_str);
1020         
1021
1022         if (action_list == NULL) {
1023                 if(alert == TRUE) alertpanel_error(_("Action string is not valid."));
1024                 goto fail;
1025         }
1026
1027         prop = filteringprop_new(enabled, name, account_id, cond, action_list);
1028
1029 fail:
1030         g_free(name);
1031         g_free(cond_str);
1032         g_free(action_str);
1033         return prop;
1034 }
1035
1036 static void prefs_filtering_register_cb(void)
1037 {
1038         FilteringProp * prop;
1039         
1040         prop = prefs_filtering_dialog_to_filtering(TRUE);
1041         if (prop == NULL)
1042                 return;
1043         prefs_filtering_list_view_set_row(-1, prop);
1044         
1045         filteringprop_free(prop);
1046
1047         prefs_filtering_reset_dialog();
1048 }
1049
1050 static void prefs_filtering_substitute_cb(void)
1051 {
1052         gint selected_row = prefs_filtering_get_selected_row
1053                 (filtering.cond_list_view);
1054         FilteringProp *prop;
1055         
1056         if (selected_row <= 0)
1057                 return;
1058
1059         prop = prefs_filtering_dialog_to_filtering(TRUE);
1060         if (prop == NULL) 
1061                 return;
1062         prefs_filtering_list_view_set_row(selected_row, prop);
1063
1064         filteringprop_free(prop);
1065
1066         prefs_filtering_reset_dialog();
1067 }
1068
1069 static void prefs_filtering_delete_cb(void)
1070 {
1071         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
1072         GtkTreeModel *model;
1073         GtkTreeIter iter;
1074         gint row;
1075         
1076         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1077         if (row <= 0) 
1078                 return; 
1079
1080         if (alertpanel(_("Delete rule"),
1081                        _("Do you really want to delete this rule?"),
1082                        GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
1083                 return;
1084
1085         model = gtk_tree_view_get_model(list_view);     
1086         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1087                 return;
1088
1089         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1090
1091         prefs_filtering_reset_dialog();
1092 }
1093
1094 static void prefs_filtering_top(void)
1095 {
1096         gint row;
1097         GtkTreeIter top, sel;
1098         GtkTreeModel *model;
1099
1100         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1101         if (row <= 1) 
1102                 return;
1103
1104         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));               
1105         
1106         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
1107         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1108                 return;
1109
1110         gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
1111         prefs_filtering_list_view_select_row(filtering.cond_list_view, 1);
1112 }
1113
1114 static void prefs_filtering_up(void)
1115 {
1116         gint row;
1117         GtkTreeIter top, sel;
1118         GtkTreeModel *model;
1119
1120         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1121         if (row <= 1) 
1122                 return;
1123                 
1124         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1125
1126         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
1127         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1128                 return;
1129
1130         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1131         prefs_filtering_list_view_select_row(filtering.cond_list_view, row - 1);
1132 }
1133
1134 static void prefs_filtering_down(void)
1135 {
1136         gint row, n_rows;
1137         GtkTreeIter top, sel;
1138         GtkTreeModel *model;
1139
1140         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1141         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1142         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1143         if (row < 1 || row >= n_rows - 1)
1144                 return;
1145
1146         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1147         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
1148                 return;
1149                         
1150         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1151         prefs_filtering_list_view_select_row(filtering.cond_list_view, row + 1);
1152 }
1153
1154 static void prefs_filtering_bottom(void)
1155 {
1156         gint row, n_rows;
1157         GtkTreeIter top, sel;
1158         GtkTreeModel *model;
1159
1160         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1161         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1162         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1163         if (row < 1 || row >= n_rows - 1)
1164                 return;
1165
1166         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1167         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
1168                 return;
1169
1170         gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);           
1171         prefs_filtering_list_view_select_row(filtering.cond_list_view, n_rows - 1);
1172 }
1173
1174 static void prefs_filtering_select_set(FilteringProp *prop)
1175 {
1176         gchar *matcher_str;
1177         gchar *action_str;
1178
1179         prefs_filtering_reset_dialog();
1180
1181         matcher_str = matcherlist_to_string(prop->matchers);
1182         if (matcher_str == NULL) {
1183                 return;
1184         }
1185         
1186         if (prop->name != NULL)
1187                 gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), prop->name);
1188
1189         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), prop->account_id);
1190                 
1191         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), matcher_str);
1192
1193         action_str = filteringaction_list_to_string(prop->action_list);
1194         if (matcher_str == NULL) {
1195                 return;
1196         }
1197         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), action_str);
1198
1199         g_free(action_str);
1200         g_free(matcher_str);
1201 }
1202
1203 static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
1204                                  gpointer data)
1205 {
1206         prefs_filtering_cancel();
1207         return TRUE;
1208 }
1209
1210 static gboolean prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
1211                                      gpointer data)
1212 {
1213         if (event && event->keyval == GDK_Escape) {
1214                 prefs_filtering_cancel();
1215                 return TRUE;                    
1216         }
1217         return FALSE;
1218 }
1219
1220 static void prefs_filtering_ok(void)
1221 {
1222         FilteringProp * prop;
1223         gchar * str;
1224         gchar * filtering_str;
1225         gint row = 1;
1226         AlertValue val;
1227         
1228         prop = prefs_filtering_dialog_to_filtering(FALSE);
1229         if (prop != NULL) {
1230                 str = filteringprop_to_string(prop);
1231
1232                 while (NULL != (filtering_str = (prefs_filtering_list_view_get_rule
1233                                                         (filtering.cond_list_view,
1234                                                          row)))) {
1235                         if (strcmp(filtering_str, str) == 0)
1236                                 break;
1237                         row++;
1238                         g_free(filtering_str);
1239                 }       
1240
1241                 if (!filtering_str) {
1242                         val = alertpanel(_("Entry not saved"),
1243                                  _("The entry was not saved. Close anyway?"),
1244                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1245                         if (G_ALERTDEFAULT != val) {
1246                                 g_free(filtering_str);
1247                                 g_free(str); /* fixed two leaks: huzzah! */
1248                                 filteringprop_free(prop);
1249                                 return;
1250                         }
1251                 }               
1252
1253                 g_free(filtering_str);
1254                 g_free(str);
1255                 filteringprop_free(prop); /* fixed a leak: huzzah! */
1256         } else {
1257                 gchar *name, *condition, *action;
1258                 name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
1259                 condition = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1260                 action = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1261                 if (strlen(name) || 
1262                     strlen(condition) || 
1263                     strlen(action)) {
1264                         val = alertpanel(_("Entry not saved"),
1265                                  _("The entry was not saved. Close anyway?"),
1266                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1267                         if (G_ALERTDEFAULT != val) {
1268                                 g_free(name);
1269                                 g_free(condition);
1270                                 g_free(action);
1271                                 return;
1272                         }
1273                 }
1274                 g_free(name);
1275                 g_free(condition);
1276                 g_free(action);
1277         }
1278         prefs_filtering_set_list();
1279         prefs_matcher_write_config();
1280         prefs_filtering_close();
1281 }
1282
1283 static void prefs_filtering_cancel(void)
1284 {
1285         prefs_matcher_read_config();
1286         prefs_filtering_close();
1287 }
1288
1289 static GtkListStore* prefs_filtering_create_data_store(void)
1290 {
1291         return gtk_list_store_new(N_PREFS_FILTERING_COLUMNS,
1292                                   G_TYPE_BOOLEAN,
1293                                   G_TYPE_STRING,
1294                                   G_TYPE_INT,
1295                                   G_TYPE_STRING,
1296                                   G_TYPE_STRING,
1297                                   G_TYPE_BOOLEAN,
1298                                  -1);
1299 }
1300
1301 /*!
1302  *\brief        Insert filtering rule into store. Note that we access the
1303  *              tree view / store by index, which is a bit suboptimal, but
1304  *              at least it made GTK 2 porting easier.
1305  *
1306  *\param        list_store Store to operate on
1307  *\param        row -1 to add a new rule to store, else change an existing
1308  *              row
1309  *\param        enabled TRUE if rule is enabled
1310  *\param        name The Name of rule
1311  *\param        account_id The account ID
1312  *\param        account_name The account name or All or (New)
1313  *\param        rule String representation of rule
1314  *\param        prop TRUE if valid filtering rule; if FALSE it's the first
1315  *              entry in the store ("(New)").
1316  *
1317  *\return       int Row of inserted / changed rule.
1318  */
1319 static gint prefs_filtering_list_view_insert_rule(GtkListStore *list_store,
1320                                                   gint row,
1321                                                   gboolean enabled,
1322                                                   const gchar *name,
1323                                                   gint account_id,
1324                                                   const gchar *account_name,
1325                                                   const gchar *rule,
1326                                                   gboolean prop) 
1327 {
1328         GtkTreeIter iter;
1329
1330         /* check if valid row at all */
1331         if (row >= 0) {
1332                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1333                                                    &iter, NULL, row))
1334                         row = -1;                                                  
1335         }
1336
1337         if (row < 0) {
1338                 /* append new */
1339                 gtk_list_store_append(list_store, &iter);
1340                 gtk_list_store_set(list_store, &iter, 
1341                                    PREFS_FILTERING_ENABLED, enabled,
1342                                    PREFS_FILTERING_NAME, name,
1343                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1344                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1345                                    PREFS_FILTERING_RULE, rule,
1346                                    PREFS_FILTERING_PROP, prop,
1347                                    -1);
1348                 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store),
1349                                                       NULL) - 1;
1350         } else {
1351                 /* change existing */
1352                 gtk_list_store_set(list_store, &iter, 
1353                                    PREFS_FILTERING_ENABLED, enabled,
1354                                    PREFS_FILTERING_NAME, name,
1355                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1356                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1357                                    PREFS_FILTERING_RULE, rule,
1358                                    -1);
1359                 return row;                                
1360         }
1361 }
1362
1363 /*!
1364  *\return       gchar * Rule at specified row - should be freed.
1365  */
1366 static gchar *prefs_filtering_list_view_get_rule(GtkWidget *list, gint row)
1367 {       
1368         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1369         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1370         GtkTreeIter iter;
1371         gchar *result;
1372
1373         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1374                 return NULL;
1375         
1376         gtk_tree_model_get(model, &iter, 
1377                            PREFS_FILTERING_RULE, &result,
1378                            -1);
1379         
1380         return result;
1381 }
1382
1383 static void prefs_filtering_list_view_get_rule_name(GtkWidget *list, gint row,
1384                                 gboolean *enabled, gchar **name, gint *account_id)
1385 {       
1386         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1387         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1388         GtkTreeIter iter;
1389
1390         *enabled = TRUE;
1391         *name = NULL;
1392
1393         if (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1394                 gtk_tree_model_get(model, &iter, 
1395                                    PREFS_FILTERING_ENABLED, enabled,
1396                                    PREFS_FILTERING_NAME, name,
1397                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1398                                    -1);
1399         }
1400 }
1401
1402 /*!
1403  *\brief        Create list view for filtering
1404  */
1405 static GtkWidget *prefs_filtering_list_view_create(void)
1406 {
1407         GtkTreeView *list_view;
1408         GtkTreeSelection *selector;
1409
1410         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL
1411                 (prefs_filtering_create_data_store())));
1412         
1413         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1414         gtk_tree_view_set_reorderable(list_view, TRUE);
1415         
1416         selector = gtk_tree_view_get_selection(list_view);
1417         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1418         gtk_tree_selection_set_select_function(selector, prefs_filtering_selected,
1419                                                NULL, NULL);
1420
1421         /* create the columns */
1422         prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view));
1423
1424         return GTK_WIDGET(list_view);
1425 }
1426
1427 static void prefs_filtering_enable_toggled(GtkCellRendererToggle *widget,
1428                 gchar *path,
1429                 GtkWidget *list_view)
1430 {
1431         GtkTreeIter iter;
1432         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1433         gboolean enabled = TRUE;
1434
1435         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1436                 return;
1437
1438         gtk_tree_model_get(model, &iter,
1439                            PREFS_FILTERING_ENABLED, &enabled,
1440                            -1);
1441
1442         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1443                            PREFS_FILTERING_ENABLED, !enabled,
1444                            -1);
1445 }
1446
1447 static void prefs_filtering_create_list_view_columns(GtkWidget *list_view)
1448 {
1449         GtkTreeViewColumn *column;
1450         GtkCellRenderer *renderer;
1451
1452         renderer = gtk_cell_renderer_toggle_new();
1453         g_object_set(renderer,
1454                      "radio", FALSE,
1455                      "activatable", TRUE,
1456                      NULL);
1457         column = gtk_tree_view_column_new_with_attributes
1458                 (_("Enable"), /* FIXME : Enable, Enabled, or 'E' ? */
1459                  renderer,
1460                  "active", PREFS_FILTERING_ENABLED,
1461                  NULL);
1462         gtk_tree_view_column_set_alignment (column, 0.5);
1463         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1464         g_signal_connect(G_OBJECT(renderer), "toggled",
1465                          G_CALLBACK(prefs_filtering_enable_toggled),
1466                          list_view);
1467
1468         renderer = gtk_cell_renderer_text_new();
1469         column = gtk_tree_view_column_new_with_attributes
1470                 (_("Name"),
1471                  renderer,
1472                  "text", PREFS_FILTERING_NAME,
1473                  NULL);
1474         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1475         gtk_tree_view_column_set_resizable(column, TRUE);
1476
1477         renderer = gtk_cell_renderer_text_new();
1478         column = gtk_tree_view_column_new_with_attributes
1479                 (_("Account"),
1480                  renderer,
1481                  "text", PREFS_FILTERING_ACCOUNT_NAME,
1482                  NULL);
1483         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1484         gtk_tree_view_column_set_resizable(column, TRUE);
1485
1486         filtering.account_name_column = column;
1487                 
1488         renderer = gtk_cell_renderer_text_new();
1489         column = gtk_tree_view_column_new_with_attributes
1490                 (_("Rule"),
1491                  renderer,
1492                  "text", PREFS_FILTERING_RULE,
1493                  NULL);
1494         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1495 }
1496
1497 static gboolean gtkut_tree_iter_comp(GtkTreeModel *model, 
1498                                      GtkTreeIter *iter1, 
1499                                      GtkTreeIter *iter2)
1500 {
1501         GtkTreePath *path1 = gtk_tree_model_get_path(model, iter1);
1502         GtkTreePath *path2 = gtk_tree_model_get_path(model, iter2);
1503         gboolean result;
1504
1505         result = gtk_tree_path_compare(path1, path2) == 0;
1506
1507         gtk_tree_path_free(path1);
1508         gtk_tree_path_free(path2);
1509         
1510         return result;
1511 }
1512
1513 /*!
1514  *\brief        Get selected row number.
1515  */
1516 static gint prefs_filtering_get_selected_row(GtkWidget *list_view)
1517 {
1518         GtkTreeView *view = GTK_TREE_VIEW(list_view);
1519         GtkTreeModel *model = gtk_tree_view_get_model(view);
1520         int n_rows = gtk_tree_model_iter_n_children(model, NULL);
1521         GtkTreeSelection *selection;
1522         GtkTreeIter iter;
1523         int row;
1524
1525         if (n_rows == 0) 
1526                 return -1;
1527         
1528         selection = gtk_tree_view_get_selection(view);
1529         if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1530                 return -1;
1531         
1532         /* get all iterators and compare them... */
1533         for (row = 0; row < n_rows; row++) {
1534                 GtkTreeIter itern;
1535
1536                 gtk_tree_model_iter_nth_child(model, &itern, NULL, row);
1537                 if (gtkut_tree_iter_comp(model, &iter, &itern))
1538                         return row;
1539         }
1540         
1541         return -1;
1542 }
1543
1544 static gboolean prefs_filtering_list_view_select_row(GtkWidget *list, gint row)
1545 {
1546         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1547         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
1548         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1549         GtkTreeIter iter;
1550         GtkTreePath *path;
1551
1552         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1553                 return FALSE;
1554         
1555         gtk_tree_selection_select_iter(selection, &iter);
1556
1557         path = gtk_tree_model_get_path(model, &iter);
1558         gtk_tree_view_set_cursor(list_view, path, NULL, FALSE);
1559         gtk_tree_path_free(path);
1560         
1561         return TRUE;
1562 }
1563
1564 /*!
1565  *\brief        Triggered when a row is selected
1566  */
1567 static gboolean prefs_filtering_selected(GtkTreeSelection *selector,
1568                                          GtkTreeModel *model, 
1569                                          GtkTreePath *path,
1570                                          gboolean currently_selected,
1571                                          gpointer data)
1572 {
1573         if (currently_selected)
1574                 return TRUE;
1575         else {          
1576                 gboolean has_prop  = FALSE;
1577                 GtkTreeIter iter;
1578
1579                 gtk_tree_model_get_iter(model, &iter, path);
1580                 gtk_tree_model_get(model, &iter,
1581                                    PREFS_FILTERING_PROP, &has_prop,
1582                                    -1);
1583
1584                 if (has_prop) {
1585                         FilteringProp *prop;
1586                         gchar *filtering_str = NULL;
1587                         gchar *name = NULL;
1588                         gint account_id = 0;
1589
1590                         gtk_tree_model_get(model, &iter,
1591                                            PREFS_FILTERING_RULE, &filtering_str,
1592                                            -1);
1593                         gtk_tree_model_get(model, &iter,
1594                                            PREFS_FILTERING_NAME, &name,
1595                                            -1);
1596                         gtk_tree_model_get(model, &iter,
1597                                            PREFS_FILTERING_ACCOUNT_ID, &account_id,
1598                                            -1);
1599
1600                         prop = matcher_parser_get_filtering(filtering_str);
1601                         if (prop) { 
1602                                 prop->name = g_strdup(name);
1603                                 prop->account_id = account_id;
1604                                 prefs_filtering_select_set(prop);
1605                                 filteringprop_free(prop);
1606                         }                               
1607                         g_free(name);
1608                         g_free(filtering_str);
1609                 } else
1610                         prefs_filtering_reset_dialog();
1611         }               
1612
1613         return TRUE;
1614 }
1615