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