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