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