b5c59e11a59767b2b3bb2fbaf4a5eb77fd879db0
[claws.git] / src / summary_search.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtkwidget.h>
30 #include <gtk/gtkwindow.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtktable.h>
33 #include <gtk/gtkoptionmenu.h>
34 #include <gtk/gtklabel.h>
35 #include <gtk/gtkentry.h>
36 #include <gtk/gtkhbox.h>
37 #include <gtk/gtkcheckbutton.h>
38 #include <gtk/gtkhbbox.h>
39 #include <gtk/gtkbutton.h>
40 #include <gtk/gtkctree.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include "main.h"
46 #include "summary_search.h"
47 #include "summaryview.h"
48 #include "messageview.h"
49 #include "mainwindow.h"
50 #include "menu.h"
51 #include "utils.h"
52 #include "gtkutils.h"
53 #include "manage_window.h"
54 #include "alertpanel.h"
55
56 static struct SummarySearchWindow {
57         GtkWidget *window;
58
59         GtkWidget *bool_optmenu;
60
61         GtkWidget *from_entry;
62         GtkWidget *to_entry;
63         GtkWidget *subject_entry;
64         GtkWidget *body_entry;
65
66         GtkWidget *case_checkbtn;
67
68         GtkWidget *clear_btn;
69         GtkWidget *all_btn;
70         GtkWidget *prev_btn;
71         GtkWidget *next_btn;
72         GtkWidget *close_btn;
73
74         SummaryView *summaryview;
75 } search_window;
76
77 static void summary_search_create       (void);
78
79 static void summary_search_execute      (gboolean        backward,
80                                          gboolean        search_all);
81
82 static void summary_search_clear        (GtkButton      *button,
83                                          gpointer        data);
84 static void summary_search_prev_clicked (GtkButton      *button,
85                                          gpointer        data);
86 static void summary_search_next_clicked (GtkButton      *button,
87                                          gpointer        data);
88 static void summary_search_all_clicked  (GtkButton      *button,
89                                          gpointer        data);
90
91 static void from_activated              (void);
92 static void to_activated                (void);
93 static void subject_activated           (void);
94 static void body_activated              (void);
95
96 static gboolean key_pressed             (GtkWidget      *widget,
97                                          GdkEventKey    *event,
98                                          gpointer        data);
99
100
101 void summary_search(SummaryView *summaryview)
102 {
103         if (!search_window.window)
104                 summary_search_create();
105         else
106                 gtk_widget_hide(search_window.window);
107
108         search_window.summaryview = summaryview;
109
110         gtk_widget_grab_focus(search_window.next_btn);
111         gtk_widget_grab_focus(search_window.subject_entry);
112         gtk_widget_show(search_window.window);
113 }
114
115 static void summary_search_create(void)
116 {
117         GtkWidget *window;
118         GtkWidget *vbox1;
119         GtkWidget *bool_hbox;
120         GtkWidget *bool_optmenu;
121         GtkWidget *bool_menu;
122         GtkWidget *menuitem;
123         GtkWidget *clear_btn;
124
125         GtkWidget *table1;
126         GtkWidget *from_label;
127         GtkWidget *from_entry;
128         GtkWidget *to_label;
129         GtkWidget *to_entry;
130         GtkWidget *subject_label;
131         GtkWidget *subject_entry;
132         GtkWidget *body_label;
133         GtkWidget *body_entry;
134
135         GtkWidget *checkbtn_hbox;
136         GtkWidget *case_checkbtn;
137
138         GtkWidget *confirm_area;
139         GtkWidget *all_btn;
140         GtkWidget *prev_btn;
141         GtkWidget *next_btn;
142         GtkWidget *close_btn;
143
144         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
145         gtk_window_set_title(GTK_WINDOW (window), _("Search messages"));
146         gtk_widget_set_size_request(window, 450, -1);
147         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
148         gtk_container_set_border_width(GTK_CONTAINER (window), 8);
149         g_signal_connect(G_OBJECT(window), "delete_event",
150                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
151         g_signal_connect(G_OBJECT(window), "key_press_event",
152                          G_CALLBACK(key_pressed), NULL);
153         MANAGE_WINDOW_SIGNALS_CONNECT(window);
154
155         vbox1 = gtk_vbox_new (FALSE, 0);
156         gtk_widget_show (vbox1);
157         gtk_container_add (GTK_CONTAINER (window), vbox1);
158
159         bool_hbox = gtk_hbox_new(FALSE, 4);
160         gtk_widget_show(bool_hbox);
161         gtk_box_pack_start(GTK_BOX(vbox1), bool_hbox, FALSE, FALSE, 0);
162
163         bool_optmenu = gtk_option_menu_new();
164         gtk_widget_show(bool_optmenu);
165         gtk_box_pack_start(GTK_BOX(bool_hbox), bool_optmenu, FALSE, FALSE, 0);
166
167         bool_menu = gtk_menu_new();
168         MENUITEM_ADD(bool_menu, menuitem, _("Match any of the following"), 0);
169         MENUITEM_ADD(bool_menu, menuitem, _("Match all of the following"), 1);
170         gtk_option_menu_set_menu(GTK_OPTION_MENU(bool_optmenu), bool_menu);
171
172         clear_btn = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
173         gtk_widget_show(clear_btn);
174         gtk_box_pack_end(GTK_BOX(bool_hbox), clear_btn, FALSE, FALSE, 0);
175
176         table1 = gtk_table_new (4, 3, FALSE);
177         gtk_widget_show (table1);
178         gtk_box_pack_start (GTK_BOX (vbox1), table1, TRUE, TRUE, 0);
179         gtk_container_set_border_width (GTK_CONTAINER (table1), 4);
180         gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
181         gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
182
183         from_entry = gtk_entry_new ();
184         gtk_widget_show (from_entry);
185         gtk_table_attach (GTK_TABLE (table1), from_entry, 1, 3, 0, 1,
186                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
187         g_signal_connect(G_OBJECT(from_entry), "activate",
188                          G_CALLBACK(from_activated), NULL);
189
190         to_entry = gtk_entry_new ();
191         gtk_widget_show (to_entry);
192         gtk_table_attach (GTK_TABLE (table1), to_entry, 1, 3, 1, 2,
193                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
194         g_signal_connect(G_OBJECT(to_entry), "activate",
195                          G_CALLBACK(to_activated), NULL);
196
197         subject_entry = gtk_entry_new ();
198         gtk_widget_show (subject_entry);
199         gtk_table_attach (GTK_TABLE (table1), subject_entry, 1, 3, 2, 3,
200                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
201         g_signal_connect(G_OBJECT(subject_entry), "activate",
202                          G_CALLBACK(subject_activated), NULL);
203
204         body_entry = gtk_entry_new ();
205         gtk_widget_show (body_entry);
206         gtk_table_attach (GTK_TABLE (table1), body_entry, 1, 3, 3, 4,
207                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
208         g_signal_connect(G_OBJECT(body_entry), "activate",
209                          G_CALLBACK(body_activated), NULL);
210
211         from_label = gtk_label_new (_("From:"));
212         gtk_widget_show (from_label);
213         gtk_table_attach (GTK_TABLE (table1), from_label, 0, 1, 0, 1,
214                           GTK_FILL, 0, 0, 0);
215         gtk_label_set_justify (GTK_LABEL (from_label), GTK_JUSTIFY_RIGHT);
216         gtk_misc_set_alignment (GTK_MISC (from_label), 1, 0.5);
217
218         to_label = gtk_label_new (_("To:"));
219         gtk_widget_show (to_label);
220         gtk_table_attach (GTK_TABLE (table1), to_label, 0, 1, 1, 2,
221                           GTK_FILL, 0, 0, 0);
222         gtk_label_set_justify (GTK_LABEL (to_label), GTK_JUSTIFY_RIGHT);
223         gtk_misc_set_alignment (GTK_MISC (to_label), 1, 0.5);
224
225         subject_label = gtk_label_new (_("Subject:"));
226         gtk_widget_show (subject_label);
227         gtk_table_attach (GTK_TABLE (table1), subject_label, 0, 1, 2, 3,
228                           GTK_FILL, 0, 0, 0);
229         gtk_label_set_justify (GTK_LABEL (subject_label), GTK_JUSTIFY_RIGHT);
230         gtk_misc_set_alignment (GTK_MISC (subject_label), 1, 0.5);
231
232         body_label = gtk_label_new (_("Body:"));
233         gtk_widget_show (body_label);
234         gtk_table_attach (GTK_TABLE (table1), body_label, 0, 1, 3, 4,
235                           GTK_FILL, 0, 0, 0);
236         gtk_label_set_justify (GTK_LABEL (body_label), GTK_JUSTIFY_RIGHT);
237         gtk_misc_set_alignment (GTK_MISC (body_label), 1, 0.5);
238
239         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
240         gtk_widget_show (checkbtn_hbox);
241         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
242         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
243
244         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
245         gtk_widget_show (case_checkbtn);
246         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
247                             FALSE, FALSE, 0);
248
249         confirm_area = gtk_hbutton_box_new();
250         gtk_widget_show (confirm_area);
251         gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
252                                   GTK_BUTTONBOX_END);
253         gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
254
255         all_btn = gtk_button_new_with_mnemonic(_("Find _all"));
256         GTK_WIDGET_SET_FLAGS(all_btn, GTK_CAN_DEFAULT);
257         gtk_box_pack_start(GTK_BOX(confirm_area), all_btn, TRUE, TRUE, 0);
258         gtk_widget_show(all_btn);
259
260         prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
261         GTK_WIDGET_SET_FLAGS(prev_btn, GTK_CAN_DEFAULT);
262         gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
263         gtk_widget_show(prev_btn);
264
265         next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
266         GTK_WIDGET_SET_FLAGS(next_btn, GTK_CAN_DEFAULT);
267         gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
268         gtk_widget_show(next_btn);
269
270         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
271         GTK_WIDGET_SET_FLAGS(close_btn, GTK_CAN_DEFAULT);
272         gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
273         gtk_widget_show(close_btn);
274
275         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
276         gtk_widget_grab_default(next_btn);
277
278         g_signal_connect(G_OBJECT(clear_btn), "clicked",
279                          G_CALLBACK(summary_search_clear), NULL);
280         g_signal_connect(G_OBJECT(all_btn), "clicked",
281                          G_CALLBACK(summary_search_all_clicked), NULL);
282         g_signal_connect(G_OBJECT(prev_btn), "clicked",
283                          G_CALLBACK(summary_search_prev_clicked), NULL);
284         g_signal_connect(G_OBJECT(next_btn), "clicked",
285                          G_CALLBACK(summary_search_next_clicked), NULL);
286         g_signal_connect_closure
287                 (G_OBJECT(close_btn), "clicked",
288                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
289                                      window, NULL),
290                  FALSE);
291
292         search_window.window = window;
293         search_window.bool_optmenu = bool_optmenu;
294         search_window.from_entry = from_entry;
295         search_window.to_entry = to_entry;
296         search_window.subject_entry = subject_entry;
297         search_window.body_entry = body_entry;
298         search_window.case_checkbtn = case_checkbtn;
299         search_window.clear_btn = clear_btn;
300         search_window.all_btn = all_btn;
301         search_window.prev_btn = prev_btn;
302         search_window.next_btn = next_btn;
303         search_window.close_btn = close_btn;
304 }
305
306 static void summary_search_execute(gboolean backward, gboolean search_all)
307 {
308         SummaryView *summaryview = search_window.summaryview;
309         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
310         GtkCTreeNode *node;
311         MsgInfo *msginfo;
312         gboolean bool_and;
313         gboolean case_sens;
314         gboolean all_searched = FALSE;
315         gboolean matched;
316         gboolean body_matched;
317         const gchar *from_str, *to_str, *subject_str, *body_str;
318         StrFindFunc str_find_func;
319
320         if (summary_is_locked(summaryview)) return;
321         summary_lock(summaryview);
322
323         bool_and = GPOINTER_TO_INT
324                 (menu_get_option_menu_active_user_data
325                         (GTK_OPTION_MENU(search_window.bool_optmenu)));
326         case_sens = gtk_toggle_button_get_active
327                 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
328
329         if (case_sens)
330                 str_find_func = str_find;
331         else
332                 str_find_func = str_case_find;
333
334         from_str    = gtk_entry_get_text(GTK_ENTRY(search_window.from_entry));
335         to_str      = gtk_entry_get_text(GTK_ENTRY(search_window.to_entry));
336         subject_str = gtk_entry_get_text(GTK_ENTRY(search_window.subject_entry));
337         body_str    = gtk_entry_get_text(GTK_ENTRY(search_window.body_entry));
338
339         if (search_all) {
340                 gtk_clist_freeze(GTK_CLIST(ctree));
341                 gtk_clist_unselect_all(GTK_CLIST(ctree));
342                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
343                 backward = FALSE;
344         } else if (!summaryview->selected) {
345                 if (backward)
346                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
347                 else
348                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
349
350                 if (!node) {
351                         summary_unlock(summaryview);
352                         return;
353                 }
354         } else {
355                 if (backward)
356                         node = gtkut_ctree_node_prev
357                                 (ctree, summaryview->selected);
358                 else
359                         node = gtkut_ctree_node_next
360                                 (ctree, summaryview->selected);
361         }
362
363         if (*body_str)
364                 main_window_cursor_wait(summaryview->mainwin);
365
366         for (;;) {
367                 if (!node) {
368                         gchar *str;
369                         AlertValue val;
370
371                         if (search_all) {
372                                 gtk_clist_thaw(GTK_CLIST(ctree));
373                                 break;
374                         }
375
376                         if (all_searched) {
377                                 alertpanel_full(_("Search failed"),
378                                                 _("Search string not found."),
379                                                 GTK_STOCK_CLOSE, NULL, NULL, FALSE,
380                                                 NULL, ALERT_WARNING, G_ALERTDEFAULT);
381                                 break;
382                         }
383
384                         if (backward)
385                                 str = _("Beginning of list reached; continue from end?");
386                         else
387                                 str = _("End of list reached; continue from beginning?");
388
389                         val = alertpanel(_("Search finished"), str,
390                                          GTK_STOCK_NO, GTK_STOCK_YES, NULL);
391                         if (G_ALERTALTERNATE == val) {
392                                 if (backward)
393                                         node = GTK_CTREE_NODE
394                                                 (GTK_CLIST(ctree)->row_list_end);
395                                 else
396                                         node = GTK_CTREE_NODE
397                                                 (GTK_CLIST(ctree)->row_list);
398
399                                 all_searched = TRUE;
400
401                                 manage_window_focus_in(search_window.window, NULL, NULL);
402                         } else
403                                 break;
404                 }
405
406
407                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
408                 body_matched = FALSE;
409
410                 if (bool_and) {
411                         matched = TRUE;
412                         if (*from_str) {
413                                 if (!msginfo->from ||
414                                     !str_find_func(msginfo->from, from_str))
415                                         matched = FALSE;
416                         }
417                         if (matched && *to_str) {
418                                 if (!msginfo->to ||
419                                     !str_find_func(msginfo->to, to_str))
420                                         matched = FALSE;
421                         }
422                         if (matched && *subject_str) {
423                                 if (!msginfo->subject ||
424                                     !str_find_func(msginfo->subject, subject_str))
425                                         matched = FALSE;
426                         }
427                         if (matched && *body_str) {
428                                 if (procmime_find_string(msginfo, body_str,
429                                                          str_find_func))
430                                         body_matched = TRUE;
431                                 else
432                                         matched = FALSE;
433                         }
434                         if (matched && !*from_str && !*to_str &&
435                             !*subject_str && !*body_str)
436                                 matched = FALSE;
437                 } else {
438                         matched = FALSE;
439                         if (*from_str && msginfo->from) {
440                                 if (str_find_func(msginfo->from, from_str))
441                                         matched = TRUE;
442                         }
443                         if (!matched && *to_str && msginfo->to) {
444                                 if (str_find_func(msginfo->to, to_str))
445                                         matched = TRUE;
446                         }
447                         if (!matched && *subject_str && msginfo->subject) {
448                                 if (str_find_func(msginfo->subject, subject_str))
449                                         matched = TRUE;
450                         }
451                         if (!matched && *body_str) {
452                                 if (procmime_find_string(msginfo, body_str,
453                                                          str_find_func)) {
454                                         matched = TRUE;
455                                         body_matched = TRUE;
456                                 }
457                         }
458                 }
459
460                 if (matched) {
461                         if (search_all)
462                                 gtk_ctree_select(ctree, node);
463                         else {
464                                 if (messageview_is_visible
465                                         (summaryview->messageview)) {
466                                         summary_unlock(summaryview);
467                                         summary_select_node
468                                                 (summaryview, node, TRUE, TRUE);
469                                         summary_lock(summaryview);
470                                         if (body_matched) {
471                                                 messageview_search_string
472                                                         (summaryview->messageview,
473                                                          body_str, case_sens);
474                                         }
475                                 } else {
476                                         summary_select_node
477                                                 (summaryview, node, FALSE, TRUE);
478                                 }
479                                 break;
480                         }
481                 }
482
483                 node = backward ? gtkut_ctree_node_prev(ctree, node)
484                                 : gtkut_ctree_node_next(ctree, node);
485         }
486
487         if (*body_str)
488                 main_window_cursor_normal(summaryview->mainwin);
489
490         summary_unlock(summaryview);
491 }
492
493 static void summary_search_clear(GtkButton *button, gpointer data)
494 {
495         gtk_editable_delete_text(GTK_EDITABLE(search_window.from_entry),
496                                  0, -1);
497         gtk_editable_delete_text(GTK_EDITABLE(search_window.to_entry),
498                                  0, -1);
499         gtk_editable_delete_text(GTK_EDITABLE(search_window.subject_entry),
500                                  0, -1);
501         gtk_editable_delete_text(GTK_EDITABLE(search_window.body_entry),
502                                  0, -1);
503 }
504
505 static void summary_search_prev_clicked(GtkButton *button, gpointer data)
506 {
507         summary_search_execute(TRUE, FALSE);
508 }
509
510 static void summary_search_next_clicked(GtkButton *button, gpointer data)
511 {
512         summary_search_execute(FALSE, FALSE);
513 }
514
515 static void summary_search_all_clicked(GtkButton *button, gpointer data)
516 {
517         summary_search_execute(FALSE, TRUE);
518 }
519
520 static void from_activated(void)
521 {
522         gtk_widget_grab_focus(search_window.to_entry);
523 }
524
525 static void to_activated(void)
526 {
527         gtk_widget_grab_focus(search_window.subject_entry);
528 }
529
530 static void subject_activated(void)
531 {
532         gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
533 }
534
535 static void body_activated(void)
536 {
537         gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
538 }
539
540 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
541                             gpointer data)
542 {
543         if (event && event->keyval == GDK_Escape)
544                 gtk_widget_hide(search_window.window);
545         return FALSE;
546 }