2006-07-31 [wwp] 2.4.0cvs3
[claws.git] / src / prefs_matcher.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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
50 #include "matcher_parser.h"
51 #include "colorlabel.h"
52
53 enum {
54         PREFS_MATCHER_COND,
55         PREFS_MATCHER_COND_VALID,
56         N_PREFS_MATCHER_COLUMNS
57 };
58
59 /*!
60  *\brief        UI data for matcher dialog
61  */
62 static struct Matcher {
63         GtkWidget *window;
64
65         GtkWidget *ok_btn;
66
67         GtkWidget *predicate_combo;
68         GtkWidget *predicate_flag_combo;
69         GtkWidget *header_combo;
70         GtkWidget *header_addr_combo;
71
72         GtkWidget *criteria_list;
73
74         GtkWidget *predicate_list;
75         GtkWidget *predicate_label;
76         GtkWidget *predicate_flag_list;
77
78         GtkWidget *bool_op_list;
79
80         GtkWidget *header_entry;
81         GtkWidget *header_label;
82         GtkWidget *header_addr_entry;
83         GtkWidget *header_addr_label;
84         GtkWidget *value_entry;
85         GtkWidget *value_label;
86         GtkWidget *addressbook_folder_label;
87         GtkWidget *addressbook_folder_combo;
88         GtkWidget *case_chkbtn;
89         GtkWidget *regexp_chkbtn;
90         GtkWidget *color_optmenu;
91
92         GtkWidget *test_btn;
93         GtkWidget *addressbook_select_btn;
94
95         GtkWidget *cond_list_view;
96
97         GtkWidget *criteria_table;
98
99         gint selected_criteria; /*!< selected criteria in combobox */ 
100 } matcher;
101
102 /*!
103  *\brief        Conditions with a negate counterpart (like unread and ~unread)
104  *              have the same CRITERIA_XXX id). I.e. both unread and ~unread
105  *              have criteria id CRITERIA_UNREAD. This id is passed as the
106  *              first parameter to #matcherprop_new and #matcherprop_unquote_new.
107  *
108  *\warning      Currently the enum constants should have the same order as the 
109  *              #criteria_text  
110  */             
111 enum {
112         CRITERIA_ALL = 0,
113
114         CRITERIA_SUBJECT = 1,
115         CRITERIA_FROM = 2,
116         CRITERIA_TO = 3,
117         CRITERIA_CC = 4,
118         CRITERIA_TO_OR_CC = 5,
119         CRITERIA_NEWSGROUPS = 6,
120         CRITERIA_INREPLYTO = 7,
121         CRITERIA_REFERENCES = 8,
122         CRITERIA_AGE_GREATER = 9,
123         CRITERIA_AGE_LOWER = 10,
124         CRITERIA_HEADER = 11,
125         CRITERIA_HEADERS_PART = 12,
126         CRITERIA_BODY_PART = 13,
127         CRITERIA_MESSAGE = 14,
128
129         CRITERIA_UNREAD = 15,
130         CRITERIA_NEW = 16,
131         CRITERIA_MARKED = 17,
132         CRITERIA_DELETED = 18,
133         CRITERIA_REPLIED = 19,
134         CRITERIA_FORWARDED = 20,
135         CRITERIA_LOCKED = 21,
136         CRITERIA_COLORLABEL = 22,
137         CRITERIA_IGNORE_THREAD = 23,
138
139         CRITERIA_SCORE_GREATER = 24,
140         CRITERIA_SCORE_LOWER = 25,
141         CRITERIA_SCORE_EQUAL = 26,
142
143         CRITERIA_TEST = 27,
144
145         CRITERIA_SIZE_GREATER = 28,
146         CRITERIA_SIZE_SMALLER = 29,
147         CRITERIA_SIZE_EQUAL   = 30,
148         
149         CRITERIA_PARTIAL = 31,
150
151         CRITERIA_FOUND_IN_ADDRESSBOOK = 32
152 };
153
154 /*!
155  *\brief        Descriptive text for conditions
156  */
157 static const gchar *criteria_text [] = {
158         N_("All messages"), N_("Subject"),
159         N_("From"), N_("To"), N_("Cc"), N_("To or Cc"),
160         N_("Newsgroups"), N_("In reply to"), N_("References"),
161         N_("Age greater than"), N_("Age lower than"),
162         N_("Header"), N_("Headers part"),
163         N_("Body part"), N_("Whole message"),
164         N_("Unread flag"), N_("New flag"),
165         N_("Marked flag"), N_("Deleted flag"),
166         N_("Replied flag"), N_("Forwarded flag"),
167         N_("Locked flag"),
168         N_("Color label"),
169         N_("Ignored thread"),
170         N_("Score greater than"), N_("Score lower than"),
171         N_("Score equal to"),
172         N_("Test"),
173         N_("Size greater than"), 
174         N_("Size smaller than"),
175         N_("Size exactly"),
176         N_("Partially downloaded"),
177         N_("Found in addressbook")
178 };
179
180 /*!
181  *\brief        Boolean / predicate constants
182  *
183  *\warning      Same order as #bool_op_text!
184  */
185 enum {
186         BOOL_OP_OR = 0,
187         BOOL_OP_AND = 1
188 };
189
190 /*!
191  *\brief        Descriptive text in UI
192  */
193 static const gchar *bool_op_text [] = {
194         N_("or"), N_("and")
195 };
196
197 /*!
198  *\brief        Contains predicate      
199  *
200  *\warning      Same order as in #predicate_text
201  */
202 enum {
203         PREDICATE_CONTAINS = 0,
204         PREDICATE_DOES_NOT_CONTAIN = 1
205 };
206
207 /*!
208  *\brief        Descriptive text in UI for predicate
209  */
210 static const gchar *predicate_text [] = {
211         N_("contains"), N_("does not contain")
212 };
213
214 /*!
215  *\brief        Preset addressbook book/folder items
216  */
217 static const gchar *addressbook_folder_text [] = {
218         N_("Any")
219 };
220
221 /*!
222  *\brief        Enabled predicate
223  *
224  *\warning      Same order as in #predicate_flag_text
225  */
226 enum {
227         PREDICATE_FLAG_ENABLED = 0,
228         PREDICATE_FLAG_DISABLED = 1
229 };
230
231 /*!
232  *\brief        Descriptive text in UI for enabled flag
233  */
234 static const gchar *predicate_flag_text [] = {
235         N_("yes"), N_("no")
236 };
237
238 /*!
239  *\brief        Hooks
240  */
241 static PrefsMatcherSignal *matchers_callback;
242
243 /* widget creating functions */
244 static void prefs_matcher_create        (void);
245
246 static void prefs_matcher_set_dialog    (MatcherList *matchers);
247 static void prefs_matcher_list_view_set_row     (GtkTreeIter *row, 
248                                                  MatcherProp *prop);
249
250 /* callback functions */
251
252 static void prefs_matcher_register_cb   (void);
253 static void prefs_matcher_substitute_cb (void);
254 static void prefs_matcher_delete_cb     (void);
255 static void prefs_matcher_up            (void);
256 static void prefs_matcher_down          (void);
257 static gboolean prefs_matcher_key_pressed(GtkWidget     *widget,
258                                          GdkEventKey    *event,
259                                          gpointer        data);
260 static void prefs_matcher_ok            (void);
261 static void prefs_matcher_cancel        (void);
262 static gint prefs_matcher_deleted       (GtkWidget *widget, GdkEventAny *event,
263                                          gpointer data);
264 static void prefs_matcher_criteria_select       (GtkList   *list,
265                                                  GtkWidget *widget,
266                                                  gpointer   user_data);
267 static MatcherList *prefs_matcher_get_list      (void);
268
269 static GtkListStore* prefs_matcher_create_data_store    (void);
270
271 static void prefs_matcher_list_view_insert_matcher      (GtkWidget *list_view,
272                                                          GtkTreeIter *row_iter,
273                                                          const gchar *matcher,
274                                                          gboolean is_valid);
275
276 static GtkWidget *prefs_matcher_list_view_create        (void);
277
278 static void prefs_matcher_create_list_view_columns      (GtkWidget *list_view);
279
280 static gboolean prefs_matcher_selected                  (GtkTreeSelection *selector,
281                                                          GtkTreeModel *model, 
282                                                          GtkTreePath *path,
283                                                          gboolean currently_selected,
284                                                          gpointer data);
285
286 /*!
287  *\brief        Find index of list selection 
288  *
289  *\param        list GTK list widget
290  *
291  *\return       gint Selection index
292  */
293 static gint get_sel_from_list(GtkList *list)
294 {
295         gint row = 0;
296         void * sel;
297         GList * child;
298
299         sel = list->selection->data;
300         for (child = list->children; child != NULL; child = g_list_next(child)) {
301                 if (child->data == sel)
302                         return row;
303                 row ++;
304         }
305         
306         return row;
307 }
308
309 /*!
310  *\brief        Opens the matcher dialog with a list of conditions
311  *
312  *\param        matchers List of conditions
313  *\param        cb Callback
314  *
315  */
316 void prefs_matcher_open(MatcherList *matchers, PrefsMatcherSignal *cb)
317 {
318         inc_lock();
319
320         if (!matcher.window) {
321                 prefs_matcher_create();
322         }
323
324         manage_window_set_transient(GTK_WINDOW(matcher.window));
325         gtk_widget_grab_focus(matcher.ok_btn);
326
327         matchers_callback = cb;
328
329         prefs_matcher_set_dialog(matchers);
330
331         gtk_widget_show(matcher.window);
332 }
333
334 /*!
335  *\brief        Save Gtk object size to prefs dataset
336  */
337 static void prefs_matcher_size_allocate_cb(GtkWidget *widget,
338                                          GtkAllocation *allocation)
339 {
340         g_return_if_fail(allocation != NULL);
341
342         prefs_common.matcherwin_width = allocation->width;
343         prefs_common.matcherwin_height = allocation->height;
344 }
345
346 /*!
347  *\brief        Create the matcher dialog
348  */
349 static void prefs_matcher_create(void)
350 {
351         GtkWidget *window;
352         GtkWidget *vbox;
353         GtkWidget *ok_btn;
354         GtkWidget *cancel_btn;
355         GtkWidget *confirm_area;
356
357         GtkWidget *vbox1;
358         GtkWidget *vbox2;
359         GtkWidget *vbox3;
360         GtkWidget *criteria_table;
361
362         GtkWidget *hbox1;
363
364         GtkWidget *header_combo;
365         GtkWidget *header_entry;
366         GtkWidget *header_label;
367         GtkWidget *header_addr_combo;
368         GtkWidget *header_addr_entry;
369         GtkWidget *header_addr_label;
370         GtkWidget *criteria_combo;
371         GtkWidget *criteria_list;
372         GtkWidget *criteria_label;
373         GtkWidget *value_label;
374         GtkWidget *value_entry;
375         GtkWidget *addressbook_folder_label;
376         GtkWidget *addressbook_folder_combo;
377         GtkWidget *predicate_combo;
378         GtkWidget *predicate_list;
379         GtkWidget *predicate_flag_combo;
380         GtkWidget *predicate_flag_list;
381         GtkWidget *predicate_label;
382         GtkWidget *bool_op_combo;
383         GtkWidget *bool_op_list;
384         GtkWidget *bool_op_label;
385
386         GtkWidget *regexp_chkbtn;
387         GtkWidget *case_chkbtn;
388
389         GtkWidget *reg_hbox;
390         GtkWidget *btn_hbox;
391         GtkWidget *arrow;
392         GtkWidget *reg_btn;
393         GtkWidget *subst_btn;
394         GtkWidget *del_btn;
395
396         GtkWidget *cond_hbox;
397         GtkWidget *cond_scrolledwin;
398         GtkWidget *cond_list_view;
399
400         GtkWidget *btn_vbox;
401         GtkWidget *up_btn;
402         GtkWidget *down_btn;
403
404         GtkWidget *test_btn;
405         GtkWidget *addressbook_select_btn;
406
407         GtkWidget *color_optmenu;
408
409         GList *combo_items;
410         gint i;
411         static GdkGeometry geometry;
412
413         debug_print("Creating matcher configuration window...\n");
414
415         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
416         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
417         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
418         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
419         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
420
421         vbox = gtk_vbox_new(FALSE, 6);
422         gtk_widget_show(vbox);
423         gtk_container_add(GTK_CONTAINER(window), vbox);
424
425         gtkut_stock_button_set_create(&confirm_area, &cancel_btn, GTK_STOCK_CANCEL,
426                                       &ok_btn, GTK_STOCK_OK, NULL, NULL);
427         gtk_widget_show(confirm_area);
428         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
429         gtk_widget_grab_default(ok_btn);
430
431         gtk_window_set_title(GTK_WINDOW(window),
432                              _("Condition configuration"));
433         g_signal_connect(G_OBJECT(window), "delete_event",
434                          G_CALLBACK(prefs_matcher_deleted), NULL);
435         g_signal_connect(G_OBJECT(window), "size_allocate",
436                          G_CALLBACK(prefs_matcher_size_allocate_cb), NULL);
437         g_signal_connect(G_OBJECT(window), "key_press_event",
438                          G_CALLBACK(prefs_matcher_key_pressed), NULL);
439         MANAGE_WINDOW_SIGNALS_CONNECT(window);
440         g_signal_connect(G_OBJECT(ok_btn), "clicked",
441                          G_CALLBACK(prefs_matcher_ok), NULL);
442         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
443                          G_CALLBACK(prefs_matcher_cancel), NULL);
444
445         vbox1 = gtk_vbox_new(FALSE, VSPACING);
446         gtk_widget_show(vbox1);
447         gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
448         gtk_container_set_border_width(GTK_CONTAINER (vbox1), 2);
449
450         criteria_table = gtk_table_new(2, 4, FALSE);
451         gtk_widget_show(criteria_table);
452
453         gtk_box_pack_start(GTK_BOX(vbox1), criteria_table, FALSE, FALSE, 0);
454         gtk_table_set_row_spacings(GTK_TABLE(criteria_table), 8);
455         gtk_table_set_col_spacings(GTK_TABLE(criteria_table), 8);
456
457         /* criteria combo box */
458
459         criteria_label = gtk_label_new(_("Match type"));
460         gtk_widget_show(criteria_label);
461         gtk_misc_set_alignment(GTK_MISC(criteria_label), 0, 0.5);
462         gtk_table_attach(GTK_TABLE(criteria_table), criteria_label, 0, 1, 0, 1,
463                          GTK_FILL, 0, 0, 0);
464
465         criteria_combo = gtk_combo_new();
466         gtk_widget_show(criteria_combo);
467
468         combo_items = NULL;
469
470         for (i = 0; i < (gint) (sizeof(criteria_text) / sizeof(gchar *)); i++) {
471                 combo_items = g_list_append(combo_items,
472                                             (gpointer) _(criteria_text[i]));
473         }
474         gtk_combo_set_popdown_strings(GTK_COMBO(criteria_combo), combo_items);
475
476         g_list_free(combo_items);
477
478         gtk_widget_set_size_request(criteria_combo, 170, -1);
479         gtk_table_attach(GTK_TABLE(criteria_table), criteria_combo, 0, 1, 1, 2,
480                           0, 0, 0, 0);
481         criteria_list = GTK_COMBO(criteria_combo)->list;
482         g_signal_connect(G_OBJECT(criteria_list), "select-child",
483                          G_CALLBACK(prefs_matcher_criteria_select),
484                          NULL);
485
486         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(criteria_combo)->entry),
487                                FALSE);
488
489         /* header name */
490
491         header_label = gtk_label_new(_("Header name"));
492         gtk_widget_show(header_label);
493         gtk_misc_set_alignment(GTK_MISC(header_label), 0, 0.5);
494         gtk_table_attach(GTK_TABLE(criteria_table), header_label, 1, 2, 0, 1,
495                          GTK_FILL, 0, 0, 0);
496
497         header_combo = gtk_combo_new();
498         gtk_widget_show(header_combo);
499         gtk_widget_set_size_request(header_combo, 120, -1);
500         gtkut_combo_set_items(GTK_COMBO (header_combo),
501                               "Subject", "From", "To", "Cc", "Reply-To",
502                               "Sender", "X-ML-Name", "X-List", "X-Sequence",
503                               "X-Mailer","X-BeenThere",
504                               NULL);
505         gtk_table_attach(GTK_TABLE(criteria_table), header_combo, 1, 2, 1, 2,
506                          0, 0, 0, 0);
507         header_entry = GTK_COMBO(header_combo)->entry;
508         gtk_entry_set_editable(GTK_ENTRY(header_entry), TRUE);
509
510         /* address header name */
511
512         header_addr_label = gtk_label_new(_("Address header"));
513         gtk_misc_set_alignment(GTK_MISC(header_addr_label), 0, 0.5);
514         gtk_table_attach(GTK_TABLE(criteria_table), header_addr_label, 1, 2, 0, 1,
515                          GTK_FILL, 0, 0, 0);
516
517         header_addr_combo = gtk_combo_new();
518         gtk_widget_set_size_request(header_addr_combo, 120, -1);
519         gtkut_combo_set_items(GTK_COMBO (header_addr_combo),
520                               _("All"), _("Any"), "From", "To", "Cc", "Reply-To", "Sender",
521                               NULL);
522         gtk_table_attach(GTK_TABLE(criteria_table), header_addr_combo, 1, 2, 1, 2,
523                          0, 0, 0, 0);
524         header_addr_entry = GTK_COMBO(header_addr_combo)->entry;
525         gtk_entry_set_editable(GTK_ENTRY(header_addr_entry), TRUE);
526
527         /* value */
528
529         value_label = gtk_label_new(_("Value"));
530         gtk_widget_show(value_label);
531         gtk_misc_set_alignment(GTK_MISC (value_label), 0, 0.5);
532         gtk_table_attach(GTK_TABLE(criteria_table), value_label, 2, 3, 0, 1,
533                          GTK_FILL | GTK_SHRINK | GTK_EXPAND, 0, 0, 0);
534
535         value_entry = gtk_entry_new();
536         gtk_widget_show(value_entry);
537         gtk_widget_set_size_request(value_entry, 200, -1);
538         gtk_table_attach(GTK_TABLE(criteria_table), value_entry, 2, 3, 1, 2,
539                          GTK_FILL | GTK_SHRINK | GTK_EXPAND, 0, 0, 0);
540
541         /* book/folder value */
542
543         addressbook_folder_label = gtk_label_new(_("Book/folder"));
544         gtk_misc_set_alignment(GTK_MISC (addressbook_folder_label), 0, 0.5);
545         gtk_table_attach(GTK_TABLE(criteria_table), addressbook_folder_label, 2, 3, 0, 1,
546                          GTK_FILL | GTK_SHRINK | GTK_EXPAND, 0, 0, 0);
547
548         addressbook_folder_combo = gtk_combo_new();
549         gtk_widget_show(addressbook_folder_combo);
550         gtk_widget_set_size_request(addressbook_folder_combo, 200, -1);
551         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(addressbook_folder_combo)->entry),
552                                TRUE);
553
554         combo_items = NULL;
555         for (i = 0; i < (gint) (sizeof(addressbook_folder_text) / sizeof(gchar *)); i++) {
556                 combo_items = g_list_append(combo_items,
557                                             (gpointer) _(addressbook_folder_text[i]));
558         }
559         gtk_combo_set_popdown_strings(GTK_COMBO(addressbook_folder_combo), combo_items);
560         g_list_free(combo_items);
561
562         gtk_table_attach(GTK_TABLE(criteria_table), addressbook_folder_combo, 2, 3, 1, 2,
563                          GTK_FILL | GTK_SHRINK | GTK_EXPAND, 0, 0, 0);
564
565 #if GTK_CHECK_VERSION(2, 8, 0)
566         test_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
567 #else
568         test_btn = gtk_button_new_with_label(_(" Info... "));
569 #endif
570         gtk_widget_show(test_btn);
571         gtk_table_attach(GTK_TABLE (criteria_table), test_btn, 3, 4, 1, 2,
572                          0, 0, 0, 0);
573         g_signal_connect(G_OBJECT (test_btn), "clicked",
574                          G_CALLBACK(prefs_matcher_test_info),
575                          NULL);
576
577         addressbook_select_btn = gtk_button_new_with_label(_(" Select... "));
578         gtk_table_attach(GTK_TABLE (criteria_table), addressbook_select_btn, 3, 4, 1, 2,
579                          0, 0, 0, 0);
580         g_signal_connect(G_OBJECT (addressbook_select_btn), "clicked",
581                          G_CALLBACK(prefs_matcher_addressbook_select),
582                          NULL);
583
584         color_optmenu = gtk_option_menu_new();
585         gtk_option_menu_set_menu(GTK_OPTION_MENU(color_optmenu),
586                                  colorlabel_create_color_menu());
587
588         /* predicate */
589
590         vbox2 = gtk_vbox_new(FALSE, VSPACING);
591         gtk_widget_show(vbox2);
592         gtk_box_pack_start(GTK_BOX(vbox1), vbox2, FALSE, FALSE, 0);
593
594         hbox1 = gtk_hbox_new(FALSE, 8);
595         gtk_widget_show(hbox1);
596         gtk_box_pack_start(GTK_BOX(vbox2), hbox1, FALSE, FALSE, 0);
597
598         predicate_label = gtk_label_new(_("Predicate"));
599         gtk_widget_show(predicate_label);
600         gtk_box_pack_start(GTK_BOX(hbox1), predicate_label,
601                            FALSE, FALSE, 0);
602
603         predicate_combo = gtk_combo_new();
604         gtk_widget_show(predicate_combo);
605         gtk_widget_set_size_request(predicate_combo, 120, -1);
606         predicate_list = GTK_COMBO(predicate_combo)->list;
607         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(predicate_combo)->entry),
608                                FALSE);
609
610         combo_items = NULL;
611
612         for (i = 0; i < (gint) (sizeof(predicate_text) / sizeof(gchar *)); i++) {
613                 combo_items = g_list_append(combo_items,
614                                             (gpointer) _(predicate_text[i]));
615         }
616         gtk_combo_set_popdown_strings(GTK_COMBO(predicate_combo), combo_items);
617
618         g_list_free(combo_items);
619
620         gtk_box_pack_start(GTK_BOX(hbox1), predicate_combo,
621                            FALSE, FALSE, 0);
622
623         /* predicate flag */
624
625         predicate_flag_combo = gtk_combo_new();
626         gtk_widget_hide(predicate_flag_combo);
627         gtk_widget_set_size_request(predicate_flag_combo, 120, -1);
628         predicate_flag_list = GTK_COMBO(predicate_flag_combo)->list;
629         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(predicate_flag_combo)->entry), FALSE);
630
631         combo_items = NULL;
632
633         for (i = 0; i < (gint) (sizeof(predicate_text) / sizeof(gchar *)); i++) {
634                 combo_items = g_list_append(combo_items, (gpointer) _(predicate_flag_text[i]));
635         }
636         gtk_combo_set_popdown_strings(GTK_COMBO(predicate_flag_combo),
637                                       combo_items);
638
639         g_list_free(combo_items);
640
641         gtk_box_pack_start(GTK_BOX(hbox1), predicate_flag_combo,
642                            FALSE, FALSE, 0);
643
644         vbox3 = gtk_vbox_new(FALSE, 0);
645         gtk_widget_show (vbox3);
646         gtk_box_pack_start(GTK_BOX(hbox1), vbox3, FALSE, FALSE, 0);
647
648         PACK_CHECK_BUTTON(vbox3, case_chkbtn, _("Case sensitive"));
649         PACK_CHECK_BUTTON(vbox3, regexp_chkbtn, _("Use regexp"));
650
651         /* register / substitute / delete */
652
653         reg_hbox = gtk_hbox_new(FALSE, 4);
654         gtk_widget_show(reg_hbox);
655         gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
656
657         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
658         gtk_widget_show(arrow);
659         gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
660         gtk_widget_set_size_request(arrow, -1, 16);
661
662         btn_hbox = gtk_hbox_new(TRUE, 4);
663         gtk_widget_show(btn_hbox);
664         gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
665
666         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
667         gtk_widget_show(reg_btn);
668         gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
669         g_signal_connect(G_OBJECT(reg_btn), "clicked",
670                          G_CALLBACK(prefs_matcher_register_cb), NULL);
671
672         subst_btn = gtkut_get_replace_btn(_("Replace"));
673         gtk_widget_show(subst_btn);
674         gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
675         g_signal_connect(G_OBJECT(subst_btn), "clicked",
676                          G_CALLBACK(prefs_matcher_substitute_cb),
677                          NULL);
678
679         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
680         gtk_widget_show(del_btn);
681         gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
682         g_signal_connect(G_OBJECT(del_btn), "clicked",
683                          G_CALLBACK(prefs_matcher_delete_cb), NULL);
684
685         /* boolean operation */
686
687         bool_op_label = gtk_label_new(_("Boolean Op"));
688         gtk_misc_set_alignment(GTK_MISC(value_label), 0, 0.5);
689         gtk_widget_show(bool_op_label);
690         gtk_box_pack_start(GTK_BOX(btn_hbox), bool_op_label,
691                            FALSE, FALSE, 0);
692
693         bool_op_combo = gtk_combo_new();
694         gtk_widget_show(bool_op_combo);
695         gtk_widget_set_size_request(bool_op_combo, 60, -1);
696         bool_op_list = GTK_COMBO(bool_op_combo)->list;
697         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(bool_op_combo)->entry),
698                                FALSE);
699
700         combo_items = NULL;
701
702         for (i = 0; i < (gint) (sizeof(bool_op_text) / sizeof(gchar *)); i++) {
703                 combo_items = g_list_append(combo_items,
704                                             (gpointer) _(bool_op_text[i]));
705         }
706         gtk_combo_set_popdown_strings(GTK_COMBO(bool_op_combo), combo_items);
707
708         g_list_free(combo_items);
709
710         gtk_box_pack_start(GTK_BOX(btn_hbox), bool_op_combo,
711                            FALSE, FALSE, 0);
712
713         cond_hbox = gtk_hbox_new(FALSE, 8);
714         gtk_widget_show(cond_hbox);
715         gtk_box_pack_start(GTK_BOX(vbox1), cond_hbox, TRUE, TRUE, 0);
716
717         cond_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
718         gtk_widget_show(cond_scrolledwin);
719         gtk_widget_set_size_request(cond_scrolledwin, -1, 150);
720         gtk_box_pack_start(GTK_BOX(cond_hbox), cond_scrolledwin,
721                            TRUE, TRUE, 0);
722         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(cond_scrolledwin),
723                                        GTK_POLICY_AUTOMATIC,
724                                        GTK_POLICY_AUTOMATIC);
725
726         cond_list_view = prefs_matcher_list_view_create();                                     
727         gtk_widget_show(cond_list_view);
728         gtk_container_add(GTK_CONTAINER(cond_scrolledwin), cond_list_view);
729
730         btn_vbox = gtk_vbox_new(FALSE, 8);
731         gtk_widget_show(btn_vbox);
732         gtk_box_pack_start(GTK_BOX(cond_hbox), btn_vbox, FALSE, FALSE, 0);
733
734         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
735         gtk_widget_show(up_btn);
736         gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0);
737         g_signal_connect(G_OBJECT(up_btn), "clicked",
738                          G_CALLBACK(prefs_matcher_up), NULL);
739
740         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
741         gtk_widget_show(down_btn);
742         gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
743         g_signal_connect(G_OBJECT(down_btn), "clicked",
744                          G_CALLBACK(prefs_matcher_down), NULL);
745
746         if (!geometry.min_height) {
747                 geometry.min_width = 520;
748                 geometry.min_height = 368;
749         }
750
751         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
752                                       GDK_HINT_MIN_SIZE);
753         gtk_widget_set_size_request(window, prefs_common.matcherwin_width,
754                                     prefs_common.matcherwin_height);
755
756         gtk_widget_show_all(window);
757         gtk_widget_hide(header_addr_label);
758         gtk_widget_hide(header_addr_combo);
759         gtk_widget_hide(addressbook_select_btn);
760         gtk_widget_hide(addressbook_folder_label);
761
762         matcher.window    = window;
763
764         matcher.ok_btn = ok_btn;
765
766         matcher.criteria_list = criteria_list;
767         matcher.header_combo = header_combo;
768         matcher.header_entry = header_entry;
769         matcher.header_label = header_label;
770         matcher.header_addr_combo = header_addr_combo;
771         matcher.header_addr_entry = header_addr_entry;
772         matcher.header_addr_label = header_addr_label;
773         matcher.value_entry = value_entry;
774         matcher.value_label = value_label;
775         matcher.addressbook_folder_label = addressbook_folder_label;
776         matcher.addressbook_folder_combo = addressbook_folder_combo;
777         matcher.predicate_label = predicate_label;
778         matcher.predicate_list = predicate_list;
779         matcher.predicate_combo = predicate_combo;
780         matcher.predicate_flag_list = predicate_flag_list;
781         matcher.predicate_flag_combo = predicate_flag_combo;
782         matcher.case_chkbtn = case_chkbtn;
783         matcher.regexp_chkbtn = regexp_chkbtn;
784         matcher.bool_op_list = bool_op_list;
785         matcher.test_btn = test_btn;
786         matcher.addressbook_select_btn = addressbook_select_btn;
787         matcher.color_optmenu = color_optmenu;
788         matcher.criteria_table = criteria_table;
789
790         matcher.cond_list_view = cond_list_view;
791
792         matcher.selected_criteria = -1;
793         prefs_matcher_criteria_select(GTK_LIST(criteria_list), NULL, NULL);
794 }
795
796 /*!
797  *\brief        Set the contents of a row
798  *
799  *\param        row Index of row to set
800  *\param        prop Condition to set
801  *
802  *\return       gint Row index \a prop has been added
803  */
804 static void prefs_matcher_list_view_set_row(GtkTreeIter *row, MatcherProp *prop)
805 {
806         gchar *matcher_str;
807
808         if (prop == NULL) {
809                 prefs_matcher_list_view_insert_matcher(matcher.cond_list_view,
810                                                        NULL, _("New"), FALSE);
811                 return;                                                
812         }
813
814         matcher_str = matcherprop_to_string(prop);
815         if (!row)
816                 prefs_matcher_list_view_insert_matcher(matcher.cond_list_view,
817                                                        NULL, matcher_str,
818                                                        TRUE);
819         else
820                 prefs_matcher_list_view_insert_matcher(matcher.cond_list_view,
821                                                        row, matcher_str, 
822                                                        TRUE);
823         g_free(matcher_str);
824 }
825
826 /*!
827  *\brief        Clears a condition in the list widget
828  */
829 static void prefs_matcher_reset_condition(void)
830 {
831         gtk_list_select_item(GTK_LIST(matcher.criteria_list), 0);
832         gtk_list_select_item(GTK_LIST(matcher.predicate_list), 0);
833         gtk_entry_set_text(GTK_ENTRY(matcher.header_entry), "");
834         gtk_entry_set_text(GTK_ENTRY(matcher.header_addr_entry), "");
835         gtk_entry_set_text(GTK_ENTRY(matcher.value_entry), "");
836         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry), "");
837 }
838
839 /*!
840  *\brief        Initializes dialog with a set of conditions
841  *
842  *\param        matchers List of conditions
843  */
844 static void prefs_matcher_set_dialog(MatcherList *matchers)
845 {
846         GSList *cur;
847         gboolean bool_op = 1;
848         GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model
849                                 (GTK_TREE_VIEW(matcher.cond_list_view)));
850
851         gtk_list_store_clear(store);                            
852
853         prefs_matcher_list_view_set_row(NULL, NULL);
854         if (matchers != NULL) {
855                 for (cur = matchers->matchers; cur != NULL;
856                      cur = g_slist_next(cur)) {
857                         MatcherProp *prop;
858                         prop = (MatcherProp *) cur->data;
859                         prefs_matcher_list_view_set_row(NULL, prop);
860                 }
861
862                 bool_op = matchers->bool_and;
863         }
864         
865         gtk_list_select_item(GTK_LIST(matcher.bool_op_list), bool_op);
866
867         prefs_matcher_reset_condition();
868 }
869
870 /*!
871  *\brief        Converts current conditions in list box in
872  *              a matcher list used by the matcher.
873  *
874  *\return       MatcherList * List of conditions.
875  */
876 static MatcherList *prefs_matcher_get_list(void)
877 {
878         gchar *matcher_str;
879         MatcherProp *prop;
880         gboolean bool_and;
881         GSList *matcher_list;
882         MatcherList *matchers;
883         GtkTreeModel *model;
884         GtkTreeIter iter;
885
886         model = gtk_tree_view_get_model(GTK_TREE_VIEW(matcher.cond_list_view));
887         if (!gtk_tree_model_get_iter_first(model, &iter))
888                 return NULL;
889
890         matcher_list = NULL;
891
892         do {
893                 gboolean is_valid;
894         
895                 gtk_tree_model_get(model, &iter,
896                                    PREFS_MATCHER_COND, &matcher_str,
897                                    PREFS_MATCHER_COND_VALID, &is_valid,
898                                    -1);
899                 
900                 if (is_valid) {
901                         /* tmp = matcher_str; */
902                         prop = matcher_parser_get_prop(matcher_str);
903                         g_free(matcher_str);
904                         if (prop == NULL)
905                                 break;
906                         
907                         matcher_list = g_slist_append(matcher_list, prop);
908                 }
909         } while (gtk_tree_model_iter_next(model, &iter));
910
911         bool_and = get_sel_from_list(GTK_LIST(matcher.bool_op_list));
912
913         matchers = matcherlist_new(matcher_list, bool_and);
914
915         return matchers;
916 }
917
918 /*!
919  *\brief        Maps a keyword id (see #get_matchparser_tab_id) to a 
920  *              criteria type (see first parameter of #matcherprop_new
921  *              or #matcherprop_unquote_new)
922  *
923  *\param        matching_id Id returned by the matcher parser.
924  *
925  *\return       gint One of the CRITERIA_xxx constants.
926  */
927 static gint prefs_matcher_get_criteria_from_matching(gint matching_id)
928 {
929         switch(matching_id) {
930         case MATCHCRITERIA_ALL:
931                 return CRITERIA_ALL;
932         case MATCHCRITERIA_NOT_UNREAD:
933         case MATCHCRITERIA_UNREAD:
934                 return CRITERIA_UNREAD;
935         case MATCHCRITERIA_NOT_NEW:
936         case MATCHCRITERIA_NEW:
937                 return CRITERIA_NEW;
938         case MATCHCRITERIA_NOT_MARKED:
939         case MATCHCRITERIA_MARKED:
940                 return CRITERIA_MARKED;
941         case MATCHCRITERIA_NOT_DELETED:
942         case MATCHCRITERIA_DELETED:
943                 return CRITERIA_DELETED;
944         case MATCHCRITERIA_NOT_REPLIED:
945         case MATCHCRITERIA_REPLIED:
946                 return CRITERIA_REPLIED;
947         case MATCHCRITERIA_NOT_FORWARDED:
948         case MATCHCRITERIA_FORWARDED:
949                 return CRITERIA_FORWARDED;
950         case MATCHCRITERIA_LOCKED:
951         case MATCHCRITERIA_NOT_LOCKED:
952                 return CRITERIA_LOCKED;
953         case MATCHCRITERIA_PARTIAL:
954         case MATCHCRITERIA_NOT_PARTIAL:
955                 return CRITERIA_PARTIAL;
956         case MATCHCRITERIA_COLORLABEL:
957         case MATCHCRITERIA_NOT_COLORLABEL:
958                 return CRITERIA_COLORLABEL;
959         case MATCHCRITERIA_IGNORE_THREAD:
960         case MATCHCRITERIA_NOT_IGNORE_THREAD:
961                 return CRITERIA_IGNORE_THREAD;
962         case MATCHCRITERIA_NOT_SUBJECT:
963         case MATCHCRITERIA_SUBJECT:
964                 return CRITERIA_SUBJECT;
965         case MATCHCRITERIA_NOT_FROM:
966         case MATCHCRITERIA_FROM:
967                 return CRITERIA_FROM;
968         case MATCHCRITERIA_NOT_TO:
969         case MATCHCRITERIA_TO:
970                 return CRITERIA_TO;
971         case MATCHCRITERIA_NOT_CC:
972         case MATCHCRITERIA_CC:
973                 return CRITERIA_CC;
974         case MATCHCRITERIA_NOT_NEWSGROUPS:
975         case MATCHCRITERIA_NEWSGROUPS:
976                 return CRITERIA_NEWSGROUPS;
977         case MATCHCRITERIA_NOT_INREPLYTO:
978         case MATCHCRITERIA_INREPLYTO:
979                 return CRITERIA_INREPLYTO;
980         case MATCHCRITERIA_NOT_REFERENCES:
981         case MATCHCRITERIA_REFERENCES:
982                 return CRITERIA_REFERENCES;
983         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
984         case MATCHCRITERIA_TO_OR_CC:
985                 return CRITERIA_TO_OR_CC;
986         case MATCHCRITERIA_NOT_BODY_PART:
987         case MATCHCRITERIA_BODY_PART:
988                 return CRITERIA_BODY_PART;
989         case MATCHCRITERIA_NOT_MESSAGE:
990         case MATCHCRITERIA_MESSAGE:
991                 return CRITERIA_MESSAGE;
992         case MATCHCRITERIA_NOT_HEADERS_PART:
993         case MATCHCRITERIA_HEADERS_PART:
994                 return CRITERIA_HEADERS_PART;
995         case MATCHCRITERIA_NOT_HEADER:
996         case MATCHCRITERIA_HEADER:
997                 return CRITERIA_HEADER;
998         case MATCHCRITERIA_AGE_GREATER:
999                 return CRITERIA_AGE_GREATER;
1000         case MATCHCRITERIA_AGE_LOWER:
1001                 return CRITERIA_AGE_LOWER;
1002         case MATCHCRITERIA_SCORE_GREATER:
1003                 return CRITERIA_SCORE_GREATER;
1004         case MATCHCRITERIA_SCORE_LOWER:
1005                 return CRITERIA_SCORE_LOWER;
1006         case MATCHCRITERIA_SCORE_EQUAL:
1007                 return CRITERIA_SCORE_EQUAL;
1008         case MATCHCRITERIA_NOT_TEST:
1009         case MATCHCRITERIA_TEST:
1010                 return CRITERIA_TEST;
1011         case MATCHCRITERIA_SIZE_GREATER:
1012                 return CRITERIA_SIZE_GREATER;
1013         case MATCHCRITERIA_SIZE_SMALLER:
1014                 return CRITERIA_SIZE_SMALLER;
1015         case MATCHCRITERIA_SIZE_EQUAL:
1016                 return CRITERIA_SIZE_EQUAL;
1017         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1018         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1019                 return CRITERIA_FOUND_IN_ADDRESSBOOK;
1020         default:
1021                 return -1;
1022         }
1023 }
1024
1025 /*!
1026  *\brief        Returns the matcher keyword id from a criteria id
1027  *
1028  *\param        criteria_id Criteria id (should not be the negate
1029  *              one)
1030  *
1031  *\return       gint A matcher keyword id. See #get_matchparser_tab_id.
1032  */
1033 static gint prefs_matcher_get_matching_from_criteria(gint criteria_id)
1034 {
1035         switch (criteria_id) {
1036         case CRITERIA_ALL:
1037                 return MATCHCRITERIA_ALL;
1038         case CRITERIA_UNREAD:
1039                 return MATCHCRITERIA_UNREAD;
1040         case CRITERIA_NEW:
1041                 return MATCHCRITERIA_NEW;
1042         case CRITERIA_MARKED:
1043                 return MATCHCRITERIA_MARKED;
1044         case CRITERIA_DELETED:
1045                 return MATCHCRITERIA_DELETED;
1046         case CRITERIA_REPLIED:
1047                 return MATCHCRITERIA_REPLIED;
1048         case CRITERIA_FORWARDED:
1049                 return MATCHCRITERIA_FORWARDED;
1050         case CRITERIA_LOCKED:
1051                 return MATCHCRITERIA_LOCKED;
1052         case CRITERIA_PARTIAL:
1053                 return MATCHCRITERIA_PARTIAL;
1054         case CRITERIA_COLORLABEL:
1055                 return MATCHCRITERIA_COLORLABEL;
1056         case CRITERIA_IGNORE_THREAD:
1057                 return MATCHCRITERIA_IGNORE_THREAD;
1058         case CRITERIA_SUBJECT:
1059                 return MATCHCRITERIA_SUBJECT;
1060         case CRITERIA_FROM:
1061                 return MATCHCRITERIA_FROM;
1062         case CRITERIA_TO:
1063                 return MATCHCRITERIA_TO;
1064         case CRITERIA_CC:
1065                 return MATCHCRITERIA_CC;
1066         case CRITERIA_TO_OR_CC:
1067                 return MATCHCRITERIA_TO_OR_CC;
1068         case CRITERIA_NEWSGROUPS:
1069                 return MATCHCRITERIA_NEWSGROUPS;
1070         case CRITERIA_INREPLYTO:
1071                 return MATCHCRITERIA_INREPLYTO;
1072         case CRITERIA_REFERENCES:
1073                 return MATCHCRITERIA_REFERENCES;
1074         case CRITERIA_AGE_GREATER:
1075                 return MATCHCRITERIA_AGE_GREATER;
1076         case CRITERIA_AGE_LOWER:
1077                 return MATCHCRITERIA_AGE_LOWER;
1078         case CRITERIA_SCORE_GREATER:
1079                 return MATCHCRITERIA_SCORE_GREATER;
1080         case CRITERIA_SCORE_LOWER:
1081                 return MATCHCRITERIA_SCORE_LOWER;
1082         case CRITERIA_SCORE_EQUAL:
1083                 return MATCHCRITERIA_SCORE_EQUAL;
1084         case CRITERIA_HEADER:
1085                 return MATCHCRITERIA_HEADER;
1086         case CRITERIA_HEADERS_PART:
1087                 return MATCHCRITERIA_HEADERS_PART;
1088         case CRITERIA_BODY_PART:
1089                 return MATCHCRITERIA_BODY_PART;
1090         case CRITERIA_MESSAGE:
1091                 return MATCHCRITERIA_MESSAGE;
1092         case CRITERIA_TEST:
1093                 return MATCHCRITERIA_TEST;
1094         case CRITERIA_SIZE_GREATER:
1095                 return MATCHCRITERIA_SIZE_GREATER;
1096         case CRITERIA_SIZE_SMALLER:
1097                 return MATCHCRITERIA_SIZE_SMALLER;
1098         case CRITERIA_SIZE_EQUAL:
1099                 return MATCHCRITERIA_SIZE_EQUAL;
1100         case CRITERIA_FOUND_IN_ADDRESSBOOK:
1101                 return MATCHCRITERIA_FOUND_IN_ADDRESSBOOK;
1102         default:
1103                 return -1;
1104         }
1105 }
1106
1107 /*!
1108  *\brief        Returns the negate matcher keyword id from a matcher keyword
1109  *              id.
1110  *
1111  *\param        matcher_criteria Matcher keyword id. 
1112  *
1113  *\return       gint A matcher keyword id. See #get_matchparser_tab_id.
1114  */
1115 static gint prefs_matcher_not_criteria(gint matcher_criteria)
1116 {
1117         switch(matcher_criteria) {
1118         case MATCHCRITERIA_UNREAD:
1119                 return MATCHCRITERIA_NOT_UNREAD;
1120         case MATCHCRITERIA_NEW:
1121                 return MATCHCRITERIA_NOT_NEW;
1122         case MATCHCRITERIA_MARKED:
1123                 return MATCHCRITERIA_NOT_MARKED;
1124         case MATCHCRITERIA_DELETED:
1125                 return MATCHCRITERIA_NOT_DELETED;
1126         case MATCHCRITERIA_REPLIED:
1127                 return MATCHCRITERIA_NOT_REPLIED;
1128         case MATCHCRITERIA_FORWARDED:
1129                 return MATCHCRITERIA_NOT_FORWARDED;
1130         case MATCHCRITERIA_LOCKED:
1131                 return MATCHCRITERIA_NOT_LOCKED;
1132         case MATCHCRITERIA_PARTIAL:
1133                 return MATCHCRITERIA_NOT_PARTIAL;
1134         case MATCHCRITERIA_COLORLABEL:
1135                 return MATCHCRITERIA_NOT_COLORLABEL;
1136         case MATCHCRITERIA_IGNORE_THREAD:
1137                 return MATCHCRITERIA_NOT_IGNORE_THREAD;
1138         case MATCHCRITERIA_SUBJECT:
1139                 return MATCHCRITERIA_NOT_SUBJECT;
1140         case MATCHCRITERIA_FROM:
1141                 return MATCHCRITERIA_NOT_FROM;
1142         case MATCHCRITERIA_TO:
1143                 return MATCHCRITERIA_NOT_TO;
1144         case MATCHCRITERIA_CC:
1145                 return MATCHCRITERIA_NOT_CC;
1146         case MATCHCRITERIA_TO_OR_CC:
1147                 return MATCHCRITERIA_NOT_TO_AND_NOT_CC;
1148         case MATCHCRITERIA_NEWSGROUPS:
1149                 return MATCHCRITERIA_NOT_NEWSGROUPS;
1150         case MATCHCRITERIA_INREPLYTO:
1151                 return MATCHCRITERIA_NOT_INREPLYTO;
1152         case MATCHCRITERIA_REFERENCES:
1153                 return MATCHCRITERIA_NOT_REFERENCES;
1154         case MATCHCRITERIA_HEADER:
1155                 return MATCHCRITERIA_NOT_HEADER;
1156         case MATCHCRITERIA_HEADERS_PART:
1157                 return MATCHCRITERIA_NOT_HEADERS_PART;
1158         case MATCHCRITERIA_MESSAGE:
1159                 return MATCHCRITERIA_NOT_MESSAGE;
1160         case MATCHCRITERIA_TEST:
1161                 return MATCHCRITERIA_NOT_TEST;
1162         case MATCHCRITERIA_BODY_PART:
1163                 return MATCHCRITERIA_NOT_BODY_PART;
1164         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1165                 return MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK;
1166         default:
1167                 return matcher_criteria;
1168         }
1169 }
1170
1171 /*!
1172  *\brief        Converts the text in the selected row to a 
1173  *              matcher structure
1174  *
1175  *\return       MatcherProp * Newly allocated matcher structure.
1176  */
1177 static MatcherProp *prefs_matcher_dialog_to_matcher(void)
1178 {
1179         MatcherProp *matcherprop;
1180         gint criteria;
1181         gint matchtype;
1182         gint value_pred;
1183         gint value_pred_flag;
1184         gint value_criteria;
1185         gboolean use_regexp;
1186         gboolean case_sensitive;
1187         const gchar *header;
1188         const gchar *expr;
1189         gint value;
1190         const gchar *value_str;
1191
1192         value_criteria = get_sel_from_list(GTK_LIST(matcher.criteria_list));
1193
1194         criteria = prefs_matcher_get_matching_from_criteria(value_criteria);
1195
1196         value_pred = get_sel_from_list(GTK_LIST(matcher.predicate_list));
1197         value_pred_flag = get_sel_from_list(GTK_LIST(matcher.predicate_flag_list));
1198
1199         use_regexp = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(matcher.regexp_chkbtn));
1200         case_sensitive = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(matcher.case_chkbtn));
1201
1202         switch (value_criteria) {
1203         case CRITERIA_UNREAD:
1204         case CRITERIA_NEW:
1205         case CRITERIA_MARKED:
1206         case CRITERIA_DELETED:
1207         case CRITERIA_REPLIED:
1208         case CRITERIA_FORWARDED:
1209         case CRITERIA_LOCKED:
1210         case CRITERIA_PARTIAL:
1211         case CRITERIA_TEST:
1212         case CRITERIA_COLORLABEL:
1213         case CRITERIA_IGNORE_THREAD:
1214         case CRITERIA_FOUND_IN_ADDRESSBOOK:
1215                 if (value_pred_flag == PREDICATE_FLAG_DISABLED)
1216                         criteria = prefs_matcher_not_criteria(criteria);
1217                 break;
1218         case CRITERIA_SUBJECT:
1219         case CRITERIA_FROM:
1220         case CRITERIA_TO:
1221         case CRITERIA_CC:
1222         case CRITERIA_TO_OR_CC:
1223         case CRITERIA_NEWSGROUPS:
1224         case CRITERIA_INREPLYTO:
1225         case CRITERIA_REFERENCES:
1226         case CRITERIA_HEADERS_PART:
1227         case CRITERIA_BODY_PART:
1228         case CRITERIA_MESSAGE:
1229         case CRITERIA_AGE_GREATER:
1230         case CRITERIA_AGE_LOWER:
1231         case CRITERIA_HEADER:
1232                 if (value_pred == PREDICATE_DOES_NOT_CONTAIN)
1233                         criteria = prefs_matcher_not_criteria(criteria);
1234                 break;
1235         }
1236
1237         if (use_regexp) {
1238                 if (case_sensitive)
1239                         matchtype = MATCHTYPE_REGEXP;
1240                 else
1241                         matchtype = MATCHTYPE_REGEXPCASE;
1242         }
1243         else {
1244                 if (case_sensitive)
1245                         matchtype = MATCHTYPE_MATCH;
1246                 else
1247                         matchtype = MATCHTYPE_MATCHCASE;
1248         }
1249
1250         header = NULL;
1251         expr = NULL;
1252         value = 0;
1253
1254         switch (value_criteria) {
1255         case CRITERIA_ALL:
1256         case CRITERIA_UNREAD:
1257         case CRITERIA_NEW:
1258         case CRITERIA_MARKED:
1259         case CRITERIA_DELETED:
1260         case CRITERIA_REPLIED:
1261         case CRITERIA_FORWARDED:
1262         case CRITERIA_LOCKED:
1263         case CRITERIA_PARTIAL:
1264         case CRITERIA_IGNORE_THREAD:
1265                 break;
1266
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_HEADERS_PART:
1276         case CRITERIA_BODY_PART:
1277         case CRITERIA_MESSAGE:
1278         case CRITERIA_TEST:
1279                 expr = gtk_entry_get_text(GTK_ENTRY(matcher.value_entry));
1280                 break;
1281
1282         case CRITERIA_AGE_GREATER:
1283         case CRITERIA_AGE_LOWER:
1284         case CRITERIA_SCORE_GREATER:
1285         case CRITERIA_SCORE_LOWER:
1286         case CRITERIA_SCORE_EQUAL:
1287         case CRITERIA_SIZE_GREATER:
1288         case CRITERIA_SIZE_SMALLER:
1289         case CRITERIA_SIZE_EQUAL:
1290                 value_str = gtk_entry_get_text(GTK_ENTRY(matcher.value_entry));
1291
1292                 if (*value_str == '\0') {
1293                     alertpanel_error(_("Value is not set."));
1294                     return NULL;
1295                 }
1296
1297                 value = atoi(value_str);
1298                 break;
1299                 
1300         case CRITERIA_COLORLABEL:
1301                 value = colorlabel_get_color_menu_active_item
1302                         (gtk_option_menu_get_menu(GTK_OPTION_MENU
1303                                 (matcher.color_optmenu))); 
1304                 break;
1305
1306         case CRITERIA_HEADER:
1307                 header = gtk_entry_get_text(GTK_ENTRY(matcher.header_entry));
1308                 expr = gtk_entry_get_text(GTK_ENTRY(matcher.value_entry));
1309
1310                 if (*header == '\0') {
1311                     alertpanel_error(_("Header name is not set."));
1312                     return NULL;
1313                 }
1314                 break;
1315
1316         case CRITERIA_FOUND_IN_ADDRESSBOOK:
1317                 header = gtk_entry_get_text(GTK_ENTRY(matcher.header_addr_entry));
1318                 expr = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry));
1319
1320                 if (*header == '\0') {
1321                     alertpanel_error(_("Header name is not set."));
1322                     return NULL;
1323                 }
1324                 if (*expr == '\0') {
1325                         gchar *msg;
1326
1327                         msg = g_strdup_printf(_("Book/folder path is not set.\n\n"
1328                                                                 "If you want to match the '%s' address against the whole address book, "
1329                                                                 "you have to select 'Any' from the book/folder drop-down list."),
1330                                                                 header);
1331                     alertpanel_error(msg);
1332                         g_free(msg);
1333                     return NULL;
1334                 }
1335                 break;
1336         }
1337
1338         matcherprop = matcherprop_new(criteria, header, matchtype,
1339                                       expr, value);
1340
1341         return matcherprop;
1342 }
1343
1344 /*!
1345  *\brief        Signal handler for register button
1346  */
1347 static void prefs_matcher_register_cb(void)
1348 {
1349         MatcherProp *matcherprop;
1350         
1351         matcherprop = prefs_matcher_dialog_to_matcher();
1352         if (matcherprop == NULL)
1353                 return;
1354
1355         prefs_matcher_list_view_set_row(NULL, matcherprop);
1356
1357         matcherprop_free(matcherprop);
1358
1359         prefs_matcher_reset_condition();
1360 }
1361
1362 /*!
1363  *\brief        Signal handler for substitute button
1364  */
1365 static void prefs_matcher_substitute_cb(void)
1366 {
1367         MatcherProp *matcherprop;
1368         GtkTreeIter row;
1369         GtkTreeSelection *selection;
1370         GtkTreeModel *model;
1371         gboolean is_valid;
1372
1373         selection = gtk_tree_view_get_selection
1374                         (GTK_TREE_VIEW(matcher.cond_list_view));
1375         
1376         if (!gtk_tree_selection_get_selected(selection, &model, &row))
1377                 return;
1378         
1379         gtk_tree_model_get(model, &row, 
1380                            PREFS_MATCHER_COND_VALID, &is_valid,
1381                            -1);
1382         if (!is_valid)
1383                 return;
1384
1385         matcherprop = prefs_matcher_dialog_to_matcher();
1386         if (matcherprop == NULL)
1387                 return;
1388
1389         prefs_matcher_list_view_set_row(&row, matcherprop);
1390
1391         matcherprop_free(matcherprop);
1392
1393         prefs_matcher_reset_condition();
1394 }
1395
1396 /*!
1397  *\brief        Signal handler for delete button
1398  */
1399 static void prefs_matcher_delete_cb(void)
1400 {
1401         GtkTreeIter row;
1402         GtkTreeSelection *selection;
1403         GtkTreeModel *model;
1404         gboolean is_valid;
1405
1406         selection = gtk_tree_view_get_selection
1407                         (GTK_TREE_VIEW(matcher.cond_list_view));
1408         
1409         if (!gtk_tree_selection_get_selected(selection, &model, &row))
1410                 return;
1411                 
1412         gtk_tree_model_get(model, &row, 
1413                            PREFS_MATCHER_COND_VALID, &is_valid,
1414                            -1);
1415
1416         if (!is_valid)
1417                 return;
1418
1419         gtk_list_store_remove(GTK_LIST_STORE(model), &row);             
1420
1421         prefs_matcher_reset_condition();
1422 }
1423
1424 /*!
1425  *\brief        Signal handler for 'move up' button
1426  */
1427 static void prefs_matcher_up(void)
1428 {
1429         GtkTreePath *prev, *sel, *try;
1430         GtkTreeIter isel;
1431         GtkListStore *store = NULL;
1432         GtkTreeModel *model = NULL;
1433         GtkTreeIter iprev;
1434         
1435         if (!gtk_tree_selection_get_selected
1436                 (gtk_tree_view_get_selection
1437                         (GTK_TREE_VIEW(matcher.cond_list_view)),
1438                  &model,        
1439                  &isel))
1440                 return;
1441         store = (GtkListStore *)model;
1442         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
1443         if (!sel)
1444                 return;
1445         
1446         /* no move if we're at row 0 or 1, looks phony, but other
1447          * solutions are more convoluted... */
1448         try = gtk_tree_path_copy(sel);
1449         if (!gtk_tree_path_prev(try) || !gtk_tree_path_prev(try)) {
1450                 gtk_tree_path_free(try);
1451                 gtk_tree_path_free(sel);
1452                 return;
1453         }
1454         gtk_tree_path_free(try);
1455
1456         prev = gtk_tree_path_copy(sel);         
1457         if (gtk_tree_path_prev(prev)) {
1458                 gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
1459                                         &iprev, prev);
1460                 gtk_list_store_swap(store, &iprev, &isel);
1461                 /* XXX: GTK2 select row?? */
1462         }
1463
1464         gtk_tree_path_free(sel);
1465         gtk_tree_path_free(prev);
1466 }
1467
1468 /*!
1469  *\brief        Signal handler for 'move down' button
1470  */
1471 static void prefs_matcher_down(void)
1472 {
1473         GtkListStore *store = NULL;
1474         GtkTreeModel *model = NULL;
1475         GtkTreeIter next, sel;
1476         GtkTreePath *try;
1477         
1478         if (!gtk_tree_selection_get_selected
1479                 (gtk_tree_view_get_selection
1480                         (GTK_TREE_VIEW(matcher.cond_list_view)),
1481                  &model,
1482                  &sel))
1483                 return;
1484         store = (GtkListStore *)model;
1485         try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
1486         if (!try) 
1487                 return;
1488         
1489         /* move when not at row 0 ... */
1490         if (gtk_tree_path_prev(try)) {
1491                 next = sel;
1492                 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next))
1493                         gtk_list_store_swap(store, &next, &sel);
1494         }
1495                 
1496         gtk_tree_path_free(try);
1497 }
1498
1499 /*!
1500  *\brief        Helper function that allows us to replace the 'Value' entry box
1501  *              by another widget.
1502  *
1503  *\param        old_widget Widget that needs to be removed.
1504  *\param        new_widget Replacement widget
1505  */
1506 static void prefs_matcher_set_value_widget(GtkWidget *old_widget, 
1507                                            GtkWidget *new_widget)
1508 {
1509         /* TODO: find out why the following spews harmless "parent errors" */
1510
1511         /* NOTE: we first need to bump up the refcount of the old_widget,
1512          * because the gtkut_container_remove() will otherwise destroy it */
1513         gtk_widget_ref(old_widget);
1514         gtkut_container_remove(GTK_CONTAINER(matcher.criteria_table), old_widget);
1515         gtk_widget_show(new_widget);
1516         gtk_widget_set_size_request(new_widget, 200, -1);
1517         gtk_table_attach(GTK_TABLE(matcher.criteria_table), new_widget, 
1518                          2, 3, 1, 2, 
1519                          GTK_FILL | GTK_SHRINK | GTK_EXPAND, 
1520                          0, 0, 0);
1521 }
1522
1523 static void prefs_matcher_disable_widget(GtkWidget* widget)
1524 {
1525         g_return_if_fail( widget != NULL);
1526
1527         gtk_widget_set_sensitive(widget, FALSE);
1528         gtk_widget_hide(widget);
1529 }
1530
1531 static void prefs_matcher_enable_widget(GtkWidget* widget)
1532 {
1533         g_return_if_fail( widget != NULL);
1534
1535         gtk_widget_set_sensitive(widget, TRUE);
1536         gtk_widget_show(widget);
1537 }
1538
1539 /*!
1540  *\brief        Change widgets depending on the selected condition
1541  *
1542  *\param        list List widget
1543  *\param        widget Not used
1544  *\param        user_data Not used      
1545  */
1546 static void prefs_matcher_criteria_select(GtkList *list,
1547                                           GtkWidget *widget,
1548                                           gpointer user_data)
1549 {
1550         gint value, old_value;
1551
1552         old_value = matcher.selected_criteria;
1553         matcher.selected_criteria = value = get_sel_from_list
1554                 (GTK_LIST(matcher.criteria_list));
1555
1556         if (old_value == matcher.selected_criteria)
1557                 return;
1558
1559         /* CLAWS: the value widget is currently either the color label combo box,
1560          * or a GtkEntry, so kiss for now */
1561         if (matcher.selected_criteria == CRITERIA_COLORLABEL) { 
1562                 prefs_matcher_set_value_widget(matcher.value_entry, 
1563                                                matcher.color_optmenu);
1564         } else if (old_value == CRITERIA_COLORLABEL) {
1565                 prefs_matcher_set_value_widget(matcher.color_optmenu,
1566                                                matcher.value_entry);
1567         }                                              
1568
1569         switch (value) {
1570         case CRITERIA_ALL:
1571                 prefs_matcher_disable_widget(matcher.header_combo);
1572                 prefs_matcher_disable_widget(matcher.header_label);
1573                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1574                 prefs_matcher_disable_widget(matcher.header_addr_label);
1575                 prefs_matcher_disable_widget(matcher.value_label);
1576                 prefs_matcher_disable_widget(matcher.value_entry);
1577                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1578                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1579                 prefs_matcher_disable_widget(matcher.predicate_label);
1580                 prefs_matcher_disable_widget(matcher.predicate_combo);
1581                 prefs_matcher_disable_widget(matcher.predicate_flag_combo);
1582                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1583                 prefs_matcher_disable_widget(matcher.regexp_chkbtn);
1584                 prefs_matcher_disable_widget(matcher.test_btn);
1585                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1586                 break;
1587
1588         case CRITERIA_UNREAD:
1589         case CRITERIA_NEW:
1590         case CRITERIA_MARKED:
1591         case CRITERIA_DELETED:
1592         case CRITERIA_REPLIED:
1593         case CRITERIA_FORWARDED:
1594         case CRITERIA_LOCKED:
1595         case CRITERIA_PARTIAL:
1596         case CRITERIA_IGNORE_THREAD:
1597                 prefs_matcher_disable_widget(matcher.header_combo);
1598                 prefs_matcher_disable_widget(matcher.header_label);
1599                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1600                 prefs_matcher_disable_widget(matcher.header_addr_label);
1601                 prefs_matcher_disable_widget(matcher.value_label);
1602                 prefs_matcher_disable_widget(matcher.value_entry);
1603                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1604                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1605                 prefs_matcher_enable_widget(matcher.predicate_label);
1606                 prefs_matcher_disable_widget(matcher.predicate_combo);
1607                 prefs_matcher_enable_widget(matcher.predicate_flag_combo);
1608                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1609                 prefs_matcher_disable_widget(matcher.regexp_chkbtn);
1610                 prefs_matcher_disable_widget(matcher.test_btn);
1611                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1612                 break;
1613                 
1614         case CRITERIA_COLORLABEL:
1615                 prefs_matcher_disable_widget(matcher.header_combo);
1616                 prefs_matcher_disable_widget(matcher.header_label);
1617                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1618                 prefs_matcher_disable_widget(matcher.header_addr_label);
1619                 prefs_matcher_enable_widget(matcher.value_label);
1620                 prefs_matcher_disable_widget(matcher.value_entry);
1621                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1622                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1623                 prefs_matcher_enable_widget(matcher.predicate_label);
1624                 prefs_matcher_disable_widget(matcher.predicate_combo);
1625                 prefs_matcher_enable_widget(matcher.predicate_flag_combo);
1626                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1627                 prefs_matcher_disable_widget(matcher.regexp_chkbtn);
1628                 prefs_matcher_disable_widget(matcher.test_btn);
1629                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1630                 break;
1631
1632         case CRITERIA_SUBJECT:
1633         case CRITERIA_FROM:
1634         case CRITERIA_TO:
1635         case CRITERIA_CC:
1636         case CRITERIA_TO_OR_CC:
1637         case CRITERIA_NEWSGROUPS:
1638         case CRITERIA_INREPLYTO:
1639         case CRITERIA_REFERENCES:
1640         case CRITERIA_HEADERS_PART:
1641         case CRITERIA_BODY_PART:
1642         case CRITERIA_MESSAGE:
1643                 prefs_matcher_disable_widget(matcher.header_combo);
1644                 prefs_matcher_disable_widget(matcher.header_label);
1645                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1646                 prefs_matcher_disable_widget(matcher.header_addr_label);
1647                 prefs_matcher_enable_widget(matcher.value_label);
1648                 prefs_matcher_enable_widget(matcher.value_entry);
1649                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1650                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1651                 prefs_matcher_enable_widget(matcher.predicate_label);
1652                 prefs_matcher_enable_widget(matcher.predicate_combo);
1653                 prefs_matcher_disable_widget(matcher.predicate_flag_combo);
1654                 prefs_matcher_enable_widget(matcher.case_chkbtn);
1655                 prefs_matcher_enable_widget(matcher.regexp_chkbtn);
1656                 prefs_matcher_disable_widget(matcher.test_btn);
1657                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1658                 break;
1659
1660         case CRITERIA_TEST:
1661                 prefs_matcher_disable_widget(matcher.header_combo);
1662                 prefs_matcher_disable_widget(matcher.header_label);
1663                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1664                 prefs_matcher_disable_widget(matcher.header_addr_label);
1665                 prefs_matcher_enable_widget(matcher.value_label);
1666                 prefs_matcher_enable_widget(matcher.value_entry);
1667                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1668                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1669                 prefs_matcher_enable_widget(matcher.predicate_label);
1670                 prefs_matcher_disable_widget(matcher.predicate_combo);
1671                 prefs_matcher_enable_widget(matcher.predicate_flag_combo);
1672                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1673                 prefs_matcher_disable_widget(matcher.regexp_chkbtn);
1674                 prefs_matcher_enable_widget(matcher.test_btn);
1675                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1676                 break;
1677
1678         case CRITERIA_AGE_GREATER:
1679         case CRITERIA_AGE_LOWER:
1680         case CRITERIA_SCORE_GREATER:
1681         case CRITERIA_SCORE_LOWER:
1682         case CRITERIA_SCORE_EQUAL:
1683         case CRITERIA_SIZE_GREATER:
1684         case CRITERIA_SIZE_SMALLER:
1685         case CRITERIA_SIZE_EQUAL:
1686                 prefs_matcher_disable_widget(matcher.header_combo);
1687                 prefs_matcher_disable_widget(matcher.header_label);
1688                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1689                 prefs_matcher_disable_widget(matcher.header_addr_label);
1690                 prefs_matcher_enable_widget(matcher.value_label);
1691                 prefs_matcher_enable_widget(matcher.value_entry);
1692                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1693                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1694                 prefs_matcher_disable_widget(matcher.predicate_label);
1695                 prefs_matcher_disable_widget(matcher.predicate_combo);
1696                 prefs_matcher_disable_widget(matcher.predicate_flag_combo);
1697                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1698                 prefs_matcher_disable_widget(matcher.regexp_chkbtn);
1699                 prefs_matcher_disable_widget(matcher.test_btn);
1700                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1701                 break;
1702
1703         case CRITERIA_HEADER:
1704                 prefs_matcher_enable_widget(matcher.header_combo);
1705                 prefs_matcher_enable_widget(matcher.header_label);
1706                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1707                 prefs_matcher_disable_widget(matcher.header_addr_label);
1708                 prefs_matcher_enable_widget(matcher.value_label);
1709                 prefs_matcher_enable_widget(matcher.value_entry);
1710                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1711                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1712                 prefs_matcher_enable_widget(matcher.predicate_label);
1713                 prefs_matcher_enable_widget(matcher.predicate_combo);
1714                 prefs_matcher_disable_widget(matcher.predicate_flag_combo);
1715                 prefs_matcher_enable_widget(matcher.case_chkbtn);
1716                 prefs_matcher_enable_widget(matcher.regexp_chkbtn);
1717                 prefs_matcher_disable_widget(matcher.test_btn);
1718                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1719                 break;
1720
1721         case CRITERIA_FOUND_IN_ADDRESSBOOK:
1722                 prefs_matcher_disable_widget(matcher.header_combo);
1723                 prefs_matcher_disable_widget(matcher.header_label);
1724                 prefs_matcher_enable_widget(matcher.header_addr_combo);
1725                 prefs_matcher_enable_widget(matcher.header_addr_label);
1726                 prefs_matcher_disable_widget(matcher.value_label);
1727                 prefs_matcher_disable_widget(matcher.value_entry);
1728                 prefs_matcher_enable_widget(matcher.addressbook_folder_label);
1729                 prefs_matcher_enable_widget(matcher.addressbook_folder_combo);
1730                 prefs_matcher_enable_widget(matcher.predicate_label);
1731                 prefs_matcher_disable_widget(matcher.predicate_combo);
1732                 prefs_matcher_enable_widget(matcher.predicate_flag_combo);
1733                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1734                 prefs_matcher_disable_widget(matcher.regexp_chkbtn);
1735                 prefs_matcher_disable_widget(matcher.test_btn);
1736                 prefs_matcher_enable_widget(matcher.addressbook_select_btn);
1737                 break;
1738         }
1739 }
1740
1741 /*!
1742  *\brief        Handle key press
1743  *
1744  *\param        widget Widget receiving key press
1745  *\param        event Key event
1746  *\param        data User data
1747  */
1748 static gboolean prefs_matcher_key_pressed(GtkWidget *widget, GdkEventKey *event,
1749                                      gpointer data)
1750 {
1751         if (event && event->keyval == GDK_Escape) {
1752                 prefs_matcher_cancel();
1753                 return TRUE;            
1754         }
1755         return FALSE;
1756 }
1757
1758 /*!
1759  *\brief        Cancel matcher dialog
1760  */
1761 static void prefs_matcher_cancel(void)
1762 {
1763         gtk_widget_hide(matcher.window);
1764         inc_unlock();
1765 }
1766
1767 /*!
1768  *\brief        Accept current matchers
1769  */
1770 static void prefs_matcher_ok(void)
1771 {
1772         MatcherList *matchers;
1773         MatcherProp *matcherprop;
1774         AlertValue val;
1775         gchar *matcher_str = NULL;
1776         gchar *str = NULL;
1777         gint row = 1;
1778         GtkTreeModel *model;
1779         GtkTreeIter iter;
1780
1781         matchers = prefs_matcher_get_list();
1782
1783         if (matchers != NULL) {
1784                 matcherprop = prefs_matcher_dialog_to_matcher();
1785                 if (matcherprop != NULL) {
1786                         str = matcherprop_to_string(matcherprop);
1787                         matcherprop_free(matcherprop);
1788                         if (strcmp(str, "all") != 0) {
1789                                 model = gtk_tree_view_get_model(GTK_TREE_VIEW
1790                                                 (matcher.cond_list_view));
1791
1792                                 while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1793                                         gtk_tree_model_get(model, &iter,
1794                                                            PREFS_MATCHER_COND, &matcher_str,
1795                                                            -1);
1796                                         if (matcher_str && strcmp(matcher_str, str) == 0) 
1797                                                 break;
1798                                         row++;
1799                                         g_free(matcher_str);
1800                                         matcher_str = NULL;
1801                                 }
1802
1803                                 if (!matcher_str || strcmp(matcher_str, str) != 0) {
1804                                         val = alertpanel(_("Entry not saved"),
1805                                                  _("The entry was not saved.\nClose anyway?"),
1806                                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1807                                         if (G_ALERTDEFAULT != val) {
1808                                                 g_free(matcher_str);                                             
1809                                                 g_free(str);
1810                                                 return;
1811                                         }
1812                                 }
1813                                 g_free(matcher_str);
1814                         }
1815                 }
1816                 g_free(str);
1817                 gtk_widget_hide(matcher.window);
1818                 inc_unlock();
1819                 if (matchers_callback != NULL)
1820                         matchers_callback(matchers);
1821                 matcherlist_free(matchers);
1822         }
1823 }
1824
1825 /*!
1826  *\brief        Called when closing dialog box
1827  *
1828  *\param        widget Dialog widget
1829  *\param        event Event info
1830  *\param        data User data
1831  *
1832  *\return       gint TRUE
1833  */
1834 static gint prefs_matcher_deleted(GtkWidget *widget, GdkEventAny *event,
1835                                   gpointer data)
1836 {
1837         prefs_matcher_cancel();
1838         return TRUE;
1839 }
1840
1841 /*
1842  * Strings describing test format strings
1843  * 
1844  * When adding new lines, remember to put 2 strings for each line
1845  */
1846 static gchar *test_desc_strings[] = {
1847         "%%",   N_("literal %"),
1848         "%s",   N_("Subject"),
1849         "%f",   N_("From"),
1850         "%t",   N_("To"),
1851         "%c",   N_("Cc"),
1852         "%d",   N_("Date"),
1853         "%i",   N_("Message-ID"),
1854         "%n",   N_("Newsgroups"),
1855         "%r",   N_("References"),
1856         "%F",   N_("filename (should not be modified)"),
1857         "\\n",  N_("new line"),
1858         "\\",   N_("escape character for quotes"),
1859         "\\\"", N_("quote character"),
1860         NULL,   NULL
1861 };
1862
1863 static DescriptionWindow test_desc_win = { 
1864         NULL,
1865         NULL, 
1866         2,
1867         N_("Match Type: 'Test'"),
1868         N_("'Test' allows you to test a message or message element "
1869            "using an external program or script. The program will "
1870            "return either 0 or 1.\n\n"
1871            "The following symbols can be used:"),
1872         test_desc_strings
1873 };
1874
1875
1876
1877 /*!
1878  *\brief        Show Test action's info
1879  */
1880 void prefs_matcher_test_info(void)
1881 {
1882         description_window_create(&test_desc_win);
1883 }
1884
1885 void prefs_matcher_addressbook_select(void)
1886 {
1887         gchar *folderpath = NULL;
1888         gboolean ret = FALSE;
1889
1890         folderpath = (gchar *) gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry));
1891         ret = addressbook_folder_selection(&folderpath);
1892         if ( ret != FALSE && folderpath != NULL)
1893                 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry), folderpath);
1894 }
1895
1896
1897 /*
1898  * list view
1899  */
1900
1901 static GtkListStore* prefs_matcher_create_data_store(void)
1902 {
1903         return gtk_list_store_new(N_PREFS_MATCHER_COLUMNS,
1904                                   G_TYPE_STRING,        
1905                                   G_TYPE_BOOLEAN,
1906                                   -1);
1907 }
1908
1909 static void prefs_matcher_list_view_insert_matcher(GtkWidget *list_view,
1910                                                    GtkTreeIter *row_iter,
1911                                                    const gchar *matcher,
1912                                                    gboolean is_valid) 
1913 {
1914         GtkTreeIter iter;
1915         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
1916                                         (GTK_TREE_VIEW(list_view)));
1917
1918         if (row_iter == NULL) {
1919                 /* append new */
1920                 gtk_list_store_append(list_store, &iter);
1921         } else {
1922                 /* change existing */
1923                 iter = *row_iter;
1924         }
1925
1926         gtk_list_store_set(list_store, &iter,
1927                            PREFS_MATCHER_COND, matcher,
1928                            PREFS_MATCHER_COND_VALID, is_valid,
1929                            -1);
1930 }
1931
1932 static GtkWidget *prefs_matcher_list_view_create(void)
1933 {
1934         GtkTreeView *list_view;
1935         GtkTreeSelection *selector;
1936         GtkTreeModel *model;
1937
1938         model = GTK_TREE_MODEL(prefs_matcher_create_data_store());
1939         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1940         g_object_unref(model);  
1941         
1942         gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
1943         
1944         selector = gtk_tree_view_get_selection(list_view);
1945         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1946         gtk_tree_selection_set_select_function(selector, prefs_matcher_selected,
1947                                                NULL, NULL);
1948
1949         /* create the columns */
1950         prefs_matcher_create_list_view_columns(GTK_WIDGET(list_view));
1951
1952         return GTK_WIDGET(list_view);
1953 }
1954
1955 static void prefs_matcher_create_list_view_columns(GtkWidget *list_view)
1956 {
1957         GtkTreeViewColumn *column;
1958         GtkCellRenderer *renderer;
1959
1960         renderer = gtk_cell_renderer_text_new();
1961         column = gtk_tree_view_column_new_with_attributes
1962                 (_("Current condition rules"),
1963                  renderer,
1964                  "text", PREFS_MATCHER_COND,
1965                  NULL);
1966         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1967 }
1968
1969 static gboolean prefs_matcher_selected(GtkTreeSelection *selector,
1970                                        GtkTreeModel *model, 
1971                                        GtkTreePath *path,
1972                                        gboolean currently_selected,
1973                                        gpointer data)
1974 {
1975         gchar *matcher_str;
1976         MatcherProp *prop;
1977         gboolean negative_cond;
1978         gint criteria;
1979         GtkTreeIter iter;
1980         gboolean is_valid;
1981
1982         if (currently_selected)
1983                 return TRUE;
1984
1985         if (!gtk_tree_model_get_iter(model, &iter, path))
1986                 return TRUE;
1987
1988         gtk_tree_model_get(model, &iter, 
1989                            PREFS_MATCHER_COND_VALID,  &is_valid,
1990                            PREFS_MATCHER_COND, &matcher_str,
1991                            -1);
1992         
1993         if (!is_valid) {
1994                 g_free(matcher_str);
1995                 prefs_matcher_reset_condition();
1996                 return TRUE;
1997         }
1998
1999         negative_cond = FALSE;
2000
2001         prop = matcher_parser_get_prop(matcher_str);
2002         if (prop == NULL) {
2003                 g_free(matcher_str);
2004                 return TRUE;
2005         }               
2006
2007         criteria = prefs_matcher_get_criteria_from_matching(prop->criteria);
2008         if (criteria != -1)
2009                 gtk_list_select_item(GTK_LIST(matcher.criteria_list),
2010                                      criteria);
2011
2012         switch(prop->criteria) {
2013         case MATCHCRITERIA_NOT_UNREAD:
2014         case MATCHCRITERIA_NOT_NEW:
2015         case MATCHCRITERIA_NOT_MARKED:
2016         case MATCHCRITERIA_NOT_DELETED:
2017         case MATCHCRITERIA_NOT_REPLIED:
2018         case MATCHCRITERIA_NOT_FORWARDED:
2019         case MATCHCRITERIA_NOT_LOCKED:
2020         case MATCHCRITERIA_NOT_PARTIAL:
2021         case MATCHCRITERIA_NOT_COLORLABEL:
2022         case MATCHCRITERIA_NOT_IGNORE_THREAD:
2023         case MATCHCRITERIA_NOT_SUBJECT:
2024         case MATCHCRITERIA_NOT_FROM:
2025         case MATCHCRITERIA_NOT_TO:
2026         case MATCHCRITERIA_NOT_CC:
2027         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
2028         case MATCHCRITERIA_NOT_NEWSGROUPS:
2029         case MATCHCRITERIA_NOT_INREPLYTO:
2030         case MATCHCRITERIA_NOT_REFERENCES:
2031         case MATCHCRITERIA_NOT_HEADER:
2032         case MATCHCRITERIA_NOT_HEADERS_PART:
2033         case MATCHCRITERIA_NOT_MESSAGE:
2034         case MATCHCRITERIA_NOT_BODY_PART:
2035         case MATCHCRITERIA_NOT_TEST:
2036         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
2037                 negative_cond = TRUE;
2038                 break;
2039         }
2040         
2041         switch(prop->criteria) {
2042         case MATCHCRITERIA_ALL:
2043                 break;
2044
2045         case MATCHCRITERIA_NOT_SUBJECT:
2046         case MATCHCRITERIA_NOT_FROM:
2047         case MATCHCRITERIA_NOT_TO:
2048         case MATCHCRITERIA_NOT_CC:
2049         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
2050         case MATCHCRITERIA_NOT_NEWSGROUPS:
2051         case MATCHCRITERIA_NOT_INREPLYTO:
2052         case MATCHCRITERIA_NOT_REFERENCES:
2053         case MATCHCRITERIA_NOT_HEADERS_PART:
2054         case MATCHCRITERIA_NOT_BODY_PART:
2055         case MATCHCRITERIA_NOT_MESSAGE:
2056         case MATCHCRITERIA_NOT_TEST:
2057         case MATCHCRITERIA_SUBJECT:
2058         case MATCHCRITERIA_FROM:
2059         case MATCHCRITERIA_TO:
2060         case MATCHCRITERIA_CC:
2061         case MATCHCRITERIA_TO_OR_CC:
2062         case MATCHCRITERIA_NEWSGROUPS:
2063         case MATCHCRITERIA_INREPLYTO:
2064         case MATCHCRITERIA_REFERENCES:
2065         case MATCHCRITERIA_HEADERS_PART:
2066         case MATCHCRITERIA_BODY_PART:
2067         case MATCHCRITERIA_MESSAGE:
2068         case MATCHCRITERIA_TEST:
2069                 gtk_entry_set_text(GTK_ENTRY(matcher.value_entry), prop->expr);
2070                 break;
2071
2072         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
2073         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
2074                 gtk_entry_set_text(GTK_ENTRY(matcher.header_addr_entry), prop->header);
2075                 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry), prop->expr);
2076                 break;
2077
2078         case MATCHCRITERIA_AGE_GREATER:
2079         case MATCHCRITERIA_AGE_LOWER:
2080         case MATCHCRITERIA_SCORE_GREATER:
2081         case MATCHCRITERIA_SCORE_LOWER:
2082         case MATCHCRITERIA_SCORE_EQUAL:
2083         case MATCHCRITERIA_SIZE_GREATER:
2084         case MATCHCRITERIA_SIZE_SMALLER:
2085         case MATCHCRITERIA_SIZE_EQUAL:
2086                 gtk_entry_set_text(GTK_ENTRY(matcher.value_entry), itos(prop->value));
2087                 break;
2088
2089         case MATCHCRITERIA_NOT_COLORLABEL:
2090         case MATCHCRITERIA_COLORLABEL:
2091                 gtk_option_menu_set_history(GTK_OPTION_MENU(matcher.color_optmenu),
2092                                             prop->value);
2093                 break;
2094
2095         case MATCHCRITERIA_NOT_HEADER:
2096         case MATCHCRITERIA_HEADER:
2097                 gtk_entry_set_text(GTK_ENTRY(matcher.header_entry), prop->header);
2098                 gtk_entry_set_text(GTK_ENTRY(matcher.value_entry), prop->expr);
2099                 break;
2100         }
2101
2102         if (negative_cond) {
2103                 gtk_list_select_item(GTK_LIST(matcher.predicate_list), 1);
2104                 gtk_list_select_item(GTK_LIST(matcher.predicate_flag_list), 1);
2105         } else {
2106                 gtk_list_select_item(GTK_LIST(matcher.predicate_list), 0);
2107                 gtk_list_select_item(GTK_LIST(matcher.predicate_flag_list), 0);
2108         }
2109         
2110         switch(prop->matchtype) {
2111         case MATCHTYPE_MATCH:
2112                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_chkbtn), FALSE);
2113                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_chkbtn), TRUE);
2114                 break;
2115
2116         case MATCHTYPE_MATCHCASE:
2117                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_chkbtn), FALSE);
2118                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_chkbtn), FALSE);
2119                 break;
2120
2121         case MATCHTYPE_REGEXP:
2122                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_chkbtn), TRUE);
2123                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_chkbtn), TRUE);
2124                 break;
2125
2126         case MATCHTYPE_REGEXPCASE:
2127                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_chkbtn), TRUE);
2128                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_chkbtn), FALSE);
2129                 break;
2130         }
2131
2132         g_free(matcher_str);
2133         return TRUE;
2134 }
2135