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