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