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