sync with sylpheed 0.7.2cvs14
[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         case_sens = gtk_toggle_button_get_active
236                 (GTK_TOGGLE_BUTTON(case_checkbtn));
237         backward = gtk_toggle_button_get_active
238                 (GTK_TOGGLE_BUTTON(backward_checkbtn));
239         search_all = gtk_toggle_button_get_active
240                 (GTK_TOGGLE_BUTTON(all_checkbtn));
241
242         if (case_sens)
243 #if HAVE_WCSSTR
244                 WCSFindFunc = wcsstr;
245 #else
246 #if HAVE_WCSWCS
247                 WCSFindFunc = wcswcs;
248 #else
249                 WCSFindFunc = wcscasestr;
250 #endif
251 #endif /* HAVE_WCSSTR */
252         else
253                 WCSFindFunc = wcscasestr;
254
255         fromwcs = (wchar_t *)GTK_ENTRY(from_entry)->text;
256         towcs   = (wchar_t *)GTK_ENTRY(to_entry)->text;
257         subjwcs = (wchar_t *)GTK_ENTRY(subject_entry)->text;
258         body_str = gtk_entry_get_text(GTK_ENTRY(body_entry));
259
260         if (search_all) {
261                 gtk_clist_freeze(GTK_CLIST(ctree));
262                 gtk_clist_unselect_all(GTK_CLIST(ctree));
263                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
264         } else if (!summaryview->selected) {
265                 if (backward)
266                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
267                 else
268                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
269
270                 if (!node) return;
271         } else {
272                 if (backward)
273                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
274                 else
275                         node = GTK_CTREE_NODE_NEXT(summaryview->selected);
276         }
277
278         if (*body_str)
279                 main_window_cursor_wait(summaryview->mainwin);
280
281         for (;;) {
282                 if (!node) {
283                         gchar *str;
284                         AlertValue val;
285
286                         if (search_all) {
287                                 gtk_clist_thaw(GTK_CLIST(ctree));
288                                 break;
289                         }
290
291                         if (all_searched) {
292                                 alertpanel_message
293                                         (_("Search failed"),
294                                          _("Search string not found."));
295                                 break;
296                         }
297
298                         if (backward)
299                                 str = _("Beginning of list reached; continue from end?");
300                         else
301                                 str = _("End of list reached; continue from beginning?");
302
303                         val = alertpanel(_("Search finished"), str,
304                                          _("Yes"), _("No"), NULL);
305                         if (G_ALERTDEFAULT == val) {
306                                 if (backward)
307                                         node = GTK_CTREE_NODE
308                                                 (GTK_CLIST(ctree)->row_list_end);
309                                 else
310                                         node = GTK_CTREE_NODE
311                                                 (GTK_CLIST(ctree)->row_list);
312
313                                 all_searched = TRUE;
314
315                                 manage_window_focus_in(window, NULL, NULL);
316                         } else
317                                 break;
318                 }
319
320                 from_matched = to_matched = subj_matched = body_matched = FALSE;
321
322                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
323
324                 if (*fromwcs && msginfo->from) {
325                         wcs_hs = strdup_mbstowcs(msginfo->from);
326                         if (wcs_hs && WCSFindFunc(wcs_hs, fromwcs) != NULL)
327                                 from_matched = TRUE;
328                         g_free(wcs_hs);
329                 }
330                 if (*towcs && msginfo->to) {
331                         wcs_hs = strdup_mbstowcs(msginfo->to);
332                         if (wcs_hs && WCSFindFunc(wcs_hs, towcs) != NULL)
333                                 to_matched = TRUE;
334                         g_free(wcs_hs);
335                 }
336                 if (*subjwcs && msginfo->subject) {
337                         wcs_hs = strdup_mbstowcs(msginfo->subject);
338                         if (wcs_hs && WCSFindFunc(wcs_hs, subjwcs) != NULL)
339                                 subj_matched = TRUE;
340                         g_free(wcs_hs);
341                 }
342                 if (*body_str) {
343                         if (procmime_find_string(msginfo, body_str, case_sens))
344                                 body_matched = TRUE;
345                 }
346
347                 if (from_matched || to_matched || subj_matched || body_matched) {
348                         if (search_all)
349                                 gtk_ctree_select(ctree, node);
350                         else {
351                                 if (summaryview->msg_is_toggled_on) {
352                                         summary_select_node
353                                                 (summaryview, node, TRUE, TRUE);
354                                         if (body_matched) {
355                                                 messageview_search_string
356                                                         (summaryview->messageview,
357                                                          body_str, case_sens);
358                                         }
359                                 } else {
360                                         summary_select_node
361                                                 (summaryview, node, FALSE, TRUE);
362                                 }
363                                 break;
364                         }
365                 }
366
367                 node = backward ? GTK_CTREE_NODE_PREV(node)
368                                 : GTK_CTREE_NODE_NEXT(node);
369         }
370
371         if (*body_str)
372                 main_window_cursor_normal(summaryview->mainwin);
373 }
374
375 static void summary_search_clear(GtkButton *button, gpointer data)
376 {
377         gtk_editable_delete_text(GTK_EDITABLE(from_entry),    0, -1);
378         gtk_editable_delete_text(GTK_EDITABLE(to_entry),      0, -1);
379         gtk_editable_delete_text(GTK_EDITABLE(subject_entry), 0, -1);
380         gtk_editable_delete_text(GTK_EDITABLE(body_entry),    0, -1);
381 }
382
383 static void from_activated(void)
384 {
385         gtk_widget_grab_focus(to_entry);
386 }
387
388 static void to_activated(void)
389 {
390         gtk_widget_grab_focus(subject_entry);
391 }
392
393 static void subject_activated(void)
394 {
395         gtk_button_clicked(GTK_BUTTON(search_btn));
396 }
397
398 static void body_activated(void)
399 {
400         gtk_button_clicked(GTK_BUTTON(search_btn));
401 }
402
403 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
404 {
405         if (event && event->keyval == GDK_Escape)
406                 gtk_widget_hide(window);
407 }