RSSyl: Fix argument order for strstr() in Atom parser.
[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 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) {                                    \
85         gtk_widget_set_sensitive(widget, sensitive);                                    \
86 }
87
88 static void message_show_stop_button(void)
89 {
90         gtk_widget_hide(search_window.close_btn);
91         gtk_widget_show(search_window.stop_btn);
92         GTK_BUTTON_SET_SENSITIVE(search_window.prev_btn, FALSE)
93         GTK_BUTTON_SET_SENSITIVE(search_window.next_btn, FALSE)
94 }
95
96 static void message_hide_stop_button(void)
97 {
98         gtk_widget_hide(search_window.stop_btn);
99         gtk_widget_show(search_window.close_btn);
100         gtk_widget_set_sensitive(search_window.prev_btn, TRUE);
101         gtk_widget_set_sensitive(search_window.next_btn, TRUE);
102 }
103
104 void message_search(MessageView *messageview)
105 {
106         if (!search_window.window)
107                 message_search_create();
108         else
109                 gtk_widget_hide(search_window.window);
110
111         search_window.messageview = messageview;
112         search_window.search_compose = FALSE;
113
114         gtk_widget_grab_focus(search_window.next_btn);
115         gtk_widget_grab_focus(search_window.body_entry);
116         gtk_widget_show(search_window.window);
117 }
118
119 void message_search_compose(Compose *compose)
120 {
121         if (!search_window.window)
122                 message_search_create();
123         else
124                 gtk_widget_hide(search_window.window);
125
126         search_window.compose = compose;
127         search_window.search_compose = TRUE;
128
129         gtk_widget_grab_focus(search_window.next_btn);
130         gtk_widget_grab_focus(search_window.body_entry);
131         gtk_widget_show(search_window.window);
132 }
133
134 static void message_search_create(void)
135 {
136         GtkWidget *window;
137
138         GtkWidget *vbox1;
139         GtkWidget *hbox1;
140         GtkWidget *body_label;
141         GtkWidget *body_entry;
142
143         GtkWidget *checkbtn_hbox;
144         GtkWidget *case_checkbtn;
145
146         GtkWidget *confirm_area;
147         GtkWidget *help_btn;
148         GtkWidget *prev_btn;
149         GtkWidget *next_btn;
150         GtkWidget *close_btn;
151         GtkWidget *stop_btn;
152
153         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "message_search");
154         gtk_window_set_title (GTK_WINDOW (window),
155                               _("Find in current message"));
156         gtk_widget_set_size_request (window, 450, -1);
157         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
158         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
159         g_signal_connect(G_OBJECT(window), "delete_event",
160                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
161         g_signal_connect(G_OBJECT(window), "key_press_event",
162                          G_CALLBACK(key_pressed), NULL);
163         MANAGE_WINDOW_SIGNALS_CONNECT(window);
164
165         vbox1 = gtk_vbox_new (FALSE, 0);
166         gtk_widget_show (vbox1);
167         gtk_container_add (GTK_CONTAINER (window), vbox1);
168
169         hbox1 = gtk_hbox_new (FALSE, 8);
170         gtk_widget_show (hbox1);
171         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
172
173         body_label = gtk_label_new (_("Find text:"));
174         gtk_widget_show (body_label);
175         gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
176
177 #if !GTK_CHECK_VERSION(2, 24, 0)
178         body_entry = gtk_combo_box_entry_new_text ();
179 #else
180         body_entry = gtk_combo_box_text_new_with_entry ();
181 #endif
182         gtk_combo_box_set_active(GTK_COMBO_BOX(body_entry), -1);
183         if (prefs_common.message_search_history)
184 #if !GTK_CHECK_VERSION(2, 24, 0)
185                 combobox_set_popdown_strings(GTK_COMBO_BOX(body_entry),
186                                 prefs_common.message_search_history);
187 #else
188                 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(body_entry),
189                                 prefs_common.message_search_history);
190 #endif
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         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
196                          "focus_in_event", G_CALLBACK(body_entry_focus_evt_in), NULL);
197         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
198                          "focus_out_event", G_CALLBACK(body_entry_focus_evt_out), NULL);
199
200         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
201         gtk_widget_show (checkbtn_hbox);
202         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
203         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
204
205         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
206         gtk_widget_show (case_checkbtn);
207         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
208                             FALSE, FALSE, 0);
209
210         confirm_area = gtk_hbutton_box_new();
211         gtk_widget_show (confirm_area);
212         gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
213                                   GTK_BUTTONBOX_END);
214         gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
215
216         gtkut_stock_button_add_help(confirm_area, &help_btn);
217
218         prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
219         gtkut_widget_set_can_default(prev_btn, TRUE);
220         gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
221         gtk_widget_show(prev_btn);
222
223         next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
224         gtkut_widget_set_can_default(next_btn, TRUE);
225         gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
226         gtk_widget_show(next_btn);
227
228         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
229         gtkut_widget_set_can_default(close_btn, TRUE);
230         gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
231         gtk_widget_show(close_btn);
232
233         /* stop button hidden */
234         stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
235         gtkut_widget_set_can_default(stop_btn, TRUE);
236         gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
237
238         gtk_widget_show (confirm_area);
239         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
240         gtk_widget_grab_default(next_btn);
241
242         g_signal_connect(G_OBJECT(help_btn), "clicked",
243                          G_CALLBACK(manual_open_with_anchor_cb),
244                          MANUAL_ANCHOR_SEARCHING);
245         g_signal_connect(G_OBJECT(prev_btn), "clicked",
246                          G_CALLBACK(message_search_prev_clicked), NULL);
247         g_signal_connect(G_OBJECT(next_btn), "clicked",
248                          G_CALLBACK(message_search_next_clicked), NULL);
249         g_signal_connect_closure
250                 (G_OBJECT(close_btn), "clicked",
251                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
252                                      window, NULL),
253                  FALSE);
254         g_signal_connect(G_OBJECT(stop_btn), "clicked",
255                          G_CALLBACK(message_search_stop_clicked), NULL);
256
257         search_window.window = window;
258         search_window.body_entry = body_entry;
259         search_window.case_checkbtn = case_checkbtn;
260         search_window.help_btn = help_btn;
261         search_window.prev_btn = prev_btn;
262         search_window.next_btn = next_btn;
263         search_window.close_btn = close_btn;
264         search_window.stop_btn = stop_btn;
265 }
266
267 static void message_search_execute(gboolean backward)
268 {
269         MessageView *messageview = search_window.messageview;
270         Compose *compose = search_window.compose;
271         gboolean case_sens;
272         gboolean all_searched = FALSE;
273         gchar *body_str;
274
275 #if !GTK_CHECK_VERSION(2, 24, 0)
276         body_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.body_entry));
277 #else
278         body_str = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(search_window.body_entry));
279 #endif
280         if (!body_str)
281                 body_str = gtk_editable_get_chars(
282                                 GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(search_window.body_entry))),0,-1);
283         if (!body_str || *body_str == '\0') return;
284
285         /* add to history */
286 #if !GTK_CHECK_VERSION(2, 24, 0)
287         combobox_unset_popdown_strings(GTK_COMBO_BOX(search_window.body_entry));
288 #else
289         combobox_unset_popdown_strings(GTK_COMBO_BOX_TEXT(search_window.body_entry));
290 #endif
291         prefs_common.message_search_history = add_history(
292                         prefs_common.message_search_history, body_str);
293 #if !GTK_CHECK_VERSION(2, 24, 0)
294         combobox_set_popdown_strings(GTK_COMBO_BOX(search_window.body_entry),
295                         prefs_common.message_search_history);
296 #else
297         combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(search_window.body_entry),
298                         prefs_common.message_search_history);
299 #endif
300
301         case_sens = gtk_toggle_button_get_active
302                 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
303
304         search_window.is_searching = TRUE;
305         message_show_stop_button();
306
307         for (; search_window.is_searching;) {
308                 gchar *str;
309                 AlertValue val;
310
311                 if (backward) {
312                         if (search_window.search_compose) {
313                                 if (compose_search_string_backward
314                                         (compose, body_str, case_sens) == TRUE)
315                                         break;
316                         } else {
317                                 if (messageview_search_string_backward
318                                         (messageview, body_str, case_sens) == TRUE)
319                                         break;
320                         }
321                 } else {
322                         if (search_window.search_compose) {
323                                 if (compose_search_string
324                                         (compose, body_str, case_sens) == TRUE)
325                                         break;
326                         } else {
327                                 if (messageview_search_string
328                                         (messageview, body_str, case_sens) == TRUE)
329                                         break;
330                         }
331                 }
332
333                 if (all_searched) {
334                         alertpanel_full(_("Search failed"),
335                                         _("Search string not found."),
336                                          GTK_STOCK_CLOSE, NULL, NULL, FALSE,
337                                          NULL, ALERT_WARNING, G_ALERTDEFAULT);
338                         break;
339                 }
340
341                 all_searched = TRUE;
342
343                 if (backward)
344                         str = _("Beginning of message reached; "
345                                 "continue from end?");
346                 else
347                         str = _("End of message reached; "
348                                 "continue from beginning?");
349
350                 val = alertpanel(_("Search finished"), str,
351                                  GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
352                 if (G_ALERTALTERNATE == val) {
353                         manage_window_focus_in(search_window.window,
354                                                NULL, NULL);
355                         if (search_window.search_compose) {
356                                 compose_set_position(compose,
357                                                          backward ? -1 : 0);
358                         } else {
359                                 messageview_set_position(messageview,
360                                                          backward ? -1 : 0);
361                         }
362                 } else
363                         break;
364         }
365
366         search_window.is_searching = FALSE;
367         message_hide_stop_button();
368         g_free(body_str);
369 }
370
371 static void body_changed(void)
372 {
373         if (!search_window.body_entry_has_focus)
374                 gtk_widget_grab_focus(search_window.body_entry);
375 }
376
377 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
378                                   gpointer data)
379 {
380         search_window.body_entry_has_focus = TRUE;
381         return FALSE;
382 }
383
384 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
385                                   gpointer data)
386 {
387         search_window.body_entry_has_focus = FALSE;
388         return FALSE;
389 }
390
391 static void message_search_prev_clicked(GtkButton *button, gpointer data)
392 {
393         message_search_execute(TRUE);
394 }
395
396 static void message_search_next_clicked(GtkButton *button, gpointer data)
397 {
398         message_search_execute(FALSE);
399 }
400
401 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
402                             gpointer data)
403 {
404         if (event && (event->keyval == GDK_KEY_Escape)) {
405                 gtk_widget_hide(search_window.window);
406         }
407
408         if (event && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
409                 message_search_execute(FALSE);
410         }
411
412         if (event && (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_Up)) {
413                 if (search_window.body_entry_has_focus) {
414                         combobox_set_value_from_arrow_key(
415                                         GTK_COMBO_BOX(search_window.body_entry),
416                                         event->keyval);
417                         return TRUE;
418                 }
419         }
420
421         return FALSE;
422 }
423
424 static void message_search_stop_clicked(GtkButton *button, gpointer data)
425 {
426         search_window.is_searching = FALSE;
427 }