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