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