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);
70 add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
80 hit = g_slice_new0 (ModestSearchHit);
82 furl = tny_folder_get_url_string (folder);
83 printf ("DEBUG: %s: folder URL=%s\n", __FUNCTION__, furl);
85 g_warning ("%s: tny_folder_get_url_string(): returned NULL for folder. Folder name=%s\n", __FUNCTION__, tny_folder_get_name (folder));
88 /* Make sure that we use the short UID instead of the long UID,
89 * and/or find out what UID form is used when finding, in camel_data_cache_get().
90 * so we can find what we get. Philip is working on this.
92 uid = tny_header_get_uid (header);
94 g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, tny_header_get_subject (header));
97 msg_url = g_strdup_printf ("%s/%s", furl, uid);
100 subject = tny_header_get_subject (header);
101 sender = tny_header_get_from (header);
103 flags = tny_header_get_flags (header);
105 hit->msgid = msg_url;
106 hit->subject = g_strdup_or_null (subject);
107 hit->sender = g_strdup_or_null (sender);
108 hit->folder = g_strdup_or_null (tny_folder_get_name (folder));
109 hit->msize = tny_header_get_message_size (header);
110 hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
111 hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
112 hit->timestamp = tny_header_get_date_received (header);
114 return g_list_prepend (list, hit);
117 /** Call this until it returns FALSE or nread is set to 0.
119 * @result: FALSE is something failed. */
121 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
126 while (_nread < count) {
127 res = tny_stream_read (stream,
130 if (res == -1) { /* error */
137 if (res == 0) { /* no more bytes read. */
147 #ifdef MODEST_HAVE_OGS
149 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
151 TnyStream *stream = NULL;
153 const gsize len = sizeof (buffer);
155 gboolean is_text_html = FALSE;
156 gboolean found = FALSE;
157 gboolean res = FALSE;
159 gboolean is_text = tny_mime_part_content_type_is (part, "text/*");
161 g_debug ("%s: tny_mime_part_content_type_is() failed to find a "
162 "text/* MIME part. Content type is %s",
163 __FUNCTION__, "Unknown (calling tny_mime_part_get_content_type(part) causes a deadlock)");
165 /* Retry with specific MIME types, because the wildcard seems to fail
167 * Actually I'm not sure anymore that it fails, so we could probalby
168 * remove this later: murrayc */
170 tny_mime_part_content_type_is (part, "text/plain") ||
171 tny_mime_part_content_type_is (part, "text/html") );
174 g_debug ("%s: Retryting with text/plain or text/html succeeded",
183 is_text_html = tny_mime_part_content_type_is (part, "text/html");
185 stream = tny_mime_part_get_stream (part);
187 res = read_chunk (stream, buffer, len, &nread);
188 while (res && (nread > 0)) {
189 /* search->text_searcher was instantiated in modest_search_folder(). */
193 found = ogs_text_searcher_search_html (search->text_searcher,
198 found = ogs_text_searcher_search_text (search->text_searcher,
208 res = read_chunk (stream, buffer, len, &nread);
212 found = ogs_text_searcher_search_done (search->text_searcher);
215 ogs_text_searcher_reset (search->text_searcher);
220 printf ("DEBUG: %s: query %s was not found in message text: %s\n",
221 __FUNCTION__, search->query, buffer);
224 printf ("DEBUG: %s: found.\n", __FUNCTION__);
234 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
244 if (! tny_mime_part_content_type_is (part, "text/*")) {
245 g_debug ("%s: No text MIME part found.\n", __FUNCTION__);
250 len = (sizeof (buffer) - 1) / 2;
252 if (strlen (search->body) > len) {
253 g_warning ("Search term bigger then chunk."
254 "We might not find everything");
257 stream = tny_mime_part_get_stream (part);
259 memset (buffer, 0, sizeof (buffer));
261 chunk[1] = buffer + len;
263 res = read_chunk (stream, chunk[0], len, &nread);
269 found = !modest_text_utils_utf8_strcmp (search->body,
276 /* This works like this:
277 * buffer: [ooooooooooo|xxxxxxxxxxxx|\0]
278 * ^chunk[0] ^chunk[1]
279 * we have prefilled chunk[0] now we always read into chunk[1]
280 * and then move the content of chunk[1] to chunk[0].
281 * The idea is to prevent not finding search terms that are
282 * spread across 2 reads:
283 * buffer: [ooooooooTES|Txxxxxxxxxxx|\0]
284 * We should catch that because we always search the whole
285 * buffer not only the chunks.
287 * Of course that breaks for search terms > sizeof (chunk)
288 * but sizeof (chunk) should be big enough I guess (see
289 * the g_warning in this function)
291 while ((res = read_chunk (stream, chunk[1], len, &nread))) {
292 buffer[len + nread] = '\0';
294 found = !modest_text_utils_utf8_strcmp (search->body,
302 /* also move the \0 */
303 g_memmove (chunk[0], chunk[1], len + 1);
307 g_object_unref (stream);
310 #endif /*MODEST_HAVE_OGS*/
313 search_string (const char *what,
315 ModestSearch *search)
318 #ifdef MODEST_HAVE_OGS
319 if (search->flags & MODEST_SEARCH_USE_OGS) {
320 found = ogs_text_searcher_search_text (search->text_searcher,
324 ogs_text_searcher_reset (search->text_searcher);
327 if (what == NULL || where == NULL) {
331 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
332 #ifdef MODEST_HAVE_OGS
339 static gboolean search_mime_part_and_child_parts (TnyMimePart *part, ModestSearch *search)
341 gboolean found = FALSE;
342 #ifdef MODEST_HAVE_OGS
343 found = search_mime_part_ogs (part, search);
345 found = search_mime_part_strcmp (part, search);
352 /* Check the child part too, recursively: */
353 TnyList *child_parts = tny_simple_list_new ();
354 tny_mime_part_get_parts (TNY_MIME_PART (part), child_parts);
356 TnyIterator *piter = tny_list_create_iterator (child_parts);
357 while (!found && !tny_iterator_is_done (piter)) {
358 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
360 found = search_mime_part_and_child_parts (pcur, search);
362 g_object_unref (pcur);
363 tny_iterator_next (piter);
366 g_object_unref (piter);
367 g_object_unref (child_parts);
374 * @folder: a #TnyFolder instance
375 * @search: a #ModestSearch query
377 * This operation will search @folder for headers that match the query @search,
378 * if the folder itself matches the query.
379 * It will return a doubly linked list with URIs that point to the message.
382 modest_search_folder (TnyFolder *folder, ModestSearch *search)
384 /* Check that we should be searching this folder. */
385 /* Note that we don't try to search sub-folders.
386 * Maybe we should, but that should be specified. */
387 if (search->folder && strlen (search->folder) && (strcmp (tny_folder_get_id (folder), search->folder) != 0))
390 GList *retval = NULL;
391 TnyIterator *iter = NULL;
392 TnyList *list = NULL;
394 #ifdef MODEST_HAVE_OGS
395 if (search->flags & MODEST_SEARCH_USE_OGS) {
397 if (search->text_searcher == NULL && search->query != NULL) {
398 OgsTextSearcher *text_searcher;
400 text_searcher = ogs_text_searcher_new (FALSE);
401 ogs_text_searcher_parse_query (text_searcher, search->query);
402 search->text_searcher = text_searcher;
407 list = tny_simple_list_new ();
408 GError *error = NULL;
409 tny_folder_get_headers (folder, list, FALSE /* don't refresh */, &error);
411 g_warning ("%s: tny_folder_get_headers() failed with error=%s.\n",
412 __FUNCTION__, error->message);
413 g_error_free (error);
417 iter = tny_list_create_iterator (list);
419 while (!tny_iterator_is_done (iter)) {
420 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
421 time_t t = tny_header_get_date_sent (cur);
422 gboolean found = FALSE;
424 if (search->flags & MODEST_SEARCH_BEFORE)
425 if (!(t <= search->before))
428 if (search->flags & MODEST_SEARCH_AFTER)
429 if (!(t >= search->after))
432 if (search->flags & MODEST_SEARCH_SIZE)
433 if (tny_header_get_message_size (cur) < search->minsize)
436 if (search->flags & MODEST_SEARCH_SUBJECT) {
437 const char *str = tny_header_get_subject (cur);
439 if ((found = search_string (search->subject, str, search))) {
440 retval = add_hit (retval, cur, folder);
444 if (!found && search->flags & MODEST_SEARCH_SENDER) {
445 char *str = g_strdup (tny_header_get_from (cur));
447 if ((found = search_string (search->from, (const gchar *) str, search))) {
448 retval = add_hit (retval, cur, folder);
453 if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
454 const char *str = tny_header_get_to (cur);
456 if ((found = search_string (search->recipient, str, search))) {
457 retval = add_hit (retval, cur, folder);
461 if (!found && search->flags & MODEST_SEARCH_BODY) {
462 TnyHeaderFlags flags;
466 flags = tny_header_get_flags (cur);
468 if (!(flags & TNY_HEADER_FLAG_CACHED)) {
472 msg = tny_folder_get_msg (folder, cur, &err);
474 if (err != NULL || msg == NULL) {
475 g_warning ("%s: Could not get message.\n", __FUNCTION__);
479 g_object_unref (msg);
483 found = search_mime_part_and_child_parts (TNY_MIME_PART (msg),
486 retval = add_hit (retval, cur, folder);
489 g_object_unref (msg);
494 g_object_unref (cur);
495 tny_iterator_next (iter);
498 g_object_unref (iter);
499 g_object_unref (list);
504 modest_search_account (TnyAccount *account, ModestSearch *search)
506 TnyFolderStore *store;
515 store = TNY_FOLDER_STORE (account);
517 folders = tny_simple_list_new ();
518 tny_folder_store_get_folders (store, folders, NULL, &error);
521 g_object_unref (folders);
525 iter = tny_list_create_iterator (folders);
526 while (!tny_iterator_is_done (iter)) {
530 folder = TNY_FOLDER (tny_iterator_get_current (iter));
531 /* g_debug ("DEBUG: %s: searching folder %s.",
532 __FUNCTION__, tny_folder_get_name (folder)); */
534 res = modest_search_folder (folder, search);
540 hits = g_list_concat (hits, res);
544 g_object_unref (folder);
545 tny_iterator_next (iter);
548 g_object_unref (iter);
549 g_object_unref (folders);
551 /* printf ("DEBUG: %s: hits length = %d\n", __FUNCTION__, g_list_length (hits)); */
556 modest_search_all_accounts (ModestSearch *search)
558 /* printf ("DEBUG: %s: query=%s\n", __FUNCTION__, search->query); */
559 ModestTnyAccountStore *astore;
565 astore = modest_runtime_get_account_store ();
567 accounts = tny_simple_list_new ();
568 tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
570 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
572 iter = tny_list_create_iterator (accounts);
573 while (!tny_iterator_is_done (iter)) {
577 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
579 /* g_debug ("DEBUG: %s: Searching account %s",
580 __FUNCTION__, tny_account_get_name (account)); */
581 res = modest_search_account (account, search);
588 hits = g_list_concat (hits, res);
592 g_object_unref (account);
593 tny_iterator_next (iter);
596 g_object_unref (accounts);
597 g_object_unref (iter);
599 /* printf ("DEBUG: %s: end: hits length=%d\n", __FUNCTION__, g_list_length(hits)); */