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