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 <libmodest-dbus-client/libmodest-dbus-client.h>
51 #include "modest-text-utils.h"
52 #include "modest-account-mgr.h"
53 #include "modest-tny-account-store.h"
54 #include "modest-tny-account.h"
55 #include "modest-tny-folder.h"
56 #include "modest-search.h"
57 #include "modest-runtime.h"
58 #include "modest-platform.h"
61 g_strdup_or_null (const gchar *str)
66 string = g_strdup (str);
73 add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
83 hit = g_slice_new0 (ModestSearchHit);
85 furl = tny_folder_get_url_string (folder);
86 printf ("DEBUG: %s: folder URL=%s\n", __FUNCTION__, furl);
88 g_warning ("%s: tny_folder_get_url_string(): returned NULL for folder. Folder name=%s\n", __FUNCTION__, tny_folder_get_name (folder));
91 /* Make sure that we use the short UID instead of the long UID,
92 * and/or find out what UID form is used when finding, in camel_data_cache_get().
93 * so we can find what we get. Philip is working on this.
95 uid = tny_header_get_uid (header);
97 g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, tny_header_get_subject (header));
100 msg_url = g_strdup_printf ("%s/%s", furl, uid);
103 subject = tny_header_get_subject (header);
104 sender = tny_header_get_from (header);
106 flags = tny_header_get_flags (header);
108 hit->msgid = msg_url;
109 hit->subject = g_strdup_or_null (subject);
110 hit->sender = g_strdup_or_null (sender);
111 hit->folder = g_strdup_or_null (tny_folder_get_name (folder));
112 hit->msize = tny_header_get_message_size (header);
113 hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
114 hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
115 hit->timestamp = MIN (tny_header_get_date_received (header), tny_header_get_date_sent (header));
117 return g_list_prepend (list, hit);
120 /** Call this until it returns FALSE or nread is set to 0.
122 * @result: FALSE is something failed. */
124 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
129 while (_nread < count) {
130 res = tny_stream_read (stream,
133 if (res == -1) { /* error */
140 if (res == 0) { /* no more bytes read. */
150 #ifdef MODEST_HAVE_OGS
152 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
154 TnyStream *stream = NULL;
156 const gsize len = sizeof (buffer);
158 gboolean is_text_html = FALSE;
159 gboolean found = FALSE;
160 gboolean res = FALSE;
162 gboolean is_text = tny_mime_part_content_type_is (part, "text/*");
164 g_debug ("%s: tny_mime_part_content_type_is() failed to find a "
165 "text/* MIME part. Content type is %s",
166 __FUNCTION__, "Unknown (calling tny_mime_part_get_content_type(part) causes a deadlock)");
168 /* Retry with specific MIME types, because the wildcard seems to fail
170 * Actually I'm not sure anymore that it fails, so we could probalby
171 * remove this later: murrayc */
173 tny_mime_part_content_type_is (part, "text/plain") ||
174 tny_mime_part_content_type_is (part, "text/html") );
177 g_debug ("%s: Retryting with text/plain or text/html succeeded",
186 is_text_html = tny_mime_part_content_type_is (part, "text/html");
188 stream = tny_mime_part_get_stream (part);
190 res = read_chunk (stream, buffer, len, &nread);
191 while (res && (nread > 0)) {
192 /* search->text_searcher was instantiated in modest_search_folder(). */
196 found = ogs_text_searcher_search_html (search->text_searcher,
201 found = ogs_text_searcher_search_text (search->text_searcher,
211 res = read_chunk (stream, buffer, len, &nread);
215 found = ogs_text_searcher_search_done (search->text_searcher);
218 ogs_text_searcher_reset (search->text_searcher);
223 printf ("DEBUG: %s: query %s was not found in message text: %s\n",
224 __FUNCTION__, search->query, buffer);
227 printf ("DEBUG: %s: found.\n", __FUNCTION__);
237 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
247 if (! tny_mime_part_content_type_is (part, "text/*")) {
248 g_debug ("%s: No text MIME part found.\n", __FUNCTION__);
253 len = (sizeof (buffer) - 1) / 2;
255 if (strlen (search->body) > len) {
256 g_warning ("Search term bigger then chunk."
257 "We might not find everything");
260 stream = tny_mime_part_get_stream (part);
262 memset (buffer, 0, sizeof (buffer));
264 chunk[1] = buffer + len;
266 res = read_chunk (stream, chunk[0], len, &nread);
272 found = !modest_text_utils_utf8_strcmp (search->body,
279 /* This works like this:
280 * buffer: [ooooooooooo|xxxxxxxxxxxx|\0]
281 * ^chunk[0] ^chunk[1]
282 * we have prefilled chunk[0] now we always read into chunk[1]
283 * and then move the content of chunk[1] to chunk[0].
284 * The idea is to prevent not finding search terms that are
285 * spread across 2 reads:
286 * buffer: [ooooooooTES|Txxxxxxxxxxx|\0]
287 * We should catch that because we always search the whole
288 * buffer not only the chunks.
290 * Of course that breaks for search terms > sizeof (chunk)
291 * but sizeof (chunk) should be big enough I guess (see
292 * the g_warning in this function)
294 while ((res = read_chunk (stream, chunk[1], len, &nread))) {
295 buffer[len + nread] = '\0';
297 found = !modest_text_utils_utf8_strcmp (search->body,
301 if ((found)||(nread == 0)) {
305 /* also move the \0 */
306 g_memmove (chunk[0], chunk[1], len + 1);
310 g_object_unref (stream);
313 #endif /*MODEST_HAVE_OGS*/
316 search_string (const char *what,
318 ModestSearch *search)
321 #ifdef MODEST_HAVE_OGS
322 if (search->flags & MODEST_SEARCH_USE_OGS) {
323 found = ogs_text_searcher_search_text (search->text_searcher,
327 ogs_text_searcher_reset (search->text_searcher);
330 if (what == NULL || where == NULL) {
334 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
335 #ifdef MODEST_HAVE_OGS
342 static gboolean search_mime_part_and_child_parts (TnyMimePart *part, ModestSearch *search)
344 gboolean found = FALSE;
345 #ifdef MODEST_HAVE_OGS
346 found = search_mime_part_ogs (part, search);
348 found = search_mime_part_strcmp (part, search);
355 /* Check the child part too, recursively: */
356 TnyList *child_parts = tny_simple_list_new ();
357 tny_mime_part_get_parts (TNY_MIME_PART (part), child_parts);
359 TnyIterator *piter = tny_list_create_iterator (child_parts);
360 while (!found && !tny_iterator_is_done (piter)) {
361 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
363 found = search_mime_part_and_child_parts (pcur, search);
365 g_object_unref (pcur);
368 tny_iterator_next (piter);
371 g_object_unref (piter);
372 g_object_unref (child_parts);
379 * @folder: a #TnyFolder instance
380 * @search: a #ModestSearch query
382 * This operation will search @folder for headers that match the query @search,
383 * if the folder itself matches the query.
384 * It will return a doubly linked list with URIs that point to the message.
387 modest_search_folder (TnyFolder *folder, ModestSearch *search)
389 /* Check that we should be searching this folder. */
390 /* Note that we don't try to search sub-folders.
391 * Maybe we should, but that should be specified. */
392 if (search->folder && strlen (search->folder)) {
393 if (!strcmp (search->folder, "outbox")) {
394 if (modest_tny_folder_guess_folder_type (folder) != TNY_FOLDER_TYPE_OUTBOX) {
397 } else if (strcmp (tny_folder_get_id (folder), search->folder) != 0) {
402 GList *retval = NULL;
403 TnyIterator *iter = NULL;
404 TnyList *list = NULL;
406 #ifdef MODEST_HAVE_OGS
407 if (search->flags & MODEST_SEARCH_USE_OGS) {
409 if (search->text_searcher == NULL && search->query != NULL) {
410 OgsTextSearcher *text_searcher;
412 text_searcher = ogs_text_searcher_new (FALSE);
413 ogs_text_searcher_parse_query (text_searcher, search->query);
414 search->text_searcher = text_searcher;
419 list = tny_simple_list_new ();
420 GError *error = NULL;
421 tny_folder_get_headers (folder, list, FALSE /* don't refresh */, &error);
423 g_warning ("%s: tny_folder_get_headers() failed with error=%s.\n",
424 __FUNCTION__, error->message);
425 g_error_free (error);
429 iter = tny_list_create_iterator (list);
431 while (!tny_iterator_is_done (iter)) {
432 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
433 const time_t t = tny_header_get_date_sent (cur);
434 gboolean found = FALSE;
436 /* Ignore deleted (not yet expunged) emails: */
437 if (tny_header_get_flags(cur) & TNY_HEADER_FLAG_DELETED)
440 if (search->flags & MODEST_SEARCH_BEFORE)
441 if (!(t <= search->end_date))
444 if (search->flags & MODEST_SEARCH_AFTER)
445 if (!(t >= search->start_date))
448 if (search->flags & MODEST_SEARCH_SIZE)
449 if (tny_header_get_message_size (cur) < search->minsize)
452 if (search->flags & MODEST_SEARCH_SUBJECT) {
453 const char *str = tny_header_get_subject (cur);
455 if ((found = search_string (search->subject, str, search))) {
456 retval = add_hit (retval, cur, folder);
460 if (!found && search->flags & MODEST_SEARCH_SENDER) {
461 char *str = g_strdup (tny_header_get_from (cur));
463 if ((found = search_string (search->from, (const gchar *) str, search))) {
464 retval = add_hit (retval, cur, folder);
469 if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
470 const char *str = tny_header_get_to (cur);
472 if ((found = search_string (search->recipient, str, search))) {
473 retval = add_hit (retval, cur, folder);
477 if (!found && search->flags & MODEST_SEARCH_BODY) {
478 TnyHeaderFlags flags;
482 flags = tny_header_get_flags (cur);
484 if (!(flags & TNY_HEADER_FLAG_CACHED)) {
488 msg = tny_folder_get_msg (folder, cur, &err);
490 if (err != NULL || msg == NULL) {
491 g_warning ("%s: Could not get message.\n", __FUNCTION__);
495 g_object_unref (msg);
499 found = search_mime_part_and_child_parts (TNY_MIME_PART (msg),
502 retval = add_hit (retval, cur, folder);
507 g_object_unref (msg);
511 g_object_unref (cur);
512 tny_iterator_next (iter);
515 g_object_unref (iter);
516 g_object_unref (list);
521 modest_search_account (TnyAccount *account, ModestSearch *search)
523 TnyFolderStore *store;
532 store = TNY_FOLDER_STORE (account);
534 folders = tny_simple_list_new ();
535 tny_folder_store_get_folders (store, folders, NULL, &error);
538 g_object_unref (folders);
542 iter = tny_list_create_iterator (folders);
543 while (!tny_iterator_is_done (iter)) {
544 TnyFolder *folder = NULL;
547 folder = TNY_FOLDER (tny_iterator_get_current (iter));
549 /* g_debug ("DEBUG: %s: searching folder %s.",
550 __FUNCTION__, tny_folder_get_name (folder)); */
552 res = modest_search_folder (folder, search);
558 hits = g_list_concat (hits, res);
562 g_object_unref (folder);
565 tny_iterator_next (iter);
568 g_object_unref (iter);
569 g_object_unref (folders);
571 /* printf ("DEBUG: %s: hits length = %d\n", __FUNCTION__, g_list_length (hits)); */
576 modest_search_all_accounts (ModestSearch *search)
578 /* printf ("DEBUG: %s: query=%s\n", __FUNCTION__, search->query); */
579 ModestTnyAccountStore *astore;
585 astore = modest_runtime_get_account_store ();
587 accounts = tny_simple_list_new ();
588 tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
590 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
592 iter = tny_list_create_iterator (accounts);
593 while (!tny_iterator_is_done (iter)) {
594 TnyAccount *account = NULL;
597 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
599 /* g_debug ("DEBUG: %s: Searching account %s",
600 __FUNCTION__, tny_account_get_name (account)); */
602 /* Give the account time to go online if necessary,
603 * for instance if this is immediately after startup,
604 * after D-Bus activation: */
605 modest_platform_check_and_wait_for_account_is_online (account);
608 res = modest_search_account (account, search);
614 hits = g_list_concat (hits, res);
618 g_object_unref (account);
621 tny_iterator_next (iter);
624 g_object_unref (accounts);
625 g_object_unref (iter);
627 /* printf ("DEBUG: %s: end: hits length=%d\n", __FUNCTION__, g_list_length(hits)); */