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