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