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