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