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