cb0f70d0d07ab670aa8b8db5e4dd336fd63d914b
[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_tag(GSList * filters,
769                         const gchar * old_tag, const gchar * new_tag)
770 {
771         GSList * action_cur;
772         GSList * cur;
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 (!strcmp(action->destination, old_tag)) {
788                                 g_free(action->destination);
789                                 action->destination = g_strdup(new_tag);
790                         }
791                 }
792         }
793 }
794
795 static gboolean prefs_filtering_rename_tag_func(GNode *node, gpointer data)
796 {
797         GSList *filters;
798         const gchar * old_tag;
799         const gchar * new_tag;
800         const gchar ** tags;
801         FolderItem *item;
802         
803         tags = data;
804         old_tag = tags[0];
805         new_tag = tags[1];
806
807         cm_return_val_if_fail(old_tag != NULL, FALSE);
808         cm_return_val_if_fail(new_tag != NULL, FALSE);
809         cm_return_val_if_fail(node != NULL, FALSE);
810
811         item = node->data;
812         if (!item || !item->prefs)
813                 return FALSE;
814         filters = item->prefs->processing;
815
816         rename_tag(filters, old_tag, new_tag);
817
818         return FALSE;
819 }
820
821 void prefs_filtering_delete_path(const gchar *path)
822 {
823         GList * cur;
824         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
825                 Folder *folder;
826                 folder = (Folder *) cur->data;
827                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
828                                 prefs_filtering_delete_path_func, (gchar *)path);
829         }
830         delete_path(&pre_global_processing, path);
831         delete_path(&post_global_processing, path);
832         delete_path(&filtering_rules, path);
833         
834         prefs_matcher_write_config();
835 }
836
837 static void delete_path(GSList ** p_filters, const gchar * path)
838 {
839         GSList * filters;
840         GSList * duplist;
841         gchar *suffix;
842         gint destlen;
843         gint prefixlen;
844         gint pathlen;
845         GSList * action_cur;
846         GSList * cur;
847         
848         filters = *p_filters;
849         pathlen = strlen(path);
850         duplist = g_slist_copy(filters);
851         for (cur = duplist ; cur != NULL; cur = g_slist_next(cur)) {
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;
858                         
859                         action = action_cur->data;
860                         
861                         if (action->type == MATCHACTION_SET_TAG ||
862                             action->type == MATCHACTION_UNSET_TAG)
863                                 continue;
864                         if (!action->destination) 
865                                 continue;
866                         
867                         destlen = strlen(action->destination);
868                         
869                         if (destlen > pathlen) {
870                                 prefixlen = destlen - pathlen;
871                                 suffix = action->destination + prefixlen;
872                                 
873                                 if (suffix && !strncmp(path, suffix, pathlen)) {
874                                         filteringprop_free(filtering);
875                                         filters = g_slist_remove(filters, filtering);
876                                 }
877                         } else if (strcmp(action->destination, path) == 0) {
878                                 filteringprop_free(filtering);
879                                 filters = g_slist_remove(filters, filtering);
880                         }
881                 }
882         }                
883         g_slist_free(duplist);
884         
885         * p_filters = filters;
886 }
887
888 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data)
889 {
890         const gchar *path = data;
891         FolderItem *item;
892         GSList ** p_filters;
893         
894         cm_return_val_if_fail(path != NULL, FALSE);
895         cm_return_val_if_fail(node != NULL, FALSE);
896
897         item = node->data;
898         if (!item || !item->prefs)
899                 return FALSE;
900         p_filters = &item->prefs->processing;
901         
902         delete_path(p_filters, path);
903
904         return FALSE;
905 }
906
907 static void prefs_filtering_clear_list(GtkListStore *list_store)
908 {
909         gtk_list_store_clear(list_store);
910
911         /* add the place holder (New) at row 0 */
912         prefs_filtering_list_view_insert_rule(list_store, -1, 
913                                               FALSE,
914                                               _("(New)"),
915                                                   0,
916                                               _("(New)"),
917                                               _("(New)"),
918                                               FALSE);
919 }
920
921 static void prefs_filtering_set_dialog(const gchar *header, const gchar *key)
922 {
923         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
924         GSList *cur;
925         GSList * prefs_filtering;
926         gchar *cond_str;
927         GtkListStore *list_store;
928         
929         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
930         prefs_filtering_clear_list(list_store);
931
932         prefs_filtering = *p_processing_list;
933
934         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur)) {
935                 FilteringProp * prop = (FilteringProp *) cur->data;
936                 gchar *account_name = NULL;
937
938                 if (prop->account_id > 0) {
939                         PrefsAccount *ac_prefs = account_find_from_id(prop->account_id);
940
941                         if (ac_prefs)
942                                 account_name = ac_prefs->account_name;
943                 }
944                 if (account_name == NULL)
945                         account_name = (gchar *)Q_("Filtering Account Menu|All");
946
947                 cond_str = filteringprop_to_string(prop);
948                 subst_char(cond_str, '\t', ':');
949
950                 prefs_filtering_list_view_insert_rule(list_store, -1, 
951                                                       prop->enabled,
952                                                       prop->name,
953                                                           prop->account_id,
954                                                           account_name,
955                                                       cond_str, TRUE);
956                 
957                 g_free(cond_str);
958         }
959
960         prefs_filtering_reset_dialog();
961
962         if (header && key) {
963                 gchar * quoted_key;
964                 gchar *match_str;
965
966                 quoted_key = matcher_quote_str(key);
967                 
968                 match_str = g_strconcat(header, " ", get_matchparser_tab_str(MATCHTYPE_MATCHCASE),
969                                         " \"", quoted_key, "\"", NULL);
970                 g_free(quoted_key);
971                 
972                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), match_str);
973                 g_free(match_str);
974         }
975 }
976
977 static void prefs_filtering_reset_dialog(void)
978 {
979         gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), "");
980         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
981         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), "");
982         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), "");
983 }
984
985 static gboolean prefs_filtering_search_func_cb (GtkTreeModel *model, gint column, const gchar *key, 
986                                                 GtkTreeIter *iter, gpointer search_data) 
987 {
988         gchar *store_string;
989         gboolean retval;
990         GtkTreePath *path;
991
992         gtk_tree_model_get (model, iter, column, &store_string, -1);
993
994         if (!store_string || !key) return FALSE;
995
996
997         retval = (strncmp (key, store_string, strlen(key)) != 0);
998
999         g_free(store_string);
1000         debug_print("selecting row\n");
1001         path = gtk_tree_model_get_path(model, iter);
1002         prefs_filtering_select_row(GTK_TREE_VIEW(filtering.cond_list_view), path);
1003         gtk_tree_path_free(path);
1004
1005         return retval;
1006 }
1007
1008 static void prefs_filtering_set_list(void)
1009 {
1010         gint row = 1;
1011         FilteringProp *prop;
1012         GSList * cur;
1013         gchar * filtering_str;
1014         GSList * prefs_filtering;
1015
1016         prefs_filtering = *p_processing_list;
1017         for (cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
1018                 filteringprop_free((FilteringProp *) cur->data);
1019         g_slist_free(prefs_filtering);
1020         prefs_filtering = NULL;
1021         
1022
1023         while (NULL != (filtering_str = prefs_filtering_list_view_get_rule
1024                                                 (filtering.cond_list_view, row))) {
1025                 /* FIXME: this strcmp() is bogus: "(New)" should never
1026                  * be inserted in the storage */
1027                 if (strcmp(filtering_str, _("(New)")) != 0) {
1028                         gboolean enabled;
1029                         gchar *name;
1030                         gint account_id = 0;
1031
1032                         prefs_filtering_list_view_get_rule_info(
1033                                         filtering.cond_list_view, row,
1034                                         &enabled, &name, &account_id);
1035                         prop = matcher_parser_get_filtering(filtering_str);
1036                         g_free(filtering_str);
1037                         if (prop) {
1038                                 prop->enabled = enabled;
1039                                 prop->name = name;
1040                                 prop->account_id = account_id;
1041                                 prefs_filtering = 
1042                                         g_slist_append(prefs_filtering, prop);
1043                         }
1044                 }
1045                 
1046                 row++;
1047         }                               
1048         
1049         *p_processing_list = prefs_filtering;
1050 }
1051
1052 static gint prefs_filtering_list_view_set_row(gint row, FilteringProp * prop)
1053 {
1054         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
1055         gchar *str = NULL;
1056         GtkListStore *list_store;
1057         gchar *name = NULL;
1058         gint account_id = 0;
1059         gchar *account_name = (gchar *)Q_("Filtering Account Menu|All");
1060         gboolean enabled = TRUE;
1061
1062         if (prop)
1063                 str = filteringprop_to_string(prop);
1064         if (str == NULL)
1065                 return -1;
1066
1067         if (prop) {
1068                 if (prop->name)
1069                         name = prop->name;
1070                 account_id = prop->account_id;
1071                 if (account_id > 0)
1072                         account_name = account_find_from_id(account_id)->account_name;
1073                 enabled = prop->enabled;
1074         }
1075
1076         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
1077
1078         row = prefs_filtering_list_view_insert_rule(list_store, row,
1079                                                     enabled,
1080                                                     name,
1081                                                     account_id,
1082                                                         account_name,
1083                                                     str,
1084                                                     prop != NULL);
1085
1086         g_free(str);
1087
1088         return row;
1089 }
1090
1091 static void prefs_filtering_condition_define_done(MatcherList * matchers)
1092 {
1093         gchar * str;
1094
1095         if (matchers == NULL)
1096                 return;
1097
1098         str = matcherlist_to_string(matchers);
1099
1100         if (str != NULL) {
1101                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), str);
1102                 g_free(str);
1103         }
1104 }
1105
1106 static void prefs_filtering_condition_define(gpointer action, gpointer data)
1107 {
1108         gchar * cond_str;
1109         MatcherList * matchers = NULL;
1110
1111         cond_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1112
1113         if (*cond_str != '\0') {
1114                 matchers = matcher_parser_get_cond(cond_str, NULL);
1115                 if (matchers == NULL)
1116                         alertpanel_error(_("Condition string is not valid."));
1117         }
1118         
1119         g_free(cond_str);
1120
1121         prefs_matcher_open(matchers, prefs_filtering_condition_define_done);
1122
1123         if (matchers != NULL)
1124                 matcherlist_free(matchers);
1125 }
1126
1127 static void prefs_filtering_action_define_done(GSList * action_list)
1128 {
1129         gchar * str;
1130
1131         if (action_list == NULL)
1132                 return;
1133
1134         str = filteringaction_list_to_string(action_list);
1135
1136         if (str != NULL) {
1137                 gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), str);
1138                 g_free(str);
1139         }
1140 }
1141
1142 static void prefs_filtering_action_define(gpointer action, gpointer data)
1143 {
1144         gchar * action_str;
1145         GSList * action_list = NULL;
1146
1147         action_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1148
1149         if (*action_str != '\0') {
1150                 action_list = matcher_parser_get_action_list(action_str);
1151                 if (action_list == NULL)
1152                         alertpanel_error(_("Action string is not valid."));
1153         }
1154         
1155         g_free(action_str);
1156
1157         prefs_filtering_action_open(action_list,
1158             prefs_filtering_action_define_done);
1159
1160         if (action_list != NULL) {
1161                 GSList * cur;
1162                 for(cur = action_list ; cur != NULL ; cur = cur->next) {
1163                         filteringaction_free(cur->data);
1164                 }
1165         }
1166 }
1167
1168
1169 /* register / substitute delete buttons */
1170
1171
1172 static FilteringProp * prefs_filtering_dialog_to_filtering(gboolean alert)
1173 {
1174         MatcherList * cond;
1175         gboolean enabled = TRUE;
1176         gchar * name = NULL;
1177         gint account_id = 0;
1178         gchar * cond_str = NULL;
1179         gchar * action_str = NULL;
1180         FilteringProp * prop = NULL;
1181         GSList * action_list;
1182
1183         name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
1184         
1185         account_id = combobox_get_active_data(GTK_COMBO_BOX(filtering.account_combobox));
1186
1187         cond_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1188         if (*cond_str == '\0') {
1189                 if(alert == TRUE) alertpanel_error(_("Condition string is empty."));
1190                 goto fail;
1191         }
1192         
1193         action_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1194         if (*action_str == '\0') {
1195                 if(alert == TRUE) alertpanel_error(_("Action string is empty."));
1196                 goto fail;
1197         }
1198
1199         cond = matcher_parser_get_cond(cond_str, NULL);
1200
1201         if (cond == NULL) {
1202                 if(alert == TRUE) alertpanel_error(_("Condition string is not valid."));
1203                 goto fail;
1204         }
1205         
1206         action_list = matcher_parser_get_action_list(action_str);
1207         
1208
1209         if (action_list == NULL) {
1210                 if(alert == TRUE) alertpanel_error(_("Action string is not valid."));
1211                 goto fail;
1212         }
1213
1214         prop = filteringprop_new(enabled, name, account_id, cond, action_list);
1215
1216 fail:
1217         g_free(name);
1218         g_free(cond_str);
1219         g_free(action_str);
1220         return prop;
1221 }
1222
1223 static void prefs_filtering_register_cb(gpointer action, gpointer data)
1224 {
1225         FilteringProp *prop;
1226         
1227         prop = prefs_filtering_dialog_to_filtering(TRUE);
1228         if (prop == NULL)
1229                 return;
1230         prefs_filtering_list_view_set_row(-1, prop);
1231         
1232         filteringprop_free(prop);
1233
1234         prefs_filtering_reset_dialog();
1235         modified = TRUE;
1236 }
1237
1238 static void prefs_filtering_substitute_cb(gpointer action, gpointer data)
1239 {
1240         gint selected_row = gtkut_list_view_get_selected_row
1241                 (filtering.cond_list_view);
1242         FilteringProp *prop;
1243         gboolean enabled;
1244         gchar *name;
1245         gint account_id;
1246         
1247         if (selected_row <= 0)
1248                 return;
1249
1250         prop = prefs_filtering_dialog_to_filtering(TRUE);
1251
1252         if (prop == NULL) 
1253                 return;
1254
1255         /* prop->emabled is always TRUE here, re-use the value from the selected row 
1256            as we don't substitute this value from dialog */
1257         prefs_filtering_list_view_get_rule_info(
1258                         filtering.cond_list_view, selected_row,
1259                         &enabled, &name, &account_id);
1260         prop->enabled = enabled;
1261
1262         prefs_filtering_list_view_set_row(selected_row, prop);
1263
1264         filteringprop_free(prop);
1265
1266         prefs_filtering_row_selected(gtk_tree_view_get_selection(
1267                                 GTK_TREE_VIEW(filtering.cond_list_view)),
1268                                 GTK_TREE_VIEW(filtering.cond_list_view));
1269         modified = TRUE;
1270 }
1271
1272 static void prefs_filtering_delete_cb(gpointer action, gpointer data)
1273 {
1274         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
1275         GtkTreeModel *model;
1276         GtkTreeIter iter;
1277         gint selected_row;
1278         
1279         selected_row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1280         if (selected_row <= 0) 
1281                 return; 
1282
1283         if (alertpanel(_("Delete rule"),
1284                        _("Do you really want to delete this rule?"),
1285                        GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
1286                 return;
1287
1288         model = gtk_tree_view_get_model(list_view);     
1289         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, selected_row))
1290                 return;
1291
1292         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1293
1294         prefs_filtering_reset_dialog();
1295         modified = TRUE;
1296 }
1297
1298 static void prefs_filtering_delete_all_cb(gpointer action, gpointer data)
1299 {
1300         GtkListStore *list_store;
1301         
1302         if (alertpanel(_("Delete all rules"),
1303                        _("Do you really want to delete all the rules?"),
1304                        GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
1305                 return;
1306
1307         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view)));
1308         prefs_filtering_clear_list(list_store);
1309
1310         prefs_filtering_reset_dialog();
1311         modified = TRUE;
1312 }
1313
1314 static void prefs_filtering_clear_cb(gpointer action, gpointer data)
1315 {
1316         prefs_filtering_reset_dialog();
1317 }
1318
1319 static void prefs_filtering_duplicate_cb(gpointer action, gpointer data)
1320 {
1321         gint selected_row = gtkut_list_view_get_selected_row
1322                 (filtering.cond_list_view);     
1323         FilteringProp *prop;
1324         gboolean enabled;
1325         gchar *name;
1326         gint account_id;
1327
1328         if (selected_row <= 0) 
1329                 return; 
1330         
1331         prop = prefs_filtering_dialog_to_filtering(TRUE);
1332         if (prop == NULL)
1333                 return;
1334
1335         /* prop->emabled is always TRUE here, re-use the value from the selected row 
1336            as we don't substitute this value from dialog */
1337         prefs_filtering_list_view_get_rule_info(
1338                         filtering.cond_list_view, selected_row,
1339                         &enabled, &name, &account_id);
1340         prop->enabled = enabled;
1341
1342         prefs_filtering_list_view_set_row(-selected_row-2, prop);
1343         
1344         filteringprop_free(prop);
1345
1346         prefs_filtering_reset_dialog();
1347         modified = TRUE;
1348 }
1349
1350 static void prefs_filtering_top(gpointer action, gpointer data)
1351 {
1352         gint row;
1353         GtkTreeIter top, sel;
1354         GtkTreeModel *model;
1355
1356         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1357         if (row <= 1) 
1358                 return;
1359
1360         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));               
1361         
1362         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
1363         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1364                 return;
1365
1366         gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
1367         gtkut_list_view_select_row(filtering.cond_list_view, 1);
1368         modified = TRUE;
1369 }
1370
1371 static void prefs_filtering_page_up(gpointer action, gpointer data)
1372 {
1373         gint row, target_row, n_rows;
1374         GtkTreeIter selected, target;
1375         GtkTreeModel *model;
1376         GtkTreePath *path;
1377         GdkRectangle cell_rect, view_rect;
1378
1379         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1380         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1381         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1382         if (row <= 1)
1383                 return;
1384
1385         if (!gtk_tree_model_iter_nth_child(model, &selected, NULL, row))
1386                 return;
1387
1388         /* compute number of rows per page (approximation) */
1389         path = gtk_tree_model_get_path(model, &selected);
1390         gtk_tree_view_get_cell_area(GTK_TREE_VIEW(filtering.cond_list_view), path, NULL, &cell_rect);
1391         gtk_tree_view_get_visible_rect(GTK_TREE_VIEW(filtering.cond_list_view), &view_rect);
1392         gtk_tree_path_free(path);
1393         target_row = row - (view_rect.height/cell_rect.height);
1394         if (target_row < 1)
1395                 target_row = 1;
1396
1397         if (!gtk_tree_model_iter_nth_child(model, &target, NULL, target_row))
1398                 return;
1399         gtk_list_store_move_before(GTK_LIST_STORE(model), &selected, &target);
1400         gtkut_list_view_select_row(filtering.cond_list_view, target_row);
1401         modified = TRUE;
1402 }
1403 static void prefs_filtering_up(gpointer action, gpointer data)
1404 {
1405         gint row;
1406         GtkTreeIter top, sel;
1407         GtkTreeModel *model;
1408
1409         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1410         if (row <= 1) 
1411                 return;
1412                 
1413         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1414
1415         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
1416         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1417                 return;
1418
1419         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1420         gtkut_list_view_select_row(filtering.cond_list_view, row - 1);
1421         modified = TRUE;
1422 }
1423
1424 static void prefs_filtering_down(gpointer action, gpointer data)
1425 {
1426         gint row, n_rows;
1427         GtkTreeIter top, sel;
1428         GtkTreeModel *model;
1429
1430         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1431         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1432         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1433         if (row < 1 || row >= n_rows - 1)
1434                 return;
1435
1436         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1437         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
1438                 return;
1439                         
1440         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1441         gtkut_list_view_select_row(filtering.cond_list_view, row + 1);
1442         modified = TRUE;
1443 }
1444
1445 static void prefs_filtering_page_down(gpointer action, gpointer data)
1446 {
1447         gint row, target_row, n_rows;
1448         GtkTreeIter selected, target;
1449         GtkTreeModel *model;
1450         GtkTreePath *path;
1451         GdkRectangle cell_rect, view_rect;
1452
1453         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1454         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1455         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1456         if (row < 1 || row >= n_rows -1)
1457                 return;
1458
1459         if (!gtk_tree_model_iter_nth_child(model, &selected, NULL, row))
1460                 return;
1461
1462         /* compute number of rows per page (approximation) */
1463         path = gtk_tree_model_get_path(model, &selected);
1464         gtk_tree_view_get_cell_area(GTK_TREE_VIEW(filtering.cond_list_view), path, NULL, &cell_rect);
1465         gtk_tree_view_get_visible_rect(GTK_TREE_VIEW(filtering.cond_list_view), &view_rect);
1466         gtk_tree_path_free(path);
1467         target_row = row + (view_rect.height/cell_rect.height);
1468         if (target_row > n_rows-1)
1469                 target_row = n_rows-1;
1470
1471         if (!gtk_tree_model_iter_nth_child(model, &target, NULL, target_row))
1472                 return;
1473         gtk_list_store_move_after(GTK_LIST_STORE(model), &selected, &target);
1474         gtkut_list_view_select_row(filtering.cond_list_view, target_row);
1475         modified = TRUE;
1476 }
1477
1478 static void prefs_filtering_bottom(gpointer action, gpointer data)
1479 {
1480         gint row, n_rows;
1481         GtkTreeIter top, sel;
1482         GtkTreeModel *model;
1483
1484         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1485         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1486         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1487         if (row < 1 || row >= n_rows - 1)
1488                 return;
1489
1490         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1491         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
1492                 return;
1493
1494         gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);           
1495         gtkut_list_view_select_row(filtering.cond_list_view, n_rows - 1);
1496         modified = TRUE;
1497 }
1498
1499 static void prefs_filtering_select_set(FilteringProp *prop)
1500 {
1501         gchar *matcher_str;
1502         gchar *action_str;
1503
1504         prefs_filtering_reset_dialog();
1505
1506         matcher_str = matcherlist_to_string(prop->matchers);
1507         if (matcher_str == NULL) {
1508                 return;
1509         }
1510         
1511         if (prop->name != NULL)
1512                 gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), prop->name);
1513
1514         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), prop->account_id);
1515                 
1516         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), matcher_str);
1517
1518         action_str = filteringaction_list_to_string(prop->action_list);
1519         if (matcher_str == NULL) {
1520                 return;
1521         }
1522         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), action_str);
1523
1524         g_free(action_str);
1525         g_free(matcher_str);
1526 }
1527
1528 static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
1529                                  gpointer data)
1530 {
1531         prefs_filtering_cancel(NULL, NULL);
1532         return TRUE;
1533 }
1534
1535 static gboolean prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
1536                                      gpointer data)
1537 {
1538         if (event && event->keyval == GDK_KEY_Escape) {
1539                 prefs_filtering_cancel(NULL, NULL);
1540                 return TRUE;                    
1541         }
1542         return FALSE;
1543 }
1544
1545 static gboolean prefs_filtering_check_mod(gboolean check_changed_list)
1546 {
1547         FilteringProp * prop;
1548         gchar * str;
1549         gchar * filtering_str;
1550         gint row = 1;
1551         AlertValue val;
1552         
1553         prop = prefs_filtering_dialog_to_filtering(FALSE);
1554         
1555         if (check_changed_list) {
1556                 if (modified && alertpanel(_("Filtering rules not saved"),
1557                                          _("The list of filtering rules have been modified. Close anyway?"),
1558                                          GTK_STOCK_CLOSE, _("+_Continue editing"), 
1559                                          NULL) != G_ALERTDEFAULT) {
1560                         return TRUE;
1561                 }
1562         }
1563         
1564         /* check if a rule is being edited */
1565         if (prop != NULL) {
1566                 str = filteringprop_to_string(prop);
1567
1568                 while (NULL != (filtering_str = (prefs_filtering_list_view_get_rule
1569                                                         (filtering.cond_list_view,
1570                                                          row)))) {
1571                         if (strcmp(filtering_str, str) == 0)
1572                                 break;
1573                         row++;
1574                         g_free(filtering_str);
1575                 }       
1576
1577                 if (!filtering_str) {
1578                         val = alertpanel(_("Entry not saved"),
1579                                  _("The entry was not saved. Close anyway?"),
1580                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1581                         if (G_ALERTDEFAULT != val) {
1582                                 g_free(filtering_str);
1583                                 g_free(str); /* fixed two leaks: huzzah! */
1584                                 filteringprop_free(prop);
1585                                 return TRUE;
1586                         }
1587                 }               
1588
1589                 g_free(filtering_str);
1590                 g_free(str);
1591                 filteringprop_free(prop); /* fixed a leak: huzzah! */
1592         } else {
1593                 gchar *name, *condition, *action;
1594                 name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
1595                 condition = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1596                 action = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1597                 if (strlen(name) || 
1598                     strlen(condition) || 
1599                     strlen(action)) {
1600                         val = alertpanel(_("Entry not saved"),
1601                                  _("The entry was not saved. Close anyway?"),
1602                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1603                         if (G_ALERTDEFAULT != val) {
1604                                 g_free(name);
1605                                 g_free(condition);
1606                                 g_free(action);
1607                                 return TRUE;
1608                         }
1609                 }
1610                 g_free(name);
1611                 g_free(condition);
1612                 g_free(action);
1613         }
1614         return FALSE;
1615 }
1616
1617 static void prefs_filtering_ok(gpointer action, gpointer data)
1618 {
1619         if (prefs_filtering_check_mod(FALSE))
1620                 return;
1621         modified = FALSE;
1622         prefs_filtering_set_list();
1623         prefs_matcher_write_config();
1624         prefs_filtering_close();
1625 }
1626
1627 static void prefs_filtering_cancel(gpointer action, gpointer data)
1628 {
1629         if (prefs_filtering_check_mod(TRUE))
1630                 return;
1631         modified = FALSE;
1632         prefs_matcher_read_config();
1633         prefs_filtering_close();
1634 }
1635
1636 static GtkListStore* prefs_filtering_create_data_store(void)
1637 {
1638         return gtk_list_store_new(N_PREFS_FILTERING_COLUMNS,
1639                                   G_TYPE_BOOLEAN,
1640                                   G_TYPE_STRING,
1641                                   G_TYPE_INT,
1642                                   G_TYPE_STRING,
1643                                   G_TYPE_STRING,
1644                                   G_TYPE_BOOLEAN,
1645                                  -1);
1646 }
1647
1648 /*!
1649  *\brief        Insert filtering rule into store. Note that we access the
1650  *              tree view / store by index, which is a bit suboptimal, but
1651  *              at least it made GTK 2 porting easier.
1652  *
1653  *\param        list_store Store to operate on
1654  *\param        row -1 to add a new rule to store,
1655                         row >=0 to change an existing row
1656                         row <-1 insert a new row after (-row-2)
1657  *\param        enabled TRUE if rule is enabled
1658  *\param        name The Name of rule
1659  *\param        account_id The account ID
1660  *\param        account_name The account name or All or (New)
1661  *\param        rule String representation of rule
1662  *\param        prop TRUE if valid filtering rule; if FALSE it's the first
1663  *              entry in the store ("(New)").
1664  *
1665  *\return       int Row of inserted / changed rule.
1666  */
1667 static gint prefs_filtering_list_view_insert_rule(GtkListStore *list_store,
1668                                                   gint row,
1669                                                   gboolean enabled,
1670                                                   const gchar *name,
1671                                                   gint account_id,
1672                                                   const gchar *account_name,
1673                                                   const gchar *rule,
1674                                                   gboolean prop) 
1675 {
1676         GtkTreeIter iter;
1677         GtkTreeIter sibling;
1678
1679         /* check if valid row at all */
1680         if (row >= 0) {
1681                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1682                                                    &iter, NULL, row))
1683                         row = -1;
1684         } else if (row < -1) {
1685                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1686                                                    &sibling, NULL, -row-2))
1687                         row = -1;                                                  
1688         }
1689
1690         if (row == -1 ) {
1691                 /* append new */
1692                 gtk_list_store_append(list_store, &iter);
1693                 gtk_list_store_set(list_store, &iter, 
1694                                    PREFS_FILTERING_ENABLED, enabled,
1695                                    PREFS_FILTERING_NAME, name,
1696                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1697                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1698                                    PREFS_FILTERING_RULE, rule,
1699                                    PREFS_FILTERING_PROP, prop,
1700                                    -1);
1701                 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store),
1702                                                       NULL) - 1;
1703         } else if (row < -1) {
1704                 /* duplicate */
1705                 gtk_list_store_insert_after(list_store, &iter, &sibling);
1706                 gtk_list_store_set(list_store, &iter, 
1707                                    PREFS_FILTERING_ENABLED, enabled,
1708                                    PREFS_FILTERING_NAME, name,
1709                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1710                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1711                                    PREFS_FILTERING_RULE, rule,
1712                                    PREFS_FILTERING_PROP, prop,
1713                                    -1);
1714                 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store),
1715                                                       NULL) - 1;
1716         } else {
1717                 /* change existing */
1718                 gtk_list_store_set(list_store, &iter, 
1719                                    PREFS_FILTERING_ENABLED, enabled,
1720                                    PREFS_FILTERING_NAME, name,
1721                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1722                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1723                                    PREFS_FILTERING_RULE, rule,
1724                                    -1);
1725                 return row;                                
1726         }
1727 }
1728
1729 /*!
1730  *\return       gchar * Rule at specified row - should be freed.
1731  */
1732 static gchar *prefs_filtering_list_view_get_rule(GtkWidget *list, gint row)
1733 {       
1734         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1735         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1736         GtkTreeIter iter;
1737         gchar *result;
1738
1739         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1740                 return NULL;
1741         
1742         gtk_tree_model_get(model, &iter, 
1743                            PREFS_FILTERING_RULE, &result,
1744                            -1);
1745         
1746         return result;
1747 }
1748
1749 static void prefs_filtering_list_view_get_rule_info(GtkWidget *list, gint row,
1750                                 gboolean *enabled, gchar **name, gint *account_id)
1751 {       
1752         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1753         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1754         GtkTreeIter iter;
1755
1756         *enabled = TRUE;
1757         *name = NULL;
1758
1759         if (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1760                 gtk_tree_model_get(model, &iter, 
1761                                    PREFS_FILTERING_ENABLED, enabled,
1762                                    PREFS_FILTERING_NAME, name,
1763                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1764                                    -1);
1765         }
1766 }
1767
1768 static GtkActionGroup *prefs_filtering_popup_action = NULL;
1769 static GtkWidget *prefs_filtering_popup_menu = NULL;
1770
1771 static GtkActionEntry prefs_filtering_popup_entries[] =
1772 {
1773         {"PrefsFilteringPopup",                 NULL, "PrefsFilteringPopup" },
1774         {"PrefsFilteringPopup/Delete",          NULL, N_("_Delete"), NULL, NULL, G_CALLBACK(prefs_filtering_delete_cb) },
1775         {"PrefsFilteringPopup/DeleteAll",       NULL, N_("Delete _all"), NULL, NULL, G_CALLBACK(prefs_filtering_delete_all_cb) },
1776         {"PrefsFilteringPopup/Duplicate",       NULL, N_("D_uplicate"), NULL, NULL, G_CALLBACK(prefs_filtering_duplicate_cb) },
1777 #ifdef GENERIC_UMPC
1778         {"PrefsFilteringPopup/---",             NULL, "---", NULL, NULL, NULL },
1779         {"PrefsFilteringPopup/PageUp",          NULL, N_("Move one page up"), NULL, NULL, G_CALLBACK(prefs_filtering_page_up) },
1780         {"PrefsFilteringPopup/PageDown",        NULL, N_("Move one page down"), NULL, NULL, G_CALLBACK(prefs_filtering_page_down) },
1781 #endif
1782 };
1783
1784 static void prefs_filtering_row_selected(GtkTreeSelection *selection,
1785                                          GtkTreeView *list_view)
1786 {
1787         GtkTreePath *path;
1788         GtkTreeIter iter;
1789         GtkTreeModel *model;
1790         
1791         if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1792                 return;
1793         
1794         path = gtk_tree_model_get_path(model, &iter);
1795         prefs_filtering_select_row(list_view, path);
1796         gtk_tree_path_free(path);
1797 }
1798
1799 static gint prefs_filtering_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1800                                     GtkTreeView *list_view)
1801 {
1802         if (event) {
1803                 /* left- or right-button click */
1804                 if (event->button == 1 || event->button == 3) {
1805                         GtkTreePath *path = NULL;
1806
1807                         if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1808                                                                 &path, NULL, NULL, NULL)) {
1809                                 prefs_filtering_select_row(list_view, path);
1810                         }
1811                         if (path)
1812                                 gtk_tree_path_free(path);
1813                 }
1814
1815                 /* right-button click */
1816                 if (event->button == 3) {
1817                         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1818                         GtkTreeIter iter;
1819                         gboolean non_empty;
1820                         gint row;
1821
1822                         if (!prefs_filtering_popup_menu) {
1823                                 prefs_filtering_popup_action = cm_menu_create_action_group("PrefsFilteringPopup", prefs_filtering_popup_entries,
1824                                         G_N_ELEMENTS(prefs_filtering_popup_entries), (gpointer)list_view);
1825                                 MENUITEM_ADDUI("/Menus", "PrefsFilteringPopup", "PrefsFilteringPopup", GTK_UI_MANAGER_MENU)
1826                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Delete", "PrefsFilteringPopup/Delete", GTK_UI_MANAGER_MENUITEM)
1827                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "DeleteAll", "PrefsFilteringPopup/DeleteAll", GTK_UI_MANAGER_MENUITEM)
1828                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Duplicate", "PrefsFilteringPopup/Duplicate", GTK_UI_MANAGER_MENUITEM)
1829 #ifdef GENERIC_UMPC
1830                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Separator1", "PrefsFilteringPopup/---", GTK_UI_MANAGER_SEPARATOR)
1831                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "PageUp", "PrefsFilteringPopup/PageUp", GTK_UI_MANAGER_MENUITEM)
1832                                 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "PageDown", "PrefsFilteringPopup/PageDown", GTK_UI_MANAGER_MENUITEM)
1833 #endif
1834                                 prefs_filtering_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1835                                         gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/PrefsFilteringPopup")) );
1836                         }
1837
1838                         /* grey out some popup menu items if there is no selected row */
1839                         row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1840                         cm_menu_set_sensitive("PrefsFilteringPopup/Delete", (row > 0));
1841                         cm_menu_set_sensitive("PrefsFilteringPopup/Duplicate", (row > 0));
1842
1843                         /* grey out seom popup menu items if there is no row
1844                            (not counting the (New) one at row 0) */
1845                         non_empty = gtk_tree_model_get_iter_first(model, &iter);
1846                         if (non_empty)
1847                                 non_empty = gtk_tree_model_iter_next(model, &iter);
1848                         cm_menu_set_sensitive("PrefsFilteringPopup/DeleteAll", non_empty);
1849
1850                         gtk_menu_popup(GTK_MENU(prefs_filtering_popup_menu), 
1851                                    NULL, NULL, NULL, NULL, 
1852                                    event->button, event->time);
1853                 }
1854         }
1855         return FALSE;
1856 }
1857
1858 static gboolean prefs_filtering_list_popup_menu(GtkWidget *widget, gpointer data)
1859 {
1860         GtkTreeView *list_view = (GtkTreeView *)data;
1861         GdkEventButton event;
1862         
1863         event.button = 3;
1864         event.time = gtk_get_current_event_time();
1865         
1866         prefs_filtering_list_btn_pressed(NULL, &event, list_view);
1867
1868         return TRUE;
1869 }
1870
1871 /*!
1872  *\brief        Create list view for filtering
1873  */
1874 static GtkWidget *prefs_filtering_list_view_create(void)
1875 {
1876         GtkTreeView *list_view;
1877         GtkTreeSelection *selector;
1878
1879         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL
1880                 (prefs_filtering_create_data_store())));
1881 #ifdef GENERIC_UMPC
1882         g_object_set(list_view, "allow-checkbox-mode", FALSE, NULL);
1883 #endif
1884
1885 #ifndef MAEMO
1886         g_signal_connect(G_OBJECT(list_view), "popup-menu",
1887                          G_CALLBACK(prefs_filtering_list_popup_menu), list_view);
1888 #else
1889         gtk_widget_tap_and_hold_setup(GTK_WIDGET(list_view), NULL, NULL,
1890                         GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
1891         g_signal_connect(G_OBJECT(list_view), "tap-and-hold",
1892                          G_CALLBACK(prefs_filtering_list_popup_menu), list_view);
1893 #endif
1894         g_signal_connect(G_OBJECT(list_view), "button-press-event",
1895                         G_CALLBACK(prefs_filtering_list_btn_pressed), list_view);
1896         
1897         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1898         gtk_tree_view_set_reorderable(list_view, TRUE);
1899         
1900         selector = gtk_tree_view_get_selection(list_view);
1901         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1902         g_signal_connect(G_OBJECT(selector), "changed",
1903                          G_CALLBACK(prefs_filtering_row_selected), list_view);
1904
1905         /* create the columns */
1906         prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view));
1907
1908         return GTK_WIDGET(list_view);
1909 }
1910
1911 static void prefs_filtering_enable_toggled(GtkCellRendererToggle *widget,
1912                 gchar *path,
1913                 GtkWidget *list_view)
1914 {
1915         GtkTreeIter iter;
1916         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1917         gboolean enabled = TRUE;
1918
1919         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1920                 return;
1921
1922         gtk_tree_model_get(model, &iter,
1923                            PREFS_FILTERING_ENABLED, &enabled,
1924                            -1);
1925
1926         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1927                            PREFS_FILTERING_ENABLED, !enabled,
1928                            -1);
1929 }
1930
1931 static void prefs_filtering_create_list_view_columns(GtkWidget *list_view)
1932 {
1933         GtkTreeViewColumn *column;
1934         GtkCellRenderer *renderer;
1935
1936         renderer = gtk_cell_renderer_toggle_new();
1937         g_object_set(renderer,
1938                      "radio", FALSE,
1939                      "activatable", TRUE,
1940                      NULL);
1941         column = gtk_tree_view_column_new_with_attributes
1942                 (_("Enable"), /* FIXME : Enable, Enabled, or 'E' ? */
1943                  renderer,
1944                  "active", PREFS_FILTERING_ENABLED,
1945                  NULL);
1946         gtk_tree_view_column_set_alignment (column, 0.5);
1947         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1948         g_signal_connect(G_OBJECT(renderer), "toggled",
1949                          G_CALLBACK(prefs_filtering_enable_toggled),
1950                          list_view);
1951
1952         renderer = gtk_cell_renderer_text_new();
1953         column = gtk_tree_view_column_new_with_attributes
1954                 (_("Name"),
1955                  renderer,
1956                  "text", PREFS_FILTERING_NAME,
1957                  NULL);
1958         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1959         gtk_tree_view_column_set_resizable(column, TRUE);
1960
1961         renderer = gtk_cell_renderer_text_new();
1962         column = gtk_tree_view_column_new_with_attributes
1963                 (_("Account"),
1964                  renderer,
1965                  "text", PREFS_FILTERING_ACCOUNT_NAME,
1966                  NULL);
1967         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1968         gtk_tree_view_column_set_resizable(column, TRUE);
1969
1970         filtering.account_name_column = column;
1971                 
1972         renderer = gtk_cell_renderer_text_new();
1973         column = gtk_tree_view_column_new_with_attributes
1974                 (_("Rule"),
1975                  renderer,
1976                  "text", PREFS_FILTERING_RULE,
1977                  NULL);
1978
1979         gtk_tree_view_set_search_column(GTK_TREE_VIEW(list_view), PREFS_FILTERING_NAME);
1980         gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(list_view), prefs_filtering_search_func_cb , NULL, NULL);
1981         
1982         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1983 }
1984
1985 /*!
1986  *\brief        Triggered when a row has to be selected
1987  */
1988 static void prefs_filtering_select_row(GtkTreeView *list_view, GtkTreePath *path)
1989 {
1990         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1991
1992         if (path && model) {
1993                 GtkTreeSelection *selection;
1994                 gboolean has_prop = FALSE;
1995                 GtkTreeIter iter;
1996
1997                 /* select row */
1998                 selection = gtk_tree_view_get_selection(list_view);
1999                 gtk_tree_selection_select_path(selection, path);
2000
2001                 /* update dialog from selection */
2002                 gtk_tree_model_get_iter(model, &iter, path);
2003                 gtk_tree_model_get(model, &iter,
2004                                    PREFS_FILTERING_PROP, &has_prop,
2005                                    -1);
2006
2007                 if (has_prop) {
2008                         FilteringProp *prop;
2009                         gchar *filtering_str = NULL;
2010                         gchar *name = NULL;
2011                         gint account_id = 0;
2012
2013                         gtk_tree_model_get(model, &iter,
2014                                            PREFS_FILTERING_RULE, &filtering_str,
2015                                            -1);
2016                         gtk_tree_model_get(model, &iter,
2017                                            PREFS_FILTERING_NAME, &name,
2018                                            -1);
2019                         gtk_tree_model_get(model, &iter,
2020                                            PREFS_FILTERING_ACCOUNT_ID, &account_id,
2021                                            -1);
2022
2023                         prop = matcher_parser_get_filtering(filtering_str);
2024                         if (prop) {
2025                                 prop->name = g_strdup(name);
2026                                 prop->account_id = account_id;
2027                                 prefs_filtering_select_set(prop);
2028                                 filteringprop_free(prop);
2029                         }                               
2030                         g_free(name);
2031                         g_free(filtering_str);
2032                 } else
2033                         prefs_filtering_reset_dialog();
2034         }
2035 }