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