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;
85 g_warning ("%s: data is NULL.\n", __FUNCTION__);
88 if (!(data->account)) {
89 g_warning ("%s: data->account is NULL.\n", __FUNCTION__);
92 if (data && data->account) {
93 printf ("DEBUG: %s: tny_account_get_connection_status()==%d\n", __FUNCTION__, tny_account_get_connection_status (data->account));
96 gboolean stop_trying = FALSE;
97 if (data && data->account &&
98 (tny_account_get_connection_status (data->account) == TNY_CONNECTION_STATUS_CONNECTED) )
100 data->is_online = TRUE;
105 /* Give up if we have tried too many times: */
106 if (data->count_tries >= NUMBER_OF_TRIES)
111 /* Wait for another timeout: */
112 ++(data->count_tries);
117 /* Allow the function that requested this idle callback to continue: */
119 g_main_loop_quit (data->loop);
122 g_object_unref (data->account);
124 return FALSE; /* Don't call this again. */
126 return TRUE; /* Call this timeout callback again. */
130 /* Return TRUE immediately if the account is already online,
131 * otherwise check every second for NUMBER_OF_TRIES seconds and return TRUE as
132 * soon as the account is online, or FALSE if the account does
133 * not become online in the NUMBER_OF_TRIES seconds.
134 * This is useful when the D-Bus method was run immediately after
135 * the application was started (when using D-Bus activation),
136 * because the account usually takes a short time to go online.
137 * The return value is maybe not very useful.
140 check_and_wait_for_account_is_online(TnyAccount *account)
142 g_return_val_if_fail (account, FALSE);
144 printf ("DEBUG: %s: account id=%s\n", __FUNCTION__, tny_account_get_id (account));
146 if (!tny_device_is_online (modest_runtime_get_device())) {
147 printf ("DEBUG: %s: device is offline.\n", __FUNCTION__);
151 printf ("DEBUG: %s: tny_account_get_connection_status()==%d\n", __FUNCTION__, tny_account_get_connection_status (account));
153 /* TODO: The local_folders account never seems to leave TNY_CONNECTION_STATUS_INIT,
154 * so we wait unnecessarily,
155 * but that enum value isn't enough to get a message list from POP and IMAP. */
156 if (tny_account_get_connection_status (account) == TNY_CONNECTION_STATUS_CONNECTED)
159 /* This blocks on the result: */
160 UtilIdleData *data = g_slice_new0 (UtilIdleData);
161 data->is_online = FALSE;
162 data->account = account;
163 g_object_ref (data->account);
164 data->count_tries = 0;
166 GMainContext *context = NULL; /* g_main_context_new (); */
167 data->loop = g_main_loop_new (context, FALSE /* not running */);
169 g_timeout_add (1000, &on_timeout_check_account_is_online, data);
171 /* This main loop will run until the idle handler has stopped it: */
172 g_main_loop_run (data->loop);
174 g_main_loop_unref (data->loop);
175 /* g_main_context_unref (context); */
177 g_slice_free (UtilIdleData, data);
179 return data->is_online;
183 add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
185 ModestSearchHit *hit;
186 TnyHeaderFlags flags;
193 hit = g_slice_new0 (ModestSearchHit);
195 furl = tny_folder_get_url_string (folder);
196 printf ("DEBUG: %s: folder URL=%s\n", __FUNCTION__, furl);
198 g_warning ("%s: tny_folder_get_url_string(): returned NULL for folder. Folder name=%s\n", __FUNCTION__, tny_folder_get_name (folder));
201 /* Make sure that we use the short UID instead of the long UID,
202 * and/or find out what UID form is used when finding, in camel_data_cache_get().
203 * so we can find what we get. Philip is working on this.
205 uid = tny_header_get_uid (header);
207 g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, tny_header_get_subject (header));
210 msg_url = g_strdup_printf ("%s/%s", furl, uid);
213 subject = tny_header_get_subject (header);
214 sender = tny_header_get_from (header);
216 flags = tny_header_get_flags (header);
218 hit->msgid = msg_url;
219 hit->subject = g_strdup_or_null (subject);
220 hit->sender = g_strdup_or_null (sender);
221 hit->folder = g_strdup_or_null (tny_folder_get_name (folder));
222 hit->msize = tny_header_get_message_size (header);
223 hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
224 hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
225 hit->timestamp = tny_header_get_date_received (header);
227 return g_list_prepend (list, hit);
230 /** Call this until it returns FALSE or nread is set to 0.
232 * @result: FALSE is something failed. */
234 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
239 while (_nread < count) {
240 res = tny_stream_read (stream,
243 if (res == -1) { /* error */
250 if (res == 0) { /* no more bytes read. */
260 #ifdef MODEST_HAVE_OGS
262 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
264 TnyStream *stream = NULL;
266 const gsize len = sizeof (buffer);
268 gboolean is_text_html = FALSE;
269 gboolean found = FALSE;
270 gboolean res = FALSE;
272 gboolean is_text = tny_mime_part_content_type_is (part, "text/*");
274 g_debug ("%s: tny_mime_part_content_type_is() failed to find a "
275 "text/* MIME part. Content type is %s",
276 __FUNCTION__, "Unknown (calling tny_mime_part_get_content_type(part) causes a deadlock)");
278 /* Retry with specific MIME types, because the wildcard seems to fail
280 * Actually I'm not sure anymore that it fails, so we could probalby
281 * remove this later: murrayc */
283 tny_mime_part_content_type_is (part, "text/plain") ||
284 tny_mime_part_content_type_is (part, "text/html") );
287 g_debug ("%s: Retryting with text/plain or text/html succeeded",
296 is_text_html = tny_mime_part_content_type_is (part, "text/html");
298 stream = tny_mime_part_get_stream (part);
300 res = read_chunk (stream, buffer, len, &nread);
301 while (res && (nread > 0)) {
302 /* search->text_searcher was instantiated in modest_search_folder(). */
306 found = ogs_text_searcher_search_html (search->text_searcher,
311 found = ogs_text_searcher_search_text (search->text_searcher,
321 res = read_chunk (stream, buffer, len, &nread);
325 found = ogs_text_searcher_search_done (search->text_searcher);
328 ogs_text_searcher_reset (search->text_searcher);
333 printf ("DEBUG: %s: query %s was not found in message text: %s\n",
334 __FUNCTION__, search->query, buffer);
337 printf ("DEBUG: %s: found.\n", __FUNCTION__);
347 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
357 if (! tny_mime_part_content_type_is (part, "text/*")) {
358 g_debug ("%s: No text MIME part found.\n", __FUNCTION__);
363 len = (sizeof (buffer) - 1) / 2;
365 if (strlen (search->body) > len) {
366 g_warning ("Search term bigger then chunk."
367 "We might not find everything");
370 stream = tny_mime_part_get_stream (part);
372 memset (buffer, 0, sizeof (buffer));
374 chunk[1] = buffer + len;
376 res = read_chunk (stream, chunk[0], len, &nread);
382 found = !modest_text_utils_utf8_strcmp (search->body,
389 /* This works like this:
390 * buffer: [ooooooooooo|xxxxxxxxxxxx|\0]
391 * ^chunk[0] ^chunk[1]
392 * we have prefilled chunk[0] now we always read into chunk[1]
393 * and then move the content of chunk[1] to chunk[0].
394 * The idea is to prevent not finding search terms that are
395 * spread across 2 reads:
396 * buffer: [ooooooooTES|Txxxxxxxxxxx|\0]
397 * We should catch that because we always search the whole
398 * buffer not only the chunks.
400 * Of course that breaks for search terms > sizeof (chunk)
401 * but sizeof (chunk) should be big enough I guess (see
402 * the g_warning in this function)
404 while ((res = read_chunk (stream, chunk[1], len, &nread))) {
405 buffer[len + nread] = '\0';
407 found = !modest_text_utils_utf8_strcmp (search->body,
415 /* also move the \0 */
416 g_memmove (chunk[0], chunk[1], len + 1);
420 g_object_unref (stream);
423 #endif /*MODEST_HAVE_OGS*/
426 search_string (const char *what,
428 ModestSearch *search)
431 #ifdef MODEST_HAVE_OGS
432 if (search->flags & MODEST_SEARCH_USE_OGS) {
433 found = ogs_text_searcher_search_text (search->text_searcher,
437 ogs_text_searcher_reset (search->text_searcher);
440 if (what == NULL || where == NULL) {
444 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
445 #ifdef MODEST_HAVE_OGS
452 static gboolean search_mime_part_and_child_parts (TnyMimePart *part, ModestSearch *search)
454 gboolean found = FALSE;
455 #ifdef MODEST_HAVE_OGS
456 found = search_mime_part_ogs (part, search);
458 found = search_mime_part_strcmp (part, search);
465 /* Check the child part too, recursively: */
466 TnyList *child_parts = tny_simple_list_new ();
467 tny_mime_part_get_parts (TNY_MIME_PART (part), child_parts);
469 TnyIterator *piter = tny_list_create_iterator (child_parts);
470 while (!found && !tny_iterator_is_done (piter)) {
471 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
473 found = search_mime_part_and_child_parts (pcur, search);
475 g_object_unref (pcur);
478 tny_iterator_next (piter);
481 g_object_unref (piter);
482 g_object_unref (child_parts);
489 * @folder: a #TnyFolder instance
490 * @search: a #ModestSearch query
492 * This operation will search @folder for headers that match the query @search,
493 * if the folder itself matches the query.
494 * It will return a doubly linked list with URIs that point to the message.
497 modest_search_folder (TnyFolder *folder, ModestSearch *search)
499 /* Check that we should be searching this folder. */
500 /* Note that we don't try to search sub-folders.
501 * Maybe we should, but that should be specified. */
502 if (search->folder && strlen (search->folder) && (strcmp (tny_folder_get_id (folder), search->folder) != 0))
505 GList *retval = NULL;
506 TnyIterator *iter = NULL;
507 TnyList *list = NULL;
509 #ifdef MODEST_HAVE_OGS
510 if (search->flags & MODEST_SEARCH_USE_OGS) {
512 if (search->text_searcher == NULL && search->query != NULL) {
513 OgsTextSearcher *text_searcher;
515 text_searcher = ogs_text_searcher_new (FALSE);
516 ogs_text_searcher_parse_query (text_searcher, search->query);
517 search->text_searcher = text_searcher;
522 list = tny_simple_list_new ();
523 GError *error = NULL;
524 tny_folder_get_headers (folder, list, FALSE /* don't refresh */, &error);
526 g_warning ("%s: tny_folder_get_headers() failed with error=%s.\n",
527 __FUNCTION__, error->message);
528 g_error_free (error);
532 iter = tny_list_create_iterator (list);
534 while (!tny_iterator_is_done (iter)) {
535 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
536 const time_t t = tny_header_get_date_sent (cur);
537 gboolean found = FALSE;
539 /* Ignore deleted (not yet expunged) emails: */
540 if (tny_header_get_flags(cur) & TNY_HEADER_FLAG_DELETED)
543 if (search->flags & MODEST_SEARCH_BEFORE)
544 if (!(t <= search->end_date))
547 if (search->flags & MODEST_SEARCH_AFTER)
548 if (!(t >= search->start_date))
551 if (search->flags & MODEST_SEARCH_SIZE)
552 if (tny_header_get_message_size (cur) < search->minsize)
555 if (search->flags & MODEST_SEARCH_SUBJECT) {
556 const char *str = tny_header_get_subject (cur);
558 if ((found = search_string (search->subject, str, search))) {
559 retval = add_hit (retval, cur, folder);
563 if (!found && search->flags & MODEST_SEARCH_SENDER) {
564 char *str = g_strdup (tny_header_get_from (cur));
566 if ((found = search_string (search->from, (const gchar *) str, search))) {
567 retval = add_hit (retval, cur, folder);
572 if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
573 const char *str = tny_header_get_to (cur);
575 if ((found = search_string (search->recipient, str, search))) {
576 retval = add_hit (retval, cur, folder);
580 if (!found && search->flags & MODEST_SEARCH_BODY) {
581 TnyHeaderFlags flags;
585 flags = tny_header_get_flags (cur);
587 if (!(flags & TNY_HEADER_FLAG_CACHED)) {
591 msg = tny_folder_get_msg (folder, cur, &err);
593 if (err != NULL || msg == NULL) {
594 g_warning ("%s: Could not get message.\n", __FUNCTION__);
598 g_object_unref (msg);
602 found = search_mime_part_and_child_parts (TNY_MIME_PART (msg),
605 retval = add_hit (retval, cur, folder);
610 g_object_unref (msg);
614 g_object_unref (cur);
615 tny_iterator_next (iter);
618 g_object_unref (iter);
619 g_object_unref (list);
624 modest_search_account (TnyAccount *account, ModestSearch *search)
626 TnyFolderStore *store;
635 store = TNY_FOLDER_STORE (account);
637 folders = tny_simple_list_new ();
638 tny_folder_store_get_folders (store, folders, NULL, &error);
641 g_object_unref (folders);
645 iter = tny_list_create_iterator (folders);
646 while (!tny_iterator_is_done (iter)) {
647 TnyFolder *folder = NULL;
650 folder = TNY_FOLDER (tny_iterator_get_current (iter));
652 /* g_debug ("DEBUG: %s: searching folder %s.",
653 __FUNCTION__, tny_folder_get_name (folder)); */
655 res = modest_search_folder (folder, search);
661 hits = g_list_concat (hits, res);
665 g_object_unref (folder);
668 tny_iterator_next (iter);
671 g_object_unref (iter);
672 g_object_unref (folders);
674 /* printf ("DEBUG: %s: hits length = %d\n", __FUNCTION__, g_list_length (hits)); */
679 modest_search_all_accounts (ModestSearch *search)
681 /* printf ("DEBUG: %s: query=%s\n", __FUNCTION__, search->query); */
682 ModestTnyAccountStore *astore;
688 astore = modest_runtime_get_account_store ();
690 accounts = tny_simple_list_new ();
691 tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
693 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
695 iter = tny_list_create_iterator (accounts);
696 while (!tny_iterator_is_done (iter)) {
697 TnyAccount *account = NULL;
700 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
702 /* g_debug ("DEBUG: %s: Searching account %s",
703 __FUNCTION__, tny_account_get_name (account)); */
705 /* Give the account time to go online if necessary,
706 * for instance if this is immediately after startup,
707 * after D-Bus activation: */
708 check_and_wait_for_account_is_online (account);
711 res = modest_search_account (account, search);
717 hits = g_list_concat (hits, res);
721 g_object_unref (account);
724 tny_iterator_next (iter);
727 g_object_unref (accounts);
728 g_object_unref (iter);
730 /* printf ("DEBUG: %s: end: hits length=%d\n", __FUNCTION__, g_list_length(hits)); */