sync with 0.7.5cvs9
[claws.git] / src / summary_search.c
index b5f46b47b41cfea1872834e9aa442930609c9dc7..60e517d618dd821f2bd1b9fa9ec2be5d915c0b36 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999,2000 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2002 Hiroyuki Yamamoto
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -44,6 +44,7 @@
 #include "main.h"
 #include "summary_search.h"
 #include "summaryview.h"
+#include "mainwindow.h"
 #include "utils.h"
 #include "gtkutils.h"
 #include "manage_window.h"
@@ -53,6 +54,7 @@ static GtkWidget *window;
 static GtkWidget *from_entry;
 static GtkWidget *to_entry;
 static GtkWidget *subject_entry;
+static GtkWidget *body_entry;
 static GtkWidget *case_checkbtn;
 static GtkWidget *backward_checkbtn;
 static GtkWidget *all_checkbtn;
@@ -66,25 +68,21 @@ static void summary_search_clear(GtkButton *button, gpointer data);
 static void from_activated(void);
 static void to_activated(void);
 static void subject_activated(void);
+static void body_activated(void);
 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
 
 void summary_search(SummaryView *summaryview)
 {
        if (!window)
                summary_search_create(summaryview);
+       else
+               gtk_widget_hide(window);
 
        gtk_widget_grab_focus(search_btn);
        gtk_widget_grab_focus(subject_entry);
        gtk_widget_show(window);
 }
 
-void summary_search_cb(gpointer data, guint action, GtkWidget *widget)
-{
-       MainWindow *mainwin = data;
-
-       summary_search(mainwin->summaryview);
-}
-
 static void summary_search_create(SummaryView *summaryview)
 {
        GtkWidget *vbox1;
@@ -92,29 +90,26 @@ static void summary_search_create(SummaryView *summaryview)
        GtkWidget *from_label;
        GtkWidget *to_label;
        GtkWidget *subject_label;
+       GtkWidget *body_label;
        GtkWidget *checkbtn_hbox;
        GtkWidget *confirm_area;
 
        window = gtk_window_new (GTK_WINDOW_DIALOG);
-       gtk_window_set_title (GTK_WINDOW (window), _("Search"));
+       gtk_window_set_title (GTK_WINDOW (window), _("Search messages"));
        gtk_widget_set_usize (window, 450, -1);
-       /*gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);*/
        gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
        gtk_container_set_border_width (GTK_CONTAINER (window), 8);
        gtk_signal_connect(GTK_OBJECT(window), "delete_event",
                           GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete), NULL);
        gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
                           GTK_SIGNAL_FUNC(key_pressed), NULL);
-       gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
-                          GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
-       gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
-                          GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
+       MANAGE_WINDOW_SIGNALS_CONNECT(window);
 
        vbox1 = gtk_vbox_new (FALSE, 0);
        gtk_widget_show (vbox1);
        gtk_container_add (GTK_CONTAINER (window), vbox1);
 
-       table1 = gtk_table_new (3, 3, FALSE);
+       table1 = gtk_table_new (4, 3, FALSE);
        gtk_widget_show (table1);
        gtk_box_pack_start (GTK_BOX (vbox1), table1, TRUE, TRUE, 0);
        gtk_container_set_border_width (GTK_CONTAINER (table1), 4);
@@ -142,6 +137,13 @@ static void summary_search_create(SummaryView *summaryview)
        gtk_signal_connect(GTK_OBJECT(subject_entry), "activate",
                           GTK_SIGNAL_FUNC(subject_activated), summaryview);
 
