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