* Define a new mail-operation to remove several messages.
[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 <libmodest-dbus-client/libmodest-dbus-client.h>
50
51 #include "modest-text-utils.h"
52 #include "modest-account-mgr.h"
53 #include "modest-tny-account-store.h"
54 #include "modest-tny-account.h"
55 #include "modest-search.h"
56 #include "modest-runtime.h"
57
58 static gchar *
59 g_strdup_or_null (const gchar *str)
60 {
61         gchar *string = NULL;
62
63         if  (str != NULL) {
64                 string = g_strdup (str);
65         }
66
67         return string;
68 }
69
70 typedef struct
71 {
72         GMainLoop* loop;
73         TnyAccount *account;
74         gboolean is_online;
75         gint count_tries;
76 } UtilIdleData;
77
78 #define NUMBER_OF_TRIES 10 /* Try approx every second, ten times. */
79
80 static gboolean 
81 on_timeout_check_account_is_online(gpointer user_data)
82 {
83         printf ("DEBUG: %s:\n", __FUNCTION__);
84         UtilIdleData *data = (UtilIdleData*)user_data;
85         
86         if (!data) {
87                 g_warning ("%s: data is NULL.\n", __FUNCTION__);
88         }
89         
90         if (!(data->account)) {
91                 g_warning ("%s: data->account is NULL.\n", __FUNCTION__);
92         }
93         
94         if (data && data->account) {
95                 printf ("DEBUG: %s: tny_account_get_connection_status()==%d\n", __FUNCTION__, tny_account_get_connection_status (data->account));       
96         }
97         
98         gboolean stop_trying = FALSE;
99         if (data && data->account && 
100                 /* We want to wait until TNY_CONNECTION_STATUS_INIT has changed to something else,
101                  * after which the account is likely to be usable, or never likely to be usable soon: */
102                 (tny_account_get_connection_status (data->account) != TNY_CONNECTION_STATUS_INIT) )
103         {
104                 data->is_online = TRUE;
105                 
106                 stop_trying = TRUE;
107         }
108         else {
109                 /* Give up if we have tried too many times: */
110                 if (data->count_tries >= NUMBER_OF_TRIES)
111                 {
112                         stop_trying = TRUE;
113                 }
114                 else {
115                         /* Wait for another timeout: */
116                         ++(data->count_tries);
117                 }
118         }
119         
120         if (stop_trying) {
121                 /* Allow the function that requested this idle callback to continue: */
122                 if (data->loop)
123                         g_main_loop_quit (data->loop);
124                         
125                 if (data->account)
126                         g_object_unref (data->account);
127                 
128                 return FALSE; /* Don't call this again. */
129         } else {
130                 return TRUE; /* Call this timeout callback again. */
131         }
132 }
133
134 /* Return TRUE immediately if the account is already online,
135  * otherwise check every second for NUMBER_OF_TRIES seconds and return TRUE as 
136  * soon as the account is online, or FALSE if the account does 
137  * not become online in the NUMBER_OF_TRIES seconds.
138  * This is useful when the D-Bus method was run immediately after 
139  * the application was started (when using D-Bus activation), 
140  * because the account usually takes a short time to go online.
141  * The return value is maybe not very useful.
142  */
143 static gboolean
144 check_and_wait_for_account_is_online(TnyAccount *account)
145 {
146         g_return_val_if_fail (account, FALSE);
147         
148         printf ("DEBUG: %s: account id=%s\n", __FUNCTION__, tny_account_get_id (account));
149         
150         if (!tny_device_is_online (modest_runtime_get_device())) {
151                 printf ("DEBUG: %s: device is offline.\n", __FUNCTION__);
152                 return FALSE;
153         }
154         
155         /* The local_folders account never seems to leave TNY_CONNECTION_STATUS_INIT,
156          * so we avoid wait unnecessarily: */
157         if (!TNY_IS_CAMEL_POP_STORE_ACCOUNT (account) && 
158                 !TNY_IS_CAMEL_IMAP_STORE_ACCOUNT (account) ) {
159                 return TRUE;            
160         }
161                 
162         printf ("DEBUG: %s: tny_account_get_connection_status()==%d\n", __FUNCTION__, tny_account_get_connection_status (account));
163         
164         /* The POP & IMAP store accounts seem to be TNY_CONNECTION_STATUS_DISCONNECTED, 
165          * and that seems to be an OK time to use them. Maybe it's just TNY_CONNECTION_STATUS_INIT that 
166          * we want to avoid. */
167         if (tny_account_get_connection_status (account) != TNY_CONNECTION_STATUS_INIT)
168                 return TRUE;
169                 
170         /* This blocks on the result: */
171         UtilIdleData *data = g_slice_new0 (UtilIdleData);
172         data->is_online = FALSE;
173         data->account = account;
174         g_object_ref (data->account);
175         data->count_tries = 0;
176                 
177         GMainContext *context = NULL; /* g_main_context_new (); */
178         data->loop = g_main_loop_new (context, FALSE /* not running */);
179
180         g_timeout_add (1000, &on_timeout_check_account_is_online, data);
181
182         /* This main loop will run until the idle handler has stopped it: */
183         g_main_loop_run (data->loop);
184
185         g_main_loop_unref (data->loop);
186         /* g_main_context_unref (context); */
187
188         g_slice_free (UtilIdleData, data);
189         
190         return data->is_online; 
191 }
192
193 static GList*
194 add_hit (GList *list, TnyHeader *header, TnyFolder *folder)
195 {
196         ModestSearchHit *hit;
197         TnyHeaderFlags   flags;
198         char            *furl;
199         char            *msg_url;
200         const char      *uid;
201         const char      *subject;
202         const char      *sender;
203
204         hit = g_slice_new0 (ModestSearchHit);
205
206         furl = tny_folder_get_url_string (folder);
207         printf ("DEBUG: %s: folder URL=%s\n", __FUNCTION__, furl);
208         if (!furl) {
209                 g_warning ("%s: tny_folder_get_url_string(): returned NULL for folder. Folder name=%s\n", __FUNCTION__, tny_folder_get_name (folder));
210         }
211         
212         /* Make sure that we use the short UID instead of the long UID,
213          * and/or find out what UID form is used when finding, in camel_data_cache_get().
214          * so we can find what we get. Philip is working on this.
215          */
216         uid = tny_header_get_uid (header);
217         if (!furl) {
218                 g_warning ("%s: tny_header_get_uid(): returned NULL for message with subject=%s\n", __FUNCTION__, tny_header_get_subject (header));
219         }
220         
221         msg_url = g_strdup_printf ("%s/%s", furl, uid);
222         g_free (furl);
223         
224         subject = tny_header_get_subject (header);
225         sender = tny_header_get_from (header);
226         
227         flags = tny_header_get_flags (header);
228
229         hit->msgid = msg_url;
230         hit->subject = g_strdup_or_null (subject);
231         hit->sender = g_strdup_or_null (sender);
232         hit->folder = g_strdup_or_null (tny_folder_get_name (folder));
233         hit->msize = tny_header_get_message_size (header);
234         hit->has_attachment = flags & TNY_HEADER_FLAG_ATTACHMENTS;
235         hit->is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
236         hit->timestamp = tny_header_get_date_received (header);
237         
238         return g_list_prepend (list, hit);
239 }
240
241 /** Call this until it returns FALSE or nread is set to 0.
242  * 
243  * @result: FALSE is something failed. */
244 static gboolean
245 read_chunk (TnyStream *stream, char *buffer, gsize count, gsize *nread)
246 {
247         gsize _nread = 0;
248         gssize res = 0;
249
250         while (_nread < count) {
251                 res = tny_stream_read (stream,
252                                        buffer + _nread, 
253                                        count - _nread);
254                 if (res == -1) { /* error */
255                         *nread = _nread;
256                         return FALSE;
257                 }
258
259                 _nread += res;
260                 
261                 if (res == 0) { /* no more bytes read. */
262                         *nread = _nread;
263                         return TRUE;
264                 }
265         }
266
267         *nread = _nread;
268         return TRUE;
269 }
270
271 #ifdef MODEST_HAVE_OGS
272 static gboolean
273 search_mime_part_ogs (TnyMimePart *part, ModestSearch *search)
274 {
275         TnyStream *stream = NULL;
276         char       buffer[4096];
277         const gsize len = sizeof (buffer);
278         gsize      nread = 0;
279         gboolean   is_text_html = FALSE;
280         gboolean   found = FALSE;
281         gboolean   res = FALSE;
282
283         gboolean is_text = tny_mime_part_content_type_is (part, "text/*");
284         if (!is_text) {
285                 g_debug ("%s: tny_mime_part_content_type_is() failed to find a "
286                         "text/* MIME part. Content type is %s", 
287                 __FUNCTION__, "Unknown (calling tny_mime_part_get_content_type(part) causes a deadlock)");
288                 
289             /* Retry with specific MIME types, because the wildcard seems to fail
290              * in tinymail.
291              * Actually I'm not sure anymore that it fails, so we could probalby 
292              * remove this later: murrayc */
293             is_text = (
294                 tny_mime_part_content_type_is (part, "text/plain") ||
295                 tny_mime_part_content_type_is (part, "text/html") );
296                 
297                 if (is_text) {
298                   g_debug ("%s: Retryting with text/plain or text/html succeeded", 
299                         __FUNCTION__);  
300                 }
301         }
302         
303         if (!is_text) {
304             return FALSE;
305         }
306         
307         is_text_html = tny_mime_part_content_type_is (part, "text/html");
308
309         stream = tny_mime_part_get_stream (part);
310
311         res = read_chunk (stream, buffer, len, &nread);
312         while (res && (nread > 0)) {
313                 /* search->text_searcher was instantiated in modest_search_folder(). */
314                 
315                 if (is_text_html) {
316
317                         found = ogs_text_searcher_search_html (search->text_searcher,
318                                                                buffer,
319                                                                nread,
320                                                                nread < len);
321                 } else {
322                         found = ogs_text_searcher_search_text (search->text_searcher,
323                                                                buffer,
324                                                                nread);
325                 }
326
327                 if (found) {
328                         break;
329                 }
330                 
331                 nread = 0;
332                 res = read_chunk (stream, buffer, len, &nread);
333         }
334
335         if (!found) {
336                 found = ogs_text_searcher_search_done (search->text_searcher);
337         }
338
339         ogs_text_searcher_reset (search->text_searcher);
340         
341         /* debug stuff:
342         if (!found) {
343                 buffer[len -1] = 0;
344                 printf ("DEBUG: %s: query %s was not found in message text: %s\n", 
345                         __FUNCTION__, search->query, buffer);   
346                 
347         } else {
348                 printf ("DEBUG: %s: found.\n", __FUNCTION__);   
349         }
350         */
351
352         return found;
353 }
354
355 #else
356
357 static gboolean
358 search_mime_part_strcmp (TnyMimePart *part, ModestSearch *search)
359 {
360         TnyStream *stream;
361         char       buffer[8193];
362         char      *chunk[2];
363         gssize     len;
364         gsize     nread;
365         gboolean   found;
366         gboolean   res;
367
368         if (! tny_mime_part_content_type_is (part, "text/*")) {
369                 g_debug ("%s: No text MIME part found.\n", __FUNCTION__);
370                 return FALSE;
371         }
372
373         found = FALSE;
374         len = (sizeof (buffer) - 1) / 2;
375
376         if (strlen (search->body) > len) {
377                 g_warning ("Search term bigger then chunk."
378                            "We might not find everything");     
379         }
380
381         stream = tny_mime_part_get_stream (part);
382
383         memset (buffer, 0, sizeof (buffer));
384         chunk[0] = buffer;
385         chunk[1] = buffer + len;
386
387         res = read_chunk (stream, chunk[0], len, &nread);
388
389         if (res == FALSE) {
390                 goto done;
391         }
392
393         found = !modest_text_utils_utf8_strcmp (search->body,
394                                                 buffer,
395                                                 TRUE);
396         if (found) {
397                 goto done;
398         }
399
400         /* This works like this:
401          * buffer: [ooooooooooo|xxxxxxxxxxxx|\0] 
402          *          ^chunk[0]  ^chunk[1]
403          * we have prefilled chunk[0] now we always read into chunk[1]
404          * and then move the content of chunk[1] to chunk[0].
405          * The idea is to prevent not finding search terms that are
406          * spread across 2 reads:        
407          * buffer: [ooooooooTES|Txxxxxxxxxxx|\0] 
408          * We should catch that because we always search the whole
409          * buffer not only the chunks.
410          *
411          * Of course that breaks for search terms > sizeof (chunk)
412          * but sizeof (chunk) should be big enough I guess (see
413          * the g_warning in this function)
414          * */   
415         while ((res = read_chunk (stream, chunk[1], len, &nread))) {
416                 buffer[len + nread] = '\0';
417
418                 found = !modest_text_utils_utf8_strcmp (search->body,
419                                                         buffer,
420                                                         TRUE);
421
422                 if (found) {
423                         break;
424                 }
425
426                 /* also move the \0 */
427                 g_memmove (chunk[0], chunk[1], len + 1);
428         }
429
430 done:
431         g_object_unref (stream);
432         return found;
433 }
434 #endif /*MODEST_HAVE_OGS*/
435
436 static gboolean
437 search_string (const char      *what,
438                const char      *where,
439                ModestSearch    *search)
440 {
441         gboolean found;
442 #ifdef MODEST_HAVE_OGS
443         if (search->flags & MODEST_SEARCH_USE_OGS) {
444                 found = ogs_text_searcher_search_text (search->text_searcher,
445                                                        where,
446                                                        strlen (where));
447
448                 ogs_text_searcher_reset (search->text_searcher);
449         } else {
450 #endif
451                 if (what == NULL || where == NULL) {
452                         return FALSE;
453                 }
454
455                 found = !modest_text_utils_utf8_strcmp (what, where, TRUE);
456 #ifdef MODEST_HAVE_OGS
457         }
458 #endif
459         return found;
460 }
461
462
463 static gboolean search_mime_part_and_child_parts (TnyMimePart *part, ModestSearch *search)
464 {
465         gboolean found = FALSE;
466         #ifdef MODEST_HAVE_OGS
467         found = search_mime_part_ogs (part, search);
468         #else
469         found = search_mime_part_strcmp (part, search);
470         #endif
471
472         if (found) {    
473                 return found;           
474         }
475         
476         /* Check the child part too, recursively: */
477         TnyList *child_parts = tny_simple_list_new ();
478         tny_mime_part_get_parts (TNY_MIME_PART (part), child_parts);
479
480         TnyIterator *piter = tny_list_create_iterator (child_parts);
481         while (!found && !tny_iterator_is_done (piter)) {
482                 TnyMimePart *pcur = (TnyMimePart *) tny_iterator_get_current (piter);
483                 if (pcur) {
484                         found = search_mime_part_and_child_parts (pcur, search);
485
486                         g_object_unref (pcur);
487                 }
488
489                 tny_iterator_next (piter);
490         }
491
492         g_object_unref (piter);
493         g_object_unref (child_parts);
494         
495         return found;
496 }
497
498 /**
499  * modest_search:
500  * @folder: a #TnyFolder instance
501  * @search: a #ModestSearch query
502  *
503  * This operation will search @folder for headers that match the query @search,
504  * if the folder itself matches the query.
505  * It will return a doubly linked list with URIs that point to the message.
506  **/
507 GList *
508 modest_search_folder (TnyFolder *folder, ModestSearch *search)
509 {
510         /* Check that we should be searching this folder. */
511         /* Note that we don't try to search sub-folders. 
512          * Maybe we should, but that should be specified. */
513         if (search->folder && strlen (search->folder) && (strcmp (tny_folder_get_id (folder), search->folder) != 0))
514                 return NULL;
515         
516         GList *retval = NULL;
517         TnyIterator *iter = NULL;
518         TnyList *list = NULL;
519         
520 #ifdef MODEST_HAVE_OGS
521         if (search->flags & MODEST_SEARCH_USE_OGS) {
522         
523                 if (search->text_searcher == NULL && search->query != NULL) {
524                         OgsTextSearcher *text_searcher; 
525
526                         text_searcher = ogs_text_searcher_new (FALSE);
527                         ogs_text_searcher_parse_query (text_searcher, search->query);
528                         search->text_searcher = text_searcher;
529                 }
530         }
531 #endif
532
533         list = tny_simple_list_new ();
534         GError *error = NULL;
535         tny_folder_get_headers (folder, list, FALSE /* don't refresh */, &error);
536         if (error) {
537                 g_warning ("%s: tny_folder_get_headers() failed with error=%s.\n", 
538                 __FUNCTION__, error->message);
539                 g_error_free (error);
540                 error = NULL;   
541         }
542
543         iter = tny_list_create_iterator (list);
544
545         while (!tny_iterator_is_done (iter)) {
546                 TnyHeader *cur = (TnyHeader *) tny_iterator_get_current (iter);
547                 const time_t t = tny_header_get_date_sent (cur);
548                 gboolean found = FALSE;
549                 
550                 /* Ignore deleted (not yet expunged) emails: */
551                 if (tny_header_get_flags(cur) & TNY_HEADER_FLAG_DELETED)
552                         goto go_next;
553                         
554                 if (search->flags & MODEST_SEARCH_BEFORE)
555                         if (!(t <= search->end_date))
556                                 goto go_next;
557
558                 if (search->flags & MODEST_SEARCH_AFTER)
559                         if (!(t >= search->start_date))
560                                 goto go_next;
561
562                 if (search->flags & MODEST_SEARCH_SIZE)
563                         if (tny_header_get_message_size (cur) < search->minsize)
564                                 goto go_next;
565
566                 if (search->flags & MODEST_SEARCH_SUBJECT) {
567                         const char *str = tny_header_get_subject (cur);
568
569                         if ((found = search_string (search->subject, str, search))) {
570                             retval = add_hit (retval, cur, folder);
571                         }
572                 }
573                 
574                 if (!found && search->flags & MODEST_SEARCH_SENDER) {
575                         char *str = g_strdup (tny_header_get_from (cur));
576
577                         if ((found = search_string (search->from, (const gchar *) str, search))) {
578                                 retval = add_hit (retval, cur, folder);
579                         }
580                         g_free (str);
581                 }
582                 
583                 if (!found && search->flags & MODEST_SEARCH_RECIPIENT) {
584                         const char *str = tny_header_get_to (cur);
585
586                         if ((found = search_string (search->recipient, str, search))) {
587                                 retval = add_hit (retval, cur, folder);
588                         }
589                 }
590         
591                 if (!found && search->flags & MODEST_SEARCH_BODY) {
592                         TnyHeaderFlags flags;
593                         GError      *err = NULL;
594                         TnyMsg      *msg = NULL;
595
596                         flags = tny_header_get_flags (cur);
597
598                         if (!(flags & TNY_HEADER_FLAG_CACHED)) {
599                                 goto go_next;
600                         }
601
602                         msg = tny_folder_get_msg (folder, cur, &err);
603
604                         if (err != NULL || msg == NULL) {
605                                 g_warning ("%s: Could not get message.\n", __FUNCTION__);
606                                 g_error_free (err);
607
608                                 if (msg) {
609                                         g_object_unref (msg);
610                                 }
611                         } else {        
612                         
613                                 found = search_mime_part_and_child_parts (TNY_MIME_PART (msg), 
614                                                                           search);
615                                 if (found) {
616                                         retval = add_hit (retval, cur, folder);
617                                 }
618                         }
619                         
620                         if (msg)
621                                 g_object_unref (msg);
622                 }
623
624 go_next:
625                 g_object_unref (cur);
626                 tny_iterator_next (iter);
627         }
628
629         g_object_unref (iter);
630         g_object_unref (list);
631         return retval;
632 }
633
634 GList *
635 modest_search_account (TnyAccount *account, ModestSearch *search)
636 {
637         TnyFolderStore      *store;
638         TnyIterator         *iter;
639         TnyList             *folders;
640         GList               *hits;
641         GError              *error;
642
643         error = NULL;
644         hits = NULL;
645
646         store = TNY_FOLDER_STORE (account);
647
648         folders = tny_simple_list_new ();
649         tny_folder_store_get_folders (store, folders, NULL, &error);
650         
651         if (error != NULL) {
652                 g_object_unref (folders);
653                 return NULL;
654         }
655
656         iter = tny_list_create_iterator (folders);
657         while (!tny_iterator_is_done (iter)) {
658                 TnyFolder *folder = NULL;
659                 GList     *res = NULL;
660
661                 folder = TNY_FOLDER (tny_iterator_get_current (iter));
662                 if (folder) {
663                         /* g_debug ("DEBUG: %s: searching folder %s.", 
664                                 __FUNCTION__, tny_folder_get_name (folder)); */
665                 
666                         res = modest_search_folder (folder, search);
667
668                         if (res != NULL) {
669                                 if (hits == NULL) {
670                                         hits = res;
671                                 } else {
672                                         hits = g_list_concat (hits, res);
673                                 }
674                         }
675
676                         g_object_unref (folder);
677                 }
678
679                 tny_iterator_next (iter);
680         }
681
682         g_object_unref (iter);
683         g_object_unref (folders);
684
685         /* printf ("DEBUG: %s: hits length = %d\n", __FUNCTION__, g_list_length (hits)); */
686         return hits;
687 }
688
689 GList *
690 modest_search_all_accounts (ModestSearch *search)
691 {
692         /* printf ("DEBUG: %s: query=%s\n", __FUNCTION__, search->query); */
693         ModestTnyAccountStore *astore;
694         TnyList               *accounts;
695         TnyIterator           *iter;
696         GList                 *hits;
697
698         hits = NULL;
699         astore = modest_runtime_get_account_store ();
700
701         accounts = tny_simple_list_new ();
702         tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
703                                         accounts,
704                                         TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
705
706         iter = tny_list_create_iterator (accounts);
707         while (!tny_iterator_is_done (iter)) {
708                 TnyAccount *account = NULL;
709                 GList      *res = NULL;
710
711                 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
712                 if (account) {
713                         /* g_debug ("DEBUG: %s: Searching account %s",
714                          __FUNCTION__, tny_account_get_name (account)); */
715                          
716                         /* Give the account time to go online if necessary, 
717                          * for instance if this is immediately after startup,
718                          * after D-Bus activation: */
719                         check_and_wait_for_account_is_online (account);
720                         
721                         /* Search: */
722                         res = modest_search_account (account, search);
723                         
724                         if (res != NULL) {      
725                                 if (hits == NULL) {
726                                         hits = res;
727                                 } else {
728                                         hits = g_list_concat (hits, res);
729                                 }
730                         }
731                         
732                         g_object_unref (account);
733                 }
734
735                 tny_iterator_next (iter);
736         }
737
738         g_object_unref (accounts);
739         g_object_unref (iter);
740
741         /* printf ("DEBUG: %s: end: hits length=%d\n", __FUNCTION__, g_list_length(hits)); */
742         return hits;
743 }
744
745