2005-02-27 [paul] 1.0.1cvs20
[claws.git] / src / summary_search.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 <gdk/gdkkeysyms.h>
28 #include <gtk/gtkwidget.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtkvbox.h>
31 #include <gtk/gtktable.h>
32 #include <gtk/gtkoptionmenu.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 "intl.h"
45 #include "main.h"
46 #include "summary_search.h"
47 #include "summaryview.h"
48 #include "messageview.h"
49 #include "mainwindow.h"
50 #include "menu.h"
51 #include "utils.h"
52 #include "gtkutils.h"
53 #include "manage_window.h"
54 #include "alertpanel.h"
55
56 static GtkWidget *window;
57 static GtkWidget *bool_optmenu;
58 static GtkWidget *from_entry;
59 static GtkWidget *to_entry;
60 static GtkWidget *subject_entry;
61 static GtkWidget *body_entry;
62 static GtkWidget *case_checkbtn;
63 static GtkWidget *backward_checkbtn;
64 static GtkWidget *all_checkbtn;
65 static GtkWidget *and_checkbtn;
66 static GtkWidget *search_btn;
67 static GtkWidget *clear_btn;
68 static GtkWidget *close_btn;
69
70 static void summary_search_create(SummaryView *summaryview);
71 static void summary_search_execute(GtkButton *button, gpointer data);
72 static void summary_search_clear(GtkButton *button, gpointer data);
73 static void from_activated(void);
74 static void to_activated(void);
75 static void subject_activated(void);
76 static void body_activated(void);
77 static void all_clicked(GtkButton *button);
78 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
79
80 void summary_search(SummaryView *summaryview)
81 {
82         if (!window)
83                 summary_search_create(summaryview);
84         else
85                 gtk_widget_hide(window);
86
87         gtk_widget_grab_focus(search_btn);
88         gtk_widget_grab_focus(subject_entry);
89         gtk_widget_show(window);
90 }
91
92 static void summary_search_create(SummaryView *summaryview)
93 {
94         GtkWidget *vbox1;
95         GtkWidget *bool_hbox;
96         GtkWidget *bool_menu;
97         GtkWidget *menuitem;
98         GtkWidget *table1;
99         GtkWidget *from_label;
100         GtkWidget *to_label;
101         GtkWidget *subject_label;
102         GtkWidget *body_label;
103         GtkWidget *checkbtn_hbox;
104         GtkWidget *confirm_area;
105
106         window = gtk_window_new (GTK_WINDOW_DIALOG);
107         gtk_window_set_title (GTK_WINDOW (window), _("Search messages"));
108         gtk_widget_set_usize (window, 450, -1);
109         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
110         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
111         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
112                            GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete), NULL);
113         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
114                            GTK_SIGNAL_FUNC(key_pressed), NULL);
115         MANAGE_WINDOW_SIGNALS_CONNECT(window);
116
117         vbox1 = gtk_vbox_new (FALSE, 0);
118         gtk_widget_show (vbox1);
119         gtk_container_add (GTK_CONTAINER (window), vbox1);
120
121         bool_hbox = gtk_hbox_new(FALSE, 4);
122         gtk_widget_show(bool_hbox);
123         gtk_box_pack_start(GTK_BOX(vbox1), bool_hbox, FALSE, FALSE, 0);
124
125         bool_optmenu = gtk_option_menu_new();
126         gtk_widget_show(bool_optmenu);
127         gtk_box_pack_start(GTK_BOX(bool_hbox), bool_optmenu, FALSE, FALSE, 0);
128
129         bool_menu = gtk_menu_new();
130         MENUITEM_ADD(bool_menu, menuitem, _("Match any of the following"), 0);
131         MENUITEM_ADD(bool_menu, menuitem, _("Match all of the following"), 1);
132         gtk_option_menu_set_menu(GTK_OPTION_MENU(bool_optmenu), bool_menu);
133
134         table1 = gtk_table_new (4, 3, FALSE);
135         gtk_widget_show (table1);
136         gtk_box_pack_start (GTK_BOX (vbox1), table1, TRUE, TRUE, 0);
137         gtk_container_set_border_width (GTK_CONTAINER (table1), 4);
138         gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
139         gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
140
141         from_entry = gtk_entry_new ();
142         gtk_widget_show (from_entry);
143         gtk_table_attach (GTK_TABLE (table1), from_entry, 1, 3, 0, 1,
144                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
145         gtk_signal_connect(GTK_OBJECT(from_entry), "activate",
146                            GTK_SIGNAL_FUNC(from_activated), summaryview);
147
148         to_entry = gtk_entry_new ();
149         gtk_widget_show (to_entry);
150         gtk_table_attach (GTK_TABLE (table1), to_entry, 1, 3, 1, 2,
151                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
152         gtk_signal_connect(GTK_OBJECT(to_entry), "activate",
153                            GTK_SIGNAL_FUNC(to_activated), summaryview);
154
155         subject_entry = gtk_entry_new ();
156         gtk_widget_show (subject_entry);
157         gtk_table_attach (GTK_TABLE (table1), subject_entry, 1, 3, 2, 3,
158                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
159         gtk_signal_connect(GTK_OBJECT(subject_entry), "activate",
160                            GTK_SIGNAL_FUNC(subject_activated), summaryview);
161
162         body_entry = gtk_entry_new ();
163         gtk_widget_show (body_entry);
164         gtk_table_attach (GTK_TABLE (table1), body_entry, 1, 3, 3, 4,
165                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
166         gtk_signal_connect(GTK_OBJECT(body_entry), "activate",
167                            GTK_SIGNAL_FUNC(body_activated), summaryview);
168
169         from_label = gtk_label_new (_("From:"));
170         gtk_widget_show (from_label);
171         gtk_table_attach (GTK_TABLE (table1), from_label, 0, 1, 0, 1,
172                           GTK_FILL, 0, 0, 0);
173         gtk_label_set_justify (GTK_LABEL (from_label), GTK_JUSTIFY_RIGHT);
174         gtk_misc_set_alignment (GTK_MISC (from_label), 1, 0.5);
175
176         to_label = gtk_label_new (_("To:"));
177         gtk_widget_show (to_label);
178         gtk_table_attach (GTK_TABLE (table1), to_label, 0, 1, 1, 2,
179                           GTK_FILL, 0, 0, 0);
180         gtk_label_set_justify (GTK_LABEL (to_label), GTK_JUSTIFY_RIGHT);
181         gtk_misc_set_alignment (GTK_MISC (to_label), 1, 0.5);
182
183         subject_label = gtk_label_new (_("Subject:"));
184         gtk_widget_show (subject_label);
185         gtk_table_attach (GTK_TABLE (table1), subject_label, 0, 1, 2, 3,
186                           GTK_FILL, 0, 0, 0);
187         gtk_label_set_justify (GTK_LABEL (subject_label), GTK_JUSTIFY_RIGHT);
188         gtk_misc_set_alignment (GTK_MISC (subject_label), 1, 0.5);
189
190         body_label = gtk_label_new (_("Body:"));
191         gtk_widget_show (body_label);
192         gtk_table_attach (GTK_TABLE (table1), body_label, 0, 1, 3, 4,
193                           GTK_FILL, 0, 0, 0);
194         gtk_label_set_justify (GTK_LABEL (body_label), GTK_JUSTIFY_RIGHT);
195         gtk_misc_set_alignment (GTK_MISC (body_label), 1, 0.5);
196
197         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
198         gtk_widget_show (checkbtn_hbox);
199         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
200         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
201
202         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
203         gtk_widget_show (case_checkbtn);
204         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
205                             FALSE, FALSE, 0);
206
207         backward_checkbtn =
208                 gtk_check_button_new_with_label (_("Backward search"));
209         gtk_widget_show (backward_checkbtn);
210         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), backward_checkbtn,
211                             FALSE, FALSE, 0);
212
213         all_checkbtn =
214                 gtk_check_button_new_with_label (_("Select all matched"));
215         gtk_widget_show (all_checkbtn);
216         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), all_checkbtn,
217                             FALSE, FALSE, 0);
218         gtk_signal_connect(GTK_OBJECT(all_checkbtn), "clicked",
219                            GTK_SIGNAL_FUNC(all_clicked), summaryview);
220
221         gtkut_button_set_create(&confirm_area,
222                                 &search_btn, _("Search"),
223                                 &clear_btn,  _("Clear"),
224                                 &close_btn,  _("Close"));
225         gtk_widget_show (confirm_area);
226         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
227         gtk_widget_grab_default(search_btn);
228
229         gtk_signal_connect(GTK_OBJECT(search_btn), "clicked",
230                            GTK_SIGNAL_FUNC(summary_search_execute),
231                            summaryview);
232         gtk_signal_connect(GTK_OBJECT(clear_btn), "clicked",
233                            GTK_SIGNAL_FUNC(summary_search_clear),
234                            summaryview);
235         gtk_signal_connect_object(GTK_OBJECT(close_btn), "clicked",
236                                   GTK_SIGNAL_FUNC(gtk_widget_hide),
237                                   GTK_OBJECT(window));
238 }
239
240 static void summary_search_execute(GtkButton *button, gpointer data)
241 {
242         SummaryView *summaryview = data;
243         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
244         GtkCTreeNode *node;
245         MsgInfo *msginfo;
246         gboolean bool_and;
247         gboolean case_sens;
248         gboolean backward;
249         gboolean search_all;
250         gboolean all_searched = FALSE;
251         gboolean matched;
252         gboolean body_matched;
253         gchar *from_str, *to_str, *subject_str, *body_str;
254         StrFindFunc str_find_func;
255
256         if (summary_is_locked(summaryview)) return;
257         summary_lock(summaryview);
258
259         bool_and = GPOINTER_TO_INT
260                 (menu_get_option_menu_active_user_data
261                         (GTK_OPTION_MENU(bool_optmenu)));
262         case_sens = gtk_toggle_button_get_active
263                 (GTK_TOGGLE_BUTTON(case_checkbtn));
264         backward = gtk_toggle_button_get_active
265                 (GTK_TOGGLE_BUTTON(backward_checkbtn));
266         search_all = gtk_toggle_button_get_active
267                 (GTK_TOGGLE_BUTTON(all_checkbtn));
268
269         if (case_sens)
270                 str_find_func = str_find;
271         else
272                 str_find_func = str_case_find;
273
274         from_str    = gtk_entry_get_text(GTK_ENTRY(from_entry));
275         to_str      = gtk_entry_get_text(GTK_ENTRY(to_entry));
276         subject_str = gtk_entry_get_text(GTK_ENTRY(subject_entry));
277         body_str    = gtk_entry_get_text(GTK_ENTRY(body_entry));
278
279         if (search_all) {
280                 gtk_clist_freeze(GTK_CLIST(ctree));
281                 gtk_clist_unselect_all(GTK_CLIST(ctree));
282                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
283                 backward = FALSE;
284         } else if (!summaryview->selected) {
285                 if (backward)
286                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
287                 else
288                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
289
290                 if (!node) {
291                         summary_unlock(summaryview);
292                         return;
293                 }
294         } else {
295                 if (backward)
296                         node = gtkut_ctree_node_prev
297                                 (ctree, summaryview->selected);
298                 else
299                         node = gtkut_ctree_node_next
300                                 (ctree, summaryview->selected);
301         }
302
303         if (*body_str)
304                 main_window_cursor_wait(summaryview->mainwin);
305
306         for (;;) {
307                 if (!node) {
308                         gchar *str;
309                         AlertValue val;
310
311                         if (search_all) {
312                                 gtk_clist_thaw(GTK_CLIST(ctree));
313                                 break;
314                         }
315
316                         if (all_searched) {
317                                 alertpanel_notice
318                                         (_("Search string not found."));
319                                 break;
320                         }
321
322                         if (backward)
323                                 str = _("Beginning of list reached; continue from end?");
324                         else
325                                 str = _("End of list reached; continue from beginning?");
326
327                         val = alertpanel(_("Search finished"), str,
328                                          _("Yes"), _("No"), NULL);
329                         if (G_ALERTDEFAULT == val) {
330                                 if (backward)
331                                         node = GTK_CTREE_NODE
332                                                 (GTK_CLIST(ctree)->row_list_end);
333                                 else
334                                         node = GTK_CTREE_NODE
335                                                 (GTK_CLIST(ctree)->row_list);
336
337                                 all_searched = TRUE;
338
339                                 manage_window_focus_in(window, NULL, NULL);
340                         } else
341                                 break;
342                 }
343
344
345                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
346                 body_matched = FALSE;
347
348                 if (bool_and) {
349                         matched = TRUE;
350                         if (*from_str) {
351                                 if (!msginfo->from ||
352                                     !str_find_func(msginfo->from, from_str))
353                                         matched = FALSE;
354                         }
355                         if (matched && *to_str) {
356                                 if (!msginfo->to ||
357                                     !str_find_func(msginfo->to, to_str))
358                                         matched = FALSE;
359                         }
360                         if (matched && *subject_str) {
361                                 if (!msginfo->subject ||
362                                     !str_find_func(msginfo->subject, subject_str))
363                                         matched = FALSE;
364                         }
365                         if (matched && *body_str) {
366                                 if (procmime_find_string(msginfo, body_str,
367                                                          str_find_func))
368                                         body_matched = TRUE;
369                                 else
370                                         matched = FALSE;
371                         }
372                         if (matched && !*from_str && !*to_str &&
373                             !*subject_str && !*body_str)
374                                 matched = FALSE;
375                 } else {
376                         matched = FALSE;
377                         if (*from_str && msginfo->from) {
378                                 if (str_find_func(msginfo->from, from_str))
379                                         matched = TRUE;
380                         }
381                         if (!matched && *to_str && msginfo->to) {
382                                 if (str_find_func(msginfo->to, to_str))
383                                         matched = TRUE;
384                         }
385                         if (!matched && *subject_str && msginfo->subject) {
386                                 if (str_find_func(msginfo->subject, subject_str))
387                                         matched = TRUE;
388                         }
389                         if (!matched && *body_str) {
390                                 if (procmime_find_string(msginfo, body_str,
391                                                          str_find_func)) {
392                                         matched = TRUE;
393                                         body_matched = TRUE;
394                                 }
395                         }
396                 }
397
398                 if (matched) {
399                         if (search_all)
400                                 gtk_ctree_select(ctree, node);
401                         else {
402                                 if (messageview_is_visible
403                                         (summaryview->messageview)) {
404                                         summary_unlock(summaryview);
405                                         summary_select_node
406                                                 (summaryview, node, TRUE, TRUE);
407                                         summary_lock(summaryview);
408                                         if (body_matched) {
409                                                 messageview_search_string
410                                                         (summaryview->messageview,
411                                                          body_str, case_sens);
412                                         }
413                                 } else {
414                                         summary_select_node
415                                                 (summaryview, node, FALSE, TRUE);
416                                 }
417                                 break;
418                         }
419                 }
420
421                 node = backward ? gtkut_ctree_node_prev(ctree, node)
422                                 : gtkut_ctree_node_next(ctree, node);
423         }
424
425         if (*body_str)
426                 main_window_cursor_normal(summaryview->mainwin);
427
428         summary_unlock(summaryview);
429 }
430
431 static void summary_search_clear(GtkButton *button, gpointer data)
432 {
433         gtk_editable_delete_text(GTK_EDITABLE(from_entry),    0, -1);
434         gtk_editable_delete_text(GTK_EDITABLE(to_entry),      0, -1);
435         gtk_editable_delete_text(GTK_EDITABLE(subject_entry), 0, -1);
436         gtk_editable_delete_text(GTK_EDITABLE(body_entry),    0, -1);
437 }
438
439 static void from_activated(void)
440 {
441         gtk_widget_grab_focus(to_entry);
442 }
443
444 static void to_activated(void)
445 {
446         gtk_widget_grab_focus(subject_entry);
447 }
448
449 static void subject_activated(void)
450 {
451         gtk_button_clicked(GTK_BUTTON(search_btn));
452 }
453
454 static void body_activated(void)
455 {
456         gtk_button_clicked(GTK_BUTTON(search_btn));
457 }
458
459 static void all_clicked(GtkButton *button)
460 {
461         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
462                 gtk_widget_set_sensitive(backward_checkbtn, FALSE);
463         else
464                 gtk_widget_set_sensitive(backward_checkbtn, TRUE);
465 }
466
467 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
468 {
469         if (event && event->keyval == GDK_Escape)
470                 gtk_widget_hide(window);
471 }