9 #include <tny-shared.h>
10 #include <tny-folder.h>
11 #include <tny-folder-store.h>
13 #include <tny-iterator.h>
14 #include <tny-simple-list.h>
16 #include "modest-text-utils.h"
17 #include "modest-account-mgr.h"
18 #include "modest-tny-account-store.h"
19 #include "modest-tny-account.h"
20 #include "modest-search.h"
21 #include "modest-runtime.h"
24 add_header (GList *list, TnyHeader *header, TnyFolder *folder)
28 /* TODO: we need this call otherwise it will crash later
29 * when we try to do that call again without having the
30 * folder around, I guess that is a bug in TinyThingy */
31 f = tny_header_get_folder (header);
33 return g_list_prepend (list, g_object_ref (header));
37 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
43 while (_nread < count) {
44 res = tny_stream_read (stream,
64 #ifdef MODEST_HAVE_OGS
66 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
72 gboolean is_html = FALSE;
77 if (! tny_mime_part_content_type_is (part, "text/ *") ||
78 ! (is_html = tny_mime_part_content_type_is (part, "text/html"))) {
83 len = sizeof (buffer);
84 stream = tny_mime_part_get_stream (part);
86 while ((res = read_chunk (stream, buffer, len, &nread))) {
90 found = ogs_text_searcher_search_html (search->text_searcher,
95 found = ogs_text_searcher_search_text (search->text_searcher,
107 found = ogs_text_searcher_search_done (search->text_searcher);
110 ogs_text_searcher_reset (search->text_searcher);
117 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
127 if (! tny_mime_part_content_type_is (part, "text/ *")) {
132 len = (sizeof (buffer) - 1) / 2;
134 if (strlen (search->body) > len) {
135 g_warning ("Search term bigger then chunk."
136 "We might not find everything");
139 stream = tny_mime_part_get_stream (part);
141 memset (buffer, 0, sizeof (buffer));
143 chunk[1] = buffer + len;
145 res = read_chunk (stream, chunk[0], len, &nread);
151 found = !modest_text_utils_utf8_strcmp (search->body,
158 /* This works like this:
159 * buffer: [ooooooooooo|xxxxxxxxxxxx|\0]
160 * ^chunk[0] ^chunk[1]
161 * we have prefilled chunk[0] now we always read into chunk[1]
162 * and then move the content of chunk[1] to chunk[0].
163 * The idea is to prevent not finding search terms that are
164 * spread across 2 reads:
165 * buffer: [ooooooooTES|Txxxxxxxxxxx|\0]
166 * We should catch that because we always search the whole
167 * buffer not only the chunks.
169 * Of course that breaks for search terms > sizeof (chunk)
170 * but sizeof (chunk) should be big enough I guess (see
171 * the g_warning in this function)
173 while ((res = read_chunk (stream, chunk[1], len, &nread))) {
174 buffer[len + nread] = '\0';
176 found = !modest_text_utils_utf8_strcmp (search->body,
184 /* also move the \0 */
185 g_memmove (chunk[0], chunk[1], len + 1);
189 g_object_unref (stream);
194 search_string (const char *what,
196 ModestSearch *search)
199 #ifdef MODEST_HAVE_OGS
200 if (search->flags & MODEST_SEARCH_USE_OGS) {
201 found = ogs_text_searcher_search_text (search->text_searcher,
205 ogs_text_searcher_reset (search->text_searcher);
208 if (what == NULL || where == NULL) {
212 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
213 #ifdef MODEST_HAVE_OGS
223 * @folder: a #TnyFolder instance
224 * @search: a #ModestSearch query
226 * This operation will search @folder for headers that match the query @search.
227 * It will return a doubly linked list with URIs that point to the message.
230 modest_search_folder (TnyFolder *folder, ModestSearch *search)
232 GList *retval = NULL;
235 gboolean (*part_search_func) (TnyMimePart *part, ModestSearch *search);
237 part_search_func = search_mime_part_strcmp;
239 #ifdef MODEST_HAVE_OGS
240 if (search->flags & MODEST_SEARCH_USE_OGS) {
242 if (search->text_searcher == NULL && search->query != NULL) {
243 OgsTextSearcher *text_searcher;
245 text_searcher = ogs_text_searcher_new (FALSE);
246 ogs_text_searcher_parse_query (text_searcher, search->query);
247 search->text_searcher = text_searcher;
250 part_search_func = search_mime_part_ogs;
254 list = tny_simple_list_new ();
255 tny_folder_get_headers (folder, list, FALSE, NULL);
257 iter = tny_list_create_iterator (list);
259 while (!tny_iterator_is_done (iter)) {
260 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
261 time_t t = tny_header_get_date_sent (cur);
262 gboolean found = FALSE;
264 if (search->flags & MODEST_SEARCH_BEFORE)
265 if (!(t <= search->before))
268 if (search->flags & MODEST_SEARCH_AFTER)
269 if (!(t >= search->after))
272 if (search->flags & MODEST_SEARCH_SIZE)
273 if (tny_header_get_message_size (cur) < search->minsize)
276 if (search->flags & MODEST_SEARCH_SUBJECT) {
277 const char *str = tny_header_get_subject (cur);
279 if ((found = search_string (search->subject, str, search))) {
280 retval = add_header (retval, cur, folder);
284 if (!found && search->flags & MODEST_SEARCH_SENDER) {
285 const char *str = tny_header_get_from (cur);
287 if ((found = search_string (search->from, str, search))) {
288 retval = add_header (retval, cur, folder);
292 if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
293 const char *str = tny_header_get_to (cur);
295 if ((found = search_string (search->recipient, str, search))) {
296 retval = add_header (retval, cur, folder);
300 if (!found && search->flags & MODEST_SEARCH_BODY) {
301 TnyHeaderFlags flags;
307 flags = tny_header_get_flags (cur);
309 if (!(flags & TNY_HEADER_FLAG_CACHED)) {
313 msg = tny_folder_get_msg (folder, cur, &err);
315 if (err != NULL || msg == NULL) {
316 g_warning ("Could not get message\n");
320 g_object_unref (msg);
324 parts = tny_simple_list_new ();
325 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
327 piter = tny_list_create_iterator (parts);
328 while (!found && !tny_iterator_is_done (piter)) {
329 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
331 if ((found = part_search_func (pcur, search))) {
332 retval = add_header (retval, cur, folder);
335 g_object_unref (pcur);
336 tny_iterator_next (piter);
339 g_object_unref (piter);
340 g_object_unref (parts);
341 g_object_unref (msg);
345 g_object_unref (cur);
346 tny_iterator_next (iter);
349 g_object_unref (iter);
350 g_object_unref (list);
355 modest_search_account (TnyAccount *account, ModestSearch *search)
357 TnyFolderStore *store;
366 store = TNY_FOLDER_STORE (account);
368 folders = tny_simple_list_new ();
369 tny_folder_store_get_folders (store, folders, NULL, &error);
372 g_object_unref (folders);
376 iter = tny_list_create_iterator (folders);
377 while (!tny_iterator_is_done (iter)) {
381 folder = TNY_FOLDER (tny_iterator_get_current (iter));
383 res = modest_search_folder (folder, search);
389 hits = g_list_concat (hits, res);
393 g_object_unref (folder);
394 tny_iterator_next (iter);
397 g_object_unref (iter);
398 g_object_unref (folders);
404 modest_search_all_accounts (ModestSearch *search)
406 ModestAccountMgr *account_mgr;
407 ModestTnyAccountStore *astore;
412 account_mgr = modest_runtime_get_account_mgr ();
414 accounts = modest_account_mgr_account_names (account_mgr, FALSE);
415 astore = modest_runtime_get_account_store ();
418 for (iter = accounts; iter; iter = iter->next) {
421 TnyAccount *account = NULL;
423 ac_name = (const char *) iter->data;
425 account = modest_tny_account_store_get_tny_account_by_account (astore,
427 TNY_ACCOUNT_TYPE_STORE);
429 if (account == NULL) {
430 g_warning ("Could not get account for %s", ac_name);
434 res = modest_search_account (account, search);
441 hits = g_list_concat (hits, res);