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