2007-10-06 [paul] 3.0.2cvs18
[claws.git] / src / prefs_filtering.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 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 <gtk/gtkoptionmenu.h>
30 #include <gdk/gdkkeysyms.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35
36 #include "main.h"
37 #include "prefs_gtk.h"
38 #include "prefs_matcher.h"
39 #include "prefs_filtering.h"
40 #include "prefs_common.h"
41 #include "mainwindow.h"
42 #include "foldersel.h"
43 #include "manage_window.h"
44 #include "inc.h"
45 #include "utils.h"
46 #include "gtkutils.h"
47 #include "alertpanel.h"
48 #include "folder.h"
49 #include "filtering.h"
50 #include "addr_compl.h"
51 #include "manual.h"
52 #include "combobox.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 void prefs_filtering_register_cb (void);
100 static void prefs_filtering_substitute_cb       (void);
101 static void prefs_filtering_delete_cb   (void);
102 static void prefs_filtering_top         (void);
103 static void prefs_filtering_up          (void);
104 static void prefs_filtering_down        (void);
105 static void prefs_filtering_bottom      (void);
106 static gint prefs_filtering_deleted     (GtkWidget      *widget,
107                                          GdkEventAny    *event,
108                                          gpointer        data);
109 static gboolean prefs_filtering_key_pressed(GtkWidget   *widget,
110                                          GdkEventKey    *event,
111                                          gpointer        data);
112 static void prefs_filtering_cancel      (void);
113 static void prefs_filtering_ok          (void);
114
115 static void prefs_filtering_condition_define    (void);
116 static void prefs_filtering_action_define(void);
117 static gint prefs_filtering_list_view_set_row   (gint row, FilteringProp * prop);
118                                           
119 static void prefs_filtering_reset_dialog        (void);
120 static gboolean prefs_filtering_rename_tag_func(GNode *node, gpointer data);
121 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data);
122 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data);
123
124 static void delete_path(GSList ** p_filters, const gchar * path);
125
126
127 static GtkListStore* prefs_filtering_create_data_store  (void);
128 static gint prefs_filtering_list_view_insert_rule       (GtkListStore *list_store,
129                                                          gint row,
130                                                          gboolean enabled,
131                                                          const gchar *name,
132                                                          gint account_id,
133                                                          const gchar *account_name,
134                                                          const gchar *rule, 
135                                                          gboolean prop);
136 static gchar *prefs_filtering_list_view_get_rule        (GtkWidget *list, 
137                                                          gint row);
138 static void prefs_filtering_list_view_get_rule_info     (GtkWidget *list, 
139                                                          gint row,
140                                                          gboolean *enabled,
141                                                          gchar **name,
142                                                          gint *account_id);
143
144 static GtkWidget *prefs_filtering_list_view_create      (void);
145 static void prefs_filtering_create_list_view_columns    (GtkWidget *list_view);
146
147 static gboolean prefs_filtering_selected                (GtkTreeSelection *selector,
148                                                          GtkTreeModel *model, 
149                                                          GtkTreePath *path,
150                                                          gboolean currently_selected,
151                                                          gpointer data);
152
153 static void prefs_filtering_account_option_menu_populate(void);
154
155 static gulong signal_id = 0; /* filtering.help_btn clicked signal */
156
157 static int modified = FALSE;
158
159 void prefs_filtering_open(GSList ** p_processing,
160                           const gchar *title,
161                           const gchar *help_url_anchor,
162                           const gchar *header,
163                           const gchar *key,
164                           gboolean per_account_filtering)
165 {
166         if (prefs_rc_is_readonly(FILTERING_RC))
167                 return;
168
169         inc_lock();
170
171         if (!filtering.window) {
172                 prefs_filtering_create();
173         } else {
174                 gtk_list_store_clear(filtering.account_combobox_list);
175                 prefs_filtering_account_option_menu_populate();
176         }
177
178         gtk_tree_view_column_set_visible(filtering.account_name_column,
179                                                                          per_account_filtering);
180
181         manage_window_set_transient(GTK_WINDOW(filtering.window));
182         gtk_widget_grab_focus(filtering.ok_btn);
183         
184         if (title != NULL)
185                 gtk_window_set_title(GTK_WINDOW(filtering.window), title);
186         else
187                 gtk_window_set_title (GTK_WINDOW(filtering.window),
188                                       _("Filtering/Processing configuration"));
189
190         if (help_url_anchor != NULL) {
191                 if (signal_id != 0) {
192                         g_signal_handler_disconnect(
193                                         G_OBJECT(filtering.help_btn),
194                                         signal_id);
195                 }
196
197                 signal_id = g_signal_connect(G_OBJECT(filtering.help_btn),
198                                 "clicked",
199                                 G_CALLBACK(manual_open_with_anchor_cb),
200                                 (gchar*)help_url_anchor);
201         }
202         else {
203                 gtk_widget_set_sensitive(filtering.help_btn, FALSE);
204         }
205
206         p_processing_list = p_processing;
207         
208         prefs_filtering_set_dialog(header, key);
209         if (per_account_filtering) {
210                 gtk_widget_show(filtering.account_label);
211                 gtk_widget_show(filtering.account_combobox);
212         } else {
213                 gtk_widget_hide(filtering.account_label);
214                 gtk_widget_hide(filtering.account_combobox);
215                 combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
216         }
217
218         gtk_widget_show(filtering.window);
219
220         start_address_completion(NULL);
221 }
222
223 static void prefs_filtering_size_allocate_cb(GtkWidget *widget,
224                                          GtkAllocation *allocation)
225 {
226         g_return_if_fail(allocation != NULL);
227
228         prefs_common.filteringwin_width = allocation->width;
229         prefs_common.filteringwin_height = allocation->height;
230 }
231
232 /* prefs_filtering_close() - just to have one common exit point */
233 static void prefs_filtering_close(void)
234 {
235         end_address_completion();
236         
237         gtk_widget_hide(filtering.window);
238         inc_unlock();
239 }
240
241 static void prefs_filtering_account_option_menu_populate(void)
242 {
243         GList *accounts = NULL;
244         GtkTreeIter iter;
245
246         accounts = account_get_list();
247
248         g_return_if_fail(accounts != NULL);
249
250         COMBOBOX_ADD(filtering.account_combobox_list, Q_("Filtering Account Menu|All"), 0);
251         for (; accounts != NULL; accounts = accounts->next) {
252                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
253
254                 COMBOBOX_ADD_ESCAPED(filtering.account_combobox_list, ac->account_name, ac->account_id);
255         }
256 }
257
258 static GtkWidget *prefs_filtering_account_option_menu(Filtering *filtering)
259 {
260         GtkWidget *optmenu = NULL;
261         GtkWidget *optmenubox = NULL;
262         GtkListStore *menu = NULL;
263         
264         optmenubox = gtk_event_box_new();
265         optmenu = gtkut_sc_combobox_create(optmenubox, FALSE);
266         menu = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(optmenu)));
267
268         filtering->account_combobox = optmenu;
269         filtering->account_combobox_list = menu;
270
271         prefs_filtering_account_option_menu_populate();
272
273         return optmenubox;
274 }
275
276 static void prefs_filtering_create(void)
277 {
278         GtkWidget *window;
279         GtkWidget *vbox;
280         GtkWidget *help_btn;
281         GtkWidget *cancel_btn;
282         GtkWidget *ok_btn;
283         GtkWidget *confirm_area;
284
285         GtkWidget *vbox1;
286         GtkWidget *reg_hbox;
287         GtkWidget *arrow;
288         GtkWidget *btn_hbox;
289
290         GtkWidget *name_label;
291         GtkWidget *name_entry;
292         GtkWidget *account_label;
293         GtkWidget *account_opt_menu;
294         GtkWidget *cond_label;
295         GtkWidget *cond_entry;
296         GtkWidget *cond_btn;
297         GtkWidget *action_label;
298         GtkWidget *action_entry;
299         GtkWidget *action_btn;
300
301         GtkWidget *reg_btn;
302         GtkWidget *subst_btn;
303         GtkWidget *del_btn;
304
305         GtkWidget *cond_hbox;
306         GtkWidget *cond_scrolledwin;
307         GtkWidget *cond_list_view;
308
309         GtkWidget *btn_vbox;
310         GtkWidget *spc_vbox;
311         GtkWidget *top_btn;
312         GtkWidget *up_btn;
313         GtkWidget *down_btn;
314         GtkWidget *bottom_btn;
315         GtkWidget *table;
316         static GdkGeometry geometry;
317
318         debug_print("Creating filtering configuration window...\n");
319
320         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_filtering");
321         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
322         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
323         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
324         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
325
326         vbox = gtk_vbox_new (FALSE, 6);
327         gtk_widget_show (vbox);
328         gtk_container_add (GTK_CONTAINER (window), vbox);
329
330         gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
331                         &cancel_btn, GTK_STOCK_CANCEL,
332                         &ok_btn, GTK_STOCK_OK,
333                         NULL, NULL);
334         gtk_widget_show (confirm_area);
335         gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
336         gtk_widget_grab_default (ok_btn);
337
338         gtk_window_set_title (GTK_WINDOW(window),
339                                     _("Filtering/Processing configuration"));
340
341         g_signal_connect(G_OBJECT(window), "delete_event",
342                          G_CALLBACK(prefs_filtering_deleted), NULL);
343         g_signal_connect(G_OBJECT(window), "size_allocate",
344                          G_CALLBACK(prefs_filtering_size_allocate_cb), NULL);
345         g_signal_connect(G_OBJECT(window), "key_press_event",
346                          G_CALLBACK(prefs_filtering_key_pressed), NULL);
347         MANAGE_WINDOW_SIGNALS_CONNECT (window);
348         g_signal_connect(G_OBJECT(ok_btn), "clicked",
349                          G_CALLBACK(prefs_filtering_ok), NULL);
350         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
351                          G_CALLBACK(prefs_filtering_cancel), NULL);
352
353         vbox1 = gtk_vbox_new (FALSE, VSPACING);
354         gtk_widget_show (vbox1);
355         gtk_box_pack_start (GTK_BOX (vbox), vbox1, FALSE, TRUE, 0);
356         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
357
358         table = gtk_table_new(4, 3, FALSE);
359         gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
360         gtk_table_set_col_spacings (GTK_TABLE (table), 4);
361         gtk_widget_show(table);
362         gtk_box_pack_start (GTK_BOX (vbox1), table, TRUE, TRUE, 0);
363
364         name_label = gtk_label_new (_("Name"));
365         gtk_widget_show (name_label);
366         gtk_misc_set_alignment (GTK_MISC (name_label), 1, 0.5);
367         gtk_table_attach (GTK_TABLE (table), name_label, 0, 1, 0, 1,
368                           (GtkAttachOptions) (GTK_FILL),
369                           (GtkAttachOptions) (0), 0, 0);
370
371         name_entry = gtk_entry_new ();
372         gtk_widget_show (name_entry);
373         gtk_table_attach (GTK_TABLE (table), name_entry, 1, 2, 0, 1,
374                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
375                           (GtkAttachOptions) (0), 0, 0);
376
377         account_label = gtk_label_new (_("Account"));
378         gtk_widget_show (account_label);
379         gtk_misc_set_alignment (GTK_MISC (account_label), 1, 0.5);
380         gtk_table_attach (GTK_TABLE (table), account_label, 0, 1, 1, 2,
381                           (GtkAttachOptions) (GTK_FILL),
382                           (GtkAttachOptions) (0), 0, 0);
383
384         account_opt_menu = prefs_filtering_account_option_menu(&filtering);
385         gtk_widget_show (account_opt_menu);
386         gtk_table_attach (GTK_TABLE (table), account_opt_menu, 1, 2, 1, 2,
387                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
388                           (GtkAttachOptions) (0), 0, 0);
389         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
390
391         cond_label = gtk_label_new (_("Condition"));
392         gtk_widget_show (cond_label);
393         gtk_misc_set_alignment (GTK_MISC (cond_label), 1, 0.5);
394         gtk_table_attach (GTK_TABLE (table), cond_label, 0, 1, 2, 3,
395                           (GtkAttachOptions) (GTK_FILL),
396                           (GtkAttachOptions) (0), 0, 0);
397
398         cond_entry = gtk_entry_new ();
399         gtk_widget_show (cond_entry);
400         gtk_table_attach (GTK_TABLE (table), cond_entry, 1, 2, 2, 3,
401                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
402                           (GtkAttachOptions) (0), 0, 0);
403
404         cond_btn = gtk_button_new_with_label (_(" Define... "));
405         gtk_widget_show (cond_btn);
406         gtk_table_attach (GTK_TABLE (table), cond_btn, 2, 3, 2, 3,
407                           (GtkAttachOptions) (GTK_FILL),
408                           (GtkAttachOptions) (0), 2, 2);
409         g_signal_connect(G_OBJECT (cond_btn), "clicked",
410                          G_CALLBACK(prefs_filtering_condition_define),
411                          NULL);
412
413         action_label = gtk_label_new (_("Action"));
414         gtk_widget_show (action_label);
415         gtk_misc_set_alignment (GTK_MISC (action_label), 1, 0.5);
416         gtk_table_attach (GTK_TABLE (table), action_label, 0, 1, 3, 4,
417                           (GtkAttachOptions) (GTK_FILL),
418                           (GtkAttachOptions) (0), 0, 0);
419
420         action_entry = gtk_entry_new ();
421         gtk_widget_show (action_entry);
422         gtk_table_attach (GTK_TABLE (table), action_entry, 1, 2, 3, 4,
423                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
424                           (GtkAttachOptions) (0), 0, 0);
425
426         action_btn = gtk_button_new_with_label (_(" Define... "));
427         gtk_widget_show (action_btn);
428         gtk_table_attach (GTK_TABLE (table), action_btn, 2, 3, 3, 4,
429                           (GtkAttachOptions) (GTK_FILL),
430                           (GtkAttachOptions) (0), 2, 2);
431         g_signal_connect(G_OBJECT (action_btn), "clicked",
432                          G_CALLBACK(prefs_filtering_action_define),
433                          NULL);
434                          
435         /* register / substitute / delete */
436         reg_hbox = gtk_hbox_new (FALSE, 4);
437         gtk_widget_show (reg_hbox);
438         gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
439
440         arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
441         gtk_widget_show (arrow);
442         gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
443         gtk_widget_set_size_request (arrow, -1, 16);
444
445         btn_hbox = gtk_hbox_new (TRUE, 4);
446         gtk_widget_show (btn_hbox);
447         gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
448
449         reg_btn = gtk_button_new_from_stock (GTK_STOCK_ADD);
450         gtk_widget_show (reg_btn);
451         gtk_box_pack_start (GTK_BOX (btn_hbox), reg_btn, FALSE, TRUE, 0);
452         g_signal_connect(G_OBJECT (reg_btn), "clicked",
453                          G_CALLBACK(prefs_filtering_register_cb), NULL);
454
455         subst_btn = gtkut_get_replace_btn (_("Replace"));
456         gtk_widget_show (subst_btn);
457         gtk_box_pack_start (GTK_BOX (btn_hbox), subst_btn, FALSE, TRUE, 0);
458         g_signal_connect(G_OBJECT (subst_btn), "clicked",
459                          G_CALLBACK(prefs_filtering_substitute_cb),
460                          NULL);
461
462         del_btn = gtk_button_new_from_stock (GTK_STOCK_DELETE);
463         gtk_widget_show (del_btn);
464         gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
465         g_signal_connect(G_OBJECT (del_btn), "clicked",
466                         G_CALLBACK(prefs_filtering_delete_cb), NULL);
467
468         cond_hbox = gtk_hbox_new (FALSE, 8);
469         gtk_widget_show (cond_hbox);
470         gtk_box_pack_start (GTK_BOX (vbox), cond_hbox, TRUE, TRUE, 0);
471
472         cond_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
473         gtk_widget_show (cond_scrolledwin);
474         gtk_widget_set_size_request (cond_scrolledwin, -1, 150);
475         gtk_box_pack_start (GTK_BOX (cond_hbox), cond_scrolledwin,
476                             TRUE, TRUE, 0);
477         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin),
478                                         GTK_POLICY_AUTOMATIC,
479                                         GTK_POLICY_AUTOMATIC);
480
481         cond_list_view = prefs_filtering_list_view_create();    
482         gtk_widget_show (cond_list_view);
483         gtk_container_add (GTK_CONTAINER (cond_scrolledwin), cond_list_view);
484
485         btn_vbox = gtk_vbox_new (FALSE, 8);
486         gtk_widget_show (btn_vbox);
487         gtk_box_pack_start (GTK_BOX (cond_hbox), btn_vbox, FALSE, FALSE, 0);
488
489         top_btn = gtk_button_new_from_stock (GTK_STOCK_GOTO_TOP);
490         gtk_widget_show (top_btn);
491         gtk_box_pack_start (GTK_BOX (btn_vbox), top_btn, FALSE, FALSE, 0);
492         g_signal_connect(G_OBJECT (top_btn), "clicked",
493                          G_CALLBACK(prefs_filtering_top), NULL);
494
495         PACK_VSPACER (btn_vbox, spc_vbox, VSPACING_NARROW_2);
496
497         up_btn = gtk_button_new_from_stock (GTK_STOCK_GO_UP);
498         gtk_widget_show (up_btn);
499         gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
500         g_signal_connect(G_OBJECT (up_btn), "clicked",
501                          G_CALLBACK(prefs_filtering_up), NULL);
502
503         down_btn = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN);
504         gtk_widget_show (down_btn);
505         gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
506         g_signal_connect(G_OBJECT (down_btn), "clicked",
507                          G_CALLBACK(prefs_filtering_down), NULL);
508
509         PACK_VSPACER (btn_vbox, spc_vbox, VSPACING_NARROW_2);
510
511         bottom_btn = gtk_button_new_from_stock (GTK_STOCK_GOTO_BOTTOM);
512         gtk_widget_show (bottom_btn);
513         gtk_box_pack_start (GTK_BOX (btn_vbox), bottom_btn, FALSE, FALSE, 0);
514         g_signal_connect(G_OBJECT (bottom_btn), "clicked",
515                          G_CALLBACK(prefs_filtering_bottom), NULL);
516
517         if (!geometry.min_height) {
518                 geometry.min_width = 500;
519                 geometry.min_height = 400;
520         }
521
522         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
523                                       GDK_HINT_MIN_SIZE);
524         gtk_widget_set_size_request(window, prefs_common.filteringwin_width,
525                                     prefs_common.filteringwin_height);
526
527         gtk_widget_show_all(window);
528
529         filtering.window    = window;
530         filtering.help_btn = help_btn;
531         filtering.ok_btn = ok_btn;
532
533         filtering.name_entry     = name_entry;
534         filtering.cond_entry     = cond_entry;
535         filtering.action_entry   = action_entry;
536         filtering.cond_list_view = cond_list_view;
537         filtering.account_label  = account_label;
538 }
539
540 static void rename_tag(GSList * filters,
541                         const gchar * old_tag, const gchar * new_tag);
542
543 void prefs_filtering_rename_tag(const gchar *old_tag, const gchar *new_tag)
544 {
545         GList * cur;
546         const gchar *tags[2] = {NULL, NULL};
547         tags[0] = old_tag;
548         tags[1] = new_tag;
549         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
550                 Folder *folder;
551                 folder = (Folder *) cur->data;
552                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
553                                 prefs_filtering_rename_tag_func, tags);
554         }
555         
556         rename_tag(pre_global_processing, old_tag, new_tag);
557         rename_tag(post_global_processing, old_tag, new_tag);
558         rename_tag(filtering_rules, old_tag, new_tag);
559         
560         prefs_matcher_write_config();
561 }
562
563
564 static void rename_path(GSList * filters,
565                         const gchar * old_path, const gchar * new_path);
566
567 void prefs_filtering_rename_path(const gchar *old_path, const gchar *new_path)
568 {
569         GList * cur;
570         const gchar *paths[2] = {NULL, NULL};
571         paths[0] = old_path;
572         paths[1] = new_path;
573         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
574                 Folder *folder;
575                 folder = (Folder *) cur->data;
576                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
577                                 prefs_filtering_rename_path_func, paths);
578         }
579         
580         rename_path(pre_global_processing, old_path, new_path);
581         rename_path(post_global_processing, old_path, new_path);
582         rename_path(filtering_rules, old_path, new_path);
583         
584         prefs_matcher_write_config();
585 }
586
587 static void rename_path(GSList * filters,
588                         const gchar * old_path, const gchar * new_path)
589 {
590         gchar *base;
591         gchar *prefix;
592         gchar *suffix;
593         gchar *dest_path;
594         gchar *old_path_with_sep;
595         gint destlen;
596         gint prefixlen;
597         gint oldpathlen;
598         GSList * action_cur;
599         GSList * cur;
600
601         oldpathlen = strlen(old_path);
602         old_path_with_sep = g_strconcat(old_path,G_DIR_SEPARATOR_S,NULL);
603
604         for (cur = filters; cur != NULL; cur = cur->next) {
605                 FilteringProp   *filtering = (FilteringProp *)cur->data;
606                 
607                 for(action_cur = filtering->action_list ; action_cur != NULL ;
608                     action_cur = action_cur->next) {
609
610                         FilteringAction *action = action_cur->data;
611                         
612                         if (action->type == MATCHACTION_SET_TAG ||
613                             action->type == MATCHACTION_UNSET_TAG)
614                                 continue;
615                         if (!action->destination) 
616                                 continue;
617                         
618                         destlen = strlen(action->destination);
619                         
620                         if (destlen > oldpathlen) {
621                                 prefixlen = destlen - oldpathlen;
622                                 suffix = action->destination + prefixlen;
623                                 
624                                 if (!strncmp(old_path, suffix, oldpathlen)) {
625                                         prefix = g_malloc0(prefixlen + 1);
626                                         strncpy2(prefix, action->destination, prefixlen);
627                                         
628                                         base = suffix + oldpathlen;
629                                         while (*base == G_DIR_SEPARATOR) base++;
630                                         if (*base == '\0')
631                                                 dest_path = g_strconcat(prefix,
632                                                     G_DIR_SEPARATOR_S,
633                                                     new_path, NULL);
634                                         else
635                                                 dest_path = g_strconcat(prefix,
636                                                     G_DIR_SEPARATOR_S,
637                                                     new_path,
638                                                     G_DIR_SEPARATOR_S,
639                                                     base, NULL);
640                                         
641                                         g_free(prefix);
642                                         g_free(action->destination);
643                                         action->destination = dest_path;
644                                 } else { /* for non-leaf folders */
645                                         /* compare with trailing slash */
646                                         if (!strncmp(old_path_with_sep, action->destination, oldpathlen+1)) {
647                                                 
648                                                 suffix = action->destination + oldpathlen + 1;
649                                                 dest_path = g_strconcat(new_path,
650                                                     G_DIR_SEPARATOR_S,
651                                                     suffix, NULL);
652                                                 g_free(action->destination);
653                                                 action->destination = dest_path;
654                                         }
655                                 }
656                         } else {
657                                 /* folder-moving a leaf */
658                                 if (!strcmp(old_path, action->destination)) {
659                                         dest_path = g_strdup(new_path);
660                                         g_free(action->destination);
661                                         action->destination = dest_path;
662                                 }
663                         }
664                 }
665         }
666 }
667
668 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
669 {
670         GSList *filters;
671         const gchar * old_path;
672         const gchar * new_path;
673         const gchar ** paths;
674         FolderItem *item;
675         
676         paths = data;
677         old_path = paths[0];
678         new_path = paths[1];
679
680         g_return_val_if_fail(old_path != NULL, FALSE);
681         g_return_val_if_fail(new_path != NULL, FALSE);
682         g_return_val_if_fail(node != NULL, FALSE);
683
684         item = node->data;
685         if (!item || !item->prefs)
686                 return FALSE;
687         filters = item->prefs->processing;
688
689         rename_path(filters, old_path, new_path);
690
691         return FALSE;
692 }
693
694 static void rename_tag(GSList * filters,
695                         const gchar * old_tag, const gchar * new_tag)
696 {
697         GSList * action_cur;
698         GSList * cur;
699
700         for (cur = filters; cur != NULL; cur = cur->next) {
701                 FilteringProp   *filtering = (FilteringProp *)cur->data;
702                 
703                 for(action_cur = filtering->action_list ; action_cur != NULL ;
704                     action_cur = action_cur->next) {
705
706                         FilteringAction *action = action_cur->data;
707                         
708                         if (action->type != MATCHACTION_SET_TAG &&
709                             action->type != MATCHACTION_UNSET_TAG)
710                                 continue;
711                         if (!action->destination)
712                                 continue;
713                         if (!strcmp(action->destination, old_tag)) {
714                                 g_free(action->destination);
715                                 action->destination = g_strdup(new_tag);
716                         }
717                 }
718         }
719 }
720
721 static gboolean prefs_filtering_rename_tag_func(GNode *node, gpointer data)
722 {
723         GSList *filters;
724         const gchar * old_tag;
725         const gchar * new_tag;
726         const gchar ** tags;
727         FolderItem *item;
728         
729         tags = data;
730         old_tag = tags[0];
731         new_tag = tags[1];
732
733         g_return_val_if_fail(old_tag != NULL, FALSE);
734         g_return_val_if_fail(new_tag != NULL, FALSE);
735         g_return_val_if_fail(node != NULL, FALSE);
736
737         item = node->data;
738         if (!item || !item->prefs)
739                 return FALSE;
740         filters = item->prefs->processing;
741
742         rename_tag(filters, old_tag, new_tag);
743
744         return FALSE;
745 }
746
747 void prefs_filtering_delete_path(const gchar *path)
748 {
749         GList * cur;
750         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
751                 Folder *folder;
752                 folder = (Folder *) cur->data;
753                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
754                                 prefs_filtering_delete_path_func, (gchar *)path);
755         }
756         delete_path(&pre_global_processing, path);
757         delete_path(&post_global_processing, path);
758         delete_path(&filtering_rules, path);
759         
760         prefs_matcher_write_config();
761 }
762
763 static void delete_path(GSList ** p_filters, const gchar * path)
764 {
765         GSList * filters;
766         GSList * duplist;
767         gchar *suffix;
768         gint destlen;
769         gint prefixlen;
770         gint pathlen;
771         GSList * action_cur;
772         GSList * cur;
773         
774         filters = *p_filters;
775         pathlen = strlen(path);
776         duplist = g_slist_copy(filters);
777         for (cur = duplist ; cur != NULL; cur = g_slist_next(cur)) {
778                 FilteringProp *filtering = (FilteringProp *) cur->data;
779                 
780                 for(action_cur = filtering->action_list ; action_cur != NULL ;
781                     action_cur = action_cur->next) {
782                 
783                         FilteringAction *action;
784                         
785                         action = action_cur->data;
786                         
787                         if (action->type == MATCHACTION_SET_TAG ||
788                             action->type == MATCHACTION_UNSET_TAG)
789                                 continue;
790                         if (!action->destination) 
791                                 continue;
792                         
793                         destlen = strlen(action->destination);
794                         
795                         if (destlen > pathlen) {
796                                 prefixlen = destlen - pathlen;
797                                 suffix = action->destination + prefixlen;
798                                 
799                                 if (suffix && !strncmp(path, suffix, pathlen)) {
800                                         filteringprop_free(filtering);
801                                         filters = g_slist_remove(filters, filtering);
802                                 }
803                         } else if (strcmp(action->destination, path) == 0) {
804                                 filteringprop_free(filtering);
805                                 filters = g_slist_remove(filters, filtering);
806                         }
807                 }
808         }                
809         g_slist_free(duplist);
810         
811         * p_filters = filters;
812 }
813
814 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data)
815 {
816         const gchar *path = data;
817         FolderItem *item;
818         GSList ** p_filters;
819         
820         g_return_val_if_fail(path != NULL, FALSE);
821         g_return_val_if_fail(node != NULL, FALSE);
822
823         item = node->data;
824         if (!item || !item->prefs)
825                 return FALSE;
826         p_filters = &item->prefs->processing;
827         
828         delete_path(p_filters, path);
829
830         return FALSE;
831 }
832
833 static void prefs_filtering_set_dialog(const gchar *header, const gchar *key)
834 {
835         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
836         GSList *cur;
837         GSList * prefs_filtering;
838         gchar *cond_str;
839         GtkListStore *list_store;
840         
841         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
842         gtk_list_store_clear(list_store);
843
844         /* add the place holder (New) at row 0 */
845         prefs_filtering_list_view_insert_rule(list_store, -1, 
846                                               FALSE,
847                                               _("(New)"),
848                                                   0,
849                                               _("(New)"),
850                                               _("(New)"),
851                                               FALSE);
852
853         prefs_filtering = *p_processing_list;
854
855         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur)) {
856                 FilteringProp * prop = (FilteringProp *) cur->data;
857                 gchar *account_name = NULL;
858
859                 if (prop->account_id > 0) {
860                         PrefsAccount *ac_prefs = account_find_from_id(prop->account_id);
861
862                         if (ac_prefs)
863                                 account_name = ac_prefs->account_name;
864                 }
865                 if (account_name == NULL)
866                         account_name = (gchar *)Q_("Filtering Account Menu|All");
867
868                 cond_str = filteringprop_to_string(prop);
869                 subst_char(cond_str, '\t', ':');
870
871                 prefs_filtering_list_view_insert_rule(list_store, -1, 
872                                                       prop->enabled,
873                                                       prop->name,
874                                                           prop->account_id,
875                                                           account_name,
876                                                       cond_str, TRUE);
877                 
878                 g_free(cond_str);
879         }
880
881         prefs_filtering_reset_dialog();
882
883         if (header && key) {
884                 gchar * quoted_key;
885                 gchar *match_str;
886
887                 quoted_key = matcher_quote_str(key);
888                 
889                 match_str = g_strconcat(header, " ", get_matchparser_tab_str(MATCHTYPE_MATCHCASE),
890                                         " \"", quoted_key, "\"", NULL);
891                 g_free(quoted_key);
892                 
893                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), match_str);
894                 g_free(match_str);
895         }
896 }
897
898 static void prefs_filtering_reset_dialog(void)
899 {
900         gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), "");
901         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
902         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), "");
903         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), "");
904 }
905
906 static void prefs_filtering_set_list(void)
907 {
908         gint row = 1;
909         FilteringProp *prop;
910         GSList * cur;
911         gchar * filtering_str;
912         GSList * prefs_filtering;
913
914         prefs_filtering = *p_processing_list;
915         for (cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
916                 filteringprop_free((FilteringProp *) cur->data);
917         g_slist_free(prefs_filtering);
918         prefs_filtering = NULL;
919         
920
921         while (NULL != (filtering_str = prefs_filtering_list_view_get_rule
922                                                 (filtering.cond_list_view, row))) {
923                 /* FIXME: this strcmp() is bogus: "(New)" should never
924                  * be inserted in the storage */
925                 if (strcmp(filtering_str, _("(New)")) != 0) {
926                         gboolean enabled;
927                         gchar *name;
928                         gint account_id = 0;
929
930                         prefs_filtering_list_view_get_rule_info(
931                                         filtering.cond_list_view, row,
932                                         &enabled, &name, &account_id);
933                         prop = matcher_parser_get_filtering(filtering_str);
934                         g_free(filtering_str);
935                         if (prop) {
936                                 prop->enabled = enabled;
937                                 prop->name = name;
938                                 prop->account_id = account_id;
939                                 prefs_filtering = 
940                                         g_slist_append(prefs_filtering, prop);
941                         }
942                 }
943                 
944                 row++;
945         }                               
946         
947         *p_processing_list = prefs_filtering;
948 }
949
950 static gint prefs_filtering_list_view_set_row(gint row, FilteringProp * prop)
951 {
952         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
953         gchar *str;
954         GtkListStore *list_store;
955         gchar *name = NULL;
956         gint account_id = 0;
957         gchar *account_name = (gchar *)Q_("Filtering Account Menu|All");
958         gboolean enabled = TRUE;
959
960         str = filteringprop_to_string(prop);
961         if (str == NULL)
962                 return -1;
963
964         if (prop) {
965                 if (prop->name)
966                         name = prop->name;
967                 account_id = prop->account_id;
968                 if (account_id > 0)
969                         account_name = account_find_from_id(account_id)->account_name;
970                 enabled = prop->enabled;
971         }
972
973         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
974
975         row = prefs_filtering_list_view_insert_rule(list_store, row,
976                                                     enabled,
977                                                     name,
978                                                     account_id,
979                                                         account_name,
980                                                     str,
981                                                     prop != NULL);
982
983         g_free(str);
984
985         return row;
986 }
987
988 static void prefs_filtering_condition_define_done(MatcherList * matchers)
989 {
990         gchar * str;
991
992         if (matchers == NULL)
993                 return;
994
995         str = matcherlist_to_string(matchers);
996
997         if (str != NULL) {
998                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), str);
999                 g_free(str);
1000         }
1001 }
1002
1003 static void prefs_filtering_condition_define(void)
1004 {
1005         gchar * cond_str;
1006         MatcherList * matchers = NULL;
1007
1008         cond_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1009
1010         if (*cond_str != '\0') {
1011                 matchers = matcher_parser_get_cond(cond_str, NULL);
1012                 if (matchers == NULL)
1013                         alertpanel_error(_("Condition string is not valid."));
1014         }
1015         
1016         g_free(cond_str);
1017
1018         prefs_matcher_open(matchers, prefs_filtering_condition_define_done);
1019
1020         if (matchers != NULL)
1021                 matcherlist_free(matchers);
1022 }
1023
1024 static void prefs_filtering_action_define_done(GSList * action_list)
1025 {
1026         gchar * str;
1027
1028         if (action_list == NULL)
1029                 return;
1030
1031         str = filteringaction_list_to_string(action_list);
1032
1033         if (str != NULL) {
1034                 gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), str);
1035                 g_free(str);
1036         }
1037 }
1038
1039 static void prefs_filtering_action_define(void)
1040 {
1041         gchar * action_str;
1042         GSList * action_list = NULL;
1043
1044         action_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1045
1046         if (*action_str != '\0') {
1047                 action_list = matcher_parser_get_action_list(action_str);
1048                 if (action_list == NULL)
1049                         alertpanel_error(_("Action string is not valid."));
1050         }
1051         
1052         g_free(action_str);
1053
1054         prefs_filtering_action_open(action_list,
1055             prefs_filtering_action_define_done);
1056
1057         if (action_list != NULL) {
1058                 GSList * cur;
1059                 for(cur = action_list ; cur != NULL ; cur = cur->next) {
1060                         filteringaction_free(cur->data);
1061                 }
1062         }
1063 }
1064
1065
1066 /* register / substitute delete buttons */
1067
1068
1069 static FilteringProp * prefs_filtering_dialog_to_filtering(gboolean alert)
1070 {
1071         MatcherList * cond;
1072         gboolean enabled = TRUE;
1073         gchar * name = NULL;
1074         gint account_id = 0;
1075         gchar * cond_str = NULL;
1076         gchar * action_str = NULL;
1077         FilteringProp * prop = NULL;
1078         GSList * action_list;
1079
1080         name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
1081         
1082         account_id = combobox_get_active_data(GTK_COMBO_BOX(filtering.account_combobox));
1083
1084         cond_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1085         if (*cond_str == '\0') {
1086                 if(alert == TRUE) alertpanel_error(_("Condition string is empty."));
1087                 goto fail;
1088         }
1089         
1090         action_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1091         if (*action_str == '\0') {
1092                 if(alert == TRUE) alertpanel_error(_("Action string is empty."));
1093                 goto fail;
1094         }
1095
1096         cond = matcher_parser_get_cond(cond_str, NULL);
1097
1098         if (cond == NULL) {
1099                 if(alert == TRUE) alertpanel_error(_("Condition string is not valid."));
1100                 goto fail;
1101         }
1102         
1103         action_list = matcher_parser_get_action_list(action_str);
1104         
1105
1106         if (action_list == NULL) {
1107                 if(alert == TRUE) alertpanel_error(_("Action string is not valid."));
1108                 goto fail;
1109         }
1110
1111         prop = filteringprop_new(enabled, name, account_id, cond, action_list);
1112
1113 fail:
1114         g_free(name);
1115         g_free(cond_str);
1116         g_free(action_str);
1117         return prop;
1118 }
1119
1120 static void prefs_filtering_register_cb(void)
1121 {
1122         FilteringProp * prop;
1123         
1124         prop = prefs_filtering_dialog_to_filtering(TRUE);
1125         if (prop == NULL)
1126                 return;
1127         prefs_filtering_list_view_set_row(-1, prop);
1128         
1129         filteringprop_free(prop);
1130
1131         prefs_filtering_reset_dialog();
1132         modified = TRUE;
1133 }
1134
1135 static void prefs_filtering_substitute_cb(void)
1136 {
1137         gint selected_row = gtkut_list_view_get_selected_row
1138                 (filtering.cond_list_view);
1139         FilteringProp *prop;
1140         gboolean enabled;
1141         gchar *name;
1142         gint account_id;
1143         
1144         if (selected_row <= 0)
1145                 return;
1146
1147         prop = prefs_filtering_dialog_to_filtering(TRUE);
1148
1149         if (prop == NULL) 
1150                 return;
1151
1152         /* prop->emabled is always TRUE here, re-use the value from the selected row 
1153            as we don't substitute this value from dialog */
1154         prefs_filtering_list_view_get_rule_info(
1155                         filtering.cond_list_view, selected_row,
1156                         &enabled, &name, &account_id);
1157         prop->enabled = enabled;
1158
1159         prefs_filtering_list_view_set_row(selected_row, prop);
1160
1161         filteringprop_free(prop);
1162
1163         prefs_filtering_reset_dialog();
1164         modified = TRUE;
1165 }
1166
1167 static void prefs_filtering_delete_cb(void)
1168 {
1169         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
1170         GtkTreeModel *model;
1171         GtkTreeIter iter;
1172         gint row;
1173         
1174         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1175         if (row <= 0) 
1176                 return; 
1177
1178         if (alertpanel(_("Delete rule"),
1179                        _("Do you really want to delete this rule?"),
1180                        GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
1181                 return;
1182
1183         model = gtk_tree_view_get_model(list_view);     
1184         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1185                 return;
1186
1187         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1188
1189         prefs_filtering_reset_dialog();
1190         modified = TRUE;
1191 }
1192
1193 static void prefs_filtering_top(void)
1194 {
1195         gint row;
1196         GtkTreeIter top, sel;
1197         GtkTreeModel *model;
1198
1199         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1200         if (row <= 1) 
1201                 return;
1202
1203         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));               
1204         
1205         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
1206         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1207                 return;
1208
1209         gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
1210         gtkut_list_view_select_row(filtering.cond_list_view, 1);
1211         modified = TRUE;
1212 }
1213
1214 static void prefs_filtering_up(void)
1215 {
1216         gint row;
1217         GtkTreeIter top, sel;
1218         GtkTreeModel *model;
1219
1220         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1221         if (row <= 1) 
1222                 return;
1223                 
1224         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1225
1226         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
1227         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1228                 return;
1229
1230         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1231         gtkut_list_view_select_row(filtering.cond_list_view, row - 1);
1232         modified = TRUE;
1233 }
1234
1235 static void prefs_filtering_down(void)
1236 {
1237         gint row, n_rows;
1238         GtkTreeIter top, sel;
1239         GtkTreeModel *model;
1240
1241         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1242         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1243         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1244         if (row < 1 || row >= n_rows - 1)
1245                 return;
1246
1247         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1248         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
1249                 return;
1250                         
1251         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1252         gtkut_list_view_select_row(filtering.cond_list_view, row + 1);
1253         modified = TRUE;
1254 }
1255
1256 static void prefs_filtering_bottom(void)
1257 {
1258         gint row, n_rows;
1259         GtkTreeIter top, sel;
1260         GtkTreeModel *model;
1261
1262         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1263         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1264         row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1265         if (row < 1 || row >= n_rows - 1)
1266                 return;
1267
1268         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1269         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
1270                 return;
1271
1272         gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);           
1273         gtkut_list_view_select_row(filtering.cond_list_view, n_rows - 1);
1274         modified = TRUE;
1275 }
1276
1277 static void prefs_filtering_select_set(FilteringProp *prop)
1278 {
1279         gchar *matcher_str;
1280         gchar *action_str;
1281
1282         prefs_filtering_reset_dialog();
1283
1284         matcher_str = matcherlist_to_string(prop->matchers);
1285         if (matcher_str == NULL) {
1286                 return;
1287         }
1288         
1289         if (prop->name != NULL)
1290                 gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), prop->name);
1291
1292         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), prop->account_id);
1293                 
1294         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), matcher_str);
1295
1296         action_str = filteringaction_list_to_string(prop->action_list);
1297         if (matcher_str == NULL) {
1298                 return;
1299         }
1300         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), action_str);
1301
1302         g_free(action_str);
1303         g_free(matcher_str);
1304 }
1305
1306 static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
1307                                  gpointer data)
1308 {
1309         prefs_filtering_cancel();
1310         return TRUE;
1311 }
1312
1313 static gboolean prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
1314                                      gpointer data)
1315 {
1316         if (event && event->keyval == GDK_Escape) {
1317                 prefs_filtering_cancel();
1318                 return TRUE;                    
1319         }
1320         return FALSE;
1321 }
1322
1323 static gboolean prefs_filtering_check_mod(gboolean check_changed_list)
1324 {
1325         FilteringProp * prop;
1326         gchar * str;
1327         gchar * filtering_str;
1328         gint row = 1;
1329         AlertValue val;
1330         
1331         prop = prefs_filtering_dialog_to_filtering(FALSE);
1332         
1333         if (check_changed_list) {
1334                 if (modified && alertpanel(_("Filtering rules not saved"),
1335                                          _("The list of filtering rules have been modified. Close anyway?"),
1336                                          GTK_STOCK_CLOSE, _("+_Continue editing"), 
1337                                          NULL) != G_ALERTDEFAULT) {
1338                         return TRUE;
1339                 }
1340         }
1341         
1342         /* check if a rule is being edited */
1343         if (prop != NULL) {
1344                 str = filteringprop_to_string(prop);
1345
1346                 while (NULL != (filtering_str = (prefs_filtering_list_view_get_rule
1347                                                         (filtering.cond_list_view,
1348                                                          row)))) {
1349                         if (strcmp(filtering_str, str) == 0)
1350                                 break;
1351                         row++;
1352                         g_free(filtering_str);
1353                 }       
1354
1355                 if (!filtering_str) {
1356                         val = alertpanel(_("Entry not saved"),
1357                                  _("The entry was not saved. Close anyway?"),
1358                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1359                         if (G_ALERTDEFAULT != val) {
1360                                 g_free(filtering_str);
1361                                 g_free(str); /* fixed two leaks: huzzah! */
1362                                 filteringprop_free(prop);
1363                                 return TRUE;
1364                         }
1365                 }               
1366
1367                 g_free(filtering_str);
1368                 g_free(str);
1369                 filteringprop_free(prop); /* fixed a leak: huzzah! */
1370         } else {
1371                 gchar *name, *condition, *action;
1372                 name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
1373                 condition = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1374                 action = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1375                 if (strlen(name) || 
1376                     strlen(condition) || 
1377                     strlen(action)) {
1378                         val = alertpanel(_("Entry not saved"),
1379                                  _("The entry was not saved. Close anyway?"),
1380                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1381                         if (G_ALERTDEFAULT != val) {
1382                                 g_free(name);
1383                                 g_free(condition);
1384                                 g_free(action);
1385                                 return TRUE;
1386                         }
1387                 }
1388                 g_free(name);
1389                 g_free(condition);
1390                 g_free(action);
1391         }
1392         return FALSE;
1393 }
1394
1395 static void prefs_filtering_ok(void)
1396 {
1397         if (prefs_filtering_check_mod(FALSE))
1398                 return;
1399         modified = FALSE;
1400         prefs_filtering_set_list();
1401         prefs_matcher_write_config();
1402         prefs_filtering_close();
1403 }
1404
1405 static void prefs_filtering_cancel(void)
1406 {
1407         if (prefs_filtering_check_mod(TRUE))
1408                 return;
1409         modified = FALSE;
1410         prefs_matcher_read_config();
1411         prefs_filtering_close();
1412 }
1413
1414 static GtkListStore* prefs_filtering_create_data_store(void)
1415 {
1416         return gtk_list_store_new(N_PREFS_FILTERING_COLUMNS,
1417                                   G_TYPE_BOOLEAN,
1418                                   G_TYPE_STRING,
1419                                   G_TYPE_INT,
1420                                   G_TYPE_STRING,
1421                                   G_TYPE_STRING,
1422                                   G_TYPE_BOOLEAN,
1423                                  -1);
1424 }
1425
1426 /*!
1427  *\brief        Insert filtering rule into store. Note that we access the
1428  *              tree view / store by index, which is a bit suboptimal, but
1429  *              at least it made GTK 2 porting easier.
1430  *
1431  *\param        list_store Store to operate on
1432  *\param        row -1 to add a new rule to store, else change an existing
1433  *              row
1434  *\param        enabled TRUE if rule is enabled
1435  *\param        name The Name of rule
1436  *\param        account_id The account ID
1437  *\param        account_name The account name or All or (New)
1438  *\param        rule String representation of rule
1439  *\param        prop TRUE if valid filtering rule; if FALSE it's the first
1440  *              entry in the store ("(New)").
1441  *
1442  *\return       int Row of inserted / changed rule.
1443  */
1444 static gint prefs_filtering_list_view_insert_rule(GtkListStore *list_store,
1445                                                   gint row,
1446                                                   gboolean enabled,
1447                                                   const gchar *name,
1448                                                   gint account_id,
1449                                                   const gchar *account_name,
1450                                                   const gchar *rule,
1451                                                   gboolean prop) 
1452 {
1453         GtkTreeIter iter;
1454
1455         /* check if valid row at all */
1456         if (row >= 0) {
1457                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1458                                                    &iter, NULL, row))
1459                         row = -1;                                                  
1460         }
1461
1462         if (row < 0) {
1463                 /* append new */
1464                 gtk_list_store_append(list_store, &iter);
1465                 gtk_list_store_set(list_store, &iter, 
1466                                    PREFS_FILTERING_ENABLED, enabled,
1467                                    PREFS_FILTERING_NAME, name,
1468                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1469                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1470                                    PREFS_FILTERING_RULE, rule,
1471                                    PREFS_FILTERING_PROP, prop,
1472                                    -1);
1473                 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store),
1474                                                       NULL) - 1;
1475         } else {
1476                 /* change existing */
1477                 gtk_list_store_set(list_store, &iter, 
1478                                    PREFS_FILTERING_ENABLED, enabled,
1479                                    PREFS_FILTERING_NAME, name,
1480                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1481                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1482                                    PREFS_FILTERING_RULE, rule,
1483                                    -1);
1484                 return row;                                
1485         }
1486 }
1487
1488 /*!
1489  *\return       gchar * Rule at specified row - should be freed.
1490  */
1491 static gchar *prefs_filtering_list_view_get_rule(GtkWidget *list, gint row)
1492 {       
1493         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1494         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1495         GtkTreeIter iter;
1496         gchar *result;
1497
1498         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1499                 return NULL;
1500         
1501         gtk_tree_model_get(model, &iter, 
1502                            PREFS_FILTERING_RULE, &result,
1503                            -1);
1504         
1505         return result;
1506 }
1507
1508 static void prefs_filtering_list_view_get_rule_info(GtkWidget *list, gint row,
1509                                 gboolean *enabled, gchar **name, gint *account_id)
1510 {       
1511         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1512         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1513         GtkTreeIter iter;
1514
1515         *enabled = TRUE;
1516         *name = NULL;
1517
1518         if (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1519                 gtk_tree_model_get(model, &iter, 
1520                                    PREFS_FILTERING_ENABLED, enabled,
1521                                    PREFS_FILTERING_NAME, name,
1522                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1523                                    -1);
1524         }
1525 }
1526
1527 /*!
1528  *\brief        Create list view for filtering
1529  */
1530 static GtkWidget *prefs_filtering_list_view_create(void)
1531 {
1532         GtkTreeView *list_view;
1533         GtkTreeSelection *selector;
1534
1535         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL
1536                 (prefs_filtering_create_data_store())));
1537 #ifdef MAEMO
1538         g_object_set(list_view, "allow-checkbox-mode", FALSE, NULL);
1539 #endif
1540         
1541         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1542         gtk_tree_view_set_reorderable(list_view, TRUE);
1543         
1544         selector = gtk_tree_view_get_selection(list_view);
1545         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1546         gtk_tree_selection_set_select_function(selector, prefs_filtering_selected,
1547                                                NULL, NULL);
1548
1549         /* create the columns */
1550         prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view));
1551
1552         return GTK_WIDGET(list_view);
1553 }
1554
1555 static void prefs_filtering_enable_toggled(GtkCellRendererToggle *widget,
1556                 gchar *path,
1557                 GtkWidget *list_view)
1558 {
1559         GtkTreeIter iter;
1560         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1561         gboolean enabled = TRUE;
1562
1563         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1564                 return;
1565
1566         gtk_tree_model_get(model, &iter,
1567                            PREFS_FILTERING_ENABLED, &enabled,
1568                            -1);
1569
1570         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1571                            PREFS_FILTERING_ENABLED, !enabled,
1572                            -1);
1573 }
1574
1575 static void prefs_filtering_create_list_view_columns(GtkWidget *list_view)
1576 {
1577         GtkTreeViewColumn *column;
1578         GtkCellRenderer *renderer;
1579
1580         renderer = gtk_cell_renderer_toggle_new();
1581         g_object_set(renderer,
1582                      "radio", FALSE,
1583                      "activatable", TRUE,
1584                      NULL);
1585         column = gtk_tree_view_column_new_with_attributes
1586                 (_("Enable"), /* FIXME : Enable, Enabled, or 'E' ? */
1587                  renderer,
1588                  "active", PREFS_FILTERING_ENABLED,
1589                  NULL);
1590         gtk_tree_view_column_set_alignment (column, 0.5);
1591         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1592         g_signal_connect(G_OBJECT(renderer), "toggled",
1593                          G_CALLBACK(prefs_filtering_enable_toggled),
1594                          list_view);
1595
1596         renderer = gtk_cell_renderer_text_new();
1597         column = gtk_tree_view_column_new_with_attributes
1598                 (_("Name"),
1599                  renderer,
1600                  "text", PREFS_FILTERING_NAME,
1601                  NULL);
1602         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1603         gtk_tree_view_column_set_resizable(column, TRUE);
1604
1605         renderer = gtk_cell_renderer_text_new();
1606         column = gtk_tree_view_column_new_with_attributes
1607                 (_("Account"),
1608                  renderer,
1609                  "text", PREFS_FILTERING_ACCOUNT_NAME,
1610                  NULL);
1611         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1612         gtk_tree_view_column_set_resizable(column, TRUE);
1613
1614         filtering.account_name_column = column;
1615                 
1616         renderer = gtk_cell_renderer_text_new();
1617         column = gtk_tree_view_column_new_with_attributes
1618                 (_("Rule"),
1619                  renderer,
1620                  "text", PREFS_FILTERING_RULE,
1621                  NULL);
1622         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1623 }
1624
1625 /*!
1626  *\brief        Triggered when a row is selected
1627  */
1628 static gboolean prefs_filtering_selected(GtkTreeSelection *selector,
1629                                          GtkTreeModel *model, 
1630                                          GtkTreePath *path,
1631                                          gboolean currently_selected,
1632                                          gpointer data)
1633 {
1634         if (currently_selected)
1635                 return TRUE;
1636         else {          
1637                 gboolean has_prop  = FALSE;
1638                 GtkTreeIter iter;
1639
1640                 gtk_tree_model_get_iter(model, &iter, path);
1641                 gtk_tree_model_get(model, &iter,
1642                                    PREFS_FILTERING_PROP, &has_prop,
1643                                    -1);
1644
1645                 if (has_prop) {
1646                         FilteringProp *prop;
1647                         gchar *filtering_str = NULL;
1648                         gchar *name = NULL;
1649                         gint account_id = 0;
1650
1651                         gtk_tree_model_get(model, &iter,
1652                                            PREFS_FILTERING_RULE, &filtering_str,
1653                                            -1);
1654                         gtk_tree_model_get(model, &iter,
1655                                            PREFS_FILTERING_NAME, &name,
1656                                            -1);
1657                         gtk_tree_model_get(model, &iter,
1658                                            PREFS_FILTERING_ACCOUNT_ID, &account_id,
1659                                            -1);
1660
1661                         prop = matcher_parser_get_filtering(filtering_str);
1662                         if (prop) {
1663                                 prop->name = g_strdup(name);
1664                                 prop->account_id = account_id;
1665                                 prefs_filtering_select_set(prop);
1666                                 filteringprop_free(prop);
1667                         }                               
1668                         g_free(name);
1669                         g_free(filtering_str);
1670                 } else
1671                         prefs_filtering_reset_dialog();
1672         }               
1673
1674         return TRUE;
1675 }
1676