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"
65 ModestSearchCallback callback;
69 static SearchHelper *create_helper (ModestSearchCallback callback,
73 static void check_search_finished (SearchHelper *helper);
76 g_strdup_or_null (const gchar *str)
81 string = g_strdup (str);
88 add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
90 ModestSearchResultHit *hit;
98 hit = g_slice_new0 (ModestSearchResultHit);
100 furl = tny_folder_get_url_string (folder);
101 g_debug ("%s: folder URL=%s\n", __FUNCTION__, furl);
103 g_warning ("%s: tny_folder_get_url_string(): returned NULL for folder. Folder name=%s\n", __FUNCTION__, tny_folder_get_name (folder));
106 /* Make sure that we use the short UID instead of the long UID,
107 * and/or find out what UID form is used when finding, in camel_data_cache_get().
108 * so we can find what we get. Philip is working on this.
110 uid = tny_header_get_uid (header);
112 g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, tny_header_get_subject (header));
115 msg_url = g_strdup_printf ("%s/%s", furl, uid);
118 subject = tny_header_get_subject (header);
119 sender = tny_header_get_from (header);
121 flags = tny_header_get_flags (header);
123 hit->msgid = msg_url;
124 hit->subject = g_strdup_or_null (subject);
125 hit->sender = g_strdup_or_null (sender);
126 hit->folder = g_strdup_or_null (tny_folder_get_name (folder));
127 hit->msize = tny_header_get_message_size (header);
128 hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
129 hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
130 hit->timestamp = MIN (tny_header_get_date_received (header), tny_header_get_date_sent (header));
132 return g_list_prepend (list, hit);
135 /** Call this until it returns FALSE or nread is set to 0.
137 * @result: FALSE is something failed. */
139 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
144 while (_nread < count) {
145 res = tny_stream_read (stream,
148 if (res == -1) { /* error */
155 if (res == 0) { /* no more bytes read. */
165 #ifdef MODEST_HAVE_OGS
167 * This function assumes that the mime part is of type "text / *"
170 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
172 TnyStream *stream = NULL;
174 const gsize len = sizeof (buffer);
176 gboolean is_text_html = FALSE;
177 gboolean found = FALSE;
178 gboolean res = FALSE;
180 is_text_html = tny_mime_part_content_type_is (part, "text/html");
182 stream = tny_mime_part_get_stream (part);
184 res = read_chunk (stream, buffer, len, &nread);
185 while (res && (nread > 0)) {
186 /* search->text_searcher was instantiated in modest_search_folder(). */
189 found = ogs_text_searcher_search_html (search->text_searcher,
194 found = ogs_text_searcher_search_text (search->text_searcher,
199 /* HACK: this helps UI refreshes because the search
200 operations could be heavy */
201 while (gtk_events_pending ())
202 gtk_main_iteration ();
209 res = read_chunk (stream, buffer, len, &nread);
211 g_object_unref (stream);
214 found = ogs_text_searcher_search_done (search->text_searcher);
217 ogs_text_searcher_reset (search->text_searcher);
225 * This function assumes that the mime part is of type "text / *"
228 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
239 len = (sizeof (buffer) - 1) / 2;
241 if (strlen (search->body) > len) {
242 g_warning ("Search term bigger then chunk."
243 "We might not find everything");
246 stream = tny_mime_part_get_stream (part);
248 memset (buffer, 0, sizeof (buffer));
250 chunk[1] = buffer + len;
252 res = read_chunk (stream, chunk[0], len, &nread);
258 found = !modest_text_utils_utf8_strcmp (search->body,
265 /* This works like this:
266 * buffer: [ooooooooooo|xxxxxxxxxxxx|\0]
267 * ^chunk[0] ^chunk[1]
268 * we have prefilled chunk[0] now we always read into chunk[1]
269 * and then move the content of chunk[1] to chunk[0].
270 * The idea is to prevent not finding search terms that are
271 * spread across 2 reads:
272 * buffer: [ooooooooTES|Txxxxxxxxxxx|\0]
273 * We should catch that because we always search the whole
274 * buffer not only the chunks.
276 * Of course that breaks for search terms > sizeof (chunk)
277 * but sizeof (chunk) should be big enough I guess (see
278 * the g_warning in this function)
280 while ((res = read_chunk (stream, chunk[1], len, &nread))) {
281 buffer[len + nread] = '\0';
283 found = !modest_text_utils_utf8_strcmp (search->body,
287 /* HACK: this helps UI refreshes because the search
288 operations could be heavy */
289 while (gtk_events_pending ())
290 gtk_main_iteration ();
292 if ((found)||(nread == 0)) {
296 /* also move the \0 */
297 g_memmove (chunk[0], chunk[1], len + 1);
301 g_object_unref (stream);
304 #endif /*MODEST_HAVE_OGS*/
307 search_string (const char *what,
309 ModestSearch *search)
311 gboolean found = FALSE;
312 #ifdef MODEST_HAVE_OGS
313 if (search->flags & MODEST_SEARCH_USE_OGS) {
314 found = ogs_text_searcher_search_text (search->text_searcher,
318 ogs_text_searcher_reset (search->text_searcher);
321 if (what == NULL || where == NULL) {
325 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
326 #ifdef MODEST_HAVE_OGS
330 /* HACK: this helps UI refreshes because the search
331 operations could be heavy */
332 while (gtk_events_pending ())
333 gtk_main_iteration ();
340 search_mime_part_and_child_parts (TnyMimePart *part, ModestSearch *search)
342 gboolean found = FALSE;
344 /* Do not search into attachments */
345 if (modest_tny_mime_part_is_attachment_for_modest (part))
348 #ifdef MODEST_HAVE_OGS
349 found = search_mime_part_ogs (part, search);
351 found = search_mime_part_strcmp (part, search);
358 /* Check the child part too, recursively: */
359 TnyList *child_parts = tny_simple_list_new ();
360 tny_mime_part_get_parts (TNY_MIME_PART (part), child_parts);
362 TnyIterator *piter = tny_list_create_iterator (child_parts);
363 while (!found && !tny_iterator_is_done (piter)) {
364 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
366 found = search_mime_part_and_child_parts (pcur, search);
368 g_object_unref (pcur);
371 tny_iterator_next (piter);
374 g_object_unref (piter);
375 g_object_unref (child_parts);
381 modest_search_folder_get_headers_cb (TnyFolder *folder,
387 TnyIterator *iter = NULL;
388 SearchHelper *helper;
390 helper = (SearchHelper *) user_data;
392 if (err || cancelled) {
396 iter = tny_list_create_iterator (headers);
398 while (!tny_iterator_is_done (iter)) {
400 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
401 const time_t t = tny_header_get_date_sent (cur);
402 gboolean found = FALSE;
404 /* Ignore deleted (not yet expunged) emails: */
405 if (tny_header_get_flags(cur) & TNY_HEADER_FLAG_DELETED)
408 if (helper->search->flags & MODEST_SEARCH_BEFORE)
409 if (!(t <= helper->search->end_date))
412 if (helper->search->flags & MODEST_SEARCH_AFTER)
413 if (!(t >= helper->search->start_date))
416 if (helper->search->flags & MODEST_SEARCH_SIZE)
417 if (tny_header_get_message_size (cur) < helper->search->minsize)
420 if (helper->search->flags & MODEST_SEARCH_SUBJECT) {
421 const char *str = tny_header_get_subject (cur);
423 if ((found = search_string (helper->search->subject, str, helper->search))) {
424 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
428 if (!found && helper->search->flags & MODEST_SEARCH_SENDER) {
429 char *str = g_strdup (tny_header_get_from (cur));
431 if ((found = search_string (helper->search->from, (const gchar *) str, helper->search))) {
432 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
437 if (!found && helper->search->flags & MODEST_SEARCH_RECIPIENT) {
438 const char *str = tny_header_get_to (cur);
440 if ((found = search_string (helper->search->recipient, str, helper->search))) {
441 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
445 if (!found && helper->search->flags & MODEST_SEARCH_BODY) {
446 TnyHeaderFlags flags;
450 flags = tny_header_get_flags (cur);
452 if (!(flags & TNY_HEADER_FLAG_CACHED)) {
456 msg = tny_folder_get_msg (folder, cur, &err);
458 if (err != NULL || msg == NULL) {
459 g_warning ("%s: Could not get message.\n", __FUNCTION__);
463 g_object_unref (msg);
466 g_debug ("Searching in %s\n", tny_header_get_subject (cur));
468 found = search_mime_part_and_child_parts (TNY_MIME_PART (msg),
471 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
476 g_object_unref (msg);
479 g_object_unref (cur);
480 tny_iterator_next (iter);
484 g_object_unref (iter);
487 g_object_unref (headers);
489 /* Check search finished */
490 helper->folder_count++;
491 check_search_finished (helper);
495 _search_folder (TnyFolder *folder,
496 SearchHelper *helper)
498 TnyList *list = NULL;
500 g_debug ("%s: searching folder %s.", __FUNCTION__, tny_folder_get_name (folder));
502 /* Check that we should be searching this folder. */
503 /* Note that we don't try to search sub-folders.
504 * Maybe we should, but that should be specified. */
505 if (helper->search->folder && strlen (helper->search->folder)) {
506 if (!strcmp (helper->search->folder, "outbox")) {
507 if (modest_tny_folder_guess_folder_type (folder) != TNY_FOLDER_TYPE_OUTBOX) {
508 modest_search_folder_get_headers_cb (folder, TRUE, NULL, NULL, helper);
511 } else if (strcmp (tny_folder_get_id (folder), helper->search->folder) != 0) {
512 modest_search_folder_get_headers_cb (folder, TRUE, NULL, NULL, helper);
517 #ifdef MODEST_HAVE_OGS
518 if (helper->search->flags & MODEST_SEARCH_USE_OGS) {
520 if (helper->search->text_searcher == NULL && helper->search->query != NULL) {
521 OgsTextSearcher *text_searcher;
523 text_searcher = ogs_text_searcher_new (FALSE);
524 ogs_text_searcher_parse_query (text_searcher, helper->search->query);
525 helper->search->text_searcher = text_searcher;
529 list = tny_simple_list_new ();
530 /* Get the headers */
531 tny_folder_get_headers_async (folder, list, FALSE,
532 modest_search_folder_get_headers_cb,
537 modest_search_folder (TnyFolder *folder,
538 ModestSearch *search,
539 ModestSearchCallback callback,
542 SearchHelper *helper;
544 /* Create the helper */
545 helper = create_helper (callback, search, user_data);
548 _search_folder (folder, helper);
552 modest_search_account_get_folders_cb (TnyFolderStore *self,
559 SearchHelper *helper;
561 helper = (SearchHelper *) user_data;
563 if (err || cancelled) {
567 iter = tny_list_create_iterator (folders);
568 while (!tny_iterator_is_done (iter)) {
569 TnyFolder *folder = NULL;
571 /* Search into folder */
572 folder = TNY_FOLDER (tny_iterator_get_current (iter));
573 helper->folder_total++;
574 _search_folder (folder, (SearchHelper *) user_data);
575 g_object_unref (folder);
577 tny_iterator_next (iter);
579 g_object_unref (iter);
582 g_object_unref (folders);
584 /* Check search finished */
585 check_search_finished (helper);
589 _search_account (TnyAccount *account,
590 SearchHelper *helper)
592 TnyList *folders = tny_simple_list_new ();
594 g_debug ("%s: Searching account %s", __FUNCTION__, tny_account_get_name (account));
597 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (account), folders, NULL,
598 modest_search_account_get_folders_cb,
603 modest_search_account (TnyAccount *account,
604 ModestSearch *search,
605 ModestSearchCallback callback,
608 SearchHelper *helper;
610 /* Create the helper */
611 helper = create_helper (callback, search, user_data);
614 _search_account (account, helper);
618 modest_search_all_accounts (ModestSearch *search,
619 ModestSearchCallback callback,
622 ModestTnyAccountStore *astore;
626 SearchHelper *helper;
629 astore = modest_runtime_get_account_store ();
631 accounts = tny_simple_list_new ();
632 tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
634 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
636 /* Create the helper */
637 helper = create_helper (callback, search, user_data);
639 /* Search through all accounts */
640 iter = tny_list_create_iterator (accounts);
641 while (!tny_iterator_is_done (iter)) {
642 TnyAccount *account = NULL;
644 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
645 _search_account (account, helper);
646 g_object_unref (account);
648 tny_iterator_next (iter);
650 g_object_unref (iter);
651 g_object_unref (accounts);
654 static SearchHelper *
655 create_helper (ModestSearchCallback callback,
656 ModestSearch *search,
659 SearchHelper *helper;
661 helper = g_slice_new0 (SearchHelper);
662 helper->folder_count = 0;
663 helper->folder_total = 0;
664 helper->search = search;
665 helper->callback = callback;
666 helper->user_data = user_data;
667 helper->msg_hits = NULL;
673 modest_search_free (ModestSearch *search)
676 g_free (search->folder);
678 g_free (search->subject);
680 g_free (search->from);
681 if (search->recipient)
682 g_free (search->recipient);
684 g_free (search->body);
686 #ifdef MODEST_HAVE_OGS
688 g_free (search->query);
689 if (search->text_searcher)
690 ogs_text_searcher_free (search->text_searcher);
695 check_search_finished (SearchHelper *helper)
697 /* If there are no more folders to check the account search has finished */
698 if (helper->folder_count == helper->folder_total) {
700 helper->callback (helper->msg_hits, helper->user_data);
703 g_list_free (helper->msg_hits);
704 g_slice_free (SearchHelper, helper);