2007-06-20 Murray Cumming <murrayc@murrayc-desktop>
[modest] / src / modest-search.c
index 958dce8..31aa485 100644 (file)
@@ -1,4 +1,35 @@
+/* Copyright (c) 2006, Nokia Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Nokia Corporation nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _GNU_SOURCE
 #define _GNU_SOURCE
+#endif
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -13,6 +44,8 @@
 #include <tny-iterator.h>
 #include <tny-simple-list.h>
 
+#include <libmodest-dbus-client/libmodest-dbus-client.h>
+
 #include "modest-text-utils.h"
 #include "modest-account-mgr.h"
 #include "modest-tny-account-store.h"
 #include "modest-search.h"
 #include "modest-runtime.h"
 
+static gchar *
+g_strdup_or_null (const gchar *str)
+{
+       gchar *string = NULL;
+
+       if  (str != NULL) {
+               string = g_strdup (str);
+       }
+
+       return string;
+}
+
+
 static GList*
-add_header (GList *list, TnyHeader *header, TnyFolder *folder)
+add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
 {
-       TnyFolder *f;
-       
-       /* TODO: we need this call otherwise it will crash later
-        * when we try to do that call again without having the
-        * folder around, I guess that is a bug in TinyThingy */
-       f = tny_header_get_folder (header);
-
-       return g_list_prepend (list, g_object_ref (header));
+       ModestSearchHit *hit;
+       TnyHeaderFlags   flags;
+       char            *furl;
+       char            *msg_url;
+       const char      *uid;
+       const char      *subject;
+       const char      *sender;
+
+       hit = g_slice_new0 (ModestSearchHit);
+
+       furl = tny_folder_get_url_string (folder);
+       printf ("DEBUG: %s: folder URL=%s\n", __FUNCTION__, furl);
+       if (!furl) {
+               g_warning ("%s: tny_folder_get_url_string(): returned NULL for folder. Folder name=%s\n", __FUNCTION__, tny_folder_get_name (folder));
+       }
+       
+       /* Make sure that we use the short UID instead of the long UID,
+        * and/or find out what UID form is used when finding, in camel_data_cache_get().
+        * so we can find what we get. Philip is working on this.
+        */
+       uid = tny_header_get_uid (header);
+       if (!furl) {
+               g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, tny_header_get_subject (header));
+       }
+       
+       msg_url = g_strdup_printf ("%s/%s", furl, uid);
+       g_free (furl);
+       
+       subject = tny_header_get_subject (header);
+       sender = tny_header_get_from (header);
+       
+       flags = tny_header_get_flags (header);
+
+       hit->msgid = msg_url;
+       hit->subject = g_strdup_or_null (subject);
+       hit->sender = g_strdup_or_null (sender);
+       hit->folder = g_strdup_or_null (tny_folder_get_name (folder));
+       hit->msize = tny_header_get_message_size (header);
+       hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
+       hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
+       hit->timestamp = tny_header_get_date_received (header);
+       
+       return g_list_prepend (list, hit);
 }
 
 static gboolean
@@ -57,8 +138,6 @@ read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
 
        *nread = _nread;
        return TRUE;
-
-
 }
 
 #ifdef MODEST_HAVE_OGS
@@ -74,8 +153,9 @@ search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
        gboolean   res;
 
 
