c8d82c09f95e656d4db6a405e13bd1556ed580bb
[modest] / src / modest-search.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 #ifndef _GNU_SOURCE
31 #define _GNU_SOURCE
32 #endif
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <string.h>
39
40 #include <tny-shared.h>
41 #include <tny-folder.h>
42 #include <tny-folder-store.h>
43 #include <tny-list.h>
44 #include <tny-iterator.h>
45 #include <tny-simple-list.h>
46
47 #include <libmodest-dbus-client/libmodest-dbus-client.h>
48
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"
55
56 static gchar *
57 g_strdup_or_null (const gchar *str)
58 {
59         gchar *string = NULL;
60
61         if  (str != NULL) {
62                 string = g_strdup (str);
63         }
64
65         return string;
66 }
67
68 static GList*
69 add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
70 {
71         ModestSearchHit *hit;
72         TnyHeaderFlags   flags;
73         char            *furl;
74         char            *msg_url;
75         const char      *uid;
76         const char      *subject;
77         const char      *sender;
78
79         hit = g_slice_new0 (ModestSearchHit);
80
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);
86
87         flags = tny_header_get_flags (header);
88
89         hit->msgid = msg_url;
90         hit->subject = g_strdup_or_null (subject);
91         hit->sender = g_strdup_or_null (sender);
92         hit->folder = furl;
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);
97
98         return g_list_prepend (list, hit);
99 }
100
101 static gboolean
102 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
103 {
104         gsize _nread;
105         gssize res;
106
107         _nread = 0;
108         while (_nread < count) {
109                 res = tny_stream_read (stream,
110                                        buffer + _nread, 
111                                        count - _nread);
112                 if (res == -1) {
113                         *nread = _nread;
114                         return FALSE;
115                 }
116
117                 if (res == 0)
118                         break;
119
120                 _nread += res;
121         }
122
123         *nread = _nread;
124         return TRUE;
125
126
127 }
128
129 #ifdef MODEST_HAVE_OGS
130 static gboolean
131 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
132 {
133         TnyStream *stream;
134         char       buffer[4096];
135         gsize      len;
136         gsize      nread;
137         gboolean   is_html = FALSE;
138         gboolean   found;
139         gboolean   res;
140
141
142         if (! tny_mime_part_content_type_is (part, "text/ *") ||
143             ! (is_html = tny_mime_part_content_type_is (part, "text/html"))) {
144                 return FALSE;
145         }
146
147         found = FALSE;
148         len = sizeof (buffer);
149         stream = tny_mime_part_get_stream (part);
150
151         while ((res = read_chunk (stream, buffer, len, &nread))) {
152
153                 if (is_html) {
154
155                         found = ogs_text_searcher_search_html (search->text_searcher,
156                                                                buffer,
157                                                                nread,
158                                                                nread < len);
159                 } else {
160                         found = ogs_text_searcher_search_text (search->text_searcher,
161                                                                buffer,
162                                                                nread);
163                 }
164
165                 if (found) {
166                         break;
167                 }
168
169         }
170
171         if (!found) {
172                 found = ogs_text_searcher_search_done (search->text_searcher);
173         }
174
175         ogs_text_searcher_reset (search->text_searcher);
176
177         return found;
178 }
179 #endif /*MODEST_HAVE_OGS*/
180
181 static gboolean
182 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
183 {
184         TnyStream *stream;
185         char       buffer[8193];
186         char      *chunk[2];
187         gsize      len;
188         gssize     nread;
189         gboolean   found;
190         gboolean   res;
191
192         if (! tny_mime_part_content_type_is (part, "text/ *")) {
193                 return FALSE;
194         }
195
196         found = FALSE;
197         len = (sizeof (buffer) - 1) / 2;
198
199         if (strlen (search->body) > len) {
200                 g_warning ("Search term bigger then chunk."
201                            "We might not find everything");     
202         }
203
204         stream = tny_mime_part_get_stream (part);
205
206         memset (buffer, 0, sizeof (buffer));
207         chunk[0] = buffer;
208         chunk[1] = buffer + len;
209
210         res = read_chunk (stream, chunk[0], len, &nread);
211
212         if (res == FALSE) {
213                 goto done;
214         }
215
216         found = !modest_text_utils_utf8_strcmp (search->body,
217                                                 buffer,
218                                                 TRUE);
219         if (found) {
220                 goto done;
221         }
222
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.
233          *
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)
237          * */   
238         while ((res = read_chunk (stream, chunk[1], len, &nread))) {
239                 buffer[len + nread] = '\0';
240
241                 found = !modest_text_utils_utf8_strcmp (search->body,
242                                                         buffer,
243                                                         TRUE);
244
245                 if (found) {
246                         break;
247                 }
248
249                 /* also move the \0 */
250                 g_memmove (chunk[0], chunk[1], len + 1);
251         }
252
253 done:
254         g_object_unref (stream);
255         return found;
256 }
257
258 static gboolean
259 search_string (const char      *what,
260                const char      *where,
261                ModestSearch    *search)
262 {
263         gboolean found;
264 #ifdef MODEST_HAVE_OGS
265         if (search->flags & MODEST_SEARCH_USE_OGS) {
266                 found = ogs_text_searcher_search_text (search->text_searcher,
267                                                        where,
268                                                        strlen (where));
269
270                 ogs_text_searcher_reset (search->text_searcher);
271         } else {
272 #endif
273                 if (what == NULL || where == NULL) {
274                         return FALSE;
275                 }
276
277                 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
278 #ifdef MODEST_HAVE_OGS
279         }
280 #endif
281         return found;
282 }
283
284
285
286 /**
287  * modest_search:
288  * @folder: a #TnyFolder instance
289  * @search: a #ModestSearch query
290  *
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.
293  **/
294 GList *
295 modest_search_folder (TnyFolder *folder, ModestSearch *search)
296 {
297         GList *retval = NULL;
298         TnyIterator *iter;
299         TnyList *list;
300         gboolean (*part_search_func) (TnyMimePart *part, ModestSearch *search);
301
302         part_search_func = search_mime_part_strcmp;
303
304 #ifdef MODEST_HAVE_OGS
305         if (search->flags & MODEST_SEARCH_USE_OGS) {
306         
307                 if (search->text_searcher == NULL && search->query != NULL) {
308                         OgsTextSearcher *text_searcher; 
309
310                         text_searcher = ogs_text_searcher_new (FALSE);
311                         ogs_text_searcher_parse_query (text_searcher, search->query);
312                         search->text_searcher = text_searcher;
313                 }
314
315                 part_search_func = search_mime_part_ogs;
316         }
317 #endif
318
319         list = tny_simple_list_new ();
320         tny_folder_get_headers (folder, list, FALSE, NULL);
321
322         iter = tny_list_create_iterator (list);
323
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;
328                 
329                 if (search->flags & MODEST_SEARCH_BEFORE)
330                         if (!(t <= search->before))
331                                 goto go_next;
332
333                 if (search->flags & MODEST_SEARCH_AFTER)
334                         if (!(t >= search->after))
335                                 goto go_next;
336
337                 if (search->flags & MODEST_SEARCH_SIZE)
338                         if (tny_header_get_message_size (cur) < search->minsize)
339                                 goto go_next;
340
341                 if (search->flags & MODEST_SEARCH_SUBJECT) {
342                         const char *str = tny_header_get_subject (cur);
343
344                         if ((found = search_string (search->subject, str, search))) {
345                             retval = add_hit (retval, cur, folder);
346                         }
347                 }
348                 
349                 if (!found && search->flags & MODEST_SEARCH_SENDER) {
350                         const char *str = tny_header_get_from (cur);
351
352                         if ((found = search_string (search->from, str, search))) {
353                                 retval = add_hit (retval, cur, folder);
354                         }
355                 }
356                 
357                 if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
358                         const char *str = tny_header_get_to (cur);
359
360                         if ((found = search_string (search->recipient, str, search))) {
361                                 retval = add_hit (retval, cur, folder);
362                         }
363                 }
364         
365                 if (!found && search->flags & MODEST_SEARCH_BODY) {
366                         TnyHeaderFlags flags;
367                         GError      *err = NULL;
368                         TnyMsg      *msg = NULL;
369                         TnyIterator *piter;
370                         TnyList     *parts;
371
372                         flags = tny_header_get_flags (cur);
373
374                         if (!(flags & TNY_HEADER_FLAG_CACHED)) {
375                                 goto go_next;
376                         }
377
378                         msg = tny_folder_get_msg (folder, cur, &err);
379
380                         if (err != NULL || msg == NULL) {
381                                 g_warning ("Could not get message\n");
382                                 g_error_free (err);
383
384                                 if (msg) {
385                                         g_object_unref (msg);
386                                 }
387                         }       
388
389                         parts = tny_simple_list_new ();
390                         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
391
392                         piter = tny_list_create_iterator (parts);
393                         while (!found && !tny_iterator_is_done (piter)) {
394                                 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
395
396                                 if ((found = part_search_func (pcur, search))) {
397                                         retval = add_hit (retval, cur, folder);                         
398                                 }
399
400                                 g_object_unref (pcur);
401                                 tny_iterator_next (piter);
402                         }
403
404                         g_object_unref (piter);
405                         g_object_unref (parts);
406                         g_object_unref (msg);
407
408                 }
409
410 go_next:
411                 g_object_unref (cur);
412                 tny_iterator_next (iter);
413         }
414
415         g_object_unref (iter);
416         g_object_unref (list);
417         return retval;
418 }
419
420 GList *
421 modest_search_account (TnyAccount *account, ModestSearch *search)
422 {
423         TnyFolderStore      *store;
424         TnyIterator         *iter;
425         TnyList             *folders;
426         GList               *hits;
427         GError              *error;
428
429         error = NULL;
430         hits = NULL;
431
432         store = TNY_FOLDER_STORE (account);
433
434         folders = tny_simple_list_new ();
435         tny_folder_store_get_folders (store, folders, NULL, &error);
436         
437         if (error != NULL) {
438                 g_object_unref (folders);
439                 return NULL;
440         }
441
442         iter = tny_list_create_iterator (folders);
443         while (!tny_iterator_is_done (iter)) {
444                 TnyFolder *folder;
445                 GList     *res;
446
447                 folder = TNY_FOLDER (tny_iterator_get_current (iter));
448                 
449                 res = modest_search_folder (folder, search);
450
451                 if (res != NULL) {
452                         if (hits == NULL) {
453                                 hits = res;
454                         } else {
455                                 hits = g_list_concat (hits, res);
456                         }
457                 }
458
459                 g_object_unref (folder);
460                 tny_iterator_next (iter);
461         }
462
463         g_object_unref (iter);
464         g_object_unref (folders);
465
466         return hits;
467 }
468
469 GList *
470 modest_search_all_accounts (ModestSearch *search)
471 {
472         ModestTnyAccountStore *astore;
473         TnyList               *accounts;
474         TnyIterator           *iter;
475         GList                 *hits;
476
477         hits = NULL;
478         astore = modest_runtime_get_account_store ();
479
480         accounts = tny_simple_list_new ();
481         tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
482                                         accounts,
483                                         TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
484
485         iter = tny_list_create_iterator (accounts);
486         while (!tny_iterator_is_done (iter)) {
487                 TnyAccount *account;
488                 GList      *res;
489
490                 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
491
492                 g_debug ("Searching account %s",
493                          tny_account_get_name (account));
494                 res = modest_search_account (account, search);
495                 
496                 if (res != NULL) {
497
498                         if (hits == NULL) {
499                                 hits = res;
500                         } else {
501                                 hits = g_list_concat (hits, res);
502                         }
503                 }
504
505                 g_object_unref (account);
506                 tny_iterator_next (iter);
507         }
508
509         g_object_unref (accounts);
510         g_object_unref (iter);
511
512         return hits;
513 }
514
515