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