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