8db88598bfd775fc12bc79392a09183063ed2fbd
[claws.git] / src / prefs_filtering.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2013 Hiroyuki Yamamoto and the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.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 "folder_item_prefs.h"
50 #include "filtering.h"
51 #include "addr_compl.h"
52 #include "manual.h"
53 #include "combobox.h"
54 #include "menu.h"
55 #include "account.h"
56
57 #include "matcher_parser.h"
58 #include "matcher.h"
59 #include "prefs_filtering_action.h"
60
61 enum {
62         PREFS_FILTERING_ENABLED,
63         PREFS_FILTERING_NAME,
64         PREFS_FILTERING_ACCOUNT_ID,
65         PREFS_FILTERING_ACCOUNT_NAME,
66         PREFS_FILTERING_RULE,
67         PREFS_FILTERING_PROP,
68         N_PREFS_FILTERING_COLUMNS
69 };
70
71 struct _Filtering {
72         GtkWidget *window;
73
74         GtkWidget *help_btn;
75         GtkWidget *ok_btn;
76         GtkWidget *name_entry;
77         GtkWidget *account_label;
78         GtkWidget *account_combobox;
79         GtkListStore *account_combobox_list;
80         GtkWidget *cond_entry;
81         GtkWidget *action_entry;
82
83         GtkWidget *cond_list_view;
84
85         GtkTreeViewColumn *account_name_column;
86 };
87
88 typedef struct _Filtering Filtering;
89
90 static Filtering  filtering;
91
92 static GSList ** p_processing_list = NULL;
93
94 /* widget creating functions */
95 static void prefs_filtering_create              (void);
96
97 static void prefs_filtering_set_dialog  (const gchar *header,
98                                          const gchar *key);
99 static void prefs_filtering_set_list    (void);
100
101 /* callback functions */
102 static gboolean prefs_filtering_search_func_cb (GtkTreeModel *model, gint column, 
103                                                 const gchar *key, GtkTreeIter *iter, 
104                                                 gpointer search_data);
105 static void prefs_filtering_register_cb (gpointer action, gpointer data);
106 static void prefs_filtering_substitute_cb       (gpointer action, gpointer data);
107 static void prefs_filtering_delete_cb   (gpointer action, gpointer data);
108 static void prefs_filtering_delete_all_cb(gpointer action, gpointer data);
109 static void prefs_filtering_clear_cb(gpointer action, gpointer data);
110 static void prefs_filtering_duplicate_cb(gpointer action, gpointer data);
111 static void prefs_filtering_top         (gpointer action, gpointer data);
112 static void prefs_filtering_page_up     (gpointer action, gpointer data);
113 static void prefs_filtering_up          (gpointer action, gpointer data);
114 static void prefs_filtering_down        (gpointer action, gpointer data);
115 static void prefs_filtering_page_down   (gpointer action, gpointer data);
116 static void prefs_filtering_bottom      (gpointer action, gpointer data);
117 static gint prefs_filtering_deleted     (GtkWidget      *widget,
118                                          GdkEventAny    *event,
119                                          gpointer        data);
120 static void prefs_filtering_row_selected(GtkTreeSelection *selection,
121                                          GtkTreeView *list_view);
122 static gboolean prefs_filtering_key_pressed(GtkWidget   *widget,
123                                          GdkEventKey    *event,
124                                          gpointer        data);
125 static void prefs_filtering_cancel      (gpointer action, gpointer data);
126 static void prefs_filtering_ok          (gpointer action, gpointer data);
127
128 static void prefs_filtering_condition_define    (gpointer action, gpointer data);
129 static void prefs_filtering_action_define       (gpointer action, gpointer data);
130 static gint prefs_filtering_list_view_set_row   (gint row, FilteringProp * prop);
131                                           
132 static void prefs_filtering_reset_dialog        (void);
133 static gboolean prefs_filtering_rename_tag_func(GNode *node, gpointer data);
134 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data);
135 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data);
136
137 static void delete_path(GSList ** p_filters, const gchar * path);
138
139
140 static GtkListStore* prefs_filtering_create_data_store  (void);
141 static gint prefs_filtering_list_view_insert_rule       (GtkListStore *list_store,
142                                                          gint row,
143                                                          gboolean enabled,
144                                                          const gchar *name,
145                                                          gint account_id,
146                                                          const gchar *account_name,
147                                                          const gchar *rule, 
148                                                          gboolean prop);
149 static gchar *prefs_filtering_list_view_get_rule        (GtkWidget *list, 
150                                                          gint row);
151 static void prefs_filtering_list_view_get_rule_info     (GtkWidget *list, 
152                                                          gint row,
153                                                          gboolean *enabled,
154                                                          gchar **name,
155                                                          gint *account_id);
156
157 static GtkWidget *prefs_filtering_list_view_create      (void);
158 static void prefs_filtering_create_list_view_columns    (GtkWidget *list_view);
159
160 static void prefs_filtering_select_row(GtkTreeView *list_view, GtkTreePath *path);
161
162 static void prefs_filtering_account_option_menu_populate(void);
163
164 static gulong signal_id = 0; /* filtering.help_btn clicked signal */
165
166 static int modified = FALSE;
167
168 void prefs_filtering_open(GSList ** p_processing,
169                           const gchar *title,
170                           const gchar *help_url_anchor,
171                           const gchar *header,
172                           const gchar *key,
173                           gboolean per_account_filtering)
174 {
175         if (prefs_rc_is_readonly(FILTERING_RC))
176                 return;
177
178         inc_lock();
179
180         if (!filtering.window) {
181                 prefs_filtering_create();
182         } else {
183                 gtk_list_store_clear(filtering.account_combobox_list);
184                 prefs_filtering_account_option_menu_populate();
185         }
186
187         gtk_tree_view_column_set_visible(filtering.account_name_column,
188                                                                          per_account_filtering);
189
190         manage_window_set_transient(GTK_WINDOW(filtering.window));
191         gtk_widget_grab_focus(filtering.ok_btn);
192         
193         if (title != NULL)
194                 gtk_window_set_title(GTK_WINDOW(filtering.window), title);
195         else
196                 gtk_window_set_title (GTK_WINDOW(filtering.window),
197                                       _("Filtering/Processing configuration"));
198
199         if (help_url_anchor != NULL) {
200                 if (signal_id != 0) {
201                         g_signal_handler_disconnect(
202                                         G_OBJECT(filtering.help_btn),
203                                         signal_id);
204                 }
205
206                 signal_id = g_signal_connect(G_OBJECT(filtering.help_btn),
207                                 "clicked",
208                                 G_CALLBACK(manual_open_with_anchor_cb),
209                                 (gchar*)help_url_anchor);
210         }
211         else {
212                 gtk_widget_set_sensitive(filtering.help_btn, FALSE);
213         }
214
215         p_processing_list = p_processing;
216         
217         prefs_filtering_set_dialog(header, key);
218         if (per_account_filtering) {
219                 gtk_widget_show(filtering.account_label);
220                 gtk_widget_show(filtering.account_combobox);
221         } else {
222                 gtk_widget_hide(filtering.account_label);
223                 gtk_widget_hide(filtering.account_combobox);
224                 combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
225         }
226
227         gtk_widget_show(filtering.window);
228         gtk_window_set_modal(GTK_WINDOW(filtering.window), TRUE);
229
230         start_address_completion(NULL);
231 }
232
233 static void prefs_filtering_size_allocate_cb(GtkWidget *widget,
234                                          GtkAllocation *allocation)
235 {
236         cm_return_if_fail(allocation != NULL);
237
238         prefs_common.filteringwin_width = allocation->width;
239         prefs_common.filteringwin_height = allocation->height;
240 }
241
242 /* prefs_filtering_close() - just to have one common exit point */
243 static void prefs_filtering_close(void)
244 {
245         GtkListStore *store;
246
247         end_address_completion();
248
249         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
250                                 (filtering.cond_list_view)));
251         gtk_list_store_clear(store);
252         gtk_widget_hide(filtering.window);
253         gtk_window_set_modal(GTK_WINDOW(filtering.window), FALSE);
254         inc_unlock();
255 }
256
257 static void prefs_filtering_account_option_menu_populate(void)
258 {
259         GList *accounts = NULL;
260         GtkTreeIter iter;
261
262         accounts = account_get_list();
263
264         cm_return_if_fail(accounts != NULL);
265
266         COMBOBOX_ADD(filtering.account_combobox_list, C_("Filtering Account Menu", "All"), 0);
267         COMBOBOX_ADD(filtering.account_combobox_list, NULL, 0);
268         for (; accounts != NULL; accounts = accounts->next) {
269                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
270
271                 COMBOBOX_ADD_ESCAPED(filtering.account_combobox_list, ac->account_name, ac->account_id);
272         }
273 }
274
275 static GtkWidget *prefs_filtering_account_option_menu(Filtering *filtering)
276 {
277         GtkWidget *optmenu = NULL;
278         GtkWidget *optmenubox = NULL;
279         GtkListStore *menu = NULL;
280         
281         optmenubox = gtk_event_box_new();
282         optmenu = gtkut_sc_combobox_create(optmenubox, FALSE);
283         menu = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(optmenu)));
284
285         filtering->account_combobox = optmenu;
286         filtering->account_combobox_list = menu;
287
288         prefs_filtering_account_option_menu_populate();
289
290         return optmenubox;
291 }
292
293 static void prefs_filtering_create(void)
294 {
295         GtkWidget *window;
296         GtkWidget *vbox;
297         GtkWidget *help_btn;
298         GtkWidget *cancel_btn;
299         GtkWidget *ok_btn;
300         GtkWidget *confirm_area;
301
302         GtkWidget *vbox1;
303         GtkWidget *reg_hbox;
304         GtkWidget *arrow;
305         GtkWidget *btn_hbox;
306
307         GtkWidget *name_label;
308         GtkWidget *name_entry;
309         GtkWidget *account_label;
310         GtkWidget *account_opt_menu;
311         GtkWidget *cond_label;
312         GtkWidget *cond_entry;
313         GtkWidget *cond_btn;
314         GtkWidget *action_label;
315         GtkWidget *action_entry;
316         GtkWidget *action_btn;
317
318         GtkWidget *reg_btn;
319         GtkWidget *subst_btn;
320         GtkWidget *del_btn;
321         GtkWidget *clear_btn;
322
323         GtkWidget *cond_hbox;
324         GtkWidget *cond_scrolledwin;
325         GtkWidget *cond_list_view;
326
327         GtkWidget *btn_vbox;
328         GtkWidget *top_btn;
329         GtkWidget *up_btn;
330         GtkWidget *down_btn;
331 #ifndef GENERIC_UMPC
332         GtkWidget *page_up_btn;
333         GtkWidget *page_down_btn;
334 #endif
335         GtkWidget *bottom_btn;
336         GtkWidget *table;
337         static GdkGeometry geometry;
338
339         debug_print("Creating filtering configuration window...\n");
340
341         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_filtering");
342         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
343         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
344         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
345
346         vbox = gtk_vbox_new (FALSE, 6);
347         gtk_widget_show (vbox);
348         gtk_container_add (GTK_CONTAINER (window), vbox);
349
350         gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
351                         &cancel_btn, GTK_STOCK_CANCEL,
352                         &ok_btn, GTK_STOCK_OK,
353                         NULL, NULL);
354         gtk_widget_show (confirm_area);
355         gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
356         gtk_widget_grab_default (ok_btn);
357
358         gtk_window_set_title (GTK_WINDOW(window),
359                                     _("Filtering/Processing configuration"));
360
361         g_signal_connect(G_OBJECT(window), "delete_event",
362                          G_CALLBACK(prefs_filtering_deleted), NULL);
363         g_signal_connect(G_OBJECT(window), "size_allocate",
364                          G_CALLBACK(prefs_filtering_size_allocate_cb), NULL);
365         g_signal_connect(G_OBJECT(window), "key_press_event",
366                          G_CALLBACK(prefs_filtering_key_pressed), NULL);
367         MANAGE_WINDOW_SIGNALS_CONNECT (window);
368         g_signal_connect(G_OBJECT(ok_btn), "clicked",
369                          G_CALLBACK(prefs_filtering_ok), NULL);
370         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
371                          G_CALLBACK(prefs_filtering_cancel), NULL);
372
373         vbox1 = gtk_vbox_new (FALSE, VSPACING);
374         gtk_widget_show (vbox1);
375         gtk_box_pack_start (GTK_BOX (vbox), vbox1, FALSE, TRUE, 0);
376         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
377
378         table = gtk_table_new(4, 3, FALSE);
379         gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
380         gtk_table_set_col_spacings (GTK_TABLE (table), 4);
381         gtk_widget_show(table);
382         gtk_box_pack_start (GTK_BOX (vbox1), table, TRUE, TRUE, 0);
383
384         name_label = gtk_label_new (_("Name"));
385         gtk_widget_show (name_label);
386         gtk_misc_set_alignment (GTK_MISC (name_label), 1, 0.5);
387         gtk_table_attach (GTK_TABLE (table), name_label, 0, 1, 0, 1,
388                           (GtkAttachOptions) (GTK_FILL),
389                           (GtkAttachOptions) (0), 0, 0);
390
391         name_entry = gtk_entry_new ();
392         gtk_widget_show (name_entry);
393         gtk_table_attach (GTK_TABLE (table), name_entry, 1, 2, 0, 1,
394                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
395                           (GtkAttachOptions) (0), 0, 0);
396
397         account_label = gtk_label_new (_("Account"));
398         gtk_widget_show (account_label);
399         gtk_misc_set_alignment (GTK_MISC (account_label), 1, 0.5);
400         gtk_table_attach (GTK_TABLE (table), account_label, 0, 1, 1, 2,
401                           (GtkAttachOptions) (GTK_FILL),
402                           (GtkAttachOptions) (0), 0, 0);
403
404         account_opt_menu = prefs_filtering_account_option_menu(&filtering);
405         gtk_widget_show (account_opt_menu);
406         gtk_table_attach (GTK_TABLE (table), account_opt_menu, 1, 2, 1, 2,
407                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
408                           (GtkAttachOptions) (0), 0, 0);
409         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
410
411         cond_label = gtk_label_new (_("Condition"));
412         gtk_widget_show (cond_label);
413         gtk_misc_set_alignment (GTK_MISC (cond_label), 1, 0.5);
414         gtk_table_attach (GTK_TABLE (table), cond_label, 0, 1, 2, 3,
415                           (GtkAttachOptions) (GTK_FILL),
416                           (GtkAttachOptions) (0), 0, 0);
417
418         cond_entry = gtk_entry_new ();
419         gtk_widget_show (cond_entry);
420         gtk_table_attach (GTK_TABLE (table), cond_entry, 1, 2, 2, 3,
421                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
422                           (GtkAttachOptions) (0), 0, 0);
423
424         cond_btn =  gtk_button_new_with_mnemonic (_(" Def_ine... "));
425         gtk_widget_show (cond_btn);
426         gtk_table_attach (GTK_TABLE (table), cond_btn, 2, 3, 2, 3,
427                           (GtkAttachOptions) (GTK_FILL),
428                           (GtkAttachOptions) (0), 2, 2);
429         g_signal_connect(G_OBJECT (cond_btn), "clicked",
430                          G_CALLBACK(prefs_filtering_condition_define),
431                          NULL);
432
433         action_label = gtk_label_new (_("Action"));
434         gtk_widget_show (action_label);
435         gtk_misc_set_alignment (GTK_MISC (action_label), 1, 0.5);
436         gtk_table_attach (GTK_TABLE (table), action_label, 0, 1, 3, 4,
437                           (GtkAttachOptions) (GTK_FILL),
438                           (GtkAttachOptions) (0), 0, 0);
439
440         action_entry = gtk_entry_new ();
441         gtk_widget_show (action_entry);
442         gtk_table_attach (GTK_TABLE (table), action_entry, 1, 2, 3, 4,
443                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
444                           (GtkAttachOptions) (0), 0, 0);
445
446         action_btn =  gtk_button_new_with_mnemonic (_(" De_fine... "));
447         gtk_widget_show (action_btn);
448         gtk_table_attach (GTK_TABLE (table), action_btn, 2, 3, 3, 4,
449                           (GtkAttachOptions) (GTK_FILL),
450                           (GtkAttachOptions) (0), 2, 2);
451         g_signal_connect(G_OBJECT (action_btn), "clicked",
452                          G_CALLBACK(prefs_filtering_action_define),
453                          NULL);
454                          
455         /* register / substitute / delete */
456         reg_hbox = gtk_hbox_new (FALSE, 4);
457         gtk_widget_show (reg_hbox);
458         gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
459
460         arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
461         gtk_widget_show (arrow);
462         gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
463         gtk_widget_set_size_request (arrow, -1, 16);
464
465         btn_hbox = gtk_hbox_new (TRUE, 4);
466         gtk_widget_show (btn_hbox);
467         gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
468
469         reg_btn = gtk_button_new_from_stock (GTK_STOCK_ADD);
470         gtk_widget_show (reg_btn);
471         gtk_box_pack_start (GTK_BOX (btn_hbox), reg_btn, FALSE, TRUE, 0);
472         g_signal_connect(G_OBJECT (reg_btn), "clicked",
473                          G_CALLBACK(prefs_filtering_register_cb), NULL);
474         CLAWS_SET_TIP(reg_btn,
475                         _("Append the new rule above to the list"));
476
477         subst_btn = gtkut_get_replace_btn (_("_Replace"));
478         gtk_widget_show (subst_btn);
479         gtk_box_pack_start (GTK_BOX (btn_hbox), subst_btn, FALSE, TRUE, 0);
480         g_signal_connect(G_OBJECT (subst_btn), "clicked",
481                          G_CALLBACK(prefs_filtering_substitute_cb),
482                          NULL);
483         CLAWS_SET_TIP(subst_btn,
484                         _("Replace the selected rule in list with the rule above"));
485
486         del_btn = gtk_button_new_with_mnemonic (_("D_elete"));
487         gtk_button_set_image(GTK_BUTTON(del_btn),
488                         gtk_image_new_from_stock(GTK_STOCK_REMOVE,GTK_ICON_SIZE_BUTTON));
489         gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
490         g_signal_connect(G_OBJECT (del_btn), "clicked",
491                         G_CALLBACK(prefs_filtering_delete_cb), NULL);
492         CLAWS_SET_TIP(del_btn,
493                         _("Delete the selected rule from the list"));
494
495         clear_btn = gtk_button_new_with_mnemonic (_("C_lear"));
496         gtk_button_set_image(GTK_BUTTON(clear_btn),
497                         gtk_image_new_from_stock(GTK_STOCK_CLEAR,GTK_ICON_SIZE_BUTTON));
498         gtk_widget_show (clear_btn);
499         gtk_box_pack_start (GTK_BOX (btn_hbox), clear_btn, FALSE, TRUE, 0);
500         g_signal_connect(G_OBJECT (clear_btn), "clicked",
501                         G_CALLBACK(prefs_filtering_clear_cb), NULL);
502         CLAWS_SET_TIP(clear_btn,
503                         _("Clear all the input fields in the dialog"));
504
505         cond_hbox = gtk_hbox_new (FALSE, 8);
506         gtk_widget_show (cond_hbox);
507         gtk_box_pack_start (GTK_BOX (vbox), cond_hbox, TRUE, TRUE, 0);
508
509         cond_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
510         gtk_widget_show (cond_scrolledwin);
511         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(cond_scrolledwin),
512                                             GTK_SHADOW_ETCHED_IN);
513         gtk_widget_set_size_request (cond_scrolledwin, -1, 150);
514         gtk_box_pack_start (GTK_BOX (cond_hbox), cond_scrolledwin,
515                             TRUE, TRUE, 0);
516         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin),
517                                         GTK_POLICY_AUTOMATIC,
518                                         GTK_POLICY_AUTOMATIC);
519
520         cond_list_view = prefs_filtering_list_view_create();    
521         gtk_widget_show (cond_list_view);
522         gtk_container_add (GTK_CONTAINER (cond_scrolledwin), cond_list_view);
523
524         btn_vbox = gtk_vbox_new (FALSE, 8);
525         gtk_widget_show (btn_vbox);
526         gtk_box_pack_start (GTK_BOX (cond_hbox), btn_vbox, FALSE, FALSE, 0);
527
528         top_btn = gtk_button_new_from_stock (GTK_STOCK_GOTO_TOP);
529         gtk_widget_show (top_btn);
530         gtk_box_pack_start (GTK_BOX (btn_vbox), top_btn, FALSE, FALSE, 0);
531         g_signal_connect(G_OBJECT (top_btn), "clicked",
532                          G_CALLBACK(prefs_filtering_top), NULL);
533         CLAWS_SET_TIP(top_btn,
534                         _("Move the selected rule to the top"));
535
536 #ifndef GENERIC_UMPC
537         page_up_btn = gtk_button_new_with_mnemonic (_("Page u_p"));
538         gtk_button_set_image(GTK_BUTTON(page_up_btn),
539                         gtk_image_new_from_stock(GTK_STOCK_GO_UP,GTK_ICON_SIZE_BUTTON));
540         gtk_widget_show (page_up_btn);
541         gtk_box_pack_start (GTK_BOX (btn_vbox), page_up_btn, FALSE, FALSE, 0);
542         g_signal_connect(G_OBJECT (page_up_btn), "clicked",
543                          G_CALLBACK(prefs_filtering_page_up), NULL);
544         CLAWS_SET_TIP(page_up_btn,
545                         _("Move the selected rule one page up"));
546 #endif
547
548         up_btn = gtk_button_new_from_stock (GTK_STOCK_GO_UP);
549         gtk_widget_show (up_btn);
550         gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
551         g_signal_connect(G_OBJECT (up_btn), "clicked",
552                          G_CALLBACK(prefs_filtering_up), NULL);
553         CLAWS_SET_TIP(up_btn,
554                         _("Move the selected rule up"));
555
556         down_btn = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN);
557         gtk_widget_show (down_btn);
558         gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
559         g_signal_connect(G_OBJECT (down_btn), "clicked",
560                          G_CALLBACK(prefs_filtering_down), NULL);
561         CLAWS_SET_TIP(down_btn,
562                         _("Move the selected rule down"));
563
564 #ifndef GENERIC_UMPC
565         page_down_btn = gtk_button_new_with_mnemonic (_("Page dow_n"));
566         gtk_button_set_image(GTK_BUTTON(page_down_btn),
567                         gtk_image_new_from_stock(GTK_STOCK_GO_DOWN,GTK_ICON_SIZE_BUTTON));
568         gtk_widget_show (page_down_btn);
569         gtk_box_pack_start (GTK_BOX (btn_vbox), page_down_btn, FALSE, FALSE, 0);
570         g_signal_connect(G_OBJECT (page_down_btn), "clicked",
571                          G_CALLBACK(prefs_filtering_page_down), NULL);
572         CLAWS_SET_TIP(page_down_btn,
573                         _("Move the selected rule one page down"));
574 #endif
575
576         bottom_btn = gtk_button_new_from_stock (GTK_STOCK_GOTO_BOTTOM);
577         gtk_widget_show (bottom_btn);
578         gtk_box_pack_start (GTK_BOX (btn_vbox), bottom_btn, FALSE, FALSE, 0);
579         g_signal_connect(G_OBJECT (bottom_btn), "clicked",
580                          G_CALLBACK(prefs_filtering_bottom), NULL);
581         CLAWS_SET_TIP(bottom_btn,
582                         _("Move the selected rule to the bottom"));
583
584         if (!geometry.min_height) {
585                 geometry.min_width = 500;
586                 geometry.min_height = 460;
587         }
588
589         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
590                                       GDK_HINT_MIN_SIZE);
591         gtk_widget_set_size_request(window, prefs_common.filteringwin_width,
592                                     prefs_common.filteringwin_height);
593
594         gtk_widget_show_all(window);
595
596         filtering.window    = window;
597         filtering.help_btn = help_btn;
598         filtering.ok_btn = ok_btn;
599
600         filtering.name_entry     = name_entry;
601         filtering.cond_entry     = cond_entry;
602         filtering.action_entry   = action_entry;
603         filtering.cond_list_view = cond_list_view;
604         filtering.account_label  = account_label;
605 }
606
607 static void rename_tag(GSList * filters,
608                         const gchar * old_tag, const gchar * new_tag);
609
610 void prefs_filtering_rename_tag(const gchar *old_tag, const gchar *new_tag)
611 {
612         GList * cur;
613         const gchar *tags[2] = {NULL, NULL};
614         tags[0] = old_tag;
615         tags[1] = new_tag;
616         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
617                 Folder *folder;
618                 folder = (Folder *) cur->data;
619                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
620                                 prefs_filtering_rename_tag_func, tags);
621         }
622         
623         rename_tag(pre_global_processing, old_tag, new_tag);
624         rename_tag(post_global_processing, old_tag, new_tag);
625         rename_tag(filtering_rules, old_tag, new_tag);
626         
627         prefs_matcher_write_config();
628 }
629
630
631 static void rename_path(GSList * filters,
632                         const gchar * old_path, const gchar * new_path);
633
634 void prefs_filtering_rename_path(const gchar *old_path, const gchar *new_path)
635 {
636         GList * cur;
637         const gchar *paths[2] = {NULL, NULL};
638         paths[0] = old_path;
639         paths[1] = new_path;
640         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
641                 Folder *folder;
642                 folder = (Folder *) cur->data;
643                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
644                                 prefs_filtering_rename_path_func, paths);
645         }
646         
647         rename_path(pre_global_processing, old_path, new_path);
648         rename_path(post_global_processing, old_path, new_path);
649         rename_path(filtering_rules, old_path, new_path);
650         
651         prefs_matcher_write_config();
652 }
653
654 static void rename_path(GSList * filters,
655                         const gchar * old_path, const gchar * new_path)
656 {
657         GSList * cur;
658
659         for (cur = filters; cur != NULL; cur = cur->next) {
660                 FilteringProp   *filtering = (FilteringProp *)cur->data;
661                 filtering_action_list_rename_path(filtering->action_list,
662                                                   old_path, new_path);
663         }
664 }
665
666 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
667 {
668         GSList *filters;
669         const gchar * old_path;
670         const gchar * new_path;
671         const gchar ** paths;
672         FolderItem *item;
673         
674         paths = data;
675         old_path = paths[0];
676         new_path = paths[1];
677
678         cm_return_val_if_fail(old_path != NULL, FALSE);
679         cm_return_val_if_fail(new_path != NULL, FALSE);
680         cm_return_val_if_fail(node != NULL, FALSE);
681
682         item = node->data;
683         if (!item || !item->prefs)
684                 return FALSE;
685         filters = item->prefs->processing;
686
687         rename_path(filters, old_path, new_path);
688
689         return FALSE;
690 }
691
692 static void rename_tag(GSList * filters,
693                         const gchar * old_tag, const gchar * new_tag)
694 {
695         GSList * action_cur;
696         GSList * cur;
697
698         for (cur = filters; cur != NULL; cur = cur->next) {
699                 FilteringProp   *filtering = (FilteringProp *)cur->data;
700                 
701                 for(action_cur = filtering->action_list ; action_cur != NULL ;
702                     action_cur = action_cur->next) {
703
704                         FilteringAction *action = action_cur->data;
705                         
706                         if (action->type != MATCHACTION_SET_TAG &&
707                             action->type != MATCHACTION_UNSET_TAG)
708                                 continue;
709                         if (!action->destination)
710                                 continue;
711                         if (!strcmp(action->destination, old_tag)) {
712                                 g_free(action->destination);
713                                 action->destination = g_strdup(new_tag);
714                         }
715                 }
716         }
717 }
718
719 static gboolean prefs_filtering_rename_tag_func(GNode *node, gpointer data)
720 {
721         GSList *filters;
722         const gchar * old_tag;
723         const gchar * new_tag;
724         const gchar ** tags;
725         FolderItem *item;
726         
727         tags = data;
728         old_tag = tags[0];
729         new_tag = tags[1];
730
731         cm_return_val_if_fail(old_tag != NULL, FALSE);
732         cm_return_val_if_fail(new_tag != NULL, FALSE);
733         cm_return_val_if_fail(node != NULL, FALSE);
734
735         item = node->data;
736         if (!item || !item->prefs)
737                 return FALSE;
738         filters = item->prefs->processing;
739
740         rename_tag(filters, old_tag, new_tag);
741
742         return FALSE;
743 }
744
745 void prefs_filtering_delete_path(const gchar *path)
746 {
747         GList * cur;
748         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
749                 Folder *folder;
750                 folder = (Folder *) cur->data;
751                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
752                                 prefs_filtering_delete_path_func, (gchar *)path);
753         }
754         delete_path(&pre_global_processing, path);
755         delete_path(&post_global_processing, path);
756         delete_path(&filtering_rules, path);
757         
758         prefs_matcher_write_config();
759 }
760
761 static void delete_path(GSList ** p_filters, const gchar * path)
762 {
763         GSList * filters;
764         GSList * duplist;
765         gchar *suffix;
766         gint destlen;
767         gint prefixlen;
768         gint pathlen;
769         GSList * action_cur;
770         GSList * cur;
771         
772         filters = *p_filters;
773         pathlen = strlen(path);
774         duplist = g_slist_copy(filters);
775         for (cur = duplist ; cur != NULL; cur = g_slist_next(cur)) {
776                 FilteringProp *filtering = (FilteringProp *) cur->data;
777                 
778                 for(action_cur = filtering->action_list ; action_cur != NULL ;
779                     action_cur = action_cur->next) {
780                 
781                         FilteringAction *action;
782                         
783                         action = action_cur->data;
784                         
785                         if (action->type == MATCHACTION_SET_TAG ||
786                             action->type == MATCHACTION_UNSET_TAG)
787                                 continue;
788                         if (!action->destination) 
789                                 continue;
790                         
791                         destlen = strlen(action->destination);
792                         
793                         if (destlen > pathlen) {
794                                 prefixlen = destlen - pathlen;
795                                 suffix = action->destination + prefixlen;
796                                 
797                                 if (suffix && !strncmp(path, suffix, pathlen)) {
798                                         filteringprop_free(filtering);
799                                         filters = g_slist_remove(filters, filtering);
800                                 }
801                         } else if (strcmp(action->destination, path) == 0) {
802                                 filteringprop_free(filtering);
803                                 filters = g_slist_remove(filters, filtering);
804                         }
805                 }
806         }                
807         g_slist_free(duplist);
808         
809         * p_filters = filters;
810 }
811
812 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data)
813 {
814         const gchar *path = data;
815         FolderItem *item;
816         GSList ** p_filters;
817         
818         cm_return_val_if_fail(path != NULL, FALSE);
819         cm_return_val_if_fail(node != NULL, FALSE);
820
821         item = node->data;
822         if (!item || !item->prefs)
823                 return FALSE;
824         p_filters = &item->prefs->processing;
825         
826         delete_path(p_filters, path);
827
828         return FALSE;
829 }
830
831 static void prefs_filtering_clear_list(GtkListStore *list_store)
832 {
833         gtk_list_store_clear(list_store);
834
835         /* add the place holder (New) at row 0 */
836         prefs_filtering_list_view_insert_rule(list_store, -1, 
837                                               FALSE,
838                                               _("(New)"),
839                                                   0,
840                                               _("(New)"),
841                                               _("(New)"),
842                                               FALSE);
843 }
844
845 static void prefs_filtering_set_dialog(const gchar *header, const gchar *key)
846 {
847         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
848         GSList *cur;
849         GSList * prefs_filtering;
850         gchar *cond_str;
851         GtkListStore *list_store;
852         
853         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
854         prefs_filtering_clear_list(list_store);
855
856         prefs_filtering = *p_processing_list;
857
858         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur)) {
859                 FilteringProp * prop = (FilteringProp *) cur->data;
860                 gchar *account_name = NULL;
861
862                 if (prop->account_id > 0) {
863                         PrefsAccount *ac_prefs = account_find_from_id(prop->account_id);
864
865                         if (ac_prefs)
866                                 account_name = ac_prefs->account_name;
867                 }
868                 if (account_name == NULL)
869                         account_name = (gchar *)C_("Filtering Account Menu", "All");
870
871                 cond_str = filteringprop_to_string(prop);
872                 subst_char(cond_str, '\t', ':');
873
874                 prefs_filtering_list_view_insert_rule(list_store, -1, 
875                                                       prop->enabled,
876                                                       prop->name,
877                                                           prop->account_id,
878                                                           account_name,
879                                                       cond_str, TRUE);
880                 
881                 g_free(cond_str);
882         }
883
884         prefs_filtering_reset_dialog();
885
886         if (header && key) {
887                 gchar * quoted_key;
888                 gchar *match_str;
889
890                 quoted_key = matcher_quote_str(key);
891                 
892                 match_str = g_strconcat(header, " ", get_matchparser_tab_str(MATCHTYPE_MATCHCASE),
893                                         " \"", quoted_key, "\"", NULL);
894                 g_free(quoted_key);
895                 
896                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), match_str);
897                 g_free(match_str);
898         }
899 }
900
901 static void prefs_filtering_reset_dialog(void)
902 {
903         gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), "");
904         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
905         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), "");
906         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), "");
907 }
908
909 static gboolean prefs_filtering_search_func_cb (GtkTreeModel *model, gint column, const gchar *key, 
910                                                 GtkTreeIter *iter, gpointer search_data) 
911 {
912         gchar *store_string;
913         gboolean retval;
914         GtkTreePath *path;
915
916         gtk_tree_model_get (model, iter, column, &store_string, -1);
917
918         if (!store_string || !key) return FALSE;
919
920
921         retval = (g_ascii_strncasecmp (key, store_string, strlen(key)) != 0);
922
923         g_free(store_string);
924         debug_print("selecting row\n");
925         path = gtk_tree_model_get_path(model, iter);
926         prefs_filtering_select_row(GTK_TREE_VIEW(filtering.cond_list_view), path);
927         gtk_tree_path_free(path);
928
929         return retval;
930 }
931
932 static void prefs_filtering_set_list(void)
933 {
934         gint row = 1;
935         FilteringProp *prop;
936         GSList * cur;
937         gchar * filtering_str;
938         GSList * prefs_filtering;
939
940         prefs_filtering = *p_processing_list;
941         for (cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
942                 filteringprop_free((FilteringProp *) cur->data);
943         g_slist_free(prefs_filtering);
944         prefs_filtering = NULL;
945         
946
947         while (NULL != (filtering_str = prefs_filtering_list_view_get_rule
948                                                 (filtering.cond_list_view, row))) {
949                 /* FIXME: this strcmp() is bogus: "(New)" should never
950                  * be inserted in the storage */
951                 if (strcmp(filtering_str, _("(New)")) != 0) {
952                         gboolean enabled;
953                         gchar *name;
954                         gint account_id = 0;
955
956                         prefs_filtering_list_view_get_rule_info(
957                                         filtering.cond_list_view, row,
958                                         &enabled, &name, &account_id);
959                         prop = matcher_parser_get_filtering(filtering_str);
960                         if (prop) {
961                                 prop->enabled = enabled;
962                                 if (prop->name != NULL)
963                                         g_free(prop->name);
964                                 prop->name = name;
965                                 prop->account_id = account_id;
966                                 prefs_filtering = 
967                                         g_slist_append(prefs_filtering, prop);
968                         }
969                 }
970
971                 g_free(filtering_str);
972                 row++;
973         }                               
974         
975         *p_processing_list = prefs_filtering;
976 }
977
978 static gint prefs_filtering_list_view_set_row(gint row, FilteringProp * prop)
979 {
980         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
981         gchar *str = NULL;
982         GtkListStore *list_store;
983         gchar *name = NULL;
984         gint account_id = 0;
985         gchar *account_name = (gchar *)C_("Filtering Account Menu", "All");
986         gboolean enabled = TRUE;
987
988         if (prop)
989                 str = filteringprop_to_string(prop);
990         if (str == NULL)
991                 return -1;
992
993         if (prop) {
994                 if (prop->name)
995                         name = prop->name;
996                 account_id = prop->account_id;
997                 if (account_id > 0)
998                         account_name = account_find_from_id(account_id)->account_name;
999                 enabled = prop->enabled;
1000         }
1001
1002         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
1003
1004         row = prefs_filtering_list_view_insert_rule(list_store, row,
1005                                                     enabled,
1006                                                     name,
1007                                                     account_id,
1008                                                         account_name,
1009                                                     str,
1010                                                     prop != NULL);
1011
1012         g_free(str);
1013
1014         return row;
1015 }
1016
1017 static void prefs_filtering_condition_define_done(MatcherList * matchers)
1018 {
1019         gchar * str;
1020
1021         if (matchers == NULL)
1022                 return;
1023
1024         str = matcherlist_to_string(matchers);
1025
1026         if (str != NULL) {
1027                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), str);
1028                 g_free(str);
1029         }
1030 }
1031
1032 static void prefs_filtering_condition_define(gpointer action, gpointer data)
1033 {
1034         gchar * cond_str;
1035         MatcherList * matchers = NULL;
1036
1037         cond_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1038
1039         if (*cond_str != '\0') {
1040                 matchers = matcher_parser_get_cond(cond_str, NULL);
1041                 if (matchers == NULL)
1042                         alertpanel_error(_("Condition string is not valid."));
1043         }
1044         
1045         g_free(cond_str);
1046
1047         prefs_matcher_open(matchers, prefs_filtering_condition_define_done);
1048
1049         if (matchers != NULL)
1050                 matcherlist_free(matchers);
1051 }
1052
1053 static void prefs_filtering_action_define_done(GSList * action_list)
1054 {
1055         gchar * str;
1056
1057         if (action_list == NULL)
1058                 return;
1059
1060         str = filteringaction_list_to_string(action_list);
1061
1062         if (str != NULL) {
1063                 gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), str);
1064                 g_free(str);
1065         }
1066 }
1067
1068 static void prefs_filtering_action_define(gpointer action, gpointer data)
1069 {
1070         gchar * action_str;
1071         GSList * action_list = NULL;
1072
1073         action_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1074
1075         if (*action_str != '\0') {
1076                 action_list = matcher_parser_get_action_list(action_str);
1077                 if (action_list == NULL)
1078                         alertpanel_error(_("Action string is not valid."));
1079         }
1080         
1081         g_free(action_str);
1082
1083         prefs_filtering_action_open(action_list,
1084             prefs_filtering_action_define_done);
1085
1086         if (action_list != NULL) {
1087                 GSList * cur;
1088                 for(cur = action_list ; cur != NULL ; cur = cur->next) {
1089                         filteringaction_free(cur->data);
1090                 }
1091         }
1092 }
1093
1094
1095 /* register / substitute delete buttons */
1096
1097
1098 static FilteringProp * prefs_filtering_dialog_to_filtering(gboolean alert)
1099 {
1100         MatcherList * cond;
1101         gboolean enabled = TRUE;
1102         gchar * name = NULL;
1103         gint account_id = 0;
1104         gchar * cond_str = NULL;
1105         gchar * action_str = NULL;
1106         FilteringProp * prop = NULL;
1107         GSList * action_list;
1108
1109         name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
1110         
1111         account_id = combobox_get_active_data(GTK_COMBO_BOX(filtering.account_combobox));
1112
1113         cond_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1114         if (*cond_str == '\0') {
1115                 if(alert == TRUE) alertpanel_error(_("Condition string is empty."));
1116                 goto fail;
1117         }
1118         
1119         action_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1120         if (*action_str == '\0') {
1121                 if(alert == TRUE) alertpanel_error(_("Action string is empty."));
1122                 goto fail;
1123         }
1124
1125         cond = matcher_parser_get_cond(cond_str, NULL);
1126
1127         if (cond == NULL) {
1128                 if(alert == TRUE) alertpanel_error(_("Condition string is not valid."));
1129                 goto fail;
1130         }
1131         
1132         action_list = matcher_parser_get_action_list(action_str);
1133         
1134
1135         if (action_list == NULL) {
1136                 if(alert == TRUE) alertpanel_error(_("Action string is not valid."));
1137                 goto fail;
1138         }
1139
1140         prop = filteringprop_new(enabled, name, account_id, cond, action_list);
1141
1142 fail:
1143         g_free(name);
1144         g_free(cond_str);
1145         g_free(action_str);
1146         return prop;
1147 }
1148
1149 static void prefs_filtering_register_cb(gpointer action, gpointer data)
1150 {
1151         FilteringProp *prop;
1152         
1153         prop = prefs_filtering_dialog_to_filtering(TRUE);
1154         if (prop == NULL)
1155                 return;
1156         prefs_filtering_list_view_set_row(-1, prop);
1157         
1158         filteringprop_free(prop);
1159
1160         prefs_filtering_reset_dialog();
1161         modified = TRUE;
1162 }
1163
1164 static void prefs_filtering_substitute_cb(gpointer action, gpointer data)
1165 {
1166         gint selected_row = gtkut_list_view_get_selected_row
1167                 (filtering.cond_list_view);
1168         FilteringProp *prop;
1169         gboolean enabled;
1170         gchar *name;
1171         gint account_id;
1172         
1173         if (selected_row <= 0)
1174                 return;
1175
1176         prop = prefs_filtering_dialog_to_filtering(TRUE);
1177
1178         if (prop == NULL) 
1179                 return;
1180
1181         /* prop->emabled is always TRUE here, re-use the value from the selected row 
1182            as we don't substitute this value from dialog */
1183         prefs_filtering_list_view_get_rule_info(
1184                         filtering.cond_list_view, selected_row,
1185                         &enabled, &name, &account_id);
1186         g_free(name); /* We're not using this. */
1187         prop->enabled = enabled;
1188
1189         prefs_filtering_list_view_set_row(selected_row, prop);
1190
1191         filteringprop_free(prop);
1192
1193         prefs_filtering_row_selected(gtk_tree_view_get_selection(
1194                                 GTK_TREE_VIEW(filtering.cond_list_view)),
1195                                 GTK_TREE_VIEW(filtering.cond_list_view));
1196         modified = TRUE;
1197 }
1198
1199 static void prefs_filtering_delete_cb(gpointer action, gpointer data)
1200 {
1201         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
1202         GtkTreeModel *model;
1203         GtkTreeIter iter;
1204         gint selected_row;
1205         
1206         selected_row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1207         if (selected_row <= 0) 
1208                 return; 
1209
1210         if (alertpanel(_("Delete rule"),
1211                        _("Do you really want to delete this rule?"),
1212                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, ALERTFOCUS_SECOND) == G_ALERTDEFAULT)
1213                 return;
1214
1215         model = gtk_tree_view_get_model(list_view);     
1216         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, selected_row))
1217                 return;
1218
1219         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1220
1221         prefs_filtering_reset_dialog();
1222         modified = TRUE;
1223 }
1224
1225 static void prefs_filtering_delete_all_cb(gpointer action, gpointer data)
1226 {
1227         GtkListStore *list_store;
1228         
1229         if (alertpanel(_("Delete all rules"),
1230                        _("Do you really want to delete all the rules?"),
1231                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, ALERTFOCUS_SECOND) == G_ALERTDEFAULT)
1232                 return;
1233
1234         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view)));
1235         prefs_filtering_clear_list(list_store);
1236
1237         prefs_filtering_reset_dialog();
1238         modified = TRUE;
1239 }
1240
1241 static void prefs_filtering_clear_cb(gpointer action, gpointer data)
1242 {
1243         prefs_filtering_reset_dialog();
1244 }
1245
1246 static void prefs_filtering_duplicate_cb(gpointer action, gpointer data)
1247 {
1248         gint selected_row = gtkut_list_view_get_selected_row
1249                 (filtering.cond_list_view);     
1250         FilteringProp *prop;
1251         gboolean enabled;
1252         gchar *name;
1253         gint account_id;
1254
1255         if (selected_row <= 0) 
1256                 return; 
1257         
1258         prop = prefs_filtering_dialog_to_filtering(TRUE);
1259         if (prop == NULL)
1260                 return;
1261
1262         /* prop->emabled is always TRUE here, re-use the value from the selected row 
1263            as we don't substitute this value from dialog */
1264         prefs_filtering_list_view_get_rule_info(
1265                         filtering.cond_list_view, selected_row,
1266                         &enabled, &name, &account_id);
1267         g_free(name); /* We're not using this. */
1268         prop->enabled = enabled;
1269
1270         prefs_filtering_list_view_set_row(-selected_row-2, prop);
1271         
1272         filteringprop_free(prop);
1273
1274         prefs_filtering_reset_dialog();
1275         modified = TRUE;
1276 }
1277
1278 static void prefs_filtering_top(gpointer action, gpointer data)
1279 {
1280         gint row;
1281         GtkTreeIter top, sel;
1282         GtkTreeModel *model;
1283
1284         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1285         if (row <= 1) 
1286                 return;
1287
1288         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));               
1289         
1290         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
1291         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1292                 return;
1293
1294         gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
1295         gtkut_list_view_select_row(filtering.cond_list_view, 1);
1296         modified = TRUE;
1297 }
1298
1299 static void prefs_filtering_page_up(gpointer action, gpointer data)
1300 {
1301         gint row, target_row;
1302         GtkTreeIter selected, target;
1303         GtkTreeModel *model;
1304         GtkTreePath *path;
1305         GdkRectangle cell_rect, view_rect;
1306
1307         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1308         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1309         if (row <= 1)
1310                 return;
1311
1312         if (!gtk_tree_model_iter_nth_child(model, &selected, NULL, row))
1313                 return;
1314
1315         /* compute number of rows per page (approximation) */
1316         path = gtk_tree_model_get_path(model, &selected);
1317         gtk_tree_view_get_cell_area(GTK_TREE_VIEW(filtering.cond_list_view), path, NULL, &cell_rect);
1318         gtk_tree_view_get_visible_rect(GTK_TREE_VIEW(filtering.cond_list_view), &view_rect);
1319         gtk_tree_path_free(path);
1320         target_row = row - (view_rect.height/cell_rect.height);
1321         if (target_row < 1)
1322                 target_row = 1;
1323
1324         if (!gtk_tree_model_iter_nth_child(model, &target, NULL, target_row))
1325                 return;
1326         gtk_list_store_move_before(GTK_LIST_STORE(model), &selected, &target);
1327         gtkut_list_view_select_row(filtering.cond_list_view, target_row);
1328         modified = TRUE;
1329 }
1330 static void prefs_filtering_up(gpointer action, gpointer data)
1331 {
1332         gint row;
1333         GtkTreeIter top, sel;
1334         GtkTreeModel *model;
1335
1336         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1337         if (row <= 1) 
1338                 return;
1339                 
1340         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1341
1342         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
1343         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1344                 return;
1345
1346         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1347         gtkut_list_view_select_row(filtering.cond_list_view, row - 1);
1348         modified = TRUE;
1349 }
1350
1351 static void prefs_filtering_down(gpointer action, gpointer data)
1352 {
1353         gint row, n_rows;
1354         GtkTreeIter top, sel;
1355         GtkTreeModel *model;
1356
1357         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1358         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1359         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1360         if (row < 1 || row >= n_rows - 1)
1361                 return;
1362
1363         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1364         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
1365                 return;
1366                         
1367         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1368         gtkut_list_view_select_row(filtering.cond_list_view, row + 1);
1369         modified = TRUE;
1370 }
1371
1372 static void prefs_filtering_page_down(gpointer action, gpointer data)
1373 {
1374         gint row, target_row, n_rows;
1375         GtkTreeIter selected, target;
1376         GtkTreeModel *model;
1377         GtkTreePath *path;
1378         GdkRectangle cell_rect, view_rect;
1379
1380         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1381         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1382         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1383         if (row < 1 || row >= n_rows -1)
1384                 return;
1385
1386         if (!gtk_tree_model_iter_nth_child(model, &selected, NULL, row))
1387                 return;
1388
1389         /* compute number of rows per page (approximation) */
1390         path = gtk_tree_model_get_path(model, &selected);
1391         gtk_tree_view_get_cell_area(GTK_TREE_VIEW(filtering.cond_list_view), path, NULL, &cell_rect);
1392         gtk_tree_view_get_visible_rect(GTK_TREE_VIEW(filtering.cond_list_view), &view_rect);
1393         gtk_tree_path_free(path);
1394         target_row = row + (view_rect.height/cell_rect.height);
1395         if (target_row > n_rows-1)
1396                 target_row = n_rows-1;
1397
1398         if (!gtk_tree_model_iter_nth_child(model, &target, NULL, target_row))
1399                 return;
1400         gtk_list_store_move_after(GTK_LIST_STORE(model), &selected, &target);
1401         gtkut_list_view_select_row(filtering.cond_list_view, target_row);
1402         modified = TRUE;
1403 }
1404
1405 static void prefs_filtering_bottom(gpointer action, gpointer data)
1406 {
1407         gint row, n_rows;
1408         GtkTreeIter top, sel;
1409         GtkTreeModel *model;
1410
1411         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1412         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1413         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1414         if (row < 1 || row >= n_rows - 1)
1415                 return;
1416
1417         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1418         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
1419                 return;
1420
1421         gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);           
1422         gtkut_list_view_select_row(filtering.cond_list_view, n_rows - 1);
1423         modified = TRUE;
1424 }
1425
1426 static void prefs_filtering_select_set(FilteringProp *prop)
1427 {
1428         gchar *matcher_str;
1429         gchar *action_str;
1430
1431         prefs_filtering_reset_dialog();
1432
1433         matcher_str = matcherlist_to_string(prop->matchers);
1434         if (matcher_str == NULL) {
1435                 return;
1436         }
1437         
1438         if (prop->name != NULL)
1439                 gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), prop->name);
1440
1441         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), prop->account_id);
1442                 
1443         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), matcher_str);
1444
1445         action_str = filteringaction_list_to_string(prop->action_list);
1446         if (action_str != NULL)
1447                 gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), action_str);
1448
1449         g_free(action_str);
1450         g_free(matcher_str);
1451 }
1452
1453 static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
1454                                  gpointer data)
1455 {
1456         prefs_filtering_cancel(NULL, NULL);
1457         return TRUE;
1458 }
1459
1460 static gboolean prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
1461                                      gpointer data)
1462 {
1463         if (event && event->keyval == GDK_KEY_Escape) {
1464                 prefs_filtering_cancel(NULL, NULL);
1465                 return TRUE;                    
1466         }
1467         return FALSE;
1468 }
1469
1470 static gboolean prefs_filtering_check_mod(gboolean check_changed_list)
1471 {
1472         FilteringProp * prop;
1473         gchar * str;
1474         gchar * filtering_str;
1475         gint row = 1;
1476         AlertValue val;
1477         
1478         prop = prefs_filtering_dialog_to_filtering(FALSE);
1479         
1480         if (check_changed_list) {
1481                 if (modified && alertpanel(_("Filtering rules not saved"),
1482                                          _("The list of filtering rules have been modified. Close anyway?"),
1483                                          GTK_STOCK_CLOSE, _("_Continue editing"), NULL,
1484                                          ALERTFOCUS_SECOND) != G_ALERTDEFAULT) {
1485                         return TRUE;
1486                 }
1487         }
1488         
1489         /* check if a rule is being edited */
1490         if (prop != NULL) {
1491                 str = filteringprop_to_string(prop);
1492
1493                 while (NULL != (filtering_str = (prefs_filtering_list_view_get_rule
1494                                                         (filtering.cond_list_view,
1495                                                          row)))) {
1496                         if (strcmp(filtering_str, str) == 0)
1497                                 break;
1498                         row++;
1499                         g_free(filtering_str);
1500                 }       
1501
1502                 if (!filtering_str) {
1503                         val = alertpanel(_("Entry not saved"),
1504                                  _("The entry was not saved. Close anyway?"),
1505                                  GTK_STOCK_CLOSE, _("_Continue editing"), NULL, ALERTFOCUS_SECOND);
1506                         if (G_ALERTDEFAULT != val) {
1507                                 g_free(filtering_str);
1508                                 g_free(str); /* fixed two leaks: huzzah! */
1509                                 filteringprop_free(prop);
1510                                 return TRUE;
1511                         }
1512                 }               
1513
1514                 g_free(filtering_str);
1515                 g_free(str);
1516                 filteringprop_free(prop); /* fixed a leak: huzzah! */
1517         } else {
1518                 gchar *name, *condition, *action;
1519                 name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
1520                 condition = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1521                 action = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1522                 if (strlen(name) || 
1523                     strlen(condition) || 
1524                     strlen(action)) {
1525                         val = alertpanel(_("Entry not saved"),
1526                                  _("The entry was not saved. Close anyway?"),
1527                                  GTK_STOCK_CLOSE, _("_Continue editing"), NULL, ALERTFOCUS_SECOND);
1528                         if (G_ALERTDEFAULT != val) {
1529                                 g_free(name);
1530                                 g_free(condition);
1531                                 g_free(action);
1532                                 return TRUE;
1533                         }
1534                 }
1535                 g_free(name);
1536                 g_free(condition);
1537                 g_free(action);
1538         }
1539         return FALSE;
1540 }
1541
1542 static void prefs_filtering_ok(gpointer action, gpointer data)
1543 {
1544         if (prefs_filtering_check_mod(FALSE))
1545                 return;
1546         modified = FALSE;
1547         prefs_filtering_set_list();
1548         prefs_matcher_write_config();
1549         prefs_filtering_close();
1550 }
1551
1552 static void prefs_filtering_cancel(gpointer action, gpointer data)
1553 {
1554         if (prefs_filtering_check_mod(TRUE))
1555                 return;
1556         modified = FALSE;
1557         prefs_matcher_read_config();
1558         prefs_filtering_close();
1559 }
1560
1561 static GtkListStore* prefs_filtering_create_data_store(void)
1562 {
1563         return gtk_list_store_new(N_PREFS_FILTERING_COLUMNS,
1564                                   G_TYPE_BOOLEAN,
1565                                   G_TYPE_STRING,
1566                                   G_TYPE_INT,
1567                                   G_TYPE_STRING,
1568                                   G_TYPE_STRING,
1569                                   G_TYPE_BOOLEAN,
1570                                  -1);
1571 }
1572
1573 /*!
1574  *\brief        Insert filtering rule into store. Note that we access the
1575  *              tree view / store by index, which is a bit suboptimal, but
1576  *              at least it made GTK 2 porting easier.
1577  *
1578  *\param        list_store Store to operate on
1579  *\param        row -1 to add a new rule to store,
1580                         row >=0 to change an existing row
1581                         row <-1 insert a new row after (-row-2)
1582  *\param        enabled TRUE if rule is enabled
1583  *\param        name The Name of rule
1584  *\param        account_id The account ID
1585  *\param        account_name The account name or All or (New)
1586  *\param        rule String representation of rule
1587  *\param        prop TRUE if valid filtering rule; if FALSE it's the first
1588  *              entry in the store ("(New)").
1589  *
1590  *\return       int Row of inserted / changed rule.
1591  */
1592 static gint prefs_filtering_list_view_insert_rule(GtkListStore *list_store,
1593                                                   gint row,
1594                                                   gboolean enabled,
1595                                                   const gchar *name,
1596                                                   gint account_id,
1597                                                   const gchar *account_name,
1598                                                   const gchar *rule,
1599                                                   gboolean prop) 
1600 {
1601         GtkTreeIter iter;
1602         GtkTreeIter sibling;
1603
1604         /* check if valid row at all */
1605         if (row >= 0) {
1606                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1607                                                    &iter, NULL, row))
1608                         row = -1;
1609         } else if (row < -1) {
1610                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1611                                                    &sibling, NULL, -row-2))
1612                         row = -1;                                                  
1613         }
1614
1615         if (row == -1 ) {
1616                 /* append new */
1617                 gtk_list_store_append(list_store, &iter);
1618                 gtk_list_store_set(list_store, &iter, 
1619                                    PREFS_FILTERING_ENABLED, enabled,
1620                                    PREFS_FILTERING_NAME, name,
1621                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1622                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1623                                    PREFS_FILTERING_RULE, rule,
1624                                    PREFS_FILTERING_PROP, prop,
1625                                    -1);
1626                 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store),
1627                                                       NULL) - 1;
1628         } else if (row < -1) {
1629                 /* duplicate */
1630                 gtk_list_store_insert_after(list_store, &iter, &sibling);
1631                 gtk_list_store_set(list_store, &iter, 
1632                                    PREFS_FILTERING_ENABLED, enabled,
1633                                    PREFS_FILTERING_NAME, name,
1634                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1635                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1636                                    PREFS_FILTERING_RULE, rule,
1637                                    PREFS_FILTERING_PROP, prop,
1638                                    -1);
1639                 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store),
1640                                                       NULL) - 1;
1641         } else {
1642                 /* change existing */
1643                 gtk_list_store_set(list_store, &iter, 
1644                                    PREFS_FILTERING_ENABLED, enabled,
1645                                    PREFS_FILTERING_NAME, name,
1646                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1647                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1648                                    PREFS_FILTERING_RULE, rule,
1649                                    -1);
1650                 return row;                                
1651         }
1652 }
1653
1654 /*!
1655  *\return       gchar * Rule at specified row - should be freed.
1656  */
1657 static gchar *prefs_filtering_list_view_get_rule(GtkWidget *list, gint row)
1658 {       
1659         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1660         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1661         GtkTreeIter iter;
1662         gchar *result;
1663
1664         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1665                 return NULL;
1666         
1667         gtk_tree_model_get(model, &iter, 
1668                            PREFS_FILTERING_RULE, &result,
1669                            -1);
1670         
1671         return result;
1672 }
1673
1674 static void prefs_filtering_list_view_get_rule_info(GtkWidget *list, gint row,
1675                                 gboolean *enabled, gchar **name, gint *account_id)
1676 {       
1677         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1678         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1679         GtkTreeIter iter;
1680
1681         *enabled = TRUE;
1682         *name = NULL;
1683
1684         if (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1685                 gtk_tree_model_get(model, &iter, 
1686                                    PREFS_FILTERING_ENABLED, enabled,
1687                                    PREFS_FILTERING_NAME, name,
1688                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1689                                    -1);
1690         }
1691 }
1692
1693 static GtkActionGroup *prefs_filtering_popup_action = NULL;
1694 static GtkWidget *prefs_filtering_popup_menu = NULL;
1695
1696 static GtkActionEntry prefs_filtering_popup_entries[] =
1697 {
1698         {"PrefsFilteringPopup",                 NULL, "PrefsFilteringPopup", NULL, NULL, NULL },
1699         {"PrefsFilteringPopup/Delete",          NULL, N_("_Delete"), NULL, NULL, G_CALLBACK(prefs_filtering_delete_cb) },
1700         {"PrefsFilteringPopup/DeleteAll",       NULL, N_("Delete _all"), NULL, NULL, G_CALLBACK(prefs_filtering_delete_all_cb) },
1701         {"PrefsFilteringPopup/Duplicate",       NULL, N_("D_uplicate"), NULL, NULL, G_CALLBACK(prefs_filtering_duplicate_cb) },
1702 #ifdef GENERIC_UMPC
1703         {"PrefsFilteringPopup/---",             NULL, "---", NULL, NULL, NULL },
1704         {"PrefsFilteringPopup/PageUp",          NULL, N_("Move one page up"), NULL, NULL, G_CALLBACK(prefs_filtering_page_up) },
1705         {"PrefsFilteringPopup/PageDown",        NULL, N_("Move one page down"), NULL, NULL, G_CALLBACK(prefs_filtering_page_down) },
1706 #endif
1707 };
1708
1709 static void prefs_filtering_row_selected(GtkTreeSelection *selection,
1710                                          GtkTreeView *list_view)
1711 {
1712         GtkTreePath *path;
1713         GtkTreeIter iter;
1714         GtkTreeModel *model;
1715         
1716         if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1717                 return;
1718         
1719         path = gtk_tree_model_get_path(model, &iter);
1720         prefs_filtering_select_row(list_view, path);
1721         gtk_tree_path_free(path);
1722 }
1723
1724 static gint prefs_filtering_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1725                                     GtkTreeView *list_view)
1726 {
1727         if (event) {
1728                 /* left- or right-button click */
1729                 if (event->button == 1 || event->button == 3) {
1730                         GtkTreePath *path = NULL;
1731
1732                         if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1733                                                                 &path, NULL, NULL, NULL)) {
1734                                 prefs_filtering_select_row(list_view, path);
1735                         }
1736                         if (path)
1737                                 gtk_tree_path_free(path);
1738                 }
1739
1740                 /* right-button click */
1741                 if (event->button == 3) {
1742                         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1743                         GtkTreeIter iter;
1744                         gboolean non_empty;
1745                         gint row;
1746
1747                         if (!prefs_filtering_popup_menu) {
1748                                 prefs_filtering_popup_action = cm_menu_create_action_group("PrefsFilteringPopup", prefs_filtering_popup_entries,
1749                                         G_N_ELEMENTS(prefs_filtering_popup_entries), (gpointer)list_view);
1750                                 MENUITEM_ADDUI("/Menus", "PrefsFilteringPopup", "PrefsFilteringPopup", GTK_UI_MANAGER_MENU)
1751                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Delete", "PrefsFilteringPopup/Delete", GTK_UI_MANAGER_MENUITEM)
1752                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "DeleteAll", "PrefsFilteringPopup/DeleteAll", GTK_UI_MANAGER_MENUITEM)
1753                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Duplicate", "PrefsFilteringPopup/Duplicate", GTK_UI_MANAGER_MENUITEM)
1754 #ifdef GENERIC_UMPC
1755                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Separator1", "PrefsFilteringPopup/---", GTK_UI_MANAGER_SEPARATOR)
1756                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "PageUp", "PrefsFilteringPopup/PageUp", GTK_UI_MANAGER_MENUITEM)
1757                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "PageDown", "PrefsFilteringPopup/PageDown", GTK_UI_MANAGER_MENUITEM)
1758 #endif
1759                                 prefs_filtering_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1760                                         gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/PrefsFilteringPopup")) );
1761                         }
1762
1763                         /* grey out some popup menu items if there is no selected row */
1764                         row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1765                         cm_menu_set_sensitive("PrefsFilteringPopup/Delete", (row > 0));
1766                         cm_menu_set_sensitive("PrefsFilteringPopup/Duplicate", (row > 0));
1767
1768                         /* grey out seom popup menu items if there is no row
1769                            (not counting the (New) one at row 0) */
1770                         non_empty = gtk_tree_model_get_iter_first(model, &iter);
1771                         if (non_empty)
1772                                 non_empty = gtk_tree_model_iter_next(model, &iter);
1773                         cm_menu_set_sensitive("PrefsFilteringPopup/DeleteAll", non_empty);
1774
1775                         gtk_menu_popup(GTK_MENU(prefs_filtering_popup_menu), 
1776                                    NULL, NULL, NULL, NULL, 
1777                                    event->button, event->time);
1778                 }
1779         }
1780         return FALSE;
1781 }
1782
1783 static gboolean prefs_filtering_list_popup_menu(GtkWidget *widget, gpointer data)
1784 {
1785         GtkTreeView *list_view = (GtkTreeView *)data;
1786         GdkEventButton event;
1787         
1788         event.button = 3;
1789         event.time = gtk_get_current_event_time();
1790         
1791         prefs_filtering_list_btn_pressed(NULL, &event, list_view);
1792
1793         return TRUE;
1794 }
1795
1796 /*!
1797  *\brief        Create list view for filtering
1798  */
1799 static GtkWidget *prefs_filtering_list_view_create(void)
1800 {
1801         GtkTreeView *list_view;
1802         GtkTreeSelection *selector;
1803         GtkListStore *store = prefs_filtering_create_data_store();
1804
1805         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(
1806                                 GTK_TREE_MODEL(store)));
1807         g_object_unref(store);
1808 #ifdef GENERIC_UMPC
1809         g_object_set(list_view, "allow-checkbox-mode", FALSE, NULL);
1810 #endif
1811
1812         g_signal_connect(G_OBJECT(list_view), "popup-menu",
1813                          G_CALLBACK(prefs_filtering_list_popup_menu), list_view);
1814         g_signal_connect(G_OBJECT(list_view), "button-press-event",
1815                         G_CALLBACK(prefs_filtering_list_btn_pressed), list_view);
1816         
1817         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1818         gtk_tree_view_set_reorderable(list_view, TRUE);
1819         
1820         selector = gtk_tree_view_get_selection(list_view);
1821         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1822         g_signal_connect(G_OBJECT(selector), "changed",
1823                          G_CALLBACK(prefs_filtering_row_selected), list_view);
1824
1825         /* create the columns */
1826         prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view));
1827
1828         return GTK_WIDGET(list_view);
1829 }
1830
1831 static void prefs_filtering_enable_toggled(GtkCellRendererToggle *widget,
1832                 gchar *path,
1833                 GtkWidget *list_view)
1834 {
1835         GtkTreeIter iter;
1836         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1837         gboolean enabled = TRUE;
1838
1839         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1840                 return;
1841
1842         gtk_tree_model_get(model, &iter,
1843                            PREFS_FILTERING_ENABLED, &enabled,
1844                            -1);
1845
1846         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1847                            PREFS_FILTERING_ENABLED, !enabled,
1848                            -1);
1849 }
1850
1851 static void prefs_filtering_create_list_view_columns(GtkWidget *list_view)
1852 {
1853         GtkTreeViewColumn *column;
1854         GtkCellRenderer *renderer;
1855
1856         renderer = gtk_cell_renderer_toggle_new();
1857         g_object_set(renderer,
1858                      "radio", FALSE,
1859                      "activatable", TRUE,
1860                      NULL);
1861         column = gtk_tree_view_column_new_with_attributes
1862                 (_("Enable"), /* FIXME : Enable, Enabled, or 'E' ? */
1863                  renderer,
1864                  "active", PREFS_FILTERING_ENABLED,
1865                  NULL);
1866         gtk_tree_view_column_set_alignment (column, 0.5);
1867         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1868         g_signal_connect(G_OBJECT(renderer), "toggled",
1869                          G_CALLBACK(prefs_filtering_enable_toggled),
1870                          list_view);
1871
1872         renderer = gtk_cell_renderer_text_new();
1873         column = gtk_tree_view_column_new_with_attributes
1874                 (_("Name"),
1875                  renderer,
1876                  "text", PREFS_FILTERING_NAME,
1877                  NULL);
1878         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1879         gtk_tree_view_column_set_resizable(column, TRUE);
1880
1881         renderer = gtk_cell_renderer_text_new();
1882         column = gtk_tree_view_column_new_with_attributes
1883                 (_("Account"),
1884                  renderer,
1885                  "text", PREFS_FILTERING_ACCOUNT_NAME,
1886                  NULL);
1887         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1888         gtk_tree_view_column_set_resizable(column, TRUE);
1889
1890         filtering.account_name_column = column;
1891                 
1892         renderer = gtk_cell_renderer_text_new();
1893         column = gtk_tree_view_column_new_with_attributes
1894                 (_("Rule"),
1895                  renderer,
1896                  "text", PREFS_FILTERING_RULE,
1897                  NULL);
1898
1899         gtk_tree_view_set_search_column(GTK_TREE_VIEW(list_view), PREFS_FILTERING_NAME);
1900         gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(list_view), prefs_filtering_search_func_cb , NULL, NULL);
1901         
1902         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1903 }
1904
1905 /*!
1906  *\brief        Triggered when a row has to be selected
1907  */
1908 static void prefs_filtering_select_row(GtkTreeView *list_view, GtkTreePath *path)
1909 {
1910         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1911
1912         if (path && model) {
1913                 GtkTreeSelection *selection;
1914                 gboolean has_prop = FALSE;
1915                 GtkTreeIter iter;
1916
1917                 /* select row */
1918                 selection = gtk_tree_view_get_selection(list_view);
1919                 gtk_tree_selection_select_path(selection, path);
1920
1921                 /* update dialog from selection */
1922                 gtk_tree_model_get_iter(model, &iter, path);
1923                 gtk_tree_model_get(model, &iter,
1924                                    PREFS_FILTERING_PROP, &has_prop,
1925                                    -1);
1926
1927                 if (has_prop) {
1928                         FilteringProp *prop;
1929                         gchar *filtering_str = NULL;
1930                         gchar *name = NULL;
1931                         gint account_id = 0;
1932
1933                         gtk_tree_model_get(model, &iter,
1934                                            PREFS_FILTERING_RULE, &filtering_str,
1935                                            PREFS_FILTERING_NAME, &name,
1936                                            PREFS_FILTERING_ACCOUNT_ID, &account_id,
1937                                            -1);
1938
1939                         prop = matcher_parser_get_filtering(filtering_str);
1940                         if (prop) {
1941                                 if (prop->name != NULL)
1942                                         g_free(prop->name);
1943                                 prop->name = g_strdup(name);
1944                                 prop->account_id = account_id;
1945                                 prefs_filtering_select_set(prop);
1946                                 filteringprop_free(prop);
1947                         }                               
1948                         g_free(name);
1949                         g_free(filtering_str);
1950                 } else
1951                         prefs_filtering_reset_dialog();
1952         }
1953 }