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