2007-06-20 Murray Cumming <murrayc@murrayc-desktop>
[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
69 static GList*
70 add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
71 {
72         ModestSearchHit *hit;
73         TnyHeaderFlags   flags;
74         char            *furl;
75         char            *msg_url;
76         const char      *uid;
77         const char      *subject;
78         const char      *sender;
79
80         hit = g_slice_new0 (ModestSearchHit);
81
82         furl = tny_folder_get_url_string (folder);
83         printf ("DEBUG: %s: folder URL=%s\n", __FUNCTION__, furl);
84         if (!furl) {
85                 g_warning ("%s: tny_folder_get_url_string(): returned NULL for folder. Folder name=%s\n", __FUNCTION__, tny_folder_get_name (folder));
86         }
87         
88         /* Make sure that we use the short UID instead of the long UID,
89          * and/or find out what UID form is used when finding, in camel_data_cache_get().
90          * so we can find what we get. Philip is working on this.
91          */
92         uid = tny_header_get_uid (header);
93         if (!furl) {
94                 g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, tny_header_get_subject (header));
95         }
96         
97         msg_url = g_strdup_printf ("%s/%s", furl, uid);
98         g_free (furl);
99         
100         subject = tny_header_get_subject (header);
101         sender = tny_header_get_from (header);
102         
103         flags = tny_header_get_flags (header);
104
105         hit->msgid = msg_url;
106         hit->subject = g_strdup_or_null (subject);
107         hit->sender = g_strdup_or_null (sender);
108         hit->folder = g_strdup_or_null (tny_folder_get_name (folder));
109         hit->msize = tny_header_get_message_size (header);
110         hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
111         hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
112         hit->timestamp = tny_header_get_date_received (header);
113         
114         return g_list_prepend (list, hit);
115 }
116
117 static gboolean
118 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
119 {
120         gsize _nread;
121         gssize res;
122
123         _nread = 0;
124         while (_nread < count) {
125                 res = tny_stream_read (stream,
126                                        buffer + _nread, 
127                                        count - _nread);
128                 if (res == -1) {
129                         *nread = _nread;
130                         return FALSE;
131                 }
132
133                 if (res == 0)
134                         break;
135
136                 _nread += res;
137         }
138
139         *nread = _nread;
140         return TRUE;
141 }
142
143 #ifdef MODEST_HAVE_OGS
144 static gboolean
145 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
146 {
147         TnyStream *stream;
148         char       buffer[4096];
149         gsize      len;
150         gsize      nread;
151         gboolean   is_html = FALSE;
152         gboolean   found;
153         gboolean   res;
154
155
156         if (! tny_mime_part_content_type_is (part, "text/*") ||
157             ! (is_html = tny_mime_part_content_type_is (part, "text/html"))) {
158             g_debug ("%s: No text or html MIME part found.\n", __FUNCTION__);
159                 return FALSE;
160         }
161
162         found = FALSE;
163         len = sizeof (buffer);
164         stream = tny_mime_part_get_stream (part);
165
166         while ((res = read_chunk (stream, buffer, len, &nread))) {
167
168                 /* search->text_searcher was instantiated in modest_search_folder(). */
169                 
170                 if (is_html) {
171
172                         found = ogs_text_searcher_search_html (search->text_searcher,
173                                                                buffer,
174                                                                nread,
175                                                                nread < len);
176                 } else {
177                         found = ogs_text_searcher_search_text (search->text_searcher,
178                                                                buffer,
179                                                                nread);
180                 }
181
182                 if (found) {
183                         break;
184                 }
185
186         }
187
188         if (!found) {
189                 found = ogs_text_searcher_search_done (search->text_searcher);
190         }
191
192         ogs_text_searcher_reset (search->text_searcher);
193         
194         /* debug stuff:
195         if (!found) {
196                 buffer[len -1] = 0;
197                 printf ("DEBUG: %s: query %s was not found in message text: %s\n", 
198                         __FUNCTION__, search->query, buffer);   
199                 
200         } else {
201                 printf ("DEBUG: %s: found.\n", __FUNCTION__);   
202         }
203         */
204
205         return found;
206 }
207
208 #else
209
210 static gboolean
211 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
212 {
213         TnyStream *stream;
214         char       buffer[8193];
215         char      *chunk[2];
216         gssize     len;
217         gsize     nread;
218         gboolean   found;
219         gboolean   res;
220
221         if (! tny_mime_part_content_type_is (part, "text/*")) {
222                 g_debug ("%s: No text MIME part found.\n", __FUNCTION__);
223                 return FALSE;
224         }
225
226         found = FALSE;
227         len = (sizeof (buffer) - 1) / 2;
228
229         if (strlen (search->body) > len) {
230                 g_warning ("Search term bigger then chunk."
231                            "We might not find everything");     
232         }
233
234         stream = tny_mime_part_get_stream (part);
235
236         memset (buffer, 0, sizeof (buffer));
237         chunk[0] = buffer;
238         chunk[1] = buffer + len;
239
240         res = read_chunk (stream, chunk[0], len, &nread);
241
242         if (res == FALSE) {
243                 goto done;
244         }
245
246         found = !modest_text_utils_utf8_strcmp (search->body,
247                                                 buffer,
248                                                 TRUE);
249         if (found) {
250                 goto done;
251         }
252
253         /* This works like this:
254          * buffer: [ooooooooooo|xxxxxxxxxxxx|\0] 
255          *          ^chunk[0]  ^chunk[1]
256          * we have prefilled chunk[0] now we always read into chunk[1]
257          * and then move the content of chunk[1] to chunk[0].
258          * The idea is to prevent not finding search terms that are
259          * spread across 2 reads:        
260          * buffer: [ooooooooTES|Txxxxxxxxxxx|\0] 
261          * We should catch that because we always search the whole
262          * buffer not only the chunks.
263          *
264          * Of course that breaks for search terms > sizeof (chunk)
265          * but sizeof (chunk) should be big enough I guess (see
266          * the g_warning in this function)
267          * */   
268         while ((res = read_chunk (stream, chunk[1], len, &nread))) {
269                 buffer[len + nread] = '\0';
270
271                 found = !modest_text_utils_utf8_strcmp (search->body,
272                                                         buffer,
273                                                         TRUE);
274
275                 if (found) {
276                         break;
277                 }
278
279                 /* also move the \0 */
280                 g_memmove (chunk[0], chunk[1], len + 1);
281         }
282
283 done:
284         g_object_unref (stream);
285         return found;
286 }
287 #endif /*MODEST_HAVE_OGS*/
288
289 static gboolean
290 search_string (const char      *what,
291                const char      *where,
292                ModestSearch    *search)
293 {
294         gboolean found;
295 #ifdef MODEST_HAVE_OGS
296         if (search->flags & MODEST_SEARCH_USE_OGS) {
297                 found = ogs_text_searcher_search_text (search->text_searcher,
298                                                        where,
299                                                        strlen (where));
300
301                 ogs_text_searcher_reset (search->text_searcher);
302         } else {
303 #endif
304                 if (what == NULL || where == NULL) {
305                         return FALSE;
306                 }
307
308                 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
309 #ifdef MODEST_HAVE_OGS
310         }
311 #endif
312         return found;
313 }
314
315
316
317 /**
318  * modest_search:
319  * @folder: a #TnyFolder instance
320  * @search: a #ModestSearch query
321  *
322  * This operation will search @folder for headers that match the query @search.
323  * It will return a doubly linked list with URIs that point to the message.
324  **/
325 GList *
326 modest_search_folder (TnyFolder *folder, ModestSearch *search)
327 {
328         GList *retval = NULL;
329         TnyIterator *iter = NULL;
330         TnyList *list = NULL;
331         
332 #ifdef MODEST_HAVE_OGS
333         if (search->flags & MODEST_SEARCH_USE_OGS) {
334         
335                 if (search->text_searcher == NULL && search->query != NULL) {
336                         OgsTextSearcher *text_searcher; 
337
338                         text_searcher = ogs_text_searcher_new (FALSE);
339                         ogs_text_searcher_parse_query (text_searcher, search->query);
340                         search->text_searcher = text_searcher;
341                 }
342         }
343 #endif
344
345         list = tny_simple_list_new ();
346         GError *error = NULL;
347         tny_folder_get_headers (folder, list, FALSE /* don't refresh */, &error);
348         if (error) {
349                 g_warning ("%s: tny_folder_get_headers() failed with error=%s.\n", 
350                 __FUNCTION__, error->message);
351                 g_error_free (error);
352                 error = NULL;   
353         }
354
355         iter = tny_list_create_iterator (list);
356
357         while (!tny_iterator_is_done (iter)) {
358                 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
359                 time_t t = tny_header_get_date_sent (cur);
360                 gboolean found = FALSE;
361                 
362                 if (search->flags & MODEST_SEARCH_BEFORE)
363                         if (!(t <= search->before))
364                                 goto go_next;
365
366                 if (search->flags & MODEST_SEARCH_AFTER)
367                         if (!(t >= search->after))
368                                 goto go_next;
369
370                 if (search->flags & MODEST_SEARCH_SIZE)
371                         if (tny_header_get_message_size (cur) < search->minsize)
372                                 goto go_next;
373
374                 if (search->flags & MODEST_SEARCH_SUBJECT) {
375                         const char *str = tny_header_get_subject (cur);
376
377                         if ((found = search_string (search->subject, str, search))) {
378                             retval = add_hit (retval, cur, folder);
379                         }
380                 }
381                 
382                 if (!found && search->flags & MODEST_SEARCH_SENDER) {
383                         char *str = g_strdup (tny_header_get_from (cur));
384
385                         if ((found = search_string (search->from, (const gchar *) str, search))) {
386                                 retval = add_hit (retval, cur, folder);
387                         }
388                         g_free (str);
389                 }
390                 
391                 if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
392                         const char *str = tny_header_get_to (cur);
393
394                         if ((found = search_string (search->recipient, str, search))) {
395                                 retval = add_hit (retval, cur, folder);
396                         }
397                 }
398         
399                 if (!found && search->flags & MODEST_SEARCH_BODY) {
400                         TnyHeaderFlags flags;
401                         GError      *err = NULL;
402                         TnyMsg      *msg = NULL;
403                         TnyIterator *piter;
404                         TnyList     *parts;
405
406                         flags = tny_header_get_flags (cur);
407
408                         if (!(flags & TNY_HEADER_FLAG_CACHED)) {
409                                 goto go_next;
410                         }
411
412                         msg = tny_folder_get_msg (folder, cur, &err);
413
414                         if (err != NULL || msg == NULL) {
415                                 g_warning ("%s: Could not get message.\n", __FUNCTION__);
416                                 g_error_free (err);
417
418                                 if (msg) {
419                                         g_object_unref (msg);
420                                 }
421                         }       
422
423                         parts = tny_simple_list_new ();
424                         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
425                         
426                         if (tny_list_get_length(parts) == 0) {
427                                 gchar *url_string = tny_msg_get_url_string (msg);
428                                 g_debug ("DEBUG: %s: tny_mime_part_get_parts(msg) returned an empty list for message url=%s", 
429                                         __FUNCTION__, url_string);
430                                 g_free (url_string);    
431                         }
432
433                         piter = tny_list_create_iterator (parts);
434                         while (!found && !tny_iterator_is_done (piter)) {
435                                 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
436
437                                 #ifdef MODEST_HAVE_OGS
438                                 found = search_mime_part_ogs (pcur, search);
439                                 #else
440                                 found = search_mime_part_strcmp (pcur, search);
441                                 #endif
442                 
443                                 if (found) {
444                                         retval = add_hit (retval, cur, folder);                         
445                                 }
446
447                                 g_object_unref (pcur);
448                                 tny_iterator_next (piter);
449                         }
450
451                         g_object_unref (piter);
452                         g_object_unref (parts);
453                         g_object_unref (msg);
454
455                 }
456
457 go_next:
458                 g_object_unref (cur);
459                 tny_iterator_next (iter);
460         }
461
462         g_object_unref (iter);
463         g_object_unref (list);
464         return retval;
465 }
466
467 GList *
468 modest_search_account (TnyAccount *account, ModestSearch *search)
469 {
470         TnyFolderStore      *store;
471         TnyIterator         *iter;
472         TnyList             *folders;
473         GList               *hits;
474         GError              *error;
475
476         error = NULL;
477         hits = NULL;
478
479         store = TNY_FOLDER_STORE (account);
480
481         folders = tny_simple_list_new ();
482         tny_folder_store_get_folders (store, folders, NULL, &error);
483         
484         if (error != NULL) {
485                 g_object_unref (folders);
486                 return NULL;
487         }
488
489         iter = tny_list_create_iterator (folders);
490         while (!tny_iterator_is_done (iter)) {
491                 TnyFolder *folder;
492                 GList     *res;
493
494                 folder = TNY_FOLDER (tny_iterator_get_current (iter));
495                 /* g_debug ("DEBUG: %s: searching folder %s.", 
496                         __FUNCTION__, tny_folder_get_name (folder)); */
497                 
498                 res = modest_search_folder (folder, search);
499
500                 if (res != NULL) {
501                         if (hits == NULL) {
502                                 hits = res;
503                         } else {
504                                 hits = g_list_concat (hits, res);
505                         }
506                 }
507
508                 g_object_unref (folder);
509                 tny_iterator_next (iter);
510         }
511
512         g_object_unref (iter);
513         g_object_unref (folders);
514
515         return hits;
516 }
517
518 GList *
519 modest_search_all_accounts (ModestSearch *search)
520 {
521         ModestTnyAccountStore *astore;
522         TnyList               *accounts;
523         TnyIterator           *iter;
524         GList                 *hits;
525
526         hits = NULL;
527         astore = modest_runtime_get_account_store ();
528
529         accounts = tny_simple_list_new ();
530         tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
531                                         accounts,
532                                         TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
533
534         iter = tny_list_create_iterator (accounts);
535         while (!tny_iterator_is_done (iter)) {
536                 TnyAccount *account;
537                 GList      *res;
538
539                 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
540
541                 /* g_debug ("DEBUG: %s: Searching account %s",
542                          __FUNCTION__, tny_account_get_name (account)); */
543                 res = modest_search_account (account, search);
544                 
545                 if (res != NULL) {
546
547                         if (hits == NULL) {
548                                 hits = res;
549                         } else {
550                                 hits = g_list_concat (hits, res);
551                         }
552                 }
553
554                 g_object_unref (account);
555                 tny_iterator_next (iter);
556         }
557
558         g_object_unref (accounts);
559         g_object_unref (iter);
560
561         return hits;
562 }
563
564