2011-10-25 [colin] 3.7.10cvs52
[claws.git] / src / message_search.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
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/gtk.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "main.h"
35 #include "message_search.h"
36 #include "messageview.h"
37 #include "compose.h"
38 #include "utils.h"
39 #include "gtkutils.h"
40 #include "combobox.h"
41 #include "manage_window.h"
42 #include "alertpanel.h"
43 #include "manual.h"
44 #include "prefs_common.h"
45
46 static struct MessageSearchWindow {
47         GtkWidget *window;
48         GtkWidget *body_entry;
49         GtkWidget *case_checkbtn;
50         GtkWidget *help_btn;
51         GtkWidget *prev_btn;
52         GtkWidget *next_btn;
53         GtkWidget *close_btn;
54         GtkWidget *stop_btn;
55
56         MessageView *messageview;
57
58         Compose *compose;
59         gboolean search_compose;
60         gboolean is_searching;
61         gboolean body_entry_has_focus;
62 } search_window;
63
64 static void message_search_create       (void);
65 static void message_search_execute      (gboolean        backward);
66
67 static void message_search_prev_clicked (GtkButton      *button,
68                                          gpointer        data);
69 static void message_search_next_clicked (GtkButton      *button,
70                                          gpointer        data);
71 static void message_search_stop_clicked (GtkButton      *button,
72                                          gpointer        data);
73 static void body_changed                (void);
74 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
75                                   gpointer data);
76 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
77                                   gpointer data);
78 static gboolean key_pressed             (GtkWidget      *widget,
79                                          GdkEventKey    *event,
80                                          gpointer        data);
81
82
83 #if !GTK_CHECK_VERSION(2,14,0)
84 /* Work around http://bugzilla.gnome.org/show_bug.cgi?id=56070 */
85 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) {                                    \
86         gboolean in_btn = FALSE;                                                        \
87         if (GTK_IS_BUTTON(widget))                                                      \
88                 in_btn = GTK_BUTTON(widget)->in_button;                                 \
89         gtk_widget_set_sensitive(widget, sensitive);                                    \
90         if (GTK_IS_BUTTON(widget))                                                      \
91                 GTK_BUTTON(widget)->in_button = in_btn;                                 \
92 }
93 #else
94 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) {                                    \
95         gtk_widget_set_sensitive(widget, sensitive);                                    \
96 }
97 #endif
98
99 static void message_show_stop_button(void)
100 {
101         gtk_widget_hide(search_window.close_btn);
102         gtk_widget_show(search_window.stop_btn);
103         GTK_BUTTON_SET_SENSITIVE(search_window.prev_btn, FALSE)
104         GTK_BUTTON_SET_SENSITIVE(search_window.next_btn, FALSE)
105 }
106
107 static void message_hide_stop_button(void)
108 {
109         gtk_widget_hide(search_window.stop_btn);
110         gtk_widget_show(search_window.close_btn);
111         gtk_widget_set_sensitive(search_window.prev_btn, TRUE);
112         gtk_widget_set_sensitive(search_window.next_btn, TRUE);
113 }
114
115 void message_search(MessageView *messageview)
116 {
117         if (!search_window.window)
118                 message_search_create();
119         else
120                 gtk_widget_hide(search_window.window);
121
122         search_window.messageview = messageview;
123         search_window.search_compose = FALSE;
124
125         gtk_widget_grab_focus(search_window.next_btn);
126         gtk_widget_grab_focus(search_window.body_entry);
127         gtk_widget_show(search_window.window);
128 }
129
130 void message_search_compose(Compose *compose)
131 {
132         if (!search_window.window)
133                 message_search_create();
134         else
135                 gtk_widget_hide(search_window.window);
136
137         search_window.compose = compose;
138         search_window.search_compose = TRUE;
139
140         gtk_widget_grab_focus(search_window.next_btn);
141         gtk_widget_grab_focus(search_window.body_entry);
142         gtk_widget_show(search_window.window);
143 }
144
145 static void message_search_create(void)
146 {
147         GtkWidget *window;
148
149         GtkWidget *vbox1;
150         GtkWidget *hbox1;
151         GtkWidget *body_label;
152         GtkWidget *body_entry;
153
154         GtkWidget *checkbtn_hbox;
155         GtkWidget *case_checkbtn;
156
157         GtkWidget *confirm_area;
158         GtkWidget *help_btn;
159         GtkWidget *prev_btn;
160         GtkWidget *next_btn;
161         GtkWidget *close_btn;
162         GtkWidget *stop_btn;
163
164         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "message_search");
165         gtk_window_set_title (GTK_WINDOW (window),
166                               _("Find in current message"));
167         gtk_widget_set_size_request (window, 450, -1);
168         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
169         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
170         g_signal_connect(G_OBJECT(window), "delete_event",
171                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
172         g_signal_connect(G_OBJECT(window), "key_press_event",
173                          G_CALLBACK(key_pressed), NULL);
174         MANAGE_WINDOW_SIGNALS_CONNECT(window);
175
176         vbox1 = gtk_vbox_new (FALSE, 0);
177         gtk_widget_show (vbox1);
178         gtk_container_add (GTK_CONTAINER (window), vbox1);
179
180         hbox1 = gtk_hbox_new (FALSE, 8);
181         gtk_widget_show (hbox1);
182         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
183
184         body_label = gtk_label_new (_("Find text:"));
185         gtk_widget_show (body_label);
186         gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
187
188         body_entry = gtk_combo_box_entry_new_text ();
189         gtk_combo_box_set_active(GTK_COMBO_BOX(body_entry), -1);
190         if (prefs_common.message_search_history)
191                 combobox_set_popdown_strings(GTK_COMBO_BOX(body_entry),
192                                 prefs_common.message_search_history);
193         gtk_widget_show (body_entry);
194         gtk_box_pack_start (GTK_BOX (hbox1), body_entry, TRUE, TRUE, 0);
195         g_signal_connect(G_OBJECT(body_entry), "changed",
196                          G_CALLBACK(body_changed), NULL);
197         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
198                          "focus_in_event", G_CALLBACK(body_entry_focus_evt_in), NULL);
199         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
200                          "focus_out_event", G_CALLBACK(body_entry_focus_evt_out), NULL);
201
202         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
203         gtk_widget_show (checkbtn_hbox);
204         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
205         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
206
207         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
208         gtk_widget_show (case_checkbtn);
209         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
210                             FALSE, FALSE, 0);
211
212         confirm_area = gtk_hbutton_box_new();
213         gtk_widget_show (confirm_area);
214         gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
215                                   GTK_BUTTONBOX_END);
216         gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
217
218         gtkut_stock_button_add_help(confirm_area, &help_btn);
219
220         prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
221         gtkut_widget_set_can_default(prev_btn, TRUE);
222         gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
223         gtk_widget_show(prev_btn);
224
225         next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
226         gtkut_widget_set_can_default(next_btn, TRUE);
227         gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
228         gtk_widget_show(next_btn);
229
230         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
231         gtkut_widget_set_can_default(close_btn, TRUE);
232         gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
233         gtk_widget_show(close_btn);
234
235         /* stop button hidden */
236         stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
237         gtkut_widget_set_can_default(stop_btn, TRUE);
238         gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
239
240         gtk_widget_show (confirm_area);
241         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
242         gtk_widget_grab_default(next_btn);
243
244         g_signal_connect(G_OBJECT(help_btn), "clicked",
245                          G_CALLBACK(manual_open_with_anchor_cb),
246                          MANUAL_ANCHOR_SEARCHING);
247         g_signal_connect(G_OBJECT(prev_btn), "clicked",
248                          G_CALLBACK(message_search_prev_clicked), NULL);
249         g_signal_connect(G_OBJECT(next_btn), "clicked",
250                          G_CALLBACK(message_search_next_clicked), NULL);
251         g_signal_connect_closure
252                 (G_OBJECT(close_btn), "clicked",
253                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
254                                      window, NULL),
255                  FALSE);
256         g_signal_connect(G_OBJECT(stop_btn), "clicked",
257                          G_CALLBACK(message_search_stop_clicked), NULL);
258
259         search_window.window = window;
260         search_window.body_entry = body_entry;
261         search_window.case_checkbtn = case_checkbtn;
262         search_window.help_btn = help_btn;
263         search_window.prev_btn = prev_btn;
264         search_window.next_btn = next_btn;
265         search_window.close_btn = close_btn;
266         search_window.stop_btn = stop_btn;
267 }
268
269 static void message_search_execute(gboolean backward)
270 {
271         MessageView *messageview = search_window.messageview;
272         Compose *compose = search_window.compose;
273         gboolean case_sens;
274         gboolean all_searched = FALSE;
275         gchar *body_str;
276
277         body_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.body_entry));
278         if (!body_str)
279                 body_str = gtk_editable_get_chars(
280                                 GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(search_window.body_entry))),0,-1);
281         if (!body_str || *body_str == '\0') return;
282
283         /* add to history */
284         combobox_unset_popdown_strings(GTK_COMBO_BOX(search_window.body_entry));
285         prefs_common.message_search_history = add_history(
286                         prefs_common.message_search_history, body_str);
287         combobox_set_popdown_strings(GTK_COMBO_BOX(search_window.body_entry),
288                         prefs_common.message_search_history);
289
290         case_sens = gtk_toggle_button_get_active
291                 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
292
293         search_window.is_searching = TRUE;
294         message_show_stop_button();
295
296         for (; search_window.is_searching;) {
297                 gchar *str;
298                 AlertValue val;
299
300                 if (backward) {
301                         if (search_window.search_compose) {
302                                 if (compose_search_string_backward
303                                         (compose, body_str, case_sens) == TRUE)
304                                         break;
305                         } else {
306                                 if (messageview_search_string_backward
307                                         (messageview, body_str, case_sens) == TRUE)
308                                         break;
309                         }
310                 } else {
311                         if (search_window.search_compose) {
312                                 if (compose_search_string
313                                         (compose, body_str, case_sens) == TRUE)
314                                         break;
315                         } else {
316                                 if (messageview_search_string
317                                         (messageview, body_str, case_sens) == TRUE)
318                                         break;
319                         }
320                 }
321
322                 if (all_searched) {
323                         alertpanel_full(_("Search failed"),
324                                         _("Search string not found."),
325                                          GTK_STOCK_CLOSE, NULL, NULL, FALSE,
326                                          NULL, ALERT_WARNING, G_ALERTDEFAULT);
327                         break;
328                 }
329
330                 all_searched = TRUE;
331
332                 if (backward)
333                         str = _("Beginning of message reached; "
334                                 "continue from end?");
335                 else
336                         str = _("End of message reached; "
337                                 "continue from beginning?");
338
339                 val = alertpanel(_("Search finished"), str,
340                                  GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
341                 if (G_ALERTALTERNATE == val) {
342                         manage_window_focus_in(search_window.window,
343                                                NULL, NULL);
344                         if (search_window.search_compose) {
345                                 compose_set_position(compose,
346                                                          backward ? -1 : 0);
347                         } else {
348                                 messageview_set_position(messageview,
349                                                          backward ? -1 : 0);
350                         }
351                 } else
352                         break;
353         }
354
355         search_window.is_searching = FALSE;
356         message_hide_stop_button();
357         g_free(body_str);
358 }
359
360 static void body_changed(void)
361 {
362         if (!search_window.body_entry_has_focus)
363                 gtk_widget_grab_focus(search_window.body_entry);
364 }
365
366 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
367                                   gpointer data)
368 {
369         search_window.body_entry_has_focus = TRUE;
370         return FALSE;
371 }
372
373 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
374                                   gpointer data)
375 {
376         search_window.body_entry_has_focus = FALSE;
377         return FALSE;
378 }
379
380 static void message_search_prev_clicked(GtkButton *button, gpointer data)
381 {
382         message_search_execute(TRUE);
383 }
384
385 static void message_search_next_clicked(GtkButton *button, gpointer data)
386 {
387         message_search_execute(FALSE);
388 }
389
390 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
391                             gpointer data)
392 {
393         if (event && (event->keyval == GDK_KEY_Escape)) {
394                 gtk_widget_hide(search_window.window);
395         }
396
397         if (event && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
398                 message_search_execute(FALSE);
399         }
400
401         if (event && (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_Up)) {
402                 if (search_window.body_entry_has_focus) {
403                         combobox_set_value_from_arrow_key(
404                                         GTK_COMBO_BOX(search_window.body_entry),
405                                         event->keyval);
406                         return TRUE;
407                 }
408         }
409
410         return FALSE;
411 }
412
413 static void message_search_stop_clicked(GtkButton *button, gpointer data)
414 {
415         search_window.is_searching = FALSE;
416 }