Fix remaining cases of bug #2923
[claws.git] / src / summary_search.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2013 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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <gtk/gtk.h>
31 #if !GTK_CHECK_VERSION(3, 0, 0)
32 #include "gtk/gtksctree.h"
33 #endif
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "main.h"
39 #include "summary_search.h"
40 #include "summaryview.h"
41 #include "messageview.h"
42 #include "mainwindow.h"
43 #include "menu.h"
44 #include "utils.h"
45 #include "gtkutils.h"
46 #include "combobox.h"
47 #include "prefs_gtk.h"
48 #include "manage_window.h"
49 #include "alertpanel.h"
50 #include "advsearch.h"
51 #include "matcher.h"
52 #include "matcher_parser.h"
53 #include "prefs_matcher.h"
54 #include "manual.h"
55 #include "prefs_common.h"
56 #include "statusbar.h"
57
58 static struct SummarySearchWindow {
59         GtkWidget *window;
60
61         GtkWidget *bool_optmenu;
62
63         GtkWidget *from_entry;
64         GtkWidget *to_entry;
65         GtkWidget *subject_entry;
66         GtkWidget *body_entry;
67
68         GtkWidget *adv_condition_entry;
69         GtkWidget *adv_condition_btn;
70         GtkWidget *adv_search_checkbtn;
71
72         GtkWidget *case_checkbtn;
73
74         GtkWidget *clear_btn;
75         GtkWidget *help_btn;
76         GtkWidget *all_btn;
77         GtkWidget *prev_btn;
78         GtkWidget *next_btn;
79         GtkWidget *close_btn;
80         GtkWidget *stop_btn;
81
82         SummaryView *summaryview;
83
84         AdvancedSearch  *advsearch;
85         gboolean        is_fast;
86         gboolean        matcher_is_outdated;
87         gboolean        search_in_progress;
88         GHashTable      *matched_msgnums;
89
90         gboolean is_searching;
91         gboolean from_entry_has_focus;
92         gboolean to_entry_has_focus;
93         gboolean subject_entry_has_focus;
94         gboolean body_entry_has_focus;
95         gboolean adv_condition_entry_has_focus;
96 } search_window;
97
98 static gchar* add_history_get(GtkWidget *from, GList **history);
99
100 static void summary_search_create       (void);
101
102 static gboolean summary_search_verify_match             (MsgInfo *msg);
103 static gboolean summary_search_prepare_matcher          ();
104 static gboolean summary_search_prereduce_msg_list       ();
105
106 static void summary_search_execute      (gboolean        backward,
107                                          gboolean        search_all);
108
109 static void summary_search_clear        (GtkButton      *button,
110                                          gpointer        data);
111 static void summary_search_prev_clicked (GtkButton      *button,
112                                          gpointer        data);
113 static void summary_search_next_clicked (GtkButton      *button,
114                                          gpointer        data);
115 static void summary_search_all_clicked  (GtkButton      *button,
116                                          gpointer        data);
117 static void summary_search_stop_clicked (GtkButton      *button,
118                                          gpointer        data);
119 static void adv_condition_btn_clicked   (GtkButton      *button,
120                                          gpointer        data);
121
122 static void from_changed                        (void);
123 static void to_changed                          (void);
124 static void subject_changed                     (void);
125 static void body_changed                        (void);
126 static void adv_condition_changed       (void);
127
128 static gboolean from_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
129                                   gpointer data);
130 static gboolean from_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
131                                   gpointer data);
132 static gboolean to_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
133                                   gpointer data);
134 static gboolean to_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
135                                   gpointer data);
136 static gboolean subject_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
137                                   gpointer data);
138 static gboolean subject_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
139                                   gpointer data);
140 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
141                                   gpointer data);
142 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
143                                   gpointer data);
144 static gboolean adv_condition_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
145                                   gpointer data);
146 static gboolean adv_condition_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
147                                   gpointer data);
148 static gboolean key_pressed             (GtkWidget      *widget,
149                                          GdkEventKey    *event,
150                                          gpointer        data);
151
152 #if !GTK_CHECK_VERSION(2,14,0)
153 /* Work around http://bugzilla.gnome.org/show_bug.cgi?id=56070 */
154 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) {                                    \
155         gboolean in_btn = FALSE;                                                        \
156         if (GTK_IS_BUTTON(widget))                                                      \
157                 in_btn = GTK_BUTTON(widget)->in_button;                                 \
158         gtk_widget_set_sensitive(widget, sensitive);                                    \
159         if (GTK_IS_BUTTON(widget))                                                      \
160                 GTK_BUTTON(widget)->in_button = in_btn;                                 \
161 }
162 #else
163 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) {                                    \
164         gtk_widget_set_sensitive(widget, sensitive);                                    \
165 }
166 #endif
167
168 static gchar* add_history_get(GtkWidget *from, GList **history)
169 {
170         gchar *result;
171
172 #if !GTK_CHECK_VERSION(2, 24, 0)
173         result = gtk_combo_box_get_active_text(GTK_COMBO_BOX(from));
174 #else
175         result = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(from));
176 #endif
177         if (!result)
178                 result = gtk_editable_get_chars(GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(from))), 0, -1);
179
180         if (result && result[0] != '\0') {
181                 /* add to history */
182 #if !GTK_CHECK_VERSION(2, 24, 0)
183                 combobox_unset_popdown_strings(GTK_COMBO_BOX(from));
184 #else
185                 combobox_unset_popdown_strings(GTK_COMBO_BOX_TEXT(from));
186 #endif
187                 *history = add_history(*history, result);
188 #if !GTK_CHECK_VERSION(2, 24, 0)
189                 combobox_set_popdown_strings(GTK_COMBO_BOX(from), *history);
190 #else
191                 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(from), *history);
192 #endif
193
194                 return result;
195         } else {
196                 g_free(result);
197                 return NULL;
198         }
199 }
200
201 void summary_search(SummaryView *summaryview)
202 {
203         if (!search_window.window) {
204                 summary_search_create();
205         } else {
206                 gtk_widget_hide(search_window.window);
207         }
208
209         search_window.summaryview = summaryview;
210
211         gtk_widget_grab_focus(search_window.next_btn);
212         gtk_widget_grab_focus(search_window.subject_entry);
213         gtk_widget_show(search_window.window);
214 }
215
216 static void summary_show_stop_button(void)
217 {
218         gtk_widget_hide(search_window.close_btn);
219         gtk_widget_show(search_window.stop_btn);
220         GTK_BUTTON_SET_SENSITIVE(search_window.all_btn, FALSE)
221         GTK_BUTTON_SET_SENSITIVE(search_window.prev_btn, FALSE)
222         GTK_BUTTON_SET_SENSITIVE(search_window.next_btn, FALSE)
223 }
224
225 static void summary_hide_stop_button(void)
226 {
227         gtk_widget_hide(search_window.stop_btn);
228         gtk_widget_show(search_window.close_btn);
229         gtk_widget_set_sensitive(search_window.all_btn, TRUE);
230         gtk_widget_set_sensitive(search_window.prev_btn, TRUE);
231         gtk_widget_set_sensitive(search_window.next_btn, TRUE);
232 }
233
234 static void summary_search_create(void)
235 {
236         GtkWidget *window;
237         GtkWidget *vbox1;
238         GtkWidget *bool_hbox;
239         GtkWidget *bool_optmenu;
240         GtkListStore *menu;
241         GtkTreeIter iter;
242         GtkWidget *clear_btn;
243
244         GtkWidget *table1;
245         GtkWidget *from_label;
246         GtkWidget *from_entry;
247         GtkWidget *to_label;
248         GtkWidget *to_entry;
249         GtkWidget *subject_label;
250         GtkWidget *subject_entry;
251         GtkWidget *body_label;
252         GtkWidget *body_entry;
253         GtkWidget *adv_condition_label;
254         GtkWidget *adv_condition_entry;
255         GtkWidget *adv_condition_btn;
256
257         GtkWidget *checkbtn_hbox;
258         GtkWidget *adv_search_checkbtn;
259         GtkWidget *case_checkbtn;
260
261         GtkWidget *confirm_area;
262         GtkWidget *help_btn;
263         GtkWidget *all_btn;
264         GtkWidget *prev_btn;
265         GtkWidget *next_btn;
266         GtkWidget *close_btn;
267         GtkWidget *stop_btn;
268         gboolean is_searching = FALSE;
269
270         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "summary_search");
271         gtk_window_set_title(GTK_WINDOW (window), _("Search messages"));
272         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
273         gtk_container_set_border_width(GTK_CONTAINER (window), 8);
274         g_signal_connect(G_OBJECT(window), "delete_event",
275                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
276         g_signal_connect(G_OBJECT(window), "key_press_event",
277                          G_CALLBACK(key_pressed), NULL);
278         MANAGE_WINDOW_SIGNALS_CONNECT(window);
279
280         vbox1 = gtk_vbox_new (FALSE, 0);
281         gtk_widget_show (vbox1);
282         gtk_container_add (GTK_CONTAINER (window), vbox1);
283
284         bool_hbox = gtk_hbox_new(FALSE, 4);
285         gtk_widget_show(bool_hbox);
286         gtk_box_pack_start(GTK_BOX(vbox1), bool_hbox, FALSE, FALSE, 0);
287
288         bool_optmenu = gtkut_sc_combobox_create(NULL, FALSE);
289         menu = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(bool_optmenu)));
290         gtk_widget_show(bool_optmenu);
291         gtk_box_pack_start(GTK_BOX(bool_hbox), bool_optmenu, FALSE, FALSE, 0);
292
293         COMBOBOX_ADD(menu, _("Match any of the following"), 0);
294         gtk_combo_box_set_active_iter(GTK_COMBO_BOX(bool_optmenu), &iter);
295         COMBOBOX_ADD(menu, _("Match all of the following"), 1);
296
297         clear_btn = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
298         gtk_widget_show(clear_btn);
299         gtk_box_pack_end(GTK_BOX(bool_hbox), clear_btn, FALSE, FALSE, 0);
300
301         table1 = gtk_table_new (5, 3, FALSE);
302         gtk_widget_show (table1);
303         gtk_box_pack_start (GTK_BOX (vbox1), table1, TRUE, TRUE, 0);
304         gtk_container_set_border_width (GTK_CONTAINER (table1), 4);
305         gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
306         gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
307
308 #if !GTK_CHECK_VERSION(2, 24, 0)
309         from_entry = gtk_combo_box_entry_new_text ();
310 #else
311         from_entry = gtk_combo_box_text_new_with_entry ();
312 #endif
313         gtk_combo_box_set_active(GTK_COMBO_BOX(from_entry), -1);
314         if (prefs_common.summary_search_from_history)
315 #if !GTK_CHECK_VERSION(2, 24, 0)
316                 combobox_set_popdown_strings(GTK_COMBO_BOX(from_entry),
317                                 prefs_common.summary_search_from_history);
318 #else
319                 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(from_entry),
320                                 prefs_common.summary_search_from_history);
321 #endif
322         gtk_widget_show (from_entry);
323         gtk_table_attach (GTK_TABLE (table1), from_entry, 1, 3, 0, 1,
324                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
325         g_signal_connect(G_OBJECT(from_entry), "changed",
326                          G_CALLBACK(from_changed), NULL);
327         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((from_entry)))),
328                          "focus_in_event", G_CALLBACK(from_entry_focus_evt_in), NULL);
329         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((from_entry)))),
330                          "focus_out_event", G_CALLBACK(from_entry_focus_evt_out), NULL);
331
332 #if !GTK_CHECK_VERSION(2, 24, 0)
333         to_entry = gtk_combo_box_entry_new_text ();
334 #else
335         to_entry = gtk_combo_box_text_new_with_entry ();
336 #endif
337         gtk_combo_box_set_active(GTK_COMBO_BOX(to_entry), -1);
338         if (prefs_common.summary_search_to_history)
339 #if !GTK_CHECK_VERSION(2, 24, 0)
340                 combobox_set_popdown_strings(GTK_COMBO_BOX(to_entry),
341                                 prefs_common.summary_search_to_history);
342 #else
343                 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(to_entry),
344                                 prefs_common.summary_search_to_history);
345 #endif
346         gtk_widget_show (to_entry);
347         gtk_table_attach (GTK_TABLE (table1), to_entry, 1, 3, 1, 2,
348                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
349         g_signal_connect(G_OBJECT(to_entry), "changed",
350                          G_CALLBACK(to_changed), NULL);
351         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((to_entry)))),
352                          "focus_in_event", G_CALLBACK(to_entry_focus_evt_in), NULL);
353         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((to_entry)))),
354                          "focus_out_event", G_CALLBACK(to_entry_focus_evt_out), NULL);
355
356 #if !GTK_CHECK_VERSION(2, 24, 0)
357         subject_entry = gtk_combo_box_entry_new_text ();
358 #else
359         subject_entry = gtk_combo_box_text_new_with_entry ();
360 #endif
361         gtk_combo_box_set_active(GTK_COMBO_BOX(subject_entry), -1);
362         if (prefs_common.summary_search_subject_history)
363 #if !GTK_CHECK_VERSION(2, 24, 0)
364                 combobox_set_popdown_strings(GTK_COMBO_BOX(subject_entry),
365                                 prefs_common.summary_search_subject_history);
366 #else
367                 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(subject_entry),
368                                 prefs_common.summary_search_subject_history);
369 #endif
370         gtk_widget_show (subject_entry);
371         gtk_table_attach (GTK_TABLE (table1), subject_entry, 1, 3, 2, 3,
372                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
373         g_signal_connect(G_OBJECT(subject_entry), "changed",
374                          G_CALLBACK(subject_changed), NULL);
375         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((subject_entry)))),
376                          "focus_in_event", G_CALLBACK(subject_entry_focus_evt_in), NULL);
377         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((subject_entry)))),
378                          "focus_out_event", G_CALLBACK(subject_entry_focus_evt_out), NULL);
379
380 #if !GTK_CHECK_VERSION(2, 24, 0)
381         body_entry = gtk_combo_box_entry_new_text ();
382 #else
383         body_entry = gtk_combo_box_text_new_with_entry ();
384 #endif
385         gtk_combo_box_set_active(GTK_COMBO_BOX(body_entry), -1);
386         if (prefs_common.summary_search_body_history)
387 #if !GTK_CHECK_VERSION(2, 24, 0)
388                 combobox_set_popdown_strings(GTK_COMBO_BOX(body_entry),
389                                 prefs_common.summary_search_body_history);
390 #else
391                 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(body_entry),
392                                 prefs_common.summary_search_body_history);
393 #endif
394         gtk_widget_show (body_entry);
395         gtk_table_attach (GTK_TABLE (table1), body_entry, 1, 3, 3, 4,
396                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
397         g_signal_connect(G_OBJECT(body_entry), "changed",
398                          G_CALLBACK(body_changed), NULL);
399         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
400                          "focus_in_event", G_CALLBACK(body_entry_focus_evt_in), NULL);
401         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
402                          "focus_out_event", G_CALLBACK(body_entry_focus_evt_out), NULL);
403
404 #if !GTK_CHECK_VERSION(2, 24, 0)
405         adv_condition_entry = gtk_combo_box_entry_new_text ();
406 #else
407         adv_condition_entry = gtk_combo_box_text_new_with_entry ();
408 #endif
409         gtk_combo_box_set_active(GTK_COMBO_BOX(adv_condition_entry), -1);
410         if (prefs_common.summary_search_adv_condition_history)
411 #if !GTK_CHECK_VERSION(2, 24, 0)
412                 combobox_set_popdown_strings(GTK_COMBO_BOX(adv_condition_entry),
413                                 prefs_common.summary_search_adv_condition_history);
414 #else
415                 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(adv_condition_entry),
416                                 prefs_common.summary_search_adv_condition_history);
417 #endif
418         gtk_widget_show (adv_condition_entry);
419         gtk_table_attach (GTK_TABLE (table1), adv_condition_entry, 1, 2, 4, 5,
420                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
421         g_signal_connect(G_OBJECT(adv_condition_entry), "changed",
422                          G_CALLBACK(adv_condition_changed), NULL);
423         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((adv_condition_entry)))),
424                          "focus_in_event", G_CALLBACK(adv_condition_entry_focus_evt_in), NULL);
425         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((adv_condition_entry)))),
426                          "focus_out_event", G_CALLBACK(adv_condition_entry_focus_evt_out), NULL);
427
428         adv_condition_btn = gtk_button_new_with_label(" ... ");
429         gtk_widget_show (adv_condition_btn);
430         gtk_table_attach (GTK_TABLE (table1), adv_condition_btn, 2, 3, 4, 5,
431                           GTK_FILL, 0, 0, 0);
432         g_signal_connect(G_OBJECT (adv_condition_btn), "clicked",
433                          G_CALLBACK(adv_condition_btn_clicked), search_window.window);
434
435         CLAWS_SET_TIP(adv_condition_btn,
436                              _("Edit search criteria"));
437
438         from_label = gtk_label_new (_("From:"));
439         gtk_widget_show (from_label);
440         gtk_table_attach (GTK_TABLE (table1), from_label, 0, 1, 0, 1,
441                           GTK_FILL, 0, 0, 0);
442         gtk_label_set_justify (GTK_LABEL (from_label), GTK_JUSTIFY_RIGHT);
443         gtk_misc_set_alignment (GTK_MISC (from_label), 1, 0.5);
444
445         to_label = gtk_label_new (_("To:"));
446         gtk_widget_show (to_label);
447         gtk_table_attach (GTK_TABLE (table1), to_label, 0, 1, 1, 2,
448                           GTK_FILL, 0, 0, 0);
449         gtk_label_set_justify (GTK_LABEL (to_label), GTK_JUSTIFY_RIGHT);
450         gtk_misc_set_alignment (GTK_MISC (to_label), 1, 0.5);
451
452         subject_label = gtk_label_new (_("Subject:"));
453         gtk_widget_show (subject_label);
454         gtk_table_attach (GTK_TABLE (table1), subject_label, 0, 1, 2, 3,
455                           GTK_FILL, 0, 0, 0);
456         gtk_label_set_justify (GTK_LABEL (subject_label), GTK_JUSTIFY_RIGHT);
457         gtk_misc_set_alignment (GTK_MISC (subject_label), 1, 0.5);
458
459         body_label = gtk_label_new (_("Body:"));
460         gtk_widget_show (body_label);
461         gtk_table_attach (GTK_TABLE (table1), body_label, 0, 1, 3, 4,
462                           GTK_FILL, 0, 0, 0);
463         gtk_label_set_justify (GTK_LABEL (body_label), GTK_JUSTIFY_RIGHT);
464         gtk_misc_set_alignment (GTK_MISC (body_label), 1, 0.5);
465
466         adv_condition_label = gtk_label_new (_("Condition:"));
467         gtk_widget_show (adv_condition_label);
468         gtk_table_attach (GTK_TABLE (table1), adv_condition_label, 0, 1, 4, 5,
469                           GTK_FILL, 0, 0, 0);
470         gtk_label_set_justify (GTK_LABEL (adv_condition_label), GTK_JUSTIFY_RIGHT);
471         gtk_misc_set_alignment (GTK_MISC (adv_condition_label), 1, 0.5);
472
473         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
474         gtk_widget_show (checkbtn_hbox);
475         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
476         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
477
478         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
479         gtk_widget_show (case_checkbtn);
480         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
481                             FALSE, FALSE, 0);
482
483         adv_search_checkbtn = gtk_check_button_new_with_label (_("Extended Search"));
484         gtk_widget_show (adv_search_checkbtn);
485         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), adv_search_checkbtn,
486                             FALSE, FALSE, 0);
487
488         confirm_area = gtk_hbutton_box_new();
489         gtk_widget_show (confirm_area);
490         gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
491                                   GTK_BUTTONBOX_END);
492         gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
493
494         gtkut_stock_button_add_help(confirm_area, &help_btn);
495
496         all_btn = gtk_button_new_with_mnemonic(_("Find _all"));
497         gtkut_widget_set_can_default(all_btn, TRUE);
498         gtk_box_pack_start(GTK_BOX(confirm_area), all_btn, TRUE, TRUE, 0);
499         gtk_widget_show(all_btn);
500
501         prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
502         gtkut_widget_set_can_default(prev_btn, TRUE);
503         gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
504         gtk_widget_show(prev_btn);
505
506         next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
507         gtkut_widget_set_can_default(next_btn, TRUE);
508         gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
509         gtk_widget_show(next_btn);
510
511         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
512         gtkut_widget_set_can_default(close_btn, TRUE);
513         gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
514         gtk_widget_show(close_btn);
515
516         /* stop button hidden */
517         stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
518         gtkut_widget_set_can_default(stop_btn, TRUE);
519         gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
520
521         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
522         gtk_widget_grab_default(next_btn);
523
524         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, bool_optmenu)
525         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, from_entry)
526         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, to_entry)
527         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, subject_entry)
528         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, body_entry)
529         SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_label)
530         SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_entry)
531         SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_btn)
532         SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, case_checkbtn)
533
534         g_signal_connect(G_OBJECT(help_btn), "clicked",
535                          G_CALLBACK(manual_open_with_anchor_cb),
536                          MANUAL_ANCHOR_SEARCHING);
537         g_signal_connect(G_OBJECT(clear_btn), "clicked",
538                          G_CALLBACK(summary_search_clear), NULL);
539         g_signal_connect(G_OBJECT(all_btn), "clicked",
540                          G_CALLBACK(summary_search_all_clicked), NULL);
541         g_signal_connect(G_OBJECT(prev_btn), "clicked",
542                          G_CALLBACK(summary_search_prev_clicked), NULL);
543         g_signal_connect(G_OBJECT(next_btn), "clicked",
544                          G_CALLBACK(summary_search_next_clicked), NULL);
545         g_signal_connect_closure
546                 (G_OBJECT(close_btn), "clicked",
547                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
548              window, NULL), FALSE);
549         g_signal_connect(G_OBJECT(stop_btn), "clicked",
550                          G_CALLBACK(summary_search_stop_clicked), NULL);
551
552         search_window.window = window;
553         search_window.bool_optmenu = bool_optmenu;
554         search_window.from_entry = from_entry;
555         search_window.to_entry = to_entry;
556         search_window.subject_entry = subject_entry;
557         search_window.body_entry = body_entry;
558         search_window.adv_condition_entry = adv_condition_entry;
559         search_window.adv_condition_btn = adv_condition_btn;
560         search_window.case_checkbtn = case_checkbtn;
561         search_window.adv_search_checkbtn = adv_search_checkbtn;
562         search_window.clear_btn = clear_btn;
563         search_window.help_btn = help_btn;
564         search_window.all_btn = all_btn;
565         search_window.prev_btn = prev_btn;
566         search_window.next_btn = next_btn;
567         search_window.close_btn = close_btn;
568         search_window.stop_btn = stop_btn;
569         search_window.advsearch = NULL;
570         search_window.matcher_is_outdated = TRUE;
571         search_window.search_in_progress = FALSE;
572         search_window.matched_msgnums = NULL;
573         search_window.is_searching = is_searching;
574 }
575
576 static gboolean summary_search_verify_match(MsgInfo *msg)
577 {
578         gpointer msgnum = GUINT_TO_POINTER(msg->msgnum);
579
580         if (g_hash_table_lookup(search_window.matched_msgnums, msgnum) != NULL)
581                 return TRUE;
582         else
583                 return FALSE;
584 }
585
586 static gboolean summary_search_progress_cb(gpointer data, guint at, guint matched, guint total)
587 {
588         if (!search_window.is_searching) {
589                 search_window.matcher_is_outdated = TRUE;
590                 return FALSE;
591         }
592
593         return summaryview_search_root_progress(search_window.summaryview, at, matched, total);
594 }
595
596 static gboolean summary_search_prepare_matcher()
597 {
598         gboolean adv_search;
599         gboolean bool_and = FALSE;
600         gboolean case_sens = FALSE;
601         gchar *matcher_str;
602         gint match_type;
603         gchar *from_str = NULL, *to_str = NULL, *subject_str = NULL;
604         gchar *body_str = NULL;
605         GSList *matchers = NULL;
606
607         if (!search_window.matcher_is_outdated)
608                 return TRUE;
609
610         if (search_window.advsearch == NULL) {
611                 search_window.advsearch = advsearch_new();
612                 advsearch_set_on_error_cb(search_window.advsearch, NULL, NULL); /* TODO */
613                 advsearch_set_on_progress_cb(search_window.advsearch, 
614                         summary_search_progress_cb, NULL);
615         }
616
617         adv_search = gtk_toggle_button_get_active
618                 (GTK_TOGGLE_BUTTON(search_window.adv_search_checkbtn));
619
620         if (adv_search) {
621                 matcher_str = add_history_get(search_window.adv_condition_entry, &prefs_common.summary_search_adv_condition_history);
622         } else {
623                 MatcherList *matcher_list;
624                 bool_and = combobox_get_active_data(GTK_COMBO_BOX(search_window.bool_optmenu));
625                 case_sens = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
626
627                 from_str    = add_history_get(search_window.from_entry, &prefs_common.summary_search_from_history);
628                 to_str      = add_history_get(search_window.to_entry, &prefs_common.summary_search_to_history);
629                 subject_str = add_history_get(search_window.subject_entry, &prefs_common.summary_search_subject_history);
630                 body_str    = add_history_get(search_window.body_entry, &prefs_common.summary_search_body_history);
631
632                 if (!from_str && !to_str && !subject_str && !body_str) {
633                         /* TODO: warn if no search criteria? (or make buttons enabled only when
634                          * at least one search criteria has been set */
635                         return FALSE;
636                 }
637
638                 match_type = case_sens ? MATCHTYPE_MATCH : MATCHTYPE_MATCHCASE;
639
640                 if (from_str) {
641                         MatcherProp *prop = matcherprop_new(MATCHCRITERIA_FROM, NULL, match_type, from_str, 0);
642                         matchers = g_slist_append(matchers, prop);
643                 }
644                 if (to_str) {
645                         MatcherProp *prop = matcherprop_new(MATCHCRITERIA_TO, NULL, match_type, to_str, 0);
646                         matchers = g_slist_append(matchers, prop);
647                 }
648                 if (subject_str) {
649                         MatcherProp *prop = matcherprop_new(MATCHCRITERIA_SUBJECT, NULL, match_type, subject_str, 0);
650                         matchers = g_slist_append(matchers, prop);
651                 }
652                 if (body_str) {
653                         MatcherProp *prop = matcherprop_new(MATCHCRITERIA_BODY_PART, NULL, match_type, body_str, 0);
654                         matchers = g_slist_append(matchers, prop);
655                 }
656                 g_free(from_str);
657                 g_free(to_str);
658                 g_free(subject_str);
659                 g_free(body_str);
660
661                 matcher_list = matcherlist_new(matchers, bool_and);
662                 if (!matcher_list)
663                         return FALSE;
664                 matcher_str = matcherlist_to_string(matcher_list);
665                 matcherlist_free(matcher_list);
666         }
667         if (!matcher_str)
668                 return FALSE;
669
670         advsearch_set(search_window.advsearch, ADVANCED_SEARCH_EXTENDED,
671                       matcher_str);
672
673         debug_print("Advsearch set: %s\n", matcher_str);
674         g_free(matcher_str);
675
676         if (!advsearch_has_proper_predicate(search_window.advsearch))
677                 return FALSE;
678
679         search_window.matcher_is_outdated = FALSE;
680
681         return TRUE;
682 }
683
684 static gboolean summary_search_prereduce_msg_list()
685 {
686         MsgInfoList *msglist = NULL;
687         MsgNumberList *msgnums = NULL;
688         MsgNumberList *cur;
689         SummaryView *summaryview = search_window.summaryview;
690         gboolean result;
691         FolderItem *item = summaryview->folder_item;
692         static GdkCursor *watch_cursor = NULL;
693         if (!watch_cursor)
694                 watch_cursor = gdk_cursor_new(GDK_WATCH);
695
696         if (search_window.matcher_is_outdated && !summary_search_prepare_matcher()) {
697                 return FALSE;
698         }
699
700         main_window_cursor_wait(mainwindow_get_mainwindow());
701         gdk_window_set_cursor(gtk_widget_get_window(search_window.window), watch_cursor);
702         statusbar_print_all(_("Searching in %s... \n"),
703                 item->path ? item->path : "(null)");
704
705         result = advsearch_search_msgs_in_folders(search_window.advsearch,
706                         &msglist, item, FALSE);
707         statusbar_pop_all();
708         statusbar_progress_all(0, 0, 0);
709         gdk_window_set_cursor(gtk_widget_get_window(search_window.window), NULL);
710         main_window_cursor_normal(mainwindow_get_mainwindow());
711
712         if (!result)
713                 return FALSE;
714         msgnums = procmsg_get_number_list_for_msgs(msglist);
715         procmsg_msg_list_free(msglist);
716
717         if (search_window.matched_msgnums == NULL)
718                 search_window.matched_msgnums = g_hash_table_new(g_direct_hash, NULL);
719
720         g_hash_table_remove_all(search_window.matched_msgnums);
721
722         for (cur = msgnums; cur != NULL; cur = cur->next) {
723                 g_hash_table_insert(search_window.matched_msgnums, cur->data, GINT_TO_POINTER(1));
724         }
725
726         g_slist_free(msgnums);
727
728         return TRUE;
729 }
730
731 static void summary_search_execute(gboolean backward, gboolean search_all)
732 {
733         SummaryView *summaryview = search_window.summaryview;
734         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
735         GtkCMCTreeNode *node;
736         MsgInfo *msginfo;
737         gboolean all_searched = FALSE;
738         gboolean matched = FALSE;
739         gint i = 0;
740
741         if (summary_is_locked(summaryview)) {
742                 return;
743         }
744         summary_lock(summaryview);
745
746         search_window.is_searching = TRUE;
747         main_window_cursor_wait(summaryview->mainwin);
748         summary_show_stop_button();
749
750         if (search_window.matcher_is_outdated && !summary_search_prereduce_msg_list()) {
751                 goto exit;
752         }
753
754         if (search_all) {
755                 summary_freeze(summaryview);
756                 summary_unselect_all(summaryview);
757                 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
758                 backward = FALSE;
759         } else if (!summaryview->selected) {
760                 if (backward) {
761                         node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list_end);
762                 } else {
763                         node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
764                 }
765
766                 if (!node) {
767                         search_window.is_searching = FALSE;
768                         summary_hide_stop_button();
769                         main_window_cursor_normal(summaryview->mainwin);
770                         summary_unlock(summaryview);
771                         return;
772                 }
773         } else {
774                 if (backward) {
775                         node = gtkut_ctree_node_prev(ctree, summaryview->selected);
776                 } else {
777                         node = gtkut_ctree_node_next(ctree, summaryview->selected);
778                 }
779         }
780
781         for (; search_window.is_searching; i++) {
782                 if (!node) {
783                         gchar *str;
784                         AlertValue val;
785
786                         if (search_all) {
787                                 break;
788                         }
789
790                         if (all_searched) {
791                                 alertpanel_full(_("Search failed"),
792                                                 _("Search string not found."),
793                                                 GTK_STOCK_CLOSE, NULL, NULL, FALSE,
794                                                 NULL, ALERT_WARNING, G_ALERTDEFAULT);
795                                 break;
796                         }
797
798                         if (backward)
799                                 str = _("Beginning of list reached; continue from end?");
800                         else
801                                 str = _("End of list reached; continue from beginning?");
802
803                         val = alertpanel(_("Search finished"), str,
804                                          GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
805                         if (G_ALERTALTERNATE == val) {
806                                 if (backward) {
807                                         node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list_end);
808                                 } else {
809                                         node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
810                                 }
811
812                                 all_searched = TRUE;
813
814                                 manage_window_focus_in(search_window.window, NULL, NULL);
815                         } else {
816                                 break;
817                         }
818                 }
819
820                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
821
822                 matched = summary_search_verify_match(msginfo);
823
824                 if (matched) {
825                         if (search_all) {
826                                 gtk_cmctree_select(ctree, node);
827                         } else {
828                                 if (messageview_is_visible(summaryview->messageview)) {
829                                         summary_unlock(summaryview);
830                                         summary_select_node(summaryview, node, TRUE, TRUE);
831                                         summary_lock(summaryview);
832                                 } else {
833                                         summary_select_node(summaryview, node, FALSE, TRUE);
834                                 }
835                                 break;
836                         }
837                 }
838
839                 if (i % (search_window.is_fast ? 1000 : 100) == 0) {
840                         GTK_EVENTS_FLUSH();
841                 }
842
843                 node = backward ? gtkut_ctree_node_prev(ctree, node)
844                                 : gtkut_ctree_node_next(ctree, node);
845         }
846
847 exit:
848         search_window.is_searching = FALSE;
849         summary_hide_stop_button();
850         main_window_cursor_normal(summaryview->mainwin);
851         if (search_all) {
852                 summary_thaw(summaryview);
853         }
854         summary_unlock(summaryview);
855 }
856
857 static void summary_search_clear(GtkButton *button, gpointer data)
858 {
859         if (gtk_toggle_button_get_active
860                 (GTK_TOGGLE_BUTTON(search_window.adv_search_checkbtn))) {
861                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.adv_condition_entry)))), "");
862         } else {
863                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.from_entry)))), "");
864                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.to_entry)))), "");
865                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.subject_entry)))), "");
866                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.body_entry)))), "");
867         }
868         /* stop searching */
869         if (search_window.is_searching) {
870                 search_window.is_searching = FALSE;
871         }
872         search_window.matcher_is_outdated = TRUE;
873 }
874
875 static void summary_search_prev_clicked(GtkButton *button, gpointer data)
876 {
877         summary_search_execute(TRUE, FALSE);
878 }
879
880 static void summary_search_next_clicked(GtkButton *button, gpointer data)
881 {
882         summary_search_execute(FALSE, FALSE);
883 }
884
885 static void summary_search_all_clicked(GtkButton *button, gpointer data)
886 {
887         summary_search_execute(FALSE, TRUE);
888 }
889
890 static void adv_condition_btn_done(MatcherList * matchers)
891 {
892         gchar *str;
893
894         cm_return_if_fail(
895                         mainwindow_get_mainwindow()->summaryview->quicksearch != NULL);
896
897         if (matchers == NULL) {
898                 return;
899         }
900
901         str = matcherlist_to_string(matchers);
902         search_window.matcher_is_outdated = TRUE;
903
904         if (str != NULL) {
905                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.adv_condition_entry)))), str);
906                 g_free(str);
907         }
908 }
909
910 static void summary_search_stop_clicked(GtkButton *button, gpointer data)
911 {
912         search_window.is_searching = FALSE;
913 }
914
915 static void adv_condition_btn_clicked(GtkButton *button, gpointer data)
916 {
917         const gchar * cond_str;
918         MatcherList * matchers = NULL;
919
920         cm_return_if_fail( search_window.window != NULL );
921
922         /* re-use the current search value if it's a condition expression,
923            otherwise ignore it silently */
924 #if !GTK_CHECK_VERSION(2, 24, 0)
925         cond_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.adv_condition_entry));
926 #else
927         cond_str = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(search_window.adv_condition_entry));
928 #endif
929         if (cond_str && *cond_str != '\0') {
930                 matchers = matcher_parser_get_cond((gchar*)cond_str, NULL);
931         }
932
933         prefs_matcher_open(matchers, adv_condition_btn_done);
934
935         if (matchers != NULL) {
936                 matcherlist_free(matchers);
937         }
938 };
939
940 static void from_changed(void)
941 {
942         if (!search_window.from_entry_has_focus)
943                 gtk_widget_grab_focus(search_window.from_entry);
944         search_window.matcher_is_outdated = TRUE;
945 }
946
947 static void to_changed(void)
948 {
949         if (!search_window.to_entry_has_focus)
950                 gtk_widget_grab_focus(search_window.to_entry);
951         search_window.matcher_is_outdated = TRUE;
952 }
953
954 static void subject_changed(void)
955 {
956         if (!search_window.subject_entry_has_focus)
957                 gtk_widget_grab_focus(search_window.subject_entry);
958         search_window.matcher_is_outdated = TRUE;
959 }
960
961 static void body_changed(void)
962 {
963         if (!search_window.body_entry_has_focus)
964                 gtk_widget_grab_focus(search_window.body_entry);
965         search_window.matcher_is_outdated = TRUE;
966 }
967
968 static void adv_condition_changed(void)
969 {
970         if (!search_window.adv_condition_entry_has_focus)
971                 gtk_widget_grab_focus(search_window.adv_condition_entry);
972         search_window.matcher_is_outdated = TRUE;
973 }
974
975 static gboolean from_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
976                                   gpointer data)
977 {
978         search_window.from_entry_has_focus = TRUE;
979         return FALSE;
980 }
981
982 static gboolean from_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
983                                   gpointer data)
984 {
985         search_window.from_entry_has_focus = FALSE;
986         return FALSE;
987 }
988
989 static gboolean to_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
990                                   gpointer data)
991 {
992         search_window.to_entry_has_focus = TRUE;
993         return FALSE;
994 }
995
996 static gboolean to_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
997                                   gpointer data)
998 {
999         search_window.to_entry_has_focus = FALSE;
1000         return FALSE;
1001 }
1002
1003 static gboolean subject_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
1004                                   gpointer data)
1005 {
1006         search_window.subject_entry_has_focus = TRUE;
1007         return FALSE;
1008 }
1009
1010 static gboolean subject_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
1011                                   gpointer data)
1012 {
1013         search_window.subject_entry_has_focus = FALSE;
1014         return FALSE;
1015 }
1016
1017 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
1018                                   gpointer data)
1019 {
1020         search_window.body_entry_has_focus = TRUE;
1021         return FALSE;
1022 }
1023
1024 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
1025                                   gpointer data)
1026 {
1027         search_window.body_entry_has_focus = FALSE;
1028         return FALSE;
1029 }
1030
1031 static gboolean adv_condition_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
1032                                   gpointer data)
1033 {
1034         search_window.adv_condition_entry_has_focus = TRUE;
1035         return FALSE;
1036 }
1037
1038 static gboolean adv_condition_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
1039                                   gpointer data)
1040 {
1041         search_window.adv_condition_entry_has_focus = FALSE;
1042         return FALSE;
1043 }
1044
1045 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
1046                             gpointer data)
1047 {
1048         if (event && (event->keyval == GDK_KEY_Escape)) {
1049                 /* ESC key will:
1050                         - stop a running search
1051                         - close the search window if no search is running
1052                 */
1053                 if (!search_window.is_searching) {
1054                         gtk_widget_hide(search_window.window);
1055                 } else {
1056                         search_window.is_searching = FALSE;
1057                 }
1058         }
1059
1060         if (event && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
1061                 if (!search_window.is_searching) {
1062                         summary_search_execute(FALSE, FALSE);
1063                 }
1064         }
1065
1066         if (event && (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_Up)) {
1067                 if (search_window.from_entry_has_focus) {
1068                         combobox_set_value_from_arrow_key(
1069                                         GTK_COMBO_BOX(search_window.from_entry),
1070                                         event->keyval);
1071                         return TRUE;
1072                 }
1073                 if (search_window.to_entry_has_focus) {
1074                         combobox_set_value_from_arrow_key(
1075                                         GTK_COMBO_BOX(search_window.to_entry),
1076                                         event->keyval);
1077                         return TRUE;
1078                 }
1079                 if (search_window.subject_entry_has_focus) {
1080                         combobox_set_value_from_arrow_key(
1081                                         GTK_COMBO_BOX(search_window.subject_entry),
1082                                         event->keyval);
1083                         return TRUE;
1084                 }
1085                 if (search_window.body_entry_has_focus) {
1086                         combobox_set_value_from_arrow_key(
1087                                         GTK_COMBO_BOX(search_window.body_entry),
1088                                         event->keyval);
1089                         return TRUE;
1090                 }
1091                 if (search_window.adv_condition_entry_has_focus) {
1092                         combobox_set_value_from_arrow_key(
1093                                         GTK_COMBO_BOX(search_window.adv_condition_entry),
1094                                         event->keyval);
1095                         return TRUE;
1096                 }
1097         }
1098
1099         return FALSE;
1100 }