cf2c44379867a1383c3f754eee51472803a94ca8
[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 Claws Mail 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 "combobox.h"
54 #include "prefs_gtk.h"
55 #include "manage_window.h"
56 #include "alertpanel.h"
57 #include "matcher.h"
58 #include "matcher_parser.h"
59 #include "prefs_matcher.h"
60 #include "manual.h"
61 #include "prefs_common.h"
62
63 static struct SummarySearchWindow {
64         GtkWidget *window;
65
66         GtkWidget *bool_optmenu;
67
68         GtkWidget *from_entry;
69         GtkWidget *to_entry;
70         GtkWidget *subject_entry;
71         GtkWidget *body_entry;
72
73         GtkWidget *adv_condition_entry;
74         GtkWidget *adv_condition_btn;
75         GtkWidget *adv_search_checkbtn;
76
77         GtkWidget *case_checkbtn;
78
79         GtkWidget *clear_btn;
80         GtkWidget *help_btn;
81         GtkWidget *all_btn;
82         GtkWidget *prev_btn;
83         GtkWidget *next_btn;
84         GtkWidget *close_btn;
85         GtkWidget *stop_btn;
86
87         SummaryView *summaryview;
88
89         MatcherList                     *matcher_list;
90
91         gboolean is_searching;
92         gboolean from_entry_has_focus;
93         gboolean to_entry_has_focus;
94         gboolean subject_entry_has_focus;
95         gboolean body_entry_has_focus;
96         gboolean adv_condition_entry_has_focus;
97 } search_window;
98
99 static void summary_search_create       (void);
100
101 static void summary_search_execute      (gboolean        backward,
102                                          gboolean        search_all);
103
104 static void summary_search_clear        (GtkButton      *button,
105                                          gpointer        data);
106 static void summary_search_prev_clicked (GtkButton      *button,
107                                          gpointer        data);
108 static void summary_search_next_clicked (GtkButton      *button,
109                                          gpointer        data);
110 static void summary_search_all_clicked  (GtkButton      *button,
111                                          gpointer        data);
112 static void summary_search_stop_clicked (GtkButton      *button,
113                                          gpointer        data);
114 static void adv_condition_btn_clicked   (GtkButton      *button,
115                                          gpointer        data);
116
117 static void from_changed                        (void);
118 static void to_changed                          (void);
119 static void subject_changed                     (void);
120 static void body_changed                        (void);
121 static void adv_condition_changed       (void);
122
123 static gboolean from_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
124                                   gpointer data);
125 static gboolean from_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
126                                   gpointer data);
127 static gboolean to_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
128                                   gpointer data);
129 static gboolean to_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
130                                   gpointer data);
131 static gboolean subject_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
132                                   gpointer data);
133 static gboolean subject_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
134                                   gpointer data);
135 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
136                                   gpointer data);
137 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
138                                   gpointer data);
139 static gboolean adv_condition_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
140                                   gpointer data);
141 static gboolean adv_condition_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
142                                   gpointer data);
143
144 static gboolean key_pressed             (GtkWidget      *widget,
145                                          GdkEventKey    *event,
146                                          gpointer        data);
147
148
149 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) {    \
150         gboolean in_btn = FALSE;                                                        \
151         if (GTK_IS_BUTTON(widget))                                                      \
152                 in_btn = GTK_BUTTON(widget)->in_button;                 \
153         gtk_widget_set_sensitive(widget, sensitive);            \
154         if (GTK_IS_BUTTON(widget))                                                      \
155                 GTK_BUTTON(widget)->in_button = in_btn;                 \
156 }
157
158 void summary_search(SummaryView *summaryview)
159 {
160         if (!search_window.window) {
161                 summary_search_create();
162         } else {
163                 gtk_widget_hide(search_window.window);
164         }
165
166         search_window.summaryview = summaryview;
167
168         gtk_widget_grab_focus(search_window.next_btn);
169         gtk_widget_grab_focus(search_window.subject_entry);
170         gtk_widget_show(search_window.window);
171 }
172
173 static void summary_show_stop_button(void)
174 {
175         gtk_widget_hide(search_window.close_btn);
176         gtk_widget_show(search_window.stop_btn);
177         GTK_BUTTON_SET_SENSITIVE(search_window.all_btn, FALSE)
178         GTK_BUTTON_SET_SENSITIVE(search_window.prev_btn, FALSE)
179         GTK_BUTTON_SET_SENSITIVE(search_window.next_btn, FALSE)
180 }
181
182 static void summary_hide_stop_button(void)
183 {
184         gtk_widget_hide(search_window.stop_btn);
185         gtk_widget_show(search_window.close_btn);
186         gtk_widget_set_sensitive(search_window.all_btn, TRUE);
187         gtk_widget_set_sensitive(search_window.prev_btn, TRUE);
188         gtk_widget_set_sensitive(search_window.next_btn, TRUE);
189 }
190
191 static void summary_search_create(void)
192 {
193         GtkWidget *window;
194         GtkWidget *vbox1;
195         GtkWidget *bool_hbox;
196         GtkWidget *bool_optmenu;
197         GtkWidget *bool_menu;
198         GtkWidget *menuitem;
199         GtkWidget *clear_btn;
200
201         GtkWidget *table1;
202         GtkWidget *from_label;
203         GtkWidget *from_entry;
204         GtkWidget *to_label;
205         GtkWidget *to_entry;
206         GtkWidget *subject_label;
207         GtkWidget *subject_entry;
208         GtkWidget *body_label;
209         GtkWidget *body_entry;
210         GtkWidget *adv_condition_label;
211         GtkWidget *adv_condition_entry;
212         GtkWidget *adv_condition_btn;
213
214         GtkWidget *checkbtn_hbox;
215         GtkWidget *adv_search_checkbtn;
216         GtkWidget *case_checkbtn;
217
218         GtkWidget *confirm_area;
219         GtkWidget *help_btn;
220         GtkWidget *all_btn;
221         GtkWidget *prev_btn;
222         GtkWidget *next_btn;
223         GtkWidget *close_btn;
224         GtkWidget *stop_btn;
225         GtkTooltips *tooltip;
226
227         gboolean is_searching = FALSE;
228
229         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
230         gtk_window_set_title(GTK_WINDOW (window), _("Search messages"));
231         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
232         gtk_container_set_border_width(GTK_CONTAINER (window), 8);
233         g_signal_connect(G_OBJECT(window), "delete_event",
234                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
235         g_signal_connect(G_OBJECT(window), "key_press_event",
236                          G_CALLBACK(key_pressed), NULL);
237         MANAGE_WINDOW_SIGNALS_CONNECT(window);
238
239         vbox1 = gtk_vbox_new (FALSE, 0);
240         gtk_widget_show (vbox1);
241         gtk_container_add (GTK_CONTAINER (window), vbox1);
242
243         bool_hbox = gtk_hbox_new(FALSE, 4);
244         gtk_widget_show(bool_hbox);
245         gtk_box_pack_start(GTK_BOX(vbox1), bool_hbox, FALSE, FALSE, 0);
246
247         bool_optmenu = gtk_option_menu_new();
248         gtk_widget_show(bool_optmenu);
249         gtk_box_pack_start(GTK_BOX(bool_hbox), bool_optmenu, FALSE, FALSE, 0);
250
251         bool_menu = gtk_menu_new();
252         MENUITEM_ADD(bool_menu, menuitem, _("Match any of the following"), 0);
253         MENUITEM_ADD(bool_menu, menuitem, _("Match all of the following"), 1);
254         gtk_option_menu_set_menu(GTK_OPTION_MENU(bool_optmenu), bool_menu);
255
256         clear_btn = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
257         gtk_widget_show(clear_btn);
258         gtk_box_pack_end(GTK_BOX(bool_hbox), clear_btn, FALSE, FALSE, 0);
259
260         table1 = gtk_table_new (5, 3, FALSE);
261         gtk_widget_show (table1);
262         gtk_box_pack_start (GTK_BOX (vbox1), table1, TRUE, TRUE, 0);
263         gtk_container_set_border_width (GTK_CONTAINER (table1), 4);
264         gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
265         gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
266
267         from_entry = gtk_combo_box_entry_new_text ();
268         gtk_combo_box_set_active(GTK_COMBO_BOX(from_entry), -1);
269         if (prefs_common.summary_search_from_history)
270                 combobox_set_popdown_strings(GTK_COMBO_BOX(from_entry),
271                                 prefs_common.summary_search_from_history);
272         gtk_widget_show (from_entry);
273         gtk_table_attach (GTK_TABLE (table1), from_entry, 1, 3, 0, 1,
274                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
275         g_signal_connect(G_OBJECT(from_entry), "changed",
276                          G_CALLBACK(from_changed), NULL);
277         g_signal_connect(G_OBJECT(GTK_BIN(from_entry)->child),
278                          "focus_in_event", G_CALLBACK(from_entry_focus_evt_in), NULL);
279         g_signal_connect(G_OBJECT(GTK_BIN(from_entry)->child),
280                          "focus_out_event", G_CALLBACK(from_entry_focus_evt_out), NULL);
281
282         to_entry = gtk_combo_box_entry_new_text ();
283         gtk_combo_box_set_active(GTK_COMBO_BOX(to_entry), -1);
284         if (prefs_common.summary_search_to_history)
285                 combobox_set_popdown_strings(GTK_COMBO_BOX(to_entry),
286                                 prefs_common.summary_search_to_history);
287         gtk_widget_show (to_entry);
288         gtk_table_attach (GTK_TABLE (table1), to_entry, 1, 3, 1, 2,
289                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
290         g_signal_connect(G_OBJECT(from_entry), "changed",
291                          G_CALLBACK(from_changed), NULL);
292         g_signal_connect(G_OBJECT(GTK_BIN(to_entry)->child),
293                          "focus_in_event", G_CALLBACK(to_entry_focus_evt_in), NULL);
294         g_signal_connect(G_OBJECT(GTK_BIN(to_entry)->child),
295                          "focus_out_event", G_CALLBACK(to_entry_focus_evt_out), NULL);
296
297         subject_entry = gtk_combo_box_entry_new_text ();
298         gtk_combo_box_set_active(GTK_COMBO_BOX(subject_entry), -1);
299         if (prefs_common.summary_search_subject_history)
300                 combobox_set_popdown_strings(GTK_COMBO_BOX(subject_entry),
301                                 prefs_common.summary_search_subject_history);
302         gtk_widget_show (subject_entry);
303         gtk_table_attach (GTK_TABLE (table1), subject_entry, 1, 3, 2, 3,
304                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
305         g_signal_connect(G_OBJECT(from_entry), "changed",
306                          G_CALLBACK(from_changed), NULL);
307         g_signal_connect(G_OBJECT(GTK_BIN(subject_entry)->child),
308                          "focus_in_event", G_CALLBACK(subject_entry_focus_evt_in), NULL);
309         g_signal_connect(G_OBJECT(GTK_BIN(subject_entry)->child),
310                          "focus_out_event", G_CALLBACK(subject_entry_focus_evt_out), NULL);
311
312         body_entry = gtk_combo_box_entry_new_text ();
313         gtk_combo_box_set_active(GTK_COMBO_BOX(body_entry), -1);
314         if (prefs_common.summary_search_body_history)
315                 combobox_set_popdown_strings(GTK_COMBO_BOX(body_entry),
316                                 prefs_common.summary_search_body_history);
317         gtk_widget_show (body_entry);
318         gtk_table_attach (GTK_TABLE (table1), body_entry, 1, 3, 3, 4,
319                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
320         g_signal_connect(G_OBJECT(from_entry), "changed",
321                          G_CALLBACK(from_changed), NULL);
322         g_signal_connect(G_OBJECT(GTK_BIN(body_entry)->child),
323                          "focus_in_event", G_CALLBACK(body_entry_focus_evt_in), NULL);
324         g_signal_connect(G_OBJECT(GTK_BIN(body_entry)->child),
325                          "focus_out_event", G_CALLBACK(body_entry_focus_evt_out), NULL);
326
327         adv_condition_entry = gtk_combo_box_entry_new_text ();
328         gtk_combo_box_set_active(GTK_COMBO_BOX(adv_condition_entry), -1);
329         if (prefs_common.summary_search_adv_condition_history)
330                 combobox_set_popdown_strings(GTK_COMBO_BOX(adv_condition_entry),
331                                 prefs_common.summary_search_adv_condition_history);
332         gtk_widget_show (adv_condition_entry);
333         gtk_table_attach (GTK_TABLE (table1), adv_condition_entry, 1, 2, 4, 5,
334                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
335         g_signal_connect(G_OBJECT(from_entry), "changed",
336                          G_CALLBACK(from_changed), NULL);
337         g_signal_connect(G_OBJECT(GTK_BIN(adv_condition_entry)->child),
338                          "focus_in_event", G_CALLBACK(adv_condition_entry_focus_evt_in), NULL);
339         g_signal_connect(G_OBJECT(GTK_BIN(adv_condition_entry)->child),
340                          "focus_out_event", G_CALLBACK(adv_condition_entry_focus_evt_out), NULL);
341
342         adv_condition_btn = gtk_button_new_with_label(" ... ");
343         gtk_widget_show (adv_condition_btn);
344         gtk_table_attach (GTK_TABLE (table1), adv_condition_btn, 2, 3, 4, 5,
345                           GTK_FILL, 0, 0, 0);
346         g_signal_connect(G_OBJECT (adv_condition_btn), "clicked",
347                          G_CALLBACK(adv_condition_btn_clicked), search_window.window);
348         tooltip = gtk_tooltips_new();
349         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltip), adv_condition_btn,
350                              _("Edit search criteria"), NULL);
351
352         from_label = gtk_label_new (_("From:"));
353         gtk_widget_show (from_label);
354         gtk_table_attach (GTK_TABLE (table1), from_label, 0, 1, 0, 1,
355                           GTK_FILL, 0, 0, 0);
356         gtk_label_set_justify (GTK_LABEL (from_label), GTK_JUSTIFY_RIGHT);
357         gtk_misc_set_alignment (GTK_MISC (from_label), 1, 0.5);
358
359         to_label = gtk_label_new (_("To:"));
360         gtk_widget_show (to_label);
361         gtk_table_attach (GTK_TABLE (table1), to_label, 0, 1, 1, 2,
362                           GTK_FILL, 0, 0, 0);
363         gtk_label_set_justify (GTK_LABEL (to_label), GTK_JUSTIFY_RIGHT);
364         gtk_misc_set_alignment (GTK_MISC (to_label), 1, 0.5);
365
366         subject_label = gtk_label_new (_("Subject:"));
367         gtk_widget_show (subject_label);
368         gtk_table_attach (GTK_TABLE (table1), subject_label, 0, 1, 2, 3,
369                           GTK_FILL, 0, 0, 0);
370         gtk_label_set_justify (GTK_LABEL (subject_label), GTK_JUSTIFY_RIGHT);
371         gtk_misc_set_alignment (GTK_MISC (subject_label), 1, 0.5);
372
373         body_label = gtk_label_new (_("Body:"));
374         gtk_widget_show (body_label);
375         gtk_table_attach (GTK_TABLE (table1), body_label, 0, 1, 3, 4,
376                           GTK_FILL, 0, 0, 0);
377         gtk_label_set_justify (GTK_LABEL (body_label), GTK_JUSTIFY_RIGHT);
378         gtk_misc_set_alignment (GTK_MISC (body_label), 1, 0.5);
379
380         adv_condition_label = gtk_label_new (_("Condition:"));
381         gtk_widget_show (adv_condition_label);
382         gtk_table_attach (GTK_TABLE (table1), adv_condition_label, 0, 1, 4, 5,
383                           GTK_FILL, 0, 0, 0);
384         gtk_label_set_justify (GTK_LABEL (adv_condition_label), GTK_JUSTIFY_RIGHT);
385         gtk_misc_set_alignment (GTK_MISC (adv_condition_label), 1, 0.5);
386
387         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
388         gtk_widget_show (checkbtn_hbox);
389         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
390         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
391
392         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
393         gtk_widget_show (case_checkbtn);
394         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
395                             FALSE, FALSE, 0);
396
397         adv_search_checkbtn = gtk_check_button_new_with_label (_("Extended Search"));
398         gtk_widget_show (adv_search_checkbtn);
399         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), adv_search_checkbtn,
400                             FALSE, FALSE, 0);
401
402         confirm_area = gtk_hbutton_box_new();
403         gtk_widget_show (confirm_area);
404         gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
405                                   GTK_BUTTONBOX_END);
406         gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
407
408         gtkut_stock_button_add_help(confirm_area, &help_btn);
409
410         all_btn = gtk_button_new_with_mnemonic(_("Find _all"));
411         GTK_WIDGET_SET_FLAGS(all_btn, GTK_CAN_DEFAULT);
412         gtk_box_pack_start(GTK_BOX(confirm_area), all_btn, TRUE, TRUE, 0);
413         gtk_widget_show(all_btn);
414
415         prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
416         GTK_WIDGET_SET_FLAGS(prev_btn, GTK_CAN_DEFAULT);
417         gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
418         gtk_widget_show(prev_btn);
419
420         next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
421         GTK_WIDGET_SET_FLAGS(next_btn, GTK_CAN_DEFAULT);
422         gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
423         gtk_widget_show(next_btn);
424
425         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
426         GTK_WIDGET_SET_FLAGS(close_btn, GTK_CAN_DEFAULT);
427         gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
428         gtk_widget_show(close_btn);
429
430         /* stop button hidden */
431         stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
432         GTK_WIDGET_SET_FLAGS(stop_btn, GTK_CAN_DEFAULT);
433         gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
434
435         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
436         gtk_widget_grab_default(next_btn);
437
438         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, bool_menu)
439         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, from_entry)
440         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, to_entry)
441         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, subject_entry)
442         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, body_entry)
443         SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_label)
444         SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_entry)
445         SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_btn)
446         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, case_checkbtn)
447
448         g_signal_connect(G_OBJECT(help_btn), "clicked",
449                          G_CALLBACK(manual_open_with_anchor_cb),
450                          MANUAL_ANCHOR_SEARCHING);
451         g_signal_connect(G_OBJECT(clear_btn), "clicked",
452                          G_CALLBACK(summary_search_clear), NULL);
453         g_signal_connect(G_OBJECT(all_btn), "clicked",
454                          G_CALLBACK(summary_search_all_clicked), NULL);
455         g_signal_connect(G_OBJECT(prev_btn), "clicked",
456                          G_CALLBACK(summary_search_prev_clicked), NULL);
457         g_signal_connect(G_OBJECT(next_btn), "clicked",
458                          G_CALLBACK(summary_search_next_clicked), NULL);
459         g_signal_connect_closure
460                 (G_OBJECT(close_btn), "clicked",
461                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
462              window, NULL), FALSE);
463         g_signal_connect(G_OBJECT(stop_btn), "clicked",
464                          G_CALLBACK(summary_search_stop_clicked), NULL);
465
466         search_window.window = window;
467         search_window.bool_optmenu = bool_optmenu;
468         search_window.from_entry = from_entry;
469         search_window.to_entry = to_entry;
470         search_window.subject_entry = subject_entry;
471         search_window.body_entry = body_entry;
472         search_window.adv_condition_entry = adv_condition_entry;
473         search_window.adv_condition_btn = adv_condition_btn;
474         search_window.case_checkbtn = case_checkbtn;
475         search_window.adv_search_checkbtn = adv_search_checkbtn;
476         search_window.clear_btn = clear_btn;
477         search_window.help_btn = help_btn;
478         search_window.all_btn = all_btn;
479         search_window.prev_btn = prev_btn;
480         search_window.next_btn = next_btn;
481         search_window.close_btn = close_btn;
482         search_window.stop_btn = stop_btn;
483         search_window.matcher_list = NULL;
484         search_window.is_searching = is_searching;
485 }
486
487 static void summary_search_execute(gboolean backward, gboolean search_all)
488 {
489         SummaryView *summaryview = search_window.summaryview;
490         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
491         GtkCTreeNode *node;
492         MsgInfo *msginfo;
493         gboolean adv_search;
494         gboolean bool_and = FALSE;
495         gboolean case_sens = FALSE;
496         gboolean all_searched = FALSE;
497         gboolean matched = FALSE;
498         gboolean body_matched = FALSE;
499         const gchar *from_str = NULL, *to_str = NULL, *subject_str = NULL;
500         const gchar *body_str = NULL, *adv_condition = NULL;
501         StrFindFunc str_find_func = NULL;
502         gboolean is_fast = TRUE;
503         gint interval = 1000;
504         gint i = 0;
505
506         if (summary_is_locked(summaryview)) {
507                 return;
508         }
509         summary_lock(summaryview);
510
511         adv_search = gtk_toggle_button_get_active
512                 (GTK_TOGGLE_BUTTON(search_window.adv_search_checkbtn));
513
514         if (adv_search) {
515                 if (search_window.matcher_list != NULL) {
516                         matcherlist_free(search_window.matcher_list);
517                         search_window.matcher_list = NULL;
518                 }
519                 adv_condition = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.adv_condition_entry));
520                 if (adv_condition[0] != '\0') {
521
522                         /* add to history */
523                         combobox_unset_popdown_strings(GTK_COMBO_BOX(search_window.adv_condition_entry));
524                         prefs_common.summary_search_adv_condition_history = add_history(
525                                         prefs_common.summary_search_adv_condition_history, adv_condition);
526                         combobox_set_popdown_strings(GTK_COMBO_BOX(search_window.adv_condition_entry),
527                                         prefs_common.summary_search_adv_condition_history);
528
529                         search_window.matcher_list = matcher_parser_get_cond((gchar*)adv_condition, &is_fast);
530                         if (!is_fast)
531                                 interval = 100;
532                         /* TODO: check for condition parsing error and show an error dialog */
533                 } else {
534                         /* TODO: warn if no search condition? (or make buttons enabled only when
535                                 at least one search condition has been set */
536                         summary_unlock(summaryview);
537                         return;
538                 }
539         } else {
540                 bool_and = GPOINTER_TO_INT
541                         (menu_get_option_menu_active_user_data
542                                 (GTK_OPTION_MENU(search_window.bool_optmenu)));
543                 case_sens = gtk_toggle_button_get_active
544                         (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
545
546                 if (case_sens) {
547                         str_find_func = str_find;
548                 } else {
549                         str_find_func = str_case_find;
550                 }
551
552                 from_str    = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.from_entry));
553                 to_str      = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.to_entry));
554                 subject_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.subject_entry));
555                 body_str    = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.body_entry));
556
557                 if (    (from_str[0] == '\0') &&
558                                 (to_str[0] == '\0') &&
559                                 (subject_str[0] == '\0') &&
560                                 (body_str[0] == '\0')) {
561                         /* TODO: warn if no search criteria? (or make buttons enabled only when
562                                 at least one search criteria has been set */
563                         summary_unlock(summaryview);
564                         return;
565                 }
566
567                 /* add to history */
568                 if (from_str[0] != '\0')
569                         combobox_unset_popdown_strings(GTK_COMBO_BOX(search_window.from_entry));
570                         prefs_common.summary_search_from_history = add_history(
571                                         prefs_common.summary_search_from_history, from_str);
572                         combobox_set_popdown_strings(GTK_COMBO_BOX(search_window.from_entry),
573                                         prefs_common.summary_search_from_history);
574                 if (to_str[0] != '\0')
575                         combobox_unset_popdown_strings(GTK_COMBO_BOX(search_window.to_entry));
576                         prefs_common.summary_search_to_history = add_history(
577                                         prefs_common.summary_search_to_history, to_str);
578                         combobox_set_popdown_strings(GTK_COMBO_BOX(search_window.to_entry),
579                                         prefs_common.summary_search_to_history);
580                 if (subject_str[0] != '\0')
581                         combobox_unset_popdown_strings(GTK_COMBO_BOX(search_window.subject_entry));
582                         prefs_common.summary_search_subject_history = add_history(
583                                         prefs_common.summary_search_subject_history, subject_str);
584                         combobox_set_popdown_strings(GTK_COMBO_BOX(search_window.subject_entry),
585                                         prefs_common.summary_search_subject_history);
586                 if (body_str[0] != '\0')
587                         combobox_unset_popdown_strings(GTK_COMBO_BOX(search_window.body_entry));
588                         prefs_common.summary_search_body_history = add_history(
589                                         prefs_common.summary_search_body_history, body_str);
590                         combobox_set_popdown_strings(GTK_COMBO_BOX(search_window.body_entry),
591                                         prefs_common.summary_search_body_history);
592         }
593
594         search_window.is_searching = TRUE;
595         main_window_cursor_wait(summaryview->mainwin);
596         summary_show_stop_button();
597
598         if (search_all) {
599                 summary_freeze(summaryview);
600                 summary_unselect_all(summaryview);
601                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
602                 backward = FALSE;
603         } else if (!summaryview->selected) {
604                 if (backward) {
605                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
606                 } else {
607                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
608                 }
609
610                 if (!node) {
611                         search_window.is_searching = FALSE;
612                         summary_hide_stop_button();
613                         main_window_cursor_normal(summaryview->mainwin);
614                         summary_unlock(summaryview);
615                         return;
616                 }
617         } else {
618                 if (backward) {
619                         node = gtkut_ctree_node_prev
620                                 (ctree, summaryview->selected);
621                 } else {
622                         node = gtkut_ctree_node_next
623                                 (ctree, summaryview->selected);
624                 }
625         }
626
627         for (; search_window.is_searching; i++) {
628                 if (!node) {
629                         gchar *str;
630                         AlertValue val;
631
632                         if (search_all) {
633                                 break;
634                         }
635
636                         if (all_searched) {
637                                 alertpanel_full(_("Search failed"),
638                                                 _("Search string not found."),
639                                                 GTK_STOCK_CLOSE, NULL, NULL, FALSE,
640                                                 NULL, ALERT_WARNING, G_ALERTDEFAULT);
641                                 break;
642                         }
643
644                         if (backward)
645                                 str = _("Beginning of list reached; continue from end?");
646                         else
647                                 str = _("End of list reached; continue from beginning?");
648
649                         val = alertpanel(_("Search finished"), str,
650                                          GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
651                         if (G_ALERTALTERNATE == val) {
652                                 if (backward) {
653                                         node = GTK_CTREE_NODE
654                                                 (GTK_CLIST(ctree)->row_list_end);
655                                 } else {
656                                         node = GTK_CTREE_NODE
657                                                 (GTK_CLIST(ctree)->row_list);
658                                 }
659
660                                 all_searched = TRUE;
661
662                                 manage_window_focus_in(search_window.window, NULL, NULL);
663                         } else {
664                                 break;
665                         }
666                 }
667
668                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
669                 body_matched = FALSE;
670
671                 if (adv_search) {
672                         matched = matcherlist_match(search_window.matcher_list, msginfo);
673                 } else {
674                         if (bool_and) {
675                                 matched = TRUE;
676                                 if (*from_str) {
677                                         if (!msginfo->from ||
678                                         !str_find_func(msginfo->from, from_str)) {
679                                                 matched = FALSE;
680                                         }
681                                 }
682                                 if (matched && *to_str) {
683                                         if (!msginfo->to ||
684                                         !str_find_func(msginfo->to, to_str)) {
685                                                 matched = FALSE;
686                                         }
687                                 }
688                                 if (matched && *subject_str) {
689                                         if (!msginfo->subject ||
690                                         !str_find_func(msginfo->subject, subject_str)) {
691                                                 matched = FALSE;
692                                         }
693                                 }
694                                 if (matched && *body_str) {
695                                         if (procmime_find_string(msginfo, body_str,
696                                                                  str_find_func)) {
697                                                 body_matched = TRUE;
698                                         } else {
699                                                 matched = FALSE;
700                                         }
701                                 }
702                                 if (matched && !*from_str && !*to_str &&
703                                 !*subject_str && !*body_str) {
704                                         matched = FALSE;
705                                 }
706                         } else {
707                                 matched = FALSE;
708                                 if (*from_str && msginfo->from) {
709                                         if (str_find_func(msginfo->from, from_str)) {
710                                                 matched = TRUE;
711                                         }
712                                 }
713                                 if (!matched && *to_str && msginfo->to) {
714                                         if (str_find_func(msginfo->to, to_str)) {
715                                                 matched = TRUE;
716                                         }
717                                 }
718                                 if (!matched && *subject_str && msginfo->subject) {
719                                         if (str_find_func(msginfo->subject, subject_str)) {
720                                                 matched = TRUE;
721                                         }
722                                 }
723                                 if (!matched && *body_str) {
724                                         if (procmime_find_string(msginfo, body_str,
725                                                                  str_find_func)) {
726                                                 matched = TRUE;
727                                                 body_matched = TRUE;
728                                         }
729                                 }
730                         }
731                 }
732
733                 if (matched) {
734                         if (search_all) {
735                                 gtk_ctree_select(ctree, node);
736                         } else {
737                                 if (messageview_is_visible
738                                         (summaryview->messageview)) {
739                                         summary_unlock(summaryview);
740                                         summary_select_node
741                                                 (summaryview, node, TRUE, TRUE);
742                                         summary_lock(summaryview);
743                                         if (body_matched) {
744                                                 messageview_search_string
745                                                         (summaryview->messageview,
746                                                          body_str, case_sens);
747                                         }
748                                 } else {
749                                         summary_select_node
750                                                 (summaryview, node, FALSE, TRUE);
751                                 }
752                                 break;
753                         }
754                 }
755
756                 node = backward ? gtkut_ctree_node_prev(ctree, node)
757                                 : gtkut_ctree_node_next(ctree, node);
758                 if (i % interval == 0)
759                         GTK_EVENTS_FLUSH();
760         }
761
762         search_window.is_searching = FALSE;
763         summary_hide_stop_button();
764         main_window_cursor_normal(summaryview->mainwin);
765         if (search_all) {
766                 summary_thaw(summaryview);
767         }
768         summary_unlock(summaryview);
769 }
770
771 static void summary_search_clear(GtkButton *button, gpointer data)
772 {
773         if (gtk_toggle_button_get_active
774                 (GTK_TOGGLE_BUTTON(search_window.adv_search_checkbtn))) {
775                 gtk_entry_set_text(GTK_ENTRY(GTK_BIN(search_window.adv_condition_entry)->child), "");
776         } else {
777                 gtk_entry_set_text(GTK_ENTRY(GTK_BIN(search_window.from_entry)->child), "");
778                 gtk_entry_set_text(GTK_ENTRY(GTK_BIN(search_window.to_entry)->child), "");
779                 gtk_entry_set_text(GTK_ENTRY(GTK_BIN(search_window.subject_entry)->child), "");
780                 gtk_entry_set_text(GTK_ENTRY(GTK_BIN(search_window.body_entry)->child), "");
781         }
782         /* stop searching */
783         if (search_window.is_searching) {
784                 search_window.is_searching = FALSE;
785         }
786 }
787
788 static void summary_search_prev_clicked(GtkButton *button, gpointer data)
789 {
790         summary_search_execute(TRUE, FALSE);
791 }
792
793 static void summary_search_next_clicked(GtkButton *button, gpointer data)
794 {
795         summary_search_execute(FALSE, FALSE);
796 }
797
798 static void summary_search_all_clicked(GtkButton *button, gpointer data)
799 {
800         summary_search_execute(FALSE, TRUE);
801 }
802
803 static void adv_condition_btn_done(MatcherList * matchers)
804 {
805         gchar *str;
806
807         g_return_if_fail(
808                         mainwindow_get_mainwindow()->summaryview->quicksearch != NULL);
809
810         if (matchers == NULL) {
811                 return;
812         }
813
814         str = matcherlist_to_string(matchers);
815
816         if (str != NULL) {
817                 gtk_entry_set_text(GTK_ENTRY(GTK_BIN(search_window.adv_condition_entry)->child), str);
818                 g_free(str);
819         }
820 }
821
822 static void summary_search_stop_clicked(GtkButton *button, gpointer data)
823 {
824         search_window.is_searching = FALSE;
825 }
826
827 static void adv_condition_btn_clicked(GtkButton *button, gpointer data)
828 {
829         const gchar * cond_str;
830         MatcherList * matchers = NULL;
831
832         g_return_if_fail( search_window.window != NULL );
833
834         /* re-use the current search value if it's a condition expression,
835            otherwise ignore it silently */
836         cond_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.adv_condition_entry));
837         if (*cond_str != '\0') {
838                 matchers = matcher_parser_get_cond((gchar*)cond_str, NULL);
839         }
840
841         prefs_matcher_open(matchers, adv_condition_btn_done);
842
843         if (matchers != NULL) {
844                 matcherlist_free(matchers);
845         }
846 };
847
848 static void from_changed(void)
849 {
850         if (!search_window.from_entry_has_focus)
851                 gtk_widget_grab_focus(search_window.from_entry);
852 }
853
854 static void to_changed(void)
855 {
856         if (!search_window.to_entry_has_focus)
857                 gtk_widget_grab_focus(search_window.to_entry);
858 }
859
860 static void subject_changed(void)
861 {
862         if (!search_window.subject_entry_has_focus)
863                 gtk_widget_grab_focus(search_window.subject_entry);
864 }
865
866 static void body_changed(void)
867 {
868         if (!search_window.body_entry_has_focus)
869                 gtk_widget_grab_focus(search_window.body_entry);
870 }
871
872 static void adv_condition_changed(void)
873 {
874         if (!search_window.adv_condition_entry_has_focus)
875                 gtk_widget_grab_focus(search_window.adv_condition_entry);
876 }
877
878 static gboolean from_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
879                                   gpointer data)
880 {
881         search_window.from_entry_has_focus = TRUE;
882         return FALSE;
883 }
884
885 static gboolean from_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
886                                   gpointer data)
887 {
888         search_window.from_entry_has_focus = FALSE;
889         return FALSE;
890 }
891
892 static gboolean to_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
893                                   gpointer data)
894 {
895         search_window.to_entry_has_focus = TRUE;
896         return FALSE;
897 }
898
899 static gboolean to_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
900                                   gpointer data)
901 {
902         search_window.to_entry_has_focus = FALSE;
903         return FALSE;
904 }
905
906 static gboolean subject_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
907                                   gpointer data)
908 {
909         search_window.subject_entry_has_focus = TRUE;
910         return FALSE;
911 }
912
913 static gboolean subject_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
914                                   gpointer data)
915 {
916         search_window.subject_entry_has_focus = FALSE;
917         return FALSE;
918 }
919
920 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
921                                   gpointer data)
922 {
923         search_window.body_entry_has_focus = TRUE;
924         return FALSE;
925 }
926
927 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
928                                   gpointer data)
929 {
930         search_window.body_entry_has_focus = FALSE;
931         return FALSE;
932 }
933
934 static gboolean adv_condition_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
935                                   gpointer data)
936 {
937         search_window.adv_condition_entry_has_focus = TRUE;
938         return FALSE;
939 }
940
941 static gboolean adv_condition_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
942                                   gpointer data)
943 {
944         search_window.adv_condition_entry_has_focus = FALSE;
945         return FALSE;
946 }
947
948 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
949                             gpointer data)
950 {
951         if (event && event->keyval == GDK_Escape) {
952                 /* ESC key will:
953                         - stop a running search
954                         - close the search window if no search is running
955                 */
956                 if (!search_window.is_searching) {
957                         gtk_widget_hide(search_window.window);
958                 } else {
959                         search_window.is_searching = FALSE;
960                 }
961         }
962         return FALSE;
963 }