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