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