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