* set the folder name in instead of the folder-url for the search results
[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         if (!furl) {
84                 g_warning ("%s: tny_folder_get_url_string(): returned NULL for folder. Folder name=%s\n", __FUNCTION__, tny_folder_get_name (folder));
85         }
86         
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.
90          */
91         uid = tny_header_get_uid (header);
92         if (!furl) {
93                 g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, tny_header_get_subject (header));
94         }
95         
96         msg_url = g_strdup_printf ("%s/%s", furl, uid);
97         
98         subject = tny_header_get_subject (header);
99         sender = tny_header_get_from (header);
100         
101         flags = tny_header_get_flags (header);
102
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);
112         
113         return g_list_prepend (list, hit);
114 }
115
116 static gboolean
117 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
118 {
119         gsize _nread;
120         gssize res;
121
122         _nread = 0;
123         while (_nread < count) {
124                 res = tny_stream_read (stream,
125                                        buffer + _nread, 
126                                        count - _nread);
127                 if (res == -1) {
128                         *nread = _nread;
129                         return FALSE;
130                 }
131
132                 if (res == 0)
133                         break;
134
135                 _nread += res;
136         }
137
138         *nread = _nread;
139         return TRUE;
140
141
142 }
143
144 #ifdef MODEST_HAVE_OGS
145 static gboolean
146 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
147 {
148         TnyStream *stream;
149         char       buffer[4096];
150         gsize      len;
151         gsize      nread;
152         gboolean   is_html = FALSE;
153         gboolean   found;
154         gboolean   res;
155
156
157         if (! tny_mime_part_content_type_is (part, "text/ *") ||
158             ! (is_html = tny_mime_part_content_type_is (part, "text/html"))) {
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                 if (is_html) {
169
170                         found = ogs_text_searcher_search_html (search->text_searcher,
171                                                                buffer,
172                                                                nread,
173                                                                nread < len);
174                 } else {
175                         found = ogs_text_searcher_search_text (search->text_searcher,
176                                                                buffer,
177                                                                nread);
178                 }
179
180                 if (found) {
181                         break;
182                 }
183
184         }
185
186         if (!found) {
187                 found = ogs_text_searcher_search_done (search->text_searcher);
188         }
189
190         ogs_text_searcher_reset (search->text_searcher);
191
192         return found;
193 }
194 #endif /*MODEST_HAVE_OGS*/
195
196 static gboolean
197 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
198 {
199         TnyStream *stream;
200         char       buffer[8193];
201         char      *chunk[2];
202         gssize     len;
203         gsize     nread;
204         gboolean   found;
205         gboolean   res;
206
207         if (! tny_mime_part_content_type_is (part, "text/ *")) {
208                 return FALSE;
209         }
210
211         found = FALSE;
212         len = (sizeof (buffer) - 1) / 2;
213
214         if (strlen (search->body) > len) {
215                 g_warning ("Search term bigger then chunk."
216                            "We might not find everything");     
217         }
218
219         stream = tny_mime_part_get_stream (part);
220
221         memset (buffer, 0, sizeof (buffer));
222         chunk[0] = buffer;
223         chunk[1] = buffer + len;
224
225         res = read_chunk (stream, chunk[0], len, &nread);
226
227         if (res == FALSE) {
228                 goto done;
229         }
230
231         found = !modest_text_utils_utf8_strcmp (search->body,
232                                                 buffer,
233                                                 TRUE);
234         if (found) {
235                 goto done;
236         }
237
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.
248          *
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)
252          * */   
253         while ((res = read_chunk (stream, chunk[1], len, &nread))) {
254                 buffer[len + nread] = '\0';
255
256                 found = !modest_text_utils_utf8_strcmp (search->body,
257                                                         buffer,
258                                                         TRUE);
259
260                 if (found) {
261                         break;
262                 }
263
264                 /* also move the \0 */
265                 g_memmove (chunk[0], chunk[1], len + 1);
266         }
267
268 done:
269         g_object_unref (stream);
270         return found;
271 }
272
273 static gboolean
274 search_string (const char      *what,
275                const char      *where,
276                ModestSearch    *search)
277 {
278         gboolean found;
279 #ifdef MODEST_HAVE_OGS
280         if (search->flags & MODEST_SEARCH_USE_OGS) {
281                 found = ogs_text_searcher_search_text (search->text_searcher,
282                                                        where,
283                                                        strlen (where));
284
285                 ogs_text_searcher_reset (search->text_searcher);
286         } else {
287 #endif
288                 if (what == NULL || where == NULL) {
289                         return FALSE;
290                 }
291
292                 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
293 #ifdef MODEST_HAVE_OGS
294         }
295 #endif
296         return found;
297 }
298
299
300
301 /**
302  * modest_search:
303  * @folder: a #TnyFolder instance
304  * @search: a #ModestSearch query
305  *
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.
308  **/
309 GList *
310 modest_search_folder (TnyFolder *folder, ModestSearch *search)
311 {
312         GList *retval = NULL;
313         TnyIterator *iter;
314         TnyList *list;
315         gboolean (*part_search_func) (TnyMimePart *part, ModestSearch *search);
316
317         part_search_func = search_mime_part_strcmp;
318
319 #ifdef MODEST_HAVE_OGS
320         if (search->flags & MODEST_SEARCH_USE_OGS) {
321         
322                 if (search->text_searcher == NULL && search->query != NULL) {
323                         OgsTextSearcher *text_searcher; 
324
325                         text_searcher = ogs_text_searcher_new (FALSE);
326                         ogs_text_searcher_parse_query (text_searcher, search->query);
327                         search->text_searcher = text_searcher;
328                 }
329
330                 part_search_func = search_mime_part_ogs;
331         }
332 #endif
333
334         list = tny_simple_list_new ();
335         tny_folder_get_headers (folder, list, FALSE, NULL);
336
337         iter = tny_list_create_iterator (list);
338
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;
343                 
344                 if (search->flags & MODEST_SEARCH_BEFORE)
345                         if (!(t <= search->before))
346                                 goto go_next;
347
348                 if (search->flags & MODEST_SEARCH_AFTER)
349                         if (!(t >= search->after))
350                                 goto go_next;
351
352                 if (search->flags & MODEST_SEARCH_SIZE)
353                         if (tny_header_get_message_size (cur) < search->minsize)
354                                 goto go_next;
355
356                 if (search->flags & MODEST_SEARCH_SUBJECT) {
357                         const char *str = tny_header_get_subject (cur);
358
359                         if ((found = search_string (search->subject, str, search))) {
360                             retval = add_hit (retval, cur, folder);
361                         }
362                 }
363                 
364                 if (!found && search->flags & MODEST_SEARCH_SENDER) {
365                         char *str = g_strdup (tny_header_get_from (cur));
366
367                         if ((found = search_string (search->from, (const gchar *) str, search))) {
368                                 retval = add_hit (retval, cur, folder);
369                         }
370                         g_free (str);
371                 }
372                 
373                 if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
374                         const char *str = tny_header_get_to (cur);
375
376                         if ((found = search_string (search->recipient, str, search))) {
377                                 retval = add_hit (retval, cur, folder);
378                         }
379                 }
380         
381                 if (!found && search->flags & MODEST_SEARCH_BODY) {
382                         TnyHeaderFlags flags;
383                         GError      *err = NULL;
384                         TnyMsg      *msg = NULL;
385                         TnyIterator *piter;
386                         TnyList     *parts;
387
388                         flags = tny_header_get_flags (cur);
389
390                         if (!(flags & TNY_HEADER_FLAG_CACHED)) {
391                                 goto go_next;
392                         }
393
394                         msg = tny_folder_get_msg (folder, cur, &err);
395
396                         if (err != NULL || msg == NULL) {
397                                 g_warning ("%s: Could not get message.\n", __FUNCTION__);
398                                 g_error_free (err);
399
400                                 if (msg) {
401                                         g_object_unref (msg);
402                                 }
403                         }       
404
405                         parts = tny_simple_list_new ();
406                         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
407
408                         piter = tny_list_create_iterator (parts);
409                         while (!found && !tny_iterator_is_done (piter)) {
410                                 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
411
412                                 if ((found = part_search_func (pcur, search))) {
413                                         retval = add_hit (retval, cur, folder);                         
414                                 }
415
416                                 g_object_unref (pcur);
417                                 tny_iterator_next (piter);
418                         }
419
420                         g_object_unref (piter);
421                         g_object_unref (parts);
422                         g_object_unref (msg);
423
424                 }
425
426 go_next:
427                 g_object_unref (cur);
428                 tny_iterator_next (iter);
429         }
430
431         g_object_unref (iter);
432         g_object_unref (list);
433         return retval;
434 }
435
436 GList *
437 modest_search_account (TnyAccount *account, ModestSearch *search)
438 {
439         TnyFolderStore      *store;
440         TnyIterator         *iter;
441         TnyList             *folders;
442         GList               *hits;
443         GError              *error;
444
445         error = NULL;
446         hits = NULL;
447
448         store = TNY_FOLDER_STORE (account);
449
450         folders = tny_simple_list_new ();
451         tny_folder_store_get_folders (store, folders, NULL, &error);
452         
453         if (error != NULL) {
454                 g_object_unref (folders);
455                 return NULL;
456         }
457
458         iter = tny_list_create_iterator (folders);
459         while (!tny_iterator_is_done (iter)) {
460                 TnyFolder *folder;
461                 GList     *res;
462
463                 folder = TNY_FOLDER (tny_iterator_get_current (iter));
464                 
465                 res = modest_search_folder (folder, search);
466
467                 if (res != NULL) {
468                         if (hits == NULL) {
469                                 hits = res;
470                         } else {
471                                 hits = g_list_concat (hits, res);
472                         }
473                 }
474
475                 g_object_unref (folder);
476                 tny_iterator_next (iter);
477         }
478
479         g_object_unref (iter);
480         g_object_unref (folders);
481
482         return hits;
483 }
484
485 GList *
486 modest_search_all_accounts (ModestSearch *search)
487 {
488         ModestTnyAccountStore *astore;
489         TnyList               *accounts;
490         TnyIterator           *iter;
491         GList                 *hits;
492
493         hits = NULL;
494         astore = modest_runtime_get_account_store ();
495
496         accounts = tny_simple_list_new ();
497         tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
498                                         accounts,
499                                         TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
500
501         iter = tny_list_create_iterator (accounts);
502         while (!tny_iterator_is_done (iter)) {
503                 TnyAccount *account;
504                 GList      *res;
505
506                 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
507
508                 g_debug ("DEBUG: %s: Searching account %s",
509                          __FUNCTION__, tny_account_get_name (account));
510                 res = modest_search_account (account, search);
511                 
512                 if (res != NULL) {
513
514                         if (hits == NULL) {
515                                 hits = res;
516                         } else {
517                                 hits = g_list_concat (hits, res);
518                         }
519                 }
520
521                 g_object_unref (account);
522                 tny_iterator_next (iter);
523         }
524
525         g_object_unref (accounts);
526         g_object_unref (iter);
527
528         return hits;
529 }
530
531