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