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