Corrected a bug that prevented sylpheed to open
[claws.git] / src / summary_search.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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         MANAGE_WINDOW_SIGNALS_CONNECT(window);
107
108         vbox1 = gtk_vbox_new (FALSE, 0);
109         gtk_widget_show (vbox1);
110         gtk_container_add (GTK_CONTAINER (window), vbox1);
111
112         table1 = gtk_table_new (4, 3, FALSE);
113         gtk_widget_show (table1);
114         gtk_box_pack_start (GTK_BOX (vbox1), table1, TRUE, TRUE, 0);
115         gtk_container_set_border_width (GTK_CONTAINER (table1), 4);
116         gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
117         gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
118
119         from_entry = gtk_entry_new ();
120         gtk_widget_show (from_entry);
121         gtk_table_attach (GTK_TABLE (table1), from_entry, 1, 3, 0, 1,
122                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
123         gtk_signal_connect(GTK_OBJECT(from_entry), "activate",
124                            GTK_SIGNAL_FUNC(from_activated), summaryview);
125
126         to_entry = gtk_entry_new ();
127         gtk_widget_show (to_entry);
128         gtk_table_attach (GTK_TABLE (table1), to_entry, 1, 3, 1, 2,
129                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
130         gtk_signal_connect(GTK_OBJECT(to_entry), "activate",
131                            GTK_SIGNAL_FUNC(to_activated), summaryview);
132
133         subject_entry = gtk_entry_new ();
134         gtk_widget_show (subject_entry);
135         gtk_table_attach (GTK_TABLE (table1), subject_entry, 1, 3, 2, 3,
136                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
137         gtk_signal_connect(GTK_OBJECT(subject_entry), "activate",
138                            GTK_SIGNAL_FUNC(subject_activated), summaryview);
139
140         body_entry = gtk_entry_new ();
141         gtk_widget_show (body_entry);
142         gtk_table_attach (GTK_TABLE (table1), body_entry, 1, 3, 3, 4,
143                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
144         gtk_signal_connect(GTK_OBJECT(body_entry), "activate",
145                            GTK_SIGNAL_FUNC(body_activated), summaryview);
146
147         from_label = gtk_label_new (_("From:"));
148         gtk_widget_show (from_label);
149         gtk_table_attach (GTK_TABLE (table1), from_label, 0, 1, 0, 1,
150                           GTK_FILL, 0, 0, 0);
151         gtk_label_set_justify (GTK_LABEL (from_label), GTK_JUSTIFY_RIGHT);
152         gtk_misc_set_alignment (GTK_MISC (from_label), 1, 0.5);
153
154         to_label = gtk_label_new (_("To:"));
155         gtk_widget_show (to_label);
156         gtk_table_attach (GTK_TABLE (table1), to_label, 0, 1, 1, 2,
157                           GTK_FILL, 0, 0, 0);
158         gtk_label_set_justify (GTK_LABEL (to_label), GTK_JUSTIFY_RIGHT);
159         gtk_misc_set_alignment (GTK_MISC (to_label), 1, 0.5);
160
161         subject_label = gtk_label_new (_("Subject:"));
162         gtk_widget_show (subject_label);
163         gtk_table_attach (GTK_TABLE (table1), subject_label, 0, 1, 2, 3,
164                           GTK_FILL, 0, 0, 0);
165         gtk_label_set_justify (GTK_LABEL (subject_label), GTK_JUSTIFY_RIGHT);
166         gtk_misc_set_alignment (GTK_MISC (subject_label), 1, 0.5);
167
168         body_label = gtk_label_new (_("Body:"));
169         gtk_widget_show (body_label);
170         gtk_table_attach (GTK_TABLE (table1), body_label, 0, 1, 3, 4,
171                           GTK_FILL, 0, 0, 0);
172         gtk_label_set_justify (GTK_LABEL (body_label), GTK_JUSTIFY_RIGHT);
173         gtk_misc_set_alignment (GTK_MISC (body_label), 1, 0.5);
174
175         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
176         gtk_widget_show (checkbtn_hbox);
177         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
178         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
179
180         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
181         gtk_widget_show (case_checkbtn);
182         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
183                             FALSE, FALSE, 0);
184
185         backward_checkbtn =
186                 gtk_check_button_new_with_label (_("Backward search"));
187         gtk_widget_show (backward_checkbtn);
188         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), backward_checkbtn,
189                             FALSE, FALSE, 0);
190
191         all_checkbtn =
192                 gtk_check_button_new_with_label (_("Select all matched"));
193         gtk_widget_show (all_checkbtn);
194         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), all_checkbtn,
195                             FALSE, FALSE, 0);
196
197         gtkut_button_set_create(&confirm_area,
198                                 &search_btn, _("Search"),
199                                 &clear_btn,  _("Clear"),
200                                 &close_btn,  _("Close"));
201         gtk_widget_show (confirm_area);
202         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
203         gtk_widget_grab_default(search_btn);
204
205         gtk_signal_connect(GTK_OBJECT(search_btn), "clicked",
206                            GTK_SIGNAL_FUNC(summary_search_execute),
207                            summaryview);
208         gtk_signal_connect(GTK_OBJECT(clear_btn), "clicked",
209                            GTK_SIGNAL_FUNC(summary_search_clear),
210                            summaryview);
211         gtk_signal_connect_object(GTK_OBJECT(close_btn), "clicked",
212                                   GTK_SIGNAL_FUNC(gtk_widget_hide),
213                                   GTK_OBJECT(window));
214 }
215
216 static void summary_search_execute(GtkButton *button, gpointer data)
217 {
218         SummaryView *summaryview = data;
219         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
220         GtkCTreeNode *node;
221         MsgInfo *msginfo;
222         gboolean case_sens;
223         gboolean backward;
224         gboolean search_all;
225         gboolean all_searched = FALSE;
226         gboolean from_matched;
227         gboolean   to_matched;
228         gboolean subj_matched;
229         gboolean body_matched;
230         gchar *body_str;
231         wchar_t *wcs_hs, *fromwcs, *towcs, *subjwcs;
232         wchar_t *(* WCSFindFunc) (const wchar_t *haystack,
233                                   const wchar_t *needle);
234
235         if (summary_is_locked(summaryview)) return;
236         summary_lock(summaryview);
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) {
274                         summary_unlock(summaryview);
275                         return;
276                 }
277         } else {
278                 if (backward)
279                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
280                 else
281                         node = GTK_CTREE_NODE_NEXT(summaryview->selected);
282         }
283
284         if (*body_str)
285                 main_window_cursor_wait(summaryview->mainwin);
286
287         for (;;) {
288                 if (!node) {
289                         gchar *str;
290                         AlertValue val;
291
292                         if (search_all) {
293                                 gtk_clist_thaw(GTK_CLIST(ctree));
294                                 break;
295                         }
296
297                         if (all_searched) {
298                                 alertpanel_message
299                                         (_("Search failed"),
300                                          _("Search string not found."));
301                                 break;
302                         }
303
304                         if (backward)
305                                 str = _("Beginning of list reached; continue from end?");
306                         else
307                                 str = _("End of list reached; continue from beginning?");
308
309                         val = alertpanel(_("Search finished"), str,
310                                          _("Yes"), _("No"), NULL);
311                         if (G_ALERTDEFAULT == val) {
312                                 if (backward)
313                                         node = GTK_CTREE_NODE
314                                                 (GTK_CLIST(ctree)->row_list_end);
315                                 else
316                                         node = GTK_CTREE_NODE
317                                                 (GTK_CLIST(ctree)->row_list);
318
319                                 all_searched = TRUE;
320
321                                 manage_window_focus_in(window, NULL, NULL);
322                         } else
323                                 break;
324                 }
325
326                 from_matched = to_matched = subj_matched = body_matched = FALSE;
327
328                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
329
330                 if (*fromwcs && msginfo->from) {
331                         wcs_hs = strdup_mbstowcs(msginfo->from);
332                         if (wcs_hs && WCSFindFunc(wcs_hs, fromwcs) != NULL)
333                                 from_matched = TRUE;
334                         g_free(wcs_hs);
335                 }
336                 if (*towcs && msginfo->to) {
337                         wcs_hs = strdup_mbstowcs(msginfo->to);
338                         if (wcs_hs && WCSFindFunc(wcs_hs, towcs) != NULL)
339                                 to_matched = TRUE;
340                         g_free(wcs_hs);
341                 }
342                 if (*subjwcs && msginfo->subject) {
343                         wcs_hs = strdup_mbstowcs(msginfo->subject);
344                         if (wcs_hs && WCSFindFunc(wcs_hs, subjwcs) != NULL)
345                                 subj_matched = TRUE;
346                         g_free(wcs_hs);
347                 }
348                 if (*body_str) {
349                         if (procmime_find_string(msginfo, body_str, case_sens))
350                                 body_matched = TRUE;
351                 }
352
353                 if (from_matched || to_matched || subj_matched || body_matched) {
354                         if (search_all)
355                                 gtk_ctree_select(ctree, node);
356                         else {
357                                 if (summaryview->msg_is_toggled_on) {
358                                         summary_unlock(summaryview);
359                                         summary_select_node
360                                                 (summaryview, node, TRUE, TRUE);
361                                         summary_lock(summaryview);
362                                         if (body_matched) {
363                                                 messageview_search_string
364                                                         (summaryview->messageview,
365                                                          body_str, case_sens);
366                                         }
367                                 } else {
368                                         summary_select_node
369                                                 (summaryview, node, FALSE, TRUE);
370                                 }
371                                 break;
372                         }
373                 }
374
375                 node = backward ? GTK_CTREE_NODE_PREV(node)
376                                 : GTK_CTREE_NODE_NEXT(node);
377         }
378
379         if (*body_str)
380                 main_window_cursor_normal(summaryview->mainwin);
381
382         summary_unlock(summaryview);
383 }
384
385 static void summary_search_clear(GtkButton *button, gpointer data)
386 {
387         gtk_editable_delete_text(GTK_EDITABLE(from_entry),    0, -1);
388         gtk_editable_delete_text(GTK_EDITABLE(to_entry),      0, -1);
389         gtk_editable_delete_text(GTK_EDITABLE(subject_entry), 0, -1);
390         gtk_editable_delete_text(GTK_EDITABLE(body_entry),    0, -1);
391 }
392
393 static void from_activated(void)
394 {
395         gtk_widget_grab_focus(to_entry);
396 }
397
398 static void to_activated(void)
399 {
400         gtk_widget_grab_focus(subject_entry);
401 }
402
403 static void subject_activated(void)
404 {
405         gtk_button_clicked(GTK_BUTTON(search_btn));
406 }
407
408 static void body_activated(void)
409 {
410         gtk_button_clicked(GTK_BUTTON(search_btn));
411 }
412
413 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
414 {
415         if (event && event->keyval == GDK_Escape)
416                 gtk_widget_hide(window);
417 }