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