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_debug ("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)) {
420 cur = (TnyHeader *) tny_iterator_get_current (iter);
421 t = tny_header_get_date_sent (cur);
424 /* Ignore deleted (not yet expunged) emails: */
425 if (tny_header_get_flags(cur) & TNY_HEADER_FLAG_DELETED)
428 if (helper->search->flags & MODEST_SEARCH_BEFORE)
429 if (!(t <= helper->search->end_date))
432 if (helper->search->flags & MODEST_SEARCH_AFTER)
433 if (!(t >= helper->search->start_date))
436 if (helper->search->flags & MODEST_SEARCH_SIZE)
437 if (tny_header_get_message_size (cur) < helper->search->minsize)
440 if (helper->search->flags & MODEST_SEARCH_SUBJECT) {
441 char *str = tny_header_dup_subject (cur);
443 if ((found = search_string (helper->search->subject, str, helper->search))) {
444 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
449 if (!found && helper->search->flags & MODEST_SEARCH_SENDER) {
450 char *str = tny_header_dup_from (cur);
452 if ((found = search_string (helper->search->from, (const gchar *) str, helper->search))) {
453 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
458 if (!found && helper->search->flags & MODEST_SEARCH_RECIPIENT) {
459 char *str = tny_header_dup_to (cur);
461 if ((found = search_string (helper->search->recipient, str, helper->search))) {
462 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
467 if (!found && helper->search->flags & MODEST_SEARCH_BODY) {
468 TnyHeaderFlags flags;
472 flags = tny_header_get_flags (cur);
474 if (!(flags & TNY_HEADER_FLAG_CACHED)) {
478 msg = tny_folder_get_msg (folder, cur, &err);
480 if (err != NULL || msg == NULL) {
481 g_warning ("%s: Could not get message.\n", __FUNCTION__);
485 g_object_unref (msg);
489 str = tny_header_dup_subject (cur);
490 g_debug ("Searching in %s\n", str);
493 found = search_mime_part_and_child_parts (TNY_MIME_PART (msg),
496 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
501 g_object_unref (msg);
504 g_object_unref (cur);
505 tny_iterator_next (iter);
509 g_object_unref (iter);
512 g_object_unref (headers);
514 /* Check search finished */
515 tny_list_remove (helper->all_folders, G_OBJECT (folder));
516 if (tny_list_get_length (helper->all_folders) == 0) {
518 helper->callback (helper->msg_hits, helper->user_data);
521 g_object_unref (helper->all_folders);
522 g_list_free (helper->msg_hits);
523 g_slice_free (SearchHelper, helper);
525 search_next_folder (helper);
530 _search_folder (TnyFolder *folder,
531 SearchHelper *helper)
533 TnyList *list = NULL;
535 g_debug ("%s: searching folder %s.", __FUNCTION__, tny_folder_get_name (folder));
537 /* Check that we should be searching this folder. */
538 /* Note that we don't try to search sub-folders.
539 * Maybe we should, but that should be specified. */
540 if (helper->search->folder && strlen (helper->search->folder)) {
541 if (!strcmp (helper->search->folder, "outbox")) {
542 if (modest_tny_folder_guess_folder_type (folder) != TNY_FOLDER_TYPE_OUTBOX) {
543 modest_search_folder_get_headers_cb (folder, TRUE, NULL, NULL, helper);
546 } else if (strcmp (tny_folder_get_id (folder), helper->search->folder) != 0) {
547 modest_search_folder_get_headers_cb (folder, TRUE, NULL, NULL, helper);
552 #ifdef MODEST_HAVE_OGS
553 if (helper->search->flags & MODEST_SEARCH_USE_OGS) {
555 if (helper->search->text_searcher == NULL && helper->search->query != NULL) {
556 OgsTextSearcher *text_searcher;
558 text_searcher = ogs_text_searcher_new (FALSE);
559 ogs_text_searcher_parse_query (text_searcher, helper->search->query);
560 helper->search->text_searcher = text_searcher;
564 list = tny_simple_list_new ();
565 /* Get the headers */
566 tny_folder_get_headers_async (folder, list, FALSE,
567 modest_search_folder_get_headers_cb,
572 modest_search_folder (TnyFolder *folder,
573 ModestSearch *search,
574 ModestSearchCallback callback,
577 SearchHelper *helper;
579 /* Create the helper */
580 helper = create_helper (callback, search, user_data);
583 _search_folder (folder, helper);
587 modest_search_account_get_folders_cb (TnyFolderStore *self,
594 SearchHelper *helper;
596 helper = (SearchHelper *) user_data;
598 if (err || cancelled) {
602 /* IMPORTANT: We need to get the headers of the folders one by
603 one, because otherwise the get_headers_async calls are
604 often canceled. That's why we firstly retrieve all folders,
605 and then we search inside them one by one. sergio */
606 iter = tny_list_create_iterator (folders);
607 while (!tny_iterator_is_done (iter)) {
608 TnyFolder *folder = NULL;
610 /* Search into folder */
611 folder = TNY_FOLDER (tny_iterator_get_current (iter));
612 tny_list_append (helper->all_folders, G_OBJECT (folder));
614 /* Search into children. Could be a merge folder */
615 if (TNY_IS_FOLDER_STORE (folder)) {
616 TnyList *children = tny_simple_list_new ();
617 helper->pending_calls++;
618 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (folder), children, NULL,
619 FALSE, modest_search_account_get_folders_cb,
623 g_object_unref (folder);
624 tny_iterator_next (iter);
626 g_object_unref (iter);
628 /* Remove the "account" reference */
629 helper->pending_calls--;
632 g_object_unref (folders);
634 /* If there are not more folders, begin to search from the first one */
635 if (helper->pending_calls == 0) {
636 TnyIterator *iter = tny_list_create_iterator (helper->all_folders);
637 TnyFolder *first = TNY_FOLDER (tny_iterator_get_current (iter));
639 _search_folder (first, helper);
641 g_object_unref (first);
642 g_object_unref (iter);
647 _search_account (TnyAccount *account,
648 SearchHelper *helper)
650 TnyList *folders = tny_simple_list_new ();
652 g_debug ("%s: Searching account %s", __FUNCTION__, tny_account_get_name (account));
654 /* Add a "reference" to the folder total. This allows the code
655 not to finalize the helper if an account is fully refreshed
656 before we get the folders of the others */
657 helper->pending_calls++;
660 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (account), folders, NULL,
661 FALSE, modest_search_account_get_folders_cb,
666 modest_search_account (TnyAccount *account,
667 ModestSearch *search,
668 ModestSearchCallback callback,
671 SearchHelper *helper;
673 /* Create the helper */
674 helper = create_helper (callback, search, user_data);
677 _search_account (account, helper);
681 modest_search_all_accounts (ModestSearch *search,
682 ModestSearchCallback callback,
685 ModestTnyAccountStore *astore;
689 SearchHelper *helper;
692 astore = modest_runtime_get_account_store ();
694 accounts = tny_simple_list_new ();
695 tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
697 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
699 /* Create the helper */
700 helper = create_helper (callback, search, user_data);
702 /* Search through all accounts */
703 iter = tny_list_create_iterator (accounts);
704 while (!tny_iterator_is_done (iter)) {
705 TnyAccount *account = NULL;
707 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
708 _search_account (account, helper);
709 g_object_unref (account);
711 tny_iterator_next (iter);
713 g_object_unref (iter);
714 g_object_unref (accounts);
717 static SearchHelper *
718 create_helper (ModestSearchCallback callback,
719 ModestSearch *search,
722 SearchHelper *helper;
724 helper = g_slice_new0 (SearchHelper);
725 helper->pending_calls = 0;
726 helper->search = search;
727 helper->callback = callback;
728 helper->user_data = user_data;
729 helper->msg_hits = NULL;
730 helper->all_folders = tny_simple_list_new ();
736 modest_search_free (ModestSearch *search)
739 g_free (search->folder);
741 g_free (search->subject);
743 g_free (search->from);
744 if (search->recipient)
745 g_free (search->recipient);
747 g_free (search->body);
749 #ifdef MODEST_HAVE_OGS
751 g_free (search->query);
752 if (search->text_searcher)
753 ogs_text_searcher_free (search->text_searcher);