2008-08-27 [colin] 3.5.0cvs76
[claws.git] / src / prefs_filtering_action.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2003-2007 the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #include "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         g_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 #if GTK_CHECK_VERSION(2, 8, 0)
546         exec_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
547 #else
548         exec_btn = gtk_button_new_with_label (_("Info..."));
549 #endif
550         gtk_box_pack_start (GTK_BOX (hbox1), exec_btn, FALSE, FALSE, 0);
551         g_signal_connect (G_OBJECT (exec_btn), "clicked",
552                           G_CALLBACK(prefs_filtering_action_exec_info),
553                           window);
554
555         /* register / substitute / delete */
556
557         reg_hbox = gtk_hbox_new(FALSE, 4);
558         gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
559
560         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
561         gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
562         gtk_widget_set_size_request(arrow, -1, 16);
563
564         btn_hbox = gtk_hbox_new(TRUE, 4);
565         gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
566
567         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
568         gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
569         g_signal_connect(G_OBJECT(reg_btn), "clicked",
570                          G_CALLBACK(prefs_filtering_action_register_cb), NULL);
571
572         subst_btn = gtkut_get_replace_btn(_("Replace"));
573         gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
574         g_signal_connect(G_OBJECT(subst_btn), "clicked",
575                          G_CALLBACK(prefs_filtering_action_substitute_cb),
576                          NULL);
577
578         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
579         gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
580         g_signal_connect(G_OBJECT(del_btn), "clicked",
581                          G_CALLBACK(prefs_filtering_action_delete_cb), NULL);
582
583         action_hbox = gtk_hbox_new(FALSE, 8);
584         gtk_box_pack_start(GTK_BOX(vbox1), action_hbox, TRUE, TRUE, 0);
585
586         action_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
587         gtk_widget_set_size_request(action_scrolledwin, -1, 150);
588         gtk_box_pack_start(GTK_BOX(action_hbox), action_scrolledwin,
589                            TRUE, TRUE, 0);
590         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(action_scrolledwin),
591                                             GTK_SHADOW_ETCHED_IN);
592         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(action_scrolledwin),
593                                        GTK_POLICY_AUTOMATIC,
594                                        GTK_POLICY_AUTOMATIC);
595
596         action_list_view = prefs_filtering_action_list_view_create();
597         gtk_container_add(GTK_CONTAINER(action_scrolledwin), action_list_view);
598
599         btn_vbox = gtk_vbox_new(FALSE, 8);
600         gtk_box_pack_start(GTK_BOX(action_hbox), btn_vbox, FALSE, FALSE, 0);
601
602         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
603         gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0);
604         g_signal_connect(G_OBJECT(up_btn), "clicked",
605                          G_CALLBACK(prefs_filtering_action_up), NULL);
606
607         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
608         gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
609         g_signal_connect(G_OBJECT(down_btn), "clicked",
610                          G_CALLBACK(prefs_filtering_action_down), NULL);
611
612         if (!geometry.min_height) {
613                 geometry.min_width = 490;
614                 geometry.min_height = 348;
615         }
616
617         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
618                                       GDK_HINT_MIN_SIZE);
619         gtk_widget_set_size_request(window, prefs_common.filteringactionwin_width,
620                                     prefs_common.filteringactionwin_height);
621
622         gtk_widget_show_all(window);
623
624         filtering_action.window    = window;
625         filtering_action.label1 = label1;
626         filtering_action.label2 = label2;
627         filtering_action.label3 = label3;
628         filtering_action.action_combo = action_combo;
629         filtering_action.account_combo = account_combo;
630         filtering_action.tags_combo = tags_combo;
631         filtering_action.dest_entry = dest_entry;
632         filtering_action.dest_btn = dest_btn;
633         filtering_action.exec_btn = exec_btn;
634         filtering_action.color_optmenu = color_optmenu;
635         filtering_action.score_entry = score_entry;
636         filtering_action.header_combo = header_combo;
637         filtering_action.header_entry = header_entry;
638         filtering_action.addressbook_btn = addressbook_btn;
639         filtering_action.ok_btn = ok_btn;
640         filtering_action.action_list_view = action_list_view;
641         
642         prefs_filtering_action_type_selection_changed(NULL, NULL);
643 }
644
645 /*!
646  *\brief        Set the contents of a row
647  *
648  *\param        row Index of row to set
649  *\param        prop Condition to set
650  *
651  */
652 static void prefs_filtering_action_list_view_set_row(GtkTreeIter *row, 
653                                                      FilteringAction *action)
654 {
655         gchar buf[256];
656
657         if (row == NULL && action == NULL) {
658                 prefs_filtering_action_list_view_insert_action
659                         (filtering_action.action_list_view,
660                          NULL, _("(New)"), FALSE);
661                 return;
662         }                        
663
664         filteringaction_to_string(buf, sizeof buf, action);
665
666         prefs_filtering_action_list_view_insert_action
667                         (filtering_action.action_list_view,
668                          row, buf, TRUE);
669 }
670
671 /*!
672  *\brief        Initializes dialog with a set of conditions
673  *
674  *\param        matchers List of conditions
675  */
676 static void prefs_filtering_action_set_dialog(GSList *action_list)
677 {
678         GSList *cur;
679
680         gtk_list_store_clear(GTK_LIST_STORE(gtk_tree_view_get_model
681                         (GTK_TREE_VIEW(filtering_action.action_list_view))));
682
683         prefs_filtering_action_list_view_set_row(NULL, NULL);
684         if (action_list != NULL) {
685                 for (cur = action_list; cur != NULL;
686                      cur = g_slist_next(cur)) {
687                         FilteringAction *action;
688                         action = (FilteringAction *) cur->data;
689                         prefs_filtering_action_list_view_set_row(NULL, action);
690                 }
691         }
692         
693         prefs_filtering_action_reset_dialog();
694         
695         combobox_set_sensitive(GTK_COMBO_BOX(filtering_action.action_combo), 9,
696                         (tags_get_size() > 0) ? TRUE : FALSE);
697 }
698
699 /*!
700  *\brief        Converts current actions in list box in
701  *              an action list used by the filtering system.
702  *
703  *\return       GSList * List of actions.
704  */
705 static GSList *prefs_filtering_action_get_list(void)
706 {
707         gchar *action_str;
708         gboolean is_valid;
709         gint row = 1;
710         GSList *action_list;
711         GtkTreeView *list_view = GTK_TREE_VIEW(filtering_action.action_list_view);
712         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
713         GtkTreeIter iter;
714
715         action_list = NULL;
716
717         while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
718
719                 gtk_tree_model_get(model, &iter, 
720                                    PFA_ACTION, &action_str,
721                                    PFA_VALID_ACTION, &is_valid,
722                                    -1);
723
724                 if (is_valid) {                            
725                         GSList * tmp_action_list;
726                         tmp_action_list = matcher_parser_get_action_list(action_str);
727                         
728                         if (tmp_action_list == NULL) {
729                                 g_free(action_str);
730                                 break;
731                         }                               
732
733                         action_list = g_slist_concat(action_list,
734                             tmp_action_list);
735                 }
736
737                 g_free(action_str);
738                 action_str = NULL;
739                 row ++;
740                 
741         }
742
743         return action_list;
744 }
745
746 /*!
747  *\brief        Returns account ID from the given list index
748  *
749  *\return       gint account ID
750  */
751 static gint get_account_id_from_list_id(gint list_id)
752 {
753         GList * accounts;
754
755         for (accounts = account_get_list() ; accounts != NULL;
756              accounts = accounts->next) {
757                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
758
759                 if (list_id == 0)
760                         return ac->account_id;
761                 list_id--;
762         }
763         return 0;
764 }
765
766 /*!
767  *\brief        Returns list index from the given account ID
768  *
769  *\return       gint list index
770  */
771 static gint get_list_id_from_account_id(gint account_id)
772 {
773         GList * accounts;
774         gint list_id = 0;
775
776         for (accounts = account_get_list() ; accounts != NULL;
777              accounts = accounts->next) {
778                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
779
780                 if (account_id == ac->account_id)
781                         return list_id;
782                 list_id++;
783         }
784         return 0;
785 }
786
787
788 /*!
789  *\brief        Returns parser action ID from internal action ID
790  *
791  *\return       gint parser action ID
792  */
793 static gint prefs_filtering_action_get_matching_from_action(Action action_id)
794 {
795         switch (action_id) {
796         case ACTION_MOVE:
797                 return MATCHACTION_MOVE;
798         case ACTION_SET_TAG:
799                 return MATCHACTION_SET_TAG;
800         case ACTION_UNSET_TAG:
801                 return MATCHACTION_UNSET_TAG;
802         case ACTION_CLEAR_TAGS:
803                 return MATCHACTION_CLEAR_TAGS;
804         case ACTION_COPY:
805                 return MATCHACTION_COPY;
806         case ACTION_DELETE:
807                 return MATCHACTION_DELETE;
808         case ACTION_MARK:
809                 return MATCHACTION_MARK;
810         case ACTION_UNMARK:
811                 return MATCHACTION_UNMARK;
812         case ACTION_LOCK:
813                 return MATCHACTION_LOCK;
814         case ACTION_UNLOCK:
815                 return MATCHACTION_UNLOCK;
816         case ACTION_MARK_AS_READ:
817                 return MATCHACTION_MARK_AS_READ;
818         case ACTION_MARK_AS_UNREAD:
819                 return MATCHACTION_MARK_AS_UNREAD;
820         case ACTION_MARK_AS_SPAM:
821                 return MATCHACTION_MARK_AS_SPAM;
822         case ACTION_MARK_AS_HAM:
823                 return MATCHACTION_MARK_AS_HAM;
824         case ACTION_FORWARD:
825                 return MATCHACTION_FORWARD;
826         case ACTION_FORWARD_AS_ATTACHMENT:
827                 return MATCHACTION_FORWARD_AS_ATTACHMENT;
828         case ACTION_REDIRECT:
829                 return MATCHACTION_REDIRECT;
830         case ACTION_EXECUTE:
831                 return MATCHACTION_EXECUTE;
832         case ACTION_COLOR:
833                 return MATCHACTION_COLOR;
834         case ACTION_HIDE:
835                 return MATCHACTION_HIDE;
836         case ACTION_IGNORE:
837                 return MATCHACTION_IGNORE;
838         case ACTION_WATCH:
839                 return MATCHACTION_WATCH;
840         case ACTION_STOP:
841                 return MATCHACTION_STOP;
842         case ACTION_CHANGE_SCORE:
843                 return MATCHACTION_CHANGE_SCORE;
844         case ACTION_SET_SCORE:
845                 return MATCHACTION_SET_SCORE;
846         case ACTION_ADD_TO_ADDRESSBOOK:
847                 return MATCHACTION_ADD_TO_ADDRESSBOOK;
848         default:
849                 return -1;
850         }
851 }
852
853 /*!
854  *\brief        Returns action from the content of the dialog
855  *
856  *\param        alert specifies whether alert dialog boxes should be shown
857  *                or not.
858  *
859  *\return       FilteringAction * action entered in the dialog box.
860  */
861 static FilteringAction * prefs_filtering_action_dialog_to_action(gboolean alert)
862 {
863         Action action_id;
864         gint action_type;
865         gint list_id;
866         gint account_id;
867         gchar * destination = NULL;
868         gint labelcolor = 0;
869         FilteringAction * action;
870         gint score;
871         gchar * header = NULL;
872         
873         action_id = combobox_get_active_data(GTK_COMBO_BOX(filtering_action.action_combo));
874         action_type = prefs_filtering_action_get_matching_from_action(action_id);
875         list_id = gtk_combo_box_get_active(GTK_COMBO_BOX(filtering_action.account_combo));
876         account_id = get_account_id_from_list_id(list_id);
877         score = 0;
878         destination = NULL;
879         
880         switch (action_id) {
881         case ACTION_MOVE:
882         case ACTION_COPY:
883         case ACTION_EXECUTE:
884                 destination = gtk_editable_get_chars(GTK_EDITABLE(filtering_action.dest_entry), 0, -1);
885                 if (*destination == '\0') {
886                         if (alert)
887                                 alertpanel_error(action_id == ACTION_EXECUTE 
888                                                  ? _("Command line not set")
889                                                  : _("Destination is not set."));
890                         g_free(destination);
891                         return NULL;
892                 }
893                 break;
894         case ACTION_FORWARD:
895         case ACTION_FORWARD_AS_ATTACHMENT:
896         case ACTION_REDIRECT:
897                 destination = gtk_editable_get_chars(GTK_EDITABLE(filtering_action.dest_entry), 0, -1);
898                 if (*destination == '\0') {
899                         if (alert)
900                                 alertpanel_error(_("Recipient is not set."));
901                         g_free(destination);
902                         return NULL;
903                 }
904                 break;
905         case ACTION_COLOR:
906                 labelcolor = colorlabel_get_color_menu_active_item(
907                         gtk_cmoption_menu_get_menu(GTK_CMOPTION_MENU(filtering_action.color_optmenu)));
908                 destination = NULL;     
909                 break;
910         case ACTION_CHANGE_SCORE:
911         case ACTION_SET_SCORE:
912                 score = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(
913                                                 filtering_action.score_entry));
914                 if (!score && action_id == ACTION_CHANGE_SCORE) {
915                         if (alert)
916                                 alertpanel_error(_("Score is not set"));
917                         return NULL;
918                 }
919                 break;
920         case ACTION_ADD_TO_ADDRESSBOOK:
921                 header = gtk_editable_get_chars(GTK_EDITABLE(filtering_action.header_entry), 0, -1);
922                 if (*header == '\0') {
923                         if (alert)
924                                 alertpanel_error(_("Header is not set."));
925                         g_free(header);
926                         return NULL;
927                 }
928                 destination = gtk_editable_get_chars(GTK_EDITABLE(filtering_action.dest_entry), 0, -1);
929                 if (*destination == '\0') {
930                         if (alert)
931                                 alertpanel_error(_("Target addressbook/folder is not set."));
932                         g_free(destination);
933                         return NULL;
934                 }
935                 break;
936         case ACTION_SET_TAG:
937         case ACTION_UNSET_TAG:
938                 destination = gtk_combo_box_get_active_text(GTK_COMBO_BOX(filtering_action.tags_combo));
939                 if (!destination || *destination == '\0') {
940                         if (alert)
941                                 alertpanel_error(_("Tag name is empty."));
942                         g_free(destination);
943                         return NULL;
944                 }
945                 break;
946         case ACTION_STOP:
947         case ACTION_HIDE:
948         case ACTION_IGNORE:
949         case ACTION_WATCH:
950         case ACTION_DELETE:
951         case ACTION_MARK:
952         case ACTION_UNMARK:
953         case ACTION_LOCK:
954         case ACTION_UNLOCK:
955         case ACTION_MARK_AS_READ:
956         case ACTION_MARK_AS_UNREAD:
957         case ACTION_MARK_AS_SPAM:
958         case ACTION_MARK_AS_HAM:
959         case ACTION_CLEAR_TAGS:
960         default:
961                 break;
962         }
963         action = filteringaction_new(action_type, account_id,
964             destination, labelcolor, score, header);
965         
966         g_free(destination);
967         return action;
968 }
969
970 /*!
971  *\brief        Signal handler for register button
972  */
973 static void prefs_filtering_action_register_cb(void)
974 {
975         FilteringAction *action;
976         
977         action = prefs_filtering_action_dialog_to_action(TRUE);
978         if (action == NULL)
979                 return;
980
981         prefs_filtering_action_list_view_set_row(NULL, action);
982
983         filteringaction_free(action);
984         /* GTK 1 NOTE:
985          * (presumably gtk_list_select_item(), called by 
986          * prefs_filtering_action_reset_dialog() activates 
987          * what seems to be a bug. this causes any other 
988          * list items to be unselectable)
989          * prefs_filtering_action_reset_dialog(); */
990         gtk_combo_box_set_active(GTK_COMBO_BOX(filtering_action.account_combo), 0);
991         gtk_combo_box_set_active(GTK_COMBO_BOX(filtering_action.tags_combo), 0);
992         gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), "");
993 }
994
995 /*!
996  *\brief        Signal handler for substitute button
997  */
998 static void prefs_filtering_action_substitute_cb(void)
999 {
1000         GtkTreeView *list_view = GTK_TREE_VIEW
1001                         (filtering_action.action_list_view);
1002         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
1003         GtkTreeModel *model;
1004         gboolean is_valid;
1005         GtkTreeIter row;
1006         FilteringAction *action;
1007
1008         if (!gtk_tree_selection_get_selected(selection, &model, &row))
1009                 return;
1010
1011         gtk_tree_model_get(model, &row, PFA_VALID_ACTION, &is_valid, -1);
1012         if (!is_valid)
1013                 return;
1014
1015         action = prefs_filtering_action_dialog_to_action(TRUE);
1016         if (action == NULL)
1017                 return;
1018
1019         prefs_filtering_action_list_view_set_row(&row, action);
1020
1021         filteringaction_free(action);
1022
1023         prefs_filtering_action_reset_dialog();
1024 }
1025
1026 /*!
1027  *\brief        Signal handler for delete button
1028  */
1029 static void prefs_filtering_action_delete_cb(void)
1030 {
1031         GtkTreeView *list_view = GTK_TREE_VIEW
1032                         (filtering_action.action_list_view);
1033         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
1034         GtkTreeModel *model;
1035         gboolean is_valid;
1036         GtkTreeIter row;
1037
1038         if (!gtk_tree_selection_get_selected(selection, &model, &row))
1039                 return;
1040
1041         gtk_tree_model_get(model, &row, PFA_VALID_ACTION, &is_valid, -1);
1042         if (!is_valid)
1043                 return;
1044
1045         gtk_list_store_remove(GTK_LIST_STORE(model), &row);             
1046
1047         prefs_filtering_action_reset_dialog();
1048 }
1049
1050 /*!
1051  *\brief        Signal handler for 'move up' button
1052  */
1053 static void prefs_filtering_action_up(void)
1054 {
1055         GtkTreePath *prev, *sel, *try;
1056         GtkTreeIter isel;
1057         GtkListStore *store = NULL;
1058         GtkTreeModel *model = NULL;
1059         GtkTreeIter iprev;
1060         
1061         if (!gtk_tree_selection_get_selected
1062                 (gtk_tree_view_get_selection
1063                         (GTK_TREE_VIEW(filtering_action.action_list_view)),
1064                  &model,        
1065                  &isel))
1066                 return;
1067         store = (GtkListStore *)model;
1068         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
1069         if (!sel)
1070                 return;
1071         
1072         /* no move if we're at row 0 or 1, looks phony, but other
1073          * solutions are more convoluted... */
1074         try = gtk_tree_path_copy(sel);
1075         if (!gtk_tree_path_prev(try) || !gtk_tree_path_prev(try)) {
1076                 gtk_tree_path_free(try);
1077                 gtk_tree_path_free(sel);
1078                 return;
1079         }
1080         gtk_tree_path_free(try);
1081
1082         prev = gtk_tree_path_copy(sel);         
1083         if (gtk_tree_path_prev(prev)) {
1084                 gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
1085                                         &iprev, prev);
1086                 gtk_list_store_swap(store, &iprev, &isel);
1087                 /* XXX: GTK2 select row?? */
1088         }
1089
1090         gtk_tree_path_free(sel);
1091         gtk_tree_path_free(prev);
1092 }
1093
1094 /*!
1095  *\brief        Signal handler for 'move down' button
1096  */
1097 static void prefs_filtering_action_down(void)
1098 {
1099         GtkListStore *store = NULL;
1100         GtkTreeModel *model = NULL;
1101         GtkTreeIter next, sel;
1102         GtkTreePath *try;
1103         
1104         if (!gtk_tree_selection_get_selected
1105                 (gtk_tree_view_get_selection
1106                         (GTK_TREE_VIEW(filtering_action.action_list_view)),
1107                  &model,
1108                  &sel))
1109                 return;
1110         store = (GtkListStore *)model;
1111         try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
1112         if (!try) 
1113                 return;
1114         
1115         /* move when not at row 0 ... */
1116         if (gtk_tree_path_prev(try)) {
1117                 next = sel;
1118                 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next))
1119                         gtk_list_store_swap(store, &next, &sel);
1120         }
1121                 
1122         gtk_tree_path_free(try);
1123 }
1124
1125 /*!
1126  *\brief        Handle key press
1127  *
1128  *\param        widget Widget receiving key press
1129  *\param        event Key event
1130  *\param        data User data
1131  */
1132 static gboolean prefs_filtering_action_key_pressed(GtkWidget *widget,
1133     GdkEventKey *event, gpointer data)
1134 {
1135         if (event && event->keyval == GDK_Escape) {
1136                 prefs_filtering_action_cancel();
1137                 return TRUE;            
1138         }
1139         return FALSE;
1140 }
1141
1142 /*!
1143  *\brief        Cancel matcher dialog
1144  */
1145 static void prefs_filtering_action_cancel(void)
1146 {
1147         gtk_widget_destroy(filtering_action.window);
1148         filtering_action.window = NULL;
1149         inc_unlock();
1150 }
1151
1152 /*!
1153  *\brief        Accept current matchers
1154  */
1155 static void prefs_filtering_action_ok(void)
1156 {
1157         GSList * action_list;
1158         GSList * cur;
1159
1160         action_list = prefs_filtering_action_get_list();
1161
1162         if (action_list == NULL) {
1163                 alertpanel_error(_("No action was defined."));
1164                 return;
1165         }
1166
1167         if (filtering_action_callback != NULL)
1168                 filtering_action_callback(action_list);
1169         for(cur = action_list ; cur != NULL ; cur = cur->next) {
1170                 filteringaction_free(cur->data);
1171         }
1172         g_slist_free(action_list);
1173
1174         gtk_widget_destroy(filtering_action.window);
1175         filtering_action.window = NULL;
1176         inc_unlock();
1177 }
1178
1179 /*!
1180  *\brief        Called when closing dialog box
1181  *
1182  *\param        widget Dialog widget
1183  *\param        event Event info
1184  *\param        data User data
1185  *
1186  *\return       gint TRUE
1187  */
1188 static gint prefs_filtering_action_deleted(GtkWidget *widget,
1189     GdkEventAny *event, gpointer data)
1190 {
1191         prefs_filtering_action_cancel();
1192         return TRUE;
1193 }
1194
1195 /*
1196  * Strings describing exec format strings
1197  * 
1198  * When adding new lines, remember to put 2 strings for each line
1199  */
1200 static gchar *exec_desc_strings[] = {
1201         "%%",   N_("literal %"),
1202         "%s",   N_("Subject"),
1203         "%f",   N_("From"),
1204         "%t",   N_("To"),
1205         "%c",   N_("Cc"),
1206         "%d",   N_("Date"),
1207         "%i",   N_("Message-ID"),
1208         "%n",   N_("Newsgroups"),
1209         "%r",   N_("References"),
1210         "%F",   N_("filename (should not be modified)"),
1211         "\\n",  N_("new line"),
1212         "\\",   N_("escape character for quotes"),
1213         "\\\"", N_("quote character"),
1214         NULL, NULL
1215 };
1216
1217 static DescriptionWindow exec_desc_win = { 
1218         NULL,
1219         NULL, 
1220         2,
1221         N_("Filtering Action: 'Execute'"),
1222         N_("'Execute' allows you to send a message or message element "
1223            "to an external program or script.\n"
1224            "The following symbols can be used:"),
1225        exec_desc_strings
1226 };
1227
1228 /*!
1229  *\brief        Show Execute action's info
1230  */
1231 static void prefs_filtering_action_exec_info(GtkWidget *widget, GtkWidget *parent)
1232 {
1233         exec_desc_win.parent = parent;
1234         description_window_create(&exec_desc_win);
1235 }
1236
1237 static void prefs_filtering_action_select_dest(void)
1238 {
1239         FolderItem *dest;
1240         gchar * path;
1241
1242         dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL, FALSE);
1243         if (!dest) return;
1244
1245         path = folder_item_get_identifier(dest);
1246
1247         gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), path);
1248         g_free(path);
1249 }
1250
1251 static void prefs_filtering_action_select_addressbook(void)
1252 {
1253         const gchar *folderpath = NULL;
1254         gchar *new_path = NULL;
1255
1256         folderpath = gtk_entry_get_text(GTK_ENTRY(filtering_action.dest_entry));
1257         new_path = addressbook_folder_selection(folderpath);
1258         if (new_path) {
1259                 gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), new_path);
1260                 g_free(new_path);
1261         } 
1262 }
1263
1264 static void prefs_filtering_action_enable_widget(GtkWidget* widget, const gboolean enable)
1265 {
1266         g_return_if_fail(widget != NULL);
1267
1268         if(enable == TRUE)
1269         {
1270                 if(GTK_IS_COMBO_BOX(widget) || GTK_IS_COMBO_BOX_ENTRY(widget))
1271                         gtk_combo_box_set_active(GTK_COMBO_BOX(widget), 0);
1272                 else if(GTK_IS_SPIN_BUTTON(widget))
1273                         gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget), 0);
1274                 else if(GTK_IS_ENTRY(widget)) {
1275                         gtk_entry_set_text(GTK_ENTRY(widget), "");
1276                 } else if(GTK_IS_CMOPTION_MENU(widget))
1277                         gtk_cmoption_menu_set_history(GTK_CMOPTION_MENU(widget), 0);
1278                 
1279                 gtk_widget_set_sensitive(widget, TRUE);
1280                 gtk_widget_show(widget);
1281         } else {
1282                 gtk_widget_set_sensitive(widget, FALSE);
1283                 gtk_widget_hide(widget);
1284         }
1285 }
1286
1287 #define ACTION_SEND(x)   (x == ACTION_FORWARD || x == ACTION_REDIRECT || \
1288                           x == ACTION_FORWARD_AS_ATTACHMENT)
1289 #define ACTION_SCORE(x)  (x == ACTION_CHANGE_SCORE || x == ACTION_SET_SCORE)
1290 #define ACTION_TAG(x)    (x == ACTION_SET_TAG || x == ACTION_UNSET_TAG)
1291 #define ACTION_FILEOP(x) (x == ACTION_MOVE || x == ACTION_COPY)
1292
1293 static void prefs_filtering_action_type_selection_changed(GtkWidget *combo,
1294                                                           gpointer user_data)
1295 {
1296         gint value;
1297
1298         value = combobox_get_active_data(GTK_COMBO_BOX(filtering_action.action_combo));
1299
1300         if (filtering_action.current_action != value) {
1301                 if (ACTION_SEND(filtering_action.current_action)) {
1302                         debug_print("unregistering address completion entry\n");
1303                         address_completion_unregister_entry(GTK_ENTRY(filtering_action.dest_entry));
1304                         address_completion_end(filtering_action.window);
1305                 }
1306                 if (ACTION_SEND(value)) {
1307                         debug_print("registering address completion entry\n");
1308                         address_completion_start(filtering_action.window);
1309                         address_completion_register_entry(
1310                                         GTK_ENTRY(filtering_action.dest_entry),
1311                                         TRUE);
1312                 }
1313                 filtering_action.current_action = value;
1314         }
1315
1316         prefs_filtering_action_enable_widget(filtering_action.label2,
1317                 (ACTION_SEND(value) || value == ACTION_ADD_TO_ADDRESSBOOK));
1318
1319         prefs_filtering_action_enable_widget(filtering_action.label3,
1320                         (ACTION_SEND(value)    || ACTION_TAG(value) || 
1321                          ACTION_SCORE(value)   || ACTION_FILEOP(value) ||
1322                          value == ACTION_COLOR || value == ACTION_EXECUTE ||
1323                          value == ACTION_ADD_TO_ADDRESSBOOK));
1324
1325         prefs_filtering_action_enable_widget(filtering_action.account_combo, 
1326                                                 ACTION_SEND(value));
1327
1328         prefs_filtering_action_enable_widget(filtering_action.tags_combo,
1329                                                 ACTION_TAG(value));
1330
1331         prefs_filtering_action_enable_widget(filtering_action.dest_entry,
1332                         (ACTION_FILEOP(value) || value == ACTION_EXECUTE ||
1333                          ACTION_SEND(value)   || value == ACTION_ADD_TO_ADDRESSBOOK));
1334                          
1335         prefs_filtering_action_enable_widget(filtering_action.dest_btn,
1336                                                 ACTION_FILEOP(value));
1337
1338         prefs_filtering_action_enable_widget(filtering_action.exec_btn,
1339                                                 (value == ACTION_EXECUTE));
1340
1341         prefs_filtering_action_enable_widget(filtering_action.color_optmenu,
1342                                                 (value == ACTION_COLOR));
1343
1344         prefs_filtering_action_enable_widget(filtering_action.header_combo,
1345                                         (value == ACTION_ADD_TO_ADDRESSBOOK));  
1346
1347         prefs_filtering_action_enable_widget(filtering_action.addressbook_btn,
1348                                         (value == ACTION_ADD_TO_ADDRESSBOOK));
1349
1350         prefs_filtering_action_enable_widget(filtering_action.score_entry,
1351                                                 ACTION_SCORE(value));
1352                 
1353         switch(value) {
1354         case ACTION_FORWARD:
1355         case ACTION_FORWARD_AS_ATTACHMENT:
1356         case ACTION_REDIRECT:
1357                 gtk_label_set_text(GTK_LABEL(filtering_action.label2), _("Account"));   
1358                 gtk_label_set_text(GTK_LABEL(filtering_action.label3), _("Recipient"));
1359                 break;
1360         case ACTION_ADD_TO_ADDRESSBOOK:
1361                 gtk_label_set_text(GTK_LABEL(filtering_action.label2), _("Header Name"));
1362                 gtk_label_set_text(GTK_LABEL(filtering_action.label3), _("Book/Folder"));
1363                 break;
1364         case ACTION_COPY:
1365         case ACTION_MOVE:
1366                 gtk_label_set_text(GTK_LABEL(filtering_action.label3), _("Destination"));
1367                 break;
1368         case ACTION_COLOR:
1369                 gtk_label_set_text(GTK_LABEL(filtering_action.label3), _("Color"));
1370                 break;
1371         case ACTION_EXECUTE:
1372                 gtk_label_set_text(GTK_LABEL(filtering_action.label3), _("Execute"));
1373                 break;
1374         case ACTION_SET_SCORE:
1375         case ACTION_CHANGE_SCORE:
1376                 gtk_label_set_text(GTK_LABEL(filtering_action.label3), _("Score"));
1377                 break;
1378         case ACTION_SET_TAG:
1379         case ACTION_UNSET_TAG:
1380                 gtk_label_set_text(GTK_LABEL(filtering_action.label3), _("Tag"));
1381                 break;
1382         }
1383 }
1384
1385 static void prefs_filtering_action_reset_dialog(void)
1386 {
1387         combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo), ACTION_MOVE);
1388         gtk_combo_box_set_active(GTK_COMBO_BOX(filtering_action.account_combo), 0);
1389         gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), "");
1390         gtk_spin_button_set_value(GTK_SPIN_BUTTON(filtering_action.score_entry), 0);
1391 }
1392
1393 static GtkListStore* prefs_filtering_action_create_data_store(void)
1394 {
1395         return gtk_list_store_new(N_PFA_COLUMNS,
1396                                   G_TYPE_STRING,
1397                                   G_TYPE_BOOLEAN,
1398                                   -1);
1399 }
1400
1401 static void prefs_filtering_action_list_view_insert_action(GtkWidget   *list_view,
1402                                                            GtkTreeIter *row,
1403                                                            const gchar *action,
1404                                                            gboolean     is_valid)
1405 {
1406         GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model
1407                                         (GTK_TREE_VIEW(list_view)));
1408         GtkTreeIter iter;
1409         
1410         
1411         /* see if row exists, if not append */
1412         if (row == NULL)
1413                 gtk_list_store_append(store, &iter);
1414         else
1415                 iter = *row;
1416
1417         gtk_list_store_set(store, &iter,
1418                            PFA_ACTION, action,
1419                            PFA_VALID_ACTION, is_valid,
1420                            -1);
1421 }
1422
1423 static GtkWidget *prefs_filtering_action_list_view_create(void)
1424 {
1425         GtkTreeView *list_view;
1426         GtkTreeModel *model;
1427         GtkTreeSelection *selector;
1428
1429         model = GTK_TREE_MODEL(prefs_filtering_action_create_data_store());
1430         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1431         g_object_unref(model);  
1432         
1433         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1434
1435         selector = gtk_tree_view_get_selection(list_view);
1436         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1437         gtk_tree_selection_set_select_function
1438                 (selector, prefs_filtering_actions_selected, NULL, NULL);
1439         
1440         /* create the columns */
1441         prefs_filtering_action_create_list_view_columns(list_view);
1442
1443         return GTK_WIDGET(list_view);
1444 }
1445
1446 static void prefs_filtering_action_create_list_view_columns(GtkTreeView *list_view)
1447 {
1448         GtkTreeViewColumn *column;
1449         GtkCellRenderer *renderer;
1450
1451         renderer = gtk_cell_renderer_text_new();
1452         column = gtk_tree_view_column_new_with_attributes
1453                 (_("Current action list"),
1454                  renderer,
1455                  "text", PFA_ACTION,
1456                  NULL);
1457         gtk_tree_view_append_column(list_view, column);         
1458 }
1459
1460 static gboolean prefs_filtering_actions_selected
1461                         (GtkTreeSelection *selector,
1462                          GtkTreeModel *model, 
1463                          GtkTreePath *path,
1464                          gboolean currently_selected,
1465                          gpointer data)
1466 {
1467         gchar *action_str;
1468         FilteringAction *action;
1469         GSList * action_list;
1470         gint list_id;
1471         GtkTreeIter iter;
1472         gboolean is_valid;
1473
1474         if (currently_selected)
1475                 return TRUE;
1476
1477         if (!gtk_tree_model_get_iter(model, &iter, path))
1478                 return TRUE;
1479
1480         gtk_tree_model_get(model, &iter, 
1481                            PFA_VALID_ACTION,  &is_valid,
1482                            -1);
1483
1484         if (!is_valid) {
1485                 prefs_filtering_action_reset_dialog();
1486                 return TRUE;
1487         }
1488
1489         gtk_tree_model_get(model, &iter, 
1490                            PFA_ACTION, &action_str,
1491                            -1);
1492
1493         action_list = matcher_parser_get_action_list(action_str);
1494         g_free(action_str);
1495
1496         if (action_list == NULL)
1497                 return TRUE;
1498
1499         action = action_list->data;
1500         g_slist_free(action_list);
1501
1502         switch(action->type) {
1503         case MATCHACTION_MOVE:
1504                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1505                                      ACTION_MOVE);
1506                 break;
1507         case MATCHACTION_COPY:
1508                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1509                                      ACTION_COPY);
1510                 break;
1511         case MATCHACTION_DELETE:
1512                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1513                                      ACTION_DELETE);
1514                 break;
1515         case MATCHACTION_MARK:
1516                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1517                                      ACTION_MARK);
1518                 break;
1519         case MATCHACTION_UNMARK:
1520                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1521                                      ACTION_UNMARK);
1522                 break;
1523         case MATCHACTION_LOCK:
1524                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1525                                      ACTION_LOCK);
1526                 break;
1527         case MATCHACTION_UNLOCK:
1528                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1529                                      ACTION_UNLOCK);
1530                 break;
1531         case MATCHACTION_MARK_AS_READ:
1532                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1533                                      ACTION_MARK_AS_READ);
1534                 break;
1535         case MATCHACTION_MARK_AS_UNREAD:
1536                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1537                                      ACTION_MARK_AS_UNREAD);
1538                 break;
1539         case MATCHACTION_MARK_AS_SPAM:
1540                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1541                                      ACTION_MARK_AS_SPAM);
1542                 break;
1543         case MATCHACTION_MARK_AS_HAM:
1544                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1545                                      ACTION_MARK_AS_HAM);
1546                 break;
1547         case MATCHACTION_FORWARD:
1548                 list_id = get_list_id_from_account_id(action->account_id);
1549                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1550                                      ACTION_FORWARD);
1551                 gtk_combo_box_set_active(GTK_COMBO_BOX(filtering_action.account_combo),
1552                                      list_id);
1553                 break;
1554         case MATCHACTION_FORWARD_AS_ATTACHMENT:
1555                 list_id = get_list_id_from_account_id(action->account_id);
1556                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1557                                      ACTION_FORWARD_AS_ATTACHMENT);
1558                 gtk_combo_box_set_active(GTK_COMBO_BOX(filtering_action.account_combo),
1559                                      list_id);
1560                 break;
1561         case MATCHACTION_REDIRECT:
1562                 list_id = get_list_id_from_account_id(action->account_id);
1563                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1564                                      ACTION_REDIRECT);
1565                 gtk_combo_box_set_active(GTK_COMBO_BOX(filtering_action.account_combo),
1566                                      list_id);
1567                 break;
1568         case MATCHACTION_EXECUTE:
1569                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1570                                      ACTION_EXECUTE);
1571                 break;
1572         case MATCHACTION_COLOR:
1573                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1574                                      ACTION_COLOR);
1575                 gtk_cmoption_menu_set_history(GTK_CMOPTION_MENU(filtering_action.color_optmenu),
1576                                             action->labelcolor);     
1577                 break;
1578         case MATCHACTION_CHANGE_SCORE:
1579                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1580                                      ACTION_CHANGE_SCORE);
1581                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(filtering_action.score_entry),
1582                                           action->score);
1583                 break;
1584         case MATCHACTION_SET_SCORE:
1585                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1586                                      ACTION_SET_SCORE);
1587                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(filtering_action.score_entry),
1588                                           action->score);
1589                 break;
1590         case MATCHACTION_STOP:
1591                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1592                                      ACTION_STOP);
1593                 break;
1594         case MATCHACTION_HIDE:
1595                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1596                                      ACTION_HIDE);
1597                 break;
1598         case MATCHACTION_IGNORE:
1599                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1600                                      ACTION_IGNORE);
1601                 break;
1602         case MATCHACTION_WATCH:
1603                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1604                                      ACTION_WATCH);
1605                 break;
1606         case MATCHACTION_ADD_TO_ADDRESSBOOK:
1607                 if (action->header)
1608                         gtk_entry_set_text(GTK_ENTRY(filtering_action.header_entry), action->header);
1609                 else
1610                         gtk_entry_set_text(GTK_ENTRY(filtering_action.header_entry), "");
1611                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1612                                      ACTION_ADD_TO_ADDRESSBOOK);
1613                 break;
1614         case MATCHACTION_SET_TAG:
1615                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1616                                      ACTION_SET_TAG);
1617                 combobox_select_by_text(GTK_COMBO_BOX(filtering_action.tags_combo),
1618                                         action->destination);
1619                 break;
1620         case MATCHACTION_UNSET_TAG:
1621                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1622                                      ACTION_UNSET_TAG);
1623                 combobox_select_by_text(GTK_COMBO_BOX(filtering_action.tags_combo),
1624                                         action->destination);
1625                 break;
1626         case MATCHACTION_CLEAR_TAGS:
1627                 combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
1628                                      ACTION_CLEAR_TAGS);
1629                 break;
1630         default:
1631                 g_warning("unhandled case !\n");
1632         }
1633         if (action->destination)
1634                 gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), action->destination);
1635         else
1636                 gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), "");
1637
1638         filteringaction_free(action); /* XXX: memleak */
1639         return TRUE;
1640 }