2007-04-20 [colin] 2.9.1cvs4
[claws.git] / src / gtk / quicksearch.c
index ac33069be6dca926a191cbdea44c2ed1cf2f41ba..ed3b7c4a60d909d7d17e297c44e691e6edc4fe82 100644 (file)
@@ -1,6 +1,7 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2004 Hiroyuki Yamamoto & the Sylpheed-Claws team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2007 Colin Leroy <colin@colino.net> 
+ * and the Claws Mail team
  *
  * 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
@@ -38,7 +39,7 @@
 #include "folderview.h"
 #include "folder.h"
 #include "prefs_matcher.h"
-#include "sylpheed.h"
+#include "claws.h"
 #include "statusbar.h"
 
 struct _QuickSearch
@@ -63,11 +64,14 @@ struct _QuickSearch
        gboolean                         deferred_free;
        FolderItem                      *root_folder_item;
        gboolean                         is_fast;
+       gboolean                         in_typing;
+       guint                            press_timeout_id;
 };
 
 static void quicksearch_set_running(QuickSearch *quicksearch, gboolean run);
 static void quicksearch_set_active(QuickSearch *quicksearch, gboolean active);
 static void quicksearch_reset_folder_items(QuickSearch *quicksearch, FolderItem *folder_item);
+static gchar *expand_search_string(const gchar *str);
 
 gboolean quicksearch_is_fast(QuickSearch *quicksearch)
 {
@@ -110,6 +114,7 @@ static void prepare_matcher(QuickSearch *quicksearch)
                        return;
                }
        } else {
+               quicksearch->is_fast = TRUE;
                g_free(quicksearch->search_string);
                quicksearch->search_string = g_strdup(search_string);
        }
@@ -134,10 +139,18 @@ static void update_extended_buttons (QuickSearch *quicksearch)
        }
 }
 
-static gboolean searchbar_focus_evt(GtkWidget *widget, GdkEventFocus *event,
-                                 QuickSearch *quicksearch)
+static gboolean searchbar_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
+                                 QuickSearch *qs)
+{
+       qs->has_focus = TRUE;
+       return FALSE;
+}
+
+static gboolean searchbar_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
+                                 QuickSearch *qs)
 {
-       quicksearch->has_focus = (event && event->in);
+       qs->has_focus = FALSE;
+       qs->in_typing = FALSE;
        return FALSE;
 }
 
@@ -146,11 +159,12 @@ gboolean quicksearch_has_focus(QuickSearch *quicksearch)
        return quicksearch->has_focus;
 }
 
-static void searchbar_run(QuickSearch *quicksearch)
+static void searchbar_run(QuickSearch *quicksearch, gboolean run_only_if_fast)
 {
        const gchar *search_string = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry));
 
-       if (search_string && strlen(search_string) != 0) {
+       /* add to history */
+       if (!quicksearch->in_typing && search_string && strlen(search_string) != 0) {
                prefs_common.summary_quicksearch_history =
                        add_history(prefs_common.summary_quicksearch_history,
                                        search_string);
@@ -159,24 +173,75 @@ static void searchbar_run(QuickSearch *quicksearch)
        }
 
        prepare_matcher(quicksearch);
-
+       if (run_only_if_fast && !quicksearch->is_fast)
+               return;
+       if (quicksearch->matcher_list == NULL && 
+           prefs_common.summary_quicksearch_type == QUICK_SEARCH_EXTENDED &&
+           search_string && strlen(search_string) != 0)
+               return;
        quicksearch_set_running(quicksearch, TRUE);
        if (quicksearch->callback != NULL)
                quicksearch->callback(quicksearch, quicksearch->callback_data);
        quicksearch_set_running(quicksearch, FALSE);
 }
 
