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