+       body_entry = gtk_entry_new ();
+       gtk_widget_show (body_entry);
+       gtk_table_attach (GTK_TABLE (table1), body_entry, 1, 3, 3, 4,
+                         GTK_EXPAND|GTK_FILL, 0, 0, 0);
+       gtk_signal_connect(GTK_OBJECT(body_entry), "activate",
+                          GTK_SIGNAL_FUNC(body_activated), summaryview);
+
        from_label = gtk_label_new (_("From:"));
        gtk_widget_show (from_label);
        gtk_table_attach (GTK_TABLE (table1), from_label, 0, 1, 0, 1,
@@ -158,12 +160,18 @@ static void summary_search_create(SummaryView *summaryview)
 
        subject_label = gtk_label_new (_("Subject:"));
        gtk_widget_show (subject_label);
-
        gtk_table_attach (GTK_TABLE (table1), subject_label, 0, 1, 2, 3,
                          GTK_FILL, 0, 0, 0);
        gtk_label_set_justify (GTK_LABEL (subject_label), GTK_JUSTIFY_RIGHT);
        gtk_misc_set_alignment (GTK_MISC (subject_label), 1, 0.5);
 
+       body_label = gtk_label_new (_("Body:"));
+       gtk_widget_show (body_label);
+       gtk_table_attach (GTK_TABLE (table1), body_label, 0, 1, 3, 4,
+                         GTK_FILL, 0, 0, 0);
+       gtk_label_set_justify (GTK_LABEL (body_label), GTK_JUSTIFY_RIGHT);
+       gtk_misc_set_alignment (GTK_MISC (body_label), 1, 0.5);
+
        checkbtn_hbox = gtk_hbox_new (FALSE, 8);
        gtk_widget_show (checkbtn_hbox);
        gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
@@ -203,13 +211,6 @@ static void summary_search_create(SummaryView *summaryview)
        gtk_signal_connect_object(GTK_OBJECT(close_btn), "clicked",
                                  GTK_SIGNAL_FUNC(gtk_widget_hide),
                                  GTK_OBJECT(window));
-
-}
-
-#define STRDUP_MBSTOWCS(wcs, s) \
-{ \
-       wcs = g_malloc((strlen(s) + 1) * sizeof(wchar_t)); \
-       mbstowcs(wcs, s, strlen(s) + 1); \
 }
 
 static void summary_search_execute(GtkButton *button, gpointer data)
@@ -225,10 +226,15 @@ static void summary_search_execute(GtkButton *button, gpointer data)
        gboolean from_matched;
        gboolean   to_matched;
        gboolean subj_matched;
+       gboolean body_matched;
+       gchar *body_str;
        wchar_t *wcs_hs, *fromwcs, *towcs, *subjwcs;
        wchar_t *(* WCSFindFunc) (const wchar_t *haystack,
                                  const wchar_t *needle);
 
+       if (summary_is_locked(summaryview)) return;
+       summary_lock(summaryview);
+
        case_sens = gtk_toggle_button_get_active
                (GTK_TOGGLE_BUTTON(case_checkbtn));
        backward = gtk_toggle_button_get_active
@@ -252,6 +258,7 @@ static void summary_search_execute(GtkButton *button, gpointer data)
        fromwcs = (wchar_t *)GTK_ENTRY(from_entry)->text;
        towcs   = (wchar_t *)GTK_ENTRY(to_entry)->text;
        subjwcs = (wchar_t *)GTK_ENTRY(subject_entry)->text;
+       body_str = gtk_entry_get_text(GTK_ENTRY(body_entry));
 
        if (search_all) {
                gtk_clist_freeze(GTK_CLIST(ctree));
@@ -263,7 +270,10 @@ static void summary_search_execute(GtkButton *button, gpointer data)
                else
                        node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
 
-               if (!node) return;
+               if (!node) {
+                       summary_unlock(summaryview);
+                       return;
+               }
        } else {
                if (backward)
                        node = GTK_CTREE_NODE_PREV(summaryview->selected);
@@ -271,6 +281,9 @@ static void summary_search_execute(GtkButton *button, gpointer data)
                        node = GTK_CTREE_NODE_NEXT(summaryview->selected);
        }
 
