1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <tny-shared.h>
41 #include <tny-folder.h>
42 #include <tny-folder-store.h>
44 #include <tny-iterator.h>
45 #include <tny-simple-list.h>
46 #include <tny-camel-imap-store-account.h>
47 #include <tny-camel-pop-store-account.h>
49 #include "modest-text-utils.h"
50 #include "modest-account-mgr.h"
51 #include "modest-tny-account-store.h"
52 #include "modest-tny-account.h"
53 #include "modest-tny-mime-part.h"
54 #include "modest-tny-folder.h"
55 #include "modest-search.h"
56 #include "modest-runtime.h"
57 #include "modest-platform.h"
64 ModestSearchCallback callback;
69 static SearchHelper *create_helper (ModestSearchCallback callback,
73 static void _search_folder (TnyFolder *folder,
74 SearchHelper *helper);
77 g_strdup_or_null (const gchar *str)
82 string = g_strdup (str);
89 add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
91 ModestSearchResultHit *hit;
99 hit = g_slice_new0 (ModestSearchResultHit);
101 furl = tny_folder_get_url_string (folder);
102 g_debug ("%s: folder URL=%s\n", __FUNCTION__, furl);
104 g_warning ("%s: tny_folder_get_url_string(): returned NULL for folder. Folder name=%s\n", __FUNCTION__, tny_folder_get_name (folder));
107 /* Make sure that we use the short UID instead of the long UID,
108 * and/or find out what UID form is used when finding, in camel_data_cache_get().
109 * so we can find what we get. Philip is working on this.
111 uid = tny_header_dup_uid (header);
113 gchar *subject = tny_header_dup_subject (header);
114 g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, subject);
118 msg_url = g_strdup_printf ("%s/%s", furl, uid);
122 subject = tny_header_dup_subject (header);
123 sender = tny_header_dup_from (header);
125 flags = tny_header_get_flags (header);
127 hit->msgid = msg_url;
128 hit->subject = subject;
129 hit->sender = sender;
130 hit->folder = g_strdup_or_null (tny_folder_get_name (folder));
131 hit->msize = tny_header_get_message_size (header);
132 hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
133 hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
134 hit->timestamp = MIN (tny_header_get_date_received (header), tny_header_get_date_sent (header));
136 return g_list_prepend (list, hit);
139 /** Call this until it returns FALSE or nread is set to 0.
141 * @result: FALSE is something failed. */
143 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
148 while (_nread < count) {
149 res = tny_stream_read (stream,
152 if (res == -1) { /* error */
159 if (res == 0) { /* no more bytes read. */
169 #ifdef MODEST_HAVE_OGS
171 * This function assumes that the mime part is of type "text / *"
174 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
176 TnyStream *stream = NULL;
178 const gsize len = sizeof (buffer);
180 gboolean is_text_html = FALSE;
181 gboolean found = FALSE;
182 gboolean res = FALSE;
184 is_text_html = tny_mime_part_content_type_is (part, "text/html");
186 stream = tny_mime_part_get_stream (part);
188 res = read_chunk (stream, buffer, len, &nread);
189 while (res && (nread > 0)) {
190 /* search->text_searcher was instantiated in modest_search_folder(). */
193 found = ogs_text_searcher_search_html (search->text_searcher,
198 found = ogs_text_searcher_search_text (search->text_searcher,
203 /* HACK: this helps UI refreshes because the search
204 operations could be heavy */
205 while (gtk_events_pending ())
206 gtk_main_iteration ();
213 res = read_chunk (stream, buffer, len, &nread);
215 g_object_unref (stream);
218 found = ogs_text_searcher_search_done (search->text_searcher);
221 ogs_text_searcher_reset (search->text_searcher);
229 * This function assumes that the mime part is of type "text / *"
232 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
243 len = (sizeof (buffer) - 1) / 2;
245 if (strlen (search->body) > len) {
246 g_warning ("Search term bigger then chunk."
247 "We might not find everything");
250 stream = tny_mime_part_get_stream (part);
252 memset (buffer, 0, sizeof (buffer));
254 chunk[1] = buffer + len;
256 res = read_chunk (stream, chunk[0], len, &nread);
262 found = !modest_text_utils_utf8_strcmp (search->body,
269 /* This works like this:
270 * buffer: [ooooooooooo|xxxxxxxxxxxx|\0]
271 * ^chunk[0] ^chunk[1]
272 * we have prefilled chunk[0] now we always read into chunk[1]
273 * and then move the content of chunk[1] to chunk[0].
274 * The idea is to prevent not finding search terms that are
275 * spread across 2 reads:
276 * buffer: [ooooooooTES|Txxxxxxxxxxx|\0]
277 * We should catch that because we always search the whole
278 * buffer not only the chunks.
280 * Of course that breaks for search terms > sizeof (chunk)
281 * but sizeof (chunk) should be big enough I guess (see
282 * the g_warning in this function)
284 while ((res = read_chunk (stream, chunk[1], len, &nread))) {
285 buffer[len + nread] = '\0';
287 found = !modest_text_utils_utf8_strcmp (search->body,
291 /* HACK: this helps UI refreshes because the search
292 operations could be heavy */
293 while (gtk_events_pending ())
294 gtk_main_iteration ();
296 if ((found)||(nread == 0)) {
300 /* also move the \0 */
301 g_memmove (chunk[0], chunk[1], len + 1);
305 g_object_unref (stream);
308 #endif /*MODEST_HAVE_OGS*/
311 search_string (const char *what,
313 ModestSearch *search)
315 gboolean found = FALSE;
316 #ifdef MODEST_HAVE_OGS
317 if (search->flags & MODEST_SEARCH_USE_OGS) {
318 found = ogs_text_searcher_search_text (search->text_searcher,
322 ogs_text_searcher_reset (search->text_searcher);
325 if (what == NULL || where == NULL) {
329 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
330 #ifdef MODEST_HAVE_OGS
334 /* HACK: this helps UI refreshes because the search
335 operations could be heavy */
336 while (gtk_events_pending ())
337 gtk_main_iteration ();
344 search_mime_part_and_child_parts (TnyMimePart *part, ModestSearch *search)
346 gboolean found = FALSE;
348 /* Do not search into attachments */
349 if (modest_tny_mime_part_is_attachment_for_modest (part) && !TNY_IS_MSG (part))
352 #ifdef MODEST_HAVE_OGS
353 found = search_mime_part_ogs (part, search);
355 found = search_mime_part_strcmp (part, search);
362 /* Check the child part too, recursively: */
363 TnyList *child_parts = tny_simple_list_new ();
364 tny_mime_part_get_parts (TNY_MIME_PART (part), child_parts);
366 TnyIterator *piter = tny_list_create_iterator (child_parts);
367 while (!found && !tny_iterator_is_done (piter)) {
368 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
370 found = search_mime_part_and_child_parts (pcur, search);
372 g_object_unref (pcur);
375 tny_iterator_next (piter);
378 g_object_unref (piter);
379 g_object_unref (child_parts);
385 search_next_folder (SearchHelper *helper)
387 TnyIterator *iter = tny_list_create_iterator (helper->all_folders);
388 TnyFolder *first = TNY_FOLDER (tny_iterator_get_current (iter));
390 _search_folder (first, helper);
392 g_object_unref (first);
393 g_object_unref (iter);
397 modest_search_folder_get_headers_cb (TnyFolder *folder,
403 TnyIterator *iter = NULL;
404 SearchHelper *helper;
406 helper = (SearchHelper *) user_data;
408 if (err || cancelled) {
412 iter = tny_list_create_iterator (headers);
414 while (!tny_iterator_is_done (iter)) {
416 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
417 const time_t t = tny_header_get_date_sent (cur);
418 gboolean found = FALSE;
420 /* Ignore deleted (not yet expunged) emails: */
421 if (tny_header_get_flags(cur) & TNY_HEADER_FLAG_DELETED)
424 if (helper->search->flags & MODEST_SEARCH_BEFORE)
425 if (!(t <= helper->search->end_date))
428 if (helper->search->flags & MODEST_SEARCH_AFTER)
429 if (!(t >= helper->search->start_date))
432 if (helper->search->flags & MODEST_SEARCH_SIZE)
433 if (tny_header_get_message_size (cur) < helper->search->minsize)
436 if (helper->search->flags & MODEST_SEARCH_SUBJECT) {
437 char *str = tny_header_dup_subject (cur);
439 if ((found = search_string (helper->search->subject, str, helper->search))) {
440 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
445 if (!found && helper->search->flags & MODEST_SEARCH_SENDER) {
446 char *str = tny_header_dup_from (cur);
448 if ((found = search_string (helper->search->from, (const gchar *) str, helper->search))) {
449 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
454 if (!found && helper->search->flags & MODEST_SEARCH_RECIPIENT) {
455 char *str = tny_header_dup_to (cur);
457 if ((found = search_string (helper->search->recipient, str, helper->search))) {
458 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
463 if (!found && helper->search->flags & MODEST_SEARCH_BODY) {
464 TnyHeaderFlags flags;
468 flags = tny_header_get_flags (cur);
470 if (!(flags & TNY_HEADER_FLAG_CACHED)) {
474 msg = tny_folder_get_msg (folder, cur, &err);
476 if (err != NULL || msg == NULL) {
477 g_warning ("%s: Could not get message.\n", __FUNCTION__);
481 g_object_unref (msg);
485 str = tny_header_dup_subject (cur);
486 g_debug ("Searching in %s\n", str);
489 found = search_mime_part_and_child_parts (TNY_MIME_PART (msg),
492 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
497 g_object_unref (msg);
500 g_object_unref (cur);
501 tny_iterator_next (iter);
505 g_object_unref (iter);
508 g_object_unref (headers);
510 /* Check search finished */
511 tny_list_remove (helper->all_folders, G_OBJECT (folder));
512 if (tny_list_get_length (helper->all_folders) == 0) {
514 helper->callback (helper->msg_hits, helper->user_data);
517 g_object_unref (helper->all_folders);
518 g_list_free (helper->msg_hits);
519 g_slice_free (SearchHelper, helper);
521 search_next_folder (helper);
526 _search_folder (TnyFolder *folder,
527 SearchHelper *helper)
529 TnyList *list = NULL;
531 g_debug ("%s: searching folder %s.", __FUNCTION__, tny_folder_get_name (folder));
533 /* Check that we should be searching this folder. */
534 /* Note that we don't try to search sub-folders.
535 * Maybe we should, but that should be specified. */
536 if (helper->search->folder && strlen (helper->search->folder)) {
537 if (!strcmp (helper->search->folder, "outbox")) {
538 if (modest_tny_folder_guess_folder_type (folder) != TNY_FOLDER_TYPE_OUTBOX) {
539 modest_search_folder_get_headers_cb (folder, TRUE, NULL, NULL, helper);
542 } else if (strcmp (tny_folder_get_id (folder), helper->search->folder) != 0) {
543 modest_search_folder_get_headers_cb (folder, TRUE, NULL, NULL, helper);
548 #ifdef MODEST_HAVE_OGS
549 if (helper->search->flags & MODEST_SEARCH_USE_OGS) {
551 if (helper->search->text_searcher == NULL && helper->search->query != NULL) {
552 OgsTextSearcher *text_searcher;
554 text_searcher = ogs_text_searcher_new (FALSE);
555 ogs_text_searcher_parse_query (text_searcher, helper->search->query);
556 helper->search->text_searcher = text_searcher;
560 list = tny_simple_list_new ();
561 /* Get the headers */
562 tny_folder_get_headers_async (folder, list, FALSE,
563 modest_search_folder_get_headers_cb,
568 modest_search_folder (TnyFolder *folder,
569 ModestSearch *search,
570 ModestSearchCallback callback,
573 SearchHelper *helper;
575 /* Create the helper */
576 helper = create_helper (callback, search, user_data);
579 _search_folder (folder, helper);
583 modest_search_account_get_folders_cb (TnyFolderStore *self,
590 SearchHelper *helper;
592 helper = (SearchHelper *) user_data;
594 if (err || cancelled) {
598 /* IMPORTANT: We need to get the headers of the folders one by
599 one, because otherwise the get_headers_async calls are
600 often canceled. That's why we firstly retrieve all folders,
601 and then we search inside them one by one. sergio */
602 iter = tny_list_create_iterator (folders);
603 while (!tny_iterator_is_done (iter)) {
604 TnyFolder *folder = NULL;
606 /* Search into folder */
607 folder = TNY_FOLDER (tny_iterator_get_current (iter));
608 tny_list_append (helper->all_folders, G_OBJECT (folder));
610 /* Search into children. Could be a merge folder */
611 if (TNY_IS_FOLDER_STORE (folder)) {
612 TnyList *children = tny_simple_list_new ();
613 helper->pending_calls++;
614 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (folder), children, NULL,
615 FALSE, modest_search_account_get_folders_cb,
619 g_object_unref (folder);
620 tny_iterator_next (iter);
622 g_object_unref (iter);
624 /* Remove the "account" reference */
625 helper->pending_calls--;
628 g_object_unref (folders);
630 /* If there are not more folders, begin to search from the first one */
631 if (helper->pending_calls == 0) {
632 TnyIterator *iter = tny_list_create_iterator (helper->all_folders);
633 TnyFolder *first = TNY_FOLDER (tny_iterator_get_current (iter));
635 _search_folder (first, helper);
637 g_object_unref (first);
638 g_object_unref (iter);
643 _search_account (TnyAccount *account,
644 SearchHelper *helper)
646 TnyList *folders = tny_simple_list_new ();
648 g_debug ("%s: Searching account %s", __FUNCTION__, tny_account_get_name (account));
650 /* Add a "reference" to the folder total. This allows the code
651 not to finalize the helper if an account is fully refreshed
652 before we get the folders of the others */
653 helper->pending_calls++;
656 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (account), folders, NULL,
657 FALSE, modest_search_account_get_folders_cb,
662 modest_search_account (TnyAccount *account,
663 ModestSearch *search,
664 ModestSearchCallback callback,
667 SearchHelper *helper;
669 /* Create the helper */
670 helper = create_helper (callback, search, user_data);
673 _search_account (account, helper);
677 modest_search_all_accounts (ModestSearch *search,
678 ModestSearchCallback callback,
681 ModestTnyAccountStore *astore;
685 SearchHelper *helper;
688 astore = modest_runtime_get_account_store ();
690 accounts = tny_simple_list_new ();
691 tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
693 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
695 /* Create the helper */
696 helper = create_helper (callback, search, user_data);
698 /* Search through all accounts */
699 iter = tny_list_create_iterator (accounts);
700 while (!tny_iterator_is_done (iter)) {
701 TnyAccount *account = NULL;
703 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
704 _search_account (account, helper);
705 g_object_unref (account);
707 tny_iterator_next (iter);
709 g_object_unref (iter);
710 g_object_unref (accounts);
713 static SearchHelper *
714 create_helper (ModestSearchCallback callback,
715 ModestSearch *search,
718 SearchHelper *helper;
720 helper = g_slice_new0 (SearchHelper);
721 helper->pending_calls = 0;
722 helper->search = search;
723 helper->callback = callback;
724 helper->user_data = user_data;
725 helper->msg_hits = NULL;
726 helper->all_folders = tny_simple_list_new ();
732 modest_search_free (ModestSearch *search)
735 g_free (search->folder);
737 g_free (search->subject);
739 g_free (search->from);
740 if (search->recipient)
741 g_free (search->recipient);
743 g_free (search->body);
745 #ifdef MODEST_HAVE_OGS
747 g_free (search->query);
748 if (search->text_searcher)
749 ogs_text_searcher_free (search->text_searcher);