+static int searchbar_changed_timeout(void *data)
+{
+       QuickSearch *qs = (QuickSearch *)data;
+       if (qs && prefs_common.summary_quicksearch_dynamic) {
+               qs->in_typing = TRUE;
+               searchbar_run(qs, TRUE);
+       }
+       return FALSE;
+}
+
+static gboolean searchbar_changed_cb(GtkWidget *widget, QuickSearch *qs)
+{
+       if (prefs_common.summary_quicksearch_dynamic) {
+               if (qs->press_timeout_id != -1) {
+                       g_source_remove(qs->press_timeout_id);
+               }
+               qs->press_timeout_id = g_timeout_add(500,
+                               searchbar_changed_timeout, qs);
+       }
+
+       return FALSE;
+}
+
 static gboolean searchbar_pressed(GtkWidget *widget, GdkEventKey *event,
                                  QuickSearch *quicksearch)
 {
        if (event != NULL && event->keyval == GDK_Escape) {
-               quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
+
+               const gchar *str;
+
+               quicksearch->in_typing = FALSE;
+
+               str = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry));
+               g_return_val_if_fail(str != NULL, TRUE);
+
+               /* If the string entry is empty -> hide quicksearch bar. If not -> empty it */
+               if (!*str) {
+                       summaryview_activate_quicksearch(
+                               mainwindow_get_mainwindow()->summaryview, 
+                               FALSE);
+               } else {
+                       quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
+                       gtk_widget_grab_focus(
+                                       mainwindow_get_mainwindow()->summaryview->ctree);
+               }
+
                return TRUE;
        }
 
        if (event != NULL && event->keyval == GDK_Return) {
+               if (quicksearch->press_timeout_id != -1) {
+                       g_source_remove(quicksearch->press_timeout_id);
+                       quicksearch->press_timeout_id = -1;
+               }
+               quicksearch->in_typing = FALSE;
                /* add expression to history list and exec quicksearch */
-               searchbar_run(quicksearch);
+               searchbar_run(quicksearch, FALSE);
 
                g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
                return TRUE;
@@ -249,6 +314,20 @@ static gboolean searchtype_sticky_changed(GtkMenuItem *widget, gpointer data)
        return TRUE;
 }
 
+static gboolean searchtype_dynamic_changed(GtkMenuItem *widget, gpointer data)
+{
+       QuickSearch *quicksearch = (QuickSearch *)data;
+       gboolean checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
+
+       prefs_common.summary_quicksearch_dynamic = checked;
+
+       /* reselect the search type */
+       gtk_option_menu_set_history(GTK_OPTION_MENU(quicksearch->search_type_opt),
+                                   prefs_common.summary_quicksearch_type);
+
+       return TRUE;
+}
+
 /*
  * Strings describing how to use Extended Search
  *
@@ -347,7 +426,7 @@ static void search_condition_expr_done(MatcherList * matchers)
                g_free(str);
 
                /* add expression to history list and exec quicksearch */
-               searchbar_run(mainwindow_get_mainwindow()->summaryview->quicksearch);
+               searchbar_run(mainwindow_get_mainwindow()->summaryview->quicksearch, FALSE);
        }
 }
 
@@ -390,7 +469,8 @@ QuickSearch *quicksearch_new()
        GtkWidget *clear_search;
        GtkWidget *search_condition_expression;
        GtkWidget *menuitem;
-       GtkTooltips *search_cond_expr_tip;
+       GtkTooltips *tips = gtk_tooltips_new();
+       gint index;
 
        quicksearch = g_new0(QuickSearch, 1);
 
@@ -414,6 +494,10 @@ QuickSearch *quicksearch_new()
        g_signal_connect(G_OBJECT(menuitem), "activate",
                         G_CALLBACK(searchtype_changed),
                         quicksearch);
+       MENUITEM_ADD (search_type, menuitem, _("From, To or Subject"), QUICK_SEARCH_MIXED);
+       g_signal_connect(G_OBJECT(menuitem), "activate",
+                        G_CALLBACK(searchtype_changed),
+                        quicksearch);
        MENUITEM_ADD (search_type, menuitem, _("Extended"), QUICK_SEARCH_EXTENDED);
        g_signal_connect(G_OBJECT(menuitem), "activate",
                         G_CALLBACK(searchtype_changed),
@@ -441,9 +525,22 @@ QuickSearch *quicksearch_new()
                         G_CALLBACK(searchtype_sticky_changed),
                         quicksearch);
 
+       menuitem = gtk_check_menu_item_new_with_label(_("Type-ahead"));
+       gtk_menu_shell_append(GTK_MENU_SHELL(search_type), menuitem);
+
+       gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
+                                       prefs_common.summary_quicksearch_dynamic);
+
+       g_signal_connect(G_OBJECT(menuitem), "activate",
+                        G_CALLBACK(searchtype_dynamic_changed),
+                        quicksearch);
+
        gtk_option_menu_set_menu(GTK_OPTION_MENU(search_type_opt), search_type);
 
