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_dup_uid (header);
112 gchar *subject = tny_header_dup_subject (header);
113 g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, subject);
117 msg_url = g_strdup_printf ("%s/%s", furl, uid);
121 subject = tny_header_dup_subject (header);
122 sender = tny_header_dup_from (header);
124 flags = tny_header_get_flags (header);
126 hit->msgid = msg_url;
127 hit->subject = subject;
128 hit->sender = sender;
129 hit->folder = g_strdup_or_null (tny_folder_get_name (folder));
130 hit->msize = tny_header_get_message_size (header);
131 hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
132 hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
133 hit->timestamp = MIN (tny_header_get_date_received (header), tny_header_get_date_sent (header));
135 return g_list_prepend (list, hit);
138 /** Call this until it returns FALSE or nread is set to 0.
140 * @result: FALSE is something failed. */
142 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
147 while (_nread < count) {
148 res = tny_stream_read (stream,
151 if (res == -1) { /* error */
158 if (res == 0) { /* no more bytes read. */
168 #ifdef MODEST_HAVE_OGS
170 * This function assumes that the mime part is of type "text / *"
173 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
175 TnyStream *stream = NULL;
177 const gsize len = sizeof (buffer);
179 gboolean is_text_html = FALSE;
180 gboolean found = FALSE;
181 gboolean res = FALSE;
183 is_text_html = tny_mime_part_content_type_is (part, "text/html");
185 stream = tny_mime_part_get_stream (part);
187 res = read_chunk (stream, buffer, len, &nread);
188 while (res && (nread > 0)) {
189 /* search->text_searcher was instantiated in modest_search_folder(). */
192 found = ogs_text_searcher_search_html (search->text_searcher,
197 found = ogs_text_searcher_search_text (search->text_searcher,
202 /* HACK: this helps UI refreshes because the search
203 operations could be heavy */
204 while (gtk_events_pending ())
205 gtk_main_iteration ();
212 res = read_chunk (stream, buffer, len, &nread);
214 g_object_unref (stream);
217 found = ogs_text_searcher_search_done (search->text_searcher);
220 ogs_text_searcher_reset (search->text_searcher);
228 * This function assumes that the mime part is of type "text / *"
231 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
242 len = (sizeof (buffer) - 1) / 2;
244 if (strlen (search->body) > len) {
245 g_warning ("Search term bigger then chunk."
246 "We might not find everything");
249 stream = tny_mime_part_get_stream (part);
251 memset (buffer, 0, sizeof (buffer));
253 chunk[1] = buffer + len;
255 res = read_chunk (stream, chunk[0], len, &nread);
261 found = !modest_text_utils_utf8_strcmp (search->body,
268 /* This works like this:
269 * buffer: [ooooooooooo|xxxxxxxxxxxx|\0]
270 * ^chunk[0] ^chunk[1]
271 * we have prefilled chunk[0] now we always read into chunk[1]
272 * and then move the content of chunk[1] to chunk[0].
273 * The idea is to prevent not finding search terms that are
274 * spread across 2 reads:
275 * buffer: [ooooooooTES|Txxxxxxxxxxx|\0]
276 * We should catch that because we always search the whole
277 * buffer not only the chunks.
279 * Of course that breaks for search terms > sizeof (chunk)
280 * but sizeof (chunk) should be big enough I guess (see
281 * the g_warning in this function)
283 while ((res = read_chunk (stream, chunk[1], len, &nread))) {
284 buffer[len + nread] = '\0';
286 found = !modest_text_utils_utf8_strcmp (search->body,
290 /* HACK: this helps UI refreshes because the search
291 operations could be heavy */
292 while (gtk_events_pending ())
293 gtk_main_iteration ();
295 if ((found)||(nread == 0)) {
299 /* also move the \0 */
300 g_memmove (chunk[0], chunk[1], len + 1);
304 g_object_unref (stream);
307 #endif /*MODEST_HAVE_OGS*/
310 search_string (const char *what,
312 ModestSearch *search)
314 gboolean found = FALSE;
315 #ifdef MODEST_HAVE_OGS
316 if (search->flags & MODEST_SEARCH_USE_OGS) {
317 found = ogs_text_searcher_search_text (search->text_searcher,
321 ogs_text_searcher_reset (search->text_searcher);
324 if (what == NULL || where == NULL) {
328 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
329 #ifdef MODEST_HAVE_OGS
333 /* HACK: this helps UI refreshes because the search
334 operations could be heavy */
335 while (gtk_events_pending ())
336 gtk_main_iteration ();
343 search_mime_part_and_child_parts (TnyMimePart *part, ModestSearch *search)
345 gboolean found = FALSE;
347 /* Do not search into attachments */
348 if (modest_tny_mime_part_is_attachment_for_modest (part))
351 #ifdef MODEST_HAVE_OGS
352 found = search_mime_part_ogs (part, search);
354 found = search_mime_part_strcmp (part, search);
361 /* Check the child part too, recursively: */
362 TnyList *child_parts = tny_simple_list_new ();
363 tny_mime_part_get_parts (TNY_MIME_PART (part), child_parts);
365 TnyIterator *piter = tny_list_create_iterator (child_parts);
366 while (!found && !tny_iterator_is_done (piter)) {
367 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
369 found = search_mime_part_and_child_parts (pcur, search);
371 g_object_unref (pcur);
374 tny_iterator_next (piter);
377 g_object_unref (piter);
378 g_object_unref (child_parts);
384 modest_search_folder_get_headers_cb (TnyFolder *folder,
390 TnyIterator *iter = NULL;
391 SearchHelper *helper;
393 helper = (SearchHelper *) user_data;
395 if (err || cancelled) {
399 iter = tny_list_create_iterator (headers);
401 while (!tny_iterator_is_done (iter)) {
403 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
404 const time_t t = tny_header_get_date_sent (cur);
405 gboolean found = FALSE;
407 /* Ignore deleted (not yet expunged) emails: */
408 if (tny_header_get_flags(cur) & TNY_HEADER_FLAG_DELETED)
411 if (helper->search->flags & MODEST_SEARCH_BEFORE)
412 if (!(t <= helper->search->end_date))
415 if (helper->search->flags & MODEST_SEARCH_AFTER)
416 if (!(t >= helper->search->start_date))
419 if (helper->search->flags & MODEST_SEARCH_SIZE)
420 if (tny_header_get_message_size (cur) < helper->search->minsize)
423 if (helper->search->flags & MODEST_SEARCH_SUBJECT) {
424 char *str = tny_header_dup_subject (cur);
426 if ((found = search_string (helper->search->subject, str, helper->search))) {
427 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
432 if (!found && helper->search->flags & MODEST_SEARCH_SENDER) {
433 char *str = tny_header_dup_from (cur);
435 if ((found = search_string (helper->search->from, (const gchar *) str, helper->search))) {
436 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
441 if (!found && helper->search->flags & MODEST_SEARCH_RECIPIENT) {
442 char *str = tny_header_dup_to (cur);
444 if ((found = search_string (helper->search->recipient, str, helper->search))) {
445 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
450 if (!found && helper->search->flags & MODEST_SEARCH_BODY) {
451 TnyHeaderFlags flags;
455 flags = tny_header_get_flags (cur);
457 if (!(flags & TNY_HEADER_FLAG_CACHED)) {
461 msg = tny_folder_get_msg (folder, cur, &err);
463 if (err != NULL || msg == NULL) {
464 g_warning ("%s: Could not get message.\n", __FUNCTION__);
468 g_object_unref (msg);
472 str = tny_header_dup_subject (cur);
473 g_debug ("Searching in %s\n", str);
476 found = search_mime_part_and_child_parts (TNY_MIME_PART (msg),
479 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
484 g_object_unref (msg);
487 g_object_unref (cur);
488 tny_iterator_next (iter);
492 g_object_unref (iter);
495 g_object_unref (headers);
497 /* Check search finished */
498 helper->folder_count++;
499 check_search_finished (helper);
503 _search_folder (TnyFolder *folder,
504 SearchHelper *helper)
506 TnyList *list = NULL;
508 g_debug ("%s: searching folder %s.", __FUNCTION__, tny_folder_get_name (folder));
510 /* Check that we should be searching this folder. */
511 /* Note that we don't try to search sub-folders.
512 * Maybe we should, but that should be specified. */
513 if (helper->search->folder && strlen (helper->search->folder)) {
514 if (!strcmp (helper->search->folder, "outbox")) {
515 if (modest_tny_folder_guess_folder_type (folder) != TNY_FOLDER_TYPE_OUTBOX) {
516 modest_search_folder_get_headers_cb (folder, TRUE, NULL, NULL, helper);
519 } else if (strcmp (tny_folder_get_id (folder), helper->search->folder) != 0) {
520 modest_search_folder_get_headers_cb (folder, TRUE, NULL, NULL, helper);
525 #ifdef MODEST_HAVE_OGS
526 if (helper->search->flags & MODEST_SEARCH_USE_OGS) {
528 if (helper->search->text_searcher == NULL && helper->search->query != NULL) {
529 OgsTextSearcher *text_searcher;
531 text_searcher = ogs_text_searcher_new (FALSE);
532 ogs_text_searcher_parse_query (text_searcher, helper->search->query);
533 helper->search->text_searcher = text_searcher;
537 list = tny_simple_list_new ();
538 /* Get the headers */
539 tny_folder_get_headers_async (folder, list, FALSE,
540 modest_search_folder_get_headers_cb,
545 modest_search_folder (TnyFolder *folder,
546 ModestSearch *search,
547 ModestSearchCallback callback,
550 SearchHelper *helper;
552 /* Create the helper */
553 helper = create_helper (callback, search, user_data);
556 _search_folder (folder, helper);
560 modest_search_account_get_folders_cb (TnyFolderStore *self,
567 SearchHelper *helper;
569 helper = (SearchHelper *) user_data;
571 if (err || cancelled) {
575 iter = tny_list_create_iterator (folders);
576 while (!tny_iterator_is_done (iter)) {
577 TnyFolder *folder = NULL;
579 /* Search into folder */
580 folder = TNY_FOLDER (tny_iterator_get_current (iter));
581 helper->folder_total++;
582 _search_folder (folder, (SearchHelper *) user_data);
583 g_object_unref (folder);
585 tny_iterator_next (iter);
587 g_object_unref (iter);
590 g_object_unref (folders);
592 /* Check search finished */
593 check_search_finished (helper);
597 _search_account (TnyAccount *account,
598 SearchHelper *helper)
600 TnyList *folders = tny_simple_list_new ();
602 g_debug ("%s: Searching account %s", __FUNCTION__, tny_account_get_name (account));
605 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (account), folders, NULL,
606 modest_search_account_get_folders_cb,
611 modest_search_account (TnyAccount *account,
612 ModestSearch *search,
613 ModestSearchCallback callback,
616 SearchHelper *helper;
618 /* Create the helper */
619 helper = create_helper (callback, search, user_data);
622 _search_account (account, helper);
626 modest_search_all_accounts (ModestSearch *search,
627 ModestSearchCallback callback,
630 ModestTnyAccountStore *astore;
634 SearchHelper *helper;
637 astore = modest_runtime_get_account_store ();
639 accounts = tny_simple_list_new ();
640 tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
642 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
644 /* Create the helper */
645 helper = create_helper (callback, search, user_data);
647 /* Search through all accounts */
648 iter = tny_list_create_iterator (accounts);
649 while (!tny_iterator_is_done (iter)) {
650 TnyAccount *account = NULL;
652 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
653 _search_account (account, helper);
654 g_object_unref (account);
656 tny_iterator_next (iter);
658 g_object_unref (iter);
659 g_object_unref (accounts);
662 static SearchHelper *
663 create_helper (ModestSearchCallback callback,
664 ModestSearch *search,
667 SearchHelper *helper;
669 helper = g_slice_new0 (SearchHelper);
670 helper->folder_count = 0;
671 helper->folder_total = 0;
672 helper->search = search;
673 helper->callback = callback;
674 helper->user_data = user_data;
675 helper->msg_hits = NULL;
681 modest_search_free (ModestSearch *search)
684 g_free (search->folder);
686 g_free (search->subject);
688 g_free (search->from);
689 if (search->recipient)
690 g_free (search->recipient);
692 g_free (search->body);
694 #ifdef MODEST_HAVE_OGS
696 g_free (search->query);
697 if (search->text_searcher)
698 ogs_text_searcher_free (search->text_searcher);
703 check_search_finished (SearchHelper *helper)
705 /* If there are no more folders to check the account search has finished */
706 if (helper->folder_count == helper->folder_total) {
708 helper->callback (helper->msg_hits, helper->user_data);
711 g_list_free (helper->msg_hits);
712 g_slice_free (SearchHelper, helper);