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);
84 g_warning ("%s: tny_folder_get_url_string(): returned NULL for folder. Folder name=%s\n", __FUNCTION__, tny_folder_get_name (folder));
87 /* Make sure that we use the short UID instead of the long UID,
88 * and/or find out what UID form is used when finding, in camel_data_cache_get().
89 * so we can find what we get. Philip is working on this.
91 uid = tny_header_get_uid (header);
93 g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, tny_header_get_subject (header));
96 msg_url = g_strdup_printf ("%s/%s", furl, uid);
98 subject = tny_header_get_subject (header);
99 sender = tny_header_get_from (header);
101 flags = tny_header_get_flags (header);
103 hit->msgid = msg_url;
104 hit->subject = g_strdup_or_null (subject);
105 hit->sender = g_strdup_or_null (sender);
106 hit->folder = g_strdup_or_null (tny_folder_get_name (folder));
107 //furl; /* We just provide our new instance instead of copying it and freeing it. */
108 hit->msize = tny_header_get_message_size (header);
109 hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
110 hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
111 hit->timestamp = tny_header_get_date_received (header);
113 return g_list_prepend (list, hit);
117 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
123 while (_nread < count) {
124 res = tny_stream_read (stream,
144 #ifdef MODEST_HAVE_OGS
146 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
152 gboolean is_html = FALSE;
157 if (! tny_mime_part_content_type_is (part, "text/ *") ||
158 ! (is_html = tny_mime_part_content_type_is (part, "text/html"))) {
163 len = sizeof (buffer);
164 stream = tny_mime_part_get_stream (part);
166 while ((res = read_chunk (stream, buffer, len, &nread))) {
170 found = ogs_text_searcher_search_html (search->text_searcher,
175 found = ogs_text_searcher_search_text (search->text_searcher,
187 found = ogs_text_searcher_search_done (search->text_searcher);
190 ogs_text_searcher_reset (search->text_searcher);
194 #endif /*MODEST_HAVE_OGS*/
197 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
207 if (! tny_mime_part_content_type_is (part, "text/ *")) {
212 len = (sizeof (buffer) - 1) / 2;
214 if (strlen (search->body) > len) {
215 g_warning ("Search term bigger then chunk."
216 "We might not find everything");
219 stream = tny_mime_part_get_stream (part);
221 memset (buffer, 0, sizeof (buffer));
223 chunk[1] = buffer + len;
225 res = read_chunk (stream, chunk[0], len, &nread);
231 found = !modest_text_utils_utf8_strcmp (search->body,
238 /* This works like this:
239 * buffer: [ooooooooooo|xxxxxxxxxxxx|\0]
240 * ^chunk[0] ^chunk[1]
241 * we have prefilled chunk[0] now we always read into chunk[1]
242 * and then move the content of chunk[1] to chunk[0].
243 * The idea is to prevent not finding search terms that are
244 * spread across 2 reads:
245 * buffer: [ooooooooTES|Txxxxxxxxxxx|\0]
246 * We should catch that because we always search the whole
247 * buffer not only the chunks.
249 * Of course that breaks for search terms > sizeof (chunk)
250 * but sizeof (chunk) should be big enough I guess (see
251 * the g_warning in this function)
253 while ((res = read_chunk (stream, chunk[1], len, &nread))) {
254 buffer[len + nread] = '\0';
256 found = !modest_text_utils_utf8_strcmp (search->body,
264 /* also move the \0 */
265 g_memmove (chunk[0], chunk[1], len + 1);
269 g_object_unref (stream);
274 search_string (const char *what,
276 ModestSearch *search)
279 #ifdef MODEST_HAVE_OGS
280 if (search->flags & MODEST_SEARCH_USE_OGS) {
281 found = ogs_text_searcher_search_text (search->text_searcher,
285 ogs_text_searcher_reset (search->text_searcher);
288 if (what == NULL || where == NULL) {
292 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
293 #ifdef MODEST_HAVE_OGS
303 * @folder: a #TnyFolder instance
304 * @search: a #ModestSearch query
306 * This operation will search @folder for headers that match the query @search.
307 * It will return a doubly linked list with URIs that point to the message.
310 modest_search_folder (TnyFolder *folder, ModestSearch *search)
312 GList *retval = NULL;
315 gboolean (*part_search_func) (TnyMimePart *part, ModestSearch *search);
317 part_search_func = search_mime_part_strcmp;
319 #ifdef MODEST_HAVE_OGS
320 if (search->flags & MODEST_SEARCH_USE_OGS) {
322 if (search->text_searcher == NULL && search->query != NULL) {
323 OgsTextSearcher *text_searcher;
325 text_searcher = ogs_text_searcher_new (FALSE);
326 ogs_text_searcher_parse_query (text_searcher, search->query);
327 search->text_searcher = text_searcher;
330 part_search_func = search_mime_part_ogs;
334 list = tny_simple_list_new ();
335 tny_folder_get_headers (folder, list, FALSE, NULL);
337 iter = tny_list_create_iterator (list);
339 while (!tny_iterator_is_done (iter)) {
340 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
341 time_t t = tny_header_get_date_sent (cur);
342 gboolean found = FALSE;
344 if (search->flags & MODEST_SEARCH_BEFORE)
345 if (!(t <= search->before))
348 if (search->flags & MODEST_SEARCH_AFTER)
349 if (!(t >= search->after))
352 if (search->flags & MODEST_SEARCH_SIZE)
353 if (tny_header_get_message_size (cur) < search->minsize)
356 if (search->flags & MODEST_SEARCH_SUBJECT) {
357 const char *str = tny_header_get_subject (cur);
359 if ((found = search_string (search->subject, str, search))) {
360 retval = add_hit (retval, cur, folder);
364 if (!found && search->flags & MODEST_SEARCH_SENDER) {
365 char *str = g_strdup (tny_header_get_from (cur));
367 if ((found = search_string (search->from, (const gchar *) str, search))) {
368 retval = add_hit (retval, cur, folder);
373 if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
374 const char *str = tny_header_get_to (cur);
376 if ((found = search_string (search->recipient, str, search))) {
377 retval = add_hit (retval, cur, folder);
381 if (!found && search->flags & MODEST_SEARCH_BODY) {
382 TnyHeaderFlags flags;
388 flags = tny_header_get_flags (cur);
390 if (!(flags & TNY_HEADER_FLAG_CACHED)) {
394 msg = tny_folder_get_msg (folder, cur, &err);
396 if (err != NULL || msg == NULL) {
397 g_warning ("%s: Could not get message.\n", __FUNCTION__);
401 g_object_unref (msg);
405 parts = tny_simple_list_new ();
406 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
408 piter = tny_list_create_iterator (parts);
409 while (!found && !tny_iterator_is_done (piter)) {
410 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
412 if ((found = part_search_func (pcur, search))) {
413 retval = add_hit (retval, cur, folder);
416 g_object_unref (pcur);
417 tny_iterator_next (piter);
420 g_object_unref (piter);
421 g_object_unref (parts);
422 g_object_unref (msg);
427 g_object_unref (cur);
428 tny_iterator_next (iter);
431 g_object_unref (iter);
432 g_object_unref (list);
437 modest_search_account (TnyAccount *account, ModestSearch *search)
439 TnyFolderStore *store;
448 store = TNY_FOLDER_STORE (account);
450 folders = tny_simple_list_new ();
451 tny_folder_store_get_folders (store, folders, NULL, &error);
454 g_object_unref (folders);
458 iter = tny_list_create_iterator (folders);
459 while (!tny_iterator_is_done (iter)) {
463 folder = TNY_FOLDER (tny_iterator_get_current (iter));
465 res = modest_search_folder (folder, search);
471 hits = g_list_concat (hits, res);
475 g_object_unref (folder);
476 tny_iterator_next (iter);
479 g_object_unref (iter);
480 g_object_unref (folders);
486 modest_search_all_accounts (ModestSearch *search)
488 ModestTnyAccountStore *astore;
494 astore = modest_runtime_get_account_store ();
496 accounts = tny_simple_list_new ();
497 tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
499 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
501 iter = tny_list_create_iterator (accounts);
502 while (!tny_iterator_is_done (iter)) {
506 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
508 g_debug ("DEBUG: %s: Searching account %s",
509 __FUNCTION__, tny_account_get_name (account));
510 res = modest_search_account (account, search);
517 hits = g_list_concat (hits, res);
521 g_object_unref (account);
522 tny_iterator_next (iter);
525 g_object_unref (accounts);
526 g_object_unref (iter);