a20b5b60502cd3eb73a4c988df5ddcd09a34d741
[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
1328                         msg = g_strdup_printf(_("Book/folder path is not set.\n\n"
1329                                                                 "If you want to match the '%s' address against the whole address book, "
1330                                                                 "you have to select 'Any' from the book/folder drop-down list."),
1331                                                                 header);
1332                     alertpanel_error(msg);
1333                         g_free(msg);
1334                     return NULL;
1335                 }
1336                 break;
1337         }
1338
1339         matcherprop = matcherprop_new(criteria, header, matchtype,
1340                                       expr, value);
1341
1342         return matcherprop;
1343 }
1344
1345 /*!
1346  *\brief        Signal handler for register button
1347  */
1348 static void prefs_matcher_register_cb(void)
1349 {
1350         MatcherProp *matcherprop;
1351         
1352         matcherprop = prefs_matcher_dialog_to_matcher();
1353         if (matcherprop == NULL)
1354                 return;
1355
1356         prefs_matcher_list_view_set_row(NULL, matcherprop);
1357
1358         matcherprop_free(matcherprop);
1359
1360         prefs_matcher_reset_condition();
1361 }
1362
1363 /*!
1364  *\brief        Signal handler for substitute button
1365  */
1366 static void prefs_matcher_substitute_cb(void)
1367 {
1368         MatcherProp *matcherprop;
1369         GtkTreeIter row;
1370         GtkTreeSelection *selection;
1371         GtkTreeModel *model;
1372         gboolean is_valid;
1373
1374         selection = gtk_tree_view_get_selection
1375                         (GTK_TREE_VIEW(matcher.cond_list_view));
1376         
1377         if (!gtk_tree_selection_get_selected(selection, &model, &row))
1378                 return;
1379         
1380         gtk_tree_model_get(model, &row, 
1381                            PREFS_MATCHER_COND_VALID, &is_valid,
1382                            -1);
1383         if (!is_valid)
1384                 return;
1385
1386         matcherprop = prefs_matcher_dialog_to_matcher();
1387         if (matcherprop == NULL)
1388                 return;
1389
1390         prefs_matcher_list_view_set_row(&row, matcherprop);
1391
1392         matcherprop_free(matcherprop);
1393
1394         prefs_matcher_reset_condition();
1395 }
1396
1397 /*!
1398  *\brief        Signal handler for delete button
1399  */
1400 static void prefs_matcher_delete_cb(void)
1401 {
1402         GtkTreeIter row;
1403         GtkTreeSelection *selection;
1404         GtkTreeModel *model;
1405         gboolean is_valid;
1406
1407         selection = gtk_tree_view_get_selection
1408                         (GTK_TREE_VIEW(matcher.cond_list_view));
1409         
1410         if (!gtk_tree_selection_get_selected(selection, &model, &row))
1411                 return;
1412                 
1413         gtk_tree_model_get(model, &row, 
1414                            PREFS_MATCHER_COND_VALID, &is_valid,
1415                            -1);
1416
1417         if (!is_valid)
1418                 return;
1419
1420         gtk_list_store_remove(GTK_LIST_STORE(model), &row);             
1421
1422         prefs_matcher_reset_condition();
1423 }
1424
1425 /*!
1426  *\brief        Signal handler for 'move up' button
1427  */
1428 static void prefs_matcher_up(void)
1429 {
1430         GtkTreePath *prev, *sel, *try;
1431         GtkTreeIter isel;
1432         GtkListStore *store = NULL;
1433         GtkTreeModel *model = NULL;
1434         GtkTreeIter iprev;
1435         
1436         if (!gtk_tree_selection_get_selected
1437                 (gtk_tree_view_get_selection
1438                         (GTK_TREE_VIEW(matcher.cond_list_view)),
1439                  &model,        
1440                  &isel))
1441                 return;
1442         store = (GtkListStore *)model;
1443         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
1444         if (!sel)
1445                 return;
1446         
1447         /* no move if we're at row 0 or 1, looks phony, but other
1448          * solutions are more convoluted... */
1449         try = gtk_tree_path_copy(sel);
1450         if (!gtk_tree_path_prev(try) || !gtk_tree_path_prev(try)) {
1451                 gtk_tree_path_free(try);
1452                 gtk_tree_path_free(sel);
1453                 return;
1454         }
1455         gtk_tree_path_free(try);
1456
1457         prev = gtk_tree_path_copy(sel);         
1458         if (gtk_tree_path_prev(prev)) {
1459                 gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
1460                                         &iprev, prev);
1461                 gtk_list_store_swap(store, &iprev, &isel);
1462                 /* XXX: GTK2 select row?? */
1463         }
1464
1465         gtk_tree_path_free(sel);
1466         gtk_tree_path_free(prev);
1467 }
1468
1469 /*!
1470  *\brief        Signal handler for 'move down' button
1471  */
1472 static void prefs_matcher_down(void)
1473 {
1474         GtkListStore *store = NULL;
1475         GtkTreeModel *model = NULL;
1476         GtkTreeIter next, sel;
1477         GtkTreePath *try;
1478         
1479         if (!gtk_tree_selection_get_selected
1480                 (gtk_tree_view_get_selection
1481                         (GTK_TREE_VIEW(matcher.cond_list_view)),
1482                  &model,
1483                  &sel))
1484                 return;
1485         store = (GtkListStore *)model;
1486         try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
1487         if (!try) 
1488                 return;
1489         
1490         /* move when not at row 0 ... */
1491         if (gtk_tree_path_prev(try)) {
1492                 next = sel;
1493                 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next))
1494                         gtk_list_store_swap(store, &next, &sel);
1495         }
1496                 
1497         gtk_tree_path_free(try);
1498 }
1499
1500 /*!
1501  *\brief        Helper function that allows us to replace the 'Value' entry box
1502  *              by another widget.
1503  *
1504  *\param        old_widget Widget that needs to be removed.
1505  *\param        new_widget Replacement widget
1506  */
1507 static void prefs_matcher_set_value_widget(GtkWidget *old_widget, 
1508                                            GtkWidget *new_widget)
1509 {
1510         /* TODO: find out why the following spews harmless "parent errors" */
1511
1512         /* NOTE: we first need to bump up the refcount of the old_widget,
1513          * because the gtkut_container_remove() will otherwise destroy it */
1514         gtk_widget_ref(old_widget);
1515         gtkut_container_remove(GTK_CONTAINER(matcher.criteria_table), old_widget);
1516         gtk_widget_show(new_widget);
1517         gtk_widget_set_size_request(new_widget, 200, -1);
1518         gtk_table_attach(GTK_TABLE(matcher.criteria_table), new_widget, 
1519                          2, 3, 1, 2, 
1520                          GTK_FILL | GTK_SHRINK | GTK_EXPAND, 
1521                          0, 0, 0);
1522 }
1523
1524 static void prefs_matcher_disable_widget(GtkWidget* widget)
1525 {
1526         g_return_if_fail( widget != NULL);
1527
1528         gtk_widget_set_sensitive(widget, FALSE);
1529         gtk_widget_hide(widget);
1530 }
1531
1532 static void prefs_matcher_enable_widget(GtkWidget* widget)
1533 {
1534         g_return_if_fail( widget != NULL);
1535
1536         gtk_widget_set_sensitive(widget, TRUE);
1537         gtk_widget_show(widget);
1538 }
1539
1540 /*!
1541  *\brief        Change widgets depending on the selected condition
1542  *
1543  *\param        list List widget
1544  *\param        widget Not used
1545  *\param        user_data Not used      
1546  */
1547 static void prefs_matcher_criteria_select(GtkList *list,
1548                                           GtkWidget *widget,
1549                                           gpointer user_data)
1550 {
1551         gint value, old_value;
1552
1553         old_value = matcher.selected_criteria;
1554         matcher.selected_criteria = value = get_sel_from_list
1555                 (GTK_LIST(matcher.criteria_list));
1556
1557         if (old_value == matcher.selected_criteria)
1558                 return;
1559
1560         /* CLAWS: the value widget is currently either the color label combo box,
1561          * or a GtkEntry, so kiss for now */
1562         if (matcher.selected_criteria == CRITERIA_COLORLABEL) { 
1563                 prefs_matcher_set_value_widget(matcher.value_entry, 
1564                                                matcher.color_optmenu);
1565         } else if (old_value == CRITERIA_COLORLABEL) {
1566                 prefs_matcher_set_value_widget(matcher.color_optmenu,
1567                                                matcher.value_entry);
1568         }                                              
1569
1570         switch (value) {
1571         case CRITERIA_ALL:
1572                 prefs_matcher_disable_widget(matcher.header_combo);
1573                 prefs_matcher_disable_widget(matcher.header_label);
1574                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1575                 prefs_matcher_disable_widget(matcher.header_addr_label);
1576                 prefs_matcher_disable_widget(matcher.value_label);
1577                 prefs_matcher_disable_widget(matcher.value_entry);
1578                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1579                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1580                 prefs_matcher_disable_widget(matcher.predicate_label);
1581                 prefs_matcher_disable_widget(matcher.predicate_combo);
1582                 prefs_matcher_disable_widget(matcher.predicate_flag_combo);
1583                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1584                 prefs_matcher_disable_widget(matcher.regexp_chkbtn);
1585                 prefs_matcher_disable_widget(matcher.test_btn);
1586                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1587                 break;
1588
1589         case CRITERIA_UNREAD:
1590         case CRITERIA_NEW:
1591         case CRITERIA_MARKED:
1592         case CRITERIA_DELETED:
1593         case CRITERIA_REPLIED:
1594         case CRITERIA_FORWARDED:
1595         case CRITERIA_LOCKED:
1596         case CRITERIA_PARTIAL:
1597         case CRITERIA_IGNORE_THREAD:
1598                 prefs_matcher_disable_widget(matcher.header_combo);
1599                 prefs_matcher_disable_widget(matcher.header_label);
1600                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1601                 prefs_matcher_disable_widget(matcher.header_addr_label);
1602                 prefs_matcher_disable_widget(matcher.value_label);
1603                 prefs_matcher_disable_widget(matcher.value_entry);
1604                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1605                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1606                 prefs_matcher_enable_widget(matcher.predicate_label);
1607                 prefs_matcher_disable_widget(matcher.predicate_combo);
1608                 prefs_matcher_enable_widget(matcher.predicate_flag_combo);
1609                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1610                 prefs_matcher_disable_widget(matcher.regexp_chkbtn);
1611                 prefs_matcher_disable_widget(matcher.test_btn);
1612                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1613                 break;
1614                 
1615         case CRITERIA_COLORLABEL:
1616                 prefs_matcher_disable_widget(matcher.header_combo);
1617                 prefs_matcher_disable_widget(matcher.header_label);
1618                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1619                 prefs_matcher_disable_widget(matcher.header_addr_label);
1620                 prefs_matcher_enable_widget(matcher.value_label);
1621                 prefs_matcher_disable_widget(matcher.value_entry);
1622                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1623                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1624                 prefs_matcher_enable_widget(matcher.predicate_label);
1625                 prefs_matcher_disable_widget(matcher.predicate_combo);
1626                 prefs_matcher_enable_widget(matcher.predicate_flag_combo);
1627                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1628                 prefs_matcher_disable_widget(matcher.regexp_chkbtn);
1629                 prefs_matcher_disable_widget(matcher.test_btn);
1630                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1631                 break;
1632
1633         case CRITERIA_SUBJECT:
1634         case CRITERIA_FROM:
1635         case CRITERIA_TO:
1636         case CRITERIA_CC:
1637         case CRITERIA_TO_OR_CC:
1638         case CRITERIA_NEWSGROUPS:
1639         case CRITERIA_INREPLYTO:
1640         case CRITERIA_REFERENCES:
1641         case CRITERIA_HEADERS_PART:
1642         case CRITERIA_BODY_PART:
1643         case CRITERIA_MESSAGE:
1644                 prefs_matcher_disable_widget(matcher.header_combo);
1645                 prefs_matcher_disable_widget(matcher.header_label);
1646                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1647                 prefs_matcher_disable_widget(matcher.header_addr_label);
1648                 prefs_matcher_enable_widget(matcher.value_label);
1649                 prefs_matcher_enable_widget(matcher.value_entry);
1650                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1651                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1652                 prefs_matcher_enable_widget(matcher.predicate_label);
1653                 prefs_matcher_enable_widget(matcher.predicate_combo);
1654                 prefs_matcher_disable_widget(matcher.predicate_flag_combo);
1655                 prefs_matcher_enable_widget(matcher.case_chkbtn);
1656                 prefs_matcher_enable_widget(matcher.regexp_chkbtn);
1657                 prefs_matcher_disable_widget(matcher.test_btn);
1658                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1659                 break;
1660
1661         case CRITERIA_TEST:
1662                 prefs_matcher_disable_widget(matcher.header_combo);
1663                 prefs_matcher_disable_widget(matcher.header_label);
1664                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1665                 prefs_matcher_disable_widget(matcher.header_addr_label);
1666                 prefs_matcher_enable_widget(matcher.value_label);
1667                 prefs_matcher_enable_widget(matcher.value_entry);
1668                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1669                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1670                 prefs_matcher_enable_widget(matcher.predicate_label);
1671                 prefs_matcher_disable_widget(matcher.predicate_combo);
1672                 prefs_matcher_enable_widget(matcher.predicate_flag_combo);
1673                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1674                 prefs_matcher_disable_widget(matcher.regexp_chkbtn);
1675                 prefs_matcher_enable_widget(matcher.test_btn);
1676                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1677                 break;
1678
1679         case CRITERIA_AGE_GREATER:
1680         case CRITERIA_AGE_LOWER:
1681         case CRITERIA_SCORE_GREATER:
1682         case CRITERIA_SCORE_LOWER:
1683         case CRITERIA_SCORE_EQUAL:
1684         case CRITERIA_SIZE_GREATER:
1685         case CRITERIA_SIZE_SMALLER:
1686         case CRITERIA_SIZE_EQUAL:
1687                 prefs_matcher_disable_widget(matcher.header_combo);
1688                 prefs_matcher_disable_widget(matcher.header_label);
1689                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1690                 prefs_matcher_disable_widget(matcher.header_addr_label);
1691                 prefs_matcher_enable_widget(matcher.value_label);
1692                 prefs_matcher_enable_widget(matcher.value_entry);
1693                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1694                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1695                 prefs_matcher_disable_widget(matcher.predicate_label);
1696                 prefs_matcher_disable_widget(matcher.predicate_combo);
1697                 prefs_matcher_disable_widget(matcher.predicate_flag_combo);
1698                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1699                 prefs_matcher_disable_widget(matcher.regexp_chkbtn);
1700                 prefs_matcher_disable_widget(matcher.test_btn);
1701                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1702                 break;
1703
1704         case CRITERIA_HEADER:
1705                 prefs_matcher_enable_widget(matcher.header_combo);
1706                 prefs_matcher_enable_widget(matcher.header_label);
1707                 prefs_matcher_disable_widget(matcher.header_addr_combo);
1708                 prefs_matcher_disable_widget(matcher.header_addr_label);
1709                 prefs_matcher_enable_widget(matcher.value_label);
1710                 prefs_matcher_enable_widget(matcher.value_entry);
1711                 prefs_matcher_disable_widget(matcher.addressbook_folder_label);
1712                 prefs_matcher_disable_widget(matcher.addressbook_folder_combo);
1713                 prefs_matcher_enable_widget(matcher.predicate_label);
1714                 prefs_matcher_enable_widget(matcher.predicate_combo);
1715                 prefs_matcher_disable_widget(matcher.predicate_flag_combo);
1716                 prefs_matcher_enable_widget(matcher.case_chkbtn);
1717                 prefs_matcher_enable_widget(matcher.regexp_chkbtn);
1718                 prefs_matcher_disable_widget(matcher.test_btn);
1719                 prefs_matcher_disable_widget(matcher.addressbook_select_btn);
1720                 break;
1721
1722         case CRITERIA_FOUND_IN_ADDRESSBOOK:
1723                 prefs_matcher_disable_widget(matcher.header_combo);
1724                 prefs_matcher_disable_widget(matcher.header_label);
1725                 prefs_matcher_enable_widget(matcher.header_addr_combo);
1726                 prefs_matcher_enable_widget(matcher.header_addr_label);
1727                 prefs_matcher_disable_widget(matcher.value_label);
1728                 prefs_matcher_disable_widget(matcher.value_entry);
1729                 prefs_matcher_enable_widget(matcher.addressbook_folder_label);
1730                 prefs_matcher_enable_widget(matcher.addressbook_folder_combo);
1731                 prefs_matcher_enable_widget(matcher.predicate_label);
1732                 prefs_matcher_disable_widget(matcher.predicate_combo);
1733                 prefs_matcher_enable_widget(matcher.predicate_flag_combo);
1734                 prefs_matcher_disable_widget(matcher.case_chkbtn);
1735                 prefs_matcher_disable_widget(matcher.regexp_chkbtn);
1736                 prefs_matcher_disable_widget(matcher.test_btn);
1737                 prefs_matcher_enable_widget(matcher.addressbook_select_btn);
1738                 break;
1739         }
1740 }
1741
1742 /*!
1743  *\brief        Handle key press
1744  *
1745  *\param        widget Widget receiving key press
1746  *\param        event Key event
1747  *\param        data User data
1748  */
1749 static gboolean prefs_matcher_key_pressed(GtkWidget *widget, GdkEventKey *event,
1750                                      gpointer data)
1751 {
1752         if (event && event->keyval == GDK_Escape) {
1753                 prefs_matcher_cancel();
1754                 return TRUE;            
1755         }
1756         return FALSE;
1757 }
1758
1759 /*!
1760  *\brief        Cancel matcher dialog
1761  */
1762 static void prefs_matcher_cancel(void)
1763 {
1764         gtk_widget_hide(matcher.window);
1765         inc_unlock();
1766 }
1767
1768 /*!
1769  *\brief        Accept current matchers
1770  */
1771 static void prefs_matcher_ok(void)
1772 {
1773         MatcherList *matchers;
1774         MatcherProp *matcherprop;
1775         AlertValue val;
1776         gchar *matcher_str = NULL;
1777         gchar *str = NULL;
1778         gint row = 1;
1779         GtkTreeModel *model;
1780         GtkTreeIter iter;
1781
1782         matchers = prefs_matcher_get_list();
1783
1784         if (matchers != NULL) {
1785                 matcherprop = prefs_matcher_dialog_to_matcher();
1786                 if (matcherprop != NULL) {
1787                         str = matcherprop_to_string(matcherprop);
1788                         matcherprop_free(matcherprop);
1789                         if (strcmp(str, "all") != 0) {
1790                                 model = gtk_tree_view_get_model(GTK_TREE_VIEW
1791                                                 (matcher.cond_list_view));
1792
1793                                 while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1794                                         gtk_tree_model_get(model, &iter,
1795                                                            PREFS_MATCHER_COND, &matcher_str,
1796                                                            -1);
1797                                         if (matcher_str && strcmp(matcher_str, str) == 0) 
1798                                                 break;
1799                                         row++;
1800                                         g_free(matcher_str);
1801                                         matcher_str = NULL;
1802                                 }
1803
1804                                 if (!matcher_str || strcmp(matcher_str, str) != 0) {
1805                                         val = alertpanel(_("Entry not saved"),
1806                                                  _("The entry was not saved.\nClose anyway?"),
1807                                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
1808                                         if (G_ALERTDEFAULT != val) {
1809                                                 g_free(matcher_str);                                             
1810                                                 g_free(str);
1811                                                 return;
1812                                         }
1813                                 }
1814                                 g_free(matcher_str);
1815                         }
1816                 }
1817                 g_free(str);
1818                 gtk_widget_hide(matcher.window);
1819                 inc_unlock();
1820                 if (matchers_callback != NULL)
1821                         matchers_callback(matchers);
1822                 matcherlist_free(matchers);
1823         }
1824 }
1825
1826 /*!
1827  *\brief        Called when closing dialog box
1828  *
1829  *\param        widget Dialog widget
1830  *\param        event Event info
1831  *\param        data User data
1832  *
1833  *\return       gint TRUE
1834  */
1835 static gint prefs_matcher_deleted(GtkWidget *widget, GdkEventAny *event,
1836                                   gpointer data)
1837 {
1838         prefs_matcher_cancel();
1839         return TRUE;
1840 }
1841
1842 /*
1843  * Strings describing test format strings
1844  * 
1845  * When adding new lines, remember to put 2 strings for each line
1846  */
1847 static gchar *test_desc_strings[] = {
1848         "%%",   N_("literal %"),
1849         "%s",   N_("Subject"),
1850         "%f",   N_("From"),
1851         "%t",   N_("To"),
1852         "%c",   N_("Cc"),
1853         "%d",   N_("Date"),
1854         "%i",   N_("Message-ID"),
1855         "%n",   N_("Newsgroups"),
1856         "%r",   N_("References"),
1857         "%F",   N_("filename (should not be modified)"),
1858         "\\n",  N_("new line"),
1859         "\\",   N_("escape character for quotes"),
1860         "\\\"", N_("quote character"),
1861         NULL,   NULL
1862 };
1863
1864 static DescriptionWindow test_desc_win = { 
1865         NULL,
1866         NULL, 
1867         2,
1868         N_("Match Type: 'Test'"),
1869         N_("'Test' allows you to test a message or message element "
1870            "using an external program or script. The program will "
1871            "return either 0 or 1.\n\n"
1872            "The following symbols can be used:"),
1873         test_desc_strings
1874 };
1875
1876
1877
1878 /*!
1879  *\brief        Show Test action's info
1880  */
1881 void prefs_matcher_test_info(void)
1882 {
1883         description_window_create(&test_desc_win);
1884 }
1885
1886 void prefs_matcher_addressbook_select(void)
1887 {
1888         gchar *folderpath = NULL;
1889         gboolean ret = FALSE;
1890
1891         folderpath = (gchar *) gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry));
1892         ret = addressbook_folder_selection(&folderpath);
1893         if ( ret != FALSE && folderpath != NULL)
1894                 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry), folderpath);
1895 }
1896
1897
1898 /*
1899  * list view
1900  */
1901
1902 static GtkListStore* prefs_matcher_create_data_store(void)
1903 {
1904         return gtk_list_store_new(N_PREFS_MATCHER_COLUMNS,
1905                                   G_TYPE_STRING,        
1906                                   G_TYPE_BOOLEAN,
1907                                   -1);
1908 }
1909
1910 static void prefs_matcher_list_view_insert_matcher(GtkWidget *list_view,
1911                                                    GtkTreeIter *row_iter,
1912                                                    const gchar *matcher,
1913                                                    gboolean is_valid) 
1914 {
1915         GtkTreeIter iter;
1916         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
1917                                         (GTK_TREE_VIEW(list_view)));
1918
1919         if (row_iter == NULL) {
1920                 /* append new */
1921                 gtk_list_store_append(list_store, &iter);
1922         } else {
1923                 /* change existing */
1924                 iter = *row_iter;
1925         }
1926
1927         gtk_list_store_set(list_store, &iter,
1928                            PREFS_MATCHER_COND, matcher,
1929                            PREFS_MATCHER_COND_VALID, is_valid,
1930                            -1);
1931 }
1932
1933 static GtkWidget *prefs_matcher_list_view_create(void)
1934 {
1935         GtkTreeView *list_view;
1936         GtkTreeSelection *selector;
1937         GtkTreeModel *model;
1938
1939         model = GTK_TREE_MODEL(prefs_matcher_create_data_store());
1940         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1941         g_object_unref(model);  
1942         
1943         gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
1944         
1945         selector = gtk_tree_view_get_selection(list_view);
1946         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1947         gtk_tree_selection_set_select_function(selector, prefs_matcher_selected,
1948                                                NULL, NULL);
1949
1950         /* create the columns */
1951         prefs_matcher_create_list_view_columns(GTK_WIDGET(list_view));
1952
1953         return GTK_WIDGET(list_view);
1954 }
1955
1956 static void prefs_matcher_create_list_view_columns(GtkWidget *list_view)
1957 {
1958         GtkTreeViewColumn *column;
1959         GtkCellRenderer *renderer;
1960
1961         renderer = gtk_cell_renderer_text_new();
1962         column = gtk_tree_view_column_new_with_attributes
1963                 (_("Current condition rules"),
1964                  renderer,
1965                  "text", PREFS_MATCHER_COND,
1966                  NULL);
1967         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1968 }
1969
1970 static gboolean prefs_matcher_selected(GtkTreeSelection *selector,
1971                                        GtkTreeModel *model, 
1972                                        GtkTreePath *path,
1973                                        gboolean currently_selected,
1974                                        gpointer data)
1975 {
1976         gchar *matcher_str;
1977         MatcherProp *prop;
1978         gboolean negative_cond;
1979         gint criteria;
1980         GtkTreeIter iter;
1981         gboolean is_valid;
1982
1983         if (currently_selected)
1984                 return TRUE;
1985
1986         if (!gtk_tree_model_get_iter(model, &iter, path))
1987                 return TRUE;
1988
1989         gtk_tree_model_get(model, &iter, 
1990                            PREFS_MATCHER_COND_VALID,  &is_valid,
1991                            PREFS_MATCHER_COND, &matcher_str,
1992                            -1);
1993         
1994         if (!is_valid) {
1995                 g_free(matcher_str);
1996                 prefs_matcher_reset_condition();
1997                 return TRUE;
1998         }
1999
2000         negative_cond = FALSE;
2001
2002         prop = matcher_parser_get_prop(matcher_str);
2003         if (prop == NULL) {
2004                 g_free(matcher_str);
2005                 return TRUE;
2006         }               
2007
2008         criteria = prefs_matcher_get_criteria_from_matching(prop->criteria);
2009         if (criteria != -1)
2010                 gtk_list_select_item(GTK_LIST(matcher.criteria_list),
2011                                      criteria);
2012
2013         switch(prop->criteria) {
2014         case MATCHCRITERIA_NOT_UNREAD:
2015         case MATCHCRITERIA_NOT_NEW:
2016         case MATCHCRITERIA_NOT_MARKED:
2017         case MATCHCRITERIA_NOT_DELETED:
2018         case MATCHCRITERIA_NOT_REPLIED:
2019         case MATCHCRITERIA_NOT_FORWARDED:
2020         case MATCHCRITERIA_NOT_LOCKED:
2021         case MATCHCRITERIA_NOT_PARTIAL:
2022         case MATCHCRITERIA_NOT_COLORLABEL:
2023         case MATCHCRITERIA_NOT_IGNORE_THREAD:
2024         case MATCHCRITERIA_NOT_SUBJECT:
2025         case MATCHCRITERIA_NOT_FROM:
2026         case MATCHCRITERIA_NOT_TO:
2027         case MATCHCRITERIA_NOT_CC:
2028         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
2029         case MATCHCRITERIA_NOT_NEWSGROUPS:
2030         case MATCHCRITERIA_NOT_INREPLYTO:
2031         case MATCHCRITERIA_NOT_REFERENCES:
2032         case MATCHCRITERIA_NOT_HEADER:
2033         case MATCHCRITERIA_NOT_HEADERS_PART:
2034         case MATCHCRITERIA_NOT_MESSAGE:
2035         case MATCHCRITERIA_NOT_BODY_PART:
2036         case MATCHCRITERIA_NOT_TEST:
2037         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
2038                 negative_cond = TRUE;
2039                 break;
2040         }
2041         
2042         switch(prop->criteria) {
2043         case MATCHCRITERIA_ALL:
2044                 break;
2045
2046         case MATCHCRITERIA_NOT_SUBJECT:
2047         case MATCHCRITERIA_NOT_FROM:
2048         case MATCHCRITERIA_NOT_TO:
2049         case MATCHCRITERIA_NOT_CC:
2050         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
2051         case MATCHCRITERIA_NOT_NEWSGROUPS:
2052         case MATCHCRITERIA_NOT_INREPLYTO:
2053         case MATCHCRITERIA_NOT_REFERENCES:
2054         case MATCHCRITERIA_NOT_HEADERS_PART:
2055         case MATCHCRITERIA_NOT_BODY_PART:
2056         case MATCHCRITERIA_NOT_MESSAGE:
2057         case MATCHCRITERIA_NOT_TEST:
2058         case MATCHCRITERIA_SUBJECT:
2059         case MATCHCRITERIA_FROM:
2060         case MATCHCRITERIA_TO:
2061         case MATCHCRITERIA_CC:
2062         case MATCHCRITERIA_TO_OR_CC:
2063         case MATCHCRITERIA_NEWSGROUPS:
2064         case MATCHCRITERIA_INREPLYTO:
2065         case MATCHCRITERIA_REFERENCES:
2066         case MATCHCRITERIA_HEADERS_PART:
2067         case MATCHCRITERIA_BODY_PART:
2068         case MATCHCRITERIA_MESSAGE:
2069         case MATCHCRITERIA_TEST:
2070                 gtk_entry_set_text(GTK_ENTRY(matcher.value_entry), prop->expr);
2071                 break;
2072
2073         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
2074         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
2075                 gtk_entry_set_text(GTK_ENTRY(matcher.header_addr_entry), prop->header);
2076                 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(matcher.addressbook_folder_combo)->entry), prop->expr);
2077                 break;
2078
2079         case MATCHCRITERIA_AGE_GREATER:
2080         case MATCHCRITERIA_AGE_LOWER:
2081         case MATCHCRITERIA_SCORE_GREATER:
2082         case MATCHCRITERIA_SCORE_LOWER:
2083         case MATCHCRITERIA_SCORE_EQUAL:
2084         case MATCHCRITERIA_SIZE_GREATER:
2085         case MATCHCRITERIA_SIZE_SMALLER:
2086         case MATCHCRITERIA_SIZE_EQUAL:
2087                 gtk_entry_set_text(GTK_ENTRY(matcher.value_entry), itos(prop->value));
2088                 break;
2089
2090         case MATCHCRITERIA_NOT_COLORLABEL:
2091         case MATCHCRITERIA_COLORLABEL:
2092                 gtk_option_menu_set_history(GTK_OPTION_MENU(matcher.color_optmenu),
2093                                             prop->value);
2094                 break;
2095
2096         case MATCHCRITERIA_NOT_HEADER:
2097         case MATCHCRITERIA_HEADER:
2098                 gtk_entry_set_text(GTK_ENTRY(matcher.header_entry), prop->header);
2099                 gtk_entry_set_text(GTK_ENTRY(matcher.value_entry), prop->expr);
2100                 break;
2101         }
2102
2103         if (negative_cond) {
2104                 gtk_list_select_item(GTK_LIST(matcher.predicate_list), 1);
2105                 gtk_list_select_item(GTK_LIST(matcher.predicate_flag_list), 1);
2106         } else {
2107                 gtk_list_select_item(GTK_LIST(matcher.predicate_list), 0);
2108                 gtk_list_select_item(GTK_LIST(matcher.predicate_flag_list), 0);
2109         }
2110         
2111         switch(prop->matchtype) {
2112         case MATCHTYPE_MATCH:
2113                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_chkbtn), FALSE);
2114                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_chkbtn), TRUE);
2115                 break;
2116
2117         case MATCHTYPE_MATCHCASE:
2118                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_chkbtn), FALSE);
2119                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_chkbtn), FALSE);
2120                 break;
2121
2122         case MATCHTYPE_REGEXP:
2123                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_chkbtn), TRUE);
2124                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_chkbtn), TRUE);
2125                 break;
2126
2127         case MATCHTYPE_REGEXPCASE:
2128                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_chkbtn), TRUE);
2129                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_chkbtn), FALSE);
2130                 break;
2131         }
2132
2133         g_free(matcher_str);
2134         return TRUE;
2135 }
2136