2011-10-22 [colin] 3.7.10cvs41
[claws.git] / src / gtk / quicksearch.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 Colin Leroy <colin@colino.net> 
4  * and the Claws Mail team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <ctype.h>
28
29 #include <gtk/gtk.h>
30 #include <gdk/gdkkeysyms.h>
31
32 #if !GTK_CHECK_VERSION(3, 0, 0)
33 #include "gtkcmoptionmenu.h"
34 #endif
35 #include "utils.h"
36 #include "combobox.h"
37 #include "menu.h"
38 #include "prefs_common.h"
39 #include "description_window.h"
40 #include "matcher.h"
41 #include "matcher_parser.h"
42 #include "quicksearch.h"
43 #include "folderview.h"
44 #include "folder.h"
45 #include "prefs_matcher.h"
46 #include "claws.h"
47 #include "statusbar.h"
48
49 struct _QuickSearchRequest
50 {
51         QuickSearchType                  type;
52         gchar                           *matchstring;
53         FolderItem                      *folderItem;
54         gboolean                         recursive;
55 };
56 typedef struct _QuickSearchRequest QuickSearchRequest;
57
58 struct _QuickSearch
59 {
60         GtkWidget                       *hbox_search;
61         GtkWidget                       *search_type;
62 #if !GTK_CHECK_VERSION(3, 0, 0)
63         GtkWidget                       *search_type_opt;
64 #endif
65         GtkWidget                       *search_string_entry;
66         GtkWidget                       *search_condition_expression;
67         GtkWidget                       *search_description;
68         GtkWidget                       *clear_search;
69
70         gboolean                         active;
71         gchar                           *search_string;
72         MatcherList                     *matcher_list;
73
74         QuickSearchRequest              *request;
75         QuickSearchExecuteCallback       callback;
76         gpointer                         callback_data;
77         gboolean                         running;
78         gboolean                         has_focus;
79         gboolean                         matching;
80         gboolean                         deferred_free;
81         FolderItem                      *root_folder_item;
82         gboolean                         is_fast;
83         gboolean                         in_typing;
84         guint                            press_timeout_id;
85
86         GList                           *normal_search_strings;
87         GList                           *extended_search_strings;
88         
89         /* dynamic and autorun qs settings are exclusive*/
90         GtkWidget                        *dynamic_menuitem;
91         GtkWidget                        *autorun_menuitem;
92
93         gboolean                        gui;
94 };
95
96 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run);
97 static void quicksearch_set_matchstring(QuickSearch *quicksearch, const gchar *matchstring);
98 static void quicksearch_set_active(QuickSearch *quicksearch, gboolean active);
99 static void quicksearch_reset_folder_items(QuickSearch *quicksearch, FolderItem *folder_item);
100 static gchar *expand_search_string(const gchar *str);
101 static gchar *expand_tag_search_string(const gchar *str);
102
103 static gboolean quicksearch_from_gui(QuickSearch *quicksearch)
104 {
105         return quicksearch->gui;
106 }
107
108 gboolean quicksearch_is_fast(QuickSearch *quicksearch)
109 {
110         return quicksearch->is_fast;
111 }
112
113 void quicksearch_set_recursive(QuickSearch *quicksearch, gboolean recursive)
114 {
115         quicksearch->request->recursive = recursive;
116 }
117
118 static void quicksearch_set_type(QuickSearch *quicksearch, gint type)
119 {
120 #if !GTK_CHECK_VERSION(3, 0, 0)
121         gint index;
122         quicksearch->request->type = type;
123         if (quicksearch->gui == FALSE)
124                 return;
125         index = menu_find_option_menu_index(GTK_CMOPTION_MENU(quicksearch->search_type_opt), 
126                                         GINT_TO_POINTER(type),
127                                         NULL);
128         gtk_cmoption_menu_set_history(GTK_CMOPTION_MENU(quicksearch->search_type_opt), index);  
129 #endif
130 }
131
132 static gchar *quicksearch_get_text(QuickSearch * quicksearch)
133 {
134         gchar *search_string = gtk_editable_get_chars(GTK_EDITABLE(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))), 0, -1);
135
136         g_strstrip(search_string);
137         return search_string;
138 }
139
140 static void quicksearch_set_popdown_strings(QuickSearch *quicksearch)
141 {
142         GtkWidget *search_string_entry = quicksearch->search_string_entry;
143
144         combobox_unset_popdown_strings(GTK_COMBO_BOX(search_string_entry));
145
146         if (prefs_common.summary_quicksearch_type == QUICK_SEARCH_EXTENDED)
147                 combobox_set_popdown_strings(GTK_COMBO_BOX(search_string_entry),
148                         quicksearch->extended_search_strings);  
149         else
150                 combobox_set_popdown_strings(GTK_COMBO_BOX(search_string_entry),
151                         quicksearch->normal_search_strings);
152 }
153
154 static void prepare_matcher(QuickSearch *quicksearch)
155 {
156         /* param search_string is "matchstring" */
157         const gchar *search_string;
158         QuickSearchType quicksearch_type;
159
160         if (quicksearch == NULL)
161                 return;
162
163         /* When called from the GUI, reset type and matchstring */
164         if (quicksearch_from_gui(quicksearch)) {
165                 gchar *s = quicksearch_get_text(quicksearch);
166                 quicksearch_set_matchstring(quicksearch, s);
167                 g_free(s);
168                 quicksearch->request->type = prefs_common.summary_quicksearch_type;
169         }
170         quicksearch_type = quicksearch->request->type;
171         search_string = quicksearch->request->matchstring;
172
173         if (search_string == NULL || search_string[0] == '\0') {
174                 quicksearch_set_active(quicksearch, FALSE);
175         }
176
177         if (quicksearch->matcher_list != NULL) {
178                 if (quicksearch->matching) {
179                         quicksearch->deferred_free = TRUE;
180                         return;
181                 }
182                 quicksearch->deferred_free = FALSE;
183                 matcherlist_free(quicksearch->matcher_list);
184                 quicksearch->matcher_list = NULL;
185         }
186
187         if (search_string == NULL || search_string[0] == '\0') {
188                 return;
189         }
190         if (quicksearch_type == QUICK_SEARCH_EXTENDED) {
191                 char *newstr = NULL;
192
193                 newstr = expand_search_string(search_string);
194                 if (newstr && newstr[0] != '\0') {
195                         quicksearch->matcher_list = matcher_parser_get_cond(newstr, &quicksearch->is_fast);
196                         g_free(newstr);
197                 } else {
198                         quicksearch->matcher_list = NULL;
199                         quicksearch_set_active(quicksearch, FALSE);
200                         return;
201                 }
202         } else if (quicksearch_type == QUICK_SEARCH_TAG) {
203                 char *newstr = expand_tag_search_string(search_string);
204                 quicksearch->matcher_list = matcher_parser_get_cond(newstr, &quicksearch->is_fast);
205                 g_free(newstr);
206         } else if (quicksearch_type == QUICK_SEARCH_MIXED) {
207                 char *newstr = expand_tag_search_string(search_string);
208                 quicksearch->matcher_list = matcher_parser_get_cond(newstr, &quicksearch->is_fast);
209                 g_free(newstr);
210                 g_free(quicksearch->search_string);
211                 quicksearch->search_string = g_utf8_casefold(search_string, -1);
212         } else {
213                 quicksearch->is_fast = TRUE;
214                 g_free(quicksearch->search_string);
215                 quicksearch->search_string = g_utf8_casefold(search_string, -1);
216         }
217         quicksearch_set_active(quicksearch, TRUE);
218 }
219
220 static void update_extended_buttons (QuickSearch *quicksearch)
221 {
222         GtkWidget *expr_btn = quicksearch->search_condition_expression;
223         GtkWidget *ext_btn = quicksearch->search_description;
224
225         cm_return_if_fail(expr_btn != NULL);
226         cm_return_if_fail(ext_btn != NULL);
227
228         if (prefs_common.summary_quicksearch_type == QUICK_SEARCH_EXTENDED) {
229                 gtk_widget_show(expr_btn);
230                 gtk_widget_show(ext_btn);
231         } else {
232                 gtk_widget_hide(expr_btn);
233                 gtk_widget_hide(ext_btn);
234         }
235 }
236
237 static gboolean searchbar_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
238                                   QuickSearch *qs)
239 {
240         qs->has_focus = TRUE;
241         return FALSE;
242 }
243
244 static gboolean searchbar_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
245                                   QuickSearch *qs)
246 {
247         qs->has_focus = FALSE;
248         qs->in_typing = FALSE;
249         return FALSE;
250 }
251
252 gboolean quicksearch_has_focus(QuickSearch *quicksearch)
253 {
254         return quicksearch->has_focus;
255 }
256
257 static void searchbar_run(QuickSearch *quicksearch, gboolean run_only_if_fast)
258 {
259         gchar *search_string = quicksearch_get_text(quicksearch);
260         quicksearch_set_matchstring(quicksearch, search_string);
261         prepare_matcher(quicksearch);
262
263         /* add to history, for extended search add only correct matching rules */
264         if (!quicksearch->in_typing && search_string && strlen(search_string) != 0) {
265                 switch (prefs_common.summary_quicksearch_type) {
266                         case QUICK_SEARCH_EXTENDED:
267                                 if (quicksearch->matcher_list) {
268                                         quicksearch->extended_search_strings =
269                                                 add_history(quicksearch->extended_search_strings,
270                                                                 g_strdup(search_string));
271                                         prefs_common.summary_quicksearch_history =
272                                                 add_history(prefs_common.summary_quicksearch_history,
273                                                                 g_strdup(search_string));
274                                 }
275                                 break;
276                         default:
277                                 quicksearch->normal_search_strings =
278                                         add_history(quicksearch->normal_search_strings,
279                                                         g_strdup(search_string));               
280                                 prefs_common.summary_quicksearch_history =
281                                         add_history(prefs_common.summary_quicksearch_history,
282                                                         g_strdup(search_string));
283                                 break;
284                 }
285
286                 quicksearch_set_popdown_strings(quicksearch);
287
288         }
289
290         if (run_only_if_fast && !quicksearch->is_fast) {
291                 g_free(search_string);
292                 return;
293         }
294         if (quicksearch->matcher_list == NULL && 
295             prefs_common.summary_quicksearch_type == QUICK_SEARCH_EXTENDED &&
296             search_string && strlen(search_string) != 0) {
297                 g_free(search_string);
298                 return;
299         }
300         quicksearch_set_running(quicksearch, TRUE);
301         if (quicksearch->callback != NULL)
302                 quicksearch->callback(quicksearch, quicksearch->callback_data);
303         quicksearch_set_running(quicksearch, FALSE);
304         g_free(search_string);
305 }
306
307 static int searchbar_changed_timeout(void *data)
308 {
309         QuickSearch *qs = (QuickSearch *)data;
310         if (qs && prefs_common.summary_quicksearch_dynamic) {
311                 qs->in_typing = TRUE;
312                 searchbar_run(qs, TRUE);
313         }
314         return FALSE;
315 }
316
317 static void searchbar_changed_cb(GtkWidget *widget, QuickSearch *qs)
318 {
319         if (!qs->has_focus && prefs_common.summary_quicksearch_autorun) {
320                 gtk_widget_grab_focus(qs->search_string_entry);
321                 searchbar_run(qs, TRUE);
322                 return;
323         }
324
325         if (prefs_common.summary_quicksearch_dynamic) {
326                 if (qs->press_timeout_id != -1) {
327                         g_source_remove(qs->press_timeout_id);
328                 }
329                 qs->press_timeout_id = g_timeout_add(500,
330                                 searchbar_changed_timeout, qs);
331         }
332
333         if (!qs->has_focus)
334                 gtk_widget_grab_focus(qs->search_string_entry);
335 }
336
337 static gboolean searchbar_pressed(GtkWidget *widget, GdkEventKey *event,
338                                   QuickSearch *quicksearch)
339 {
340         if (event && (event->keyval == GDK_KEY_Escape)) {
341                 gchar *str;
342
343                 quicksearch->in_typing = FALSE;
344
345                 str = quicksearch_get_text(quicksearch);
346                 cm_return_val_if_fail(str != NULL, TRUE);
347
348                 /* If the string entry is empty -> hide quicksearch bar. If not -> empty it */
349                 if (!*str) {
350                         summaryview_activate_quicksearch(
351                                 mainwindow_get_mainwindow()->summaryview, 
352                                 FALSE);
353                 } else {
354                         quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
355                         gtk_widget_grab_focus(
356                                         mainwindow_get_mainwindow()->summaryview->ctree);
357                 }
358                 g_free(str);
359                 return TRUE;
360         }
361
362         if (event != NULL && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
363                 if (quicksearch->press_timeout_id != -1) {
364                         g_source_remove(quicksearch->press_timeout_id);
365                         quicksearch->press_timeout_id = -1;
366                 }
367                 quicksearch->in_typing = FALSE;
368                 /* add expression to history list and exec quicksearch */
369                 searchbar_run(quicksearch, FALSE);
370
371                 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
372                 return TRUE;
373         }
374
375         if (event && (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_Up)) {
376                 combobox_set_value_from_arrow_key(
377                                 GTK_COMBO_BOX(quicksearch->search_string_entry),
378                                 event->keyval);
379                 return TRUE;
380         }
381
382         return FALSE;
383 }
384
385 static gboolean searchtype_changed(GtkMenuItem *widget, gpointer data)
386 {
387         QuickSearch *quicksearch = (QuickSearch *)data;
388         gchar *search_string = quicksearch_get_text(quicksearch);
389         quicksearch_set_matchstring(quicksearch, search_string);
390
391         prefs_common.summary_quicksearch_type = GPOINTER_TO_INT(g_object_get_data(
392                                    G_OBJECT(GTK_MENU_ITEM(gtk_menu_get_active(
393                                    GTK_MENU(quicksearch->search_type)))), MENU_VAL_ID));
394
395         /* Show extended search description button, only when Extended is selected */
396         update_extended_buttons(quicksearch);
397         quicksearch_set_popdown_strings(quicksearch);
398
399         if (!search_string || *(search_string) == 0) {
400                 g_free(search_string);
401                 return TRUE;
402         }
403
404         prepare_matcher(quicksearch);
405
406         quicksearch_set_running(quicksearch, TRUE);
407         if (quicksearch->callback != NULL)
408                 quicksearch->callback(quicksearch, quicksearch->callback_data);
409         quicksearch_set_running(quicksearch, FALSE);
410         g_free(search_string);
411         return TRUE;
412 }
413
414 static gboolean searchtype_recursive_changed(GtkMenuItem *widget, gpointer data)
415 {
416         QuickSearch *quicksearch = (QuickSearch *)data;
417         gboolean checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
418         gchar *search_string = quicksearch_get_text(quicksearch);
419         /* not needed to quicksearch_set_matchstring(search_string);
420            wait for prepare_matcher() */
421
422         prefs_common.summary_quicksearch_recurse = checked;
423         quicksearch_set_recursive(quicksearch, checked);
424
425         /* reselect the search type */
426         quicksearch_set_type(quicksearch, prefs_common.summary_quicksearch_type);
427
428         if (!search_string || *(search_string) == 0) {
429                 g_free(search_string);
430                 return TRUE;
431         }
432
433         prepare_matcher(quicksearch);
434
435         quicksearch_set_running(quicksearch, TRUE);
436         if (quicksearch->callback != NULL)
437                 quicksearch->callback(quicksearch, quicksearch->callback_data);
438         quicksearch_set_running(quicksearch, FALSE);
439         g_free(search_string);
440         return TRUE;
441 }
442
443 static gboolean searchtype_sticky_changed(GtkMenuItem *widget, gpointer data)
444 {
445         QuickSearch *quicksearch = (QuickSearch *)data;
446         gboolean checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
447
448         prefs_common.summary_quicksearch_sticky = checked;
449
450         /* reselect the search type */
451         quicksearch_set_type(quicksearch, prefs_common.summary_quicksearch_type);
452
453         return TRUE;
454 }
455
456 static gboolean searchtype_dynamic_changed(GtkMenuItem *widget, gpointer data)
457 {
458         QuickSearch *quicksearch = (QuickSearch *)data;
459         gboolean checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
460
461         prefs_common.summary_quicksearch_dynamic = checked;
462         if (checked)
463                 gtk_check_menu_item_set_active(
464                                 GTK_CHECK_MENU_ITEM(quicksearch->autorun_menuitem),
465                                 FALSE);
466
467         /* reselect the search type */
468         quicksearch_set_type(quicksearch, prefs_common.summary_quicksearch_type);
469
470         return TRUE;
471 }
472
473 static gboolean searchtype_autorun_changed(GtkMenuItem *widget, gpointer data)
474 {
475         QuickSearch *quicksearch = (QuickSearch *)data;
476         gboolean checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
477
478         prefs_common.summary_quicksearch_autorun = checked;
479         if (checked)
480                 gtk_check_menu_item_set_active(
481                                 GTK_CHECK_MENU_ITEM(quicksearch->dynamic_menuitem),
482                                 FALSE);
483
484         /* reselect the search type */
485         quicksearch_set_type(quicksearch, prefs_common.summary_quicksearch_type);
486
487         return TRUE;
488 }
489
490 /*
491  * Strings describing how to use Extended Search
492  *
493  * When adding new lines, remember to put 2 strings for each line
494  */
495 static gchar *search_descr_strings[] = {
496         "a",     N_("all messages"),
497         "ag #",  N_("messages whose age is greater than #"),
498         "al #",  N_("messages whose age is less than #"),
499         "b S",   N_("messages which contain S in the message body"),
500         "B S",   N_("messages which contain S in the whole message"),
501         "c S",   N_("messages carbon-copied to S"),
502         "C S",   N_("message is either to: or cc: to S"),
503         "D",     N_("deleted messages"), /** how I can filter deleted messages **/
504         "e S",   N_("messages which contain S in the Sender field"),
505         "E S",   N_("true if execute \"S\" succeeds"),
506         "f S",   N_("messages originating from user S"),
507         "F",     N_("forwarded messages"),
508         "h S",   N_("messages which contain header S"),
509         "i S",   N_("messages which contain S in Message-ID header"),
510         "I S",   N_("messages which contain S in In-Reply-To header"),
511         "k #",   N_("messages which are marked with color #"),
512         "L",     N_("locked messages"),
513         "n S",   N_("messages which are in newsgroup S"),
514         "N",     N_("new messages"),
515         "O",     N_("old messages"),
516         "p",     N_("incomplete messages (not entirely downloaded)"),
517         "r",     N_("messages which have been replied to"),
518         "R",     N_("read messages"),
519         "s S",   N_("messages which contain S in subject"),
520         "se #",  N_("messages whose score is equal to #"),
521         "sg #",  N_("messages whose score is greater than #"),
522         "sl #",  N_("messages whose score is lower than #"),
523         "Se #",  N_("messages whose size is equal to #"),
524         "Sg #",  N_("messages whose size is greater than #"),
525         "Ss #",  N_("messages whose size is smaller than #"),
526         "t S",   N_("messages which have been sent to S"),
527         "tg S",  N_("messages which tags contain S"),
528         "tagged",N_("messages which have tag(s)"),
529         "T",     N_("marked messages"),
530         "U",     N_("unread messages"),
531         "x S",   N_("messages which contain S in References header"),
532         "X \"cmd args\"", N_("messages returning 0 when passed to command - %F is message file"),
533         "y S",   N_("messages which contain S in X-Label header"),
534         "",      "" ,
535         "&amp;",         N_("logical AND operator"),
536         "|",     N_("logical OR operator"),
537         "! or ~",       N_("logical NOT operator"),
538         "%",     N_("case sensitive search"),
539         "",      "" ,
540         " ",     N_("all filtering expressions are allowed"),
541         NULL,    NULL
542 };
543
544 static DescriptionWindow search_descr = {
545         NULL,
546         NULL,
547         2,
548         N_("Extended Search"),
549         N_("Extended Search allows the user to define criteria that messages must "
550            "have in order to match and be displayed in the message list.\n"
551            "The following symbols can be used:"),
552         search_descr_strings
553 };
554
555 static void search_description_cb(GtkWidget *widget)
556 {
557         description_window_create(&search_descr);
558 };
559
560 static gboolean clear_search_cb(GtkMenuItem *widget, gpointer data)
561 {
562         QuickSearch *quicksearch = (QuickSearch *)data;
563
564         if (!quicksearch->active)
565                 return TRUE;
566
567         quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
568
569         return TRUE;
570 };
571
572 static void search_condition_expr_done(MatcherList * matchers)
573 {
574         gchar *str;
575
576         cm_return_if_fail(
577                         mainwindow_get_mainwindow()->summaryview->quicksearch != NULL);
578
579         if (matchers == NULL)
580                 return;
581
582         str = matcherlist_to_string(matchers);
583
584         if (str != NULL) {
585                 quicksearch_set(mainwindow_get_mainwindow()->summaryview->quicksearch,
586                                 prefs_common.summary_quicksearch_type, str);
587                 g_free(str);
588
589                 /* add expression to history list and exec quicksearch */
590                 searchbar_run(mainwindow_get_mainwindow()->summaryview->quicksearch, FALSE);
591         }
592 }
593
594 static gboolean search_condition_expr(GtkMenuItem *widget, gpointer data)
595 {
596         gchar * cond_str;
597         MatcherList * matchers = NULL;
598         
599         cm_return_val_if_fail(
600                         mainwindow_get_mainwindow()->summaryview->quicksearch != NULL,
601                         FALSE);
602
603         /* re-use the current quicksearch value if it's a condition expression,
604            otherwise ignore it silently */
605         cond_str = quicksearch_get_text(mainwindow_get_mainwindow()->summaryview->quicksearch);
606
607         if (*cond_str != '\0') {
608                 matchers = matcher_parser_get_cond((gchar*)cond_str, NULL);
609         }
610
611         prefs_matcher_open(matchers, search_condition_expr_done);
612
613         if (matchers != NULL)
614                 matcherlist_free(matchers);
615
616         g_free(cond_str);
617
618         return TRUE;
619 };
620
621 static void quicksearch_set_button(GtkButton *button, const gchar *icon, const gchar *text)
622 {
623         GList *children = gtk_container_get_children(GTK_CONTAINER(button));
624         GList *cur;
625         GtkWidget *box;
626         gboolean icon_visible;
627
628         g_object_get(gtk_settings_get_default(), 
629                                          "gtk-button-images", &icon_visible, 
630                                          NULL);
631
632         for (cur = children; cur; cur = cur->next)
633                 gtk_container_remove(GTK_CONTAINER(button), GTK_WIDGET(cur->data));
634         
635         g_list_free(children);
636         box = gtk_hbox_new(FALSE, 0);
637         
638         gtk_container_add(GTK_CONTAINER(button), box);
639         if (icon_visible || !text || !*text)
640                 gtk_box_pack_start(GTK_BOX(box), gtk_image_new_from_stock(icon, 
641                         GTK_ICON_SIZE_BUTTON), FALSE, FALSE, 0);
642         gtk_box_pack_start(GTK_BOX(box), gtk_label_new_with_mnemonic(text), FALSE, FALSE, 0);
643         gtk_widget_show_all(box);
644 }
645
646 /*
647  * Builds a new QuickSearchRequest
648  */
649 static QuickSearchRequest *quicksearchrequest_new(void)
650 {
651         QuickSearchRequest *request;
652         request = g_new0(QuickSearchRequest, 1);
653         return request;
654 }
655
656 /*
657  * Builds a new QuickSearch object independent from the GUI
658  */
659 QuickSearch *quicksearch_new_nogui(void)
660 {
661         QuickSearch *quicksearch;
662         QuickSearchRequest *request;
663
664         request = quicksearchrequest_new();
665         quicksearch = g_new0(QuickSearch, 1);
666         quicksearch->request = request;
667         quicksearch->gui = FALSE;
668
669         /* init. values initally found in quicksearch_new().
670            There's no need to init. all pointers to NULL since we use g_new0
671          */
672         quicksearch->matcher_list = NULL;
673         quicksearch->active = FALSE;
674         quicksearch->running = FALSE;
675         quicksearch->in_typing = FALSE;
676         quicksearch->press_timeout_id = -1;
677         quicksearch->normal_search_strings = NULL;
678         quicksearch->extended_search_strings = NULL;
679
680         return quicksearch;
681 }
682
683 QuickSearch *quicksearch_new()
684 {
685         QuickSearch *quicksearch;
686
687         GtkWidget *hbox_search;
688 #if !GTK_CHECK_VERSION(3, 0, 0)
689         GtkWidget *search_type_opt;
690 #endif
691         GtkWidget *search_type;
692         GtkWidget *search_string_entry;
693         GtkWidget *search_hbox;
694         GtkWidget *search_description;
695         GtkWidget *clear_search;
696         GtkWidget *search_condition_expression;
697         GtkWidget *menuitem;
698         GtkWidget *vbox;
699
700         quicksearch = quicksearch_new_nogui();
701         quicksearch->gui = TRUE;
702
703         /* quick search */
704         hbox_search = gtk_hbox_new(FALSE, 0);
705
706 #if !GTK_CHECK_VERSION(3, 0, 0)
707         search_type_opt = gtk_cmoption_menu_new();
708         gtk_widget_show(search_type_opt);
709         gtk_box_pack_start(GTK_BOX(hbox_search), search_type_opt, FALSE, FALSE, 0);
710 #endif
711
712         search_type = gtk_menu_new();
713         MENUITEM_ADD (search_type, menuitem,
714                         prefs_common_translated_header_name("Subject"), QUICK_SEARCH_SUBJECT);
715         g_signal_connect(G_OBJECT(menuitem), "activate",
716                          G_CALLBACK(searchtype_changed),
717                          quicksearch);
718         MENUITEM_ADD (search_type, menuitem,
719                         prefs_common_translated_header_name("From"), QUICK_SEARCH_FROM);
720         g_signal_connect(G_OBJECT(menuitem), "activate",
721                          G_CALLBACK(searchtype_changed),
722                          quicksearch);
723         MENUITEM_ADD (search_type, menuitem,
724                         prefs_common_translated_header_name("To"), QUICK_SEARCH_TO);
725         g_signal_connect(G_OBJECT(menuitem), "activate",
726                          G_CALLBACK(searchtype_changed),
727                          quicksearch);
728         MENUITEM_ADD (search_type, menuitem,
729                         prefs_common_translated_header_name("Tag"), QUICK_SEARCH_TAG);
730         g_signal_connect(G_OBJECT(menuitem), "activate",
731                          G_CALLBACK(searchtype_changed),
732                          quicksearch);
733         MENUITEM_ADD (search_type, menuitem,
734                         _("From/To/Subject/Tag"), QUICK_SEARCH_MIXED);
735         g_signal_connect(G_OBJECT(menuitem), "activate",
736                          G_CALLBACK(searchtype_changed),
737                          quicksearch);
738         MENUITEM_ADD (search_type, menuitem, _("Extended"), QUICK_SEARCH_EXTENDED);
739         g_signal_connect(G_OBJECT(menuitem), "activate",
740                          G_CALLBACK(searchtype_changed),
741                          quicksearch);
742
743         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), gtk_separator_menu_item_new());
744
745         menuitem = gtk_check_menu_item_new_with_label(_("Recursive"));
746         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
747
748         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
749                                         prefs_common.summary_quicksearch_recurse);
750         quicksearch_set_recursive(quicksearch, prefs_common.summary_quicksearch_recurse);
751         g_signal_connect(G_OBJECT(menuitem), "activate",
752                          G_CALLBACK(searchtype_recursive_changed),
753                          quicksearch);
754
755         menuitem = gtk_check_menu_item_new_with_label(_("Sticky"));
756         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
757
758         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
759                                         prefs_common.summary_quicksearch_sticky);
760
761         g_signal_connect(G_OBJECT(menuitem), "activate",
762                          G_CALLBACK(searchtype_sticky_changed),
763                          quicksearch);
764
765         menuitem = gtk_check_menu_item_new_with_label(_("Type-ahead"));
766         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
767
768         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
769                                         prefs_common.summary_quicksearch_dynamic);
770
771         quicksearch->dynamic_menuitem = menuitem;
772
773         g_signal_connect(G_OBJECT(menuitem), "activate",
774                          G_CALLBACK(searchtype_dynamic_changed),
775                          quicksearch);
776
777         menuitem = gtk_check_menu_item_new_with_label(_("Run on select"));
778         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
779
780         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
781                                         prefs_common.summary_quicksearch_autorun);
782
783         quicksearch->autorun_menuitem = menuitem;
784
785         g_signal_connect(G_OBJECT(menuitem), "activate",
786                          G_CALLBACK(searchtype_autorun_changed),
787                          quicksearch);
788
789 #if !GTK_CHECK_VERSION(3, 0, 0)
790         gtk_cmoption_menu_set_menu(GTK_CMOPTION_MENU(search_type_opt), search_type);
791
792         quicksearch->search_type_opt = search_type_opt;
793 #endif
794         quicksearch_set_type(quicksearch, prefs_common.summary_quicksearch_type);
795
796         gtk_widget_show(search_type);
797
798         search_string_entry = gtk_combo_box_entry_new_text ();
799         gtk_combo_box_set_active(GTK_COMBO_BOX(search_string_entry), -1);
800
801         vbox = gtk_vbox_new(TRUE, 0);
802         gtk_box_pack_start(GTK_BOX(vbox), search_string_entry, FALSE, FALSE, 0);
803         gtk_box_pack_start(GTK_BOX(hbox_search), vbox, TRUE, TRUE, 4);
804
805         gtk_widget_show(vbox);
806         gtk_widget_show(search_string_entry);
807
808         search_hbox = gtk_hbox_new(FALSE, 5);
809         clear_search = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
810         gtk_box_pack_start(GTK_BOX(search_hbox), clear_search,
811                            FALSE, FALSE, 0);
812         g_signal_connect(G_OBJECT(clear_search), "clicked",
813                          G_CALLBACK(clear_search_cb), quicksearch);
814         CLAWS_SET_TIP(clear_search,
815                              _("Clear the current search"));
816         gtk_widget_show(clear_search);
817
818         search_condition_expression = gtk_button_new_from_stock(GTK_STOCK_EDIT);
819         gtk_box_pack_start(GTK_BOX(search_hbox), search_condition_expression,
820                            FALSE, FALSE, 0);
821         g_signal_connect(G_OBJECT (search_condition_expression), "clicked",
822                          G_CALLBACK(search_condition_expr),
823                          quicksearch);
824         CLAWS_SET_TIP(search_condition_expression,
825                              _("Edit search criteria"));
826         gtk_widget_show(search_condition_expression);
827
828         search_description = gtk_button_new_from_stock(GTK_STOCK_INFO);
829         gtk_box_pack_start(GTK_BOX(search_hbox), search_description,
830                            FALSE, FALSE, 0);
831         g_signal_connect(G_OBJECT(search_description), "clicked",
832                          G_CALLBACK(search_description_cb), NULL);
833         CLAWS_SET_TIP(search_description,
834                              _("Information about extended symbols"));
835         gtk_widget_show(search_description);
836
837         gtk_box_pack_start(GTK_BOX(hbox_search), search_hbox, FALSE, FALSE, 2);
838         gtk_widget_show(search_hbox);
839
840         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((search_string_entry)))),
841                            "key_press_event",
842                            G_CALLBACK(searchbar_pressed),
843                            quicksearch);
844
845         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((search_string_entry)))),
846                          "changed",
847                          G_CALLBACK(searchbar_changed_cb),
848                          quicksearch);
849
850         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((search_string_entry)))),
851                          "focus_in_event",
852                          G_CALLBACK(searchbar_focus_evt_in),
853                          quicksearch);
854         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((search_string_entry)))),
855                          "focus_out_event",
856                          G_CALLBACK(searchbar_focus_evt_out),
857                          quicksearch);
858
859         quicksearch->hbox_search = hbox_search;
860         quicksearch->search_type = search_type;
861         quicksearch->search_string_entry = search_string_entry;
862         quicksearch->search_condition_expression = search_condition_expression;
863         quicksearch->search_description = search_description;
864         quicksearch->matcher_list = NULL;
865         quicksearch->active = FALSE;
866         quicksearch->running = FALSE;
867         quicksearch->clear_search = clear_search;
868         quicksearch->in_typing = FALSE;
869         quicksearch->press_timeout_id = -1;
870         quicksearch->normal_search_strings = NULL;
871         quicksearch->extended_search_strings = NULL;
872
873         quicksearch_set_button(GTK_BUTTON(quicksearch->search_description), GTK_STOCK_INFO, _("_Information"));
874         quicksearch_set_button(GTK_BUTTON(quicksearch->search_condition_expression), GTK_STOCK_EDIT, _("_Edit"));
875         quicksearch_set_button(GTK_BUTTON(quicksearch->clear_search), GTK_STOCK_CLEAR, _("C_lear"));
876         
877         update_extended_buttons(quicksearch);
878
879         return quicksearch;
880 }
881
882 void quicksearch_relayout(QuickSearch *quicksearch)
883 {
884         switch (prefs_common.layout_mode) {
885         case NORMAL_LAYOUT:
886         case WIDE_LAYOUT:
887         case WIDE_MSGLIST_LAYOUT:
888                 quicksearch_set_button(GTK_BUTTON(quicksearch->search_description), GTK_STOCK_INFO, _("_Information"));
889                 quicksearch_set_button(GTK_BUTTON(quicksearch->search_condition_expression), GTK_STOCK_EDIT, _("_Edit"));
890                 quicksearch_set_button(GTK_BUTTON(quicksearch->clear_search), GTK_STOCK_CLEAR, _("C_lear"));
891                 break;
892         case SMALL_LAYOUT:
893         case VERTICAL_LAYOUT:
894                 quicksearch_set_button(GTK_BUTTON(quicksearch->search_description), GTK_STOCK_INFO, "");
895                 quicksearch_set_button(GTK_BUTTON(quicksearch->search_condition_expression), GTK_STOCK_EDIT, "");
896                 quicksearch_set_button(GTK_BUTTON(quicksearch->clear_search), GTK_STOCK_CLEAR, "");
897                 break;
898         }
899 }
900
901 GtkWidget *quicksearch_get_widget(QuickSearch *quicksearch)
902 {
903         return quicksearch->hbox_search;
904 }
905
906 void quicksearch_show(QuickSearch *quicksearch)
907 {
908         MainWindow *mainwin = mainwindow_get_mainwindow();
909         GtkWidget *ctree = NULL;
910         prepare_matcher(quicksearch);
911         gtk_widget_show(quicksearch->hbox_search);
912         update_extended_buttons(quicksearch);
913         gtk_widget_grab_focus(
914                 GTK_WIDGET(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))));
915
916         GTK_EVENTS_FLUSH();
917
918         if (!mainwin || !mainwin->summaryview) {
919                 return;
920         }
921         
922         ctree = summary_get_main_widget(mainwin->summaryview);
923         
924         if (ctree && mainwin->summaryview->selected)
925                 gtk_cmctree_node_moveto(GTK_CMCTREE(ctree), 
926                                 mainwin->summaryview->selected, 
927                                 0, 0.5, 0);
928 }
929
930 void quicksearch_hide(QuickSearch *quicksearch)
931 {
932         if (quicksearch_is_active(quicksearch)) {
933                 quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
934                 quicksearch_set_active(quicksearch, FALSE);
935         }
936         gtk_widget_hide(quicksearch->hbox_search);
937 }
938
939 /*
940  *\brief        Sets the matchstring.
941  *
942  *\param        quicksearch quicksearch to set
943  *\param        matchstring the match string; it is duplicated, not stored
944  */
945 static void quicksearch_set_matchstring(QuickSearch *quicksearch,
946                                         const gchar *matchstring)
947 {
948         g_free(quicksearch->request->matchstring);
949         quicksearch->request->matchstring = g_strdup(matchstring);
950 }
951
952 void quicksearch_set(QuickSearch *quicksearch, QuickSearchType type,
953                      const gchar *matchstring)
954 {
955         quicksearch_set_type(quicksearch, type);
956
957         if (!matchstring || !(*matchstring))
958                 quicksearch->in_typing = FALSE;
959
960         quicksearch_set_matchstring(quicksearch, matchstring);
961
962         if (!quicksearch_from_gui(quicksearch)) {
963                 prepare_matcher(quicksearch);
964                 /* no callback */
965                 return;
966         }
967                 
968         g_signal_handlers_block_by_func(G_OBJECT(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))),
969                                         G_CALLBACK(searchbar_changed_cb), quicksearch);
970         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))),
971                            matchstring);
972         g_signal_handlers_unblock_by_func(G_OBJECT(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry)))),
973                                           G_CALLBACK(searchbar_changed_cb), quicksearch);
974
975         prefs_common.summary_quicksearch_type = type;
976
977         prepare_matcher(quicksearch);
978
979         quicksearch_set_running(quicksearch, TRUE);
980         if (quicksearch->callback != NULL)
981                 quicksearch->callback(quicksearch, quicksearch->callback_data);
982         quicksearch_set_running(quicksearch, FALSE);
983 }
984
985 gboolean quicksearch_is_active(QuickSearch *quicksearch)
986 {
987         return quicksearch->active && 
988                 (prefs_common.summary_quicksearch_type != QUICK_SEARCH_EXTENDED
989                  || quicksearch->matcher_list != NULL);
990 }
991
992 static void quicksearch_set_active(QuickSearch *quicksearch, gboolean active)
993 {
994 #if !GTK_CHECK_VERSION(3, 0, 0)
995         static GdkColor yellow;
996         static GdkColor red;
997         static GdkColor black;
998         static gboolean colors_initialised = FALSE;
999 #else
1000         static GdkColor yellow = { (guint32)0, (guint16)0xf5, (guint16)0xf6, (guint16)0xbe };
1001         static GdkColor red = { (guint32)0, (guint16)0xff, (guint16)0x70, (guint16)0x70 };
1002         static GdkColor black = { (guint32)0, (guint16)0x0, (guint16)0x0, (guint16)0x0 };
1003 #endif
1004         gboolean error = FALSE;
1005
1006         
1007         quicksearch->active = active;
1008         if (quicksearch->gui == FALSE)
1009                 return;
1010
1011 #if !GTK_CHECK_VERSION(3, 0, 0)
1012         if (!colors_initialised) {
1013                 gdk_color_parse("#f5f6be", &yellow);
1014                 gdk_color_parse("#000000", &black);
1015                 gdk_color_parse("#ff7070", &red);
1016                 colors_initialised = gdk_colormap_alloc_color(
1017                         gdk_colormap_get_system(), &yellow, FALSE, TRUE);
1018                 colors_initialised &= gdk_colormap_alloc_color(
1019                         gdk_colormap_get_system(), &black, FALSE, TRUE);
1020                 colors_initialised &= gdk_colormap_alloc_color(
1021                         gdk_colormap_get_system(), &red, FALSE, TRUE);
1022         }
1023 #endif
1024
1025         if (active && 
1026                 (prefs_common.summary_quicksearch_type == QUICK_SEARCH_EXTENDED
1027                  && quicksearch->matcher_list == NULL))
1028                 error = TRUE;
1029
1030         if (active) {
1031                 gtk_widget_set_sensitive(quicksearch->clear_search, TRUE);
1032 #if !GTK_CHECK_VERSION(3, 0, 0)
1033                 if (colors_initialised) {
1034 #endif
1035                         gtk_widget_modify_base(
1036                                 gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))),
1037                                 GTK_STATE_NORMAL, error ? &red : &yellow);
1038                         gtk_widget_modify_text(
1039                                 gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))),
1040                                 GTK_STATE_NORMAL, &black);
1041 #if !GTK_CHECK_VERSION(3, 0, 0)
1042                 }
1043 #endif
1044         } else {
1045                 gtk_widget_set_sensitive(quicksearch->clear_search, FALSE);
1046                 if (colors_initialised) {
1047                         gtk_widget_modify_base(
1048                                 gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))),
1049                                 GTK_STATE_NORMAL, NULL);
1050                         gtk_widget_modify_text(
1051                                 gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))),
1052                                 GTK_STATE_NORMAL, NULL);
1053 #if !GTK_CHECK_VERSION(3, 0, 0)
1054                 }
1055 #endif
1056         }
1057
1058         if (!active) {
1059                 quicksearch_reset_cur_folder_item(quicksearch);
1060         }
1061 }
1062
1063 void quicksearch_set_execute_callback(QuickSearch *quicksearch,
1064                                       QuickSearchExecuteCallback callback,
1065                                       gpointer data)
1066 {
1067         quicksearch->callback = callback;
1068         quicksearch->callback_data = data;
1069 }
1070
1071 gboolean quicksearch_match(QuickSearch *quicksearch, MsgInfo *msginfo)
1072 {
1073         gchar *searched_header = NULL;
1074         gboolean result = FALSE;
1075         gchar *to = NULL, *from = NULL, *subject = NULL;
1076         QuickSearchType quicksearch_type;
1077
1078         if (!quicksearch->active)
1079                 return TRUE;
1080
1081         quicksearch_type = quicksearch->request->type;
1082
1083         switch (quicksearch_type) {
1084         case QUICK_SEARCH_SUBJECT:
1085                 if (msginfo->subject)
1086                         searched_header = g_utf8_casefold(msginfo->subject, -1);
1087                 else
1088                         return FALSE;
1089                 break;
1090         case QUICK_SEARCH_FROM:
1091                 if (msginfo->from)
1092                         searched_header = g_utf8_casefold(msginfo->from, -1);
1093                 else
1094                         return FALSE;
1095                 break;
1096         case QUICK_SEARCH_TO:
1097                 if (msginfo->to)
1098                         searched_header = g_utf8_casefold(msginfo->to, -1);
1099                 else
1100                         return FALSE;
1101                 break;
1102         case QUICK_SEARCH_MIXED:
1103                 if (msginfo->to)
1104                         to = g_utf8_casefold(msginfo->to, -1);
1105                 if (msginfo->from)
1106                         from = g_utf8_casefold(msginfo->from, -1);
1107                 if (msginfo->subject)
1108                         subject = g_utf8_casefold(msginfo->subject, -1);
1109                 break;
1110         case QUICK_SEARCH_EXTENDED:
1111                 break;
1112         default:
1113                 debug_print("unknown search type (%d)\n", quicksearch_type);
1114                 break;
1115         }
1116
1117         quicksearch->matching = TRUE;
1118         if (quicksearch_type != QUICK_SEARCH_EXTENDED &&
1119             quicksearch_type != QUICK_SEARCH_MIXED &&
1120             quicksearch_type != QUICK_SEARCH_TAG &&
1121             quicksearch->search_string &&
1122             searched_header && strstr(searched_header, quicksearch->search_string) != NULL)
1123                 result = TRUE;
1124         else if (quicksearch_type == QUICK_SEARCH_MIXED &&
1125                 quicksearch->search_string && (
1126                 (to && strstr(to, quicksearch->search_string) != NULL) ||
1127                 (from && strstr(from, quicksearch->search_string) != NULL) ||
1128                 (subject && strstr(subject, quicksearch->search_string) != NULL) ||
1129                 ((quicksearch->matcher_list != NULL) &&
1130                  matcherlist_match(quicksearch->matcher_list, msginfo))  ))
1131                 result = TRUE;
1132         else if ((quicksearch->matcher_list != NULL) &&
1133                  matcherlist_match(quicksearch->matcher_list, msginfo))
1134                 result = TRUE;
1135
1136         quicksearch->matching = FALSE;
1137         if (quicksearch_from_gui(quicksearch)==TRUE && quicksearch->deferred_free) {
1138                 /* Ref. http://lists.claws-mail.org/pipermail/users/2010-August/003063.html
1139                    See also 2.0.0cvs140 ChangeLog entry
1140                    and comment in search_msgs_in_folder() */
1141                 prepare_matcher(quicksearch);
1142         }
1143
1144         g_free(to);
1145         g_free(from);
1146         g_free(subject);
1147         g_free(searched_header);
1148
1149         return result;
1150 }
1151
1152 /* allow Mutt-like patterns in quick search */
1153 static gchar *expand_search_string(const gchar *search_string)
1154 {
1155         int i = 0;
1156         gchar term_char, save_char;
1157         gchar *cmd_start, *cmd_end;
1158         GString *matcherstr;
1159         gchar *returnstr = NULL;
1160         gchar *copy_str;
1161         gboolean casesens, dontmatch;
1162         /* list of allowed pattern abbreviations */
1163         struct {
1164                 gchar           *abbreviated;   /* abbreviation */
1165                 gchar           *command;       /* actual matcher command */
1166                 gint            numparams;      /* number of params for cmd */
1167                 gboolean        qualifier;      /* do we append regexpcase */
1168                 gboolean        quotes;         /* do we need quotes */
1169         }
1170         cmds[] = {
1171                 { "a",  "all",                          0,      FALSE,  FALSE },
1172                 { "ag", "age_greater",                  1,      FALSE,  FALSE },
1173                 { "al", "age_lower",                    1,      FALSE,  FALSE },
1174                 { "b",  "body_part",                    1,      TRUE,   TRUE  },
1175                 { "B",  "message",                      1,      TRUE,   TRUE  },
1176                 { "c",  "cc",                           1,      TRUE,   TRUE  },
1177                 { "C",  "to_or_cc",                     1,      TRUE,   TRUE  },
1178                 { "D",  "deleted",                      0,      FALSE,  FALSE },
1179                 { "e",  "header \"Sender\"",            1,      TRUE,   TRUE  },
1180                 { "E",  "execute",                      1,      FALSE,  TRUE  },
1181                 { "f",  "from",                         1,      TRUE,   TRUE  },
1182                 { "F",  "forwarded",                    0,      FALSE,  FALSE },
1183                 { "h",  "headers_part",                 1,      TRUE,   TRUE  },
1184                 { "i",  "header \"Message-ID\"",        1,      TRUE,   TRUE  },
1185                 { "I",  "inreplyto",                    1,      TRUE,   TRUE  },
1186                 { "k",  "colorlabel",                   1,      FALSE,  FALSE },
1187                 { "L",  "locked",                       0,      FALSE,  FALSE },
1188                 { "n",  "newsgroups",                   1,      TRUE,   TRUE  },
1189                 { "N",  "new",                          0,      FALSE,  FALSE },
1190                 { "O",  "~new",                         0,      FALSE,  FALSE },
1191                 { "r",  "replied",                      0,      FALSE,  FALSE },
1192                 { "R",  "~unread",                      0,      FALSE,  FALSE },
1193                 { "s",  "subject",                      1,      TRUE,   TRUE  },
1194                 { "se", "score_equal",                  1,      FALSE,  FALSE },
1195                 { "sg", "score_greater",                1,      FALSE,  FALSE },
1196                 { "sl", "score_lower",                  1,      FALSE,  FALSE },
1197                 { "Se", "size_equal",                   1,      FALSE,  FALSE },
1198                 { "Sg", "size_greater",                 1,      FALSE,  FALSE },
1199                 { "Ss", "size_smaller",                 1,      FALSE,  FALSE },
1200                 { "t",  "to",                           1,      TRUE,   TRUE  },
1201                 { "tg", "tag",                          1,      TRUE,   TRUE  },
1202                 { "T",  "marked",                       0,      FALSE,  FALSE },
1203                 { "U",  "unread",                       0,      FALSE,  FALSE },
1204                 { "x",  "header \"References\"",        1,      TRUE,   TRUE  },
1205                 { "X",  "test",                         1,      FALSE,  FALSE },
1206                 { "y",  "header \"X-Label\"",           1,      TRUE,   TRUE  },
1207                 { "&",  "&",                            0,      FALSE,  FALSE },
1208                 { "|",  "|",                            0,      FALSE,  FALSE },
1209                 { "p",  "partial",                      0,      FALSE,  FALSE },
1210                 { NULL, NULL,                           0,      FALSE,  FALSE }
1211         };
1212
1213         if (search_string == NULL)
1214                 return NULL;
1215
1216         copy_str = g_strdup(search_string);
1217
1218         matcherstr = g_string_sized_new(16);
1219         cmd_start = copy_str;
1220         while (cmd_start && *cmd_start) {
1221                 /* skip all white spaces */
1222                 while (*cmd_start && isspace((guchar)*cmd_start))
1223                         cmd_start++;
1224                 cmd_end = cmd_start;
1225
1226                 /* extract a command */
1227                 while (*cmd_end && !isspace((guchar)*cmd_end))
1228                         cmd_end++;
1229
1230                 /* save character */
1231                 save_char = *cmd_end;
1232                 *cmd_end = '\0';
1233
1234                 dontmatch = FALSE;
1235                 casesens = FALSE;
1236
1237                 /* ~ and ! mean logical NOT */
1238                 if (*cmd_start == '~' || *cmd_start == '!')
1239                 {
1240                         dontmatch = TRUE;
1241                         cmd_start++;
1242                 }
1243                 /* % means case sensitive match */
1244                 if (*cmd_start == '%')
1245                 {
1246                         casesens = TRUE;
1247                         cmd_start++;
1248                 }
1249
1250                 /* find matching abbreviation */
1251                 for (i = 0; cmds[i].command; i++) {
1252                         if (!strcmp(cmd_start, cmds[i].abbreviated)) {
1253                                 /* restore character */
1254                                 *cmd_end = save_char;
1255
1256                                 /* copy command */
1257                                 if (matcherstr->len > 0) {
1258                                         g_string_append(matcherstr, " ");
1259                                 }
1260                                 if (dontmatch)
1261                                         g_string_append(matcherstr, "~");
1262                                 g_string_append(matcherstr, cmds[i].command);
1263                                 g_string_append(matcherstr, " ");
1264
1265                                 /* stop if no params required */
1266                                 if (cmds[i].numparams == 0)
1267                                         break;
1268
1269                                 /* extract a parameter, allow quotes */
1270                                 while (*cmd_end && isspace((guchar)*cmd_end))
1271                                         cmd_end++;
1272
1273                                 cmd_start = cmd_end;
1274                                 if (*cmd_start == '"') {
1275                                         term_char = '"';
1276                                         cmd_end++;
1277                                 }
1278                                 else
1279                                         term_char = ' ';
1280
1281                                 /* extract actual parameter */
1282                                 while ((*cmd_end) && (*cmd_end != term_char))
1283                                         cmd_end++;
1284
1285                                 if (*cmd_end == '"')
1286                                         cmd_end++;
1287
1288                                 save_char = *cmd_end;
1289                                 *cmd_end = '\0';
1290
1291                                 if (cmds[i].qualifier) {
1292                                         if (casesens)
1293                                                 g_string_append(matcherstr, "regexp ");
1294                                         else
1295                                                 g_string_append(matcherstr, "regexpcase ");
1296                                 }
1297
1298                                 /* do we need to add quotes ? */
1299                                 if (cmds[i].quotes && term_char != '"')
1300                                         g_string_append(matcherstr, "\"");
1301
1302                                 /* copy actual parameter */
1303                                 g_string_append(matcherstr, cmd_start);
1304
1305                                 /* do we need to add quotes ? */
1306                                 if (cmds[i].quotes && term_char != '"')
1307                                         g_string_append(matcherstr, "\"");
1308
1309                                 /* restore original character */
1310                                 *cmd_end = save_char;
1311
1312                                 break;
1313                         }
1314                 }
1315
1316                 if (*cmd_end)
1317                         cmd_end++;
1318                 cmd_start = cmd_end;
1319         }
1320
1321         g_free(copy_str);
1322
1323         /* return search string if no match is found to allow
1324            all available filtering expressions in quicksearch */
1325         if (matcherstr->len > 0) returnstr = matcherstr->str;
1326         else returnstr = g_strdup(search_string);
1327         g_string_free(matcherstr, FALSE);
1328         return returnstr;
1329 }
1330
1331 static gchar *expand_tag_search_string(const gchar *search_string)
1332 {
1333         gchar *newstr = NULL;
1334         gchar **words = search_string ? g_strsplit(search_string, " ", -1):NULL;
1335         gint i = 0;
1336         while (words && words[i] && *words[i]) {
1337                 g_strstrip(words[i]);
1338                 if (!newstr) {
1339                         newstr = g_strdup_printf("tag regexpcase \"%s\"", words[i]);
1340                 } else {
1341                         gint o_len = strlen(newstr);
1342                         gint s_len = 18; /* strlen("|tag regexpcase \"\"") */
1343                         gint n_len = s_len + strlen(words[i]);
1344                         newstr = g_realloc(newstr,o_len+n_len+1);
1345                         strcpy(newstr+o_len, "|tag regexpcase \"");
1346                         strcpy(newstr+o_len+(s_len-1), words[i]);
1347                         strcpy(newstr+o_len+(n_len-1), "\"");
1348                 }
1349                 i++;
1350         }
1351         g_strfreev(words);
1352         return newstr;
1353 }
1354
1355 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run)
1356 {
1357         quicksearch->running = run;
1358 }
1359
1360 gboolean quicksearch_is_running(QuickSearch *quicksearch)
1361 {
1362         return quicksearch->running;
1363 }
1364
1365 void quicksearch_pass_key(QuickSearch *quicksearch, guint val, GdkModifierType mod)
1366 {
1367         GtkEntry *entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN((quicksearch->search_string_entry))));
1368         glong curpos = gtk_editable_get_position(GTK_EDITABLE(entry));
1369         guint32 c;
1370         char *str = g_strdup(gtk_entry_get_text(entry));
1371         char *begin = str;
1372         char *end = NULL;
1373         char *new = NULL;
1374         char key[7] = "";
1375         guint char_len = 0;
1376
1377         if (gtk_editable_get_selection_bounds(GTK_EDITABLE(entry), NULL, NULL)) {
1378                 /* remove selection */
1379                 gtk_editable_delete_selection(GTK_EDITABLE(entry));
1380                 curpos = gtk_editable_get_position(GTK_EDITABLE(entry));
1381                 /* refresh string */
1382                 g_free(str);
1383                 str = g_strdup(gtk_entry_get_text(entry));
1384                 begin = str;
1385         }
1386
1387         if (!(c = gdk_keyval_to_unicode(val))) {
1388                 g_free(str);
1389                 return;
1390         }
1391         char_len = g_unichar_to_utf8(c, key);
1392         if (char_len < 0)
1393                 return;
1394         key[char_len] = '\0';
1395         if (curpos < g_utf8_strlen(str, -1)) {
1396                 gchar *stop = g_utf8_offset_to_pointer(begin, curpos);
1397                 end = g_strdup(g_utf8_offset_to_pointer(str, curpos));
1398                 *stop = '\0';
1399                 new = g_strdup_printf("%s%s%s", begin, key, end);
1400                 gtk_entry_set_text(entry, new);
1401                 g_free(end);
1402         } else {
1403                 new = g_strdup_printf("%s%s", begin, key);
1404                 gtk_entry_set_text(entry, new);
1405         }
1406         g_free(str);
1407         g_free(new);
1408         gtk_editable_set_position(GTK_EDITABLE(entry), curpos+1);
1409
1410 }
1411
1412 static gboolean quicksearch_match_subfolder(QuickSearch *quicksearch,
1413                                  FolderItem *src)
1414 {
1415         GSList *msglist = NULL;
1416         GSList *cur;
1417         gboolean result = FALSE;
1418         gint num = 0, total = 0;
1419         gint interval = quicksearch_is_fast(quicksearch) ? 5000:100;
1420
1421         statusbar_print_all(_("Searching in %s... \n"),
1422                 src->path ? src->path : "(null)");
1423                 
1424         msglist = folder_item_get_msg_list(src);
1425         total = src->total_msgs;
1426         folder_item_update_freeze();
1427         for (cur = msglist; cur != NULL; cur = cur->next) {
1428                 MsgInfo *msg = (MsgInfo *)cur->data;
1429                 statusbar_progress_all(num++,total, interval);
1430                 if (quicksearch_match(quicksearch, msg)) {
1431                         result = TRUE;
1432                         break;
1433                 }
1434                 if (num % interval == 0)
1435                         GTK_EVENTS_FLUSH();
1436                 if (!quicksearch_is_active(quicksearch))
1437                         break;
1438         }
1439         folder_item_update_thaw();
1440         statusbar_progress_all(0,0,0);
1441         statusbar_pop_all();
1442
1443         procmsg_msg_list_free(msglist);
1444         return result;
1445 }
1446
1447 gboolean quicksearch_is_in_subfolder(QuickSearch *quicksearch, FolderItem *cur)
1448 {
1449         if (quicksearch->root_folder_item == NULL)
1450                 return FALSE;
1451         
1452         while (cur) {
1453                 if (cur == quicksearch->root_folder_item) {
1454                         return TRUE;
1455                 }
1456                 cur = folder_item_parent(cur);
1457         }
1458         return FALSE;
1459 }
1460
1461 void quicksearch_search_subfolders(QuickSearch *quicksearch,
1462                                    FolderView *folderview,
1463                                    FolderItem *folder_item)
1464 {
1465         FolderItem *cur = NULL;
1466         GNode *node = folder_item->node->children;
1467
1468         if (!prefs_common.summary_quicksearch_recurse
1469         ||  quicksearch->in_typing == TRUE)
1470                 return;
1471
1472         for (; node != NULL; node = node->next) {
1473                 cur = FOLDER_ITEM(node->data);
1474                 if (quicksearch_match_subfolder(quicksearch, cur)) {
1475                         folderview_update_search_icon(cur, TRUE);
1476                 } else {
1477                         folderview_update_search_icon(cur, FALSE);
1478                 }
1479                 if (cur->node->children)
1480                         quicksearch_search_subfolders(quicksearch,
1481                                                       folderview,
1482                                                       cur);
1483         }
1484         quicksearch->root_folder_item = folder_item;
1485         if (!quicksearch_is_active(quicksearch))
1486                 quicksearch_reset_cur_folder_item(quicksearch);
1487 }
1488
1489 static void quicksearch_reset_folder_items(QuickSearch *quicksearch,
1490                                     FolderItem *folder_item)
1491 {
1492         FolderItem *cur = NULL;
1493         GNode *node = (folder_item && folder_item->node) ?
1494                         folder_item->node->children : NULL;
1495
1496         for (; node != NULL; node = node->next) {
1497                 cur = FOLDER_ITEM(node->data);
1498                 folderview_update_search_icon(cur, FALSE);
1499                 if (cur->node->children)
1500                         quicksearch_reset_folder_items(quicksearch,
1501                                                        cur);
1502         }
1503 }
1504
1505 void quicksearch_reset_cur_folder_item(QuickSearch *quicksearch)
1506 {
1507         if (quicksearch->root_folder_item)
1508                 quicksearch_reset_folder_items(quicksearch,
1509                                                quicksearch->root_folder_item);
1510
1511         quicksearch->root_folder_item = NULL;
1512 }
1513
1514 gboolean quicksearch_is_in_typing(QuickSearch *quicksearch)
1515 {
1516         return quicksearch->in_typing;
1517 }
1518
1519 void quicksearch_set_search_strings(QuickSearch *quicksearch)
1520 {
1521         GList *strings = prefs_common.summary_quicksearch_history;
1522         gchar *newstr = NULL;
1523         MatcherList *matcher_list = NULL;
1524
1525         if (!strings)
1526                 return;
1527
1528         matcher_parser_disable_warnings(TRUE);
1529         
1530         do {
1531                 newstr = expand_search_string((gchar *) strings->data);
1532                 if (newstr && newstr[0] != '\0') {
1533                         if (!strchr(newstr, ' ')) {
1534                                 quicksearch->normal_search_strings =
1535                                         g_list_append(
1536                                                 quicksearch->normal_search_strings,
1537                                                 g_strdup(strings->data));
1538                                 g_free(newstr);
1539                                 continue;
1540                         }
1541                         
1542                         matcher_list = matcher_parser_get_cond(newstr, FALSE);
1543                         g_free(newstr);
1544                         
1545                         if (matcher_list) {
1546                                 quicksearch->extended_search_strings =
1547                                         g_list_prepend(
1548                                                 quicksearch->extended_search_strings,
1549                                                 g_strdup(strings->data));
1550                                 matcherlist_free(matcher_list);
1551                         } else
1552                                 quicksearch->normal_search_strings =
1553                                         g_list_prepend(
1554                                                 quicksearch->normal_search_strings,
1555                                                 g_strdup(strings->data));
1556                 }
1557         
1558         } while ((strings = g_list_next(strings)) != NULL);
1559
1560         matcher_parser_disable_warnings(FALSE); 
1561
1562         quicksearch->normal_search_strings = g_list_reverse(quicksearch->normal_search_strings);
1563         quicksearch->extended_search_strings = g_list_reverse(quicksearch->extended_search_strings);
1564
1565         quicksearch_set_popdown_strings(quicksearch);
1566 }
1567
1568 /*
1569  * Searches in the supplied folderItem the messages (MessageInfo) matching a
1570  * QuickSearchType + search string (ex.: QUICK_SEARCH_FROM and "foo@bar.com").
1571  *
1572  * Found messages are appended to the array 'messages' and their ref.counts
1573  * are incremented by 1 --so they need to be released (procmsg_msginfo_free())
1574  * before the array 'messages' is freed.
1575  */
1576 void search_msgs_in_folder(GSList **messages, QuickSearch* quicksearch,
1577                            FolderItem* folderItem)
1578 {
1579         /* from quicksearch_match_subfolder */
1580         GSList *msglist = NULL;
1581         GSList *cur;
1582
1583         /* The list is built w/ MsgInfo items whose ref.counts are incremented,
1584            but they are decremented when the list is freed by
1585            procmsg_msg_list_free(): we'll  ask for a new ref., below
1586         */
1587         msglist = folder_item_get_msg_list(folderItem);
1588
1589         for (cur = msglist; cur != NULL; cur = cur->next) {
1590                 MsgInfo *msg = (MsgInfo *)cur->data;
1591                 if (quicksearch_match(quicksearch, msg)) {
1592                         /*debug_print("found: %s from:%s\n",procmsg_get_message_file_path(msg),msg->from);*/
1593                         *messages = g_slist_prepend(*messages, procmsg_msginfo_new_ref(msg));
1594                 }
1595                 /* See 2.0.0cvs140 ChangeLog entry for details
1596                    see also comments in quicksearch_match() */
1597                 if (quicksearch_from_gui(quicksearch)==TRUE
1598                     && !quicksearch_is_active(quicksearch))
1599                         break;
1600         }
1601         procmsg_msg_list_free(msglist);
1602 }
1603
1604 /*
1605  * Searches within the folderItem and its sub-folders (if recursive is TRUE)
1606  * the messages matching the search request.
1607  *
1608  * NB: search within a Folder can be done this way:
1609  *         search_msg_in_folders(messages, quicksearch, searchType,
1610  *                               FOLDER_ITEM(folder->node->data), TRUE);
1611  */
1612 void search_msgs_in_folders(GSList **messages, QuickSearch* quicksearch,
1613                             FolderItem* folderItem)
1614 {
1615         FolderItem *cur = NULL;
1616
1617         search_msgs_in_folder(messages, quicksearch, folderItem);
1618         if (quicksearch->request->recursive == FALSE)
1619                 return;
1620
1621         GNode *node = folderItem->node->children;
1622         for (; node != NULL; node = node->next) {
1623                 cur = FOLDER_ITEM(node->data);
1624                 debug_print("in: %s\n",cur->path);
1625                 if (cur->node->children)
1626                         search_msgs_in_folders(messages, quicksearch, cur);
1627                 else
1628                         search_msgs_in_folder(messages, quicksearch, cur);
1629         }
1630         *messages = g_slist_reverse(*messages);
1631 }
1632
1633  /*
1634   * Returns the QuickSearchType associated to the supplied string.
1635   */
1636 QuickSearchType quicksearch_type(const gchar* type)
1637 {
1638         QuickSearchType searchType = QUICK_SEARCH_EXTENDED;
1639         if (!type)
1640                 return searchType;
1641         switch(toupper(*type)) {
1642         case 'S':
1643                 searchType = QUICK_SEARCH_SUBJECT;
1644         break;
1645         case 'F':
1646                 searchType = QUICK_SEARCH_FROM;
1647         break;
1648         case 'T':
1649                 searchType = QUICK_SEARCH_TO;
1650         break;
1651         case 'E':
1652                 searchType = QUICK_SEARCH_EXTENDED;
1653         break;
1654         case 'M':
1655                 searchType = QUICK_SEARCH_MIXED;
1656         break;
1657         case 'G':
1658                 searchType = QUICK_SEARCH_TAG;
1659         break;
1660         }
1661         return searchType;
1662 }