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