-       gtk_option_menu_set_history(GTK_OPTION_MENU(search_type_opt), prefs_common.summary_quicksearch_type);
+       index = menu_find_option_menu_index(GTK_OPTION_MENU(search_type_opt), 
+                                       GINT_TO_POINTER(prefs_common.summary_quicksearch_type),
+                                       NULL);
+       gtk_option_menu_set_history(GTK_OPTION_MENU(search_type_opt), index);
 
        gtk_widget_show(search_type);
 
@@ -468,6 +565,9 @@ QuickSearch *quicksearch_new()
                           FALSE, FALSE, 0);
        g_signal_connect(G_OBJECT(clear_search), "clicked",
                         G_CALLBACK(clear_search_cb), quicksearch);
+       gtk_tooltips_set_tip(GTK_TOOLTIPS(tips),
+                            clear_search,
+                            _("Clear the current search"), NULL);
        gtk_widget_show(clear_search);
 
 #if GTK_CHECK_VERSION(2, 8, 0)
@@ -480,8 +580,7 @@ QuickSearch *quicksearch_new()
        g_signal_connect(G_OBJECT (search_condition_expression), "clicked",
                         G_CALLBACK(search_condition_expr),
                         quicksearch);
-       search_cond_expr_tip = gtk_tooltips_new();
-       gtk_tooltips_set_tip(GTK_TOOLTIPS(search_cond_expr_tip),
+       gtk_tooltips_set_tip(GTK_TOOLTIPS(tips),
                             search_condition_expression,
                             _("Edit search criteria"), NULL);
        gtk_widget_show(search_condition_expression);
@@ -495,6 +594,9 @@ QuickSearch *quicksearch_new()
                           FALSE, FALSE, 0);
        g_signal_connect(G_OBJECT(search_description), "clicked",
                         G_CALLBACK(search_description_cb), NULL);
+       gtk_tooltips_set_tip(GTK_TOOLTIPS(tips),
+                            search_description,
+                            _("Information about extended symbols"), NULL);
        gtk_widget_show(search_description);
 
        gtk_box_pack_start(GTK_BOX(hbox_search), search_hbox, FALSE, FALSE, 2);
@@ -504,13 +606,19 @@ QuickSearch *quicksearch_new()
                           "key_press_event",
                           G_CALLBACK(searchbar_pressed),
                           quicksearch);
+
+       g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry),
+                        "changed",
+                        G_CALLBACK(searchbar_changed_cb),
+                        quicksearch);
+
        g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry),
                         "focus_in_event",
-                        G_CALLBACK(searchbar_focus_evt),
+                        G_CALLBACK(searchbar_focus_evt_in),
                         quicksearch);
        g_signal_connect(G_OBJECT(GTK_COMBO(search_string_entry)->entry),
                         "focus_out_event",
-                        G_CALLBACK(searchbar_focus_evt),
+                        G_CALLBACK(searchbar_focus_evt_out),
                         quicksearch);
 
        quicksearch->hbox_search = hbox_search;
@@ -523,12 +631,51 @@ QuickSearch *quicksearch_new()
        quicksearch->active = FALSE;
        quicksearch->running = FALSE;
        quicksearch->clear_search = clear_search;
+       quicksearch->in_typing = FALSE;
+       quicksearch->press_timeout_id = -1;
 
        update_extended_buttons(quicksearch);
 
        return quicksearch;
 }
 
