0803f7ce1c3a8add2d21b7e9ab1e7f18bfb5ae6f
[claws.git] / src / summary_search.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and 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 "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtkwidget.h>
30 #include <gtk/gtkwindow.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtktable.h>
33 #include <gtk/gtkoptionmenu.h>
34 #include <gtk/gtklabel.h>
35 #include <gtk/gtkentry.h>
36 #include <gtk/gtkhbox.h>
37 #include <gtk/gtkcheckbutton.h>
38 #include <gtk/gtkhbbox.h>
39 #include <gtk/gtkbutton.h>
40 #include <gtk/gtkctree.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include "main.h"
46 #include "summary_search.h"
47 #include "summaryview.h"
48 #include "messageview.h"
49 #include "mainwindow.h"
50 #include "menu.h"
51 #include "utils.h"
52 #include "gtkutils.h"
53 #include "prefs_gtk.h"
54 #include "manage_window.h"
55 #include "alertpanel.h"
56 #include "matcher.h"
57 #include "matcher_parser.h"
58 #include "prefs_matcher.h"
59
60 static struct SummarySearchWindow {
61         GtkWidget *window;
62
63         GtkWidget *bool_optmenu;
64
65         GtkWidget *from_entry;
66         GtkWidget *to_entry;
67         GtkWidget *subject_entry;
68         GtkWidget *body_entry;
69
70         GtkWidget *adv_condition_entry;
71         GtkWidget *adv_condition_btn;
72         GtkWidget *adv_search_checkbtn;
73
74         GtkWidget *case_checkbtn;
75
76         GtkWidget *clear_btn;
77         GtkWidget *all_btn;
78         GtkWidget *prev_btn;
79         GtkWidget *next_btn;
80         GtkWidget *close_btn;
81         GtkWidget *stop_btn;
82
83         SummaryView *summaryview;
84
85         MatcherList                     *matcher_list;
86
87         gboolean is_searching;
88 } search_window;
89
90 static void summary_search_create       (void);
91
92 static void summary_search_execute      (gboolean        backward,
93                                          gboolean        search_all);
94
95 static void summary_search_clear        (GtkButton      *button,
96                                          gpointer        data);
97 static void summary_search_prev_clicked (GtkButton      *button,
98                                          gpointer        data);
99 static void summary_search_next_clicked (GtkButton      *button,
100                                          gpointer        data);
101 static void summary_search_all_clicked  (GtkButton      *button,
102                                          gpointer        data);
103 static void summary_search_stop_clicked (GtkButton      *button,
104                                          gpointer        data);
105 static void adv_condition_btn_clicked   (GtkButton      *button,
106                                          gpointer        data);
107
108 static void from_activated              (void);
109 static void to_activated                (void);
110 static void subject_activated           (void);
111 static void body_activated              (void);
112 static void adv_condition_activated     (void);
113
114 static gboolean key_pressed             (GtkWidget      *widget,
115                                          GdkEventKey    *event,
116                                          gpointer        data);
117
118
119 void summary_search(SummaryView *summaryview)
120 {
121         if (!search_window.window) {
122                 summary_search_create();
123         } else {
124                 gtk_widget_hide(search_window.window);
125         }
126
127         search_window.summaryview = summaryview;
128
129         gtk_widget_grab_focus(search_window.next_btn);
130         gtk_widget_grab_focus(search_window.subject_entry);
131         gtk_widget_show(search_window.window);
132 }
133
134 static void summary_search_create(void)
135 {
136         GtkWidget *window;
137         GtkWidget *vbox1;
138         GtkWidget *bool_hbox;
139         GtkWidget *bool_optmenu;
140         GtkWidget *bool_menu;
141         GtkWidget *menuitem;
142         GtkWidget *clear_btn;
143
144         GtkWidget *table1;
145         GtkWidget *from_label;
146         GtkWidget *from_entry;
147         GtkWidget *to_label;
148         GtkWidget *to_entry;
149         GtkWidget *subject_label;
150         GtkWidget *subject_entry;
151         GtkWidget *body_label;
152         GtkWidget *body_entry;
153         GtkWidget *adv_condition_label;
154         GtkWidget *adv_condition_entry;
155         GtkWidget *adv_condition_btn;
156
157         GtkWidget *checkbtn_hbox;
158         GtkWidget *adv_search_checkbtn;
159         GtkWidget *case_checkbtn;
160
161         GtkWidget *confirm_area;
162         GtkWidget *all_btn;
163         GtkWidget *prev_btn;
164         GtkWidget *next_btn;
165         GtkWidget *close_btn;
166         GtkWidget *stop_btn;
167         GtkTooltips *tooltip;
168
169         gboolean is_searching = FALSE;
170
171         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
172         gtk_window_set_title(GTK_WINDOW (window), _("Search messages"));
173         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
174         gtk_container_set_border_width(GTK_CONTAINER (window), 8);
175         g_signal_connect(G_OBJECT(window), "delete_event",
176                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
177         g_signal_connect(G_OBJECT(window), "key_press_event",
178                          G_CALLBACK(key_pressed), NULL);
179         MANAGE_WINDOW_SIGNALS_CONNECT(window);
180
181         vbox1 = gtk_vbox_new (FALSE, 0);
182         gtk_widget_show (vbox1);
183         gtk_container_add (GTK_CONTAINER (window), vbox1);
184
185         bool_hbox = gtk_hbox_new(FALSE, 4);
186         gtk_widget_show(bool_hbox);
187         gtk_box_pack_start(GTK_BOX(vbox1), bool_hbox, FALSE, FALSE, 0);
188
189         bool_optmenu = gtk_option_menu_new();
190         gtk_widget_show(bool_optmenu);
191         gtk_box_pack_start(GTK_BOX(bool_hbox), bool_optmenu, FALSE, FALSE, 0);
192
193         bool_menu = gtk_menu_new();
194         MENUITEM_ADD(bool_menu, menuitem, _("Match any of the following"), 0);
195         MENUITEM_ADD(bool_menu, menuitem, _("Match all of the following"), 1);
196         gtk_option_menu_set_menu(GTK_OPTION_MENU(bool_optmenu), bool_menu);
197
198         clear_btn = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
199         gtk_widget_show(clear_btn);
200         gtk_box_pack_end(GTK_BOX(bool_hbox), clear_btn, FALSE, FALSE, 0);
201
202         table1 = gtk_table_new (5, 3, FALSE);
203         gtk_widget_show (table1);
204         gtk_box_pack_start (GTK_BOX (vbox1), table1, TRUE, TRUE, 0);
205         gtk_container_set_border_width (GTK_CONTAINER (table1), 4);
206         gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
207         gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
208
209         from_entry = gtk_entry_new ();
210         gtk_widget_show (from_entry);
211         gtk_table_attach (GTK_TABLE (table1), from_entry, 1, 3, 0, 1,
212                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
213         g_signal_connect(G_OBJECT(from_entry), "activate",
214                          G_CALLBACK(from_activated), NULL);
215
216         to_entry = gtk_entry_new ();
217         gtk_widget_show (to_entry);
218         gtk_table_attach (GTK_TABLE (table1), to_entry, 1, 3, 1, 2,
219                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
220         g_signal_connect(G_OBJECT(to_entry), "activate",
221                          G_CALLBACK(to_activated), NULL);
222
223         subject_entry = gtk_entry_new ();
224         gtk_widget_show (subject_entry);
225         gtk_table_attach (GTK_TABLE (table1), subject_entry, 1, 3, 2, 3,
226                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
227         g_signal_connect(G_OBJECT(subject_entry), "activate",
228                          G_CALLBACK(subject_activated), NULL);
229
230         body_entry = gtk_entry_new ();
231         gtk_widget_show (body_entry);
232         gtk_table_attach (GTK_TABLE (table1), body_entry, 1, 3, 3, 4,
233                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
234         g_signal_connect(G_OBJECT(body_entry), "activate",
235                          G_CALLBACK(body_activated), NULL);
236
237         adv_condition_entry = gtk_entry_new ();
238         gtk_widget_show (adv_condition_entry);
239         gtk_table_attach (GTK_TABLE (table1), adv_condition_entry, 1, 2, 4, 5,
240                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
241         g_signal_connect(G_OBJECT(adv_condition_entry), "activate",
242                          G_CALLBACK(adv_condition_activated), NULL);
243
244         adv_condition_btn = gtk_button_new_with_label(" ... ");
245         gtk_widget_show (adv_condition_btn);
246         gtk_table_attach (GTK_TABLE (table1), adv_condition_btn, 2, 3, 4, 5,
247                           GTK_FILL, 0, 0, 0);
248         g_signal_connect(G_OBJECT (adv_condition_btn), "clicked",
249                          G_CALLBACK(adv_condition_btn_clicked), search_window.window);
250         tooltip = gtk_tooltips_new();
251         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltip), adv_condition_btn,
252                              _("Edit search criteria"), NULL);
253
254         from_label = gtk_label_new (_("From:"));
255         gtk_widget_show (from_label);
256         gtk_table_attach (GTK_TABLE (table1), from_label, 0, 1, 0, 1,
257                           GTK_FILL, 0, 0, 0);
258         gtk_label_set_justify (GTK_LABEL (from_label), GTK_JUSTIFY_RIGHT);
259         gtk_misc_set_alignment (GTK_MISC (from_label), 1, 0.5);
260
261         to_label = gtk_label_new (_("To:"));
262         gtk_widget_show (to_label);
263         gtk_table_attach (GTK_TABLE (table1), to_label, 0, 1, 1, 2,
264                           GTK_FILL, 0, 0, 0);
265         gtk_label_set_justify (GTK_LABEL (to_label), GTK_JUSTIFY_RIGHT);
266         gtk_misc_set_alignment (GTK_MISC (to_label), 1, 0.5);
267
268         subject_label = gtk_label_new (_("Subject:"));
269         gtk_widget_show (subject_label);
270         gtk_table_attach (GTK_TABLE (table1), subject_label, 0, 1, 2, 3,
271                           GTK_FILL, 0, 0, 0);
272         gtk_label_set_justify (GTK_LABEL (subject_label), GTK_JUSTIFY_RIGHT);
273         gtk_misc_set_alignment (GTK_MISC (subject_label), 1, 0.5);
274
275         body_label = gtk_label_new (_("Body:"));
276         gtk_widget_show (body_label);
277         gtk_table_attach (GTK_TABLE (table1), body_label, 0, 1, 3, 4,
278                           GTK_FILL, 0, 0, 0);
279         gtk_label_set_justify (GTK_LABEL (body_label), GTK_JUSTIFY_RIGHT);
280         gtk_misc_set_alignment (GTK_MISC (body_label), 1, 0.5);
281
282         adv_condition_label = gtk_label_new (_("Condition:"));
283         gtk_widget_show (adv_condition_label);
284         gtk_table_attach (GTK_TABLE (table1), adv_condition_label, 0, 1, 4, 5,
285                           GTK_FILL, 0, 0, 0);
286         gtk_label_set_justify (GTK_LABEL (adv_condition_label), GTK_JUSTIFY_RIGHT);
287         gtk_misc_set_alignment (GTK_MISC (adv_condition_label), 1, 0.5);
288
289         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
290         gtk_widget_show (checkbtn_hbox);
291         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
292         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
293
294         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
295         gtk_widget_show (case_checkbtn);
296         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
297                             FALSE, FALSE, 0);
298
299         adv_search_checkbtn = gtk_check_button_new_with_label (_("Extended Search"));
300         gtk_widget_show (adv_search_checkbtn);
301         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), adv_search_checkbtn,
302                             FALSE, FALSE, 0);
303
304         confirm_area = gtk_hbutton_box_new();
305         gtk_widget_show (confirm_area);
306         gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
307                                   GTK_BUTTONBOX_END);
308         gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
309
310         all_btn = gtk_button_new_with_mnemonic(_("Find _all"));
311         GTK_WIDGET_SET_FLAGS(all_btn, GTK_CAN_DEFAULT);
312         gtk_box_pack_start(GTK_BOX(confirm_area), all_btn, TRUE, TRUE, 0);
313         gtk_widget_show(all_btn);
314
315         prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
316         GTK_WIDGET_SET_FLAGS(prev_btn, GTK_CAN_DEFAULT);
317         gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
318         gtk_widget_show(prev_btn);
319
320         next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
321         GTK_WIDGET_SET_FLAGS(next_btn, GTK_CAN_DEFAULT);
322         gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
323         gtk_widget_show(next_btn);
324
325         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
326         GTK_WIDGET_SET_FLAGS(close_btn, GTK_CAN_DEFAULT);
327         gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
328         gtk_widget_show(close_btn);
329
330         /* stop button hidden */
331         stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
332         GTK_WIDGET_SET_FLAGS(stop_btn, GTK_CAN_DEFAULT);
333         gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
334
335         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
336         gtk_widget_grab_default(next_btn);
337
338         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, bool_menu)
339         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, from_entry)
340         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, to_entry)
341         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, subject_entry)
342         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, body_entry)
343         SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_label)
344         SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_entry)
345         SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_btn)
346         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, case_checkbtn)
347
348         g_signal_connect(G_OBJECT(clear_btn), "clicked",
349                          G_CALLBACK(summary_search_clear), NULL);
350         g_signal_connect(G_OBJECT(all_btn), "clicked",
351                          G_CALLBACK(summary_search_all_clicked), NULL);
352         g_signal_connect(G_OBJECT(prev_btn), "clicked",
353                          G_CALLBACK(summary_search_prev_clicked), NULL);
354         g_signal_connect(G_OBJECT(next_btn), "clicked",
355                          G_CALLBACK(summary_search_next_clicked), NULL);
356         g_signal_connect_closure
357                 (G_OBJECT(close_btn), "clicked",
358                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
359              window, NULL), FALSE);
360         g_signal_connect(G_OBJECT(stop_btn), "clicked",
361                          G_CALLBACK(summary_search_stop_clicked), NULL);
362
363         search_window.window = window;
364         search_window.bool_optmenu = bool_optmenu;
365         search_window.from_entry = from_entry;
366         search_window.to_entry = to_entry;
367         search_window.subject_entry = subject_entry;
368         search_window.body_entry = body_entry;
369         search_window.adv_condition_entry = adv_condition_entry;
370         search_window.adv_condition_btn = adv_condition_btn;
371         search_window.case_checkbtn = case_checkbtn;
372         search_window.adv_search_checkbtn = adv_search_checkbtn;
373         search_window.clear_btn = clear_btn;
374         search_window.all_btn = all_btn;
375         search_window.prev_btn = prev_btn;
376         search_window.next_btn = next_btn;
377         search_window.close_btn = close_btn;
378         search_window.stop_btn = stop_btn;
379         search_window.matcher_list = NULL;
380         search_window.is_searching = is_searching;
381 }
382
383 static void summary_search_execute(gboolean backward, gboolean search_all)
384 {
385         SummaryView *summaryview = search_window.summaryview;
386         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
387         GtkCTreeNode *node;
388         MsgInfo *msginfo;
389         gboolean adv_search;
390         gboolean bool_and = FALSE;
391         gboolean case_sens = FALSE;
392         gboolean all_searched = FALSE;
393         gboolean matched = FALSE;
394         gboolean body_matched = FALSE;
395         const gchar *from_str = NULL, *to_str = NULL, *subject_str = NULL;
396         const gchar *body_str = NULL, *adv_condition = NULL;
397         StrFindFunc str_find_func = NULL;
398
399         if (summary_is_locked(summaryview)) {
400                 return;
401         }
402         summary_lock(summaryview);
403
404         adv_search = gtk_toggle_button_get_active
405                 (GTK_TOGGLE_BUTTON(search_window.adv_search_checkbtn));
406
407         if (adv_search) {
408                 if (search_window.matcher_list != NULL) {
409                         matcherlist_free(search_window.matcher_list);
410                         search_window.matcher_list = NULL;
411                 }
412                 adv_condition = gtk_entry_get_text(GTK_ENTRY(search_window.adv_condition_entry));
413                 if (adv_condition[0] != '\0') {
414                         search_window.matcher_list = matcher_parser_get_cond((gchar*)adv_condition);
415                         /* TODO: check for condition parsing error and show an error dialog */
416                 } else {
417                         /* TODO: warn if no search condition? (or make buttons enabled only when
418                                 at least one search condition has been set */
419                         summary_unlock(summaryview);
420                         return;
421                 }
422         } else {
423                 bool_and = GPOINTER_TO_INT
424                         (menu_get_option_menu_active_user_data
425                                 (GTK_OPTION_MENU(search_window.bool_optmenu)));
426                 case_sens = gtk_toggle_button_get_active
427                         (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
428
429                 if (case_sens) {
430                         str_find_func = str_find;
431                 } else {
432                         str_find_func = str_case_find;
433                 }
434
435                 from_str    = gtk_entry_get_text(GTK_ENTRY(search_window.from_entry));
436                 to_str      = gtk_entry_get_text(GTK_ENTRY(search_window.to_entry));
437                 subject_str = gtk_entry_get_text(GTK_ENTRY(search_window.subject_entry));
438                 body_str    = gtk_entry_get_text(GTK_ENTRY(search_window.body_entry));
439
440                 if (    (from_str[0] == '\0') &&
441                                 (to_str[0] == '\0') &&
442                                 (subject_str[0] == '\0') &&
443                                 (body_str[0] == '\0')) {
444                         /* TODO: warn if no search criteria? (or make buttons enabled only when
445                                 at least one search criteria has been set */
446                         summary_unlock(summaryview);
447                         return;
448                 }
449         }
450
451         search_window.is_searching = TRUE;
452         main_window_cursor_wait(summaryview->mainwin);
453         gtk_widget_hide(search_window.close_btn);
454         gtk_widget_show(search_window.stop_btn);
455
456         if (search_all) {
457                 gtk_clist_freeze(GTK_CLIST(ctree));
458                 gtk_clist_unselect_all(GTK_CLIST(ctree));
459                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
460                 backward = FALSE;
461         } else if (!summaryview->selected) {
462                 if (backward) {
463                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
464                 } else {
465                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
466                 }
467
468                 if (!node) {
469                         search_window.is_searching = FALSE;
470                         gtk_widget_hide(search_window.stop_btn);
471                         gtk_widget_show(search_window.close_btn);
472                         main_window_cursor_normal(summaryview->mainwin);
473                         summary_unlock(summaryview);
474                         return;
475                 }
476         } else {
477                 if (backward) {
478                         node = gtkut_ctree_node_prev
479                                 (ctree, summaryview->selected);
480                 } else {
481                         node = gtkut_ctree_node_next
482                                 (ctree, summaryview->selected);
483                 }
484         }
485
486         for (; search_window.is_searching;) {
487                 if (!node) {
488                         gchar *str;
489                         AlertValue val;
490
491                         if (search_all) {
492                                 break;
493                         }
494
495                         if (all_searched) {
496                                 alertpanel_full(_("Search failed"),
497                                                 _("Search string not found."),
498                                                 GTK_STOCK_CLOSE, NULL, NULL, FALSE,
499                                                 NULL, ALERT_WARNING, G_ALERTDEFAULT);
500                                 break;
501                         }
502
503                         if (backward)
504                                 str = _("Beginning of list reached; continue from end?");
505                         else
506                                 str = _("End of list reached; continue from beginning?");
507
508                         val = alertpanel(_("Search finished"), str,
509                                          GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
510                         if (G_ALERTALTERNATE == val) {
511                                 if (backward) {
512                                         node = GTK_CTREE_NODE
513                                                 (GTK_CLIST(ctree)->row_list_end);
514                                 } else {
515                                         node = GTK_CTREE_NODE
516                                                 (GTK_CLIST(ctree)->row_list);
517                                 }
518
519                                 all_searched = TRUE;
520
521                                 manage_window_focus_in(search_window.window, NULL, NULL);
522                         } else {
523                                 break;
524                         }
525                 }
526
527                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
528                 body_matched = FALSE;
529
530                 if (adv_search) {
531                         matched = matcherlist_match(search_window.matcher_list, msginfo);
532                 } else {
533                         if (bool_and) {
534                                 matched = TRUE;
535                                 if (*from_str) {
536                                         if (!msginfo->from ||
537                                         !str_find_func(msginfo->from, from_str)) {
538                                                 matched = FALSE;
539                                         }
540                                 }
541                                 if (matched && *to_str) {
542                                         if (!msginfo->to ||
543                                         !str_find_func(msginfo->to, to_str)) {
544                                                 matched = FALSE;
545                                         }
546                                 }
547                                 if (matched && *subject_str) {
548                                         if (!msginfo->subject ||
549                                         !str_find_func(msginfo->subject, subject_str)) {
550                                                 matched = FALSE;
551                                         }
552                                 }
553                                 if (matched && *body_str) {
554                                         if (procmime_find_string(msginfo, body_str,
555                                                                  str_find_func)) {
556                                                 body_matched = TRUE;
557                                         } else {
558                                                 matched = FALSE;
559                                         }
560                                 }
561                                 if (matched && !*from_str && !*to_str &&
562                                 !*subject_str && !*body_str) {
563                                         matched = FALSE;
564                                 }
565                         } else {
566                                 matched = FALSE;
567                                 if (*from_str && msginfo->from) {
568                                         if (str_find_func(msginfo->from, from_str)) {
569                                                 matched = TRUE;
570                                         }
571                                 }
572                                 if (!matched && *to_str && msginfo->to) {
573                                         if (str_find_func(msginfo->to, to_str)) {
574                                                 matched = TRUE;
575                                         }
576                                 }
577                                 if (!matched && *subject_str && msginfo->subject) {
578                                         if (str_find_func(msginfo->subject, subject_str)) {
579                                                 matched = TRUE;
580                                         }
581                                 }
582                                 if (!matched && *body_str) {
583                                         if (procmime_find_string(msginfo, body_str,
584                                                                  str_find_func)) {
585                                                 matched = TRUE;
586                                                 body_matched = TRUE;
587                                         }
588                                 }
589                         }
590                 }
591
592                 if (matched) {
593                         if (search_all) {
594                                 gtk_ctree_select(ctree, node);
595                         } else {
596                                 if (messageview_is_visible
597                                         (summaryview->messageview)) {
598                                         summary_unlock(summaryview);
599                                         summary_select_node
600                                                 (summaryview, node, TRUE, TRUE);
601                                         summary_lock(summaryview);
602                                         if (body_matched) {
603                                                 messageview_search_string
604                                                         (summaryview->messageview,
605                                                          body_str, case_sens);
606                                         }
607                                 } else {
608                                         summary_select_node
609                                                 (summaryview, node, FALSE, TRUE);
610                                 }
611                                 break;
612                         }
613                 }
614
615                 node = backward ? gtkut_ctree_node_prev(ctree, node)
616                                 : gtkut_ctree_node_next(ctree, node);
617
618                 GTK_EVENTS_FLUSH();
619         }
620
621         search_window.is_searching = FALSE;
622         gtk_widget_hide(search_window.stop_btn);
623         gtk_widget_show(search_window.close_btn);
624         main_window_cursor_normal(summaryview->mainwin);
625         if (search_all) {
626                 gtk_clist_thaw(GTK_CLIST(ctree));
627         }
628         summary_unlock(summaryview);
629 }
630
631 static void summary_search_clear(GtkButton *button, gpointer data)
632 {
633         if (gtk_toggle_button_get_active
634                 (GTK_TOGGLE_BUTTON(search_window.adv_search_checkbtn))) {
635                 gtk_editable_delete_text(GTK_EDITABLE(search_window.adv_condition_entry),
636                                          0, -1);
637         } else {
638                 gtk_editable_delete_text(GTK_EDITABLE(search_window.from_entry),
639                                          0, -1);
640                 gtk_editable_delete_text(GTK_EDITABLE(search_window.to_entry),
641                                          0, -1);
642                 gtk_editable_delete_text(GTK_EDITABLE(search_window.subject_entry),
643                                          0, -1);
644                 gtk_editable_delete_text(GTK_EDITABLE(search_window.body_entry),
645                                          0, -1);
646         }
647         /* stop searching */
648         if (search_window.is_searching) {
649                 search_window.is_searching = FALSE;
650         }
651 }
652
653 static void summary_search_prev_clicked(GtkButton *button, gpointer data)
654 {
655         summary_search_execute(TRUE, FALSE);
656 }
657
658 static void summary_search_next_clicked(GtkButton *button, gpointer data)
659 {
660         summary_search_execute(FALSE, FALSE);
661 }
662
663 static void summary_search_all_clicked(GtkButton *button, gpointer data)
664 {
665         summary_search_execute(FALSE, TRUE);
666 }
667
668 static void adv_condition_btn_done(MatcherList * matchers)
669 {
670         gchar *str;
671
672         g_return_if_fail(
673                         mainwindow_get_mainwindow()->summaryview->quicksearch != NULL);
674
675         if (matchers == NULL) {
676                 return;
677         }
678
679         str = matcherlist_to_string(matchers);
680
681         if (str != NULL) {
682                 gtk_entry_set_text(
683                         GTK_ENTRY(search_window.adv_condition_entry), str);
684                 g_free(str);
685         }
686 }
687
688 static void summary_search_stop_clicked(GtkButton *button, gpointer data)
689 {
690         search_window.is_searching = FALSE;
691 }
692
693 static void adv_condition_btn_clicked(GtkButton *button, gpointer data)
694 {
695         const gchar * cond_str;
696         MatcherList * matchers = NULL;
697
698         g_return_if_fail( search_window.window != NULL );
699
700         /* re-use it the current quicksearch value if it's a condition expression,
701            otherwise ignore it silently */
702         cond_str = gtk_entry_get_text(
703                         GTK_ENTRY(search_window.adv_condition_entry));
704         if (*cond_str != '\0') {
705                 matchers = matcher_parser_get_cond((gchar*)cond_str);
706         }
707
708         prefs_matcher_open(matchers, adv_condition_btn_done);
709
710         if (matchers != NULL) {
711                 matcherlist_free(matchers);
712         }
713 };
714
715 static void from_activated(void)
716 {
717         gtk_widget_grab_focus(search_window.to_entry);
718 }
719
720 static void to_activated(void)
721 {
722         gtk_widget_grab_focus(search_window.subject_entry);
723 }
724
725 static void subject_activated(void)
726 {
727         gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
728 }
729
730 static void body_activated(void)
731 {
732         gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
733 }
734
735 static void adv_condition_activated(void)
736 {
737         gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
738 }
739
740 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
741                             gpointer data)
742 {
743         if (event && event->keyval == GDK_Escape) {
744                 /* ESC key will:
745                         - stop a running search
746                         - close the search window if no search is running
747                 */
748                 if (!search_window.is_searching) {
749                         gtk_widget_hide(search_window.window);
750                 } else {
751                         search_window.is_searching = FALSE;
752                 }
753         }
754         return FALSE;
755 }