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