de09d3b790cf1f8e5467c4b4ccbce7cfa6cc987d
[claws.git] / src / prefs_filtering.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 "colorlabel.h"
52 #include "manual.h"
53 #include "combobox.h"
54
55 #include "matcher_parser.h"
56 #include "matcher.h"
57 #include "prefs_filtering_action.h"
58
59 enum {
60         PREFS_FILTERING_ENABLED,
61         PREFS_FILTERING_NAME,
62         PREFS_FILTERING_ACCOUNT_ID,
63         PREFS_FILTERING_ACCOUNT_NAME,
64         PREFS_FILTERING_RULE,
65         PREFS_FILTERING_PROP,
66         N_PREFS_FILTERING_COLUMNS
67 };
68
69 struct _Filtering {
70         GtkWidget *window;
71
72         GtkWidget *help_btn;
73         GtkWidget *ok_btn;
74         GtkWidget *name_entry;
75         GtkWidget *account_label;
76         GtkWidget *account_combobox;
77         GtkListStore *account_combobox_list;
78         GtkWidget *cond_entry;
79         GtkWidget *action_entry;
80
81         GtkWidget *cond_list_view;
82
83         GtkTreeViewColumn *account_name_column;
84 };
85
86 typedef struct _Filtering Filtering;
87
88 static Filtering  filtering;
89
90 static GSList ** p_processing_list = NULL;
91
92 /* widget creating functions */
93 static void prefs_filtering_create              (void);
94
95 static void prefs_filtering_set_dialog  (const gchar *header,
96                                          const gchar *key);
97 static void prefs_filtering_set_list    (void);
98
99 /* callback functions */
100 static void prefs_filtering_register_cb (void);
101 static void prefs_filtering_substitute_cb       (void);
102 static void prefs_filtering_delete_cb   (void);
103 static void prefs_filtering_top         (void);
104 static void prefs_filtering_up          (void);
105 static void prefs_filtering_down        (void);
106 static void prefs_filtering_bottom      (void);
107 static gint prefs_filtering_deleted     (GtkWidget      *widget,
108                                          GdkEventAny    *event,
109                                          gpointer        data);
110 static gboolean prefs_filtering_key_pressed(GtkWidget   *widget,
111                                          GdkEventKey    *event,
112                                          gpointer        data);
113 static void prefs_filtering_cancel      (void);
114 static void prefs_filtering_ok          (void);
115
116 static void prefs_filtering_condition_define    (void);
117 static void prefs_filtering_action_define(void);
118 static gint prefs_filtering_list_view_set_row   (gint row, FilteringProp * prop);
119                                           
120 static void prefs_filtering_reset_dialog        (void);
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_name     (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 static gint prefs_filtering_get_selected_row            (GtkWidget *list_view);
147 static gboolean prefs_filtering_list_view_select_row    (GtkWidget *list, gint row);
148
149 static gboolean prefs_filtering_selected                (GtkTreeSelection *selector,
150                                                          GtkTreeModel *model, 
151                                                          GtkTreePath *path,
152                                                          gboolean currently_selected,
153                                                          gpointer data);
154
155 static void prefs_filtering_account_option_menu_populate(void);
156
157 static gulong signal_id = 0; /* filtering.help_btn clicked signal */
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(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 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
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(3, 4, FALSE);
359         gtk_widget_show(table);
360         gtk_box_pack_start (GTK_BOX (vbox1), table, TRUE, TRUE, 0);
361         
362
363         name_label = gtk_label_new (_("Name: "));
364         gtk_widget_show (name_label);
365         gtk_misc_set_alignment (GTK_MISC (name_label), 1, 0.5);
366         gtk_table_attach (GTK_TABLE (table), name_label, 0, 1, 0, 1,
367                           (GtkAttachOptions) (GTK_FILL),
368                           (GtkAttachOptions) (0), 0, 0);
369
370         name_entry = gtk_entry_new ();
371         gtk_widget_show (name_entry);
372         gtk_table_attach (GTK_TABLE (table), name_entry, 1, 2, 0, 1,
373                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
374                           (GtkAttachOptions) (0), 0, 0);
375
376         account_label = gtk_label_new (_("Account:"));
377         gtk_widget_show (account_label);
378         gtk_misc_set_alignment (GTK_MISC (account_label), 1, 0.5);
379         gtk_table_attach (GTK_TABLE (table), account_label, 0, 1, 1, 2,
380                           (GtkAttachOptions) (GTK_FILL),
381                           (GtkAttachOptions) (0), 0, 0);
382
383         account_opt_menu = prefs_filtering_account_option_menu(&filtering);
384         gtk_widget_show (account_opt_menu);
385         gtk_table_attach (GTK_TABLE (table), account_opt_menu, 1, 2, 1, 2,
386                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
387                           (GtkAttachOptions) (0), 0, 0);
388         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
389
390         cond_label = gtk_label_new (_("Condition: "));
391         gtk_widget_show (cond_label);
392         gtk_misc_set_alignment (GTK_MISC (cond_label), 1, 0.5);
393         gtk_table_attach (GTK_TABLE (table), cond_label, 0, 1, 2, 3,
394                           (GtkAttachOptions) (GTK_FILL),
395                           (GtkAttachOptions) (0), 0, 0);
396
397         cond_entry = gtk_entry_new ();
398         gtk_widget_show (cond_entry);
399         gtk_table_attach (GTK_TABLE (table), cond_entry, 1, 2, 2, 3,
400                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
401                           (GtkAttachOptions) (0), 0, 0);
402
403         cond_btn = gtk_button_new_with_label (_(" Define... "));
404         gtk_widget_show (cond_btn);
405         gtk_table_attach (GTK_TABLE (table), cond_btn, 2, 3, 2, 3,
406                           (GtkAttachOptions) (GTK_FILL),
407                           (GtkAttachOptions) (0), 2, 2);
408         g_signal_connect(G_OBJECT (cond_btn), "clicked",
409                          G_CALLBACK(prefs_filtering_condition_define),
410                          NULL);
411
412         action_label = gtk_label_new (_("Action: "));
413         gtk_widget_show (action_label);
414         gtk_misc_set_alignment (GTK_MISC (action_label), 1, 0.5);
415         gtk_table_attach (GTK_TABLE (table), action_label, 0, 1, 3, 4,
416                           (GtkAttachOptions) (GTK_FILL),
417                           (GtkAttachOptions) (0), 0, 0);
418
419         action_entry = gtk_entry_new ();
420         gtk_widget_show (action_entry);
421         gtk_table_attach (GTK_TABLE (table), action_entry, 1, 2, 3, 4,
422                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
423                           (GtkAttachOptions) (0), 0, 0);
424
425         action_btn = gtk_button_new_with_label (_(" Define... "));
426         gtk_widget_show (action_btn);
427         gtk_table_attach (GTK_TABLE (table), action_btn, 2, 3, 3, 4,
428                           (GtkAttachOptions) (GTK_FILL),
429                           (GtkAttachOptions) (0), 2, 2);
430         g_signal_connect(G_OBJECT (action_btn), "clicked",
431                          G_CALLBACK(prefs_filtering_action_define),
432                          NULL);
433                          
434         /* register / substitute / delete */
435         reg_hbox = gtk_hbox_new (FALSE, 4);
436         gtk_widget_show (reg_hbox);
437         gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
438
439         arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
440         gtk_widget_show (arrow);
441         gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
442         gtk_widget_set_size_request (arrow, -1, 16);
443
444         btn_hbox = gtk_hbox_new (TRUE, 4);
445         gtk_widget_show (btn_hbox);
446         gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
447
448         reg_btn = gtk_button_new_from_stock (GTK_STOCK_ADD);
449         gtk_widget_show (reg_btn);
450         gtk_box_pack_start (GTK_BOX (btn_hbox), reg_btn, FALSE, TRUE, 0);
451         g_signal_connect(G_OBJECT (reg_btn), "clicked",
452                          G_CALLBACK(prefs_filtering_register_cb), NULL);
453
454         subst_btn = gtkut_get_replace_btn (_("Replace"));
455         gtk_widget_show (subst_btn);
456         gtk_box_pack_start (GTK_BOX (btn_hbox), subst_btn, FALSE, TRUE, 0);
457         g_signal_connect(G_OBJECT (subst_btn), "clicked",
458                          G_CALLBACK(prefs_filtering_substitute_cb),
459                          NULL);
460
461         del_btn = gtk_button_new_from_stock (GTK_STOCK_DELETE);
462         gtk_widget_show (del_btn);
463         gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
464         g_signal_connect(G_OBJECT (del_btn), "clicked",
465                         G_CALLBACK(prefs_filtering_delete_cb), NULL);
466
467         cond_hbox = gtk_hbox_new (FALSE, 8);
468         gtk_widget_show (cond_hbox);
469         gtk_box_pack_start (GTK_BOX (vbox), cond_hbox, TRUE, TRUE, 0);
470
471         cond_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
472         gtk_widget_show (cond_scrolledwin);
473         gtk_widget_set_size_request (cond_scrolledwin, -1, 150);
474         gtk_box_pack_start (GTK_BOX (cond_hbox), cond_scrolledwin,
475                             TRUE, TRUE, 0);
476         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin),
477                                         GTK_POLICY_AUTOMATIC,
478                                         GTK_POLICY_AUTOMATIC);
479
480         cond_list_view = prefs_filtering_list_view_create();    
481         gtk_widget_show (cond_list_view);
482         gtk_container_add (GTK_CONTAINER (cond_scrolledwin), cond_list_view);
483
484         btn_vbox = gtk_vbox_new (FALSE, 8);
485         gtk_widget_show (btn_vbox);
486         gtk_box_pack_start (GTK_BOX (cond_hbox), btn_vbox, FALSE, FALSE, 0);
487
488         top_btn = gtk_button_new_from_stock (GTK_STOCK_GOTO_TOP);
489         gtk_widget_show (top_btn);
490         gtk_box_pack_start (GTK_BOX (btn_vbox), top_btn, FALSE, FALSE, 0);
491         g_signal_connect(G_OBJECT (top_btn), "clicked",
492                          G_CALLBACK(prefs_filtering_top), NULL);
493
494         PACK_VSPACER (btn_vbox, spc_vbox, VSPACING_NARROW_2);
495
496         up_btn = gtk_button_new_from_stock (GTK_STOCK_GO_UP);
497         gtk_widget_show (up_btn);
498         gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
499         g_signal_connect(G_OBJECT (up_btn), "clicked",
500                          G_CALLBACK(prefs_filtering_up), NULL);
501
502         down_btn = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN);
503         gtk_widget_show (down_btn);
504         gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
505         g_signal_connect(G_OBJECT (down_btn), "clicked",
506                          G_CALLBACK(prefs_filtering_down), NULL);
507
508         PACK_VSPACER (btn_vbox, spc_vbox, VSPACING_NARROW_2);
509
510         bottom_btn = gtk_button_new_from_stock (GTK_STOCK_GOTO_BOTTOM);
511         gtk_widget_show (bottom_btn);
512         gtk_box_pack_start (GTK_BOX (btn_vbox), bottom_btn, FALSE, FALSE, 0);
513         g_signal_connect(G_OBJECT (bottom_btn), "clicked",
514                          G_CALLBACK(prefs_filtering_bottom), NULL);
515
516         if (!geometry.min_height) {
517                 geometry.min_width = 500;
518                 geometry.min_height = 400;
519         }
520
521         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
522                                       GDK_HINT_MIN_SIZE);
523         gtk_widget_set_size_request(window, prefs_common.filteringwin_width,
524                                     prefs_common.filteringwin_height);
525
526         gtk_widget_show_all(window);
527
528         filtering.window    = window;
529         filtering.help_btn = help_btn;
530         filtering.ok_btn = ok_btn;
531
532         filtering.name_entry     = name_entry;
533         filtering.cond_entry     = cond_entry;
534         filtering.action_entry   = action_entry;
535         filtering.cond_list_view = cond_list_view;
536         filtering.account_label  = account_label;
537 }
538
539 static void rename_path(GSList * filters,
540                         const gchar * old_path, const gchar * new_path);
541
542 void prefs_filtering_rename_path(const gchar *old_path, const gchar *new_path)
543 {
544         GList * cur;
545         const gchar *paths[2] = {NULL, NULL};
546         paths[0] = old_path;
547         paths[1] = new_path;
548         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
549                 Folder *folder;
550                 folder = (Folder *) cur->data;
551                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
552                                 prefs_filtering_rename_path_func, paths);
553         }
554         
555         rename_path(pre_global_processing, old_path, new_path);
556         rename_path(post_global_processing, old_path, new_path);
557         rename_path(filtering_rules, old_path, new_path);
558         
559         prefs_matcher_write_config();
560 }
561
562 static void rename_path(GSList * filters,
563                         const gchar * old_path, const gchar * new_path)
564 {
565         gchar *base;
566         gchar *prefix;
567         gchar *suffix;
568         gchar *dest_path;
569         gchar *old_path_with_sep;
570         gint destlen;
571         gint prefixlen;
572         gint oldpathlen;
573         GSList * action_cur;
574         GSList * cur;
575
576         oldpathlen = strlen(old_path);
577         old_path_with_sep = g_strconcat(old_path,G_DIR_SEPARATOR_S,NULL);
578
579         for (cur = filters; cur != NULL; cur = cur->next) {
580                 FilteringProp   *filtering = (FilteringProp *)cur->data;
581                 
582                 for(action_cur = filtering->action_list ; action_cur != NULL ;
583                     action_cur = action_cur->next) {
584
585                         FilteringAction *action = action_cur->data;
586                         
587                         if (!action->destination) continue;
588                         
589                         destlen = strlen(action->destination);
590                         
591                         if (destlen > oldpathlen) {
592                                 prefixlen = destlen - oldpathlen;
593                                 suffix = action->destination + prefixlen;
594                                 
595                                 if (!strncmp(old_path, suffix, oldpathlen)) {
596                                         prefix = g_malloc0(prefixlen + 1);
597                                         strncpy2(prefix, action->destination, prefixlen);
598                                         
599                                         base = suffix + oldpathlen;
600                                         while (*base == G_DIR_SEPARATOR) base++;
601                                         if (*base == '\0')
602                                                 dest_path = g_strconcat(prefix,
603                                                     G_DIR_SEPARATOR_S,
604                                                     new_path, NULL);
605                                         else
606                                                 dest_path = g_strconcat(prefix,
607                                                     G_DIR_SEPARATOR_S,
608                                                     new_path,
609                                                     G_DIR_SEPARATOR_S,
610                                                     base, NULL);
611                                         
612                                         g_free(prefix);
613                                         g_free(action->destination);
614                                         action->destination = dest_path;
615                                 } else { /* for non-leaf folders */
616                                         /* compare with trailing slash */
617                                         if (!strncmp(old_path_with_sep, action->destination, oldpathlen+1)) {
618                                                 
619                                                 suffix = action->destination + oldpathlen + 1;
620                                                 dest_path = g_strconcat(new_path,
621                                                     G_DIR_SEPARATOR_S,
622                                                     suffix, NULL);
623                                                 g_free(action->destination);
624                                                 action->destination = dest_path;
625                                         }
626                                 }
627                         } else {
628                                 /* folder-moving a leaf */
629                                 if (!strcmp(old_path, action->destination)) {
630                                         dest_path = g_strdup(new_path);
631                                         g_free(action->destination);
632                                         action->destination = dest_path;
633                                 }
634                         }
635                 }
636         }
637 }
638
639 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
640 {
641         GSList *filters;
642         const gchar * old_path;
643         const gchar * new_path;
644         const gchar ** paths;
645         FolderItem *item;
646         
647         paths = data;
648         old_path = paths[0];
649         new_path = paths[1];
650
651         g_return_val_if_fail(old_path != NULL, FALSE);
652         g_return_val_if_fail(new_path != NULL, FALSE);
653         g_return_val_if_fail(node != NULL, FALSE);
654
655         item = node->data;
656         if (!item || !item->prefs)
657                 return FALSE;
658         filters = item->prefs->processing;
659
660         rename_path(filters, old_path, new_path);
661
662         return FALSE;
663 }
664
665 void prefs_filtering_delete_path(const gchar *path)
666 {
667         GList * cur;
668         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
669                 Folder *folder;
670                 folder = (Folder *) cur->data;
671                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
672                                 prefs_filtering_delete_path_func, (gchar *)path);
673         }
674         delete_path(&pre_global_processing, path);
675         delete_path(&post_global_processing, path);
676         delete_path(&filtering_rules, path);
677         
678         prefs_matcher_write_config();
679 }
680
681 static void delete_path(GSList ** p_filters, const gchar * path)
682 {
683         GSList * filters;
684         GSList * duplist;
685         gchar *suffix;
686         gint destlen;
687         gint prefixlen;
688         gint pathlen;
689         GSList * action_cur;
690         GSList * cur;
691         
692         filters = *p_filters;
693         pathlen = strlen(path);
694         duplist = g_slist_copy(filters);
695         for (cur = duplist ; cur != NULL; cur = g_slist_next(cur)) {
696                 FilteringProp *filtering = (FilteringProp *) cur->data;
697                 
698                 for(action_cur = filtering->action_list ; action_cur != NULL ;
699                     action_cur = action_cur->next) {
700                 
701                         FilteringAction *action;
702                         
703                         action = action_cur->data;
704                         
705                         if (!action->destination) continue;
706                         
707                         destlen = strlen(action->destination);
708                         
709                         if (destlen > pathlen) {
710                                 prefixlen = destlen - pathlen;
711                                 suffix = action->destination + prefixlen;
712                                 
713                                 if (suffix && !strncmp(path, suffix, pathlen)) {
714                                         filteringprop_free(filtering);
715                                         filters = g_slist_remove(filters, filtering);
716                                 }
717                         } else if (strcmp(action->destination, path) == 0) {
718                                 filteringprop_free(filtering);
719                                 filters = g_slist_remove(filters, filtering);
720                         }
721                 }
722         }                
723         g_slist_free(duplist);
724         
725         * p_filters = filters;
726 }
727
728 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data)
729 {
730         const gchar *path = data;
731         FolderItem *item;
732         GSList ** p_filters;
733         
734         g_return_val_if_fail(path != 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         p_filters = &item->prefs->processing;
741         
742         delete_path(p_filters, path);
743
744         return FALSE;
745 }
746
747 static void prefs_filtering_set_dialog(const gchar *header, const gchar *key)
748 {
749         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
750         GSList *cur;
751         GSList * prefs_filtering;
752         gchar *cond_str;
753         GtkListStore *list_store;
754         
755         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
756         gtk_list_store_clear(list_store);
757
758         /* add the place holder (New) at row 0 */
759         prefs_filtering_list_view_insert_rule(list_store, -1, 
760                                               FALSE,
761                                               _("(New)"),
762                                                   0,
763                                               _("(New)"),
764                                               _("(New)"),
765                                               FALSE);
766
767         prefs_filtering = *p_processing_list;
768
769         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur)) {
770                 FilteringProp * prop = (FilteringProp *) cur->data;
771                 gchar *account_name;
772
773                 if (prop->account_id > 0) {
774                         account_name = account_find_from_id(prop->account_id)->account_name;
775                 } else {
776                         account_name = (gchar *)Q_("Filtering Account Menu|All");
777                 }
778
779                 cond_str = filteringprop_to_string(prop);
780                 subst_char(cond_str, '\t', ':');
781
782                 prefs_filtering_list_view_insert_rule(list_store, -1, 
783                                                       prop->enabled,
784                                                       prop->name,
785                                                           prop->account_id,
786                                                           account_name,
787                                                       cond_str, TRUE);
788                 
789                 g_free(cond_str);
790         }
791
792         prefs_filtering_reset_dialog();
793
794         if (header && key) {
795                 gchar * quoted_key;
796                 gchar *match_str;
797
798                 quoted_key = matcher_quote_str(key);
799                 
800                 match_str = g_strconcat(header, " ", get_matchparser_tab_str(MATCHTYPE_MATCHCASE),
801                                         " \"", quoted_key, "\"", NULL);
802                 g_free(quoted_key);
803                 
804                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), match_str);
805                 g_free(match_str);
806         }
807 }
808
809 static void prefs_filtering_reset_dialog(void)
810 {
811         gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), "");
812         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
813         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), "");
814         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), "");
815 }
816
817 static void prefs_filtering_set_list(void)
818 {
819         gint row = 1;
820         FilteringProp *prop;
821         GSList * cur;
822         gchar * filtering_str;
823         GSList * prefs_filtering;
824
825         prefs_filtering = *p_processing_list;
826         for (cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
827                 filteringprop_free((FilteringProp *) cur->data);
828         g_slist_free(prefs_filtering);
829         prefs_filtering = NULL;
830         
831
832         while (NULL != (filtering_str = prefs_filtering_list_view_get_rule
833                                                 (filtering.cond_list_view, row))) {
834                 /* FIXME: this strcmp() is bogus: "(New)" should never
835                  * be inserted in the storage */
836                 if (strcmp(filtering_str, _("(New)")) != 0) {
837                         gboolean enabled;
838                         gchar *name;
839                         gint account_id = 0;
840
841                         prefs_filtering_list_view_get_rule_name(
842                                         filtering.cond_list_view, row,
843                                         &enabled, &name, &account_id);
844                         prop = matcher_parser_get_filtering(filtering_str);
845                         g_free(filtering_str);
846                         if (prop) {
847                                 prop->enabled = enabled;
848                                 prop->name = name;
849                                 prop->account_id = account_id;
850                                 prefs_filtering = 
851                                         g_slist_append(prefs_filtering, prop);
852                         }
853                 }
854                 
855                 row++;
856         }                               
857         
858         *p_processing_list = prefs_filtering;
859 }
860
861 static gint prefs_filtering_list_view_set_row(gint row, FilteringProp * prop)
862 {
863         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
864         gchar *str;
865         GtkListStore *list_store;
866         gchar *name = NULL;
867         gint account_id = 0;
868         gchar *account_name = (gchar *)Q_("Filtering Account Menu|All");
869         gboolean enabled = TRUE;
870
871         str = filteringprop_to_string(prop);
872         if (str == NULL)
873                 return -1;
874
875         if (prop) {
876                 if (prop->name)
877                         name = prop->name;
878                 account_id = prop->account_id;
879                 if (account_id > 0) {
880                         account_name = account_find_from_id(account_id)->account_name;
881                 } else {
882                         account_name = (gchar *)Q_("Filtering Account Menu|All");
883                 }
884                 enabled = prop->enabled;
885         }
886
887         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
888
889         row = prefs_filtering_list_view_insert_rule(list_store, row,
890                                                     enabled,
891                                                     name,
892                                                     account_id,
893                                                         account_name,
894                                                     str,
895                                                     prop != NULL);
896
897         g_free(str);
898
899         return row;
900 }
901
902 static void prefs_filtering_condition_define_done(MatcherList * matchers)
903 {
904         gchar * str;
905
906         if (matchers == NULL)
907                 return;
908
909         str = matcherlist_to_string(matchers);
910
911         if (str != NULL) {
912                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), str);
913                 g_free(str);
914         }
915 }
916
917 static void prefs_filtering_condition_define(void)
918 {
919         gchar * cond_str;
920         MatcherList * matchers = NULL;
921
922         cond_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
923
924         if (*cond_str != '\0') {
925                 matchers = matcher_parser_get_cond(cond_str);
926                 if (matchers == NULL)
927                         alertpanel_error(_("Condition string is not valid."));
928         }
929         
930         g_free(cond_str);
931
932         prefs_matcher_open(matchers, prefs_filtering_condition_define_done);
933
934         if (matchers != NULL)
935                 matcherlist_free(matchers);
936 }
937
938 static void prefs_filtering_action_define_done(GSList * action_list)
939 {
940         gchar * str;
941
942         if (action_list == NULL)
943                 return;
944
945         str = filteringaction_list_to_string(action_list);
946
947         if (str != NULL) {
948                 gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), str);
949                 g_free(str);
950         }
951 }
952
953 static void prefs_filtering_action_define(void)
954 {
955         gchar * action_str;
956         GSList * action_list = NULL;
957
958         action_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
959
960         if (*action_str != '\0') {
961                 action_list = matcher_parser_get_action_list(action_str);
962                 if (action_list == NULL)
963                         alertpanel_error(_("Action string is not valid."));
964         }
965         
966         g_free(action_str);
967
968         prefs_filtering_action_open(action_list,
969             prefs_filtering_action_define_done);
970
971         if (action_list != NULL) {
972                 GSList * cur;
973                 for(cur = action_list ; cur != NULL ; cur = cur->next) {
974                         filteringaction_free(cur->data);
975                 }
976         }
977 }
978
979
980 /* register / substitute delete buttons */
981
982
983 static FilteringProp * prefs_filtering_dialog_to_filtering(gboolean alert)
984 {
985         MatcherList * cond;
986         gboolean enabled = TRUE;
987         gchar * name = NULL;
988         gint account_id = 0;
989         gchar * cond_str = NULL;
990         gchar * action_str = NULL;
991         FilteringProp * prop = NULL;
992         GSList * action_list;
993
994         name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
995         
996         account_id = combobox_get_active_data(GTK_COMBO_BOX(filtering.account_combobox));
997
998         cond_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
999         if (*cond_str == '\0') {
1000                 if(alert == TRUE) alertpanel_error(_("Condition string is empty."));
1001                 goto fail;
1002         }
1003         
1004         action_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1005         if (*action_str == '\0') {
1006                 if(alert == TRUE) alertpanel_error(_("Action string is empty."));
1007                 goto fail;
1008         }
1009
1010         cond = matcher_parser_get_cond(cond_str);
1011
1012         if (cond == NULL) {
1013                 if(alert == TRUE) alertpanel_error(_("Condition string is not valid."));
1014                 goto fail;
1015         }
1016         
1017         action_list = matcher_parser_get_action_list(action_str);
1018         
1019
1020         if (action_list == NULL) {
1021                 if(alert == TRUE) alertpanel_error(_("Action string is not valid."));
1022                 goto fail;
1023         }
1024
1025         prop = filteringprop_new(enabled, name, account_id, cond, action_list);
1026
1027 fail:
1028         g_free(name);
1029         g_free(cond_str);
1030         g_free(action_str);
1031         return prop;
1032 }
1033
1034 static void prefs_filtering_register_cb(void)
1035 {
1036         FilteringProp * prop;
1037         
1038         prop = prefs_filtering_dialog_to_filtering(TRUE);
1039         if (prop == NULL)
1040                 return;
1041         prefs_filtering_list_view_set_row(-1, prop);
1042         
1043         filteringprop_free(prop);
1044
1045         prefs_filtering_reset_dialog();
1046 }
1047
1048 static void prefs_filtering_substitute_cb(void)
1049 {
1050         gint selected_row = prefs_filtering_get_selected_row
1051                 (filtering.cond_list_view);
1052         FilteringProp *prop;
1053         
1054         if (selected_row <= 0)
1055                 return;
1056
1057         prop = prefs_filtering_dialog_to_filtering(TRUE);
1058         if (prop == NULL) 
1059                 return;
1060         prefs_filtering_list_view_set_row(selected_row, prop);
1061
1062         filteringprop_free(prop);
1063
1064         prefs_filtering_reset_dialog();
1065 }
1066
1067 static void prefs_filtering_delete_cb(void)
1068 {
1069         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
1070         GtkTreeModel *model;
1071         GtkTreeIter iter;
1072         gint row;
1073         
1074         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1075         if (row <= 0) 
1076                 return; 
1077
1078         if (alertpanel(_("Delete rule"),
1079                        _("Do you really want to delete this rule?"),
1080                        GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
1081                 return;
1082
1083         model = gtk_tree_view_get_model(list_view);     
1084         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1085                 return;
1086
1087         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1088
1089         prefs_filtering_reset_dialog();
1090 }
1091
1092 static void prefs_filtering_top(void)
1093 {
1094         gint row;
1095         GtkTreeIter top, sel;
1096         GtkTreeModel *model;
1097
1098         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1099         if (row <= 1) 
1100                 return;
1101
1102         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));               
1103         
1104         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
1105         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1106                 return;
1107
1108         gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
1109         prefs_filtering_list_view_select_row(filtering.cond_list_view, 1);
1110 }
1111
1112 static void prefs_filtering_up(void)
1113 {
1114         gint row;
1115         GtkTreeIter top, sel;
1116         GtkTreeModel *model;
1117
1118         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1119         if (row <= 1) 
1120                 return;
1121                 
1122         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1123
1124         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
1125         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1126                 return;
1127
1128         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1129         prefs_filtering_list_view_select_row(filtering.cond_list_view, row - 1);
1130 }
1131
1132 static void prefs_filtering_down(void)
1133 {
1134         gint row, n_rows;
1135         GtkTreeIter top, sel;
1136         GtkTreeModel *model;
1137
1138         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1139         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1140         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1141         if (row < 1 || row >= n_rows - 1)
1142                 return;
1143
1144         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1145         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
1146                 return;
1147                         
1148         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1149         prefs_filtering_list_view_select_row(filtering.cond_list_view, row + 1);
1150 }
1151
1152 static void prefs_filtering_bottom(void)
1153 {
1154         gint row, n_rows;
1155         GtkTreeIter top, sel;
1156         GtkTreeModel *model;
1157
1158         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1159         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1160         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1161         if (row < 1 || row >= n_rows - 1)
1162                 return;
1163
1164         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1165         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
1166                 return;
1167
1168         gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);           
1169         prefs_filtering_list_view_select_row(filtering.cond_list_view, n_rows - 1);
1170 }
1171
1172 static void prefs_filtering_select_set(FilteringProp *prop)
1173 {
1174         gchar *matcher_str;
1175         gchar *action_str;
1176
1177         prefs_filtering_reset_dialog();
1178
1179         matcher_str = matcherlist_to_string(prop->matchers);
1180         if (matcher_str == NULL) {
1181                 return;
1182         }
1183         
1184         if (prop->name != NULL)
1185                 gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), prop->name);
1186
1187         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), prop->account_id);
1188                 
1189         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), matcher_str);
1190
1191         action_str = filteringaction_list_to_string(prop->action_list);
1192         if (matcher_str == NULL) {
1193                 return;
1194         }
1195         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), action_str);
1196
1197         g_free(action_str);
1198         g_free(matcher_str);
1199 }
1200
1201 static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
1202                                  gpointer data)
1203 {
1204         prefs_filtering_cancel();
1205         return TRUE;
1206 }
1207
1208 static gboolean prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
1209                                      gpointer data)
1210 {
1211         if (event && event->keyval == GDK_Escape) {
1212                 prefs_filtering_cancel();
1213                 return TRUE;                    
1214         }
1215         return FALSE;
1216 }
1217
1218 static void prefs_filtering_ok(void)
1219 {
1220         FilteringProp * prop;
1221         gchar * str;
1222         gchar * filtering_str;
1223         gint row = 1;
1224         AlertValue val;
1225         
1226         prop = prefs_filtering_dialog_to_filtering(FALSE);
1227         if (prop != NULL) {
1228                 str = filteringprop_to_string(prop);
1229
1230                 while (NULL != (filtering_str = (prefs_filtering_list_view_get_rule
1231                                                         (filtering.cond_list_view,
1232                                                          row)))) {
1233                         if (strcmp(filtering_str, str) == 0)
1234                                 break;
1235                         row++;
1236                         g_free(filtering_str);
1237                 }       
1238
1239                 if (!filtering_str) {
1240                         val = alertpanel(_("Entry not saved"),
1241                                  _("The entry was not saved. Close anyway?"),
1242                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1243                         if (G_ALERTDEFAULT != val) {
1244                                 g_free(filtering_str);
1245                                 g_free(str); /* fixed two leaks: huzzah! */
1246                                 filteringprop_free(prop);
1247                                 return;
1248                         }
1249                 }               
1250
1251                 g_free(filtering_str);
1252                 g_free(str);
1253                 filteringprop_free(prop); /* fixed a leak: huzzah! */
1254         } else {
1255                 gchar *name, *condition, *action;
1256                 name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
1257                 condition = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1258                 action = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1259                 if (strlen(name) || 
1260                     strlen(condition) || 
1261                     strlen(action)) {
1262                         val = alertpanel(_("Entry not saved"),
1263                                  _("The entry was not saved. Close anyway?"),
1264                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1265                         if (G_ALERTDEFAULT != val) {
1266                                 g_free(name);
1267                                 g_free(condition);
1268                                 g_free(action);
1269                                 return;
1270                         }
1271                 }
1272                 g_free(name);
1273                 g_free(condition);
1274                 g_free(action);
1275         }
1276         prefs_filtering_set_list();
1277         prefs_matcher_write_config();
1278         prefs_filtering_close();
1279 }
1280
1281 static void prefs_filtering_cancel(void)
1282 {
1283         prefs_matcher_read_config();
1284         prefs_filtering_close();
1285 }
1286
1287 static GtkListStore* prefs_filtering_create_data_store(void)
1288 {
1289         return gtk_list_store_new(N_PREFS_FILTERING_COLUMNS,
1290                                   G_TYPE_BOOLEAN,
1291                                   G_TYPE_STRING,
1292                                   G_TYPE_INT,
1293                                   G_TYPE_STRING,
1294                                   G_TYPE_STRING,
1295                                   G_TYPE_BOOLEAN,
1296                                  -1);
1297 }
1298
1299 /*!
1300  *\brief        Insert filtering rule into store. Note that we access the
1301  *              tree view / store by index, which is a bit suboptimal, but
1302  *              at least it made GTK 2 porting easier.
1303  *
1304  *\param        list_store Store to operate on
1305  *\param        row -1 to add a new rule to store, else change an existing
1306  *              row
1307  *\param        enabled TRUE if rule is enabled
1308  *\param        name The Name of rule
1309  *\param        account_id The account ID
1310  *\param        account_name The account name or All or (New)
1311  *\param        rule String representation of rule
1312  *\param        prop TRUE if valid filtering rule; if FALSE it's the first
1313  *              entry in the store ("(New)").
1314  *
1315  *\return       int Row of inserted / changed rule.
1316  */
1317 static gint prefs_filtering_list_view_insert_rule(GtkListStore *list_store,
1318                                                   gint row,
1319                                                   gboolean enabled,
1320                                                   const gchar *name,
1321                                                   gint account_id,
1322                                                   const gchar *account_name,
1323                                                   const gchar *rule,
1324                                                   gboolean prop) 
1325 {
1326         GtkTreeIter iter;
1327
1328         /* check if valid row at all */
1329         if (row >= 0) {
1330                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1331                                                    &iter, NULL, row))
1332                         row = -1;                                                  
1333         }
1334
1335         if (row < 0) {
1336                 /* append new */
1337                 gtk_list_store_append(list_store, &iter);
1338                 gtk_list_store_set(list_store, &iter, 
1339                                    PREFS_FILTERING_ENABLED, enabled,
1340                                    PREFS_FILTERING_NAME, name,
1341                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1342                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1343                                    PREFS_FILTERING_RULE, rule,
1344                                    PREFS_FILTERING_PROP, prop,
1345                                    -1);
1346                 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store),
1347                                                       NULL) - 1;
1348         } else {
1349                 /* change existing */
1350                 gtk_list_store_set(list_store, &iter, 
1351                                    PREFS_FILTERING_ENABLED, enabled,
1352                                    PREFS_FILTERING_NAME, name,
1353                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1354                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1355                                    PREFS_FILTERING_RULE, rule,
1356                                    -1);
1357                 return row;                                
1358         }
1359 }
1360
1361 /*!
1362  *\return       gchar * Rule at specified row - should be freed.
1363  */
1364 static gchar *prefs_filtering_list_view_get_rule(GtkWidget *list, gint row)
1365 {       
1366         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1367         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1368         GtkTreeIter iter;
1369         gchar *result;
1370
1371         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1372                 return NULL;
1373         
1374         gtk_tree_model_get(model, &iter, 
1375                            PREFS_FILTERING_RULE, &result,
1376                            -1);
1377         
1378         return result;
1379 }
1380
1381 static void prefs_filtering_list_view_get_rule_name(GtkWidget *list, gint row,
1382                                 gboolean *enabled, gchar **name, gint *account_id)
1383 {       
1384         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1385         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1386         GtkTreeIter iter;
1387
1388         *enabled = TRUE;
1389         *name = NULL;
1390
1391         if (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1392                 gtk_tree_model_get(model, &iter, 
1393                                    PREFS_FILTERING_ENABLED, enabled,
1394                                    PREFS_FILTERING_NAME, name,
1395                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1396                                    -1);
1397         }
1398 }
1399
1400 /*!
1401  *\brief        Create list view for filtering
1402  */
1403 static GtkWidget *prefs_filtering_list_view_create(void)
1404 {
1405         GtkTreeView *list_view;
1406         GtkTreeSelection *selector;
1407
1408         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL
1409                 (prefs_filtering_create_data_store())));
1410         
1411         gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
1412         gtk_tree_view_set_reorderable(list_view, TRUE);
1413         
1414         selector = gtk_tree_view_get_selection(list_view);
1415         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1416         gtk_tree_selection_set_select_function(selector, prefs_filtering_selected,
1417                                                NULL, NULL);
1418
1419         /* create the columns */
1420         prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view));
1421
1422         return GTK_WIDGET(list_view);
1423 }
1424
1425 static void prefs_filtering_enable_toggled(GtkCellRendererToggle *widget,
1426                 gchar *path,
1427                 GtkWidget *list_view)
1428 {
1429         GtkTreeIter iter;
1430         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1431         gboolean enabled = TRUE;
1432
1433         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1434                 return;
1435
1436         gtk_tree_model_get(model, &iter,
1437                            PREFS_FILTERING_ENABLED, &enabled,
1438                            -1);
1439
1440         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1441                            PREFS_FILTERING_ENABLED, !enabled,
1442                            -1);
1443 }
1444
1445 static void prefs_filtering_create_list_view_columns(GtkWidget *list_view)
1446 {
1447         GtkTreeViewColumn *column;
1448         GtkCellRenderer *renderer;
1449
1450         renderer = gtk_cell_renderer_toggle_new();
1451         g_object_set(renderer,
1452                      "radio", FALSE,
1453                      "activatable", TRUE,
1454                      NULL);
1455         column = gtk_tree_view_column_new_with_attributes
1456                 (_("Enable"), /* FIXME : Enable, Enabled, or 'E' ? */
1457                  renderer,
1458                  "active", PREFS_FILTERING_ENABLED,
1459                  NULL);
1460         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1461         g_signal_connect(G_OBJECT(renderer), "toggled",
1462                          G_CALLBACK(prefs_filtering_enable_toggled),
1463                          list_view);
1464
1465         renderer = gtk_cell_renderer_text_new();
1466         column = gtk_tree_view_column_new_with_attributes
1467                 (_("Name"),
1468                  renderer,
1469                  "text", PREFS_FILTERING_NAME,
1470                  NULL);
1471         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1472         gtk_tree_view_column_set_resizable(column, TRUE);
1473
1474         renderer = gtk_cell_renderer_text_new();
1475         column = gtk_tree_view_column_new_with_attributes
1476                 (_("Account"),
1477                  renderer,
1478                  "text", PREFS_FILTERING_ACCOUNT_NAME,
1479                  NULL);
1480         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1481         gtk_tree_view_column_set_resizable(column, TRUE);
1482
1483         filtering.account_name_column = column;
1484                 
1485         renderer = gtk_cell_renderer_text_new();
1486         column = gtk_tree_view_column_new_with_attributes
1487                 (_("Rule"),
1488                  renderer,
1489                  "text", PREFS_FILTERING_RULE,
1490                  NULL);
1491         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1492 }
1493
1494 static gboolean gtkut_tree_iter_comp(GtkTreeModel *model, 
1495                                      GtkTreeIter *iter1, 
1496                                      GtkTreeIter *iter2)
1497 {
1498         GtkTreePath *path1 = gtk_tree_model_get_path(model, iter1);
1499         GtkTreePath *path2 = gtk_tree_model_get_path(model, iter2);
1500         gboolean result;
1501
1502         result = gtk_tree_path_compare(path1, path2) == 0;
1503
1504         gtk_tree_path_free(path1);
1505         gtk_tree_path_free(path2);
1506         
1507         return result;
1508 }
1509
1510 /*!
1511  *\brief        Get selected row number.
1512  */
1513 static gint prefs_filtering_get_selected_row(GtkWidget *list_view)
1514 {
1515         GtkTreeView *view = GTK_TREE_VIEW(list_view);
1516         GtkTreeModel *model = gtk_tree_view_get_model(view);
1517         int n_rows = gtk_tree_model_iter_n_children(model, NULL);
1518         GtkTreeSelection *selection;
1519         GtkTreeIter iter;
1520         int row;
1521
1522         if (n_rows == 0) 
1523                 return -1;
1524         
1525         selection = gtk_tree_view_get_selection(view);
1526         if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1527                 return -1;
1528         
1529         /* get all iterators and compare them... */
1530         for (row = 0; row < n_rows; row++) {
1531                 GtkTreeIter itern;
1532
1533                 gtk_tree_model_iter_nth_child(model, &itern, NULL, row);
1534                 if (gtkut_tree_iter_comp(model, &iter, &itern))
1535                         return row;
1536         }
1537         
1538         return -1;
1539 }
1540
1541 static gboolean prefs_filtering_list_view_select_row(GtkWidget *list, gint row)
1542 {
1543         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1544         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
1545         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1546         GtkTreeIter iter;
1547         GtkTreePath *path;
1548
1549         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1550                 return FALSE;
1551         
1552         gtk_tree_selection_select_iter(selection, &iter);
1553
1554         path = gtk_tree_model_get_path(model, &iter);
1555         gtk_tree_view_set_cursor(list_view, path, NULL, FALSE);
1556         gtk_tree_path_free(path);
1557         
1558         return TRUE;
1559 }
1560
1561 /*!
1562  *\brief        Triggered when a row is selected
1563  */
1564 static gboolean prefs_filtering_selected(GtkTreeSelection *selector,
1565                                          GtkTreeModel *model, 
1566                                          GtkTreePath *path,
1567                                          gboolean currently_selected,
1568                                          gpointer data)
1569 {
1570         if (currently_selected)
1571                 return TRUE;
1572         else {          
1573                 gboolean has_prop  = FALSE;
1574                 GtkTreeIter iter;
1575
1576                 gtk_tree_model_get_iter(model, &iter, path);
1577                 gtk_tree_model_get(model, &iter,
1578                                    PREFS_FILTERING_PROP, &has_prop,
1579                                    -1);
1580
1581                 if (has_prop) {
1582                         FilteringProp *prop;
1583                         gchar *filtering_str = NULL;
1584                         gchar *name = NULL;
1585                         gint account_id = 0;
1586
1587                         gtk_tree_model_get(model, &iter,
1588                                            PREFS_FILTERING_RULE, &filtering_str,
1589                                            -1);
1590                         gtk_tree_model_get(model, &iter,
1591                                            PREFS_FILTERING_NAME, &name,
1592                                            -1);
1593                         gtk_tree_model_get(model, &iter,
1594                                            PREFS_FILTERING_ACCOUNT_ID, &account_id,
1595                                            -1);
1596
1597                         prop = matcher_parser_get_filtering(filtering_str);
1598                         if (prop) { 
1599                                 prop->name = g_strdup(name);
1600                                 prop->account_id = account_id;
1601                                 prefs_filtering_select_set(prop);
1602                                 filteringprop_free(prop);
1603                         }                               
1604                         g_free(name);
1605                         g_free(filtering_str);
1606                 } else
1607                         prefs_filtering_reset_dialog();
1608         }               
1609
1610         return TRUE;
1611 }
1612