949a33f06ca3d8433874d47b8b9d7e7f8b9ac3e5
[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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
41 struct _QuickSearch
42 {
43         GtkWidget                       *hbox_search;
44         GtkWidget                       *search_type;
45         GtkWidget                       *search_type_opt;
46         GtkWidget                       *search_string_entry;
47         GtkWidget                       *search_description;
48
49         gboolean                         active;
50         gchar                           *search_string;
51         MatcherList                     *matcher_list;
52
53         QuickSearchExecuteCallback       callback;
54         gpointer                         callback_data;
55         gboolean                         running;
56         gboolean                         has_focus;
57         FolderItem                      *root_folder_item;
58 };
59
60 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run);
61 static void quicksearch_set_active(QuickSearch *quicksearch, gboolean active);
62 static void quicksearch_reset_folder_items(QuickSearch *quicksearch, FolderItem *folder_item);
63
64 static void prepare_matcher(QuickSearch *quicksearch)
65 {
66         const gchar *search_string = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry));
67
68         if (quicksearch->matcher_list != NULL) {
69                 matcherlist_free(quicksearch->matcher_list);
70                 quicksearch->matcher_list = NULL;
71         }
72
73         if (search_string == NULL || search_string[0] == '\0') {
74                 quicksearch_set_active(quicksearch, FALSE);
75                 return;
76         }
77
78         if (prefs_common.summary_quicksearch_type == QUICK_SEARCH_EXTENDED) {
79                 char *newstr = NULL;
80
81                 newstr = expand_search_string(search_string);
82                 if (newstr && newstr[0] != '\0') {
83                         quicksearch->matcher_list = matcher_parser_get_cond(newstr);
84                         g_free(newstr);
85                 } else {
86                         quicksearch->matcher_list = NULL;
87                         quicksearch_set_active(quicksearch, FALSE);
88
89                         return;
90                 }
91         } else {
92                 if (quicksearch->search_string != NULL)
93                         g_free(quicksearch->search_string);
94                 quicksearch->search_string = g_strdup(search_string);
95         }
96
97         quicksearch_set_active(quicksearch, TRUE);
98 }
99
100 static void update_extended_button (QuickSearch *quicksearch)
101 {
102         GtkWidget *btn = quicksearch->search_description;
103         
104         g_return_if_fail(btn != NULL);
105                 
106         if (prefs_common.summary_quicksearch_type == QUICK_SEARCH_EXTENDED) {
107                 gtk_button_set_label(GTK_BUTTON(btn), _("Extended symbols"));
108                 gtk_widget_show(btn);
109         } else {
110                 gtk_widget_hide(btn);
111         }
112         
113 }
114
115 static gboolean searchbar_focus_evt(GtkWidget *widget, GdkEventFocus *event,
116                                   QuickSearch *quicksearch)
117 {
118         quicksearch->has_focus = (event && event->in);
119         return FALSE;
120 }
121
122 gboolean quicksearch_has_focus(QuickSearch *quicksearch)
123 {
124         return quicksearch->has_focus;
125 }
126
127 static gboolean searchbar_pressed(GtkWidget *widget, GdkEventKey *event,
128                                   QuickSearch *quicksearch)
129 {
130         if (event != NULL && event->keyval == GDK_Escape) {
131                 quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
132                 gtk_widget_grab_focus(GTK_WIDGET(GTK_COMBO(quicksearch->search_string_entry)->entry));
133                 return TRUE;
134         }
135
136         if (event != NULL && event->keyval == GDK_Return) {
137                 const gchar *search_string = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry));
138
139                 if (search_string && strlen(search_string) != 0) {
140                         prefs_common.summary_quicksearch_history =
141                                 add_history(prefs_common.summary_quicksearch_history,
142                                             search_string);
143                         gtk_combo_set_popdown_strings(GTK_COMBO(quicksearch->search_string_entry), 
144                                 prefs_common.summary_quicksearch_history);                      
145                 }
146
147                 prepare_matcher(quicksearch);
148
149                 quicksearch_set_running(quicksearch, TRUE);
150                 if (quicksearch->callback != NULL)
151                         quicksearch->callback(quicksearch, quicksearch->callback_data);
152                 quicksearch_set_running(quicksearch, FALSE);
153                 g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
154                 gtk_widget_grab_focus(GTK_WIDGET(GTK_COMBO(
155                         quicksearch->search_string_entry)->entry));
156                 return TRUE;
157         }
158
159         return FALSE;           
160 }
161
162 static gboolean searchtype_changed(GtkMenuItem *widget, gpointer data)
163 {
164         QuickSearch *quicksearch = (QuickSearch *)data;
165
166         prefs_common.summary_quicksearch_type = GPOINTER_TO_INT(g_object_get_data(
167                                    G_OBJECT(GTK_MENU_ITEM(gtk_menu_get_active(
168                                    GTK_MENU(quicksearch->search_type)))), MENU_VAL_ID));
169
170         /* Show extended search description button, only when Extended is selected */
171         update_extended_button(quicksearch);
172
173         prepare_matcher(quicksearch);
174
175         quicksearch_set_running(quicksearch, TRUE);
176         if (quicksearch->callback != NULL)
177                 quicksearch->callback(quicksearch, quicksearch->callback_data);
178         quicksearch_set_running(quicksearch, FALSE);
179         return TRUE;
180 }
181
182 static gboolean searchtype_recursive_changed(GtkMenuItem *widget, gpointer data)
183 {
184         QuickSearch *quicksearch = (QuickSearch *)data;
185         gboolean checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
186         
187         prefs_common.summary_quicksearch_recurse = checked; 
188
189         /* reselect the search type */
190         gtk_option_menu_set_history(GTK_OPTION_MENU(quicksearch->search_type_opt), 
191                                     prefs_common.summary_quicksearch_type);
192
193         prepare_matcher(quicksearch);
194
195         quicksearch_set_running(quicksearch, TRUE);
196         if (quicksearch->callback != NULL)
197                 quicksearch->callback(quicksearch, quicksearch->callback_data);
198         quicksearch_set_running(quicksearch, FALSE);
199         return TRUE;
200 }
201
202 /*
203  * Strings describing how to use Extended Search
204  * 
205  * When adding new lines, remember to put 2 strings for each line
206  */
207 static gchar *search_descr_strings[] = {
208         "a",     N_("all messages"),
209         "ag #",  N_("messages whose age is greater than #"),
210         "al #",  N_("messages whose age is less than #"),
211         "b S",   N_("messages which contain S in the message body"),
212         "B S",   N_("messages which contain S in the whole message"),
213         "c S",   N_("messages carbon-copied to S"),
214         "C S",   N_("message is either to: or cc: to S"),
215         "D",     N_("deleted messages"), /** how I can filter deleted messages **/
216         "e S",   N_("messages which contain S in the Sender field"),
217         "E S",   N_("true if execute \"S\" succeeds"),
218         "f S",   N_("messages originating from user S"),
219         "F",     N_("forwarded messages"),
220         "h S",   N_("messages which contain header S"),
221         "i S",   N_("messages which contain S in Message-ID header"),
222         "I S",   N_("messages which contain S in inreplyto header"),
223         "L",     N_("locked messages"),
224         "n S",   N_("messages which are in newsgroup S"),
225         "N",     N_("new messages"),
226         "O",     N_("old messages"),
227         "p",     N_("incomplete messages (not entirely downloaded)"),
228         "r",     N_("messages which have been replied to"),
229         "R",     N_("read messages"),
230         "s S",   N_("messages which contain S in subject"),
231         "se #",  N_("messages whose score is equal to #"),
232         "sg #",  N_("messages whose score is greater than #"),
233         "sl #",  N_("messages whose score is lower than #"),
234         "Se #",  N_("messages whose size is equal to #"),
235         "Sg #",  N_("messages whose size is greater than #"),
236         "Ss #",  N_("messages whose size is smaller than #"),
237         "t S",   N_("messages which have been sent to S"),
238         "T",     N_("marked messages"),
239         "U",     N_("unread messages"),
240         "x S",   N_("messages which contain S in References header"),
241         "X cmd", N_("messages returning 0 when passed to command"),
242         "y S",   N_("messages which contain S in X-Label header"),
243         "",      "" ,
244         "&",     N_("logical AND operator"),
245         "|",     N_("logical OR operator"),
246         "! or ~",       N_("logical NOT operator"),
247         "%",     N_("case sensitive search"),
248         "",      "" ,
249         " ",     N_("all filtering expressions are allowed"),
250         NULL,    NULL 
251 };
252  
253 static DescriptionWindow search_descr = {
254         NULL, 
255         2,
256         N_("Extended Search symbols"),
257         search_descr_strings
258 };
259         
260 static void search_description_cb(GtkWidget *widget)
261 {
262         description_window_create(&search_descr);
263 };
264
265 static gboolean clear_search_cb(GtkMenuItem *widget, gpointer data)
266 {
267         QuickSearch *quicksearch = (QuickSearch *)data;
268         
269         quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
270         
271         return TRUE;
272 };
273
274 QuickSearch *quicksearch_new()
275 {
276         QuickSearch *quicksearch;
277
278         GtkWidget *hbox_search;
279         GtkWidget *search_type_opt;
280         GtkWidget *search_type;
281         GtkWidget *search_string_entry;
282         GtkWidget *search_hbbox;
283         GtkWidget *search_description;
284         GtkWidget *clear_search;
285         GtkWidget *menuitem;
286
287         quicksearch = g_new0(QuickSearch, 1);
288
289         /* quick search */
290         hbox_search = gtk_hbox_new(FALSE, 0);
291
292         search_type_opt = gtk_option_menu_new();
293         gtk_widget_show(search_type_opt);
294         gtk_box_pack_start(GTK_BOX(hbox_search), search_type_opt, FALSE, FALSE, 0);
295
296         search_type = gtk_menu_new();
297         MENUITEM_ADD (search_type, menuitem, _("Subject"), QUICK_SEARCH_SUBJECT);
298         g_signal_connect(G_OBJECT(menuitem), "activate",
299                          G_CALLBACK(searchtype_changed),
300                          quicksearch);
301         MENUITEM_ADD (search_type, menuitem, _("From"), QUICK_SEARCH_FROM);
302         g_signal_connect(G_OBJECT(menuitem), "activate",
303                          G_CALLBACK(searchtype_changed),
304                          quicksearch);
305         MENUITEM_ADD (search_type, menuitem, _("To"), QUICK_SEARCH_TO);
306         g_signal_connect(G_OBJECT(menuitem), "activate",
307                          G_CALLBACK(searchtype_changed),
308                          quicksearch);
309         MENUITEM_ADD (search_type, menuitem, _("Extended"), QUICK_SEARCH_EXTENDED);
310         g_signal_connect(G_OBJECT(menuitem), "activate",
311                          G_CALLBACK(searchtype_changed),
312                          quicksearch);
313
314         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), gtk_separator_menu_item_new());
315         
316         menuitem = gtk_check_menu_item_new_with_label(_("Recursive"));
317         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
318         
319         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
320                                         prefs_common.summary_quicksearch_recurse);
321         
322         g_signal_connect(G_OBJECT(menuitem), "activate",
323                          G_CALLBACK(searchtype_recursive_changed),
324                          quicksearch);
325
326         gtk_option_menu_set_menu(GTK_OPTION_MENU(search_type_opt), search_type);
327         
328         gtk_option_menu_set_history(GTK_OPTION_MENU(search_type_opt), prefs_common.summary_quicksearch_type);
329         
330         gtk_widget_show(search_type);
331         
332         search_string_entry = gtk_combo_new();
333         gtk_box_pack_start(GTK_BOX(hbox_search), search_string_entry, FALSE, FALSE, 2);
334         gtk_combo_set_value_in_list(GTK_COMBO(search_string_entry), FALSE, TRUE);
335         gtk_combo_set_case_sensitive(GTK_COMBO(search_string_entry), TRUE);
336         if (prefs_common.summary_quicksearch_history) 
337                 gtk_combo_set_popdown_strings(GTK_COMBO(search_string_entry), 
338                         prefs_common.summary_quicksearch_history);
339         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(search_string_entry)->entry), "");
340         gtk_widget_show(search_string_entry);
341
342         search_hbbox = gtk_hbutton_box_new();
343         gtk_button_box_set_layout(GTK_BUTTON_BOX(search_hbbox),         
344                                   GTK_BUTTONBOX_START);
345
346         gtk_box_set_spacing(GTK_BOX(search_hbbox), 5);
347                 
348         if (prefs_common.summary_quicksearch_sticky) {
349                 clear_search = gtk_button_new_with_label(_("Clear"));
350                 gtk_box_pack_start(GTK_BOX(search_hbbox), clear_search,
351                                    FALSE, FALSE, 0);
352                 gtk_widget_set_size_request(clear_search, 120, -1);
353                 g_signal_connect(G_OBJECT(clear_search), "clicked",
354                                  G_CALLBACK(clear_search_cb), quicksearch);
355                 gtk_widget_show(clear_search);
356         }
357
358         search_description = gtk_button_new_with_label(_("Extended Symbols"));
359         gtk_box_pack_start(GTK_BOX(search_hbbox), search_description,
360                            TRUE, TRUE, 0);
361         gtk_widget_show(search_description);
362                 
363         g_signal_connect(G_OBJECT(search_description), "clicked",
364                          G_CALLBACK(search_description_cb), NULL);
365
366         gtk_box_pack_start(GTK_BOX(hbox_search), search_hbbox, FALSE, FALSE, 2);                                
367         gtk_widget_show(search_hbbox);
368         if (prefs_common.summary_quicksearch_type == QUICK_SEARCH_EXTENDED)
369                 gtk_widget_show(search_description);
370         else
371                 gtk_widget_hide(search_description);
372         
373         g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry), 
374                            "key_press_event",
375                            G_CALLBACK(searchbar_pressed),
376                            quicksearch);
377
378         
379         g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry),
380                          "focus_in_event",
381                          G_CALLBACK(searchbar_focus_evt),
382                          quicksearch);
383
384         g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry),
385                          "focus_out_event",
386                          G_CALLBACK(searchbar_focus_evt),
387                          quicksearch);
388         
389
390         quicksearch->hbox_search = hbox_search;
391         quicksearch->search_type = search_type;
392         quicksearch->search_type_opt = search_type_opt;
393         quicksearch->search_string_entry = search_string_entry;
394         quicksearch->search_description = search_description;
395         quicksearch->matcher_list = NULL;
396         quicksearch->active = FALSE;
397         quicksearch->running = FALSE;
398         
399         update_extended_button(quicksearch);
400         
401         return quicksearch;
402 }
403
404 GtkWidget *quicksearch_get_widget(QuickSearch *quicksearch)
405 {
406         return quicksearch->hbox_search;
407 }
408
409 void quicksearch_show(QuickSearch *quicksearch)
410 {
411         prepare_matcher(quicksearch);
412         gtk_widget_show(quicksearch->hbox_search);
413         update_extended_button(quicksearch);
414         gtk_widget_grab_focus(
415                 GTK_WIDGET(GTK_COMBO(quicksearch->search_string_entry)->entry));
416 }
417
418 void quicksearch_hide(QuickSearch *quicksearch)
419 {
420         quicksearch_set_active(quicksearch, FALSE);
421         gtk_widget_hide(quicksearch->hbox_search);
422 }
423
424 void quicksearch_set(QuickSearch *quicksearch, QuickSearchType type,
425                      const gchar *matchstring)
426 {
427         gtk_option_menu_set_history(GTK_OPTION_MENU(quicksearch->search_type_opt),
428                                     type);
429         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry),
430                            matchstring);
431         prefs_common.summary_quicksearch_type = type;
432
433         prepare_matcher(quicksearch);
434
435         quicksearch_set_running(quicksearch, TRUE);
436         if (quicksearch->callback != NULL)
437                 quicksearch->callback(quicksearch, quicksearch->callback_data); 
438         quicksearch_set_running(quicksearch, FALSE);
439 }
440
441 gboolean quicksearch_is_active(QuickSearch *quicksearch)
442 {
443         return quicksearch->active;
444 }
445
446 static void quicksearch_set_active(QuickSearch *quicksearch, gboolean active)
447 {
448         quicksearch->active = active;
449         if (!active) {
450                 quicksearch_reset_cur_folder_item(quicksearch);
451         }
452 }
453
454 void quicksearch_set_execute_callback(QuickSearch *quicksearch,
455                                       QuickSearchExecuteCallback callback,
456                                       gpointer data)
457 {
458         quicksearch->callback = callback;
459         quicksearch->callback_data = data;
460 }
461
462 gboolean quicksearch_match(QuickSearch *quicksearch, MsgInfo *msginfo)
463 {
464         gchar *searched_header = NULL;
465
466         if (!quicksearch->active)
467                 return TRUE;
468
469         switch (prefs_common.summary_quicksearch_type) {
470         case QUICK_SEARCH_SUBJECT:
471                 searched_header = msginfo->subject;
472                 break;
473         case QUICK_SEARCH_FROM:
474                 searched_header = msginfo->from;
475                 break;
476         case QUICK_SEARCH_TO:
477                 searched_header = msginfo->to;
478                 break;
479         case QUICK_SEARCH_EXTENDED:
480                 break;
481         default:
482                 debug_print("unknown search type (%d)\n", prefs_common.summary_quicksearch_type);
483                 break;
484         }
485
486         if (prefs_common.summary_quicksearch_type != QUICK_SEARCH_EXTENDED && 
487             quicksearch->search_string &&
488             searched_header && strcasestr(searched_header, quicksearch->search_string) != NULL)
489                 return TRUE;
490         else if ((quicksearch->matcher_list != NULL) && 
491                  matcherlist_match(quicksearch->matcher_list, msginfo))
492                 return TRUE;
493
494         return FALSE;
495 }
496
497 /* allow Mutt-like patterns in quick search */
498 gchar *expand_search_string(const gchar *search_string)
499 {
500         int i = 0;
501         gchar term_char, save_char;
502         gchar *cmd_start, *cmd_end;
503         GString *matcherstr;
504         gchar *returnstr = NULL;
505         gchar *copy_str;
506         gboolean casesens, dontmatch;
507         /* list of allowed pattern abbreviations */
508         struct {
509                 gchar           *abbreviated;   /* abbreviation */
510                 gchar           *command;       /* actual matcher command */ 
511                 gint            numparams;      /* number of params for cmd */
512                 gboolean        qualifier;      /* do we append regexpcase */
513                 gboolean        quotes;         /* do we need quotes */
514         }
515         cmds[] = {
516                 { "a",  "all",                          0,      FALSE,  FALSE },
517                 { "ag", "age_greater",                  1,      FALSE,  FALSE },
518                 { "al", "age_lower",                    1,      FALSE,  FALSE },
519                 { "b",  "body_part",                    1,      TRUE,   TRUE  },
520                 { "B",  "message",                      1,      TRUE,   TRUE  },
521                 { "c",  "cc",                           1,      TRUE,   TRUE  },
522                 { "C",  "to_or_cc",                     1,      TRUE,   TRUE  },
523                 { "D",  "deleted",                      0,      FALSE,  FALSE },
524                 { "e",  "header \"Sender\"",            1,      TRUE,   TRUE  },
525                 { "E",  "execute",                      1,      FALSE,  TRUE  },
526                 { "f",  "from",                         1,      TRUE,   TRUE  },
527                 { "F",  "forwarded",                    0,      FALSE,  FALSE },
528                 { "h",  "headers_part",                 1,      TRUE,   TRUE  },
529                 { "i",  "header \"Message-ID\"",        1,      TRUE,   TRUE  },
530                 { "I",  "inreplyto",                    1,      TRUE,   TRUE  },
531                 { "L",  "locked",                       0,      FALSE,  FALSE },
532                 { "n",  "newsgroups",                   1,      TRUE,   TRUE  },
533                 { "N",  "new",                          0,      FALSE,  FALSE },
534                 { "O",  "~new",                         0,      FALSE,  FALSE },
535                 { "r",  "replied",                      0,      FALSE,  FALSE },
536                 { "R",  "~unread",                      0,      FALSE,  FALSE },
537                 { "s",  "subject",                      1,      TRUE,   TRUE  },
538                 { "se", "score_equal",                  1,      FALSE,  FALSE },
539                 { "sg", "score_greater",                1,      FALSE,  FALSE },
540                 { "sl", "score_lower",                  1,      FALSE,  FALSE },
541                 { "Se", "size_equal",                   1,      FALSE,  FALSE },
542                 { "Sg", "size_greater",                 1,      FALSE,  FALSE },
543                 { "Ss", "size_smaller",                 1,      FALSE,  FALSE },
544                 { "t",  "to",                           1,      TRUE,   TRUE  },
545                 { "T",  "marked",                       0,      FALSE,  FALSE },
546                 { "U",  "unread",                       0,      FALSE,  FALSE },
547                 { "x",  "header \"References\"",        1,      TRUE,   TRUE  },
548                 { "X",  "test",                         1,      FALSE,  FALSE }, 
549                 { "y",  "header \"X-Label\"",           1,      TRUE,   TRUE  },
550                 { "&",  "&",                            0,      FALSE,  FALSE },
551                 { "|",  "|",                            0,      FALSE,  FALSE },
552                 { "p",  "partial",                      0,      FALSE,  FALSE },
553                 { NULL, NULL,                           0,      FALSE,  FALSE }
554         };
555
556         if (search_string == NULL)
557                 return NULL;
558
559         copy_str = g_strdup(search_string);
560
561         matcherstr = g_string_sized_new(16);
562         cmd_start = copy_str;
563         while (cmd_start && *cmd_start) {
564                 /* skip all white spaces */
565                 while (*cmd_start && isspace((guchar)*cmd_start))
566                         cmd_start++;
567                 cmd_end = cmd_start;
568
569                 /* extract a command */
570                 while (*cmd_end && !isspace((guchar)*cmd_end))
571                         cmd_end++;
572
573                 /* save character */
574                 save_char = *cmd_end;
575                 *cmd_end = '\0';
576
577                 dontmatch = FALSE;
578                 casesens = FALSE;
579
580                 /* ~ and ! mean logical NOT */
581                 if (*cmd_start == '~' || *cmd_start == '!')
582                 {
583                         dontmatch = TRUE;
584                         cmd_start++;
585                 }
586                 /* % means case sensitive match */
587                 if (*cmd_start == '%')
588                 {
589                         casesens = TRUE;
590                         cmd_start++;
591                 }
592
593                 /* find matching abbreviation */
594                 for (i = 0; cmds[i].command; i++) {
595                         if (!strcmp(cmd_start, cmds[i].abbreviated)) {
596                                 /* restore character */
597                                 *cmd_end = save_char;
598
599                                 /* copy command */
600                                 if (matcherstr->len > 0) {
601                                         g_string_append(matcherstr, " ");
602                                 }
603                                 if (dontmatch)
604                                         g_string_append(matcherstr, "~");
605                                 g_string_append(matcherstr, cmds[i].command);
606                                 g_string_append(matcherstr, " ");
607
608                                 /* stop if no params required */
609                                 if (cmds[i].numparams == 0)
610                                         break;
611
612                                 /* extract a parameter, allow quotes */
613                                 while (*cmd_end && isspace((guchar)*cmd_end))
614                                         cmd_end++;
615
616                                 cmd_start = cmd_end;
617                                 if (*cmd_start == '"') {
618                                         term_char = '"';
619                                         cmd_end++;
620                                 }
621                                 else
622                                         term_char = ' ';
623
624                                 /* extract actual parameter */
625                                 while ((*cmd_end) && (*cmd_end != term_char))
626                                         cmd_end++;
627
628                                 if (*cmd_end == '"')
629                                         cmd_end++;
630
631                                 save_char = *cmd_end;
632                                 *cmd_end = '\0';
633
634                                 if (cmds[i].qualifier) {
635                                         if (casesens)
636                                                 g_string_append(matcherstr, "regexp ");
637                                         else
638                                                 g_string_append(matcherstr, "regexpcase ");
639                                 }
640
641                                 /* do we need to add quotes ? */
642                                 if (cmds[i].quotes && term_char != '"')
643                                         g_string_append(matcherstr, "\"");
644
645                                 /* copy actual parameter */
646                                 g_string_append(matcherstr, cmd_start);
647
648                                 /* do we need to add quotes ? */
649                                 if (cmds[i].quotes && term_char != '"')
650                                         g_string_append(matcherstr, "\"");
651
652                                 /* restore original character */
653                                 *cmd_end = save_char;
654
655                                 break;
656                         }
657                 }
658
659                 if (*cmd_end)
660                         cmd_end++;
661                 cmd_start = cmd_end;
662         }
663
664         g_free(copy_str);
665
666         /* return search string if no match is found to allow 
667            all available filtering expressions in quicksearch */
668         if (matcherstr->len > 0) returnstr = matcherstr->str;
669         else returnstr = g_strdup(search_string);
670
671         g_string_free(matcherstr, FALSE);
672         return returnstr;
673 }
674
675 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run)
676 {
677         quicksearch->running = run;
678 }
679
680 gboolean quicksearch_is_running(QuickSearch *quicksearch) 
681 {
682         return quicksearch->running;
683 }
684
685 void quicksearch_pass_key(QuickSearch *quicksearch, guint val, GdkModifierType mod)
686 {
687         GtkEntry *entry = GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry);
688         gint curpos = gtk_editable_get_position(GTK_EDITABLE(entry));
689         char *str = g_strdup(gtk_entry_get_text(entry));
690         char *begin = str;
691         char *end = NULL;
692         char *new = NULL;
693         
694         if (mod == GDK_SHIFT_MASK)
695                 val = toupper(val);
696         
697         if (curpos < strlen(str)-1) {
698                 end = g_strdup(str+curpos);
699                 *(str+curpos) = '\0';
700                 new = g_strdup_printf("%s%c%s", begin, val, end);
701                 gtk_entry_set_text(entry, new);
702                 g_free(end);
703         } else {
704                 new = g_strdup_printf("%s%c", begin, val);
705                 gtk_entry_set_text(entry, new);
706         }
707         g_free(str);
708         g_free(new);
709         gtk_editable_set_position(GTK_EDITABLE(entry), curpos+1);
710         
711 }
712
713 static gboolean quicksearch_match_subfolder(QuickSearch *quicksearch, 
714                                  FolderItem *src)
715 {
716         GSList *msglist = folder_item_get_msg_list(src);
717         GSList *cur;
718         gboolean result = FALSE;
719         
720         for (cur = msglist; cur != NULL; cur = cur->next) {
721                 MsgInfo *msg = (MsgInfo *)cur->data;
722                 if (quicksearch_match(quicksearch, msg)) {
723                         procmsg_msginfo_free(msg);
724                         result = TRUE;
725                         break;
726                 }
727                 procmsg_msginfo_free(msg);
728         }
729
730         g_slist_free(msglist);
731         return result;
732 }
733
734 void quicksearch_search_subfolders(QuickSearch *quicksearch, 
735                                    FolderView *folderview,
736                                    FolderItem *folder_item)
737 {
738         FolderItem *cur = NULL;
739         GNode *node = folder_item->node->children;
740         
741         if (!prefs_common.summary_quicksearch_recurse)
742                 return;
743
744         for (; node != NULL; node = node->next) {
745                 cur = FOLDER_ITEM(node->data);
746                 if (quicksearch_match_subfolder(quicksearch, cur)) {
747                         folderview_update_search_icon(cur, TRUE);
748                 } else {
749                         folderview_update_search_icon(cur, FALSE);
750                 }
751                 if (cur->node->children)
752                         quicksearch_search_subfolders(quicksearch,
753                                                       folderview,
754                                                       cur);
755         }
756         quicksearch->root_folder_item = folder_item;
757 }
758
759 static void quicksearch_reset_folder_items(QuickSearch *quicksearch,
760                                     FolderItem *folder_item)
761 {
762         FolderItem *cur = NULL;
763         GNode *node = folder_item->node->children;
764         
765         for (; node != NULL; node = node->next) {
766                 cur = FOLDER_ITEM(node->data);
767                 folderview_update_search_icon(cur, FALSE);
768                 if (cur->node->children)
769                         quicksearch_reset_folder_items(quicksearch,
770                                                        cur);
771         }
772 }
773
774 void quicksearch_reset_cur_folder_item(QuickSearch *quicksearch)
775 {
776         if (quicksearch->root_folder_item)
777                 quicksearch_reset_folder_items(quicksearch, 
778                                                quicksearch->root_folder_item);
779         
780         quicksearch->root_folder_item = NULL;
781 }