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);
118 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
124 while (_nread < count) {
125 res = tny_stream_read (stream,
143 #ifdef MODEST_HAVE_OGS
145 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
151 gboolean is_html = FALSE;
156 if (! tny_mime_part_content_type_is (part, "text/*") ||
157 ! (is_html = tny_mime_part_content_type_is (part, "text/html"))) {
158 g_debug ("%s: No text or html MIME part found.\n", __FUNCTION__);
163 len = sizeof (buffer);
164 stream = tny_mime_part_get_stream (part);
166 while ((res = read_chunk (stream, buffer, len, &nread))) {
168 /* search->text_searcher was instantiated in modest_search_folder(). */
172 found = ogs_text_searcher_search_html (search->text_searcher,
177 found = ogs_text_searcher_search_text (search->text_searcher,
189 found = ogs_text_searcher_search_done (search->text_searcher);
192 ogs_text_searcher_reset (search->text_searcher);
197 printf ("DEBUG: %s: query %s was not found in message text: %s\n",
198 __FUNCTION__, search->query, buffer);
201 printf ("DEBUG: %s: found.\n", __FUNCTION__);
211 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
221 if (! tny_mime_part_content_type_is (part, "text/*")) {
222 g_debug ("%s: No text MIME part found.\n", __FUNCTION__);
227 len = (sizeof (buffer) - 1) / 2;
229 if (strlen (search->body) > len) {
230 g_warning ("Search term bigger then chunk."
231 "We might not find everything");
234 stream = tny_mime_part_get_stream (part);
236 memset (buffer, 0, sizeof (buffer));
238 chunk[1] = buffer + len;
240 res = read_chunk (stream, chunk[0], len, &nread);
246 found = !modest_text_utils_utf8_strcmp (search->body,
253 /* This works like this:
254 * buffer: [ooooooooooo|xxxxxxxxxxxx|\0]
255 * ^chunk[0] ^chunk[1]
256 * we have prefilled chunk[0] now we always read into chunk[1]
257 * and then move the content of chunk[1] to chunk[0].
258 * The idea is to prevent not finding search terms that are
259 * spread across 2 reads:
260 * buffer: [ooooooooTES|Txxxxxxxxxxx|\0]
261 * We should catch that because we always search the whole
262 * buffer not only the chunks.
264 * Of course that breaks for search terms > sizeof (chunk)
265 * but sizeof (chunk) should be big enough I guess (see
266 * the g_warning in this function)
268 while ((res = read_chunk (stream, chunk[1], len, &nread))) {
269 buffer[len + nread] = '\0';
271 found = !modest_text_utils_utf8_strcmp (search->body,
279 /* also move the \0 */
280 g_memmove (chunk[0], chunk[1], len + 1);
284 g_object_unref (stream);
287 #endif /*MODEST_HAVE_OGS*/
290 search_string (const char *what,
292 ModestSearch *search)
295 #ifdef MODEST_HAVE_OGS
296 if (search->flags & MODEST_SEARCH_USE_OGS) {
297 found = ogs_text_searcher_search_text (search->text_searcher,
301 ogs_text_searcher_reset (search->text_searcher);
304 if (what == NULL || where == NULL) {
308 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
309 #ifdef MODEST_HAVE_OGS
319 * @folder: a #TnyFolder instance
320 * @search: a #ModestSearch query
322 * This operation will search @folder for headers that match the query @search.
323 * It will return a doubly linked list with URIs that point to the message.
326 modest_search_folder (TnyFolder *folder, ModestSearch *search)
328 GList *retval = NULL;
329 TnyIterator *iter = NULL;
330 TnyList *list = NULL;
332 #ifdef MODEST_HAVE_OGS
333 if (search->flags & MODEST_SEARCH_USE_OGS) {
335 if (search->text_searcher == NULL && search->query != NULL) {
336 OgsTextSearcher *text_searcher;
338 text_searcher = ogs_text_searcher_new (FALSE);
339 ogs_text_searcher_parse_query (text_searcher, search->query);
340 search->text_searcher = text_searcher;
345 list = tny_simple_list_new ();
346 tny_folder_get_headers (folder, list, FALSE, NULL);
348 iter = tny_list_create_iterator (list);
350 while (!tny_iterator_is_done (iter)) {
351 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
352 time_t t = tny_header_get_date_sent (cur);
353 gboolean found = FALSE;
355 if (search->flags & MODEST_SEARCH_BEFORE)
356 if (!(t <= search->before))
359 if (search->flags & MODEST_SEARCH_AFTER)
360 if (!(t >= search->after))
363 if (search->flags & MODEST_SEARCH_SIZE)
364 if (tny_header_get_message_size (cur) < search->minsize)
367 if (search->flags & MODEST_SEARCH_SUBJECT) {
368 const char *str = tny_header_get_subject (cur);
370 if ((found = search_string (search->subject, str, search))) {
371 retval = add_hit (retval, cur, folder);
375 if (!found && search->flags & MODEST_SEARCH_SENDER) {
376 char *str = g_strdup (tny_header_get_from (cur));
378 if ((found = search_string (search->from, (const gchar *) str, search))) {
379 retval = add_hit (retval, cur, folder);
384 if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
385 const char *str = tny_header_get_to (cur);
387 if ((found = search_string (search->recipient, str, search))) {
388 retval = add_hit (retval, cur, folder);
392 if (!found && search->flags & MODEST_SEARCH_BODY) {
393 TnyHeaderFlags flags;
399 flags = tny_header_get_flags (cur);
401 if (!(flags & TNY_HEADER_FLAG_CACHED)) {
405 msg = tny_folder_get_msg (folder, cur, &err);
407 if (err != NULL || msg == NULL) {
408 g_warning ("%s: Could not get message.\n", __FUNCTION__);
412 g_object_unref (msg);
416 parts = tny_simple_list_new ();
417 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
419 if (tny_list_get_length(parts) == 0) {
420 gchar *url_string = tny_msg_get_url_string (msg);
421 g_debug ("DEBUG: %s: tny_mime_part_get_parts(msg) returned an empty list for message url=%s",
422 __FUNCTION__, url_string);
426 piter = tny_list_create_iterator (parts);
427 while (!found && !tny_iterator_is_done (piter)) {
428 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
430 #ifdef MODEST_HAVE_OGS
431 found = search_mime_part_ogs (pcur, search);
433 found = search_mime_part_strcmp (pcur, search);
437 retval = add_hit (retval, cur, folder);
440 g_object_unref (pcur);
441 tny_iterator_next (piter);
444 g_object_unref (piter);
445 g_object_unref (parts);
446 g_object_unref (msg);
451 g_object_unref (cur);
452 tny_iterator_next (iter);
455 g_object_unref (iter);
456 g_object_unref (list);
461 modest_search_account (TnyAccount *account, ModestSearch *search)
463 TnyFolderStore *store;
472 store = TNY_FOLDER_STORE (account);
474 folders = tny_simple_list_new ();
475 tny_folder_store_get_folders (store, folders, NULL, &error);
478 g_object_unref (folders);
482 iter = tny_list_create_iterator (folders);
483 while (!tny_iterator_is_done (iter)) {
487 folder = TNY_FOLDER (tny_iterator_get_current (iter));
488 /* g_debug ("DEBUG: %s: searching folder %s.",
489 __FUNCTION__, tny_folder_get_name (folder)); */
491 res = modest_search_folder (folder, search);
497 hits = g_list_concat (hits, res);
501 g_object_unref (folder);
502 tny_iterator_next (iter);
505 g_object_unref (iter);
506 g_object_unref (folders);
512 modest_search_all_accounts (ModestSearch *search)
514 ModestTnyAccountStore *astore;
520 astore = modest_runtime_get_account_store ();
522 accounts = tny_simple_list_new ();
523 tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
525 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
527 iter = tny_list_create_iterator (accounts);
528 while (!tny_iterator_is_done (iter)) {
532 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
534 /* g_debug ("DEBUG: %s: Searching account %s",
535 __FUNCTION__, tny_account_get_name (account)); */
536 res = modest_search_account (account, search);
543 hits = g_list_concat (hits, res);
547 g_object_unref (account);
548 tny_iterator_next (iter);
551 g_object_unref (accounts);
552 g_object_unref (iter);