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