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