85d9447d47d300cb79d5368825b25e6c722be0a8
[modest] / libmodest-dbus-client / libmodest-dbus-client.c
1 /* Copyright (c) 2007, 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 #include "libmodest-dbus-client.h"
31 #include <dbus_api/modest-dbus-api.h> /* For the API strings. */
32
33 //#define DBUS_API_SUBJECT_TO_CHANGE 1
34 #include <dbus/dbus.h>
35 #include <dbus/dbus-glib-lowlevel.h>
36 #include <string.h>
37
38
39
40 /** Get a comma-separated list of attachement URI strings, 
41  * from a list of strings.
42  */
43 static gchar* get_attachments_string (GSList *attachments)
44 {
45         if (!attachments)
46                 return NULL;
47                 
48         gchar *attachments_str = g_strdup("");
49         
50         GSList *iter = attachments;
51         while (iter)
52         {
53                 if (iter->data) {
54                         gchar *tmp = g_strconcat(attachments_str, ",", (gchar *) (iter->data), NULL);
55                         g_free(attachments_str);
56                         attachments_str = tmp;
57                 }
58                 
59                 iter = g_slist_next(iter);
60         }
61         
62         return attachments_str;
63 }
64
65 /* TODO: Is this actually used by anything?
66  * I guess that everything uses *_compose_mail() instead. murrayc.
67  */
68  
69 /**
70  * libmodest_dbus_client_send_mail:
71  * @osso_context: a valid #osso_context_t object.
72  * @to: The Recipients (From: line)
73  * @cc: Recipients for carbon copies
74  * @bcc: Recipients for blind carbon copies
75  * @subject: Subject line
76  * @body: The actual body of the mail to send
77  * @attachments: Additional list of attachments
78  * 
79  * This function will try to do a remote procedure call (rpc)
80  * into modest (or start it if necessary) and send a new 
81  * email with the supplied parameters.
82  *
83  * Return value: Whether or not the rpc call to modest
84  * was successfull
85  **/
86 gboolean
87 libmodest_dbus_client_send_mail (osso_context_t *osso_context, const gchar *to, const gchar *cc, 
88         const gchar *bcc, const gchar* subject, const gchar* body, GSList *attachments)
89 {
90         gchar *attachments_str = get_attachments_string(attachments);
91         
92         osso_rpc_t retval;
93         const osso_return_t ret = osso_rpc_run_with_defaults(osso_context, 
94                    MODEST_DBUS_NAME, 
95                    MODEST_DBUS_METHOD_SEND_MAIL, &retval, 
96                    DBUS_TYPE_STRING, to, 
97                    DBUS_TYPE_STRING, cc, 
98                    DBUS_TYPE_STRING, bcc, 
99                    DBUS_TYPE_STRING, subject, 
100                    DBUS_TYPE_STRING, body, 
101                    DBUS_TYPE_STRING, attachments_str,
102                    DBUS_TYPE_INVALID);
103                 
104         if (ret != OSSO_OK) {
105                 printf("debug: %s: osso_rpc_run() failed.\n", __FUNCTION__);
106                 return FALSE;
107         } else {
108                 printf("debug: %s: osso_rpc_run() succeeded.\n", __FUNCTION__);
109         }
110         
111         osso_rpc_free_val(&retval);
112         
113         g_free (attachments_str);
114         
115         return TRUE;
116 }
117         
118 gboolean 
119 libmodest_dbus_client_mail_to (osso_context_t *osso_context, const gchar *mailto_uri)
120 {
121         osso_rpc_t retval;
122         const osso_return_t ret = osso_rpc_run_with_defaults(osso_context, 
123                    MODEST_DBUS_NAME, 
124                    MODEST_DBUS_METHOD_MAIL_TO, &retval, 
125                    DBUS_TYPE_STRING, mailto_uri, 
126                    DBUS_TYPE_INVALID);
127                 
128         if (ret != OSSO_OK) {
129                 printf("debug: %s: osso_rpc_run() failed.\n", __FUNCTION__);
130                 return FALSE;
131         } else {
132                 printf("debug: %s: osso_rpc_run() succeeded.\n", __FUNCTION__);
133         }
134         
135         osso_rpc_free_val(&retval);
136         
137         return TRUE;
138 }
139
140 /**
141  * libmodest_dbus_client_compose_mail:
142  * @osso_context: a valid #osso_context_t object.
143  * @to: The Recipients (From: line)
144  * @cc: Recipients for carbon copies
145  * @bcc: Recipients for blind carbon copies
146  * @subject: Subject line
147  * @body: The actual body of the mail to send
148  * @attachments: Additional list of attachments
149  * 
150  * This function will try to do a remote procedure call (rpc)
151  * into modest (or start it if necessary) and open a composer
152  * window with the supplied parameters prefilled.
153  *
154  * Return value: Whether or not the rpc call to modest
155  * was successfull
156  **/
157 gboolean
158 libmodest_dbus_client_compose_mail (osso_context_t *osso_context, const gchar *to, const gchar *cc, 
159         const gchar *bcc, const gchar* subject, const gchar* body, GSList *attachments)
160 {
161         osso_rpc_t retval;
162         
163         gchar *attachments_str = get_attachments_string(attachments);
164
165         const osso_return_t ret = osso_rpc_run_with_defaults(osso_context, 
166                    MODEST_DBUS_NAME, 
167                    MODEST_DBUS_METHOD_COMPOSE_MAIL, &retval, 
168                    DBUS_TYPE_STRING, to, 
169                    DBUS_TYPE_STRING, cc, 
170                    DBUS_TYPE_STRING, bcc, 
171                    DBUS_TYPE_STRING, subject, 
172                    DBUS_TYPE_STRING, body,
173                    DBUS_TYPE_STRING, attachments_str,
174                    DBUS_TYPE_INVALID);
175                 
176         if (ret != OSSO_OK) {
177                 printf("debug: %s: osso_rpc_run() failed.\n", __FUNCTION__);
178                 return FALSE;
179         } else {
180                 printf("debug: %s: osso_rpc_run() succeeded.\n", __FUNCTION__);
181         }
182         
183         osso_rpc_free_val(&retval);
184
185         g_free (attachments_str);
186         
187         return TRUE;
188 }
189
190 /**
191  * libmodest_dbus_client_open_message:
192  * @osso_context: a valid #osso_context_t object.
193  * @msg_uri: A valid url to a mail
194  *
195  * This method will try to find the message supplied
196  * by @msg_uri and open it for display if found. 
197  * It will use remote procedure calls (rpc) over 
198  * dbus to do so.
199  *  
200  * Return value: TRUE on successs, FALSE on error
201  **/
202 gboolean 
203 libmodest_dbus_client_open_message (osso_context_t *osso_context, const gchar *mail_uri)
204 {
205         osso_rpc_t retval;
206         const osso_return_t ret = osso_rpc_run_with_defaults(osso_context, 
207                    MODEST_DBUS_NAME, 
208                    MODEST_DBUS_METHOD_OPEN_MESSAGE, &retval, 
209                    DBUS_TYPE_STRING, mail_uri, 
210                    DBUS_TYPE_INVALID);
211                 
212         if (ret != OSSO_OK) {
213                 printf("debug: %s: osso_rpc_run() failed.\n", __FUNCTION__);
214                 return FALSE;
215         } else {
216                 printf("debug: %s: osso_rpc_run() succeeded.\n", __FUNCTION__);
217         }
218         
219         osso_rpc_free_val(&retval);
220         
221         return TRUE;
222 }
223
224 gboolean 
225 libmodest_dbus_client_send_and_receive (osso_context_t *osso_context)
226 {
227         osso_rpc_t retval;
228         const osso_return_t ret = osso_rpc_run_with_defaults(osso_context, 
229                    MODEST_DBUS_NAME, 
230                    MODEST_DBUS_METHOD_SEND_RECEIVE, &retval, 
231                    DBUS_TYPE_INVALID);
232                 
233         if (ret != OSSO_OK) {
234                 printf("debug: %s: osso_rpc_run() failed.\n", __FUNCTION__);
235                 return FALSE;
236         } else {
237                 printf("debug: %s: osso_rpc_run() succeeded.\n", __FUNCTION__);
238         }
239         
240         osso_rpc_free_val(&retval);
241         
242         return TRUE;
243 }
244
245 gboolean 
246 libmodest_dbus_client_open_default_inbox (osso_context_t *osso_context)
247 {
248         osso_rpc_t retval;
249         const osso_return_t ret = osso_rpc_run_with_defaults(osso_context, 
250                    MODEST_DBUS_NAME, 
251                    MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX, &retval, 
252                    DBUS_TYPE_INVALID);
253                 
254         if (ret != OSSO_OK) {
255                 printf("debug: %s: osso_rpc_run() failed.\n", __FUNCTION__);
256                 return FALSE;
257         } else {
258                 printf("debug: %s: osso_rpc_run() succeeded.\n", __FUNCTION__);
259         }
260         
261         osso_rpc_free_val(&retval);
262         
263         return TRUE;
264 }
265
266 /**
267  * libmodest_dbus_client_delete_message:
268  * @osso_context: a valid #osso_context_t object.
269  * @msg_uri: A valid url to a mail 
270  *
271  * This method will try to find the message supplied
272  * by @msg_uri and if found delete it. It will use
273  * remote procedure calls (rpc) over dbus to do so.
274  * 
275  * Return value: TRUE on successs, FALSE on error
276  **/
277 gboolean
278 libmodest_dbus_client_delete_message (osso_context_t   *osso_ctx,
279                                       const char       *msg_uri)
280 {
281         osso_rpc_t    retval;
282         osso_return_t ret;
283        
284         ret = osso_rpc_run_with_defaults (osso_ctx, 
285                                           MODEST_DBUS_NAME, 
286                                           MODEST_DBUS_METHOD_DELETE_MESSAGE, &retval, 
287                                           DBUS_TYPE_STRING, msg_uri, 
288                                           DBUS_TYPE_INVALID);
289                 
290         if (ret != OSSO_OK) {
291                 g_debug ("debug: osso_rpc_run() failed.\n");
292         } else {
293                 g_debug ("debug: osso_rpc_run() succeeded.\n");
294         }
295         
296         osso_rpc_free_val (&retval);
297
298         return ret == OSSO_OK;
299 }
300
301 static void
302 modest_search_hit_free (ModestSearchHit *hit)
303 {
304         g_free (hit->msgid);
305         g_slice_free (ModestSearchHit, hit);
306 }
307
308 void
309 modest_search_hit_list_free (GList *hits)
310 {
311         GList *iter;
312
313         if (hits == NULL) {
314                 return;
315         }
316
317         for (iter = hits; iter; iter = iter->next) {
318                 modest_search_hit_free ((ModestSearchHit *) iter->data);
319         }
320
321         g_list_free (hits);
322 }
323
324 static char *
325 _dbus_iter_get_string_or_null (DBusMessageIter *iter)
326 {
327         const char *string = NULL;
328         char       *ret = NULL;
329
330         dbus_message_iter_get_basic (iter, &string);
331         
332         if (string && strlen (string)) {
333                 ret = g_strdup (string);
334         }
335
336         return ret;
337 }
338
339 static guint64
340 _dbus_iter_get_uint64 (DBusMessageIter *iter)
341 {
342         dbus_uint64_t ui64v;
343         guint64       ret;
344
345         ui64v = 0;
346         dbus_message_iter_get_basic (iter, &ui64v);
347
348         ret = (guint64) ui64v;
349
350         return ret;
351 }
352
353
354 static gint64
355 _dbus_iter_get_int64 (DBusMessageIter *iter)
356 {
357         dbus_int64_t i64v;
358         gint64       ret;
359
360         i64v = 0;
361         dbus_message_iter_get_basic (iter, &i64v);
362
363         ret = (gint64) i64v;
364
365         return ret;
366 }
367
368 static gboolean
369 _dbus_iter_get_boolean (DBusMessageIter *iter)
370
371 {
372         dbus_bool_t  val;
373         gboolean     ret;
374
375         val = FALSE;
376         dbus_message_iter_get_basic (iter, &val);
377
378         ret = (gboolean) val;
379
380         return ret;
381 }
382
383 /** Get the values from the complex type (SEARCH_HIT_DBUS_TYPE)
384  * in the D-Bus return message. */
385 static ModestSearchHit *
386 modest_dbus_message_iter_get_search_hit (DBusMessageIter *parent)
387 {
388         ModestSearchHit *hit;
389         DBusMessageIter  child;
390         dbus_bool_t      res;
391         int              arg_type;
392         gboolean         error;
393
394         error = FALSE;
395         hit = g_slice_new0 (ModestSearchHit);
396
397         arg_type = dbus_message_iter_get_arg_type (parent);
398
399         if (arg_type != 'r') {
400                 return NULL;
401         }
402
403         dbus_message_iter_recurse (parent, &child);
404         
405         /* msgid  */
406         arg_type = dbus_message_iter_get_arg_type (&child);
407
408         if (arg_type != DBUS_TYPE_STRING) {
409                 error = TRUE;
410                 goto out;
411         }
412
413         hit->msgid = _dbus_iter_get_string_or_null (&child);
414
415         res = dbus_message_iter_next (&child);
416         if (res == FALSE) {
417                 error = TRUE;
418                 goto out;
419         }
420
421         /* subject  */
422         arg_type = dbus_message_iter_get_arg_type (&child);
423
424         if (arg_type != DBUS_TYPE_STRING) {
425                 error = TRUE;
426                 goto out;
427         }
428
429         hit->subject = _dbus_iter_get_string_or_null (&child);
430
431         res = dbus_message_iter_next (&child);
432         if (res == FALSE) {
433                 error = TRUE;
434                 goto out;
435         }
436
437         /* folder  */
438         arg_type = dbus_message_iter_get_arg_type (&child);
439
440         if (arg_type != DBUS_TYPE_STRING) {
441                 error = TRUE;
442                 goto out;
443         }
444
445         hit->folder = _dbus_iter_get_string_or_null (&child);
446
447         res = dbus_message_iter_next (&child);
448         if (res == FALSE) {
449                 error = TRUE;
450                 goto out;
451         }
452
453         /* sender  */
454         arg_type = dbus_message_iter_get_arg_type (&child);
455
456         if (arg_type != DBUS_TYPE_STRING) {
457                 error = TRUE;
458                 goto out;
459         }
460
461         hit->sender = _dbus_iter_get_string_or_null (&child);
462
463         res = dbus_message_iter_next (&child);
464         if (res == FALSE) {
465                 error = TRUE;
466                 goto out;
467         }
468
469         /* msize  */
470         arg_type = dbus_message_iter_get_arg_type (&child);
471
472         if (arg_type != DBUS_TYPE_UINT64) {
473                 error = TRUE;
474                 goto out;
475         }
476
477         hit->msize = _dbus_iter_get_uint64 (&child);
478
479         res = dbus_message_iter_next (&child);
480         if (res == FALSE) {
481                 error = TRUE;
482                 goto out;
483         }
484
485         /* has_attachment  */
486         arg_type = dbus_message_iter_get_arg_type (&child);
487
488         if (arg_type != DBUS_TYPE_BOOLEAN) {
489                 error = TRUE;
490                 goto out;
491         }
492
493         hit->has_attachment = _dbus_iter_get_boolean (&child); 
494
495         res = dbus_message_iter_next (&child);
496         if (res == FALSE) {
497                 error = TRUE;
498                 goto out;
499         }
500
501         /* is_unread  */
502         arg_type = dbus_message_iter_get_arg_type (&child);
503
504         if (arg_type != DBUS_TYPE_BOOLEAN) {
505                 error = TRUE;
506                 goto out;
507         }
508
509         hit->is_unread = _dbus_iter_get_boolean (&child);  
510
511         res = dbus_message_iter_next (&child);
512         if (res == FALSE) {
513                 error = TRUE;
514                 goto out;
515         }
516
517         /* timestamp  */
518         arg_type = dbus_message_iter_get_arg_type (&child);
519
520         if (arg_type != DBUS_TYPE_INT64) {
521                 error = TRUE;
522                 goto out;
523         }
524
525         hit->timestamp = _dbus_iter_get_int64 (&child); 
526
527         res = dbus_message_iter_next (&child);
528         if (res == TRUE) {
529                 error = TRUE;
530                 goto out;
531         }       
532
533 out:
534         if (error) {
535                 g_warning ("%s: Error during unmarshalling", __FUNCTION__);
536                 modest_search_hit_free (hit);
537                 hit = NULL;
538         }
539
540         return hit;
541 }
542
543 /**
544  * libmodest_dbus_client_search:
545  * @osso_ctx: A valid #osso_context_t object.
546  * @query: The term to search for.
547  * @folder: An url to specific folder or %NULL to search everywhere.
548  * @start_date: Search hits before this date will be ignored.
549  * @end_date: Search hits after this date will be ignored.
550  * @min_size: Messagers smaller then this size will be ingored.
551  * @flags: A list of flags where to search so the documentation 
552  * of %ModestDBusSearchFlags for details.
553  * @hits: A pointer to a valid GList pointer that will contain the search
554  * hits (ModestSearchHit). The list and the items must be freed by the caller 
555  * with modest_search_hit_list_free().
556  *
557  * This method will search the folder specified by a valid url in @folder or all
558  * known accounts (local and remote) if %NULL for matches of the search term(s)
559  * specified in @query. It is legal to specify 0 in @start_date, @end_date and
560  * @min_size to ignore these parameters during the search otherwise those message
561  * that do not meet the specifed dates or size will be ignored.
562  * Where to search, be it subject, sender or the whole body can be specified by
563  * the @flags parameter.
564  *
565  * Upon success TRUE is returned and @hits will include the search hits or the list
566  * migh be empty if none of the messages matched the search criteria. The returned
567  * list must be freed with modest_search_hit_list_free (). It is save to pass
568  * %NULL to this function so you can call this function on the result list no matter
569  * if a hit was found or not (means the list is empty - i.e. %NULL)
570  * FALSE will only be return if an error during the remote procedure call (rpc) 
571  * occured or if the specified folder could not be found.
572  *
573  * NOTE: The body of a message can only be searched if it was previously downloaded by
574  * modest. This function does also not attempt do to remote searches (i.e. IMAP search).
575  *
576  * Example to search every account for message containing "no":
577  * <informalexample><programlisting>
578  * ModestDBusSearchFlags  flags;
579  * osso_context_t        *osso_context;
580  * GList                 *hits;
581  * GList                 *iter;
582  * gboolean               res;
583  * 
584  * [...] Initialize osso context [...]
585  *
586  * res = libmodest_dbus_client_search (osso_context,
587  *                                     "no",
588  *                                     NULL,
589  *                                     0,
590  *                                     0,
591  *                                     0,
592  *                                     flags,
593  *                                     &hits);
594  * 
595  * for (iter = hits; iter; iter = iter->next) {
596  *      ModestSearchHit *hit = (ModestSearchHit *) iter->data;
597  *      
598  *      [...] Do something with the hit [...]
599  *
600  *      }
601  *
602  *      modest_search_hit_list_free (hits);
603  * </programlisting></informalexample>
604  * 
605  * Return value: TRUE if the search succeded or FALSE for an error during the search
606  **/
607 gboolean
608 libmodest_dbus_client_search (osso_context_t          *osso_ctx,
609                               const gchar             *query,
610                               const gchar             *folder,
611                               time_t                   start_date,
612                               time_t                   end_date,
613                               guint32                  min_size,
614                               ModestDBusSearchFlags    flags,
615                               GList                  **hits)
616 {
617
618         DBusMessage *msg;
619         dbus_bool_t res;
620         DBusError err;
621         DBusConnection *con;
622         DBusMessageIter iter;
623         DBusMessageIter child;
624         DBusMessage *reply = NULL;
625         gint timeout;
626         int arg_type;
627         dbus_int64_t sd_v;
628         dbus_int64_t ed_v;
629         dbus_int32_t flags_v;
630         dbus_uint32_t size_v;
631
632         if (query == NULL) {
633                 return FALSE;
634         }
635
636         con = osso_get_dbus_connection (osso_ctx);
637
638         if (con == NULL) {
639                 g_warning ("Could not get dbus connection\n");
640                 return FALSE;
641
642         }
643
644
645         msg = dbus_message_new_method_call (MODEST_DBUS_SERVICE,
646                 MODEST_DBUS_OBJECT,
647                 MODEST_DBUS_IFACE,
648                 MODEST_DBUS_METHOD_SEARCH);
649
650     if (msg == NULL) {
651         //ULOG_ERR_F("dbus_message_new_method_call failed");
652                 return OSSO_ERROR;
653     }
654
655         if (folder == NULL) {
656                 folder = "";
657         }
658
659         sd_v = start_date;
660         ed_v = end_date;
661         flags_v = (dbus_int32_t) flags;
662         size_v = (dbus_uint32_t) min_size;
663
664         res  = dbus_message_append_args (msg,
665                                          DBUS_TYPE_STRING, &query,
666                                          DBUS_TYPE_STRING, &folder,
667                                          DBUS_TYPE_INT64, &sd_v,
668                                          DBUS_TYPE_INT64, &ed_v,
669                                          DBUS_TYPE_INT32, &flags_v,
670                                          DBUS_TYPE_UINT32, &size_v,
671                                          DBUS_TYPE_INVALID);
672
673         dbus_message_set_auto_start (msg, TRUE);
674
675         dbus_error_init (&err);
676
677         timeout = 1000; //XXX
678         osso_rpc_get_timeout (osso_ctx, &timeout);
679
680         reply = dbus_connection_send_with_reply_and_block (con,
681                                                            msg, 
682                                                            timeout,
683                                                            &err);
684
685         dbus_message_unref (msg);
686
687
688         if (reply == NULL) {
689             //ULOG_ERR_F("dbus_connection_send_with_reply_and_block error: %s", err.message);
690             //XXX to GError?! 
691             return FALSE;
692         }
693
694         switch (dbus_message_get_type (reply)) {
695
696                 case DBUS_MESSAGE_TYPE_ERROR:
697                         dbus_set_error_from_message (&err, reply);
698                         //XXX to GError?!
699                         dbus_error_free (&err);
700                         dbus_message_unref (reply);
701                         return FALSE;
702
703                 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
704                         /* ok we are good to go
705                          * lets drop outa here and handle that */
706                         break;
707                 default:
708                         //ULOG_WARN_F("got unknown message type as reply");
709                         //retval->type = DBUS_TYPE_STRING;
710                         //retval->value.s = g_strdup("Invalid return value");
711                         //XXX to GError?! 
712                         dbus_message_unref (reply);
713                         return FALSE;
714         }
715
716         g_debug ("%s: message return", __FUNCTION__);
717
718         dbus_message_iter_init (reply, &iter);
719         arg_type = dbus_message_iter_get_arg_type (&iter);
720         
721         dbus_message_iter_recurse (&iter, &child);
722         *hits = NULL;
723
724         do {
725                 ModestSearchHit *hit;
726
727                 hit = modest_dbus_message_iter_get_search_hit (&child);
728
729                 if (hit) {
730                         *hits = g_list_prepend (*hits, hit);    
731                 }
732
733         } while (dbus_message_iter_next (&child));
734
735         dbus_message_unref (reply);
736
737
738         /* TODO: This is from osso source, do we need it? */
739 #if 0
740         /* Tell TaskNavigator to show "launch banner" */
741         msg = dbus_message_new_method_call (TASK_NAV_SERVICE,
742                                             APP_LAUNCH_BANNER_METHOD_PATH,
743                                             APP_LAUNCH_BANNER_METHOD_INTERFACE,
744                                             APP_LAUNCH_BANNER_METHOD);
745
746         if (msg == NULL) {
747                 g_warn ("dbus_message_new_method_call failed");
748         }
749
750
751
752         dbus_message_append_args (msg,
753                                   DBUS_TYPE_STRING,
754                                   &service,
755                                   DBUS_TYPE_INVALID);
756
757         b = dbus_connection_send (conn, msg, NULL);
758
759         if (b == NULL) {
760                 ULOG_WARN_F("dbus_connection_send failed");
761         }
762
763         dbus_message_unref (msg);
764 #endif
765
766         return TRUE;
767 }
768
769
770 static void
771 modest_folder_result_free (ModestFolderResult *item)
772 {
773         g_free (item->folder_name);
774         g_free (item->folder_uri);
775         g_slice_free (ModestFolderResult, item);
776 }
777
778 void
779 modest_folder_result_list_free (GList *list)
780 {
781         GList *iter;
782
783         if (list == NULL) {
784                 return;
785         }
786
787         for (iter = list; iter; iter = iter->next) {
788                 modest_folder_result_free ((ModestFolderResult *) iter->data);
789         }
790
791         g_list_free (list);
792 }
793
794
795 /** Get the values from the complex type (GET_FOLDERS_RESULT_DBUS_TYPE)
796  * in the D-Bus return message. */
797 static ModestFolderResult *
798 modest_dbus_message_iter_get_folder_item (DBusMessageIter *parent)
799 {
800         gboolean error = FALSE;
801         ModestFolderResult *item = g_slice_new0 (ModestFolderResult);
802
803         int arg_type = dbus_message_iter_get_arg_type (parent);
804
805         if (arg_type != 'r') {
806                 return NULL;
807         }
808
809         DBusMessageIter  child;
810         dbus_message_iter_recurse (parent, &child);
811         
812         /* folder name: */
813         arg_type = dbus_message_iter_get_arg_type (&child);
814
815         if (arg_type != DBUS_TYPE_STRING) {
816                 error = TRUE;
817                 goto out;
818         }
819
820         item->folder_name = _dbus_iter_get_string_or_null (&child);
821         
822         
823         dbus_bool_t res = dbus_message_iter_next (&child);
824         if (res == FALSE) {
825                 error = TRUE;
826                 goto out;
827         }
828
829         /* folder URI:  */
830         arg_type = dbus_message_iter_get_arg_type (&child);
831
832         if (arg_type != DBUS_TYPE_STRING) {
833                 error = TRUE;
834                 goto out;
835         }
836
837         item->folder_uri = _dbus_iter_get_string_or_null (&child);
838
839
840 out:
841         if (error) {
842                 g_warning ("%s: Error during unmarshalling", __FUNCTION__);
843                 modest_folder_result_free (item);
844                 item = NULL;
845         }
846
847         return item;
848 }
849
850 /**
851  * libmodest_dbus_client_get_folders:
852  * @osso_ctx: A valid #osso_context_t object.
853  * @folders: A pointer to a valid GList pointer that will contain the folder items
854  * (ModestFolderResult). The list and the items must be freed by the caller 
855  * with modest_folder_result_list_free().
856  *
857  * This method will obtain a list of folders in the default account.
858  *
859  * Upon success TRUE is returned and @folders will include the folders or the list
860  * might be empty if there are no folders. The returned
861  * list must be freed with modest_folder_result_list_free ().
862  *
863  * NOTE: A folder will only be retrieved if it was previously downloaded by
864  * modest. This function does also not attempt do to remote refreshes (i.e. IMAP).
865  * 
866  * Return value: TRUE if the request succeded or FALSE for an error.
867  **/
868 gboolean
869 libmodest_dbus_client_get_folders (osso_context_t          *osso_ctx,
870                               GList                  **folders)
871 {
872         /* Initialize output argument: */
873         if (folders)
874                 *folders = NULL;
875         else
876                 return FALSE;
877
878         DBusConnection *con = osso_get_dbus_connection (osso_ctx);
879
880         if (con == NULL) {
881                 g_warning ("Could not get dbus connection\n");
882                 return FALSE;
883
884         }
885
886         DBusMessage *msg = dbus_message_new_method_call (MODEST_DBUS_SERVICE,
887                 MODEST_DBUS_OBJECT,
888                 MODEST_DBUS_IFACE,
889                 MODEST_DBUS_METHOD_GET_FOLDERS);
890
891     if (msg == NULL) {
892         //ULOG_ERR_F("dbus_message_new_method_call failed");
893                 return OSSO_ERROR;
894     }
895
896         dbus_message_set_auto_start (msg, TRUE);
897
898         gint timeout = 1000; //XXX
899         osso_rpc_get_timeout (osso_ctx, &timeout);
900
901         DBusError err;
902         dbus_error_init (&err);
903         DBusMessage *reply = dbus_connection_send_with_reply_and_block (con,
904                                                            msg, 
905                                                            timeout,
906                                                            &err);
907
908         dbus_message_unref (msg);
909         msg = NULL;
910
911         if (reply == NULL) {
912                 //ULOG_ERR_F("dbus_connection_send_with_reply_and_block error: %s", err.message);
913                 //XXX to GError?! 
914                 return FALSE;
915         }
916
917         switch (dbus_message_get_type (reply)) {
918
919                 case DBUS_MESSAGE_TYPE_ERROR:
920                         dbus_set_error_from_message (&err, reply);
921                         //XXX to GError?!
922                         dbus_error_free (&err);
923                         dbus_message_unref (reply);
924                         return FALSE;
925
926                 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
927                         /* ok we are good to go
928                          * lets drop outa here and handle that */
929                         break;
930                 default:
931                         //ULOG_WARN_F("got unknown message type as reply");
932                         //retval->type = DBUS_TYPE_STRING;
933                         //retval->value.s = g_strdup("Invalid return value");
934                         //XXX to GError?! 
935                         dbus_message_unref (reply);
936                         return FALSE;
937         }
938
939         g_debug ("%s: message return", __FUNCTION__);
940
941         DBusMessageIter iter;
942         dbus_message_iter_init (reply, &iter);
943         /* int arg_type = dbus_message_iter_get_arg_type (&iter); */
944         
945         DBusMessageIter child;
946         dbus_message_iter_recurse (&iter, &child);
947
948         do {
949                 ModestFolderResult *item = modest_dbus_message_iter_get_folder_item (&child);
950
951                 if (item) {
952                         *folders = g_list_append (*folders, item);      
953                 }
954
955         } while (dbus_message_iter_next (&child));
956
957         dbus_message_unref (reply);
958
959
960         /* TODO: This is from osso source, do we need it? */
961 #if 0
962         /* Tell TaskNavigator to show "launch banner" */
963         msg = dbus_message_new_method_call (TASK_NAV_SERVICE,
964                                             APP_LAUNCH_BANNER_METHOD_PATH,
965                                             APP_LAUNCH_BANNER_METHOD_INTERFACE,
966                                             APP_LAUNCH_BANNER_METHOD);
967
968         if (msg == NULL) {
969                 g_warn ("dbus_message_new_method_call failed");
970         }
971
972
973
974         dbus_message_append_args (msg,
975                                   DBUS_TYPE_STRING,
976                                   &service,
977                                   DBUS_TYPE_INVALID);
978
979         b = dbus_connection_send (conn, msg, NULL);
980
981         if (b == NULL) {
982                 ULOG_WARN_F("dbus_connection_send failed");
983         }
984
985         dbus_message_unref (msg);
986 #endif
987
988         return TRUE;
989 }
990
991