e1289944246b8b23c5271efd73e1f5b70654a8f1
[claws.git] / src / message_search.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtkwidget.h>
30 #include <gtk/gtkwindow.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtktable.h>
33 #include <gtk/gtklabel.h>
34 #include <gtk/gtkentry.h>
35 #include <gtk/gtkhbox.h>
36 #include <gtk/gtkcheckbutton.h>
37 #include <gtk/gtkhbbox.h>
38 #include <gtk/gtkbutton.h>
39 #include <gtk/gtkctree.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include "main.h"
45 #include "message_search.h"
46 #include "messageview.h"
47 #include "compose.h"
48 #include "utils.h"
49 #include "gtkutils.h"
50 #include "manage_window.h"
51 #include "alertpanel.h"
52 #include "manual.h"
53
54 static struct MessageSearchWindow {
55         GtkWidget *window;
56         GtkWidget *body_entry;
57         GtkWidget *case_checkbtn;
58         GtkWidget *help_btn;
59         GtkWidget *prev_btn;
60         GtkWidget *next_btn;
61         GtkWidget *close_btn;
62
63         MessageView *messageview;
64
65         Compose *compose;
66         gboolean search_compose;
67 } search_window;
68
69 static void message_search_create       (void);
70 static void message_search_execute      (gboolean        backward);
71
72 static void message_search_prev_clicked (GtkButton      *button,
73                                          gpointer        data);
74 static void message_search_next_clicked (GtkButton      *button,
75                                          gpointer        data);
76 static void body_activated              (void);
77 static gboolean key_pressed             (GtkWidget      *widget,
78                                          GdkEventKey    *event,
79                                          gpointer        data);
80
81 void message_search(MessageView *messageview)
82 {
83         if (!search_window.window)
84                 message_search_create();
85         else
86                 gtk_widget_hide(search_window.window);
87
88         search_window.messageview = messageview;
89         search_window.search_compose = FALSE;
90
91         gtk_widget_grab_focus(search_window.next_btn);
92         gtk_widget_grab_focus(search_window.body_entry);
93         gtk_widget_show(search_window.window);
94 }
95
96 void message_search_compose(Compose *compose)
97 {
98         if (!search_window.window)
99                 message_search_create();
100         else
101                 gtk_widget_hide(search_window.window);
102
103         search_window.compose = compose;
104         search_window.search_compose = TRUE;
105
106         gtk_widget_grab_focus(search_window.next_btn);
107         gtk_widget_grab_focus(search_window.body_entry);
108         gtk_widget_show(search_window.window);
109 }
110
111 static void message_search_create(void)
112 {
113         GtkWidget *window;
114
115         GtkWidget *vbox1;
116         GtkWidget *hbox1;
117         GtkWidget *body_label;
118         GtkWidget *body_entry;
119
120         GtkWidget *checkbtn_hbox;
121         GtkWidget *case_checkbtn;
122
123         GtkWidget *confirm_area;
124         GtkWidget *help_btn;
125         GtkWidget *prev_btn;
126         GtkWidget *next_btn;
127         GtkWidget *close_btn;
128
129         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
130         gtk_window_set_title (GTK_WINDOW (window),
131                               _("Find in current message"));
132         gtk_widget_set_size_request (window, 450, -1);
133         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
134         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
135         g_signal_connect(G_OBJECT(window), "delete_event",
136                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
137         g_signal_connect(G_OBJECT(window), "key_press_event",
138                          G_CALLBACK(key_pressed), NULL);
139         MANAGE_WINDOW_SIGNALS_CONNECT(window);
140
141         vbox1 = gtk_vbox_new (FALSE, 0);
142         gtk_widget_show (vbox1);
143         gtk_container_add (GTK_CONTAINER (window), vbox1);
144
145         hbox1 = gtk_hbox_new (FALSE, 8);
146         gtk_widget_show (hbox1);
147         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
148
149         body_label = gtk_label_new (_("Find text:"));
150         gtk_widget_show (body_label);
151         gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
152
153         body_entry = gtk_entry_new ();
154         gtk_widget_show (body_entry);
155         gtk_box_pack_start (GTK_BOX (hbox1), body_entry, TRUE, TRUE, 0);
156         g_signal_connect(G_OBJECT(body_entry), "activate",
157                          G_CALLBACK(body_activated), NULL);
158
159         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
160         gtk_widget_show (checkbtn_hbox);
161         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
162         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
163
164         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
165         gtk_widget_show (case_checkbtn);
166         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
167                             FALSE, FALSE, 0);
168
169         gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
170                         &prev_btn, GTK_STOCK_GO_BACK,
171                         &next_btn, GTK_STOCK_GO_FORWARD,
172                         &close_btn, GTK_STOCK_CLOSE);
173         gtk_widget_show (confirm_area);
174         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
175         gtk_widget_grab_default(next_btn);
176
177         g_signal_connect(G_OBJECT(help_btn), "clicked",
178                          G_CALLBACK(manual_open_with_anchor_cb),
179                          MANUAL_ANCHOR_SEARCHING);
180         g_signal_connect(G_OBJECT(prev_btn), "clicked",
181                          G_CALLBACK(message_search_prev_clicked), NULL);
182         g_signal_connect(G_OBJECT(next_btn), "clicked",
183                          G_CALLBACK(message_search_next_clicked), NULL);
184         g_signal_connect_closure
185                 (G_OBJECT(close_btn), "clicked",
186                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
187                                      window, NULL),
188                  FALSE);
189
190         search_window.window = window;
191         search_window.body_entry = body_entry;
192         search_window.case_checkbtn = case_checkbtn;
193         search_window.help_btn = help_btn;
194         search_window.prev_btn = prev_btn;
195         search_window.next_btn = next_btn;
196         search_window.close_btn = close_btn;
197 }
198
199 static void message_search_execute(gboolean backward)
200 {
201         MessageView *messageview = search_window.messageview;
202         Compose *compose = search_window.compose;
203         gboolean case_sens;
204         gboolean all_searched = FALSE;
205         const gchar *body_str;
206
207         body_str = gtk_entry_get_text(GTK_ENTRY(search_window.body_entry));
208         if (*body_str == '\0') return;
209
210         case_sens = gtk_toggle_button_get_active
211                 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
212
213         for (;;) {
214                 gchar *str;
215                 AlertValue val;
216
217                 if (backward) {
218                         if (search_window.search_compose) {
219                                 if (compose_search_string_backward
220                                         (compose, body_str, case_sens) == TRUE)
221                                         break;
222                         } else {
223                                 if (messageview_search_string_backward
224                                         (messageview, body_str, case_sens) == TRUE)
225                                         break;
226                         }
227                 } else {
228                         if (search_window.search_compose) {
229                                 if (compose_search_string
230                                         (compose, body_str, case_sens) == TRUE)
231                                         break;
232                         } else {
233                                 if (messageview_search_string
234                                         (messageview, body_str, case_sens) == TRUE)
235                                         break;
236                         }
237                 }
238
239                 if (all_searched) {
240                         alertpanel_full(_("Search failed"),
241                                         _("Search string not found."),
242                                          GTK_STOCK_CLOSE, NULL, NULL, FALSE,
243                                          NULL, ALERT_WARNING, G_ALERTDEFAULT);
244                         break;
245                 }
246
247                 all_searched = TRUE;
248
249                 if (backward)
250                         str = _("Beginning of message reached; "
251                                 "continue from end?");
252                 else
253                         str = _("End of message reached; "
254                                 "continue from beginning?");
255
256                 val = alertpanel(_("Search finished"), str,
257                                  GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
258                 if (G_ALERTALTERNATE == val) {
259                         manage_window_focus_in(search_window.window,
260                                                NULL, NULL);
261                         if (search_window.search_compose) {
262                                 compose_set_position(compose,
263                                                          backward ? -1 : 0);
264                         } else {
265                                 messageview_set_position(messageview,
266                                                          backward ? -1 : 0);
267                         }
268                 } else
269                         break;
270         }
271 }
272
273 static void message_search_prev_clicked(GtkButton *button, gpointer data)
274 {
275         message_search_execute(TRUE);
276 }
277
278 static void message_search_next_clicked(GtkButton *button, gpointer data)
279 {
280         message_search_execute(FALSE);
281 }
282
283 static void body_activated(void)
284 {
285         gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
286 }
287
288 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
289                             gpointer data)
290 {
291         if (event && event->keyval == GDK_Escape)
292                 gtk_widget_hide(search_window.window);
293         return FALSE;
294 }