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