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);
69 add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
79 hit = g_slice_new0 (ModestSearchHit);
81 furl = tny_folder_get_url_string (folder);
83 g_warning ("%s: tny_folder_get_url_string(): returned NULL for folder. Folder name=%s\n", __FUNCTION__, tny_folder_get_name (folder));
86 /* Make sure that we use the short UID instead of the long UID,
87 * and/or find out what UID form is used when finding, in camel_data_cache_get().
88 * so we can find what we get. Philip is working on this.
90 uid = tny_header_get_uid (header);
92 g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, tny_header_get_subject (header));
95 msg_url = g_strdup_printf ("%s/%s", furl, uid);
97 subject = tny_header_get_subject (header);
98 sender = tny_header_get_from (header);
100 flags = tny_header_get_flags (header);
102 hit->msgid = msg_url;
103 hit->subject = g_strdup_or_null (subject);
104 hit->sender = g_strdup_or_null (sender);
105 hit->folder = furl; /* We just provide our new instance instead of copying it and freeing it. */
106 hit->msize = tny_header_get_message_size (header);
107 hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
108 hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
109 hit->timestamp = tny_header_get_date_received (header);
111 return g_list_prepend (list, hit);
115 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
121 while (_nread < count) {
122 res = tny_stream_read (stream,
142 #ifdef MODEST_HAVE_OGS
144 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
150 gboolean is_html = FALSE;
155 if (! tny_mime_part_content_type_is (part, "text/ *") ||
156 ! (is_html = tny_mime_part_content_type_is (part, "text/html"))) {
161 len = sizeof (buffer);
162 stream = tny_mime_part_get_stream (part);
164 while ((res = read_chunk (stream, buffer, len, &nread))) {
168 found = ogs_text_searcher_search_html (search->text_searcher,
173 found = ogs_text_searcher_search_text (search->text_searcher,
185 found = ogs_text_searcher_search_done (search->text_searcher);
188 ogs_text_searcher_reset (search->text_searcher);
192 #endif /*MODEST_HAVE_OGS*/
195 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
205 if (! tny_mime_part_content_type_is (part, "text/ *")) {
210 len = (sizeof (buffer) - 1) / 2;
212 if (strlen (search->body) > len) {
213 g_warning ("Search term bigger then chunk."
214 "We might not find everything");
217 stream = tny_mime_part_get_stream (part);
219 memset (buffer, 0, sizeof (buffer));
221 chunk[1] = buffer + len;
223 res = read_chunk (stream, chunk[0], len, &nread);
229 found = !modest_text_utils_utf8_strcmp (search->body,
236 /* This works like this:
237 * buffer: [ooooooooooo|xxxxxxxxxxxx|\0]
238 * ^chunk[0] ^chunk[1]
239 * we have prefilled chunk[0] now we always read into chunk[1]
240 * and then move the content of chunk[1] to chunk[0].
241 * The idea is to prevent not finding search terms that are
242 * spread across 2 reads:
243 * buffer: [ooooooooTES|Txxxxxxxxxxx|\0]
244 * We should catch that because we always search the whole
245 * buffer not only the chunks.
247 * Of course that breaks for search terms > sizeof (chunk)
248 * but sizeof (chunk) should be big enough I guess (see
249 * the g_warning in this function)
251 while ((res = read_chunk (stream, chunk[1], len, &nread))) {
252 buffer[len + nread] = '\0';
254 found = !modest_text_utils_utf8_strcmp (search->body,
262 /* also move the \0 */
263 g_memmove (chunk[0], chunk[1], len + 1);
267 g_object_unref (stream);
272 search_string (const char *what,
274 ModestSearch *search)
277 #ifdef MODEST_HAVE_OGS
278 if (search->flags & MODEST_SEARCH_USE_OGS) {
279 found = ogs_text_searcher_search_text (search->text_searcher,
283 ogs_text_searcher_reset (search->text_searcher);
286 if (what == NULL || where == NULL) {
290 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
291 #ifdef MODEST_HAVE_OGS
301 * @folder: a #TnyFolder instance
302 * @search: a #ModestSearch query
304 * This operation will search @folder for headers that match the query @search.
305 * It will return a doubly linked list with URIs that point to the message.
308 modest_search_folder (TnyFolder *folder, ModestSearch *search)
310 GList *retval = NULL;
313 gboolean (*part_search_func) (TnyMimePart *part, ModestSearch *search);
315 part_search_func = search_mime_part_strcmp;
317 #ifdef MODEST_HAVE_OGS
318 if (search->flags & MODEST_SEARCH_USE_OGS) {
320 if (search->text_searcher == NULL && search->query != NULL) {
321 OgsTextSearcher *text_searcher;
323 text_searcher = ogs_text_searcher_new (FALSE);
324 ogs_text_searcher_parse_query (text_searcher, search->query);
325 search->text_searcher = text_searcher;
328 part_search_func = search_mime_part_ogs;
332 list = tny_simple_list_new ();
333 tny_folder_get_headers (folder, list, FALSE, NULL);
335 iter = tny_list_create_iterator (list);
337 while (!tny_iterator_is_done (iter)) {
338 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
339 time_t t = tny_header_get_date_sent (cur);
340 gboolean found = FALSE;
342 if (search->flags & MODEST_SEARCH_BEFORE)
343 if (!(t <= search->before))
346 if (search->flags & MODEST_SEARCH_AFTER)
347 if (!(t >= search->after))
350 if (search->flags & MODEST_SEARCH_SIZE)
351 if (tny_header_get_message_size (cur) < search->minsize)
354 if (search->flags & MODEST_SEARCH_SUBJECT) {
355 const char *str = tny_header_get_subject (cur);
357 if ((found = search_string (search->subject, str, search))) {
358 retval = add_hit (retval, cur, folder);
362 if (!found && search->flags & MODEST_SEARCH_SENDER) {
363 char *str = g_strdup (tny_header_get_from (cur));
365 if ((found = search_string (search->from, (const gchar *) str, search))) {
366 retval = add_hit (retval, cur, folder);
371 if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
372 const char *str = tny_header_get_to (cur);
374 if ((found = search_string (search->recipient, str, search))) {
375 retval = add_hit (retval, cur, folder);
379 if (!found && search->flags & MODEST_SEARCH_BODY) {
380 TnyHeaderFlags flags;
386 flags = tny_header_get_flags (cur);
388 if (!(flags & TNY_HEADER_FLAG_CACHED)) {
392 msg = tny_folder_get_msg (folder, cur, &err);
394 if (err != NULL || msg == NULL) {
395 g_warning ("%s: Could not get message.\n", __FUNCTION__);
399 g_object_unref (msg);
403 parts = tny_simple_list_new ();
404 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
406 piter = tny_list_create_iterator (parts);
407 while (!found && !tny_iterator_is_done (piter)) {
408 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
410 if ((found = part_search_func (pcur, search))) {
411 retval = add_hit (retval, cur, folder);
414 g_object_unref (pcur);
415 tny_iterator_next (piter);
418 g_object_unref (piter);
419 g_object_unref (parts);
420 g_object_unref (msg);
425 g_object_unref (cur);
426 tny_iterator_next (iter);
429 g_object_unref (iter);
430 g_object_unref (list);
435 modest_search_account (TnyAccount *account, ModestSearch *search)
437 TnyFolderStore *store;
446 store = TNY_FOLDER_STORE (account);
448 folders = tny_simple_list_new ();
449 tny_folder_store_get_folders (store, folders, NULL, &error);
452 g_object_unref (folders);
456 iter = tny_list_create_iterator (folders);
457 while (!tny_iterator_is_done (iter)) {
461 folder = TNY_FOLDER (tny_iterator_get_current (iter));
463 res = modest_search_folder (folder, search);
469 hits = g_list_concat (hits, res);
473 g_object_unref (folder);
474 tny_iterator_next (iter);
477 g_object_unref (iter);
478 g_object_unref (folders);
484 modest_search_all_accounts (ModestSearch *search)
486 ModestTnyAccountStore *astore;
492 astore = modest_runtime_get_account_store ();
494 accounts = tny_simple_list_new ();
495 tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
497 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
499 iter = tny_list_create_iterator (accounts);
500 while (!tny_iterator_is_done (iter)) {
504 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
506 g_debug ("DEBUG: %s: Searching account %s",
507 __FUNCTION__, tny_account_get_name (account));
508 res = modest_search_account (account, search);
515 hits = g_list_concat (hits, res);
519 g_object_unref (account);
520 tny_iterator_next (iter);
523 g_object_unref (accounts);
524 g_object_unref (iter);