2006-01-22 [paul] 1.9.100cvs179
[claws.git] / src / summary_search.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtkwidget.h>
30 #include <gtk/gtkwindow.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtktable.h>
33 #include <gtk/gtkoptionmenu.h>
34 #include <gtk/gtklabel.h>
35 #include <gtk/gtkentry.h>
36 #include <gtk/gtkhbox.h>
37 #include <gtk/gtkcheckbutton.h>
38 #include <gtk/gtkhbbox.h>
39 #include <gtk/gtkbutton.h>
40 #include <gtk/gtkctree.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
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 struct SummarySearchWindow {
57         GtkWidget *window;
58
59         GtkWidget *bool_optmenu;
60
61         GtkWidget *from_entry;
62         GtkWidget *to_entry;
63         GtkWidget *subject_entry;
64         GtkWidget *body_entry;
65
66         GtkWidget *case_checkbtn;
67
68         GtkWidget *clear_btn;
69         GtkWidget *all_btn;
70         GtkWidget *prev_btn;
71         GtkWidget *next_btn;
72         GtkWidget *close_btn;
73
74         SummaryView *summaryview;
75 } search_window;
76
77 static void summary_search_create       (void);
78
79 static void summary_search_execute      (gboolean        backward,
80                                          gboolean        search_all);
81
82 static void summary_search_clear        (GtkButton      *button,
83                                          gpointer        data);
84 static void summary_search_prev_clicked (GtkButton      *button,
85                                          gpointer        data);
86 static void summary_search_next_clicked (GtkButton      *button,
87                                          gpointer        data);
88 static void summary_search_all_clicked  (GtkButton      *button,
89                                          gpointer        data);
90
91 static void from_activated              (void);
92 static void to_activated                (void);
93 static void subject_activated           (void);
94 static void body_activated              (void);
95
96 static gboolean key_pressed             (GtkWidget      *widget,
97                                          GdkEventKey    *event,
98                                          gpointer        data);
99
100
101 void summary_search(SummaryView *summaryview)
102 {
103         if (!search_window.window)
104                 summary_search_create();
105         else
106                 gtk_widget_hide(search_window.window);
107
108         search_window.summaryview = summaryview;
109
110         gtk_widget_grab_focus(search_window.next_btn);
111         gtk_widget_grab_focus(search_window.subject_entry);
112         gtk_widget_show(search_window.window);
113 }
114
115 static void summary_search_create(void)
116 {
117         GtkWidget *window;
118         GtkWidget *vbox1;
119         GtkWidget *bool_hbox;
120         GtkWidget *bool_optmenu;
121         GtkWidget *bool_menu;
122         GtkWidget *menuitem;
123         GtkWidget *clear_btn;
124
125         GtkWidget *table1;
126         GtkWidget *from_label;
127         GtkWidget *from_entry;
128         GtkWidget *to_label;
129         GtkWidget *to_entry;
130         GtkWidget *subject_label;
131         GtkWidget *subject_entry;
132         GtkWidget *body_label;
133         GtkWidget *body_entry;
134
135         GtkWidget *checkbtn_hbox;
136         GtkWidget *case_checkbtn;
137
138         GtkWidget *confirm_area;
139         GtkWidget *all_btn;
140         GtkWidget *prev_btn;
141         GtkWidget *next_btn;
142         GtkWidget *close_btn;
143
144         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
145         gtk_window_set_title(GTK_WINDOW (window), _("Search messages"));
146         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
147         gtk_container_set_border_width(GTK_CONTAINER (window), 8);
148         g_signal_connect(G_OBJECT(window), "delete_event",
149                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
150         g_signal_connect(G_OBJECT(window), "key_press_event",
151                          G_CALLBACK(key_pressed), NULL);
152         MANAGE_WINDOW_SIGNALS_CONNECT(window);
153
154         vbox1 = gtk_vbox_new (FALSE, 0);
155         gtk_widget_show (vbox1);
156         gtk_container_add (GTK_CONTAINER (window), vbox1);
157
158         bool_hbox = gtk_hbox_new(FALSE, 4);
159         gtk_widget_show(bool_hbox);
160         gtk_box_pack_start(GTK_BOX(vbox1), bool_hbox, FALSE, FALSE, 0);
161
162         bool_optmenu = gtk_option_menu_new();
163         gtk_widget_show(bool_optmenu);
164         gtk_box_pack_start(GTK_BOX(bool_hbox), bool_optmenu, FALSE, FALSE, 0);
165
166         bool_menu = gtk_menu_new();
167         MENUITEM_ADD(bool_menu, menuitem, _("Match any of the following"), 0);
168         MENUITEM_ADD(bool_menu, menuitem, _("Match all of the following"), 1);
169         gtk_option_menu_set_menu(GTK_OPTION_MENU(bool_optmenu), bool_menu);
170
171         clear_btn = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
172         gtk_widget_show(clear_btn);
173         gtk_box_pack_end(GTK_BOX(bool_hbox), clear_btn, FALSE, FALSE, 0);
174
175         table1 = gtk_table_new (4, 3, FALSE);
176         gtk_widget_show (table1);
177         gtk_box_pack_start (GTK_BOX (vbox1), table1, TRUE, TRUE, 0);
178         gtk_container_set_border_width (GTK_CONTAINER (table1), 4);
179         gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
180         gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
181
182         from_entry = gtk_entry_new ();
183         gtk_widget_show (from_entry);
184         gtk_table_attach (GTK_TABLE (table1), from_entry, 1, 3, 0, 1,
185                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
186         g_signal_connect(G_OBJECT(from_entry), "activate",
187                          G_CALLBACK(from_activated), NULL);
188
189         to_entry = gtk_entry_new ();
190         gtk_widget_show (to_entry);
191         gtk_table_attach (GTK_TABLE (table1), to_entry, 1, 3, 1, 2,
192                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
193         g_signal_connect(G_OBJECT(to_entry), "activate",
194                          G_CALLBACK(to_activated), NULL);
195
196         subject_entry = gtk_entry_new ();
197         gtk_widget_show (subject_entry);
198         gtk_table_attach (GTK_TABLE (table1), subject_entry, 1, 3, 2, 3,
199                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
200         g_signal_connect(G_OBJECT(subject_entry), "activate",
201                          G_CALLBACK(subject_activated), NULL);
202
203         body_entry = gtk_entry_new ();
204         gtk_widget_show (body_entry);
205         gtk_table_attach (GTK_TABLE (table1), body_entry, 1, 3, 3, 4,
206                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
207         g_signal_connect(G_OBJECT(body_entry), "activate",
208                          G_CALLBACK(body_activated), NULL);
209
210         from_label = gtk_label_new (_("From:"));
211         gtk_widget_show (from_label);
212         gtk_table_attach (GTK_TABLE (table1), from_label, 0, 1, 0, 1,
213                           GTK_FILL, 0, 0, 0);
214         gtk_label_set_justify (GTK_LABEL (from_label), GTK_JUSTIFY_RIGHT);
215         gtk_misc_set_alignment (GTK_MISC (from_label), 1, 0.5);
216
217         to_label = gtk_label_new (_("To:"));
218         gtk_widget_show (to_label);
219         gtk_table_attach (GTK_TABLE (table1), to_label, 0, 1, 1, 2,
220                           GTK_FILL, 0, 0, 0);
221         gtk_label_set_justify (GTK_LABEL (to_label), GTK_JUSTIFY_RIGHT);
222         gtk_misc_set_alignment (GTK_MISC (to_label), 1, 0.5);
223
224         subject_label = gtk_label_new (_("Subject:"));
225         gtk_widget_show (subject_label);
226         gtk_table_attach (GTK_TABLE (table1), subject_label, 0, 1, 2, 3,
227                           GTK_FILL, 0, 0, 0);
228         gtk_label_set_justify (GTK_LABEL (subject_label), GTK_JUSTIFY_RIGHT);
229         gtk_misc_set_alignment (GTK_MISC (subject_label), 1, 0.5);
230
231         body_label = gtk_label_new (_("Body:"));
232         gtk_widget_show (body_label);
233         gtk_table_attach (GTK_TABLE (table1), body_label, 0, 1, 3, 4,
234                           GTK_FILL, 0, 0, 0);
235         gtk_label_set_justify (GTK_LABEL (body_label), GTK_JUSTIFY_RIGHT);
236         gtk_misc_set_alignment (GTK_MISC (body_label), 1, 0.5);
237
238         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
239         gtk_widget_show (checkbtn_hbox);
240         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
241         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
242
243         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
244         gtk_widget_show (case_checkbtn);
245         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
246                             FALSE, FALSE, 0);
247
248         confirm_area = gtk_hbutton_box_new();
249         gtk_widget_show (confirm_area);
250         gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
251                                   GTK_BUTTONBOX_END);
252         gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
253
254         all_btn = gtk_button_new_with_mnemonic(_("Find _all"));
255         GTK_WIDGET_SET_FLAGS(all_btn, GTK_CAN_DEFAULT);
256         gtk_box_pack_start(GTK_BOX(confirm_area), all_btn, TRUE, TRUE, 0);
257         gtk_widget_show(all_btn);
258
259         prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
260         GTK_WIDGET_SET_FLAGS(prev_btn, GTK_CAN_DEFAULT);
261         gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
262         gtk_widget_show(prev_btn);
263
264         next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
265         GTK_WIDGET_SET_FLAGS(next_btn, GTK_CAN_DEFAULT);
266         gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
267         gtk_widget_show(next_btn);
268
269         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
270         GTK_WIDGET_SET_FLAGS(close_btn, GTK_CAN_DEFAULT);
271         gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
272         gtk_widget_show(close_btn);
273
274         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
275         gtk_widget_grab_default(next_btn);
276
277         g_signal_connect(G_OBJECT(clear_btn), "clicked",
278                          G_CALLBACK(summary_search_clear), NULL);
279         g_signal_connect(G_OBJECT(all_btn), "clicked",
280                          G_CALLBACK(summary_search_all_clicked), NULL);
281         g_signal_connect(G_OBJECT(prev_btn), "clicked",
282                          G_CALLBACK(summary_search_prev_clicked), NULL);
283         g_signal_connect(G_OBJECT(next_btn), "clicked",
284                          G_CALLBACK(summary_search_next_clicked), NULL);
285         g_signal_connect_closure
286                 (G_OBJECT(close_btn), "clicked",
287                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
288                                      window, NULL),
289                  FALSE);
290
291         search_window.window = window;
292         search_window.bool_optmenu = bool_optmenu;
293         search_window.from_entry = from_entry;
294         search_window.to_entry = to_entry;
295         search_window.subject_entry = subject_entry;
296         search_window.body_entry = body_entry;
297         search_window.case_checkbtn = case_checkbtn;
298         search_window.clear_btn = clear_btn;
299         search_window.all_btn = all_btn;
300         search_window.prev_btn = prev_btn;
301         search_window.next_btn = next_btn;
302         search_window.close_btn = close_btn;
303 }
304
305 static void summary_search_execute(gboolean backward, gboolean search_all)
306 {
307         SummaryView *summaryview = search_window.summaryview;
308         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
309         GtkCTreeNode *node;
310         MsgInfo *msginfo;
311         gboolean bool_and;
312         gboolean case_sens;
313         gboolean all_searched = FALSE;
314         gboolean matched;
315         gboolean body_matched;
316         const gchar *from_str, *to_str, *subject_str, *body_str;
317         StrFindFunc str_find_func;
318
319         if (summary_is_locked(summaryview)) return;
320         summary_lock(summaryview);
321
322         bool_and = GPOINTER_TO_INT
323                 (menu_get_option_menu_active_user_data
324                         (GTK_OPTION_MENU(search_window.bool_optmenu)));
325         case_sens = gtk_toggle_button_get_active
326                 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
327
328         if (case_sens)
329                 str_find_func = str_find;
330         else
331                 str_find_func = str_case_find;
332
333         from_str    = gtk_entry_get_text(GTK_ENTRY(search_window.from_entry));
334         to_str      = gtk_entry_get_text(GTK_ENTRY(search_window.to_entry));
335         subject_str = gtk_entry_get_text(GTK_ENTRY(search_window.subject_entry));
336         body_str    = gtk_entry_get_text(GTK_ENTRY(search_window.body_entry));
337
338         if (search_all) {
339                 gtk_clist_freeze(GTK_CLIST(ctree));
340                 gtk_clist_unselect_all(GTK_CLIST(ctree));
341                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
342                 backward = FALSE;
343         } else if (!summaryview->selected) {
344                 if (backward)
345                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
346                 else
347                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
348
349                 if (!node) {
350                         summary_unlock(summaryview);
351                         return;
352                 }
353         } else {
354                 if (backward)
355                         node = gtkut_ctree_node_prev
356                                 (ctree, summaryview->selected);
357                 else
358                         node = gtkut_ctree_node_next
359                                 (ctree, summaryview->selected);
360         }
361
362         if (*body_str)
363                 main_window_cursor_wait(summaryview->mainwin);
364
365         for (;;) {
366                 if (!node) {
367                         gchar *str;
368                         AlertValue val;
369
370                         if (search_all) {
371                                 gtk_clist_thaw(GTK_CLIST(ctree));
372                                 break;
373                         }
374
375                         if (all_searched) {
376                                 alertpanel_full(_("Search failed"),
377                                                 _("Search string not found."),
378                                                 GTK_STOCK_CLOSE, NULL, NULL, FALSE,
379                                                 NULL, ALERT_WARNING, G_ALERTDEFAULT);
380                                 break;
381                         }
382
383                         if (backward)
384                                 str = _("Beginning of list reached; continue from end?");
385                         else
386                                 str = _("End of list reached; continue from beginning?");
387
388                         val = alertpanel(_("Search finished"), str,
389                                          GTK_STOCK_NO, GTK_STOCK_YES, NULL);
390                         if (G_ALERTALTERNATE == val) {
391                                 if (backward)
392                                         node = GTK_CTREE_NODE
393                                                 (GTK_CLIST(ctree)->row_list_end);
394                                 else
395                                         node = GTK_CTREE_NODE
396                                                 (GTK_CLIST(ctree)->row_list);
397
398                                 all_searched = TRUE;
399
400                                 manage_window_focus_in(search_window.window, NULL, NULL);
401                         } else
402                                 break;
403                 }
404
405
406                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
407                 body_matched = FALSE;
408
409                 if (bool_and) {
410                         matched = TRUE;
411                         if (*from_str) {
412                                 if (!msginfo->from ||
413                                     !str_find_func(msginfo->from, from_str))
414                                         matched = FALSE;
415                         }
416                         if (matched && *to_str) {
417                                 if (!msginfo->to ||
418                                     !str_find_func(msginfo->to, to_str))
419                                         matched = FALSE;
420                         }
421                         if (matched && *subject_str) {
422                                 if (!msginfo->subject ||
423                                     !str_find_func(msginfo->subject, subject_str))
424                                         matched = FALSE;
425                         }
426                         if (matched && *body_str) {
427                                 if (procmime_find_string(msginfo, body_str,
428                                                          str_find_func))
429                                         body_matched = TRUE;
430                                 else
431                                         matched = FALSE;
432                         }
433                         if (matched && !*from_str && !*to_str &&
434                             !*subject_str && !*body_str)
435                                 matched = FALSE;
436                 } else {
437                         matched = FALSE;
438                         if (*from_str && msginfo->from) {
439                                 if (str_find_func(msginfo->from, from_str))
440                                         matched = TRUE;
441                         }
442                         if (!matched && *to_str && msginfo->to) {
443                                 if (str_find_func(msginfo->to, to_str))
444                                         matched = TRUE;
445                         }
446                         if (!matched && *subject_str && msginfo->subject) {
447                                 if (str_find_func(msginfo->subject, subject_str))
448                                         matched = TRUE;
449                         }
450                         if (!matched && *body_str) {
451                                 if (procmime_find_string(msginfo, body_str,
452                                                          str_find_func)) {
453                                         matched = TRUE;
454                                         body_matched = TRUE;
455                                 }
456                         }
457                 }
458
459                 if (matched) {
460                         if (search_all)
461                                 gtk_ctree_select(ctree, node);
462                         else {
463                                 if (messageview_is_visible
464                                         (summaryview->messageview)) {
465                                         summary_unlock(summaryview);
466                                         summary_select_node
467                                                 (summaryview, node, TRUE, TRUE);
468                                         summary_lock(summaryview);
469                                         if (body_matched) {
470                                                 messageview_search_string
471                                                         (summaryview->messageview,
472                                                          body_str, case_sens);
473                                         }
474                                 } else {
475                                         summary_select_node
476                                                 (summaryview, node, FALSE, TRUE);
477                                 }
478                                 break;
479                         }
480                 }
481
482                 node = backward ? gtkut_ctree_node_prev(ctree, node)
483                                 : gtkut_ctree_node_next(ctree, node);
484         }
485
486         if (*body_str)
487                 main_window_cursor_normal(summaryview->mainwin);
488
489         summary_unlock(summaryview);
490 }
491
492 static void summary_search_clear(GtkButton *button, gpointer data)
493 {
494         gtk_editable_delete_text(GTK_EDITABLE(search_window.from_entry),
495                                  0, -1);
496         gtk_editable_delete_text(GTK_EDITABLE(search_window.to_entry),
497                                  0, -1);
498         gtk_editable_delete_text(GTK_EDITABLE(search_window.subject_entry),
499                                  0, -1);
500         gtk_editable_delete_text(GTK_EDITABLE(search_window.body_entry),
501                                  0, -1);
502 }
503
504 static void summary_search_prev_clicked(GtkButton *button, gpointer data)
505 {
506         summary_search_execute(TRUE, FALSE);
507 }
508
509 static void summary_search_next_clicked(GtkButton *button, gpointer data)
510 {
511         summary_search_execute(FALSE, FALSE);
512 }
513
514 static void summary_search_all_clicked(GtkButton *button, gpointer data)
515 {
516         summary_search_execute(FALSE, TRUE);
517 }
518
519 static void from_activated(void)
520 {
521         gtk_widget_grab_focus(search_window.to_entry);
522 }
523
524 static void to_activated(void)
525 {
526         gtk_widget_grab_focus(search_window.subject_entry);
527 }
528
529 static void subject_activated(void)
530 {
531         gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
532 }
533
534 static void body_activated(void)
535 {
536         gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
537 }
538
539 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
540                             gpointer data)
541 {
542         if (event && event->keyval == GDK_Escape)
543                 gtk_widget_hide(search_window.window);
544         return FALSE;
545 }