* check for a valid foldername
[modest] / src / modest-search.c
index f5b7186..3ed4ed6 100644 (file)
@@ -80,6 +80,7 @@ add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
        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));
        }
@@ -94,6 +95,7 @@ add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
        }
        
        msg_url = g_strdup_printf ("%s/%s", furl, uid);
+       g_free (furl);
        
        subject = tny_header_get_subject (header);
        sender = tny_header_get_from (header);
@@ -104,7 +106,6 @@ add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
        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));
-               //furl; /* We just provide our new instance instead of copying it and freeing it. */
        hit->msize = tny_header_get_message_size (header);
        hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
        hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
@@ -113,59 +114,81 @@ add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
        return g_list_prepend (list, hit);
 }
 
+/** Call this until it returns FALSE or nread is set to 0.
+ * 
+ * @result: FALSE is something failed. */
 static gboolean
 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
 {
-       gsize _nread;
-       gssize res;
+       gsize _nread = 0;
+       gssize res = 0;
 
-       _nread = 0;
        while (_nread < count) {
                res = tny_stream_read (stream,
                                       buffer + _nread, 
                                       count - _nread);
-               if (res == -1) {
+               if (res == -1) { /* error */
                        *nread = _nread;
                        return FALSE;
                }
 
-               if (res == 0)
-                       break;
-
                _nread += res;
+               
+               if (res == 0) { /* no more bytes read. */
+                       *nread = _nread;
+                       return TRUE;
+               }
        }
 
        *nread = _nread;
        return TRUE;
-
-
 }
 
 #ifdef MODEST_HAVE_OGS
 static gboolean
 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
 {
-       TnyStream *stream;
+       TnyStream *stream = NULL;
        char       buffer[4096];
-       gsize      len;
-       gsize      nread;
-       gboolean   is_html = FALSE;
-       gboolean   found;
-       gboolean   res;
-
-
-       if (! tny_mime_part_content_type_is (part, "text/ *") ||
-           ! (is_html = tny_mime_part_content_type_is (part, "text/html"))) {
-               return FALSE;
+       const gsize len = sizeof (buffer);
+       gsize      nread = 0;
+       gboolean   is_text_html = FALSE;
+       gboolean   found = FALSE;
+       gboolean   res = FALSE;
+
+       gboolean is_text = tny_mime_part_content_type_is (part, "text/*");
+       if (!is_text) {
+               g_debug ("%s: tny_mime_part_content_type_is() failed to find a "
+                       "text/* MIME part. Content type is %s", 
+               __FUNCTION__, "Unknown (calling tny_mime_part_get_content_type(part) causes a deadlock)");
+               
+           /* Retry with specific MIME types, because the wildcard seems to fail
+            * in tinymail.
+            * Actually I'm not sure anymore that it fails, so we could probalby 
+            * remove this later: murrayc */
+           is_text = (
+               tny_mime_part_content_type_is (part, "text/plain") ||
+               tny_mime_part_content_type_is (part, "text/html") );
+               
+               if (is_text) {
+                 g_debug ("%s: Retryting with text/plain or text/html succeeded", 
+                       __FUNCTION__);  
+               }
+       }
+       
+       if (!is_text) {
+           return FALSE;
        }
+       
+       is_text_html = tny_mime_part_content_type_is (part, "text/html");
 
-       found = FALSE;
-       len = sizeof (buffer);
        stream = tny_mime_part_get_stream (part);
 
-       while ((res = read_chunk (stream, buffer, len, &nread))) {
-
-               if (is_html) {
+       res = read_chunk (stream, buffer, len, &nread);
+       while (res && (nread > 0)) {
+               /* search->text_searcher was instantiated in modest_search_folder(). */
+               
+               if (is_text_html) {
 
                        found = ogs_text_searcher_search_html (search->text_searcher,
                                                               buffer,
@@ -180,7 +203,9 @@ search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
                if (found) {
                        break;
                }
-
+               
+               nread = 0;
+               res = read_chunk (stream, buffer, len, &nread);
        }
 
        if (!found) {
@@ -188,10 +213,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 /*MODEST_HAVE_OGS*/
+
+#else
 
 static gboolean
 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
@@ -204,7 +241,8 @@ search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
        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;
        }
 
@@ -269,6 +307,7 @@ done:
        g_object_unref (stream);
        return found;
 }
+#endif /*MODEST_HAVE_OGS*/
 
 static gboolean
 search_string (const char      *what,
@@ -297,25 +336,63 @@ search_string (const char      *what,
 }
 
 
+static gboolean search_mime_part_and_child_parts (TnyMimePart *part, ModestSearch *search)
+{
+       gboolean found = FALSE;
+       #ifdef MODEST_HAVE_OGS
+       found = search_mime_part_ogs (part, search);
+       #else
+       found = search_mime_part_strcmp (part, search);
+       #endif
+
+       if (found) {    
+               return found;           
+       }
+       
+       /* Check the child part too, recursively: */
+       TnyList *child_parts = tny_simple_list_new ();
+       tny_mime_part_get_parts (TNY_MIME_PART (part), child_parts);
+
+       TnyIterator *piter = tny_list_create_iterator (child_parts);
+       while (!found && !tny_iterator_is_done (piter)) {
+               TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
+               if (pcur) {
+                       found = search_mime_part_and_child_parts (pcur, search);
+
+                       g_object_unref (pcur);
+               }
+
+               tny_iterator_next (piter);
+       }
+
+       g_object_unref (piter);
+       g_object_unref (child_parts);
+       
+       return found;
+}
 
 /**
  * modest_search:
  * @folder: a #TnyFolder instance
  * @search: a #ModestSearch query
  *
- * This operation will search @folder for headers that match the query @search.
+ * This operation will search @folder for headers that match the query @search,
+ * if the folder itself matches the query.
  * It will return a doubly linked list with URIs that point to the message.
  **/
 GList *
 modest_search_folder (TnyFolder *folder, ModestSearch *search)
 {
+       /* Check that we should be searching this folder. */
+       /* Note that we don't try to search sub-folders. 
+        * Maybe we should, but that should be specified. */
+       if (search->folder && strlen (search->folder) && (strcmp (tny_folder_get_id (folder), search->folder) != 0))
+               return NULL;
+       
        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) {
        
@@ -326,21 +403,30 @@ 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);
 
        while (!tny_iterator_is_done (iter)) {
                TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
-               time_t t = tny_header_get_date_sent (cur);
+               const time_t t = tny_header_get_date_sent (cur);
                gboolean found = FALSE;
                
+               /* Ignore deleted (not yet expunged) emails: */
+               if (tny_header_get_flags(cur) & TNY_HEADER_FLAG_DELETED)
+                       goto go_next;
+                       
                if (search->flags & MODEST_SEARCH_BEFORE)
                        if (!(t <= search->before))
                                goto go_next;
@@ -382,8 +468,6 @@ modest_search_folder (TnyFolder *folder, ModestSearch *search)
                        TnyHeaderFlags flags;
                        GError      *err = NULL;
                        TnyMsg      *msg = NULL;
-                       TnyIterator *piter;
-                       TnyList     *parts;
 
                        flags = tny_header_get_flags (cur);
 
@@ -400,27 +484,17 @@ modest_search_folder (TnyFolder *folder, ModestSearch *search)
                                if (msg) {
                                        g_object_unref (msg);
                                }
-                       }       
-
-                       parts = tny_simple_list_new ();
-                       tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
-
-                       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_hit (retval, cur, folder);                         
+                       } else {        
+                       
+                               found = search_mime_part_and_child_parts (TNY_MIME_PART (msg), 
+                                                                         search);
+                               if (found) {
+                                       retval = add_hit (retval, cur, folder);
                                }
-
-                               g_object_unref (pcur);
-                               tny_iterator_next (piter);
                        }
-
-                       g_object_unref (piter);
-                       g_object_unref (parts);
-                       g_object_unref (msg);
-
+                       
+                       if (msg)
+                               g_object_unref (msg);
                }
 
 go_next:
@@ -457,34 +531,41 @@ modest_search_account (TnyAccount *account, ModestSearch *search)
 
        iter = tny_list_create_iterator (folders);
        while (!tny_iterator_is_done (iter)) {
-               TnyFolder *folder;
-               GList     *res;
+               TnyFolder *folder = NULL;
+               GList     *res = NULL;
 
                folder = TNY_FOLDER (tny_iterator_get_current (iter));
+               if (folder) {
+                       /* g_debug ("DEBUG: %s: searching folder %s.", 
+                               __FUNCTION__, tny_folder_get_name (folder)); */
                
-               res = modest_search_folder (folder, search);
+                       res = modest_search_folder (folder, search);
 
-               if (res != NULL) {
-                       if (hits == NULL) {
-                               hits = res;
-                       } else {
-                               hits = g_list_concat (hits, res);
+                       if (res != NULL) {
+                               if (hits == NULL) {
+                                       hits = res;
+                               } else {
+                                       hits = g_list_concat (hits, res);
+                               }
                        }
+
+                       g_object_unref (folder);
                }
 
-               g_object_unref (folder);
                tny_iterator_next (iter);
        }
 
        g_object_unref (iter);
        g_object_unref (folders);
 
+       /* printf ("DEBUG: %s: hits length = %d\n", __FUNCTION__, g_list_length (hits)); */
        return hits;
 }
 
 GList *
 modest_search_all_accounts (ModestSearch *search)
 {
+       /* printf ("DEBUG: %s: query=%s\n", __FUNCTION__, search->query); */
        ModestTnyAccountStore *astore;
        TnyList               *accounts;
        TnyIterator           *iter;
@@ -500,31 +581,33 @@ modest_search_all_accounts (ModestSearch *search)
 
        iter = tny_list_create_iterator (accounts);
        while (!tny_iterator_is_done (iter)) {
-               TnyAccount *account;
-               GList      *res;
+               TnyAccount *account = NULL;
+               GList      *res = NULL;
 
                account = TNY_ACCOUNT (tny_iterator_get_current (iter));
-
-               g_debug ("DEBUG: %s: Searching account %s",
-                        __FUNCTION__, tny_account_get_name (account));
-               res = modest_search_account (account, search);
-               
-               if (res != NULL) {
-
-                       if (hits == NULL) {
-                               hits = res;
-                       } else {
-                               hits = g_list_concat (hits, res);
+               if (account) {
+                       /* g_debug ("DEBUG: %s: Searching account %s",
+                        __FUNCTION__, tny_account_get_name (account)); */
+                       res = modest_search_account (account, search);
+                       
+                       if (res != NULL) {      
+                               if (hits == NULL) {
+                                       hits = res;
+                               } else {
+                                       hits = g_list_concat (hits, res);
+                               }
                        }
+                       
+                       g_object_unref (account);
                }
 
-               g_object_unref (account);
                tny_iterator_next (iter);
        }
 
        g_object_unref (accounts);
        g_object_unref (iter);
 
+       /* printf ("DEBUG: %s: end: hits length=%d\n", __FUNCTION__, g_list_length(hits)); */
        return hits;
 }