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