5f817980c662bdd6470dabe5b24942c533cb051f
[claws.git] / src / prefs_matcher.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 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 static gboolean match_combo2_model_set(void)
873 {
874         GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(matcher.match_combo2));
875         if (model == matcher.model_age_units ||
876             model == matcher.model_found ||
877             model == matcher.model_partial ||
878             model == matcher.model_phrase ||
879             model == matcher.model_set ||
880             model == matcher.model_size_units ||
881             model == matcher.model_tags ||
882             model == matcher.model_thread)
883                 return TRUE;
884         else
885                 debug_print("match_combo2 model unset.\n");
886
887         return FALSE;
888 }
889
890 static gboolean match_combo_model_set(void)
891 {
892         GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(matcher.match_combo));
893         if (model == matcher.model_age ||
894             model == matcher.model_contain ||
895             model == matcher.model_flags ||
896             model == matcher.model_score ||
897             model == matcher.model_size ||
898             model == matcher.model_test)
899                 return TRUE;
900         else
901                 debug_print("match_combo model unset.\n");
902
903         return FALSE;
904 }
905
906 /*!
907  *\brief        Clears a condition in the list widget
908  */
909 static void prefs_matcher_reset_condition(void)
910 {
911         gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.criteria_combo), MATCH_ALL);
912         gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.criteria_combo2), 0);
913         if (match_combo_model_set())
914                 gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.match_combo), 0);
915         if (match_combo2_model_set())
916                 gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.match_combo2), 0);
917         gtk_cmoption_menu_set_history(GTK_CMOPTION_MENU(matcher.color_optmenu), 0);
918         gtk_spin_button_set_value(GTK_SPIN_BUTTON(matcher.numeric_entry), 0);
919         gtk_entry_set_text(GTK_ENTRY(matcher.header_entry), "");
920         gtk_entry_set_text(GTK_ENTRY(matcher.header_addr_entry), "");
921         gtk_entry_set_text(GTK_ENTRY(matcher.string_entry), "");
922         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((matcher.addressbook_folder_combo)))), "");
923 #ifndef G_OS_WIN32
924         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_checkbtn), FALSE);
925 #endif
926         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_checkbtn), FALSE);
927 }
928
929 /*!
930  *\brief        Initializes dialog with a set of conditions
931  *
932  *\param        matchers List of conditions
933  */
934 static void prefs_matcher_set_dialog(MatcherList *matchers)
935 {
936         GSList *cur;
937         gboolean bool_op = 1;
938         GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model
939                                 (GTK_TREE_VIEW(matcher.cond_list_view)));
940
941         gtk_list_store_clear(store);                            
942
943         prefs_matcher_list_view_set_row(NULL, NULL);
944         if (matchers != NULL) {
945                 for (cur = matchers->matchers; cur != NULL;
946                      cur = g_slist_next(cur)) {
947                         MatcherProp *prop;
948                         prop = (MatcherProp *) cur->data;
949                         prefs_matcher_list_view_set_row(NULL, prop);
950                 }
951
952                 bool_op = matchers->bool_and;
953         }
954         
955         gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.bool_op_combo), bool_op);
956
957         prefs_matcher_reset_condition();
958         
959         combobox_set_sensitive(GTK_COMBO_BOX(matcher.criteria_combo), MATCH_TAGS,
960                         (tags_get_size() > 0) ? TRUE : FALSE);
961 }
962
963 /*!
964  *\brief        Converts current conditions in list box in
965  *              a matcher list used by the matcher.
966  *
967  *\return       MatcherList * List of conditions.
968  */
969 static MatcherList *prefs_matcher_get_list(void)
970 {
971         gchar *matcher_str;
972         MatcherProp *prop;
973         gboolean bool_and;
974         GSList *matcher_list;
975         MatcherList *matchers;
976         GtkTreeModel *model;
977         GtkTreeIter iter;
978
979         model = gtk_tree_view_get_model(GTK_TREE_VIEW(matcher.cond_list_view));
980         if (!gtk_tree_model_get_iter_first(model, &iter))
981                 return NULL;
982
983         matcher_list = NULL;
984
985         do {
986                 gboolean is_valid;
987         
988                 gtk_tree_model_get(model, &iter,
989                                    PREFS_MATCHER_COND, &matcher_str,
990                                    PREFS_MATCHER_COND_VALID, &is_valid,
991                                    -1);
992                 
993                 if (is_valid) {
994                         /* tmp = matcher_str; */
995                         prop = matcher_parser_get_prop(matcher_str);
996                         g_free(matcher_str);
997                         if (prop == NULL)
998                                 break;
999                         
1000                         matcher_list = g_slist_append(matcher_list, prop);
1001                 }
1002         } while (gtk_tree_model_iter_next(model, &iter));
1003
1004         bool_and = gtk_combo_box_get_active(GTK_COMBO_BOX(matcher.bool_op_combo));
1005
1006         matchers = matcherlist_new(matcher_list, bool_and);
1007
1008         return matchers;
1009 }
1010
1011 /*!
1012  *\brief        Maps a keyword id (see #get_matchparser_tab_id) to a 
1013  *              criteria type (see first parameter of #matcherprop_new
1014  *              or #matcherprop_unquote_new)
1015  *
1016  *\param        matching_id Id returned by the matcher parser.
1017  *
1018  *\return       gint One of the CRITERIA_xxx constants.
1019  */
1020 static gint prefs_matcher_get_criteria_from_matching(gint matching_id)
1021 {
1022         switch(matching_id) {
1023         case MATCHCRITERIA_ALL:
1024                 return CRITERIA_ALL;
1025         case MATCHCRITERIA_NOT_UNREAD:
1026         case MATCHCRITERIA_UNREAD:
1027                 return CRITERIA_UNREAD;
1028         case MATCHCRITERIA_NOT_NEW:
1029         case MATCHCRITERIA_NEW:
1030                 return CRITERIA_NEW;
1031         case MATCHCRITERIA_NOT_MARKED:
1032         case MATCHCRITERIA_MARKED:
1033                 return CRITERIA_MARKED;
1034         case MATCHCRITERIA_NOT_DELETED:
1035         case MATCHCRITERIA_DELETED:
1036                 return CRITERIA_DELETED;
1037         case MATCHCRITERIA_NOT_REPLIED:
1038         case MATCHCRITERIA_REPLIED:
1039                 return CRITERIA_REPLIED;
1040         case MATCHCRITERIA_NOT_FORWARDED:
1041         case MATCHCRITERIA_FORWARDED:
1042                 return CRITERIA_FORWARDED;
1043         case MATCHCRITERIA_LOCKED:
1044         case MATCHCRITERIA_NOT_LOCKED:
1045                 return CRITERIA_LOCKED;
1046         case MATCHCRITERIA_NOT_SPAM:
1047         case MATCHCRITERIA_SPAM:
1048                 return CRITERIA_SPAM;
1049         case MATCHCRITERIA_HAS_ATTACHMENT:
1050         case MATCHCRITERIA_HAS_NO_ATTACHMENT:
1051                 return CRITERIA_HAS_ATTACHMENT;
1052         case MATCHCRITERIA_SIGNED:
1053         case MATCHCRITERIA_NOT_SIGNED:
1054                 return CRITERIA_SIGNED;
1055         case MATCHCRITERIA_PARTIAL:
1056         case MATCHCRITERIA_NOT_PARTIAL:
1057                 return CRITERIA_PARTIAL;
1058         case MATCHCRITERIA_COLORLABEL:
1059         case MATCHCRITERIA_NOT_COLORLABEL:
1060                 return CRITERIA_COLORLABEL;
1061         case MATCHCRITERIA_IGNORE_THREAD:
1062         case MATCHCRITERIA_NOT_IGNORE_THREAD:
1063                 return CRITERIA_IGNORE_THREAD;
1064         case MATCHCRITERIA_WATCH_THREAD:
1065         case MATCHCRITERIA_NOT_WATCH_THREAD:
1066                 return CRITERIA_WATCH_THREAD;
1067         case MATCHCRITERIA_NOT_SUBJECT:
1068         case MATCHCRITERIA_SUBJECT:
1069                 return CRITERIA_SUBJECT;
1070         case MATCHCRITERIA_NOT_FROM:
1071         case MATCHCRITERIA_FROM:
1072                 return CRITERIA_FROM;
1073         case MATCHCRITERIA_NOT_TO:
1074         case MATCHCRITERIA_TO:
1075                 return CRITERIA_TO;
1076         case MATCHCRITERIA_NOT_CC:
1077         case MATCHCRITERIA_CC:
1078                 return CRITERIA_CC;
1079         case MATCHCRITERIA_NOT_NEWSGROUPS:
1080         case MATCHCRITERIA_NEWSGROUPS:
1081                 return CRITERIA_NEWSGROUPS;
1082         case MATCHCRITERIA_NOT_INREPLYTO:
1083         case MATCHCRITERIA_INREPLYTO:
1084                 return CRITERIA_INREPLYTO;
1085         case MATCHCRITERIA_NOT_REFERENCES:
1086         case MATCHCRITERIA_REFERENCES:
1087                 return CRITERIA_REFERENCES;
1088         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1089         case MATCHCRITERIA_TO_OR_CC:
1090                 return CRITERIA_TO_OR_CC;
1091         case MATCHCRITERIA_NOT_TAG:
1092         case MATCHCRITERIA_TAG:
1093                 return CRITERIA_TAG;
1094         case MATCHCRITERIA_NOT_TAGGED:
1095         case MATCHCRITERIA_TAGGED:
1096                 return CRITERIA_TAGGED;
1097         case MATCHCRITERIA_NOT_BODY_PART:
1098         case MATCHCRITERIA_BODY_PART:
1099                 return CRITERIA_BODY_PART;
1100         case MATCHCRITERIA_NOT_MESSAGE:
1101         case MATCHCRITERIA_MESSAGE:
1102                 return CRITERIA_MESSAGE;
1103         case MATCHCRITERIA_NOT_HEADERS_PART:
1104         case MATCHCRITERIA_HEADERS_PART:
1105                 return CRITERIA_HEADERS_PART;
1106         case MATCHCRITERIA_NOT_HEADER:
1107         case MATCHCRITERIA_HEADER:
1108                 return CRITERIA_HEADER;
1109         case MATCHCRITERIA_AGE_GREATER:
1110                 return CRITERIA_AGE_GREATER;
1111         case MATCHCRITERIA_AGE_LOWER:
1112                 return CRITERIA_AGE_LOWER;
1113         case MATCHCRITERIA_SCORE_GREATER:
1114                 return CRITERIA_SCORE_GREATER;
1115         case MATCHCRITERIA_SCORE_LOWER:
1116                 return CRITERIA_SCORE_LOWER;
1117         case MATCHCRITERIA_SCORE_EQUAL:
1118                 return CRITERIA_SCORE_EQUAL;
1119         case MATCHCRITERIA_NOT_TEST:
1120         case MATCHCRITERIA_TEST:
1121                 return CRITERIA_TEST;
1122         case MATCHCRITERIA_SIZE_GREATER:
1123                 return CRITERIA_SIZE_GREATER;
1124         case MATCHCRITERIA_SIZE_SMALLER:
1125                 return CRITERIA_SIZE_SMALLER;
1126         case MATCHCRITERIA_SIZE_EQUAL:
1127                 return CRITERIA_SIZE_EQUAL;
1128         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1129         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1130                 return CRITERIA_FOUND_IN_ADDRESSBOOK;
1131         default:
1132                 return -1;
1133         }
1134 }
1135
1136 /*!
1137  *\brief        Returns the matcher keyword id from a criteria id
1138  *
1139  *\param        criteria_id Criteria id (should not be the negate
1140  *              one)
1141  *
1142  *\return       gint A matcher keyword id. See #get_matchparser_tab_id.
1143  */
1144 static gint prefs_matcher_get_matching_from_criteria(gint criteria_id)
1145 {
1146         switch (criteria_id) {
1147         case CRITERIA_ALL:
1148                 return MATCHCRITERIA_ALL;
1149         case CRITERIA_UNREAD:
1150                 return MATCHCRITERIA_UNREAD;
1151         case CRITERIA_NEW:
1152                 return MATCHCRITERIA_NEW;
1153         case CRITERIA_MARKED:
1154                 return MATCHCRITERIA_MARKED;
1155         case CRITERIA_DELETED:
1156                 return MATCHCRITERIA_DELETED;
1157         case CRITERIA_REPLIED:
1158                 return MATCHCRITERIA_REPLIED;
1159         case CRITERIA_FORWARDED:
1160                 return MATCHCRITERIA_FORWARDED;
1161         case CRITERIA_LOCKED:
1162                 return MATCHCRITERIA_LOCKED;
1163         case CRITERIA_SPAM:
1164                 return MATCHCRITERIA_SPAM;
1165         case CRITERIA_HAS_ATTACHMENT:
1166                 return MATCHCRITERIA_HAS_ATTACHMENT;
1167         case CRITERIA_SIGNED:
1168                 return MATCHCRITERIA_SIGNED;
1169         case CRITERIA_PARTIAL:
1170                 return MATCHCRITERIA_PARTIAL;
1171         case CRITERIA_COLORLABEL:
1172                 return MATCHCRITERIA_COLORLABEL;
1173         case CRITERIA_IGNORE_THREAD:
1174                 return MATCHCRITERIA_IGNORE_THREAD;
1175         case CRITERIA_WATCH_THREAD:
1176                 return MATCHCRITERIA_WATCH_THREAD;
1177         case CRITERIA_SUBJECT:
1178                 return MATCHCRITERIA_SUBJECT;
1179         case CRITERIA_FROM:
1180                 return MATCHCRITERIA_FROM;
1181         case CRITERIA_TO:
1182                 return MATCHCRITERIA_TO;
1183         case CRITERIA_CC:
1184                 return MATCHCRITERIA_CC;
1185         case CRITERIA_TO_OR_CC:
1186                 return MATCHCRITERIA_TO_OR_CC;
1187         case CRITERIA_TAG:
1188                 return MATCHCRITERIA_TAG;
1189         case CRITERIA_TAGGED:
1190                 return MATCHCRITERIA_TAGGED;
1191         case CRITERIA_NEWSGROUPS:
1192                 return MATCHCRITERIA_NEWSGROUPS;
1193         case CRITERIA_INREPLYTO:
1194                 return MATCHCRITERIA_INREPLYTO;
1195         case CRITERIA_REFERENCES:
1196                 return MATCHCRITERIA_REFERENCES;
1197         case CRITERIA_AGE_GREATER:
1198                 return MATCHCRITERIA_AGE_GREATER;
1199         case CRITERIA_AGE_LOWER:
1200                 return MATCHCRITERIA_AGE_LOWER;
1201         case CRITERIA_SCORE_GREATER:
1202                 return MATCHCRITERIA_SCORE_GREATER;
1203         case CRITERIA_SCORE_LOWER:
1204                 return MATCHCRITERIA_SCORE_LOWER;
1205         case CRITERIA_SCORE_EQUAL:
1206                 return MATCHCRITERIA_SCORE_EQUAL;
1207         case CRITERIA_HEADER:
1208                 return MATCHCRITERIA_HEADER;
1209         case CRITERIA_HEADERS_PART:
1210                 return MATCHCRITERIA_HEADERS_PART;
1211         case CRITERIA_BODY_PART:
1212                 return MATCHCRITERIA_BODY_PART;
1213         case CRITERIA_MESSAGE:
1214                 return MATCHCRITERIA_MESSAGE;
1215         case CRITERIA_TEST:
1216                 return MATCHCRITERIA_TEST;
1217         case CRITERIA_SIZE_GREATER:
1218                 return MATCHCRITERIA_SIZE_GREATER;
1219         case CRITERIA_SIZE_SMALLER:
1220                 return MATCHCRITERIA_SIZE_SMALLER;
1221         case CRITERIA_SIZE_EQUAL:
1222                 return MATCHCRITERIA_SIZE_EQUAL;
1223         case CRITERIA_FOUND_IN_ADDRESSBOOK:
1224                 return MATCHCRITERIA_FOUND_IN_ADDRESSBOOK;
1225         default:
1226                 return -1;
1227         }
1228 }
1229
1230 /*!
1231  *\brief        Returns the negate matcher keyword id from a matcher keyword
1232  *              id.
1233  *
1234  *\param        matcher_criteria Matcher keyword id. 
1235  *
1236  *\return       gint A matcher keyword id. See #get_matchparser_tab_id.
1237  */
1238 static gint prefs_matcher_not_criteria(gint matcher_criteria)
1239 {
1240         switch(matcher_criteria) {
1241         case MATCHCRITERIA_UNREAD:
1242                 return MATCHCRITERIA_NOT_UNREAD;
1243         case MATCHCRITERIA_NEW:
1244                 return MATCHCRITERIA_NOT_NEW;
1245         case MATCHCRITERIA_MARKED:
1246                 return MATCHCRITERIA_NOT_MARKED;
1247         case MATCHCRITERIA_DELETED:
1248                 return MATCHCRITERIA_NOT_DELETED;
1249         case MATCHCRITERIA_REPLIED:
1250                 return MATCHCRITERIA_NOT_REPLIED;
1251         case MATCHCRITERIA_FORWARDED:
1252                 return MATCHCRITERIA_NOT_FORWARDED;
1253         case MATCHCRITERIA_LOCKED:
1254                 return MATCHCRITERIA_NOT_LOCKED;
1255         case MATCHCRITERIA_SPAM:
1256                 return MATCHCRITERIA_NOT_SPAM;
1257         case MATCHCRITERIA_HAS_ATTACHMENT:
1258                 return MATCHCRITERIA_HAS_NO_ATTACHMENT;
1259         case MATCHCRITERIA_SIGNED:
1260                 return MATCHCRITERIA_NOT_SIGNED;
1261         case MATCHCRITERIA_PARTIAL:
1262                 return MATCHCRITERIA_NOT_PARTIAL;
1263         case MATCHCRITERIA_COLORLABEL:
1264                 return MATCHCRITERIA_NOT_COLORLABEL;
1265         case MATCHCRITERIA_IGNORE_THREAD:
1266                 return MATCHCRITERIA_NOT_IGNORE_THREAD;
1267         case MATCHCRITERIA_WATCH_THREAD:
1268                 return MATCHCRITERIA_NOT_WATCH_THREAD;
1269         case MATCHCRITERIA_SUBJECT:
1270                 return MATCHCRITERIA_NOT_SUBJECT;
1271         case MATCHCRITERIA_FROM:
1272                 return MATCHCRITERIA_NOT_FROM;
1273         case MATCHCRITERIA_TO:
1274                 return MATCHCRITERIA_NOT_TO;
1275         case MATCHCRITERIA_CC:
1276                 return MATCHCRITERIA_NOT_CC;
1277         case MATCHCRITERIA_TO_OR_CC:
1278                 return MATCHCRITERIA_NOT_TO_AND_NOT_CC;
1279         case MATCHCRITERIA_TAG:
1280                 return MATCHCRITERIA_NOT_TAG;
1281         case MATCHCRITERIA_TAGGED:
1282                 return MATCHCRITERIA_NOT_TAGGED;
1283         case MATCHCRITERIA_NEWSGROUPS:
1284                 return MATCHCRITERIA_NOT_NEWSGROUPS;
1285         case MATCHCRITERIA_INREPLYTO:
1286                 return MATCHCRITERIA_NOT_INREPLYTO;
1287         case MATCHCRITERIA_REFERENCES:
1288                 return MATCHCRITERIA_NOT_REFERENCES;
1289         case MATCHCRITERIA_HEADER:
1290                 return MATCHCRITERIA_NOT_HEADER;
1291         case MATCHCRITERIA_HEADERS_PART:
1292                 return MATCHCRITERIA_NOT_HEADERS_PART;
1293         case MATCHCRITERIA_MESSAGE:
1294                 return MATCHCRITERIA_NOT_MESSAGE;
1295         case MATCHCRITERIA_TEST:
1296                 return MATCHCRITERIA_NOT_TEST;
1297         case MATCHCRITERIA_BODY_PART:
1298                 return MATCHCRITERIA_NOT_BODY_PART;
1299         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1300                 return MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK;
1301         default:
1302                 return matcher_criteria;
1303         }
1304 }
1305
1306 static gint prefs_matcher_get_criteria(void)
1307 {
1308         gint match_criteria = gtk_combo_box_get_active(GTK_COMBO_BOX(
1309                                         matcher.criteria_combo));
1310         const gchar *header = NULL;
1311           
1312         switch (match_criteria) {
1313         case MATCH_ABOOK:
1314                 return CRITERIA_FOUND_IN_ADDRESSBOOK;   
1315         case MATCH_ALL:
1316                 return CRITERIA_ALL;
1317         case MATCH_AGE:
1318         case MATCH_SCORE:
1319         case MATCH_SIZE:
1320         case MATCH_FLAG:
1321                 return combobox_get_active_data(GTK_COMBO_BOX(
1322                                         matcher.match_combo));
1323         case MATCH_HEADER:
1324                 header = gtk_entry_get_text(GTK_ENTRY(matcher.header_entry));
1325                 return header_name_to_crit(header);
1326         case MATCH_LABEL:
1327                 return CRITERIA_COLORLABEL;
1328         case MATCH_PARTIAL:
1329                 return CRITERIA_PARTIAL;
1330         case MATCH_TEST:
1331                 return CRITERIA_TEST;
1332         case MATCH_PHRASE:
1333         case MATCH_TAGS:
1334         case MATCH_THREAD:
1335                 return combobox_get_active_data(GTK_COMBO_BOX(
1336                                         matcher.criteria_combo2));
1337         }
1338         
1339         return -1;
1340 }
1341
1342 static gint prefs_matcher_get_pred(const gint criteria)
1343 {
1344         switch(criteria) {
1345         case CRITERIA_SUBJECT:
1346         case CRITERIA_FROM:
1347         case CRITERIA_TO:
1348         case CRITERIA_CC:
1349         case CRITERIA_TO_OR_CC:
1350         case CRITERIA_NEWSGROUPS:
1351         case CRITERIA_INREPLYTO:
1352         case CRITERIA_REFERENCES:
1353         case CRITERIA_HEADER:
1354         case CRITERIA_HEADERS_PART:
1355         case CRITERIA_BODY_PART:
1356         case CRITERIA_MESSAGE:
1357         case CRITERIA_TAG:
1358         case CRITERIA_TAGGED:
1359         case CRITERIA_TEST:
1360                 return gtk_combo_box_get_active(GTK_COMBO_BOX(matcher.match_combo));
1361         case CRITERIA_FOUND_IN_ADDRESSBOOK:
1362         case CRITERIA_UNREAD:
1363         case CRITERIA_NEW:
1364         case CRITERIA_MARKED:
1365         case CRITERIA_DELETED:
1366         case CRITERIA_REPLIED:
1367         case CRITERIA_FORWARDED:
1368         case CRITERIA_LOCKED:
1369         case CRITERIA_SPAM:
1370         case CRITERIA_HAS_ATTACHMENT:
1371         case CRITERIA_SIGNED:
1372         case CRITERIA_COLORLABEL:
1373                 return gtk_combo_box_get_active(GTK_COMBO_BOX(matcher.match_combo2));
1374         case CRITERIA_WATCH_THREAD:
1375                 return gtk_combo_box_get_active(GTK_COMBO_BOX(matcher.criteria_combo2)) - 2;
1376         case CRITERIA_IGNORE_THREAD:
1377         case CRITERIA_PARTIAL:
1378                 return gtk_combo_box_get_active(GTK_COMBO_BOX(matcher.criteria_combo2));
1379         }
1380         
1381         return 0;
1382 }
1383
1384 /*!
1385  *\brief        Converts the text in the selected row to a 
1386  *              matcher structure
1387  *
1388  *\return       MatcherProp * Newly allocated matcher structure.
1389  */
1390 static MatcherProp *prefs_matcher_dialog_to_matcher(void)
1391 {
1392         MatcherProp *matcherprop;
1393         gint criteria;
1394         gint matchtype;
1395         gint value_pred;
1396         gint value_criteria = prefs_matcher_get_criteria();
1397         gboolean use_regexp;
1398         gboolean case_sensitive;
1399         const gchar *header;
1400         const gchar *expr;
1401         gint value, sel;
1402
1403         if (value_criteria == -1)
1404                 return NULL;
1405
1406         criteria = prefs_matcher_get_matching_from_criteria(value_criteria);
1407
1408         value_pred = prefs_matcher_get_pred(value_criteria);
1409         if(value_pred)
1410                 criteria = prefs_matcher_not_criteria(criteria);
1411
1412 #ifndef G_OS_WIN32
1413         use_regexp = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(matcher.regexp_checkbtn));
1414 #else
1415         use_regexp = FALSE;
1416 #endif
1417         case_sensitive = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(matcher.case_checkbtn));
1418
1419         if (use_regexp) {
1420                 if (case_sensitive)
1421                         matchtype = MATCHTYPE_REGEXP;
1422                 else
1423                         matchtype = MATCHTYPE_REGEXPCASE;
1424         }
1425         else {
1426                 if (case_sensitive)
1427                         matchtype = MATCHTYPE_MATCH;
1428                 else
1429                         matchtype = MATCHTYPE_MATCHCASE;
1430         }
1431
1432         header = NULL;
1433         expr = NULL;
1434         value = 0;
1435
1436         switch (value_criteria) {
1437         case CRITERIA_ALL:
1438         case CRITERIA_UNREAD:
1439         case CRITERIA_NEW:
1440         case CRITERIA_MARKED:
1441         case CRITERIA_DELETED:
1442         case CRITERIA_REPLIED:
1443         case CRITERIA_FORWARDED:
1444         case CRITERIA_LOCKED:
1445         case CRITERIA_SPAM:
1446         case CRITERIA_HAS_ATTACHMENT:
1447         case CRITERIA_SIGNED:
1448         case CRITERIA_PARTIAL:
1449         case CRITERIA_IGNORE_THREAD:
1450         case CRITERIA_WATCH_THREAD:
1451         case CRITERIA_TAGGED:
1452                 break;
1453
1454         case CRITERIA_SUBJECT:
1455         case CRITERIA_FROM:
1456         case CRITERIA_TO:
1457         case CRITERIA_CC:
1458         case CRITERIA_TO_OR_CC:
1459         case CRITERIA_TAG:
1460         case CRITERIA_NEWSGROUPS:
1461         case CRITERIA_INREPLYTO:
1462         case CRITERIA_REFERENCES:
1463         case CRITERIA_HEADERS_PART:
1464         case CRITERIA_BODY_PART:
1465         case CRITERIA_MESSAGE:
1466                 expr = gtk_entry_get_text(GTK_ENTRY(matcher.string_entry));
1467                 
1468                 if(*expr == '\0') {
1469                         alertpanel_error(_("Search pattern is not set."));
1470                         return NULL;
1471                 }
1472                 break;
1473
1474         case CRITERIA_TEST:
1475                 expr = gtk_entry_get_text(GTK_ENTRY(matcher.string_entry));
1476                 
1477                 if(*expr == '\0') {
1478                         alertpanel_error(_("Test command is not set."));
1479                         return NULL;
1480                 }
1481                 break;
1482
1483         case CRITERIA_AGE_GREATER:
1484         case CRITERIA_AGE_LOWER:
1485                 value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(
1486                                                          matcher.numeric_entry));
1487                 sel = gtk_combo_box_get_active(GTK_COMBO_BOX(matcher.match_combo2));
1488                 if(sel == AGE_WEEKS)
1489                         value *= 7;
1490                 break;
1491                         
1492         case CRITERIA_SCORE_GREATER:
1493         case CRITERIA_SCORE_LOWER:
1494         case CRITERIA_SCORE_EQUAL:
1495                 value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(
1496                                                          matcher.numeric_entry));
1497                 break;
1498                                                          
1499         case CRITERIA_SIZE_GREATER:
1500         case CRITERIA_SIZE_SMALLER:
1501         case CRITERIA_SIZE_EQUAL:
1502                 value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(
1503                                                          matcher.numeric_entry));
1504                 sel = gtk_combo_box_get_active(GTK_COMBO_BOX(matcher.match_combo2));
1505                 if(sel == SIZE_UNIT_MBYTES)
1506                         value *= MB_SIZE;
1507                 if(sel == SIZE_UNIT_KBYTES)
1508                         value *= KB_SIZE;
1509                 break;
1510                 
1511         case CRITERIA_COLORLABEL:
1512                 value = colorlabel_get_color_menu_active_item
1513                         (gtk_cmoption_menu_get_menu(GTK_CMOPTION_MENU
1514                                 (matcher.color_optmenu))); 
1515                 break;
1516
1517         case CRITERIA_HEADER:
1518                 header = gtk_entry_get_text(GTK_ENTRY(matcher.header_entry));
1519                 expr = gtk_entry_get_text(GTK_ENTRY(matcher.string_entry));
1520
1521                 if (*header == '\0') {
1522                     alertpanel_error(_("Header name is not set."));
1523                     return NULL;
1524                 }
1525                 
1526                 if(*expr == '\0') {
1527                         alertpanel_error(_("Search pattern is not set."));
1528                         return NULL;
1529                 } 
1530                 break;
1531
1532         case CRITERIA_FOUND_IN_ADDRESSBOOK:
1533                 header = gtk_entry_get_text(GTK_ENTRY(matcher.header_addr_entry));
1534                 expr = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((matcher.addressbook_folder_combo)))));
1535
1536                 if (*header == '\0') {
1537                     alertpanel_error(_("Header name is not set."));
1538                     return NULL;
1539                 }
1540                 if (*expr == '\0') {
1541                         gchar *tmp;
1542
1543                         if (g_utf8_collate(header, Q_("Filtering Matcher Menu|All")) == 0)
1544                                 tmp = g_strdup(_("all addresses in all headers"));
1545                         else
1546                         if (g_utf8_collate(header, _("Any")) == 0)
1547                                 tmp = g_strdup(_("any address in any header"));
1548                         else
1549                                 tmp = g_strdup_printf(_("the address(es) in header '%s'"), header);
1550                         alertpanel_error(_("Book/folder path is not set.\n\n"
1551                                                 "If you want to match %s against the whole address book, "
1552                                                 "you have to select '%s' from the book/folder drop-down list."),
1553                                                 tmp, _("Any"));
1554                         g_free(tmp);
1555                     return NULL;
1556                 }
1557                 /* store UNtranslated "Any"/"All" in matcher expressions */
1558                 if (g_utf8_collate(header, Q_("Filtering Matcher Menu|All")) == 0)
1559                         header = "All";
1560                 else
1561                         if (g_utf8_collate(header, _("Any")) == 0)
1562                                 header = "Any";
1563                 if (g_utf8_collate(expr, _("Any")) == 0)
1564                         expr = "Any";
1565                 break;
1566         }
1567
1568         matcherprop = matcherprop_new_create(criteria, header, matchtype,
1569                                       expr, value);
1570
1571         return matcherprop;
1572 }
1573
1574 /*!
1575  *\brief        Signal handler for register button
1576  */
1577 static void prefs_matcher_register_cb(void)
1578 {
1579         MatcherProp *matcherprop;
1580         
1581         matcherprop = prefs_matcher_dialog_to_matcher();
1582         if (matcherprop == NULL)
1583                 return;
1584
1585         prefs_matcher_list_view_set_row(NULL, matcherprop);
1586
1587         matcherprop_free(matcherprop);
1588         
1589         prefs_matcher_reset_condition();
1590 }
1591
1592 /*!
1593  *\brief        Signal handler for substitute button
1594  */
1595 static void prefs_matcher_substitute_cb(void)
1596 {
1597         MatcherProp *matcherprop;
1598         GtkTreeIter row;
1599         GtkTreeSelection *selection;
1600         GtkTreeModel *model;
1601         gboolean is_valid;
1602
1603         selection = gtk_tree_view_get_selection
1604                         (GTK_TREE_VIEW(matcher.cond_list_view));
1605         
1606         if (!gtk_tree_selection_get_selected(selection, &model, &row))
1607                 return;
1608         
1609         gtk_tree_model_get(model, &row, 
1610                            PREFS_MATCHER_COND_VALID, &is_valid,
1611                            -1);
1612         if (!is_valid)
1613                 return;
1614
1615         matcherprop = prefs_matcher_dialog_to_matcher();
1616         if (matcherprop == NULL)
1617                 return;
1618
1619         prefs_matcher_list_view_set_row(&row, matcherprop);
1620
1621         matcherprop_free(matcherprop);
1622 }
1623
1624 /*!
1625  *\brief        Signal handler for delete button
1626  */
1627 static void prefs_matcher_delete_cb(void)
1628 {
1629         GtkTreeIter row;
1630         GtkTreeSelection *selection;
1631         GtkTreeModel *model;
1632         gboolean is_valid;
1633
1634         selection = gtk_tree_view_get_selection
1635                         (GTK_TREE_VIEW(matcher.cond_list_view));
1636         
1637         if (!gtk_tree_selection_get_selected(selection, &model, &row))
1638                 return;
1639                 
1640         gtk_tree_model_get(model, &row, 
1641                            PREFS_MATCHER_COND_VALID, &is_valid,
1642                            -1);
1643
1644         if (!is_valid)
1645                 return;
1646
1647         gtk_list_store_remove(GTK_LIST_STORE(model), &row);             
1648
1649         prefs_matcher_reset_condition();
1650 }
1651
1652 /*!
1653  *\brief        Signal handler for 'move up' button
1654  */
1655 static void prefs_matcher_up(void)
1656 {
1657         GtkTreePath *prev, *sel, *try;
1658         GtkTreeIter isel;
1659         GtkListStore *store = NULL;
1660         GtkTreeModel *model = NULL;
1661         GtkTreeIter iprev;
1662         
1663         if (!gtk_tree_selection_get_selected
1664                 (gtk_tree_view_get_selection
1665                         (GTK_TREE_VIEW(matcher.cond_list_view)),
1666                  &model,        
1667                  &isel))
1668                 return;
1669         store = (GtkListStore *)model;
1670         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
1671         if (!sel)
1672                 return;
1673         
1674         /* no move if we're at row 0 or 1, looks phony, but other
1675          * solutions are more convoluted... */
1676         try = gtk_tree_path_copy(sel);
1677         if (!gtk_tree_path_prev(try) || !gtk_tree_path_prev(try)) {
1678                 gtk_tree_path_free(try);
1679                 gtk_tree_path_free(sel);
1680                 return;
1681         }
1682         gtk_tree_path_free(try);
1683
1684         prev = gtk_tree_path_copy(sel);         
1685         if (gtk_tree_path_prev(prev)) {
1686                 gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
1687                                         &iprev, prev);
1688                 gtk_list_store_swap(store, &iprev, &isel);
1689                 /* XXX: GTK2 select row?? */
1690         }
1691
1692         gtk_tree_path_free(sel);
1693         gtk_tree_path_free(prev);
1694 }
1695
1696 /*!
1697  *\brief        Signal handler for 'move down' button
1698  */
1699 static void prefs_matcher_down(void)
1700 {
1701         GtkListStore *store = NULL;
1702         GtkTreeModel *model = NULL;
1703         GtkTreeIter next, sel;
1704         GtkTreePath *try;
1705         
1706         if (!gtk_tree_selection_get_selected
1707                 (gtk_tree_view_get_selection
1708                         (GTK_TREE_VIEW(matcher.cond_list_view)),
1709                  &model,
1710                  &sel))
1711                 return;
1712         store = (GtkListStore *)model;
1713         try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
1714         if (!try) 
1715                 return;
1716         
1717         /* move when not at row 0 ... */
1718         if (gtk_tree_path_prev(try)) {
1719                 next = sel;
1720                 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next))
1721                         gtk_list_store_swap(store, &next, &sel);
1722         }
1723                 
1724         gtk_tree_path_free(try);
1725 }
1726
1727 static void prefs_matcher_enable_widget(GtkWidget* widget, const gboolean enable)
1728 {
1729         cm_return_if_fail(widget != NULL);
1730
1731         if(enable == TRUE) {
1732                 gtk_widget_set_sensitive(widget, TRUE);
1733                 gtk_widget_show(widget);        
1734         } else {
1735                 gtk_widget_set_sensitive(widget, FALSE);
1736                 gtk_widget_hide(widget);
1737         }
1738 }
1739
1740 static void prefs_matcher_set_model(GtkWidget *widget, GtkTreeModel *model)
1741 {
1742         cm_return_if_fail(widget != NULL);
1743         cm_return_if_fail(model != NULL);
1744         
1745         gtk_combo_box_set_model(GTK_COMBO_BOX(widget), model);
1746         gtk_combo_box_set_active(GTK_COMBO_BOX(widget), 0);
1747 }
1748
1749 static void prefs_matcher_second_criteria_sel(GtkWidget *widget,
1750                                               gpointer user_data)
1751 {
1752         gint criteria = gtk_combo_box_get_active(GTK_COMBO_BOX(
1753                                                 matcher.criteria_combo));
1754         gint criteria2 = combobox_get_active_data(GTK_COMBO_BOX(
1755                                                 matcher.criteria_combo2));
1756         
1757         if(criteria != MATCH_PHRASE && criteria != MATCH_TAGS) return;
1758         
1759         if(criteria == MATCH_PHRASE) {
1760                 switch(criteria2) {
1761                 case CRITERIA_HEADERS_PART:
1762                         gtk_label_set_text(GTK_LABEL(matcher.match_label),
1763                                         _("Headers part"));
1764                         break;
1765                 case CRITERIA_BODY_PART:
1766                         gtk_label_set_text(GTK_LABEL(matcher.match_label),
1767                                         _("Body part"));
1768                         break;  
1769                 case CRITERIA_MESSAGE:
1770                         gtk_label_set_text(GTK_LABEL(matcher.match_label),
1771                                         _("Whole message"));
1772                         break;
1773                 }
1774         }
1775         
1776         if(criteria == MATCH_TAGS) {
1777                 if(criteria2 == CRITERIA_TAGGED) {
1778                         prefs_matcher_enable_widget(matcher.upper_filler, FALSE);
1779                         prefs_matcher_enable_widget(matcher.match_label2, TRUE);
1780                         prefs_matcher_enable_widget(matcher.string_entry, FALSE);
1781                         prefs_matcher_enable_widget(matcher.case_checkbtn, FALSE);
1782 #ifndef G_OS_WIN32
1783                         prefs_matcher_enable_widget(matcher.regexp_checkbtn, FALSE);
1784 #endif
1785                 } else {
1786                         prefs_matcher_enable_widget(matcher.upper_filler, TRUE);
1787                         prefs_matcher_enable_widget(matcher.match_label2, FALSE);
1788                         prefs_matcher_enable_widget(matcher.string_entry, TRUE);
1789                         prefs_matcher_enable_widget(matcher.case_checkbtn, TRUE);
1790 #ifndef G_OS_WIN32
1791                         prefs_matcher_enable_widget(matcher.regexp_checkbtn, TRUE);
1792                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
1793                                                 matcher.regexp_checkbtn), FALSE);
1794 #endif
1795                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
1796                                                 matcher.case_checkbtn), FALSE);
1797                 }
1798         }
1799 }
1800
1801 #define MATCH_COMBO_IS_ENABLED(x) (x != MATCH_ALL && x != MATCH_ABOOK && \
1802                 x != MATCH_PARTIAL && x != MATCH_THREAD && x != MATCH_LABEL) ? TRUE : FALSE
1803 #define MATCH_CASE_REGEXP(x) (x == MATCH_HEADER || x == MATCH_PHRASE) ? TRUE : FALSE 
1804 #define MATCH_NUMERIC(x) (x == MATCH_AGE || x == MATCH_SCORE || \
1805                           x == MATCH_SIZE) ? TRUE : FALSE
1806
1807 /*!
1808  *\brief        Change widgets depending on the selected condition
1809  *
1810  *\param        criteria combo widget
1811  *\param        user_data Not used      
1812  */
1813 static void prefs_matcher_criteria_select(GtkWidget *widget,
1814                                           gpointer user_data)
1815 {
1816         gint value, old_value;
1817
1818         old_value = matcher.selected_criteria;
1819         matcher.selected_criteria = value = gtk_combo_box_get_active
1820                 (GTK_COMBO_BOX(matcher.criteria_combo));
1821
1822         if (old_value == matcher.selected_criteria)
1823                 return;
1824
1825         prefs_matcher_enable_widget(matcher.criteria_label2,
1826                                     (value == MATCH_ABOOK   ||
1827                                      value == MATCH_PHRASE  ||
1828                                      value == MATCH_HEADER  ||
1829                                      value == MATCH_PARTIAL ||
1830                                      value == MATCH_TAGS    ||
1831                                      value == MATCH_THREAD));
1832         prefs_matcher_enable_widget(matcher.headers_combo,
1833                                     (value == MATCH_HEADER));
1834         prefs_matcher_enable_widget(matcher.criteria_combo2,
1835                                     (value == MATCH_PHRASE  ||
1836                                      value == MATCH_PARTIAL ||
1837                                      value == MATCH_TAGS    ||
1838                                      value == MATCH_THREAD));
1839         prefs_matcher_enable_widget(matcher.match_combo2,
1840                                     (value == MATCH_ABOOK ||
1841                                      value == MATCH_AGE   ||
1842                                      value == MATCH_FLAG  ||
1843                                      value == MATCH_LABEL ||
1844                                      value == MATCH_SIZE));
1845         prefs_matcher_enable_widget(matcher.match_label2,
1846                                     (value == MATCH_ABOOK ||
1847                                      value == MATCH_FLAG  ||
1848                                      value == MATCH_LABEL ||
1849                                      value == MATCH_TAGS));
1850         prefs_matcher_enable_widget(matcher.header_addr_combo,
1851                                     (value == MATCH_ABOOK));
1852         prefs_matcher_enable_widget(matcher.string_entry,
1853                                     (MATCH_CASE_REGEXP(value) ||
1854                                      value == MATCH_TEST));
1855         prefs_matcher_enable_widget(matcher.numeric_entry,
1856                                     MATCH_NUMERIC(value));
1857         prefs_matcher_enable_widget(matcher.numeric_label,
1858                                     (value == MATCH_SCORE));
1859         prefs_matcher_enable_widget(matcher.addressbook_folder_combo,
1860                                     (value == MATCH_ABOOK));
1861         prefs_matcher_enable_widget(matcher.match_combo,
1862                                     MATCH_COMBO_IS_ENABLED(value));
1863         prefs_matcher_enable_widget(matcher.case_checkbtn,
1864                                     MATCH_CASE_REGEXP(value));
1865 #ifndef G_OS_WIN32
1866         prefs_matcher_enable_widget(matcher.regexp_checkbtn,
1867                                     MATCH_CASE_REGEXP(value));
1868 #endif
1869         prefs_matcher_enable_widget(matcher.test_btn,
1870                                     (value == MATCH_TEST));
1871         prefs_matcher_enable_widget(matcher.addressbook_select_btn,
1872                                     (value == MATCH_ABOOK));
1873         prefs_matcher_enable_widget(matcher.color_optmenu,
1874                                     (value == MATCH_LABEL));
1875         prefs_matcher_enable_widget(matcher.upper_filler,
1876                                     MATCH_CASE_REGEXP(value));
1877         prefs_matcher_enable_widget(matcher.lower_filler,
1878                                     (value == MATCH_ABOOK));
1879                                 
1880         gtk_label_set_text(GTK_LABEL(matcher.match_label), "");
1881         gtk_entry_set_text(GTK_ENTRY(matcher.string_entry), "");
1882
1883         switch(value) {
1884         case MATCH_ABOOK:
1885                 gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.header_addr_combo), 0);  
1886                 gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.addressbook_folder_combo), 0);
1887                 prefs_matcher_set_model(matcher.match_combo2, matcher.model_found);
1888                 gtk_label_set_text(GTK_LABEL(matcher.criteria_label2), _("in"));
1889                 gtk_label_set_text(GTK_LABEL(matcher.match_label), _("Header"));
1890                 gtk_label_set_text(GTK_LABEL(matcher.match_label2), _("content is"));
1891                 break;
1892         case MATCH_AGE:
1893                 prefs_matcher_set_model(matcher.match_combo, matcher.model_age);
1894                 prefs_matcher_set_model(matcher.match_combo2, matcher.model_age_units);
1895                 gtk_spin_button_set_range(GTK_SPIN_BUTTON(
1896                                   matcher.numeric_entry), 0, 1000);
1897                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(matcher.numeric_entry), 0);
1898                 gtk_label_set_text(GTK_LABEL(matcher.match_label), _("Age is"));
1899                 break;
1900         case MATCH_FLAG:
1901                 prefs_matcher_set_model(matcher.match_combo, matcher.model_flags);
1902                 prefs_matcher_set_model(matcher.match_combo2, matcher.model_set);
1903                 gtk_label_set_text(GTK_LABEL(matcher.match_label), _("Flag"));
1904                 gtk_label_set_text(GTK_LABEL(matcher.match_label2), _("is"));
1905                 break;
1906         case MATCH_HEADER:
1907                 gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.headers_combo), 0);
1908                 prefs_matcher_set_model(matcher.match_combo, matcher.model_contain);
1909                 gtk_label_set_text(GTK_LABEL(matcher.criteria_label2), _("Name:"));
1910                 gtk_label_set_text(GTK_LABEL(matcher.match_label), _("Header"));
1911 #ifndef G_OS_WIN32
1912                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_checkbtn), FALSE);
1913 #endif
1914                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_checkbtn), FALSE);
1915                 break;
1916         case MATCH_LABEL:
1917                 gtk_cmoption_menu_set_history(GTK_CMOPTION_MENU(matcher.color_optmenu), 0);
1918                 prefs_matcher_set_model(matcher.match_combo2, matcher.model_set);
1919                 gtk_label_set_text(GTK_LABEL(matcher.match_label), _("Label"));
1920                 gtk_label_set_text(GTK_LABEL(matcher.match_label2), _("is"));
1921                 break;
1922         case MATCH_PARTIAL:
1923                 prefs_matcher_set_model(matcher.criteria_combo2, matcher.model_partial);
1924                 gtk_label_set_text(GTK_LABEL(matcher.criteria_label2), _("Value:"));
1925                 break;
1926         case MATCH_PHRASE:
1927                 prefs_matcher_set_model(matcher.criteria_combo2, matcher.model_phrase);
1928                 prefs_matcher_set_model(matcher.match_combo, matcher.model_contain);
1929                 gtk_label_set_text(GTK_LABEL(matcher.criteria_label2), _("in"));
1930 #ifndef G_OS_WIN32
1931                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_checkbtn), FALSE);
1932 #endif
1933                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_checkbtn), FALSE);
1934                 prefs_matcher_second_criteria_sel(NULL, NULL);
1935                 break;  
1936         case MATCH_SCORE:
1937                 prefs_matcher_set_model(matcher.match_combo, matcher.model_score);
1938                 gtk_spin_button_set_range(GTK_SPIN_BUTTON(
1939                                           matcher.numeric_entry), -1000, 1000);
1940                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(matcher.numeric_entry), 0);
1941                 gtk_label_set_text(GTK_LABEL(matcher.match_label), _("Score is"));
1942                 gtk_label_set_text(GTK_LABEL(matcher.numeric_label), _("points"));
1943                 break;  
1944         case MATCH_SIZE:
1945                 prefs_matcher_set_model(matcher.match_combo, matcher.model_size);
1946                 prefs_matcher_set_model(matcher.match_combo2, matcher.model_size_units);
1947                 gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.match_combo2),
1948                                          SIZE_UNIT_KBYTES);
1949                 gtk_spin_button_set_range(GTK_SPIN_BUTTON(
1950                                           matcher.numeric_entry), 0, 100000);
1951                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(matcher.numeric_entry), 0);
1952                 gtk_label_set_text(GTK_LABEL(matcher.match_label), _("Size is"));
1953                 break;
1954         case MATCH_TAGS:
1955                 prefs_matcher_set_model(matcher.criteria_combo2, matcher.model_tags);
1956                 prefs_matcher_set_model(matcher.match_combo, matcher.model_contain);
1957                 gtk_label_set_text(GTK_LABEL(matcher.criteria_label2), _("Scope:"));
1958                 gtk_label_set_text(GTK_LABEL(matcher.match_label), _("Message"));
1959                 gtk_label_set_text(GTK_LABEL(matcher.match_label2), _("tags"));
1960                 prefs_matcher_second_criteria_sel(NULL, NULL);
1961                 break;
1962         case MATCH_THREAD:
1963                 prefs_matcher_set_model(matcher.criteria_combo2, matcher.model_thread);
1964                 gtk_label_set_text(GTK_LABEL(matcher.criteria_label2), _("type is"));
1965                 break;
1966         case MATCH_TEST:
1967                 prefs_matcher_set_model(matcher.match_combo, matcher.model_test);
1968                 gtk_label_set_text(GTK_LABEL(matcher.match_label), _("Program returns"));
1969                 break;
1970         }       
1971 }
1972
1973 /*!
1974  *\brief        Handle key press
1975  *
1976  *\param        widget Widget receiving key press
1977  *\param        event Key event
1978  *\param        data User data
1979  */
1980 static gboolean prefs_matcher_key_pressed(GtkWidget *widget, GdkEventKey *event,
1981                                      gpointer data)
1982 {
1983         if (event && event->keyval == GDK_KEY_Escape) {
1984                 prefs_matcher_cancel();
1985                 return TRUE;            
1986         }
1987         return FALSE;
1988 }
1989
1990 /*!
1991  *\brief        Cancel matcher dialog
1992  */
1993 static void prefs_matcher_cancel(void)
1994 {
1995         gtk_widget_hide(matcher.window);
1996         gtk_window_set_modal(GTK_WINDOW(matcher.window), FALSE);
1997         inc_unlock();
1998 }
1999
2000 /*!
2001  *\brief        Accept current matchers
2002  */
2003 static void prefs_matcher_ok(void)
2004 {
2005         MatcherList *matchers;
2006         MatcherProp *matcherprop;
2007         AlertValue val;
2008         gchar *matcher_str = NULL;
2009         gchar *str = NULL;
2010         gint row = 1;
2011         GtkTreeModel *model;
2012         GtkTreeIter iter;
2013
2014         matchers = prefs_matcher_get_list();
2015
2016         if (matchers != NULL) {
2017                 matcherprop = prefs_matcher_dialog_to_matcher();
2018                 if (matcherprop != NULL) {
2019                         str = matcherprop_to_string(matcherprop);
2020                         matcherprop_free(matcherprop);
2021                         if (strcmp(str, "all") != 0) {
2022                                 model = gtk_tree_view_get_model(GTK_TREE_VIEW
2023                                                 (matcher.cond_list_view));
2024
2025                                 while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
2026                                         gtk_tree_model_get(model, &iter,
2027                                                            PREFS_MATCHER_COND, &matcher_str,
2028                                                            -1);
2029                                         if (matcher_str && strcmp(matcher_str, str) == 0) 
2030                                                 break;
2031                                         row++;
2032                                         g_free(matcher_str);
2033                                         matcher_str = NULL;
2034                                 }
2035
2036                                 if (!matcher_str || strcmp(matcher_str, str) != 0) {
2037                                         val = alertpanel(_("Entry not saved"),
2038                                                  _("The entry was not saved.\nClose anyway?"),
2039                                                  GTK_STOCK_CLOSE, _("+_Continue editing"), NULL);
2040                                         if (G_ALERTDEFAULT != val) {
2041                                                 g_free(matcher_str);                                             
2042                                                 g_free(str);
2043                                                 return;
2044                                         }
2045                                 }
2046                                 g_free(matcher_str);
2047                         }
2048                 }
2049                 g_free(str);
2050                 gtk_widget_hide(matcher.window);
2051                 gtk_window_set_modal(GTK_WINDOW(matcher.window), FALSE);
2052                 if (matchers_callback != NULL)
2053                         matchers_callback(matchers);
2054                 matcherlist_free(matchers);
2055         }
2056         inc_unlock();
2057 }
2058
2059 /*!
2060  *\brief        Called when closing dialog box
2061  *
2062  *\param        widget Dialog widget
2063  *\param        event Event info
2064  *\param        data User data
2065  *
2066  *\return       gint TRUE
2067  */
2068 static gint prefs_matcher_deleted(GtkWidget *widget, GdkEventAny *event,
2069                                   gpointer data)
2070 {
2071         prefs_matcher_cancel();
2072         return TRUE;
2073 }
2074
2075 /*
2076  * Strings describing test format strings
2077  * 
2078  * When adding new lines, remember to put 2 strings for each line
2079  */
2080 static gchar *test_desc_strings[] = {
2081         "%%",   N_("literal %"),
2082         "%s",   N_("Subject"),
2083         "%f",   N_("From"),
2084         "%t",   N_("To"),
2085         "%c",   N_("Cc"),
2086         "%d",   N_("Date"),
2087         "%i",   N_("Message-ID"),
2088         "%n",   N_("Newsgroups"),
2089         "%r",   N_("References"),
2090         "%F",   N_("filename (should not be modified)"),
2091         "\\n",  N_("new line"),
2092         "\\",   N_("escape character for quotes"),
2093         "\\\"", N_("quote character"),
2094         NULL,   NULL
2095 };
2096
2097 static DescriptionWindow test_desc_win = { 
2098         NULL,
2099         NULL, 
2100         2,
2101         N_("Match Type: 'Test'"),
2102         N_("'Test' allows you to test a message or message element "
2103            "using an external program or script. The program will "
2104            "return either 0 or 1.\n\n"
2105            "The following symbols can be used:"),
2106         test_desc_strings
2107 };
2108
2109
2110
2111 /*!
2112  *\brief        Show Test action's info
2113  */
2114 static void prefs_matcher_test_info(GtkWidget *widget, GtkWidget *parent)
2115 {
2116         test_desc_win.parent = parent;
2117         description_window_create(&test_desc_win);
2118 }
2119
2120 static void prefs_matcher_addressbook_select(void)
2121 {
2122         const gchar *folderpath = NULL;
2123         gchar *new_path = NULL;
2124
2125         folderpath = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((matcher.addressbook_folder_combo)))));
2126         new_path = addressbook_folder_selection(folderpath);
2127         if (new_path) {
2128                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((matcher.addressbook_folder_combo)))), new_path);
2129                 g_free(new_path);
2130         } 
2131 }
2132
2133
2134 /*
2135  * list view
2136  */
2137
2138 static GtkListStore* prefs_matcher_create_data_store(void)
2139 {
2140         return gtk_list_store_new(N_PREFS_MATCHER_COLUMNS,
2141                                   G_TYPE_STRING,        
2142                                   G_TYPE_BOOLEAN,
2143                                   -1);
2144 }
2145
2146 static void prefs_matcher_list_view_insert_matcher(GtkWidget *list_view,
2147                                                    GtkTreeIter *row_iter,
2148                                                    const gchar *matcher,
2149                                                    gboolean is_valid) 
2150 {
2151         GtkTreeIter iter;
2152         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
2153                                         (GTK_TREE_VIEW(list_view)));
2154
2155         if (row_iter == NULL) {
2156                 /* append new */
2157                 gtk_list_store_append(list_store, &iter);
2158         } else {
2159                 /* change existing */
2160                 iter = *row_iter;
2161         }
2162
2163         gtk_list_store_set(list_store, &iter,
2164                            PREFS_MATCHER_COND, matcher,
2165                            PREFS_MATCHER_COND_VALID, is_valid,
2166                            -1);
2167 }
2168
2169 static GtkWidget *prefs_matcher_list_view_create(void)
2170 {
2171         GtkTreeView *list_view;
2172         GtkTreeSelection *selector;
2173         GtkTreeModel *model;
2174
2175         model = GTK_TREE_MODEL(prefs_matcher_create_data_store());
2176         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
2177         g_object_unref(model);  
2178         
2179         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
2180         gtk_tree_view_set_reorderable(list_view, TRUE);
2181         
2182         selector = gtk_tree_view_get_selection(list_view);
2183         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
2184         gtk_tree_selection_set_select_function(selector, prefs_matcher_selected,
2185                                                NULL, NULL);
2186
2187         /* create the columns */
2188         prefs_matcher_create_list_view_columns(GTK_WIDGET(list_view));
2189
2190         return GTK_WIDGET(list_view);
2191 }
2192
2193 static void prefs_matcher_create_list_view_columns(GtkWidget *list_view)
2194 {
2195         GtkTreeViewColumn *column;
2196         GtkCellRenderer *renderer;
2197
2198         renderer = gtk_cell_renderer_text_new();
2199         column = gtk_tree_view_column_new_with_attributes
2200                 (_("Current condition rules"),
2201                  renderer,
2202                  "text", PREFS_MATCHER_COND,
2203                  NULL);
2204         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
2205 }
2206
2207 static void prefs_matcher_set_criteria(const gint criteria)
2208 {
2209         gint match_criteria = 0;
2210         
2211         switch (criteria) {
2212         case CRITERIA_FOUND_IN_ADDRESSBOOK:
2213                 match_criteria = MATCH_ABOOK;
2214                 break;  
2215         case CRITERIA_ALL:
2216                 match_criteria = MATCH_ALL;
2217                 break;
2218         case CRITERIA_AGE_GREATER:
2219         case CRITERIA_AGE_LOWER:
2220                 match_criteria = MATCH_AGE;
2221                 break;
2222         case CRITERIA_SCORE_GREATER:
2223         case CRITERIA_SCORE_LOWER:
2224         case CRITERIA_SCORE_EQUAL:
2225                 match_criteria = MATCH_SCORE;
2226                 break;
2227         case CRITERIA_SIZE_GREATER:
2228         case CRITERIA_SIZE_SMALLER:
2229         case CRITERIA_SIZE_EQUAL:
2230                 match_criteria = MATCH_SIZE;
2231                 break;
2232         case CRITERIA_SUBJECT:
2233         case CRITERIA_FROM:
2234         case CRITERIA_TO:
2235         case CRITERIA_CC:
2236         case CRITERIA_TO_OR_CC:
2237         case CRITERIA_NEWSGROUPS:
2238         case CRITERIA_INREPLYTO:
2239         case CRITERIA_REFERENCES:
2240         case CRITERIA_HEADER:
2241                 match_criteria = MATCH_HEADER;
2242                 break;
2243         case CRITERIA_HEADERS_PART:
2244         case CRITERIA_BODY_PART:
2245         case CRITERIA_MESSAGE:
2246                 match_criteria = MATCH_PHRASE;
2247                 break;
2248         case CRITERIA_TEST:
2249                 match_criteria = MATCH_TEST;
2250                 break;
2251         case CRITERIA_COLORLABEL:
2252                 match_criteria = MATCH_LABEL;
2253                 break;
2254         case CRITERIA_TAG:
2255         case CRITERIA_TAGGED:
2256                 match_criteria = MATCH_TAGS;
2257                 break;
2258         case CRITERIA_UNREAD:
2259         case CRITERIA_NEW:
2260         case CRITERIA_MARKED:
2261         case CRITERIA_DELETED:
2262         case CRITERIA_REPLIED:
2263         case CRITERIA_FORWARDED:
2264         case CRITERIA_LOCKED:
2265         case CRITERIA_SPAM:
2266         case CRITERIA_HAS_ATTACHMENT:
2267         case CRITERIA_SIGNED:
2268                 match_criteria = MATCH_FLAG;
2269                 break;
2270         case CRITERIA_PARTIAL:
2271                 match_criteria = MATCH_PARTIAL;
2272                 break;
2273         case CRITERIA_IGNORE_THREAD:
2274         case CRITERIA_WATCH_THREAD:
2275                 match_criteria = MATCH_THREAD;
2276                 break;
2277         }
2278         
2279         gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.criteria_combo),
2280                                  match_criteria);
2281         
2282         switch(match_criteria) {
2283         case MATCH_HEADER:
2284                 if(criteria != CRITERIA_HEADER)
2285                         combobox_select_by_data(GTK_COMBO_BOX(
2286                                                 matcher.headers_combo),
2287                                                 criteria);
2288                 break;
2289         case MATCH_AGE:
2290         case MATCH_SCORE:
2291         case MATCH_SIZE:
2292         case MATCH_FLAG:
2293                 combobox_select_by_data(GTK_COMBO_BOX(
2294                                         matcher.match_combo), criteria);
2295                 break;
2296         case MATCH_PHRASE:
2297         case MATCH_TAGS:
2298                 combobox_select_by_data(GTK_COMBO_BOX(
2299                                         matcher.criteria_combo2), criteria);
2300                 break;
2301         }
2302 }
2303
2304 static gboolean prefs_matcher_selected(GtkTreeSelection *selector,
2305                                        GtkTreeModel *model, 
2306                                        GtkTreePath *path,
2307                                        gboolean currently_selected,
2308                                        gpointer data)
2309 {
2310         gchar *matcher_str;
2311         MatcherProp *prop;
2312         gboolean negative_cond;
2313         gint criteria;
2314         GtkWidget *menu;
2315         GtkTreeIter iter;
2316         gboolean is_valid;
2317
2318         if (currently_selected)
2319                 return TRUE;
2320
2321         if (!gtk_tree_model_get_iter(model, &iter, path))
2322                 return TRUE;
2323
2324         gtk_tree_model_get(model, &iter, 
2325                            PREFS_MATCHER_COND_VALID,  &is_valid,
2326                            PREFS_MATCHER_COND, &matcher_str,
2327                            -1);
2328         
2329         if (!is_valid) {
2330                 g_free(matcher_str);
2331                 prefs_matcher_reset_condition();
2332                 return TRUE;
2333         }
2334
2335         negative_cond = FALSE;
2336
2337         prop = matcher_parser_get_prop(matcher_str);
2338         if (prop == NULL) {
2339                 g_free(matcher_str);
2340                 return TRUE;
2341         }               
2342
2343         criteria = prefs_matcher_get_criteria_from_matching(prop->criteria);
2344         prefs_matcher_set_criteria(criteria);
2345
2346         switch(prop->criteria) {
2347         case MATCHCRITERIA_NOT_UNREAD:
2348         case MATCHCRITERIA_NOT_NEW:
2349         case MATCHCRITERIA_NOT_MARKED:
2350         case MATCHCRITERIA_NOT_DELETED:
2351         case MATCHCRITERIA_NOT_REPLIED:
2352         case MATCHCRITERIA_NOT_FORWARDED:
2353         case MATCHCRITERIA_NOT_LOCKED:
2354         case MATCHCRITERIA_NOT_SPAM:
2355         case MATCHCRITERIA_HAS_NO_ATTACHMENT:
2356         case MATCHCRITERIA_NOT_SIGNED:
2357         case MATCHCRITERIA_NOT_PARTIAL:
2358         case MATCHCRITERIA_NOT_COLORLABEL:
2359         case MATCHCRITERIA_NOT_IGNORE_THREAD:
2360         case MATCHCRITERIA_NOT_WATCH_THREAD:
2361         case MATCHCRITERIA_NOT_SUBJECT:
2362         case MATCHCRITERIA_NOT_FROM:
2363         case MATCHCRITERIA_NOT_TO:
2364         case MATCHCRITERIA_NOT_CC:
2365         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
2366         case MATCHCRITERIA_NOT_TAG:
2367         case MATCHCRITERIA_NOT_TAGGED:
2368         case MATCHCRITERIA_NOT_NEWSGROUPS:
2369         case MATCHCRITERIA_NOT_INREPLYTO:
2370         case MATCHCRITERIA_NOT_REFERENCES:
2371         case MATCHCRITERIA_NOT_HEADER:
2372         case MATCHCRITERIA_NOT_HEADERS_PART:
2373         case MATCHCRITERIA_NOT_MESSAGE:
2374         case MATCHCRITERIA_NOT_BODY_PART:
2375         case MATCHCRITERIA_NOT_TEST:
2376         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
2377                 negative_cond = TRUE;
2378                 break;
2379         }
2380         
2381         switch(prop->criteria) {
2382         case MATCHCRITERIA_ALL:
2383                 break;
2384
2385         case MATCHCRITERIA_NOT_SUBJECT:
2386         case MATCHCRITERIA_NOT_FROM:
2387         case MATCHCRITERIA_NOT_TO:
2388         case MATCHCRITERIA_NOT_CC:
2389         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
2390         case MATCHCRITERIA_NOT_TAG:
2391         case MATCHCRITERIA_NOT_NEWSGROUPS:
2392         case MATCHCRITERIA_NOT_INREPLYTO:
2393         case MATCHCRITERIA_NOT_REFERENCES:
2394         case MATCHCRITERIA_NOT_HEADERS_PART:
2395         case MATCHCRITERIA_NOT_BODY_PART:
2396         case MATCHCRITERIA_NOT_MESSAGE:
2397         case MATCHCRITERIA_NOT_TEST:
2398         case MATCHCRITERIA_SUBJECT:
2399         case MATCHCRITERIA_FROM:
2400         case MATCHCRITERIA_TO:
2401         case MATCHCRITERIA_CC:
2402         case MATCHCRITERIA_TO_OR_CC:
2403         case MATCHCRITERIA_TAG:
2404         case MATCHCRITERIA_NEWSGROUPS:
2405         case MATCHCRITERIA_INREPLYTO:
2406         case MATCHCRITERIA_REFERENCES:
2407         case MATCHCRITERIA_HEADERS_PART:
2408         case MATCHCRITERIA_BODY_PART:
2409         case MATCHCRITERIA_MESSAGE:
2410         case MATCHCRITERIA_TEST:
2411                 gtk_entry_set_text(GTK_ENTRY(matcher.string_entry), prop->expr);
2412                 break;
2413
2414         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
2415         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
2416         {
2417                 gchar *header;
2418                 gchar *expr;
2419
2420                 /* matcher expressions contain UNtranslated "Any"/"All",
2421                   select the relevant translated combo item */
2422                 if (strcasecmp(prop->header, "All") == 0)
2423                         header = (gchar*)Q_("Filtering Matcher Menu|All");
2424                 else
2425                         if (strcasecmp(prop->header, "Any") == 0)
2426                                 header = _("Any");
2427                         else
2428                                 header = prop->header;
2429                 if (strcasecmp(prop->expr, "Any") == 0)
2430                         expr = _("Any");
2431                 else
2432                         expr = prop->expr;
2433
2434                 gtk_entry_set_text(GTK_ENTRY(matcher.header_addr_entry), header);
2435                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((matcher.addressbook_folder_combo)))), expr);
2436                 break;
2437         }
2438
2439         case MATCHCRITERIA_AGE_GREATER:
2440         case MATCHCRITERIA_AGE_LOWER:
2441                 if(prop->value >= 7 && !(prop->value % 7)) {
2442                         gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.match_combo2),
2443                                                  AGE_WEEKS);
2444                         gtk_spin_button_set_value(GTK_SPIN_BUTTON(
2445                                         matcher.numeric_entry), prop->value/7);
2446                 } else {
2447                         gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.match_combo2),
2448                                                  AGE_DAYS);
2449                         gtk_spin_button_set_value(GTK_SPIN_BUTTON(
2450                                         matcher.numeric_entry), prop->value);
2451                 }
2452                 break;
2453                 
2454         case MATCHCRITERIA_SCORE_GREATER:
2455         case MATCHCRITERIA_SCORE_LOWER:
2456         case MATCHCRITERIA_SCORE_EQUAL:
2457                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(matcher.numeric_entry),
2458                                           prop->value);
2459                 break;
2460
2461         case MATCHCRITERIA_SIZE_GREATER:
2462         case MATCHCRITERIA_SIZE_SMALLER:
2463         case MATCHCRITERIA_SIZE_EQUAL:
2464                 if(prop->value >= MB_SIZE && !(prop->value % MB_SIZE)) {
2465                         gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.match_combo2),
2466                                                  SIZE_UNIT_MBYTES);
2467                         gtk_spin_button_set_value(GTK_SPIN_BUTTON(
2468                                         matcher.numeric_entry), prop->value/MB_SIZE);
2469                 } else if(prop->value >= KB_SIZE && !(prop->value % KB_SIZE)) {
2470                         gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.match_combo2),
2471                                                  SIZE_UNIT_KBYTES);
2472                         gtk_spin_button_set_value(GTK_SPIN_BUTTON(
2473                                         matcher.numeric_entry), prop->value/KB_SIZE);
2474                 } else {
2475                         gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.match_combo2),
2476                                                  SIZE_UNIT_BYTES);
2477                         gtk_spin_button_set_value(GTK_SPIN_BUTTON(
2478                                         matcher.numeric_entry), prop->value);           
2479                 }
2480                 break;
2481
2482         case MATCHCRITERIA_NOT_COLORLABEL:
2483         case MATCHCRITERIA_COLORLABEL:
2484                 gtk_cmoption_menu_set_history(GTK_CMOPTION_MENU(matcher.color_optmenu),
2485                                             prop->value + 1);
2486                 menu = gtk_cmoption_menu_get_menu(GTK_CMOPTION_MENU(matcher.color_optmenu));
2487                 g_signal_emit_by_name(G_OBJECT(menu), "selection-done", menu);
2488                 break;
2489
2490         case MATCHCRITERIA_NOT_HEADER:
2491         case MATCHCRITERIA_HEADER:
2492                 gtk_entry_set_text(GTK_ENTRY(matcher.header_entry), prop->header);
2493                 gtk_entry_set_text(GTK_ENTRY(matcher.string_entry), prop->expr);
2494                 break;
2495         }
2496
2497         switch(criteria) {
2498         case CRITERIA_SUBJECT:
2499         case CRITERIA_FROM:
2500         case CRITERIA_TO:
2501         case CRITERIA_CC:
2502         case CRITERIA_TO_OR_CC:
2503         case CRITERIA_NEWSGROUPS:
2504         case CRITERIA_INREPLYTO:
2505         case CRITERIA_REFERENCES:
2506         case CRITERIA_HEADER:
2507         case CRITERIA_HEADERS_PART:
2508         case CRITERIA_BODY_PART:
2509         case CRITERIA_MESSAGE:
2510         case CRITERIA_TAG:
2511         case CRITERIA_TAGGED:
2512         case CRITERIA_TEST:
2513                 gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.match_combo),
2514                                         negative_cond ? PREDICATE_DOES_NOT_CONTAIN :
2515                                                         PREDICATE_CONTAINS);
2516                 break;
2517         case CRITERIA_FOUND_IN_ADDRESSBOOK:
2518         case CRITERIA_UNREAD:
2519         case CRITERIA_NEW:
2520         case CRITERIA_MARKED:
2521         case CRITERIA_DELETED:
2522         case CRITERIA_REPLIED:
2523         case CRITERIA_FORWARDED:
2524         case CRITERIA_LOCKED:
2525         case CRITERIA_SPAM:
2526         case CRITERIA_HAS_ATTACHMENT:
2527         case CRITERIA_SIGNED:
2528         case CRITERIA_COLORLABEL:
2529                 gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.match_combo2),
2530                                          negative_cond ? PREDICATE_FLAG_DISABLED :
2531                                                          PREDICATE_FLAG_ENABLED);
2532                 break;
2533         case CRITERIA_WATCH_THREAD:
2534                 gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.criteria_combo2),
2535                                          negative_cond ? THREAD_NOT_WATCHED :
2536                                                          THREAD_WATCHED);
2537                 break;
2538         case CRITERIA_IGNORE_THREAD:
2539                 gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.criteria_combo2),
2540                                          negative_cond ? THREAD_NOT_IGNORED :
2541                                                          THREAD_IGNORED);
2542                 break;  
2543         case CRITERIA_PARTIAL:
2544                 gtk_combo_box_set_active(GTK_COMBO_BOX(matcher.criteria_combo2),
2545                                          negative_cond ? PREDICATE_FLAG_DISABLED :
2546                                                          PREDICATE_FLAG_ENABLED);
2547                 break;
2548         }
2549
2550         switch(prop->matchtype) {
2551         case MATCHTYPE_MATCH:
2552 #ifndef G_OS_WIN32
2553                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_checkbtn), FALSE);
2554 #endif
2555                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_checkbtn), TRUE);
2556                 break;
2557
2558         case MATCHTYPE_MATCHCASE:
2559 #ifndef G_OS_WIN32
2560                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_checkbtn), FALSE);
2561 #endif
2562                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_checkbtn), FALSE);
2563                 break;
2564
2565         case MATCHTYPE_REGEXP:
2566 #ifndef G_OS_WIN32
2567                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_checkbtn), TRUE);
2568 #endif
2569                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_checkbtn), TRUE);
2570                 break;
2571
2572         case MATCHTYPE_REGEXPCASE:
2573 #ifndef G_OS_WIN32
2574                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.regexp_checkbtn), TRUE);
2575 #endif
2576                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(matcher.case_checkbtn), FALSE);
2577                 break;
2578         }
2579
2580         g_free(matcher_str);
2581         return TRUE;
2582 }
2583