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);
82 uid = tny_header_get_uid (header);
83 msg_url = g_strdup_printf ("%s/%s", furl, uid);
84 subject = tny_header_get_subject (header);
85 sender = tny_header_get_from (header);
87 flags = tny_header_get_flags (header);
90 hit->subject = g_strdup_or_null (subject);
91 hit->sender = g_strdup_or_null (sender);
93 hit->msize = tny_header_get_message_size (header);
94 hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
95 hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
96 hit->timestamp = tny_header_get_date_received (header);
98 return g_list_prepend (list, hit);
102 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
108 while (_nread < count) {
109 res = tny_stream_read (stream,
129 #ifdef MODEST_HAVE_OGS
131 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
137 gboolean is_html = FALSE;
142 if (! tny_mime_part_content_type_is (part, "text/ *") ||
143 ! (is_html = tny_mime_part_content_type_is (part, "text/html"))) {
148 len = sizeof (buffer);
149 stream = tny_mime_part_get_stream (part);
151 while ((res = read_chunk (stream, buffer, len, &nread))) {
155 found = ogs_text_searcher_search_html (search->text_searcher,
160 found = ogs_text_searcher_search_text (search->text_searcher,
172 found = ogs_text_searcher_search_done (search->text_searcher);
175 ogs_text_searcher_reset (search->text_searcher);
179 #endif /*MODEST_HAVE_OGS*/
182 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
192 if (! tny_mime_part_content_type_is (part, "text/ *")) {
197 len = (sizeof (buffer) - 1) / 2;
199 if (strlen (search->body) > len) {
200 g_warning ("Search term bigger then chunk."
201 "We might not find everything");
204 stream = tny_mime_part_get_stream (part);
206 memset (buffer, 0, sizeof (buffer));
208 chunk[1] = buffer + len;
210 res = read_chunk (stream, chunk[0], len, &nread);
216 found = !modest_text_utils_utf8_strcmp (search->body,
223 /* This works like this:
224 * buffer: [ooooooooooo|xxxxxxxxxxxx|\0]
225 * ^chunk[0] ^chunk[1]
226 * we have prefilled chunk[0] now we always read into chunk[1]
227 * and then move the content of chunk[1] to chunk[0].
228 * The idea is to prevent not finding search terms that are
229 * spread across 2 reads:
230 * buffer: [ooooooooTES|Txxxxxxxxxxx|\0]
231 * We should catch that because we always search the whole
232 * buffer not only the chunks.
234 * Of course that breaks for search terms > sizeof (chunk)
235 * but sizeof (chunk) should be big enough I guess (see
236 * the g_warning in this function)
238 while ((res = read_chunk (stream, chunk[1], len, &nread))) {
239 buffer[len + nread] = '\0';
241 found = !modest_text_utils_utf8_strcmp (search->body,
249 /* also move the \0 */
250 g_memmove (chunk[0], chunk[1], len + 1);
254 g_object_unref (stream);
259 search_string (const char *what,
261 ModestSearch *search)
264 #ifdef MODEST_HAVE_OGS
265 if (search->flags & MODEST_SEARCH_USE_OGS) {
266 found = ogs_text_searcher_search_text (search->text_searcher,
270 ogs_text_searcher_reset (search->text_searcher);
273 if (what == NULL || where == NULL) {
277 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
278 #ifdef MODEST_HAVE_OGS
288 * @folder: a #TnyFolder instance
289 * @search: a #ModestSearch query
291 * This operation will search @folder for headers that match the query @search.
292 * It will return a doubly linked list with URIs that point to the message.
295 modest_search_folder (TnyFolder *folder, ModestSearch *search)
297 GList *retval = NULL;
300 gboolean (*part_search_func) (TnyMimePart *part, ModestSearch *search);
302 part_search_func = search_mime_part_strcmp;
304 #ifdef MODEST_HAVE_OGS
305 if (search->flags & MODEST_SEARCH_USE_OGS) {
307 if (search->text_searcher == NULL && search->query != NULL) {
308 OgsTextSearcher *text_searcher;
310 text_searcher = ogs_text_searcher_new (FALSE);
311 ogs_text_searcher_parse_query (text_searcher, search->query);
312 search->text_searcher = text_searcher;
315 part_search_func = search_mime_part_ogs;
319 list = tny_simple_list_new ();
320 tny_folder_get_headers (folder, list, FALSE, NULL);
322 iter = tny_list_create_iterator (list);
324 while (!tny_iterator_is_done (iter)) {
325 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
326 time_t t = tny_header_get_date_sent (cur);
327 gboolean found = FALSE;
329 if (search->flags & MODEST_SEARCH_BEFORE)
330 if (!(t <= search->before))
333 if (search->flags & MODEST_SEARCH_AFTER)
334 if (!(t >= search->after))
337 if (search->flags & MODEST_SEARCH_SIZE)
338 if (tny_header_get_message_size (cur) < search->minsize)
341 if (search->flags & MODEST_SEARCH_SUBJECT) {
342 const char *str = tny_header_get_subject (cur);
344 if ((found = search_string (search->subject, str, search))) {
345 retval = add_hit (retval, cur, folder);
349 if (!found && search->flags & MODEST_SEARCH_SENDER) {
350 const char *str = tny_header_get_from (cur);
352 if ((found = search_string (search->from, str, search))) {
353 retval = add_hit (retval, cur, folder);
357 if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
358 const char *str = tny_header_get_to (cur);
360 if ((found = search_string (search->recipient, str, search))) {
361 retval = add_hit (retval, cur, folder);
365 if (!found && search->flags & MODEST_SEARCH_BODY) {
366 TnyHeaderFlags flags;
372 flags = tny_header_get_flags (cur);
374 if (!(flags & TNY_HEADER_FLAG_CACHED)) {
378 msg = tny_folder_get_msg (folder, cur, &err);
380 if (err != NULL || msg == NULL) {
381 g_warning ("Could not get message\n");
385 g_object_unref (msg);
389 parts = tny_simple_list_new ();
390 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
392 piter = tny_list_create_iterator (parts);
393 while (!found && !tny_iterator_is_done (piter)) {
394 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
396 if ((found = part_search_func (pcur, search))) {
397 retval = add_hit (retval, cur, folder);
400 g_object_unref (pcur);
401 tny_iterator_next (piter);
404 g_object_unref (piter);
405 g_object_unref (parts);
406 g_object_unref (msg);
411 g_object_unref (cur);
412 tny_iterator_next (iter);
415 g_object_unref (iter);
416 g_object_unref (list);
421 modest_search_account (TnyAccount *account, ModestSearch *search)
423 TnyFolderStore *store;
432 store = TNY_FOLDER_STORE (account);
434 folders = tny_simple_list_new ();
435 tny_folder_store_get_folders (store, folders, NULL, &error);
438 g_object_unref (folders);
442 iter = tny_list_create_iterator (folders);
443 while (!tny_iterator_is_done (iter)) {
447 folder = TNY_FOLDER (tny_iterator_get_current (iter));
449 res = modest_search_folder (folder, search);
455 hits = g_list_concat (hits, res);
459 g_object_unref (folder);
460 tny_iterator_next (iter);
463 g_object_unref (iter);
464 g_object_unref (folders);
470 modest_search_all_accounts (ModestSearch *search)
472 ModestTnyAccountStore *astore;
478 astore = modest_runtime_get_account_store ();
480 accounts = tny_simple_list_new ();
481 tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
483 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
485 iter = tny_list_create_iterator (accounts);
486 while (!tny_iterator_is_done (iter)) {
490 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
492 g_debug ("Searching account %s",
493 tny_account_get_name (account));
494 res = modest_search_account (account, search);
501 hits = g_list_concat (hits, res);
505 g_object_unref (account);
506 tny_iterator_next (iter);
509 g_object_unref (accounts);
510 g_object_unref (iter);