2007-05-03 [wwp] 2.9.1cvs40
[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 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 "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_path_func(GNode *node, gpointer data);
121 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data);
122
123 static void delete_path(GSList ** p_filters, const gchar * path);
124
125
126 static GtkListStore* prefs_filtering_create_data_store  (void);
127 static gint prefs_filtering_list_view_insert_rule       (GtkListStore *list_store,
128                                                          gint row,
129                                                          gboolean enabled,
130                                                          const gchar *name,
131                                                          gint account_id,
132                                                          const gchar *account_name,
133                                                          const gchar *rule, 
134                                                          gboolean prop);
135 static gchar *prefs_filtering_list_view_get_rule        (GtkWidget *list, 
136                                                          gint row);
137 static void prefs_filtering_list_view_get_rule_info     (GtkWidget *list, 
138                                                          gint row,
139                                                          gboolean *enabled,
140                                                          gchar **name,
141                                                          gint *account_id);
142
143 static GtkWidget *prefs_filtering_list_view_create      (void);
144 static void prefs_filtering_create_list_view_columns    (GtkWidget *list_view);
145 static gint prefs_filtering_get_selected_row            (GtkWidget *list_view);
146 static gboolean prefs_filtering_list_view_select_row    (GtkWidget *list, gint row);
147
148 static gboolean prefs_filtering_selected                (GtkTreeSelection *selector,
149                                                          GtkTreeModel *model, 
150                                                          GtkTreePath *path,
151                                                          gboolean currently_selected,
152                                                          gpointer data);
153
154 static void prefs_filtering_account_option_menu_populate(void);
155
156 static gulong signal_id = 0; /* filtering.help_btn clicked signal */
157
158 void prefs_filtering_open(GSList ** p_processing,
159                           const gchar *title,
160                           const gchar *help_url_anchor,
161                           const gchar *header,
162                           const gchar *key,
163                           gboolean per_account_filtering)
164 {
165         if (prefs_rc_is_readonly(FILTERING_RC))
166                 return;
167
168         inc_lock();
169
170         if (!filtering.window) {
171                 prefs_filtering_create();
172         } else {
173                 gtk_list_store_clear(filtering.account_combobox_list);
174                 prefs_filtering_account_option_menu_populate();
175         }
176
177         gtk_tree_view_column_set_visible(filtering.account_name_column,
178                                                                          per_account_filtering);
179
180         manage_window_set_transient(GTK_WINDOW(filtering.window));
181         gtk_widget_grab_focus(filtering.ok_btn);
182         
183         if (title != NULL)
184                 gtk_window_set_title(GTK_WINDOW(filtering.window), title);
185         else
186                 gtk_window_set_title (GTK_WINDOW(filtering.window),
187                                       _("Filtering/Processing configuration"));
188
189         if (help_url_anchor != NULL) {
190                 if (signal_id != 0) {
191                         g_signal_handler_disconnect(
192                                         G_OBJECT(filtering.help_btn),
193                                         signal_id);
194                 }
195
196                 signal_id = g_signal_connect(G_OBJECT(filtering.help_btn),
197                                 "clicked",
198                                 G_CALLBACK(manual_open_with_anchor_cb),
199                                 (gchar*)help_url_anchor);
200         }
201         else {
202                 gtk_widget_set_sensitive(filtering.help_btn, FALSE);
203         }
204
205         p_processing_list = p_processing;
206         
207         prefs_filtering_set_dialog(header, key);
208         if (per_account_filtering) {
209                 gtk_widget_show(filtering.account_label);
210                 gtk_widget_show(filtering.account_combobox);
211         } else {
212                 gtk_widget_hide(filtering.account_label);
213                 gtk_widget_hide(filtering.account_combobox);
214                 combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
215         }
216
217         gtk_widget_show(filtering.window);
218
219         start_address_completion(NULL);
220 }
221
222 static void prefs_filtering_size_allocate_cb(GtkWidget *widget,
223                                          GtkAllocation *allocation)
224 {
225         g_return_if_fail(allocation != NULL);
226
227         prefs_common.filteringwin_width = allocation->width;
228         prefs_common.filteringwin_height = allocation->height;
229 }
230
231 /* prefs_filtering_close() - just to have one common exit point */
232 static void prefs_filtering_close(void)
233 {
234         end_address_completion();
235         
236         gtk_widget_hide(filtering.window);
237         inc_unlock();
238 }
239
240 static void prefs_filtering_account_option_menu_populate(void)
241 {
242         GList *accounts = NULL;
243         GtkTreeIter iter;
244
245         accounts = account_get_list();
246
247         g_return_if_fail(accounts != NULL);
248
249         COMBOBOX_ADD(filtering.account_combobox_list, Q_("Filtering Account Menu|All"), 0);
250         for (; accounts != NULL; accounts = accounts->next) {
251                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
252
253                 COMBOBOX_ADD_ESCAPED(filtering.account_combobox_list, ac->account_name, ac->account_id);
254         }
255 }
256
257 static GtkWidget *prefs_filtering_account_option_menu(Filtering *filtering)
258 {
259         GtkWidget *optmenu = NULL;
260         GtkWidget *optmenubox = NULL;
261         GtkListStore *menu = NULL;
262         
263         optmenubox = gtk_event_box_new();
264         optmenu = gtkut_sc_combobox_create(optmenubox, FALSE);
265         menu = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(optmenu)));
266
267         filtering->account_combobox = optmenu;
268         filtering->account_combobox_list = menu;
269
270         prefs_filtering_account_option_menu_populate();
271
272         return optmenubox;
273 }
274
275 static void prefs_filtering_create(void)
276 {
277         GtkWidget *window;
278         GtkWidget *vbox;
279         GtkWidget *help_btn;
280         GtkWidget *cancel_btn;
281         GtkWidget *ok_btn;
282         GtkWidget *confirm_area;
283
284         GtkWidget *vbox1;
285         GtkWidget *reg_hbox;
286         GtkWidget *arrow;
287         GtkWidget *btn_hbox;
288
289         GtkWidget *name_label;
290         GtkWidget *name_entry;
291         GtkWidget *account_label;
292         GtkWidget *account_opt_menu;
293         GtkWidget *cond_label;
294         GtkWidget *cond_entry;
295         GtkWidget *cond_btn;
296         GtkWidget *action_label;
297         GtkWidget *action_entry;
298         GtkWidget *action_btn;
299
300         GtkWidget *reg_btn;
301         GtkWidget *subst_btn;
302         GtkWidget *del_btn;
303
304         GtkWidget *cond_hbox;
305         GtkWidget *cond_scrolledwin;
306         GtkWidget *cond_list_view;
307
308         GtkWidget *btn_vbox;
309         GtkWidget *spc_vbox;
310         GtkWidget *top_btn;
311         GtkWidget *up_btn;
312         GtkWidget *down_btn;
313         GtkWidget *bottom_btn;
314         GtkWidget *table;
315         static GdkGeometry geometry;
316
317         debug_print("Creating filtering configuration window...\n");
318
319         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_filtering");
320         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
321         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
322         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
323         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
324
325         vbox = gtk_vbox_new (FALSE, 6);
326         gtk_widget_show (vbox);
327         gtk_container_add (GTK_CONTAINER (window), vbox);
328
329         gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
330                         &cancel_btn, GTK_STOCK_CANCEL,
331                         &ok_btn, GTK_STOCK_OK,
332                         NULL, NULL);
333         gtk_widget_show (confirm_area);
334         gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
335         gtk_widget_grab_default (ok_btn);
336
337         gtk_window_set_title (GTK_WINDOW(window),
338                                     _("Filtering/Processing configuration"));
339
340         g_signal_connect(G_OBJECT(window), "delete_event",
341                          G_CALLBACK(prefs_filtering_deleted), NULL);
342         g_signal_connect(G_OBJECT(window), "size_allocate",
343                          G_CALLBACK(prefs_filtering_size_allocate_cb), NULL);
344         g_signal_connect(G_OBJECT(window), "key_press_event",
345                          G_CALLBACK(prefs_filtering_key_pressed), NULL);
346         MANAGE_WINDOW_SIGNALS_CONNECT (window);
347         g_signal_connect(G_OBJECT(ok_btn), "clicked",
348                          G_CALLBACK(prefs_filtering_ok), NULL);
349         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
350                          G_CALLBACK(prefs_filtering_cancel), NULL);
351
352         vbox1 = gtk_vbox_new (FALSE, VSPACING);
353         gtk_widget_show (vbox1);
354         gtk_box_pack_start (GTK_BOX (vbox), vbox1, FALSE, TRUE, 0);
355         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
356
357         table = gtk_table_new(4, 3, FALSE);
358         gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
359         gtk_table_set_col_spacings (GTK_TABLE (table), 4);
360         gtk_widget_show(table);
361         gtk_box_pack_start (GTK_BOX (vbox1), table, TRUE, TRUE, 0);
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 = NULL;
772
773                 if (prop->account_id > 0) {
774                         PrefsAccount *ac_prefs = account_find_from_id(prop->account_id);
775
776                         if (ac_prefs)
777                                 account_name = ac_prefs->account_name;
778                 }
779                 if (account_name == NULL)
780                         account_name = (gchar *)Q_("Filtering Account Menu|All");
781
782                 cond_str = filteringprop_to_string(prop);
783                 subst_char(cond_str, '\t', ':');
784
785                 prefs_filtering_list_view_insert_rule(list_store, -1, 
786                                                       prop->enabled,
787                                                       prop->name,
788                                                           prop->account_id,
789                                                           account_name,
790                                                       cond_str, TRUE);
791                 
792                 g_free(cond_str);
793         }
794
795         prefs_filtering_reset_dialog();
796
797         if (header && key) {
798                 gchar * quoted_key;
799                 gchar *match_str;
800
801                 quoted_key = matcher_quote_str(key);
802                 
803                 match_str = g_strconcat(header, " ", get_matchparser_tab_str(MATCHTYPE_MATCHCASE),
804                                         " \"", quoted_key, "\"", NULL);
805                 g_free(quoted_key);
806                 
807                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), match_str);
808                 g_free(match_str);
809         }
810 }
811
812 static void prefs_filtering_reset_dialog(void)
813 {
814         gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), "");
815         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
816         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), "");
817         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), "");
818 }
819
820 static void prefs_filtering_set_list(void)
821 {
822         gint row = 1;
823         FilteringProp *prop;
824         GSList * cur;
825         gchar * filtering_str;
826         GSList * prefs_filtering;
827
828         prefs_filtering = *p_processing_list;
829         for (cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
830                 filteringprop_free((FilteringProp *) cur->data);
831         g_slist_free(prefs_filtering);
832         prefs_filtering = NULL;
833         
834
835         while (NULL != (filtering_str = prefs_filtering_list_view_get_rule
836                                                 (filtering.cond_list_view, row))) {
837                 /* FIXME: this strcmp() is bogus: "(New)" should never
838                  * be inserted in the storage */
839                 if (strcmp(filtering_str, _("(New)")) != 0) {
840                         gboolean enabled;
841                         gchar *name;
842                         gint account_id = 0;
843
844                         prefs_filtering_list_view_get_rule_info(
845                                         filtering.cond_list_view, row,
846                                         &enabled, &name, &account_id);
847                         prop = matcher_parser_get_filtering(filtering_str);
848                         g_free(filtering_str);
849                         if (prop) {
850                                 prop->enabled = enabled;
851                                 prop->name = name;
852                                 prop->account_id = account_id;
853                                 prefs_filtering = 
854                                         g_slist_append(prefs_filtering, prop);
855                         }
856                 }
857                 
858                 row++;
859         }                               
860         
861         *p_processing_list = prefs_filtering;
862 }
863
864 static gint prefs_filtering_list_view_set_row(gint row, FilteringProp * prop)
865 {
866         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
867         gchar *str;
868         GtkListStore *list_store;
869         gchar *name = NULL;
870         gint account_id = 0;
871         gchar *account_name = (gchar *)Q_("Filtering Account Menu|All");
872         gboolean enabled = TRUE;
873
874         str = filteringprop_to_string(prop);
875         if (str == NULL)
876                 return -1;
877
878         if (prop) {
879                 if (prop->name)
880                         name = prop->name;
881                 account_id = prop->account_id;
882                 if (account_id > 0)
883                         account_name = account_find_from_id(account_id)->account_name;
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, NULL);
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, NULL);
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         gboolean enabled;
1054         gchar *name;
1055         gint account_id;
1056         
1057         if (selected_row <= 0)
1058                 return;
1059
1060         prop = prefs_filtering_dialog_to_filtering(TRUE);
1061
1062         if (prop == NULL) 
1063                 return;
1064
1065         /* prop->emabled is always TRUE here, re-use the value from the selected row 
1066            as we don't substitute this value from dialog */
1067         prefs_filtering_list_view_get_rule_info(
1068                         filtering.cond_list_view, selected_row,
1069                         &enabled, &name, &account_id);
1070         prop->enabled = enabled;
1071
1072         prefs_filtering_list_view_set_row(selected_row, prop);
1073
1074         filteringprop_free(prop);
1075
1076         prefs_filtering_reset_dialog();
1077 }
1078
1079 static void prefs_filtering_delete_cb(void)
1080 {
1081         GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
1082         GtkTreeModel *model;
1083         GtkTreeIter iter;
1084         gint row;
1085         
1086         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1087         if (row <= 0) 
1088                 return; 
1089
1090         if (alertpanel(_("Delete rule"),
1091                        _("Do you really want to delete this rule?"),
1092                        GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
1093                 return;
1094
1095         model = gtk_tree_view_get_model(list_view);     
1096         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1097                 return;
1098
1099         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1100
1101         prefs_filtering_reset_dialog();
1102 }
1103
1104 static void prefs_filtering_top(void)
1105 {
1106         gint row;
1107         GtkTreeIter top, sel;
1108         GtkTreeModel *model;
1109
1110         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1111         if (row <= 1) 
1112                 return;
1113
1114         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));               
1115         
1116         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
1117         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1118                 return;
1119
1120         gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
1121         prefs_filtering_list_view_select_row(filtering.cond_list_view, 1);
1122 }
1123
1124 static void prefs_filtering_up(void)
1125 {
1126         gint row;
1127         GtkTreeIter top, sel;
1128         GtkTreeModel *model;
1129
1130         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1131         if (row <= 1) 
1132                 return;
1133                 
1134         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1135
1136         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
1137         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1138                 return;
1139
1140         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1141         prefs_filtering_list_view_select_row(filtering.cond_list_view, row - 1);
1142 }
1143
1144 static void prefs_filtering_down(void)
1145 {
1146         gint row, n_rows;
1147         GtkTreeIter top, sel;
1148         GtkTreeModel *model;
1149
1150         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1151         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1152         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1153         if (row < 1 || row >= n_rows - 1)
1154                 return;
1155
1156         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1157         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
1158                 return;
1159                         
1160         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1161         prefs_filtering_list_view_select_row(filtering.cond_list_view, row + 1);
1162 }
1163
1164 static void prefs_filtering_bottom(void)
1165 {
1166         gint row, n_rows;
1167         GtkTreeIter top, sel;
1168         GtkTreeModel *model;
1169
1170         model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));       
1171         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1172         row = prefs_filtering_get_selected_row(filtering.cond_list_view);
1173         if (row < 1 || row >= n_rows - 1)
1174                 return;
1175
1176         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1177         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
1178                 return;
1179
1180         gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);           
1181         prefs_filtering_list_view_select_row(filtering.cond_list_view, n_rows - 1);
1182 }
1183
1184 static void prefs_filtering_select_set(FilteringProp *prop)
1185 {
1186         gchar *matcher_str;
1187         gchar *action_str;
1188
1189         prefs_filtering_reset_dialog();
1190
1191         matcher_str = matcherlist_to_string(prop->matchers);
1192         if (matcher_str == NULL) {
1193                 return;
1194         }
1195         
1196         if (prop->name != NULL)
1197                 gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), prop->name);
1198
1199         combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), prop->account_id);
1200                 
1201         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), matcher_str);
1202
1203         action_str = filteringaction_list_to_string(prop->action_list);
1204         if (matcher_str == NULL) {
1205                 return;
1206         }
1207         gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), action_str);
1208
1209         g_free(action_str);
1210         g_free(matcher_str);
1211 }
1212
1213 static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
1214                                  gpointer data)
1215 {
1216         prefs_filtering_cancel();
1217         return TRUE;
1218 }
1219
1220 static gboolean prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
1221                                      gpointer data)
1222 {
1223         if (event && event->keyval == GDK_Escape) {
1224                 prefs_filtering_cancel();
1225                 return TRUE;                    
1226         }
1227         return FALSE;
1228 }
1229
1230 static void prefs_filtering_ok(void)
1231 {
1232         FilteringProp * prop;
1233         gchar * str;
1234         gchar * filtering_str;
1235         gint row = 1;
1236         AlertValue val;
1237         
1238         prop = prefs_filtering_dialog_to_filtering(FALSE);
1239         if (prop != NULL) {
1240                 str = filteringprop_to_string(prop);
1241
1242                 while (NULL != (filtering_str = (prefs_filtering_list_view_get_rule
1243                                                         (filtering.cond_list_view,
1244                                                          row)))) {
1245                         if (strcmp(filtering_str, str) == 0)
1246                                 break;
1247                         row++;
1248                         g_free(filtering_str);
1249                 }       
1250
1251                 if (!filtering_str) {
1252                         val = alertpanel(_("Entry not saved"),
1253                                  _("The entry was not saved. Close anyway?"),
1254                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1255                         if (G_ALERTDEFAULT != val) {
1256                                 g_free(filtering_str);
1257                                 g_free(str); /* fixed two leaks: huzzah! */
1258                                 filteringprop_free(prop);
1259                                 return;
1260                         }
1261                 }               
1262
1263                 g_free(filtering_str);
1264                 g_free(str);
1265                 filteringprop_free(prop); /* fixed a leak: huzzah! */
1266         } else {
1267                 gchar *name, *condition, *action;
1268                 name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
1269                 condition = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1270                 action = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1271                 if (strlen(name) || 
1272                     strlen(condition) || 
1273                     strlen(action)) {
1274                         val = alertpanel(_("Entry not saved"),
1275                                  _("The entry was not saved. Close anyway?"),
1276                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1277                         if (G_ALERTDEFAULT != val) {
1278                                 g_free(name);
1279                                 g_free(condition);
1280                                 g_free(action);
1281                                 return;
1282                         }
1283                 }
1284                 g_free(name);
1285                 g_free(condition);
1286                 g_free(action);
1287         }
1288         prefs_filtering_set_list();
1289         prefs_matcher_write_config();
1290         prefs_filtering_close();
1291 }
1292
1293 static void prefs_filtering_cancel(void)
1294 {
1295         prefs_matcher_read_config();
1296         prefs_filtering_close();
1297 }
1298
1299 static GtkListStore* prefs_filtering_create_data_store(void)
1300 {
1301         return gtk_list_store_new(N_PREFS_FILTERING_COLUMNS,
1302                                   G_TYPE_BOOLEAN,
1303                                   G_TYPE_STRING,
1304                                   G_TYPE_INT,
1305                                   G_TYPE_STRING,
1306                                   G_TYPE_STRING,
1307                                   G_TYPE_BOOLEAN,
1308                                  -1);
1309 }
1310
1311 /*!
1312  *\brief        Insert filtering rule into store. Note that we access the
1313  *              tree view / store by index, which is a bit suboptimal, but
1314  *              at least it made GTK 2 porting easier.
1315  *
1316  *\param        list_store Store to operate on
1317  *\param        row -1 to add a new rule to store, else change an existing
1318  *              row
1319  *\param        enabled TRUE if rule is enabled
1320  *\param        name The Name of rule
1321  *\param        account_id The account ID
1322  *\param        account_name The account name or All or (New)
1323  *\param        rule String representation of rule
1324  *\param        prop TRUE if valid filtering rule; if FALSE it's the first
1325  *              entry in the store ("(New)").
1326  *
1327  *\return       int Row of inserted / changed rule.
1328  */
1329 static gint prefs_filtering_list_view_insert_rule(GtkListStore *list_store,
1330                                                   gint row,
1331                                                   gboolean enabled,
1332                                                   const gchar *name,
1333                                                   gint account_id,
1334                                                   const gchar *account_name,
1335                                                   const gchar *rule,
1336                                                   gboolean prop) 
1337 {
1338         GtkTreeIter iter;
1339
1340         /* check if valid row at all */
1341         if (row >= 0) {
1342                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1343                                                    &iter, NULL, row))
1344                         row = -1;                                                  
1345         }
1346
1347         if (row < 0) {
1348                 /* append new */
1349                 gtk_list_store_append(list_store, &iter);
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                                    PREFS_FILTERING_PROP, prop,
1357                                    -1);
1358                 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store),
1359                                                       NULL) - 1;
1360         } else {
1361                 /* change existing */
1362                 gtk_list_store_set(list_store, &iter, 
1363                                    PREFS_FILTERING_ENABLED, enabled,
1364                                    PREFS_FILTERING_NAME, name,
1365                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1366                                    PREFS_FILTERING_ACCOUNT_NAME, account_name,
1367                                    PREFS_FILTERING_RULE, rule,
1368                                    -1);
1369                 return row;                                
1370         }
1371 }
1372
1373 /*!
1374  *\return       gchar * Rule at specified row - should be freed.
1375  */
1376 static gchar *prefs_filtering_list_view_get_rule(GtkWidget *list, gint row)
1377 {       
1378         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1379         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1380         GtkTreeIter iter;
1381         gchar *result;
1382
1383         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1384                 return NULL;
1385         
1386         gtk_tree_model_get(model, &iter, 
1387                            PREFS_FILTERING_RULE, &result,
1388                            -1);
1389         
1390         return result;
1391 }
1392
1393 static void prefs_filtering_list_view_get_rule_info(GtkWidget *list, gint row,
1394                                 gboolean *enabled, gchar **name, gint *account_id)
1395 {       
1396         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1397         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1398         GtkTreeIter iter;
1399
1400         *enabled = TRUE;
1401         *name = NULL;
1402
1403         if (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1404                 gtk_tree_model_get(model, &iter, 
1405                                    PREFS_FILTERING_ENABLED, enabled,
1406                                    PREFS_FILTERING_NAME, name,
1407                                    PREFS_FILTERING_ACCOUNT_ID, account_id,
1408                                    -1);
1409         }
1410 }
1411
1412 /*!
1413  *\brief        Create list view for filtering
1414  */
1415 static GtkWidget *prefs_filtering_list_view_create(void)
1416 {
1417         GtkTreeView *list_view;
1418         GtkTreeSelection *selector;
1419
1420         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL
1421                 (prefs_filtering_create_data_store())));
1422         
1423         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1424         gtk_tree_view_set_reorderable(list_view, TRUE);
1425         
1426         selector = gtk_tree_view_get_selection(list_view);
1427         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1428         gtk_tree_selection_set_select_function(selector, prefs_filtering_selected,
1429                                                NULL, NULL);
1430
1431         /* create the columns */
1432         prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view));
1433
1434         return GTK_WIDGET(list_view);
1435 }
1436
1437 static void prefs_filtering_enable_toggled(GtkCellRendererToggle *widget,
1438                 gchar *path,
1439                 GtkWidget *list_view)
1440 {
1441         GtkTreeIter iter;
1442         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1443         gboolean enabled = TRUE;
1444
1445         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1446                 return;
1447
1448         gtk_tree_model_get(model, &iter,
1449                            PREFS_FILTERING_ENABLED, &enabled,
1450                            -1);
1451
1452         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1453                            PREFS_FILTERING_ENABLED, !enabled,
1454                            -1);
1455 }
1456
1457 static void prefs_filtering_create_list_view_columns(GtkWidget *list_view)
1458 {
1459         GtkTreeViewColumn *column;
1460         GtkCellRenderer *renderer;
1461
1462         renderer = gtk_cell_renderer_toggle_new();
1463         g_object_set(renderer,
1464                      "radio", FALSE,
1465                      "activatable", TRUE,
1466                      NULL);
1467         column = gtk_tree_view_column_new_with_attributes
1468                 (_("Enable"), /* FIXME : Enable, Enabled, or 'E' ? */
1469                  renderer,
1470                  "active", PREFS_FILTERING_ENABLED,
1471                  NULL);
1472         gtk_tree_view_column_set_alignment (column, 0.5);
1473         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1474         g_signal_connect(G_OBJECT(renderer), "toggled",
1475                          G_CALLBACK(prefs_filtering_enable_toggled),
1476                          list_view);
1477
1478         renderer = gtk_cell_renderer_text_new();
1479         column = gtk_tree_view_column_new_with_attributes
1480                 (_("Name"),
1481                  renderer,
1482                  "text", PREFS_FILTERING_NAME,
1483                  NULL);
1484         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1485         gtk_tree_view_column_set_resizable(column, TRUE);
1486
1487         renderer = gtk_cell_renderer_text_new();
1488         column = gtk_tree_view_column_new_with_attributes
1489                 (_("Account"),
1490                  renderer,
1491                  "text", PREFS_FILTERING_ACCOUNT_NAME,
1492                  NULL);
1493         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1494         gtk_tree_view_column_set_resizable(column, TRUE);
1495
1496         filtering.account_name_column = column;
1497                 
1498         renderer = gtk_cell_renderer_text_new();
1499         column = gtk_tree_view_column_new_with_attributes
1500                 (_("Rule"),
1501                  renderer,
1502                  "text", PREFS_FILTERING_RULE,
1503                  NULL);
1504         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1505 }
1506
1507 static gboolean gtkut_tree_iter_comp(GtkTreeModel *model, 
1508                                      GtkTreeIter *iter1, 
1509                                      GtkTreeIter *iter2)
1510 {
1511         GtkTreePath *path1 = gtk_tree_model_get_path(model, iter1);
1512         GtkTreePath *path2 = gtk_tree_model_get_path(model, iter2);
1513         gboolean result;
1514
1515         result = gtk_tree_path_compare(path1, path2) == 0;
1516
1517         gtk_tree_path_free(path1);
1518         gtk_tree_path_free(path2);
1519         
1520         return result;
1521 }
1522
1523 /*!
1524  *\brief        Get selected row number.
1525  */
1526 static gint prefs_filtering_get_selected_row(GtkWidget *list_view)
1527 {
1528         GtkTreeView *view = GTK_TREE_VIEW(list_view);
1529         GtkTreeModel *model = gtk_tree_view_get_model(view);
1530         int n_rows = gtk_tree_model_iter_n_children(model, NULL);
1531         GtkTreeSelection *selection;
1532         GtkTreeIter iter;
1533         int row;
1534
1535         if (n_rows == 0) 
1536                 return -1;
1537         
1538         selection = gtk_tree_view_get_selection(view);
1539         if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1540                 return -1;
1541         
1542         /* get all iterators and compare them... */
1543         for (row = 0; row < n_rows; row++) {
1544                 GtkTreeIter itern;
1545
1546                 gtk_tree_model_iter_nth_child(model, &itern, NULL, row);
1547                 if (gtkut_tree_iter_comp(model, &iter, &itern))
1548                         return row;
1549         }
1550         
1551         return -1;
1552 }
1553
1554 static gboolean prefs_filtering_list_view_select_row(GtkWidget *list, gint row)
1555 {
1556         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1557         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
1558         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1559         GtkTreeIter iter;
1560         GtkTreePath *path;
1561
1562         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1563                 return FALSE;
1564         
1565         gtk_tree_selection_select_iter(selection, &iter);
1566
1567         path = gtk_tree_model_get_path(model, &iter);
1568         gtk_tree_view_set_cursor(list_view, path, NULL, FALSE);
1569         gtk_tree_path_free(path);
1570         
1571         return TRUE;
1572 }
1573
1574 /*!
1575  *\brief        Triggered when a row is selected
1576  */
1577 static gboolean prefs_filtering_selected(GtkTreeSelection *selector,
1578                                          GtkTreeModel *model, 
1579                                          GtkTreePath *path,
1580                                          gboolean currently_selected,
1581                                          gpointer data)
1582 {
1583         if (currently_selected)
1584                 return TRUE;
1585         else {          
1586                 gboolean has_prop  = FALSE;
1587                 GtkTreeIter iter;
1588
1589                 gtk_tree_model_get_iter(model, &iter, path);
1590                 gtk_tree_model_get(model, &iter,
1591                                    PREFS_FILTERING_PROP, &has_prop,
1592                                    -1);
1593
1594                 if (has_prop) {
1595                         FilteringProp *prop;
1596                         gchar *filtering_str = NULL;
1597                         gchar *name = NULL;
1598                         gint account_id = 0;
1599
1600                         gtk_tree_model_get(model, &iter,
1601                                            PREFS_FILTERING_RULE, &filtering_str,
1602                                            -1);
1603                         gtk_tree_model_get(model, &iter,
1604                                            PREFS_FILTERING_NAME, &name,
1605                                            -1);
1606                         gtk_tree_model_get(model, &iter,
1607                                            PREFS_FILTERING_ACCOUNT_ID, &account_id,
1608                                            -1);
1609
1610                         prop = matcher_parser_get_filtering(filtering_str);
1611                         if (prop) {
1612                                 prop->name = g_strdup(name);
1613                                 prop->account_id = account_id;
1614                                 prefs_filtering_select_set(prop);
1615                                 filteringprop_free(prop);
1616                         }                               
1617                         g_free(name);
1618                         g_free(filtering_str);
1619                 } else
1620                         prefs_filtering_reset_dialog();
1621         }               
1622
1623         return TRUE;
1624 }
1625