sync with sylpheed 0.7.1cvs9
[claws.git] / src / summary_search.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 "mainwindow.h"
48 #include "utils.h"
49 #include "gtkutils.h"
50 #include "manage_window.h"
51 #include "alertpanel.h"
52
53 static GtkWidget *window;
54 static GtkWidget *from_entry;
55 static GtkWidget *to_entry;
56 static GtkWidget *subject_entry;
57 static GtkWidget *body_entry;
58 static GtkWidget *case_checkbtn;
59 static GtkWidget *backward_checkbtn;
60 static GtkWidget *all_checkbtn;
61 static GtkWidget *search_btn;
62 static GtkWidget *clear_btn;
63 static GtkWidget *close_btn;
64
65 static void summary_search_create(SummaryView *summaryview);
66 static void summary_search_execute(GtkButton *button, gpointer data);
67 static void summary_search_clear(GtkButton *button, gpointer data);
68 static void from_activated(void);
69 static void to_activated(void);
70 static void subject_activated(void);
71 static void body_activated(void);
72 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
73
74 void summary_search(SummaryView *summaryview)
75 {
76         if (!window)
77                 summary_search_create(summaryview);
78         else
79                 gtk_widget_hide(window);
80
81         gtk_widget_grab_focus(search_btn);
82         gtk_widget_grab_focus(subject_entry);
83         gtk_widget_show(window);
84 }
85
86 static void summary_search_create(SummaryView *summaryview)
87 {
88         GtkWidget *vbox1;
89         GtkWidget *table1;
90         GtkWidget *from_label;
91         GtkWidget *to_label;
92         GtkWidget *subject_label;
93         GtkWidget *body_label;
94         GtkWidget *checkbtn_hbox;
95         GtkWidget *confirm_area;
96
97         window = gtk_window_new (GTK_WINDOW_DIALOG);
98         gtk_window_set_title (GTK_WINDOW (window), _("Search messages"));
99         gtk_widget_set_usize (window, 450, -1);
100         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
101         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
102         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
103                            GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete), NULL);
104         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
105                            GTK_SIGNAL_FUNC(key_pressed), NULL);
106         gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
107                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
108         gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
109                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
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         gtk_signal_connect(GTK_OBJECT(from_entry), "activate",
127                            GTK_SIGNAL_FUNC(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         gtk_signal_connect(GTK_OBJECT(to_entry), "activate",
134                            GTK_SIGNAL_FUNC(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         gtk_signal_connect(GTK_OBJECT(subject_entry), "activate",
141                            GTK_SIGNAL_FUNC(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         gtk_signal_connect(GTK_OBJECT(body_entry), "activate",
148                            GTK_SIGNAL_FUNC(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_widget_show (case_checkbtn);
185         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
186                             FALSE, FALSE, 0);
187
188         backward_checkbtn =
189                 gtk_check_button_new_with_label (_("Backward search"));
190         gtk_widget_show (backward_checkbtn);
191         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), backward_checkbtn,
192                             FALSE, FALSE, 0);
193
194         all_checkbtn =
195                 gtk_check_button_new_with_label (_("Select all matched"));
196         gtk_widget_show (all_checkbtn);
197         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), all_checkbtn,
198                             FALSE, FALSE, 0);
199
200         gtkut_button_set_create(&confirm_area,
201                                 &search_btn, _("Search"),
202                                 &clear_btn,  _("Clear"),
203                                 &close_btn,  _("Close"));
204         gtk_widget_show (confirm_area);
205         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
206         gtk_widget_grab_default(search_btn);
207
208         gtk_signal_connect(GTK_OBJECT(search_btn), "clicked",
209                            GTK_SIGNAL_FUNC(summary_search_execute),
210                            summaryview);
211         gtk_signal_connect(GTK_OBJECT(clear_btn), "clicked",
212                            GTK_SIGNAL_FUNC(summary_search_clear),
213                            summaryview);
214         gtk_signal_connect_object(GTK_OBJECT(close_btn), "clicked",
215                                   GTK_SIGNAL_FUNC(gtk_widget_hide),
216                                   GTK_OBJECT(window));
217 }
218
219 static void summary_search_execute(GtkButton *button, gpointer data)
220 {
221         SummaryView *summaryview = data;
222         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
223         GtkCTreeNode *node;
224         MsgInfo *msginfo;
225         gboolean case_sens;
226         gboolean backward;
227         gboolean search_all;
228         gboolean all_searched = FALSE;
229         gboolean from_matched;
230         gboolean   to_matched;
231         gboolean subj_matched;
232         gboolean body_matched;
233         gchar *body_str;
234         wchar_t *wcs_hs, *fromwcs, *towcs, *subjwcs;
235         wchar_t *(* WCSFindFunc) (const wchar_t *haystack,
236                                   const wchar_t *needle);
237
238         case_sens = gtk_toggle_button_get_active
239                 (GTK_TOGGLE_BUTTON(case_checkbtn));
240         backward = gtk_toggle_button_get_active
241                 (GTK_TOGGLE_BUTTON(backward_checkbtn));
242         search_all = gtk_toggle_button_get_active
243                 (GTK_TOGGLE_BUTTON(all_checkbtn));
244
245         if (case_sens)
246 #if HAVE_WCSSTR
247                 WCSFindFunc = wcsstr;
248 #else
249 #if HAVE_WCSWCS
250                 WCSFindFunc = wcswcs;
251 #else
252                 WCSFindFunc = wcscasestr;
253 #endif
254 #endif /* HAVE_WCSSTR */
255         else
256                 WCSFindFunc = wcscasestr;
257
258         fromwcs = (wchar_t *)GTK_ENTRY(from_entry)->text;
259         towcs   = (wchar_t *)GTK_ENTRY(to_entry)->text;
260         subjwcs = (wchar_t *)GTK_ENTRY(subject_entry)->text;
261         body_str = gtk_entry_get_text(GTK_ENTRY(body_entry));
262
263         if (search_all) {
264                 gtk_clist_freeze(GTK_CLIST(ctree));
265                 gtk_clist_unselect_all(GTK_CLIST(ctree));
266                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
267         } else if (!summaryview->selected) {
268                 if (backward)
269                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
270                 else
271                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
272
273                 if (!node) return;
274         } else {
275                 if (backward)
276                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
277                 else
278                         node = GTK_CTREE_NODE_NEXT(summaryview->selected);
279         }
280
281         if (*body_str)
282                 main_window_cursor_wait(summaryview->mainwin);
283
284         for (;;) {
285                 if (!node) {
286                         gchar *str;
287                         AlertValue val;
288
289                         if (search_all) {
290                                 gtk_clist_thaw(GTK_CLIST(ctree));
291                                 break;
292                         }
293
294                         if (all_searched) {
295                                 alertpanel_message
296                                         (_("Search failed"),
297                                          _("Search string not found."));
298                                 break;
299                         }
300
301                         if (backward)
302                                 str = _("Beginning of list reached; continue from end?");
303                         else
304                                 str = _("End of list reached; continue from beginning?");
305
306                         val = alertpanel(_("Search finished"), str,
307                                          _("Yes"), _("No"), NULL);
308                         if (G_ALERTDEFAULT == val) {
309                                 if (backward)
310                                         node = GTK_CTREE_NODE
311                                                 (GTK_CLIST(ctree)->row_list_end);
312                                 else
313                                         node = GTK_CTREE_NODE
314                                                 (GTK_CLIST(ctree)->row_list);
315
316                                 all_searched = TRUE;
317
318                                 manage_window_focus_in(window, NULL, NULL);
319                         } else
320                                 break;
321                 }
322
323                 from_matched = to_matched = subj_matched = body_matched = FALSE;
324
325                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
326
327                 if (*fromwcs && msginfo->from) {
328                         wcs_hs = strdup_mbstowcs(msginfo->from);
329                         if (wcs_hs && WCSFindFunc(wcs_hs, fromwcs) != NULL)
330                                 from_matched = TRUE;
331                         g_free(wcs_hs);
332                 }
333                 if (*towcs && msginfo->to) {
334                         wcs_hs = strdup_mbstowcs(msginfo->to);
335                         if (wcs_hs && WCSFindFunc(wcs_hs, towcs) != NULL)
336                                 to_matched = TRUE;
337                         g_free(wcs_hs);
338                 }
339                 if (*subjwcs && msginfo->subject) {
340                         wcs_hs = strdup_mbstowcs(msginfo->subject);
341                         if (wcs_hs && WCSFindFunc(wcs_hs, subjwcs) != NULL)
342                                 subj_matched = TRUE;
343                         g_free(wcs_hs);
344                 }
345                 if (*body_str) {
346                         if (procmime_find_string(msginfo, body_str, case_sens))
347                                 body_matched = TRUE;
348                 }
349
350                 if (from_matched || to_matched || subj_matched || body_matched) {
351                         if (search_all)
352                                 gtk_ctree_select(ctree, node);
353                         else {
354                                 if (summaryview->msg_is_toggled_on) {
355                                         summary_select_node
356                                                 (summaryview, node, TRUE, FALSE);
357                                         if (body_matched) {
358                                                 messageview_search_string
359                                                         (summaryview->messageview,
360                                                          body_str, case_sens);
361                                         }
362                                 } else {
363                                         summary_select_node
364                                                 (summaryview, node, FALSE, FALSE);
365                                 }
366                                 break;
367                         }
368                 }
369
370                 node = backward ? GTK_CTREE_NODE_PREV(node)
371                                 : GTK_CTREE_NODE_NEXT(node);
372         }
373
374         if (*body_str)
375                 main_window_cursor_normal(summaryview->mainwin);
376 }
377
378 static void summary_search_clear(GtkButton *button, gpointer data)
379 {
380         gtk_editable_delete_text(GTK_EDITABLE(from_entry),    0, -1);
381         gtk_editable_delete_text(GTK_EDITABLE(to_entry),      0, -1);
382         gtk_editable_delete_text(GTK_EDITABLE(subject_entry), 0, -1);
383         gtk_editable_delete_text(GTK_EDITABLE(body_entry),    0, -1);
384 }
385
386 static void from_activated(void)
387 {
388         gtk_widget_grab_focus(to_entry);
389 }
390
391 static void to_activated(void)
392 {
393         gtk_widget_grab_focus(subject_entry);
394 }
395
396 static void subject_activated(void)
397 {
398         gtk_button_clicked(GTK_BUTTON(search_btn));
399 }
400
401 static void body_activated(void)
402 {
403         gtk_button_clicked(GTK_BUTTON(search_btn));
404 }
405
406 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
407 {
408         if (event && event->keyval == GDK_Escape)
409                 gtk_widget_hide(window);
410 }