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