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