summary_search.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*
  2. * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
  3. * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail team
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include "config.h"
  21. #endif
  22. #include "defs.h"
  23. #include <glib.h>
  24. #include <glib/gi18n.h>
  25. #include <gdk/gdkkeysyms.h>
  26. #include <gtk/gtkwidget.h>
  27. #include <gtk/gtkwindow.h>
  28. #include <gtk/gtkvbox.h>
  29. #include <gtk/gtktable.h>
  30. #include <gtk/gtkoptionmenu.h>
  31. #include <gtk/gtklabel.h>
  32. #include <gtk/gtkentry.h>
  33. #include <gtk/gtkhbox.h>
  34. #include <gtk/gtkcheckbutton.h>
  35. #include <gtk/gtkhbbox.h>
  36. #include <gtk/gtkbutton.h>
  37. #include <gtk/gtkctree.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include "main.h"
  42. #include "summary_search.h"
  43. #include "summaryview.h"
  44. #include "messageview.h"
  45. #include "mainwindow.h"
  46. #include "menu.h"
  47. #include "utils.h"
  48. #include "gtkutils.h"
  49. #include "prefs_gtk.h"
  50. #include "manage_window.h"
  51. #include "alertpanel.h"
  52. #include "matcher.h"
  53. #include "matcher_parser.h"
  54. #include "prefs_matcher.h"
  55. #include "manual.h"
  56. static struct SummarySearchWindow {
  57. GtkWidget *window;
  58. GtkWidget *bool_optmenu;
  59. GtkWidget *from_entry;
  60. GtkWidget *to_entry;
  61. GtkWidget *subject_entry;
  62. GtkWidget *body_entry;
  63. GtkWidget *adv_condition_entry;
  64. GtkWidget *adv_condition_btn;
  65. GtkWidget *adv_search_checkbtn;
  66. GtkWidget *case_checkbtn;
  67. GtkWidget *clear_btn;
  68. GtkWidget *help_btn;
  69. GtkWidget *all_btn;
  70. GtkWidget *prev_btn;
  71. GtkWidget *next_btn;
  72. GtkWidget *close_btn;
  73. GtkWidget *stop_btn;
  74. SummaryView *summaryview;
  75. MatcherList *matcher_list;
  76. gboolean is_searching;
  77. } search_window;
  78. static void summary_search_create (void);
  79. static void summary_search_execute (gboolean backward,
  80. gboolean search_all);
  81. static void summary_search_clear (GtkButton *button,
  82. gpointer data);
  83. static void summary_search_prev_clicked (GtkButton *button,
  84. gpointer data);
  85. static void summary_search_next_clicked (GtkButton *button,
  86. gpointer data);
  87. static void summary_search_all_clicked (GtkButton *button,
  88. gpointer data);
  89. static void summary_search_stop_clicked (GtkButton *button,
  90. gpointer data);
  91. static void adv_condition_btn_clicked (GtkButton *button,
  92. gpointer data);
  93. static void from_activated (void);
  94. static void to_activated (void);
  95. static void subject_activated (void);
  96. static void body_activated (void);
  97. static void adv_condition_activated (void);
  98. static gboolean key_pressed (GtkWidget *widget,
  99. GdkEventKey *event,
  100. gpointer data);
  101. #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) { \
  102. gboolean in_btn = FALSE; \
  103. if (GTK_IS_BUTTON(widget)) \
  104. in_btn = GTK_BUTTON(widget)->in_button; \
  105. gtk_widget_set_sensitive(widget, sensitive); \
  106. if (GTK_IS_BUTTON(widget)) \
  107. GTK_BUTTON(widget)->in_button = in_btn; \
  108. }
  109. void summary_search(SummaryView *summaryview)
  110. {
  111. if (!search_window.window) {
  112. summary_search_create();
  113. } else {
  114. gtk_widget_hide(search_window.window);
  115. }
  116. search_window.summaryview = summaryview;
  117. gtk_widget_grab_focus(search_window.next_btn);
  118. gtk_widget_grab_focus(search_window.subject_entry);
  119. gtk_widget_show(search_window.window);
  120. }
  121. static void summary_show_stop_button(void)
  122. {
  123. gtk_widget_hide(search_window.close_btn);
  124. gtk_widget_show(search_window.stop_btn);
  125. GTK_BUTTON_SET_SENSITIVE(search_window.all_btn, FALSE)
  126. GTK_BUTTON_SET_SENSITIVE(search_window.prev_btn, FALSE)
  127. GTK_BUTTON_SET_SENSITIVE(search_window.next_btn, FALSE)
  128. }
  129. static void summary_hide_stop_button(void)
  130. {
  131. gtk_widget_hide(search_window.stop_btn);
  132. gtk_widget_show(search_window.close_btn);
  133. gtk_widget_set_sensitive(search_window.all_btn, TRUE);
  134. gtk_widget_set_sensitive(search_window.prev_btn, TRUE);
  135. gtk_widget_set_sensitive(search_window.next_btn, TRUE);
  136. }
  137. static void summary_search_create(void)
  138. {
  139. GtkWidget *window;
  140. GtkWidget *vbox1;
  141. GtkWidget *bool_hbox;
  142. GtkWidget *bool_optmenu;
  143. GtkWidget *bool_menu;
  144. GtkWidget *menuitem;
  145. GtkWidget *clear_btn;
  146. GtkWidget *table1;
  147. GtkWidget *from_label;
  148. GtkWidget *from_entry;
  149. GtkWidget *to_label;
  150. GtkWidget *to_entry;
  151. GtkWidget *subject_label;
  152. GtkWidget *subject_entry;
  153. GtkWidget *body_label;
  154. GtkWidget *body_entry;
  155. GtkWidget *adv_condition_label;
  156. GtkWidget *adv_condition_entry;
  157. GtkWidget *adv_condition_btn;
  158. GtkWidget *checkbtn_hbox;
  159. GtkWidget *adv_search_checkbtn;
  160. GtkWidget *case_checkbtn;
  161. GtkWidget *confirm_area;
  162. GtkWidget *help_btn;
  163. GtkWidget *all_btn;
  164. GtkWidget *prev_btn;
  165. GtkWidget *next_btn;
  166. GtkWidget *close_btn;
  167. GtkWidget *stop_btn;
  168. GtkTooltips *tooltip;
  169. gboolean is_searching = FALSE;
  170. window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  171. gtk_window_set_title(GTK_WINDOW (window), _("Search messages"));
  172. gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
  173. gtk_container_set_border_width(GTK_CONTAINER (window), 8);
  174. g_signal_connect(G_OBJECT(window), "delete_event",
  175. G_CALLBACK(gtk_widget_hide_on_delete), NULL);
  176. g_signal_connect(G_OBJECT(window), "key_press_event",
  177. G_CALLBACK(key_pressed), NULL);
  178. MANAGE_WINDOW_SIGNALS_CONNECT(window);
  179. vbox1 = gtk_vbox_new (FALSE, 0);
  180. gtk_widget_show (vbox1);
  181. gtk_container_add (GTK_CONTAINER (window), vbox1);
  182. bool_hbox = gtk_hbox_new(FALSE, 4);
  183. gtk_widget_show(bool_hbox);
  184. gtk_box_pack_start(GTK_BOX(vbox1), bool_hbox, FALSE, FALSE, 0);
  185. bool_optmenu = gtk_option_menu_new();
  186. gtk_widget_show(bool_optmenu);
  187. gtk_box_pack_start(GTK_BOX(bool_hbox), bool_optmenu, FALSE, FALSE, 0);
  188. bool_menu = gtk_menu_new();
  189. MENUITEM_ADD(bool_menu, menuitem, _("Match any of the following"), 0);
  190. MENUITEM_ADD(bool_menu, menuitem, _("Match all of the following"), 1);
  191. gtk_option_menu_set_menu(GTK_OPTION_MENU(bool_optmenu), bool_menu);
  192. clear_btn = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
  193. gtk_widget_show(clear_btn);
  194. gtk_box_pack_end(GTK_BOX(bool_hbox), clear_btn, FALSE, FALSE, 0);
  195. table1 = gtk_table_new (5, 3, FALSE);
  196. gtk_widget_show (table1);
  197. gtk_box_pack_start (GTK_BOX (vbox1), table1, TRUE, TRUE, 0);
  198. gtk_container_set_border_width (GTK_CONTAINER (table1), 4);
  199. gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
  200. gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
  201. from_entry = gtk_entry_new ();
  202. gtk_widget_show (from_entry);
  203. gtk_table_attach (GTK_TABLE (table1), from_entry, 1, 3, 0, 1,
  204. GTK_EXPAND|GTK_FILL, 0, 0, 0);
  205. g_signal_connect(G_OBJECT(from_entry), "activate",
  206. G_CALLBACK(from_activated), NULL);
  207. to_entry = gtk_entry_new ();
  208. gtk_widget_show (to_entry);
  209. gtk_table_attach (GTK_TABLE (table1), to_entry, 1, 3, 1, 2,
  210. GTK_EXPAND|GTK_FILL, 0, 0, 0);
  211. g_signal_connect(G_OBJECT(to_entry), "activate",
  212. G_CALLBACK(to_activated), NULL);
  213. subject_entry = gtk_entry_new ();
  214. gtk_widget_show (subject_entry);
  215. gtk_table_attach (GTK_TABLE (table1), subject_entry, 1, 3, 2, 3,
  216. GTK_EXPAND|GTK_FILL, 0, 0, 0);
  217. g_signal_connect(G_OBJECT(subject_entry), "activate",
  218. G_CALLBACK(subject_activated), NULL);
  219. body_entry = gtk_entry_new ();
  220. gtk_widget_show (body_entry);
  221. gtk_table_attach (GTK_TABLE (table1), body_entry, 1, 3, 3, 4,
  222. GTK_EXPAND|GTK_FILL, 0, 0, 0);
  223. g_signal_connect(G_OBJECT(body_entry), "activate",
  224. G_CALLBACK(body_activated), NULL);
  225. adv_condition_entry = gtk_entry_new ();
  226. gtk_widget_show (adv_condition_entry);
  227. gtk_table_attach (GTK_TABLE (table1), adv_condition_entry, 1, 2, 4, 5,
  228. GTK_EXPAND|GTK_FILL, 0, 0, 0);
  229. g_signal_connect(G_OBJECT(adv_condition_entry), "activate",
  230. G_CALLBACK(adv_condition_activated), NULL);
  231. adv_condition_btn = gtk_button_new_with_label(" ... ");
  232. gtk_widget_show (adv_condition_btn);
  233. gtk_table_attach (GTK_TABLE (table1), adv_condition_btn, 2, 3, 4, 5,
  234. GTK_FILL, 0, 0, 0);
  235. g_signal_connect(G_OBJECT (adv_condition_btn), "clicked",
  236. G_CALLBACK(adv_condition_btn_clicked), search_window.window);
  237. tooltip = gtk_tooltips_new();
  238. gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltip), adv_condition_btn,
  239. _("Edit search criteria"), NULL);
  240. from_label = gtk_label_new (_("From:"));
  241. gtk_widget_show (from_label);
  242. gtk_table_attach (GTK_TABLE (table1), from_label, 0, 1, 0, 1,
  243. GTK_FILL, 0, 0, 0);
  244. gtk_label_set_justify (GTK_LABEL (from_label), GTK_JUSTIFY_RIGHT);
  245. gtk_misc_set_alignment (GTK_MISC (from_label), 1, 0.5);
  246. to_label = gtk_label_new (_("To:"));
  247. gtk_widget_show (to_label);
  248. gtk_table_attach (GTK_TABLE (table1), to_label, 0, 1, 1, 2,
  249. GTK_FILL, 0, 0, 0);
  250. gtk_label_set_justify (GTK_LABEL (to_label), GTK_JUSTIFY_RIGHT);
  251. gtk_misc_set_alignment (GTK_MISC (to_label), 1, 0.5);
  252. subject_label = gtk_label_new (_("Subject:"));
  253. gtk_widget_show (subject_label);
  254. gtk_table_attach (GTK_TABLE (table1), subject_label, 0, 1, 2, 3,
  255. GTK_FILL, 0, 0, 0);
  256. gtk_label_set_justify (GTK_LABEL (subject_label), GTK_JUSTIFY_RIGHT);
  257. gtk_misc_set_alignment (GTK_MISC (subject_label), 1, 0.5);
  258. body_label = gtk_label_new (_("Body:"));
  259. gtk_widget_show (body_label);
  260. gtk_table_attach (GTK_TABLE (table1), body_label, 0, 1, 3, 4,
  261. GTK_FILL, 0, 0, 0);
  262. gtk_label_set_justify (GTK_LABEL (body_label), GTK_JUSTIFY_RIGHT);
  263. gtk_misc_set_alignment (GTK_MISC (body_label), 1, 0.5);
  264. adv_condition_label = gtk_label_new (_("Condition:"));
  265. gtk_widget_show (adv_condition_label);
  266. gtk_table_attach (GTK_TABLE (table1), adv_condition_label, 0, 1, 4, 5,
  267. GTK_FILL, 0, 0, 0);
  268. gtk_label_set_justify (GTK_LABEL (adv_condition_label), GTK_JUSTIFY_RIGHT);
  269. gtk_misc_set_alignment (GTK_MISC (adv_condition_label), 1, 0.5);
  270. checkbtn_hbox = gtk_hbox_new (FALSE, 8);
  271. gtk_widget_show (checkbtn_hbox);
  272. gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
  273. gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
  274. case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
  275. gtk_widget_show (case_checkbtn);
  276. gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
  277. FALSE, FALSE, 0);
  278. adv_search_checkbtn = gtk_check_button_new_with_label (_("Extended Search"));
  279. gtk_widget_show (adv_search_checkbtn);
  280. gtk_box_pack_start (GTK_BOX (checkbtn_hbox), adv_search_checkbtn,
  281. FALSE, FALSE, 0);
  282. confirm_area = gtk_hbutton_box_new();
  283. gtk_widget_show (confirm_area);
  284. gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
  285. GTK_BUTTONBOX_END);
  286. gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
  287. gtkut_stock_button_add_help(confirm_area, &help_btn);
  288. all_btn = gtk_button_new_with_mnemonic(_("Find _all"));
  289. GTK_WIDGET_SET_FLAGS(all_btn, GTK_CAN_DEFAULT);
  290. gtk_box_pack_start(GTK_BOX(confirm_area), all_btn, TRUE, TRUE, 0);
  291. gtk_widget_show(all_btn);
  292. prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
  293. GTK_WIDGET_SET_FLAGS(prev_btn, GTK_CAN_DEFAULT);
  294. gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
  295. gtk_widget_show(prev_btn);
  296. next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
  297. GTK_WIDGET_SET_FLAGS(next_btn, GTK_CAN_DEFAULT);
  298. gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
  299. gtk_widget_show(next_btn);
  300. close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
  301. GTK_WIDGET_SET_FLAGS(close_btn, GTK_CAN_DEFAULT);
  302. gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
  303. gtk_widget_show(close_btn);
  304. /* stop button hidden */
  305. stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
  306. GTK_WIDGET_SET_FLAGS(stop_btn, GTK_CAN_DEFAULT);
  307. gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
  308. gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
  309. gtk_widget_grab_default(next_btn);
  310. SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, bool_menu)
  311. SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, from_entry)
  312. SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, to_entry)
  313. SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, subject_entry)
  314. SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, body_entry)
  315. SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_label)
  316. SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_entry)
  317. SET_TOGGLE_SENSITIVITY(adv_search_checkbtn, adv_condition_btn)
  318. SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, case_checkbtn)
  319. g_signal_connect(G_OBJECT(help_btn), "clicked",
  320. G_CALLBACK(manual_open_with_anchor_cb),
  321. MANUAL_ANCHOR_SEARCHING);
  322. g_signal_connect(G_OBJECT(clear_btn), "clicked",
  323. G_CALLBACK(summary_search_clear), NULL);
  324. g_signal_connect(G_OBJECT(all_btn), "clicked",
  325. G_CALLBACK(summary_search_all_clicked), NULL);
  326. g_signal_connect(G_OBJECT(prev_btn), "clicked",
  327. G_CALLBACK(summary_search_prev_clicked), NULL);
  328. g_signal_connect(G_OBJECT(next_btn), "clicked",
  329. G_CALLBACK(summary_search_next_clicked), NULL);
  330. g_signal_connect_closure
  331. (G_OBJECT(close_btn), "clicked",
  332. g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
  333. window, NULL), FALSE);
  334. g_signal_connect(G_OBJECT(stop_btn), "clicked",
  335. G_CALLBACK(summary_search_stop_clicked), NULL);
  336. search_window.window = window;
  337. search_window.bool_optmenu = bool_optmenu;
  338. search_window.from_entry = from_entry;
  339. search_window.to_entry = to_entry;
  340. search_window.subject_entry = subject_entry;
  341. search_window.body_entry = body_entry;
  342. search_window.adv_condition_entry = adv_condition_entry;
  343. search_window.adv_condition_btn = adv_condition_btn;
  344. search_window.case_checkbtn = case_checkbtn;
  345. search_window.adv_search_checkbtn = adv_search_checkbtn;
  346. search_window.clear_btn = clear_btn;
  347. search_window.help_btn = help_btn;
  348. search_window.all_btn = all_btn;
  349. search_window.prev_btn = prev_btn;
  350. search_window.next_btn = next_btn;
  351. search_window.close_btn = close_btn;
  352. search_window.stop_btn = stop_btn;
  353. search_window.matcher_list = NULL;
  354. search_window.is_searching = is_searching;
  355. }
  356. static void summary_search_execute(gboolean backward, gboolean search_all)
  357. {
  358. SummaryView *summaryview = search_window.summaryview;
  359. GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
  360. GtkCTreeNode *node;
  361. MsgInfo *msginfo;
  362. gboolean adv_search;
  363. gboolean bool_and = FALSE;
  364. gboolean case_sens = FALSE;
  365. gboolean all_searched = FALSE;
  366. gboolean matched = FALSE;
  367. gboolean body_matched = FALSE;
  368. const gchar *from_str = NULL, *to_str = NULL, *subject_str = NULL;
  369. const gchar *body_str = NULL, *adv_condition = NULL;
  370. StrFindFunc str_find_func = NULL;
  371. gboolean is_fast = TRUE;
  372. gint interval = 1000;
  373. gint i = 0;
  374. if (summary_is_locked(summaryview)) {
  375. return;
  376. }
  377. summary_lock(summaryview);
  378. adv_search = gtk_toggle_button_get_active
  379. (GTK_TOGGLE_BUTTON(search_window.adv_search_checkbtn));
  380. if (adv_search) {
  381. if (search_window.matcher_list != NULL) {
  382. matcherlist_free(search_window.matcher_list);
  383. search_window.matcher_list = NULL;
  384. }
  385. adv_condition = gtk_entry_get_text(GTK_ENTRY(search_window.adv_condition_entry));
  386. if (adv_condition[0] != '\0') {
  387. search_window.matcher_list = matcher_parser_get_cond((gchar*)adv_condition, &is_fast);
  388. if (!is_fast)
  389. interval = 100;
  390. /* TODO: check for condition parsing error and show an error dialog */
  391. } else {
  392. /* TODO: warn if no search condition? (or make buttons enabled only when
  393. at least one search condition has been set */
  394. summary_unlock(summaryview);
  395. return;
  396. }
  397. } else {
  398. bool_and = GPOINTER_TO_INT
  399. (menu_get_option_menu_active_user_data
  400. (GTK_OPTION_MENU(search_window.bool_optmenu)));
  401. case_sens = gtk_toggle_button_get_active
  402. (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
  403. if (case_sens) {
  404. str_find_func = str_find;
  405. } else {
  406. str_find_func = str_case_find;
  407. }
  408. from_str = gtk_entry_get_text(GTK_ENTRY(search_window.from_entry));
  409. to_str = gtk_entry_get_text(GTK_ENTRY(search_window.to_entry));
  410. subject_str = gtk_entry_get_text(GTK_ENTRY(search_window.subject_entry));
  411. body_str = gtk_entry_get_text(GTK_ENTRY(search_window.body_entry));
  412. if ( (from_str[0] == '\0') &&
  413. (to_str[0] == '\0') &&
  414. (subject_str[0] == '\0') &&
  415. (body_str[0] == '\0')) {
  416. /* TODO: warn if no search criteria? (or make buttons enabled only when
  417. at least one search criteria has been set */
  418. summary_unlock(summaryview);
  419. return;
  420. }
  421. }
  422. search_window.is_searching = TRUE;
  423. main_window_cursor_wait(summaryview->mainwin);
  424. summary_show_stop_button();
  425. if (search_all) {
  426. summary_freeze(summaryview);
  427. summary_unselect_all(summaryview);
  428. node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
  429. backward = FALSE;
  430. } else if (!summaryview->selected) {
  431. if (backward) {
  432. node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
  433. } else {
  434. node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
  435. }
  436. if (!node) {
  437. search_window.is_searching = FALSE;
  438. summary_hide_stop_button();
  439. main_window_cursor_normal(summaryview->mainwin);
  440. summary_unlock(summaryview);
  441. return;
  442. }
  443. } else {
  444. if (backward) {
  445. node = gtkut_ctree_node_prev
  446. (ctree, summaryview->selected);
  447. } else {
  448. node = gtkut_ctree_node_next
  449. (ctree, summaryview->selected);
  450. }
  451. }
  452. for (; search_window.is_searching; i++) {
  453. if (!node) {
  454. gchar *str;
  455. AlertValue val;
  456. if (search_all) {
  457. break;
  458. }
  459. if (all_searched) {
  460. alertpanel_full(_("Search failed"),
  461. _("Search string not found."),
  462. GTK_STOCK_CLOSE, NULL, NULL, FALSE,
  463. NULL, ALERT_WARNING, G_ALERTDEFAULT);
  464. break;
  465. }
  466. if (backward)
  467. str = _("Beginning of list reached; continue from end?");
  468. else
  469. str = _("End of list reached; continue from beginning?");
  470. val = alertpanel(_("Search finished"), str,
  471. GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
  472. if (G_ALERTALTERNATE == val) {
  473. if (backward) {
  474. node = GTK_CTREE_NODE
  475. (GTK_CLIST(ctree)->row_list_end);
  476. } else {
  477. node = GTK_CTREE_NODE
  478. (GTK_CLIST(ctree)->row_list);
  479. }
  480. all_searched = TRUE;
  481. manage_window_focus_in(search_window.window, NULL, NULL);
  482. } else {
  483. break;
  484. }
  485. }
  486. msginfo = gtk_ctree_node_get_row_data(ctree, node);
  487. body_matched = FALSE;
  488. if (adv_search) {
  489. matched = matcherlist_match(search_window.matcher_list, msginfo);
  490. } else {
  491. if (bool_and) {
  492. matched = TRUE;
  493. if (*from_str) {
  494. if (!msginfo->from ||
  495. !str_find_func(msginfo->from, from_str)) {
  496. matched = FALSE;
  497. }
  498. }
  499. if (matched && *to_str) {
  500. if (!msginfo->to ||
  501. !str_find_func(msginfo->to, to_str)) {
  502. matched = FALSE;
  503. }
  504. }
  505. if (matched && *subject_str) {
  506. if (!msginfo->subject ||
  507. !str_find_func(msginfo->subject, subject_str)) {
  508. matched = FALSE;
  509. }
  510. }
  511. if (matched && *body_str) {
  512. if (procmime_find_string(msginfo, body_str,
  513. str_find_func)) {
  514. body_matched = TRUE;
  515. } else {
  516. matched = FALSE;
  517. }
  518. }
  519. if (matched && !*from_str && !*to_str &&
  520. !*subject_str && !*body_str) {
  521. matched = FALSE;
  522. }
  523. } else {
  524. matched = FALSE;
  525. if (*from_str && msginfo->from) {
  526. if (str_find_func(msginfo->from, from_str)) {
  527. matched = TRUE;
  528. }
  529. }
  530. if (!matched && *to_str && msginfo->to) {
  531. if (str_find_func(msginfo->to, to_str)) {
  532. matched = TRUE;
  533. }
  534. }
  535. if (!matched && *subject_str && msginfo->subject) {
  536. if (str_find_func(msginfo->subject, subject_str)) {
  537. matched = TRUE;
  538. }
  539. }
  540. if (!matched && *body_str) {
  541. if (procmime_find_string(msginfo, body_str,
  542. str_find_func)) {
  543. matched = TRUE;
  544. body_matched = TRUE;
  545. }
  546. }
  547. }
  548. }
  549. if (matched) {
  550. if (search_all) {
  551. gtk_ctree_select(ctree, node);
  552. } else {
  553. if (messageview_is_visible
  554. (summaryview->messageview)) {
  555. summary_unlock(summaryview);
  556. summary_select_node
  557. (summaryview, node, TRUE, TRUE);
  558. summary_lock(summaryview);
  559. if (body_matched) {
  560. messageview_search_string
  561. (summaryview->messageview,
  562. body_str, case_sens);
  563. }
  564. } else {
  565. summary_select_node
  566. (summaryview, node, FALSE, TRUE);
  567. }
  568. break;
  569. }
  570. }
  571. node = backward ? gtkut_ctree_node_prev(ctree, node)
  572. : gtkut_ctree_node_next(ctree, node);
  573. if (i % interval == 0)
  574. GTK_EVENTS_FLUSH();
  575. }
  576. search_window.is_searching = FALSE;
  577. summary_hide_stop_button();
  578. main_window_cursor_normal(summaryview->mainwin);
  579. if (search_all) {
  580. summary_thaw(summaryview);
  581. }
  582. summary_unlock(summaryview);
  583. }
  584. static void summary_search_clear(GtkButton *button, gpointer data)
  585. {
  586. if (gtk_toggle_button_get_active
  587. (GTK_TOGGLE_BUTTON(search_window.adv_search_checkbtn))) {
  588. gtk_editable_delete_text(GTK_EDITABLE(search_window.adv_condition_entry),
  589. 0, -1);
  590. } else {
  591. gtk_editable_delete_text(GTK_EDITABLE(search_window.from_entry),
  592. 0, -1);
  593. gtk_editable_delete_text(GTK_EDITABLE(search_window.to_entry),
  594. 0, -1);
  595. gtk_editable_delete_text(GTK_EDITABLE(search_window.subject_entry),
  596. 0, -1);
  597. gtk_editable_delete_text(GTK_EDITABLE(search_window.body_entry),
  598. 0, -1);
  599. }
  600. /* stop searching */
  601. if (search_window.is_searching) {
  602. search_window.is_searching = FALSE;
  603. }
  604. }
  605. static void summary_search_prev_clicked(GtkButton *button, gpointer data)
  606. {
  607. summary_search_execute(TRUE, FALSE);
  608. }
  609. static void summary_search_next_clicked(GtkButton *button, gpointer data)
  610. {
  611. summary_search_execute(FALSE, FALSE);
  612. }
  613. static void summary_search_all_clicked(GtkButton *button, gpointer data)
  614. {
  615. summary_search_execute(FALSE, TRUE);
  616. }
  617. static void adv_condition_btn_done(MatcherList * matchers)
  618. {
  619. gchar *str;
  620. g_return_if_fail(
  621. mainwindow_get_mainwindow()->summaryview->quicksearch != NULL);
  622. if (matchers == NULL) {
  623. return;
  624. }
  625. str = matcherlist_to_string(matchers);
  626. if (str != NULL) {
  627. gtk_entry_set_text(
  628. GTK_ENTRY(search_window.adv_condition_entry), str);
  629. g_free(str);
  630. }
  631. }
  632. static void summary_search_stop_clicked(GtkButton *button, gpointer data)
  633. {
  634. search_window.is_searching = FALSE;
  635. }
  636. static void adv_condition_btn_clicked(GtkButton *button, gpointer data)
  637. {
  638. const gchar * cond_str;
  639. MatcherList * matchers = NULL;
  640. g_return_if_fail( search_window.window != NULL );
  641. /* re-use it the current quicksearch value if it's a condition expression,
  642. otherwise ignore it silently */
  643. cond_str = gtk_entry_get_text(
  644. GTK_ENTRY(search_window.adv_condition_entry));
  645. if (*cond_str != '\0') {
  646. matchers = matcher_parser_get_cond((gchar*)cond_str, NULL);
  647. }
  648. prefs_matcher_open(matchers, adv_condition_btn_done);
  649. if (matchers != NULL) {
  650. matcherlist_free(matchers);
  651. }
  652. };
  653. static void from_activated(void)
  654. {
  655. gtk_widget_grab_focus(search_window.to_entry);
  656. }
  657. static void to_activated(void)
  658. {
  659. gtk_widget_grab_focus(search_window.subject_entry);
  660. }
  661. static void subject_activated(void)
  662. {
  663. gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
  664. }
  665. static void body_activated(void)
  666. {
  667. gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
  668. }
  669. static void adv_condition_activated(void)
  670. {
  671. gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
  672. }
  673. static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
  674. gpointer data)
  675. {
  676. if (event && event->keyval == GDK_Escape) {
  677. /* ESC key will:
  678. - stop a running search
  679. - close the search window if no search is running
  680. */
  681. if (!search_window.is_searching) {
  682. gtk_widget_hide(search_window.window);
  683. } else {
  684. search_window.is_searching = FALSE;
  685. }
  686. }
  687. return FALSE;
  688. }