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"
59 g_strdup_or_null (const gchar *str)
64 string = g_strdup (str);
78 #define NUMBER_OF_TRIES 10 /* Try approx every second, ten times. */
81 on_timeout_check_account_is_online(gpointer user_data)
83 printf ("DEBUG: %s:\n", __FUNCTION__);
84 UtilIdleData *data = (UtilIdleData*)user_data;
87 g_warning ("%s: data is NULL.\n", __FUNCTION__);
90 if (!(data->account)) {
91 g_warning ("%s: data->account is NULL.\n", __FUNCTION__);
94 if (data && data->account) {
95 printf ("DEBUG: %s: tny_account_get_connection_status()==%d\n", __FUNCTION__, tny_account_get_connection_status (data->account));
98 gboolean stop_trying = FALSE;
99 if (data && data->account &&
100 /* We want to wait until TNY_CONNECTION_STATUS_INIT has changed to something else,
101 * after which the account is likely to be usable, or never likely to be usable soon: */
102 (tny_account_get_connection_status (data->account) != TNY_CONNECTION_STATUS_INIT) )
104 data->is_online = TRUE;
109 /* Give up if we have tried too many times: */
110 if (data->count_tries >= NUMBER_OF_TRIES)
115 /* Wait for another timeout: */
116 ++(data->count_tries);
121 /* Allow the function that requested this idle callback to continue: */
123 g_main_loop_quit (data->loop);
126 g_object_unref (data->account);
128 return FALSE; /* Don't call this again. */
130 return TRUE; /* Call this timeout callback again. */
134 /* Return TRUE immediately if the account is already online,
135 * otherwise check every second for NUMBER_OF_TRIES seconds and return TRUE as
136 * soon as the account is online, or FALSE if the account does
137 * not become online in the NUMBER_OF_TRIES seconds.
138 * This is useful when the D-Bus method was run immediately after
139 * the application was started (when using D-Bus activation),
140 * because the account usually takes a short time to go online.
141 * The return value is maybe not very useful.
144 check_and_wait_for_account_is_online(TnyAccount *account)
146 g_return_val_if_fail (account, FALSE);
148 printf ("DEBUG: %s: account id=%s\n", __FUNCTION__, tny_account_get_id (account));
150 if (!tny_device_is_online (modest_runtime_get_device())) {
151 printf ("DEBUG: %s: device is offline.\n", __FUNCTION__);
155 /* The local_folders account never seems to leave TNY_CONNECTION_STATUS_INIT,
156 * so we avoid wait unnecessarily: */
157 if (!TNY_IS_CAMEL_POP_STORE_ACCOUNT (account) &&
158 !TNY_IS_CAMEL_IMAP_STORE_ACCOUNT (account) ) {
162 printf ("DEBUG: %s: tny_account_get_connection_status()==%d\n", __FUNCTION__, tny_account_get_connection_status (account));
164 /* The POP & IMAP store accounts seem to be TNY_CONNECTION_STATUS_DISCONNECTED,
165 * and that seems to be an OK time to use them. Maybe it's just TNY_CONNECTION_STATUS_INIT that
166 * we want to avoid. */
167 if (tny_account_get_connection_status (account) != TNY_CONNECTION_STATUS_INIT)
170 /* This blocks on the result: */
171 UtilIdleData *data = g_slice_new0 (UtilIdleData);
172 data->is_online = FALSE;
173 data->account = account;
174 g_object_ref (data->account);
175 data->count_tries = 0;
177 GMainContext *context = NULL; /* g_main_context_new (); */
178 data->loop = g_main_loop_new (context, FALSE /* not running */);
180 g_timeout_add (1000, &on_timeout_check_account_is_online, data);
182 /* This main loop will run until the idle handler has stopped it: */
183 g_main_loop_run (data->loop);
185 g_main_loop_unref (data->loop);
186 /* g_main_context_unref (context); */
188 g_slice_free (UtilIdleData, data);
190 return data->is_online;
194 add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
196 ModestSearchHit *hit;
197 TnyHeaderFlags flags;
204 hit = g_slice_new0 (ModestSearchHit);
206 furl = tny_folder_get_url_string (folder);
207 printf ("DEBUG: %s: folder URL=%s\n", __FUNCTION__, furl);
209 g_warning ("%s: tny_folder_get_url_string(): returned NULL for folder. Folder name=%s\n", __FUNCTION__, tny_folder_get_name (folder));
212 /* Make sure that we use the short UID instead of the long UID,
213 * and/or find out what UID form is used when finding, in camel_data_cache_get().
214 * so we can find what we get. Philip is working on this.
216 uid = tny_header_get_uid (header);
218 g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, tny_header_get_subject (header));
221 msg_url = g_strdup_printf ("%s/%s", furl, uid);
224 subject = tny_header_get_subject (header);
225 sender = tny_header_get_from (header);
227 flags = tny_header_get_flags (header);
229 hit->msgid = msg_url;
230 hit->subject = g_strdup_or_null (subject);
231 hit->sender = g_strdup_or_null (sender);
232 hit->folder = g_strdup_or_null (tny_folder_get_name (folder));
233 hit->msize = tny_header_get_message_size (header);
234 hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
235 hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
236 hit->timestamp = tny_header_get_date_received (header);
238 return g_list_prepend (list, hit);
241 /** Call this until it returns FALSE or nread is set to 0.
243 * @result: FALSE is something failed. */
245 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
250 while (_nread < count) {
251 res = tny_stream_read (stream,
254 if (res == -1) { /* error */
261 if (res == 0) { /* no more bytes read. */
271 #ifdef MODEST_HAVE_OGS
273 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
275 TnyStream *stream = NULL;
277 const gsize len = sizeof (buffer);
279 gboolean is_text_html = FALSE;
280 gboolean found = FALSE;
281 gboolean res = FALSE;
283 gboolean is_text = tny_mime_part_content_type_is (part, "text/*");
285 g_debug ("%s: tny_mime_part_content_type_is() failed to find a "
286 "text/* MIME part. Content type is %s",
287 __FUNCTION__, "Unknown (calling tny_mime_part_get_content_type(part) causes a deadlock)");
289 /* Retry with specific MIME types, because the wildcard seems to fail
291 * Actually I'm not sure anymore that it fails, so we could probalby
292 * remove this later: murrayc */
294 tny_mime_part_content_type_is (part, "text/plain") ||
295 tny_mime_part_content_type_is (part, "text/html") );
298 g_debug ("%s: Retryting with text/plain or text/html succeeded",
307 is_text_html = tny_mime_part_content_type_is (part, "text/html");
309 stream = tny_mime_part_get_stream (part);
311 res = read_chunk (stream, buffer, len, &nread);
312 while (res && (nread > 0)) {
313 /* search->text_searcher was instantiated in modest_search_folder(). */
317 found = ogs_text_searcher_search_html (search->text_searcher,
322 found = ogs_text_searcher_search_text (search->text_searcher,
332 res = read_chunk (stream, buffer, len, &nread);
336 found = ogs_text_searcher_search_done (search->text_searcher);
339 ogs_text_searcher_reset (search->text_searcher);
344 printf ("DEBUG: %s: query %s was not found in message text: %s\n",
345 __FUNCTION__, search->query, buffer);
348 printf ("DEBUG: %s: found.\n", __FUNCTION__);
358 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
368 if (! tny_mime_part_content_type_is (part, "text/*")) {
369 g_debug ("%s: No text MIME part found.\n", __FUNCTION__);
374 len = (sizeof (buffer) - 1) / 2;
376 if (strlen (search->body) > len) {
377 g_warning ("Search term bigger then chunk."
378 "We might not find everything");
381 stream = tny_mime_part_get_stream (part);
383 memset (buffer, 0, sizeof (buffer));
385 chunk[1] = buffer + len;
387 res = read_chunk (stream, chunk[0], len, &nread);
393 found = !modest_text_utils_utf8_strcmp (search->body,
400 /* This works like this:
401 * buffer: [ooooooooooo|xxxxxxxxxxxx|\0]
402 * ^chunk[0] ^chunk[1]
403 * we have prefilled chunk[0] now we always read into chunk[1]
404 * and then move the content of chunk[1] to chunk[0].
405 * The idea is to prevent not finding search terms that are
406 * spread across 2 reads:
407 * buffer: [ooooooooTES|Txxxxxxxxxxx|\0]
408 * We should catch that because we always search the whole
409 * buffer not only the chunks.
411 * Of course that breaks for search terms > sizeof (chunk)
412 * but sizeof (chunk) should be big enough I guess (see
413 * the g_warning in this function)
415 while ((res = read_chunk (stream, chunk[1], len, &nread))) {
416 buffer[len + nread] = '\0';
418 found = !modest_text_utils_utf8_strcmp (search->body,
426 /* also move the \0 */
427 g_memmove (chunk[0], chunk[1], len + 1);
431 g_object_unref (stream);
434 #endif /*MODEST_HAVE_OGS*/
437 search_string (const char *what,
439 ModestSearch *search)
442 #ifdef MODEST_HAVE_OGS
443 if (search->flags & MODEST_SEARCH_USE_OGS) {
444 found = ogs_text_searcher_search_text (search->text_searcher,
448 ogs_text_searcher_reset (search->text_searcher);
451 if (what == NULL || where == NULL) {
455 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
456 #ifdef MODEST_HAVE_OGS
463 static gboolean search_mime_part_and_child_parts (TnyMimePart *part, ModestSearch *search)
465 gboolean found = FALSE;
466 #ifdef MODEST_HAVE_OGS
467 found = search_mime_part_ogs (part, search);
469 found = search_mime_part_strcmp (part, search);
476 /* Check the child part too, recursively: */
477 TnyList *child_parts = tny_simple_list_new ();
478 tny_mime_part_get_parts (TNY_MIME_PART (part), child_parts);
480 TnyIterator *piter = tny_list_create_iterator (child_parts);
481 while (!found && !tny_iterator_is_done (piter)) {
482 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
484 found = search_mime_part_and_child_parts (pcur, search);
486 g_object_unref (pcur);
489 tny_iterator_next (piter);
492 g_object_unref (piter);
493 g_object_unref (child_parts);
500 * @folder: a #TnyFolder instance
501 * @search: a #ModestSearch query
503 * This operation will search @folder for headers that match the query @search,
504 * if the folder itself matches the query.
505 * It will return a doubly linked list with URIs that point to the message.
508 modest_search_folder (TnyFolder *folder, ModestSearch *search)
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 (search->folder && strlen (search->folder) && (strcmp (tny_folder_get_id (folder), search->folder) != 0))
516 GList *retval = NULL;
517 TnyIterator *iter = NULL;
518 TnyList *list = NULL;
520 #ifdef MODEST_HAVE_OGS
521 if (search->flags & MODEST_SEARCH_USE_OGS) {
523 if (search->text_searcher == NULL && search->query != NULL) {
524 OgsTextSearcher *text_searcher;
526 text_searcher = ogs_text_searcher_new (FALSE);
527 ogs_text_searcher_parse_query (text_searcher, search->query);
528 search->text_searcher = text_searcher;
533 list = tny_simple_list_new ();
534 GError *error = NULL;
535 tny_folder_get_headers (folder, list, FALSE /* don't refresh */, &error);
537 g_warning ("%s: tny_folder_get_headers() failed with error=%s.\n",
538 __FUNCTION__, error->message);
539 g_error_free (error);
543 iter = tny_list_create_iterator (list);
545 while (!tny_iterator_is_done (iter)) {
546 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
547 const time_t t = tny_header_get_date_sent (cur);
548 gboolean found = FALSE;
550 /* Ignore deleted (not yet expunged) emails: */
551 if (tny_header_get_flags(cur) & TNY_HEADER_FLAG_DELETED)
554 if (search->flags & MODEST_SEARCH_BEFORE)
555 if (!(t <= search->end_date))
558 if (search->flags & MODEST_SEARCH_AFTER)
559 if (!(t >= search->start_date))
562 if (search->flags & MODEST_SEARCH_SIZE)
563 if (tny_header_get_message_size (cur) < search->minsize)
566 if (search->flags & MODEST_SEARCH_SUBJECT) {
567 const char *str = tny_header_get_subject (cur);
569 if ((found = search_string (search->subject, str, search))) {
570 retval = add_hit (retval, cur, folder);
574 if (!found && search->flags & MODEST_SEARCH_SENDER) {
575 char *str = g_strdup (tny_header_get_from (cur));
577 if ((found = search_string (search->from, (const gchar *) str, search))) {
578 retval = add_hit (retval, cur, folder);
583 if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
584 const char *str = tny_header_get_to (cur);
586 if ((found = search_string (search->recipient, str, search))) {
587 retval = add_hit (retval, cur, folder);
591 if (!found && search->flags & MODEST_SEARCH_BODY) {
592 TnyHeaderFlags flags;
596 flags = tny_header_get_flags (cur);
598 if (!(flags & TNY_HEADER_FLAG_CACHED)) {
602 msg = tny_folder_get_msg (folder, cur, &err);
604 if (err != NULL || msg == NULL) {
605 g_warning ("%s: Could not get message.\n", __FUNCTION__);
609 g_object_unref (msg);
613 found = search_mime_part_and_child_parts (TNY_MIME_PART (msg),
616 retval = add_hit (retval, cur, folder);
621 g_object_unref (msg);
625 g_object_unref (cur);
626 tny_iterator_next (iter);
629 g_object_unref (iter);
630 g_object_unref (list);
635 modest_search_account (TnyAccount *account, ModestSearch *search)
637 TnyFolderStore *store;
646 store = TNY_FOLDER_STORE (account);
648 folders = tny_simple_list_new ();
649 tny_folder_store_get_folders (store, folders, NULL, &error);
652 g_object_unref (folders);
656 iter = tny_list_create_iterator (folders);
657 while (!tny_iterator_is_done (iter)) {
658 TnyFolder *folder = NULL;
661 folder = TNY_FOLDER (tny_iterator_get_current (iter));
663 /* g_debug ("DEBUG: %s: searching folder %s.",
664 __FUNCTION__, tny_folder_get_name (folder)); */
666 res = modest_search_folder (folder, search);
672 hits = g_list_concat (hits, res);
676 g_object_unref (folder);
679 tny_iterator_next (iter);
682 g_object_unref (iter);
683 g_object_unref (folders);
685 /* printf ("DEBUG: %s: hits length = %d\n", __FUNCTION__, g_list_length (hits)); */
690 modest_search_all_accounts (ModestSearch *search)
692 /* printf ("DEBUG: %s: query=%s\n", __FUNCTION__, search->query); */
693 ModestTnyAccountStore *astore;
699 astore = modest_runtime_get_account_store ();
701 accounts = tny_simple_list_new ();
702 tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
704 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
706 iter = tny_list_create_iterator (accounts);
707 while (!tny_iterator_is_done (iter)) {
708 TnyAccount *account = NULL;
711 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
713 /* g_debug ("DEBUG: %s: Searching account %s",
714 __FUNCTION__, tny_account_get_name (account)); */
716 /* Give the account time to go online if necessary,
717 * for instance if this is immediately after startup,
718 * after D-Bus activation: */
719 check_and_wait_for_account_is_online (account);
722 res = modest_search_account (account, search);
728 hits = g_list_concat (hits, res);
732 g_object_unref (account);
735 tny_iterator_next (iter);
738 g_object_unref (accounts);
739 g_object_unref (iter);
741 /* printf ("DEBUG: %s: end: hits length=%d\n", __FUNCTION__, g_list_length(hits)); */