2006-09-25 [wwp] 2.5.0cvs1
[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         GtkWidget *stop_btn;
63
64         MessageView *messageview;
65
66         Compose *compose;
67         gboolean search_compose;
68         gboolean is_searching;
69 } search_window;
70
71 static void message_search_create       (void);
72 static void message_search_execute      (gboolean        backward);
73
74 static void message_search_prev_clicked (GtkButton      *button,
75                                          gpointer        data);
76 static void message_search_next_clicked (GtkButton      *button,
77                                          gpointer        data);
78 static void message_search_stop_clicked (GtkButton      *button,
79                                          gpointer        data);
80 static void body_activated              (void);
81 static gboolean key_pressed             (GtkWidget      *widget,
82                                          GdkEventKey    *event,
83                                          gpointer        data);
84
85
86 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) {    \
87         gboolean in_btn = FALSE;                                                        \
88         if (GTK_IS_BUTTON(widget))                                                      \
89                 in_btn = GTK_BUTTON(widget)->in_button;                 \
90         gtk_widget_set_sensitive(widget, sensitive);            \
91         if (GTK_IS_BUTTON(widget))                                                      \
92                 GTK_BUTTON(widget)->in_button = in_btn;                 \
93 }
94
95 static void message_show_stop_button(void)
96 {
97         gtk_widget_hide(search_window.close_btn);
98         gtk_widget_show(search_window.stop_btn);
99         GTK_BUTTON_SET_SENSITIVE(search_window.prev_btn, FALSE)
100         GTK_BUTTON_SET_SENSITIVE(search_window.next_btn, FALSE)
101 }
102
103 static void message_hide_stop_button(void)
104 {
105         gtk_widget_hide(search_window.stop_btn);
106         gtk_widget_show(search_window.close_btn);
107         gtk_widget_set_sensitive(search_window.prev_btn, TRUE);
108         gtk_widget_set_sensitive(search_window.next_btn, TRUE);
109 }
110
111 void message_search(MessageView *messageview)
112 {
113         if (!search_window.window)
114                 message_search_create();
115         else
116                 gtk_widget_hide(search_window.window);
117
118         search_window.messageview = messageview;
119         search_window.search_compose = FALSE;
120
121         gtk_widget_grab_focus(search_window.next_btn);
122         gtk_widget_grab_focus(search_window.body_entry);
123         gtk_widget_show(search_window.window);
124 }
125
126 void message_search_compose(Compose *compose)
127 {
128         if (!search_window.window)
129                 message_search_create();
130         else
131                 gtk_widget_hide(search_window.window);
132
133         search_window.compose = compose;
134         search_window.search_compose = TRUE;
135
136         gtk_widget_grab_focus(search_window.next_btn);
137         gtk_widget_grab_focus(search_window.body_entry);
138         gtk_widget_show(search_window.window);
139 }
140
141 static void message_search_create(void)
142 {
143         GtkWidget *window;
144
145         GtkWidget *vbox1;
146         GtkWidget *hbox1;
147         GtkWidget *body_label;
148         GtkWidget *body_entry;
149
150         GtkWidget *checkbtn_hbox;
151         GtkWidget *case_checkbtn;
152
153         GtkWidget *confirm_area;
154         GtkWidget *help_btn;
155         GtkWidget *prev_btn;
156         GtkWidget *next_btn;
157         GtkWidget *close_btn;
158         GtkWidget *stop_btn;
159
160         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
161         gtk_window_set_title (GTK_WINDOW (window),
162                               _("Find in current message"));
163         gtk_widget_set_size_request (window, 450, -1);
164         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
165         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
166         g_signal_connect(G_OBJECT(window), "delete_event",
167                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
168         g_signal_connect(G_OBJECT(window), "key_press_event",
169                          G_CALLBACK(key_pressed), NULL);
170         MANAGE_WINDOW_SIGNALS_CONNECT(window);
171
172         vbox1 = gtk_vbox_new (FALSE, 0);
173         gtk_widget_show (vbox1);
174         gtk_container_add (GTK_CONTAINER (window), vbox1);
175
176         hbox1 = gtk_hbox_new (FALSE, 8);
177         gtk_widget_show (hbox1);
178         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
179
180         body_label = gtk_label_new (_("Find text:"));
181         gtk_widget_show (body_label);
182         gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
183
184         body_entry = gtk_entry_new ();
185         gtk_widget_show (body_entry);
186         gtk_box_pack_start (GTK_BOX (hbox1), body_entry, TRUE, TRUE, 0);
187         g_signal_connect(G_OBJECT(body_entry), "activate",
188                          G_CALLBACK(body_activated), NULL);
189
190         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
191         gtk_widget_show (checkbtn_hbox);
192         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
193         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
194
195         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
196         gtk_widget_show (case_checkbtn);
197         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
198                             FALSE, FALSE, 0);
199
200         confirm_area = gtk_hbutton_box_new();
201         gtk_widget_show (confirm_area);
202         gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
203                                   GTK_BUTTONBOX_END);
204         gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
205
206         gtkut_stock_button_add_help(confirm_area, &help_btn);
207
208         prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
209         GTK_WIDGET_SET_FLAGS(prev_btn, GTK_CAN_DEFAULT);
210         gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
211         gtk_widget_show(prev_btn);
212
213         next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
214         GTK_WIDGET_SET_FLAGS(next_btn, GTK_CAN_DEFAULT);
215         gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
216         gtk_widget_show(next_btn);
217
218         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
219         GTK_WIDGET_SET_FLAGS(close_btn, GTK_CAN_DEFAULT);
220         gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
221         gtk_widget_show(close_btn);
222
223         /* stop button hidden */
224         stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
225         GTK_WIDGET_SET_FLAGS(stop_btn, GTK_CAN_DEFAULT);
226         gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
227
228         gtk_widget_show (confirm_area);
229         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
230         gtk_widget_grab_default(next_btn);
231
232         g_signal_connect(G_OBJECT(help_btn), "clicked",
233                          G_CALLBACK(manual_open_with_anchor_cb),
234                          MANUAL_ANCHOR_SEARCHING);
235         g_signal_connect(G_OBJECT(prev_btn), "clicked",
236                          G_CALLBACK(message_search_prev_clicked), NULL);
237         g_signal_connect(G_OBJECT(next_btn), "clicked",
238                          G_CALLBACK(message_search_next_clicked), NULL);
239         g_signal_connect_closure
240                 (G_OBJECT(close_btn), "clicked",
241                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
242                                      window, NULL),
243                  FALSE);
244         g_signal_connect(G_OBJECT(stop_btn), "clicked",
245                          G_CALLBACK(message_search_stop_clicked), NULL);
246
247         search_window.window = window;
248         search_window.body_entry = body_entry;
249         search_window.case_checkbtn = case_checkbtn;
250         search_window.help_btn = help_btn;
251         search_window.prev_btn = prev_btn;
252         search_window.next_btn = next_btn;
253         search_window.close_btn = close_btn;
254         search_window.stop_btn = stop_btn;
255 }
256
257 static void message_search_execute(gboolean backward)
258 {
259         MessageView *messageview = search_window.messageview;
260         Compose *compose = search_window.compose;
261         gboolean case_sens;
262         gboolean all_searched = FALSE;
263         const gchar *body_str;
264
265         body_str = gtk_entry_get_text(GTK_ENTRY(search_window.body_entry));
266         if (*body_str == '\0') return;
267
268         case_sens = gtk_toggle_button_get_active
269                 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
270
271         search_window.is_searching = TRUE;
272         message_show_stop_button();
273
274         for (; search_window.is_searching;) {
275                 gchar *str;
276                 AlertValue val;
277
278                 if (backward) {
279                         if (search_window.search_compose) {
280                                 if (compose_search_string_backward
281                                         (compose, body_str, case_sens) == TRUE)
282                                         break;
283                         } else {
284                                 if (messageview_search_string_backward
285                                         (messageview, body_str, case_sens) == TRUE)
286                                         break;
287                         }
288                 } else {
289                         if (search_window.search_compose) {
290                                 if (compose_search_string
291                                         (compose, body_str, case_sens) == TRUE)
292                                         break;
293                         } else {
294                                 if (messageview_search_string
295                                         (messageview, body_str, case_sens) == TRUE)
296                                         break;
297                         }
298                 }
299
300                 if (all_searched) {
301                         alertpanel_full(_("Search failed"),
302                                         _("Search string not found."),
303                                          GTK_STOCK_CLOSE, NULL, NULL, FALSE,
304                                          NULL, ALERT_WARNING, G_ALERTDEFAULT);
305                         break;
306                 }
307
308                 all_searched = TRUE;
309
310                 if (backward)
311                         str = _("Beginning of message reached; "
312                                 "continue from end?");
313                 else
314                         str = _("End of message reached; "
315                                 "continue from beginning?");
316
317                 val = alertpanel(_("Search finished"), str,
318                                  GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
319                 if (G_ALERTALTERNATE == val) {
320                         manage_window_focus_in(search_window.window,
321                                                NULL, NULL);
322                         if (search_window.search_compose) {
323                                 compose_set_position(compose,
324                                                          backward ? -1 : 0);
325                         } else {
326                                 messageview_set_position(messageview,
327                                                          backward ? -1 : 0);
328                         }
329                 } else
330                         break;
331         }
332
333         search_window.is_searching = FALSE;
334         message_hide_stop_button();
335 }
336
337 static void message_search_prev_clicked(GtkButton *button, gpointer data)
338 {
339         message_search_execute(TRUE);
340 }
341
342 static void message_search_next_clicked(GtkButton *button, gpointer data)
343 {
344         message_search_execute(FALSE);
345 }
346
347 static void body_activated(void)
348 {
349         gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
350 }
351
352 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
353                             gpointer data)
354 {
355         if (event && event->keyval == GDK_Escape)
356                 gtk_widget_hide(search_window.window);
357         return FALSE;
358 }
359
360 static void message_search_stop_clicked(GtkButton *button, gpointer data)
361 {
362         search_window.is_searching = FALSE;
363 }