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