Add support for setting brand images in message view widgets
[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 #include <tny-camel-imap-store-account.h>
47 #include <tny-camel-pop-store-account.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-tny-mime-part.h"
54 #include "modest-tny-folder.h"
55 #include "modest-search.h"
56 #include "modest-runtime.h"
57 #include "modest-platform.h"
58
59 typedef struct 
60 {
61         guint pending_calls;
62         GList *msg_hits;
63         ModestSearch *search;
64         ModestSearchCallback callback;
65         gpointer user_data;
66         TnyList *all_folders;
67 } SearchHelper;
68
69 static SearchHelper *create_helper (ModestSearchCallback callback, 
70                                     ModestSearch *search,
71                                     gpointer user_data);
72
73 static void          _search_folder (TnyFolder *folder, 
74                                      SearchHelper *helper);
75
76 static gchar *
77 g_strdup_or_null (const gchar *str)
78 {
79         gchar *string = NULL;
80
81         if  (str != NULL) {
82                 string = g_strdup (str);
83         }
84
85         return string;
86 }
87
88 static GList*
89 add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
90 {
91         ModestSearchResultHit *hit;
92         TnyHeaderFlags   flags;
93         char            *furl;
94         char            *msg_url;
95         char      *uid;
96         char      *subject;
97         char      *sender;
98
99         hit = g_slice_new0 (ModestSearchResultHit);
100
101         furl = tny_folder_get_url_string (folder);
102         g_debug ("%s: folder URL=%s\n", __FUNCTION__, furl);
103         if (!furl) {
104                 g_warning ("%s: tny_folder_get_url_string(): returned NULL for folder. Folder name=%s\n", __FUNCTION__, tny_folder_get_name (folder));
105         }
106         
107         /* Make sure that we use the short UID instead of the long UID,
108          * and/or find out what UID form is used when finding, in camel_data_cache_get().
109          * so we can find what we get. Philip is working on this.
110          */
111         uid = tny_header_dup_uid (header);
112         if (!uid) {
113                 gchar *subject = tny_header_dup_subject (header);
114                 g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, subject);
115                 g_free (subject);
116         }
117         
118         msg_url = g_strdup_printf ("%s/%s", furl, uid);
119         g_free (furl);
120         g_free (uid);
121         
122         subject = tny_header_dup_subject (header);
123         sender = tny_header_dup_from (header);
124         
125         flags = tny_header_get_flags (header);
126
127         hit->msgid = msg_url;
128         hit->subject = subject;
129         hit->sender = sender;
130         hit->folder = g_strdup_or_null (tny_folder_get_name (folder));
131         hit->msize = tny_header_get_message_size (header);
132         hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
133         hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
134         hit->timestamp = MIN (tny_header_get_date_received (header), tny_header_get_date_sent (header));
135         
136         return g_list_prepend (list, hit);
137 }
138
139 /** Call this until it returns FALSE or nread is set to 0.
140  * 
141  * @result: FALSE is something failed. */
142 static gboolean
143 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
144 {
145         gsize _nread = 0;
146         gssize res = 0;
147
148         while (_nread < count) {
149                 res = tny_stream_read (stream,
150                                        buffer + _nread, 
151                                        count - _nread);
152                 if (res == -1) { /* error */
153                         *nread = _nread;
154                         return FALSE;
155                 }
156
157                 _nread += res;
158                 
159                 if (res == 0) { /* no more bytes read. */
160                         *nread = _nread;
161                         return TRUE;
162                 }
163         }
164
165         *nread = _nread;
166         return TRUE;
167 }
168
169 #ifdef MODEST_HAVE_OGS
170 /*
171  * This function assumes that the mime part is of type "text / *"
172  */
173 static gboolean
174 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
175 {
176         TnyStream *stream = NULL;
177         char       buffer[4096];
178         const gsize len = sizeof (buffer);
179         gsize      nread = 0;
180         gboolean   is_text_html = FALSE;
181         gboolean   found = FALSE;
182         gboolean   res = FALSE;
183         
184         is_text_html = tny_mime_part_content_type_is (part, "text/html");
185
186         stream = tny_mime_part_get_stream (part);
187
188         res = read_chunk (stream, buffer, len, &nread);
189         while (res && (nread > 0)) {
190                 /* search->text_searcher was instantiated in modest_search_folder(). */
191                 
192                 if (is_text_html) {
193                         found = ogs_text_searcher_search_html (search->text_searcher,
194                                                                buffer,
195                                                                nread,
196                                                                nread < len);
197                 } else {
198                         found = ogs_text_searcher_search_text (search->text_searcher,
199                                                                buffer,
200                                                                nread);
201                 }
202
203                 /* HACK: this helps UI refreshes because the search
204                    operations could be heavy */
205                 while (gtk_events_pending ())
206                         gtk_main_iteration ();
207
208                 if (found) {
209                         break;
210                 }
211                 
212                 nread = 0;
213                 res = read_chunk (stream, buffer, len, &nread);
214         }
215         g_object_unref (stream);
216
217         if (!found) {
218                 found = ogs_text_searcher_search_done (search->text_searcher);
219         }
220
221         ogs_text_searcher_reset (search->text_searcher);
222
223         return found;
224 }
225
226 #else
227
228 /*
229  * This function assumes that the mime part is of type "text / *"
230  */
231 static gboolean
232 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
233 {
234         TnyStream *stream;
235         char       buffer[8193];
236         char      *chunk[2];
237         gssize     len;
238         gsize     nread;
239         gboolean   found;
240         gboolean   res;
241
242         found = FALSE;
243         len = (sizeof (buffer) - 1) / 2;
244
245         if (strlen (search->body) > len) {
246                 g_debug ("Search term bigger then chunk."
247                          "We might not find everything");       
248         }
249
250         stream = tny_mime_part_get_stream (part);
251
252         memset (buffer, 0, sizeof (buffer));
253         chunk[0] = buffer;
254         chunk[1] = buffer + len;
255
256         res = read_chunk (stream, chunk[0], len, &nread);
257
258         if (res == FALSE) {
259                 goto done;
260         }
261
262         found = !modest_text_utils_utf8_strcmp (search->body,
263                                                 buffer,
264                                                 TRUE);
265         if (found) {
266                 goto done;
267         }
268
269         /* This works like this:
270          * buffer: [ooooooooooo|xxxxxxxxxxxx|\0] 
271          *          ^chunk[0]  ^chunk[1]
272          * we have prefilled chunk[0] now we always read into chunk[1]
273          * and then move the content of chunk[1] to chunk[0].
274          * The idea is to prevent not finding search terms that are
275          * spread across 2 reads:        
276          * buffer: [ooooooooTES|Txxxxxxxxxxx|\0] 
277          * We should catch that because we always search the whole
278          * buffer not only the chunks.
279          *
280          * Of course that breaks for search terms > sizeof (chunk)
281          * but sizeof (chunk) should be big enough I guess (see
282          * the g_warning in this function)
283          * */   
284         while ((res = read_chunk (stream, chunk[1], len, &nread))) {
285                 buffer[len + nread] = '\0';
286
287                 found = !modest_text_utils_utf8_strcmp (search->body,
288                                                         buffer,
289                                                         TRUE);
290
291                 /* HACK: this helps UI refreshes because the search
292                    operations could be heavy */
293                 while (gtk_events_pending ())
294                         gtk_main_iteration ();
295
296                 if ((found)||(nread == 0)) {
297                         break;
298                 }
299
300                 /* also move the \0 */
301                 g_memmove (chunk[0], chunk[1], len + 1);
302         }
303
304 done:
305         g_object_unref (stream);
306         return found;
307 }
308 #endif /*MODEST_HAVE_OGS*/
309
310 static gboolean
311 search_string (const char      *what,
312                const char      *where,
313                ModestSearch    *search)
314 {
315         gboolean found = FALSE;
316 #ifdef MODEST_HAVE_OGS
317         if (search->flags & MODEST_SEARCH_USE_OGS) {
318                 found = ogs_text_searcher_search_text (search->text_searcher,
319                                                        where,
320                                                        strlen (where));
321
322                 ogs_text_searcher_reset (search->text_searcher);
323         } else {
324 #endif
325                 if (what == NULL || where == NULL) {
326                         return FALSE;
327                 }
328
329                 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
330 #ifdef MODEST_HAVE_OGS
331         }
332 #endif
333
334         /* HACK: this helps UI refreshes because the search
335            operations could be heavy */
336         while (gtk_events_pending ())
337                 gtk_main_iteration ();
338
339         return found;
340 }
341
342
343 static gboolean 
344 search_mime_part_and_child_parts (TnyMimePart *part, ModestSearch *search)
345 {
346         gboolean found = FALSE;
347
348         /* Do not search into attachments */
349         if (modest_tny_mime_part_is_attachment_for_modest (part) && !TNY_IS_MSG (part))
350                 return FALSE;
351
352         #ifdef MODEST_HAVE_OGS
353         found = search_mime_part_ogs (part, search);
354         #else
355         found = search_mime_part_strcmp (part, search);
356         #endif
357
358         if (found) {    
359                 return found;           
360         }
361         
362         /* Check the child part too, recursively: */
363         TnyList *child_parts = tny_simple_list_new ();
364         tny_mime_part_get_parts (TNY_MIME_PART (part), child_parts);
365
366         TnyIterator *piter = tny_list_create_iterator (child_parts);
367         while (!found && !tny_iterator_is_done (piter)) {
368                 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
369                 if (pcur) {
370                         found = search_mime_part_and_child_parts (pcur, search);
371
372                         g_object_unref (pcur);
373                 }
374
375                 tny_iterator_next (piter);
376         }
377
378         g_object_unref (piter);
379         g_object_unref (child_parts);
380         
381         return found;
382 }
383
384 static void
385 search_next_folder (SearchHelper *helper) 
386 {
387         TnyIterator *iter = tny_list_create_iterator (helper->all_folders);
388         TnyFolder *first = TNY_FOLDER (tny_iterator_get_current (iter));
389         
390         _search_folder (first, helper);
391
392         g_object_unref (first);
393         g_object_unref (iter);
394 }
395
396 static void 
397 modest_search_folder_get_headers_cb (TnyFolder *folder, 
398                                      gboolean cancelled, 
399                                      TnyList *headers, 
400                                      GError *err, 
401                                      gpointer user_data)
402 {
403         TnyIterator *iter = NULL;
404         SearchHelper *helper;
405
406         helper = (SearchHelper *) user_data;
407
408         if (err || cancelled) {
409                 goto end;
410         }
411
412         iter = tny_list_create_iterator (headers);
413
414         while (!tny_iterator_is_done (iter)) {
415
416                 TnyHeader *cur;
417                 time_t t;
418                 gboolean found;
419
420                 cur = (TnyHeader *) tny_iterator_get_current (iter);
421                 t = tny_header_get_date_sent (cur);
422                 found = FALSE;
423
424                 /* Ignore deleted (not yet expunged) emails: */
425                 if (tny_header_get_flags(cur) & TNY_HEADER_FLAG_DELETED)
426                         goto go_next;
427
428                 if (helper->search->flags & MODEST_SEARCH_BEFORE)
429                         if (!(t <= helper->search->end_date))
430                                 goto go_next;
431
432                 if (helper->search->flags & MODEST_SEARCH_AFTER)
433                         if (!(t >= helper->search->start_date))
434                                 goto go_next;
435
436                 if (helper->search->flags & MODEST_SEARCH_SIZE)
437                         if (tny_header_get_message_size (cur) < helper->search->minsize)
438                                 goto go_next;
439
440                 if (helper->search->flags & MODEST_SEARCH_SUBJECT) {
441                         char *str = tny_header_dup_subject (cur);
442
443                         if ((found = search_string (helper->search->subject, str, helper->search))) {
444                             helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
445                         }
446                         g_free (str);
447                 }
448                 
449                 if (!found && helper->search->flags & MODEST_SEARCH_SENDER) {
450                         char *str = tny_header_dup_from (cur);
451
452                         if ((found = search_string (helper->search->from, (const gchar *) str, helper->search))) {
453                                 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
454                         }
455                         g_free (str);
456                 }
457                 
458                 if (!found && helper->search->flags & MODEST_SEARCH_RECIPIENT) {
459                         char *str = tny_header_dup_to (cur);
460
461                         if ((found = search_string (helper->search->recipient, str, helper->search))) {
462                                 helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
463                         }
464                         g_free (str);
465                 }
466         
467                 if (!found && helper->search->flags & MODEST_SEARCH_BODY) {
468                         TnyHeaderFlags flags;
469                         GError      *err = NULL;
470                         TnyMsg      *msg = NULL;
471
472                         flags = tny_header_get_flags (cur);
473
474                         if (!(flags & TNY_HEADER_FLAG_CACHED)) {
475                                 goto go_next;
476                         }
477
478                         msg = tny_folder_get_msg (folder, cur, &err);
479
480                         if (err != NULL || msg == NULL) {
481                                 g_warning ("%s: Could not get message.\n", __FUNCTION__);
482                                 g_error_free (err);
483
484                                 if (msg) {
485                                         g_object_unref (msg);
486                                 }
487                         } else {        
488                                 gchar *str;
489                                 str = tny_header_dup_subject (cur);
490                                 g_debug ("Searching in %s\n", str);
491                                 g_free (str);
492                         
493                                 found = search_mime_part_and_child_parts (TNY_MIME_PART (msg),
494                                                                           helper->search);
495                                 if (found) {
496                                         helper->msg_hits = add_hit (helper->msg_hits, cur, folder);
497                                 }
498                         }
499                         
500                         if (msg)
501                                 g_object_unref (msg);
502                 }
503         go_next:
504                 g_object_unref (cur);
505                 tny_iterator_next (iter);
506         }
507
508         /* Frees */
509         g_object_unref (iter);
510  end:
511         if (headers)
512                 g_object_unref (headers);
513
514         /* Check search finished */
515         tny_list_remove (helper->all_folders, G_OBJECT (folder));
516         if (tny_list_get_length (helper->all_folders) == 0) {
517                 /* callback */
518                 helper->callback (helper->msg_hits, helper->user_data);
519                 
520                 /* free helper */
521                 g_object_unref (helper->all_folders);
522                 g_list_free (helper->msg_hits);
523                 g_slice_free (SearchHelper, helper);
524         } else {
525                 search_next_folder (helper);
526         }
527 }
528
529 static void
530 _search_folder (TnyFolder *folder, 
531                 SearchHelper *helper)
532 {
533         TnyList *list = NULL;
534
535         g_debug ("%s: searching folder %s.", __FUNCTION__, tny_folder_get_name (folder));
536         
537         /* Check that we should be searching this folder. */
538         /* Note that we don't try to search sub-folders. 
539          * Maybe we should, but that should be specified. */
540         if (helper->search->folder && strlen (helper->search->folder)) {
541                 if (!strcmp (helper->search->folder, "outbox")) {
542                         if (modest_tny_folder_guess_folder_type (folder) != TNY_FOLDER_TYPE_OUTBOX) {
543                                 modest_search_folder_get_headers_cb (folder, TRUE, NULL, NULL, helper); 
544                                 return;
545                         }
546                 } else if (strcmp (tny_folder_get_id (folder), helper->search->folder) != 0) {
547                         modest_search_folder_get_headers_cb (folder, TRUE, NULL, NULL, helper); 
548                         return;
549                 }
550         }
551         
552 #ifdef MODEST_HAVE_OGS
553         if (helper->search->flags & MODEST_SEARCH_USE_OGS) {
554         
555                 if (helper->search->text_searcher == NULL && helper->search->query != NULL) {
556                         OgsTextSearcher *text_searcher; 
557
558                         text_searcher = ogs_text_searcher_new (FALSE);
559                         ogs_text_searcher_parse_query (text_searcher, helper->search->query);
560                         helper->search->text_searcher = text_searcher;
561                 }
562         }
563 #endif
564         list = tny_simple_list_new ();
565         /* Get the headers */
566         tny_folder_get_headers_async (folder, list, FALSE, 
567                                       modest_search_folder_get_headers_cb, 
568                                       NULL, helper);
569 }
570
571 void
572 modest_search_folder (TnyFolder *folder, 
573                       ModestSearch *search,
574                       ModestSearchCallback callback,
575                       gpointer user_data)
576 {
577         SearchHelper *helper;
578
579         /* Create the helper */
580         helper = create_helper (callback, search, user_data);
581
582         /* Search */
583         _search_folder (folder, helper);
584 }
585
586 static void
587 modest_search_account_get_folders_cb (TnyFolderStore *self, 
588                                       gboolean cancelled, 
589                                       TnyList *folders, 
590                                       GError *err, 
591                                       gpointer user_data)
592 {
593         TnyIterator *iter;
594         SearchHelper *helper;
595
596         helper = (SearchHelper *) user_data;
597
598         if (err || cancelled) {
599                 goto end;
600         }
601
602         /* IMPORTANT: We need to get the headers of the folders one by
603            one, because otherwise the get_headers_async calls are
604            often canceled. That's why we firstly retrieve all folders,
605            and then we search inside them one by one. sergio */
606         iter = tny_list_create_iterator (folders);
607         while (!tny_iterator_is_done (iter)) {
608                 TnyFolder *folder = NULL;
609
610                 /* Search into folder */
611                 folder = TNY_FOLDER (tny_iterator_get_current (iter));  
612                 tny_list_append (helper->all_folders, G_OBJECT (folder));
613
614                 /* Search into children. Could be a merge folder */
615                 if (TNY_IS_FOLDER_STORE (folder)) {
616                         TnyList *children = tny_simple_list_new ();
617                         helper->pending_calls++;
618                         tny_folder_store_get_folders_async (TNY_FOLDER_STORE (folder), children, NULL, 
619                                                             FALSE, modest_search_account_get_folders_cb, 
620                                                             NULL, helper);
621                 }
622
623                 g_object_unref (folder);
624                 tny_iterator_next (iter);
625         }
626         g_object_unref (iter);
627  end:
628         /* Remove the "account" reference */
629         helper->pending_calls--;
630
631         if (folders)
632                 g_object_unref (folders);
633
634         /* If there are not more folders, begin to search from the first one */
635         if (helper->pending_calls == 0) {
636                 TnyIterator *iter = tny_list_create_iterator (helper->all_folders);
637                 TnyFolder *first = TNY_FOLDER (tny_iterator_get_current (iter));
638
639                 _search_folder (first, helper);
640
641                 g_object_unref (first);
642                 g_object_unref (iter);
643         }
644 }
645
646 static void
647 _search_account (TnyAccount *account, 
648                  SearchHelper *helper)
649 {
650         TnyList *folders = tny_simple_list_new ();
651
652         g_debug ("%s: Searching account %s", __FUNCTION__, tny_account_get_name (account));
653
654         /* Add a "reference" to the folder total. This allows the code
655            not to finalize the helper if an account is fully refreshed
656            before we get the folders of the others */
657         helper->pending_calls++;
658
659         /* Get folders */
660         tny_folder_store_get_folders_async (TNY_FOLDER_STORE (account), folders, NULL, 
661                                             FALSE, modest_search_account_get_folders_cb, 
662                                             NULL, helper);
663 }
664
665 void
666 modest_search_account (TnyAccount *account, 
667                        ModestSearch *search,
668                        ModestSearchCallback callback,
669                        gpointer user_data)
670 {
671         SearchHelper *helper;
672
673         /* Create the helper */
674         helper = create_helper (callback, search, user_data);
675
676         /* Search */
677         _search_account (account, helper);
678 }
679
680 void
681 modest_search_all_accounts (ModestSearch *search,
682                             ModestSearchCallback callback,
683                             gpointer user_data)
684 {
685         ModestTnyAccountStore *astore;
686         TnyList *accounts;
687         TnyIterator *iter;
688         GList *hits;
689         SearchHelper *helper;
690
691         hits = NULL;
692         astore = modest_runtime_get_account_store ();
693
694         accounts = tny_simple_list_new ();
695         tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
696                                         accounts,
697                                         TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
698
699         /* Create the helper */
700         helper = create_helper (callback, search, user_data);
701
702         /* Search through all accounts */
703         iter = tny_list_create_iterator (accounts);
704         while (!tny_iterator_is_done (iter)) {
705                 TnyAccount *account = NULL;
706
707                 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
708                 _search_account (account, helper);
709                 g_object_unref (account);
710
711                 tny_iterator_next (iter);
712         }
713         g_object_unref (iter);
714         g_object_unref (accounts);
715 }
716
717 static SearchHelper *
718 create_helper (ModestSearchCallback callback, 
719                ModestSearch *search,
720                gpointer user_data)
721 {
722         SearchHelper *helper;
723
724         helper = g_slice_new0 (SearchHelper);
725         helper->pending_calls = 0;
726         helper->search = search;
727         helper->callback = callback;
728         helper->user_data = user_data;
729         helper->msg_hits = NULL;
730         helper->all_folders = tny_simple_list_new ();
731
732         return helper;
733 }
734
735 void 
736 modest_search_free (ModestSearch *search)
737 {
738         if (search->folder)
739                 g_free (search->folder);
740         if (search->subject)
741                 g_free (search->subject);
742         if (search->from)
743                 g_free (search->from);
744         if (search->recipient)
745                 g_free (search->recipient);
746         if (search->body)
747                 g_free (search->body);
748
749 #ifdef MODEST_HAVE_OGS
750         if (search->query)
751                 g_free (search->query);
752         if (search->text_searcher)
753                 ogs_text_searcher_free (search->text_searcher); 
754 #endif
755 }