2007-10-10 [paul] 3.0.2cvs54
[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 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <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 #include "tags.h"
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_select_addressbook(void);
78 static void prefs_filtering_action_up(void);
79 static void prefs_filtering_action_down(void);
80 static void prefs_filtering_action_set_dialog(GSList *action_list);
81 static GSList *prefs_filtering_action_get_list(void);
82
83 static GtkListStore* prefs_filtering_action_create_data_store   (void);
84 static void prefs_filtering_action_list_view_insert_action      (GtkWidget   *list_view,
85                                                                  GtkTreeIter *row,
86                                                                  const gchar *action,
87                                                                  gboolean     is_valid);
88 static GtkWidget *prefs_filtering_action_list_view_create       (void);
89 static void prefs_filtering_action_create_list_view_columns     (GtkTreeView *list_view);
90 static gboolean prefs_filtering_actions_selected                (GtkTreeSelection *selector,
91                                                                  GtkTreeModel *model, 
92                                                                  GtkTreePath *path,
93                                                                  gboolean currently_selected,
94                                                                  gpointer data);
95
96 /*!
97  *\brief        UI data for matcher dialog
98  */
99 static struct FilteringAction_ {
100         GtkWidget *window;
101
102         GtkWidget *ok_btn;
103
104         GtkWidget *action_list_view;
105         GtkWidget *action_type_list;
106         GtkWidget *action_combo;
107         GtkWidget *account_label;
108         GtkWidget *account_list;
109         GtkWidget *account_combo;
110         GtkWidget *dest_entry;
111         GtkWidget *dest_btn;
112         GtkWidget *dest_label;
113         GtkWidget *recip_label;
114         GtkWidget *exec_label;
115         GtkWidget *exec_btn;
116         GtkWidget *color_label;
117         GtkWidget *color_optmenu;
118         GtkWidget *score_label;
119         GtkWidget *header_label;
120         GtkWidget *header_combo;
121         GtkWidget *header_entry;
122         GtkWidget *addressbook_label;
123         GtkWidget *addressbook_btn;
124         GtkWidget *tags_label;
125         GtkWidget *tags_list;
126         GtkWidget *tags_combo;
127
128         gint current_action;
129 } filtering_action;
130
131
132 typedef enum Action_ {
133         ACTION_MOVE,
134         ACTION_COPY,
135         ACTION_DELETE,
136         ACTION_MARK,
137         ACTION_UNMARK,
138         ACTION_LOCK,
139         ACTION_UNLOCK,
140         ACTION_MARK_AS_READ,
141         ACTION_MARK_AS_UNREAD,
142         ACTION_MARK_AS_SPAM,
143         ACTION_MARK_AS_HAM,
144         ACTION_FORWARD,
145         ACTION_FORWARD_AS_ATTACHMENT,
146         ACTION_REDIRECT,
147         ACTION_EXECUTE,
148         ACTION_COLOR,
149         ACTION_CHANGE_SCORE,
150         ACTION_SET_SCORE,
151         ACTION_SET_TAG,
152         ACTION_UNSET_TAG,
153         ACTION_CLEAR_TAGS,
154         ACTION_HIDE,
155         ACTION_IGNORE,
156         ACTION_WATCH,
157         ACTION_ADD_TO_ADDRESSBOOK,
158         ACTION_STOP,
159         /* add other action constants */
160 } Action;
161
162 static struct {
163         gchar *text;
164         Action action;
165 } action_text [] = {
166         { N_("Move"),                   ACTION_MOVE     },      
167         { N_("Copy"),                   ACTION_COPY     },
168         { N_("Delete"),                 ACTION_DELETE   },
169         { N_("Mark"),                   ACTION_MARK     },
170         { N_("Unmark"),                 ACTION_UNMARK   },
171         { N_("Lock"),                   ACTION_LOCK     },
172         { N_("Unlock"),                 ACTION_UNLOCK   },
173         { N_("Mark as read"),           ACTION_MARK_AS_READ },
174         { N_("Mark as unread"),         ACTION_MARK_AS_UNREAD },
175         { N_("Mark as spam"),           ACTION_MARK_AS_SPAM },
176         { N_("Mark as ham"),            ACTION_MARK_AS_HAM },
177         { N_("Forward"),                ACTION_FORWARD  },
178         { N_("Forward as attachment"),  ACTION_FORWARD_AS_ATTACHMENT },
179         { N_("Redirect"),               ACTION_REDIRECT },
180         { N_("Execute"),                ACTION_EXECUTE  },
181         { N_("Color"),                  ACTION_COLOR    },
182         { N_("Change score"),           ACTION_CHANGE_SCORE},
183         { N_("Set score"),              ACTION_SET_SCORE},
184         { N_("Apply tag"),              ACTION_SET_TAG},
185         { N_("Unset tag"),              ACTION_UNSET_TAG},
186         { N_("Clear tags"),             ACTION_CLEAR_TAGS},
187         { N_("Hide"),                   ACTION_HIDE     },
188         { N_("Ignore thread"),          ACTION_IGNORE   },
189         { N_("Watch thread"),           ACTION_WATCH    },
190         { N_("Add to address book"),    ACTION_ADD_TO_ADDRESSBOOK       },
191         { N_("Stop filter"),            ACTION_STOP     },
192 };
193
194
195 /*!
196  *\brief        Hooks
197  */
198 static PrefsFilteringActionSignal *filtering_action_callback;
199
200 /*!
201  *\brief        Find index of list selection 
202  *
203  *\param        list GTK list widget
204  *
205  *\return       gint Selection index
206  */
207 static gint get_sel_from_list(GtkList *list)
208 {
209         gint row = 0;
210         void * sel;
211         GList * child;
212
213         if (list->selection == NULL) 
214                 return -1;
215
216         sel = list->selection->data;
217         for (child = list->children; child != NULL; child = g_list_next(child)) {
218                 if (child->data == sel)
219                         return row;
220                 row ++;
221         }
222         
223         return row;
224 }
225
226 /*!
227  *\brief        Opens the filtering action dialog with a list of actions
228  *
229  *\param        matchers List of conditions
230  *\param        cb Callback
231  *
232  */
233 void prefs_filtering_action_open(GSList *action_list,
234     PrefsFilteringActionSignal *cb)
235 {
236         inc_lock();
237
238         if (!filtering_action.window) {
239                 prefs_filtering_action_create();
240         } else {
241                 /* update color label menu */
242                 gtk_option_menu_set_menu(GTK_OPTION_MENU(filtering_action.color_optmenu),
243                                 colorlabel_create_color_menu());
244         }
245
246         manage_window_set_transient(GTK_WINDOW(filtering_action.window));
247         gtk_widget_grab_focus(filtering_action.ok_btn);
248
249         filtering_action_callback = cb;
250
251         prefs_filtering_action_set_dialog(action_list);
252
253         gtk_widget_show(filtering_action.window);
254 }
255
256 /*!
257  *\brief        Save Gtk object size to prefs dataset
258  */
259 static void prefs_filtering_action_size_allocate_cb(GtkWidget *widget,
260                                          GtkAllocation *allocation)
261 {
262         g_return_if_fail(allocation != NULL);
263
264         prefs_common.filteringactionwin_width = allocation->width;
265         prefs_common.filteringactionwin_height = allocation->height;
266 }
267
268 /*!
269  *\brief        Create the matcher dialog
270  */
271 static void prefs_filtering_action_create(void)
272 {
273         GtkWidget *window;
274         GtkWidget *vbox;
275         GtkWidget *ok_btn;
276         GtkWidget *cancel_btn;
277         GtkWidget *confirm_area;
278
279         GtkWidget *vbox1;
280
281         GtkWidget *hbox1;
282
283         GtkWidget *action_label;
284         GtkWidget *recip_label;
285         GtkWidget *action_combo;
286         GtkWidget *action_type_list;
287         GtkWidget *account_list;
288         GtkWidget *dest_label;
289         GtkWidget *exec_label;
290         GtkWidget *score_label;
291         GtkWidget *color_label;
292         GtkWidget *account_label;
293         GtkWidget *account_combo;
294         GtkWidget *header_label;
295         GtkWidget *header_combo;
296         GtkWidget *header_entry;
297         GtkWidget *addressbook_label;
298         GtkWidget *addressbook_btn;
299         GtkWidget *dest_entry;
300         GtkWidget *dest_btn;
301         GtkWidget *tags_label;
302         GtkWidget *tags_list;
303         GtkWidget *tags_combo;
304         GList * cur;
305
306         GtkWidget *reg_hbox;
307         GtkWidget *btn_hbox;
308         GtkWidget *arrow;
309         GtkWidget *reg_btn;
310         GtkWidget *subst_btn;
311         GtkWidget *del_btn;
312
313         GtkWidget *action_hbox;
314         GtkWidget *action_scrolledwin;
315         GtkWidget *action_list_view;
316
317         GtkWidget *btn_vbox;
318         GtkWidget *up_btn;
319         GtkWidget *down_btn;
320
321         GtkWidget *exec_btn;
322
323         GtkWidget *color_optmenu;
324
325         GList *combo_items;
326         gint i;
327         static GdkGeometry geometry;
328
329         GList * accounts;
330         GSList *tmp, *tags;
331
332         debug_print("Creating matcher configuration window...\n");
333
334         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_filtering_action");
335         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
336         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
337         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
338         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
339
340         vbox = gtk_vbox_new(FALSE, 6);
341         gtk_widget_show(vbox);
342         gtk_container_add(GTK_CONTAINER(window), vbox);
343
344         gtkut_stock_button_set_create(&confirm_area,
345                                       &cancel_btn, GTK_STOCK_CANCEL,
346                                       &ok_btn, GTK_STOCK_OK,
347                                       NULL, NULL);
348         gtk_widget_show(confirm_area);
349         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
350         gtk_widget_grab_default(ok_btn);
351
352         gtk_window_set_title(GTK_WINDOW(window),
353                              _("Filtering action configuration"));
354         g_signal_connect(G_OBJECT(window), "delete_event",
355                          G_CALLBACK(prefs_filtering_action_deleted), NULL);
356         g_signal_connect(G_OBJECT(window), "size_allocate",
357                          G_CALLBACK(prefs_filtering_action_size_allocate_cb), NULL);
358         g_signal_connect(G_OBJECT(window), "key_press_event",
359                          G_CALLBACK(prefs_filtering_action_key_pressed), NULL);
360         MANAGE_WINDOW_SIGNALS_CONNECT(window);
361         g_signal_connect(G_OBJECT(ok_btn), "clicked",
362                          G_CALLBACK(prefs_filtering_action_ok), NULL);
363         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
364                          G_CALLBACK(prefs_filtering_action_cancel), NULL);
365
366         vbox1 = gtk_vbox_new(FALSE, VSPACING);
367         gtk_widget_show(vbox1);
368         gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
369         gtk_container_set_border_width(GTK_CONTAINER (vbox1), 2);
370
371         /* action to be defined */
372
373         hbox1 = gtk_hbox_new (FALSE, VSPACING);
374         gtk_widget_show (hbox1);
375         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
376         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
377
378         action_label = gtk_label_new (_("Action"));
379         gtk_widget_show (action_label);
380         gtk_misc_set_alignment (GTK_MISC (action_label), 0, 0.5);
381         gtk_box_pack_start (GTK_BOX (hbox1), action_label, FALSE, FALSE, 0);
382
383         action_combo = gtk_combo_new ();
384         gtk_widget_show (action_combo);
385         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(action_combo)->entry),
386                                FALSE);
387
388         combo_items = NULL;
389         for (i = 0; i < sizeof action_text / sizeof action_text[0]; i++)
390                 combo_items = g_list_append
391                         (combo_items, (gpointer) _(action_text[i].text));
392         gtk_combo_set_popdown_strings(GTK_COMBO(action_combo), combo_items);
393
394         g_list_free(combo_items);
395
396         gtk_box_pack_start (GTK_BOX (hbox1), action_combo,
397                             TRUE, TRUE, 0);
398         action_type_list = GTK_COMBO(action_combo)->list;
399         g_signal_connect (G_OBJECT(action_type_list), "select-child",
400                           G_CALLBACK(prefs_filtering_action_type_select),
401                           NULL);
402
403         g_signal_connect(G_OBJECT(action_type_list), "selection-changed",
404                          G_CALLBACK(prefs_filtering_action_type_selection_changed),
405                          NULL);
406
407         /* accounts */
408
409         hbox1 = gtk_hbox_new (FALSE, VSPACING);
410         gtk_widget_show (vbox1);
411         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
412         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
413
414         account_label = gtk_label_new (_("Account"));
415         gtk_widget_show (account_label);
416         gtk_misc_set_alignment (GTK_MISC (account_label), 0, 0.5);
417         gtk_box_pack_start (GTK_BOX (hbox1), account_label, FALSE, FALSE, 0);
418
419         account_combo = gtk_combo_new ();
420         gtk_widget_set_size_request (account_combo, 150, -1);
421         gtk_widget_show (account_combo);
422
423         combo_items = NULL;
424         for (accounts = account_get_list() ; accounts != NULL;
425              accounts = accounts->next) {
426                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
427                 gchar *name;
428
429                 name = g_strdup_printf("%s <%s> (%s)",
430                                        ac->name, ac->address,
431                                        ac->account_name);
432                 combo_items = g_list_append(combo_items, (gpointer) name);
433         }
434
435         gtk_combo_set_popdown_strings(GTK_COMBO(account_combo), combo_items);
436
437         for(cur = g_list_first(combo_items) ; cur != NULL ;
438             cur = g_list_next(cur))
439                 g_free(cur->data);
440         g_list_free(combo_items);
441
442         gtk_box_pack_start (GTK_BOX (hbox1), account_combo,
443                             TRUE, TRUE, 0);
444         account_list = GTK_COMBO(account_combo)->list;
445         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(account_combo)->entry),
446                                FALSE);
447
448         /* header */
449
450         header_label = gtk_label_new(_("Header name"));
451         gtk_widget_show(header_label);
452         gtk_misc_set_alignment(GTK_MISC(header_label), 0, 0.5);
453         gtk_box_pack_start (GTK_BOX (hbox1), header_label, FALSE, FALSE, 0);
454
455         header_combo = gtk_combo_new();
456         gtk_widget_show(header_combo);
457         gtk_widget_set_size_request(header_combo, 120, -1);
458         gtkut_combo_set_items(GTK_COMBO (header_combo),
459                               "From", "To", "Cc", "Reply-To", "Sender",
460                               NULL);
461         gtk_box_pack_start (GTK_BOX (hbox1), header_combo,
462                             TRUE, TRUE, 0);
463         header_entry = GTK_COMBO(header_combo)->entry;
464         gtk_entry_set_editable(GTK_ENTRY(header_entry), TRUE);
465
466         /* destination */
467
468         hbox1 = gtk_hbox_new (FALSE, VSPACING);
469         gtk_widget_show (vbox1);
470         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
471         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
472
473         dest_label = gtk_label_new (_("Destination"));
474         gtk_widget_show (dest_label);
475         gtk_misc_set_alignment (GTK_MISC (dest_label), 0, 0.5);
476         gtk_box_pack_start (GTK_BOX (hbox1), dest_label, FALSE, FALSE, 0);
477
478         recip_label = gtk_label_new (_("Recipient"));
479         gtk_widget_show (recip_label);
480         gtk_misc_set_alignment (GTK_MISC (recip_label), 0, 0.5);
481         gtk_box_pack_start (GTK_BOX (hbox1), recip_label, FALSE, FALSE, 0);
482
483         exec_label = gtk_label_new (_("Execute"));
484         gtk_widget_show (exec_label);
485         gtk_misc_set_alignment (GTK_MISC (exec_label), 0, 0.5);
486         gtk_box_pack_start (GTK_BOX (hbox1), exec_label, FALSE, FALSE, 0);
487         
488         color_label = gtk_label_new (_("Color"));
489         gtk_widget_show(color_label);
490         gtk_misc_set_alignment(GTK_MISC(color_label), 0, 0.5);
491         gtk_box_pack_start(GTK_BOX(hbox1), color_label, FALSE, FALSE, 0);
492
493         score_label = gtk_label_new (_("Score"));
494         gtk_widget_show (score_label);
495         gtk_misc_set_alignment (GTK_MISC (score_label), 0, 0.5);
496         gtk_box_pack_start (GTK_BOX (hbox1), score_label, FALSE, FALSE, 0);
497
498         addressbook_label = gtk_label_new (_("Book/folder"));
499         gtk_widget_show(addressbook_label);
500         gtk_misc_set_alignment(GTK_MISC(addressbook_label), 0, 0.5);
501         gtk_box_pack_start(GTK_BOX(hbox1), addressbook_label, FALSE, FALSE, 0);
502
503         tags_label = gtk_label_new (_("Tag"));
504         gtk_widget_show (tags_label);
505         gtk_misc_set_alignment (GTK_MISC (tags_label), 0, 0.5);
506         gtk_box_pack_start (GTK_BOX (hbox1), tags_label, FALSE, FALSE, 0);
507
508         dest_entry = gtk_entry_new ();
509         gtk_widget_set_size_request (dest_entry, 150, -1);
510         gtk_widget_show (dest_entry);
511         gtk_box_pack_start (GTK_BOX (hbox1), dest_entry, TRUE, TRUE, 0);
512         
513         color_optmenu = gtk_option_menu_new();
514         gtk_option_menu_set_menu(GTK_OPTION_MENU(color_optmenu),
515                                  colorlabel_create_color_menu());
516         gtk_box_pack_start(GTK_BOX(hbox1), color_optmenu, TRUE, TRUE, 0);
517
518         tags_combo = gtk_combo_new ();
519         gtk_widget_set_size_request (tags_combo, 150, -1);
520         gtk_widget_show (tags_combo);
521
522         combo_items = NULL;
523         for (tmp = tags = tags_get_list() ; tmp != NULL;
524              tmp = tmp->next) {
525                 gchar *name = g_strdup(tags_get_tag(GPOINTER_TO_INT(tmp->data)));
526
527                 combo_items = g_list_append(combo_items, (gpointer) name);
528         }
529
530         gtk_combo_set_popdown_strings(GTK_COMBO(tags_combo), combo_items);
531
532         for(cur = g_list_first(combo_items) ; cur != NULL ;
533             cur = g_list_next(cur))
534                 g_free(cur->data);
535         g_list_free(combo_items);
536         g_slist_free(tags);
537
538         gtk_box_pack_start (GTK_BOX (hbox1), tags_combo,
539                             TRUE, TRUE, 0);
540         tags_list = GTK_COMBO(tags_combo)->list;
541         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(tags_combo)->entry),
542                                FALSE);
543         dest_btn = gtk_button_new_with_label (_("Select ..."));
544         gtk_widget_show (dest_btn);
545         gtk_box_pack_start (GTK_BOX (hbox1), dest_btn, FALSE, FALSE, 0);
546         g_signal_connect (G_OBJECT (dest_btn), "clicked",
547                           G_CALLBACK(prefs_filtering_action_select_dest),
548                           NULL);
549
550         addressbook_btn = gtk_button_new_with_label (_("Select ..."));
551         gtk_widget_show (addressbook_btn);
552         gtk_box_pack_start (GTK_BOX (hbox1), addressbook_btn, FALSE, FALSE, 0);
553         g_signal_connect (G_OBJECT (addressbook_btn), "clicked",
554                           G_CALLBACK(prefs_filtering_action_select_addressbook),
555                           NULL);
556
557 #if GTK_CHECK_VERSION(2, 8, 0)
558         exec_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
559 #else
560         exec_btn = gtk_button_new_with_label (_("Info..."));
561 #endif
562         gtk_widget_show (exec_btn);
563         gtk_box_pack_start (GTK_BOX (hbox1), exec_btn, FALSE, FALSE, 0);
564         g_signal_connect (G_OBJECT (exec_btn), "clicked",
565                           G_CALLBACK(prefs_filtering_action_exec_info),
566                           NULL);
567
568         /* register / substitute / delete */
569
570         reg_hbox = gtk_hbox_new(FALSE, 4);
571         gtk_widget_show(reg_hbox);
572         gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
573
574         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
575         gtk_widget_show(arrow);
576         gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
577         gtk_widget_set_size_request(arrow, -1, 16);
578
579         btn_hbox = gtk_hbox_new(TRUE, 4);
580         gtk_widget_show(btn_hbox);
581         gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
582
583         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
584         gtk_widget_show(reg_btn);
585         gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
586         g_signal_connect(G_OBJECT(reg_btn), "clicked",
587                          G_CALLBACK(prefs_filtering_action_register_cb), NULL);
588
589         subst_btn = gtkut_get_replace_btn(_("Replace"));
590         gtk_widget_show(subst_btn);
591         gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
592         g_signal_connect(G_OBJECT(subst_btn), "clicked",
593                          G_CALLBACK(prefs_filtering_action_substitute_cb),
594                          NULL);
595
596         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
597         gtk_widget_show(del_btn);
598         gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
599         g_signal_connect(G_OBJECT(del_btn), "clicked",
600                          G_CALLBACK(prefs_filtering_action_delete_cb), NULL);
601
602         action_hbox = gtk_hbox_new(FALSE, 8);
603         gtk_widget_show(action_hbox);
604         gtk_box_pack_start(GTK_BOX(vbox1), action_hbox, TRUE, TRUE, 0);
605
606         action_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
607         gtk_widget_show(action_scrolledwin);
608         gtk_widget_set_size_request(action_scrolledwin, -1, 150);
609         gtk_box_pack_start(GTK_BOX(action_hbox), action_scrolledwin,
610                            TRUE, TRUE, 0);
611         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(action_scrolledwin),
612                                        GTK_POLICY_AUTOMATIC,
613                                        GTK_POLICY_AUTOMATIC);
614
615         action_list_view = prefs_filtering_action_list_view_create();
616         gtk_widget_show(action_list_view);
617         gtk_container_add(GTK_CONTAINER(action_scrolledwin), action_list_view);
618
619         btn_vbox = gtk_vbox_new(FALSE, 8);
620         gtk_widget_show(btn_vbox);
621         gtk_box_pack_start(GTK_BOX(action_hbox), btn_vbox, FALSE, FALSE, 0);
622
623         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
624         gtk_widget_show(up_btn);
625         gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0);
626         g_signal_connect(G_OBJECT(up_btn), "clicked",
627                          G_CALLBACK(prefs_filtering_action_up), NULL);
628
629         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
630         gtk_widget_show(down_btn);
631         gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
632         g_signal_connect(G_OBJECT(down_btn), "clicked",
633                          G_CALLBACK(prefs_filtering_action_down), NULL);
634
635         if (!geometry.min_height) {
636                 geometry.min_width = 490;
637                 geometry.min_height = 328;
638         }
639
640         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
641                                       GDK_HINT_MIN_SIZE);
642         gtk_widget_set_size_request(window, prefs_common.filteringactionwin_width,
643                                     prefs_common.filteringactionwin_height);
644
645         gtk_widget_show_all(window);
646
647         filtering_action.window    = window;
648         filtering_action.action_type_list = action_type_list;
649         filtering_action.action_combo = action_combo;
650         filtering_action.account_label = account_label;
651         filtering_action.account_list = account_list;
652         filtering_action.account_combo = account_combo;
653         filtering_action.tags_label = tags_label;
654         filtering_action.tags_list = tags_list;
655         filtering_action.tags_combo = tags_combo;
656         filtering_action.dest_entry = dest_entry;
657         filtering_action.dest_btn = dest_btn;
658         filtering_action.dest_label = dest_label;
659         filtering_action.recip_label = recip_label;
660         filtering_action.exec_label = exec_label;
661         filtering_action.exec_btn = exec_btn;
662         filtering_action.color_label   = color_label;
663         filtering_action.color_optmenu = color_optmenu;
664         filtering_action.score_label = score_label;
665         filtering_action.header_label = header_label;
666         filtering_action.header_combo = header_combo;
667         filtering_action.header_entry = header_entry;
668         filtering_action.addressbook_label = addressbook_label;
669         filtering_action.addressbook_btn = addressbook_btn;
670         filtering_action.ok_btn = ok_btn;
671         filtering_action.action_list_view = action_list_view;
672 }
673
674 /*!
675  *\brief        Set the contents of a row
676  *
677  *\param        row Index of row to set
678  *\param        prop Condition to set
679  *
680  */
681 static void prefs_filtering_action_list_view_set_row(GtkTreeIter *row, 
682                                                      FilteringAction *action)
683 {
684         gchar buf[256];
685
686         if (row == NULL && action == NULL) {
687                 prefs_filtering_action_list_view_insert_action
688                         (filtering_action.action_list_view,
689                          NULL, _("(New)"), FALSE);
690                 return;
691         }                        
692
693         filteringaction_to_string(buf, sizeof buf, action);
694
695         prefs_filtering_action_list_view_insert_action
696                         (filtering_action.action_list_view,
697                          row, buf, TRUE);
698 }
699
700 /*!
701  *\brief        Initializes dialog with a set of conditions
702  *
703  *\param        matchers List of conditions
704  */
705 static void prefs_filtering_action_set_dialog(GSList *action_list)
706 {
707         GSList *cur;
708
709         gtk_list_store_clear(GTK_LIST_STORE(gtk_tree_view_get_model
710                         (GTK_TREE_VIEW(filtering_action.action_list_view))));
711
712         prefs_filtering_action_list_view_set_row(NULL, NULL);
713         if (action_list != NULL) {
714                 for (cur = action_list; cur != NULL;
715                      cur = g_slist_next(cur)) {
716                         FilteringAction *action;
717                         action = (FilteringAction *) cur->data;
718                         prefs_filtering_action_list_view_set_row(NULL, action);
719                 }
720         }
721         
722         prefs_filtering_action_reset_dialog();
723 }
724
725 /*!
726  *\brief        Converts current actions in list box in
727  *              an action list used by the filtering system.
728  *
729  *\return       GSList * List of actions.
730  */
731 static GSList *prefs_filtering_action_get_list(void)
732 {
733         gchar *action_str;
734         gboolean is_valid;
735         gint row = 1;
736         GSList *action_list;
737         GtkTreeView *list_view = GTK_TREE_VIEW(filtering_action.action_list_view);
738         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
739         GtkTreeIter iter;
740
741         action_list = NULL;
742
743         while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
744
745                 gtk_tree_model_get(model, &iter, 
746                                    PFA_ACTION, &action_str,
747                                    PFA_VALID_ACTION, &is_valid,
748                                    -1);
749
750                 if (is_valid) {                            
751                         GSList * tmp_action_list;
752                         tmp_action_list = matcher_parser_get_action_list(action_str);
753                         
754                         if (tmp_action_list == NULL) {
755                                 g_free(action_str);
756                                 break;
757                         }                               
758
759                         action_list = g_slist_concat(action_list,
760                             tmp_action_list);
761                 }
762
763                 g_free(action_str);
764                 action_str = NULL;
765                 row ++;
766                 
767         }
768
769         return action_list;
770 }
771
772 /*!
773  *\brief        Returns account ID from the given list index
774  *
775  *\return       gint account ID
776  */
777 static gint get_account_id_from_list_id(gint list_id)
778 {
779         GList * accounts;
780
781         for (accounts = account_get_list() ; accounts != NULL;
782              accounts = accounts->next) {
783                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
784
785                 if (list_id == 0)
786                         return ac->account_id;
787                 list_id--;
788         }
789         return 0;
790 }
791
792 /*!
793  *\brief        Returns list index from the given account ID
794  *
795  *\return       gint list index
796  */
797 static gint get_list_id_from_account_id(gint account_id)
798 {
799         GList * accounts;
800         gint list_id = 0;
801
802         for (accounts = account_get_list() ; accounts != NULL;
803              accounts = accounts->next) {
804                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
805
806                 if (account_id == ac->account_id)
807                         return list_id;
808                 list_id++;
809         }
810         return 0;
811 }
812
813
814 /*!
815  *\brief        Returns parser action ID from internal action ID
816  *
817  *\return       gint parser action ID
818  */
819 static gint prefs_filtering_action_get_matching_from_action(Action action_id)
820 {
821         switch (action_id) {
822         case ACTION_MOVE:
823                 return MATCHACTION_MOVE;
824         case ACTION_SET_TAG:
825                 return MATCHACTION_SET_TAG;
826         case ACTION_UNSET_TAG:
827                 return MATCHACTION_UNSET_TAG;
828         case ACTION_CLEAR_TAGS:
829                 return MATCHACTION_CLEAR_TAGS;
830         case ACTION_COPY:
831                 return MATCHACTION_COPY;
832         case ACTION_DELETE:
833                 return MATCHACTION_DELETE;
834         case ACTION_MARK:
835                 return MATCHACTION_MARK;
836         case ACTION_UNMARK:
837                 return MATCHACTION_UNMARK;
838         case ACTION_LOCK:
839                 return MATCHACTION_LOCK;
840         case ACTION_UNLOCK:
841                 return MATCHACTION_UNLOCK;
842         case ACTION_MARK_AS_READ:
843                 return MATCHACTION_MARK_AS_READ;
844         case ACTION_MARK_AS_UNREAD:
845                 return MATCHACTION_MARK_AS_UNREAD;
846         case ACTION_MARK_AS_SPAM:
847                 return MATCHACTION_MARK_AS_SPAM;
848         case ACTION_MARK_AS_HAM:
849                 return MATCHACTION_MARK_AS_HAM;
850         case ACTION_FORWARD:
851                 return MATCHACTION_FORWARD;
852         case ACTION_FORWARD_AS_ATTACHMENT:
853                 return MATCHACTION_FORWARD_AS_ATTACHMENT;
854         case ACTION_REDIRECT:
855                 return MATCHACTION_REDIRECT;
856         case ACTION_EXECUTE:
857                 return MATCHACTION_EXECUTE;
858         case ACTION_COLOR:
859                 return MATCHACTION_COLOR;
860         case ACTION_HIDE:
861                 return MATCHACTION_HIDE;
862         case ACTION_IGNORE:
863                 return MATCHACTION_IGNORE;
864         case ACTION_WATCH:
865                 return MATCHACTION_WATCH;
866         case ACTION_STOP:
867                 return MATCHACTION_STOP;
868         case ACTION_CHANGE_SCORE:
869                 return MATCHACTION_CHANGE_SCORE;
870         case ACTION_SET_SCORE:
871                 return MATCHACTION_SET_SCORE;
872         case ACTION_ADD_TO_ADDRESSBOOK:
873                 return MATCHACTION_ADD_TO_ADDRESSBOOK;
874         default:
875                 return -1;
876         }
877 }
878
879 /*!
880  *\brief        Returns action from the content of the dialog
881  *
882  *\param        alert specifies whether alert dialog boxes should be shown
883  *                or not.
884  *
885  *\return       FilteringAction * action entered in the dialog box.
886  */
887 static FilteringAction * prefs_filtering_action_dialog_to_action(gboolean alert)
888 {
889         Action action_id;
890         gint action_type;
891         gint list_id;
892         gint account_id;
893         gchar * destination = NULL;
894         gint labelcolor = 0;
895         FilteringAction * action;
896         gchar * score_str = NULL;
897         gint score;
898         gchar * header = NULL;
899         
900         action_id = get_sel_from_list(GTK_LIST(filtering_action.action_type_list));
901         action_type = prefs_filtering_action_get_matching_from_action(action_id);
902         list_id = get_sel_from_list(GTK_LIST(filtering_action.account_list));
903         account_id = get_account_id_from_list_id(list_id);
904         score = 0;
905         destination = NULL;
906         
907         switch (action_id) {
908         case ACTION_MOVE:
909         case ACTION_COPY:
910         case ACTION_EXECUTE:
911                 destination = gtk_editable_get_chars(GTK_EDITABLE(filtering_action.dest_entry), 0, -1);
912                 if (*destination == '\0') {
913                         if (alert)
914                                 alertpanel_error(action_id == ACTION_EXECUTE 
915                                                  ? _("Command line not set")
916                                                  : _("Destination is not set."));
917                         g_free(destination);
918                         return NULL;
919                 }
920                 break;
921         case ACTION_FORWARD:
922         case ACTION_FORWARD_AS_ATTACHMENT:
923         case ACTION_REDIRECT:
924                 destination = gtk_editable_get_chars(GTK_EDITABLE(filtering_action.dest_entry), 0, -1);
925                 if (*destination == '\0') {
926                         if (alert)
927                                 alertpanel_error(_("Recipient is not set."));
928                         g_free(destination);
929                         return NULL;
930                 }
931                 break;
932         case ACTION_COLOR:
933                 labelcolor = colorlabel_get_color_menu_active_item(
934                         gtk_option_menu_get_menu(GTK_OPTION_MENU(filtering_action.color_optmenu)));
935                 destination = NULL;     
936                 break;
937         case ACTION_CHANGE_SCORE:
938         case ACTION_SET_SCORE:
939                 score_str = gtk_editable_get_chars(GTK_EDITABLE(filtering_action.dest_entry), 0, -1);
940                 if (*score_str == '\0') {
941                         if (alert)
942                                 alertpanel_error(_("Score is not set"));
943                         g_free(score_str);
944                         return NULL;
945                 }
946                 score = strtol(score_str, NULL, 10);
947                 break;
948         case ACTION_ADD_TO_ADDRESSBOOK:
949                 header = gtk_editable_get_chars(GTK_EDITABLE(filtering_action.header_entry), 0, -1);
950                 if (*header == '\0') {
951                         if (alert)
952                                 alertpanel_error(_("Header is not set."));
953                         g_free(header);
954                         return NULL;
955                 }
956                 destination = gtk_editable_get_chars(GTK_EDITABLE(filtering_action.dest_entry), 0, -1);
957                 if (*destination == '\0') {
958                         if (alert)
959                                 alertpanel_error(_("Target addressbook/folder is not set."));
960                         g_free(destination);
961                         return NULL;
962                 }
963                 break;
964         case ACTION_SET_TAG:
965         case ACTION_UNSET_TAG:
966                 destination = gtk_editable_get_chars(GTK_EDITABLE(GTK_COMBO(filtering_action.tags_combo)->entry), 0, -1);
967                 if (*destination == '\0') {
968                         if (alert)
969                                 alertpanel_error(_("Tag name is empty."));
970                         g_free(destination);
971                         return NULL;
972                 }
973                 break;
974         case ACTION_STOP:
975         case ACTION_HIDE:
976         case ACTION_IGNORE:
977         case ACTION_WATCH:
978         case ACTION_DELETE:
979         case ACTION_MARK:
980         case ACTION_UNMARK:
981         case ACTION_LOCK:
982         case ACTION_UNLOCK:
983         case ACTION_MARK_AS_READ:
984         case ACTION_MARK_AS_UNREAD:
985         case ACTION_MARK_AS_SPAM:
986         case ACTION_MARK_AS_HAM:
987         case ACTION_CLEAR_TAGS:
988         default:
989                 break;
990         }
991         action = filteringaction_new(action_type, account_id,
992             destination, labelcolor, score, header);
993         
994         g_free(destination);
995         g_free(score_str);
996         return action;
997 }
998
999 /*!
1000  *\brief        Signal handler for register button
1001  */
1002 static void prefs_filtering_action_register_cb(void)
1003 {
1004         FilteringAction *action;
1005         
1006         action = prefs_filtering_action_dialog_to_action(TRUE);
1007         if (action == NULL)
1008                 return;
1009
1010         prefs_filtering_action_list_view_set_row(NULL, action);
1011
1012         filteringaction_free(action);
1013         /* GTK 1 NOTE:
1014          * (presumably gtk_list_select_item(), called by 
1015          * prefs_filtering_action_reset_dialog() activates 
1016          * what seems to be a bug. this causes any other 
1017          * list items to be unselectable)
1018          * prefs_filtering_action_reset_dialog(); */
1019         gtk_list_select_item(GTK_LIST(filtering_action.account_list), 0);
1020         gtk_list_select_item(GTK_LIST(filtering_action.tags_list), 0);
1021         gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), "");
1022 }
1023
1024 /*!
1025  *\brief        Signal handler for substitute button
1026  */
1027 static void prefs_filtering_action_substitute_cb(void)
1028 {
1029         GtkTreeView *list_view = GTK_TREE_VIEW
1030                         (filtering_action.action_list_view);
1031         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
1032         GtkTreeModel *model;
1033         gboolean is_valid;
1034         GtkTreeIter row;
1035         FilteringAction *action;
1036
1037         if (!gtk_tree_selection_get_selected(selection, &model, &row))
1038                 return;
1039
1040         gtk_tree_model_get(model, &row, PFA_VALID_ACTION, &is_valid, -1);
1041         if (!is_valid)
1042                 return;
1043
1044         action = prefs_filtering_action_dialog_to_action(TRUE);
1045         if (action == NULL)
1046                 return;
1047
1048         prefs_filtering_action_list_view_set_row(&row, action);
1049
1050         filteringaction_free(action);
1051
1052         prefs_filtering_action_reset_dialog();
1053 }
1054
1055 /*!
1056  *\brief        Signal handler for delete button
1057  */
1058 static void prefs_filtering_action_delete_cb(void)
1059 {
1060         GtkTreeView *list_view = GTK_TREE_VIEW
1061                         (filtering_action.action_list_view);
1062         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
1063         GtkTreeModel *model;
1064         gboolean is_valid;
1065         GtkTreeIter row;
1066
1067         if (!gtk_tree_selection_get_selected(selection, &model, &row))
1068                 return;
1069
1070         gtk_tree_model_get(model, &row, PFA_VALID_ACTION, &is_valid, -1);
1071         if (!is_valid)
1072                 return;
1073
1074         gtk_list_store_remove(GTK_LIST_STORE(model), &row);             
1075
1076         prefs_filtering_action_reset_dialog();
1077 }
1078
1079 /*!
1080  *\brief        Signal handler for 'move up' button
1081  */
1082 static void prefs_filtering_action_up(void)
1083 {
1084         GtkTreePath *prev, *sel, *try;
1085         GtkTreeIter isel;
1086         GtkListStore *store = NULL;
1087         GtkTreeModel *model = NULL;
1088         GtkTreeIter iprev;
1089         
1090         if (!gtk_tree_selection_get_selected
1091                 (gtk_tree_view_get_selection
1092                         (GTK_TREE_VIEW(filtering_action.action_list_view)),
1093                  &model,        
1094                  &isel))
1095                 return;
1096         store = (GtkListStore *)model;
1097         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
1098         if (!sel)
1099                 return;
1100         
1101         /* no move if we're at row 0 or 1, looks phony, but other
1102          * solutions are more convoluted... */
1103         try = gtk_tree_path_copy(sel);
1104         if (!gtk_tree_path_prev(try) || !gtk_tree_path_prev(try)) {
1105                 gtk_tree_path_free(try);
1106                 gtk_tree_path_free(sel);
1107                 return;
1108         }
1109         gtk_tree_path_free(try);
1110
1111         prev = gtk_tree_path_copy(sel);         
1112         if (gtk_tree_path_prev(prev)) {
1113                 gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
1114                                         &iprev, prev);
1115                 gtk_list_store_swap(store, &iprev, &isel);
1116                 /* XXX: GTK2 select row?? */
1117         }
1118
1119         gtk_tree_path_free(sel);
1120         gtk_tree_path_free(prev);
1121 }
1122
1123 /*!
1124  *\brief        Signal handler for 'move down' button
1125  */
1126 static void prefs_filtering_action_down(void)
1127 {
1128         GtkListStore *store = NULL;
1129         GtkTreeModel *model = NULL;
1130         GtkTreeIter next, sel;
1131         GtkTreePath *try;
1132         
1133         if (!gtk_tree_selection_get_selected
1134                 (gtk_tree_view_get_selection
1135                         (GTK_TREE_VIEW(filtering_action.action_list_view)),
1136                  &model,
1137                  &sel))
1138                 return;
1139         store = (GtkListStore *)model;
1140         try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
1141         if (!try) 
1142                 return;
1143         
1144         /* move when not at row 0 ... */
1145         if (gtk_tree_path_prev(try)) {
1146                 next = sel;
1147                 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next))
1148                         gtk_list_store_swap(store, &next, &sel);
1149         }
1150                 
1151         gtk_tree_path_free(try);
1152 }
1153
1154 /*!
1155  *\brief        Handle key press
1156  *
1157  *\param        widget Widget receiving key press
1158  *\param        event Key event
1159  *\param        data User data
1160  */
1161 static gboolean prefs_filtering_action_key_pressed(GtkWidget *widget,
1162     GdkEventKey *event, gpointer data)
1163 {
1164         if (event && event->keyval == GDK_Escape) {
1165                 prefs_filtering_action_cancel();
1166                 return TRUE;            
1167         }
1168         return FALSE;
1169 }
1170
1171 /*!
1172  *\brief        Cancel matcher dialog
1173  */
1174 static void prefs_filtering_action_cancel(void)
1175 {
1176         gtk_widget_destroy(filtering_action.window);
1177         filtering_action.window = NULL;
1178         inc_unlock();
1179 }
1180
1181 /*!
1182  *\brief        Accept current matchers
1183  */
1184 static void prefs_filtering_action_ok(void)
1185 {
1186         GSList * action_list;
1187         GSList * cur;
1188
1189         action_list = prefs_filtering_action_get_list();
1190
1191         if (action_list == NULL) {
1192                 alertpanel_error(_("No action was defined."));
1193                 return;
1194         }
1195
1196         if (filtering_action_callback != NULL)
1197                 filtering_action_callback(action_list);
1198         for(cur = action_list ; cur != NULL ; cur = cur->next) {
1199                 filteringaction_free(cur->data);
1200         }
1201         g_slist_free(action_list);
1202
1203         gtk_widget_destroy(filtering_action.window);
1204         filtering_action.window = NULL;
1205         inc_unlock();
1206 }
1207
1208 /*!
1209  *\brief        Called when closing dialog box
1210  *
1211  *\param        widget Dialog widget
1212  *\param        event Event info
1213  *\param        data User data
1214  *
1215  *\return       gint TRUE
1216  */
1217 static gint prefs_filtering_action_deleted(GtkWidget *widget,
1218     GdkEventAny *event, gpointer data)
1219 {
1220         prefs_filtering_action_cancel();
1221         return TRUE;
1222 }
1223
1224 /*
1225  * Strings describing exec format strings
1226  * 
1227  * When adding new lines, remember to put 2 strings for each line
1228  */
1229 static gchar *exec_desc_strings[] = {
1230         "%%",   N_("literal %"),
1231         "%s",   N_("Subject"),
1232         "%f",   N_("From"),
1233         "%t",   N_("To"),
1234         "%c",   N_("Cc"),
1235         "%d",   N_("Date"),
1236         "%i",   N_("Message-ID"),
1237         "%n",   N_("Newsgroups"),
1238         "%r",   N_("References"),
1239         "%F",   N_("filename (should not be modified)"),
1240         "\\n",  N_("new line"),
1241         "\\",   N_("escape character for quotes"),
1242         "\\\"", N_("quote character"),
1243         NULL, NULL
1244 };
1245
1246 static DescriptionWindow exec_desc_win = { 
1247         NULL,
1248         NULL, 
1249         2,
1250         N_("Filtering Action: 'Execute'"),
1251         N_("'Execute' allows you to send a message or message element "
1252            "to an external program or script.\n"
1253            "The following symbols can be used:"),
1254        exec_desc_strings
1255 };
1256
1257 /*!
1258  *\brief        Show Execute action's info
1259  */
1260 void prefs_filtering_action_exec_info(void)
1261 {
1262         description_window_create(&exec_desc_win);
1263 }
1264
1265 static void prefs_filtering_action_select_dest(void)
1266 {
1267         FolderItem *dest;
1268         gchar * path;
1269
1270         dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL);
1271         if (!dest) return;
1272
1273         path = folder_item_get_identifier(dest);
1274
1275         gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), path);
1276         g_free(path);
1277 }
1278
1279 static void prefs_filtering_action_select_addressbook(void)
1280 {
1281         gchar *folderpath = NULL;
1282         gchar *prev = NULL;
1283         gboolean ret = FALSE;
1284
1285         prev = g_strdup(gtk_editable_get_chars(GTK_EDITABLE(filtering_action.dest_entry), 0, -1));
1286         folderpath = prev;
1287         ret = addressbook_folder_selection(&folderpath);
1288         if ( ret != FALSE && folderpath != NULL)
1289                 gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), folderpath);
1290         g_free(prev);
1291 }
1292
1293 static void prefs_filtering_action_type_selection_changed(GtkList *list,
1294     gpointer user_data)
1295 {
1296         gint value;
1297
1298         value = get_sel_from_list(GTK_LIST(filtering_action.action_type_list));
1299
1300         if (filtering_action.current_action != value) {
1301                 if (filtering_action.current_action == ACTION_FORWARD 
1302                 ||  filtering_action.current_action == ACTION_FORWARD_AS_ATTACHMENT
1303                 ||  filtering_action.current_action == ACTION_REDIRECT) {
1304                         debug_print("unregistering address completion entry\n");
1305                         address_completion_unregister_entry(GTK_ENTRY(filtering_action.dest_entry));
1306                         address_completion_end(filtering_action.window);
1307                 }
1308                 if (value == ACTION_FORWARD || value == ACTION_FORWARD_AS_ATTACHMENT
1309                 ||  value == ACTION_REDIRECT) {
1310                         debug_print("registering address completion entry\n");
1311                         address_completion_start(filtering_action.window);
1312                         address_completion_register_entry(
1313                                         GTK_ENTRY(filtering_action.dest_entry),
1314                                         TRUE);
1315                 }
1316                 filtering_action.current_action = value;
1317         }
1318 }
1319
1320 static void prefs_filtering_action_type_select(GtkList *list,
1321     GtkWidget *widget, gpointer user_data)
1322 {
1323         Action value;
1324
1325         value = (Action) get_sel_from_list(GTK_LIST(filtering_action.action_type_list));
1326
1327         switch (value) {
1328         case ACTION_MOVE:
1329                 gtk_widget_show(filtering_action.account_label);
1330                 gtk_widget_set_sensitive(filtering_action.account_label, FALSE);
1331                 gtk_widget_show(filtering_action.account_combo);
1332                 gtk_widget_set_sensitive(filtering_action.account_combo, FALSE);
1333                 gtk_widget_hide(filtering_action.tags_label);
1334                 gtk_widget_set_sensitive(filtering_action.tags_label, FALSE);
1335                 gtk_widget_hide(filtering_action.tags_combo);
1336                 gtk_widget_set_sensitive(filtering_action.tags_combo, FALSE);
1337                 gtk_widget_show(filtering_action.dest_entry);
1338                 gtk_widget_set_sensitive(filtering_action.dest_entry, TRUE);
1339                 gtk_widget_show(filtering_action.dest_btn);
1340                 gtk_widget_set_sensitive(filtering_action.dest_btn, TRUE);
1341                 gtk_widget_show(filtering_action.dest_label);
1342                 gtk_widget_set_sensitive(filtering_action.dest_label, TRUE);
1343                 gtk_widget_hide(filtering_action.recip_label);
1344                 gtk_widget_hide(filtering_action.exec_label);
1345                 gtk_widget_hide(filtering_action.exec_btn);
1346                 gtk_widget_hide(filtering_action.color_optmenu);
1347                 gtk_widget_hide(filtering_action.color_label);
1348                 gtk_widget_hide(filtering_action.score_label);
1349                 gtk_widget_hide(filtering_action.addressbook_label);
1350                 gtk_widget_hide(filtering_action.header_label);
1351                 gtk_widget_hide(filtering_action.header_combo);
1352                 gtk_widget_hide(filtering_action.header_entry);
1353                 gtk_widget_set_sensitive(filtering_action.header_entry, FALSE);
1354                 gtk_widget_hide(filtering_action.addressbook_btn);
1355                 gtk_widget_set_sensitive(filtering_action.addressbook_btn, FALSE);
1356                 break;
1357         case ACTION_COPY:
1358                 gtk_widget_show(filtering_action.account_label);
1359                 gtk_widget_set_sensitive(filtering_action.account_label, FALSE);
1360                 gtk_widget_show(filtering_action.account_combo);
1361                 gtk_widget_set_sensitive(filtering_action.account_combo, FALSE);
1362                 gtk_widget_hide(filtering_action.tags_label);
1363                 gtk_widget_set_sensitive(filtering_action.tags_label, FALSE);
1364                 gtk_widget_hide(filtering_action.tags_combo);
1365                 gtk_widget_set_sensitive(filtering_action.tags_combo, FALSE);
1366                 gtk_widget_show(filtering_action.dest_entry);
1367                 gtk_widget_set_sensitive(filtering_action.dest_entry, TRUE);
1368                 gtk_widget_show(filtering_action.dest_btn);
1369                 gtk_widget_set_sensitive(filtering_action.dest_btn, TRUE);
1370                 gtk_widget_show(filtering_action.dest_label);
1371                 gtk_widget_set_sensitive(filtering_action.dest_label, TRUE);
1372                 gtk_widget_hide(filtering_action.recip_label);
1373                 gtk_widget_hide(filtering_action.exec_label);
1374                 gtk_widget_hide(filtering_action.exec_btn);
1375                 gtk_widget_hide(filtering_action.color_optmenu);
1376                 gtk_widget_hide(filtering_action.color_label);
1377                 gtk_widget_hide(filtering_action.score_label);
1378                 gtk_widget_hide(filtering_action.header_label);
1379                 gtk_widget_hide(filtering_action.header_combo);
1380                 gtk_widget_hide(filtering_action.header_entry);
1381                 gtk_widget_set_sensitive(filtering_action.header_entry, FALSE);
1382                 gtk_widget_hide(filtering_action.addressbook_label);
1383                 gtk_widget_hide(filtering_action.addressbook_btn);
1384                 gtk_widget_set_sensitive(filtering_action.addressbook_btn, FALSE);
1385                 break;
1386         case ACTION_DELETE:
1387                 gtk_widget_show(filtering_action.account_label);
1388                 gtk_widget_set_sensitive(filtering_action.account_label, FALSE);
1389                 gtk_widget_show(filtering_action.account_combo);
1390                 gtk_widget_set_sensitive(filtering_action.account_combo, FALSE);
1391                 gtk_widget_hide(filtering_action.tags_label);
1392                 gtk_widget_set_sensitive(filtering_action.tags_label, FALSE);
1393                 gtk_widget_hide(filtering_action.tags_combo);
1394                 gtk_widget_set_sensitive(filtering_action.tags_combo, FALSE);
1395                 gtk_widget_show(filtering_action.dest_entry);
1396                 gtk_widget_set_sensitive(filtering_action.dest_entry, FALSE);
1397                 gtk_widget_show(filtering_action.dest_btn);
1398                 gtk_widget_set_sensitive(filtering_action.dest_btn, FALSE);
1399                 gtk_widget_show(filtering_action.dest_label);
1400                 gtk_widget_set_sensitive(filtering_action.dest_label, FALSE);
1401                 gtk_widget_hide(filtering_action.recip_label);
1402                 gtk_widget_hide(filtering_action.exec_label);
1403                 gtk_widget_hide(filtering_action.exec_btn);
1404                 gtk_widget_hide(filtering_action.color_optmenu);
1405                 gtk_widget_hide(filtering_action.color_label);
1406                 gtk_widget_hide(filtering_action.score_label);
1407                 gtk_widget_hide(filtering_action.header_label);
1408                 gtk_widget_hide(filtering_action.header_combo);
1409                 gtk_widget_hide(filtering_action.header_entry);
1410                 gtk_widget_set_sensitive(filtering_action.header_entry, FALSE);
1411                 gtk_widget_hide(filtering_action.addressbook_label);
1412                 gtk_widget_hide(filtering_action.addressbook_btn);
1413                 gtk_widget_set_sensitive(filtering_action.addressbook_btn, FALSE);
1414                 break;
1415         case ACTION_MARK:
1416         case ACTION_UNMARK:
1417         case ACTION_LOCK:
1418         case ACTION_UNLOCK:
1419         case ACTION_MARK_AS_READ:
1420         case ACTION_MARK_AS_UNREAD:
1421         case ACTION_MARK_AS_SPAM:
1422         case ACTION_MARK_AS_HAM:
1423         case ACTION_STOP:
1424         case ACTION_HIDE:
1425         case ACTION_IGNORE:
1426         case ACTION_WATCH:
1427         case ACTION_CLEAR_TAGS:
1428                 gtk_widget_show(filtering_action.account_label);
1429                 gtk_widget_set_sensitive(filtering_action.account_label, FALSE);
1430                 gtk_widget_show(filtering_action.account_combo);
1431                 gtk_widget_set_sensitive(filtering_action.account_combo, FALSE);
1432                 gtk_widget_hide(filtering_action.tags_label);
1433                 gtk_widget_set_sensitive(filtering_action.tags_label, FALSE);
1434                 gtk_widget_hide(filtering_action.tags_combo);
1435                 gtk_widget_set_sensitive(filtering_action.tags_combo, FALSE);
1436                 gtk_widget_show(filtering_action.dest_entry);
1437                 gtk_widget_set_sensitive(filtering_action.dest_entry, FALSE);
1438                 gtk_widget_show(filtering_action.dest_btn);
1439                 gtk_widget_set_sensitive(filtering_action.dest_btn, FALSE);
1440                 gtk_widget_show(filtering_action.dest_label);
1441                 gtk_widget_set_sensitive(filtering_action.dest_label, FALSE);
1442                 gtk_widget_hide(filtering_action.recip_label);
1443                 gtk_widget_hide(filtering_action.exec_label);
1444                 gtk_widget_hide(filtering_action.exec_btn);
1445                 gtk_widget_hide(filtering_action.color_optmenu);
1446                 gtk_widget_hide(filtering_action.color_label);
1447                 gtk_widget_hide(filtering_action.score_label);
1448                 gtk_widget_hide(filtering_action.header_label);
1449                 gtk_widget_hide(filtering_action.header_combo);
1450                 gtk_widget_hide(filtering_action.header_entry);
1451                 gtk_widget_set_sensitive(filtering_action.header_entry, FALSE);
1452                 gtk_widget_hide(filtering_action.addressbook_label);
1453                 gtk_widget_hide(filtering_action.addressbook_btn);
1454                 gtk_widget_set_sensitive(filtering_action.addressbook_btn, FALSE);
1455                 break;
1456         case ACTION_FORWARD:
1457                 gtk_widget_show(filtering_action.account_label);
1458                 gtk_widget_set_sensitive(filtering_action.account_label, TRUE);
1459                 gtk_widget_show(filtering_action.account_combo);
1460                 gtk_widget_set_sensitive(filtering_action.account_combo, TRUE);
1461                 gtk_widget_hide(filtering_action.tags_label);
1462                 gtk_widget_set_sensitive(filtering_action.tags_label, FALSE);
1463                 gtk_widget_hide(filtering_action.tags_combo);
1464                 gtk_widget_set_sensitive(filtering_action.tags_combo, FALSE);
1465                 gtk_widget_show(filtering_action.dest_entry);
1466                 gtk_widget_set_sensitive(filtering_action.dest_entry, TRUE);
1467                 gtk_widget_show(filtering_action.dest_btn);
1468                 gtk_widget_set_sensitive(filtering_action.dest_btn, FALSE);
1469                 gtk_widget_hide(filtering_action.dest_label);
1470                 gtk_widget_show(filtering_action.recip_label);
1471                 gtk_widget_set_sensitive(filtering_action.recip_label, TRUE);
1472                 gtk_widget_hide(filtering_action.exec_label);
1473                 gtk_widget_hide(filtering_action.exec_btn);
1474                 gtk_widget_hide(filtering_action.color_optmenu);
1475                 gtk_widget_hide(filtering_action.color_label);
1476                 gtk_widget_hide(filtering_action.score_label);
1477                 gtk_widget_hide(filtering_action.header_label);
1478                 gtk_widget_hide(filtering_action.header_combo);
1479                 gtk_widget_hide(filtering_action.header_entry);
1480                 gtk_widget_set_sensitive(filtering_action.header_entry, FALSE);
1481                 gtk_widget_hide(filtering_action.addressbook_label);
1482                 gtk_widget_hide(filtering_action.addressbook_btn);
1483                 gtk_widget_set_sensitive(filtering_action.addressbook_btn, FALSE);
1484                 break;
1485         case ACTION_FORWARD_AS_ATTACHMENT:
1486                 gtk_widget_show(filtering_action.account_label);
1487                 gtk_widget_set_sensitive(filtering_action.account_label, TRUE);
1488                 gtk_widget_show(filtering_action.account_combo);
1489                 gtk_widget_set_sensitive(filtering_action.account_combo, TRUE);
1490                 gtk_widget_hide(filtering_action.tags_label);
1491                 gtk_widget_set_sensitive(filtering_action.tags_label, FALSE);
1492                 gtk_widget_hide(filtering_action.tags_combo);
1493                 gtk_widget_set_sensitive(filtering_action.tags_combo, FALSE);
1494                 gtk_widget_show(filtering_action.dest_entry);
1495                 gtk_widget_set_sensitive(filtering_action.dest_entry, TRUE);
1496                 gtk_widget_show(filtering_action.dest_btn);
1497                 gtk_widget_set_sensitive(filtering_action.dest_btn, FALSE);
1498                 gtk_widget_hide(filtering_action.dest_label);
1499                 gtk_widget_show(filtering_action.recip_label);
1500                 gtk_widget_set_sensitive(filtering_action.recip_label, TRUE);
1501                 gtk_widget_hide(filtering_action.exec_label);
1502                 gtk_widget_hide(filtering_action.exec_btn);
1503                 gtk_widget_hide(filtering_action.color_optmenu);
1504                 gtk_widget_hide(filtering_action.color_label);
1505                 gtk_widget_hide(filtering_action.score_label);
1506                 gtk_widget_hide(filtering_action.header_label);
1507                 gtk_widget_hide(filtering_action.header_entry);
1508                 gtk_widget_hide(filtering_action.header_combo);
1509                 gtk_widget_set_sensitive(filtering_action.header_entry, FALSE);
1510                 gtk_widget_hide(filtering_action.addressbook_label);
1511                 gtk_widget_hide(filtering_action.addressbook_btn);
1512                 gtk_widget_set_sensitive(filtering_action.addressbook_btn, FALSE);
1513                 break;
1514         case ACTION_REDIRECT:
1515                 gtk_widget_show(filtering_action.account_label);
1516                 gtk_widget_set_sensitive(filtering_action.account_label, TRUE);
1517                 gtk_widget_show(filtering_action.account_combo);
1518                 gtk_widget_set_sensitive(filtering_action.account_combo, TRUE);
1519                 gtk_widget_hide(filtering_action.tags_label);
1520                 gtk_widget_set_sensitive(filtering_action.tags_label, FALSE);
1521                 gtk_widget_hide(filtering_action.tags_combo);
1522                 gtk_widget_set_sensitive(filtering_action.tags_combo, FALSE);
1523                 gtk_widget_show(filtering_action.dest_entry);
1524                 gtk_widget_set_sensitive(filtering_action.dest_entry, TRUE);
1525                 gtk_widget_show(filtering_action.dest_btn);
1526                 gtk_widget_set_sensitive(filtering_action.dest_btn, FALSE);
1527                 gtk_widget_hide(filtering_action.dest_label);
1528                 gtk_widget_show(filtering_action.recip_label);
1529                 gtk_widget_set_sensitive(filtering_action.recip_label, TRUE);
1530                 gtk_widget_hide(filtering_action.exec_label);
1531                 gtk_widget_hide(filtering_action.exec_btn);
1532                 gtk_widget_hide(filtering_action.color_optmenu);
1533                 gtk_widget_hide(filtering_action.color_label);
1534                 gtk_widget_hide(filtering_action.score_label);
1535                 gtk_widget_hide(filtering_action.header_label);
1536                 gtk_widget_hide(filtering_action.header_combo);
1537                 gtk_widget_hide(filtering_action.header_entry);
1538                 gtk_widget_set_sensitive(filtering_action.header_entry, FALSE);
1539                 gtk_widget_hide(filtering_action.addressbook_label);
1540                 gtk_widget_hide(filtering_action.addressbook_btn);
1541                 gtk_widget_set_sensitive(filtering_action.addressbook_btn, FALSE);
1542                 break;
1543         case ACTION_EXECUTE:
1544                 gtk_widget_show(filtering_action.account_label);
1545                 gtk_widget_set_sensitive(filtering_action.account_label, FALSE);
1546                 gtk_widget_show(filtering_action.account_combo);
1547                 gtk_widget_set_sensitive(filtering_action.account_combo, FALSE);
1548                 gtk_widget_hide(filtering_action.tags_label);
1549                 gtk_widget_set_sensitive(filtering_action.tags_label, FALSE);
1550                 gtk_widget_hide(filtering_action.tags_combo);
1551                 gtk_widget_set_sensitive(filtering_action.tags_combo, FALSE);
1552                 gtk_widget_show(filtering_action.dest_entry);
1553                 gtk_widget_set_sensitive(filtering_action.dest_entry, TRUE);
1554                 gtk_widget_hide(filtering_action.dest_btn);
1555                 gtk_widget_hide(filtering_action.dest_label);
1556                 gtk_widget_hide(filtering_action.recip_label);
1557                 gtk_widget_show(filtering_action.exec_label);
1558                 gtk_widget_set_sensitive(filtering_action.exec_btn, TRUE);
1559                 gtk_widget_show(filtering_action.exec_btn);
1560                 gtk_widget_hide(filtering_action.color_optmenu);
1561                 gtk_widget_hide(filtering_action.color_label);
1562                 gtk_widget_hide(filtering_action.score_label);
1563                 gtk_widget_hide(filtering_action.header_label);
1564                 gtk_widget_hide(filtering_action.header_combo);
1565                 gtk_widget_hide(filtering_action.header_entry);
1566                 gtk_widget_set_sensitive(filtering_action.header_entry, FALSE);
1567                 gtk_widget_hide(filtering_action.addressbook_label);
1568                 gtk_widget_hide(filtering_action.addressbook_btn);
1569                 gtk_widget_set_sensitive(filtering_action.addressbook_btn, FALSE);
1570                 break;
1571         case ACTION_COLOR:
1572                 gtk_widget_show(filtering_action.account_label);
1573                 gtk_widget_set_sensitive(filtering_action.account_label, FALSE);
1574                 gtk_widget_show(filtering_action.account_combo);
1575                 gtk_widget_set_sensitive(filtering_action.account_combo, FALSE);
1576                 gtk_widget_hide(filtering_action.tags_label);
1577                 gtk_widget_set_sensitive(filtering_action.tags_label, FALSE);
1578                 gtk_widget_hide(filtering_action.tags_combo);
1579                 gtk_widget_set_sensitive(filtering_action.tags_combo, FALSE);
1580                 gtk_widget_hide(filtering_action.dest_entry);
1581                 gtk_widget_hide(filtering_action.dest_btn);
1582                 gtk_widget_hide(filtering_action.dest_label);
1583                 gtk_widget_hide(filtering_action.recip_label);
1584                 gtk_widget_hide(filtering_action.exec_label);
1585                 gtk_widget_show(filtering_action.exec_btn);
1586                 gtk_widget_set_sensitive(filtering_action.exec_btn, FALSE);
1587                 gtk_widget_show(filtering_action.color_optmenu);
1588                 gtk_widget_show(filtering_action.color_label);
1589                 gtk_widget_hide(filtering_action.score_label);
1590                 gtk_widget_hide(filtering_action.header_label);
1591                 gtk_widget_hide(filtering_action.header_combo);
1592                 gtk_widget_hide(filtering_action.header_entry);
1593                 gtk_widget_set_sensitive(filtering_action.header_entry, FALSE);
1594                 gtk_widget_hide(filtering_action.addressbook_label);
1595                 gtk_widget_hide(filtering_action.addressbook_btn);
1596                 gtk_widget_set_sensitive(filtering_action.addressbook_btn, FALSE);
1597                 break;
1598         case ACTION_CHANGE_SCORE:
1599         case ACTION_SET_SCORE:
1600                 gtk_widget_show(filtering_action.account_label);
1601                 gtk_widget_set_sensitive(filtering_action.account_label, FALSE);
1602                 gtk_widget_show(filtering_action.account_combo);
1603                 gtk_widget_set_sensitive(filtering_action.account_combo, FALSE);
1604                 gtk_widget_hide(filtering_action.tags_label);
1605                 gtk_widget_set_sensitive(filtering_action.tags_label, FALSE);
1606                 gtk_widget_hide(filtering_action.tags_combo);
1607                 gtk_widget_set_sensitive(filtering_action.tags_combo, FALSE);
1608                 gtk_widget_show(filtering_action.dest_entry);
1609                 gtk_widget_set_sensitive(filtering_action.dest_entry, TRUE);
1610                 gtk_widget_hide(filtering_action.dest_btn);
1611                 gtk_widget_hide(filtering_action.dest_label);
1612                 gtk_widget_hide(filtering_action.recip_label);
1613                 gtk_widget_hide(filtering_action.exec_label);
1614                 gtk_widget_show(filtering_action.exec_btn);
1615                 gtk_widget_set_sensitive(filtering_action.exec_btn, FALSE);
1616                 gtk_widget_hide(filtering_action.color_optmenu);
1617                 gtk_widget_hide(filtering_action.color_label);
1618                 gtk_widget_show(filtering_action.score_label);
1619                 gtk_widget_hide(filtering_action.header_label);
1620                 gtk_widget_hide(filtering_action.header_combo);
1621                 gtk_widget_hide(filtering_action.header_entry);
1622                 gtk_widget_set_sensitive(filtering_action.header_entry, FALSE);
1623                 gtk_widget_hide(filtering_action.addressbook_label);
1624                 gtk_widget_hide(filtering_action.addressbook_btn);
1625                 gtk_widget_set_sensitive(filtering_action.addressbook_btn, FALSE);
1626                 break;
1627         case ACTION_ADD_TO_ADDRESSBOOK:
1628                 gtk_widget_hide(filtering_action.account_label);
1629                 gtk_widget_set_sensitive(filtering_action.account_label, FALSE);
1630                 gtk_widget_hide(filtering_action.account_combo);
1631                 gtk_widget_set_sensitive(filtering_action.account_combo, FALSE);
1632                 gtk_widget_hide(filtering_action.tags_label);
1633                 gtk_widget_set_sensitive(filtering_action.tags_label, FALSE);
1634                 gtk_widget_hide(filtering_action.tags_combo);
1635                 gtk_widget_set_sensitive(filtering_action.tags_combo, FALSE);
1636                 gtk_widget_show(filtering_action.dest_entry);
1637                 gtk_widget_set_sensitive(filtering_action.dest_entry, TRUE);
1638                 gtk_widget_hide(filtering_action.dest_btn);
1639                 gtk_widget_set_sensitive(filtering_action.dest_btn, FALSE);
1640                 gtk_widget_hide(filtering_action.dest_label);
1641                 gtk_widget_set_sensitive(filtering_action.dest_label, FALSE);
1642                 gtk_widget_hide(filtering_action.recip_label);
1643                 gtk_widget_hide(filtering_action.exec_label);
1644                 gtk_widget_hide(filtering_action.exec_btn);
1645                 gtk_widget_hide(filtering_action.color_optmenu);
1646                 gtk_widget_hide(filtering_action.color_label);
1647                 gtk_widget_hide(filtering_action.score_label);
1648                 gtk_widget_show(filtering_action.header_label);
1649                 gtk_widget_show(filtering_action.header_combo);
1650                 gtk_widget_show(filtering_action.header_entry);
1651                 gtk_widget_set_sensitive(filtering_action.header_entry, TRUE);
1652                 gtk_widget_show(filtering_action.addressbook_label);
1653                 gtk_widget_show(filtering_action.addressbook_btn);
1654                 gtk_widget_set_sensitive(filtering_action.addressbook_btn, TRUE);
1655                 break;
1656         case ACTION_SET_TAG:
1657         case ACTION_UNSET_TAG:
1658                 gtk_widget_show(filtering_action.account_label);
1659                 gtk_widget_set_sensitive(filtering_action.account_label, FALSE);
1660                 gtk_widget_show(filtering_action.account_combo);
1661                 gtk_widget_set_sensitive(filtering_action.account_combo, FALSE);
1662                 gtk_widget_show(filtering_action.tags_label);
1663                 gtk_widget_set_sensitive(filtering_action.tags_label, TRUE);
1664                 gtk_widget_show(filtering_action.tags_combo);
1665                 gtk_widget_set_sensitive(filtering_action.tags_combo, TRUE);
1666                 gtk_widget_hide(filtering_action.dest_entry);
1667                 gtk_widget_set_sensitive(filtering_action.dest_entry, FALSE);
1668                 gtk_widget_hide(filtering_action.dest_btn);
1669                 gtk_widget_set_sensitive(filtering_action.dest_btn, FALSE);
1670                 gtk_widget_hide(filtering_action.dest_label);
1671                 gtk_widget_hide(filtering_action.recip_label);
1672                 gtk_widget_set_sensitive(filtering_action.recip_label, TRUE);
1673                 gtk_widget_hide(filtering_action.exec_label);
1674                 gtk_widget_hide(filtering_action.exec_btn);
1675                 gtk_widget_hide(filtering_action.color_optmenu);
1676                 gtk_widget_hide(filtering_action.color_label);
1677                 gtk_widget_hide(filtering_action.score_label);
1678                 gtk_widget_hide(filtering_action.header_label);
1679                 gtk_widget_hide(filtering_action.header_combo);
1680                 gtk_widget_hide(filtering_action.header_entry);
1681                 gtk_widget_set_sensitive(filtering_action.header_entry, FALSE);
1682                 gtk_widget_hide(filtering_action.addressbook_label);
1683                 gtk_widget_hide(filtering_action.addressbook_btn);
1684                 gtk_widget_set_sensitive(filtering_action.addressbook_btn, FALSE);
1685                 break;
1686         }
1687 }
1688
1689 static void prefs_filtering_action_reset_dialog(void)
1690 {
1691         gtk_list_select_item(GTK_LIST(filtering_action.action_type_list), 0);
1692         gtk_list_select_item(GTK_LIST(filtering_action.account_list), 0);
1693         gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), "");
1694 }
1695
1696 static GtkListStore* prefs_filtering_action_create_data_store(void)
1697 {
1698         return gtk_list_store_new(N_PFA_COLUMNS,
1699                                   G_TYPE_STRING,
1700                                   G_TYPE_BOOLEAN,
1701                                   -1);
1702 }
1703
1704 static void prefs_filtering_action_list_view_insert_action(GtkWidget   *list_view,
1705                                                            GtkTreeIter *row,
1706                                                            const gchar *action,
1707                                                            gboolean     is_valid)
1708 {
1709         GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model
1710                                         (GTK_TREE_VIEW(list_view)));
1711         GtkTreeIter iter;
1712         
1713         
1714         /* see if row exists, if not append */
1715         if (row == NULL)
1716                 gtk_list_store_append(store, &iter);
1717         else
1718                 iter = *row;
1719
1720         gtk_list_store_set(store, &iter,
1721                            PFA_ACTION, action,
1722                            PFA_VALID_ACTION, is_valid,
1723                            -1);
1724 }
1725
1726 static GtkWidget *prefs_filtering_action_list_view_create(void)
1727 {
1728         GtkTreeView *list_view;
1729         GtkTreeModel *model;
1730         GtkTreeSelection *selector;
1731
1732         model = GTK_TREE_MODEL(prefs_filtering_action_create_data_store());
1733         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1734         g_object_unref(model);  
1735         
1736         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1737
1738         selector = gtk_tree_view_get_selection(list_view);
1739         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1740         gtk_tree_selection_set_select_function
1741                 (selector, prefs_filtering_actions_selected, NULL, NULL);
1742         
1743         /* create the columns */
1744         prefs_filtering_action_create_list_view_columns(list_view);
1745
1746         return GTK_WIDGET(list_view);
1747 }
1748
1749 static void prefs_filtering_action_create_list_view_columns(GtkTreeView *list_view)
1750 {
1751         GtkTreeViewColumn *column;
1752         GtkCellRenderer *renderer;
1753
1754         renderer = gtk_cell_renderer_text_new();
1755         column = gtk_tree_view_column_new_with_attributes
1756                 (_("Current action list"),
1757                  renderer,
1758                  "text", PFA_ACTION,
1759                  NULL);
1760         gtk_tree_view_append_column(list_view, column);         
1761 }
1762
1763 static gboolean prefs_filtering_actions_selected
1764                         (GtkTreeSelection *selector,
1765                          GtkTreeModel *model, 
1766                          GtkTreePath *path,
1767                          gboolean currently_selected,
1768                          gpointer data)
1769 {
1770         gchar *action_str;
1771         FilteringAction *action;
1772         GSList * action_list;
1773         gint list_id;
1774         GtkTreeIter iter;
1775         gboolean is_valid;
1776
1777         if (currently_selected)
1778                 return TRUE;
1779
1780         if (!gtk_tree_model_get_iter(model, &iter, path))
1781                 return TRUE;
1782
1783         gtk_tree_model_get(model, &iter, 
1784                            PFA_VALID_ACTION,  &is_valid,
1785                            -1);
1786
1787         if (!is_valid) {
1788                 prefs_filtering_action_reset_dialog();
1789                 return TRUE;
1790         }
1791
1792         gtk_tree_model_get(model, &iter, 
1793                            PFA_ACTION, &action_str,
1794                            -1);
1795
1796         action_list = matcher_parser_get_action_list(action_str);
1797         g_free(action_str);
1798
1799         if (action_list == NULL)
1800                 return TRUE;
1801
1802         action = action_list->data;
1803         g_slist_free(action_list);
1804
1805         if (action->destination)
1806                 gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), action->destination);
1807         else
1808                 gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), "");
1809
1810         switch(action->type) {
1811         case MATCHACTION_MOVE:
1812                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1813                                      ACTION_MOVE);
1814                 break;
1815         case MATCHACTION_COPY:
1816                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1817                                      ACTION_COPY);
1818                 break;
1819         case MATCHACTION_DELETE:
1820                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1821                                      ACTION_DELETE);
1822                 break;
1823         case MATCHACTION_MARK:
1824                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1825                                      ACTION_MARK);
1826                 break;
1827         case MATCHACTION_UNMARK:
1828                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1829                                      ACTION_UNMARK);
1830                 break;
1831         case MATCHACTION_LOCK:
1832                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1833                                      ACTION_LOCK);
1834                 break;
1835         case MATCHACTION_UNLOCK:
1836                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1837                                      ACTION_UNLOCK);
1838                 break;
1839         case MATCHACTION_MARK_AS_READ:
1840                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1841                                      ACTION_MARK_AS_READ);
1842                 break;
1843         case MATCHACTION_MARK_AS_UNREAD:
1844                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1845                                      ACTION_MARK_AS_UNREAD);
1846                 break;
1847         case MATCHACTION_MARK_AS_SPAM:
1848                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1849                                      ACTION_MARK_AS_SPAM);
1850                 break;
1851         case MATCHACTION_MARK_AS_HAM:
1852                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1853                                      ACTION_MARK_AS_HAM);
1854                 break;
1855         case MATCHACTION_FORWARD:
1856                 list_id = get_list_id_from_account_id(action->account_id);
1857                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1858                                      ACTION_FORWARD);
1859                 gtk_list_select_item(GTK_LIST(filtering_action.account_list),
1860                                      list_id);
1861                 break;
1862         case MATCHACTION_FORWARD_AS_ATTACHMENT:
1863                 list_id = get_list_id_from_account_id(action->account_id);
1864                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1865                                      ACTION_FORWARD_AS_ATTACHMENT);
1866                 gtk_list_select_item(GTK_LIST(filtering_action.account_list),
1867                                      list_id);
1868                 break;
1869         case MATCHACTION_REDIRECT:
1870                 list_id = get_list_id_from_account_id(action->account_id);
1871                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1872                                      ACTION_REDIRECT);
1873                 gtk_list_select_item(GTK_LIST(filtering_action.account_list),
1874                                      list_id);
1875                 break;
1876         case MATCHACTION_EXECUTE:
1877                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1878                                      ACTION_EXECUTE);
1879                 break;
1880         case MATCHACTION_COLOR:
1881                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1882                                      ACTION_COLOR);
1883                 gtk_option_menu_set_history(GTK_OPTION_MENU(filtering_action.color_optmenu), action->labelcolor);     
1884                 break;
1885         case MATCHACTION_CHANGE_SCORE:
1886                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1887                                      ACTION_CHANGE_SCORE);
1888                 break;
1889         case MATCHACTION_SET_SCORE:
1890                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1891                                      ACTION_SET_SCORE);
1892                 break;
1893         case MATCHACTION_STOP:
1894                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1895                                      ACTION_STOP);
1896                 break;
1897         case MATCHACTION_HIDE:
1898                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1899                                      ACTION_HIDE);
1900                 break;
1901         case MATCHACTION_IGNORE:
1902                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1903                                      ACTION_IGNORE);
1904                 break;
1905         case MATCHACTION_WATCH:
1906                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1907                                      ACTION_WATCH);
1908                 break;
1909         case MATCHACTION_ADD_TO_ADDRESSBOOK:
1910                 if (action->header)
1911                         gtk_entry_set_text(GTK_ENTRY(filtering_action.header_entry), action->header);
1912                 else
1913                         gtk_entry_set_text(GTK_ENTRY(filtering_action.header_entry), "");
1914                 gtk_list_select_item(GTK_LIST(filtering_action.action_type_list),
1915                                      ACTION_ADD_TO_ADDRESSBOOK);
1916         }
1917
1918         filteringaction_free(action); /* XXX: memleak */
1919         return TRUE;
1920 }