2004-12-30 [colin] 0.9.13cvs22.4
[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 gboolean all_clicked(GtkButton *button);
78 static gboolean 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_TOPLEVEL);
107         gtk_window_set_title (GTK_WINDOW (window), _("Search messages"));
108         gtk_widget_set_size_request (window, 450, -1);
109         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
110         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
111         g_signal_connect(G_OBJECT(window), "delete_event",
112                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
113         g_signal_connect(G_OBJECT(window), "key_press_event",
114                          G_CALLBACK(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         g_signal_connect(G_OBJECT(from_entry), "activate",
146                          G_CALLBACK(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         g_signal_connect(G_OBJECT(to_entry), "activate",
153                          G_CALLBACK(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         g_signal_connect(G_OBJECT(subject_entry), "activate",
160                          G_CALLBACK(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         g_signal_connect(G_OBJECT(body_entry), "activate",
167                          G_CALLBACK(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_toggle_button_set_active(GTK_TOGGLE_BUTTON(case_checkbtn), FALSE);
204         gtk_widget_show (case_checkbtn);
205         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
206                             FALSE, FALSE, 0);
207
208         backward_checkbtn =
209                 gtk_check_button_new_with_label (_("Backward search"));
210         gtk_widget_show (backward_checkbtn);
211         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), backward_checkbtn,
212                             FALSE, FALSE, 0);
213
214         all_checkbtn =
215                 gtk_check_button_new_with_label (_("Select all matched"));
216         gtk_widget_show (all_checkbtn);
217         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), all_checkbtn,
218                             FALSE, FALSE, 0);
219         g_signal_connect(G_OBJECT(all_checkbtn), "clicked",
220                          G_CALLBACK(all_clicked), summaryview);
221
222         gtkut_button_set_create_stock(&confirm_area,
223                                       &search_btn, GTK_STOCK_FIND,
224                                       &clear_btn,  GTK_STOCK_CLEAR,
225                                       &close_btn,  GTK_STOCK_CLOSE);
226         gtk_widget_show (confirm_area);
227         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
228         gtk_widget_grab_default(search_btn);
229
230         g_signal_connect(G_OBJECT(search_btn), "clicked",
231                          G_CALLBACK(summary_search_execute),
232                          summaryview);
233         g_signal_connect(G_OBJECT(clear_btn), "clicked",
234                          G_CALLBACK(summary_search_clear),
235                          summaryview);
236         g_signal_connect_closure
237                 (G_OBJECT(close_btn), "clicked",
238                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
239                                      window, NULL),
240                  FALSE);
241 }
242
243 static void summary_search_execute(GtkButton *button, gpointer data)
244 {
245         SummaryView *summaryview = data;
246         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
247         GtkCTreeNode *node;
248         MsgInfo *msginfo;
249         gboolean bool_and;
250         gboolean case_sens;
251         gboolean backward;
252         gboolean search_all;
253         gboolean all_searched = FALSE;
254         gboolean matched;
255         gboolean body_matched;
256         const gchar *from_str, *to_str, *subject_str, *body_str;
257         StrFindFunc str_find_func;
258
259         if (summary_is_locked(summaryview)) return;
260         summary_lock(summaryview);
261
262         bool_and = GPOINTER_TO_INT
263                 (menu_get_option_menu_active_user_data
264                         (GTK_OPTION_MENU(bool_optmenu)));
265         case_sens = gtk_toggle_button_get_active
266                 (GTK_TOGGLE_BUTTON(case_checkbtn));
267         backward = gtk_toggle_button_get_active
268                 (GTK_TOGGLE_BUTTON(backward_checkbtn));
269         search_all = gtk_toggle_button_get_active
270                 (GTK_TOGGLE_BUTTON(all_checkbtn));
271
272         if (case_sens)
273                 str_find_func = str_find;
274         else
275                 str_find_func = str_case_find;
276
277         from_str    = gtk_entry_get_text(GTK_ENTRY(from_entry));
278         to_str      = gtk_entry_get_text(GTK_ENTRY(to_entry));
279         subject_str = gtk_entry_get_text(GTK_ENTRY(subject_entry));
280         body_str    = gtk_entry_get_text(GTK_ENTRY(body_entry));
281
282         if (search_all) {
283                 gtk_clist_freeze(GTK_CLIST(ctree));
284                 gtk_clist_unselect_all(GTK_CLIST(ctree));
285                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
286                 backward = FALSE;
287         } else if (!summaryview->selected) {
288                 if (backward)
289                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
290                 else
291                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
292
293                 if (!node) {
294                         summary_unlock(summaryview);
295                         return;
296                 }
297         } else {
298                 if (backward)
299                         node = gtkut_ctree_node_prev
300                                 (ctree, summaryview->selected);
301                 else
302                         node = gtkut_ctree_node_next
303                                 (ctree, summaryview->selected);
304         }
305
306         if (*body_str)
307                 main_window_cursor_wait(summaryview->mainwin);
308
309         for (;;) {
310                 if (!node) {
311                         gchar *str;
312                         AlertValue val;
313
314                         if (search_all) {
315                                 gtk_clist_thaw(GTK_CLIST(ctree));
316                                 break;
317                         }
318
319                         if (all_searched) {
320                                 alertpanel_notice
321                                         (_("Search string not found."));
322                                 break;
323                         }
324
325                         if (backward)
326                                 str = _("Beginning of list reached; continue from end?");
327                         else
328                                 str = _("End of list reached; continue from beginning?");
329
330                         val = alertpanel(_("Search finished"), str,
331                                          _("Yes"), _("No"), NULL);
332                         if (G_ALERTDEFAULT == val) {
333                                 if (backward)
334                                         node = GTK_CTREE_NODE
335                                                 (GTK_CLIST(ctree)->row_list_end);
336                                 else
337                                         node = GTK_CTREE_NODE
338                                                 (GTK_CLIST(ctree)->row_list);
339
340                                 all_searched = TRUE;
341
342                                 manage_window_focus_in(window, NULL, NULL);
343                         } else
344                                 break;
345                 }
346
347
348                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
349                 body_matched = FALSE;
350
351                 if (bool_and) {
352                         matched = TRUE;
353                         if (*from_str) {
354                                 if (!msginfo->from ||
355                                     !str_find_func(msginfo->from, from_str))
356                                         matched = FALSE;
357                         }
358                         if (matched && *to_str) {
359                                 if (!msginfo->to ||
360                                     !str_find_func(msginfo->to, to_str))
361                                         matched = FALSE;
362                         }
363                         if (matched && *subject_str) {
364                                 if (!msginfo->subject ||
365                                     !str_find_func(msginfo->subject, subject_str))
366                                         matched = FALSE;
367                         }
368                         if (matched && *body_str) {
369                                 if (procmime_find_string(msginfo, body_str,
370                                                          str_find_func))
371                                         body_matched = TRUE;
372                                 else
373                                         matched = FALSE;
374                         }
375                         if (matched && !*from_str && !*to_str &&
376                             !*subject_str && !*body_str)
377                                 matched = FALSE;
378                 } else {
379                         matched = FALSE;
380                         if (*from_str && msginfo->from) {
381                                 if (str_find_func(msginfo->from, from_str))
382                                         matched = TRUE;
383                         }
384                         if (!matched && *to_str && msginfo->to) {
385                                 if (str_find_func(msginfo->to, to_str))
386                                         matched = TRUE;
387                         }
388                         if (!matched && *subject_str && msginfo->subject) {
389                                 if (str_find_func(msginfo->subject, subject_str))
390                                         matched = TRUE;
391                         }
392                         if (!matched && *body_str) {
393                                 if (procmime_find_string(msginfo, body_str,
394                                                          str_find_func)) {
395                                         matched = TRUE;
396                                         body_matched = TRUE;
397                                 }
398                         }
399                 }
400
401                 if (matched) {
402                         if (search_all)
403                                 gtk_ctree_select(ctree, node);
404                         else {
405                                 if (messageview_is_visible
406                                         (summaryview->messageview)) {
407                                         summary_unlock(summaryview);
408                                         summary_select_node
409                                                 (summaryview, node, TRUE, TRUE);
410                                         summary_lock(summaryview);
411                                         if (body_matched) {
412                                                 messageview_search_string
413                                                         (summaryview->messageview,
414                                                          body_str, case_sens);
415                                         }
416                                 } else {
417                                         summary_select_node
418                                                 (summaryview, node, FALSE, TRUE);
419                                 }
420                                 break;
421                         }
422                 }
423
424                 node = backward ? gtkut_ctree_node_prev(ctree, node)
425                                 : gtkut_ctree_node_next(ctree, node);
426         }
427
428         if (*body_str)
429                 main_window_cursor_normal(summaryview->mainwin);
430
431         summary_unlock(summaryview);
432 }
433
434 static void summary_search_clear(GtkButton *button, gpointer data)
435 {
436         gtk_editable_delete_text(GTK_EDITABLE(from_entry),    0, -1);
437         gtk_editable_delete_text(GTK_EDITABLE(to_entry),      0, -1);
438         gtk_editable_delete_text(GTK_EDITABLE(subject_entry), 0, -1);
439         gtk_editable_delete_text(GTK_EDITABLE(body_entry),    0, -1);
440 }
441
442 static void from_activated(void)
443 {
444         gtk_widget_grab_focus(to_entry);
445 }
446
447 static void to_activated(void)
448 {
449         gtk_widget_grab_focus(subject_entry);
450 }
451
452 static void subject_activated(void)
453 {
454         gtk_button_clicked(GTK_BUTTON(search_btn));
455 }
456
457 static void body_activated(void)
458 {
459         gtk_button_clicked(GTK_BUTTON(search_btn));
460 }
461
462 static gboolean all_clicked(GtkButton *button)
463 {
464         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
465                 gtk_widget_set_sensitive(backward_checkbtn, FALSE);
466         else
467                 gtk_widget_set_sensitive(backward_checkbtn, TRUE);
468         return FALSE;
469 }
470
471 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
472 {
473         if (event && event->keyval == GDK_Escape)
474                 gtk_widget_hide(window);
475         return FALSE;
476 }