2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2013 Hiroyuki Yamamoto and the Claws Mail team
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.
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.
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/>.
22 #include "claws-features.h"
28 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
31 #if !GTK_CHECK_VERSION(3, 0, 0)
32 #include "gtk/gtksctree.h"
39 #include "summary_search.h"
40 #include "summaryview.h"
41 #include "messageview.h"
42 #include "mainwindow.h"
47 #include "prefs_gtk.h"
48 #include "manage_window.h"
49 #include "alertpanel.h"
50 #include "advsearch.h"
52 #include "matcher_parser.h"
53 #include "prefs_matcher.h"
55 #include "prefs_common.h"
56 #include "statusbar.h"
58 static struct SummarySearchWindow {
61 GtkWidget *bool_optmenu;
63 GtkWidget *from_label;
64 GtkWidget *from_entry;
67 GtkWidget *subject_label;
68 GtkWidget *subject_entry;
69 GtkWidget *body_label;
70 GtkWidget *body_entry;
72 GtkWidget *adv_condition_label;
73 GtkWidget *adv_condition_entry;
74 GtkWidget *adv_condition_btn;
75 GtkWidget *adv_search_checkbtn;
77 GtkWidget *case_checkbtn;
87 SummaryView *summaryview;
89 AdvancedSearch *advsearch;
91 gboolean matcher_is_outdated;
92 gboolean search_in_progress;
93 GHashTable *matched_msgnums;
95 gboolean is_searching;
96 gboolean from_entry_has_focus;
97 gboolean to_entry_has_focus;
98 gboolean subject_entry_has_focus;
99 gboolean body_entry_has_focus;
100 gboolean adv_condition_entry_has_focus;
103 static gchar* add_history_get(GtkWidget *from, GList **history);
105 static void summary_search_create (void);
107 static gboolean summary_search_verify_match (MsgInfo *msg);
108 static gboolean summary_search_prepare_matcher ();
109 static gboolean summary_search_prereduce_msg_list ();
111 static void summary_search_execute (gboolean backward,
112 gboolean search_all);
114 static void summary_search_clear (GtkButton *button,
116 static void summary_search_prev_clicked (GtkButton *button,
118 static void summary_search_next_clicked (GtkButton *button,
120 static void summary_search_all_clicked (GtkButton *button,
122 static void summary_search_stop_clicked (GtkButton *button,
124 static void adv_condition_btn_clicked (GtkButton *button,
127 static void optmenu_changed (GtkComboBox *widget, gpointer user_data);
128 static void from_changed (void);
129 static void to_changed (void);
130 static void subject_changed (void);
131 static void body_changed (void);
132 static void adv_condition_changed (void);
133 static void case_changed (GtkToggleButton *togglebutton, gpointer user_data);
134 static void adv_search_changed (GtkToggleButton *togglebutton, gpointer user_data);
136 static gboolean from_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
138 static gboolean from_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
140 static gboolean to_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
142 static gboolean to_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
144 static gboolean subject_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
146 static gboolean subject_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
148 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
150 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
152 static gboolean adv_condition_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
154 static gboolean adv_condition_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
156 static gboolean key_pressed (GtkWidget *widget,
160 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) { \
161 gtk_widget_set_sensitive(widget, sensitive); \
164 static gchar* add_history_get(GtkWidget *from, GList **history)
168 #if !GTK_CHECK_VERSION(2, 24, 0)
169 result = gtk_combo_box_get_active_text(GTK_COMBO_BOX(from));
171 result = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(from));
174 result = gtk_editable_get_chars(GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(from))), 0, -1);
176 if (result && result[0] != '\0') {
178 #if !GTK_CHECK_VERSION(2, 24, 0)
179 combobox_unset_popdown_strings(GTK_COMBO_BOX(from));
181 combobox_unset_popdown_strings(GTK_COMBO_BOX_TEXT(from));
183 *history = add_history(*history, result);
184 #if !GTK_CHECK_VERSION(2, 24, 0)
185 combobox_set_popdown_strings(GTK_COMBO_BOX(from), *history);
187 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(from), *history);
197 void summary_search(SummaryView *summaryview)
199 if (!search_window.window) {
200 summary_search_create();
202 gtk_widget_hide(search_window.window);
205 search_window.summaryview = summaryview;
207 gtk_widget_grab_focus(search_window.next_btn);
208 gtk_widget_grab_focus(search_window.subject_entry);
209 gtk_widget_show(search_window.window);
212 static void summary_show_stop_button(void)
214 gtk_widget_hide(search_window.close_btn);
215 gtk_widget_show(search_window.stop_btn);
216 GTK_BUTTON_SET_SENSITIVE(search_window.clear_btn, FALSE)
217 GTK_BUTTON_SET_SENSITIVE(search_window.all_btn, FALSE)
218 GTK_BUTTON_SET_SENSITIVE(search_window.prev_btn, FALSE)
219 GTK_BUTTON_SET_SENSITIVE(search_window.next_btn, FALSE)
220 gtk_widget_set_sensitive(search_window.adv_condition_label, FALSE);
221 gtk_widget_set_sensitive(search_window.adv_condition_entry, FALSE);
222 gtk_widget_set_sensitive(search_window.adv_condition_btn, FALSE);
223 gtk_widget_set_sensitive(search_window.to_label, FALSE);
224 gtk_widget_set_sensitive(search_window.to_entry, FALSE);
225 gtk_widget_set_sensitive(search_window.from_label, FALSE);
226 gtk_widget_set_sensitive(search_window.from_entry, FALSE);
227 gtk_widget_set_sensitive(search_window.subject_label, FALSE);
228 gtk_widget_set_sensitive(search_window.subject_entry, FALSE);
229 gtk_widget_set_sensitive(search_window.body_label, FALSE);
230 gtk_widget_set_sensitive(search_window.body_entry, FALSE);
231 gtk_widget_set_sensitive(search_window.bool_optmenu, FALSE);
232 gtk_widget_set_sensitive(search_window.clear_btn, FALSE);
233 gtk_widget_set_sensitive(search_window.case_checkbtn, FALSE);
234 gtk_widget_set_sensitive(search_window.adv_search_checkbtn, FALSE);
237 static void summary_hide_stop_button(void)
239 GTK_BUTTON_SET_SENSITIVE(search_window.clear_btn, TRUE)
240 gtk_widget_hide(search_window.stop_btn);
241 gtk_widget_show(search_window.close_btn);
242 if (gtk_toggle_button_get_active
243 (GTK_TOGGLE_BUTTON(search_window.adv_search_checkbtn))) {
244 gtk_widget_set_sensitive(search_window.adv_condition_label, TRUE);
245 gtk_widget_set_sensitive(search_window.adv_condition_entry, TRUE);
246 gtk_widget_set_sensitive(search_window.adv_condition_btn, TRUE);
247 gtk_widget_set_sensitive(search_window.case_checkbtn, FALSE);
248 gtk_widget_set_sensitive(search_window.bool_optmenu, FALSE);
250 gtk_widget_set_sensitive(search_window.to_label, TRUE);
251 gtk_widget_set_sensitive(search_window.to_entry, TRUE);
252 gtk_widget_set_sensitive(search_window.from_label, TRUE);
253 gtk_widget_set_sensitive(search_window.from_entry, TRUE);
254 gtk_widget_set_sensitive(search_window.subject_label, TRUE);
255 gtk_widget_set_sensitive(search_window.subject_entry, TRUE);
256 gtk_widget_set_sensitive(search_window.body_label, TRUE);
257 gtk_widget_set_sensitive(search_window.body_entry, TRUE);
258 gtk_widget_set_sensitive(search_window.case_checkbtn, TRUE);
259 gtk_widget_set_sensitive(search_window.bool_optmenu, TRUE);
261 gtk_widget_set_sensitive(search_window.clear_btn, TRUE);
262 gtk_widget_set_sensitive(search_window.all_btn, TRUE);
263 gtk_widget_set_sensitive(search_window.prev_btn, TRUE);
264 gtk_widget_set_sensitive(search_window.next_btn, TRUE);
265 gtk_widget_set_sensitive(search_window.adv_search_checkbtn, TRUE);
268 static void summary_search_create(void)
272 GtkWidget *bool_hbox;
273 GtkWidget *bool_optmenu;
276 GtkWidget *clear_btn;
279 GtkWidget *from_label;
280 GtkWidget *from_entry;
283 GtkWidget *subject_label;
284 GtkWidget *subject_entry;
285 GtkWidget *body_label;
286 GtkWidget *body_entry;
287 GtkWidget *adv_condition_label;
288 GtkWidget *adv_condition_entry;
289 GtkWidget *adv_condition_btn;
291 GtkWidget *checkbtn_hbox;
292 GtkWidget *adv_search_checkbtn;
293 GtkWidget *case_checkbtn;
295 GtkWidget *confirm_area;
300 GtkWidget *close_btn;
302 gboolean is_searching = FALSE;
304 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "summary_search");
305 gtk_window_set_title(GTK_WINDOW (window), _("Search messages"));
306 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
307 gtk_container_set_border_width(GTK_CONTAINER (window), 8);
308 g_signal_connect(G_OBJECT(window), "delete_event",
309 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
310 g_signal_connect(G_OBJECT(window), "key_press_event",
311 G_CALLBACK(key_pressed), NULL);
312 MANAGE_WINDOW_SIGNALS_CONNECT(window);
314 vbox1 = gtk_vbox_new (FALSE, 0);
315 gtk_widget_show (vbox1);
316 gtk_container_add (GTK_CONTAINER (window), vbox1);
318 bool_hbox = gtk_hbox_new(FALSE, 4);
319 gtk_widget_show(bool_hbox);
320 gtk_box_pack_start(GTK_BOX(vbox1), bool_hbox, FALSE, FALSE, 0);
322 bool_optmenu = gtkut_sc_combobox_create(NULL, FALSE);
323 menu = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(bool_optmenu)));
324 gtk_widget_show(bool_optmenu);
325 gtk_box_pack_start(GTK_BOX(bool_hbox), bool_optmenu, FALSE, FALSE, 0);
327 COMBOBOX_ADD(menu, _("Match any of the following"), 0);
328 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(bool_optmenu), &iter);
329 COMBOBOX_ADD(menu, _("Match all of the following"), 1);
330 g_signal_connect(G_OBJECT(bool_optmenu), "changed",
331 G_CALLBACK(optmenu_changed), NULL);
333 clear_btn = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
334 gtk_widget_show(clear_btn);
335 gtk_box_pack_end(GTK_BOX(bool_hbox), clear_btn, FALSE, FALSE, 0);
337 table1 = gtk_table_new (5, 3, FALSE);
338 gtk_widget_show (table1);
339 gtk_box_pack_start (GTK_BOX (vbox1), table1, TRUE, TRUE, 0);
340 gtk_container_set_border_width (GTK_CONTAINER (table1), 4);
341 gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
342 gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
344 #if !GTK_CHECK_VERSION(2, 24, 0)
345 from_entry = gtk_combo_box_entry_new_text ();
347 from_entry = gtk_combo_box_text_new_with_entry ();
349 gtk_combo_box_set_active(GTK_COMBO_BOX(from_entry), -1);
350 if (prefs_common.summary_search_from_history)
351 #if !GTK_CHECK_VERSION(2, 24, 0)
352 combobox_set_popdown_strings(GTK_COMBO_BOX(from_entry),
353 prefs_common.summary_search_from_history);
355 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(from_entry),
356 prefs_common.summary_search_from_history);
358 gtk_widget_show (from_entry);
359 gtk_table_attach (GTK_TABLE (table1), from_entry, 1, 3, 0, 1,
360 GTK_EXPAND|GTK_FILL, 0, 0, 0);
361 g_signal_connect(G_OBJECT(from_entry), "changed",
362 G_CALLBACK(from_changed), NULL);
363 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((from_entry)))),
364 "focus_in_event", G_CALLBACK(from_entry_focus_evt_in), NULL);
365 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((from_entry)))),
366 "focus_out_event", G_CALLBACK(from_entry_focus_evt_out), NULL);
368 #if !GTK_CHECK_VERSION(2, 24, 0)
369 to_entry = gtk_combo_box_entry_new_text ();
371 to_entry = gtk_combo_box_text_new_with_entry ();
373 gtk_combo_box_set_active(GTK_COMBO_BOX(to_entry), -1);
374 if (prefs_common.summary_search_to_history)
375 #if !GTK_CHECK_VERSION(2, 24, 0)
376 combobox_set_popdown_strings(GTK_COMBO_BOX(to_entry),
377 prefs_common.summary_search_to_history);
379 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(to_entry),
380 prefs_common.summary_search_to_history);
382 gtk_widget_show (to_entry);
383 gtk_table_attach (GTK_TABLE (table1), to_entry, 1, 3, 1, 2,
384 GTK_EXPAND|GTK_FILL, 0, 0, 0);
385 g_signal_connect(G_OBJECT(to_entry), "changed",
386 G_CALLBACK(to_changed), NULL);
387 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((to_entry)))),
388 "focus_in_event", G_CALLBACK(to_entry_focus_evt_in), NULL);
389 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((to_entry)))),
390 "focus_out_event", G_CALLBACK(to_entry_focus_evt_out), NULL);
392 #if !GTK_CHECK_VERSION(2, 24, 0)
393 subject_entry = gtk_combo_box_entry_new_text ();
395 subject_entry = gtk_combo_box_text_new_with_entry ();
397 gtk_combo_box_set_active(GTK_COMBO_BOX(subject_entry), -1);
398 if (prefs_common.summary_search_subject_history)
399 #if !GTK_CHECK_VERSION(2, 24, 0)
400 combobox_set_popdown_strings(GTK_COMBO_BOX(subject_entry),
401 prefs_common.summary_search_subject_history);
403 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(subject_entry),
404 prefs_common.summary_search_subject_history);
406 gtk_widget_show (subject_entry);
407 gtk_table_attach (GTK_TABLE (table1), subject_entry, 1, 3, 2, 3,
408 GTK_EXPAND|GTK_FILL, 0, 0, 0);
409 g_signal_connect(G_OBJECT(subject_entry), "changed",
410 G_CALLBACK(subject_changed), NULL);
411 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((subject_entry)))),
412 "focus_in_event", G_CALLBACK(subject_entry_focus_evt_in), NULL);
413 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((subject_entry)))),
414 "focus_out_event", G_CALLBACK(subject_entry_focus_evt_out), NULL);
416 #if !GTK_CHECK_VERSION(2, 24, 0)
417 body_entry = gtk_combo_box_entry_new_text ();
419 body_entry = gtk_combo_box_text_new_with_entry ();
421 gtk_combo_box_set_active(GTK_COMBO_BOX(body_entry), -1);
422 if (prefs_common.summary_search_body_history)
423 #if !GTK_CHECK_VERSION(2, 24, 0)
424 combobox_set_popdown_strings(GTK_COMBO_BOX(body_entry),
425 prefs_common.summary_search_body_history);
427 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(body_entry),
428 prefs_common.summary_search_body_history);
430 gtk_widget_show (body_entry);
431 gtk_table_attach (GTK_TABLE (table1), body_entry, 1, 3, 3, 4,
432 GTK_EXPAND|GTK_FILL, 0, 0, 0);
433 g_signal_connect(G_OBJECT(body_entry), "changed",
434 G_CALLBACK(body_changed), NULL);
435 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
436 "focus_in_event", G_CALLBACK(body_entry_focus_evt_in), NULL);
437 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
438 "focus_out_event", G_CALLBACK(body_entry_focus_evt_out), NULL);
440 #if !GTK_CHECK_VERSION(2, 24, 0)
441 adv_condition_entry = gtk_combo_box_entry_new_text ();
443 adv_condition_entry = gtk_combo_box_text_new_with_entry ();
445 gtk_combo_box_set_active(GTK_COMBO_BOX(adv_condition_entry), -1);
446 if (prefs_common.summary_search_adv_condition_history)
447 #if !GTK_CHECK_VERSION(2, 24, 0)
448 combobox_set_popdown_strings(GTK_COMBO_BOX(adv_condition_entry),
449 prefs_common.summary_search_adv_condition_history);
451 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(adv_condition_entry),
452 prefs_common.summary_search_adv_condition_history);
454 gtk_widget_show (adv_condition_entry);
455 gtk_table_attach (GTK_TABLE (table1), adv_condition_entry, 1, 2, 4, 5,
456 GTK_EXPAND|GTK_FILL, 0, 0, 0);
457 g_signal_connect(G_OBJECT(adv_condition_entry), "changed",
458 G_CALLBACK(adv_condition_changed), NULL);
459 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((adv_condition_entry)))),
460 "focus_in_event", G_CALLBACK(adv_condition_entry_focus_evt_in), NULL);
461 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((adv_condition_entry)))),
462 "focus_out_event", G_CALLBACK(adv_condition_entry_focus_evt_out), NULL);
464 adv_condition_btn = gtk_button_new_with_label(" ... ");
465 gtk_widget_show (adv_condition_btn);
466 gtk_table_attach (GTK_TABLE (table1), adv_condition_btn, 2, 3, 4, 5,
468 g_signal_connect(G_OBJECT (adv_condition_btn), "clicked",
469 G_CALLBACK(adv_condition_btn_clicked), search_window.window);
471 CLAWS_SET_TIP(adv_condition_btn,
472 _("Edit search criteria"));
474 from_label = gtk_label_new (_("From:"));
475 gtk_widget_show (from_label);
476 gtk_table_attach (GTK_TABLE (table1), from_label, 0, 1, 0, 1,
478 gtk_label_set_justify (GTK_LABEL (from_label), GTK_JUSTIFY_RIGHT);
479 gtk_misc_set_alignment (GTK_MISC (from_label), 1, 0.5);
481 to_label = gtk_label_new (_("To:"));
482 gtk_widget_show (to_label);
483 gtk_table_attach (GTK_TABLE (table1), to_label, 0, 1, 1, 2,
485 gtk_label_set_justify (GTK_LABEL (to_label), GTK_JUSTIFY_RIGHT);
486 gtk_misc_set_alignment (GTK_MISC (to_label), 1, 0.5);
488 subject_label = gtk_label_new (_("Subject:"));
489 gtk_widget_show (subject_label);
490 gtk_table_attach (GTK_TABLE (table1), subject_label, 0, 1, 2, 3,
492 gtk_label_set_justify (GTK_LABEL (subject_label), GTK_JUSTIFY_RIGHT);
493 gtk_misc_set_alignment (GTK_MISC (subject_label), 1, 0.5);
495 body_label = gtk_label_new (_("Body:"));
496 gtk_widget_show (body_label);
497 gtk_table_attach (GTK_TABLE (table1), body_label, 0, 1, 3, 4,
499 gtk_label_set_justify (GTK_LABEL (body_label), GTK_JUSTIFY_RIGHT);
500 gtk_misc_set_alignment (GTK_MISC (body_label), 1, 0.5);
502 adv_condition_label = gtk_label_new (_("Condition:"));
503 gtk_widget_show (adv_condition_label);
504 gtk_table_attach (GTK_TABLE (table1), adv_condition_label, 0, 1, 4, 5,
506 gtk_label_set_justify (GTK_LABEL (adv_condition_label), GTK_JUSTIFY_RIGHT);
507 gtk_misc_set_alignment (GTK_MISC (adv_condition_label), 1, 0.5);
509 checkbtn_hbox = gtk_hbox_new (FALSE, 8);
510 gtk_widget_show (checkbtn_hbox);
511 gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
512 gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
514 case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
515 gtk_widget_show (case_checkbtn);
516 gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
518 g_signal_connect(G_OBJECT(case_checkbtn), "changed",
519 G_CALLBACK(case_changed), NULL);
521 adv_search_checkbtn = gtk_check_button_new_with_label (_("Extended Search"));
522 gtk_widget_show (adv_search_checkbtn);
523 gtk_box_pack_start (GTK_BOX (checkbtn_hbox), adv_search_checkbtn,
525 g_signal_connect(G_OBJECT(adv_search_checkbtn), "changed",
526 G_CALLBACK(case_changed), NULL);
528 confirm_area = gtk_hbutton_box_new();
529 gtk_widget_show (confirm_area);
530 gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
532 gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
534 gtkut_stock_button_add_help(confirm_area, &help_btn);
536 all_btn = gtk_button_new_with_mnemonic(_("Find _all"));
537 gtkut_widget_set_can_default(all_btn, TRUE);
538 gtk_box_pack_start(GTK_BOX(confirm_area), all_btn, TRUE, TRUE, 0);
539 gtk_widget_show(all_btn);
541 prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
542 gtkut_widget_set_can_default(prev_btn, TRUE);
543 gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
544 gtk_widget_show(prev_btn);
546 next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
547 gtkut_widget_set_can_default(next_btn, TRUE);
548 gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
549 gtk_widget_show(next_btn);
551 close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
552 gtkut_widget_set_can_default(close_btn, TRUE);
553 gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
554 gtk_widget_show(close_btn);
556 /* stop button hidden */
557 stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
558 gtkut_widget_set_can_default(stop_btn, TRUE);
559 gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
561 gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
562 gtk_widget_grab_default(next_btn);
564 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, bool_optmenu)
565 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, from_label)
566 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, from_entry)
567 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, to_label)
568 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, to_entry)
569 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, subject_label)
570 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, subject_entry)
571 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, body_label)
572 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, body_entry)
573 SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_label)
574 SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_entry)
575 SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_btn)
576 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, case_checkbtn)
578 g_signal_connect(G_OBJECT(help_btn), "clicked",
579 G_CALLBACK(manual_open_with_anchor_cb),
580 MANUAL_ANCHOR_SEARCHING);
581 g_signal_connect(G_OBJECT(clear_btn), "clicked",
582 G_CALLBACK(summary_search_clear), NULL);
583 g_signal_connect(G_OBJECT(all_btn), "clicked",
584 G_CALLBACK(summary_search_all_clicked), NULL);
585 g_signal_connect(G_OBJECT(prev_btn), "clicked",
586 G_CALLBACK(summary_search_prev_clicked), NULL);
587 g_signal_connect(G_OBJECT(next_btn), "clicked",
588 G_CALLBACK(summary_search_next_clicked), NULL);
589 g_signal_connect_closure
590 (G_OBJECT(close_btn), "clicked",
591 g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
592 window, NULL), FALSE);
593 g_signal_connect(G_OBJECT(stop_btn), "clicked",
594 G_CALLBACK(summary_search_stop_clicked), NULL);
596 search_window.window = window;
597 search_window.bool_optmenu = bool_optmenu;
598 search_window.from_label = from_label;
599 search_window.from_entry = from_entry;
600 search_window.to_label = to_label;
601 search_window.to_entry = to_entry;
602 search_window.subject_label = subject_label;
603 search_window.subject_entry = subject_entry;
604 search_window.body_label = body_label;
605 search_window.body_entry = body_entry;
606 search_window.adv_condition_label = adv_condition_label;
607 search_window.adv_condition_entry = adv_condition_entry;
608 search_window.adv_condition_btn = adv_condition_btn;
609 search_window.case_checkbtn = case_checkbtn;
610 search_window.adv_search_checkbtn = adv_search_checkbtn;
611 search_window.clear_btn = clear_btn;
612 search_window.help_btn = help_btn;
613 search_window.all_btn = all_btn;
614 search_window.prev_btn = prev_btn;
615 search_window.next_btn = next_btn;
616 search_window.close_btn = close_btn;
617 search_window.stop_btn = stop_btn;
618 search_window.advsearch = NULL;
619 search_window.matcher_is_outdated = TRUE;
620 search_window.search_in_progress = FALSE;
621 search_window.matched_msgnums = NULL;
622 search_window.is_searching = is_searching;
625 static gboolean summary_search_verify_match(MsgInfo *msg)
627 gpointer msgnum = GUINT_TO_POINTER(msg->msgnum);
629 if (g_hash_table_lookup(search_window.matched_msgnums, msgnum) != NULL)
635 static gboolean summary_search_progress_cb(gpointer data, guint at, guint matched, guint total)
637 if (!search_window.is_searching) {
638 search_window.matcher_is_outdated = TRUE;
642 return summaryview_search_root_progress(search_window.summaryview, at, matched, total);
645 static gboolean summary_search_prepare_matcher()
648 gboolean bool_and = FALSE;
649 gboolean case_sens = FALSE;
652 gchar *from_str = NULL, *to_str = NULL, *subject_str = NULL;
653 gchar *body_str = NULL;
654 GSList *matchers = NULL;
656 if (!search_window.matcher_is_outdated)
659 if (search_window.advsearch == NULL) {
660 search_window.advsearch = advsearch_new();
661 advsearch_set_on_error_cb(search_window.advsearch, NULL, NULL); /* TODO */
662 advsearch_set_on_progress_cb(search_window.advsearch,
663 summary_search_progress_cb, NULL);
666 adv_search = gtk_toggle_button_get_active
667 (GTK_TOGGLE_BUTTON(search_window.adv_search_checkbtn));
670 matcher_str = add_history_get(search_window.adv_condition_entry, &prefs_common.summary_search_adv_condition_history);
672 MatcherList *matcher_list;
673 bool_and = combobox_get_active_data(GTK_COMBO_BOX(search_window.bool_optmenu));
674 case_sens = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
676 from_str = add_history_get(search_window.from_entry, &prefs_common.summary_search_from_history);
677 to_str = add_history_get(search_window.to_entry, &prefs_common.summary_search_to_history);
678 subject_str = add_history_get(search_window.subject_entry, &prefs_common.summary_search_subject_history);
679 body_str = add_history_get(search_window.body_entry, &prefs_common.summary_search_body_history);
681 if (!from_str && !to_str && !subject_str && !body_str) {
682 /* TODO: warn if no search criteria? (or make buttons enabled only when
683 * at least one search criteria has been set */
687 match_type = case_sens ? MATCHTYPE_MATCH : MATCHTYPE_MATCHCASE;
690 MatcherProp *prop = matcherprop_new(MATCHCRITERIA_FROM, NULL, match_type, from_str, 0);
691 matchers = g_slist_append(matchers, prop);
694 MatcherProp *prop = matcherprop_new(MATCHCRITERIA_TO, NULL, match_type, to_str, 0);
695 matchers = g_slist_append(matchers, prop);
698 MatcherProp *prop = matcherprop_new(MATCHCRITERIA_SUBJECT, NULL, match_type, subject_str, 0);
699 matchers = g_slist_append(matchers, prop);
702 MatcherProp *prop = matcherprop_new(MATCHCRITERIA_BODY_PART, NULL, match_type, body_str, 0);
703 matchers = g_slist_append(matchers, prop);
710 matcher_list = matcherlist_new(matchers, bool_and);
714 matcher_str = matcherlist_to_string(matcher_list);
715 matcherlist_free(matcher_list);
720 advsearch_set(search_window.advsearch, ADVANCED_SEARCH_EXTENDED,
723 debug_print("Advsearch set: %s\n", matcher_str);
726 if (!advsearch_has_proper_predicate(search_window.advsearch))
729 search_window.matcher_is_outdated = FALSE;
734 static gboolean summary_search_prereduce_msg_list()
736 MsgInfoList *msglist = NULL;
737 MsgNumberList *msgnums = NULL;
739 SummaryView *summaryview = search_window.summaryview;
741 FolderItem *item = summaryview->folder_item;
742 static GdkCursor *watch_cursor = NULL;
744 watch_cursor = gdk_cursor_new(GDK_WATCH);
746 if (search_window.matcher_is_outdated && !summary_search_prepare_matcher())
749 main_window_cursor_wait(mainwindow_get_mainwindow());
750 gdk_window_set_cursor(gtk_widget_get_window(search_window.window), watch_cursor);
751 statusbar_print_all(_("Searching in %s... \n"),
752 item->path ? item->path : "(null)");
754 result = advsearch_search_msgs_in_folders(search_window.advsearch,
755 &msglist, item, FALSE);
757 statusbar_progress_all(0, 0, 0);
758 gdk_window_set_cursor(gtk_widget_get_window(search_window.window), NULL);
759 main_window_cursor_normal(mainwindow_get_mainwindow());
764 msgnums = procmsg_get_number_list_for_msgs(msglist);
765 procmsg_msg_list_free(msglist);
767 if (search_window.matched_msgnums == NULL)
768 search_window.matched_msgnums = g_hash_table_new(g_direct_hash, NULL);
770 g_hash_table_remove_all(search_window.matched_msgnums);
772 for (cur = msgnums; cur != NULL; cur = cur->next)
773 g_hash_table_insert(search_window.matched_msgnums, cur->data, GINT_TO_POINTER(1));
775 g_slist_free(msgnums);
780 static void summary_search_execute(gboolean backward, gboolean search_all)
782 SummaryView *summaryview = search_window.summaryview;
783 GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
784 GtkCMCTreeNode *node;
786 gboolean all_searched = FALSE;
787 gboolean matched = FALSE;
790 if (summary_is_locked(summaryview))
793 summary_lock(summaryview);
795 search_window.is_searching = TRUE;
796 main_window_cursor_wait(summaryview->mainwin);
797 summary_show_stop_button();
799 if (search_window.matcher_is_outdated && !summary_search_prereduce_msg_list())
803 summary_freeze(summaryview);
804 summary_unselect_all(summaryview);
805 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
807 } else if (!summaryview->selected) {
808 /* no selection, search from from list start */
810 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list_end);
812 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
815 search_window.is_searching = FALSE;
816 summary_hide_stop_button();
817 main_window_cursor_normal(summaryview->mainwin);
818 summary_unlock(summaryview);
822 /* search from current selection */
824 node = gtkut_ctree_node_prev(ctree, summaryview->selected);
826 node = gtkut_ctree_node_next(ctree, summaryview->selected);
829 for (; search_window.is_searching; i++) {
838 alertpanel_full(_("Search failed"),
839 _("Search string not found."),
840 GTK_STOCK_CLOSE, NULL, NULL, FALSE,
841 NULL, ALERT_WARNING, G_ALERTDEFAULT);
846 str = _("Beginning of list reached; continue from end?");
848 str = _("End of list reached; continue from beginning?");
850 val = alertpanel(_("Search finished"), str,
851 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
852 if (G_ALERTALTERNATE == val) {
854 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list_end);
856 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
861 manage_window_focus_in(search_window.window, NULL, NULL);
866 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
869 matched = summary_search_verify_match(msginfo);
875 gtk_cmctree_select(ctree, node);
877 summary_unlock(summaryview);
878 summary_select_node(summaryview, node,
879 OPEN_SELECTED_ON_SEARCH_RESULTS);
880 summary_lock(summaryview);
885 if (i % (search_window.is_fast ? 1000 : 100) == 0)
888 node = backward ? gtkut_ctree_node_prev(ctree, node)
889 : gtkut_ctree_node_next(ctree, node);
893 search_window.is_searching = FALSE;
894 summary_hide_stop_button();
895 main_window_cursor_normal(summaryview->mainwin);
897 summary_thaw_with_status(summaryview);
898 summary_unlock(summaryview);
901 static void summary_search_clear(GtkButton *button, gpointer data)
903 if (gtk_toggle_button_get_active
904 (GTK_TOGGLE_BUTTON(search_window.adv_search_checkbtn))) {
905 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.adv_condition_entry)))), "");
907 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.from_entry)))), "");
908 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.to_entry)))), "");
909 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.subject_entry)))), "");
910 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.body_entry)))), "");
913 if (search_window.is_searching) {
914 search_window.is_searching = FALSE;
916 search_window.matcher_is_outdated = TRUE;
919 static void summary_search_prev_clicked(GtkButton *button, gpointer data)
921 summary_search_execute(TRUE, FALSE);
924 static void summary_search_next_clicked(GtkButton *button, gpointer data)
926 summary_search_execute(FALSE, FALSE);
929 static void summary_search_all_clicked(GtkButton *button, gpointer data)
931 summary_search_execute(FALSE, TRUE);
934 static void adv_condition_btn_done(MatcherList * matchers)
939 mainwindow_get_mainwindow()->summaryview->quicksearch != NULL);
941 if (matchers == NULL) {
945 str = matcherlist_to_string(matchers);
946 search_window.matcher_is_outdated = TRUE;
949 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.adv_condition_entry)))), str);
954 static void summary_search_stop_clicked(GtkButton *button, gpointer data)
956 search_window.is_searching = FALSE;
959 static void adv_condition_btn_clicked(GtkButton *button, gpointer data)
961 const gchar * cond_str;
962 MatcherList * matchers = NULL;
964 cm_return_if_fail( search_window.window != NULL );
966 /* re-use the current search value if it's a condition expression,
967 otherwise ignore it silently */
968 #if !GTK_CHECK_VERSION(2, 24, 0)
969 cond_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.adv_condition_entry));
971 cond_str = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(search_window.adv_condition_entry));
973 if (cond_str && *cond_str != '\0') {
974 matchers = matcher_parser_get_cond((gchar*)cond_str, NULL);
977 prefs_matcher_open(matchers, adv_condition_btn_done);
979 if (matchers != NULL) {
980 matcherlist_free(matchers);
984 static void optmenu_changed(GtkComboBox *widget, gpointer user_data)
986 search_window.matcher_is_outdated = TRUE;
989 static void from_changed(void)
991 if (!search_window.from_entry_has_focus)
992 gtk_widget_grab_focus(search_window.from_entry);
993 search_window.matcher_is_outdated = TRUE;
996 static void to_changed(void)
998 if (!search_window.to_entry_has_focus)
999 gtk_widget_grab_focus(search_window.to_entry);
1000 search_window.matcher_is_outdated = TRUE;
1003 static void subject_changed(void)
1005 if (!search_window.subject_entry_has_focus)
1006 gtk_widget_grab_focus(search_window.subject_entry);
1007 search_window.matcher_is_outdated = TRUE;
1010 static void body_changed(void)
1012 if (!search_window.body_entry_has_focus)
1013 gtk_widget_grab_focus(search_window.body_entry);
1014 search_window.matcher_is_outdated = TRUE;
1017 static void adv_condition_changed(void)
1019 if (!search_window.adv_condition_entry_has_focus)
1020 gtk_widget_grab_focus(search_window.adv_condition_entry);
1021 search_window.matcher_is_outdated = TRUE;
1024 static void case_changed(GtkToggleButton *togglebutton, gpointer user_data)
1026 search_window.matcher_is_outdated = TRUE;
1029 static void adv_search_changed(GtkToggleButton *togglebutton, gpointer user_data)
1031 search_window.matcher_is_outdated = TRUE;
1034 static gboolean from_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
1037 search_window.from_entry_has_focus = TRUE;
1041 static gboolean from_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
1044 search_window.from_entry_has_focus = FALSE;
1048 static gboolean to_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
1051 search_window.to_entry_has_focus = TRUE;
1055 static gboolean to_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
1058 search_window.to_entry_has_focus = FALSE;
1062 static gboolean subject_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
1065 search_window.subject_entry_has_focus = TRUE;
1069 static gboolean subject_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
1072 search_window.subject_entry_has_focus = FALSE;
1076 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
1079 search_window.body_entry_has_focus = TRUE;
1083 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
1086 search_window.body_entry_has_focus = FALSE;
1090 static gboolean adv_condition_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
1093 search_window.adv_condition_entry_has_focus = TRUE;
1097 static gboolean adv_condition_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
1100 search_window.adv_condition_entry_has_focus = FALSE;
1104 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
1107 if (event && (event->keyval == GDK_KEY_Escape)) {
1109 - stop a running search
1110 - close the search window if no search is running
1112 if (!search_window.is_searching) {
1113 gtk_widget_hide(search_window.window);
1115 search_window.is_searching = FALSE;
1119 if (event && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
1120 if (!search_window.is_searching) {
1121 summary_search_execute(FALSE, FALSE);
1125 if (event && (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_Up)) {
1126 if (search_window.from_entry_has_focus) {
1127 combobox_set_value_from_arrow_key(
1128 GTK_COMBO_BOX(search_window.from_entry),
1132 if (search_window.to_entry_has_focus) {
1133 combobox_set_value_from_arrow_key(
1134 GTK_COMBO_BOX(search_window.to_entry),
1138 if (search_window.subject_entry_has_focus) {
1139 combobox_set_value_from_arrow_key(
1140 GTK_COMBO_BOX(search_window.subject_entry),
1144 if (search_window.body_entry_has_focus) {
1145 combobox_set_value_from_arrow_key(
1146 GTK_COMBO_BOX(search_window.body_entry),
1150 if (search_window.adv_condition_entry_has_focus) {
1151 combobox_set_value_from_arrow_key(
1152 GTK_COMBO_BOX(search_window.adv_condition_entry),