2007-07-30 [paul] 2.10.0cvs80
[claws.git] / src / prefs_matcher.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #include "main.h"
36 #include "prefs_gtk.h"
37 #include "prefs_matcher.h"
38 #include "prefs_common.h"
39 #include "mainwindow.h"
40 #include "foldersel.h"
41 #include "manage_window.h"
42 #include "inc.h"
43 #include "matcher.h"
44 #include "utils.h"
45 #include "gtkutils.h"
46 #include "alertpanel.h"
47 #include "folder.h"
48 #include "description_window.h"
49
50 #include "matcher_parser.h"
51 #include "colorlabel.h"
52
53 enum {
54         PREFS_MATCHER_COND,
55         PREFS_MATCHER_COND_VALID,
56         N_PREFS_MATCHER_COLUMNS
57 };
58
59 /*!
60  *\brief        UI data for matcher dialog
61  */
62 static struct Matcher {
63         GtkWidget *window;
64
65         GtkWidget *ok_btn;
66
67         GtkWidget *predicate_combo;
68         GtkWidget *predicate_flag_combo;
69         GtkWidget *header_combo;
70         GtkWidget *header_addr_combo;
71
72         GtkWidget *criteria_list;
73
74         GtkWidget *predicate_list;
75         GtkWidget *predicate_label;
76         GtkWidget *predicate_flag_list;
77
78         GtkWidget *bool_op_list;
79
80         GtkWidget *header_entry;
81         GtkWidget *header_label;
82         GtkWidget *header_addr_entry;
83         GtkWidget *header_addr_label;
84         GtkWidget *value_entry;
85         GtkWidget *value_label;
86         GtkWidget *addressbook_folder_label;
87         GtkWidget *addressbook_folder_combo;
88         GtkWidget *case_checkbtn;
89         GtkWidget *regexp_checkbtn;
90         GtkWidget *color_optmenu;
91
92         GtkWidget *test_btn;
93         GtkWidget *addressbook_select_btn;
94
95         GtkWidget *cond_list_view;
96
97         GtkWidget *criteria_table;
98
99         gint selected_criteria; /*!< selected criteria in combobox */ 
100 } matcher;
101
102 /*!
103  *\brief        Conditions with a negate counterpart (like unread and ~unread)
104  *              have the same CRITERIA_XXX id). I.e. both unread and ~unread
105  *              have criteria id CRITERIA_UNREAD. This id is passed as the
106  *              first parameter to #matcherprop_new and #matcherprop_unquote_new.
107  *
108  *\warning      Currently the enum constants should have the same order as the 
109  *              #criteria_text  
110  */             
111 enum {
112         CRITERIA_ALL = 0,
113
114         CRITERIA_SUBJECT = 1,
115         CRITERIA_FROM = 2,
116         CRITERIA_TO = 3,
117         CRITERIA_CC = 4,
118         CRITERIA_TO_OR_CC = 5,
119         CRITERIA_NEWSGROUPS = 6,
120         CRITERIA_INREPLYTO = 7,
121         CRITERIA_REFERENCES = 8,
122         CRITERIA_AGE_GREATER = 9,
123         CRITERIA_AGE_LOWER = 10,
124         CRITERIA_HEADER = 11,
125         CRITERIA_HEADERS_PART = 12,
126         CRITERIA_BODY_PART = 13,
127         CRITERIA_MESSAGE = 14,
128
129         CRITERIA_UNREAD = 15,
130         CRITERIA_NEW = 16,
131         CRITERIA_MARKED = 17,
132         CRITERIA_DELETED = 18,
133         CRITERIA_REPLIED = 19,
134         CRITERIA_FORWARDED = 20,
135         CRITERIA_LOCKED = 21,
136         CRITERIA_SPAM = 22,
137         CRITERIA_COLORLABEL = 23,
138         CRITERIA_IGNORE_THREAD = 24,
139         CRITERIA_WATCH_THREAD = 25,
140
141         CRITERIA_SCORE_GREATER = 26,
142         CRITERIA_SCORE_LOWER = 27,
143         CRITERIA_SCORE_EQUAL = 28,
144
145         CRITERIA_TEST = 29,
146
147         CRITERIA_SIZE_GREATER = 30,
148         CRITERIA_SIZE_SMALLER = 31,
149         CRITERIA_SIZE_EQUAL   = 32,
150         
151         CRITERIA_PARTIAL = 33,
152
153         CRITERIA_FOUND_IN_ADDRESSBOOK = 34,
154         
155         CRITERIA_TAG = 35,
156         CRITERIA_TAGGED = 36
157 };
158
159 /*!
160  *\brief        Descriptive text for conditions
161  */
162 typedef struct _struct_criteria_text struct_criteria_text;
163 struct _struct_criteria_text {
164         const gchar     *text;
165         gboolean         contains_header_name;
166         /* if contains_header_name is TRUE, prefs_common_translated_headername(text)
167            will be used
168         */
169 };
170
171 static struct_criteria_text criteria_text [] = {
172         { N_("All messages"), FALSE },
173         { N_("Subject"), TRUE },
174         { N_("From"), TRUE },
175         { N_("To"), TRUE },
176         { N_("Cc"), TRUE },
177         { N_("To or Cc"), TRUE },
178         { N_("Newsgroups"), TRUE },
179         { N_("In reply to"), TRUE },
180         { N_("References"), TRUE },
181         { N_("Age greater than (days)"), FALSE },
182         { N_("Age less than (days)"), FALSE },
183         { N_("Header"), FALSE },
184         { N_("Headers part"), FALSE },
185         { N_("Body part"), FALSE },
186         { N_("Whole message"), FALSE },
187         { N_("Unread flag"), FALSE },
188         { N_("New flag"), FALSE },
189         { N_("Marked flag"), FALSE },
190         { N_("Deleted flag"), FALSE },
191         { N_("Replied flag"), FALSE },
192         { N_("Forwarded flag"), FALSE },
193         { N_("Locked flag"), FALSE },
194         { N_("Spam flag"), FALSE },
195         { N_("Color label"), FALSE },
196         { N_("Ignored thread"), FALSE },
197         { N_("Watched thread"), FALSE },
198         { N_("Score greater than"), FALSE },
199         { N_("Score lower than"), FALSE },
200         { N_("Score equal to"), FALSE },
201         { N_("Test"), FALSE },
202         { N_("Size greater than (bytes)"), FALSE }, 
203         { N_("Size smaller than (bytes)"), FALSE },
204         { N_("Size exactly (bytes)"), FALSE },
205         { N_("Partially downloaded"), FALSE },
206         { N_("Found in addressbook"), FALSE },
207         { N_("Tags"), FALSE },
208         { N_("Tagged"), FALSE },
209         { NULL, FALSE }
210 };
211
212 /*!
213  *\brief        Boolean / predicate constants
214  *
215  *\warning      Same order as #bool_op_text!
216  */
217 enum {
218         BOOL_OP_OR = 0,
219         BOOL_OP_AND = 1
220 };
221
222 /*!
223  *\brief        Descriptive text in UI
224  */
225 static const gchar *bool_op_text [] = {
226         N_("or"), N_("and")
227 };
228
229 /*!
230  *\brief        Contains predicate      
231  *
232  *\warning      Same order as in #predicate_text
233  */
234 enum {
235         PREDICATE_CONTAINS = 0,
236         PREDICATE_DOES_NOT_CONTAIN = 1
237 };
238
239 /*!
240  *\brief        Descriptive text in UI for predicate
241  */
242 static const gchar *predicate_text [] = {
243         N_("contains"), N_("does not contain")
244 };
245
246 /*!
247  *\brief        Preset addressbook book/folder items
248  */
249 static const gchar *addressbook_folder_text [] = {
250         N_("Any")
251 };
252
253 /*!
254  *\brief        Enabled predicate
255  *
256  *\warning      Same order as in #predicate_flag_text
257  */
258 enum {
259         PREDICATE_FLAG_ENABLED = 0,
260         PREDICATE_FLAG_DISABLED = 1
261 };
262
263 /*!
264  *\brief        Descriptive text in UI for enabled flag
265  */
266 static const gchar *predicate_flag_text [] = {
267         N_("yes"), N_("no")
268 };
269
270 /*!
271  *\brief        Hooks
272  */
273 static PrefsMatcherSignal *matchers_callback;
274
275 /* widget creating functions */
276 static void prefs_matcher_create        (void);
277
278 static void prefs_matcher_set_dialog    (MatcherList *matchers);
279 static void prefs_matcher_list_view_set_row     (GtkTreeIter *row, 
280                                                  MatcherProp *prop);
281
282 /* callback functions */
283
284 static void prefs_matcher_register_cb   (void);
285 static void prefs_matcher_substitute_cb (void);
286 static void prefs_matcher_delete_cb     (void);
287 static void prefs_matcher_up            (void);
288 static void prefs_matcher_down          (void);
289 static gboolean prefs_matcher_key_pressed(GtkWidget     *widget,
290                                          GdkEventKey    *event,
291                                          gpointer        data);
292 static void prefs_matcher_ok            (void);
293 static void prefs_matcher_cancel        (void);
294 static gint prefs_matcher_deleted       (GtkWidget *widget, GdkEventAny *event,
295                                          gpointer data);
296 static void prefs_matcher_criteria_select       (GtkList   *list,
297                                                  GtkWidget *widget,
298                                                  gpointer   user_data);
299 static MatcherList *prefs_matcher_get_list      (void);
300
301 static GtkListStore* prefs_matcher_create_data_store    (void);
302
303 static void prefs_matcher_list_view_insert_matcher      (GtkWidget *list_view,
304                                                          GtkTreeIter *row_iter,
305                                                          const gchar *matcher,
306                                                          gboolean is_valid);
307
308 static GtkWidget *prefs_matcher_list_view_create        (void);
309
310 static void prefs_matcher_create_list_view_columns      (GtkWidget *list_view);
311
312 static gboolean prefs_matcher_selected                  (GtkTreeSelection *selector,
313                                                          GtkTreeModel *model, 
314                                                          GtkTreePath *path,
315                                                          gboolean currently_selected,
316                                                          gpointer data);
317
318 /*!
319  *\brief        Find index of list selection 
320  *
321  *\param        list GTK list widget
322  *
323  *\return       gint Selection index
324  */
325 static gint get_sel_from_list(GtkList *list)
326 {
327         gint row = 0;
328         void * sel;
329         GList * child;
330
331         sel = list->selection->data;
332         for (child = list->children; child != NULL; child = g_list_next(child)) {
333                 if (child->data == sel)
334                         return row;
335                 row ++;
336         }
337         
338         return row;
339 }
340
341 /*!
342  *\brief        Opens the matcher dialog with a list of conditions
343  *
344  *\param        matchers List of conditions
345  *\param        cb Callback
346  *
347  */
348 void prefs_matcher_open(MatcherList *matchers, PrefsMatcherSignal *cb)
349 {
350         inc_lock();
351
352         if (!matcher.window) {
353                 prefs_matcher_create();
354         } else {
355                 /* update color label menu */
356                 gtk_option_menu_set_menu(GTK_OPTION_MENU(matcher.color_optmenu),
357                                 colorlabel_create_color_menu());
358         }
359
360         manage_window_set_transient(GTK_WINDOW(matcher.window));
361         gtk_widget_grab_focus(matcher.ok_btn);
362
363         matchers_callback = cb;
364
365         prefs_matcher_set_dialog(matchers);
366
367         gtk_widget_show(matcher.window);
368 }
369
370 /*!
371  *\brief        Save Gtk object size to prefs dataset
372  */
373 static void prefs_matcher_size_allocate_cb(GtkWidget *widget,
374                                          GtkAllocation *allocation)
375 {
376         g_return_if_fail(allocation != NULL);
377
378         prefs_common.matcherwin_width = allocation->width;
379         prefs_common.matcherwin_height = allocation->height;
380 }
381
382 /*!
383  *\brief        Create the matcher dialog
384  */
385 static void prefs_matcher_create(void)
386 {
387         GtkWidget *window;
388         GtkWidget *vbox;
389         GtkWidget *ok_btn;
390         GtkWidget *cancel_btn;
391         GtkWidget *confirm_area;
392
393         GtkWidget *vbox1;
394         GtkWidget *vbox2;
395         GtkWidget *vbox3;
396         GtkWidget *criteria_table;
397
398         GtkWidget *hbox1;
399
400         GtkWidget *header_combo;
401         GtkWidget *header_entry;
402         GtkWidget *header_label;
403         GtkWidget *header_addr_combo;
404         GtkWidget *header_addr_entry;
405         GtkWidget *header_addr_label;
406         GtkWidget *criteria_combo;
407         GtkWidget *criteria_list;
408         GtkWidget *criteria_label;
409         GtkWidget *value_label;
410         GtkWidget *value_entry;
411         GtkWidget *addressbook_folder_label;
412         GtkWidget *addressbook_folder_combo;
413         GtkWidget *predicate_combo;
414         GtkWidget *predicate_list;
415         GtkWidget *predicate_flag_combo;
416         GtkWidget *predicate_flag_list;
417         GtkWidget *predicate_label;
418         GtkWidget *bool_op_combo;
419         GtkWidget *bool_op_list;
420         GtkWidget *bool_op_label;
421
422         GtkWidget *regexp_checkbtn;
423         GtkWidget *case_checkbtn;
424
425         GtkWidget *reg_hbox;
426         GtkWidget *btn_hbox;
427         GtkWidget *arrow;
428         GtkWidget *reg_btn;
429         GtkWidget *subst_btn;
430         GtkWidget *del_btn;
431
432         GtkWidget *cond_hbox;
433         GtkWidget *cond_scrolledwin;
434         GtkWidget *cond_list_view;
435
436         GtkWidget *btn_vbox;
437         GtkWidget *up_btn;
438         GtkWidget *down_btn;
439
440         GtkWidget *test_btn;
441         GtkWidget *addressbook_select_btn;
442
443         GtkWidget *color_optmenu;
444
445         GList *combo_items;
446         gint i;
447         static GdkGeometry geometry;
448
449         debug_print("Creating matcher configuration window...\n");
450
451         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_matcher");
452         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
453         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
454         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
455         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
456
457         vbox = gtk_vbox_new(FALSE, 6);
458         gtk_widget_show(vbox);
459         gtk_container_add(GTK_CONTAINER(window), vbox);
460
461         gtkut_stock_button_set_create(&confirm_area, &cancel_btn, GTK_STOCK_CANCEL,
462                                       &ok_btn, GTK_STOCK_OK, NULL, NULL);
463         gtk_widget_show(confirm_area);
464         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
465         gtk_widget_grab_default(ok_btn);
466
467         gtk_window_set_title(GTK_WINDOW(window),
468                              _("Condition configuration"));
469         g_signal_connect(G_OBJECT(window), "delete_event",
470                          G_CALLBACK(prefs_matcher_deleted), NULL);
471         g_signal_connect(G_OBJECT(window), "size_allocate",
472                          G_CALLBACK(prefs_matcher_size_allocate_cb), NULL);
473         g_signal_connect(G_OBJECT(window), "key_press_event",
474                          G_CALLBACK(prefs_matcher_key_pressed), NULL);
475         MANAGE_WINDOW_SIGNALS_CONNECT(window);
476         g_signal_connect(G_OBJECT(ok_btn), "clicked",
477                          G_CALLBACK(prefs_matcher_ok), NULL);
478         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
479                          G_CALLBACK(prefs_matcher_cancel), NULL);
480
481         vbox1 = gtk_vbox_new(FALSE, VSPACING);
482         gtk_widget_show(vbox1);
483         gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
484         gtk_container_set_border_width(GTK_CONTAINER (vbox1), 2);
485
486         criteria_table = gtk_table_new(2, 4, FALSE);
487         gtk_widget_show(criteria_table);
488
489         gtk_box_pack_start(GTK_BOX(vbox1), criteria_table, FALSE, FALSE, 0);
490         gtk_table_set_row_spacings(GTK_TABLE(criteria_table), 8);
491         gtk_table_set_col_spacings(GTK_TABLE(criteria_table), 8);
492
493         /* criteria combo box */
494
495         criteria_label = gtk_label_new(_("Match type"));
496         gtk_widget_show(criteria_label);
497         gtk_misc_set_alignment(GTK_MISC(criteria_label), 0, 0.5);
498         gtk_table_attach(GTK_TABLE(criteria_table), criteria_label, 0, 1, 0, 1,
499                          GTK_FILL, 0, 0, 0);
500
501         criteria_combo = gtk_combo_new();
502         gtk_widget_show(criteria_combo);
503
504         combo_items = NULL;
505
506         for (i = 0; criteria_text[i].text != NULL; i++) {
507                 if (criteria_text[i].contains_header_name)
508                         combo_items = g_list_append(combo_items,
509                                             (gpointer) prefs_common_translated_header_name(criteria_text[i].text));
510                 else
511                         combo_items = g_list_append(combo_items,
512                                             (gpointer) gettext(criteria_text[i].text));
513         }
514         gtk_combo_set_popdown_strings(GTK_COMBO(criteria_combo), combo_items);
515
516         g_list_free(combo_items);
517
518         gtk_widget_set_size_request(criteria_combo, 190, -1);
519         gtk_table_attach(GTK_TABLE(criteria_table), criteria_combo, 0, 1, 1, 2,
520                           0, 0, 0, 0);
521         criteria_list = GTK_COMBO(criteria_combo)->list;
522         g_signal_connect(G_OBJECT(criteria_list), "select-child",
523                          G_CALLBACK(prefs_matcher_criteria_select),
524                          NULL);
525
526         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(criteria_combo)->entry),
527                                FALSE);
528
529         /* header name */
530
531         header_label = gtk_label_new(_("Header name"));
532         gtk_widget_show(header_label);
533         gtk_misc_set_alignment(GTK_MISC(header_label), 0, 0.5);
534         gtk_table_attach(GTK_TABLE(criteria_table), header_label, 1, 2, 0, 1,
535                          GTK_FILL, 0, 0, 0);
536
537         header_combo = gtk_combo_new();
538         gtk_widget_show(header_combo);
539         gtk_widget_set_size_request(header_combo, 120, -1);
540         gtkut_combo_set_items(GTK_COMBO (header_combo),
541                               "Subject", "From", "To", "Cc", "Reply-To",
542                               "Sender", "X-ML-Name", "X-List", "X-Sequence",
543                               "X-Mailer","X-BeenThere",
544                               NULL);
545         gtk_table_attach(GTK_TABLE(criteria_table), header_combo, 1, 2, 1, 2,
546                          0, 0, 0, 0);
547         header_entry = GTK_COMBO(header_combo)->entry;
548         gtk_entry_set_editable(GTK_ENTRY(header_entry), TRUE);
549
550         /* address header name */
551
552         header_addr_label = gtk_label_new(_("Address header"));
553         gtk_misc_set_alignment(GTK_MISC(header_addr_label), 0, 0.5);
554         gtk_table_attach(GTK_TABLE(criteria_table), header_addr_label, 1, 2, 0, 1,
555                          GTK_FILL, 0, 0, 0);
556
557         header_addr_combo = gtk_combo_new();
558         gtk_widget_set_size_request(header_addr_combo, 120, -1);
559         gtkut_combo_set_items(GTK_COMBO (header_addr_combo),
560                               Q_("Filtering Matcher Menu|All"),
561                               _("Any"), "From", "To", "Cc", "Reply-To", "Sender",
562                               NULL);
563         gtk_table_attach(GTK_TABLE(criteria_table), header_addr_combo, 1, 2, 1, 2,
564                          0, 0, 0, 0);
565         header_addr_entry = GTK_COMBO(header_addr_combo)->entry;
566         gtk_entry_set_editable(GTK_ENTRY(header_addr_entry), TRUE);
567
568         /* value */
569
570         value_label = gtk_label_new(_("Value"));
571         gtk_widget_show(value_label);
572         gtk_misc_set_alignment(GTK_MISC (value_label), 0, 0.5);
573         gtk_table_attach(GTK_TABLE(criteria_table), value_label, 2, 3, 0, 1,
574                          GTK_FILL | GTK_SHRINK | GTK_EXPAND, 0, 0, 0);
575
576         value_entry = gtk_entry_new();
577         gtk_widget_show(value_entry);
578         gtk_widget_set_size_request(value_entry, 200, -1);
579         gtk_table_attach(GTK_TABLE(criteria_table), value_entry, 2, 3, 1, 2,
580                          GTK_FILL | GTK_SHRINK | GTK_EXPAND, 0, 0, 0);
581
582         /* book/folder value */
583
584         addressbook_folder_label = gtk_label_new(_("Book/folder"));
585         gtk_misc_set_alignment(GTK_MISC (addressbook_folder_label), 0, 0.5);
586         gtk_table_attach(GTK_TABLE(criteria_table), addressbook_folder_label, 2, 3, 0, 1,
587                          GTK_FILL | GTK_SHRINK | GTK_EXPAND, 0, 0, 0);
588
589         addressbook_folder_combo = gtk_combo_new();
590         gtk_widget_show(addressbook_folder_combo);
591         gtk_widget_set_size_request(addressbook_folder_combo, 200, -1);
592         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(addressbook_folder_combo)->entry),
593                                TRUE);
594
595         combo_items = NULL;
596         for (i = 0; i < (gint) (sizeof(addressbook_folder_text) / sizeof(gchar *)); i++) {
597                 combo_items = g_list_append(combo_items,
598                                             (gpointer) _(addressbook_folder_text[i]));
599         }
600         gtk_combo_set_popdown_strings(GTK_COMBO(addressbook_folder_combo), combo_items);
601         g_list_free(combo_items);
602
603         gtk_table_attach(GTK_TABLE(criteria_table), addressbook_folder_combo, 2, 3, 1, 2,
604                          GTK_FILL | GTK_SHRINK | GTK_EXPAND, 0, 0, 0);
605
606 #if GTK_CHECK_VERSION(2, 8, 0)
607         test_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
608 #else
609         test_btn = gtk_button_new_with_label(_("Info..."));
610 #endif
611         gtk_widget_show(test_btn);
612         gtk_table_attach(GTK_TABLE (criteria_table), test_btn, 3, 4, 1, 2,
613                          0, 0, 0, 0);
614         g_signal_connect(G_OBJECT (test_btn), "clicked",
615                          G_CALLBACK(prefs_matcher_test_info),
616                          NULL);
617
618         addressbook_select_btn = gtk_button_new_with_label(_("Select ..."));
619         gtk_table_attach(GTK_TABLE (criteria_table), addressbook_select_btn, 3, 4, 1, 2,
620                          0, 0, 0, 0);
621         g_signal_connect(G_OBJECT (addressbook_select_btn), "clicked",
622                          G_CALLBACK(prefs_matcher_addressbook_select),
623                          NULL);
624
625         color_optmenu = gtk_option_menu_new();
626         gtk_option_menu_set_menu(GTK_OPTION_MENU(color_optmenu),
627                                  colorlabel_create_color_menu());
628
629         /* predicate */
630
631         vbox2 = gtk_vbox_new(FALSE, VSPACING);
632         gtk_widget_show(vbox2);
633         gtk_box_pack_start(GTK_BOX(vbox1), vbox2, FALSE, FALSE, 0);
634
635         hbox1 = gtk_hbox_new(FALSE, 8);
636         gtk_widget_show(hbox1);
637         gtk_box_pack_start(GTK_BOX(vbox2), hbox1, FALSE, FALSE, 0);
638
639         predicate_label = gtk_label_new(_("Predicate"));
640         gtk_widget_show(predicate_label);
641         gtk_box_pack_start(GTK_BOX(hbox1), predicate_label,
642                            FALSE, FALSE, 0);
643
644         predicate_combo = gtk_combo_new();
645         gtk_widget_show(predicate_combo);
646         gtk_widget_set_size_request(predicate_combo, 120, -1);
647         predicate_list = GTK_COMBO(predicate_combo)->list;
648         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(predicate_combo)->entry),
649                                FALSE);
650
651         combo_items = NULL;
652
653         for (i = 0; i < (gint) (sizeof(predicate_text) / sizeof(gchar *)); i++) {
654                 combo_items = g_list_append(combo_items,
655                                             (gpointer) _(predicate_text[i]));
656         }
657         gtk_combo_set_popdown_strings(GTK_COMBO(predicate_combo), combo_items);
658
659         g_list_free(combo_items);
660
661         gtk_box_pack_start(GTK_BOX(hbox1), predicate_combo,
662                            FALSE, FALSE, 0);
663
664         /* predicate flag */
665
666         predicate_flag_combo = gtk_combo_new();
667         gtk_widget_hide(predicate_flag_combo);
668         gtk_widget_set_size_request(predicate_flag_combo, 120, -1);
669         predicate_flag_list = GTK_COMBO(predicate_flag_combo)->list;
670         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(predicate_flag_combo)->entry), FALSE);
671
672         combo_items = NULL;
673
674         for (i = 0; i < (gint) (sizeof(predicate_text) / sizeof(gchar *)); i++) {
675                 combo_items = g_list_append(combo_items, (gpointer) _(predicate_flag_text[i]));
676         }
677         gtk_combo_set_popdown_strings(GTK_COMBO(predicate_flag_combo),
678                                       combo_items);
679
680         g_list_free(combo_items);
681
682         gtk_box_pack_start(GTK_BOX(hbox1), predicate_flag_combo,
683                            FALSE, FALSE, 0);
684
685         vbox3 = gtk_vbox_new(FALSE, 0);
686         gtk_widget_show (vbox3);
687         gtk_box_pack_start(GTK_BOX(hbox1), vbox3, FALSE, FALSE, 0);
688
689         PACK_CHECK_BUTTON(vbox3, case_checkbtn, _("Case sensitive"));
690         PACK_CHECK_BUTTON(vbox3, regexp_checkbtn, _("Use regexp"));
691
692         /* register / substitute / delete */
693
694         reg_hbox = gtk_hbox_new(FALSE, 4);
695         gtk_widget_show(reg_hbox);
696         gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
697
698         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
699         gtk_widget_show(arrow);
700         gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
701         gtk_widget_set_size_request(arrow, -1, 16);
702
703         btn_hbox = gtk_hbox_new(FALSE, 4);
704         gtk_widget_show(btn_hbox);
705         gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
706
707         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
708         gtk_widget_show(reg_btn);
709         gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
710         g_signal_connect(G_OBJECT(reg_btn), "clicked",
711                          G_CALLBACK(prefs_matcher_register_cb), NULL);
712
713         subst_btn = gtkut_get_replace_btn(_("Replace"));
714         gtk_widget_show(subst_btn);
715         gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
716         g_signal_connect(G_OBJECT(subst_btn), "clicked",
717                          G_CALLBACK(prefs_matcher_substitute_cb),
718                          NULL);
719
720         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
721         gtk_widget_show(del_btn);
722         gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
723         g_signal_connect(G_OBJECT(del_btn), "clicked",
724                          G_CALLBACK(prefs_matcher_delete_cb), NULL);
725
726         /* boolean operation */
727
728         bool_op_label = gtk_label_new(_("Boolean Op"));
729         gtk_misc_set_alignment(GTK_MISC(value_label), 0, 0.5);
730         gtk_widget_show(bool_op_label);
731         gtk_box_pack_start(GTK_BOX(btn_hbox), bool_op_label,
732                            FALSE, FALSE, 0);
733
734         bool_op_combo = gtk_combo_new();
735         gtk_widget_show(bool_op_combo);
736         gtk_widget_set_size_request(bool_op_combo, 60, -1);
737         bool_op_list = GTK_COMBO(bool_op_combo)->list;
738         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(bool_op_combo)->entry),
739                                FALSE);
740
741         combo_items = NULL;
742
743         for (i = 0; i < (gint) (sizeof(bool_op_text) / sizeof(gchar *)); i++) {
744                 combo_items = g_list_append(combo_items,
745                                             (gpointer) _(bool_op_text[i]));
746         }
747         gtk_combo_set_popdown_strings(GTK_COMBO(bool_op_combo), combo_items);
748
749         g_list_free(combo_items);
750
751         gtk_box_pack_start(GTK_BOX(btn_hbox), bool_op_combo,
752                            FALSE, FALSE, 0);
753
754         cond_hbox = gtk_hbox_new(FALSE, 8);
755         gtk_widget_show(cond_hbox);
756         gtk_box_pack_start(GTK_BOX(vbox1), cond_hbox, TRUE, TRUE, 0);
757
758         cond_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
759         gtk_widget_show(cond_scrolledwin);
760         gtk_widget_set_size_request(cond_scrolledwin, -1, 150);
761         gtk_box_pack_start(GTK_BOX(cond_hbox), cond_scrolledwin,
762                            TRUE, TRUE, 0);
763         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(cond_scrolledwin),
764                                        GTK_POLICY_AUTOMATIC,
765                                        GTK_POLICY_AUTOMATIC);
766
767         cond_list_view = prefs_matcher_list_view_create();                                     
768         gtk_widget_show(cond_list_view);
769         gtk_container_add(GTK_CONTAINER(cond_scrolledwin), cond_list_view);
770
771         btn_vbox = gtk_vbox_new(FALSE, 8);
772         gtk_widget_show(btn_vbox);
773         gtk_box_pack_start(GTK_BOX(cond_hbox), btn_vbox, FALSE, FALSE, 0);
774
775         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
776         gtk_widget_show(up_btn);
777         gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0);
778         g_signal_connect(G_OBJECT(up_btn), "clicked",
779                          G_CALLBACK(prefs_matcher_up), NULL);
780
781         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
782         gtk_widget_show(down_btn);
783         gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
784         g_signal_connect(G_OBJECT(down_btn), "clicked",
785                          G_CALLBACK(prefs_matcher_down), NULL);
786
787         if (!geometry.min_height) {
788                 geometry.min_width = 520;
789                 geometry.min_height = 368;
790         }
791
792         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
793                                       GDK_HINT_MIN_SIZE);
794         gtk_widget_set_size_request(window, prefs_common.matcherwin_width,
795                                     prefs_common.matcherwin_height);
796
797         gtk_widget_show_all(window);
798         gtk_widget_hide(header_addr_label);
799         gtk_widget_hide(header_addr_combo);
800         gtk_widget_hide(addressbook_select_btn);
801         gtk_widget_hide(addressbook_folder_label);
802
803         matcher.window    = window;
804
805         matcher.ok_btn = ok_btn;
806
807         matcher.criteria_list = criteria_list;
808         matcher.header_combo = header_combo;
809         matcher.header_entry = header_entry;
810         matcher.header_label = header_label;
811         matcher.header_addr_combo = header_addr_combo;
812         matcher.header_addr_entry = header_addr_entry;
813         matcher.header_addr_label = header_addr_label;
814         matcher.value_entry = value_entry;
815         matcher.value_label = value_label;
816         matcher.addressbook_folder_label = addressbook_folder_label;
817         matcher.addressbook_folder_combo = addressbook_folder_combo;
818         matcher.predicate_label = predicate_label;
819         matcher.predicate_list = predicate_list;
820         matcher.predicate_combo = predicate_combo;
821         matcher.predicate_flag_list = predicate_flag_list;
822         matcher.predicate_flag_combo = predicate_flag_combo;
823         matcher.case_checkbtn = case_checkbtn;
824         matcher.regexp_checkbtn = regexp_checkbtn;
825         matcher.bool_op_list = bool_op_list;
826         matcher.test_btn = test_btn;
827         matcher.addressbook_select_btn = addressbook_select_btn;
828         matcher.color_optmenu = color_optmenu;
829         matcher.criteria_table = criteria_table;
830
831         matcher.cond_list_view = cond_list_view;
832
833         matcher.selected_criteria = -1;
834         prefs_matcher_criteria_select(GTK_LIST(criteria_list), NULL, NULL);
835 }
836
837 /*!
838  *\brief        Set the contents of a row
839  *
840  *\param        row Index of row to set
841  *\param        prop Condition to set
842  *
843  *\return       gint Row index \a prop has been added
844  */
845 static void prefs_matcher_list_view_set_row(GtkTreeIter *row, MatcherProp *prop)
846 {
847         gchar *matcher_str;
848
849         if (prop == NULL) {
850                 prefs_matcher_list_view_insert_matcher(matcher.cond_list_view,
851                                                        NULL, _("(New)"), FALSE);
852                 return;                                                
853         }
854
855         matcher_str = matcherprop_to_string(prop);
856         if (!row)
857                 prefs_matcher_list_view_insert_matcher(matcher.cond_list_view,
858                                                        NULL, matcher_str,
859                                                        TRUE);
860         else
861                 prefs_matcher_list_view_insert_matcher(matcher.cond_list_view,
862                                                        row, matcher_str, 
863                                                        TRUE);
864         g_free(matcher_str);
865 }
866
867 /*!
868  *\brief        Clears a condition in the list widget
869  */
870 static void prefs_matcher_reset_condition(void)
871 {
872         gtk_list_select_item(GTK_LIST(matcher.criteria_list), 0);
873         gtk_list_select_item(GTK_LIST(matcher.predicate_list), 0);
874         gtk_entry_set_text(GTK_ENTRY(matcher.header_entry), "");
875         gtk_entry_set_text(GTK_ENTRY(matcher.header_addr_entry), "");
876         gtk_entry_set_text(GTK_ENTRY(matcher.value_entry), "");
877         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry), "");
878 }
879
880 /*!
881  *\brief        Initializes dialog with a set of conditions
882  *
883  *\param        matchers List of conditions
884  */
885 static void prefs_matcher_set_dialog(MatcherList *matchers)
886 {
887         GSList *cur;
888         gboolean bool_op = 1;
889         GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model
890                                 (GTK_TREE_VIEW(matcher.cond_list_view)));
891
892         gtk_list_store_clear(store);                            
893
894         prefs_matcher_list_view_set_row(NULL, NULL);
895         if (matchers != NULL) {
896                 for (cur = matchers->matchers; cur != NULL;
897                      cur = g_slist_next(cur)) {
898                         MatcherProp *prop;
899                         prop = (MatcherProp *) cur->data;
900                         prefs_matcher_list_view_set_row(NULL, prop);
901                 }
902
903                 bool_op = matchers->bool_and;
904         }
905         
906         gtk_list_select_item(GTK_LIST(matcher.bool_op_list), bool_op);
907
908         prefs_matcher_reset_condition();
909 }
910
911 /*!
912  *\brief        Converts current conditions in list box in
913  *              a matcher list used by the matcher.
914  *
915  *\return       MatcherList * List of conditions.
916  */
917 static MatcherList *prefs_matcher_get_list(void)
918 {
919         gchar *matcher_str;
920         MatcherProp *prop;
921         gboolean bool_and;
922         GSList *matcher_list;
923         MatcherList *matchers;
924         GtkTreeModel *model;
925         GtkTreeIter iter;
926
927         model = gtk_tree_view_get_model(GTK_TREE_VIEW(matcher.cond_list_view));
928         if (!gtk_tree_model_get_iter_first(model, &iter))
929                 return NULL;
930
931         matcher_list = NULL;
932
933         do {
934                 gboolean is_valid;
935         
936                 gtk_tree_model_get(model, &iter,
937                                    PREFS_MATCHER_COND, &matcher_str,
938                                    PREFS_MATCHER_COND_VALID, &is_valid,
939                                    -1);
940                 
941                 if (is_valid) {
942                         /* tmp = matcher_str; */
943                         prop = matcher_parser_get_prop(matcher_str);
944                         g_free(matcher_str);
945                         if (prop == NULL)
946                                 break;
947                         
948                         matcher_list = g_slist_append(matcher_list, prop);
949                 }
950         } while (gtk_tree_model_iter_next(model, &iter));
951
952         bool_and = get_sel_from_list(GTK_LIST(matcher.bool_op_list));
953
954         matchers = matcherlist_new(matcher_list, bool_and);
955
956         return matchers;
957 }
958
959 /*!
960  *\brief        Maps a keyword id (see #get_matchparser_tab_id) to a 
961  *              criteria type (see first parameter of #matcherprop_new
962  *              or #matcherprop_unquote_new)
963  *
964  *\param        matching_id Id returned by the matcher parser.
965  *
966  *\return       gint One of the CRITERIA_xxx constants.
967  */
968 static gint prefs_matcher_get_criteria_from_matching(gint matching_id)
969 {
970         switch(matching_id) {
971         case MATCHCRITERIA_ALL:
972                 return CRITERIA_ALL;
973         case MATCHCRITERIA_NOT_UNREAD:
974         case MATCHCRITERIA_UNREAD:
975                 return CRITERIA_UNREAD;
976         case MATCHCRITERIA_NOT_NEW:
977         case MATCHCRITERIA_NEW:
978                 return CRITERIA_NEW;
979         case MATCHCRITERIA_NOT_MARKED:
980         case MATCHCRITERIA_MARKED:
981                 return CRITERIA_MARKED;
982         case MATCHCRITERIA_NOT_DELETED:
983         case MATCHCRITERIA_DELETED:
984                 return CRITERIA_DELETED;
985         case MATCHCRITERIA_NOT_REPLIED:
986         case MATCHCRITERIA_REPLIED:
987                 return CRITERIA_REPLIED;
988         case MATCHCRITERIA_NOT_FORWARDED:
989         case MATCHCRITERIA_FORWARDED:
990                 return CRITERIA_FORWARDED;
991         case MATCHCRITERIA_LOCKED:
992         case MATCHCRITERIA_NOT_LOCKED:
993                 return CRITERIA_LOCKED;
994         case MATCHCRITERIA_NOT_SPAM:
995         case MATCHCRITERIA_SPAM:
996                 return CRITERIA_SPAM;
997         case MATCHCRITERIA_PARTIAL:
998         case MATCHCRITERIA_NOT_PARTIAL:
999                 return CRITERIA_PARTIAL;
1000         case MATCHCRITERIA_COLORLABEL:
1001         case MATCHCRITERIA_NOT_COLORLABEL:
1002                 return CRITERIA_COLORLABEL;
1003         case MATCHCRITERIA_IGNORE_THREAD:
1004         case MATCHCRITERIA_NOT_IGNORE_THREAD:
1005                 return CRITERIA_IGNORE_THREAD;
1006         case MATCHCRITERIA_WATCH_THREAD:
1007         case MATCHCRITERIA_NOT_WATCH_THREAD:
1008                 return CRITERIA_WATCH_THREAD;
1009         case MATCHCRITERIA_NOT_SUBJECT:
1010         case MATCHCRITERIA_SUBJECT:
1011                 return CRITERIA_SUBJECT;
1012         case MATCHCRITERIA_NOT_FROM:
1013         case MATCHCRITERIA_FROM:
1014                 return CRITERIA_FROM;
1015         case MATCHCRITERIA_NOT_TO:
1016         case MATCHCRITERIA_TO:
1017                 return CRITERIA_TO;
1018         case MATCHCRITERIA_NOT_CC:
1019         case MATCHCRITERIA_CC:
1020                 return CRITERIA_CC;
1021         case MATCHCRITERIA_NOT_NEWSGROUPS:
1022         case MATCHCRITERIA_NEWSGROUPS:
1023                 return CRITERIA_NEWSGROUPS;
1024         case MATCHCRITERIA_NOT_INREPLYTO:
1025         case MATCHCRITERIA_INREPLYTO:
1026                 return CRITERIA_INREPLYTO;
1027         case MATCHCRITERIA_NOT_REFERENCES:
1028         case MATCHCRITERIA_REFERENCES:
1029                 return CRITERIA_REFERENCES;
1030         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1031         case MATCHCRITERIA_TO_OR_CC:
1032                 return CRITERIA_TO_OR_CC;
1033         case MATCHCRITERIA_NOT_TAG:
1034         case MATCHCRITERIA_TAG:
1035                 return CRITERIA_TAG;
1036         case MATCHCRITERIA_NOT_TAGGED:
1037         case MATCHCRITERIA_TAGGED:
1038                 return CRITERIA_TAGGED;
1039         case MATCHCRITERIA_NOT_BODY_PART:
1040         case MATCHCRITERIA_BODY_PART:
1041                 return CRITERIA_BODY_PART;
1042         case MATCHCRITERIA_NOT_MESSAGE:
1043         case MATCHCRITERIA_MESSAGE:
1044                 return CRITERIA_MESSAGE;
1045         case MATCHCRITERIA_NOT_HEADERS_PART:
1046         case MATCHCRITERIA_HEADERS_PART:
1047                 return CRITERIA_HEADERS_PART;
1048         case MATCHCRITERIA_NOT_HEADER:
1049         case MATCHCRITERIA_HEADER:
1050                 return CRITERIA_HEADER;
1051         case MATCHCRITERIA_AGE_GREATER:
1052                 return CRITERIA_AGE_GREATER;
1053         case MATCHCRITERIA_AGE_LOWER:
1054                 return CRITERIA_AGE_LOWER;
1055         case MATCHCRITERIA_SCORE_GREATER:
1056                 return CRITERIA_SCORE_GREATER;
1057         case MATCHCRITERIA_SCORE_LOWER:
1058                 return CRITERIA_SCORE_LOWER;
1059         case MATCHCRITERIA_SCORE_EQUAL:
1060                 return CRITERIA_SCORE_EQUAL;
1061         case MATCHCRITERIA_NOT_TEST:
1062         case MATCHCRITERIA_TEST:
1063                 return CRITERIA_TEST;
1064         case MATCHCRITERIA_SIZE_GREATER:
1065                 return CRITERIA_SIZE_GREATER;
1066         case MATCHCRITERIA_SIZE_SMALLER:
1067                 return CRITERIA_SIZE_SMALLER;
1068         case MATCHCRITERIA_SIZE_EQUAL:
1069                 return CRITERIA_SIZE_EQUAL;
1070         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1071         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1072                 return CRITERIA_FOUND_IN_ADDRESSBOOK;
1073         default:
1074                 return -1;
1075         }
1076 }
1077
1078 /*!
1079  *\brief        Returns the matcher keyword id from a criteria id
1080  *
1081  *\param        criteria_id Criteria id (should not be the negate
1082  *              one)
1083  *
1084  *\return       gint A matcher keyword id. See #get_matchparser_tab_id.
1085  */
1086 static gint prefs_matcher_get_matching_from_criteria(gint criteria_id)
1087 {
1088         switch (criteria_id) {
1089         case CRITERIA_ALL:
1090                 return MATCHCRITERIA_ALL;
1091         case CRITERIA_UNREAD:
1092                 return MATCHCRITERIA_UNREAD;
1093         case CRITERIA_NEW:
1094                 return MATCHCRITERIA_NEW;
1095         case CRITERIA_MARKED:
1096                 return MATCHCRITERIA_MARKED;
1097         case CRITERIA_DELETED:
1098                 return MATCHCRITERIA_DELETED;
1099         case CRITERIA_REPLIED:
1100                 return MATCHCRITERIA_REPLIED;
1101         case CRITERIA_FORWARDED:
1102                 return MATCHCRITERIA_FORWARDED;
1103         case CRITERIA_LOCKED:
1104                 return MATCHCRITERIA_LOCKED;
1105         case CRITERIA_SPAM:
1106                 return MATCHCRITERIA_SPAM;
1107         case CRITERIA_PARTIAL:
1108                 return MATCHCRITERIA_PARTIAL;
1109         case CRITERIA_COLORLABEL:
1110                 return MATCHCRITERIA_COLORLABEL;
1111         case CRITERIA_IGNORE_THREAD:
1112                 return MATCHCRITERIA_IGNORE_THREAD;
1113         case CRITERIA_WATCH_THREAD:
1114                 return MATCHCRITERIA_WATCH_THREAD;
1115         case CRITERIA_SUBJECT:
1116                 return MATCHCRITERIA_SUBJECT;
1117         case CRITERIA_FROM:
1118                 return MATCHCRITERIA_FROM;
1119         case CRITERIA_TO:
1120                 return MATCHCRITERIA_TO;
1121         case CRITERIA_CC:
1122                 return MATCHCRITERIA_CC;
1123         case CRITERIA_TO_OR_CC:
1124                 return MATCHCRITERIA_TO_OR_CC;
1125         case CRITERIA_TAG:
1126                 return MATCHCRITERIA_TAG;
1127         case CRITERIA_TAGGED:
1128                 return MATCHCRITERIA_TAGGED;
1129         case CRITERIA_NEWSGROUPS:
1130                 return MATCHCRITERIA_NEWSGROUPS;
1131         case CRITERIA_INREPLYTO:
1132                 return MATCHCRITERIA_INREPLYTO;
1133         case CRITERIA_REFERENCES:
1134                 return MATCHCRITERIA_REFERENCES;
1135         case CRITERIA_AGE_GREATER:
1136                 return MATCHCRITERIA_AGE_GREATER;
1137         case CRITERIA_AGE_LOWER:
1138                 return MATCHCRITERIA_AGE_LOWER;
1139         case CRITERIA_SCORE_GREATER:
1140                 return MATCHCRITERIA_SCORE_GREATER;
1141         case CRITERIA_SCORE_LOWER:
1142                 return MATCHCRITERIA_SCORE_LOWER;
1143         case CRITERIA_SCORE_EQUAL:
1144                 return MATCHCRITERIA_SCORE_EQUAL;
1145         case CRITERIA_HEADER:
1146                 return MATCHCRITERIA_HEADER;
1147         case CRITERIA_HEADERS_PART:
1148                 return MATCHCRITERIA_HEADERS_PART;
1149         case CRITERIA_BODY_PART:
1150                 return MATCHCRITERIA_BODY_PART;
1151         case CRITERIA_MESSAGE:
1152                 return MATCHCRITERIA_MESSAGE;
1153         case CRITERIA_TEST:
1154                 return MATCHCRITERIA_TEST;
1155         case CRITERIA_SIZE_GREATER:
1156                 return MATCHCRITERIA_SIZE_GREATER;
1157         case CRITERIA_SIZE_SMALLER:
1158                 return MATCHCRITERIA_SIZE_SMALLER;
1159         case CRITERIA_SIZE_EQUAL:
1160                 return MATCHCRITERIA_SIZE_EQUAL;
1161         case CRITERIA_FOUND_IN_ADDRESSBOOK:
1162                 return MATCHCRITERIA_FOUND_IN_ADDRESSBOOK;
1163         default:
1164                 return -1;
1165         }
1166 }
1167
1168 /*!
1169  *\brief        Returns the negate matcher keyword id from a matcher keyword
1170  *              id.
1171  *
1172  *\param        matcher_criteria Matcher keyword id. 
1173  *
1174  *\return       gint A matcher keyword id. See #get_matchparser_tab_id.
1175  */
1176 static gint prefs_matcher_not_criteria(gint matcher_criteria)
1177 {
1178         switch(matcher_criteria) {
1179         case MATCHCRITERIA_UNREAD:
1180                 return MATCHCRITERIA_NOT_UNREAD;
1181         case MATCHCRITERIA_NEW:
1182                 return MATCHCRITERIA_NOT_NEW;
1183         case MATCHCRITERIA_MARKED:
1184                 return MATCHCRITERIA_NOT_MARKED;
1185         case MATCHCRITERIA_DELETED:
1186                 return MATCHCRITERIA_NOT_DELETED;
1187         case MATCHCRITERIA_REPLIED:
1188                 return MATCHCRITERIA_NOT_REPLIED;
1189         case MATCHCRITERIA_FORWARDED:
1190                 return MATCHCRITERIA_NOT_FORWARDED;
1191         case MATCHCRITERIA_LOCKED:
1192                 return MATCHCRITERIA_NOT_LOCKED;
1193         case MATCHCRITERIA_SPAM:
1194                 return MATCHCRITERIA_NOT_SPAM;
1195         case MATCHCRITERIA_PARTIAL:
1196                 return MATCHCRITERIA_NOT_PARTIAL;
1197         case MATCHCRITERIA_COLORLABEL:
1198                 return MATCHCRITERIA_NOT_COLORLABEL;
1199         case MATCHCRITERIA_IGNORE_THREAD:
1200                 return MATCHCRITERIA_NOT_IGNORE_THREAD;
1201         case MATCHCRITERIA_WATCH_THREAD:
1202                 return MATCHCRITERIA_NOT_WATCH_THREAD;
1203         case MATCHCRITERIA_SUBJECT:
1204                 return MATCHCRITERIA_NOT_SUBJECT;
1205         case MATCHCRITERIA_FROM:
1206                 return MATCHCRITERIA_NOT_FROM;
1207         case MATCHCRITERIA_TO:
1208                 return MATCHCRITERIA_NOT_TO;
1209         case MATCHCRITERIA_CC:
1210                 return MATCHCRITERIA_NOT_CC;
1211         case MATCHCRITERIA_TO_OR_CC:
1212                 return MATCHCRITERIA_NOT_TO_AND_NOT_CC;
1213         case MATCHCRITERIA_TAG:
1214                 return MATCHCRITERIA_NOT_TAG;
1215         case MATCHCRITERIA_TAGGED:
1216                 return MATCHCRITERIA_NOT_TAGGED;
1217         case MATCHCRITERIA_NEWSGROUPS:
1218                 return MATCHCRITERIA_NOT_NEWSGROUPS;
1219         case MATCHCRITERIA_INREPLYTO:
1220                 return MATCHCRITERIA_NOT_INREPLYTO;
1221         case MATCHCRITERIA_REFERENCES:
1222                 return MATCHCRITERIA_NOT_REFERENCES;
1223         case MATCHCRITERIA_HEADER:
1224                 return MATCHCRITERIA_NOT_HEADER;
1225         case MATCHCRITERIA_HEADERS_PART:
1226                 return MATCHCRITERIA_NOT_HEADERS_PART;
1227         case MATCHCRITERIA_MESSAGE:
1228                 return MATCHCRITERIA_NOT_MESSAGE;
1229         case MATCHCRITERIA_TEST:
1230                 return MATCHCRITERIA_NOT_TEST;
1231         case MATCHCRITERIA_BODY_PART:
1232                 return MATCHCRITERIA_NOT_BODY_PART;
1233         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1234                 return MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK;
1235         default:
1236                 return matcher_criteria;
1237         }
1238 }
1239
1240 /*!
1241  *\brief        Converts the text in the selected row to a 
1242  *              matcher structure
1243  *
1244  *\return       MatcherProp * Newly allocated matcher structure.
1245  */
1246 static MatcherProp *prefs_matcher_dialog_to_matcher(void)
1247 {
1248         MatcherProp *matcherprop;
1249         gint criteria;
1250         gint matchtype;
1251         gint value_pred;
1252         gint value_pred_flag;
1253         gint value_criteria;
1254         gboolean use_regexp;
1255         gboolean case_sensitive;
1256         const gchar *header;
1257         const gchar *expr;
1258         gint value;
1259         const gchar *value_str;
1260
1261         value_criteria = get_sel_from_list(GTK_LIST(matcher.criteria_list));
1262
1263         criteria = prefs_matcher_get_matching_from_criteria(value_criteria);
1264
1265         value_pred = get_sel_from_list(GTK_LIST(matcher.predicate_list));
1266         value_pred_flag = get_sel_from_list(GTK_LIST(matcher.predicate_flag_list));
1267
1268         use_regexp = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(matcher.regexp_checkbtn));
1269         case_sensitive = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(matcher.case_checkbtn));
1270
1271         switch (value_criteria) {
1272         case CRITERIA_UNREAD:
1273         case CRITERIA_NEW:
1274         case CRITERIA_MARKED:
1275         case CRITERIA_DELETED:
1276         case CRITERIA_REPLIED:
1277         case CRITERIA_FORWARDED:
1278         case CRITERIA_LOCKED:
1279         case CRITERIA_SPAM:
1280         case CRITERIA_PARTIAL:
1281         case CRITERIA_TEST:
1282         case CRITERIA_COLORLABEL:
1283         case CRITERIA_IGNORE_THREAD:
1284         case CRITERIA_WATCH_THREAD:
1285         case CRITERIA_FOUND_IN_ADDRESSBOOK:
1286         case CRITERIA_TAGGED:
1287                 if (value_pred_flag == PREDICATE_FLAG_DISABLED)
1288                         criteria = prefs_matcher_not_criteria(criteria);
1289                 break;
1290         case CRITERIA_SUBJECT:
1291         case CRITERIA_FROM:
1292         case CRITERIA_TO:
1293         case CRITERIA_CC:
1294         case CRITERIA_TO_OR_CC:
1295         case CRITERIA_TAG:
1296         case CRITERIA_NEWSGROUPS:
1297         case CRITERIA_INREPLYTO:
1298         case CRITERIA_REFERENCES:
1299         case CRITERIA_HEADERS_PART:
1300         case CRITERIA_BODY_PART:
1301         case CRITERIA_MESSAGE:
1302         case CRITERIA_AGE_GREATER:
1303         case CRITERIA_AGE_LOWER:
1304         case CRITERIA_HEADER:
1305                 if (value_pred == PREDICATE_DOES_NOT_CONTAIN)
1306                         criteria = prefs_matcher_not_criteria(criteria);
1307                 break;
1308         }
1309
1310         if (use_regexp) {
1311                 if (case_sensitive)
1312                         matchtype = MATCHTYPE_REGEXP;
1313                 else
1314                         matchtype = MATCHTYPE_REGEXPCASE;
1315         }
1316         else {
1317                 if (case_sensitive)
1318                         matchtype = MATCHTYPE_MATCH;
1319                 else
1320                         matchtype = MATCHTYPE_MATCHCASE;
1321         }
1322
1323         header = NULL;
1324         expr = NULL;
1325         value = 0;
1326
1327         switch (value_criteria) {
1328         case CRITERIA_ALL:
1329         case CRITERIA_UNREAD:
1330         case CRITERIA_NEW:
1331         case CRITERIA_MARKED:
1332         case CRITERIA_DELETED:
1333         case CRITERIA_REPLIED:
1334         case CRITERIA_FORWARDED:
1335         case CRITERIA_LOCKED:
1336         case CRITERIA_SPAM:
1337         case CRITERIA_PARTIAL:
1338         case CRITERIA_IGNORE_THREAD:
1339         case CRITERIA_WATCH_THREAD:
1340         case CRITERIA_TAGGED:
1341                 break;
1342
1343         case CRITERIA_SUBJECT:
1344         case CRITERIA_FROM:
1345         case CRITERIA_TO:
1346         case CRITERIA_CC:
1347         case CRITERIA_TO_OR_CC:
1348         case CRITERIA_TAG:
1349         case CRITERIA_NEWSGROUPS:
1350         case CRITERIA_INREPLYTO:
1351         case CRITERIA_REFERENCES:
1352         case CRITERIA_HEADERS_PART:
1353         case CRITERIA_BODY_PART:
1354         case CRITERIA_MESSAGE:
1355         case CRITERIA_TEST:
1356                 expr = gtk_entry_get_text(GTK_ENTRY(matcher.value_entry));
1357                 break;
1358
1359         case CRITERIA_AGE_GREATER:
1360         case CRITERIA_AGE_LOWER:
1361         case CRITERIA_SCORE_GREATER:
1362         case CRITERIA_SCORE_LOWER:
1363         case CRITERIA_SCORE_EQUAL:
1364         case CRITERIA_SIZE_GREATER:
1365         case CRITERIA_SIZE_SMALLER:
1366         case CRITERIA_SIZE_EQUAL:
1367                 value_str = gtk_entry_get_text(GTK_ENTRY(matcher.value_entry));
1368
1369                 if (*value_str == '\0') {
1370                     alertpanel_error(_("Value is not set."));
1371                     return NULL;
1372                 }
1373
1374                 value = atoi(value_str);
1375                 break;
1376                 
1377         case CRITERIA_COLORLABEL:
1378                 value = colorlabel_get_color_menu_active_item
1379                         (gtk_option_menu_get_menu(GTK_OPTION_MENU
1380                                 (matcher.color_optmenu))); 
1381                 break;
1382
1383         case CRITERIA_HEADER:
1384                 header = gtk_entry_get_text(GTK_ENTRY(matcher.header_entry));
1385                 expr = gtk_entry_get_text(GTK_ENTRY(matcher.value_entry));
1386
1387                 if (*header == '\0') {
1388                     alertpanel_error(_("Header name is not set."));
1389                     return NULL;
1390                 }
1391                 break;
1392
1393         case CRITERIA_FOUND_IN_ADDRESSBOOK:
1394                 header = gtk_entry_get_text(GTK_ENTRY(matcher.header_addr_entry));
1395                 expr = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry));
1396
1397                 if (*header == '\0') {
1398                     alertpanel_error(_("Header name is not set."));
1399                     return NULL;
1400                 }
1401                 if (*expr == '\0') {
1402                         gchar *msg;
1403                         gchar *tmp;
1404
1405                         if (strcasecmp(header, Q_("Filtering Matcher Menu|All")) == 0)
1406                                 tmp = g_strdup(_("all addresses in all headers"));
1407                         else
1408                         if (strcasecmp(header, _("Any")) == 0)
1409                                 tmp = g_strdup(_("any address in any header"));
1410                         else
1411                                 tmp = g_strdup_printf(_("the address(es) in header '%s'"), header);
1412                         msg = g_strdup_printf(_("Book/folder path is not set.\n\n"
1413                                                 "If you want to match %s against the whole address book, "
1414                                                 "you have to select 'Any' from the book/folder drop-down list."),
1415                                                 tmp);
1416                     alertpanel_error(msg);
1417                         g_free(msg);
1418                         g_free(tmp);
1419                     return NULL;
1420                 }
1421                 /* don't store translated "Any"/"All" in matcher expressions */
1422                 if (strcasecmp(header, Q_("Filtering Matcher Menu|All")) == 0)
1423                         header = "All";
1424                 else
1425                         if (strcasecmp(header, _("Any")) == 0)
1426                                 header = "Any";
1427                 if (strcasecmp(expr, _("Any")) == 0)
1428                         expr = "Any";
1429                 break;
1430         }
1431
1432         matcherprop = matcherprop_new(criteria, header, matchtype,
1433                                       expr, value);
1434
1435         return matcherprop;
1436 }
1437
1438 /*!
1439  *\brief        Signal handler for register button
1440  */
1441 static void prefs_matcher_register_cb(void)
1442 {
1443         MatcherProp *matcherprop;
1444         
1445         matcherprop = prefs_matcher_dialog_to_matcher();
1446         if (matcherprop == NULL)
1447                 return;
1448
1449         prefs_matcher_list_view_set_row(NULL, matcherprop);
1450
1451         matcherprop_free(matcherprop);
1452
1453         prefs_matcher_reset_condition();
1454 }
1455
1456 /*!
1457  *\brief        Signal handler for substitute button
1458  */
1459 static void prefs_matcher_substitute_cb(void)
1460 {
1461         MatcherProp *matcherprop;
1462         GtkTreeIter row;
1463         GtkTreeSelection *selection;
1464         GtkTreeModel *model;
1465         gboolean is_valid;
1466
1467         selection = gtk_tree_view_get_selection
1468                         (GTK_TREE_VIEW(matcher.cond_list_view));
1469         
1470         if (!gtk_tree_selection_get_selected(selection, &model, &row))
1471                 return;
1472         
1473         gtk_tree_model_get(model, &row, 
1474                            PREFS_MATCHER_COND_VALID, &is_valid,
1475                            -1);
1476         if (!is_valid)
1477                 return;
1478
1479         matcherprop = prefs_matcher_dialog_to_matcher();
1480         if (matcherprop == NULL)
1481                 return;
1482
1483         prefs_matcher_list_view_set_row(&row, matcherprop);
1484
1485         matcherprop_free(matcherprop);
1486
1487         prefs_matcher_reset_condition();
1488 }
1489
1490 /*!
1491  *\brief        Signal handler for delete button
1492  */
1493 static void prefs_matcher_delete_cb(void)
1494 {
1495         GtkTreeIter row;
1496         GtkTreeSelection *selection;
1497         GtkTreeModel *model;
1498         gboolean is_valid;
1499
1500         selection = gtk_tree_view_get_selection
1501                         (GTK_TREE_VIEW(matcher.cond_list_view));
1502         
1503         if (!gtk_tree_selection_get_selected(selection, &model, &row))
1504                 return;
1505                 
1506         gtk_tree_model_get(model, &row, 
1507                            PREFS_MATCHER_COND_VALID, &is_valid,
1508                            -1);
1509
1510         if (!is_valid)
1511                 return;
1512
1513         gtk_list_store_remove(GTK_LIST_STORE(model), &row);             
1514
1515         prefs_matcher_reset_condition();
1516 }
1517
1518 /*!
1519  *\brief        Signal handler for 'move up' button
1520  */
1521 static void prefs_matcher_up(void)
1522 {
1523         GtkTreePath *prev, *sel, *try;
1524         GtkTreeIter isel;
1525         GtkListStore *store = NULL;
1526         GtkTreeModel *model = NULL;
1527         GtkTreeIter iprev;
1528         
1529         if (!gtk_tree_selection_get_selected
1530                 (gtk_tree_view_get_selection
1531                         (GTK_TREE_VIEW(matcher.cond_list_view)),
1532                  &model,        
1533                  &isel))
1534                 return;
1535         store = (GtkListStore *)model;
1536         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
1537         if (!sel)
1538                 return;
1539         
1540         /* no move if we're at row 0 or 1, looks phony, but other
1541          * solutions are more convoluted... */
1542         try = gtk_tree_path_copy(sel);
1543         if (!gtk_tree_path_prev(try) || !gtk_tree_path_prev(try)) {
1544                 gtk_tree_path_free(try);
1545                 gtk_tree_path_free(sel);
1546                 return;
1547         }
1548         gtk_tree_path_free(try);
1549
1550         prev = gtk_tree_path_copy(sel);         
1551         if (gtk_tree_path_prev(prev)) {
1552                 gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
1553                                         &iprev, prev);
1554                 gtk_list_store_swap(store, &iprev, &isel);
1555                 /* XXX: GTK2 select row?? */
1556         }
1557
1558         gtk_tree_path_free(sel);
1559         gtk_tree_path_free(prev);
1560 }
1561
1562 /*!
1563  *\brief        Signal handler for 'move down' button
1564  */
1565 static void prefs_matcher_down(void)
1566 {
1567         GtkListStore *store = NULL;
1568         GtkTreeModel *model = NULL;
1569         GtkTreeIter next, sel;
1570         GtkTreePath *try;
1571         
1572         if (!gtk_tree_selection_get_selected
1573                 (gtk_tree_view_get_selection
1574                         (GTK_TREE_VIEW(matcher.cond_list_view)),
1575                  &model,
1576                  &sel))
1577                 return;
1578         store = (GtkListStore *)model;
1579         try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
1580         if (!try) 
1581                 return;
1582         
1583         /* move when not at row 0 ... */
1584         if (gtk_tree_path_prev(try)) {
1585                 next = sel;
1586                 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next))
1587                         gtk_list_store_swap(store, &next, &sel);
1588         }
1589                 
1590         gtk_tree_path_free(try);
1591 }
1592
1593 /*!
1594  *\brief        Helper function that allows us to replace the 'Value' entry box
1595  *              by another widget.
1596  *
1597  *\param        old_widget Widget that needs to be removed.
1598  *\param        new_widget Replacement widget
1599  */
1600 static void prefs_matcher_set_value_widget(GtkWidget *old_widget, 
1601                                            GtkWidget *new_widget)
1602 {
1603         /* TODO: find out why the following spews harmless "parent errors" */
1604
1605         /* NOTE: we first need to bump up the refcount of the old_widget,
1606          * because the gtkut_container_remove() will otherwise destroy it */
1607         gtk_widget_ref(old_widget);
1608         gtkut_container_remove(GTK_CONTAINER(matcher.criteria_table), old_widget);
1609         gtk_widget_show(new_widget);
1610         gtk_widget_set_size_request(new_widget, 200, -1);
1611         gtk_table_attach(GTK_TABLE(matcher.criteria_table), new_widget, 
1612                          2, 3, 1, 2, 
1613                          GTK_FILL | GTK_SHRINK | GTK_EXPAND, 
1614                          0, 0, 0);
1615 }
1616
1617 static void prefs_matcher_disable_widget(GtkWidget* widget)
1618 {
1619         g_return_if_fail( widget != NULL);
1620
1621         gtk_widget_set_sensitive(widget, FALSE);
1622         gtk_widget_hide(widget);
1623 }
1624
1625 static void prefs_matcher_enable_widget(GtkWidget* widget)
1626 {
1627         g_return_if_fail( widget != NULL);
1628
1629         gtk_widget_set_sensitive(widget, TRUE);
1630         gtk_widget_show(widget);
1631 }
1632
1633 /*!
1634  *\brief        Change widgets depending on the selected condition
1635  *
1636  *\param        list List widget
1637  *\param        widget Not used
1638  *\param        user_data Not used      
1639  */
1640 static void prefs_matcher_criteria_select(GtkList *list,
1641                                           GtkWidget *widget,
1642                                           gpointer user_data)
1643 {
1644         gint value, old_value;
1645
1646         old_value = matcher.selected_criteria;
1647         matcher.selected_criteria = value = get_sel_from_list
1648                 (GTK_LIST(matcher.criteria_list));
1649
1650         if (old_value == matcher.selected_criteria)
1651                 return;
1652
1653         /* CLAWS: the value widget is currently either the color label combo box,
1654          * or a GtkEntry, so kiss for now */
1655         if (matcher.selected_criteria == CRITERIA_COLORLABEL) { 
1656                 prefs_matcher_set_value_widget(matcher.value_entry, 
1657                                                matcher.color_optmenu);
1658         } else if (old_value == CRITERIA_COLORLABEL) {
1659                 prefs_matcher_set_value_widget(matcher.color_optmenu,
1660                                                matcher.value_entry);
1661         }                                              
1662
1663         switch (value) {
1664         case CRITERIA_ALL:
1665                 prefs_matcher_disable_widget(matcher.header_combo);
1666                 prefs_matcher_disable_widget(matcher.header_label);
1667                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1668                 prefs_matcher_disable_widget(matcher.header_addr_label);
1669                 prefs_matcher_disable_widget(matcher.value_label);
1670                 prefs_matcher_disable_widget(matcher.value_entry);
1671                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1672                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1673                 prefs_matcher_disable_widget(matcher.predicate_label);
1674                 prefs_matcher_disable_widget(matcher.predicate_combo);
1675                 prefs_matcher_disable_widget(matcher.predicate_flag_combo);
1676                 prefs_matcher_disable_widget(matcher.case_checkbtn);
1677                 prefs_matcher_disable_widget(matcher.regexp_checkbtn);
1678                 prefs_matcher_disable_widget(matcher.test_btn);
1679                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1680                 break;
1681
1682         case CRITERIA_UNREAD:
1683         case CRITERIA_NEW:
1684         case CRITERIA_MARKED:
1685         case CRITERIA_DELETED:
1686         case CRITERIA_REPLIED:
1687         case CRITERIA_FORWARDED:
1688         case CRITERIA_LOCKED:
1689         case CRITERIA_SPAM:
1690         case CRITERIA_PARTIAL:
1691         case CRITERIA_IGNORE_THREAD:
1692         case CRITERIA_WATCH_THREAD:
1693         case CRITERIA_TAGGED:
1694                 prefs_matcher_disable_widget(matcher.header_combo);
1695                 prefs_matcher_disable_widget(matcher.header_label);
1696                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1697                 prefs_matcher_disable_widget(matcher.header_addr_label);
1698                 prefs_matcher_disable_widget(matcher.value_label);
1699                 prefs_matcher_disable_widget(matcher.value_entry);
1700                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1701                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1702                 prefs_matcher_enable_widget(matcher.predicate_label);
1703                 prefs_matcher_disable_widget(matcher.predicate_combo);
1704                 prefs_matcher_enable_widget(matcher.predicate_flag_combo);
1705                 prefs_matcher_disable_widget(matcher.case_checkbtn);
1706                 prefs_matcher_disable_widget(matcher.regexp_checkbtn);
1707                 prefs_matcher_disable_widget(matcher.test_btn);
1708                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1709                 break;
1710                 
1711         case CRITERIA_COLORLABEL:
1712                 prefs_matcher_disable_widget(matcher.header_combo);
1713                 prefs_matcher_disable_widget(matcher.header_label);
1714                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1715                 prefs_matcher_disable_widget(matcher.header_addr_label);
1716                 prefs_matcher_enable_widget(matcher.value_label);
1717                 prefs_matcher_disable_widget(matcher.value_entry);
1718                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1719                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1720                 prefs_matcher_enable_widget(matcher.predicate_label);
1721                 prefs_matcher_disable_widget(matcher.predicate_combo);
1722                 prefs_matcher_enable_widget(matcher.predicate_flag_combo);
1723                 prefs_matcher_disable_widget(matcher.case_checkbtn);
1724                 prefs_matcher_disable_widget(matcher.regexp_checkbtn);
1725                 prefs_matcher_disable_widget(matcher.test_btn);
1726                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1727                 break;
1728
1729         case CRITERIA_SUBJECT:
1730         case CRITERIA_FROM:
1731         case CRITERIA_TO:
1732         case CRITERIA_CC:
1733         case CRITERIA_TO_OR_CC:
1734         case CRITERIA_TAG:
1735         case CRITERIA_NEWSGROUPS:
1736         case CRITERIA_INREPLYTO:
1737         case CRITERIA_REFERENCES:
1738         case CRITERIA_HEADERS_PART:
1739         case CRITERIA_BODY_PART:
1740         case CRITERIA_MESSAGE:
1741                 prefs_matcher_disable_widget(matcher.header_combo);
1742                 prefs_matcher_disable_widget(matcher.header_label);
1743                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1744                 prefs_matcher_disable_widget(matcher.header_addr_label);
1745                 prefs_matcher_enable_widget(matcher.value_label);
1746                 prefs_matcher_enable_widget(matcher.value_entry);
1747                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1748                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1749                 prefs_matcher_enable_widget(matcher.predicate_label);
1750                 prefs_matcher_enable_widget(matcher.predicate_combo);
1751                 prefs_matcher_disable_widget(matcher.predicate_flag_combo);
1752                 prefs_matcher_enable_widget(matcher.case_checkbtn);
1753                 prefs_matcher_enable_widget(matcher.regexp_checkbtn);
1754                 prefs_matcher_disable_widget(matcher.test_btn);
1755                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1756                 break;
1757
1758         case CRITERIA_TEST:
1759                 prefs_matcher_disable_widget(matcher.header_combo);
1760                 prefs_matcher_disable_widget(matcher.header_label);
1761                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1762                 prefs_matcher_disable_widget(matcher.header_addr_label);
1763                 prefs_matcher_enable_widget(matcher.value_label);
1764                 prefs_matcher_enable_widget(matcher.value_entry);
1765                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1766                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1767                 prefs_matcher_enable_widget(matcher.predicate_label);
1768                 prefs_matcher_disable_widget(matcher.predicate_combo);
1769                 prefs_matcher_enable_widget(matcher.predicate_flag_combo);
1770                 prefs_matcher_disable_widget(matcher.case_checkbtn);
1771                 prefs_matcher_disable_widget(matcher.regexp_checkbtn);
1772                 prefs_matcher_enable_widget(matcher.test_btn);
1773                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1774                 break;
1775
1776         case CRITERIA_AGE_GREATER:
1777         case CRITERIA_AGE_LOWER:
1778         case CRITERIA_SCORE_GREATER:
1779         case CRITERIA_SCORE_LOWER:
1780         case CRITERIA_SCORE_EQUAL:
1781         case CRITERIA_SIZE_GREATER:
1782         case CRITERIA_SIZE_SMALLER:
1783         case CRITERIA_SIZE_EQUAL:
1784                 prefs_matcher_disable_widget(matcher.header_combo);
1785                 prefs_matcher_disable_widget(matcher.header_label);
1786                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1787                 prefs_matcher_disable_widget(matcher.header_addr_label);
1788                 prefs_matcher_enable_widget(matcher.value_label);
1789                 prefs_matcher_enable_widget(matcher.value_entry);
1790                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1791                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1792                 prefs_matcher_disable_widget(matcher.predicate_label);
1793                 prefs_matcher_disable_widget(matcher.predicate_combo);
1794                 prefs_matcher_disable_widget(matcher.predicate_flag_combo);
1795                 prefs_matcher_disable_widget(matcher.case_checkbtn);
1796                 prefs_matcher_disable_widget(matcher.regexp_checkbtn);
1797                 prefs_matcher_disable_widget(matcher.test_btn);
1798                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1799                 break;
1800
1801         case CRITERIA_HEADER:
1802                 prefs_matcher_enable_widget(matcher.header_combo);
1803                 prefs_matcher_enable_widget(matcher.header_label);
1804                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1805                 prefs_matcher_disable_widget(matcher.header_addr_label);
1806                 prefs_matcher_enable_widget(matcher.value_label);
1807                 prefs_matcher_enable_widget(matcher.value_entry);
1808                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1809                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1810                 prefs_matcher_enable_widget(matcher.predicate_label);
1811                 prefs_matcher_enable_widget(matcher.predicate_combo);
1812                 prefs_matcher_disable_widget(matcher.predicate_flag_combo);
1813                 prefs_matcher_enable_widget(matcher.case_checkbtn);
1814                 prefs_matcher_enable_widget(matcher.regexp_checkbtn);
1815                 prefs_matcher_disable_widget(matcher.test_btn);
1816                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1817                 break;
1818
1819         case CRITERIA_FOUND_IN_ADDRESSBOOK:
1820                 prefs_matcher_disable_widget(matcher.header_combo);
1821                 prefs_matcher_disable_widget(matcher.header_label);
1822                 prefs_matcher_enable_widget(matcher.header_addr_combo);
1823                 prefs_matcher_enable_widget(matcher.header_addr_label);
1824                 prefs_matcher_disable_widget(matcher.value_label);
1825                 prefs_matcher_disable_widget(matcher.value_entry);
1826                 prefs_matcher_enable_widget(matcher.addressbook_folder_label);
1827                 prefs_matcher_enable_widget(matcher.addressbook_folder_combo);
1828                 prefs_matcher_enable_widget(matcher.predicate_label);
1829                 prefs_matcher_disable_widget(matcher.predicate_combo);
1830                 prefs_matcher_enable_widget(matcher.predicate_flag_combo);
1831                 prefs_matcher_disable_widget(matcher.case_checkbtn);
1832                 prefs_matcher_disable_widget(matcher.regexp_checkbtn);
1833                 prefs_matcher_disable_widget(matcher.test_btn);
1834                 prefs_matcher_enable_widget(matcher.addressbook_select_btn);
1835                 break;
1836         }
1837 }
1838
1839 /*!
1840  *\brief        Handle key press
1841  *
1842  *\param        widget Widget receiving key press
1843  *\param        event Key event
1844  *\param        data User data
1845  */
1846 static gboolean prefs_matcher_key_pressed(GtkWidget *widget, GdkEventKey *event,
1847                                      gpointer data)
1848 {
1849         if (event && event->keyval == GDK_Escape) {
1850                 prefs_matcher_cancel();
1851                 return TRUE;            
1852         }
1853         return FALSE;
1854 }
1855
1856 /*!
1857  *\brief        Cancel matcher dialog
1858  */
1859 static void prefs_matcher_cancel(void)
1860 {
1861         gtk_widget_hide(matcher.window);
1862         inc_unlock();
1863 }
1864
1865 /*!
1866  *\brief        Accept current matchers
1867  */
1868 static void prefs_matcher_ok(void)
1869 {
1870         MatcherList *matchers;
1871         MatcherProp *matcherprop;
1872         AlertValue val;
1873         gchar *matcher_str = NULL;
1874         gchar *str = NULL;
1875         gint row = 1;
1876         GtkTreeModel *model;
1877         GtkTreeIter iter;
1878
1879         matchers = prefs_matcher_get_list();
1880
1881         if (matchers != NULL) {
1882                 matcherprop = prefs_matcher_dialog_to_matcher();
1883                 if (matcherprop != NULL) {
1884                         str = matcherprop_to_string(matcherprop);
1885                         matcherprop_free(matcherprop);
1886                         if (strcmp(str, "all") != 0) {
1887                                 model = gtk_tree_view_get_model(GTK_TREE_VIEW
1888                                                 (matcher.cond_list_view));
1889
1890                                 while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1891                                         gtk_tree_model_get(model, &iter,
1892                                                            PREFS_MATCHER_COND, &matcher_str,
1893                                                            -1);
1894                                         if (matcher_str && strcmp(matcher_str, str) == 0) 
1895                                                 break;
1896                                         row++;
1897                                         g_free(matcher_str);
1898                                         matcher_str = NULL;
1899                                 }
1900
1901                                 if (!matcher_str || strcmp(matcher_str, str) != 0) {
1902                                         val = alertpanel(_("Entry not saved"),
1903                                                  _("The entry was not saved.\nClose anyway?"),
1904                                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1905                                         if (G_ALERTDEFAULT != val) {
1906                                                 g_free(matcher_str);                                             
1907                                                 g_free(str);
1908                                                 return;
1909                                         }
1910                                 }
1911                                 g_free(matcher_str);
1912                         }
1913                 }
1914                 g_free(str);
1915                 gtk_widget_hide(matcher.window);
1916                 inc_unlock();
1917                 if (matchers_callback != NULL)
1918                         matchers_callback(matchers);
1919                 matcherlist_free(matchers);
1920         }
1921 }
1922
1923 /*!
1924  *\brief        Called when closing dialog box
1925  *
1926  *\param        widget Dialog widget
1927  *\param        event Event info
1928  *\param        data User data
1929  *
1930  *\return       gint TRUE
1931  */
1932 static gint prefs_matcher_deleted(GtkWidget *widget, GdkEventAny *event,
1933                                   gpointer data)
1934 {
1935         prefs_matcher_cancel();
1936         return TRUE;
1937 }
1938
1939 /*
1940  * Strings describing test format strings
1941  * 
1942  * When adding new lines, remember to put 2 strings for each line
1943  */
1944 static gchar *test_desc_strings[] = {
1945         "%%",   N_("literal %"),
1946         "%s",   N_("Subject"),
1947         "%f",   N_("From"),
1948         "%t",   N_("To"),
1949         "%c",   N_("Cc"),
1950         "%d",   N_("Date"),
1951         "%i",   N_("Message-ID"),
1952         "%n",   N_("Newsgroups"),
1953         "%r",   N_("References"),
1954         "%F",   N_("filename (should not be modified)"),
1955         "\\n",  N_("new line"),
1956         "\\",   N_("escape character for quotes"),
1957         "\\\"", N_("quote character"),
1958         NULL,   NULL
1959 };
1960
1961 static DescriptionWindow test_desc_win = { 
1962         NULL,
1963         NULL, 
1964         2,
1965         N_("Match Type: 'Test'"),
1966         N_("'Test' allows you to test a message or message element "
1967            "using an external program or script. The program will "
1968            "return either 0 or 1.\n\n"
1969            "The following symbols can be used:"),
1970         test_desc_strings
1971 };
1972
1973
1974
1975 /*!
1976  *\brief        Show Test action's info
1977  */
1978 void prefs_matcher_test_info(void)
1979 {
1980         description_window_create(&test_desc_win);
1981 }
1982
1983 void prefs_matcher_addressbook_select(void)
1984 {
1985         gchar *folderpath = NULL;
1986         gboolean ret = FALSE;
1987
1988         folderpath = (gchar *) gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry));
1989         ret = addressbook_folder_selection(&folderpath);
1990         if ( ret != FALSE && folderpath != NULL)
1991                 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry), folderpath);
1992 }
1993
1994
1995 /*
1996  * list view
1997  */
1998
1999 static GtkListStore* prefs_matcher_create_data_store(void)
2000 {
2001         return gtk_list_store_new(N_PREFS_MATCHER_COLUMNS,
2002                                   G_TYPE_STRING,        
2003                                   G_TYPE_BOOLEAN,
2004                                   -1);
2005 }
2006
2007 static void prefs_matcher_list_view_insert_matcher(GtkWidget *list_view,
2008                                                    GtkTreeIter *row_iter,
2009                                                    const gchar *matcher,
2010                                                    gboolean is_valid) 
2011 {
2012         GtkTreeIter iter;
2013         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
2014                                         (GTK_TREE_VIEW(list_view)));
2015
2016         if (row_iter == NULL) {
2017                 /* append new */
2018                 gtk_list_store_append(list_store, &iter);
2019         } else {
2020                 /* change existing */
2021                 iter = *row_iter;
2022         }
2023
2024         gtk_list_store_set(list_store, &iter,
2025                            PREFS_MATCHER_COND, matcher,
2026                            PREFS_MATCHER_COND_VALID, is_valid,
2027                            -1);
2028 }
2029
2030 static GtkWidget *prefs_matcher_list_view_create(void)
2031 {
2032         GtkTreeView *list_view;
2033         GtkTreeSelection *selector;
2034         GtkTreeModel *model;
2035
2036         model = GTK_TREE_MODEL(prefs_matcher_create_data_store());
2037         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
2038         g_object_unref(model);  
2039         
2040         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
2041         
2042         selector = gtk_tree_view_get_selection(list_view);
2043         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
2044         gtk_tree_selection_set_select_function(selector, prefs_matcher_selected,
2045                                                NULL, NULL);
2046
2047         /* create the columns */
2048         prefs_matcher_create_list_view_columns(GTK_WIDGET(list_view));
2049
2050         return GTK_WIDGET(list_view);
2051 }
2052
2053 static void prefs_matcher_create_list_view_columns(GtkWidget *list_view)
2054 {
2055         GtkTreeViewColumn *column;
2056         GtkCellRenderer *renderer;
2057
2058         renderer = gtk_cell_renderer_text_new();
2059         column = gtk_tree_view_column_new_with_attributes
2060                 (_("Current condition rules"),
2061                  renderer,
2062                  "text", PREFS_MATCHER_COND,
2063                  NULL);
2064         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
2065 }
2066
2067 static gboolean prefs_matcher_selected(GtkTreeSelection *selector,
2068                                        GtkTreeModel *model, 
2069                                        GtkTreePath *path,
2070                                        gboolean currently_selected,
2071                                        gpointer data)
2072 {
2073         gchar *matcher_str;
2074         MatcherProp *prop;
2075         gboolean negative_cond;
2076         gint criteria;
2077         GtkTreeIter iter;
2078         gboolean is_valid;
2079
2080         if (currently_selected)
2081                 return TRUE;
2082
2083         if (!gtk_tree_model_get_iter(model, &iter, path))
2084                 return TRUE;
2085
2086         gtk_tree_model_get(model, &iter, 
2087                            PREFS_MATCHER_COND_VALID,  &is_valid,
2088                            PREFS_MATCHER_COND, &matcher_str,
2089                            -1);
2090         
2091         if (!is_valid) {
2092                 g_free(matcher_str);
2093                 prefs_matcher_reset_condition();
2094                 return TRUE;
2095         }
2096
2097         negative_cond = FALSE;
2098
2099         prop = matcher_parser_get_prop(matcher_str);
2100         if (prop == NULL) {
2101                 g_free(matcher_str);
2102                 return TRUE;
2103         }               
2104
2105         criteria = prefs_matcher_get_criteria_from_matching(prop->criteria);
2106         if (criteria != -1)
2107                 gtk_list_select_item(GTK_LIST(matcher.criteria_list),
2108                                      criteria);
2109
2110         switch(prop->criteria) {
2111         case MATCHCRITERIA_NOT_UNREAD:
2112         case MATCHCRITERIA_NOT_NEW:
2113         case MATCHCRITERIA_NOT_MARKED:
2114         case MATCHCRITERIA_NOT_DELETED:
2115         case MATCHCRITERIA_NOT_REPLIED:
2116         case MATCHCRITERIA_NOT_FORWARDED:
2117         case MATCHCRITERIA_NOT_LOCKED:
2118         case MATCHCRITERIA_NOT_SPAM:
2119         case MATCHCRITERIA_NOT_PARTIAL:
2120         case MATCHCRITERIA_NOT_COLORLABEL:
2121         case MATCHCRITERIA_NOT_IGNORE_THREAD:
2122         case MATCHCRITERIA_NOT_WATCH_THREAD:
2123         case MATCHCRITERIA_NOT_SUBJECT:
2124         case MATCHCRITERIA_NOT_FROM:
2125         case MATCHCRITERIA_NOT_TO:
2126         case MATCHCRITERIA_NOT_CC:
2127         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
2128         case MATCHCRITERIA_NOT_TAG:
2129         case MATCHCRITERIA_NOT_TAGGED:
2130         case MATCHCRITERIA_NOT_NEWSGROUPS:
2131         case MATCHCRITERIA_NOT_INREPLYTO:
2132         case MATCHCRITERIA_NOT_REFERENCES:
2133         case MATCHCRITERIA_NOT_HEADER:
2134         case MATCHCRITERIA_NOT_HEADERS_PART:
2135         case MATCHCRITERIA_NOT_MESSAGE:
2136         case MATCHCRITERIA_NOT_BODY_PART:
2137         case MATCHCRITERIA_NOT_TEST:
2138         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
2139                 negative_cond = TRUE;
2140                 break;
2141         }
2142         
2143         switch(prop->criteria) {
2144         case MATCHCRITERIA_ALL:
2145                 break;
2146
2147         case MATCHCRITERIA_NOT_SUBJECT:
2148         case MATCHCRITERIA_NOT_FROM:
2149         case MATCHCRITERIA_NOT_TO:
2150         case MATCHCRITERIA_NOT_CC:
2151         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
2152         case MATCHCRITERIA_NOT_TAG:
2153         case MATCHCRITERIA_NOT_NEWSGROUPS:
2154         case MATCHCRITERIA_NOT_INREPLYTO:
2155         case MATCHCRITERIA_NOT_REFERENCES:
2156         case MATCHCRITERIA_NOT_HEADERS_PART:
2157         case MATCHCRITERIA_NOT_BODY_PART:
2158         case MATCHCRITERIA_NOT_MESSAGE:
2159         case MATCHCRITERIA_NOT_TEST:
2160         case MATCHCRITERIA_SUBJECT:
2161         case MATCHCRITERIA_FROM:
2162         case MATCHCRITERIA_TO:
2163         case MATCHCRITERIA_CC:
2164         case MATCHCRITERIA_TO_OR_CC:
2165         case MATCHCRITERIA_TAG:
2166         case MATCHCRITERIA_NEWSGROUPS:
2167         case MATCHCRITERIA_INREPLYTO:
2168         case MATCHCRITERIA_REFERENCES:
2169         case MATCHCRITERIA_HEADERS_PART:
2170         case MATCHCRITERIA_BODY_PART:
2171         case MATCHCRITERIA_MESSAGE:
2172         case MATCHCRITERIA_TEST:
2173                 gtk_entry_set_text(GTK_ENTRY(matcher.value_entry), prop->expr);
2174                 break;
2175
2176         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
2177         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
2178         {
2179                 gchar *header;
2180                 gchar *expr;
2181
2182                 /* matcher expressions contain UNtranslated "Any"/"All",
2183                   select the relevant translated combo item */
2184                 if (strcasecmp(prop->header, "All") == 0)
2185                         header = (gchar*)Q_("Filtering Matcher Menu|All");
2186                 else
2187                         if (strcasecmp(prop->header, "Any") == 0)
2188                                 header = _("Any");
2189                         else
2190                                 header = prop->header;
2191                 if (strcasecmp(prop->expr, "Any") == 0)
2192                         expr = _("Any");
2193                 else
2194                         expr = prop->expr;
2195
2196                 gtk_entry_set_text(GTK_ENTRY(matcher.header_addr_entry), header);
2197                 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry), expr);
2198                 break;
2199         }
2200
2201         case MATCHCRITERIA_AGE_GREATER:
2202         case MATCHCRITERIA_AGE_LOWER:
2203         case MATCHCRITERIA_SCORE_GREATER:
2204         case MATCHCRITERIA_SCORE_LOWER:
2205         case MATCHCRITERIA_SCORE_EQUAL:
2206         case MATCHCRITERIA_SIZE_GREATER:
2207         case MATCHCRITERIA_SIZE_SMALLER:
2208         case MATCHCRITERIA_SIZE_EQUAL:
2209                 gtk_entry_set_text(GTK_ENTRY(matcher.value_entry), itos(prop->value));
2210                 break;
2211
2212         case MATCHCRITERIA_NOT_COLORLABEL:
2213         case MATCHCRITERIA_COLORLABEL:
2214                 gtk_option_menu_set_history(GTK_OPTION_MENU(matcher.color_optmenu),
2215                                             prop->value);
2216                 break;
2217
2218         case MATCHCRITERIA_NOT_HEADER:
2219         case MATCHCRITERIA_HEADER:
2220                 gtk_entry_set_text(GTK_ENTRY(matcher.header_entry), prop->header);
2221                 gtk_entry_set_text(GTK_ENTRY(matcher.value_entry), prop->expr);
2222                 break;
2223         }
2224
2225         if (negative_cond) {
2226                 gtk_list_select_item(GTK_LIST(matcher.predicate_list), 1);
2227                 gtk_list_select_item(GTK_LIST(matcher.predicate_flag_list), 1);
2228         } else {
2229                 gtk_list_select_item(GTK_LIST(matcher.predicate_list), 0);
2230                 gtk_list_select_item(GTK_LIST(matcher.predicate_flag_list), 0);
2231         }
2232         
2233         switch(prop->matchtype) {
2234         case MATCHTYPE_MATCH:
2235                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_checkbtn), FALSE);
2236                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_checkbtn), TRUE);
2237                 break;
2238
2239         case MATCHTYPE_MATCHCASE:
2240                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_checkbtn), FALSE);
2241                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_checkbtn), FALSE);
2242                 break;
2243
2244         case MATCHTYPE_REGEXP:
2245                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_checkbtn), TRUE);
2246                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_checkbtn), TRUE);
2247                 break;
2248
2249         case MATCHTYPE_REGEXPCASE:
2250                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_checkbtn), TRUE);
2251                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_checkbtn), FALSE);
2252                 break;
2253         }
2254
2255         g_free(matcher_str);
2256         return TRUE;
2257 }
2258