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