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