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-search.h"
56 #include "modest-runtime.h"
57 #include "modest-platform.h"
60 g_strdup_or_null (const gchar *str)
65 string = g_strdup (str);
72 add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
82 hit = g_slice_new0 (ModestSearchHit);
84 furl = tny_folder_get_url_string (folder);
85 printf ("DEBUG: %s: folder URL=%s\n", __FUNCTION__, furl);
87 g_warning ("%s: tny_folder_get_url_string(): returned NULL for folder. Folder name=%s\n", __FUNCTION__, tny_folder_get_name (folder));
90 /* Make sure that we use the short UID instead of the long UID,
91 * and/or find out what UID form is used when finding, in camel_data_cache_get().
92 * so we can find what we get. Philip is working on this.
94 uid = tny_header_get_uid (header);
96 g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, tny_header_get_subject (header));
99 msg_url = g_strdup_printf ("%s/%s", furl, uid);
102 subject = tny_header_get_subject (header);
103 sender = tny_header_get_from (header);
105 flags = tny_header_get_flags (header);
107 hit->msgid = msg_url;
108 hit->subject = g_strdup_or_null (subject);
109 hit->sender = g_strdup_or_null (sender);
110 hit->folder = g_strdup_or_null (tny_folder_get_name (folder));
111 hit->msize = tny_header_get_message_size (header);
112 hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
113 hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
114 hit->timestamp = tny_header_get_date_received (header);
116 return g_list_prepend (list, hit);
119 /** Call this until it returns FALSE or nread is set to 0.
121 * @result: FALSE is something failed. */
123 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
128 while (_nread < count) {
129 res = tny_stream_read (stream,
132 if (res == -1) { /* error */
139 if (res == 0) { /* no more bytes read. */
149 #ifdef MODEST_HAVE_OGS
151 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
153 TnyStream *stream = NULL;
155 const gsize len = sizeof (buffer);
157 gboolean is_text_html = FALSE;
158 gboolean found = FALSE;
159 gboolean res = FALSE;
161 gboolean is_text = tny_mime_part_content_type_is (part, "text/*");
163 g_debug ("%s: tny_mime_part_content_type_is() failed to find a "
164 "text/* MIME part. Content type is %s",
165 __FUNCTION__, "Unknown (calling tny_mime_part_get_content_type(part) causes a deadlock)");
167 /* Retry with specific MIME types, because the wildcard seems to fail
169 * Actually I'm not sure anymore that it fails, so we could probalby
170 * remove this later: murrayc */
172 tny_mime_part_content_type_is (part, "text/plain") ||
173 tny_mime_part_content_type_is (part, "text/html") );
176 g_debug ("%s: Retryting with text/plain or text/html succeeded",
185 is_text_html = tny_mime_part_content_type_is (part, "text/html");
187 stream = tny_mime_part_get_stream (part);
189 res = read_chunk (stream, buffer, len, &nread);
190 while (res && (nread > 0)) {
191 /* search->text_searcher was instantiated in modest_search_folder(). */
195 found = ogs_text_searcher_search_html (search->text_searcher,
200 found = ogs_text_searcher_search_text (search->text_searcher,
210 res = read_chunk (stream, buffer, len, &nread);
214 found = ogs_text_searcher_search_done (search->text_searcher);
217 ogs_text_searcher_reset (search->text_searcher);
222 printf ("DEBUG: %s: query %s was not found in message text: %s\n",
223 __FUNCTION__, search->query, buffer);
226 printf ("DEBUG: %s: found.\n", __FUNCTION__);
236 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
246 if (! tny_mime_part_content_type_is (part, "text/*")) {
247 g_debug ("%s: No text MIME part found.\n", __FUNCTION__);
252 len = (sizeof (buffer) - 1) / 2;
254 if (strlen (search->body) > len) {
255 g_warning ("Search term bigger then chunk."
256 "We might not find everything");
259 stream = tny_mime_part_get_stream (part);
261 memset (buffer, 0, sizeof (buffer));
263 chunk[1] = buffer + len;
265 res = read_chunk (stream, chunk[0], len, &nread);
271 found = !modest_text_utils_utf8_strcmp (search->body,
278 /* This works like this:
279 * buffer: [ooooooooooo|xxxxxxxxxxxx|\0]
280 * ^chunk[0] ^chunk[1]
281 * we have prefilled chunk[0] now we always read into chunk[1]
282 * and then move the content of chunk[1] to chunk[0].
283 * The idea is to prevent not finding search terms that are
284 * spread across 2 reads:
285 * buffer: [ooooooooTES|Txxxxxxxxxxx|\0]
286 * We should catch that because we always search the whole
287 * buffer not only the chunks.
289 * Of course that breaks for search terms > sizeof (chunk)
290 * but sizeof (chunk) should be big enough I guess (see
291 * the g_warning in this function)
293 while ((res = read_chunk (stream, chunk[1], len, &nread))) {
294 buffer[len + nread] = '\0';
296 found = !modest_text_utils_utf8_strcmp (search->body,
304 /* also move the \0 */
305 g_memmove (chunk[0], chunk[1], len + 1);
309 g_object_unref (stream);
312 #endif /*MODEST_HAVE_OGS*/
315 search_string (const char *what,
317 ModestSearch *search)
320 #ifdef MODEST_HAVE_OGS
321 if (search->flags & MODEST_SEARCH_USE_OGS) {
322 found = ogs_text_searcher_search_text (search->text_searcher,
326 ogs_text_searcher_reset (search->text_searcher);
329 if (what == NULL || where == NULL) {
333 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
334 #ifdef MODEST_HAVE_OGS
341 static gboolean search_mime_part_and_child_parts (TnyMimePart *part, ModestSearch *search)
343 gboolean found = FALSE;
344 #ifdef MODEST_HAVE_OGS
345 found = search_mime_part_ogs (part, search);
347 found = search_mime_part_strcmp (part, search);
354 /* Check the child part too, recursively: */
355 TnyList *child_parts = tny_simple_list_new ();
356 tny_mime_part_get_parts (TNY_MIME_PART (part), child_parts);
358 TnyIterator *piter = tny_list_create_iterator (child_parts);
359 while (!found && !tny_iterator_is_done (piter)) {
360 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
362 found = search_mime_part_and_child_parts (pcur, search);
364 g_object_unref (pcur);
367 tny_iterator_next (piter);
370 g_object_unref (piter);
371 g_object_unref (child_parts);
378 * @folder: a #TnyFolder instance
379 * @search: a #ModestSearch query
381 * This operation will search @folder for headers that match the query @search,
382 * if the folder itself matches the query.
383 * It will return a doubly linked list with URIs that point to the message.
386 modest_search_folder (TnyFolder *folder, ModestSearch *search)
388 /* Check that we should be searching this folder. */
389 /* Note that we don't try to search sub-folders.
390 * Maybe we should, but that should be specified. */
391 if (search->folder && strlen (search->folder) && (strcmp (tny_folder_get_id (folder), search->folder) != 0))
394 GList *retval = NULL;
395 TnyIterator *iter = NULL;
396 TnyList *list = NULL;
398 #ifdef MODEST_HAVE_OGS
399 if (search->flags & MODEST_SEARCH_USE_OGS) {
401 if (search->text_searcher == NULL && search->query != NULL) {
402 OgsTextSearcher *text_searcher;
404 text_searcher = ogs_text_searcher_new (FALSE);
405 ogs_text_searcher_parse_query (text_searcher, search->query);
406 search->text_searcher = text_searcher;
411 list = tny_simple_list_new ();
412 GError *error = NULL;
413 tny_folder_get_headers (folder, list, FALSE /* don't refresh */, &error);
415 g_warning ("%s: tny_folder_get_headers() failed with error=%s.\n",
416 __FUNCTION__, error->message);
417 g_error_free (error);
421 iter = tny_list_create_iterator (list);
423 while (!tny_iterator_is_done (iter)) {
424 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
425 const time_t t = tny_header_get_date_sent (cur);
426 gboolean found = FALSE;
428 /* Ignore deleted (not yet expunged) emails: */
429 if (tny_header_get_flags(cur) & TNY_HEADER_FLAG_DELETED)
432 if (search->flags & MODEST_SEARCH_BEFORE)
433 if (!(t <= search->end_date))
436 if (search->flags & MODEST_SEARCH_AFTER)
437 if (!(t >= search->start_date))
440 if (search->flags & MODEST_SEARCH_SIZE)
441 if (tny_header_get_message_size (cur) < search->minsize)
444 if (search->flags & MODEST_SEARCH_SUBJECT) {
445 const char *str = tny_header_get_subject (cur);
447 if ((found = search_string (search->subject, str, search))) {
448 retval = add_hit (retval, cur, folder);
452 if (!found && search->flags & MODEST_SEARCH_SENDER) {
453 char *str = g_strdup (tny_header_get_from (cur));
455 if ((found = search_string (search->from, (const gchar *) str, search))) {
456 retval = add_hit (retval, cur, folder);
461 if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
462 const char *str = tny_header_get_to (cur);
464 if ((found = search_string (search->recipient, str, search))) {
465 retval = add_hit (retval, cur, folder);
469 if (!found && search->flags & MODEST_SEARCH_BODY) {
470 TnyHeaderFlags flags;
474 flags = tny_header_get_flags (cur);
476 if (!(flags & TNY_HEADER_FLAG_CACHED)) {
480 msg = tny_folder_get_msg (folder, cur, &err);
482 if (err != NULL || msg == NULL) {
483 g_warning ("%s: Could not get message.\n", __FUNCTION__);
487 g_object_unref (msg);
491 found = search_mime_part_and_child_parts (TNY_MIME_PART (msg),
494 retval = add_hit (retval, cur, folder);
499 g_object_unref (msg);
503 g_object_unref (cur);
504 tny_iterator_next (iter);
507 g_object_unref (iter);
508 g_object_unref (list);
513 modest_search_account (TnyAccount *account, ModestSearch *search)
515 TnyFolderStore *store;
524 store = TNY_FOLDER_STORE (account);
526 folders = tny_simple_list_new ();
527 tny_folder_store_get_folders (store, folders, NULL, &error);
530 g_object_unref (folders);
534 iter = tny_list_create_iterator (folders);
535 while (!tny_iterator_is_done (iter)) {
536 TnyFolder *folder = NULL;
539 folder = TNY_FOLDER (tny_iterator_get_current (iter));
541 /* g_debug ("DEBUG: %s: searching folder %s.",
542 __FUNCTION__, tny_folder_get_name (folder)); */
544 res = modest_search_folder (folder, search);
550 hits = g_list_concat (hits, res);
554 g_object_unref (folder);
557 tny_iterator_next (iter);
560 g_object_unref (iter);
561 g_object_unref (folders);
563 /* printf ("DEBUG: %s: hits length = %d\n", __FUNCTION__, g_list_length (hits)); */
568 modest_search_all_accounts (ModestSearch *search)
570 /* printf ("DEBUG: %s: query=%s\n", __FUNCTION__, search->query); */
571 ModestTnyAccountStore *astore;
577 astore = modest_runtime_get_account_store ();
579 accounts = tny_simple_list_new ();
580 tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
582 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
584 iter = tny_list_create_iterator (accounts);
585 while (!tny_iterator_is_done (iter)) {
586 TnyAccount *account = NULL;
589 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
591 /* g_debug ("DEBUG: %s: Searching account %s",
592 __FUNCTION__, tny_account_get_name (account)); */
594 /* Give the account time to go online if necessary,
595 * for instance if this is immediately after startup,
596 * after D-Bus activation: */
597 modest_platform_check_and_wait_for_account_is_online (account);
600 res = modest_search_account (account, search);
606 hits = g_list_concat (hits, res);
610 g_object_unref (account);
613 tny_iterator_next (iter);
616 g_object_unref (accounts);
617 g_object_unref (iter);
619 /* printf ("DEBUG: %s: end: hits length=%d\n", __FUNCTION__, g_list_length(hits)); */