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