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