71be2e480502f01d66beb25c40d821437c9e5531
[claws.git] / src / gtk / quicksearch.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2004 Hiroyuki Yamamoto & the Sylpheed-Claws team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <ctype.h>
27
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30
31 #include "utils.h"
32 #include "menu.h"
33 #include "prefs_common.h"
34 #include "description_window.h"
35 #include "matcher.h"
36 #include "matcher_parser.h"
37 #include "quicksearch.h"
38 #include "folderview.h"
39 #include "folder.h"
40 #include "prefs_matcher.h"
41 #include "sylpheed.h"
42 #include "statusbar.h"
43
44 struct _QuickSearch
45 {
46         GtkWidget                       *hbox_search;
47         GtkWidget                       *search_type;
48         GtkWidget                       *search_type_opt;
49         GtkWidget                       *search_string_entry;
50         GtkWidget                       *search_condition_expression;
51         GtkWidget                       *search_description;
52         GtkWidget                       *clear_search;
53
54         gboolean                         active;
55         gchar                           *search_string;
56         MatcherList                     *matcher_list;
57
58         QuickSearchExecuteCallback       callback;
59         gpointer                         callback_data;
60         gboolean                         running;
61         gboolean                         has_focus;
62         gboolean                         matching;
63         gboolean                         deferred_free;
64         FolderItem                      *root_folder_item;
65 };
66
67 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run);
68 static void quicksearch_set_active(QuickSearch *quicksearch, gboolean active);
69 static void quicksearch_reset_folder_items(QuickSearch *quicksearch, FolderItem *folder_item);
70
71 static void prepare_matcher(QuickSearch *quicksearch)
72 {
73         const gchar *search_string = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry));
74
75         if (search_string == NULL || search_string[0] == '\0') {
76                 quicksearch_set_active(quicksearch, FALSE);
77         }
78
79         if (quicksearch->matcher_list != NULL) {
80                 if (quicksearch->matching) {
81                         quicksearch->deferred_free = TRUE;
82                         return;
83                 }
84                 quicksearch->deferred_free = FALSE;
85                 matcherlist_free(quicksearch->matcher_list);
86                 quicksearch->matcher_list = NULL;
87         }
88
89         if (search_string == NULL || search_string[0] == '\0') {
90                 return;
91         }
92
93         if (prefs_common.summary_quicksearch_type == QUICK_SEARCH_EXTENDED) {
94                 char *newstr = NULL;
95
96                 newstr = expand_search_string(search_string);
97                 if (newstr && newstr[0] != '\0') {
98                         quicksearch->matcher_list = matcher_parser_get_cond(newstr);
99                         g_free(newstr);
100                 } else {
101                         quicksearch->matcher_list = NULL;
102                         quicksearch_set_active(quicksearch, FALSE);
103
104                         return;
105                 }
106         } else {
107                 if (quicksearch->search_string != NULL)
108                         g_free(quicksearch->search_string);
109                 quicksearch->search_string = g_strdup(search_string);
110         }
111
112         quicksearch_set_active(quicksearch, TRUE);
113 }
114
115 static void update_extended_buttons (QuickSearch *quicksearch)
116 {
117         GtkWidget *expr_btn = quicksearch->search_condition_expression;
118         GtkWidget *ext_btn = quicksearch->search_description;
119         
120         g_return_if_fail(expr_btn != NULL);
121         g_return_if_fail(ext_btn != NULL);
122                 
123         if (prefs_common.summary_quicksearch_type == QUICK_SEARCH_EXTENDED) {
124                 gtk_widget_show(expr_btn);
125                 gtk_widget_show(ext_btn);
126         } else {
127                 gtk_widget_hide(expr_btn);
128                 gtk_widget_hide(ext_btn);
129         }
130 }
131
132 static gboolean searchbar_focus_evt(GtkWidget *widget, GdkEventFocus *event,
133                                   QuickSearch *quicksearch)
134 {
135         quicksearch->has_focus = (event && event->in);
136         return FALSE;
137 }
138
139 gboolean quicksearch_has_focus(QuickSearch *quicksearch)
140 {
141         return quicksearch->has_focus;
142 }
143
144 static void searchbar_run(QuickSearch *quicksearch)
145 {
146         const gchar *search_string = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry));
147
148         if (search_string && strlen(search_string) != 0) {
149                 prefs_common.summary_quicksearch_history =
150                         add_history(prefs_common.summary_quicksearch_history,
151                                         search_string);
152                 gtk_combo_set_popdown_strings(GTK_COMBO(quicksearch->search_string_entry), 
153                         prefs_common.summary_quicksearch_history);                      
154         }
155
156         prepare_matcher(quicksearch);
157
158         quicksearch_set_running(quicksearch, TRUE);
159         if (quicksearch->callback != NULL)
160                 quicksearch->callback(quicksearch, quicksearch->callback_data);
161         quicksearch_set_running(quicksearch, FALSE);
162 }
163
164 static gboolean searchbar_pressed(GtkWidget *widget, GdkEventKey *event,
165                                   QuickSearch *quicksearch)
166 {
167         if (event != NULL && event->keyval == GDK_Escape) {
168                 quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
169                 return TRUE;
170         }
171
172         if (event != NULL && event->keyval == GDK_Return) {
173                 /* add expression to history list and exec quicksearch */
174                 searchbar_run(quicksearch);
175
176                 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
177                 return TRUE;
178         }
179
180         return FALSE;           
181 }
182
183 static gboolean searchtype_changed(GtkMenuItem *widget, gpointer data)
184 {
185         QuickSearch *quicksearch = (QuickSearch *)data;
186
187         prefs_common.summary_quicksearch_type = GPOINTER_TO_INT(g_object_get_data(
188                                    G_OBJECT(GTK_MENU_ITEM(gtk_menu_get_active(
189                                    GTK_MENU(quicksearch->search_type)))), MENU_VAL_ID));
190
191         /* Show extended search description button, only when Extended is selected */
192         update_extended_buttons(quicksearch);
193
194         prepare_matcher(quicksearch);
195
196         quicksearch_set_running(quicksearch, TRUE);
197         if (quicksearch->callback != NULL)
198                 quicksearch->callback(quicksearch, quicksearch->callback_data);
199         quicksearch_set_running(quicksearch, FALSE);
200         return TRUE;
201 }
202
203 static gboolean searchtype_recursive_changed(GtkMenuItem *widget, gpointer data)
204 {
205         QuickSearch *quicksearch = (QuickSearch *)data;
206         gboolean checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
207         
208         prefs_common.summary_quicksearch_recurse = checked; 
209
210         /* reselect the search type */
211         gtk_option_menu_set_history(GTK_OPTION_MENU(quicksearch->search_type_opt), 
212                                     prefs_common.summary_quicksearch_type);
213
214         prepare_matcher(quicksearch);
215
216         quicksearch_set_running(quicksearch, TRUE);
217         if (quicksearch->callback != NULL)
218                 quicksearch->callback(quicksearch, quicksearch->callback_data);
219         quicksearch_set_running(quicksearch, FALSE);
220         return TRUE;
221 }
222
223 static gboolean searchtype_sticky_changed(GtkMenuItem *widget, gpointer data)
224 {
225         QuickSearch *quicksearch = (QuickSearch *)data;
226         gboolean checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
227         
228         prefs_common.summary_quicksearch_sticky = checked; 
229
230         /* reselect the search type */
231         gtk_option_menu_set_history(GTK_OPTION_MENU(quicksearch->search_type_opt), 
232                                     prefs_common.summary_quicksearch_type);
233
234         return TRUE;
235 }
236
237 /*
238  * Strings describing how to use Extended Search
239  * 
240  * When adding new lines, remember to put 2 strings for each line
241  */
242 static gchar *search_descr_strings[] = {
243         "a",     N_("all messages"),
244         "ag #",  N_("messages whose age is greater than #"),
245         "al #",  N_("messages whose age is less than #"),
246         "b S",   N_("messages which contain S in the message body"),
247         "B S",   N_("messages which contain S in the whole message"),
248         "c S",   N_("messages carbon-copied to S"),
249         "C S",   N_("message is either to: or cc: to S"),
250         "D",     N_("deleted messages"), /** how I can filter deleted messages **/
251         "e S",   N_("messages which contain S in the Sender field"),
252         "E S",   N_("true if execute \"S\" succeeds"),
253         "f S",   N_("messages originating from user S"),
254         "F",     N_("forwarded messages"),
255         "h S",   N_("messages which contain header S"),
256         "i S",   N_("messages which contain S in Message-ID header"),
257         "I S",   N_("messages which contain S in inreplyto header"),
258         "L",     N_("locked messages"),
259         "n S",   N_("messages which are in newsgroup S"),
260         "N",     N_("new messages"),
261         "O",     N_("old messages"),
262         "p",     N_("incomplete messages (not entirely downloaded)"),
263         "r",     N_("messages which have been replied to"),
264         "R",     N_("read messages"),
265         "s S",   N_("messages which contain S in subject"),
266         "se #",  N_("messages whose score is equal to #"),
267         "sg #",  N_("messages whose score is greater than #"),
268         "sl #",  N_("messages whose score is lower than #"),
269         "Se #",  N_("messages whose size is equal to #"),
270         "Sg #",  N_("messages whose size is greater than #"),
271         "Ss #",  N_("messages whose size is smaller than #"),
272         "t S",   N_("messages which have been sent to S"),
273         "T",     N_("marked messages"),
274         "U",     N_("unread messages"),
275         "x S",   N_("messages which contain S in References header"),
276         "X cmd", N_("messages returning 0 when passed to command"),
277         "y S",   N_("messages which contain S in X-Label header"),
278         "",      "" ,
279         "&amp;",         N_("logical AND operator"),
280         "|",     N_("logical OR operator"),
281         "! or ~",       N_("logical NOT operator"),
282         "%",     N_("case sensitive search"),
283         "",      "" ,
284         " ",     N_("all filtering expressions are allowed"),
285         NULL,    NULL 
286 };
287  
288 static DescriptionWindow search_descr = {
289         NULL,
290         NULL, 
291         2,
292         N_("Extended Search"),
293         N_("Extended Search allows the user to define criteria that messages must "
294            "have in order to match and be displayed in the message list.\n\n"
295            "The following symbols can be used:"),
296         search_descr_strings
297 };
298         
299 static void search_description_cb(GtkWidget *widget)
300 {
301         description_window_create(&search_descr);
302 };
303
304 static gboolean clear_search_cb(GtkMenuItem *widget, gpointer data)
305 {
306         QuickSearch *quicksearch = (QuickSearch *)data;
307
308         if (!quicksearch->active)
309                 return TRUE;
310
311         quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
312         
313         return TRUE;
314 };
315
316 static void search_condition_expr_done(MatcherList * matchers)
317 {
318         gchar *str;
319
320         g_return_if_fail(
321                         mainwindow_get_mainwindow()->summaryview->quicksearch != NULL);
322
323         if (matchers == NULL)
324                 return;
325
326         str = matcherlist_to_string(matchers);
327
328         if (str != NULL) {
329                 quicksearch_set(mainwindow_get_mainwindow()->summaryview->quicksearch,
330                                 prefs_common.summary_quicksearch_type, str);
331                 g_free(str);
332
333                 /* add expression to history list and exec quicksearch */
334                 searchbar_run(mainwindow_get_mainwindow()->summaryview->quicksearch);
335         }
336 }
337
338 static gboolean search_condition_expr(GtkMenuItem *widget, gpointer data)
339 {
340         const gchar * cond_str;
341         MatcherList * matchers = NULL;
342
343         g_return_val_if_fail(
344                         mainwindow_get_mainwindow()->summaryview->quicksearch != NULL,
345                         FALSE);
346
347         /* re-use it the current quicksearch value if it's a condition expression,
348            otherwise ignore it silently */
349         cond_str = gtk_entry_get_text(
350                         GTK_ENTRY(GTK_COMBO(mainwindow_get_mainwindow()->summaryview->quicksearch->
351                         search_string_entry)->entry));
352         if (*cond_str != '\0') {
353                 matchers = matcher_parser_get_cond((gchar*)cond_str);
354         }
355
356         prefs_matcher_open(matchers, search_condition_expr_done);
357
358         if (matchers != NULL)
359                 matcherlist_free(matchers);
360
361         return TRUE;
362 };
363                 
364 QuickSearch *quicksearch_new()
365 {
366         QuickSearch *quicksearch;
367
368         GtkWidget *hbox_search;
369         GtkWidget *search_type_opt;
370         GtkWidget *search_type;
371         GtkWidget *search_string_entry;
372         GtkWidget *search_hbox;
373         GtkWidget *search_description;
374         GtkWidget *clear_search;
375         GtkWidget *search_condition_expression;
376         GtkWidget *menuitem;
377         GtkTooltips *search_cond_expr_tip;
378
379         quicksearch = g_new0(QuickSearch, 1);
380
381         /* quick search */
382         hbox_search = gtk_hbox_new(FALSE, 0);
383
384         search_type_opt = gtk_option_menu_new();
385         gtk_widget_show(search_type_opt);
386         gtk_box_pack_start(GTK_BOX(hbox_search), search_type_opt, FALSE, FALSE, 0);
387
388         search_type = gtk_menu_new();
389         MENUITEM_ADD (search_type, menuitem, _("Subject"), QUICK_SEARCH_SUBJECT);
390         g_signal_connect(G_OBJECT(menuitem), "activate",
391                          G_CALLBACK(searchtype_changed),
392                          quicksearch);
393         MENUITEM_ADD (search_type, menuitem, _("From"), QUICK_SEARCH_FROM);
394         g_signal_connect(G_OBJECT(menuitem), "activate",
395                          G_CALLBACK(searchtype_changed),
396                          quicksearch);
397         MENUITEM_ADD (search_type, menuitem, _("To"), QUICK_SEARCH_TO);
398         g_signal_connect(G_OBJECT(menuitem), "activate",
399                          G_CALLBACK(searchtype_changed),
400                          quicksearch);
401         MENUITEM_ADD (search_type, menuitem, _("Extended"), QUICK_SEARCH_EXTENDED);
402         g_signal_connect(G_OBJECT(menuitem), "activate",
403                          G_CALLBACK(searchtype_changed),
404                          quicksearch);
405
406         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), gtk_separator_menu_item_new());
407         
408         menuitem = gtk_check_menu_item_new_with_label(_("Recursive"));
409         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
410         
411         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
412                                         prefs_common.summary_quicksearch_recurse);
413         
414         g_signal_connect(G_OBJECT(menuitem), "activate",
415                          G_CALLBACK(searchtype_recursive_changed),
416                          quicksearch);
417
418         menuitem = gtk_check_menu_item_new_with_label(_("Sticky"));
419         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
420         
421         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
422                                         prefs_common.summary_quicksearch_sticky);
423         
424         g_signal_connect(G_OBJECT(menuitem), "activate",
425                          G_CALLBACK(searchtype_sticky_changed),
426                          quicksearch);
427
428         gtk_option_menu_set_menu(GTK_OPTION_MENU(search_type_opt), search_type);
429         
430         gtk_option_menu_set_history(GTK_OPTION_MENU(search_type_opt), prefs_common.summary_quicksearch_type);
431         
432         gtk_widget_show(search_type);
433         
434         search_string_entry = gtk_combo_new();
435         gtk_box_pack_start(GTK_BOX(hbox_search), search_string_entry, FALSE, FALSE, 2);
436         gtk_combo_set_value_in_list(GTK_COMBO(search_string_entry), FALSE, TRUE);
437         gtk_combo_set_case_sensitive(GTK_COMBO(search_string_entry), TRUE);
438         if (prefs_common.summary_quicksearch_history) 
439                 gtk_combo_set_popdown_strings(GTK_COMBO(search_string_entry), 
440                         prefs_common.summary_quicksearch_history);
441         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(search_string_entry)->entry), "");
442         gtk_widget_show(search_string_entry);
443
444         search_hbox = gtk_hbox_new(FALSE, 5);
445
446         clear_search = gtk_button_new_with_label(_(" Clear "));
447         gtk_box_pack_start(GTK_BOX(search_hbox), clear_search,
448                            FALSE, FALSE, 0);
449         g_signal_connect(G_OBJECT(clear_search), "clicked",
450                          G_CALLBACK(clear_search_cb), quicksearch);
451         gtk_widget_show(clear_search);
452
453         search_condition_expression = gtk_button_new_with_label (_(" ... "));
454         gtk_box_pack_start(GTK_BOX(search_hbox), search_condition_expression,
455                            FALSE, FALSE, 0);
456         g_signal_connect(G_OBJECT (search_condition_expression), "clicked",
457                          G_CALLBACK(search_condition_expr),
458                          quicksearch);
459         search_cond_expr_tip = gtk_tooltips_new();
460         gtk_tooltips_set_tip(GTK_TOOLTIPS(search_cond_expr_tip),
461                              search_condition_expression,
462                              _("Edit search criteria"), NULL);
463         gtk_widget_show(search_condition_expression);
464
465         search_description = gtk_button_new_with_label(_(" Extended Symbols... "));
466         gtk_box_pack_start(GTK_BOX(search_hbox), search_description,
467                            FALSE, FALSE, 0);
468         g_signal_connect(G_OBJECT(search_description), "clicked",
469                          G_CALLBACK(search_description_cb), NULL);
470         gtk_widget_show(search_description);
471
472         gtk_box_pack_start(GTK_BOX(hbox_search), search_hbox, FALSE, FALSE, 2);                         
473         gtk_widget_show(search_hbox);
474         
475         g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry), 
476                            "key_press_event",
477                            G_CALLBACK(searchbar_pressed),
478                            quicksearch);
479         g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry),
480                          "focus_in_event",
481                          G_CALLBACK(searchbar_focus_evt),
482                          quicksearch);
483         g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry),
484                          "focus_out_event",
485                          G_CALLBACK(searchbar_focus_evt),
486                          quicksearch);
487
488         quicksearch->hbox_search = hbox_search;
489         quicksearch->search_type = search_type;
490         quicksearch->search_type_opt = search_type_opt;
491         quicksearch->search_string_entry = search_string_entry;
492         quicksearch->search_condition_expression = search_condition_expression;
493         quicksearch->search_description = search_description;
494         quicksearch->matcher_list = NULL;
495         quicksearch->active = FALSE;
496         quicksearch->running = FALSE;
497         quicksearch->clear_search = clear_search;
498
499         update_extended_buttons(quicksearch);
500         
501         return quicksearch;
502 }
503
504 GtkWidget *quicksearch_get_widget(QuickSearch *quicksearch)
505 {
506         return quicksearch->hbox_search;
507 }
508
509 void quicksearch_show(QuickSearch *quicksearch)
510 {
511         prepare_matcher(quicksearch);
512         gtk_widget_show(quicksearch->hbox_search);
513         update_extended_buttons(quicksearch);
514         gtk_widget_grab_focus(
515                 GTK_WIDGET(GTK_COMBO(quicksearch->search_string_entry)->entry));
516 }
517
518 void quicksearch_hide(QuickSearch *quicksearch)
519 {
520         quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
521         quicksearch_set_active(quicksearch, FALSE);
522         gtk_widget_hide(quicksearch->hbox_search);
523 }
524
525 void quicksearch_set(QuickSearch *quicksearch, QuickSearchType type,
526                      const gchar *matchstring)
527 {
528         gtk_option_menu_set_history(GTK_OPTION_MENU(quicksearch->search_type_opt),
529                                     type);
530         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry),
531                            matchstring);
532         prefs_common.summary_quicksearch_type = type;
533
534         prepare_matcher(quicksearch);
535
536         quicksearch_set_running(quicksearch, TRUE);
537         if (quicksearch->callback != NULL)
538                 quicksearch->callback(quicksearch, quicksearch->callback_data); 
539         quicksearch_set_running(quicksearch, FALSE);
540 }
541
542 gboolean quicksearch_is_active(QuickSearch *quicksearch)
543 {
544         return quicksearch->active;
545 }
546
547 static void quicksearch_set_active(QuickSearch *quicksearch, gboolean active)
548 {
549         static GdkColor yellow;
550         static GdkColor black;
551         static gboolean yellow_initialised = FALSE;
552
553         if (!yellow_initialised) {
554                 gdk_color_parse("#f5f6be", &yellow);
555                 gdk_color_parse("#000000", &black);
556                 yellow_initialised = gdk_colormap_alloc_color(
557                         gdk_colormap_get_system(), &yellow, FALSE, TRUE);
558                 yellow_initialised &= gdk_colormap_alloc_color(
559                         gdk_colormap_get_system(), &black, FALSE, TRUE);
560                 
561         }
562         quicksearch->active = active;
563
564         if (active) {
565                 gtk_widget_set_sensitive(quicksearch->clear_search, TRUE);
566                 if (yellow_initialised)
567                         gtk_widget_modify_base(
568                                 GTK_COMBO(quicksearch->search_string_entry)->entry, 
569                                 GTK_STATE_NORMAL, &yellow);
570                         gtk_widget_modify_text(
571                                 GTK_COMBO(quicksearch->search_string_entry)->entry, 
572                                 GTK_STATE_NORMAL, &black);
573         } else {
574                 gtk_widget_set_sensitive(quicksearch->clear_search, FALSE);
575                 if (yellow_initialised)
576                         gtk_widget_modify_base(
577                                 GTK_COMBO(quicksearch->search_string_entry)->entry, 
578                                 GTK_STATE_NORMAL, NULL);
579                         gtk_widget_modify_text(
580                                 GTK_COMBO(quicksearch->search_string_entry)->entry, 
581                                 GTK_STATE_NORMAL, NULL);
582         }
583         
584         if (!active) {
585                 quicksearch_reset_cur_folder_item(quicksearch);
586         }
587 }
588
589 void quicksearch_set_execute_callback(QuickSearch *quicksearch,
590                                       QuickSearchExecuteCallback callback,
591                                       gpointer data)
592 {
593         quicksearch->callback = callback;
594         quicksearch->callback_data = data;
595 }
596
597 gboolean quicksearch_match(QuickSearch *quicksearch, MsgInfo *msginfo)
598 {
599         gchar *searched_header = NULL;
600         gboolean result = FALSE;
601
602         if (!quicksearch->active)
603                 return TRUE;
604
605         switch (prefs_common.summary_quicksearch_type) {
606         case QUICK_SEARCH_SUBJECT:
607                 searched_header = msginfo->subject;
608                 break;
609         case QUICK_SEARCH_FROM:
610                 searched_header = msginfo->from;
611                 break;
612         case QUICK_SEARCH_TO:
613                 searched_header = msginfo->to;
614                 break;
615         case QUICK_SEARCH_EXTENDED:
616                 break;
617         default:
618                 debug_print("unknown search type (%d)\n", prefs_common.summary_quicksearch_type);
619                 break;
620         }
621         quicksearch->matching = TRUE;
622         if (prefs_common.summary_quicksearch_type != QUICK_SEARCH_EXTENDED && 
623             quicksearch->search_string &&
624             searched_header && strcasestr(searched_header, quicksearch->search_string) != NULL)
625                 result = TRUE;
626         else if ((quicksearch->matcher_list != NULL) && 
627                  matcherlist_match(quicksearch->matcher_list, msginfo)) 
628                 result = TRUE;
629
630         quicksearch->matching = FALSE;
631         if (quicksearch->deferred_free) {
632                 prepare_matcher(quicksearch);
633         }
634
635         return result;
636 }
637
638 /* allow Mutt-like patterns in quick search */
639 gchar *expand_search_string(const gchar *search_string)
640 {
641         int i = 0;
642         gchar term_char, save_char;
643         gchar *cmd_start, *cmd_end;
644         GString *matcherstr;
645         gchar *returnstr = NULL;
646         gchar *copy_str;
647         gboolean casesens, dontmatch;
648         /* list of allowed pattern abbreviations */
649         struct {
650                 gchar           *abbreviated;   /* abbreviation */
651                 gchar           *command;       /* actual matcher command */ 
652                 gint            numparams;      /* number of params for cmd */
653                 gboolean        qualifier;      /* do we append regexpcase */
654                 gboolean        quotes;         /* do we need quotes */
655         }
656         cmds[] = {
657                 { "a",  "all",                          0,      FALSE,  FALSE },
658                 { "ag", "age_greater",                  1,      FALSE,  FALSE },
659                 { "al", "age_lower",                    1,      FALSE,  FALSE },
660                 { "b",  "body_part",                    1,      TRUE,   TRUE  },
661                 { "B",  "message",                      1,      TRUE,   TRUE  },
662                 { "c",  "cc",                           1,      TRUE,   TRUE  },
663                 { "C",  "to_or_cc",                     1,      TRUE,   TRUE  },
664                 { "D",  "deleted",                      0,      FALSE,  FALSE },
665                 { "e",  "header \"Sender\"",            1,      TRUE,   TRUE  },
666                 { "E",  "execute",                      1,      FALSE,  TRUE  },
667                 { "f",  "from",                         1,      TRUE,   TRUE  },
668                 { "F",  "forwarded",                    0,      FALSE,  FALSE },
669                 { "h",  "headers_part",                 1,      TRUE,   TRUE  },
670                 { "i",  "header \"Message-ID\"",        1,      TRUE,   TRUE  },
671                 { "I",  "inreplyto",                    1,      TRUE,   TRUE  },
672                 { "L",  "locked",                       0,      FALSE,  FALSE },
673                 { "n",  "newsgroups",                   1,      TRUE,   TRUE  },
674                 { "N",  "new",                          0,      FALSE,  FALSE },
675                 { "O",  "~new",                         0,      FALSE,  FALSE },
676                 { "r",  "replied",                      0,      FALSE,  FALSE },
677                 { "R",  "~unread",                      0,      FALSE,  FALSE },
678                 { "s",  "subject",                      1,      TRUE,   TRUE  },
679                 { "se", "score_equal",                  1,      FALSE,  FALSE },
680                 { "sg", "score_greater",                1,      FALSE,  FALSE },
681                 { "sl", "score_lower",                  1,      FALSE,  FALSE },
682                 { "Se", "size_equal",                   1,      FALSE,  FALSE },
683                 { "Sg", "size_greater",                 1,      FALSE,  FALSE },
684                 { "Ss", "size_smaller",                 1,      FALSE,  FALSE },
685                 { "t",  "to",                           1,      TRUE,   TRUE  },
686                 { "T",  "marked",                       0,      FALSE,  FALSE },
687                 { "U",  "unread",                       0,      FALSE,  FALSE },
688                 { "x",  "header \"References\"",        1,      TRUE,   TRUE  },
689                 { "X",  "test",                         1,      FALSE,  FALSE }, 
690                 { "y",  "header \"X-Label\"",           1,      TRUE,   TRUE  },
691                 { "&",  "&",                            0,      FALSE,  FALSE },
692                 { "|",  "|",                            0,      FALSE,  FALSE },
693                 { "p",  "partial",                      0,      FALSE,  FALSE },
694                 { NULL, NULL,                           0,      FALSE,  FALSE }
695         };
696
697         if (search_string == NULL)
698                 return NULL;
699
700         copy_str = g_strdup(search_string);
701
702         matcherstr = g_string_sized_new(16);
703         cmd_start = copy_str;
704         while (cmd_start && *cmd_start) {
705                 /* skip all white spaces */
706                 while (*cmd_start && isspace((guchar)*cmd_start))
707                         cmd_start++;
708                 cmd_end = cmd_start;
709
710                 /* extract a command */
711                 while (*cmd_end && !isspace((guchar)*cmd_end))
712                         cmd_end++;
713
714                 /* save character */
715                 save_char = *cmd_end;
716                 *cmd_end = '\0';
717
718                 dontmatch = FALSE;
719                 casesens = FALSE;
720
721                 /* ~ and ! mean logical NOT */
722                 if (*cmd_start == '~' || *cmd_start == '!')
723                 {
724                         dontmatch = TRUE;
725                         cmd_start++;
726                 }
727                 /* % means case sensitive match */
728                 if (*cmd_start == '%')
729                 {
730                         casesens = TRUE;
731                         cmd_start++;
732                 }
733
734                 /* find matching abbreviation */
735                 for (i = 0; cmds[i].command; i++) {
736                         if (!strcmp(cmd_start, cmds[i].abbreviated)) {
737                                 /* restore character */
738                                 *cmd_end = save_char;
739
740                                 /* copy command */
741                                 if (matcherstr->len > 0) {
742                                         g_string_append(matcherstr, " ");
743                                 }
744                                 if (dontmatch)
745                                         g_string_append(matcherstr, "~");
746                                 g_string_append(matcherstr, cmds[i].command);
747                                 g_string_append(matcherstr, " ");
748
749                                 /* stop if no params required */
750                                 if (cmds[i].numparams == 0)
751                                         break;
752
753                                 /* extract a parameter, allow quotes */
754                                 while (*cmd_end && isspace((guchar)*cmd_end))
755                                         cmd_end++;
756
757                                 cmd_start = cmd_end;
758                                 if (*cmd_start == '"') {
759                                         term_char = '"';
760                                         cmd_end++;
761                                 }
762                                 else
763                                         term_char = ' ';
764
765                                 /* extract actual parameter */
766                                 while ((*cmd_end) && (*cmd_end != term_char))
767                                         cmd_end++;
768
769                                 if (*cmd_end == '"')
770                                         cmd_end++;
771
772                                 save_char = *cmd_end;
773                                 *cmd_end = '\0';
774
775                                 if (cmds[i].qualifier) {
776                                         if (casesens)
777                                                 g_string_append(matcherstr, "regexp ");
778                                         else
779                                                 g_string_append(matcherstr, "regexpcase ");
780                                 }
781
782                                 /* do we need to add quotes ? */
783                                 if (cmds[i].quotes && term_char != '"')
784                                         g_string_append(matcherstr, "\"");
785
786                                 /* copy actual parameter */
787                                 g_string_append(matcherstr, cmd_start);
788
789                                 /* do we need to add quotes ? */
790                                 if (cmds[i].quotes && term_char != '"')
791                                         g_string_append(matcherstr, "\"");
792
793                                 /* restore original character */
794                                 *cmd_end = save_char;
795
796                                 break;
797                         }
798                 }
799
800                 if (*cmd_end)
801                         cmd_end++;
802                 cmd_start = cmd_end;
803         }
804
805         g_free(copy_str);
806
807         /* return search string if no match is found to allow 
808            all available filtering expressions in quicksearch */
809         if (matcherstr->len > 0) returnstr = matcherstr->str;
810         else returnstr = g_strdup(search_string);
811
812         g_string_free(matcherstr, FALSE);
813         return returnstr;
814 }
815
816 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run)
817 {
818         quicksearch->running = run;
819 }
820
821 gboolean quicksearch_is_running(QuickSearch *quicksearch) 
822 {
823         return quicksearch->running;
824 }
825
826 void quicksearch_pass_key(QuickSearch *quicksearch, guint val, GdkModifierType mod)
827 {
828         GtkEntry *entry = GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry);
829         gint curpos = gtk_editable_get_position(GTK_EDITABLE(entry));
830         char *str = g_strdup(gtk_entry_get_text(entry));
831         char *begin = str;
832         char *end = NULL;
833         char *new = NULL;
834         
835         if (mod == GDK_SHIFT_MASK)
836                 val = toupper(val);
837         
838         if (curpos < strlen(str)-1) {
839                 end = g_strdup(str+curpos);
840                 *(str+curpos) = '\0';
841                 new = g_strdup_printf("%s%c%s", begin, val, end);
842                 gtk_entry_set_text(entry, new);
843                 g_free(end);
844         } else {
845                 new = g_strdup_printf("%s%c", begin, val);
846                 gtk_entry_set_text(entry, new);
847         }
848         g_free(str);
849         g_free(new);
850         gtk_editable_set_position(GTK_EDITABLE(entry), curpos+1);
851         
852 }
853
854 static gboolean quicksearch_match_subfolder(QuickSearch *quicksearch, 
855                                  FolderItem *src)
856 {
857         GSList *msglist = folder_item_get_msg_list(src);
858         GSList *cur;
859         gboolean result = FALSE;
860         gint num = 0, total = src->total_msgs;
861         statusbar_print_all(_("Searching in %s... \n"), 
862                 src->path ? src->path : "(null)");
863         for (cur = msglist; cur != NULL; cur = cur->next) {
864                 MsgInfo *msg = (MsgInfo *)cur->data;
865                 statusbar_progress_all(num++,total, 50);
866                 if (quicksearch_match(quicksearch, msg)) {
867                         procmsg_msginfo_free(msg);
868                         result = TRUE;
869                         break;
870                 }
871                 procmsg_msginfo_free(msg);
872                 GTK_EVENTS_FLUSH();
873                 if (!quicksearch_is_active(quicksearch))
874                         break;
875         }
876         statusbar_progress_all(0,0,0);
877         statusbar_pop_all();
878
879         g_slist_free(msglist);
880         return result;
881 }
882
883 void quicksearch_search_subfolders(QuickSearch *quicksearch, 
884                                    FolderView *folderview,
885                                    FolderItem *folder_item)
886 {
887         FolderItem *cur = NULL;
888         GNode *node = folder_item->node->children;
889         
890         if (!prefs_common.summary_quicksearch_recurse)
891                 return;
892
893         for (; node != NULL; node = node->next) {
894                 cur = FOLDER_ITEM(node->data);
895                 if (quicksearch_match_subfolder(quicksearch, cur)) {
896                         folderview_update_search_icon(cur, TRUE);
897                 } else {
898                         folderview_update_search_icon(cur, FALSE);
899                 }
900                 if (cur->node->children)
901                         quicksearch_search_subfolders(quicksearch,
902                                                       folderview,
903                                                       cur);
904         }
905         quicksearch->root_folder_item = folder_item;
906         if (!quicksearch_is_active(quicksearch))
907                 quicksearch_reset_cur_folder_item(quicksearch);
908 }
909
910 static void quicksearch_reset_folder_items(QuickSearch *quicksearch,
911                                     FolderItem *folder_item)
912 {
913         FolderItem *cur = NULL;
914         GNode *node = (folder_item && folder_item->node) ? 
915                         folder_item->node->children : NULL;
916         
917         for (; node != NULL; node = node->next) {
918                 cur = FOLDER_ITEM(node->data);
919                 folderview_update_search_icon(cur, FALSE);
920                 if (cur->node->children)
921                         quicksearch_reset_folder_items(quicksearch,
922                                                        cur);
923         }
924 }
925
926 void quicksearch_reset_cur_folder_item(QuickSearch *quicksearch)
927 {
928         if (quicksearch->root_folder_item)
929                 quicksearch_reset_folder_items(quicksearch, 
930                                                quicksearch->root_folder_item);
931         
932         quicksearch->root_folder_item = NULL;
933 }