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>
47 #include <libmodest-dbus-client/libmodest-dbus-client.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-search.h"
54 #include "modest-runtime.h"
57 g_strdup_or_null (const gchar *str)
62 string = g_strdup (str);
76 #define NUMBER_OF_TRIES = 10; /* Try approx every second, ten times. */
79 on_timeout_check_account_is_online(gpointer user_data)
81 printf ("DEBUG: %s:\n", __FUNCTION__);
82 UtilIdleData *data = (UtilIdleData*)user_data;
84 gboolean stop_trying = FALSE;
85 if (data && data->account &&
86 (tny_account_get_connection_status (data->account) == TNY_CONNECTION_STATUS_CONNECTED) )
88 data->is_online = TRUE;
93 /* Give up if we have tried too many times: */
94 if (data->count_tries >= NUMBER_OF_TRIES)
99 /* Wait for another timeout: */
100 ++(data->count_tries);
105 /* Allow the function that requested this idle callback to continue: */
107 g_main_loop_quit (data->loop);
109 return FALSE; /* Don't call this again. */
111 return TRUE; /* Call this timeout callback again. */
115 /* Return TRUE immediately if the account is already online,
116 * otherwise check every second for NUMBER_OF_TRIES seconds and return TRUE as
117 * soon as the account is online, or FALSE if the account does
118 * not become online in the NUMBER_OF_TRIES seconds.
119 * This is useful when the D-Bus method was run immediately after
120 * the application was started (when using D-Bus activation),
121 * because the account usually takes a short time to go online.
124 check_and_wait_for_account_is_online(TnyAccount *account)
126 if (tny_account_get_connection_status (account) == TNY_CONNECTION_STATUS_CONNECTED)
129 /* This blocks on the result: */
130 UtilIdleData *data = g_slice_new0 (UtilIdleData);
131 data->is_online = FALSE;
133 GMainContext *context = NULL; /* g_main_context_new (); */
134 data->loop = g_main_loop_new (context, FALSE /* not running */);
136 g_timeout_add (1000, &on_timeout_check_account_is_online, data);
138 /* This main loop will run until the idle handler has stopped it: */
139 g_main_loop_run (data->loop);
141 g_main_loop_unref (data->loop);
142 /* g_main_context_unref (context); */
144 g_slice_free (UtilIdleData, data);
146 return data->is_online;
150 add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
152 ModestSearchHit *hit;
153 TnyHeaderFlags flags;
160 hit = g_slice_new0 (ModestSearchHit);
162 furl = tny_folder_get_url_string (folder);
163 printf ("DEBUG: %s: folder URL=%s\n", __FUNCTION__, furl);
165 g_warning ("%s: tny_folder_get_url_string(): returned NULL for folder. Folder name=%s\n", __FUNCTION__, tny_folder_get_name (folder));
168 /* Make sure that we use the short UID instead of the long UID,
169 * and/or find out what UID form is used when finding, in camel_data_cache_get().
170 * so we can find what we get. Philip is working on this.
172 uid = tny_header_get_uid (header);
174 g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, tny_header_get_subject (header));
177 msg_url = g_strdup_printf ("%s/%s", furl, uid);
180 subject = tny_header_get_subject (header);
181 sender = tny_header_get_from (header);
183 flags = tny_header_get_flags (header);
185 hit->msgid = msg_url;
186 hit->subject = g_strdup_or_null (subject);
187 hit->sender = g_strdup_or_null (sender);
188 hit->folder = g_strdup_or_null (tny_folder_get_name (folder));
189 hit->msize = tny_header_get_message_size (header);
190 hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
191 hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
192 hit->timestamp = tny_header_get_date_received (header);
194 return g_list_prepend (list, hit);
197 /** Call this until it returns FALSE or nread is set to 0.
199 * @result: FALSE is something failed. */
201 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
206 while (_nread < count) {
207 res = tny_stream_read (stream,
210 if (res == -1) { /* error */
217 if (res == 0) { /* no more bytes read. */
227 #ifdef MODEST_HAVE_OGS
229 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
231 TnyStream *stream = NULL;
233 const gsize len = sizeof (buffer);
235 gboolean is_text_html = FALSE;
236 gboolean found = FALSE;
237 gboolean res = FALSE;
239 gboolean is_text = tny_mime_part_content_type_is (part, "text/*");
241 g_debug ("%s: tny_mime_part_content_type_is() failed to find a "
242 "text/* MIME part. Content type is %s",
243 __FUNCTION__, "Unknown (calling tny_mime_part_get_content_type(part) causes a deadlock)");
245 /* Retry with specific MIME types, because the wildcard seems to fail
247 * Actually I'm not sure anymore that it fails, so we could probalby
248 * remove this later: murrayc */
250 tny_mime_part_content_type_is (part, "text/plain") ||
251 tny_mime_part_content_type_is (part, "text/html") );
254 g_debug ("%s: Retryting with text/plain or text/html succeeded",
263 is_text_html = tny_mime_part_content_type_is (part, "text/html");
265 stream = tny_mime_part_get_stream (part);
267 res = read_chunk (stream, buffer, len, &nread);
268 while (res && (nread > 0)) {
269 /* search->text_searcher was instantiated in modest_search_folder(). */
273 found = ogs_text_searcher_search_html (search->text_searcher,
278 found = ogs_text_searcher_search_text (search->text_searcher,
288 res = read_chunk (stream, buffer, len, &nread);
292 found = ogs_text_searcher_search_done (search->text_searcher);
295 ogs_text_searcher_reset (search->text_searcher);
300 printf ("DEBUG: %s: query %s was not found in message text: %s\n",
301 __FUNCTION__, search->query, buffer);
304 printf ("DEBUG: %s: found.\n", __FUNCTION__);
314 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
324 if (! tny_mime_part_content_type_is (part, "text/*")) {
325 g_debug ("%s: No text MIME part found.\n", __FUNCTION__);
330 len = (sizeof (buffer) - 1) / 2;
332 if (strlen (search->body) > len) {
333 g_warning ("Search term bigger then chunk."
334 "We might not find everything");
337 stream = tny_mime_part_get_stream (part);
339 memset (buffer, 0, sizeof (buffer));
341 chunk[1] = buffer + len;
343 res = read_chunk (stream, chunk[0], len, &nread);
349 found = !modest_text_utils_utf8_strcmp (search->body,
356 /* This works like this:
357 * buffer: [ooooooooooo|xxxxxxxxxxxx|\0]
358 * ^chunk[0] ^chunk[1]
359 * we have prefilled chunk[0] now we always read into chunk[1]
360 * and then move the content of chunk[1] to chunk[0].
361 * The idea is to prevent not finding search terms that are
362 * spread across 2 reads:
363 * buffer: [ooooooooTES|Txxxxxxxxxxx|\0]
364 * We should catch that because we always search the whole
365 * buffer not only the chunks.
367 * Of course that breaks for search terms > sizeof (chunk)
368 * but sizeof (chunk) should be big enough I guess (see
369 * the g_warning in this function)
371 while ((res = read_chunk (stream, chunk[1], len, &nread))) {
372 buffer[len + nread] = '\0';
374 found = !modest_text_utils_utf8_strcmp (search->body,
382 /* also move the \0 */
383 g_memmove (chunk[0], chunk[1], len + 1);
387 g_object_unref (stream);
390 #endif /*MODEST_HAVE_OGS*/
393 search_string (const char *what,
395 ModestSearch *search)
398 #ifdef MODEST_HAVE_OGS
399 if (search->flags & MODEST_SEARCH_USE_OGS) {
400 found = ogs_text_searcher_search_text (search->text_searcher,
404 ogs_text_searcher_reset (search->text_searcher);
407 if (what == NULL || where == NULL) {
411 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
412 #ifdef MODEST_HAVE_OGS
419 static gboolean search_mime_part_and_child_parts (TnyMimePart *part, ModestSearch *search)
421 gboolean found = FALSE;
422 #ifdef MODEST_HAVE_OGS
423 found = search_mime_part_ogs (part, search);
425 found = search_mime_part_strcmp (part, search);
432 /* Check the child part too, recursively: */
433 TnyList *child_parts = tny_simple_list_new ();
434 tny_mime_part_get_parts (TNY_MIME_PART (part), child_parts);
436 TnyIterator *piter = tny_list_create_iterator (child_parts);
437 while (!found && !tny_iterator_is_done (piter)) {
438 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
440 found = search_mime_part_and_child_parts (pcur, search);
442 g_object_unref (pcur);
445 tny_iterator_next (piter);
448 g_object_unref (piter);
449 g_object_unref (child_parts);
456 * @folder: a #TnyFolder instance
457 * @search: a #ModestSearch query
459 * This operation will search @folder for headers that match the query @search,
460 * if the folder itself matches the query.
461 * It will return a doubly linked list with URIs that point to the message.
464 modest_search_folder (TnyFolder *folder, ModestSearch *search)
466 /* Check that we should be searching this folder. */
467 /* Note that we don't try to search sub-folders.
468 * Maybe we should, but that should be specified. */
469 if (search->folder && strlen (search->folder) && (strcmp (tny_folder_get_id (folder), search->folder) != 0))
472 GList *retval = NULL;
473 TnyIterator *iter = NULL;
474 TnyList *list = NULL;
476 #ifdef MODEST_HAVE_OGS
477 if (search->flags & MODEST_SEARCH_USE_OGS) {
479 if (search->text_searcher == NULL && search->query != NULL) {
480 OgsTextSearcher *text_searcher;
482 text_searcher = ogs_text_searcher_new (FALSE);
483 ogs_text_searcher_parse_query (text_searcher, search->query);
484 search->text_searcher = text_searcher;
489 list = tny_simple_list_new ();
490 GError *error = NULL;
491 tny_folder_get_headers (folder, list, FALSE /* don't refresh */, &error);
493 g_warning ("%s: tny_folder_get_headers() failed with error=%s.\n",
494 __FUNCTION__, error->message);
495 g_error_free (error);
499 iter = tny_list_create_iterator (list);
501 while (!tny_iterator_is_done (iter)) {
502 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
503 const time_t t = tny_header_get_date_sent (cur);
504 gboolean found = FALSE;
506 /* Ignore deleted (not yet expunged) emails: */
507 if (tny_header_get_flags(cur) & TNY_HEADER_FLAG_DELETED)
510 if (search->flags & MODEST_SEARCH_BEFORE)
511 if (!(t <= search->end_date))
514 if (search->flags & MODEST_SEARCH_AFTER)
515 if (!(t >= search->start_date))
518 if (search->flags & MODEST_SEARCH_SIZE)
519 if (tny_header_get_message_size (cur) < search->minsize)
522 if (search->flags & MODEST_SEARCH_SUBJECT) {
523 const char *str = tny_header_get_subject (cur);
525 if ((found = search_string (search->subject, str, search))) {
526 retval = add_hit (retval, cur, folder);
530 if (!found && search->flags & MODEST_SEARCH_SENDER) {
531 char *str = g_strdup (tny_header_get_from (cur));
533 if ((found = search_string (search->from, (const gchar *) str, search))) {
534 retval = add_hit (retval, cur, folder);
539 if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
540 const char *str = tny_header_get_to (cur);
542 if ((found = search_string (search->recipient, str, search))) {
543 retval = add_hit (retval, cur, folder);
547 if (!found && search->flags & MODEST_SEARCH_BODY) {
548 TnyHeaderFlags flags;
552 flags = tny_header_get_flags (cur);
554 if (!(flags & TNY_HEADER_FLAG_CACHED)) {
558 msg = tny_folder_get_msg (folder, cur, &err);
560 if (err != NULL || msg == NULL) {
561 g_warning ("%s: Could not get message.\n", __FUNCTION__);
565 g_object_unref (msg);
569 found = search_mime_part_and_child_parts (TNY_MIME_PART (msg),
572 retval = add_hit (retval, cur, folder);
577 g_object_unref (msg);
581 g_object_unref (cur);
582 tny_iterator_next (iter);
585 g_object_unref (iter);
586 g_object_unref (list);
591 modest_search_account (TnyAccount *account, ModestSearch *search)
593 TnyFolderStore *store;
602 store = TNY_FOLDER_STORE (account);
604 folders = tny_simple_list_new ();
605 tny_folder_store_get_folders (store, folders, NULL, &error);
608 g_object_unref (folders);
612 iter = tny_list_create_iterator (folders);
613 while (!tny_iterator_is_done (iter)) {
614 TnyFolder *folder = NULL;
617 folder = TNY_FOLDER (tny_iterator_get_current (iter));
619 /* g_debug ("DEBUG: %s: searching folder %s.",
620 __FUNCTION__, tny_folder_get_name (folder)); */
622 res = modest_search_folder (folder, search);
628 hits = g_list_concat (hits, res);
632 g_object_unref (folder);
635 tny_iterator_next (iter);
638 g_object_unref (iter);
639 g_object_unref (folders);
641 /* printf ("DEBUG: %s: hits length = %d\n", __FUNCTION__, g_list_length (hits)); */
646 modest_search_all_accounts (ModestSearch *search)
648 /* printf ("DEBUG: %s: query=%s\n", __FUNCTION__, search->query); */
649 ModestTnyAccountStore *astore;
655 astore = modest_runtime_get_account_store ();
657 accounts = tny_simple_list_new ();
658 tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
660 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
662 iter = tny_list_create_iterator (accounts);
663 while (!tny_iterator_is_done (iter)) {
664 TnyAccount *account = NULL;
667 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
669 /* g_debug ("DEBUG: %s: Searching account %s",
670 __FUNCTION__, tny_account_get_name (account)); */
672 /* Give the account time to go online if necessary,
673 * for instance if this is immediately after startup,
674 * after D-Bus activation: */
675 if (check_and_wait_for_account_is_online (account)) {
677 res = modest_search_account (account, search);
683 hits = g_list_concat (hits, res);
688 g_object_unref (account);
691 tny_iterator_next (iter);
694 g_object_unref (accounts);
695 g_object_unref (iter);
697 /* printf ("DEBUG: %s: end: hits length=%d\n", __FUNCTION__, g_list_length(hits)); */