abbc1f017693229bdd6fd4169af3f56920c35237
[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 gboolean all_clicked(GtkButton *button);
75 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
76
77 void summary_search(SummaryView *summaryview)
78 {
79         if (!window)
80                 summary_search_create(summaryview);
81         else
82                 gtk_widget_hide(window);
83
84         gtk_widget_grab_focus(search_btn);
85         gtk_widget_grab_focus(subject_entry);
86         gtk_widget_show(window);
87 }
88
89 static void summary_search_create(SummaryView *summaryview)
90 {
91         GtkWidget *vbox1;
92         GtkWidget *table1;
93         GtkWidget *from_label;
94         GtkWidget *to_label;
95         GtkWidget *subject_label;
96         GtkWidget *body_label;
97         GtkWidget *checkbtn_hbox;
98         GtkWidget *confirm_area;
99
100         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
101         gtk_window_set_title (GTK_WINDOW (window), _("Search messages"));
102         gtk_widget_set_size_request (window, 450, -1);
103         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
104         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
105         g_signal_connect(G_OBJECT(window), "delete_event",
106                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
107         g_signal_connect(G_OBJECT(window), "key_press_event",
108                          G_CALLBACK(key_pressed), NULL);
109         MANAGE_WINDOW_SIGNALS_CONNECT(window);
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         g_signal_connect(G_OBJECT(from_entry), "activate",
127                          G_CALLBACK(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         g_signal_connect(G_OBJECT(to_entry), "activate",
134                          G_CALLBACK(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         g_signal_connect(G_OBJECT(subject_entry), "activate",
141                          G_CALLBACK(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         g_signal_connect(G_OBJECT(body_entry), "activate",
148                          G_CALLBACK(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         g_signal_connect(G_OBJECT(all_checkbtn), "clicked",
200                          G_CALLBACK(all_clicked), summaryview);
201
202         and_checkbtn =
203                 gtk_check_button_new_with_label (_("AND search"));
204         gtk_widget_show (and_checkbtn);
205         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), and_checkbtn,
206                             FALSE, FALSE, 0);
207
208         gtkut_button_set_create(&confirm_area,
209                                 &search_btn, _("Search"),
210                                 &clear_btn,  _("Clear"),
211                                 &close_btn,  _("Close"));
212         gtk_widget_show (confirm_area);
213         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
214         gtk_widget_grab_default(search_btn);
215
216         g_signal_connect(G_OBJECT(search_btn), "clicked",
217                          G_CALLBACK(summary_search_execute),
218                          summaryview);
219         g_signal_connect(G_OBJECT(clear_btn), "clicked",
220                          G_CALLBACK(summary_search_clear),
221                          summaryview);
222         g_signal_connect_closure
223                 (G_OBJECT(close_btn), "clicked",
224                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
225                                      window, NULL),
226                  FALSE);
227 }
228
229 #define GET_ENTRY(entry) gtk_entry_get_text(GTK_ENTRY(entry))
230
231 #define SHOW_ERROR_MESSAGE(error) \
232 { \
233    if (error != NULL) { \
234       g_warning("%s\n", error->message); \
235       g_error_free(error); \
236    } \
237 }
238
239 #warning FIXME_GTK2
240 static void summary_search_execute(GtkButton *button, gpointer data)
241 {
242         SummaryView *summaryview = data;
243         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
244         GtkCTreeNode *node;
245         MsgInfo *msginfo;
246         gboolean case_sens;
247         gboolean backward;
248         gboolean search_all;
249         gboolean search_and;
250         gboolean all_searched = FALSE;
251         gboolean all_matched = FALSE;
252         gboolean from_matched;
253         gboolean   to_matched;
254         gboolean subj_matched;
255         gboolean body_matched;
256         const gchar *body_str;
257         wchar_t *wcs_hs, *fromwcs, *towcs, *subjwcs;
258         wchar_t *(* WCSFindFunc) (const wchar_t *haystack,
259                                   const wchar_t *needle);
260         glong items_read, items_written;
261         GError *error = NULL;
262
263         if (summary_is_locked(summaryview)) return;
264         summary_lock(summaryview);
265
266         case_sens = gtk_toggle_button_get_active
267                 (GTK_TOGGLE_BUTTON(case_checkbtn));
268         backward = gtk_toggle_button_get_active
269                 (GTK_TOGGLE_BUTTON(backward_checkbtn));
270         search_all = gtk_toggle_button_get_active
271                 (GTK_TOGGLE_BUTTON(all_checkbtn));
272         search_and = gtk_toggle_button_get_active
273                 (GTK_TOGGLE_BUTTON(and_checkbtn));
274         
275         if (case_sens)
276 #if HAVE_WCSSTR
277                 WCSFindFunc = wcsstr;
278 #else
279 #if HAVE_WCSWCS
280                 WCSFindFunc = wcswcs;
281 #else
282                 WCSFindFunc = wcscasestr;
283 #endif
284 #endif /* HAVE_WCSSTR */
285         else
286                 WCSFindFunc = wcscasestr;
287
288         fromwcs = (wchar_t *)g_utf8_to_ucs4(GET_ENTRY(from_entry),
289                                             -1, &items_read, &items_written,
290                                             &error);
291         SHOW_ERROR_MESSAGE(error);
292         towcs   = (wchar_t *)g_utf8_to_ucs4(GET_ENTRY(to_entry),
293                                             -1, &items_read, &items_written,
294                                             &error);
295         SHOW_ERROR_MESSAGE(error);
296         subjwcs = (wchar_t *)g_utf8_to_ucs4(GET_ENTRY(subject_entry),
297                                             -1, &items_read, &items_written,
298                                             &error);
299         SHOW_ERROR_MESSAGE(error);
300         body_str = GET_ENTRY(body_entry);
301
302         if (search_all) {
303                 gtk_clist_freeze(GTK_CLIST(ctree));
304                 gtk_clist_unselect_all(GTK_CLIST(ctree));
305                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
306                 backward = FALSE;
307         } else if (!summaryview->selected) {
308                 if (backward)
309                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
310                 else
311                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
312
313                 if (!node) {
314                         summary_unlock(summaryview);
315                         return;
316                 }
317         } else {
318                 if (backward)
319                         node = gtkut_ctree_node_prev
320                                 (ctree, summaryview->selected);
321                 else
322                         node = gtkut_ctree_node_next
323                                 (ctree, summaryview->selected);
324         }
325
326         if (*body_str)
327                 main_window_cursor_wait(summaryview->mainwin);
328
329         for (;;) {
330                 if (!node) {
331                         gchar *str;
332                         AlertValue val;
333
334                         if (search_all) {
335                                 gtk_clist_thaw(GTK_CLIST(ctree));
336                                 break;
337                         }
338
339                         if (all_searched) {
340                                 alertpanel_message
341                                         (_("Search failed"),
342                                          _("Search string not found."));
343                                 break;
344                         }
345
346                         if (backward)
347                                 str = _("Beginning of list reached; continue from end?");
348                         else
349                                 str = _("End of list reached; continue from beginning?");
350
351                         val = alertpanel(_("Search finished"), str,
352                                          _("Yes"), _("No"), NULL);
353                         if (G_ALERTDEFAULT == val) {
354                                 if (backward)
355                                         node = GTK_CTREE_NODE
356                                                 (GTK_CLIST(ctree)->row_list_end);
357                                 else
358                                         node = GTK_CTREE_NODE
359                                                 (GTK_CLIST(ctree)->row_list);
360
361                                 all_searched = TRUE;
362
363                                 manage_window_focus_in(window, NULL, NULL);
364                         } else
365                                 break;
366                 }
367
368                 from_matched = to_matched = subj_matched = body_matched = FALSE;
369                 all_matched = search_and;
370                 
371                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
372
373                 if (fromwcs && *fromwcs && msginfo->from) {
374                         wcs_hs = (wchar_t *)g_utf8_to_ucs4(msginfo->from,
375                                                             -1, &items_read, &items_written,
376                                                             &error);
377                         SHOW_ERROR_MESSAGE(error);
378                         if (wcs_hs && WCSFindFunc(wcs_hs, fromwcs) != NULL)
379                                 from_matched = TRUE;
380                         else
381                                 all_matched = FALSE;
382                         g_free(wcs_hs);
383                 }       
384                 if (*towcs && msginfo->to) {
385                         wcs_hs = (wchar_t *)g_utf8_to_ucs4(msginfo->to,
386                                                             -1, &items_read, &items_written,
387                                                             &error);
388                         SHOW_ERROR_MESSAGE(error);
389                         if (wcs_hs && WCSFindFunc(wcs_hs, towcs) != NULL)
390                                 to_matched = TRUE;
391                         else
392                                 all_matched = FALSE;
393                         g_free(wcs_hs);
394                 }
395                 if (subjwcs && *subjwcs && msginfo->subject) {
396                         wcs_hs = (wchar_t *)g_utf8_to_ucs4(msginfo->subject,
397                                                             -1, &items_read, &items_written,
398                                                             &error);
399                         SHOW_ERROR_MESSAGE(error);
400                         if (wcs_hs && WCSFindFunc(wcs_hs, subjwcs) != NULL)
401                                 subj_matched = TRUE;
402                         else
403                                 all_matched = FALSE;
404                         g_free(wcs_hs);
405                 }
406                 if (body_str && *body_str) {
407                         if (procmime_find_string(msginfo, body_str, case_sens))
408                                 body_matched = TRUE;
409                         else
410                                 all_matched = FALSE;
411                 }
412
413                 if ((from_matched || to_matched || subj_matched || body_matched)
414                     && (!search_and || all_matched)) {
415                         if (search_all)
416                                 gtk_ctree_select(ctree, node);
417                         else {
418                                 if (messageview_is_visible
419                                         (summaryview->messageview)) {
420                                         summary_unlock(summaryview);
421                                         summary_select_node
422                                                 (summaryview, node, TRUE, TRUE);
423                                         summary_lock(summaryview);
424                                         if (body_matched) {
425                                                 messageview_search_string
426                                                         (summaryview->messageview,
427                                                          body_str, case_sens);
428                                         }
429                                 } else {
430                                         summary_select_node
431                                                 (summaryview, node, FALSE, TRUE);
432                                 }
433                                 break;
434                         }
435                 }
436
437                 node = backward ? gtkut_ctree_node_prev(ctree, node)
438                                 : gtkut_ctree_node_next(ctree, node);
439         }
440
441         if (*body_str)
442                 main_window_cursor_normal(summaryview->mainwin);
443
444         g_free(fromwcs);
445         g_free(towcs);
446         g_free(subjwcs);
447
448         summary_unlock(summaryview);
449 }
450
451 #undef GET_ENTRY
452
453 static void summary_search_clear(GtkButton *button, gpointer data)
454 {
455         gtk_editable_delete_text(GTK_EDITABLE(from_entry),    0, -1);
456         gtk_editable_delete_text(GTK_EDITABLE(to_entry),      0, -1);
457         gtk_editable_delete_text(GTK_EDITABLE(subject_entry), 0, -1);
458         gtk_editable_delete_text(GTK_EDITABLE(body_entry),    0, -1);
459 }
460
461 static void from_activated(void)
462 {
463         gtk_widget_grab_focus(to_entry);
464 }
465
466 static void to_activated(void)
467 {
468         gtk_widget_grab_focus(subject_entry);
469 }
470
471 static void subject_activated(void)
472 {
473         gtk_button_clicked(GTK_BUTTON(search_btn));
474 }
475
476 static void body_activated(void)
477 {
478         gtk_button_clicked(GTK_BUTTON(search_btn));
479 }
480
481 static gboolean all_clicked(GtkButton *button)
482 {
483         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
484                 gtk_widget_set_sensitive(backward_checkbtn, FALSE);
485         else
486                 gtk_widget_set_sensitive(backward_checkbtn, TRUE);
487         return FALSE;
488 }
489
490 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
491 {
492         if (event && event->keyval == GDK_Escape)
493                 gtk_widget_hide(window);
494         return FALSE;
495 }