+void quicksearch_relayout(QuickSearch *quicksearch)
+{
+       switch (prefs_common.layout_mode) {
+       case NORMAL_LAYOUT:
+       case WIDE_LAYOUT:
+       case WIDE_MSGLIST_LAYOUT:
+#if GTK_CHECK_VERSION(2, 8, 0)
+               gtk_button_set_label(GTK_BUTTON(quicksearch->search_description), GTK_STOCK_INFO);
+               gtk_button_set_label(GTK_BUTTON(quicksearch->search_condition_expression), GTK_STOCK_EDIT);
+               gtk_button_set_label(GTK_BUTTON(quicksearch->clear_search), GTK_STOCK_CLEAR);
+#else
+               gtk_button_set_label(GTK_BUTTON(quicksearch->search_description), _(" Extended Symbols... "));
+               gtk_button_set_label(GTK_BUTTON(quicksearch->search_condition_expression), " ... ");
+               gtk_button_set_label(GTK_BUTTON(quicksearch->clear_search), _(" Clear "));
+#endif
+               break;
+       case VERTICAL_LAYOUT:
+#if GTK_CHECK_VERSION(2, 8, 0)
+               gtk_button_set_label(GTK_BUTTON(quicksearch->search_description), "");
+               gtk_button_set_label(GTK_BUTTON(quicksearch->search_condition_expression), "");
+               gtk_button_set_label(GTK_BUTTON(quicksearch->clear_search), "");
+
+               gtk_button_set_image(GTK_BUTTON(quicksearch->search_description),
+                       gtk_image_new_from_stock(GTK_STOCK_INFO, GTK_ICON_SIZE_BUTTON));
+               gtk_button_set_image(GTK_BUTTON(quicksearch->search_condition_expression),
+                       gtk_image_new_from_stock(GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON));
+               gtk_button_set_image(GTK_BUTTON(quicksearch->clear_search),
+                       gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_BUTTON));
+#else
+               gtk_button_set_label(GTK_BUTTON(quicksearch->search_description), _("Info"));
+               gtk_button_set_label(GTK_BUTTON(quicksearch->search_condition_expression), "...");
+               gtk_button_set_label(GTK_BUTTON(quicksearch->clear_search), _("Clear"));
+#endif
+               break;
+       }
+}
+
 GtkWidget *quicksearch_get_widget(QuickSearch *quicksearch)
 {
        return quicksearch->hbox_search;
@@ -536,17 +683,34 @@ GtkWidget *quicksearch_get_widget(QuickSearch *quicksearch)
 
 void quicksearch_show(QuickSearch *quicksearch)
 {
+       MainWindow *mainwin = mainwindow_get_mainwindow();
+       GtkWidget *ctree = NULL;
        prepare_matcher(quicksearch);
        gtk_widget_show(quicksearch->hbox_search);
        update_extended_buttons(quicksearch);
        gtk_widget_grab_focus(
                GTK_WIDGET(GTK_COMBO(quicksearch->search_string_entry)->entry));
+
+       GTK_EVENTS_FLUSH();
+
+       if (!mainwin || !mainwin->summaryview) {
+               return;
+       }
+       
+       ctree = summary_get_main_widget(mainwin->summaryview);
+       
+       if (ctree && mainwin->summaryview->selected)
+               gtk_ctree_node_moveto(GTK_CTREE(ctree), 
+                               mainwin->summaryview->selected, 
+                               0, 0.5, 0);
 }
 
 void quicksearch_hide(QuickSearch *quicksearch)
 {
-       quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
-       quicksearch_set_active(quicksearch, FALSE);
+       if (quicksearch_is_active(quicksearch)) {
+               quicksearch_set(quicksearch, prefs_common.summary_quicksearch_type, "");
+               quicksearch_set_active(quicksearch, FALSE);
+       }
        gtk_widget_hide(quicksearch->hbox_search);
 }
 
@@ -555,8 +719,17 @@ void quicksearch_set(QuickSearch *quicksearch, QuickSearchType type,
 {
        gtk_option_menu_set_history(GTK_OPTION_MENU(quicksearch->search_type_opt),
                                    type);
+
+       if (!matchstring || !(*matchstring))
+               quicksearch->in_typing = FALSE;
+
+       g_signal_handlers_block_by_func(G_OBJECT(GTK_COMBO(quicksearch->search_string_entry)->entry),
+                       G_CALLBACK(searchbar_changed_cb), quicksearch);
        gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(quicksearch->search_string_entry)->entry),
                           matchstring);
+       g_signal_handlers_unblock_by_func(G_OBJECT(GTK_COMBO(quicksearch->search_string_entry)->entry),
+                       G_CALLBACK(searchbar_changed_cb), quicksearch);
+
        prefs_common.summary_quicksearch_type = type;
 
        prepare_matcher(quicksearch);
@@ -654,6 +827,8 @@ gboolean quicksearch_match(QuickSearch *quicksearch, MsgInfo *msginfo)
        case QUICK_SEARCH_TO:
                searched_header = msginfo->to;
                break;
+       case QUICK_SEARCH_MIXED:
+               break;
        case QUICK_SEARCH_EXTENDED:
                break;
        default:
@@ -662,9 +837,16 @@ gboolean quicksearch_match(QuickSearch *quicksearch, MsgInfo *msginfo)
        }
        quicksearch->matching = TRUE;
        if (prefs_common.summary_quicksearch_type != QUICK_SEARCH_EXTENDED &&
+           prefs_common.summary_quicksearch_type != QUICK_SEARCH_MIXED &&
            quicksearch->search_string &&
             searched_header && strcasestr(searched_header, quicksearch->search_string) != NULL)
                result = TRUE;
