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