image viewer: don't make the 'load image' button larger than necessary
[claws.git] / src / summary_search.c
blob82363a92eac5435798b059d10bda3c30410a19e6
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2022 the Claws Mail team and Hiroyuki Yamamoto
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #include "defs.h"
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtk.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include "main.h"
35 #include "summary_search.h"
36 #include "summaryview.h"
37 #include "messageview.h"
38 #include "mainwindow.h"
39 #include "menu.h"
40 #include "utils.h"
41 #include "gtkutils.h"
42 #include "combobox.h"
43 #include "prefs_gtk.h"
44 #include "manage_window.h"
45 #include "alertpanel.h"
46 #include "advsearch.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 #include "statusbar.h"
54 static struct SummarySearchWindow {
55 GtkWidget *window;
57 GtkWidget *bool_optmenu;
59 GtkWidget *from_label;
60 GtkWidget *from_entry;
61 GtkWidget *to_label;
62 GtkWidget *to_entry;
63 GtkWidget *subject_label;
64 GtkWidget *subject_entry;
65 GtkWidget *body_label;
66 GtkWidget *body_entry;
68 GtkWidget *adv_condition_label;
69 GtkWidget *adv_condition_entry;
70 GtkWidget *adv_condition_btn;
71 GtkWidget *adv_search_checkbtn;
73 GtkWidget *case_checkbtn;
75 GtkWidget *clear_btn;
76 GtkWidget *help_btn;
77 GtkWidget *all_btn;
78 GtkWidget *prev_btn;
79 GtkWidget *next_btn;
80 GtkWidget *close_btn;
81 GtkWidget *stop_btn;
83 SummaryView *summaryview;
85 AdvancedSearch *advsearch;
86 gboolean is_fast;
87 gboolean matcher_is_outdated;
88 gboolean search_in_progress;
89 GHashTable *matched_msgnums;
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;
99 static gchar* add_history_get(GtkWidget *from, GList **history);
101 static void summary_search_create (void);
103 static gboolean summary_search_verify_match (MsgInfo *msg);
104 static gboolean summary_search_prepare_matcher ();
105 static gboolean summary_search_prereduce_msg_list ();
107 static void summary_search_execute (gboolean backward,
108 gboolean search_all);
110 static void summary_search_clear (GtkButton *button,
111 gpointer data);
112 static void summary_search_prev_clicked (GtkButton *button,
113 gpointer data);
114 static void summary_search_next_clicked (GtkButton *button,
115 gpointer data);
116 static void summary_search_all_clicked (GtkButton *button,
117 gpointer data);
118 static void summary_search_stop_clicked (GtkButton *button,
119 gpointer data);
120 static void adv_condition_btn_clicked (GtkButton *button,
121 gpointer data);
123 static void optmenu_changed (GtkComboBox *widget, gpointer user_data);
124 static void from_changed (void);
125 static void to_changed (void);
126 static void subject_changed (void);
127 static void body_changed (void);
128 static void adv_condition_changed (void);
129 static void case_changed (GtkToggleButton *togglebutton, gpointer user_data);
131 static gboolean from_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
132 gpointer data);
133 static gboolean from_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
134 gpointer data);
135 static gboolean to_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
136 gpointer data);
137 static gboolean to_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
138 gpointer data);
139 static gboolean subject_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
140 gpointer data);
141 static gboolean subject_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
142 gpointer data);
143 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
144 gpointer data);
145 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
146 gpointer data);
147 static gboolean adv_condition_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
148 gpointer data);
149 static gboolean adv_condition_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
150 gpointer data);
151 static gboolean key_pressed (GtkWidget *widget,
152 GdkEventKey *event,
153 gpointer data);
155 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) { \
156 gtk_widget_set_sensitive(widget, sensitive); \
159 static gchar* add_history_get(GtkWidget *from, GList **history)
161 gchar *result;
163 result = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(from));
164 if (!result)
165 result = gtk_editable_get_chars(GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(from))), 0, -1);
167 if (result && result[0] != '\0') {
168 /* add to history */
169 combobox_unset_popdown_strings(GTK_COMBO_BOX_TEXT(from));
170 *history = add_history(*history, result);
171 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(from), *history);
173 return result;
174 } else {
175 g_free(result);
176 return NULL;
180 void summary_search(SummaryView *summaryview)
182 if (!search_window.window) {
183 summary_search_create();
184 } else {
185 gtk_widget_hide(search_window.window);
188 search_window.summaryview = summaryview;
190 gtk_widget_grab_focus(search_window.next_btn);
191 gtk_widget_grab_focus(search_window.subject_entry);
192 gtk_widget_show(search_window.window);
195 static void summary_show_stop_button(void)
197 gtk_widget_hide(search_window.close_btn);
198 gtk_widget_show(search_window.stop_btn);
199 GTK_BUTTON_SET_SENSITIVE(search_window.clear_btn, FALSE)
200 GTK_BUTTON_SET_SENSITIVE(search_window.all_btn, FALSE)
201 GTK_BUTTON_SET_SENSITIVE(search_window.prev_btn, FALSE)
202 GTK_BUTTON_SET_SENSITIVE(search_window.next_btn, FALSE)
203 gtk_widget_set_sensitive(search_window.adv_condition_label, FALSE);
204 gtk_widget_set_sensitive(search_window.adv_condition_entry, FALSE);
205 gtk_widget_set_sensitive(search_window.adv_condition_btn, FALSE);
206 gtk_widget_set_sensitive(search_window.to_label, FALSE);
207 gtk_widget_set_sensitive(search_window.to_entry, FALSE);
208 gtk_widget_set_sensitive(search_window.from_label, FALSE);
209 gtk_widget_set_sensitive(search_window.from_entry, FALSE);
210 gtk_widget_set_sensitive(search_window.subject_label, FALSE);
211 gtk_widget_set_sensitive(search_window.subject_entry, FALSE);
212 gtk_widget_set_sensitive(search_window.body_label, FALSE);
213 gtk_widget_set_sensitive(search_window.body_entry, FALSE);
214 gtk_widget_set_sensitive(search_window.bool_optmenu, FALSE);
215 gtk_widget_set_sensitive(search_window.clear_btn, FALSE);
216 gtk_widget_set_sensitive(search_window.case_checkbtn, FALSE);
217 gtk_widget_set_sensitive(search_window.adv_search_checkbtn, FALSE);
220 static void summary_hide_stop_button(void)
222 GTK_BUTTON_SET_SENSITIVE(search_window.clear_btn, TRUE)
223 gtk_widget_hide(search_window.stop_btn);
224 gtk_widget_show(search_window.close_btn);
225 if (gtk_toggle_button_get_active
226 (GTK_TOGGLE_BUTTON(search_window.adv_search_checkbtn))) {
227 gtk_widget_set_sensitive(search_window.adv_condition_label, TRUE);
228 gtk_widget_set_sensitive(search_window.adv_condition_entry, TRUE);
229 gtk_widget_set_sensitive(search_window.adv_condition_btn, TRUE);
230 gtk_widget_set_sensitive(search_window.case_checkbtn, FALSE);
231 gtk_widget_set_sensitive(search_window.bool_optmenu, FALSE);
232 } else {
233 gtk_widget_set_sensitive(search_window.to_label, TRUE);
234 gtk_widget_set_sensitive(search_window.to_entry, TRUE);
235 gtk_widget_set_sensitive(search_window.from_label, TRUE);
236 gtk_widget_set_sensitive(search_window.from_entry, TRUE);
237 gtk_widget_set_sensitive(search_window.subject_label, TRUE);
238 gtk_widget_set_sensitive(search_window.subject_entry, TRUE);
239 gtk_widget_set_sensitive(search_window.body_label, TRUE);
240 gtk_widget_set_sensitive(search_window.body_entry, TRUE);
241 gtk_widget_set_sensitive(search_window.case_checkbtn, TRUE);
242 gtk_widget_set_sensitive(search_window.bool_optmenu, TRUE);
244 gtk_widget_set_sensitive(search_window.clear_btn, TRUE);
245 gtk_widget_set_sensitive(search_window.all_btn, TRUE);
246 gtk_widget_set_sensitive(search_window.prev_btn, TRUE);
247 gtk_widget_set_sensitive(search_window.next_btn, TRUE);
248 gtk_widget_set_sensitive(search_window.adv_search_checkbtn, TRUE);
251 static void summary_search_create(void)
253 GtkWidget *window;
254 GtkWidget *vbox1;
255 GtkWidget *bool_hbox;
256 GtkWidget *bool_optmenu;
257 GtkListStore *menu;
258 GtkTreeIter iter;
259 GtkWidget *clear_btn;
261 GtkWidget *table1;
262 GtkWidget *from_label;
263 GtkWidget *from_entry;
264 GtkWidget *to_label;
265 GtkWidget *to_entry;
266 GtkWidget *subject_label;
267 GtkWidget *subject_entry;
268 GtkWidget *body_label;
269 GtkWidget *body_entry;
270 GtkWidget *adv_condition_label;
271 GtkWidget *adv_condition_entry;
272 GtkWidget *adv_condition_btn;
274 GtkWidget *checkbtn_hbox;
275 GtkWidget *adv_search_checkbtn;
276 GtkWidget *case_checkbtn;
278 GtkWidget *confirm_area;
279 GtkWidget *help_btn;
280 GtkWidget *all_btn;
281 GtkWidget *prev_btn;
282 GtkWidget *next_btn;
283 GtkWidget *close_btn;
284 GtkWidget *stop_btn;
285 gboolean is_searching = FALSE;
287 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "summary_search");
288 gtk_window_set_title(GTK_WINDOW (window), _("Search messages"));
289 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
290 gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
291 gtk_container_set_border_width(GTK_CONTAINER (window), 8);
292 g_signal_connect(G_OBJECT(window), "delete_event",
293 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
294 g_signal_connect(G_OBJECT(window), "key_press_event",
295 G_CALLBACK(key_pressed), NULL);
296 MANAGE_WINDOW_SIGNALS_CONNECT(window);
298 vbox1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
299 gtk_widget_show (vbox1);
300 gtk_container_add (GTK_CONTAINER (window), vbox1);
302 bool_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
303 gtk_widget_show(bool_hbox);
304 gtk_box_pack_start(GTK_BOX(vbox1), bool_hbox, FALSE, FALSE, 0);
306 bool_optmenu = gtkut_sc_combobox_create(NULL, FALSE);
307 menu = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(bool_optmenu)));
308 gtk_widget_show(bool_optmenu);
309 gtk_box_pack_start(GTK_BOX(bool_hbox), bool_optmenu, FALSE, FALSE, 0);
311 COMBOBOX_ADD(menu, _("Match any of the following"), 0);
312 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(bool_optmenu), &iter);
313 COMBOBOX_ADD(menu, _("Match all of the following"), 1);
314 g_signal_connect(G_OBJECT(bool_optmenu), "changed",
315 G_CALLBACK(optmenu_changed), NULL);
317 clear_btn = gtkut_stock_button("edit-clear", _("C_lear"));
318 gtk_widget_show(clear_btn);
319 gtk_box_pack_end(GTK_BOX(bool_hbox), clear_btn, FALSE, FALSE, 0);
321 table1 = gtk_grid_new();
322 gtk_widget_show (table1);
323 gtk_box_pack_start (GTK_BOX (vbox1), table1, TRUE, TRUE, 0);
324 gtk_container_set_border_width (GTK_CONTAINER (table1), 4);
325 gtk_grid_set_row_spacing(GTK_GRID(table1), 8);
326 gtk_grid_set_column_spacing(GTK_GRID(table1), 8);
328 from_entry = gtk_combo_box_text_new_with_entry ();
329 gtk_combo_box_set_active(GTK_COMBO_BOX(from_entry), -1);
330 if (prefs_common.summary_search_from_history)
331 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(from_entry),
332 prefs_common.summary_search_from_history);
333 gtk_widget_show (from_entry);
334 gtk_grid_attach(GTK_GRID(table1), from_entry, 1, 0, 1, 1);
335 gtk_widget_set_hexpand(from_entry, TRUE);
336 gtk_widget_set_halign(from_entry, GTK_ALIGN_FILL);
338 g_signal_connect(G_OBJECT(from_entry), "changed",
339 G_CALLBACK(from_changed), NULL);
340 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((from_entry)))),
341 "focus_in_event", G_CALLBACK(from_entry_focus_evt_in), NULL);
342 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((from_entry)))),
343 "focus_out_event", G_CALLBACK(from_entry_focus_evt_out), NULL);
345 to_entry = gtk_combo_box_text_new_with_entry ();
346 gtk_combo_box_set_active(GTK_COMBO_BOX(to_entry), -1);
347 if (prefs_common.summary_search_to_history)
348 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(to_entry),
349 prefs_common.summary_search_to_history);
350 gtk_widget_show (to_entry);
351 gtk_grid_attach(GTK_GRID(table1), to_entry, 1, 1, 1, 1);
352 gtk_widget_set_hexpand(to_entry, TRUE);
353 gtk_widget_set_halign(to_entry, GTK_ALIGN_FILL);
355 g_signal_connect(G_OBJECT(to_entry), "changed",
356 G_CALLBACK(to_changed), NULL);
357 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((to_entry)))),
358 "focus_in_event", G_CALLBACK(to_entry_focus_evt_in), NULL);
359 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((to_entry)))),
360 "focus_out_event", G_CALLBACK(to_entry_focus_evt_out), NULL);
362 subject_entry = gtk_combo_box_text_new_with_entry ();
363 gtk_combo_box_set_active(GTK_COMBO_BOX(subject_entry), -1);
364 if (prefs_common.summary_search_subject_history)
365 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(subject_entry),
366 prefs_common.summary_search_subject_history);
367 gtk_widget_show (subject_entry);
368 gtk_grid_attach(GTK_GRID(table1), subject_entry, 1, 2, 1, 1);
369 gtk_widget_set_hexpand(subject_entry, TRUE);
370 gtk_widget_set_halign(subject_entry, GTK_ALIGN_FILL);
372 g_signal_connect(G_OBJECT(subject_entry), "changed",
373 G_CALLBACK(subject_changed), NULL);
374 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((subject_entry)))),
375 "focus_in_event", G_CALLBACK(subject_entry_focus_evt_in), NULL);
376 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((subject_entry)))),
377 "focus_out_event", G_CALLBACK(subject_entry_focus_evt_out), NULL);
379 body_entry = gtk_combo_box_text_new_with_entry ();
380 gtk_combo_box_set_active(GTK_COMBO_BOX(body_entry), -1);
381 if (prefs_common.summary_search_body_history)
382 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(body_entry),
383 prefs_common.summary_search_body_history);
384 gtk_widget_show (body_entry);
385 gtk_grid_attach(GTK_GRID(table1), body_entry, 1, 3, 1, 1);
386 gtk_widget_set_hexpand(body_entry, TRUE);
387 gtk_widget_set_halign(body_entry, GTK_ALIGN_FILL);
389 g_signal_connect(G_OBJECT(body_entry), "changed",
390 G_CALLBACK(body_changed), NULL);
391 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
392 "focus_in_event", G_CALLBACK(body_entry_focus_evt_in), NULL);
393 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
394 "focus_out_event", G_CALLBACK(body_entry_focus_evt_out), NULL);
396 adv_condition_entry = gtk_combo_box_text_new_with_entry ();
397 gtk_combo_box_set_active(GTK_COMBO_BOX(adv_condition_entry), -1);
398 if (prefs_common.summary_search_adv_condition_history)
399 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(adv_condition_entry),
400 prefs_common.summary_search_adv_condition_history);
401 gtk_widget_show (adv_condition_entry);
402 gtk_grid_attach(GTK_GRID(table1), adv_condition_entry, 1, 4, 1, 1);
403 gtk_widget_set_hexpand(adv_condition_entry, TRUE);
404 gtk_widget_set_halign(adv_condition_entry, GTK_ALIGN_FILL);
406 g_signal_connect(G_OBJECT(adv_condition_entry), "changed",
407 G_CALLBACK(adv_condition_changed), NULL);
408 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((adv_condition_entry)))),
409 "focus_in_event", G_CALLBACK(adv_condition_entry_focus_evt_in), NULL);
410 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((adv_condition_entry)))),
411 "focus_out_event", G_CALLBACK(adv_condition_entry_focus_evt_out), NULL);
413 adv_condition_btn = gtk_button_new_with_label(" ... ");
414 gtk_widget_show (adv_condition_btn);
415 gtk_grid_attach(GTK_GRID(table1), adv_condition_btn, 2, 4, 1, 1);
417 g_signal_connect(G_OBJECT (adv_condition_btn), "clicked",
418 G_CALLBACK(adv_condition_btn_clicked), search_window.window);
420 CLAWS_SET_TIP(adv_condition_btn,
421 _("Edit search criteria"));
423 from_label = gtk_label_new (_("From:"));
424 gtk_widget_show (from_label);
425 gtk_label_set_justify (GTK_LABEL (from_label), GTK_JUSTIFY_RIGHT);
426 gtk_label_set_xalign (GTK_LABEL (from_label), 1.0);
427 gtk_grid_attach(GTK_GRID(table1), from_label, 0, 0, 1, 1);
429 to_label = gtk_label_new (_("To:"));
430 gtk_widget_show (to_label);
431 gtk_label_set_justify (GTK_LABEL (to_label), GTK_JUSTIFY_RIGHT);
432 gtk_label_set_xalign (GTK_LABEL (to_label), 1.0);
433 gtk_grid_attach(GTK_GRID(table1), to_label, 0, 1, 1, 1);
435 subject_label = gtk_label_new (_("Subject:"));
436 gtk_widget_show (subject_label);
437 gtk_label_set_justify (GTK_LABEL (subject_label), GTK_JUSTIFY_RIGHT);
438 gtk_label_set_xalign (GTK_LABEL (subject_label), 1.0);
439 gtk_grid_attach(GTK_GRID(table1), subject_label, 0, 2, 1, 1);
441 body_label = gtk_label_new (_("Body:"));
442 gtk_widget_show (body_label);
443 gtk_label_set_justify (GTK_LABEL (body_label), GTK_JUSTIFY_RIGHT);
444 gtk_label_set_xalign (GTK_LABEL (body_label), 1.0);
445 gtk_grid_attach(GTK_GRID(table1), body_label, 0, 3, 1, 1);
447 adv_condition_label = gtk_label_new (_("Condition:"));
448 gtk_widget_show (adv_condition_label);
449 gtk_label_set_justify (GTK_LABEL (adv_condition_label), GTK_JUSTIFY_RIGHT);
450 gtk_label_set_xalign (GTK_LABEL (adv_condition_label), 1.0);
451 gtk_grid_attach(GTK_GRID(table1), adv_condition_label, 0, 4, 1, 1);
453 checkbtn_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
454 gtk_widget_show (checkbtn_hbox);
455 gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
456 gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
458 case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
459 gtk_widget_show (case_checkbtn);
460 gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
461 FALSE, FALSE, 0);
462 g_signal_connect(G_OBJECT(case_checkbtn), "changed",
463 G_CALLBACK(case_changed), NULL);
465 adv_search_checkbtn = gtk_check_button_new_with_label (_("Extended Search"));
466 gtk_widget_show (adv_search_checkbtn);
467 gtk_box_pack_start (GTK_BOX (checkbtn_hbox), adv_search_checkbtn,
468 FALSE, FALSE, 0);
469 g_signal_connect(G_OBJECT(adv_search_checkbtn), "changed",
470 G_CALLBACK(case_changed), NULL);
472 confirm_area = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
473 gtk_widget_show (confirm_area);
474 gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
475 GTK_BUTTONBOX_END);
476 gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
478 gtkut_stock_button_add_help(confirm_area, &help_btn);
480 all_btn = gtk_button_new_with_mnemonic(_("Find _all"));
481 gtk_widget_set_can_default(all_btn, TRUE);
482 gtk_box_pack_start(GTK_BOX(confirm_area), all_btn, TRUE, TRUE, 0);
483 gtk_widget_show(all_btn);
485 prev_btn = gtkut_stock_button("go-previous", _("_Previous"));
486 gtk_widget_set_can_default(prev_btn, TRUE);
487 gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
488 gtk_widget_show(prev_btn);
490 next_btn = gtkut_stock_button("go-next", _("_Next"));
491 gtk_widget_set_can_default(next_btn, TRUE);
492 gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
493 gtk_widget_show(next_btn);
495 close_btn = gtkut_stock_button("window-close", _("_Close"));
496 gtk_widget_set_can_default(close_btn, TRUE);
497 gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
498 gtk_widget_show(close_btn);
500 /* stop button hidden */
501 stop_btn = gtk_button_new_with_mnemonic("_Stop");
502 gtk_widget_set_can_default(stop_btn, TRUE);
503 gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
505 gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
506 gtk_widget_grab_default(next_btn);
508 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, bool_optmenu)
509 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, from_label)
510 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, from_entry)
511 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, to_label)
512 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, to_entry)
513 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, subject_label)
514 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, subject_entry)
515 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, body_label)
516 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, body_entry)
517 SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_label)
518 SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_entry)
519 SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_btn)
520 SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, case_checkbtn)
522 g_signal_connect(G_OBJECT(help_btn), "clicked",
523 G_CALLBACK(manual_open_with_anchor_cb),
524 MANUAL_ANCHOR_SEARCHING);
525 g_signal_connect(G_OBJECT(clear_btn), "clicked",
526 G_CALLBACK(summary_search_clear), NULL);
527 g_signal_connect(G_OBJECT(all_btn), "clicked",
528 G_CALLBACK(summary_search_all_clicked), NULL);
529 g_signal_connect(G_OBJECT(prev_btn), "clicked",
530 G_CALLBACK(summary_search_prev_clicked), NULL);
531 g_signal_connect(G_OBJECT(next_btn), "clicked",
532 G_CALLBACK(summary_search_next_clicked), NULL);
533 g_signal_connect_closure
534 (G_OBJECT(close_btn), "clicked",
535 g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
536 window, NULL), FALSE);
537 g_signal_connect(G_OBJECT(stop_btn), "clicked",
538 G_CALLBACK(summary_search_stop_clicked), NULL);
540 search_window.window = window;
541 search_window.bool_optmenu = bool_optmenu;
542 search_window.from_label = from_label;
543 search_window.from_entry = from_entry;
544 search_window.to_label = to_label;
545 search_window.to_entry = to_entry;
546 search_window.subject_label = subject_label;
547 search_window.subject_entry = subject_entry;
548 search_window.body_label = body_label;
549 search_window.body_entry = body_entry;
550 search_window.adv_condition_label = adv_condition_label;
551 search_window.adv_condition_entry = adv_condition_entry;
552 search_window.adv_condition_btn = adv_condition_btn;
553 search_window.case_checkbtn = case_checkbtn;
554 search_window.adv_search_checkbtn = adv_search_checkbtn;
555 search_window.clear_btn = clear_btn;
556 search_window.help_btn = help_btn;
557 search_window.all_btn = all_btn;
558 search_window.prev_btn = prev_btn;
559 search_window.next_btn = next_btn;
560 search_window.close_btn = close_btn;
561 search_window.stop_btn = stop_btn;
562 search_window.advsearch = NULL;
563 search_window.matcher_is_outdated = TRUE;
564 search_window.search_in_progress = FALSE;
565 search_window.matched_msgnums = NULL;
566 search_window.is_searching = is_searching;
569 static gboolean summary_search_verify_match(MsgInfo *msg)
571 gpointer msgnum = GUINT_TO_POINTER(msg->msgnum);
573 if (g_hash_table_lookup(search_window.matched_msgnums, msgnum) != NULL)
574 return TRUE;
575 else
576 return FALSE;
579 static gboolean summary_search_progress_cb(gpointer data, guint at, guint matched, guint total)
581 if (!search_window.is_searching) {
582 search_window.matcher_is_outdated = TRUE;
583 return FALSE;
586 return summaryview_search_root_progress(search_window.summaryview, at, matched, total);
589 static gboolean summary_search_prepare_matcher()
591 gboolean adv_search;
592 gboolean bool_and = FALSE;
593 gboolean case_sens = FALSE;
594 gchar *matcher_str;
595 gint match_type;
596 gchar *from_str = NULL, *to_str = NULL, *subject_str = NULL;
597 gchar *body_str = NULL;
598 GSList *matchers = NULL;
600 if (!search_window.matcher_is_outdated)
601 return TRUE;
603 if (search_window.advsearch == NULL) {
604 search_window.advsearch = advsearch_new();
605 advsearch_set_on_error_cb(search_window.advsearch, NULL, NULL); /* TODO */
606 advsearch_set_on_progress_cb(search_window.advsearch,
607 summary_search_progress_cb, NULL);
610 adv_search = gtk_toggle_button_get_active
611 (GTK_TOGGLE_BUTTON(search_window.adv_search_checkbtn));
613 if (adv_search) {
614 matcher_str = add_history_get(search_window.adv_condition_entry, &prefs_common.summary_search_adv_condition_history);
615 } else {
616 MatcherList *matcher_list;
617 bool_and = combobox_get_active_data(GTK_COMBO_BOX(search_window.bool_optmenu));
618 case_sens = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
620 from_str = add_history_get(search_window.from_entry, &prefs_common.summary_search_from_history);
621 to_str = add_history_get(search_window.to_entry, &prefs_common.summary_search_to_history);
622 subject_str = add_history_get(search_window.subject_entry, &prefs_common.summary_search_subject_history);
623 body_str = add_history_get(search_window.body_entry, &prefs_common.summary_search_body_history);
625 if (!from_str && !to_str && !subject_str && !body_str) {
626 /* TODO: warn if no search criteria? (or make buttons enabled only when
627 * at least one search criteria has been set */
628 return FALSE;
631 match_type = case_sens ? MATCHTYPE_MATCH : MATCHTYPE_MATCHCASE;
633 if (from_str) {
634 MatcherProp *prop = matcherprop_new(MATCHCRITERIA_FROM, NULL, match_type, from_str, 0);
635 matchers = g_slist_append(matchers, prop);
637 if (to_str) {
638 MatcherProp *prop = matcherprop_new(MATCHCRITERIA_TO, NULL, match_type, to_str, 0);
639 matchers = g_slist_append(matchers, prop);
641 if (subject_str) {
642 MatcherProp *prop = matcherprop_new(MATCHCRITERIA_SUBJECT, NULL, match_type, subject_str, 0);
643 matchers = g_slist_append(matchers, prop);
645 if (body_str) {
646 MatcherProp *prop = matcherprop_new(MATCHCRITERIA_BODY_PART, NULL, match_type, body_str, 0);
647 matchers = g_slist_append(matchers, prop);
649 g_free(from_str);
650 g_free(to_str);
651 g_free(subject_str);
652 g_free(body_str);
654 matcher_list = matcherlist_new(matchers, bool_and);
655 if (!matcher_list)
656 return FALSE;
658 matcher_str = matcherlist_to_string(matcher_list);
659 matcherlist_free(matcher_list);
661 if (!matcher_str)
662 return FALSE;
664 advsearch_set(search_window.advsearch, ADVANCED_SEARCH_EXTENDED,
665 matcher_str);
667 debug_print("Advsearch set: %s\n", matcher_str);
668 g_free(matcher_str);
670 if (!advsearch_has_proper_predicate(search_window.advsearch))
671 return FALSE;
673 search_window.matcher_is_outdated = FALSE;
675 return TRUE;
678 static gboolean summary_search_prereduce_msg_list()
680 MsgInfoList *msglist = NULL;
681 MsgNumberList *msgnums = NULL;
682 MsgNumberList *cur;
683 SummaryView *summaryview = search_window.summaryview;
684 gboolean result;
685 FolderItem *item = summaryview->folder_item;
686 static GdkCursor *watch_cursor = NULL;
687 if (!watch_cursor)
688 watch_cursor = gdk_cursor_new_for_display(
689 gtk_widget_get_display(summaryview->scrolledwin), GDK_WATCH);
691 if (search_window.matcher_is_outdated && !summary_search_prepare_matcher())
692 return FALSE;
694 main_window_cursor_wait(mainwindow_get_mainwindow());
695 gdk_window_set_cursor(gtk_widget_get_window(search_window.window), watch_cursor);
696 statusbar_print_all(_("Searching in %s... \n"),
697 item->path ? item->path : "(null)");
699 result = advsearch_search_msgs_in_folders(search_window.advsearch,
700 &msglist, item, FALSE);
701 statusbar_pop_all();
702 statusbar_progress_all(0, 0, 0);
703 gdk_window_set_cursor(gtk_widget_get_window(search_window.window), NULL);
704 main_window_cursor_normal(mainwindow_get_mainwindow());
706 if (!result)
707 return FALSE;
709 msgnums = procmsg_get_number_list_for_msgs(msglist);
710 procmsg_msg_list_free(msglist);
712 if (search_window.matched_msgnums == NULL)
713 search_window.matched_msgnums = g_hash_table_new(g_direct_hash, NULL);
715 g_hash_table_remove_all(search_window.matched_msgnums);
717 for (cur = msgnums; cur != NULL; cur = cur->next)
718 g_hash_table_insert(search_window.matched_msgnums, cur->data, GINT_TO_POINTER(1));
720 g_slist_free(msgnums);
722 return TRUE;
725 static void summary_search_execute(gboolean backward, gboolean search_all)
727 SummaryView *summaryview = search_window.summaryview;
728 GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
729 GtkCMCTreeNode *node;
730 MsgInfo *msginfo;
731 gboolean all_searched = FALSE;
732 gboolean matched = FALSE;
733 gint i = 0;
735 if (summary_is_locked(summaryview))
736 return;
738 summary_lock(summaryview);
740 search_window.is_searching = TRUE;
741 main_window_cursor_wait(summaryview->mainwin);
742 summary_show_stop_button();
744 if (search_window.matcher_is_outdated && !summary_search_prereduce_msg_list())
745 goto exit;
747 if (search_all) {
748 summary_freeze(summaryview);
749 summary_unselect_all(summaryview);
750 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
751 backward = FALSE;
752 } else if (!summaryview->selected) {
753 /* no selection, search from from list start */
754 if (backward)
755 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list_end);
756 else
757 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
759 if (!node) {
760 search_window.is_searching = FALSE;
761 summary_hide_stop_button();
762 main_window_cursor_normal(summaryview->mainwin);
763 summary_unlock(summaryview);
764 return;
766 } else {
767 /* search from current selection */
768 if (backward)
769 node = gtkut_ctree_node_prev(ctree, summaryview->selected);
770 else
771 node = gtkut_ctree_node_next(ctree, summaryview->selected);
774 for (; search_window.is_searching; i++) {
775 if (!node) {
776 gchar *str;
777 AlertValue val;
779 if (search_all)
780 break;
782 if (all_searched) {
783 alertpanel_full(_("Search failed"),
784 _("Search string not found."),
785 "window-close", _("_Close"), NULL, NULL,
786 NULL, NULL, ALERTFOCUS_FIRST,
787 FALSE, NULL, ALERT_WARNING);
788 break;
791 if (backward)
792 str = _("Beginning of list reached; continue from end?");
793 else
794 str = _("End of list reached; continue from beginning?");
796 val = alertpanel(_("Search finished"), str,
797 NULL, _("_No"), NULL, _("_Yes"), NULL, NULL,
798 ALERTFOCUS_SECOND);
799 if (G_ALERTALTERNATE == val) {
800 if (backward) {
801 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list_end);
802 } else {
803 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
806 all_searched = TRUE;
808 manage_window_focus_in(search_window.window, NULL, NULL);
809 } else
810 break;
813 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
815 if (msginfo)
816 matched = summary_search_verify_match(msginfo);
817 else
818 matched = FALSE;
820 if (matched) {
821 if (search_all) {
822 gtk_cmctree_select(ctree, node);
823 } else {
824 summary_unlock(summaryview);
825 summary_select_node(summaryview, node,
826 OPEN_SELECTED_ON_SEARCH_RESULTS);
827 summary_lock(summaryview);
828 break;
832 if (i % (search_window.is_fast ? 1000 : 100) == 0)
833 GTK_EVENTS_FLUSH();
835 node = backward ? gtkut_ctree_node_prev(ctree, node)
836 : gtkut_ctree_node_next(ctree, node);
839 exit:
840 search_window.is_searching = FALSE;
841 summary_hide_stop_button();
842 main_window_cursor_normal(summaryview->mainwin);
843 if (search_all)
844 summary_thaw_with_status(summaryview);
845 summary_unlock(summaryview);
848 static void summary_search_clear(GtkButton *button, gpointer data)
850 if (gtk_toggle_button_get_active
851 (GTK_TOGGLE_BUTTON(search_window.adv_search_checkbtn))) {
852 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.adv_condition_entry)))), "");
853 } else {
854 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.from_entry)))), "");
855 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.to_entry)))), "");
856 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.subject_entry)))), "");
857 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.body_entry)))), "");
859 /* stop searching */
860 if (search_window.is_searching) {
861 search_window.is_searching = FALSE;
863 search_window.matcher_is_outdated = TRUE;
866 static void summary_search_prev_clicked(GtkButton *button, gpointer data)
868 summary_search_execute(TRUE, FALSE);
871 static void summary_search_next_clicked(GtkButton *button, gpointer data)
873 summary_search_execute(FALSE, FALSE);
876 static void summary_search_all_clicked(GtkButton *button, gpointer data)
878 summary_search_execute(FALSE, TRUE);
881 static void adv_condition_btn_done(MatcherList * matchers)
883 gchar *str;
885 cm_return_if_fail(
886 mainwindow_get_mainwindow()->summaryview->quicksearch != NULL);
888 if (matchers == NULL) {
889 return;
892 str = matcherlist_to_string(matchers);
893 search_window.matcher_is_outdated = TRUE;
895 if (str != NULL) {
896 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((search_window.adv_condition_entry)))), str);
897 g_free(str);
901 static void summary_search_stop_clicked(GtkButton *button, gpointer data)
903 search_window.is_searching = FALSE;
906 static void adv_condition_btn_clicked(GtkButton *button, gpointer data)
908 const gchar * cond_str;
909 MatcherList * matchers = NULL;
911 cm_return_if_fail( search_window.window != NULL );
913 /* re-use the current search value if it's a condition expression,
914 otherwise ignore it silently */
915 cond_str = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(search_window.adv_condition_entry));
916 if (cond_str && *cond_str != '\0') {
917 matchers = matcher_parser_get_cond((gchar*)cond_str, NULL);
920 prefs_matcher_open(matchers, adv_condition_btn_done);
922 if (matchers != NULL) {
923 matcherlist_free(matchers);
927 static void optmenu_changed(GtkComboBox *widget, gpointer user_data)
929 search_window.matcher_is_outdated = TRUE;
932 static void from_changed(void)
934 if (!search_window.from_entry_has_focus)
935 gtk_widget_grab_focus(search_window.from_entry);
936 search_window.matcher_is_outdated = TRUE;
939 static void to_changed(void)
941 if (!search_window.to_entry_has_focus)
942 gtk_widget_grab_focus(search_window.to_entry);
943 search_window.matcher_is_outdated = TRUE;
946 static void subject_changed(void)
948 if (!search_window.subject_entry_has_focus)
949 gtk_widget_grab_focus(search_window.subject_entry);
950 search_window.matcher_is_outdated = TRUE;
953 static void body_changed(void)
955 if (!search_window.body_entry_has_focus)
956 gtk_widget_grab_focus(search_window.body_entry);
957 search_window.matcher_is_outdated = TRUE;
960 static void adv_condition_changed(void)
962 if (!search_window.adv_condition_entry_has_focus)
963 gtk_widget_grab_focus(search_window.adv_condition_entry);
964 search_window.matcher_is_outdated = TRUE;
967 static void case_changed(GtkToggleButton *togglebutton, gpointer user_data)
969 search_window.matcher_is_outdated = TRUE;
972 static gboolean from_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
973 gpointer data)
975 search_window.from_entry_has_focus = TRUE;
976 return FALSE;
979 static gboolean from_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
980 gpointer data)
982 search_window.from_entry_has_focus = FALSE;
983 return FALSE;
986 static gboolean to_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
987 gpointer data)
989 search_window.to_entry_has_focus = TRUE;
990 return FALSE;
993 static gboolean to_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
994 gpointer data)
996 search_window.to_entry_has_focus = FALSE;
997 return FALSE;
1000 static gboolean subject_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
1001 gpointer data)
1003 search_window.subject_entry_has_focus = TRUE;
1004 return FALSE;
1007 static gboolean subject_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
1008 gpointer data)
1010 search_window.subject_entry_has_focus = FALSE;
1011 return FALSE;
1014 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
1015 gpointer data)
1017 search_window.body_entry_has_focus = TRUE;
1018 return FALSE;
1021 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
1022 gpointer data)
1024 search_window.body_entry_has_focus = FALSE;
1025 return FALSE;
1028 static gboolean adv_condition_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
1029 gpointer data)
1031 search_window.adv_condition_entry_has_focus = TRUE;
1032 return FALSE;
1035 static gboolean adv_condition_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
1036 gpointer data)
1038 search_window.adv_condition_entry_has_focus = FALSE;
1039 return FALSE;
1042 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
1043 gpointer data)
1045 if (event && (event->keyval == GDK_KEY_Escape)) {
1046 /* ESC key will:
1047 - stop a running search
1048 - close the search window if no search is running
1050 if (!search_window.is_searching) {
1051 gtk_widget_hide(search_window.window);
1052 } else {
1053 search_window.is_searching = FALSE;
1057 if (event && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
1058 if (!search_window.is_searching) {
1059 summary_search_execute(FALSE, FALSE);
1063 if (event && (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_Up)) {
1064 if (search_window.from_entry_has_focus) {
1065 combobox_set_value_from_arrow_key(
1066 GTK_COMBO_BOX(search_window.from_entry),
1067 event->keyval);
1068 return TRUE;
1070 if (search_window.to_entry_has_focus) {
1071 combobox_set_value_from_arrow_key(
1072 GTK_COMBO_BOX(search_window.to_entry),
1073 event->keyval);
1074 return TRUE;
1076 if (search_window.subject_entry_has_focus) {
1077 combobox_set_value_from_arrow_key(
1078 GTK_COMBO_BOX(search_window.subject_entry),
1079 event->keyval);
1080 return TRUE;
1082 if (search_window.body_entry_has_focus) {
1083 combobox_set_value_from_arrow_key(
1084 GTK_COMBO_BOX(search_window.body_entry),
1085 event->keyval);
1086 return TRUE;
1088 if (search_window.adv_condition_entry_has_focus) {
1089 combobox_set_value_from_arrow_key(
1090 GTK_COMBO_BOX(search_window.adv_condition_entry),
1091 event->keyval);
1092 return TRUE;
1096 return FALSE;