remove vanity labelling
[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         SearchInterface *interface;
58         void *interface_obj;
59
60         gboolean is_searching;
61         gboolean body_entry_has_focus;
62 } search_window;
63
64 static SearchInterface compose_interface = {
65         .search_string_backward = (SearchStringFunc) compose_search_string_backward,
66         .set_position = (SetPositionFunc) compose_set_position,
67         .search_string = (SearchStringFunc) compose_search_string,
68 };
69
70 static SearchInterface messageview_interface = {
71         .set_position = (SetPositionFunc) messageview_set_position,
72         .search_string = (SearchStringFunc) messageview_search_string,
73         .search_string_backward = (SearchStringFunc) messageview_search_string_backward,
74 };
75
76 static void message_search_create       (void);
77 static void message_search_execute      (gboolean        backward);
78
79 static void message_search_prev_clicked (GtkButton      *button,
80                                          gpointer        data);
81 static void message_search_next_clicked (GtkButton      *button,
82                                          gpointer        data);
83 static void message_search_stop_clicked (GtkButton      *button,
84                                          gpointer        data);
85 static void body_changed                (void);
86 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
87                                   gpointer data);
88 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
89                                   gpointer data);
90 static gboolean key_pressed             (GtkWidget      *widget,
91                                          GdkEventKey    *event,
92                                          gpointer        data);
93
94
95 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) {                                    \
96         gtk_widget_set_sensitive(widget, sensitive);                                    \
97 }
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         message_search_other(&messageview_interface, (void *)messageview);
118 }
119
120 void message_search_compose(Compose *compose)
121 {
122         message_search_other(&compose_interface, (void *)compose);
123 }
124
125 void message_search_other(SearchInterface *interface, void *obj)
126 {
127         if (!search_window.window)
128                 message_search_create();
129         else
130                 gtk_widget_hide(search_window.window);
131
132         search_window.interface_obj = obj;
133         search_window.interface = interface;
134
135         gtk_widget_grab_focus(search_window.next_btn);
136         gtk_widget_grab_focus(search_window.body_entry);
137         gtk_widget_show(search_window.window);
138 }
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 = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "message_search");
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 #if !GTK_CHECK_VERSION(2, 24, 0)
185         body_entry = gtk_combo_box_entry_new_text ();
186 #else
187         body_entry = gtk_combo_box_text_new_with_entry ();
188 #endif
189         gtk_combo_box_set_active(GTK_COMBO_BOX(body_entry), -1);
190         if (prefs_common.message_search_history)
191 #if !GTK_CHECK_VERSION(2, 24, 0)
192                 combobox_set_popdown_strings(GTK_COMBO_BOX(body_entry),
193                                 prefs_common.message_search_history);
194 #else
195                 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(body_entry),
196                                 prefs_common.message_search_history);
197 #endif
198         gtk_widget_show (body_entry);
199         gtk_box_pack_start (GTK_BOX (hbox1), body_entry, TRUE, TRUE, 0);
200         g_signal_connect(G_OBJECT(body_entry), "changed",
201                          G_CALLBACK(body_changed), NULL);
202         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
203                          "focus_in_event", G_CALLBACK(body_entry_focus_evt_in), NULL);
204         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
205                          "focus_out_event", G_CALLBACK(body_entry_focus_evt_out), NULL);
206
207         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
208         gtk_widget_show (checkbtn_hbox);
209         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
210         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
211
212         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
213         gtk_widget_show (case_checkbtn);
214         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
215                             FALSE, FALSE, 0);
216
217         confirm_area = gtk_hbutton_box_new();
218         gtk_widget_show (confirm_area);
219         gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
220                                   GTK_BUTTONBOX_END);
221         gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
222
223         gtkut_stock_button_add_help(confirm_area, &help_btn);
224
225         prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
226         gtkut_widget_set_can_default(prev_btn, TRUE);
227         gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
228         gtk_widget_show(prev_btn);
229
230         next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
231         gtkut_widget_set_can_default(next_btn, TRUE);
232         gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
233         gtk_widget_show(next_btn);
234
235         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
236         gtkut_widget_set_can_default(close_btn, TRUE);
237         gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
238         gtk_widget_show(close_btn);
239
240         /* stop button hidden */
241         stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
242         gtkut_widget_set_can_default(stop_btn, TRUE);
243         gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
244
245         gtk_widget_show (confirm_area);
246         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
247         gtk_widget_grab_default(next_btn);
248
249         g_signal_connect(G_OBJECT(help_btn), "clicked",
250                          G_CALLBACK(manual_open_with_anchor_cb),
251                          MANUAL_ANCHOR_SEARCHING);
252         g_signal_connect(G_OBJECT(prev_btn), "clicked",
253                          G_CALLBACK(message_search_prev_clicked), NULL);
254         g_signal_connect(G_OBJECT(next_btn), "clicked",
255                          G_CALLBACK(message_search_next_clicked), NULL);
256         g_signal_connect_closure
257                 (G_OBJECT(close_btn), "clicked",
258                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
259                                      window, NULL),
260                  FALSE);
261         g_signal_connect(G_OBJECT(stop_btn), "clicked",
262                          G_CALLBACK(message_search_stop_clicked), NULL);
263
264         search_window.window = window;
265         search_window.body_entry = body_entry;
266         search_window.case_checkbtn = case_checkbtn;
267         search_window.help_btn = help_btn;
268         search_window.prev_btn = prev_btn;
269         search_window.next_btn = next_btn;
270         search_window.close_btn = close_btn;
271         search_window.stop_btn = stop_btn;
272 }
273
274 static void message_search_execute(gboolean backward)
275 {
276         void *interface_obj = search_window.interface_obj;
277         gboolean case_sens;
278         gboolean all_searched = FALSE;
279         gchar *body_str;
280
281 #if !GTK_CHECK_VERSION(2, 24, 0)
282         body_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.body_entry));
283 #else
284         body_str = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(search_window.body_entry));
285 #endif
286         if (!body_str)
287                 body_str = gtk_editable_get_chars(
288                                 GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(search_window.body_entry))),0,-1);
289         if (!body_str || *body_str == '\0') return;
290
291         /* add to history */
292 #if !GTK_CHECK_VERSION(2, 24, 0)
293         combobox_unset_popdown_strings(GTK_COMBO_BOX(search_window.body_entry));
294 #else
295         combobox_unset_popdown_strings(GTK_COMBO_BOX_TEXT(search_window.body_entry));
296 #endif
297         prefs_common.message_search_history = add_history(
298                         prefs_common.message_search_history, body_str);
299 #if !GTK_CHECK_VERSION(2, 24, 0)
300         combobox_set_popdown_strings(GTK_COMBO_BOX(search_window.body_entry),
301                         prefs_common.message_search_history);
302 #else
303         combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(search_window.body_entry),
304                         prefs_common.message_search_history);
305 #endif
306
307         case_sens = gtk_toggle_button_get_active
308                 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
309
310         search_window.is_searching = TRUE;
311         message_show_stop_button();
312
313         for (; search_window.is_searching;) {
314                 gchar *str;
315                 AlertValue val;
316
317                 if (backward) {
318                         if (search_window.interface->search_string_backward
319                                 (interface_obj, body_str, case_sens) == TRUE)
320                                 break;
321                 } else {
322                         if (search_window.interface->search_string
323                                 (interface_obj, body_str, case_sens) == TRUE)
324                                 break;
325                 }
326
327                 if (all_searched) {
328                         alertpanel_full(_("Search failed"),
329                                         _("Search string not found."),
330                                          GTK_STOCK_CLOSE, NULL, NULL, FALSE,
331                                          NULL, ALERT_WARNING, G_ALERTDEFAULT);
332                         break;
333                 }
334
335                 all_searched = TRUE;
336
337                 if (backward)
338                         str = _("Beginning of message reached; "
339                                 "continue from end?");
340                 else
341                         str = _("End of message reached; "
342                                 "continue from beginning?");
343
344                 val = alertpanel(_("Search finished"), str,
345                                  GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
346                 if (G_ALERTALTERNATE == val) {
347                         manage_window_focus_in(search_window.window,
348                                                NULL, NULL);
349                         search_window.interface->set_position(interface_obj,
350                                                         backward ? -1 : 0);
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 }