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