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