2005-05-27 [paul] 1.9.11cvs16
[claws.git] / src / summary_search.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 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 <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_widget_set_size_request(window, 450, -1);
147         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
148         gtk_container_set_border_width(GTK_CONTAINER (window), 8);
149         g_signal_connect(G_OBJECT(window), "delete_event",
150                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
151         g_signal_connect(G_OBJECT(window), "key_press_event",
152                          G_CALLBACK(key_pressed), NULL);
153         MANAGE_WINDOW_SIGNALS_CONNECT(window);
154
155         vbox1 = gtk_vbox_new (FALSE, 0);
156         gtk_widget_show (vbox1);
157         gtk_container_add (GTK_CONTAINER (window), vbox1);
158
159         bool_hbox = gtk_hbox_new(FALSE, 4);
160         gtk_widget_show(bool_hbox);
161         gtk_box_pack_start(GTK_BOX(vbox1), bool_hbox, FALSE, FALSE, 0);
162
163         bool_optmenu = gtk_option_menu_new();
164         gtk_widget_show(bool_optmenu);
165         gtk_box_pack_start(GTK_BOX(bool_hbox), bool_optmenu, FALSE, FALSE, 0);
166
167         bool_menu = gtk_menu_new();
168         MENUITEM_ADD(bool_menu, menuitem, _("Match any of the following"), 0);
169         MENUITEM_ADD(bool_menu, menuitem, _("Match all of the following"), 1);
170         gtk_option_menu_set_menu(GTK_OPTION_MENU(bool_optmenu), bool_menu);
171
172         clear_btn = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
173         gtk_widget_show(clear_btn);
174         gtk_box_pack_end(GTK_BOX(bool_hbox), clear_btn, FALSE, FALSE, 0);
175
176         table1 = gtk_table_new (4, 3, FALSE);
177         gtk_widget_show (table1);
178         gtk_box_pack_start (GTK_BOX (vbox1), table1, TRUE, TRUE, 0);
179         gtk_container_set_border_width (GTK_CONTAINER (table1), 4);
180         gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
181         gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
182
183         from_entry = gtk_entry_new ();
184         gtk_widget_show (from_entry);
185         gtk_table_attach (GTK_TABLE (table1), from_entry, 1, 3, 0, 1,
186                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
187         g_signal_connect(G_OBJECT(from_entry), "activate",
188                          G_CALLBACK(from_activated), NULL);
189
190         to_entry = gtk_entry_new ();
191         gtk_widget_show (to_entry);
192         gtk_table_attach (GTK_TABLE (table1), to_entry, 1, 3, 1, 2,
193                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
194         g_signal_connect(G_OBJECT(to_entry), "activate",
195                          G_CALLBACK(to_activated), NULL);
196
197         subject_entry = gtk_entry_new ();
198         gtk_widget_show (subject_entry);
199         gtk_table_attach (GTK_TABLE (table1), subject_entry, 1, 3, 2, 3,
200                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
201         g_signal_connect(G_OBJECT(subject_entry), "activate",
202                          G_CALLBACK(subject_activated), NULL);
203
204         body_entry = gtk_entry_new ();
205         gtk_widget_show (body_entry);
206         gtk_table_attach (GTK_TABLE (table1), body_entry, 1, 3, 3, 4,
207                           GTK_EXPAND|GTK_FILL, 0, 0, 0);
208         g_signal_connect(G_OBJECT(body_entry), "activate",
209                          G_CALLBACK(body_activated), NULL);
210
211         from_label = gtk_label_new (_("From:"));
212         gtk_widget_show (from_label);
213         gtk_table_attach (GTK_TABLE (table1), from_label, 0, 1, 0, 1,
214                           GTK_FILL, 0, 0, 0);
215         gtk_label_set_justify (GTK_LABEL (from_label), GTK_JUSTIFY_RIGHT);
216         gtk_misc_set_alignment (GTK_MISC (from_label), 1, 0.5);
217
218         to_label = gtk_label_new (_("To:"));
219         gtk_widget_show (to_label);
220         gtk_table_attach (GTK_TABLE (table1), to_label, 0, 1, 1, 2,
221                           GTK_FILL, 0, 0, 0);
222         gtk_label_set_justify (GTK_LABEL (to_label), GTK_JUSTIFY_RIGHT);
223         gtk_misc_set_alignment (GTK_MISC (to_label), 1, 0.5);
224
225         subject_label = gtk_label_new (_("Subject:"));
226         gtk_widget_show (subject_label);
227         gtk_table_attach (GTK_TABLE (table1), subject_label, 0, 1, 2, 3,
228                           GTK_FILL, 0, 0, 0);
229         gtk_label_set_justify (GTK_LABEL (subject_label), GTK_JUSTIFY_RIGHT);
230         gtk_misc_set_alignment (GTK_MISC (subject_label), 1, 0.5);
231
232         body_label = gtk_label_new (_("Body:"));
233         gtk_widget_show (body_label);
234         gtk_table_attach (GTK_TABLE (table1), body_label, 0, 1, 3, 4,
235                           GTK_FILL, 0, 0, 0);
236         gtk_label_set_justify (GTK_LABEL (body_label), GTK_JUSTIFY_RIGHT);
237         gtk_misc_set_alignment (GTK_MISC (body_label), 1, 0.5);
238
239         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
240         gtk_widget_show (checkbtn_hbox);
241         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
242         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
243
244         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
245         gtk_widget_show (case_checkbtn);
246         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
247                             FALSE, FALSE, 0);
248
249         confirm_area = gtk_hbutton_box_new();
250         gtk_widget_show (confirm_area);
251         gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
252                                   GTK_BUTTONBOX_END);
253         gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
254
255         all_btn = gtk_button_new_from_stock(_("Find all"));
256         GTK_WIDGET_SET_FLAGS(all_btn, GTK_CAN_DEFAULT);
257         gtk_box_pack_start(GTK_BOX(confirm_area), all_btn, TRUE, TRUE, 0);
258         gtk_widget_show(all_btn);
259
260         prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
261         GTK_WIDGET_SET_FLAGS(prev_btn, GTK_CAN_DEFAULT);
262         gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
263         gtk_widget_show(prev_btn);
264
265         next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
266         GTK_WIDGET_SET_FLAGS(next_btn, GTK_CAN_DEFAULT);
267         gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
268         gtk_widget_show(next_btn);
269
270         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
271         GTK_WIDGET_SET_FLAGS(close_btn, GTK_CAN_DEFAULT);
272         gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
273         gtk_widget_show(close_btn);
274
275         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
276         gtk_widget_grab_default(next_btn);
277
278         g_signal_connect(G_OBJECT(clear_btn), "clicked",
279                          G_CALLBACK(summary_search_clear), NULL);
280         g_signal_connect(G_OBJECT(all_btn), "clicked",
281                          G_CALLBACK(summary_search_all_clicked), NULL);
282         g_signal_connect(G_OBJECT(prev_btn), "clicked",
283                          G_CALLBACK(summary_search_prev_clicked), NULL);
284         g_signal_connect(G_OBJECT(next_btn), "clicked",
285                          G_CALLBACK(summary_search_next_clicked), NULL);
286         g_signal_connect_closure
287                 (G_OBJECT(close_btn), "clicked",
288                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
289                                      window, NULL),
290                  FALSE);
291
292         search_window.window = window;
293         search_window.bool_optmenu = bool_optmenu;
294         search_window.from_entry = from_entry;
295         search_window.to_entry = to_entry;
296         search_window.subject_entry = subject_entry;
297         search_window.body_entry = body_entry;
298         search_window.case_checkbtn = case_checkbtn;
299         search_window.clear_btn = clear_btn;
300         search_window.all_btn = all_btn;
301         search_window.prev_btn = prev_btn;
302         search_window.next_btn = next_btn;
303         search_window.close_btn = close_btn;
304 }
305
306 static void summary_search_execute(gboolean backward, gboolean search_all)
307 {
308         SummaryView *summaryview = search_window.summaryview;
309         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
310         GtkCTreeNode *node;
311         MsgInfo *msginfo;
312         gboolean bool_and;
313         gboolean case_sens;
314         gboolean all_searched = FALSE;
315         gboolean matched;
316         gboolean body_matched;
317         const gchar *from_str, *to_str, *subject_str, *body_str;
318         StrFindFunc str_find_func;
319         gboolean valid;
320
321         if (summary_is_locked(summaryview)) return;
322         summary_lock(summaryview);
323
324         bool_and = GPOINTER_TO_INT
325                 (menu_get_option_menu_active_user_data
326                         (GTK_OPTION_MENU(search_window.bool_optmenu)));
327         case_sens = gtk_toggle_button_get_active
328                 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
329
330         if (case_sens)
331                 str_find_func = str_find;
332         else
333                 str_find_func = str_case_find;
334
335         from_str    = gtk_entry_get_text(GTK_ENTRY(search_window.from_entry));
336         to_str      = gtk_entry_get_text(GTK_ENTRY(search_window.to_entry));
337         subject_str = gtk_entry_get_text(GTK_ENTRY(search_window.subject_entry));
338         body_str    = gtk_entry_get_text(GTK_ENTRY(search_window.body_entry));
339
340         if (search_all) {
341                 gtk_clist_freeze(GTK_CLIST(ctree));
342                 gtk_clist_unselect_all(GTK_CLIST(ctree));
343                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
344                 backward = FALSE;
345         } else if (!summaryview->selected) {
346                 if (backward)
347                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list_end);
348                 else
349                         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
350
351                 if (!node) {
352                         summary_unlock(summaryview);
353                         return;
354                 }
355         } else {
356                 if (backward)
357                         node = gtkut_ctree_node_prev
358                                 (ctree, summaryview->selected);
359                 else
360                         node = gtkut_ctree_node_next
361                                 (ctree, summaryview->selected);
362         }
363
364         if (*body_str)
365                 main_window_cursor_wait(summaryview->mainwin);
366
367         for (;;) {
368                 if (!node) {
369                         gchar *str;
370                         AlertValue val;
371
372                         if (search_all) {
373                                 gtk_clist_thaw(GTK_CLIST(ctree));
374                                 break;
375                         }
376
377                         if (all_searched) {
378                                 alertpanel_with_type
379                                         (_("Search failed"),
380                                          _("Search string not found."),
381                                          NULL, NULL, NULL, NULL,
382                                          ALERT_WARNING);
383                                 break;
384                         }
385
386                         if (backward)
387                                 str = _("Beginning of list reached; continue from end?");
388                         else
389                                 str = _("End of list reached; continue from beginning?");
390
391                         val = alertpanel(_("Search finished"), str,
392                                          GTK_STOCK_YES, GTK_STOCK_NO, NULL);
393                         if (G_ALERTDEFAULT == val) {
394                                 if (backward)
395                                         node = GTK_CTREE_NODE
396                                                 (GTK_CLIST(ctree)->row_list_end);
397                                 else
398                                         node = GTK_CTREE_NODE
399                                                 (GTK_CLIST(ctree)->row_list);
400
401                                 all_searched = TRUE;
402
403                                 manage_window_focus_in(search_window.window, NULL, NULL);
404                         } else
405                                 break;
406                 }
407
408
409                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
410                 body_matched = FALSE;
411
412                 if (bool_and) {
413                         matched = TRUE;
414                         if (*from_str) {
415                                 if (!msginfo->from ||
416                                     !str_find_func(msginfo->from, from_str))
417                                         matched = FALSE;
418                         }
419                         if (matched && *to_str) {
420                                 if (!msginfo->to ||
421                                     !str_find_func(msginfo->to, to_str))
422                                         matched = FALSE;
423                         }
424                         if (matched && *subject_str) {
425                                 if (!msginfo->subject ||
426                                     !str_find_func(msginfo->subject, subject_str))
427                                         matched = FALSE;
428                         }
429                         if (matched && *body_str) {
430                                 if (procmime_find_string(msginfo, body_str,
431                                                          str_find_func))
432                                         body_matched = TRUE;
433                                 else
434                                         matched = FALSE;
435                         }
436                         if (matched && !*from_str && !*to_str &&
437                             !*subject_str && !*body_str)
438                                 matched = FALSE;
439                 } else {
440                         matched = FALSE;
441                         if (*from_str && msginfo->from) {
442                                 if (str_find_func(msginfo->from, from_str))
443                                         matched = TRUE;
444                         }
445                         if (!matched && *to_str && msginfo->to) {
446                                 if (str_find_func(msginfo->to, to_str))
447                                         matched = TRUE;
448                         }
449                         if (!matched && *subject_str && msginfo->subject) {
450                                 if (str_find_func(msginfo->subject, subject_str))
451                                         matched = TRUE;
452                         }
453                         if (!matched && *body_str) {
454                                 if (procmime_find_string(msginfo, body_str,
455                                                          str_find_func)) {
456                                         matched = TRUE;
457                                         body_matched = TRUE;
458                                 }
459                         }
460                 }
461
462                 if (matched) {
463                         if (search_all)
464                                 gtk_ctree_select(ctree, node);
465                         else {
466                                 if (messageview_is_visible
467                                         (summaryview->messageview)) {
468                                         summary_unlock(summaryview);
469                                         summary_select_node
470                                                 (summaryview, node, TRUE, TRUE);
471                                         summary_lock(summaryview);
472                                         if (body_matched) {
473                                                 messageview_search_string
474                                                         (summaryview->messageview,
475                                                          body_str, case_sens);
476                                         }
477                                 } else {
478                                         summary_select_node
479                                                 (summaryview, node, FALSE, TRUE);
480                                 }
481                                 break;
482                         }
483                 }
484
485                 node = backward ? gtkut_ctree_node_prev(ctree, node)
486                                 : gtkut_ctree_node_next(ctree, node);
487         }
488
489         if (*body_str)
490                 main_window_cursor_normal(summaryview->mainwin);
491
492         summary_unlock(summaryview);
493 }
494
495 static void summary_search_clear(GtkButton *button, gpointer data)
496 {
497         gtk_editable_delete_text(GTK_EDITABLE(search_window.from_entry),
498                                  0, -1);
499         gtk_editable_delete_text(GTK_EDITABLE(search_window.to_entry),
500                                  0, -1);
501         gtk_editable_delete_text(GTK_EDITABLE(search_window.subject_entry),
502                                  0, -1);
503         gtk_editable_delete_text(GTK_EDITABLE(search_window.body_entry),
504                                  0, -1);
505 }
506
507 static void summary_search_prev_clicked(GtkButton *button, gpointer data)
508 {
509         summary_search_execute(TRUE, FALSE);
510 }
511
512 static void summary_search_next_clicked(GtkButton *button, gpointer data)
513 {
514         summary_search_execute(FALSE, FALSE);
515 }
516
517 static void summary_search_all_clicked(GtkButton *button, gpointer data)
518 {
519         summary_search_execute(FALSE, TRUE);
520 }
521
522 static void from_activated(void)
523 {
524         gtk_widget_grab_focus(search_window.to_entry);
525 }
526
527 static void to_activated(void)
528 {
529         gtk_widget_grab_focus(search_window.subject_entry);
530 }
531
532 static void subject_activated(void)
533 {
534         gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
535 }
536
537 static void body_activated(void)
538 {
539         gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
540 }
541
542 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
543                             gpointer data)
544 {
545         if (event && event->keyval == GDK_Escape)
546                 gtk_widget_hide(search_window.window);
547         return FALSE;
548 }