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