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