+       if (*body_str)
+               main_window_cursor_wait(summaryview->mainwin);
+
        for (;;) {
                if (!node) {
                        gchar *str;
@@ -310,39 +323,51 @@ static void summary_search_execute(GtkButton *button, gpointer data)
                                break;
                }
 
-               from_matched = to_matched = subj_matched = FALSE;
+               from_matched = to_matched = subj_matched = body_matched = FALSE;
 
                msginfo = gtk_ctree_node_get_row_data(ctree, node);
 
                if (*fromwcs && msginfo->from) {
-                       STRDUP_MBSTOWCS(wcs_hs, msginfo->from);
-                       if (WCSFindFunc(wcs_hs, fromwcs) != NULL)
+                       wcs_hs = strdup_mbstowcs(msginfo->from);
+                       if (wcs_hs && WCSFindFunc(wcs_hs, fromwcs) != NULL)
                                from_matched = TRUE;
                        g_free(wcs_hs);
                }
                if (*towcs && msginfo->to) {
-                       STRDUP_MBSTOWCS(wcs_hs, msginfo->to);
-                       if (WCSFindFunc(wcs_hs, towcs) != NULL)
+                       wcs_hs = strdup_mbstowcs(msginfo->to);
+                       if (wcs_hs && WCSFindFunc(wcs_hs, towcs) != NULL)
                                to_matched = TRUE;
                        g_free(wcs_hs);
                }
                if (*subjwcs && msginfo->subject) {
-                       STRDUP_MBSTOWCS(wcs_hs, msginfo->subject);
-                       if (WCSFindFunc(wcs_hs, subjwcs) != NULL)
+                       wcs_hs = strdup_mbstowcs(msginfo->subject);
+                       if (wcs_hs && WCSFindFunc(wcs_hs, subjwcs) != NULL)
                                subj_matched = TRUE;
                        g_free(wcs_hs);
                }
+               if (*body_str) {
+                       if (procmime_find_string(msginfo, body_str, case_sens))
+                               body_matched = TRUE;
+               }
 
-               if (from_matched || to_matched || subj_matched) {
+               if (from_matched || to_matched || subj_matched || body_matched) {
                        if (search_all)
                                gtk_ctree_select(ctree, node);
                        else {
-                               gtk_clist_freeze(GTK_CLIST(ctree));
-                               gtk_clist_unselect_all(GTK_CLIST(ctree));
-                               gtk_ctree_select(ctree, node);
-                               gtk_ctree_node_moveto(ctree, node, 0, 0.5, 0);
-                               gtkut_ctree_set_focus_row(ctree, node);
-                               gtk_clist_thaw(GTK_CLIST(ctree));
+                               if (summaryview->msg_is_toggled_on) {
+                                       summary_unlock(summaryview);
+                                       summary_select_node
+                                               (summaryview, node, TRUE, TRUE);
+                                       summary_lock(summaryview);
+                                       if (body_matched) {
+                                               messageview_search_string
+                                                       (summaryview->messageview,
+                                                        body_str, case_sens);
+                                       }
+                               } else {
+                                       summary_select_node
+                                               (summaryview, node, FALSE, TRUE);
+                               }
                                break;
                        }
                }
@@ -350,6 +375,11 @@ static void summary_search_execute(GtkButton *button, gpointer data)
                node = backward ? GTK_CTREE_NODE_PREV(node)
                                : GTK_CTREE_NODE_NEXT(node);
        }
+
+       if (*body_str)
+               main_window_cursor_normal(summaryview->mainwin);
+
+       summary_unlock(summaryview);
 }
 
 static void summary_search_clear(GtkButton *button, gpointer data)
@@ -357,6 +387,7 @@ static void summary_search_clear(GtkButton *button, gpointer data)
        gtk_editable_delete_text(GTK_EDITABLE(from_entry),    0, -1);
        gtk_editable_delete_text(GTK_EDITABLE(to_entry),      0, -1);
        gtk_editable_delete_text(GTK_EDITABLE(subject_entry), 0, -1);
+       gtk_editable_delete_text(GTK_EDITABLE(body_entry),    0, -1);
 }
 
 static void from_activated(void)
@@ -374,6 +405,11 @@ static void subject_activated(void)
        gtk_button_clicked(GTK_BUTTON(search_btn));
 }
 
+static void body_activated(void)
+{
+       gtk_button_clicked(GTK_BUTTON(search_btn));
+}
+
 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
 {
        if (event && event->keyval == GDK_Escape)