c87e5c8d3305f9d4509ea95ae74787545a78d172
[claws.git] / src / prefs_filtering_action.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto
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 <gdk/gdkkeysyms.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #include "main.h"
36 #include "prefs_gtk.h"
37 #include "prefs_filtering_action.h"
38 #include "prefs_common.h"
39 #include "mainwindow.h"
40 #include "foldersel.h"
41 #include "manage_window.h"
42 #include "inc.h"
43 #include "matcher.h"
44 #include "utils.h"
45 #include "gtkutils.h"
46 #include "alertpanel.h"
47 #include "folder.h"
48 #include "description_window.h"
49 #include "addr_compl.h"
50
51 #include "matcher_parser.h"
52 #include "colorlabel.h"
53
54 enum {
55         PFA_ACTION,
56         PFA_VALID_ACTION,
57         N_PFA_COLUMNS
58 };
59
60
61 static void prefs_filtering_action_create(void);
62 static void prefs_filtering_action_delete_cb(void);
63 static void prefs_filtering_action_substitute_cb(void);
64 static void prefs_filtering_action_register_cb(void);
65 static void prefs_filtering_action_reset_dialog(void);
66 static gboolean prefs_filtering_action_key_pressed(GtkWidget *widget,
67     GdkEventKey *event, gpointer data);
68 static void prefs_filtering_action_cancel(void);
69 static void prefs_filtering_action_ok(void);
70 static gint prefs_filtering_action_deleted(GtkWidget *widget,
71     GdkEventAny *event, gpointer data);
72 static void prefs_filtering_action_type_selection_changed(GtkList *list,
73     gpointer user_data);
74 static void prefs_filtering_action_type_select(GtkList *list,
75     GtkWidget *widget, gpointer user_data);
76 static void prefs_filtering_action_select_dest(void);
77 static void prefs_filtering_action_up(void);
78 static void prefs_filtering_action_down(void);
79 static void prefs_filtering_action_set_dialog(GSList *action_list);
80 static GSList *prefs_filtering_action_get_list(void);
81
82 static GtkListStore* prefs_filtering_action_create_data_store   (void);
83 static void prefs_filtering_action_list_view_insert_action      (GtkWidget   *list_view,
84                                                                  GtkTreeIter *row,
85                                                                  const gchar *action,
86                                                                  gboolean     is_valid);
87 static GtkWidget *prefs_filtering_action_list_view_create       (void);
88 static void prefs_filtering_action_create_list_view_columns     (GtkTreeView *list_view);
89 static gboolean prefs_filtering_actions_selected                (GtkTreeSelection *selector,
90                                                                  GtkTreeModel *model, 
91                                                                  GtkTreePath *path,
92                                                                  gboolean currently_selected,
93                                                                  gpointer data);
94
95 /*!
96  *\brief        UI data for matcher dialog
97  */
98 static struct FilteringAction_ {
99         GtkWidget *window;
100
101         GtkWidget *ok_btn;
102
103         GtkWidget *action_list_view;
104         GtkWidget *action_type_list;
105         GtkWidget *action_combo;
106         GtkWidget *account_label;
107         GtkWidget *account_list;
108         GtkWidget *account_combo;
109         GtkWidget *dest_entry;
110         GtkWidget *dest_btn;
111         GtkWidget *dest_label;
112         GtkWidget *recip_label;
113         GtkWidget *exec_label;
114         GtkWidget *exec_btn;
115         GtkWidget *color_label;
116         GtkWidget *color_optmenu;
117         GtkWidget *score_label;
118
119         gint current_action;
120 } filtering_action;
121
122
123 typedef enum Action_ {
124         ACTION_MOVE,
125         ACTION_COPY,
126         ACTION_DELETE,
127         ACTION_MARK,
128         ACTION_UNMARK,
129         ACTION_LOCK,
130         ACTION_UNLOCK,
131         ACTION_MARK_AS_READ,
132         ACTION_MARK_AS_UNREAD,
133         ACTION_FORWARD,
134         ACTION_FORWARD_AS_ATTACHMENT,
135         ACTION_REDIRECT,
136         ACTION_EXECUTE,
137         ACTION_COLOR,
138         ACTION_CHANGE_SCORE,
139         ACTION_SET_SCORE,
140         ACTION_HIDE,
141         ACTION_IGNORE,
142         ACTION_STOP,
143         /* add other action constants */
144 } Action;
145
146 static struct {
147         gchar *text;
148         Action action;
149 } action_text [] = {
150         { N_("Move"),                   ACTION_MOVE     },      
151         { N_("Copy"),                   ACTION_COPY     },
152         { N_("Delete"),                 ACTION_DELETE   },
153         { N_("Mark"),                   ACTION_MARK     },
154         { N_("Unmark"),                 ACTION_UNMARK   },
155         { N_("Lock"),                   ACTION_LOCK     },
156         { N_("Unlock"),                 ACTION_UNLOCK   },
157         { N_("Mark as read"),           ACTION_MARK_AS_READ },
158         { N_("Mark as unread"),         ACTION_MARK_AS_UNREAD },
159         { N_("Forward"),                ACTION_FORWARD  },
160         { N_("Forward as attachment"),  ACTION_FORWARD_AS_ATTACHMENT },
161         { N_("Redirect"),               ACTION_REDIRECT },
162         { N_("Execute"),                ACTION_EXECUTE  },
163         { N_("Color"),                  ACTION_COLOR    },
164         { N_("Change score"),           ACTION_CHANGE_SCORE},
165         { N_("Set score"),              ACTION_SET_SCORE},
166         { N_("Hide"),                   ACTION_HIDE     },
167         { N_("Ignore thread"),          ACTION_IGNORE   },
168         { N_("Stop filter"),            ACTION_STOP     },
169 };
170
171
172 /*!
173  *\brief        Hooks
174  */
175 static PrefsFilteringActionSignal *filtering_action_callback;
176
177 /*!
178  *\brief        Find index of list selection 
179  *
180  *\param        list GTK list widget
181  *
182  *\return       gint Selection index
183  */
184 static gint get_sel_from_list(GtkList *list)
185 {
186         gint row = 0;
187         void * sel;
188         GList * child;
189
190         if (list->selection == NULL) 
191                 return -1;
192
193         sel = list->selection->data;
194         for (child = list->children; child != NULL; child = g_list_next(child)) {
195                 if (child->data == sel)
196                         return row;
197                 row ++;
198         }
199         
200         return row;
201 }
202
203 /*!
204  *\brief        Opens the filtering action dialog with a list of actions
205  *
206  *\param        matchers List of conditions
207  *\param        cb Callback
208  *
209  */
210 void prefs_filtering_action_open(GSList *action_list,
211     PrefsFilteringActionSignal *cb)
212 {
213         inc_lock();
214
215         if (!filtering_action.window) {
216                 prefs_filtering_action_create();
217         } else {
218                 /* update color label menu */
219                 gtk_option_menu_set_menu(GTK_OPTION_MENU(filtering_action.color_optmenu),
220                                 colorlabel_create_color_menu());
221         }
222
223         manage_window_set_transient(GTK_WINDOW(filtering_action.window));
224         gtk_widget_grab_focus(filtering_action.ok_btn);
225
226         filtering_action_callback = cb;
227
228         prefs_filtering_action_set_dialog(action_list);
229
230         gtk_widget_show(filtering_action.window);
231 }
232
233 /*!
234  *\brief        Save Gtk object size to prefs dataset
235  */
236 static void prefs_filtering_action_size_allocate_cb(GtkWidget *widget,
237                                          GtkAllocation *allocation)
238 {
239         g_return_if_fail(allocation != NULL);
240
241         prefs_common.filteringactionwin_width = allocation->width;
242         prefs_common.filteringactionwin_height = allocation->height;
243 }
244
245 /*!
246  *\brief        Create the matcher dialog
247  */
248 static void prefs_filtering_action_create(void)
249 {
250         GtkWidget *window;
251         GtkWidget *vbox;
252         GtkWidget *ok_btn;
253         GtkWidget *cancel_btn;
254         GtkWidget *confirm_area;
255
256         GtkWidget *vbox1;
257
258         GtkWidget *hbox1;
259
260         GtkWidget *action_label;
261         GtkWidget *recip_label;
262         GtkWidget *action_combo;
263         GtkWidget *action_type_list;
264         GtkWidget *account_list;
265         GtkWidget *dest_label;
266         GtkWidget *exec_label;
267         GtkWidget *score_label;
268         GtkWidget *color_label;
269         GtkWidget *account_label;
270         GtkWidget *account_combo;
271         GtkWidget *dest_entry;
272         GtkWidget *dest_btn;
273         GList * cur;
274
275         GtkWidget *reg_hbox;
276         GtkWidget *btn_hbox;
277         GtkWidget *arrow;
278         GtkWidget *reg_btn;
279         GtkWidget *subst_btn;
280         GtkWidget *del_btn;
281
282         GtkWidget *action_hbox;
283         GtkWidget *action_scrolledwin;
284         GtkWidget *action_list_view;
285
286         GtkWidget *btn_vbox;
287         GtkWidget *up_btn;
288         GtkWidget *down_btn;
289
290         GtkWidget *exec_btn;
291
292         GtkWidget *color_optmenu;
293
294         GList *combo_items;
295         gint i;
296         static GdkGeometry geometry;
297
298         GList * accounts;
299
300         debug_print("Creating matcher configuration window...\n");
301
302         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_filtering_action");
303         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
304         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
305         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
306         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
307
308         vbox = gtk_vbox_new(FALSE, 6);
309         gtk_widget_show(vbox);
310         gtk_container_add(GTK_CONTAINER(window), vbox);
311
312         gtkut_stock_button_set_create(&confirm_area,
313                                       &cancel_btn, GTK_STOCK_CANCEL,
314                                       &ok_btn, GTK_STOCK_OK,
315                                       NULL, NULL);
316         gtk_widget_show(confirm_area);
317         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
318         gtk_widget_grab_default(ok_btn);
319
320         gtk_window_set_title(GTK_WINDOW(window),
321                              _("Filtering action configuration"));
322         g_signal_connect(G_OBJECT(window), "delete_event",
323                          G_CALLBACK(prefs_filtering_action_deleted), NULL);
324         g_signal_connect(G_OBJECT(window), "size_allocate",
325                          G_CALLBACK(prefs_filtering_action_size_allocate_cb), NULL);
326         g_signal_connect(G_OBJECT(window), "key_press_event",
327                          G_CALLBACK(prefs_filtering_action_key_pressed), NULL);
328         MANAGE_WINDOW_SIGNALS_CONNECT(window);
329         g_signal_connect(G_OBJECT(ok_btn), "clicked",
330                          G_CALLBACK(prefs_filtering_action_ok), NULL);
331         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
332                          G_CALLBACK(prefs_filtering_action_cancel), NULL);
333
334         vbox1 = gtk_vbox_new(FALSE, VSPACING);
335         gtk_widget_show(vbox1);
336         gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
337         gtk_container_set_border_width(GTK_CONTAINER (vbox1), 2);
338
339         /* action to be defined */
340
341         hbox1 = gtk_hbox_new (FALSE, VSPACING);
342         gtk_widget_show (hbox1);
343         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
344         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
345
346         action_label = gtk_label_new (_("Action"));
347         gtk_widget_show (action_label);
348         gtk_misc_set_alignment (GTK_MISC (action_label), 0, 0.5);
349         gtk_box_pack_start (GTK_BOX (hbox1), action_label, FALSE, FALSE, 0);
350
351         action_combo = gtk_combo_new ();
352         gtk_widget_show (action_combo);
353         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(action_combo)->entry),
354                                FALSE);
355
356         combo_items = NULL;
357         for (i = 0; i < sizeof action_text / sizeof action_text[0]; i++)
358                 combo_items = g_list_append
359                         (combo_items, (gpointer) _(action_text[i].text));
360         gtk_combo_set_popdown_strings(GTK_COMBO(action_combo), combo_items);
361
362         g_list_free(combo_items);
363
364         gtk_box_pack_start (GTK_BOX (hbox1), action_combo,
365                             TRUE, TRUE, 0);
366         action_type_list = GTK_COMBO(action_combo)->list;
367         g_signal_connect (G_OBJECT(action_type_list), "select-child",
368                           G_CALLBACK(prefs_filtering_action_type_select),
369                           NULL);
370
371         g_signal_connect(G_OBJECT(action_type_list), "selection-changed",
372                          G_CALLBACK(prefs_filtering_action_type_selection_changed),
373                          NULL);
374
375         /* accounts */
376
377         hbox1 = gtk_hbox_new (FALSE, VSPACING);
378         gtk_widget_show (vbox1);
379         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
380         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
381
382         account_label = gtk_label_new (_("Account"));
383         gtk_widget_show (account_label);
384         gtk_misc_set_alignment (GTK_MISC (account_label), 0, 0.5);
385         gtk_box_pack_start (GTK_BOX (hbox1), account_label, FALSE, FALSE, 0);
386
387         account_combo = gtk_combo_new ();
388         gtk_widget_set_size_request (account_combo, 150, -1);
389         gtk_widget_show (account_combo);
390
391         combo_items = NULL;
392         for (accounts = account_get_list() ; accounts != NULL;
393              accounts = accounts->next) {
394                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
395                 gchar *name;
396
397                 name = g_strdup_printf("%s <%s> (%s)",
398                                        ac->name, ac->address,
399                                        ac->account_name);
400                 combo_items = g_list_append(combo_items, (gpointer) name);
401         }
402
403         gtk_combo_set_popdown_strings(GTK_COMBO(account_combo), combo_items);
404
405         for(cur = g_list_first(combo_items) ; cur != NULL ;
406             cur = g_list_next(cur))
407                 g_free(cur->data);
408         g_list_free(combo_items);
409
410         gtk_box_pack_start (GTK_BOX (hbox1), account_combo,
411                             TRUE, TRUE, 0);
412         account_list = GTK_COMBO(account_combo)->list;
413         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(account_combo)->entry),
414                                FALSE);
415
416         /* destination */
417
418         hbox1 = gtk_hbox_new (FALSE, VSPACING);
419         gtk_widget_show (vbox1);
420         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
421         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
422
423         dest_label = gtk_label_new (_("Destination"));
424         gtk_widget_show (dest_label);
425         gtk_misc_set_alignment (GTK_MISC (dest_label), 0, 0.5);
426         gtk_box_pack_start (GTK_BOX (hbox1), dest_label, FALSE, FALSE, 0);
427
428         recip_label = gtk_label_new (_("Recipient"));
429         gtk_widget_show (recip_label);
430         gtk_misc_set_alignment (GTK_MISC (recip_label), 0, 0.5);
431         gtk_box_pack_start (GTK_BOX (hbox1), recip_label, FALSE, FALSE, 0);
432
433         exec_label = gtk_label_new (_("Execute"));
434         gtk_widget_show (exec_label);
435         gtk_misc_set_alignment (GTK_MISC (exec_label), 0, 0.5);
436         gtk_box_pack_start (GTK_BOX (hbox1), exec_label, FALSE, FALSE, 0);
437         
438         color_label = gtk_label_new (_("Color"));
439         gtk_widget_show(color_label);
440         gtk_misc_set_alignment(GTK_MISC(color_label), 0, 0.5);
441         gtk_box_pack_start(GTK_BOX(hbox1), color_label, FALSE, FALSE, 0);
442
443         score_label = gtk_label_new (_("Score"));
444         gtk_widget_show (score_label);
445         gtk_misc_set_alignment (GTK_MISC (score_label), 0, 0.5);
446         gtk_box_pack_start (GTK_BOX (hbox1), score_label, FALSE, FALSE, 0);
447
448         dest_entry = gtk_entry_new ();
449         gtk_widget_set_size_request (dest_entry, 150, -1);
450         gtk_widget_show (dest_entry);
451         gtk_box_pack_start (GTK_BOX (hbox1), dest_entry, TRUE, TRUE, 0);
452         
453         color_optmenu = gtk_option_menu_new();
454         gtk_option_menu_set_menu(GTK_OPTION_MENU(color_optmenu),
455                                  colorlabel_create_color_menu());
456         gtk_box_pack_start(GTK_BOX(hbox1), color_optmenu, TRUE, TRUE, 0);
457
458         dest_btn = gtk_button_new_with_label (_("Select ..."));
459         gtk_widget_show (dest_btn);
460         gtk_box_pack_start (GTK_BOX (hbox1), dest_btn, FALSE, FALSE, 0);
461         g_signal_connect (G_OBJECT (dest_btn), "clicked",
462                           G_CALLBACK(prefs_filtering_action_select_dest),
463                           NULL);
464
465 #if GTK_CHECK_VERSION(2, 8, 0)
466         exec_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
467 #else
468         exec_btn = gtk_button_new_with_label (_("Info..."));
469 #endif
470         gtk_widget_show (exec_btn);
471         gtk_box_pack_start (GTK_BOX (hbox1), exec_btn, FALSE, FALSE, 0);
472         g_signal_connect (G_OBJECT (exec_btn), "clicked",
473                           G_CALLBACK(prefs_filtering_action_exec_info),
474                           NULL);
475
476         /* register / substitute / delete */
477
478         reg_hbox = gtk_hbox_new(FALSE, 4);
479         gtk_widget_show(reg_hbox);
480         gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
481
482         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
483         gtk_widget_show(arrow);
484         gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
485         gtk_widget_set_size_request(arrow, -1, 16);
486
487         btn_hbox = gtk_hbox_new(TRUE, 4);
488         gtk_widget_show(btn_hbox);
489         gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
490
491         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
492         gtk_widget_show(reg_btn);
493         gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
494         g_signal_connect(G_OBJECT(reg_btn), "clicked",
495                          G_CALLBACK(prefs_filtering_action_register_cb), NULL);
496
497         subst_btn = gtkut_get_replace_btn(_("Replace"));
498         gtk_widget_show(subst_btn);
499         gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
500         g_signal_connect(G_OBJECT(subst_btn), "clicked",
501                          G_CALLBACK(prefs_filtering_action_substitute_cb),
502                          NULL);
503
504         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
505         gtk_widget_show(del_btn);
506         gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
507         g_signal_connect(G_OBJECT(del_btn), "clicked",
508                          G_CALLBACK(prefs_filtering_action_delete_cb), NULL);
509
510         action_hbox = gtk_hbox_new(FALSE, 8);
511         gtk_widget_show(action_hbox);
512         gtk_box_pack_start(GTK_BOX(vbox1), action_hbox, TRUE, TRUE, 0);
513
514         action_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
515         gtk_widget_show(action_scrolledwin);
516         gtk_widget_set_size_request(action_scrolledwin, -1, 150);
517         gtk_box_pack_start(GTK_BOX(action_hbox), action_scrolledwin,
518                            TRUE, TRUE, 0);
519         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(action_scrolledwin),
520                                        GTK_POLICY_AUTOMATIC,
521                                        GTK_POLICY_AUTOMATIC);
522
523         action_list_view = prefs_filtering_action_list_view_create();
524         gtk_widget_show(action_list_view);
525         gtk_container_add(GTK_CONTAINER(action_scrolledwin), action_list_view);
526
527         btn_vbox = gtk_vbox_new(FALSE, 8);
528         gtk_widget_show(btn_vbox);
529         gtk_box_pack_start(GTK_BOX(action_hbox), btn_vbox, FALSE, FALSE, 0);
530
531         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
532         gtk_widget_show(up_btn);
533         gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0);
534         g_signal_connect(G_OBJECT(up_btn), "clicked",
535                          G_CALLBACK(prefs_filtering_action_up), NULL);
536
537         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
538         gtk_widget_show(down_btn);
539         gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
540         g_signal_connect(G_OBJECT(down_btn), "clicked",
541                          G_CALLBACK(prefs_filtering_action_down), NULL);
542
543         if (!geometry.min_height) {
544                 geometry.min_width = 490;
545                 geometry.min_height = 328;
546         }
547
548         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
549                                       GDK_HINT_MIN_SIZE);
550         gtk_widget_set_size_request(window, prefs_common.filteringactionwin_width,
551                                     prefs_common.filteringactionwin_height);
552
553         gtk_widget_show_all(window);
554
555         filtering_action.window    = window;
556         filtering_action.action_type_list = action_type_list;
557         filtering_action.action_combo = action_combo;
558         filtering_action.account_label = account_label;
559         filtering_action.account_list = account_list;
560         filtering_action.account_combo = account_combo;
561         filtering_action.dest_entry = dest_entry;
562         filtering_action.dest_btn = dest_btn;
563         filtering_action.dest_label = dest_label;
564         filtering_action.recip_label = recip_label;
565         filtering_action.exec_label = exec_label;
566         filtering_action.exec_btn = exec_btn;
567         filtering_action.color_label   = color_label;
568         filtering_action.color_optmenu = color_optmenu;
569         filtering_action.score_label = score_label;
570         filtering_action.ok_btn = ok_btn;
571         filtering_action.action_list_view = action_list_view;
572 }
573
574 /*!
575  *\brief        Set the contents of a row
576  *
577  *\param        row Index of row to set
578  *\param        prop Condition to set
579  *
580  */
581 static void prefs_filtering_action_list_view_set_row(GtkTreeIter *row, 
582                                                      FilteringAction *action)
583 {
584         gchar buf[256];
585
586         if (row == NULL && action == NULL) {
587                 prefs_filtering_action_list_view_insert_action
588                         (filtering_action.action_list_view,
589                          NULL, _("(New)"), FALSE);
590                 return;
591         }                        
592
593         filteringaction_to_string(buf, sizeof buf, action);
594
595         prefs_filtering_action_list_view_insert_action
596                         (filtering_action.action_list_view,
597                          row, buf, TRUE);
598 }
599
600 /*!
601  *\brief        Initializes dialog with a set of conditions
602  *
603  *\param        matchers List of conditions
604  */
605 static void prefs_filtering_action_set_dialog(GSList *action_list)
606 {
607         GSList *cur;
608
609         gtk_list_store_clear(GTK_LIST_STORE(gtk_tree_view_get_model
610                         (GTK_TREE_VIEW(filtering_action.action_list_view))));
611
612         prefs_filtering_action_list_view_set_row(NULL, NULL);
613         if (action_list != NULL) {
614                 for (cur = action_list; cur != NULL;
615                      cur = g_slist_next(cur)) {
616                         FilteringAction *action;
617                         action = (FilteringAction *) cur->data;
618                         prefs_filtering_action_list_view_set_row(NULL, action);
619                 }
620         }
621         
622         prefs_filtering_action_reset_dialog();
623 }
624
625 /*!
626  *\brief        Converts current actions in list box in
627  *              an action list used by the filtering system.
628  *
629  *\return       GSList * List of actions.
630  */
631 static GSList *prefs_filtering_action_get_list(void)
632 {
633         gchar *action_str;
634         gboolean is_valid;
635         gint row = 1;
636         GSList *action_list;
637         GtkTreeView *list_view = GTK_TREE_VIEW(filtering_action.action_list_view);
638         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
639         GtkTreeIter iter;
640
641         action_list = NULL;
642
643         while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
644
645                 gtk_tree_model_get(model, &iter, 
646                                    PFA_ACTION, &action_str,
647                                    PFA_VALID_ACTION, &is_valid,
648                                    -1);
649
650                 if (is_valid) {                            
651                         GSList * tmp_action_list;
652                         tmp_action_list = matcher_parser_get_action_list(action_str);
653                         
654                         if (tmp_action_list == NULL) {
655                                 g_free(action_str);
656                                 break;
657                         }                               
658
659                         action_list = g_slist_concat(action_list,
660                             tmp_action_list);
661                 }
662
663                 g_free(action_str);
664                 action_str = NULL;
665                 row ++;
666                 
667         }
668
669         return action_list;
670 }
671
672 /*!
673  *\brief        Returns account ID from the given list index
674  *
675  *\return       gint account ID
676  */
677 static gint get_account_id_from_list_id(gint list_id)
678 {
679         GList * accounts;
680
681         for (accounts = account_get_list() ; accounts != NULL;
682              accounts = accounts->next) {
683                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
684
685                 if (list_id == 0)
686                         return ac->account_id;
687                 list_id--;
688         }
689         return 0;
690 }
691
692 /*!
693  *\brief        Returns list index from the given account ID
694  *
695  *\return       gint list index
696  */
697 static gint get_list_id_from_account_id(gint account_id)
698 {
699         GList * accounts;
700         gint list_id = 0;
701
702         for (accounts = account_get_list() ; accounts != NULL;
703              accounts = accounts->next) {
704                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
705
706                 if (account_id == ac->account_id)
707                         return list_id;
708                 list_id++;
709         }
710         return 0;
711 }
712
713
714 /*!
715  *\brief        Returns parser action ID from internal action ID
716  *
717  *\return       gint parser action ID
718  */
719 static gint prefs_filtering_action_get_matching_from_action(Action action_id)
720 {
721         switch (action_id) {
722         case ACTION_MOVE:
723                 return MATCHACTION_MOVE;
724         case ACTION_COPY:
725                 return MATCHACTION_COPY;
726         case ACTION_DELETE:
727                 return MATCHACTION_DELETE;
728         case ACTION_MARK:
729                 return MATCHACTION_MARK;
730         case ACTION_UNMARK:
731                 return MATCHACTION_UNMARK;
732         case ACTION_LOCK:
733                 return MATCHACTION_LOCK;
734         case ACTION_UNLOCK:
735                 return MATCHACTION_UNLOCK;
736         case ACTION_MARK_AS_READ:
737                 return MATCHACTION_MARK_AS_READ;
738         case ACTION_MARK_AS_UNREAD:
739                 return MATCHACTION_MARK_AS_UNREAD;
740         case ACTION_FORWARD:
741                 return MATCHACTION_FORWARD;
742         case ACTION_FORWARD_AS_ATTACHMENT:
743                 return MATCHACTION_FORWARD_AS_ATTACHMENT;
744         case ACTION_REDIRECT:
745                 return MATCHACTION_REDIRECT;
746         case ACTION_EXECUTE:
747                 return MATCHACTION_EXECUTE;
748         case ACTION_COLOR:
749                 return MATCHACTION_COLOR;
750         case ACTION_HIDE:
751                 return MATCHACTION_HIDE;
752         case ACTION_IGNORE:
753                 return MATCHACTION_IGNORE;
754         case ACTION_STOP:
755                 return MATCHACTION_STOP;
756         case ACTION_CHANGE_SCORE:
757                 return MATCHACTION_CHANGE_SCORE;
758         case ACTION_SET_SCORE:
759                 return MATCHACTION_SET_SCORE;
760         default:
761                 return -1;
762         }
763 }
764
765 /*!
766  *\brief        Returns action from the content of the dialog
767  *
768  *\param        alert specifies whether alert dialog boxes should be shown
769  *                or not.
770  *
771  *\return       FilteringAction * action entered in the dialog box.
772  */
773 static FilteringAction * prefs_filtering_action_dialog_to_action(gboolean alert)
774 {
775         Action action_id;
776         gint action_type;
777         gint list_id;
778         gint account_id;
779         gchar * destination = NULL;
780         gint labelcolor = 0;
781         FilteringAction * action;
782         gchar * score_str = NULL;
783         gint score;
784         
785         action_id = get_sel_from_list(GTK_LIST(filtering_action.action_type_list));
786         action_type = prefs_filtering_action_get_matching_from_action(action_id);
787         list_id = get_sel_from_list(GTK_LIST(filtering_action.account_list));
788         account_id = get_account_id_from_list_id(list_id);
789         score = 0;
790         destination = NULL;
791         
792         switch (action_id) {
793         case ACTION_MOVE:
794         case ACTION_COPY:
795         case ACTION_EXECUTE:
796                 destination = gtk_editable_get_chars(GTK_EDITABLE(filtering_action.dest_entry), 0, -1);
797                 if (*destination == '\0') {
798                         if (alert)
799                                 alertpanel_error(action_id == ACTION_EXECUTE 
800                                                  ? _("Command line not set")
801                                                  : _("Destination is not set."));
802                         g_free(destination);
803                         return NULL;
804                 }
805                 break;
806         case ACTION_FORWARD:
807         case ACTION_FORWARD_AS_ATTACHMENT:
808         case ACTION_REDIRECT:
809                 destination = gtk_editable_get_chars(GTK_EDITABLE(filtering_action.dest_entry), 0, -1);
810                 if (*destination == '\0') {
811                         if (alert)
812                                 alertpanel_error(_("Recipient is not set."));
813                         g_free(destination);
814                         return NULL;
815                 }
816                 break;
817         case ACTION_COLOR:
818                 labelcolor = colorlabel_get_color_menu_active_item(
819                         gtk_option_menu_get_menu(GTK_OPTION_MENU(filtering_action.color_optmenu)));
820                 destination = NULL;     
821                 break;
822         case ACTION_CHANGE_SCORE:
823         case ACTION_SET_SCORE:
824                 score_str = gtk_editable_get_chars(GTK_EDITABLE(filtering_action.dest_entry), 0, -1);
825                 if (*score_str == '\0') {
826                         if (alert)
827                                 alertpanel_error(_("Score is not set"));
828                         g_free(score_str);
829                         return NULL;
830                 }
831                 score = strtol(score_str, NULL, 10);
832                 break;
833         case ACTION_STOP:
834         case ACTION_HIDE:
835         case ACTION_IGNORE:
836         case ACTION_DELETE:
837         case ACTION_MARK:
838         case ACTION_UNMARK:
839         case ACTION_LOCK:
840         case ACTION_UNLOCK:
841         case ACTION_MARK_AS_READ:
842         case ACTION_MARK_AS_UNREAD:
843         default:
844                 break;
845         }
846         
847         action = filteringaction_new(action_type, account_id,
848             destination, labelcolor, score);
849         
850         g_free(destination);
851         g_free(score_str);
852         return action;
853 }
854
855 /*!
856  *\brief        Signal handler for register button
857  */
858 static void prefs_filtering_action_register_cb(void)
859 {
860         FilteringAction *action;
861         
862         action = prefs_filtering_action_dialog_to_action(TRUE);
863         if (action == NULL)
864                 return;
865
866         prefs_filtering_action_list_view_set_row(NULL, action);
867
868         filteringaction_free(action);
869         /* GTK 1 NOTE:
870          * (presumably gtk_list_select_item(), called by 
871          * prefs_filtering_action_reset_dialog() activates 
872          * what seems to be a bug. this causes any other 
873          * list items to be unselectable)
874          * prefs_filtering_action_reset_dialog(); */
875         gtk_list_select_item(GTK_LIST(filtering_action.account_list), 0);
876         gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), "");
877 }
878
879 /*!
880  *\brief        Signal handler for substitute button
881  */
882 static void prefs_filtering_action_substitute_cb(void)
883 {
884         GtkTreeView *list_view = GTK_TREE_VIEW
885                         (filtering_action.action_list_view);
886         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
887         GtkTreeModel *model;
888         gboolean is_valid;
889         GtkTreeIter row;
890         FilteringAction *action;
891
892         if (!gtk_tree_selection_get_selected(selection, &model, &row))
893                 return;
894
895         gtk_tree_model_get(model, &row, PFA_VALID_ACTION, &is_valid, -1);
896         if (!is_valid)
897                 return;
898
899         action = prefs_filtering_action_dialog_to_action(TRUE);
900         if (action == NULL)
901                 return;
902
903         prefs_filtering_action_list_view_set_row(&row, action);
904
905         filteringaction_free(action);
906
907         prefs_filtering_action_reset_dialog();
908 }
909
910 /*!
911  *\brief        Signal handler for delete button
912  */
913 static void prefs_filtering_action_delete_cb(void)
914 {
915         GtkTreeView *list_view = GTK_TREE_VIEW
916                         (filtering_action.action_list_view);
917         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
918         GtkTreeModel *model;
919         gboolean is_valid;
920         GtkTreeIter row;
921
922         if (!gtk_tree_selection_get_selected(selection, &model, &row))
923                 return;
924
925         gtk_tree_model_get(model, &row, PFA_VALID_ACTION, &is_valid, -1);
926         if (!is_valid)
927                 return;
928
929         gtk_list_store_remove(GTK_LIST_STORE(model), &row);             
930
931         prefs_filtering_action_reset_dialog();
932 }
933
934 /*!
935  *\brief        Signal handler for 'move up' button
936  */
937 static void prefs_filtering_action_up(void)
938 {
939         GtkTreePath *prev, *sel, *try;
940         GtkTreeIter isel;
941         GtkListStore *store = NULL;
942         GtkTreeModel *model = NULL;
943         GtkTreeIter iprev;
944         
945         if (!gtk_tree_selection_get_selected
946                 (gtk_tree_view_get_selection
947                         (GTK_TREE_VIEW(filtering_action.action_list_view)),
948                  &model,        
949                  &isel))
950                 return;
951         store = (GtkListStore *)model;
952         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
953         if (!sel)
954                 return;
955         
956         /* no move if we're at row 0 or 1, looks phony, but other
957          * solutions are more convoluted... */
958         try = gtk_tree_path_copy(sel);
959         if (!gtk_tree_path_prev(try) || !gtk_tree_path_prev(try)) {
960                 gtk_tree_path_free(try);
961                 gtk_tree_path_free(sel);
962                 return;
963         }
964         gtk_tree_path_free(try);
965
966         prev = gtk_tree_path_copy(sel);         
967         if (gtk_tree_path_prev(prev)) {
968                 gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
969                                         &iprev, prev);
970                 gtk_list_store_swap(store, &iprev, &isel);
971                 /* XXX: GTK2 select row?? */
972         }
973
974         gtk_tree_path_free(sel);
975         gtk_tree_path_free(prev);
976 }
977
978 /*!
979  *\brief        Signal handler for 'move down' button
980  */
981 static void prefs_filtering_action_down(void)
982 {
983         GtkListStore *store = NULL;
984         GtkTreeModel *model = NULL;
985         GtkTreeIter next, sel;
986         GtkTreePath *try;
987         
988         if (!gtk_tree_selection_get_selected
989                 (gtk_tree_view_get_selection
990                         (GTK_TREE_VIEW(filtering_action.action_list_view)),
991                  &model,
992                  &sel))
993                 return;
994         store = (GtkListStore *)model;
995         try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
996         if (!try) 
997                 return;
998         
999         /* move when not at row 0 ... */
1000         if (gtk_tree_path_prev(try)) {
1001                 next = sel;
1002                 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next))
1003                         gtk_list_store_swap(store, &next, &sel);
1004         }
1005                 
1006         gtk_tree_path_free(try);
1007 }
1008
1009 /*!
1010  *\brief        Handle key press
1011  *
1012  *\param        widget Widget receiving key press
1013  *\param        event Key event
1014  *\param        data User data
1015  */
1016 static gboolean prefs_filtering_action_key_pressed(GtkWidget *widget,
1017     GdkEventKey *event, gpointer data)
1018 {
1019         if (event && event->keyval == GDK_Escape) {
1020                 prefs_filtering_action_cancel();
1021                 return TRUE;            
1022         }
1023         return FALSE;
1024 }
1025
1026 /*!
1027  *\brief        Cancel matcher dialog
1028  */
1029 static void prefs_filtering_action_cancel(void)
1030 {
1031         gtk_widget_hide(filtering_action.window);
1032         inc_unlock();
1033 }
1034
1035 /*!
1036  *\brief        Accept current matchers
1037  */
1038 static void prefs_filtering_action_ok(void)
1039 {
1040         GSList * action_list;
1041         GSList * cur;
1042
1043         action_list = prefs_filtering_action_get_list();
1044
1045         if (action_list == NULL) {
1046                 alertpanel_error(_("No action was defined."));
1047                 return;
1048         }
1049
1050         if (filtering_action_callback != NULL)
1051                 filtering_action_callback(action_list);
1052         for(cur = action_list ; cur != NULL ; cur = cur->next) {
1053                 filteringaction_free(cur->data);
1054         }
1055         g_slist_free(action_list);
1056
1057         gtk_widget_hide(filtering_action.window);
1058         inc_unlock();
1059 }
1060
1061 /*!
1062  *\brief        Called when closing dialog box
1063  *
1064  *\param        widget Dialog widget
1065  *\param        event Event info
1066  *\param        data User data
1067  *
1068  *\return       gint TRUE
1069  */
1070 static gint prefs_filtering_action_deleted(GtkWidget *widget,
1071     GdkEventAny *event, gpointer data)
1072 {
1073         prefs_filtering_action_cancel();
1074         return TRUE;
1075 }
1076
1077 /*
1078  * Strings describing exec format strings
1079  * 
1080  * When adding new lines, remember to put 2 strings for each line
1081  */
1082 static gchar *exec_desc_strings[] = {
1083         "%%",   N_("literal %"),
1084         "%s",   N_("Subject"),
1085         "%f",   N_("From"),
1086         "%t",   N_("To"),
1087         "%c",   N_("Cc"),
1088         "%d",   N_("Date"),
1089         "%i",   N_("Message-ID"),
1090         "%n",   N_("Newsgroups"),
1091         "%r",   N_("References"),
1092         "%F",   N_("filename (should not be modified)"),
1093         "\\n",  N_("new line"),
1094         "\\",   N_("escape character for quotes"),
1095         "\\\"", N_("quote character"),
1096         NULL, NULL
1097 };
1098
1099 static DescriptionWindow exec_desc_win = { 
1100         NULL,
1101         NULL, 
1102         2,
1103         N_("Filtering Action: 'Execute'"),
1104         N_("'Execute' allows you to send a message or message element "
1105            "to an external program or script.\n\n"
1106            "The following symbols can be used:"),
1107        exec_desc_strings
1108 };
1109
1110 /*!
1111  *\brief        Show Execute action's info
1112  */
1113 void prefs_filtering_action_exec_info(void)
1114 {
1115         description_window_create(&exec_desc_win);
1116 }
1117
1118 static void prefs_filtering_action_select_dest(void)
1119 {
1120         FolderItem *dest;
1121         gchar * path;
1122
1123         dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL);
1124         if (!dest) return;
1125
1126         path = folder_item_get_identifier(dest);
1127
1128         gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), path);
1129         g_free(path);
1130 }
1131
1132 static void prefs_filtering_action_type_selection_changed(GtkList *list,
1133     gpointer user_data)
1134 {
1135         gint value;
1136
1137         value = get_sel_from_list(GTK_LIST(filtering_action.action_type_list));
1138
1139         if (filtering_action.current_action != value) {
1140                 if (filtering_action.current_action == ACTION_FORWARD 
1141                 ||  filtering_action.current_action == ACTION_FORWARD_AS_ATTACHMENT
1142                 ||  filtering_action.current_action == ACTION_REDIRECT) {
1143                         debug_print("unregistering address completion entry\n");
1144                         address_completion_unregister_entry(GTK_ENTRY(filtering_action.dest_entry));
1145                         address_completion_end(filtering_action.window);
1146                 }
1147                 if (value == ACTION_FORWARD || value == ACTION_FORWARD_AS_ATTACHMENT
1148                 ||  value == ACTION_REDIRECT) {
1149                         debug_print("registering address completion entry\n");
1150                         address_completion_start(filtering_action.window);
1151                         address_completion_register_entry(
1152                                         GTK_ENTRY(filtering_action.dest_entry),
1153                                         TRUE);
1154                 }
1155                 filtering_action.current_action = value;
1156         }
1157 }
1158
1159 static void prefs_filtering_action_type_select(GtkList *list,
1160     GtkWidget *widget, gpointer user_data)
1161 {
1162         Action value;
1163
1164         value = (Action) get_sel_from_list(GTK_LIST(filtering_action.action_type_list));
1165
1166         switch (value) {
1167         case ACTION_MOVE:
1168                 gtk_widget_show(filtering_action.account_label);
1169                 gtk_widget_set_sensitive(filtering_action.account_label, FALSE);
1170                 gtk_widget_set_sensitive(filtering_action.account_combo, FALSE);
1171                 gtk_widget_show(filtering_action.dest_entry);
1172                 gtk_widget_set_sensitive(filtering_action.dest_entry, TRUE);
1173                 gtk_widget_show(filtering_action.dest_btn);
1174                 gtk_widget_set_sensitive(filtering_action.dest_btn, TRUE);
1175                 gtk_widget_show(filtering_action.dest_label);
1176                 gtk_widget_set_sensitive(filtering_action.dest_label, TRUE);
1177                 gtk_widget_hide(filtering_action.recip_label);
1178                 gtk_widget_hide(filtering_action.exec_label);
1179                 gtk_widget_hide(filtering_action.exec_btn);
1180                 gtk_widget_hide(filtering_action.color_optmenu);
1181                 gtk_widget_hide(filtering_action.color_label);
1182                 gtk_widget_hide(filtering_action.score_label);
1183                 break;
1184         case ACTION_COPY:
1185                 gtk_widget_show(filtering_action.account_label);
1186                 gtk_widget_set_sensitive(filtering_action.account_label, FALSE);
1187                 gtk_widget_set_sensitive(filtering_action.account_combo, FALSE);
1188                 gtk_widget_show(filtering_action.dest_entry);
1189                 gtk_widget_set_sensitive(filtering_action.dest_entry, TRUE);
1190                 gtk_widget_show(filtering_action.dest_btn);
1191                 gtk_widget_set_sensitive(filtering_action.dest_btn, TRUE);
1192                 gtk_widget_show(filtering_action.dest_label);
1193                 gtk_widget_set_sensitive(filtering_action.dest_label, TRUE);
1194                 gtk_widget_hide(filtering_action.recip_label);
1195                 gtk_widget_hide(filtering_action.exec_label);
1196                 gtk_widget_hide(filtering_action.exec_btn);
1197                 gtk_widget_hide(filtering_action.color_optmenu);
1198                 gtk_widget_hide(filtering_action.color_label);
1199                 gtk_widget_hide(filtering_action.score_label);
1200                 break;
1201         case ACTION_DELETE:
1202                 gtk_widget_show(filtering_action.account_label);
1203                 gtk_widget_set_sensitive(filtering_action.account_label, FALSE);
1204                 gtk_widget_set_sensitive(filtering_action.account_combo, FALSE);
1205                 gtk_widget_show(filtering_action.dest_entry);
1206                 gtk_widget_set_sensitive(filtering_action.dest_entry, FALSE);
1207                 gtk_widget_show(filtering_action.dest_btn);
1208                 gtk_widget_set_sensitive(filtering_action.dest_btn, FALSE);
1209                 gtk_widget_show(filtering_action.dest_label);
1210                 gtk_widget_set_sensitive(filtering_action.dest_label, FALSE);
1211                 gtk_widget_hide(filtering_action.recip_label);
1212                 gtk_widget_hide(filtering_action.exec_label);
1213                 gtk_widget_hide(filtering_action.exec_btn);
1214                 gtk_widget_hide(filtering_action.color_optmenu);
1215                 gtk_widget_hide(filtering_action.color_label);
1216                 gtk_widget_hide(filtering_action.score_label);
1217                 break;
1218         case ACTION_MARK:
1219         case ACTION_UNMARK:
1220         case ACTION_LOCK:
1221         case ACTION_UNLOCK:
1222         case ACTION_MARK_AS_READ:
1223         case ACTION_MARK_AS_UNREAD:
1224         case ACTION_STOP:
1225         case ACTION_HIDE:
1226         case ACTION_IGNORE:
1227                 gtk_widget_show(filtering_action.account_label);
1228                 gtk_widget_set_sensitive(filtering_action.account_label, FALSE);
1229                 gtk_widget_set_sensitive(filtering_action.account_combo, FALSE);
1230                 gtk_widget_show(filtering_action.dest_entry);
1231                 gtk_widget_set_sensitive(filtering_action.dest_entry, FALSE);
1232                 gtk_widget_show(filtering_action.dest_btn);
1233                 gtk_widget_set_sensitive(filtering_action.dest_btn, FALSE);
1234                 gtk_widget_show(filtering_action.dest_label);
1235                 gtk_widget_set_sensitive(filtering_action.dest_label, FALSE);
1236                 gtk_widget_hide(filtering_action.recip_label);
1237                 gtk_widget_hide(filtering_action.exec_label);
1238                 gtk_widget_hide(filtering_action.exec_btn);
1239                 gtk_widget_hide(filtering_action.color_optmenu);
1240                 gtk_widget_hide(filtering_action.color_label);
1241                 gtk_widget_hide(filtering_action.score_label);
1242                 break;
1243         case ACTION_FORWARD:
1244                 gtk_widget_show(filtering_action.account_label);
1245                 gtk_widget_set_sensitive(filtering_action.account_label, TRUE);
1246                 gtk_widget_set_sensitive(filtering_action.account_combo, TRUE);
1247                 gtk_widget_show(filtering_action.dest_entry);
1248                 gtk_widget_set_sensitive(filtering_action.dest_entry, TRUE);
1249                 gtk_widget_show(filtering_action.dest_btn);
1250                 gtk_widget_set_sensitive(filtering_action.dest_btn, FALSE);
1251                 gtk_widget_hide(filtering_action.dest_label);
1252                 gtk_widget_show(filtering_action.recip_label);
1253                 gtk_widget_set_sensitive(filtering_action.recip_label, TRUE);
1254                 gtk_widget_hide(filtering_action.exec_label);
1255                 gtk_widget_hide(filtering_action.exec_btn);
1256                 gtk_widget_hide(filtering_action.color_optmenu);
1257                 gtk_widget_hide(filtering_action.color_label);
1258                 gtk_widget_hide(filtering_action.score_label);
1259                 break;
1260         case ACTION_FORWARD_AS_ATTACHMENT:
1261                 gtk_widget_show(filtering_action.account_label);
1262                 gtk_widget_set_sensitive(filtering_action.account_label, TRUE);
1263                 gtk_widget_set_sensitive(filtering_action.account_combo, TRUE);
1264                 gtk_widget_show(filtering_action.dest_entry);
1265                 gtk_widget_set_sensitive(filtering_action.dest_entry, TRUE);
1266                 gtk_widget_show(filtering_action.dest_btn);
1267                 gtk_widget_set_sensitive(filtering_action.dest_btn, FALSE);
1268                 gtk_widget_hide(filtering_action.dest_label);
1269                 gtk_widget_show(filtering_action.recip_label);
1270                 gtk_widget_set_sensitive(filtering_action.recip_label, TRUE);
1271                 gtk_widget_hide(filtering_action.exec_label);
1272                 gtk_widget_hide(filtering_action.exec_btn);
1273                 gtk_widget_hide(filtering_action.color_optmenu);
1274                 gtk_widget_hide(filtering_action.color_label);
1275                 gtk_widget_hide(filtering_action.score_label);
1276                 break;
1277         case ACTION_REDIRECT:
1278                 gtk_widget_show(filtering_action.account_label);
1279                 gtk_widget_set_sensitive(filtering_action.account_label, TRUE);
1280                 gtk_widget_set_sensitive(filtering_action.account_combo, TRUE);
1281                 gtk_widget_show(filtering_action.dest_entry);
1282                 gtk_widget_set_sensitive(filtering_action.dest_entry, TRUE);
1283                 gtk_widget_show(filtering_action.dest_btn);
1284                 gtk_widget_set_sensitive(filtering_action.dest_btn, FALSE);
1285                 gtk_widget_hide(filtering_action.dest_label);
1286                 gtk_widget_show(filtering_action.recip_label);
1287                 gtk_widget_set_sensitive(filtering_action.recip_label, TRUE);
1288                 gtk_widget_hide(filtering_action.exec_label);
1289                 gtk_widget_hide(filtering_action.exec_btn);
1290                 gtk_widget_hide(filtering_action.color_optmenu);
1291                 gtk_widget_hide(filtering_action.color_label);
1292                 gtk_widget_hide(filtering_action.score_label);
1293                 break;
1294         case ACTION_EXECUTE:
1295                 gtk_widget_show(filtering_action.account_label);
1296                 gtk_widget_set_sensitive(filtering_action.account_label, FALSE);
1297                 gtk_widget_set_sensitive(filtering_action.account_combo, FALSE);
1298                 gtk_widget_show(filtering_action.dest_entry);
1299                 gtk_widget_set_sensitive(filtering_action.dest_entry, TRUE);
1300                 gtk_widget_hide(filtering_action.dest_btn);
1301                 gtk_widget_hide(filtering_action.dest_label);
1302                 gtk_widget_hide(filtering_action.recip_label);
1303                 gtk_widget_show(filtering_action.exec_label);
1304                 gtk_widget_set_sensitive(filtering_action.exec_btn, TRUE);
1305                 gtk_widget_show(filtering_action.exec_btn);
1306                 gtk_widget_hide(filtering_action.color_optmenu);
1307                 gtk_widget_hide(filtering_action.color_label);
1308                 gtk_widget_hide(filtering_action.score_label);
1309                 break;
1310         case ACTION_COLOR:
1311                 gtk_widget_show(filtering_action.account_label);
1312                 gtk_widget_set_sensitive(filtering_action.account_label, FALSE);
1313                 gtk_widget_set_sensitive(filtering_action.account_combo, FALSE);
1314                 gtk_widget_hide(filtering_action.dest_entry);
1315                 gtk_widget_hide(filtering_action.dest_btn);
1316                 gtk_widget_hide(filtering_action.dest_label);
1317                 gtk_widget_hide(filtering_action.recip_label);
1318                 gtk_widget_hide(filtering_action.exec_label);
1319                 gtk_widget_show(filtering_action.exec_btn);
1320                 gtk_widget_set_sensitive(filtering_action.exec_btn, FALSE);
1321                 gtk_widget_show(filtering_action.color_optmenu);
1322                 gtk_widget_show(filtering_action.color_label);
1323                 gtk_widget_hide(filtering_action.score_label);
1324                 break;
1325         case ACTION_CHANGE_SCORE:
1326         case ACTION_SET_SCORE:
1327                 gtk_widget_show(filtering_action.account_label);
1328                 gtk_widget_set_sensitive(filtering_action.account_label, FALSE);
1329                 gtk_widget_set_sensitive(filtering_action.account_combo, FALSE);
1330                 gtk_widget_show(filtering_action.dest_entry);
1331                 gtk_widget_set_sensitive(filtering_action.dest_entry, TRUE);
1332                 gtk_widget_hide(filtering_action.dest_btn);
1333                 gtk_widget_hide(filtering_action.dest_label);
1334                 gtk_widget_hide(filtering_action.recip_label);
1335                 gtk_widget_hide(filtering_action.exec_label);
1336                 gtk_widget_show(filtering_action.exec_btn);
1337                 gtk_widget_set_sensitive(filtering_action.exec_btn, FALSE);
1338                 gtk_widget_hide(filtering_action.color_optmenu);
1339                 gtk_widget_hide(filtering_action.color_label);
1340                 gtk_widget_show(filtering_action.score_label);
1341                 break;
1342         }
1343 }
1344
1345 static void prefs_filtering_action_reset_dialog(void)
1346 {
1347         gtk_list_select_item(GTK_LIST(filtering_action.action_type_list), 0);
1348         gtk_list_select_item(GTK_LIST(filtering_action.account_list), 0);
1349         gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), "");
1350 }
1351
1352 static GtkListStore* prefs_filtering_action_create_data_store(void)
1353 {
1354         return gtk_list_store_new(N_PFA_COLUMNS,
1355                                   G_TYPE_STRING,
1356                                   G_TYPE_BOOLEAN,
1357                                   -1);
1358 }
1359
1360 static void prefs_filtering_action_list_view_insert_action(GtkWidget   *list_view,
1361                                                            GtkTreeIter *row,
1362                                                            const gchar *action,
1363                                                            gboolean     is_valid)
1364 {
1365         GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model
1366                                         (GTK_TREE_VIEW(list_view)));
1367         GtkTreeIter iter;
1368         
1369         
1370         /* see if row exists, if not append */
1371         if (row == NULL)
1372                 gtk_list_store_append(store, &iter);
1373         else
1374                 iter = *row;
1375
1376         gtk_list_store_set(store, &iter,
1377                            PFA_ACTION, action,
1378                            PFA_VALID_ACTION, is_valid,
1379                            -1);
1380 }
1381
1382 static GtkWidget *prefs_filtering_action_list_view_create(void)
1383 {
1384         GtkTreeView *list_view;
1385         GtkTreeModel *model;
1386         GtkTreeSelection *selector;
1387
1388         model = GTK_TREE_MODEL(prefs_filtering_action_create_data_store());
1389         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1390         g_object_unref(model);  
1391         
1392         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1393
1394         selector = gtk_tree_view_get_selection(list_view);
1395         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1396         gtk_tree_selection_set_select_function
1397                 (selector, prefs_filtering_actions_selected, NULL, NULL);
1398         
1399         /* create the columns */
1400         prefs_filtering_action_create_list_view_columns(list_view);
1401
1402         return GTK_WIDGET(list_view);
1403 }
1404
1405 static void prefs_filtering_action_create_list_view_columns(GtkTreeView *list_view)
1406 {
1407         GtkTreeViewColumn *column;
1408         GtkCellRenderer *renderer;
1409
1410         renderer = gtk_cell_renderer_text_new();
1411         column = gtk_tree_view_column_new_with_attributes
1412                 (_("Current action list"),
1413                  renderer,
1414                  "text", PFA_ACTION,
1415                  NULL);
1416         gtk_tree_view_append_column(list_view, column);         
1417 }
1418
1419 static gboolean prefs_filtering_actions_selected
1420                         (GtkTreeSelection *selector,
1421                          GtkTreeModel *model, 
1422                          GtkTreePath *path,
1423                          gboolean currently_selected,
1424                          gpointer data)
1425 {
1426         gchar *action_str;
1427         FilteringAction *action;
1428         GSList * action_list;
1429         gint list_id;
1430         GtkTreeIter iter;
1431         gboolean is_valid;
1432
1433         if (currently_selected)
1434                 return TRUE;
1435
1436         if (!gtk_tree_model_get_iter(model, &iter, path))
1437                 return TRUE;
1438
1439         gtk_tree_model_get(model, &iter, 
1440                            PFA_VALID_ACTION,  &is_valid,
1441                            -1);
1442
1443         if (!is_valid) {
1444                 prefs_filtering_action_reset_dialog();
1445                 return TRUE;
1446         }
1447
1448         gtk_tree_model_get(model, &iter, 
1449                            PFA_ACTION, &action_str,
1450                            -1);
1451
1452         action_list = matcher_parser_get_action_list(action_str);
1453         g_free(action_str);
1454
1455         if (action_list == NULL)
1456                 return TRUE;
1457
1458         action = action_list->data;
1459         g_slist_free(action_list);
1460
1461         if (action->destination)
1462                 gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), action->destination);
1463         else
1464                 gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), "");
1465
1466         switch(action->type) {
1467         case MATCHACTION_MOVE:
1468                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1469                                      ACTION_MOVE);
1470                 break;
1471         case MATCHACTION_COPY:
1472                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1473                                      ACTION_COPY);
1474                 break;
1475         case MATCHACTION_DELETE:
1476                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1477                                      ACTION_DELETE);
1478                 break;
1479         case MATCHACTION_MARK:
1480                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1481                                      ACTION_MARK);
1482                 break;
1483         case MATCHACTION_UNMARK:
1484                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1485                                      ACTION_UNMARK);
1486                 break;
1487         case MATCHACTION_LOCK:
1488                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1489                                      ACTION_LOCK);
1490                 break;
1491         case MATCHACTION_UNLOCK:
1492                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1493                                      ACTION_UNLOCK);
1494                 break;
1495         case MATCHACTION_MARK_AS_READ:
1496                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1497                                      ACTION_MARK_AS_READ);
1498                 break;
1499         case MATCHACTION_MARK_AS_UNREAD:
1500                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1501                                      ACTION_MARK_AS_UNREAD);
1502                 break;
1503         case MATCHACTION_FORWARD:
1504                 list_id = get_list_id_from_account_id(action->account_id);
1505                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1506                                      ACTION_FORWARD);
1507                 gtk_list_select_item(GTK_LIST(filtering_action.account_list),
1508                                      list_id);
1509                 break;
1510         case MATCHACTION_FORWARD_AS_ATTACHMENT:
1511                 list_id = get_list_id_from_account_id(action->account_id);
1512                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1513                                      ACTION_FORWARD_AS_ATTACHMENT);
1514                 gtk_list_select_item(GTK_LIST(filtering_action.account_list),
1515                                      list_id);
1516                 break;
1517         case MATCHACTION_REDIRECT:
1518                 list_id = get_list_id_from_account_id(action->account_id);
1519                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1520                                      ACTION_REDIRECT);
1521                 gtk_list_select_item(GTK_LIST(filtering_action.account_list),
1522                                      list_id);
1523                 break;
1524         case MATCHACTION_EXECUTE:
1525                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1526                                      ACTION_EXECUTE);
1527                 break;
1528         case MATCHACTION_COLOR:
1529                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1530                                      ACTION_COLOR);
1531                 gtk_option_menu_set_history(GTK_OPTION_MENU(filtering_action.color_optmenu), action->labelcolor);     
1532                 break;
1533         case MATCHACTION_CHANGE_SCORE:
1534                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1535                                      ACTION_CHANGE_SCORE);
1536                 break;
1537         case MATCHACTION_SET_SCORE:
1538                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1539                                      ACTION_SET_SCORE);
1540                 break;
1541         case MATCHACTION_STOP:
1542                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1543                                      ACTION_STOP);
1544                 break;
1545         case MATCHACTION_HIDE:
1546                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1547                                      ACTION_HIDE);
1548                 break;
1549         case MATCHACTION_IGNORE:
1550                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1551                                      ACTION_IGNORE);
1552                 break;
1553         }
1554
1555         filteringaction_free(action); /* XXX: memleak */
1556         return TRUE;
1557 }
1558