2007-09-27 [colin] 3.0.1cvs35
[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_strdup(search_string);
151         } else {
152                 quicksearch->is_fast = TRUE;
153                 g_free(quicksearch->search_string);
154                 quicksearch->search_string = g_strdup(search_string);
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 inreplyto 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\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         GtkTooltips *tips = gtk_tooltips_new();
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         gtk_tooltips_set_tip(GTK_TOOLTIPS(tips),
673                              clear_search,
674                              _("Clear the current search"), NULL);
675         gtk_widget_show(clear_search);
676
677 #if GTK_CHECK_VERSION(2, 8, 0)
678         search_condition_expression = gtk_button_new_from_stock(GTK_STOCK_EDIT);
679 #else
680         search_condition_expression = gtk_button_new_with_label(" ... ");
681 #endif
682         gtk_box_pack_start(GTK_BOX(search_hbox), search_condition_expression,
683                            FALSE, FALSE, 0);
684         g_signal_connect(G_OBJECT (search_condition_expression), "clicked",
685                          G_CALLBACK(search_condition_expr),
686                          quicksearch);
687         gtk_tooltips_set_tip(GTK_TOOLTIPS(tips),
688                              search_condition_expression,
689                              _("Edit search criteria"), NULL);
690         gtk_widget_show(search_condition_expression);
691
692 #if GTK_CHECK_VERSION(2, 8, 0)
693         search_description = gtk_button_new_from_stock(GTK_STOCK_INFO);
694 #else
695         search_description = gtk_button_new_with_label(_(" Extended Symbols... "));
696 #endif
697         gtk_box_pack_start(GTK_BOX(search_hbox), search_description,
698                            FALSE, FALSE, 0);
699         g_signal_connect(G_OBJECT(search_description), "clicked",
700                          G_CALLBACK(search_description_cb), NULL);
701         gtk_tooltips_set_tip(GTK_TOOLTIPS(tips),
702                              search_description,
703                              _("Information about extended symbols"), NULL);
704         gtk_widget_show(search_description);
705
706         gtk_box_pack_start(GTK_BOX(hbox_search), search_hbox, FALSE, FALSE, 2);
707         gtk_widget_show(search_hbox);
708
709         g_signal_connect(G_OBJECT(GTK_BIN(search_string_entry)->child),
710                            "key_press_event",
711                            G_CALLBACK(searchbar_pressed),
712                            quicksearch);
713
714         g_signal_connect(G_OBJECT(GTK_BIN(search_string_entry)->child),
715                          "changed",
716                          G_CALLBACK(searchbar_changed_cb),
717                          quicksearch);
718
719         g_signal_connect(G_OBJECT(GTK_BIN(search_string_entry)->child),
720                          "focus_in_event",
721                          G_CALLBACK(searchbar_focus_evt_in),
722                          quicksearch);
723         g_signal_connect(G_OBJECT(GTK_BIN(search_string_entry)->child),
724                          "focus_out_event",
725                          G_CALLBACK(searchbar_focus_evt_out),
726                          quicksearch);
727
728         quicksearch->hbox_search = hbox_search;
729         quicksearch->search_type = search_type;
730         quicksearch->search_string_entry = search_string_entry;
731         quicksearch->search_condition_expression = search_condition_expression;
732         quicksearch->search_description = search_description;
733         quicksearch->matcher_list = NULL;
734         quicksearch->active = FALSE;
735         quicksearch->running = FALSE;
736         quicksearch->clear_search = clear_search;
737         quicksearch->in_typing = FALSE;
738         quicksearch->press_timeout_id = -1;
739
740         update_extended_buttons(quicksearch);
741
742         return quicksearch;
743 }
744
745 void quicksearch_relayout(QuickSearch *quicksearch)
746 {
747         switch (prefs_common.layout_mode) {
748         case NORMAL_LAYOUT:
749         case WIDE_LAYOUT:
750         case WIDE_MSGLIST_LAYOUT:
751 #if GTK_CHECK_VERSION(2, 8, 0)
752                 gtk_button_set_label(GTK_BUTTON(quicksearch->search_description), GTK_STOCK_INFO);
753                 gtk_button_set_label(GTK_BUTTON(quicksearch->search_condition_expression), GTK_STOCK_EDIT);
754                 gtk_button_set_label(GTK_BUTTON(quicksearch->clear_search), GTK_STOCK_CLEAR);
755 #else
756                 gtk_button_set_label(GTK_BUTTON(quicksearch->search_description), _(" Extended Symbols... "));
757                 gtk_button_set_label(GTK_BUTTON(quicksearch->search_condition_expression), " ... ");
758                 gtk_button_set_label(GTK_BUTTON(quicksearch->clear_search), _(" Clear "));
759 #endif
760                 break;
761         case VERTICAL_LAYOUT:
762 #if GTK_CHECK_VERSION(2, 8, 0)
763                 gtk_button_set_label(GTK_BUTTON(quicksearch->search_description), "");
764                 gtk_button_set_label(GTK_BUTTON(quicksearch->search_condition_expression), "");
765                 gtk_button_set_label(GTK_BUTTON(quicksearch->clear_search), "");
766
767                 gtk_button_set_image(GTK_BUTTON(quicksearch->search_description),
768                         gtk_image_new_from_stock(GTK_STOCK_INFO, GTK_ICON_SIZE_BUTTON));
769                 gtk_button_set_image(GTK_BUTTON(quicksearch->search_condition_expression),
770                         gtk_image_new_from_stock(GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON));
771                 gtk_button_set_image(GTK_BUTTON(quicksearch->clear_search),
772                         gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_BUTTON));
773 #else
774                 gtk_button_set_label(GTK_BUTTON(quicksearch->search_description), _("Info"));
775                 gtk_button_set_label(GTK_BUTTON(quicksearch->search_condition_expression), "...");
776                 gtk_button_set_label(GTK_BUTTON(quicksearch->clear_search), _("Clear"));
777 #endif
778                 break;
779         }
780 }
781
782 GtkWidget *quicksearch_get_widget(QuickSearch *quicksearch)
783 {
784         return quicksearch->hbox_search;
785 }
786
787 void quicksearch_show(QuickSearch *quicksearch)
788 {
789         MainWindow *mainwin = mainwindow_get_mainwindow();
790         GtkWidget *ctree = NULL;
791         prepare_matcher(quicksearch);
792         gtk_widget_show(quicksearch->hbox_search);
793         update_extended_buttons(quicksearch);
794         gtk_widget_grab_focus(
795                 GTK_WIDGET(GTK_BIN(quicksearch->search_string_entry)->child));
796
797         GTK_EVENTS_FLUSH();
798
799         if (!mainwin || !mainwin->summaryview) {
800                 return;
801         }
802         
803         ctree = summary_get_main_widget(mainwin->summaryview);
804         
805         if (ctree && mainwin->summaryview->selected)
806                 gtk_ctree_node_moveto(GTK_CTREE(ctree), 
807                                 mainwin->summaryview->selected, 
808                                 0, 0.5, 0);
809 }
810
811 void quicksearch_hide(QuickSearch *quicksearch)
812 {
813         if (quicksearch_is_active(quicksearch)) {
814                 quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
815                 quicksearch_set_active(quicksearch, FALSE);
816         }
817         gtk_widget_hide(quicksearch->hbox_search);
818 }
819
820 void quicksearch_set(QuickSearch *quicksearch, QuickSearchType type,
821                      const gchar *matchstring)
822 {
823         quicksearch_set_type(quicksearch, type);
824
825         if (!matchstring || !(*matchstring))
826                 quicksearch->in_typing = FALSE;
827
828         g_signal_handlers_block_by_func(G_OBJECT(GTK_BIN(quicksearch->search_string_entry)->child),
829                         G_CALLBACK(searchbar_changed_cb), quicksearch);
830         gtk_entry_set_text(GTK_ENTRY(GTK_BIN(quicksearch->search_string_entry)->child),
831                            matchstring);
832         g_signal_handlers_unblock_by_func(G_OBJECT(GTK_BIN(quicksearch->search_string_entry)->child),
833                         G_CALLBACK(searchbar_changed_cb), quicksearch);
834
835         prefs_common.summary_quicksearch_type = type;
836
837         prepare_matcher(quicksearch);
838
839         quicksearch_set_running(quicksearch, TRUE);
840         if (quicksearch->callback != NULL)
841                 quicksearch->callback(quicksearch, quicksearch->callback_data);
842         quicksearch_set_running(quicksearch, FALSE);
843 }
844
845 gboolean quicksearch_is_active(QuickSearch *quicksearch)
846 {
847         return quicksearch->active && 
848                 (prefs_common.summary_quicksearch_type != QUICK_SEARCH_EXTENDED
849                  || quicksearch->matcher_list != NULL);
850 }
851
852 static void quicksearch_set_active(QuickSearch *quicksearch, gboolean active)
853 {
854         static GdkColor yellow;
855         static GdkColor red;
856         static GdkColor black;
857         static gboolean colors_initialised = FALSE;
858         gboolean error = FALSE;
859
860         if (!colors_initialised) {
861                 gdk_color_parse("#f5f6be", &yellow);
862                 gdk_color_parse("#000000", &black);
863                 gdk_color_parse("#ff7070", &red);
864                 colors_initialised = gdk_colormap_alloc_color(
865                         gdk_colormap_get_system(), &yellow, FALSE, TRUE);
866                 colors_initialised &= gdk_colormap_alloc_color(
867                         gdk_colormap_get_system(), &black, FALSE, TRUE);
868                 colors_initialised &= gdk_colormap_alloc_color(
869                         gdk_colormap_get_system(), &red, FALSE, TRUE);
870         }
871
872         quicksearch->active = active;
873
874         if (active && 
875                 (prefs_common.summary_quicksearch_type == QUICK_SEARCH_EXTENDED
876                  && quicksearch->matcher_list == NULL))
877                 error = TRUE;
878
879         if (active) {
880                 gtk_widget_set_sensitive(quicksearch->clear_search, TRUE);
881                 if (colors_initialised) {
882                         gtk_widget_modify_base(
883                                 GTK_BIN(quicksearch->search_string_entry)->child,
884                                 GTK_STATE_NORMAL, error ? &red : &yellow);
885                         gtk_widget_modify_text(
886                                 GTK_BIN(quicksearch->search_string_entry)->child,
887                                 GTK_STATE_NORMAL, &black);
888                 }
889         } else {
890                 gtk_widget_set_sensitive(quicksearch->clear_search, FALSE);
891                 if (colors_initialised) {
892                         gtk_widget_modify_base(
893                                 GTK_BIN(quicksearch->search_string_entry)->child,
894                                 GTK_STATE_NORMAL, NULL);
895                         gtk_widget_modify_text(
896                                 GTK_BIN(quicksearch->search_string_entry)->child,
897                                 GTK_STATE_NORMAL, NULL);
898                 }
899         }
900
901         if (!active) {
902                 quicksearch_reset_cur_folder_item(quicksearch);
903         }
904 }
905
906 void quicksearch_set_execute_callback(QuickSearch *quicksearch,
907                                       QuickSearchExecuteCallback callback,
908                                       gpointer data)
909 {
910         quicksearch->callback = callback;
911         quicksearch->callback_data = data;
912 }
913
914 gboolean quicksearch_match(QuickSearch *quicksearch, MsgInfo *msginfo)
915 {
916         gchar *searched_header = NULL;
917         gboolean result = FALSE;
918
919         if (!quicksearch->active)
920                 return TRUE;
921
922         switch (prefs_common.summary_quicksearch_type) {
923         case QUICK_SEARCH_SUBJECT:
924                 searched_header = msginfo->subject;
925                 break;
926         case QUICK_SEARCH_FROM:
927                 searched_header = msginfo->from;
928                 break;
929         case QUICK_SEARCH_TO:
930                 searched_header = msginfo->to;
931                 break;
932         case QUICK_SEARCH_MIXED:
933                 break;
934         case QUICK_SEARCH_EXTENDED:
935                 break;
936         default:
937                 debug_print("unknown search type (%d)\n", prefs_common.summary_quicksearch_type);
938                 break;
939         }
940         quicksearch->matching = TRUE;
941         if (prefs_common.summary_quicksearch_type != QUICK_SEARCH_EXTENDED &&
942             prefs_common.summary_quicksearch_type != QUICK_SEARCH_MIXED &&
943             prefs_common.summary_quicksearch_type != QUICK_SEARCH_TAG &&
944             quicksearch->search_string &&
945             searched_header && strcasestr(searched_header, quicksearch->search_string) != NULL)
946                 result = TRUE;
947         else if (prefs_common.summary_quicksearch_type == QUICK_SEARCH_MIXED &&
948                 quicksearch->search_string && (
949                 (msginfo->to && strcasestr(msginfo->to, quicksearch->search_string) != NULL) ||
950                 (msginfo->from && strcasestr(msginfo->from, quicksearch->search_string) != NULL) ||
951                 (msginfo->subject && strcasestr(msginfo->subject, quicksearch->search_string) != NULL) ||
952                 ((quicksearch->matcher_list != NULL) &&
953                  matcherlist_match(quicksearch->matcher_list, msginfo))  ))
954                 result = TRUE;
955         else if ((quicksearch->matcher_list != NULL) &&
956                  matcherlist_match(quicksearch->matcher_list, msginfo))
957                 result = TRUE;
958
959         quicksearch->matching = FALSE;
960         if (quicksearch->deferred_free) {
961                 prepare_matcher(quicksearch);
962         }
963
964         return result;
965 }
966
967 /* allow Mutt-like patterns in quick search */
968 static gchar *expand_search_string(const gchar *search_string)
969 {
970         int i = 0;
971         gchar term_char, save_char;
972         gchar *cmd_start, *cmd_end;
973         GString *matcherstr;
974         gchar *returnstr = NULL;
975         gchar *copy_str;
976         gboolean casesens, dontmatch;
977         /* list of allowed pattern abbreviations */
978         struct {
979                 gchar           *abbreviated;   /* abbreviation */
980                 gchar           *command;       /* actual matcher command */
981                 gint            numparams;      /* number of params for cmd */
982                 gboolean        qualifier;      /* do we append regexpcase */
983                 gboolean        quotes;         /* do we need quotes */
984         }
985         cmds[] = {
986                 { "a",  "all",                          0,      FALSE,  FALSE },
987                 { "ag", "age_greater",                  1,      FALSE,  FALSE },
988                 { "al", "age_lower",                    1,      FALSE,  FALSE },
989                 { "b",  "body_part",                    1,      TRUE,   TRUE  },
990                 { "B",  "message",                      1,      TRUE,   TRUE  },
991                 { "c",  "cc",                           1,      TRUE,   TRUE  },
992                 { "C",  "to_or_cc",                     1,      TRUE,   TRUE  },
993                 { "D",  "deleted",                      0,      FALSE,  FALSE },
994                 { "e",  "header \"Sender\"",            1,      TRUE,   TRUE  },
995                 { "E",  "execute",                      1,      FALSE,  TRUE  },
996                 { "f",  "from",                         1,      TRUE,   TRUE  },
997                 { "F",  "forwarded",                    0,      FALSE,  FALSE },
998                 { "h",  "headers_part",                 1,      TRUE,   TRUE  },
999                 { "i",  "header \"Message-ID\"",        1,      TRUE,   TRUE  },
1000                 { "I",  "inreplyto",                    1,      TRUE,   TRUE  },
1001                 { "k",  "colorlabel",                   1,      FALSE,  FALSE },
1002                 { "L",  "locked",                       0,      FALSE,  FALSE },
1003                 { "n",  "newsgroups",                   1,      TRUE,   TRUE  },
1004                 { "N",  "new",                          0,      FALSE,  FALSE },
1005                 { "O",  "~new",                         0,      FALSE,  FALSE },
1006                 { "r",  "replied",                      0,      FALSE,  FALSE },
1007                 { "R",  "~unread",                      0,      FALSE,  FALSE },
1008                 { "s",  "subject",                      1,      TRUE,   TRUE  },
1009                 { "se", "score_equal",                  1,      FALSE,  FALSE },
1010                 { "sg", "score_greater",                1,      FALSE,  FALSE },
1011                 { "sl", "score_lower",                  1,      FALSE,  FALSE },
1012                 { "Se", "size_equal",                   1,      FALSE,  FALSE },
1013                 { "Sg", "size_greater",                 1,      FALSE,  FALSE },
1014                 { "Ss", "size_smaller",                 1,      FALSE,  FALSE },
1015                 { "t",  "to",                           1,      TRUE,   TRUE  },
1016                 { "tg", "tag",                          1,      TRUE,   TRUE  },
1017                 { "T",  "marked",                       0,      FALSE,  FALSE },
1018                 { "U",  "unread",                       0,      FALSE,  FALSE },
1019                 { "x",  "header \"References\"",        1,      TRUE,   TRUE  },
1020                 { "X",  "test",                         1,      FALSE,  FALSE },
1021                 { "y",  "header \"X-Label\"",           1,      TRUE,   TRUE  },
1022                 { "&",  "&",                            0,      FALSE,  FALSE },
1023                 { "|",  "|",                            0,      FALSE,  FALSE },
1024                 { "p",  "partial",                      0,      FALSE,  FALSE },
1025                 { NULL, NULL,                           0,      FALSE,  FALSE }
1026         };
1027
1028         if (search_string == NULL)
1029                 return NULL;
1030
1031         copy_str = g_strdup(search_string);
1032
1033         matcherstr = g_string_sized_new(16);
1034         cmd_start = copy_str;
1035         while (cmd_start && *cmd_start) {
1036                 /* skip all white spaces */
1037                 while (*cmd_start && isspace((guchar)*cmd_start))
1038                         cmd_start++;
1039                 cmd_end = cmd_start;
1040
1041                 /* extract a command */
1042                 while (*cmd_end && !isspace((guchar)*cmd_end))
1043                         cmd_end++;
1044
1045                 /* save character */
1046                 save_char = *cmd_end;
1047                 *cmd_end = '\0';
1048
1049                 dontmatch = FALSE;
1050                 casesens = FALSE;
1051
1052                 /* ~ and ! mean logical NOT */
1053                 if (*cmd_start == '~' || *cmd_start == '!')
1054                 {
1055                         dontmatch = TRUE;
1056                         cmd_start++;
1057                 }
1058                 /* % means case sensitive match */
1059                 if (*cmd_start == '%')
1060                 {
1061                         casesens = TRUE;
1062                         cmd_start++;
1063                 }
1064
1065                 /* find matching abbreviation */
1066                 for (i = 0; cmds[i].command; i++) {
1067                         if (!strcmp(cmd_start, cmds[i].abbreviated)) {
1068                                 /* restore character */
1069                                 *cmd_end = save_char;
1070
1071                                 /* copy command */
1072                                 if (matcherstr->len > 0) {
1073                                         g_string_append(matcherstr, " ");
1074                                 }
1075                                 if (dontmatch)
1076                                         g_string_append(matcherstr, "~");
1077                                 g_string_append(matcherstr, cmds[i].command);
1078                                 g_string_append(matcherstr, " ");
1079
1080                                 /* stop if no params required */
1081                                 if (cmds[i].numparams == 0)
1082                                         break;
1083
1084                                 /* extract a parameter, allow quotes */
1085                                 while (*cmd_end && isspace((guchar)*cmd_end))
1086                                         cmd_end++;
1087
1088                                 cmd_start = cmd_end;
1089                                 if (*cmd_start == '"') {
1090                                         term_char = '"';
1091                                         cmd_end++;
1092                                 }
1093                                 else
1094                                         term_char = ' ';
1095
1096                                 /* extract actual parameter */
1097                                 while ((*cmd_end) && (*cmd_end != term_char))
1098                                         cmd_end++;
1099
1100                                 if (*cmd_end == '"')
1101                                         cmd_end++;
1102
1103                                 save_char = *cmd_end;
1104                                 *cmd_end = '\0';
1105
1106                                 if (cmds[i].qualifier) {
1107                                         if (casesens)
1108                                                 g_string_append(matcherstr, "regexp ");
1109                                         else
1110                                                 g_string_append(matcherstr, "regexpcase ");
1111                                 }
1112
1113                                 /* do we need to add quotes ? */
1114                                 if (cmds[i].quotes && term_char != '"')
1115                                         g_string_append(matcherstr, "\"");
1116
1117                                 /* copy actual parameter */
1118                                 g_string_append(matcherstr, cmd_start);
1119
1120                                 /* do we need to add quotes ? */
1121                                 if (cmds[i].quotes && term_char != '"')
1122                                         g_string_append(matcherstr, "\"");
1123
1124                                 /* restore original character */
1125                                 *cmd_end = save_char;
1126
1127                                 break;
1128                         }
1129                 }
1130
1131                 if (*cmd_end)
1132                         cmd_end++;
1133                 cmd_start = cmd_end;
1134         }
1135
1136         g_free(copy_str);
1137
1138         /* return search string if no match is found to allow
1139            all available filtering expressions in quicksearch */
1140         if (matcherstr->len > 0) returnstr = matcherstr->str;
1141         else returnstr = g_strdup(search_string);
1142
1143         g_string_free(matcherstr, FALSE);
1144         return returnstr;
1145 }
1146
1147 static gchar *expand_tag_search_string(const gchar *search_string)
1148 {
1149         gchar *newstr = NULL;
1150         gchar **words = search_string ? g_strsplit(search_string, " ", -1):NULL;
1151         gint i = 0;
1152         while (words && words[i] && *words[i]) {
1153                 g_strstrip(words[i]);
1154                 if (!newstr) {
1155                         newstr = g_strdup_printf("tag regexpcase \"%s\"", words[i]);
1156                 } else {
1157                         gint o_len = strlen(newstr);
1158                         gint s_len = 18; /* strlen("|tag regexpcase \"\"") */
1159                         gint n_len = s_len + strlen(words[i]);
1160                         newstr = g_realloc(newstr,o_len+n_len+1);
1161                         strcpy(newstr+o_len, "|tag regexpcase \"");
1162                         strcpy(newstr+o_len+(s_len-1), words[i]);
1163                         strcpy(newstr+o_len+s_len, "\"");
1164                 }
1165                 i++;
1166         }
1167         g_strfreev(words);
1168         return newstr;
1169 }
1170
1171 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run)
1172 {
1173         quicksearch->running = run;
1174 }
1175
1176 gboolean quicksearch_is_running(QuickSearch *quicksearch)
1177 {
1178         return quicksearch->running;
1179 }
1180
1181 void quicksearch_pass_key(QuickSearch *quicksearch, guint val, GdkModifierType mod)
1182 {
1183         GtkEntry *entry = GTK_ENTRY(GTK_BIN(quicksearch->search_string_entry)->child);
1184         glong curpos = gtk_editable_get_position(GTK_EDITABLE(entry));
1185         guint32 c;
1186         char *str = g_strdup(gtk_entry_get_text(entry));
1187         char *begin = str;
1188         char *end = NULL;
1189         char *new = NULL;
1190         char key[7] = "";
1191         guint char_len = 0;
1192
1193         if (gtk_editable_get_selection_bounds(GTK_EDITABLE(entry), NULL, NULL)) {
1194                 /* remove selection */
1195                 gtk_editable_delete_selection(GTK_EDITABLE(entry));
1196                 curpos = gtk_editable_get_position(GTK_EDITABLE(entry));
1197                 /* refresh string */
1198                 g_free(str);
1199                 str = g_strdup(gtk_entry_get_text(entry));
1200                 begin = str;
1201         }
1202
1203         if (!(c = gdk_keyval_to_unicode(val))) {
1204                 g_free(str);
1205                 return;
1206         }
1207         char_len = g_unichar_to_utf8(c, key);
1208         if (char_len < 0)
1209                 return;
1210         key[char_len] = '\0';
1211         if (curpos < g_utf8_strlen(str, -1)) {
1212                 gchar *stop = g_utf8_offset_to_pointer(begin, curpos);
1213                 end = g_strdup(g_utf8_offset_to_pointer(str, curpos));
1214                 *stop = '\0';
1215                 new = g_strdup_printf("%s%s%s", begin, key, end);
1216                 gtk_entry_set_text(entry, new);
1217                 g_free(end);
1218         } else {
1219                 new = g_strdup_printf("%s%s", begin, key);
1220                 gtk_entry_set_text(entry, new);
1221         }
1222         g_free(str);
1223         g_free(new);
1224         gtk_editable_set_position(GTK_EDITABLE(entry), curpos+1);
1225
1226 }
1227
1228 static gboolean quicksearch_match_subfolder(QuickSearch *quicksearch,
1229                                  FolderItem *src)
1230 {
1231         GSList *msglist = NULL;
1232         GSList *cur;
1233         gboolean result = FALSE;
1234         gint num = 0, total = 0;
1235         gint interval = quicksearch_is_fast(quicksearch) ? 5000:100;
1236
1237         statusbar_print_all(_("Searching in %s... \n"),
1238                 src->path ? src->path : "(null)");
1239                 
1240         msglist = folder_item_get_msg_list(src);
1241         total = src->total_msgs;
1242         folder_item_update_freeze();
1243         for (cur = msglist; cur != NULL; cur = cur->next) {
1244                 MsgInfo *msg = (MsgInfo *)cur->data;
1245                 statusbar_progress_all(num++,total, interval);
1246                 if (quicksearch_match(quicksearch, msg)) {
1247                         result = TRUE;
1248                         break;
1249                 }
1250                 if (num % interval == 0)
1251                         GTK_EVENTS_FLUSH();
1252                 if (!quicksearch_is_active(quicksearch))
1253                         break;
1254         }
1255         folder_item_update_thaw();
1256         statusbar_progress_all(0,0,0);
1257         statusbar_pop_all();
1258
1259         procmsg_msg_list_free(msglist);
1260         return result;
1261 }
1262
1263 void quicksearch_search_subfolders(QuickSearch *quicksearch,
1264                                    FolderView *folderview,
1265                                    FolderItem *folder_item)
1266 {
1267         FolderItem *cur = NULL;
1268         GNode *node = folder_item->node->children;
1269
1270         if (!prefs_common.summary_quicksearch_recurse
1271         ||  quicksearch->in_typing == TRUE)
1272                 return;
1273
1274         for (; node != NULL; node = node->next) {
1275                 cur = FOLDER_ITEM(node->data);
1276                 if (quicksearch_match_subfolder(quicksearch, cur)) {
1277                         folderview_update_search_icon(cur, TRUE);
1278                 } else {
1279                         folderview_update_search_icon(cur, FALSE);
1280                 }
1281                 if (cur->node->children)
1282                         quicksearch_search_subfolders(quicksearch,
1283                                                       folderview,
1284                                                       cur);
1285         }
1286         quicksearch->root_folder_item = folder_item;
1287         if (!quicksearch_is_active(quicksearch))
1288                 quicksearch_reset_cur_folder_item(quicksearch);
1289 }
1290
1291 static void quicksearch_reset_folder_items(QuickSearch *quicksearch,
1292                                     FolderItem *folder_item)
1293 {
1294         FolderItem *cur = NULL;
1295         GNode *node = (folder_item && folder_item->node) ?
1296                         folder_item->node->children : NULL;
1297
1298         for (; node != NULL; node = node->next) {
1299                 cur = FOLDER_ITEM(node->data);
1300                 folderview_update_search_icon(cur, FALSE);
1301                 if (cur->node->children)
1302                         quicksearch_reset_folder_items(quicksearch,
1303                                                        cur);
1304         }
1305 }
1306
1307 void quicksearch_reset_cur_folder_item(QuickSearch *quicksearch)
1308 {
1309         if (quicksearch->root_folder_item)
1310                 quicksearch_reset_folder_items(quicksearch,
1311                                                quicksearch->root_folder_item);
1312
1313         quicksearch->root_folder_item = NULL;
1314 }
1315
1316 gboolean quicksearch_is_in_typing(QuickSearch *quicksearch)
1317 {
1318         return quicksearch->in_typing;
1319 }