c4e524aa8247c8ef855080025036a43fe93d787d
[claws.git] / src / summary_search.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 <gdk/gdkkeysyms.h>
28 #include <gtk/gtkwidget.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtkvbox.h>
31 #include <gtk/gtktable.h>
32 #include <gtk/gtklabel.h>
33 #include <gtk/gtkentry.h>
34 #include <gtk/gtkhbox.h>
35 #include <gtk/gtkcheckbutton.h>
36 #include <gtk/gtkhbbox.h>
37 #include <gtk/gtkbutton.h>
38 #include <gtk/gtkctree.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include "intl.h"
44 #include "main.h"
45 #include "summary_search.h"
46 #include "summaryview.h"
47 #include "messageview.h"
48 #include "mainwindow.h"
49 #include "utils.h"
50 #include "gtkutils.h"
51 #include "manage_window.h"
52 #include "alertpanel.h"
53
54 static GtkWidget *window;
55 static GtkWidget *from_entry;
56 static GtkWidget *to_entry;
57 static GtkWidget *subject_entry;
58 static GtkWidget *body_entry;
59 static GtkWidget *case_checkbtn;
60 static GtkWidget *backward_checkbtn;
61 static GtkWidget *all_checkbtn;
62 static GtkWidget *and_checkbtn;
63 static GtkWidget *search_btn;
64 static GtkWidget *clear_btn;
65 static GtkWidget *close_btn;
66
67 static void summary_search_create(SummaryView *summaryview);
68 static void summary_search_execute(GtkButton *button, gpointer data);
69 static void summary_search_clear(GtkButton *button, gpointer data);
70 static void from_activated(void);
71 static void to_activated(void);
72 static void subject_activated(void);
73 static void body_activated(void);
74 static gboolean all_clicked(GtkButton *button);
75 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
76
77 void summary_search(SummaryView *summaryview)
78 {
79         if (!window)
80                 summary_search_create(summaryview);
81         else
82                 gtk_widget_hide(window);
83
84         gtk_widget_grab_focus(search_btn);
85         gtk_widget_grab_focus(subject_entry);
86         gtk_widget_show(window);
87 }
88
89 static void summary_search_create(SummaryView *summaryview)
90 {
91         GtkWidget *vbox1;
92         GtkWidget *table1;
93         GtkWidget *from_label;
94         GtkWidget *to_label;
95         GtkWidget *subject_label;
96         GtkWidget *body_label;
97         GtkWidget *checkbtn_hbox;
98         GtkWidget *confirm_area;
99
100         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
101         gtk_window_set_title (GTK_WINDOW (window), _("Search messages"));
102         gtk_widget_set_size_request (window, 450, -1);
103         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
104         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
105         g_signal_connect(G_OBJECT(window), "delete_event",
106                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
107         g_signal_connect(G_OBJECT(window), "key_press_event",
108                          G_CALLBACK(key_pressed), NULL);
109         MANAGE_WINDOW_SIGNALS_CONNECT(window);
110
111         vbox1 = gtk_vbox_new (FALSE, 0);
112         gtk_widget_show (vbox1);
113         gtk_container_add (GTK_CONTAINER (window), vbox1);
114
115         table1 = gtk_table_new (4, 3, FALSE);
116         gtk_widget_show (table1);
117         gtk_box_pack_start (GTK_BOX (vbox1), table1, TRUE, TRUE, 0);
118         gtk_container_set_border_width (GTK_CONTAINER (table1), 4);
119         gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
120         gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
121
122         from_entry = gtk_entry_new ();
123         gtk_widget_show (from_entry);
124         gtk_table_attach (GTK_TABLE (table1), from_entry, 1, 3, 0, 1,
125                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
126         g_signal_connect(G_OBJECT(from_entry), "activate",
127                          G_CALLBACK(from_activated), summaryview);
128
129         to_entry = gtk_entry_new ();
130         gtk_widget_show (to_entry);
131         gtk_table_attach (GTK_TABLE (table1), to_entry, 1, 3, 1, 2,
132                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
133         g_signal_connect(G_OBJECT(to_entry), "activate",
134                          G_CALLBACK(to_activated), summaryview);
135
136         subject_entry = gtk_entry_new ();
137         gtk_widget_show (subject_entry);
138         gtk_table_attach (GTK_TABLE (table1), subject_entry, 1, 3, 2, 3,
139                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
140         g_signal_connect(G_OBJECT(subject_entry), "activate",
141                          G_CALLBACK(subject_activated), summaryview);
142
143         body_entry = gtk_entry_new ();
144         gtk_widget_show (body_entry);
145         gtk_table_attach (GTK_TABLE (table1), body_entry, 1, 3, 3, 4,
146                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
147         g_signal_connect(G_OBJECT(body_entry), "activate",
148                          G_CALLBACK(body_activated), summaryview);
149
150         from_label = gtk_label_new (_("From:"));
151         gtk_widget_show (from_label);
152         gtk_table_attach (GTK_TABLE (table1), from_label, 0, 1, 0, 1,
153                           GTK_FILL, 0, 0, 0);
154         gtk_label_set_justify (GTK_LABEL (from_label), GTK_JUSTIFY_RIGHT);
155         gtk_misc_set_alignment (GTK_MISC (from_label), 1, 0.5);
156
157         to_label = gtk_label_new (_("To:"));
158         gtk_widget_show (to_label);
159         gtk_table_attach (GTK_TABLE (table1), to_label, 0, 1, 1, 2,
160                           GTK_FILL, 0, 0, 0);
161         gtk_label_set_justify (GTK_LABEL (to_label), GTK_JUSTIFY_RIGHT);
162         gtk_misc_set_alignment (GTK_MISC (to_label), 1, 0.5);
163
164         subject_label = gtk_label_new (_("Subject:"));
165         gtk_widget_show (subject_label);
166         gtk_table_attach (GTK_TABLE (table1), subject_label, 0, 1, 2, 3,
167                           GTK_FILL, 0, 0, 0);
168         gtk_label_set_justify (GTK_LABEL (subject_label), GTK_JUSTIFY_RIGHT);
169         gtk_misc_set_alignment (GTK_MISC (subject_label), 1, 0.5);
170
171         body_label = gtk_label_new (_("Body:"));
172         gtk_widget_show (body_label);
173         gtk_table_attach (GTK_TABLE (table1), body_label, 0, 1, 3, 4,
174                           GTK_FILL, 0, 0, 0);
175         gtk_label_set_justify (GTK_LABEL (body_label), GTK_JUSTIFY_RIGHT);
176         gtk_misc_set_alignment (GTK_MISC (body_label), 1, 0.5);
177
178         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
179         gtk_widget_show (checkbtn_hbox);
180         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
181         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
182
183         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
184         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(case_checkbtn), FALSE);
185         gtk_widget_show (case_checkbtn);
186         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
187                             FALSE, FALSE, 0);
188
189         backward_checkbtn =
190                 gtk_check_button_new_with_label (_("Backward search"));
191         gtk_widget_show (backward_checkbtn);
192         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), backward_checkbtn,
193                             FALSE, FALSE, 0);
194
195         all_checkbtn =
196                 gtk_check_button_new_with_label (_("Select all matched"));
197         gtk_widget_show (all_checkbtn);
198         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), all_checkbtn,
199                             FALSE, FALSE, 0);
200         g_signal_connect(G_OBJECT(all_checkbtn), "clicked",
201                          G_CALLBACK(all_clicked), summaryview);
202
203         and_checkbtn =
204                 gtk_check_button_new_with_label (_("AND search"));
205         gtk_widget_show (and_checkbtn);
206         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), and_checkbtn,
207                             FALSE, FALSE, 0);
208
209         gtkut_button_set_create(&confirm_area,
210                                 &search_btn, _("Search"),
211                                 &clear_btn,  _("Clear"),
212                                 &close_btn,  _("Close"));
213         gtk_widget_show (confirm_area);
214         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
215         gtk_widget_grab_default(search_btn);
216
217         g_signal_connect(G_OBJECT(search_btn), "clicked",
218                          G_CALLBACK(summary_search_execute),
219                          summaryview);
220         g_signal_connect(G_OBJECT(clear_btn), "clicked",
221                          G_CALLBACK(summary_search_clear),
222                          summaryview);
223         g_signal_connect_closure
224                 (G_OBJECT(close_btn), "clicked",
225                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
226                                      window, NULL),
227                  FALSE);
228 }
229
230 #define GET_ENTRY(entry) gtk_entry_get_text(GTK_ENTRY(entry))
231
232 static void summary_search_execute(GtkButton *button, gpointer data)
233 {
234         SummaryView *summaryview = data;
235         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
236         GtkCTreeNode *node;
237         MsgInfo *msginfo;
238         gboolean case_sens;
239         gboolean backward;
240         gboolean search_all;
241         gboolean search_and;
242         gboolean all_searched = FALSE;
243         gboolean all_matched = FALSE;
244         gboolean from_matched;
245         gboolean   to_matched;
246         gboolean subj_matched;
247         gboolean body_matched;
248         const gchar *body_str, *from_str, *to_str, *subj_str;
249         char *(*findfunc) (const char *haystack, const char *needle);
250
251         if (summary_is_locked(summaryview)) return;
252         summary_lock(summaryview);
253
254         case_sens = gtk_toggle_button_get_active
255                 (GTK_TOGGLE_BUTTON(case_checkbtn));
256         backward = gtk_toggle_button_get_active
257                 (GTK_TOGGLE_BUTTON(backward_checkbtn));
258         search_all = gtk_toggle_button_get_active
259                 (GTK_TOGGLE_BUTTON(all_checkbtn));
260         search_and = gtk_toggle_button_get_active
261                 (GTK_TOGGLE_BUTTON(and_checkbtn));
262         
263         if (case_sens)
264                 findfunc = strstr;
265         else
266                 findfunc = strcasestr;
267
268         from_str = GET_ENTRY(from_entry);
269         to_str   = GET_ENTRY(to_entry);
270         subj_str = GET_ENTRY(subject_entry);
271         body_str = GET_ENTRY(body_entry);
272
273         if (search_all) {
274                 gtk_clist_freeze(GTK_CLIST(ctree));
275                 gtk_clist_unselect_all(GTK_CLIST(ctree));
276                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
277                 backward = FALSE;
278         } else if (!summaryview->selected) {
279                 if (backward)
280                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
281                 else
282                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
283
284                 if (!node) {
285                         summary_unlock(summaryview);
286                         return;
287                 }
288         } else {
289                 if (backward)
290                         node = gtkut_ctree_node_prev
291                                 (ctree, summaryview->selected);
292                 else
293                         node = gtkut_ctree_node_next
294                                 (ctree, summaryview->selected);
295         }
296
297         if (*body_str)
298                 main_window_cursor_wait(summaryview->mainwin);
299
300         for (;;) {
301                 if (!node) {
302                         gchar *str;
303                         AlertValue val;
304
305                         if (search_all) {
306                                 gtk_clist_thaw(GTK_CLIST(ctree));
307                                 break;
308                         }
309
310                         if (all_searched) {
311                                 alertpanel_notice
312                                         (_("Search string not found."));
313                                 break;
314                         }
315
316                         if (backward)
317                                 str = _("Beginning of list reached; continue from end?");
318                         else
319                                 str = _("End of list reached; continue from beginning?");
320
321                         val = alertpanel(_("Search finished"), str,
322                                          _("Yes"), _("No"), NULL);
323                         if (G_ALERTDEFAULT == val) {
324                                 if (backward)
325                                         node = GTK_CTREE_NODE
326                                                 (GTK_CLIST(ctree)->row_list_end);
327                                 else
328                                         node = GTK_CTREE_NODE
329                                                 (GTK_CLIST(ctree)->row_list);
330
331                                 all_searched = TRUE;
332
333                                 manage_window_focus_in(window, NULL, NULL);
334                         } else
335                                 break;
336                 }
337
338                 from_matched = to_matched = subj_matched = body_matched = FALSE;
339                 all_matched = search_and;
340                 
341                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
342
343                 if (from_str && *from_str && msginfo->from) {
344                         if (findfunc(msginfo->from, from_str) != NULL)
345                                 from_matched = TRUE;
346                         else
347                                 all_matched = FALSE;
348                 }       
349                 if (to_str && *to_str && msginfo->to) {
350                         if (findfunc(msginfo->to, to_str) != NULL)
351                                 to_matched = TRUE;
352                         else
353                                 all_matched = FALSE;
354                 }       
355                 if (subj_str && *subj_str && msginfo->subject) {
356                         if (findfunc(msginfo->subject, subj_str) != NULL)
357                                 from_matched = TRUE;
358                         else
359                                 all_matched = FALSE;
360                 }       
361                 if (body_str && *body_str) {
362                         if (procmime_find_string(msginfo, body_str, case_sens))
363                                 body_matched = TRUE;
364                         else
365                                 all_matched = FALSE;
366                 }
367
368                 if ((from_matched || to_matched || subj_matched || body_matched)
369                     && (!search_and || all_matched)) {
370                         if (search_all)
371                                 gtk_ctree_select(ctree, node);
372                         else {
373                                 if (messageview_is_visible
374                                         (summaryview->messageview)) {
375                                         summary_unlock(summaryview);
376                                         summary_select_node
377                                                 (summaryview, node, TRUE, TRUE);
378                                         summary_lock(summaryview);
379                                         if (body_matched) {
380                                                 messageview_search_string
381                                                         (summaryview->messageview,
382                                                          body_str, case_sens);
383                                         }
384                                 } else {
385                                         summary_select_node
386                                                 (summaryview, node, FALSE, TRUE);
387                                 }
388                                 break;
389                         }
390                 }
391                 node = backward ? gtkut_ctree_node_prev(ctree, node)
392                                 : gtkut_ctree_node_next(ctree, node);
393         }
394
395         if (*body_str)
396                 main_window_cursor_normal(summaryview->mainwin);
397
398         summary_unlock(summaryview);
399 }
400
401 #undef GET_ENTRY
402
403 static void summary_search_clear(GtkButton *button, gpointer data)
404 {
405         gtk_editable_delete_text(GTK_EDITABLE(from_entry),    0, -1);
406         gtk_editable_delete_text(GTK_EDITABLE(to_entry),      0, -1);
407         gtk_editable_delete_text(GTK_EDITABLE(subject_entry), 0, -1);
408         gtk_editable_delete_text(GTK_EDITABLE(body_entry),    0, -1);
409 }
410
411 static void from_activated(void)
412 {
413         gtk_widget_grab_focus(to_entry);
414 }
415
416 static void to_activated(void)
417 {
418         gtk_widget_grab_focus(subject_entry);
419 }
420
421 static void subject_activated(void)
422 {
423         gtk_button_clicked(GTK_BUTTON(search_btn));
424 }
425
426 static void body_activated(void)
427 {
428         gtk_button_clicked(GTK_BUTTON(search_btn));
429 }
430
431 static gboolean all_clicked(GtkButton *button)
432 {
433         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
434                 gtk_widget_set_sensitive(backward_checkbtn, FALSE);
435         else
436                 gtk_widget_set_sensitive(backward_checkbtn, TRUE);
437         return FALSE;
438 }
439
440 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
441 {
442         if (event && event->keyval == GDK_Escape)
443                 gtk_widget_hide(window);
444         return FALSE;
445 }