2007-10-10 [paul] 3.0.2cvs54
[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 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"
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         gchar *to = NULL, *from = NULL, *subject = NULL;
919
920         if (!quicksearch->active)
921                 return TRUE;
922
923         switch (prefs_common.summary_quicksearch_type) {
924         case QUICK_SEARCH_SUBJECT:
925                 if (msginfo->subject)
926                         searched_header = g_utf8_casefold(msginfo->subject, -1);
927                 else
928                         return FALSE;
929                 break;
930         case QUICK_SEARCH_FROM:
931                 if (msginfo->from)
932                         searched_header = g_utf8_casefold(msginfo->from, -1);
933                 else
934                         return FALSE;
935                 break;
936         case QUICK_SEARCH_TO:
937                 if (msginfo->to)
938                         searched_header = g_utf8_casefold(msginfo->to, -1);
939                 else
940                         return FALSE;
941                 break;
942         case QUICK_SEARCH_MIXED:
943                 if (msginfo->to)
944                         to = g_utf8_casefold(msginfo->to, -1);
945                 if (msginfo->from)
946                         from = g_utf8_casefold(msginfo->from, -1);
947                 if (msginfo->subject)
948                         subject = g_utf8_casefold(msginfo->subject, -1);
949                 break;
950         case QUICK_SEARCH_EXTENDED:
951                 break;
952         default:
953                 debug_print("unknown search type (%d)\n", prefs_common.summary_quicksearch_type);
954                 break;
955         }
956
957         quicksearch->matching = TRUE;
958         if (prefs_common.summary_quicksearch_type != QUICK_SEARCH_EXTENDED &&
959             prefs_common.summary_quicksearch_type != QUICK_SEARCH_MIXED &&
960             prefs_common.summary_quicksearch_type != QUICK_SEARCH_TAG &&
961             quicksearch->search_string &&
962             searched_header && strstr(searched_header, quicksearch->search_string) != NULL)
963                 result = TRUE;
964         else if (prefs_common.summary_quicksearch_type == QUICK_SEARCH_MIXED &&
965                 quicksearch->search_string && (
966                 (to && strstr(to, quicksearch->search_string) != NULL) ||
967                 (from && strstr(from, quicksearch->search_string) != NULL) ||
968                 (subject && strstr(subject, quicksearch->search_string) != NULL) ||
969                 ((quicksearch->matcher_list != NULL) &&
970                  matcherlist_match(quicksearch->matcher_list, msginfo))  ))
971                 result = TRUE;
972         else if ((quicksearch->matcher_list != NULL) &&
973                  matcherlist_match(quicksearch->matcher_list, msginfo))
974                 result = TRUE;
975
976         quicksearch->matching = FALSE;
977         if (quicksearch->deferred_free) {
978                 prepare_matcher(quicksearch);
979         }
980
981         g_free(to);
982         g_free(from);
983         g_free(subject);
984         g_free(searched_header);
985
986         return result;
987 }
988
989 /* allow Mutt-like patterns in quick search */
990 static gchar *expand_search_string(const gchar *search_string)
991 {
992         int i = 0;
993         gchar term_char, save_char;
994         gchar *cmd_start, *cmd_end;
995         GString *matcherstr;
996         gchar *returnstr = NULL;
997         gchar *copy_str;
998         gboolean casesens, dontmatch;
999         /* list of allowed pattern abbreviations */
1000         struct {
1001                 gchar           *abbreviated;   /* abbreviation */
1002                 gchar           *command;       /* actual matcher command */
1003                 gint            numparams;      /* number of params for cmd */
1004                 gboolean        qualifier;      /* do we append regexpcase */
1005                 gboolean        quotes;         /* do we need quotes */
1006         }
1007         cmds[] = {
1008                 { "a",  "all",                          0,      FALSE,  FALSE },
1009                 { "ag", "age_greater",                  1,      FALSE,  FALSE },
1010                 { "al", "age_lower",                    1,      FALSE,  FALSE },
1011                 { "b",  "body_part",                    1,      TRUE,   TRUE  },
1012                 { "B",  "message",                      1,      TRUE,   TRUE  },
1013                 { "c",  "cc",                           1,      TRUE,   TRUE  },
1014                 { "C",  "to_or_cc",                     1,      TRUE,   TRUE  },
1015                 { "D",  "deleted",                      0,      FALSE,  FALSE },
1016                 { "e",  "header \"Sender\"",            1,      TRUE,   TRUE  },
1017                 { "E",  "execute",                      1,      FALSE,  TRUE  },
1018                 { "f",  "from",                         1,      TRUE,   TRUE  },
1019                 { "F",  "forwarded",                    0,      FALSE,  FALSE },
1020                 { "h",  "headers_part",                 1,      TRUE,   TRUE  },
1021                 { "i",  "header \"Message-ID\"",        1,      TRUE,   TRUE  },
1022                 { "I",  "inreplyto",                    1,      TRUE,   TRUE  },
1023                 { "k",  "colorlabel",                   1,      FALSE,  FALSE },
1024                 { "L",  "locked",                       0,      FALSE,  FALSE },
1025                 { "n",  "newsgroups",                   1,      TRUE,   TRUE  },
1026                 { "N",  "new",                          0,      FALSE,  FALSE },
1027                 { "O",  "~new",                         0,      FALSE,  FALSE },
1028                 { "r",  "replied",                      0,      FALSE,  FALSE },
1029                 { "R",  "~unread",                      0,      FALSE,  FALSE },
1030                 { "s",  "subject",                      1,      TRUE,   TRUE  },
1031                 { "se", "score_equal",                  1,      FALSE,  FALSE },
1032                 { "sg", "score_greater",                1,      FALSE,  FALSE },
1033                 { "sl", "score_lower",                  1,      FALSE,  FALSE },
1034                 { "Se", "size_equal",                   1,      FALSE,  FALSE },
1035                 { "Sg", "size_greater",                 1,      FALSE,  FALSE },
1036                 { "Ss", "size_smaller",                 1,      FALSE,  FALSE },
1037                 { "t",  "to",                           1,      TRUE,   TRUE  },
1038                 { "tg", "tag",                          1,      TRUE,   TRUE  },
1039                 { "T",  "marked",                       0,      FALSE,  FALSE },
1040                 { "U",  "unread",                       0,      FALSE,  FALSE },
1041                 { "x",  "header \"References\"",        1,      TRUE,   TRUE  },
1042                 { "X",  "test",                         1,      FALSE,  FALSE },
1043                 { "y",  "header \"X-Label\"",           1,      TRUE,   TRUE  },
1044                 { "&",  "&",                            0,      FALSE,  FALSE },
1045                 { "|",  "|",                            0,      FALSE,  FALSE },
1046                 { "p",  "partial",                      0,      FALSE,  FALSE },
1047                 { NULL, NULL,                           0,      FALSE,  FALSE }
1048         };
1049
1050         if (search_string == NULL)
1051                 return NULL;
1052
1053         copy_str = g_strdup(search_string);
1054
1055         matcherstr = g_string_sized_new(16);
1056         cmd_start = copy_str;
1057         while (cmd_start && *cmd_start) {
1058                 /* skip all white spaces */
1059                 while (*cmd_start && isspace((guchar)*cmd_start))
1060                         cmd_start++;
1061                 cmd_end = cmd_start;
1062
1063                 /* extract a command */
1064                 while (*cmd_end && !isspace((guchar)*cmd_end))
1065                         cmd_end++;
1066
1067                 /* save character */
1068                 save_char = *cmd_end;
1069                 *cmd_end = '\0';
1070
1071                 dontmatch = FALSE;
1072                 casesens = FALSE;
1073
1074                 /* ~ and ! mean logical NOT */
1075                 if (*cmd_start == '~' || *cmd_start == '!')
1076                 {
1077                         dontmatch = TRUE;
1078                         cmd_start++;
1079                 }
1080                 /* % means case sensitive match */
1081                 if (*cmd_start == '%')
1082                 {
1083                         casesens = TRUE;
1084                         cmd_start++;
1085                 }
1086
1087                 /* find matching abbreviation */
1088                 for (i = 0; cmds[i].command; i++) {
1089                         if (!strcmp(cmd_start, cmds[i].abbreviated)) {
1090                                 /* restore character */
1091                                 *cmd_end = save_char;
1092
1093                                 /* copy command */
1094                                 if (matcherstr->len > 0) {
1095                                         g_string_append(matcherstr, " ");
1096                                 }
1097                                 if (dontmatch)
1098                                         g_string_append(matcherstr, "~");
1099                                 g_string_append(matcherstr, cmds[i].command);
1100                                 g_string_append(matcherstr, " ");
1101
1102                                 /* stop if no params required */
1103                                 if (cmds[i].numparams == 0)
1104                                         break;
1105
1106                                 /* extract a parameter, allow quotes */
1107                                 while (*cmd_end && isspace((guchar)*cmd_end))
1108                                         cmd_end++;
1109
1110                                 cmd_start = cmd_end;
1111                                 if (*cmd_start == '"') {
1112                                         term_char = '"';
1113                                         cmd_end++;
1114                                 }
1115                                 else
1116                                         term_char = ' ';
1117
1118                                 /* extract actual parameter */
1119                                 while ((*cmd_end) && (*cmd_end != term_char))
1120                                         cmd_end++;
1121
1122                                 if (*cmd_end == '"')
1123                                         cmd_end++;
1124
1125                                 save_char = *cmd_end;
1126                                 *cmd_end = '\0';
1127
1128                                 if (cmds[i].qualifier) {
1129                                         if (casesens)
1130                                                 g_string_append(matcherstr, "regexp ");
1131                                         else
1132                                                 g_string_append(matcherstr, "regexpcase ");
1133                                 }
1134
1135                                 /* do we need to add quotes ? */
1136                                 if (cmds[i].quotes && term_char != '"')
1137                                         g_string_append(matcherstr, "\"");
1138
1139                                 /* copy actual parameter */
1140                                 g_string_append(matcherstr, cmd_start);
1141
1142                                 /* do we need to add quotes ? */
1143                                 if (cmds[i].quotes && term_char != '"')
1144                                         g_string_append(matcherstr, "\"");
1145
1146                                 /* restore original character */
1147                                 *cmd_end = save_char;
1148
1149                                 break;
1150                         }
1151                 }
1152
1153                 if (*cmd_end)
1154                         cmd_end++;
1155                 cmd_start = cmd_end;
1156         }
1157
1158         g_free(copy_str);
1159
1160         /* return search string if no match is found to allow
1161            all available filtering expressions in quicksearch */
1162         if (matcherstr->len > 0) returnstr = matcherstr->str;
1163         else returnstr = g_strdup(search_string);
1164
1165         g_string_free(matcherstr, FALSE);
1166         return returnstr;
1167 }
1168
1169 static gchar *expand_tag_search_string(const gchar *search_string)
1170 {
1171         gchar *newstr = NULL;
1172         gchar **words = search_string ? g_strsplit(search_string, " ", -1):NULL;
1173         gint i = 0;
1174         while (words && words[i] && *words[i]) {
1175                 g_strstrip(words[i]);
1176                 if (!newstr) {
1177                         newstr = g_strdup_printf("tag regexpcase \"%s\"", words[i]);
1178                 } else {
1179                         gint o_len = strlen(newstr);
1180                         gint s_len = 18; /* strlen("|tag regexpcase \"\"") */
1181                         gint n_len = s_len + strlen(words[i]);
1182                         newstr = g_realloc(newstr,o_len+n_len+1);
1183                         strcpy(newstr+o_len, "|tag regexpcase \"");
1184                         strcpy(newstr+o_len+(s_len-1), words[i]);
1185                         strcpy(newstr+o_len+(n_len-1), "\"");
1186                 }
1187                 i++;
1188         }
1189         g_strfreev(words);
1190         return newstr;
1191 }
1192
1193 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run)
1194 {
1195         quicksearch->running = run;
1196 }
1197
1198 gboolean quicksearch_is_running(QuickSearch *quicksearch)
1199 {
1200         return quicksearch->running;
1201 }
1202
1203 void quicksearch_pass_key(QuickSearch *quicksearch, guint val, GdkModifierType mod)
1204 {
1205         GtkEntry *entry = GTK_ENTRY(GTK_BIN(quicksearch->search_string_entry)->child);
1206         glong curpos = gtk_editable_get_position(GTK_EDITABLE(entry));
1207         guint32 c;
1208         char *str = g_strdup(gtk_entry_get_text(entry));
1209         char *begin = str;
1210         char *end = NULL;
1211         char *new = NULL;
1212         char key[7] = "";
1213         guint char_len = 0;
1214
1215         if (gtk_editable_get_selection_bounds(GTK_EDITABLE(entry), NULL, NULL)) {
1216                 /* remove selection */
1217                 gtk_editable_delete_selection(GTK_EDITABLE(entry));
1218                 curpos = gtk_editable_get_position(GTK_EDITABLE(entry));
1219                 /* refresh string */
1220                 g_free(str);
1221                 str = g_strdup(gtk_entry_get_text(entry));
1222                 begin = str;
1223         }
1224
1225         if (!(c = gdk_keyval_to_unicode(val))) {
1226                 g_free(str);
1227                 return;
1228         }
1229         char_len = g_unichar_to_utf8(c, key);
1230         if (char_len < 0)
1231                 return;
1232         key[char_len] = '\0';
1233         if (curpos < g_utf8_strlen(str, -1)) {
1234                 gchar *stop = g_utf8_offset_to_pointer(begin, curpos);
1235                 end = g_strdup(g_utf8_offset_to_pointer(str, curpos));
1236                 *stop = '\0';
1237                 new = g_strdup_printf("%s%s%s", begin, key, end);
1238                 gtk_entry_set_text(entry, new);
1239                 g_free(end);
1240         } else {
1241                 new = g_strdup_printf("%s%s", begin, key);
1242                 gtk_entry_set_text(entry, new);
1243         }
1244         g_free(str);
1245         g_free(new);
1246         gtk_editable_set_position(GTK_EDITABLE(entry), curpos+1);
1247
1248 }
1249
1250 static gboolean quicksearch_match_subfolder(QuickSearch *quicksearch,
1251                                  FolderItem *src)
1252 {
1253         GSList *msglist = NULL;
1254         GSList *cur;
1255         gboolean result = FALSE;
1256         gint num = 0, total = 0;
1257         gint interval = quicksearch_is_fast(quicksearch) ? 5000:100;
1258
1259         statusbar_print_all(_("Searching in %s... \n"),
1260                 src->path ? src->path : "(null)");
1261                 
1262         msglist = folder_item_get_msg_list(src);
1263         total = src->total_msgs;
1264         folder_item_update_freeze();
1265         for (cur = msglist; cur != NULL; cur = cur->next) {
1266                 MsgInfo *msg = (MsgInfo *)cur->data;
1267                 statusbar_progress_all(num++,total, interval);
1268                 if (quicksearch_match(quicksearch, msg)) {
1269                         result = TRUE;
1270                         break;
1271                 }
1272                 if (num % interval == 0)
1273                         GTK_EVENTS_FLUSH();
1274                 if (!quicksearch_is_active(quicksearch))
1275                         break;
1276         }
1277         folder_item_update_thaw();
1278         statusbar_progress_all(0,0,0);
1279         statusbar_pop_all();
1280
1281         procmsg_msg_list_free(msglist);
1282         return result;
1283 }
1284
1285 void quicksearch_search_subfolders(QuickSearch *quicksearch,
1286                                    FolderView *folderview,
1287                                    FolderItem *folder_item)
1288 {
1289         FolderItem *cur = NULL;
1290         GNode *node = folder_item->node->children;
1291
1292         if (!prefs_common.summary_quicksearch_recurse
1293         ||  quicksearch->in_typing == TRUE)
1294                 return;
1295
1296         for (; node != NULL; node = node->next) {
1297                 cur = FOLDER_ITEM(node->data);
1298                 if (quicksearch_match_subfolder(quicksearch, cur)) {
1299                         folderview_update_search_icon(cur, TRUE);
1300                 } else {
1301                         folderview_update_search_icon(cur, FALSE);
1302                 }
1303                 if (cur->node->children)
1304                         quicksearch_search_subfolders(quicksearch,
1305                                                       folderview,
1306                                                       cur);
1307         }
1308         quicksearch->root_folder_item = folder_item;
1309         if (!quicksearch_is_active(quicksearch))
1310                 quicksearch_reset_cur_folder_item(quicksearch);
1311 }
1312
1313 static void quicksearch_reset_folder_items(QuickSearch *quicksearch,
1314                                     FolderItem *folder_item)
1315 {
1316         FolderItem *cur = NULL;
1317         GNode *node = (folder_item && folder_item->node) ?
1318                         folder_item->node->children : NULL;
1319
1320         for (; node != NULL; node = node->next) {
1321                 cur = FOLDER_ITEM(node->data);
1322                 folderview_update_search_icon(cur, FALSE);
1323                 if (cur->node->children)
1324                         quicksearch_reset_folder_items(quicksearch,
1325                                                        cur);
1326         }
1327 }
1328
1329 void quicksearch_reset_cur_folder_item(QuickSearch *quicksearch)
1330 {
1331         if (quicksearch->root_folder_item)
1332                 quicksearch_reset_folder_items(quicksearch,
1333                                                quicksearch->root_folder_item);
1334
1335         quicksearch->root_folder_item = NULL;
1336 }
1337
1338 gboolean quicksearch_is_in_typing(QuickSearch *quicksearch)
1339 {
1340         return quicksearch->in_typing;
1341 }