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