70131fb15e49e012db425814fc22cc4b2c6cd176
[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
362         quicksearch = g_new0(QuickSearch, 1);
363
364         /* quick search */
365         hbox_search = gtk_hbox_new(FALSE, 0);
366
367         search_type_opt = gtk_option_menu_new();
368         gtk_widget_show(search_type_opt);
369         gtk_box_pack_start(GTK_BOX(hbox_search), search_type_opt, FALSE, FALSE, 0);
370
371         search_type = gtk_menu_new();
372         MENUITEM_ADD (search_type, menuitem, _("Subject"), QUICK_SEARCH_SUBJECT);
373         g_signal_connect(G_OBJECT(menuitem), "activate",
374                          G_CALLBACK(searchtype_changed),
375                          quicksearch);
376         MENUITEM_ADD (search_type, menuitem, _("From"), QUICK_SEARCH_FROM);
377         g_signal_connect(G_OBJECT(menuitem), "activate",
378                          G_CALLBACK(searchtype_changed),
379                          quicksearch);
380         MENUITEM_ADD (search_type, menuitem, _("To"), QUICK_SEARCH_TO);
381         g_signal_connect(G_OBJECT(menuitem), "activate",
382                          G_CALLBACK(searchtype_changed),
383                          quicksearch);
384         MENUITEM_ADD (search_type, menuitem, _("Extended"), QUICK_SEARCH_EXTENDED);
385         g_signal_connect(G_OBJECT(menuitem), "activate",
386                          G_CALLBACK(searchtype_changed),
387                          quicksearch);
388
389         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), gtk_separator_menu_item_new());
390         
391         menuitem = gtk_check_menu_item_new_with_label(_("Recursive"));
392         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
393         
394         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
395                                         prefs_common.summary_quicksearch_recurse);
396         
397         g_signal_connect(G_OBJECT(menuitem), "activate",
398                          G_CALLBACK(searchtype_recursive_changed),
399                          quicksearch);
400
401         menuitem = gtk_check_menu_item_new_with_label(_("Sticky"));
402         gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
403         
404         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
405                                         prefs_common.summary_quicksearch_sticky);
406         
407         g_signal_connect(G_OBJECT(menuitem), "activate",
408                          G_CALLBACK(searchtype_sticky_changed),
409                          quicksearch);
410
411         gtk_option_menu_set_menu(GTK_OPTION_MENU(search_type_opt), search_type);
412         
413         gtk_option_menu_set_history(GTK_OPTION_MENU(search_type_opt), prefs_common.summary_quicksearch_type);
414         
415         gtk_widget_show(search_type);
416         
417         search_string_entry = gtk_combo_new();
418         gtk_box_pack_start(GTK_BOX(hbox_search), search_string_entry, FALSE, FALSE, 2);
419         gtk_combo_set_value_in_list(GTK_COMBO(search_string_entry), FALSE, TRUE);
420         gtk_combo_set_case_sensitive(GTK_COMBO(search_string_entry), TRUE);
421         if (prefs_common.summary_quicksearch_history) 
422                 gtk_combo_set_popdown_strings(GTK_COMBO(search_string_entry), 
423                         prefs_common.summary_quicksearch_history);
424         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(search_string_entry)->entry), "");
425         gtk_widget_show(search_string_entry);
426
427         search_hbox = gtk_hbox_new(FALSE, 5);
428
429         search_condition_expression = gtk_button_new_with_label (_(" ... "));
430         gtk_box_pack_start(GTK_BOX(search_hbox), search_condition_expression,
431                            FALSE, FALSE, 0);
432         g_signal_connect(G_OBJECT (search_condition_expression), "clicked",
433                          G_CALLBACK(search_condition_expr),
434                          quicksearch);
435         gtk_widget_show(search_condition_expression);
436
437         clear_search = gtk_button_new_with_label(_(" Clear "));
438         gtk_box_pack_start(GTK_BOX(search_hbox), clear_search,
439                            FALSE, FALSE, 0);
440         g_signal_connect(G_OBJECT(clear_search), "clicked",
441                          G_CALLBACK(clear_search_cb), quicksearch);
442         gtk_widget_show(clear_search);
443
444         search_description = gtk_button_new_with_label(_("Extended Symbols"));
445         gtk_box_pack_start(GTK_BOX(search_hbox), search_description,
446                            FALSE, FALSE, 0);
447         gtk_widget_show(search_description);
448                 
449         g_signal_connect(G_OBJECT(search_description), "clicked",
450                          G_CALLBACK(search_description_cb), NULL);
451
452         gtk_box_pack_start(GTK_BOX(hbox_search), search_hbox, FALSE, FALSE, 2);                         
453         gtk_widget_show(search_hbox);
454         if (prefs_common.summary_quicksearch_type == QUICK_SEARCH_EXTENDED) {
455                 gtk_widget_show(search_condition_expression);
456                 gtk_widget_show(search_description);
457         } else {
458                 gtk_widget_hide(search_condition_expression);
459                 gtk_widget_hide(search_description);
460         }
461         
462         g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry), 
463                            "key_press_event",
464                            G_CALLBACK(searchbar_pressed),
465                            quicksearch);
466
467         
468         g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry),
469                          "focus_in_event",
470                          G_CALLBACK(searchbar_focus_evt),
471                          quicksearch);
472
473         g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry),
474                          "focus_out_event",
475                          G_CALLBACK(searchbar_focus_evt),
476                          quicksearch);
477         
478
479         quicksearch->hbox_search = hbox_search;
480         quicksearch->search_type = search_type;
481         quicksearch->search_type_opt = search_type_opt;
482         quicksearch->search_string_entry = search_string_entry;
483         quicksearch->search_condition_expression = search_condition_expression;
484         quicksearch->search_description = search_description;
485         quicksearch->matcher_list = NULL;
486         quicksearch->active = FALSE;
487         quicksearch->running = FALSE;
488         
489         update_extended_buttons(quicksearch);
490         
491         return quicksearch;
492 }
493
494 GtkWidget *quicksearch_get_widget(QuickSearch *quicksearch)
495 {
496         return quicksearch->hbox_search;
497 }
498
499 void quicksearch_show(QuickSearch *quicksearch)
500 {
501         prepare_matcher(quicksearch);
502         gtk_widget_show(quicksearch->hbox_search);
503         update_extended_buttons(quicksearch);
504         gtk_widget_grab_focus(
505                 GTK_WIDGET(GTK_COMBO(quicksearch->search_string_entry)->entry));
506 }
507
508 void quicksearch_hide(QuickSearch *quicksearch)
509 {
510         quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
511         quicksearch_set_active(quicksearch, FALSE);
512         gtk_widget_hide(quicksearch->hbox_search);
513 }
514
515 void quicksearch_set(QuickSearch *quicksearch, QuickSearchType type,
516                      const gchar *matchstring)
517 {
518         gtk_option_menu_set_history(GTK_OPTION_MENU(quicksearch->search_type_opt),
519                                     type);
520         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry),
521                            matchstring);
522         prefs_common.summary_quicksearch_type = type;
523
524         prepare_matcher(quicksearch);
525
526         quicksearch_set_running(quicksearch, TRUE);
527         if (quicksearch->callback != NULL)
528                 quicksearch->callback(quicksearch, quicksearch->callback_data); 
529         quicksearch_set_running(quicksearch, FALSE);
530 }
531
532 gboolean quicksearch_is_active(QuickSearch *quicksearch)
533 {
534         return quicksearch->active;
535 }
536
537 static void quicksearch_set_active(QuickSearch *quicksearch, gboolean active)
538 {
539         quicksearch->active = active;
540         if (!active) {
541                 quicksearch_reset_cur_folder_item(quicksearch);
542         }
543 }
544
545 void quicksearch_set_execute_callback(QuickSearch *quicksearch,
546                                       QuickSearchExecuteCallback callback,
547                                       gpointer data)
548 {
549         quicksearch->callback = callback;
550         quicksearch->callback_data = data;
551 }
552
553 gboolean quicksearch_match(QuickSearch *quicksearch, MsgInfo *msginfo)
554 {
555         gchar *searched_header = NULL;
556
557         if (!quicksearch->active)
558                 return TRUE;
559
560         switch (prefs_common.summary_quicksearch_type) {
561         case QUICK_SEARCH_SUBJECT:
562                 searched_header = msginfo->subject;
563                 break;
564         case QUICK_SEARCH_FROM:
565                 searched_header = msginfo->from;
566                 break;
567         case QUICK_SEARCH_TO:
568                 searched_header = msginfo->to;
569                 break;
570         case QUICK_SEARCH_EXTENDED:
571                 break;
572         default:
573                 debug_print("unknown search type (%d)\n", prefs_common.summary_quicksearch_type);
574                 break;
575         }
576
577         if (prefs_common.summary_quicksearch_type != QUICK_SEARCH_EXTENDED && 
578             quicksearch->search_string &&
579             searched_header && strcasestr(searched_header, quicksearch->search_string) != NULL)
580                 return TRUE;
581         else if ((quicksearch->matcher_list != NULL) && 
582                  matcherlist_match(quicksearch->matcher_list, msginfo))
583                 return TRUE;
584
585         return FALSE;
586 }
587
588 /* allow Mutt-like patterns in quick search */
589 gchar *expand_search_string(const gchar *search_string)
590 {
591         int i = 0;
592         gchar term_char, save_char;
593         gchar *cmd_start, *cmd_end;
594         GString *matcherstr;
595         gchar *returnstr = NULL;
596         gchar *copy_str;
597         gboolean casesens, dontmatch;
598         /* list of allowed pattern abbreviations */
599         struct {
600                 gchar           *abbreviated;   /* abbreviation */
601                 gchar           *command;       /* actual matcher command */ 
602                 gint            numparams;      /* number of params for cmd */
603                 gboolean        qualifier;      /* do we append regexpcase */
604                 gboolean        quotes;         /* do we need quotes */
605         }
606         cmds[] = {
607                 { "a",  "all",                          0,      FALSE,  FALSE },
608                 { "ag", "age_greater",                  1,      FALSE,  FALSE },
609                 { "al", "age_lower",                    1,      FALSE,  FALSE },
610                 { "b",  "body_part",                    1,      TRUE,   TRUE  },
611                 { "B",  "message",                      1,      TRUE,   TRUE  },
612                 { "c",  "cc",                           1,      TRUE,   TRUE  },
613                 { "C",  "to_or_cc",                     1,      TRUE,   TRUE  },
614                 { "D",  "deleted",                      0,      FALSE,  FALSE },
615                 { "e",  "header \"Sender\"",            1,      TRUE,   TRUE  },
616                 { "E",  "execute",                      1,      FALSE,  TRUE  },
617                 { "f",  "from",                         1,      TRUE,   TRUE  },
618                 { "F",  "forwarded",                    0,      FALSE,  FALSE },
619                 { "h",  "headers_part",                 1,      TRUE,   TRUE  },
620                 { "i",  "header \"Message-ID\"",        1,      TRUE,   TRUE  },
621                 { "I",  "inreplyto",                    1,      TRUE,   TRUE  },
622                 { "L",  "locked",                       0,      FALSE,  FALSE },
623                 { "n",  "newsgroups",                   1,      TRUE,   TRUE  },
624                 { "N",  "new",                          0,      FALSE,  FALSE },
625                 { "O",  "~new",                         0,      FALSE,  FALSE },
626                 { "r",  "replied",                      0,      FALSE,  FALSE },
627                 { "R",  "~unread",                      0,      FALSE,  FALSE },
628                 { "s",  "subject",                      1,      TRUE,   TRUE  },
629                 { "se", "score_equal",                  1,      FALSE,  FALSE },
630                 { "sg", "score_greater",                1,      FALSE,  FALSE },
631                 { "sl", "score_lower",                  1,      FALSE,  FALSE },
632                 { "Se", "size_equal",                   1,      FALSE,  FALSE },
633                 { "Sg", "size_greater",                 1,      FALSE,  FALSE },
634                 { "Ss", "size_smaller",                 1,      FALSE,  FALSE },
635                 { "t",  "to",                           1,      TRUE,   TRUE  },
636                 { "T",  "marked",                       0,      FALSE,  FALSE },
637                 { "U",  "unread",                       0,      FALSE,  FALSE },
638                 { "x",  "header \"References\"",        1,      TRUE,   TRUE  },
639                 { "X",  "test",                         1,      FALSE,  FALSE }, 
640                 { "y",  "header \"X-Label\"",           1,      TRUE,   TRUE  },
641                 { "&",  "&",                            0,      FALSE,  FALSE },
642                 { "|",  "|",                            0,      FALSE,  FALSE },
643                 { "p",  "partial",                      0,      FALSE,  FALSE },
644                 { NULL, NULL,                           0,      FALSE,  FALSE }
645         };
646
647         if (search_string == NULL)
648                 return NULL;
649
650         copy_str = g_strdup(search_string);
651
652         matcherstr = g_string_sized_new(16);
653         cmd_start = copy_str;
654         while (cmd_start && *cmd_start) {
655                 /* skip all white spaces */
656                 while (*cmd_start && isspace((guchar)*cmd_start))
657                         cmd_start++;
658                 cmd_end = cmd_start;
659
660                 /* extract a command */
661                 while (*cmd_end && !isspace((guchar)*cmd_end))
662                         cmd_end++;
663
664                 /* save character */
665                 save_char = *cmd_end;
666                 *cmd_end = '\0';
667
668                 dontmatch = FALSE;
669                 casesens = FALSE;
670
671                 /* ~ and ! mean logical NOT */
672                 if (*cmd_start == '~' || *cmd_start == '!')
673                 {
674                         dontmatch = TRUE;
675                         cmd_start++;
676                 }
677                 /* % means case sensitive match */
678                 if (*cmd_start == '%')
679                 {
680                         casesens = TRUE;
681                         cmd_start++;
682                 }
683
684                 /* find matching abbreviation */
685                 for (i = 0; cmds[i].command; i++) {
686                         if (!strcmp(cmd_start, cmds[i].abbreviated)) {
687                                 /* restore character */
688                                 *cmd_end = save_char;
689
690                                 /* copy command */
691                                 if (matcherstr->len > 0) {
692                                         g_string_append(matcherstr, " ");
693                                 }
694                                 if (dontmatch)
695                                         g_string_append(matcherstr, "~");
696                                 g_string_append(matcherstr, cmds[i].command);
697                                 g_string_append(matcherstr, " ");
698
699                                 /* stop if no params required */
700                                 if (cmds[i].numparams == 0)
701                                         break;
702
703                                 /* extract a parameter, allow quotes */
704                                 while (*cmd_end && isspace((guchar)*cmd_end))
705                                         cmd_end++;
706
707                                 cmd_start = cmd_end;
708                                 if (*cmd_start == '"') {
709                                         term_char = '"';
710                                         cmd_end++;
711                                 }
712                                 else
713                                         term_char = ' ';
714
715                                 /* extract actual parameter */
716                                 while ((*cmd_end) && (*cmd_end != term_char))
717                                         cmd_end++;
718
719                                 if (*cmd_end == '"')
720                                         cmd_end++;
721
722                                 save_char = *cmd_end;
723                                 *cmd_end = '\0';
724
725                                 if (cmds[i].qualifier) {
726                                         if (casesens)
727                                                 g_string_append(matcherstr, "regexp ");
728                                         else
729                                                 g_string_append(matcherstr, "regexpcase ");
730                                 }
731
732                                 /* do we need to add quotes ? */
733                                 if (cmds[i].quotes && term_char != '"')
734                                         g_string_append(matcherstr, "\"");
735
736                                 /* copy actual parameter */
737                                 g_string_append(matcherstr, cmd_start);
738
739                                 /* do we need to add quotes ? */
740                                 if (cmds[i].quotes && term_char != '"')
741                                         g_string_append(matcherstr, "\"");
742
743                                 /* restore original character */
744                                 *cmd_end = save_char;
745
746                                 break;
747                         }
748                 }
749
750                 if (*cmd_end)
751                         cmd_end++;
752                 cmd_start = cmd_end;
753         }
754
755         g_free(copy_str);
756
757         /* return search string if no match is found to allow 
758            all available filtering expressions in quicksearch */
759         if (matcherstr->len > 0) returnstr = matcherstr->str;
760         else returnstr = g_strdup(search_string);
761
762         g_string_free(matcherstr, FALSE);
763         return returnstr;
764 }
765
766 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run)
767 {
768         quicksearch->running = run;
769 }
770
771 gboolean quicksearch_is_running(QuickSearch *quicksearch) 
772 {
773         return quicksearch->running;
774 }
775
776 void quicksearch_pass_key(QuickSearch *quicksearch, guint val, GdkModifierType mod)
777 {
778         GtkEntry *entry = GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry);
779         gint curpos = gtk_editable_get_position(GTK_EDITABLE(entry));
780         char *str = g_strdup(gtk_entry_get_text(entry));
781         char *begin = str;
782         char *end = NULL;
783         char *new = NULL;
784         
785         if (mod == GDK_SHIFT_MASK)
786                 val = toupper(val);
787         
788         if (curpos < strlen(str)-1) {
789                 end = g_strdup(str+curpos);
790                 *(str+curpos) = '\0';
791                 new = g_strdup_printf("%s%c%s", begin, val, end);
792                 gtk_entry_set_text(entry, new);
793                 g_free(end);
794         } else {
795                 new = g_strdup_printf("%s%c", begin, val);
796                 gtk_entry_set_text(entry, new);
797         }
798         g_free(str);
799         g_free(new);
800         gtk_editable_set_position(GTK_EDITABLE(entry), curpos+1);
801         
802 }
803
804 static gboolean quicksearch_match_subfolder(QuickSearch *quicksearch, 
805                                  FolderItem *src)
806 {
807         GSList *msglist = folder_item_get_msg_list(src);
808         GSList *cur;
809         gboolean result = FALSE;
810         
811         for (cur = msglist; cur != NULL; cur = cur->next) {
812                 MsgInfo *msg = (MsgInfo *)cur->data;
813                 if (quicksearch_match(quicksearch, msg)) {
814                         procmsg_msginfo_free(msg);
815                         result = TRUE;
816                         break;
817                 }
818                 procmsg_msginfo_free(msg);
819         }
820
821         g_slist_free(msglist);
822         return result;
823 }
824
825 void quicksearch_search_subfolders(QuickSearch *quicksearch, 
826                                    FolderView *folderview,
827                                    FolderItem *folder_item)
828 {
829         FolderItem *cur = NULL;
830         GNode *node = folder_item->node->children;
831         
832         if (!prefs_common.summary_quicksearch_recurse)
833                 return;
834
835         for (; node != NULL; node = node->next) {
836                 cur = FOLDER_ITEM(node->data);
837                 if (quicksearch_match_subfolder(quicksearch, cur)) {
838                         folderview_update_search_icon(cur, TRUE);
839                 } else {
840                         folderview_update_search_icon(cur, FALSE);
841                 }
842                 if (cur->node->children)
843                         quicksearch_search_subfolders(quicksearch,
844                                                       folderview,
845                                                       cur);
846         }
847         quicksearch->root_folder_item = folder_item;
848 }
849
850 static void quicksearch_reset_folder_items(QuickSearch *quicksearch,
851                                     FolderItem *folder_item)
852 {
853         FolderItem *cur = NULL;
854         GNode *node = (folder_item && folder_item->node) ? 
855                         folder_item->node->children : NULL;
856         
857         for (; node != NULL; node = node->next) {
858                 cur = FOLDER_ITEM(node->data);
859                 folderview_update_search_icon(cur, FALSE);
860                 if (cur->node->children)
861                         quicksearch_reset_folder_items(quicksearch,
862                                                        cur);
863         }
864 }
865
866 void quicksearch_reset_cur_folder_item(QuickSearch *quicksearch)
867 {
868         if (quicksearch->root_folder_item)
869                 quicksearch_reset_folder_items(quicksearch, 
870                                                quicksearch->root_folder_item);
871         
872         quicksearch->root_folder_item = NULL;
873 }