* AUTHORS
[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 void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
75
76 void summary_search(SummaryView *summaryview)
77 {
78         if (!window)
79                 summary_search_create(summaryview);
80         else
81                 gtk_widget_hide(window);
82
83         gtk_widget_grab_focus(search_btn);
84         gtk_widget_grab_focus(subject_entry);
85         gtk_widget_show(window);
86 }
87
88 static void summary_search_create(SummaryView *summaryview)
89 {
90         GtkWidget *vbox1;
91         GtkWidget *table1;
92         GtkWidget *from_label;
93         GtkWidget *to_label;
94         GtkWidget *subject_label;
95         GtkWidget *body_label;
96         GtkWidget *checkbtn_hbox;
97         GtkWidget *confirm_area;
98
99         window = gtk_window_new (GTK_WINDOW_DIALOG);
100         gtk_window_set_title (GTK_WINDOW (window), _("Search messages"));
101         gtk_widget_set_usize (window, 450, -1);
102         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
103         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
104         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
105                            GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete), NULL);
106         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
107                            GTK_SIGNAL_FUNC(key_pressed), NULL);
108         MANAGE_WINDOW_SIGNALS_CONNECT(window);
109
110         vbox1 = gtk_vbox_new (FALSE, 0);
111         gtk_widget_show (vbox1);
112         gtk_container_add (GTK_CONTAINER (window), vbox1);
113
114         table1 = gtk_table_new (4, 3, FALSE);
115         gtk_widget_show (table1);
116         gtk_box_pack_start (GTK_BOX (vbox1), table1, TRUE, TRUE, 0);
117         gtk_container_set_border_width (GTK_CONTAINER (table1), 4);
118         gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
119         gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
120
121         from_entry = gtk_entry_new ();
122         gtk_widget_show (from_entry);
123         gtk_table_attach (GTK_TABLE (table1), from_entry, 1, 3, 0, 1,
124                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
125         gtk_signal_connect(GTK_OBJECT(from_entry), "activate",
126                            GTK_SIGNAL_FUNC(from_activated), summaryview);
127
128         to_entry = gtk_entry_new ();
129         gtk_widget_show (to_entry);
130         gtk_table_attach (GTK_TABLE (table1), to_entry, 1, 3, 1, 2,
131                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
132         gtk_signal_connect(GTK_OBJECT(to_entry), "activate",
133                            GTK_SIGNAL_FUNC(to_activated), summaryview);
134
135         subject_entry = gtk_entry_new ();
136         gtk_widget_show (subject_entry);
137         gtk_table_attach (GTK_TABLE (table1), subject_entry, 1, 3, 2, 3,
138                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
139         gtk_signal_connect(GTK_OBJECT(subject_entry), "activate",
140                            GTK_SIGNAL_FUNC(subject_activated), summaryview);
141
142         body_entry = gtk_entry_new ();
143         gtk_widget_show (body_entry);
144         gtk_table_attach (GTK_TABLE (table1), body_entry, 1, 3, 3, 4,
145                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
146         gtk_signal_connect(GTK_OBJECT(body_entry), "activate",
147                            GTK_SIGNAL_FUNC(body_activated), summaryview);
148
149         from_label = gtk_label_new (_("From:"));
150         gtk_widget_show (from_label);
151         gtk_table_attach (GTK_TABLE (table1), from_label, 0, 1, 0, 1,
152                           GTK_FILL, 0, 0, 0);
153         gtk_label_set_justify (GTK_LABEL (from_label), GTK_JUSTIFY_RIGHT);
154         gtk_misc_set_alignment (GTK_MISC (from_label), 1, 0.5);
155
156         to_label = gtk_label_new (_("To:"));
157         gtk_widget_show (to_label);
158         gtk_table_attach (GTK_TABLE (table1), to_label, 0, 1, 1, 2,
159                           GTK_FILL, 0, 0, 0);
160         gtk_label_set_justify (GTK_LABEL (to_label), GTK_JUSTIFY_RIGHT);
161         gtk_misc_set_alignment (GTK_MISC (to_label), 1, 0.5);
162
163         subject_label = gtk_label_new (_("Subject:"));
164         gtk_widget_show (subject_label);
165         gtk_table_attach (GTK_TABLE (table1), subject_label, 0, 1, 2, 3,
166                           GTK_FILL, 0, 0, 0);
167         gtk_label_set_justify (GTK_LABEL (subject_label), GTK_JUSTIFY_RIGHT);
168         gtk_misc_set_alignment (GTK_MISC (subject_label), 1, 0.5);
169
170         body_label = gtk_label_new (_("Body:"));
171         gtk_widget_show (body_label);
172         gtk_table_attach (GTK_TABLE (table1), body_label, 0, 1, 3, 4,
173                           GTK_FILL, 0, 0, 0);
174         gtk_label_set_justify (GTK_LABEL (body_label), GTK_JUSTIFY_RIGHT);
175         gtk_misc_set_alignment (GTK_MISC (body_label), 1, 0.5);
176
177         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
178         gtk_widget_show (checkbtn_hbox);
179         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
180         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
181
182         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
183         gtk_widget_show (case_checkbtn);
184         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
185                             FALSE, FALSE, 0);
186
187         backward_checkbtn =
188                 gtk_check_button_new_with_label (_("Backward search"));
189         gtk_widget_show (backward_checkbtn);
190         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), backward_checkbtn,
191                             FALSE, FALSE, 0);
192
193         all_checkbtn =
194                 gtk_check_button_new_with_label (_("Select all matched"));
195         gtk_widget_show (all_checkbtn);
196         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), all_checkbtn,
197                             FALSE, FALSE, 0);
198
199         and_checkbtn =
200                 gtk_check_button_new_with_label (_("AND search"));
201         gtk_widget_show (and_checkbtn);
202         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), and_checkbtn,
203                             FALSE, FALSE, 0);
204
205         gtkut_button_set_create(&confirm_area,
206                                 &search_btn, _("Search"),
207                                 &clear_btn,  _("Clear"),
208                                 &close_btn,  _("Close"));
209         gtk_widget_show (confirm_area);
210         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
211         gtk_widget_grab_default(search_btn);
212
213         gtk_signal_connect(GTK_OBJECT(search_btn), "clicked",
214                            GTK_SIGNAL_FUNC(summary_search_execute),
215                            summaryview);
216         gtk_signal_connect(GTK_OBJECT(clear_btn), "clicked",
217                            GTK_SIGNAL_FUNC(summary_search_clear),
218                            summaryview);
219         gtk_signal_connect_object(GTK_OBJECT(close_btn), "clicked",
220                                   GTK_SIGNAL_FUNC(gtk_widget_hide),
221                                   GTK_OBJECT(window));
222 }
223
224 static void summary_search_execute(GtkButton *button, gpointer data)
225 {
226         SummaryView *summaryview = data;
227         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
228         GtkCTreeNode *node;
229         MsgInfo *msginfo;
230         gboolean case_sens;
231         gboolean backward;
232         gboolean search_all;
233         gboolean search_and;
234         gboolean all_searched = FALSE;
235         gboolean all_matched = FALSE;
236         gboolean from_matched;
237         gboolean   to_matched;
238         gboolean subj_matched;
239         gboolean body_matched;
240         gchar *body_str;
241         wchar_t *wcs_hs, *fromwcs, *towcs, *subjwcs;
242         wchar_t *(* WCSFindFunc) (const wchar_t *haystack,
243                                   const wchar_t *needle);
244
245         if (summary_is_locked(summaryview)) return;
246         summary_lock(summaryview);
247
248         case_sens = gtk_toggle_button_get_active
249                 (GTK_TOGGLE_BUTTON(case_checkbtn));
250         backward = gtk_toggle_button_get_active
251                 (GTK_TOGGLE_BUTTON(backward_checkbtn));
252         search_all = gtk_toggle_button_get_active
253                 (GTK_TOGGLE_BUTTON(all_checkbtn));
254         search_and = gtk_toggle_button_get_active
255                 (GTK_TOGGLE_BUTTON(and_checkbtn));
256         
257         if (case_sens)
258 #if HAVE_WCSSTR
259                 WCSFindFunc = wcsstr;
260 #else
261 #if HAVE_WCSWCS
262                 WCSFindFunc = wcswcs;
263 #else
264                 WCSFindFunc = wcscasestr;
265 #endif
266 #endif /* HAVE_WCSSTR */
267         else
268                 WCSFindFunc = wcscasestr;
269
270         fromwcs = (wchar_t *)GTK_ENTRY(from_entry)->text;
271         towcs   = (wchar_t *)GTK_ENTRY(to_entry)->text;
272         subjwcs = (wchar_t *)GTK_ENTRY(subject_entry)->text;
273         body_str = gtk_entry_get_text(GTK_ENTRY(body_entry));
274
275         if (search_all) {
276                 gtk_clist_freeze(GTK_CLIST(ctree));
277                 gtk_clist_unselect_all(GTK_CLIST(ctree));
278                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
279         } else if (!summaryview->selected) {
280                 if (backward)
281                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
282                 else
283                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
284
285                 if (!node) {
286                         summary_unlock(summaryview);
287                         return;
288                 }
289         } else {
290                 if (backward)
291                         node = gtkut_ctree_node_prev
292                                 (ctree, summaryview->selected);
293                 else
294                         node = gtkut_ctree_node_next
295                                 (ctree, summaryview->selected);
296         }
297
298         if (*body_str)
299                 main_window_cursor_wait(summaryview->mainwin);
300
301         for (;;) {
302                 if (!node) {
303                         gchar *str;
304                         AlertValue val;
305
306                         if (search_all) {
307                                 gtk_clist_thaw(GTK_CLIST(ctree));
308                                 break;
309                         }
310
311                         if (all_searched) {
312                                 alertpanel_message
313                                         (_("Search failed"),
314                                          _("Search string not found."));
315                                 break;
316                         }
317
318                         if (backward)
319                                 str = _("Beginning of list reached; continue from end?");
320                         else
321                                 str = _("End of list reached; continue from beginning?");
322
323                         val = alertpanel(_("Search finished"), str,
324                                          _("Yes"), _("No"), NULL);
325                         if (G_ALERTDEFAULT == val) {
326                                 if (backward)
327                                         node = GTK_CTREE_NODE
328                                                 (GTK_CLIST(ctree)->row_list_end);
329                                 else
330                                         node = GTK_CTREE_NODE
331                                                 (GTK_CLIST(ctree)->row_list);
332
333                                 all_searched = TRUE;
334
335                                 manage_window_focus_in(window, NULL, NULL);
336                         } else
337                                 break;
338                 }
339
340                 from_matched = to_matched = subj_matched = body_matched = FALSE;
341                 all_matched = search_and;
342                 
343                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
344
345                 if (*fromwcs && msginfo->from) {
346                         wcs_hs = strdup_mbstowcs(msginfo->from);
347                         if (wcs_hs && WCSFindFunc(wcs_hs, fromwcs) != NULL)
348                                 from_matched = TRUE;
349                         else
350                                 all_matched = FALSE;
351                         g_free(wcs_hs);
352                 }       
353                 if (*towcs && msginfo->to) {
354                         wcs_hs = strdup_mbstowcs(msginfo->to);
355                         if (wcs_hs && WCSFindFunc(wcs_hs, towcs) != NULL)
356                                 to_matched = TRUE;
357                         else
358                                 all_matched = FALSE;
359                         g_free(wcs_hs);
360                 }
361                 if (*subjwcs && msginfo->subject) {
362                         wcs_hs = strdup_mbstowcs(msginfo->subject);
363                         if (wcs_hs && WCSFindFunc(wcs_hs, subjwcs) != NULL)
364                                 subj_matched = TRUE;
365                         else
366                                 all_matched = FALSE;
367                         g_free(wcs_hs);
368                 }
369                 if (*body_str) {
370                         if (procmime_find_string(msginfo, body_str, case_sens))
371                                 body_matched = TRUE;
372                         else
373                                 all_matched = FALSE;
374                 }
375
376                 if ((from_matched || to_matched || subj_matched || body_matched)
377                     && (!search_and || all_matched)) {
378                         if (search_all)
379                                 gtk_ctree_select(ctree, node);
380                         else {
381                                 if (messageview_is_visible
382                                         (summaryview->messageview)) {
383                                         summary_unlock(summaryview);
384                                         summary_select_node
385                                                 (summaryview, node, TRUE, TRUE);
386                                         summary_lock(summaryview);
387                                         if (body_matched) {
388                                                 messageview_search_string
389                                                         (summaryview->messageview,
390                                                          body_str, case_sens);
391                                         }
392                                 } else {
393                                         summary_select_node
394                                                 (summaryview, node, FALSE, TRUE);
395                                 }
396                                 break;
397                         }
398                 }
399
400                 node = backward ? gtkut_ctree_node_prev(ctree, node)
401                                 : gtkut_ctree_node_next(ctree, node);
402         }
403
404         if (*body_str)
405                 main_window_cursor_normal(summaryview->mainwin);
406
407         summary_unlock(summaryview);
408 }
409
410 static void summary_search_clear(GtkButton *button, gpointer data)
411 {
412         gtk_editable_delete_text(GTK_EDITABLE(from_entry),    0, -1);
413         gtk_editable_delete_text(GTK_EDITABLE(to_entry),      0, -1);
414         gtk_editable_delete_text(GTK_EDITABLE(subject_entry), 0, -1);
415         gtk_editable_delete_text(GTK_EDITABLE(body_entry),    0, -1);
416 }
417
418 static void from_activated(void)
419 {
420         gtk_widget_grab_focus(to_entry);
421 }
422
423 static void to_activated(void)
424 {
425         gtk_widget_grab_focus(subject_entry);
426 }
427
428 static void subject_activated(void)
429 {
430         gtk_button_clicked(GTK_BUTTON(search_btn));
431 }
432
433 static void body_activated(void)
434 {
435         gtk_button_clicked(GTK_BUTTON(search_btn));
436 }
437
438 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
439 {
440         if (event && event->keyval == GDK_Escape)
441                 gtk_widget_hide(window);
442 }