2007-02-22 [paul] 2.7.2cvs61
[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                                         GTK_WIDGET(GTK_COMBO(quicksearch->search_string_entry)->entry));
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
474         quicksearch = g_new0(QuickSearch, 1);
475
476         /* quick search */
477         hbox_search = gtk_hbox_new(FALSE, 0);
478
479         search_type_opt = gtk_option_menu_new();
480         gtk_widget_show(search_type_opt);
481         gtk_box_pack_start(GTK_BOX(hbox_search), search_type_opt, FALSE, FALSE, 0);
482
483         search_type = gtk_menu_new();
484         MENUITEM_ADD (search_type, menuitem, _("Subject"), QUICK_SEARCH_SUBJECT);
485         g_signal_connect(G_OBJECT(menuitem), "activate",
486                          G_CALLBACK(searchtype_changed),
487                          quicksearch);
488         MENUITEM_ADD (search_type, menuitem, _("From"), QUICK_SEARCH_FROM);
489         g_signal_connect(G_OBJECT(menuitem), "activate",
490                          G_CALLBACK(searchtype_changed),
491                          quicksearch);
492         MENUITEM_ADD (search_type, menuitem, _("To"), QUICK_SEARCH_TO);
493         g_signal_connect(G_OBJECT(menuitem), "activate",
494                          G_CALLBACK(searchtype_changed),
495                          quicksearch);
496         MENUITEM_ADD (search_type, menuitem, _("Extended"), QUICK_SEARCH_EXTENDED);
497         g_signal_connect(G_OBJECT(menuitem), "activate",
498                          G_CALLBACK(searchtype_changed),
499                          quicksearch);
500
501         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), gtk_separator_menu_item_new());
502
503         menuitem = gtk_check_menu_item_new_with_label(_("Recursive"));
504         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
505
506         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
507                                         prefs_common.summary_quicksearch_recurse);
508
509         g_signal_connect(G_OBJECT(menuitem), "activate",
510                          G_CALLBACK(searchtype_recursive_changed),
511                          quicksearch);
512
513         menuitem = gtk_check_menu_item_new_with_label(_("Sticky"));
514         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
515
516         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
517                                         prefs_common.summary_quicksearch_sticky);
518
519         g_signal_connect(G_OBJECT(menuitem), "activate",
520                          G_CALLBACK(searchtype_sticky_changed),
521                          quicksearch);
522
523         menuitem = gtk_check_menu_item_new_with_label(_("Type-ahead"));
524         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
525
526         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
527                                         prefs_common.summary_quicksearch_dynamic);
528
529         g_signal_connect(G_OBJECT(menuitem), "activate",
530                          G_CALLBACK(searchtype_dynamic_changed),
531                          quicksearch);
532
533         gtk_option_menu_set_menu(GTK_OPTION_MENU(search_type_opt), search_type);
534
535         gtk_option_menu_set_history(GTK_OPTION_MENU(search_type_opt), prefs_common.summary_quicksearch_type);
536
537         gtk_widget_show(search_type);
538
539         search_string_entry = gtk_combo_new();
540         gtk_box_pack_start(GTK_BOX(hbox_search), search_string_entry, FALSE, FALSE, 2);
541         gtk_combo_set_value_in_list(GTK_COMBO(search_string_entry), FALSE, TRUE);
542         gtk_combo_set_case_sensitive(GTK_COMBO(search_string_entry), TRUE);
543         if (prefs_common.summary_quicksearch_history)
544                 gtk_combo_set_popdown_strings(GTK_COMBO(search_string_entry),
545                         prefs_common.summary_quicksearch_history);
546         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(search_string_entry)->entry), "");
547         gtk_widget_show(search_string_entry);
548
549         search_hbox = gtk_hbox_new(FALSE, 5);
550
551 #if GTK_CHECK_VERSION(2, 8, 0)
552         clear_search = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
553 #else
554         clear_search = gtk_button_new_with_label(_(" Clear "));
555 #endif
556         gtk_box_pack_start(GTK_BOX(search_hbox), clear_search,
557                            FALSE, FALSE, 0);
558         g_signal_connect(G_OBJECT(clear_search), "clicked",
559                          G_CALLBACK(clear_search_cb), quicksearch);
560         gtk_tooltips_set_tip(GTK_TOOLTIPS(tips),
561                              clear_search,
562                              _("Clear the current search"), NULL);
563         gtk_widget_show(clear_search);
564
565 #if GTK_CHECK_VERSION(2, 8, 0)
566         search_condition_expression = gtk_button_new_from_stock(GTK_STOCK_EDIT);
567 #else
568         search_condition_expression = gtk_button_new_with_label(" ... ");
569 #endif
570         gtk_box_pack_start(GTK_BOX(search_hbox), search_condition_expression,
571                            FALSE, FALSE, 0);
572         g_signal_connect(G_OBJECT (search_condition_expression), "clicked",
573                          G_CALLBACK(search_condition_expr),
574                          quicksearch);
575         gtk_tooltips_set_tip(GTK_TOOLTIPS(tips),
576                              search_condition_expression,
577                              _("Edit search criteria"), NULL);
578         gtk_widget_show(search_condition_expression);
579
580 #if GTK_CHECK_VERSION(2, 8, 0)
581         search_description = gtk_button_new_from_stock(GTK_STOCK_INFO);
582 #else
583         search_description = gtk_button_new_with_label(_(" Extended Symbols... "));
584 #endif
585         gtk_box_pack_start(GTK_BOX(search_hbox), search_description,
586                            FALSE, FALSE, 0);
587         g_signal_connect(G_OBJECT(search_description), "clicked",
588                          G_CALLBACK(search_description_cb), NULL);
589         gtk_tooltips_set_tip(GTK_TOOLTIPS(tips),
590                              search_description,
591                              _("Information about extended symbols"), NULL);
592         gtk_widget_show(search_description);
593
594         gtk_box_pack_start(GTK_BOX(hbox_search), search_hbox, FALSE, FALSE, 2);
595         gtk_widget_show(search_hbox);
596
597         g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry),
598                            "key_press_event",
599                            G_CALLBACK(searchbar_pressed),
600                            quicksearch);
601
602         g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry),
603                          "changed",
604                          G_CALLBACK(searchbar_changed_cb),
605                          quicksearch);
606
607         g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry),
608                          "focus_in_event",
609                          G_CALLBACK(searchbar_focus_evt_in),
610                          quicksearch);
611         g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry),
612                          "focus_out_event",
613                          G_CALLBACK(searchbar_focus_evt_out),
614                          quicksearch);
615
616         quicksearch->hbox_search = hbox_search;
617         quicksearch->search_type = search_type;
618         quicksearch->search_type_opt = search_type_opt;
619         quicksearch->search_string_entry = search_string_entry;
620         quicksearch->search_condition_expression = search_condition_expression;
621         quicksearch->search_description = search_description;
622         quicksearch->matcher_list = NULL;
623         quicksearch->active = FALSE;
624         quicksearch->running = FALSE;
625         quicksearch->clear_search = clear_search;
626         quicksearch->in_typing = FALSE;
627         quicksearch->press_timeout_id = -1;
628
629         update_extended_buttons(quicksearch);
630
631         return quicksearch;
632 }
633
634 void quicksearch_relayout(QuickSearch *quicksearch)
635 {
636         switch (prefs_common.layout_mode) {
637         case NORMAL_LAYOUT:
638         case WIDE_LAYOUT:
639         case WIDE_MSGLIST_LAYOUT:
640 #if GTK_CHECK_VERSION(2, 8, 0)
641                 gtk_button_set_label(GTK_BUTTON(quicksearch->search_description), GTK_STOCK_INFO);
642                 gtk_button_set_label(GTK_BUTTON(quicksearch->search_condition_expression), GTK_STOCK_EDIT);
643                 gtk_button_set_label(GTK_BUTTON(quicksearch->clear_search), GTK_STOCK_CLEAR);
644 #else
645                 gtk_button_set_label(GTK_BUTTON(quicksearch->search_description), _(" Extended Symbols... "));
646                 gtk_button_set_label(GTK_BUTTON(quicksearch->search_condition_expression), " ... ");
647                 gtk_button_set_label(GTK_BUTTON(quicksearch->clear_search), _(" Clear "));
648 #endif
649                 break;
650         case VERTICAL_LAYOUT:
651 #if GTK_CHECK_VERSION(2, 8, 0)
652                 gtk_button_set_label(GTK_BUTTON(quicksearch->search_description), "");
653                 gtk_button_set_label(GTK_BUTTON(quicksearch->search_condition_expression), "");
654                 gtk_button_set_label(GTK_BUTTON(quicksearch->clear_search), "");
655
656                 gtk_button_set_image(GTK_BUTTON(quicksearch->search_description),
657                         gtk_image_new_from_stock(GTK_STOCK_INFO, GTK_ICON_SIZE_BUTTON));
658                 gtk_button_set_image(GTK_BUTTON(quicksearch->search_condition_expression),
659                         gtk_image_new_from_stock(GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON));
660                 gtk_button_set_image(GTK_BUTTON(quicksearch->clear_search),
661                         gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_BUTTON));
662 #else
663                 gtk_button_set_label(GTK_BUTTON(quicksearch->search_description), _("Info"));
664                 gtk_button_set_label(GTK_BUTTON(quicksearch->search_condition_expression), "...");
665                 gtk_button_set_label(GTK_BUTTON(quicksearch->clear_search), _("Clear"));
666 #endif
667                 break;
668         }
669 }
670
671 GtkWidget *quicksearch_get_widget(QuickSearch *quicksearch)
672 {
673         return quicksearch->hbox_search;
674 }
675
676 void quicksearch_show(QuickSearch *quicksearch)
677 {
678         MainWindow *mainwin = mainwindow_get_mainwindow();
679         GtkWidget *ctree = NULL;
680         prepare_matcher(quicksearch);
681         gtk_widget_show(quicksearch->hbox_search);
682         update_extended_buttons(quicksearch);
683         gtk_widget_grab_focus(
684                 GTK_WIDGET(GTK_COMBO(quicksearch->search_string_entry)->entry));
685
686         GTK_EVENTS_FLUSH();
687
688         if (!mainwin || !mainwin->summaryview) {
689                 return;
690         }
691         
692         ctree = summary_get_main_widget(mainwin->summaryview);
693         
694         if (ctree && mainwin->summaryview->selected)
695                 gtk_ctree_node_moveto(GTK_CTREE(ctree), 
696                                 mainwin->summaryview->selected, 
697                                 0, 0.5, 0);
698 }
699
700 void quicksearch_hide(QuickSearch *quicksearch)
701 {
702         if (quicksearch_is_active(quicksearch)) {
703                 quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
704                 quicksearch_set_active(quicksearch, FALSE);
705         }
706         gtk_widget_hide(quicksearch->hbox_search);
707 }
708
709 void quicksearch_set(QuickSearch *quicksearch, QuickSearchType type,
710                      const gchar *matchstring)
711 {
712         gtk_option_menu_set_history(GTK_OPTION_MENU(quicksearch->search_type_opt),
713                                     type);
714
715         if (!matchstring || !(*matchstring))
716                 quicksearch->in_typing = FALSE;
717
718         g_signal_handlers_block_by_func(G_OBJECT(GTK_COMBO(quicksearch->search_string_entry)->entry),
719                         G_CALLBACK(searchbar_changed_cb), quicksearch);
720         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry),
721                            matchstring);
722         g_signal_handlers_unblock_by_func(G_OBJECT(GTK_COMBO(quicksearch->search_string_entry)->entry),
723                         G_CALLBACK(searchbar_changed_cb), quicksearch);
724
725         prefs_common.summary_quicksearch_type = type;
726
727         prepare_matcher(quicksearch);
728
729         quicksearch_set_running(quicksearch, TRUE);
730         if (quicksearch->callback != NULL)
731                 quicksearch->callback(quicksearch, quicksearch->callback_data);
732         quicksearch_set_running(quicksearch, FALSE);
733 }
734
735 gboolean quicksearch_is_active(QuickSearch *quicksearch)
736 {
737         return quicksearch->active && 
738                 (prefs_common.summary_quicksearch_type != QUICK_SEARCH_EXTENDED
739                  || quicksearch->matcher_list != NULL);
740 }
741
742 static void quicksearch_set_active(QuickSearch *quicksearch, gboolean active)
743 {
744         static GdkColor yellow;
745         static GdkColor red;
746         static GdkColor black;
747         static gboolean colors_initialised = FALSE;
748         gboolean error = FALSE;
749
750         if (!colors_initialised) {
751                 gdk_color_parse("#f5f6be", &yellow);
752                 gdk_color_parse("#000000", &black);
753                 gdk_color_parse("#ff7070", &red);
754                 colors_initialised = gdk_colormap_alloc_color(
755                         gdk_colormap_get_system(), &yellow, FALSE, TRUE);
756                 colors_initialised &= gdk_colormap_alloc_color(
757                         gdk_colormap_get_system(), &black, FALSE, TRUE);
758                 colors_initialised &= gdk_colormap_alloc_color(
759                         gdk_colormap_get_system(), &red, FALSE, TRUE);
760         }
761
762         quicksearch->active = active;
763
764         if (active && 
765                 (prefs_common.summary_quicksearch_type == QUICK_SEARCH_EXTENDED
766                  && quicksearch->matcher_list == NULL))
767                 error = TRUE;
768
769         if (active) {
770                 gtk_widget_set_sensitive(quicksearch->clear_search, TRUE);
771                 if (colors_initialised) {
772                         gtk_widget_modify_base(
773                                 GTK_COMBO(quicksearch->search_string_entry)->entry,
774                                 GTK_STATE_NORMAL, error ? &red : &yellow);
775                         gtk_widget_modify_text(
776                                 GTK_COMBO(quicksearch->search_string_entry)->entry,
777                                 GTK_STATE_NORMAL, &black);
778                 }
779         } else {
780                 gtk_widget_set_sensitive(quicksearch->clear_search, FALSE);
781                 if (colors_initialised) {
782                         gtk_widget_modify_base(
783                                 GTK_COMBO(quicksearch->search_string_entry)->entry,
784                                 GTK_STATE_NORMAL, NULL);
785                         gtk_widget_modify_text(
786                                 GTK_COMBO(quicksearch->search_string_entry)->entry,
787                                 GTK_STATE_NORMAL, NULL);
788                 }
789         }
790
791         if (!active) {
792                 quicksearch_reset_cur_folder_item(quicksearch);
793         }
794 }
795
796 void quicksearch_set_execute_callback(QuickSearch *quicksearch,
797                                       QuickSearchExecuteCallback callback,
798                                       gpointer data)
799 {
800         quicksearch->callback = callback;
801         quicksearch->callback_data = data;
802 }
803
804 gboolean quicksearch_match(QuickSearch *quicksearch, MsgInfo *msginfo)
805 {
806         gchar *searched_header = NULL;
807         gboolean result = FALSE;
808
809         if (!quicksearch->active)
810                 return TRUE;
811
812         switch (prefs_common.summary_quicksearch_type) {
813         case QUICK_SEARCH_SUBJECT:
814                 searched_header = msginfo->subject;
815                 break;
816         case QUICK_SEARCH_FROM:
817                 searched_header = msginfo->from;
818                 break;
819         case QUICK_SEARCH_TO:
820                 searched_header = msginfo->to;
821                 break;
822         case QUICK_SEARCH_EXTENDED:
823                 break;
824         default:
825                 debug_print("unknown search type (%d)\n", prefs_common.summary_quicksearch_type);
826                 break;
827         }
828         quicksearch->matching = TRUE;
829         if (prefs_common.summary_quicksearch_type != QUICK_SEARCH_EXTENDED &&
830             quicksearch->search_string &&
831             searched_header && strcasestr(searched_header, quicksearch->search_string) != NULL)
832                 result = TRUE;
833         else if ((quicksearch->matcher_list != NULL) &&
834                  matcherlist_match(quicksearch->matcher_list, msginfo))
835                 result = TRUE;
836
837         quicksearch->matching = FALSE;
838         if (quicksearch->deferred_free) {
839                 prepare_matcher(quicksearch);
840         }
841
842         return result;
843 }
844
845 /* allow Mutt-like patterns in quick search */
846 static gchar *expand_search_string(const gchar *search_string)
847 {
848         int i = 0;
849         gchar term_char, save_char;
850         gchar *cmd_start, *cmd_end;
851         GString *matcherstr;
852         gchar *returnstr = NULL;
853         gchar *copy_str;
854         gboolean casesens, dontmatch;
855         /* list of allowed pattern abbreviations */
856         struct {
857                 gchar           *abbreviated;   /* abbreviation */
858                 gchar           *command;       /* actual matcher command */
859                 gint            numparams;      /* number of params for cmd */
860                 gboolean        qualifier;      /* do we append regexpcase */
861                 gboolean        quotes;         /* do we need quotes */
862         }
863         cmds[] = {
864                 { "a",  "all",                          0,      FALSE,  FALSE },
865                 { "ag", "age_greater",                  1,      FALSE,  FALSE },
866                 { "al", "age_lower",                    1,      FALSE,  FALSE },
867                 { "b",  "body_part",                    1,      TRUE,   TRUE  },
868                 { "B",  "message",                      1,      TRUE,   TRUE  },
869                 { "c",  "cc",                           1,      TRUE,   TRUE  },
870                 { "C",  "to_or_cc",                     1,      TRUE,   TRUE  },
871                 { "D",  "deleted",                      0,      FALSE,  FALSE },
872                 { "e",  "header \"Sender\"",            1,      TRUE,   TRUE  },
873                 { "E",  "execute",                      1,      FALSE,  TRUE  },
874                 { "f",  "from",                         1,      TRUE,   TRUE  },
875                 { "F",  "forwarded",                    0,      FALSE,  FALSE },
876                 { "h",  "headers_part",                 1,      TRUE,   TRUE  },
877                 { "i",  "header \"Message-ID\"",        1,      TRUE,   TRUE  },
878                 { "I",  "inreplyto",                    1,      TRUE,   TRUE  },
879                 { "k",  "colorlabel",                   1,      FALSE,  FALSE },
880                 { "L",  "locked",                       0,      FALSE,  FALSE },
881                 { "n",  "newsgroups",                   1,      TRUE,   TRUE  },
882                 { "N",  "new",                          0,      FALSE,  FALSE },
883                 { "O",  "~new",                         0,      FALSE,  FALSE },
884                 { "r",  "replied",                      0,      FALSE,  FALSE },
885                 { "R",  "~unread",                      0,      FALSE,  FALSE },
886                 { "s",  "subject",                      1,      TRUE,   TRUE  },
887                 { "se", "score_equal",                  1,      FALSE,  FALSE },
888                 { "sg", "score_greater",                1,      FALSE,  FALSE },
889                 { "sl", "score_lower",                  1,      FALSE,  FALSE },
890                 { "Se", "size_equal",                   1,      FALSE,  FALSE },
891                 { "Sg", "size_greater",                 1,      FALSE,  FALSE },
892                 { "Ss", "size_smaller",                 1,      FALSE,  FALSE },
893                 { "t",  "to",                           1,      TRUE,   TRUE  },
894                 { "T",  "marked",                       0,      FALSE,  FALSE },
895                 { "U",  "unread",                       0,      FALSE,  FALSE },
896                 { "x",  "header \"References\"",        1,      TRUE,   TRUE  },
897                 { "X",  "test",                         1,      FALSE,  FALSE },
898                 { "y",  "header \"X-Label\"",           1,      TRUE,   TRUE  },
899                 { "&",  "&",                            0,      FALSE,  FALSE },
900                 { "|",  "|",                            0,      FALSE,  FALSE },
901                 { "p",  "partial",                      0,      FALSE,  FALSE },
902                 { NULL, NULL,                           0,      FALSE,  FALSE }
903         };
904
905         if (search_string == NULL)
906                 return NULL;
907
908         copy_str = g_strdup(search_string);
909
910         matcherstr = g_string_sized_new(16);
911         cmd_start = copy_str;
912         while (cmd_start && *cmd_start) {
913                 /* skip all white spaces */
914                 while (*cmd_start && isspace((guchar)*cmd_start))
915                         cmd_start++;
916                 cmd_end = cmd_start;
917
918                 /* extract a command */
919                 while (*cmd_end && !isspace((guchar)*cmd_end))
920                         cmd_end++;
921
922                 /* save character */
923                 save_char = *cmd_end;
924                 *cmd_end = '\0';
925
926                 dontmatch = FALSE;
927                 casesens = FALSE;
928
929                 /* ~ and ! mean logical NOT */
930                 if (*cmd_start == '~' || *cmd_start == '!')
931                 {
932                         dontmatch = TRUE;
933                         cmd_start++;
934                 }
935                 /* % means case sensitive match */
936                 if (*cmd_start == '%')
937                 {
938                         casesens = TRUE;
939                         cmd_start++;
940                 }
941
942                 /* find matching abbreviation */
943                 for (i = 0; cmds[i].command; i++) {
944                         if (!strcmp(cmd_start, cmds[i].abbreviated)) {
945                                 /* restore character */
946                                 *cmd_end = save_char;
947
948                                 /* copy command */
949                                 if (matcherstr->len > 0) {
950                                         g_string_append(matcherstr, " ");
951                                 }
952                                 if (dontmatch)
953                                         g_string_append(matcherstr, "~");
954                                 g_string_append(matcherstr, cmds[i].command);
955                                 g_string_append(matcherstr, " ");
956
957                                 /* stop if no params required */
958                                 if (cmds[i].numparams == 0)
959                                         break;
960
961                                 /* extract a parameter, allow quotes */
962                                 while (*cmd_end && isspace((guchar)*cmd_end))
963                                         cmd_end++;
964
965                                 cmd_start = cmd_end;
966                                 if (*cmd_start == '"') {
967                                         term_char = '"';
968                                         cmd_end++;
969                                 }
970                                 else
971                                         term_char = ' ';
972
973                                 /* extract actual parameter */
974                                 while ((*cmd_end) && (*cmd_end != term_char))
975                                         cmd_end++;
976
977                                 if (*cmd_end == '"')
978                                         cmd_end++;
979
980                                 save_char = *cmd_end;
981                                 *cmd_end = '\0';
982
983                                 if (cmds[i].qualifier) {
984                                         if (casesens)
985                                                 g_string_append(matcherstr, "regexp ");
986                                         else
987                                                 g_string_append(matcherstr, "regexpcase ");
988                                 }
989
990                                 /* do we need to add quotes ? */
991                                 if (cmds[i].quotes && term_char != '"')
992                                         g_string_append(matcherstr, "\"");
993
994                                 /* copy actual parameter */
995                                 g_string_append(matcherstr, cmd_start);
996
997                                 /* do we need to add quotes ? */
998                                 if (cmds[i].quotes && term_char != '"')
999                                         g_string_append(matcherstr, "\"");
1000
1001                                 /* restore original character */
1002                                 *cmd_end = save_char;
1003
1004                                 break;
1005                         }
1006                 }
1007
1008                 if (*cmd_end)
1009                         cmd_end++;
1010                 cmd_start = cmd_end;
1011         }
1012
1013         g_free(copy_str);
1014
1015         /* return search string if no match is found to allow
1016            all available filtering expressions in quicksearch */
1017         if (matcherstr->len > 0) returnstr = matcherstr->str;
1018         else returnstr = g_strdup(search_string);
1019
1020         g_string_free(matcherstr, FALSE);
1021         return returnstr;
1022 }
1023
1024 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run)
1025 {
1026         quicksearch->running = run;
1027 }
1028
1029 gboolean quicksearch_is_running(QuickSearch *quicksearch)
1030 {
1031         return quicksearch->running;
1032 }
1033
1034 void quicksearch_pass_key(QuickSearch *quicksearch, guint val, GdkModifierType mod)
1035 {
1036         GtkEntry *entry = GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry);
1037         glong curpos = gtk_editable_get_position(GTK_EDITABLE(entry));
1038         guint32 c;
1039         char *str = g_strdup(gtk_entry_get_text(entry));
1040         char *begin = str;
1041         char *end = NULL;
1042         char *new = NULL;
1043         char key[7] = "";
1044         guint char_len = 0;
1045
1046         if (gtk_editable_get_selection_bounds(GTK_EDITABLE(entry), NULL, NULL)) {
1047                 /* remove selection */
1048                 gtk_editable_delete_selection(GTK_EDITABLE(entry));
1049                 curpos = gtk_editable_get_position(GTK_EDITABLE(entry));
1050                 /* refresh string */
1051                 g_free(str);
1052                 str = g_strdup(gtk_entry_get_text(entry));
1053                 begin = str;
1054         }
1055
1056         if (!(c = gdk_keyval_to_unicode(val))) {
1057                 g_free(str);
1058                 return;
1059         }
1060         char_len = g_unichar_to_utf8(c, key);
1061         if (char_len < 0)
1062                 return;
1063         key[char_len] = '\0';
1064         if (curpos < g_utf8_strlen(str, -1)) {
1065                 gchar *stop = g_utf8_offset_to_pointer(begin, curpos);
1066                 end = g_strdup(g_utf8_offset_to_pointer(str, curpos));
1067                 *stop = '\0';
1068                 new = g_strdup_printf("%s%s%s", begin, key, end);
1069                 gtk_entry_set_text(entry, new);
1070                 g_free(end);
1071         } else {
1072                 new = g_strdup_printf("%s%s", begin, key);
1073                 gtk_entry_set_text(entry, new);
1074         }
1075         g_free(str);
1076         g_free(new);
1077         gtk_editable_set_position(GTK_EDITABLE(entry), curpos+1);
1078
1079 }
1080
1081 static gboolean quicksearch_match_subfolder(QuickSearch *quicksearch,
1082                                  FolderItem *src)
1083 {
1084         GSList *msglist = folder_item_get_msg_list(src);
1085         GSList *cur;
1086         gboolean result = FALSE;
1087         gint num = 0, total = src->total_msgs;
1088         gint interval = quicksearch_is_fast(quicksearch) ? 5000:100;
1089
1090         statusbar_print_all(_("Searching in %s... \n"),
1091                 src->path ? src->path : "(null)");
1092         folder_item_update_freeze();
1093         for (cur = msglist; cur != NULL; cur = cur->next) {
1094                 MsgInfo *msg = (MsgInfo *)cur->data;
1095                 statusbar_progress_all(num++,total, interval);
1096                 if (quicksearch_match(quicksearch, msg)) {
1097                         result = TRUE;
1098                         break;
1099                 }
1100                 if (num % interval == 0)
1101                         GTK_EVENTS_FLUSH();
1102                 if (!quicksearch_is_active(quicksearch))
1103                         break;
1104         }
1105         folder_item_update_thaw();
1106         statusbar_progress_all(0,0,0);
1107         statusbar_pop_all();
1108
1109         procmsg_msg_list_free(msglist);
1110         return result;
1111 }
1112
1113 void quicksearch_search_subfolders(QuickSearch *quicksearch,
1114                                    FolderView *folderview,
1115                                    FolderItem *folder_item)
1116 {
1117         FolderItem *cur = NULL;
1118         GNode *node = folder_item->node->children;
1119
1120         if (!prefs_common.summary_quicksearch_recurse
1121         ||  quicksearch->in_typing == TRUE)
1122                 return;
1123
1124         for (; node != NULL; node = node->next) {
1125                 cur = FOLDER_ITEM(node->data);
1126                 if (quicksearch_match_subfolder(quicksearch, cur)) {
1127                         folderview_update_search_icon(cur, TRUE);
1128                 } else {
1129                         folderview_update_search_icon(cur, FALSE);
1130                 }
1131                 if (cur->node->children)
1132                         quicksearch_search_subfolders(quicksearch,
1133                                                       folderview,
1134                                                       cur);
1135         }
1136         quicksearch->root_folder_item = folder_item;
1137         if (!quicksearch_is_active(quicksearch))
1138                 quicksearch_reset_cur_folder_item(quicksearch);
1139 }
1140
1141 static void quicksearch_reset_folder_items(QuickSearch *quicksearch,
1142                                     FolderItem *folder_item)
1143 {
1144         FolderItem *cur = NULL;
1145         GNode *node = (folder_item && folder_item->node) ?
1146                         folder_item->node->children : NULL;
1147
1148         for (; node != NULL; node = node->next) {
1149                 cur = FOLDER_ITEM(node->data);
1150                 folderview_update_search_icon(cur, FALSE);
1151                 if (cur->node->children)
1152                         quicksearch_reset_folder_items(quicksearch,
1153                                                        cur);
1154         }
1155 }
1156
1157 void quicksearch_reset_cur_folder_item(QuickSearch *quicksearch)
1158 {
1159         if (quicksearch->root_folder_item)
1160                 quicksearch_reset_folder_items(quicksearch,
1161                                                quicksearch->root_folder_item);
1162
1163         quicksearch->root_folder_item = NULL;
1164 }
1165
1166 gboolean quicksearch_is_in_typing(QuickSearch *quicksearch)
1167 {
1168         return quicksearch->in_typing;
1169 }