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