+       else if (prefs_common.summary_quicksearch_type == QUICK_SEARCH_MIXED &&
+               quicksearch->search_string && (
+               (msginfo->to && strcasestr(msginfo->to, quicksearch->search_string) != NULL) ||
+               (msginfo->from && strcasestr(msginfo->from, quicksearch->search_string) != NULL) ||
+               (msginfo->subject && strcasestr(msginfo->subject, quicksearch->search_string) != NULL)  ))
+               result = TRUE;
        else if ((quicksearch->matcher_list != NULL) &&
                 matcherlist_match(quicksearch->matcher_list, msginfo))
                result = TRUE;
@@ -678,7 +860,7 @@ gboolean quicksearch_match(QuickSearch *quicksearch, MsgInfo *msginfo)
 }
 
 /* allow Mutt-like patterns in quick search */
-gchar *expand_search_string(const gchar *search_string)
+static gchar *expand_search_string(const gchar *search_string)
 {
        int i = 0;
        gchar term_char, save_char;
@@ -916,24 +1098,25 @@ void quicksearch_pass_key(QuickSearch *quicksearch, guint val, GdkModifierType m
 static gboolean quicksearch_match_subfolder(QuickSearch *quicksearch,
                                 FolderItem *src)
 {
-       GSList *msglist = folder_item_get_msg_list(src);
+       GSList *msglist = NULL;
        GSList *cur;
        gboolean result = FALSE;
-       gint num = 0, total = src->total_msgs;
-       gint interval = quicksearch_is_fast(quicksearch) ? 1000:100;
+       gint num = 0, total = 0;
+       gint interval = quicksearch_is_fast(quicksearch) ? 5000:100;
 
        statusbar_print_all(_("Searching in %s... \n"),
                src->path ? src->path : "(null)");
+               
+       msglist = folder_item_get_msg_list(src);
+       total = src->total_msgs;
        folder_item_update_freeze();
        for (cur = msglist; cur != NULL; cur = cur->next) {
                MsgInfo *msg = (MsgInfo *)cur->data;
                statusbar_progress_all(num++,total, interval);
                if (quicksearch_match(quicksearch, msg)) {
-                       procmsg_msginfo_free(msg);
                        result = TRUE;
                        break;
                }
-               procmsg_msginfo_free(msg);
                if (num % interval == 0)
                        GTK_EVENTS_FLUSH();
                if (!quicksearch_is_active(quicksearch))
@@ -943,7 +1126,7 @@ static gboolean quicksearch_match_subfolder(QuickSearch *quicksearch,
        statusbar_progress_all(0,0,0);
        statusbar_pop_all();
 
-       g_slist_free(msglist);
+       procmsg_msg_list_free(msglist);
        return result;
 }
 
@@ -954,7 +1137,8 @@ void quicksearch_search_subfolders(QuickSearch *quicksearch,
        FolderItem *cur = NULL;
        GNode *node = folder_item->node->children;
 
-       if (!prefs_common.summary_quicksearch_recurse)
+       if (!prefs_common.summary_quicksearch_recurse
+       ||  quicksearch->in_typing == TRUE)
                return;
 
        for (; node != NULL; node = node->next) {
@@ -998,3 +1182,8 @@ void quicksearch_reset_cur_folder_item(QuickSearch *quicksearch)
 
        quicksearch->root_folder_item = NULL;
 }
+
+gboolean quicksearch_is_in_typing(QuickSearch *quicksearch)
+{
+       return quicksearch->in_typing;
+}