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