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