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