-       if (! tny_mime_part_content_type_is (part, "text/ *") ||
+       if (! tny_mime_part_content_type_is (part, "text/*") ||
            ! (is_html = tny_mime_part_content_type_is (part, "text/html"))) {
+           g_debug ("%s: No text or html MIME part found.\n", __FUNCTION__);
                return FALSE;
        }
 
@@ -85,6 +165,8 @@ search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
 
        while ((res = read_chunk (stream, buffer, len, &nread))) {
 
+               /* search->text_searcher was instantiated in modest_search_folder(). */
+               
                if (is_html) {
 
                        found = ogs_text_searcher_search_html (search->text_searcher,
@@ -108,10 +190,22 @@ search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
        }
 
        ogs_text_searcher_reset (search->text_searcher);
+       
+       /* debug stuff:
+       if (!found) {
+               buffer[len -1] = 0;
+               printf ("DEBUG: %s: query %s was not found in message text: %s\n", 
+                       __FUNCTION__, search->query, buffer);   
+               
+       } else {
+               printf ("DEBUG: %s: found.\n", __FUNCTION__);   
+       }
+       */
 
        return found;
 }
-#endif
+
+#else
 
 static gboolean
 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
@@ -119,12 +213,13 @@ search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
        TnyStream *stream;
        char       buffer[8193];
        char      *chunk[2];
-       gsize      len;
-       gssize     nread;
+       gssize     len;
+       gsize     nread;
        gboolean   found;
        gboolean   res;
 
-       if (! tny_mime_part_content_type_is (part, "text/ *")) {
+       if (! tny_mime_part_content_type_is (part, "text/*")) {
+               g_debug ("%s: No text MIME part found.\n", __FUNCTION__);
                return FALSE;
        }
 
@@ -189,6 +284,7 @@ done:
        g_object_unref (stream);
        return found;
 }
+#endif /*MODEST_HAVE_OGS*/
 
 static gboolean
 search_string (const char      *what,
@@ -230,12 +326,9 @@ GList *
 modest_search_folder (TnyFolder *folder, ModestSearch *search)
 {
        GList *retval = NULL;
-       TnyIterator *iter;
-       TnyList *list;
-       gboolean (*part_search_func) (TnyMimePart *part, ModestSearch *search);
-
-       part_search_func = search_mime_part_strcmp;
-
+       TnyIterator *iter = NULL;
+       TnyList *list = NULL;
+       
 #ifdef MODEST_HAVE_OGS
        if (search->flags & MODEST_SEARCH_USE_OGS) {
        
@@ -246,13 +339,18 @@ modest_search_folder (TnyFolder *folder, ModestSearch *search)
                        ogs_text_searcher_parse_query (text_searcher, search->query);
                        search->text_searcher = text_searcher;
                }
-
-               part_search_func = search_mime_part_ogs;
        }
 #endif
 
        list = tny_simple_list_new ();
-       tny_folder_get_headers (folder, list, FALSE, NULL);
+       GError *error = NULL;
+       tny_folder_get_headers (folder, list, FALSE /* don't refresh */, &error);
+       if (error) {
+               g_warning ("%s: tny_folder_get_headers() failed with error=%s.\n", 
+               __FUNCTION__, error->message);
+               g_error_free (error);
+               error = NULL;   
+       }
 
        iter = tny_list_create_iterator (list);
 
@@ -277,23 +375,24 @@ modest_search_folder (TnyFolder *folder, ModestSearch *search)
                        const char *str = tny_header_get_subject (cur);
 
                        if ((found = search_string (search->subject, str, search))) {
-                           retval = add_header (retval, cur, folder);
+                           retval = add_hit (retval, cur, folder);
                        }
                }
                
                if (!found && search->flags & MODEST_SEARCH_SENDER) {
-                       const char *str = tny_header_get_from (cur);
+                       char *str = g_strdup (tny_header_get_from (cur));
 
-                       if ((found = search_string (search->from, str, search))) {
-                               retval = add_header (retval, cur, folder);
+                       if ((found = search_string (search->from, (const gchar *) str, search))) {
+                               retval = add_hit (retval, cur, folder);
                        }
+                       g_free (str);
                }
                
                if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
                        const char *str = tny_header_get_to (cur);
 
                        if ((found = search_string (search->recipient, str, search))) {
-                               retval = add_header (retval, cur, folder);
+                               retval = add_hit (retval, cur, folder);
                        }
                }
        
@@ -313,7 +412,7 @@ modest_search_folder (TnyFolder *folder, ModestSearch *search)
                        msg = tny_folder_get_msg (folder, cur, &err);
 
                        if (err != NULL || msg == NULL) {
-                               g_warning ("Could not get message\n");
+                               g_warning ("%s: Could not get message.\n", __FUNCTION__);
                                g_error_free (err);
 
                                if (msg) {
@@ -323,13 +422,26 @@ modest_search_folder (TnyFolder *folder, ModestSearch *search)
 
                        parts = tny_simple_list_new ();
                        tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
+                       
+                       if (tny_list_get_length(parts) == 0) {
+                               gchar *url_string = tny_msg_get_url_string (msg);
+                               g_debug ("DEBUG: %s: tny_mime_part_get_parts(msg) returned an empty list for message url=%s", 
+                                       __FUNCTION__, url_string);
+                               g_free (url_string);    
+                       }
 
                        piter = tny_list_create_iterator (parts);
                        while (!found && !tny_iterator_is_done (piter)) {
                                TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
 
-                               if ((found = part_search_func (pcur, search))) {
-                                       retval = add_header (retval, cur, folder);                              
+                               #ifdef MODEST_HAVE_OGS
+                               found = search_mime_part_ogs (pcur, search);
+                               #else
+                               found = search_mime_part_strcmp (pcur, search);
+                               #endif
+               
+                               if (found) {
+                                       retval = add_hit (retval, cur, folder);                         
                                }
 
                                g_object_unref (pcur);
@@ -380,6 +492,8 @@ modest_search_account (TnyAccount *account, ModestSearch *search)
                GList     *res;
 
                folder = TNY_FOLDER (tny_iterator_get_current (iter));
+               /* g_debug ("DEBUG: %s: searching folder %s.", 
+                       __FUNCTION__, tny_folder_get_name (folder)); */
                
                res = modest_search_folder (folder, search);
 
@@ -424,8 +538,8 @@ modest_search_all_accounts (ModestSearch *search)
 
                account = TNY_ACCOUNT (tny_iterator_get_current (iter));
 
-               g_debug ("Searching account %s",
-                        tny_account_get_name (account));
+               /* g_debug ("DEBUG: %s: Searching account %s",
+                        __FUNCTION__, tny_account_get_name (account)); */
                res = modest_search_account (account, search);
                
                if (res != NULL) {
@@ -442,6 +556,7 @@ modest_search_all_accounts (ModestSearch *search)
        }
 
        g_object_unref (accounts);
+       g_object_unref (iter);
 
        return hits;
 }