2012-09-16 [mones] 3.8.1cvs60
[claws.git] / src / prefs_filtering.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 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, Q_("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_label (_(" Define... "));
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_label (_(" Define... "));
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_from_stock (GTK_STOCK_DELETE);
487         gtk_widget_show (del_btn);
488         gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
489         g_signal_connect(G_OBJECT (del_btn), "clicked",
490                         G_CALLBACK(prefs_filtering_delete_cb), NULL);
491         CLAWS_SET_TIP(del_btn,
492                         _("Delete the selected rule from the list"));
493
494         clear_btn = gtk_button_new_from_stock (GTK_STOCK_CLEAR);
495         gtk_widget_show (clear_btn);
496         gtk_box_pack_start (GTK_BOX (btn_hbox), clear_btn, FALSE, TRUE, 0);
497         g_signal_connect(G_OBJECT (clear_btn), "clicked",
498                         G_CALLBACK(prefs_filtering_clear_cb), NULL);
499         CLAWS_SET_TIP(clear_btn,
500                         _("Clear all the input fields in the dialog"));
501
502         cond_hbox = gtk_hbox_new (FALSE, 8);
503         gtk_widget_show (cond_hbox);
504         gtk_box_pack_start (GTK_BOX (vbox), cond_hbox, TRUE, TRUE, 0);
505
506         cond_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
507         gtk_widget_show (cond_scrolledwin);
508         gtk_widget_set_size_request (cond_scrolledwin, -1, 150);
509         gtk_box_pack_start (GTK_BOX (cond_hbox), cond_scrolledwin,
510                             TRUE, TRUE, 0);
511         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin),
512                                         GTK_POLICY_AUTOMATIC,
513                                         GTK_POLICY_AUTOMATIC);
514
515         cond_list_view = prefs_filtering_list_view_create();    
516         gtk_widget_show (cond_list_view);
517         gtk_container_add (GTK_CONTAINER (cond_scrolledwin), cond_list_view);
518
519         btn_vbox = gtk_vbox_new (FALSE, 8);
520         gtk_widget_show (btn_vbox);
521         gtk_box_pack_start (GTK_BOX (cond_hbox), btn_vbox, FALSE, FALSE, 0);
522
523         top_btn = gtk_button_new_from_stock (GTK_STOCK_GOTO_TOP);
524         gtk_widget_show (top_btn);
525         gtk_box_pack_start (GTK_BOX (btn_vbox), top_btn, FALSE, FALSE, 0);
526         g_signal_connect(G_OBJECT (top_btn), "clicked",
527                          G_CALLBACK(prefs_filtering_top), NULL);
528         CLAWS_SET_TIP(top_btn,
529                         _("Move the selected rule to the top"));
530
531 #ifndef GENERIC_UMPC
532         page_up_btn = gtk_button_new_with_mnemonic (_("Page up"));
533         gtk_button_set_image(GTK_BUTTON(page_up_btn),
534                         gtk_image_new_from_stock(GTK_STOCK_GO_UP,GTK_ICON_SIZE_BUTTON));
535         gtk_widget_show (page_up_btn);
536         gtk_box_pack_start (GTK_BOX (btn_vbox), page_up_btn, FALSE, FALSE, 0);
537         g_signal_connect(G_OBJECT (page_up_btn), "clicked",
538                          G_CALLBACK(prefs_filtering_page_up), NULL);
539         CLAWS_SET_TIP(page_up_btn,
540                         _("Move the selected rule one page up"));
541 #endif
542
543         up_btn = gtk_button_new_from_stock (GTK_STOCK_GO_UP);
544         gtk_widget_show (up_btn);
545         gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
546         g_signal_connect(G_OBJECT (up_btn), "clicked",
547                          G_CALLBACK(prefs_filtering_up), NULL);
548         CLAWS_SET_TIP(up_btn,
549                         _("Move the selected rule up"));
550
551         down_btn = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN);
552         gtk_widget_show (down_btn);
553         gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
554         g_signal_connect(G_OBJECT (down_btn), "clicked",
555                          G_CALLBACK(prefs_filtering_down), NULL);
556         CLAWS_SET_TIP(down_btn,
557                         _("Move the selected rule down"));
558
559 #ifndef GENERIC_UMPC
560         page_down_btn = gtk_button_new_with_mnemonic (_("Page down"));
561         gtk_button_set_image(GTK_BUTTON(page_down_btn),
562                         gtk_image_new_from_stock(GTK_STOCK_GO_DOWN,GTK_ICON_SIZE_BUTTON));
563         gtk_widget_show (page_down_btn);
564         gtk_box_pack_start (GTK_BOX (btn_vbox), page_down_btn, FALSE, FALSE, 0);
565         g_signal_connect(G_OBJECT (page_down_btn), "clicked",
566                          G_CALLBACK(prefs_filtering_page_down), NULL);
567         CLAWS_SET_TIP(page_down_btn,
568                         _("Move the selected rule one page down"));
569 #endif
570
571         bottom_btn = gtk_button_new_from_stock (GTK_STOCK_GOTO_BOTTOM);
572         gtk_widget_show (bottom_btn);
573         gtk_box_pack_start (GTK_BOX (btn_vbox), bottom_btn, FALSE, FALSE, 0);
574         g_signal_connect(G_OBJECT (bottom_btn), "clicked",
575                          G_CALLBACK(prefs_filtering_bottom), NULL);
576         CLAWS_SET_TIP(bottom_btn,
577                         _("Move the selected rule to the bottom"));
578
579         if (!geometry.min_height) {
580                 geometry.min_width = 500;
581                 geometry.min_height = 460;
582         }
583
584         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
585                                       GDK_HINT_MIN_SIZE);
586         gtk_widget_set_size_request(window, prefs_common.filteringwin_width,
587                                     prefs_common.filteringwin_height);
588
589         gtk_widget_show_all(window);
590
591         filtering.window    = window;
592         filtering.help_btn = help_btn;
593         filtering.ok_btn = ok_btn;
594
595         filtering.name_entry     = name_entry;
596         filtering.cond_entry     = cond_entry;
597         filtering.action_entry   = action_entry;
598         filtering.cond_list_view = cond_list_view;
599         filtering.account_label  = account_label;
600 }
601
602 static void rename_tag(GSList * filters,
603                         const gchar * old_tag, const gchar * new_tag);
604
605 void prefs_filtering_rename_tag(const gchar *old_tag, const gchar *new_tag)
606 {
607         GList * cur;
608         const gchar *tags[2] = {NULL, NULL};
609         tags[0] = old_tag;
610         tags[1] = new_tag;
611         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
612                 Folder *folder;
613                 folder = (Folder *) cur->data;
614                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
615                                 prefs_filtering_rename_tag_func, tags);
616         }
617         
618         rename_tag(pre_global_processing, old_tag, new_tag);
619         rename_tag(post_global_processing, old_tag, new_tag);
620         rename_tag(filtering_rules, old_tag, new_tag);
621         
622         prefs_matcher_write_config();
623 }
624
625
626 static void rename_path(GSList * filters,
627                         const gchar * old_path, const gchar * new_path);
628
629 void prefs_filtering_rename_path(const gchar *old_path, const gchar *new_path)
630 {
631         GList * cur;
632         const gchar *paths[2] = {NULL, NULL};
633         paths[0] = old_path;
634         paths[1] = new_path;
635         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
636                 Folder *folder;
637                 folder = (Folder *) cur->data;
638                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
639                                 prefs_filtering_rename_path_func, paths);
640         }
641         
642         rename_path(pre_global_processing, old_path, new_path);
643         rename_path(post_global_processing, old_path, new_path);
644         rename_path(filtering_rules, old_path, new_path);
645         
646         prefs_matcher_write_config();
647 }
648
649 static void rename_path(GSList * filters,
650                         const gchar * old_path, const gchar * new_path)
651 {
652         GSList * cur;
653
654         for (cur = filters; cur != NULL; cur = cur->next) {
655                 FilteringProp   *filtering = (FilteringProp *)cur->data;
656                 filtering_action_list_rename_path(filtering->action_list,
657                                                   old_path, new_path);
658         }
659 }
660
661 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
662 {
663         GSList *filters;
664         const gchar * old_path;
665         const gchar * new_path;
666         const gchar ** paths;
667         FolderItem *item;
668         
669         paths = data;
670         old_path = paths[0];
671         new_path = paths[1];
672
673         cm_return_val_if_fail(old_path != NULL, FALSE);
674         cm_return_val_if_fail(new_path != NULL, FALSE);
675         cm_return_val_if_fail(node != NULL, FALSE);
676
677         item = node->data;
678         if (!item || !item->prefs)
679                 return FALSE;
680         filters = item->prefs->processing;
681
682         rename_path(filters, old_path, new_path);
683
684         return FALSE;
685 }
686
687 static void rename_tag(GSList * filters,
688                         const gchar * old_tag, const gchar * new_tag)
689 {
690         GSList * action_cur;
691         GSList * cur;
692
693         for (cur = filters; cur != NULL; cur = cur->next) {
694                 FilteringProp   *filtering = (FilteringProp *)cur->data;
695                 
696                 for(action_cur = filtering->action_list ; action_cur != NULL ;
697                     action_cur = action_cur->next) {
698
699                         FilteringAction *action = action_cur->data;
700                         
701                         if (action->type != MATCHACTION_SET_TAG &&
702                             action->type != MATCHACTION_UNSET_TAG)
703                                 continue;
704                         if (!action->destination)
705                                 continue;
706                         if (!strcmp(action->destination, old_tag)) {
707                                 g_free(action->destination);
708                                 action->destination = g_strdup(new_tag);
709                         }
710                 }
711         }
712 }
713
714 static gboolean prefs_filtering_rename_tag_func(GNode *node, gpointer data)
715 {
716         GSList *filters;
717         const gchar * old_tag;
718         const gchar * new_tag;
719         const gchar ** tags;
720         FolderItem *item;
721         
722         tags = data;
723         old_tag = tags[0];
724         new_tag = tags[1];
725
726         cm_return_val_if_fail(old_tag != NULL, FALSE);
727         cm_return_val_if_fail(new_tag != NULL, FALSE);
728         cm_return_val_if_fail(node != NULL, FALSE);
729
730         item = node->data;
731         if (!item || !item->prefs)
732                 return FALSE;
733         filters = item->prefs->processing;
734
735         rename_tag(filters, old_tag, new_tag);
736
737         return FALSE;
738 }
739
740 void prefs_filtering_delete_path(const gchar *path)
741 {
742         GList * cur;
743         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
744                 Folder *folder;
745                 folder = (Folder *) cur->data;
746                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
747                                 prefs_filtering_delete_path_func, (gchar *)path);
748         }
749         delete_path(&pre_global_processing, path);
750         delete_path(&post_global_processing, path);
751         delete_path(&filtering_rules, path);
752         
753         prefs_matcher_write_config();
754 }
755
756 static void delete_path(GSList ** p_filters, const gchar * path)
757 {
758         GSList * filters;
759         GSList * duplist;
760         gchar *suffix;
761         gint destlen;
762         gint prefixlen;
763         gint pathlen;
764         GSList * action_cur;
765         GSList * cur;
766         
767         filters = *p_filters;
768         pathlen = strlen(path);
769         duplist = g_slist_copy(filters);
770         for (cur = duplist ; cur != NULL; cur = g_slist_next(cur)) {
771                 FilteringProp *filtering = (FilteringProp *) cur->data;
772                 
773                 for(action_cur = filtering->action_list ; action_cur != NULL ;
774                     action_cur = action_cur->next) {
775                 
776                         FilteringAction *action;
777                         
778                         action = action_cur->data;
779                         
780                         if (action->type == MATCHACTION_SET_TAG ||
781                             action->type == MATCHACTION_UNSET_TAG)
782                                 continue;
783                         if (!action->destination) 
784                                 continue;
785                         
786                         destlen = strlen(action->destination);
787                         
788                         if (destlen > pathlen) {
789                                 prefixlen = destlen - pathlen;
790                                 suffix = action->destination + prefixlen;
791                                 
792                                 if (suffix && !strncmp(path, suffix, pathlen)) {
793                                         filteringprop_free(filtering);
794                                         filters = g_slist_remove(filters, filtering);
795                                 }
796                         } else if (strcmp(action->destination, path) == 0) {
797                                 filteringprop_free(filtering);
798                                 filters = g_slist_remove(filters, filtering);
799                         }
800                 }
801         }                
802         g_slist_free(duplist);
803         
804         * p_filters = filters;
805 }
806
807 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data)
808 {
809         const gchar *path = data;
810         FolderItem *item;
811         GSList ** p_filters;
812         
813         cm_return_val_if_fail(path != NULL, FALSE);
814         cm_return_val_if_fail(node != NULL, FALSE);
815
816         item = node->data;
817         if (!item || !item->prefs)
818                 return FALSE;
819         p_filters = &item->prefs->processing;
820         
821         delete_path(p_filters, path);
822
823         return FALSE;
824 }
825
826 static void prefs_filtering_clear_list(GtkListStore *list_store)
827 {
828         gtk_list_store_clear(list_store);
829
830         /* add the place holder (New) at row 0 */
831         prefs_filtering_list_view_insert_rule(list_store, -1, 
832                                               FALSE,
833                                               _("(New)"),
834                                                   0,
835                                               _("(New)"),
836                                               _("(New)"),
837                                               FALSE);
838 }
839
840 static void prefs_filtering_set_dialog(const gchar *header, const gchar *key)
841 {
842         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
843         GSList *cur;
844         GSList * prefs_filtering;
845         gchar *cond_str;
846         GtkListStore *list_store;
847         
848         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
849         prefs_filtering_clear_list(list_store);
850
851         prefs_filtering = *p_processing_list;
852
853         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur)) {
854                 FilteringProp * prop = (FilteringProp *) cur->data;
855                 gchar *account_name = NULL;
856
857                 if (prop->account_id > 0) {
858                         PrefsAccount *ac_prefs = account_find_from_id(prop->account_id);
859
860                         if (ac_prefs)
861                                 account_name = ac_prefs->account_name;
862                 }
863                 if (account_name == NULL)
864                         account_name = (gchar *)Q_("Filtering Account Menu|All");
865
866                 cond_str = filteringprop_to_string(prop);
867                 subst_char(cond_str, '\t', ':');
868
869                 prefs_filtering_list_view_insert_rule(list_store, -1, 
870                                                       prop->enabled,
871                                                       prop->name,
872                                                           prop->account_id,
873                                                           account_name,
874                                                       cond_str, TRUE);
875                 
876                 g_free(cond_str);
877         }
878
879         prefs_filtering_reset_dialog();
880
881         if (header && key) {
882                 gchar * quoted_key;
883                 gchar *match_str;
884
885                 quoted_key = matcher_quote_str(key);
886                 
887                 match_str = g_strconcat(header, " ", get_matchparser_tab_str(MATCHTYPE_MATCHCASE),
888                                         " \"", quoted_key, "\"", NULL);
889                 g_free(quoted_key);
890                 
891                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), match_str);
892                 g_free(match_str);
893         }
894 }
895
896 static void prefs_filtering_reset_dialog(void)
897 {
898         gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), "");
899         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
900         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), "");
901         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), "");
902 }
903
904 static gboolean prefs_filtering_search_func_cb (GtkTreeModel *model, gint column, const gchar *key, 
905                                                 GtkTreeIter *iter, gpointer search_data) 
906 {
907         gchar *store_string;
908         gboolean retval;
909         GtkTreePath *path;
910
911         gtk_tree_model_get (model, iter, column, &store_string, -1);
912
913         if (!store_string || !key) return FALSE;
914
915
916         retval = (g_ascii_strncasecmp (key, store_string, strlen(key)) != 0);
917
918         g_free(store_string);
919         debug_print("selecting row\n");
920         path = gtk_tree_model_get_path(model, iter);
921         prefs_filtering_select_row(GTK_TREE_VIEW(filtering.cond_list_view), path);
922         gtk_tree_path_free(path);
923
924         return retval;
925 }
926
927 static void prefs_filtering_set_list(void)
928 {
929         gint row = 1;
930         FilteringProp *prop;
931         GSList * cur;
932         gchar * filtering_str;
933         GSList * prefs_filtering;
934
935         prefs_filtering = *p_processing_list;
936         for (cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
937                 filteringprop_free((FilteringProp *) cur->data);
938         g_slist_free(prefs_filtering);
939         prefs_filtering = NULL;
940         
941
942         while (NULL != (filtering_str = prefs_filtering_list_view_get_rule
943                                                 (filtering.cond_list_view, row))) {
944                 /* FIXME: this strcmp() is bogus: "(New)" should never
945                  * be inserted in the storage */
946                 if (strcmp(filtering_str, _("(New)")) != 0) {
947                         gboolean enabled;
948                         gchar *name;
949                         gint account_id = 0;
950
951                         prefs_filtering_list_view_get_rule_info(
952                                         filtering.cond_list_view, row,
953                                         &enabled, &name, &account_id);
954                         prop = matcher_parser_get_filtering(filtering_str);
955                         g_free(filtering_str);
956                         if (prop) {
957                                 prop->enabled = enabled;
958                                 prop->name = name;
959                                 prop->account_id = account_id;
960                                 prefs_filtering = 
961                                         g_slist_append(prefs_filtering, prop);
962                         }
963                 }
964                 
965                 row++;
966         }                               
967         
968         *p_processing_list = prefs_filtering;
969 }
970
971 static gint prefs_filtering_list_view_set_row(gint row, FilteringProp * prop)
972 {
973         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
974         gchar *str = NULL;
975         GtkListStore *list_store;
976         gchar *name = NULL;
977         gint account_id = 0;
978         gchar *account_name = (gchar *)Q_("Filtering Account Menu|All");
979         gboolean enabled = TRUE;
980
981         if (prop)
982                 str = filteringprop_to_string(prop);
983         if (str == NULL)
984                 return -1;
985
986         if (prop) {
987                 if (prop->name)
988                         name = prop->name;
989                 account_id = prop->account_id;
990                 if (account_id > 0)
991                         account_name = account_find_from_id(account_id)->account_name;
992                 enabled = prop->enabled;
993         }
994
995         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
996
997         row = prefs_filtering_list_view_insert_rule(list_store, row,
998                                                     enabled,
999                                                     name,
1000                                                     account_id,
1001                                                         account_name,
1002                                                     str,
1003                                                     prop != NULL);
1004
1005         g_free(str);
1006
1007         return row;
1008 }
1009
1010 static void prefs_filtering_condition_define_done(MatcherList * matchers)
1011 {
1012         gchar * str;
1013
1014         if (matchers == NULL)
1015                 return;
1016
1017         str = matcherlist_to_string(matchers);
1018
1019         if (str != NULL) {
1020                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), str);
1021                 g_free(str);
1022         }
1023 }
1024
1025 static void prefs_filtering_condition_define(gpointer action, gpointer data)
1026 {
1027         gchar * cond_str;
1028         MatcherList * matchers = NULL;
1029
1030         cond_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1031
1032         if (*cond_str != '\0') {
1033                 matchers = matcher_parser_get_cond(cond_str, NULL);
1034                 if (matchers == NULL)
1035                         alertpanel_error(_("Condition string is not valid."));
1036         }
1037         
1038         g_free(cond_str);
1039
1040         prefs_matcher_open(matchers, prefs_filtering_condition_define_done);
1041
1042         if (matchers != NULL)
1043                 matcherlist_free(matchers);
1044 }
1045
1046 static void prefs_filtering_action_define_done(GSList * action_list)
1047 {
1048         gchar * str;
1049
1050         if (action_list == NULL)
1051                 return;
1052
1053         str = filteringaction_list_to_string(action_list);
1054
1055         if (str != NULL) {
1056                 gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), str);
1057                 g_free(str);
1058         }
1059 }
1060
1061 static void prefs_filtering_action_define(gpointer action, gpointer data)
1062 {
1063         gchar * action_str;
1064         GSList * action_list = NULL;
1065
1066         action_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1067
1068         if (*action_str != '\0') {
1069                 action_list = matcher_parser_get_action_list(action_str);
1070                 if (action_list == NULL)
1071                         alertpanel_error(_("Action string is not valid."));
1072         }
1073         
1074         g_free(action_str);
1075
1076         prefs_filtering_action_open(action_list,
1077             prefs_filtering_action_define_done);
1078
1079         if (action_list != NULL) {
1080                 GSList * cur;
1081                 for(cur = action_list ; cur != NULL ; cur = cur->next) {
1082                         filteringaction_free(cur->data);
1083                 }
1084         }
1085 }
1086
1087
1088 /* register / substitute delete buttons */
1089
1090
1091 static FilteringProp * prefs_filtering_dialog_to_filtering(gboolean alert)
1092 {
1093         MatcherList * cond;
1094         gboolean enabled = TRUE;
1095         gchar * name = NULL;
1096         gint account_id = 0;
1097         gchar * cond_str = NULL;
1098         gchar * action_str = NULL;
1099         FilteringProp * prop = NULL;
1100         GSList * action_list;
1101
1102         name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
1103         
1104         account_id = combobox_get_active_data(GTK_COMBO_BOX(filtering.account_combobox));
1105
1106         cond_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1107         if (*cond_str == '\0') {
1108                 if(alert == TRUE) alertpanel_error(_("Condition string is empty."));
1109                 goto fail;
1110         }
1111         
1112         action_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1113         if (*action_str == '\0') {
1114                 if(alert == TRUE) alertpanel_error(_("Action string is empty."));
1115                 goto fail;
1116         }
1117
1118         cond = matcher_parser_get_cond(cond_str, NULL);
1119
1120         if (cond == NULL) {
1121                 if(alert == TRUE) alertpanel_error(_("Condition string is not valid."));
1122                 goto fail;
1123         }
1124         
1125         action_list = matcher_parser_get_action_list(action_str);
1126         
1127
1128         if (action_list == NULL) {
1129                 if(alert == TRUE) alertpanel_error(_("Action string is not valid."));
1130                 goto fail;
1131         }
1132
1133         prop = filteringprop_new(enabled, name, account_id, cond, action_list);
1134
1135 fail:
1136         g_free(name);
1137         g_free(cond_str);
1138         g_free(action_str);
1139         return prop;
1140 }
1141
1142 static void prefs_filtering_register_cb(gpointer action, gpointer data)
1143 {
1144         FilteringProp *prop;
1145         
1146         prop = prefs_filtering_dialog_to_filtering(TRUE);
1147         if (prop == NULL)
1148                 return;
1149         prefs_filtering_list_view_set_row(-1, prop);
1150         
1151         filteringprop_free(prop);
1152
1153         prefs_filtering_reset_dialog();
1154         modified = TRUE;
1155 }
1156
1157 static void prefs_filtering_substitute_cb(gpointer action, gpointer data)
1158 {
1159         gint selected_row = gtkut_list_view_get_selected_row
1160                 (filtering.cond_list_view);
1161         FilteringProp *prop;
1162         gboolean enabled;
1163         gchar *name;
1164         gint account_id;
1165         
1166         if (selected_row <= 0)
1167                 return;
1168
1169         prop = prefs_filtering_dialog_to_filtering(TRUE);
1170
1171         if (prop == NULL) 
1172                 return;
1173
1174         /* prop->emabled is always TRUE here, re-use the value from the selected row 
1175            as we don't substitute this value from dialog */
1176         prefs_filtering_list_view_get_rule_info(
1177                         filtering.cond_list_view, selected_row,
1178                         &enabled, &name, &account_id);
1179         prop->enabled = enabled;
1180
1181         prefs_filtering_list_view_set_row(selected_row, prop);
1182
1183         filteringprop_free(prop);
1184
1185         prefs_filtering_row_selected(gtk_tree_view_get_selection(
1186                                 GTK_TREE_VIEW(filtering.cond_list_view)),
1187                                 GTK_TREE_VIEW(filtering.cond_list_view));
1188         modified = TRUE;
1189 }
1190
1191 static void prefs_filtering_delete_cb(gpointer action, gpointer data)
1192 {
1193         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
1194         GtkTreeModel *model;
1195         GtkTreeIter iter;
1196         gint selected_row;
1197         
1198         selected_row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1199         if (selected_row <= 0) 
1200                 return; 
1201
1202         if (alertpanel(_("Delete rule"),
1203                        _("Do you really want to delete this rule?"),
1204                        GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
1205                 return;
1206
1207         model = gtk_tree_view_get_model(list_view);     
1208         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, selected_row))
1209                 return;
1210
1211         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1212
1213         prefs_filtering_reset_dialog();
1214         modified = TRUE;
1215 }
1216
1217 static void prefs_filtering_delete_all_cb(gpointer action, gpointer data)
1218 {
1219         GtkListStore *list_store;
1220         
1221         if (alertpanel(_("Delete all rules"),
1222                        _("Do you really want to delete all the rules?"),
1223                        GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
1224                 return;
1225
1226         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view)));
1227         prefs_filtering_clear_list(list_store);
1228
1229         prefs_filtering_reset_dialog();
1230         modified = TRUE;
1231 }
1232
1233 static void prefs_filtering_clear_cb(gpointer action, gpointer data)
1234 {
1235         prefs_filtering_reset_dialog();
1236 }
1237
1238 static void prefs_filtering_duplicate_cb(gpointer action, gpointer data)
1239 {
1240         gint selected_row = gtkut_list_view_get_selected_row
1241                 (filtering.cond_list_view);     
1242         FilteringProp *prop;
1243         gboolean enabled;
1244         gchar *name;
1245         gint account_id;
1246
1247         if (selected_row <= 0) 
1248                 return; 
1249         
1250         prop = prefs_filtering_dialog_to_filtering(TRUE);
1251         if (prop == NULL)
1252                 return;
1253
1254         /* prop->emabled is always TRUE here, re-use the value from the selected row 
1255            as we don't substitute this value from dialog */
1256         prefs_filtering_list_view_get_rule_info(
1257                         filtering.cond_list_view, selected_row,
1258                         &enabled, &name, &account_id);
1259         prop->enabled = enabled;
1260
1261         prefs_filtering_list_view_set_row(-selected_row-2, prop);
1262         
1263         filteringprop_free(prop);
1264
1265         prefs_filtering_reset_dialog();
1266         modified = TRUE;
1267 }
1268
1269 static void prefs_filtering_top(gpointer action, gpointer data)
1270 {
1271         gint row;
1272         GtkTreeIter top, sel;
1273         GtkTreeModel *model;
1274
1275         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1276         if (row <= 1) 
1277                 return;
1278
1279         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));               
1280         
1281         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
1282         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1283                 return;
1284
1285         gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
1286         gtkut_list_view_select_row(filtering.cond_list_view, 1);
1287         modified = TRUE;
1288 }
1289
1290 static void prefs_filtering_page_up(gpointer action, gpointer data)
1291 {
1292         gint row, target_row;
1293         GtkTreeIter selected, target;
1294         GtkTreeModel *model;
1295         GtkTreePath *path;
1296         GdkRectangle cell_rect, view_rect;
1297
1298         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1299         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1300         if (row <= 1)
1301                 return;
1302
1303         if (!gtk_tree_model_iter_nth_child(model, &selected, NULL, row))
1304                 return;
1305
1306         /* compute number of rows per page (approximation) */
1307         path = gtk_tree_model_get_path(model, &selected);
1308         gtk_tree_view_get_cell_area(GTK_TREE_VIEW(filtering.cond_list_view), path, NULL, &cell_rect);
1309         gtk_tree_view_get_visible_rect(GTK_TREE_VIEW(filtering.cond_list_view), &view_rect);
1310         gtk_tree_path_free(path);
1311         target_row = row - (view_rect.height/cell_rect.height);
1312         if (target_row < 1)
1313                 target_row = 1;
1314
1315         if (!gtk_tree_model_iter_nth_child(model, &target, NULL, target_row))
1316                 return;
1317         gtk_list_store_move_before(GTK_LIST_STORE(model), &selected, &target);
1318         gtkut_list_view_select_row(filtering.cond_list_view, target_row);
1319         modified = TRUE;
1320 }
1321 static void prefs_filtering_up(gpointer action, gpointer data)
1322 {
1323         gint row;
1324         GtkTreeIter top, sel;
1325         GtkTreeModel *model;
1326
1327         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1328         if (row <= 1) 
1329                 return;
1330                 
1331         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1332
1333         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
1334         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1335                 return;
1336
1337         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1338         gtkut_list_view_select_row(filtering.cond_list_view, row - 1);
1339         modified = TRUE;
1340 }
1341
1342 static void prefs_filtering_down(gpointer action, gpointer data)
1343 {
1344         gint row, n_rows;
1345         GtkTreeIter top, sel;
1346         GtkTreeModel *model;
1347
1348         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1349         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1350         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1351         if (row < 1 || row >= n_rows - 1)
1352                 return;
1353
1354         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1355         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
1356                 return;
1357                         
1358         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1359         gtkut_list_view_select_row(filtering.cond_list_view, row + 1);
1360         modified = TRUE;
1361 }
1362
1363 static void prefs_filtering_page_down(gpointer action, gpointer data)
1364 {
1365         gint row, target_row, n_rows;
1366         GtkTreeIter selected, target;
1367         GtkTreeModel *model;
1368         GtkTreePath *path;
1369         GdkRectangle cell_rect, view_rect;
1370
1371         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1372         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1373         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1374         if (row < 1 || row >= n_rows -1)
1375                 return;
1376
1377         if (!gtk_tree_model_iter_nth_child(model, &selected, NULL, row))
1378                 return;
1379
1380         /* compute number of rows per page (approximation) */
1381         path = gtk_tree_model_get_path(model, &selected);
1382         gtk_tree_view_get_cell_area(GTK_TREE_VIEW(filtering.cond_list_view), path, NULL, &cell_rect);
1383         gtk_tree_view_get_visible_rect(GTK_TREE_VIEW(filtering.cond_list_view), &view_rect);
1384         gtk_tree_path_free(path);
1385         target_row = row + (view_rect.height/cell_rect.height);
1386         if (target_row > n_rows-1)
1387                 target_row = n_rows-1;
1388
1389         if (!gtk_tree_model_iter_nth_child(model, &target, NULL, target_row))
1390                 return;
1391         gtk_list_store_move_after(GTK_LIST_STORE(model), &selected, &target);
1392         gtkut_list_view_select_row(filtering.cond_list_view, target_row);
1393         modified = TRUE;
1394 }
1395
1396 static void prefs_filtering_bottom(gpointer action, gpointer data)
1397 {
1398         gint row, n_rows;
1399         GtkTreeIter top, sel;
1400         GtkTreeModel *model;
1401
1402         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1403         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1404         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1405         if (row < 1 || row >= n_rows - 1)
1406                 return;
1407
1408         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1409         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
1410                 return;
1411
1412         gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);           
1413         gtkut_list_view_select_row(filtering.cond_list_view, n_rows - 1);
1414         modified = TRUE;
1415 }
1416
1417 static void prefs_filtering_select_set(FilteringProp *prop)
1418 {
1419         gchar *matcher_str;
1420         gchar *action_str;
1421
1422         prefs_filtering_reset_dialog();
1423
1424         matcher_str = matcherlist_to_string(prop->matchers);
1425         if (matcher_str == NULL) {
1426                 return;
1427         }
1428         
1429         if (prop->name != NULL)
1430                 gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), prop->name);
1431
1432         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), prop->account_id);
1433                 
1434         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), matcher_str);
1435
1436         action_str = filteringaction_list_to_string(prop->action_list);
1437         if (matcher_str == NULL) {
1438                 return;
1439         }
1440         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), action_str);
1441
1442         g_free(action_str);
1443         g_free(matcher_str);
1444 }
1445
1446 static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
1447                                  gpointer data)
1448 {
1449         prefs_filtering_cancel(NULL, NULL);
1450         return TRUE;
1451 }
1452
1453 static gboolean prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
1454                                      gpointer data)
1455 {
1456         if (event && event->keyval == GDK_KEY_Escape) {
1457                 prefs_filtering_cancel(NULL, NULL);
1458                 return TRUE;                    
1459         }
1460         return FALSE;
1461 }
1462
1463 static gboolean prefs_filtering_check_mod(gboolean check_changed_list)
1464 {
1465         FilteringProp * prop;
1466         gchar * str;
1467         gchar * filtering_str;
1468         gint row = 1;
1469         AlertValue val;
1470         
1471         prop = prefs_filtering_dialog_to_filtering(FALSE);
1472         
1473         if (check_changed_list) {
1474                 if (modified && alertpanel(_("Filtering rules not saved"),
1475                                          _("The list of filtering rules have been modified. Close anyway?"),
1476                                          GTK_STOCK_CLOSE, _("+_Continue editing"), 
1477                                          NULL) != G_ALERTDEFAULT) {
1478                         return TRUE;
1479                 }
1480         }
1481         
1482         /* check if a rule is being edited */
1483         if (prop != NULL) {
1484                 str = filteringprop_to_string(prop);
1485
1486                 while (NULL != (filtering_str = (prefs_filtering_list_view_get_rule
1487                                                         (filtering.cond_list_view,
1488                                                          row)))) {
1489                         if (strcmp(filtering_str, str) == 0)
1490                                 break;
1491                         row++;
1492                         g_free(filtering_str);
1493                 }       
1494
1495                 if (!filtering_str) {
1496                         val = alertpanel(_("Entry not saved"),
1497                                  _("The entry was not saved. Close anyway?"),
1498                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1499                         if (G_ALERTDEFAULT != val) {
1500                                 g_free(filtering_str);
1501                                 g_free(str); /* fixed two leaks: huzzah! */
1502                                 filteringprop_free(prop);
1503                                 return TRUE;
1504                         }
1505                 }               
1506
1507                 g_free(filtering_str);
1508                 g_free(str);
1509                 filteringprop_free(prop); /* fixed a leak: huzzah! */
1510         } else {
1511                 gchar *name, *condition, *action;
1512                 name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
1513                 condition = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1514                 action = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1515                 if (strlen(name) || 
1516                     strlen(condition) || 
1517                     strlen(action)) {
1518                         val = alertpanel(_("Entry not saved"),
1519                                  _("The entry was not saved. Close anyway?"),
1520                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1521                         if (G_ALERTDEFAULT != val) {
1522                                 g_free(name);
1523                                 g_free(condition);
1524                                 g_free(action);
1525                                 return TRUE;
1526                         }
1527                 }
1528                 g_free(name);
1529                 g_free(condition);
1530                 g_free(action);
1531         }
1532         return FALSE;
1533 }
1534
1535 static void prefs_filtering_ok(gpointer action, gpointer data)
1536 {
1537         if (prefs_filtering_check_mod(FALSE))
1538                 return;
1539         modified = FALSE;
1540         prefs_filtering_set_list();
1541         prefs_matcher_write_config();
1542         prefs_filtering_close();
1543 }
1544
1545 static void prefs_filtering_cancel(gpointer action, gpointer data)
1546 {
1547         if (prefs_filtering_check_mod(TRUE))
1548                 return;
1549         modified = FALSE;
1550         prefs_matcher_read_config();
1551         prefs_filtering_close();
1552 }
1553
1554 static GtkListStore* prefs_filtering_create_data_store(void)
1555 {
1556         return gtk_list_store_new(N_PREFS_FILTERING_COLUMNS,
1557                                   G_TYPE_BOOLEAN,
1558                                   G_TYPE_STRING,
1559                                   G_TYPE_INT,
1560                                   G_TYPE_STRING,
1561                                   G_TYPE_STRING,
1562                                   G_TYPE_BOOLEAN,
1563                                  -1);
1564 }
1565
1566 /*!
1567  *\brief        Insert filtering rule into store. Note that we access the
1568  *              tree view / store by index, which is a bit suboptimal, but
1569  *              at least it made GTK 2 porting easier.
1570  *
1571  *\param        list_store Store to operate on
1572  *\param        row -1 to add a new rule to store,
1573                         row >=0 to change an existing row
1574                         row <-1 insert a new row after (-row-2)
1575  *\param        enabled TRUE if rule is enabled
1576  *\param        name The Name of rule
1577  *\param        account_id The account ID
1578  *\param        account_name The account name or All or (New)
1579  *\param        rule String representation of rule
1580  *\param        prop TRUE if valid filtering rule; if FALSE it's the first
1581  *              entry in the store ("(New)").
1582  *
1583  *\return       int Row of inserted / changed rule.
1584  */
1585 static gint prefs_filtering_list_view_insert_rule(GtkListStore *list_store,
1586                                                   gint row,
1587                                                   gboolean enabled,
1588                                                   const gchar *name,
1589                                                   gint account_id,
1590                                                   const gchar *account_name,
1591                                                   const gchar *rule,
1592                                                   gboolean prop) 
1593 {
1594         GtkTreeIter iter;
1595         GtkTreeIter sibling;
1596
1597         /* check if valid row at all */
1598         if (row >= 0) {
1599                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1600                                                    &iter, NULL, row))
1601                         row = -1;
1602         } else if (row < -1) {
1603                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1604                                                    &sibling, NULL, -row-2))
1605                         row = -1;                                                  
1606         }
1607
1608         if (row == -1 ) {
1609                 /* append new */
1610                 gtk_list_store_append(list_store, &iter);
1611                 gtk_list_store_set(list_store, &iter, 
1612                                    PREFS_FILTERING_ENABLED, enabled,
1613                                    PREFS_FILTERING_NAME, name,
1614                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1615                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1616                                    PREFS_FILTERING_RULE, rule,
1617                                    PREFS_FILTERING_PROP, prop,
1618                                    -1);
1619                 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store),
1620                                                       NULL) - 1;
1621         } else if (row < -1) {
1622                 /* duplicate */
1623                 gtk_list_store_insert_after(list_store, &iter, &sibling);
1624                 gtk_list_store_set(list_store, &iter, 
1625                                    PREFS_FILTERING_ENABLED, enabled,
1626                                    PREFS_FILTERING_NAME, name,
1627                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1628                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1629                                    PREFS_FILTERING_RULE, rule,
1630                                    PREFS_FILTERING_PROP, prop,
1631                                    -1);
1632                 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store),
1633                                                       NULL) - 1;
1634         } else {
1635                 /* change existing */
1636                 gtk_list_store_set(list_store, &iter, 
1637                                    PREFS_FILTERING_ENABLED, enabled,
1638                                    PREFS_FILTERING_NAME, name,
1639                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1640                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1641                                    PREFS_FILTERING_RULE, rule,
1642                                    -1);
1643                 return row;                                
1644         }
1645 }
1646
1647 /*!
1648  *\return       gchar * Rule at specified row - should be freed.
1649  */
1650 static gchar *prefs_filtering_list_view_get_rule(GtkWidget *list, gint row)
1651 {       
1652         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1653         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1654         GtkTreeIter iter;
1655         gchar *result;
1656
1657         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1658                 return NULL;
1659         
1660         gtk_tree_model_get(model, &iter, 
1661                            PREFS_FILTERING_RULE, &result,
1662                            -1);
1663         
1664         return result;
1665 }
1666
1667 static void prefs_filtering_list_view_get_rule_info(GtkWidget *list, gint row,
1668                                 gboolean *enabled, gchar **name, gint *account_id)
1669 {       
1670         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1671         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1672         GtkTreeIter iter;
1673
1674         *enabled = TRUE;
1675         *name = NULL;
1676
1677         if (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1678                 gtk_tree_model_get(model, &iter, 
1679                                    PREFS_FILTERING_ENABLED, enabled,
1680                                    PREFS_FILTERING_NAME, name,
1681                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1682                                    -1);
1683         }
1684 }
1685
1686 static GtkActionGroup *prefs_filtering_popup_action = NULL;
1687 static GtkWidget *prefs_filtering_popup_menu = NULL;
1688
1689 static GtkActionEntry prefs_filtering_popup_entries[] =
1690 {
1691         {"PrefsFilteringPopup",                 NULL, "PrefsFilteringPopup" },
1692         {"PrefsFilteringPopup/Delete",          NULL, N_("_Delete"), NULL, NULL, G_CALLBACK(prefs_filtering_delete_cb) },
1693         {"PrefsFilteringPopup/DeleteAll",       NULL, N_("Delete _all"), NULL, NULL, G_CALLBACK(prefs_filtering_delete_all_cb) },
1694         {"PrefsFilteringPopup/Duplicate",       NULL, N_("D_uplicate"), NULL, NULL, G_CALLBACK(prefs_filtering_duplicate_cb) },
1695 #ifdef GENERIC_UMPC
1696         {"PrefsFilteringPopup/---",             NULL, "---", NULL, NULL, NULL },
1697         {"PrefsFilteringPopup/PageUp",          NULL, N_("Move one page up"), NULL, NULL, G_CALLBACK(prefs_filtering_page_up) },
1698         {"PrefsFilteringPopup/PageDown",        NULL, N_("Move one page down"), NULL, NULL, G_CALLBACK(prefs_filtering_page_down) },
1699 #endif
1700 };
1701
1702 static void prefs_filtering_row_selected(GtkTreeSelection *selection,
1703                                          GtkTreeView *list_view)
1704 {
1705         GtkTreePath *path;
1706         GtkTreeIter iter;
1707         GtkTreeModel *model;
1708         
1709         if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1710                 return;
1711         
1712         path = gtk_tree_model_get_path(model, &iter);
1713         prefs_filtering_select_row(list_view, path);
1714         gtk_tree_path_free(path);
1715 }
1716
1717 static gint prefs_filtering_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1718                                     GtkTreeView *list_view)
1719 {
1720         if (event) {
1721                 /* left- or right-button click */
1722                 if (event->button == 1 || event->button == 3) {
1723                         GtkTreePath *path = NULL;
1724
1725                         if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1726                                                                 &path, NULL, NULL, NULL)) {
1727                                 prefs_filtering_select_row(list_view, path);
1728                         }
1729                         if (path)
1730                                 gtk_tree_path_free(path);
1731                 }
1732
1733                 /* right-button click */
1734                 if (event->button == 3) {
1735                         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1736                         GtkTreeIter iter;
1737                         gboolean non_empty;
1738                         gint row;
1739
1740                         if (!prefs_filtering_popup_menu) {
1741                                 prefs_filtering_popup_action = cm_menu_create_action_group("PrefsFilteringPopup", prefs_filtering_popup_entries,
1742                                         G_N_ELEMENTS(prefs_filtering_popup_entries), (gpointer)list_view);
1743                                 MENUITEM_ADDUI("/Menus", "PrefsFilteringPopup", "PrefsFilteringPopup", GTK_UI_MANAGER_MENU)
1744                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Delete", "PrefsFilteringPopup/Delete", GTK_UI_MANAGER_MENUITEM)
1745                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "DeleteAll", "PrefsFilteringPopup/DeleteAll", GTK_UI_MANAGER_MENUITEM)
1746                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Duplicate", "PrefsFilteringPopup/Duplicate", GTK_UI_MANAGER_MENUITEM)
1747 #ifdef GENERIC_UMPC
1748                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Separator1", "PrefsFilteringPopup/---", GTK_UI_MANAGER_SEPARATOR)
1749                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "PageUp", "PrefsFilteringPopup/PageUp", GTK_UI_MANAGER_MENUITEM)
1750                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "PageDown", "PrefsFilteringPopup/PageDown", GTK_UI_MANAGER_MENUITEM)
1751 #endif
1752                                 prefs_filtering_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1753                                         gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/PrefsFilteringPopup")) );
1754                         }
1755
1756                         /* grey out some popup menu items if there is no selected row */
1757                         row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1758                         cm_menu_set_sensitive("PrefsFilteringPopup/Delete", (row > 0));
1759                         cm_menu_set_sensitive("PrefsFilteringPopup/Duplicate", (row > 0));
1760
1761                         /* grey out seom popup menu items if there is no row
1762                            (not counting the (New) one at row 0) */
1763                         non_empty = gtk_tree_model_get_iter_first(model, &iter);
1764                         if (non_empty)
1765                                 non_empty = gtk_tree_model_iter_next(model, &iter);
1766                         cm_menu_set_sensitive("PrefsFilteringPopup/DeleteAll", non_empty);
1767
1768                         gtk_menu_popup(GTK_MENU(prefs_filtering_popup_menu), 
1769                                    NULL, NULL, NULL, NULL, 
1770                                    event->button, event->time);
1771                 }
1772         }
1773         return FALSE;
1774 }
1775
1776 static gboolean prefs_filtering_list_popup_menu(GtkWidget *widget, gpointer data)
1777 {
1778         GtkTreeView *list_view = (GtkTreeView *)data;
1779         GdkEventButton event;
1780         
1781         event.button = 3;
1782         event.time = gtk_get_current_event_time();
1783         
1784         prefs_filtering_list_btn_pressed(NULL, &event, list_view);
1785
1786         return TRUE;
1787 }
1788
1789 /*!
1790  *\brief        Create list view for filtering
1791  */
1792 static GtkWidget *prefs_filtering_list_view_create(void)
1793 {
1794         GtkTreeView *list_view;
1795         GtkTreeSelection *selector;
1796
1797         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL
1798                 (prefs_filtering_create_data_store())));
1799 #ifdef GENERIC_UMPC
1800         g_object_set(list_view, "allow-checkbox-mode", FALSE, NULL);
1801 #endif
1802
1803 #ifndef MAEMO
1804         g_signal_connect(G_OBJECT(list_view), "popup-menu",
1805                          G_CALLBACK(prefs_filtering_list_popup_menu), list_view);
1806 #else
1807         gtk_widget_tap_and_hold_setup(GTK_WIDGET(list_view), NULL, NULL,
1808                         GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
1809         g_signal_connect(G_OBJECT(list_view), "tap-and-hold",
1810                          G_CALLBACK(prefs_filtering_list_popup_menu), list_view);
1811 #endif
1812         g_signal_connect(G_OBJECT(list_view), "button-press-event",
1813                         G_CALLBACK(prefs_filtering_list_btn_pressed), list_view);
1814         
1815         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1816         gtk_tree_view_set_reorderable(list_view, TRUE);
1817         
1818         selector = gtk_tree_view_get_selection(list_view);
1819         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1820         g_signal_connect(G_OBJECT(selector), "changed",
1821                          G_CALLBACK(prefs_filtering_row_selected), list_view);
1822
1823         /* create the columns */
1824         prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view));
1825
1826         return GTK_WIDGET(list_view);
1827 }
1828
1829 static void prefs_filtering_enable_toggled(GtkCellRendererToggle *widget,
1830                 gchar *path,
1831                 GtkWidget *list_view)
1832 {
1833         GtkTreeIter iter;
1834         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1835         gboolean enabled = TRUE;
1836
1837         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1838                 return;
1839
1840         gtk_tree_model_get(model, &iter,
1841                            PREFS_FILTERING_ENABLED, &enabled,
1842                            -1);
1843
1844         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1845                            PREFS_FILTERING_ENABLED, !enabled,
1846                            -1);
1847 }
1848
1849 static void prefs_filtering_create_list_view_columns(GtkWidget *list_view)
1850 {
1851         GtkTreeViewColumn *column;
1852         GtkCellRenderer *renderer;
1853
1854         renderer = gtk_cell_renderer_toggle_new();
1855         g_object_set(renderer,
1856                      "radio", FALSE,
1857                      "activatable", TRUE,
1858                      NULL);
1859         column = gtk_tree_view_column_new_with_attributes
1860                 (_("Enable"), /* FIXME : Enable, Enabled, or 'E' ? */
1861                  renderer,
1862                  "active", PREFS_FILTERING_ENABLED,
1863                  NULL);
1864         gtk_tree_view_column_set_alignment (column, 0.5);
1865         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1866         g_signal_connect(G_OBJECT(renderer), "toggled",
1867                          G_CALLBACK(prefs_filtering_enable_toggled),
1868                          list_view);
1869
1870         renderer = gtk_cell_renderer_text_new();
1871         column = gtk_tree_view_column_new_with_attributes
1872                 (_("Name"),
1873                  renderer,
1874                  "text", PREFS_FILTERING_NAME,
1875                  NULL);
1876         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1877         gtk_tree_view_column_set_resizable(column, TRUE);
1878
1879         renderer = gtk_cell_renderer_text_new();
1880         column = gtk_tree_view_column_new_with_attributes
1881                 (_("Account"),
1882                  renderer,
1883                  "text", PREFS_FILTERING_ACCOUNT_NAME,
1884                  NULL);
1885         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1886         gtk_tree_view_column_set_resizable(column, TRUE);
1887
1888         filtering.account_name_column = column;
1889                 
1890         renderer = gtk_cell_renderer_text_new();
1891         column = gtk_tree_view_column_new_with_attributes
1892                 (_("Rule"),
1893                  renderer,
1894                  "text", PREFS_FILTERING_RULE,
1895                  NULL);
1896
1897         gtk_tree_view_set_search_column(GTK_TREE_VIEW(list_view), PREFS_FILTERING_NAME);
1898         gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(list_view), prefs_filtering_search_func_cb , NULL, NULL);
1899         
1900         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1901 }
1902
1903 /*!
1904  *\brief        Triggered when a row has to be selected
1905  */
1906 static void prefs_filtering_select_row(GtkTreeView *list_view, GtkTreePath *path)
1907 {
1908         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1909
1910         if (path && model) {
1911                 GtkTreeSelection *selection;
1912                 gboolean has_prop = FALSE;
1913                 GtkTreeIter iter;
1914
1915                 /* select row */
1916                 selection = gtk_tree_view_get_selection(list_view);
1917                 gtk_tree_selection_select_path(selection, path);
1918
1919                 /* update dialog from selection */
1920                 gtk_tree_model_get_iter(model, &iter, path);
1921                 gtk_tree_model_get(model, &iter,
1922                                    PREFS_FILTERING_PROP, &has_prop,
1923                                    -1);
1924
1925                 if (has_prop) {
1926                         FilteringProp *prop;
1927                         gchar *filtering_str = NULL;
1928                         gchar *name = NULL;
1929                         gint account_id = 0;
1930
1931                         gtk_tree_model_get(model, &iter,
1932                                            PREFS_FILTERING_RULE, &filtering_str,
1933                                            -1);
1934                         gtk_tree_model_get(model, &iter,
1935                                            PREFS_FILTERING_NAME, &name,
1936                                            -1);
1937                         gtk_tree_model_get(model, &iter,
1938                                            PREFS_FILTERING_ACCOUNT_ID, &account_id,
1939                                            -1);
1940
1941                         prop = matcher_parser_get_filtering(filtering_str);
1942                         if (prop) {
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 }