2006-09-12 [colin] 2.4.0cvs178
[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                               Q_("Filtering Matcher Menu|All"),
521                               _("Any"), "From", "To", "Cc", "Reply-To", "Sender",
522                               NULL);
523         gtk_table_attach(GTK_TABLE(criteria_table), header_addr_combo, 1, 2, 1, 2,
524                          0, 0, 0, 0);
525         header_addr_entry = GTK_COMBO(header_addr_combo)->entry;
526         gtk_entry_set_editable(GTK_ENTRY(header_addr_entry), TRUE);
527
528         /* value */
529
530         value_label = gtk_label_new(_("Value"));
531         gtk_widget_show(value_label);
532         gtk_misc_set_alignment(GTK_MISC (value_label), 0, 0.5);
533         gtk_table_attach(GTK_TABLE(criteria_table), value_label, 2, 3, 0, 1,
534                          GTK_FILL | GTK_SHRINK | GTK_EXPAND, 0, 0, 0);
535
536         value_entry = gtk_entry_new();
537         gtk_widget_show(value_entry);
538         gtk_widget_set_size_request(value_entry, 200, -1);
539         gtk_table_attach(GTK_TABLE(criteria_table), value_entry, 2, 3, 1, 2,
540                          GTK_FILL | GTK_SHRINK | GTK_EXPAND, 0, 0, 0);
541
542         /* book/folder value */
543
544         addressbook_folder_label = gtk_label_new(_("Book/folder"));
545         gtk_misc_set_alignment(GTK_MISC (addressbook_folder_label), 0, 0.5);
546         gtk_table_attach(GTK_TABLE(criteria_table), addressbook_folder_label, 2, 3, 0, 1,
547                          GTK_FILL | GTK_SHRINK | GTK_EXPAND, 0, 0, 0);
548
549         addressbook_folder_combo = gtk_combo_new();
550         gtk_widget_show(addressbook_folder_combo);
551         gtk_widget_set_size_request(addressbook_folder_combo, 200, -1);
552         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(addressbook_folder_combo)->entry),
553                                TRUE);
554
555         combo_items = NULL;
556         for (i = 0; i < (gint) (sizeof(addressbook_folder_text) / sizeof(gchar *)); i++) {
557                 combo_items = g_list_append(combo_items,
558                                             (gpointer) _(addressbook_folder_text[i]));
559         }
560         gtk_combo_set_popdown_strings(GTK_COMBO(addressbook_folder_combo), combo_items);
561         g_list_free(combo_items);
562
563         gtk_table_attach(GTK_TABLE(criteria_table), addressbook_folder_combo, 2, 3, 1, 2,
564                          GTK_FILL | GTK_SHRINK | GTK_EXPAND, 0, 0, 0);
565
566 #if GTK_CHECK_VERSION(2, 8, 0)
567         test_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
568 #else
569         test_btn = gtk_button_new_with_label(_("Info..."));
570 #endif
571         gtk_widget_show(test_btn);
572         gtk_table_attach(GTK_TABLE (criteria_table), test_btn, 3, 4, 1, 2,
573                          0, 0, 0, 0);
574         g_signal_connect(G_OBJECT (test_btn), "clicked",
575                          G_CALLBACK(prefs_matcher_test_info),
576                          NULL);
577
578         addressbook_select_btn = gtk_button_new_with_label(_(" Select... "));
579         gtk_table_attach(GTK_TABLE (criteria_table), addressbook_select_btn, 3, 4, 1, 2,
580                          0, 0, 0, 0);
581         g_signal_connect(G_OBJECT (addressbook_select_btn), "clicked",
582                          G_CALLBACK(prefs_matcher_addressbook_select),
583                          NULL);
584
585         color_optmenu = gtk_option_menu_new();
586         gtk_option_menu_set_menu(GTK_OPTION_MENU(color_optmenu),
587                                  colorlabel_create_color_menu());
588
589         /* predicate */
590
591         vbox2 = gtk_vbox_new(FALSE, VSPACING);
592         gtk_widget_show(vbox2);
593         gtk_box_pack_start(GTK_BOX(vbox1), vbox2, FALSE, FALSE, 0);
594
595         hbox1 = gtk_hbox_new(FALSE, 8);
596         gtk_widget_show(hbox1);
597         gtk_box_pack_start(GTK_BOX(vbox2), hbox1, FALSE, FALSE, 0);
598
599         predicate_label = gtk_label_new(_("Predicate"));
600         gtk_widget_show(predicate_label);
601         gtk_box_pack_start(GTK_BOX(hbox1), predicate_label,
602                            FALSE, FALSE, 0);
603
604         predicate_combo = gtk_combo_new();
605         gtk_widget_show(predicate_combo);
606         gtk_widget_set_size_request(predicate_combo, 120, -1);
607         predicate_list = GTK_COMBO(predicate_combo)->list;
608         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(predicate_combo)->entry),
609                                FALSE);
610
611         combo_items = NULL;
612
613         for (i = 0; i < (gint) (sizeof(predicate_text) / sizeof(gchar *)); i++) {
614                 combo_items = g_list_append(combo_items,
615                                             (gpointer) _(predicate_text[i]));
616         }
617         gtk_combo_set_popdown_strings(GTK_COMBO(predicate_combo), combo_items);
618
619         g_list_free(combo_items);
620
621         gtk_box_pack_start(GTK_BOX(hbox1), predicate_combo,
622                            FALSE, FALSE, 0);
623
624         /* predicate flag */
625
626         predicate_flag_combo = gtk_combo_new();
627         gtk_widget_hide(predicate_flag_combo);
628         gtk_widget_set_size_request(predicate_flag_combo, 120, -1);
629         predicate_flag_list = GTK_COMBO(predicate_flag_combo)->list;
630         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(predicate_flag_combo)->entry), FALSE);
631
632         combo_items = NULL;
633
634         for (i = 0; i < (gint) (sizeof(predicate_text) / sizeof(gchar *)); i++) {
635                 combo_items = g_list_append(combo_items, (gpointer) _(predicate_flag_text[i]));
636         }
637         gtk_combo_set_popdown_strings(GTK_COMBO(predicate_flag_combo),
638                                       combo_items);
639
640         g_list_free(combo_items);
641
642         gtk_box_pack_start(GTK_BOX(hbox1), predicate_flag_combo,
643                            FALSE, FALSE, 0);
644
645         vbox3 = gtk_vbox_new(FALSE, 0);
646         gtk_widget_show (vbox3);
647         gtk_box_pack_start(GTK_BOX(hbox1), vbox3, FALSE, FALSE, 0);
648
649         PACK_CHECK_BUTTON(vbox3, case_chkbtn, _("Case sensitive"));
650         PACK_CHECK_BUTTON(vbox3, regexp_chkbtn, _("Use regexp"));
651
652         /* register / substitute / delete */
653
654         reg_hbox = gtk_hbox_new(FALSE, 4);
655         gtk_widget_show(reg_hbox);
656         gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
657
658         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
659         gtk_widget_show(arrow);
660         gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
661         gtk_widget_set_size_request(arrow, -1, 16);
662
663         btn_hbox = gtk_hbox_new(TRUE, 4);
664         gtk_widget_show(btn_hbox);
665         gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
666
667         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
668         gtk_widget_show(reg_btn);
669         gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
670         g_signal_connect(G_OBJECT(reg_btn), "clicked",
671                          G_CALLBACK(prefs_matcher_register_cb), NULL);
672
673         subst_btn = gtkut_get_replace_btn(_("Replace"));
674         gtk_widget_show(subst_btn);
675         gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
676         g_signal_connect(G_OBJECT(subst_btn), "clicked",
677                          G_CALLBACK(prefs_matcher_substitute_cb),
678                          NULL);
679
680         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
681         gtk_widget_show(del_btn);
682         gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
683         g_signal_connect(G_OBJECT(del_btn), "clicked",
684                          G_CALLBACK(prefs_matcher_delete_cb), NULL);
685
686         /* boolean operation */
687
688         bool_op_label = gtk_label_new(_("Boolean Op"));
689         gtk_misc_set_alignment(GTK_MISC(value_label), 0, 0.5);
690         gtk_widget_show(bool_op_label);
691         gtk_box_pack_start(GTK_BOX(btn_hbox), bool_op_label,
692                            FALSE, FALSE, 0);
693
694         bool_op_combo = gtk_combo_new();
695         gtk_widget_show(bool_op_combo);
696         gtk_widget_set_size_request(bool_op_combo, 60, -1);
697         bool_op_list = GTK_COMBO(bool_op_combo)->list;
698         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(bool_op_combo)->entry),
699                                FALSE);
700
701         combo_items = NULL;
702
703         for (i = 0; i < (gint) (sizeof(bool_op_text) / sizeof(gchar *)); i++) {
704                 combo_items = g_list_append(combo_items,
705                                             (gpointer) _(bool_op_text[i]));
706         }
707         gtk_combo_set_popdown_strings(GTK_COMBO(bool_op_combo), combo_items);
708
709         g_list_free(combo_items);
710
711         gtk_box_pack_start(GTK_BOX(btn_hbox), bool_op_combo,
712                            FALSE, FALSE, 0);
713
714         cond_hbox = gtk_hbox_new(FALSE, 8);
715         gtk_widget_show(cond_hbox);
716         gtk_box_pack_start(GTK_BOX(vbox1), cond_hbox, TRUE, TRUE, 0);
717
718         cond_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
719         gtk_widget_show(cond_scrolledwin);
720         gtk_widget_set_size_request(cond_scrolledwin, -1, 150);
721         gtk_box_pack_start(GTK_BOX(cond_hbox), cond_scrolledwin,
722                            TRUE, TRUE, 0);
723         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(cond_scrolledwin),
724                                        GTK_POLICY_AUTOMATIC,
725                                        GTK_POLICY_AUTOMATIC);
726
727         cond_list_view = prefs_matcher_list_view_create();                                     
728         gtk_widget_show(cond_list_view);
729         gtk_container_add(GTK_CONTAINER(cond_scrolledwin), cond_list_view);
730
731         btn_vbox = gtk_vbox_new(FALSE, 8);
732         gtk_widget_show(btn_vbox);
733         gtk_box_pack_start(GTK_BOX(cond_hbox), btn_vbox, FALSE, FALSE, 0);
734
735         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
736         gtk_widget_show(up_btn);
737         gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0);
738         g_signal_connect(G_OBJECT(up_btn), "clicked",
739                          G_CALLBACK(prefs_matcher_up), NULL);
740
741         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
742         gtk_widget_show(down_btn);
743         gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
744         g_signal_connect(G_OBJECT(down_btn), "clicked",
745                          G_CALLBACK(prefs_matcher_down), NULL);
746
747         if (!geometry.min_height) {
748                 geometry.min_width = 520;
749                 geometry.min_height = 368;
750         }
751
752         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
753                                       GDK_HINT_MIN_SIZE);
754         gtk_widget_set_size_request(window, prefs_common.matcherwin_width,
755                                     prefs_common.matcherwin_height);
756
757         gtk_widget_show_all(window);
758         gtk_widget_hide(header_addr_label);
759         gtk_widget_hide(header_addr_combo);
760         gtk_widget_hide(addressbook_select_btn);
761         gtk_widget_hide(addressbook_folder_label);
762
763         matcher.window    = window;
764
765         matcher.ok_btn = ok_btn;
766
767         matcher.criteria_list = criteria_list;
768         matcher.header_combo = header_combo;
769         matcher.header_entry = header_entry;
770         matcher.header_label = header_label;
771         matcher.header_addr_combo = header_addr_combo;
772         matcher.header_addr_entry = header_addr_entry;
773         matcher.header_addr_label = header_addr_label;
774         matcher.value_entry = value_entry;
775         matcher.value_label = value_label;
776         matcher.addressbook_folder_label = addressbook_folder_label;
777         matcher.addressbook_folder_combo = addressbook_folder_combo;
778         matcher.predicate_label = predicate_label;
779         matcher.predicate_list = predicate_list;
780         matcher.predicate_combo = predicate_combo;
781         matcher.predicate_flag_list = predicate_flag_list;
782         matcher.predicate_flag_combo = predicate_flag_combo;
783         matcher.case_chkbtn = case_chkbtn;
784         matcher.regexp_chkbtn = regexp_chkbtn;
785         matcher.bool_op_list = bool_op_list;
786         matcher.test_btn = test_btn;
787         matcher.addressbook_select_btn = addressbook_select_btn;
788         matcher.color_optmenu = color_optmenu;
789         matcher.criteria_table = criteria_table;
790
791         matcher.cond_list_view = cond_list_view;
792
793         matcher.selected_criteria = -1;
794         prefs_matcher_criteria_select(GTK_LIST(criteria_list), NULL, NULL);
795 }
796
797 /*!
798  *\brief        Set the contents of a row
799  *
800  *\param        row Index of row to set
801  *\param        prop Condition to set
802  *
803  *\return       gint Row index \a prop has been added
804  */
805 static void prefs_matcher_list_view_set_row(GtkTreeIter *row, MatcherProp *prop)
806 {
807         gchar *matcher_str;
808
809         if (prop == NULL) {
810                 prefs_matcher_list_view_insert_matcher(matcher.cond_list_view,
811                                                        NULL, _("(New)"), FALSE);
812                 return;                                                
813         }
814
815         matcher_str = matcherprop_to_string(prop);
816         if (!row)
817                 prefs_matcher_list_view_insert_matcher(matcher.cond_list_view,
818                                                        NULL, matcher_str,
819                                                        TRUE);
820         else
821                 prefs_matcher_list_view_insert_matcher(matcher.cond_list_view,
822                                                        row, matcher_str, 
823                                                        TRUE);
824         g_free(matcher_str);
825 }
826
827 /*!
828  *\brief        Clears a condition in the list widget
829  */
830 static void prefs_matcher_reset_condition(void)
831 {
832         gtk_list_select_item(GTK_LIST(matcher.criteria_list), 0);
833         gtk_list_select_item(GTK_LIST(matcher.predicate_list), 0);
834         gtk_entry_set_text(GTK_ENTRY(matcher.header_entry), "");
835         gtk_entry_set_text(GTK_ENTRY(matcher.header_addr_entry), "");
836         gtk_entry_set_text(GTK_ENTRY(matcher.value_entry), "");
837         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry), "");
838 }
839
840 /*!
841  *\brief        Initializes dialog with a set of conditions
842  *
843  *\param        matchers List of conditions
844  */
845 static void prefs_matcher_set_dialog(MatcherList *matchers)
846 {
847         GSList *cur;
848         gboolean bool_op = 1;
849         GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model
850                                 (GTK_TREE_VIEW(matcher.cond_list_view)));
851
852         gtk_list_store_clear(store);                            
853
854         prefs_matcher_list_view_set_row(NULL, NULL);
855         if (matchers != NULL) {
856                 for (cur = matchers->matchers; cur != NULL;
857                      cur = g_slist_next(cur)) {
858                         MatcherProp *prop;
859                         prop = (MatcherProp *) cur->data;
860                         prefs_matcher_list_view_set_row(NULL, prop);
861                 }
862
863                 bool_op = matchers->bool_and;
864         }
865         
866         gtk_list_select_item(GTK_LIST(matcher.bool_op_list), bool_op);
867
868         prefs_matcher_reset_condition();
869 }
870
871 /*!
872  *\brief        Converts current conditions in list box in
873  *              a matcher list used by the matcher.
874  *
875  *\return       MatcherList * List of conditions.
876  */
877 static MatcherList *prefs_matcher_get_list(void)
878 {
879         gchar *matcher_str;
880         MatcherProp *prop;
881         gboolean bool_and;
882         GSList *matcher_list;
883         MatcherList *matchers;
884         GtkTreeModel *model;
885         GtkTreeIter iter;
886
887         model = gtk_tree_view_get_model(GTK_TREE_VIEW(matcher.cond_list_view));
888         if (!gtk_tree_model_get_iter_first(model, &iter))
889                 return NULL;
890
891         matcher_list = NULL;
892
893         do {
894                 gboolean is_valid;
895         
896                 gtk_tree_model_get(model, &iter,
897                                    PREFS_MATCHER_COND, &matcher_str,
898                                    PREFS_MATCHER_COND_VALID, &is_valid,
899                                    -1);
900                 
901                 if (is_valid) {
902                         /* tmp = matcher_str; */
903                         prop = matcher_parser_get_prop(matcher_str);
904                         g_free(matcher_str);
905                         if (prop == NULL)
906                                 break;
907                         
908                         matcher_list = g_slist_append(matcher_list, prop);
909                 }
910         } while (gtk_tree_model_iter_next(model, &iter));
911
912         bool_and = get_sel_from_list(GTK_LIST(matcher.bool_op_list));
913
914         matchers = matcherlist_new(matcher_list, bool_and);
915
916         return matchers;
917 }
918
919 /*!
920  *\brief        Maps a keyword id (see #get_matchparser_tab_id) to a 
921  *              criteria type (see first parameter of #matcherprop_new
922  *              or #matcherprop_unquote_new)
923  *
924  *\param        matching_id Id returned by the matcher parser.
925  *
926  *\return       gint One of the CRITERIA_xxx constants.
927  */
928 static gint prefs_matcher_get_criteria_from_matching(gint matching_id)
929 {
930         switch(matching_id) {
931         case MATCHCRITERIA_ALL:
932                 return CRITERIA_ALL;
933         case MATCHCRITERIA_NOT_UNREAD:
934         case MATCHCRITERIA_UNREAD:
935                 return CRITERIA_UNREAD;
936         case MATCHCRITERIA_NOT_NEW:
937         case MATCHCRITERIA_NEW:
938                 return CRITERIA_NEW;
939         case MATCHCRITERIA_NOT_MARKED:
940         case MATCHCRITERIA_MARKED:
941                 return CRITERIA_MARKED;
942         case MATCHCRITERIA_NOT_DELETED:
943         case MATCHCRITERIA_DELETED:
944                 return CRITERIA_DELETED;
945         case MATCHCRITERIA_NOT_REPLIED:
946         case MATCHCRITERIA_REPLIED:
947                 return CRITERIA_REPLIED;
948         case MATCHCRITERIA_NOT_FORWARDED:
949         case MATCHCRITERIA_FORWARDED:
950                 return CRITERIA_FORWARDED;
951         case MATCHCRITERIA_LOCKED:
952         case MATCHCRITERIA_NOT_LOCKED:
953                 return CRITERIA_LOCKED;
954         case MATCHCRITERIA_PARTIAL:
955         case MATCHCRITERIA_NOT_PARTIAL:
956                 return CRITERIA_PARTIAL;
957         case MATCHCRITERIA_COLORLABEL:
958         case MATCHCRITERIA_NOT_COLORLABEL:
959                 return CRITERIA_COLORLABEL;
960         case MATCHCRITERIA_IGNORE_THREAD:
961         case MATCHCRITERIA_NOT_IGNORE_THREAD:
962                 return CRITERIA_IGNORE_THREAD;
963         case MATCHCRITERIA_NOT_SUBJECT:
964         case MATCHCRITERIA_SUBJECT:
965                 return CRITERIA_SUBJECT;
966         case MATCHCRITERIA_NOT_FROM:
967         case MATCHCRITERIA_FROM:
968                 return CRITERIA_FROM;
969         case MATCHCRITERIA_NOT_TO:
970         case MATCHCRITERIA_TO:
971                 return CRITERIA_TO;
972         case MATCHCRITERIA_NOT_CC:
973         case MATCHCRITERIA_CC:
974                 return CRITERIA_CC;
975         case MATCHCRITERIA_NOT_NEWSGROUPS:
976         case MATCHCRITERIA_NEWSGROUPS:
977                 return CRITERIA_NEWSGROUPS;
978         case MATCHCRITERIA_NOT_INREPLYTO:
979         case MATCHCRITERIA_INREPLYTO:
980                 return CRITERIA_INREPLYTO;
981         case MATCHCRITERIA_NOT_REFERENCES:
982         case MATCHCRITERIA_REFERENCES:
983                 return CRITERIA_REFERENCES;
984         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
985         case MATCHCRITERIA_TO_OR_CC:
986                 return CRITERIA_TO_OR_CC;
987         case MATCHCRITERIA_NOT_BODY_PART:
988         case MATCHCRITERIA_BODY_PART:
989                 return CRITERIA_BODY_PART;
990         case MATCHCRITERIA_NOT_MESSAGE:
991         case MATCHCRITERIA_MESSAGE:
992                 return CRITERIA_MESSAGE;
993         case MATCHCRITERIA_NOT_HEADERS_PART:
994         case MATCHCRITERIA_HEADERS_PART:
995                 return CRITERIA_HEADERS_PART;
996         case MATCHCRITERIA_NOT_HEADER:
997         case MATCHCRITERIA_HEADER:
998                 return CRITERIA_HEADER;
999         case MATCHCRITERIA_AGE_GREATER:
1000                 return CRITERIA_AGE_GREATER;
1001         case MATCHCRITERIA_AGE_LOWER:
1002                 return CRITERIA_AGE_LOWER;
1003         case MATCHCRITERIA_SCORE_GREATER:
1004                 return CRITERIA_SCORE_GREATER;
1005         case MATCHCRITERIA_SCORE_LOWER:
1006                 return CRITERIA_SCORE_LOWER;
1007         case MATCHCRITERIA_SCORE_EQUAL:
1008                 return CRITERIA_SCORE_EQUAL;
1009         case MATCHCRITERIA_NOT_TEST:
1010         case MATCHCRITERIA_TEST:
1011                 return CRITERIA_TEST;
1012         case MATCHCRITERIA_SIZE_GREATER:
1013                 return CRITERIA_SIZE_GREATER;
1014         case MATCHCRITERIA_SIZE_SMALLER:
1015                 return CRITERIA_SIZE_SMALLER;
1016         case MATCHCRITERIA_SIZE_EQUAL:
1017                 return CRITERIA_SIZE_EQUAL;
1018         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1019         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1020                 return CRITERIA_FOUND_IN_ADDRESSBOOK;
1021         default:
1022                 return -1;
1023         }
1024 }
1025
1026 /*!
1027  *\brief        Returns the matcher keyword id from a criteria id
1028  *
1029  *\param        criteria_id Criteria id (should not be the negate
1030  *              one)
1031  *
1032  *\return       gint A matcher keyword id. See #get_matchparser_tab_id.
1033  */
1034 static gint prefs_matcher_get_matching_from_criteria(gint criteria_id)
1035 {
1036         switch (criteria_id) {
1037         case CRITERIA_ALL:
1038                 return MATCHCRITERIA_ALL;
1039         case CRITERIA_UNREAD:
1040                 return MATCHCRITERIA_UNREAD;
1041         case CRITERIA_NEW:
1042                 return MATCHCRITERIA_NEW;
1043         case CRITERIA_MARKED:
1044                 return MATCHCRITERIA_MARKED;
1045         case CRITERIA_DELETED:
1046                 return MATCHCRITERIA_DELETED;
1047         case CRITERIA_REPLIED:
1048                 return MATCHCRITERIA_REPLIED;
1049         case CRITERIA_FORWARDED:
1050                 return MATCHCRITERIA_FORWARDED;
1051         case CRITERIA_LOCKED:
1052                 return MATCHCRITERIA_LOCKED;
1053         case CRITERIA_PARTIAL:
1054                 return MATCHCRITERIA_PARTIAL;
1055         case CRITERIA_COLORLABEL:
1056                 return MATCHCRITERIA_COLORLABEL;
1057         case CRITERIA_IGNORE_THREAD:
1058                 return MATCHCRITERIA_IGNORE_THREAD;
1059         case CRITERIA_SUBJECT:
1060                 return MATCHCRITERIA_SUBJECT;
1061         case CRITERIA_FROM:
1062                 return MATCHCRITERIA_FROM;
1063         case CRITERIA_TO:
1064                 return MATCHCRITERIA_TO;
1065         case CRITERIA_CC:
1066                 return MATCHCRITERIA_CC;
1067         case CRITERIA_TO_OR_CC:
1068                 return MATCHCRITERIA_TO_OR_CC;
1069         case CRITERIA_NEWSGROUPS:
1070                 return MATCHCRITERIA_NEWSGROUPS;
1071         case CRITERIA_INREPLYTO:
1072                 return MATCHCRITERIA_INREPLYTO;
1073         case CRITERIA_REFERENCES:
1074                 return MATCHCRITERIA_REFERENCES;
1075         case CRITERIA_AGE_GREATER:
1076                 return MATCHCRITERIA_AGE_GREATER;
1077         case CRITERIA_AGE_LOWER:
1078                 return MATCHCRITERIA_AGE_LOWER;
1079         case CRITERIA_SCORE_GREATER:
1080                 return MATCHCRITERIA_SCORE_GREATER;
1081         case CRITERIA_SCORE_LOWER:
1082                 return MATCHCRITERIA_SCORE_LOWER;
1083         case CRITERIA_SCORE_EQUAL:
1084                 return MATCHCRITERIA_SCORE_EQUAL;
1085         case CRITERIA_HEADER:
1086                 return MATCHCRITERIA_HEADER;
1087         case CRITERIA_HEADERS_PART:
1088                 return MATCHCRITERIA_HEADERS_PART;
1089         case CRITERIA_BODY_PART:
1090                 return MATCHCRITERIA_BODY_PART;
1091         case CRITERIA_MESSAGE:
1092                 return MATCHCRITERIA_MESSAGE;
1093         case CRITERIA_TEST:
1094                 return MATCHCRITERIA_TEST;
1095         case CRITERIA_SIZE_GREATER:
1096                 return MATCHCRITERIA_SIZE_GREATER;
1097         case CRITERIA_SIZE_SMALLER:
1098                 return MATCHCRITERIA_SIZE_SMALLER;
1099         case CRITERIA_SIZE_EQUAL:
1100                 return MATCHCRITERIA_SIZE_EQUAL;
1101         case CRITERIA_FOUND_IN_ADDRESSBOOK:
1102                 return MATCHCRITERIA_FOUND_IN_ADDRESSBOOK;
1103         default:
1104                 return -1;
1105         }
1106 }
1107
1108 /*!
1109  *\brief        Returns the negate matcher keyword id from a matcher keyword
1110  *              id.
1111  *
1112  *\param        matcher_criteria Matcher keyword id. 
1113  *
1114  *\return       gint A matcher keyword id. See #get_matchparser_tab_id.
1115  */
1116 static gint prefs_matcher_not_criteria(gint matcher_criteria)
1117 {
1118         switch(matcher_criteria) {
1119         case MATCHCRITERIA_UNREAD:
1120                 return MATCHCRITERIA_NOT_UNREAD;
1121         case MATCHCRITERIA_NEW:
1122                 return MATCHCRITERIA_NOT_NEW;
1123         case MATCHCRITERIA_MARKED:
1124                 return MATCHCRITERIA_NOT_MARKED;
1125         case MATCHCRITERIA_DELETED:
1126                 return MATCHCRITERIA_NOT_DELETED;
1127         case MATCHCRITERIA_REPLIED:
1128                 return MATCHCRITERIA_NOT_REPLIED;
1129         case MATCHCRITERIA_FORWARDED:
1130                 return MATCHCRITERIA_NOT_FORWARDED;
1131         case MATCHCRITERIA_LOCKED:
1132                 return MATCHCRITERIA_NOT_LOCKED;
1133         case MATCHCRITERIA_PARTIAL:
1134                 return MATCHCRITERIA_NOT_PARTIAL;
1135         case MATCHCRITERIA_COLORLABEL:
1136                 return MATCHCRITERIA_NOT_COLORLABEL;
1137         case MATCHCRITERIA_IGNORE_THREAD:
1138                 return MATCHCRITERIA_NOT_IGNORE_THREAD;
1139         case MATCHCRITERIA_SUBJECT:
1140                 return MATCHCRITERIA_NOT_SUBJECT;
1141         case MATCHCRITERIA_FROM:
1142                 return MATCHCRITERIA_NOT_FROM;
1143         case MATCHCRITERIA_TO:
1144                 return MATCHCRITERIA_NOT_TO;
1145         case MATCHCRITERIA_CC:
1146                 return MATCHCRITERIA_NOT_CC;
1147         case MATCHCRITERIA_TO_OR_CC:
1148                 return MATCHCRITERIA_NOT_TO_AND_NOT_CC;
1149         case MATCHCRITERIA_NEWSGROUPS:
1150                 return MATCHCRITERIA_NOT_NEWSGROUPS;
1151         case MATCHCRITERIA_INREPLYTO:
1152                 return MATCHCRITERIA_NOT_INREPLYTO;
1153         case MATCHCRITERIA_REFERENCES:
1154                 return MATCHCRITERIA_NOT_REFERENCES;
1155         case MATCHCRITERIA_HEADER:
1156                 return MATCHCRITERIA_NOT_HEADER;
1157         case MATCHCRITERIA_HEADERS_PART:
1158                 return MATCHCRITERIA_NOT_HEADERS_PART;
1159         case MATCHCRITERIA_MESSAGE:
1160                 return MATCHCRITERIA_NOT_MESSAGE;
1161         case MATCHCRITERIA_TEST:
1162                 return MATCHCRITERIA_NOT_TEST;
1163         case MATCHCRITERIA_BODY_PART:
1164                 return MATCHCRITERIA_NOT_BODY_PART;
1165         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1166                 return MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK;
1167         default:
1168                 return matcher_criteria;
1169         }
1170 }
1171
1172 /*!
1173  *\brief        Converts the text in the selected row to a 
1174  *              matcher structure
1175  *
1176  *\return       MatcherProp * Newly allocated matcher structure.
1177  */
1178 static MatcherProp *prefs_matcher_dialog_to_matcher(void)
1179 {
1180         MatcherProp *matcherprop;
1181         gint criteria;
1182         gint matchtype;
1183         gint value_pred;
1184         gint value_pred_flag;
1185         gint value_criteria;
1186         gboolean use_regexp;
1187         gboolean case_sensitive;
1188         const gchar *header;
1189         const gchar *expr;
1190         gint value;
1191         const gchar *value_str;
1192
1193         value_criteria = get_sel_from_list(GTK_LIST(matcher.criteria_list));
1194
1195         criteria = prefs_matcher_get_matching_from_criteria(value_criteria);
1196
1197         value_pred = get_sel_from_list(GTK_LIST(matcher.predicate_list));
1198         value_pred_flag = get_sel_from_list(GTK_LIST(matcher.predicate_flag_list));
1199
1200         use_regexp = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(matcher.regexp_chkbtn));
1201         case_sensitive = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(matcher.case_chkbtn));
1202
1203         switch (value_criteria) {
1204         case CRITERIA_UNREAD:
1205         case CRITERIA_NEW:
1206         case CRITERIA_MARKED:
1207         case CRITERIA_DELETED:
1208         case CRITERIA_REPLIED:
1209         case CRITERIA_FORWARDED:
1210         case CRITERIA_LOCKED:
1211         case CRITERIA_PARTIAL:
1212         case CRITERIA_TEST:
1213         case CRITERIA_COLORLABEL:
1214         case CRITERIA_IGNORE_THREAD:
1215         case CRITERIA_FOUND_IN_ADDRESSBOOK:
1216                 if (value_pred_flag == PREDICATE_FLAG_DISABLED)
1217                         criteria = prefs_matcher_not_criteria(criteria);
1218                 break;
1219         case CRITERIA_SUBJECT:
1220         case CRITERIA_FROM:
1221         case CRITERIA_TO:
1222         case CRITERIA_CC:
1223         case CRITERIA_TO_OR_CC:
1224         case CRITERIA_NEWSGROUPS:
1225         case CRITERIA_INREPLYTO:
1226         case CRITERIA_REFERENCES:
1227         case CRITERIA_HEADERS_PART:
1228         case CRITERIA_BODY_PART:
1229         case CRITERIA_MESSAGE:
1230         case CRITERIA_AGE_GREATER:
1231         case CRITERIA_AGE_LOWER:
1232         case CRITERIA_HEADER:
1233                 if (value_pred == PREDICATE_DOES_NOT_CONTAIN)
1234                         criteria = prefs_matcher_not_criteria(criteria);
1235                 break;
1236         }
1237
1238         if (use_regexp) {
1239                 if (case_sensitive)
1240                         matchtype = MATCHTYPE_REGEXP;
1241                 else
1242                         matchtype = MATCHTYPE_REGEXPCASE;
1243         }
1244         else {
1245                 if (case_sensitive)
1246                         matchtype = MATCHTYPE_MATCH;
1247                 else
1248                         matchtype = MATCHTYPE_MATCHCASE;
1249         }
1250
1251         header = NULL;
1252         expr = NULL;
1253         value = 0;
1254
1255         switch (value_criteria) {
1256         case CRITERIA_ALL:
1257         case CRITERIA_UNREAD:
1258         case CRITERIA_NEW:
1259         case CRITERIA_MARKED:
1260         case CRITERIA_DELETED:
1261         case CRITERIA_REPLIED:
1262         case CRITERIA_FORWARDED:
1263         case CRITERIA_LOCKED:
1264         case CRITERIA_PARTIAL:
1265         case CRITERIA_IGNORE_THREAD:
1266                 break;
1267
1268         case CRITERIA_SUBJECT:
1269         case CRITERIA_FROM:
1270         case CRITERIA_TO:
1271         case CRITERIA_CC:
1272         case CRITERIA_TO_OR_CC:
1273         case CRITERIA_NEWSGROUPS:
1274         case CRITERIA_INREPLYTO:
1275         case CRITERIA_REFERENCES:
1276         case CRITERIA_HEADERS_PART:
1277         case CRITERIA_BODY_PART:
1278         case CRITERIA_MESSAGE:
1279         case CRITERIA_TEST:
1280                 expr = gtk_entry_get_text(GTK_ENTRY(matcher.value_entry));
1281                 break;
1282
1283         case CRITERIA_AGE_GREATER:
1284         case CRITERIA_AGE_LOWER:
1285         case CRITERIA_SCORE_GREATER:
1286         case CRITERIA_SCORE_LOWER:
1287         case CRITERIA_SCORE_EQUAL:
1288         case CRITERIA_SIZE_GREATER:
1289         case CRITERIA_SIZE_SMALLER:
1290         case CRITERIA_SIZE_EQUAL:
1291                 value_str = gtk_entry_get_text(GTK_ENTRY(matcher.value_entry));
1292
1293                 if (*value_str == '\0') {
1294                     alertpanel_error(_("Value is not set."));
1295                     return NULL;
1296                 }
1297
1298                 value = atoi(value_str);
1299                 break;
1300                 
1301         case CRITERIA_COLORLABEL:
1302                 value = colorlabel_get_color_menu_active_item
1303                         (gtk_option_menu_get_menu(GTK_OPTION_MENU
1304                                 (matcher.color_optmenu))); 
1305                 break;
1306
1307         case CRITERIA_HEADER:
1308                 header = gtk_entry_get_text(GTK_ENTRY(matcher.header_entry));
1309                 expr = gtk_entry_get_text(GTK_ENTRY(matcher.value_entry));
1310
1311                 if (*header == '\0') {
1312                     alertpanel_error(_("Header name is not set."));
1313                     return NULL;
1314                 }
1315                 break;
1316
1317         case CRITERIA_FOUND_IN_ADDRESSBOOK:
1318                 header = gtk_entry_get_text(GTK_ENTRY(matcher.header_addr_entry));
1319                 expr = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry));
1320
1321                 if (*header == '\0') {
1322                     alertpanel_error(_("Header name is not set."));
1323                     return NULL;
1324                 }
1325                 if (*expr == '\0') {
1326                         gchar *msg;
1327                         gchar *tmp;
1328
1329                         if (strcasecmp(header, Q_("Filtering Matcher Menu|All")) == 0)
1330                                 tmp = g_strdup(_("all addresses in all headers"));
1331                         else
1332                         if (strcasecmp(header, _("Any")) == 0)
1333                                 tmp = g_strdup(_("any address in any header"));
1334                         else
1335                                 tmp = g_strdup_printf(_("the address(es) in header '%s'"), header);
1336                         msg = g_strdup_printf(_("Book/folder path is not set.\n\n"
1337                                                 "If you want to match %s against the whole address book, "
1338                                                 "you have to select 'Any' from the book/folder drop-down list."),
1339                                                 tmp);
1340                     alertpanel_error(msg);
1341                         g_free(msg);
1342                         g_free(tmp);
1343                     return NULL;
1344                 }
1345                 /* don't store translated "Any"/"All" in matcher expressions */
1346                 if (strcasecmp(header, Q_("Filtering Matcher Menu|All")) == 0)
1347                         header = "All";
1348                 else
1349                         if (strcasecmp(header, _("Any")) == 0)
1350                                 header = "Any";
1351                 if (strcasecmp(expr, _("Any")) == 0)
1352                         expr = "Any";
1353                 break;
1354         }
1355
1356         matcherprop = matcherprop_new(criteria, header, matchtype,
1357                                       expr, value);
1358
1359         return matcherprop;
1360 }
1361
1362 /*!
1363  *\brief        Signal handler for register button
1364  */
1365 static void prefs_matcher_register_cb(void)
1366 {
1367         MatcherProp *matcherprop;
1368         
1369         matcherprop = prefs_matcher_dialog_to_matcher();
1370         if (matcherprop == NULL)
1371                 return;
1372
1373         prefs_matcher_list_view_set_row(NULL, matcherprop);
1374
1375         matcherprop_free(matcherprop);
1376
1377         prefs_matcher_reset_condition();
1378 }
1379
1380 /*!
1381  *\brief        Signal handler for substitute button
1382  */
1383 static void prefs_matcher_substitute_cb(void)
1384 {
1385         MatcherProp *matcherprop;
1386         GtkTreeIter row;
1387         GtkTreeSelection *selection;
1388         GtkTreeModel *model;
1389         gboolean is_valid;
1390
1391         selection = gtk_tree_view_get_selection
1392                         (GTK_TREE_VIEW(matcher.cond_list_view));
1393         
1394         if (!gtk_tree_selection_get_selected(selection, &model, &row))
1395                 return;
1396         
1397         gtk_tree_model_get(model, &row, 
1398                            PREFS_MATCHER_COND_VALID, &is_valid,
1399                            -1);
1400         if (!is_valid)
1401                 return;
1402
1403         matcherprop = prefs_matcher_dialog_to_matcher();
1404         if (matcherprop == NULL)
1405                 return;
1406
1407         prefs_matcher_list_view_set_row(&row, matcherprop);
1408
1409         matcherprop_free(matcherprop);
1410
1411         prefs_matcher_reset_condition();
1412 }
1413
1414 /*!
1415  *\brief        Signal handler for delete button
1416  */
1417 static void prefs_matcher_delete_cb(void)
1418 {
1419         GtkTreeIter row;
1420         GtkTreeSelection *selection;
1421         GtkTreeModel *model;
1422         gboolean is_valid;
1423
1424         selection = gtk_tree_view_get_selection
1425                         (GTK_TREE_VIEW(matcher.cond_list_view));
1426         
1427         if (!gtk_tree_selection_get_selected(selection, &model, &row))
1428                 return;
1429                 
1430         gtk_tree_model_get(model, &row, 
1431                            PREFS_MATCHER_COND_VALID, &is_valid,
1432                            -1);
1433
1434         if (!is_valid)
1435                 return;
1436
1437         gtk_list_store_remove(GTK_LIST_STORE(model), &row);             
1438
1439         prefs_matcher_reset_condition();
1440 }
1441
1442 /*!
1443  *\brief        Signal handler for 'move up' button
1444  */
1445 static void prefs_matcher_up(void)
1446 {
1447         GtkTreePath *prev, *sel, *try;
1448         GtkTreeIter isel;
1449         GtkListStore *store = NULL;
1450         GtkTreeModel *model = NULL;
1451         GtkTreeIter iprev;
1452         
1453         if (!gtk_tree_selection_get_selected
1454                 (gtk_tree_view_get_selection
1455                         (GTK_TREE_VIEW(matcher.cond_list_view)),
1456                  &model,        
1457                  &isel))
1458                 return;
1459         store = (GtkListStore *)model;
1460         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
1461         if (!sel)
1462                 return;
1463         
1464         /* no move if we're at row 0 or 1, looks phony, but other
1465          * solutions are more convoluted... */
1466         try = gtk_tree_path_copy(sel);
1467         if (!gtk_tree_path_prev(try) || !gtk_tree_path_prev(try)) {
1468                 gtk_tree_path_free(try);
1469                 gtk_tree_path_free(sel);
1470                 return;
1471         }
1472         gtk_tree_path_free(try);
1473
1474         prev = gtk_tree_path_copy(sel);         
1475         if (gtk_tree_path_prev(prev)) {
1476                 gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
1477                                         &iprev, prev);
1478                 gtk_list_store_swap(store, &iprev, &isel);
1479                 /* XXX: GTK2 select row?? */
1480         }
1481
1482         gtk_tree_path_free(sel);
1483         gtk_tree_path_free(prev);
1484 }
1485
1486 /*!
1487  *\brief        Signal handler for 'move down' button
1488  */
1489 static void prefs_matcher_down(void)
1490 {
1491         GtkListStore *store = NULL;
1492         GtkTreeModel *model = NULL;
1493         GtkTreeIter next, sel;
1494         GtkTreePath *try;
1495         
1496         if (!gtk_tree_selection_get_selected
1497                 (gtk_tree_view_get_selection
1498                         (GTK_TREE_VIEW(matcher.cond_list_view)),
1499                  &model,
1500                  &sel))
1501                 return;
1502         store = (GtkListStore *)model;
1503         try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
1504         if (!try) 
1505                 return;
1506         
1507         /* move when not at row 0 ... */
1508         if (gtk_tree_path_prev(try)) {
1509                 next = sel;
1510                 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next))
1511                         gtk_list_store_swap(store, &next, &sel);
1512         }
1513                 
1514         gtk_tree_path_free(try);
1515 }
1516
1517 /*!
1518  *\brief        Helper function that allows us to replace the 'Value' entry box
1519  *              by another widget.
1520  *
1521  *\param        old_widget Widget that needs to be removed.
1522  *\param        new_widget Replacement widget
1523  */
1524 static void prefs_matcher_set_value_widget(GtkWidget *old_widget, 
1525                                            GtkWidget *new_widget)
1526 {
1527         /* TODO: find out why the following spews harmless "parent errors" */
1528
1529         /* NOTE: we first need to bump up the refcount of the old_widget,
1530          * because the gtkut_container_remove() will otherwise destroy it */
1531         gtk_widget_ref(old_widget);
1532         gtkut_container_remove(GTK_CONTAINER(matcher.criteria_table), old_widget);
1533         gtk_widget_show(new_widget);
1534         gtk_widget_set_size_request(new_widget, 200, -1);
1535         gtk_table_attach(GTK_TABLE(matcher.criteria_table), new_widget, 
1536                          2, 3, 1, 2, 
1537                          GTK_FILL | GTK_SHRINK | GTK_EXPAND, 
1538                          0, 0, 0);
1539 }
1540
1541 static void prefs_matcher_disable_widget(GtkWidget* widget)
1542 {
1543         g_return_if_fail( widget != NULL);
1544
1545         gtk_widget_set_sensitive(widget, FALSE);
1546         gtk_widget_hide(widget);
1547 }
1548
1549 static void prefs_matcher_enable_widget(GtkWidget* widget)
1550 {
1551         g_return_if_fail( widget != NULL);
1552
1553         gtk_widget_set_sensitive(widget, TRUE);
1554         gtk_widget_show(widget);
1555 }
1556
1557 /*!
1558  *\brief        Change widgets depending on the selected condition
1559  *
1560  *\param        list List widget
1561  *\param        widget Not used
1562  *\param        user_data Not used      
1563  */
1564 static void prefs_matcher_criteria_select(GtkList *list,
1565                                           GtkWidget *widget,
1566                                           gpointer user_data)
1567 {
1568         gint value, old_value;
1569
1570         old_value = matcher.selected_criteria;
1571         matcher.selected_criteria = value = get_sel_from_list
1572                 (GTK_LIST(matcher.criteria_list));
1573
1574         if (old_value == matcher.selected_criteria)
1575                 return;
1576
1577         /* CLAWS: the value widget is currently either the color label combo box,
1578          * or a GtkEntry, so kiss for now */
1579         if (matcher.selected_criteria == CRITERIA_COLORLABEL) { 
1580                 prefs_matcher_set_value_widget(matcher.value_entry, 
1581                                                matcher.color_optmenu);
1582         } else if (old_value == CRITERIA_COLORLABEL) {
1583                 prefs_matcher_set_value_widget(matcher.color_optmenu,
1584                                                matcher.value_entry);
1585         }                                              
1586
1587         switch (value) {
1588         case CRITERIA_ALL:
1589                 prefs_matcher_disable_widget(matcher.header_combo);
1590                 prefs_matcher_disable_widget(matcher.header_label);
1591                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1592                 prefs_matcher_disable_widget(matcher.header_addr_label);
1593                 prefs_matcher_disable_widget(matcher.value_label);
1594                 prefs_matcher_disable_widget(matcher.value_entry);
1595                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1596                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1597                 prefs_matcher_disable_widget(matcher.predicate_label);
1598                 prefs_matcher_disable_widget(matcher.predicate_combo);
1599                 prefs_matcher_disable_widget(matcher.predicate_flag_combo);
1600                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1601                 prefs_matcher_disable_widget(matcher.regexp_chkbtn);
1602                 prefs_matcher_disable_widget(matcher.test_btn);
1603                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1604                 break;
1605
1606         case CRITERIA_UNREAD:
1607         case CRITERIA_NEW:
1608         case CRITERIA_MARKED:
1609         case CRITERIA_DELETED:
1610         case CRITERIA_REPLIED:
1611         case CRITERIA_FORWARDED:
1612         case CRITERIA_LOCKED:
1613         case CRITERIA_PARTIAL:
1614         case CRITERIA_IGNORE_THREAD:
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_disable_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_COLORLABEL:
1633                 prefs_matcher_disable_widget(matcher.header_combo);
1634                 prefs_matcher_disable_widget(matcher.header_label);
1635                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1636                 prefs_matcher_disable_widget(matcher.header_addr_label);
1637                 prefs_matcher_enable_widget(matcher.value_label);
1638                 prefs_matcher_disable_widget(matcher.value_entry);
1639                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1640                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1641                 prefs_matcher_enable_widget(matcher.predicate_label);
1642                 prefs_matcher_disable_widget(matcher.predicate_combo);
1643                 prefs_matcher_enable_widget(matcher.predicate_flag_combo);
1644                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1645                 prefs_matcher_disable_widget(matcher.regexp_chkbtn);
1646                 prefs_matcher_disable_widget(matcher.test_btn);
1647                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1648                 break;
1649
1650         case CRITERIA_SUBJECT:
1651         case CRITERIA_FROM:
1652         case CRITERIA_TO:
1653         case CRITERIA_CC:
1654         case CRITERIA_TO_OR_CC:
1655         case CRITERIA_NEWSGROUPS:
1656         case CRITERIA_INREPLYTO:
1657         case CRITERIA_REFERENCES:
1658         case CRITERIA_HEADERS_PART:
1659         case CRITERIA_BODY_PART:
1660         case CRITERIA_MESSAGE:
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_enable_widget(matcher.predicate_combo);
1671                 prefs_matcher_disable_widget(matcher.predicate_flag_combo);
1672                 prefs_matcher_enable_widget(matcher.case_chkbtn);
1673                 prefs_matcher_enable_widget(matcher.regexp_chkbtn);
1674                 prefs_matcher_disable_widget(matcher.test_btn);
1675                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1676                 break;
1677
1678         case CRITERIA_TEST:
1679                 prefs_matcher_disable_widget(matcher.header_combo);
1680                 prefs_matcher_disable_widget(matcher.header_label);
1681                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1682                 prefs_matcher_disable_widget(matcher.header_addr_label);
1683                 prefs_matcher_enable_widget(matcher.value_label);
1684                 prefs_matcher_enable_widget(matcher.value_entry);
1685                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1686                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1687                 prefs_matcher_enable_widget(matcher.predicate_label);
1688                 prefs_matcher_disable_widget(matcher.predicate_combo);
1689                 prefs_matcher_enable_widget(matcher.predicate_flag_combo);
1690                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1691                 prefs_matcher_disable_widget(matcher.regexp_chkbtn);
1692                 prefs_matcher_enable_widget(matcher.test_btn);
1693                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1694                 break;
1695
1696         case CRITERIA_AGE_GREATER:
1697         case CRITERIA_AGE_LOWER:
1698         case CRITERIA_SCORE_GREATER:
1699         case CRITERIA_SCORE_LOWER:
1700         case CRITERIA_SCORE_EQUAL:
1701         case CRITERIA_SIZE_GREATER:
1702         case CRITERIA_SIZE_SMALLER:
1703         case CRITERIA_SIZE_EQUAL:
1704                 prefs_matcher_disable_widget(matcher.header_combo);
1705                 prefs_matcher_disable_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_disable_widget(matcher.predicate_label);
1713                 prefs_matcher_disable_widget(matcher.predicate_combo);
1714                 prefs_matcher_disable_widget(matcher.predicate_flag_combo);
1715                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1716                 prefs_matcher_disable_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_HEADER:
1722                 prefs_matcher_enable_widget(matcher.header_combo);
1723                 prefs_matcher_enable_widget(matcher.header_label);
1724                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1725                 prefs_matcher_disable_widget(matcher.header_addr_label);
1726                 prefs_matcher_enable_widget(matcher.value_label);
1727                 prefs_matcher_enable_widget(matcher.value_entry);
1728                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1729                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1730                 prefs_matcher_enable_widget(matcher.predicate_label);
1731                 prefs_matcher_enable_widget(matcher.predicate_combo);
1732                 prefs_matcher_disable_widget(matcher.predicate_flag_combo);
1733                 prefs_matcher_enable_widget(matcher.case_chkbtn);
1734                 prefs_matcher_enable_widget(matcher.regexp_chkbtn);
1735                 prefs_matcher_disable_widget(matcher.test_btn);
1736                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1737                 break;
1738
1739         case CRITERIA_FOUND_IN_ADDRESSBOOK:
1740                 prefs_matcher_disable_widget(matcher.header_combo);
1741                 prefs_matcher_disable_widget(matcher.header_label);
1742                 prefs_matcher_enable_widget(matcher.header_addr_combo);
1743                 prefs_matcher_enable_widget(matcher.header_addr_label);
1744                 prefs_matcher_disable_widget(matcher.value_label);
1745                 prefs_matcher_disable_widget(matcher.value_entry);
1746                 prefs_matcher_enable_widget(matcher.addressbook_folder_label);
1747                 prefs_matcher_enable_widget(matcher.addressbook_folder_combo);
1748                 prefs_matcher_enable_widget(matcher.predicate_label);
1749                 prefs_matcher_disable_widget(matcher.predicate_combo);
1750                 prefs_matcher_enable_widget(matcher.predicate_flag_combo);
1751                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1752                 prefs_matcher_disable_widget(matcher.regexp_chkbtn);
1753                 prefs_matcher_disable_widget(matcher.test_btn);
1754                 prefs_matcher_enable_widget(matcher.addressbook_select_btn);
1755                 break;
1756         }
1757 }
1758
1759 /*!
1760  *\brief        Handle key press
1761  *
1762  *\param        widget Widget receiving key press
1763  *\param        event Key event
1764  *\param        data User data
1765  */
1766 static gboolean prefs_matcher_key_pressed(GtkWidget *widget, GdkEventKey *event,
1767                                      gpointer data)
1768 {
1769         if (event && event->keyval == GDK_Escape) {
1770                 prefs_matcher_cancel();
1771                 return TRUE;            
1772         }
1773         return FALSE;
1774 }
1775
1776 /*!
1777  *\brief        Cancel matcher dialog
1778  */
1779 static void prefs_matcher_cancel(void)
1780 {
1781         gtk_widget_hide(matcher.window);
1782         inc_unlock();
1783 }
1784
1785 /*!
1786  *\brief        Accept current matchers
1787  */
1788 static void prefs_matcher_ok(void)
1789 {
1790         MatcherList *matchers;
1791         MatcherProp *matcherprop;
1792         AlertValue val;
1793         gchar *matcher_str = NULL;
1794         gchar *str = NULL;
1795         gint row = 1;
1796         GtkTreeModel *model;
1797         GtkTreeIter iter;
1798
1799         matchers = prefs_matcher_get_list();
1800
1801         if (matchers != NULL) {
1802                 matcherprop = prefs_matcher_dialog_to_matcher();
1803                 if (matcherprop != NULL) {
1804                         str = matcherprop_to_string(matcherprop);
1805                         matcherprop_free(matcherprop);
1806                         if (strcmp(str, "all") != 0) {
1807                                 model = gtk_tree_view_get_model(GTK_TREE_VIEW
1808                                                 (matcher.cond_list_view));
1809
1810                                 while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1811                                         gtk_tree_model_get(model, &iter,
1812                                                            PREFS_MATCHER_COND, &matcher_str,
1813                                                            -1);
1814                                         if (matcher_str && strcmp(matcher_str, str) == 0) 
1815                                                 break;
1816                                         row++;
1817                                         g_free(matcher_str);
1818                                         matcher_str = NULL;
1819                                 }
1820
1821                                 if (!matcher_str || strcmp(matcher_str, str) != 0) {
1822                                         val = alertpanel(_("Entry not saved"),
1823                                                  _("The entry was not saved.\nClose anyway?"),
1824                                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1825                                         if (G_ALERTDEFAULT != val) {
1826                                                 g_free(matcher_str);                                             
1827                                                 g_free(str);
1828                                                 return;
1829                                         }
1830                                 }
1831                                 g_free(matcher_str);
1832                         }
1833                 }
1834                 g_free(str);
1835                 gtk_widget_hide(matcher.window);
1836                 inc_unlock();
1837                 if (matchers_callback != NULL)
1838                         matchers_callback(matchers);
1839                 matcherlist_free(matchers);
1840         }
1841 }
1842
1843 /*!
1844  *\brief        Called when closing dialog box
1845  *
1846  *\param        widget Dialog widget
1847  *\param        event Event info
1848  *\param        data User data
1849  *
1850  *\return       gint TRUE
1851  */
1852 static gint prefs_matcher_deleted(GtkWidget *widget, GdkEventAny *event,
1853                                   gpointer data)
1854 {
1855         prefs_matcher_cancel();
1856         return TRUE;
1857 }
1858
1859 /*
1860  * Strings describing test format strings
1861  * 
1862  * When adding new lines, remember to put 2 strings for each line
1863  */
1864 static gchar *test_desc_strings[] = {
1865         "%%",   N_("literal %"),
1866         "%s",   N_("Subject"),
1867         "%f",   N_("From"),
1868         "%t",   N_("To"),
1869         "%c",   N_("Cc"),
1870         "%d",   N_("Date"),
1871         "%i",   N_("Message-ID"),
1872         "%n",   N_("Newsgroups"),
1873         "%r",   N_("References"),
1874         "%F",   N_("filename (should not be modified)"),
1875         "\\n",  N_("new line"),
1876         "\\",   N_("escape character for quotes"),
1877         "\\\"", N_("quote character"),
1878         NULL,   NULL
1879 };
1880
1881 static DescriptionWindow test_desc_win = { 
1882         NULL,
1883         NULL, 
1884         2,
1885         N_("Match Type: 'Test'"),
1886         N_("'Test' allows you to test a message or message element "
1887            "using an external program or script. The program will "
1888            "return either 0 or 1.\n\n"
1889            "The following symbols can be used:"),
1890         test_desc_strings
1891 };
1892
1893
1894
1895 /*!
1896  *\brief        Show Test action's info
1897  */
1898 void prefs_matcher_test_info(void)
1899 {
1900         description_window_create(&test_desc_win);
1901 }
1902
1903 void prefs_matcher_addressbook_select(void)
1904 {
1905         gchar *folderpath = NULL;
1906         gboolean ret = FALSE;
1907
1908         folderpath = (gchar *) gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry));
1909         ret = addressbook_folder_selection(&folderpath);
1910         if ( ret != FALSE && folderpath != NULL)
1911                 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry), folderpath);
1912 }
1913
1914
1915 /*
1916  * list view
1917  */
1918
1919 static GtkListStore* prefs_matcher_create_data_store(void)
1920 {
1921         return gtk_list_store_new(N_PREFS_MATCHER_COLUMNS,
1922                                   G_TYPE_STRING,        
1923                                   G_TYPE_BOOLEAN,
1924                                   -1);
1925 }
1926
1927 static void prefs_matcher_list_view_insert_matcher(GtkWidget *list_view,
1928                                                    GtkTreeIter *row_iter,
1929                                                    const gchar *matcher,
1930                                                    gboolean is_valid) 
1931 {
1932         GtkTreeIter iter;
1933         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
1934                                         (GTK_TREE_VIEW(list_view)));
1935
1936         if (row_iter == NULL) {
1937                 /* append new */
1938                 gtk_list_store_append(list_store, &iter);
1939         } else {
1940                 /* change existing */
1941                 iter = *row_iter;
1942         }
1943
1944         gtk_list_store_set(list_store, &iter,
1945                            PREFS_MATCHER_COND, matcher,
1946                            PREFS_MATCHER_COND_VALID, is_valid,
1947                            -1);
1948 }
1949
1950 static GtkWidget *prefs_matcher_list_view_create(void)
1951 {
1952         GtkTreeView *list_view;
1953         GtkTreeSelection *selector;
1954         GtkTreeModel *model;
1955
1956         model = GTK_TREE_MODEL(prefs_matcher_create_data_store());
1957         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1958         g_object_unref(model);  
1959         
1960         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1961         
1962         selector = gtk_tree_view_get_selection(list_view);
1963         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1964         gtk_tree_selection_set_select_function(selector, prefs_matcher_selected,
1965                                                NULL, NULL);
1966
1967         /* create the columns */
1968         prefs_matcher_create_list_view_columns(GTK_WIDGET(list_view));
1969
1970         return GTK_WIDGET(list_view);
1971 }
1972
1973 static void prefs_matcher_create_list_view_columns(GtkWidget *list_view)
1974 {
1975         GtkTreeViewColumn *column;
1976         GtkCellRenderer *renderer;
1977
1978         renderer = gtk_cell_renderer_text_new();
1979         column = gtk_tree_view_column_new_with_attributes
1980                 (_("Current condition rules"),
1981                  renderer,
1982                  "text", PREFS_MATCHER_COND,
1983                  NULL);
1984         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1985 }
1986
1987 static gboolean prefs_matcher_selected(GtkTreeSelection *selector,
1988                                        GtkTreeModel *model, 
1989                                        GtkTreePath *path,
1990                                        gboolean currently_selected,
1991                                        gpointer data)
1992 {
1993         gchar *matcher_str;
1994         MatcherProp *prop;
1995         gboolean negative_cond;
1996         gint criteria;
1997         GtkTreeIter iter;
1998         gboolean is_valid;
1999
2000         if (currently_selected)
2001                 return TRUE;
2002
2003         if (!gtk_tree_model_get_iter(model, &iter, path))
2004                 return TRUE;
2005
2006         gtk_tree_model_get(model, &iter, 
2007                            PREFS_MATCHER_COND_VALID,  &is_valid,
2008                            PREFS_MATCHER_COND, &matcher_str,
2009                            -1);
2010         
2011         if (!is_valid) {
2012                 g_free(matcher_str);
2013                 prefs_matcher_reset_condition();
2014                 return TRUE;
2015         }
2016
2017         negative_cond = FALSE;
2018
2019         prop = matcher_parser_get_prop(matcher_str);
2020         if (prop == NULL) {
2021                 g_free(matcher_str);
2022                 return TRUE;
2023         }               
2024
2025         criteria = prefs_matcher_get_criteria_from_matching(prop->criteria);
2026         if (criteria != -1)
2027                 gtk_list_select_item(GTK_LIST(matcher.criteria_list),
2028                                      criteria);
2029
2030         switch(prop->criteria) {
2031         case MATCHCRITERIA_NOT_UNREAD:
2032         case MATCHCRITERIA_NOT_NEW:
2033         case MATCHCRITERIA_NOT_MARKED:
2034         case MATCHCRITERIA_NOT_DELETED:
2035         case MATCHCRITERIA_NOT_REPLIED:
2036         case MATCHCRITERIA_NOT_FORWARDED:
2037         case MATCHCRITERIA_NOT_LOCKED:
2038         case MATCHCRITERIA_NOT_PARTIAL:
2039         case MATCHCRITERIA_NOT_COLORLABEL:
2040         case MATCHCRITERIA_NOT_IGNORE_THREAD:
2041         case MATCHCRITERIA_NOT_SUBJECT:
2042         case MATCHCRITERIA_NOT_FROM:
2043         case MATCHCRITERIA_NOT_TO:
2044         case MATCHCRITERIA_NOT_CC:
2045         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
2046         case MATCHCRITERIA_NOT_NEWSGROUPS:
2047         case MATCHCRITERIA_NOT_INREPLYTO:
2048         case MATCHCRITERIA_NOT_REFERENCES:
2049         case MATCHCRITERIA_NOT_HEADER:
2050         case MATCHCRITERIA_NOT_HEADERS_PART:
2051         case MATCHCRITERIA_NOT_MESSAGE:
2052         case MATCHCRITERIA_NOT_BODY_PART:
2053         case MATCHCRITERIA_NOT_TEST:
2054         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
2055                 negative_cond = TRUE;
2056                 break;
2057         }
2058         
2059         switch(prop->criteria) {
2060         case MATCHCRITERIA_ALL:
2061                 break;
2062
2063         case MATCHCRITERIA_NOT_SUBJECT:
2064         case MATCHCRITERIA_NOT_FROM:
2065         case MATCHCRITERIA_NOT_TO:
2066         case MATCHCRITERIA_NOT_CC:
2067         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
2068         case MATCHCRITERIA_NOT_NEWSGROUPS:
2069         case MATCHCRITERIA_NOT_INREPLYTO:
2070         case MATCHCRITERIA_NOT_REFERENCES:
2071         case MATCHCRITERIA_NOT_HEADERS_PART:
2072         case MATCHCRITERIA_NOT_BODY_PART:
2073         case MATCHCRITERIA_NOT_MESSAGE:
2074         case MATCHCRITERIA_NOT_TEST:
2075         case MATCHCRITERIA_SUBJECT:
2076         case MATCHCRITERIA_FROM:
2077         case MATCHCRITERIA_TO:
2078         case MATCHCRITERIA_CC:
2079         case MATCHCRITERIA_TO_OR_CC:
2080         case MATCHCRITERIA_NEWSGROUPS:
2081         case MATCHCRITERIA_INREPLYTO:
2082         case MATCHCRITERIA_REFERENCES:
2083         case MATCHCRITERIA_HEADERS_PART:
2084         case MATCHCRITERIA_BODY_PART:
2085         case MATCHCRITERIA_MESSAGE:
2086         case MATCHCRITERIA_TEST:
2087                 gtk_entry_set_text(GTK_ENTRY(matcher.value_entry), prop->expr);
2088                 break;
2089
2090         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
2091         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
2092         {
2093                 gchar *header;
2094                 gchar *expr;
2095
2096                 /* matcher expressions contain UNtranslated "Any"/"All",
2097                   select the relevant translated combo item */
2098                 if (strcasecmp(prop->header, "All") == 0)
2099                         header = Q_("Filtering Matcher Menu|All");
2100                 else
2101                         if (strcasecmp(prop->header, "Any") == 0)
2102                                 header = _("Any");
2103                         else
2104                                 header = prop->header;
2105                 if (strcasecmp(prop->expr, "Any") == 0)
2106                         expr = _("Any");
2107                 else
2108                         expr = prop->expr;
2109
2110                 gtk_entry_set_text(GTK_ENTRY(matcher.header_addr_entry), header);
2111                 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry), expr);
2112                 break;
2113         }
2114
2115         case MATCHCRITERIA_AGE_GREATER:
2116         case MATCHCRITERIA_AGE_LOWER:
2117         case MATCHCRITERIA_SCORE_GREATER:
2118         case MATCHCRITERIA_SCORE_LOWER:
2119         case MATCHCRITERIA_SCORE_EQUAL:
2120         case MATCHCRITERIA_SIZE_GREATER:
2121         case MATCHCRITERIA_SIZE_SMALLER:
2122         case MATCHCRITERIA_SIZE_EQUAL:
2123                 gtk_entry_set_text(GTK_ENTRY(matcher.value_entry), itos(prop->value));
2124                 break;
2125
2126         case MATCHCRITERIA_NOT_COLORLABEL:
2127         case MATCHCRITERIA_COLORLABEL:
2128                 gtk_option_menu_set_history(GTK_OPTION_MENU(matcher.color_optmenu),
2129                                             prop->value);
2130                 break;
2131
2132         case MATCHCRITERIA_NOT_HEADER:
2133         case MATCHCRITERIA_HEADER:
2134                 gtk_entry_set_text(GTK_ENTRY(matcher.header_entry), prop->header);
2135                 gtk_entry_set_text(GTK_ENTRY(matcher.value_entry), prop->expr);
2136                 break;
2137         }
2138
2139         if (negative_cond) {
2140                 gtk_list_select_item(GTK_LIST(matcher.predicate_list), 1);
2141                 gtk_list_select_item(GTK_LIST(matcher.predicate_flag_list), 1);
2142         } else {
2143                 gtk_list_select_item(GTK_LIST(matcher.predicate_list), 0);
2144                 gtk_list_select_item(GTK_LIST(matcher.predicate_flag_list), 0);
2145         }
2146         
2147         switch(prop->matchtype) {
2148         case MATCHTYPE_MATCH:
2149                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_chkbtn), FALSE);
2150                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_chkbtn), TRUE);
2151                 break;
2152
2153         case MATCHTYPE_MATCHCASE:
2154                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_chkbtn), FALSE);
2155                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_chkbtn), FALSE);
2156                 break;
2157
2158         case MATCHTYPE_REGEXP:
2159                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_chkbtn), TRUE);
2160                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_chkbtn), TRUE);
2161                 break;
2162
2163         case MATCHTYPE_REGEXPCASE:
2164                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_chkbtn), TRUE);
2165                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_chkbtn), FALSE);
2166                 break;
2167         }
2168
2169         g_free(matcher_str);
2170         return TRUE;
2171 }
2172