2007-06-25 Murray Cumming <murrayc@murrayc.com>
[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         DBusConnection *con;
621         DBusMessageIter iter;
622         DBusMessageIter child;
623         DBusMessage *reply = NULL;
624         gint timeout;
625         int arg_type;
626         dbus_int64_t sd_v;
627         dbus_int64_t ed_v;
628         dbus_int32_t flags_v;
629         dbus_uint32_t size_v;
630
631         if (query == NULL) {
632                 return FALSE;
633         }
634
635         con = osso_get_dbus_connection (osso_ctx);
636
637         if (con == NULL) {
638                 g_warning ("Could not get dbus connection\n");
639                 return FALSE;
640
641         }
642
643
644         msg = dbus_message_new_method_call (MODEST_DBUS_SERVICE,
645                 MODEST_DBUS_OBJECT,
646                 MODEST_DBUS_IFACE,
647                 MODEST_DBUS_METHOD_SEARCH);
648
649     if (msg == NULL) {
650         //ULOG_ERR_F("dbus_message_new_method_call failed");
651                 return OSSO_ERROR;
652     }
653
654         if (folder == NULL) {
655                 folder = "";
656         }
657
658         sd_v = start_date;
659         ed_v = end_date;
660         flags_v = (dbus_int32_t) flags;
661         size_v = (dbus_uint32_t) min_size;
662
663         res  = dbus_message_append_args (msg,
664                                          DBUS_TYPE_STRING, &query,
665                                          DBUS_TYPE_STRING, &folder,
666                                          DBUS_TYPE_INT64, &sd_v,
667                                          DBUS_TYPE_INT64, &ed_v,
668                                          DBUS_TYPE_INT32, &flags_v,
669                                          DBUS_TYPE_UINT32, &size_v,
670                                          DBUS_TYPE_INVALID);
671
672         dbus_message_set_auto_start (msg, TRUE);
673
674         /* Use a long timeout (2 minutes) because the search currently 
675          * gets folders and messages from the servers. */
676         timeout = 120000; //milliseconds.
677         //osso_rpc_get_timeout (osso_ctx, &timeout);
678
679     /*printf("DEBUG: %s: Before dbus_connection_send_with_reply_and_block().\n", 
680                 __FUNCTION__); */
681         /* TODO: Detect the timeout somehow. */
682         DBusError err;
683         dbus_error_init (&err);
684         reply = dbus_connection_send_with_reply_and_block (con,
685                                                            msg, 
686                                                            timeout,
687                                                            &err);
688         /* printf("DEBUG: %s: dbus_connection_send_with_reply_and_block() finished.\n", 
689                 __FUNCTION__); */
690
691         dbus_message_unref (msg);
692
693         if (!reply) {
694                 g_warning("%s: dbus_connection_send_with_reply_and_block() error: %s", 
695                         __FUNCTION__, err.message);
696                 return FALSE;
697         }
698
699         switch (dbus_message_get_type (reply)) {
700
701                 case DBUS_MESSAGE_TYPE_ERROR:
702                         dbus_set_error_from_message (&err, reply);
703                         //XXX to GError?!
704                         dbus_error_free (&err);
705                         dbus_message_unref (reply);
706                         return FALSE;
707
708                 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
709                         /* ok we are good to go
710                          * lets drop outa here and handle that */
711                         break;
712                 default:
713                         //ULOG_WARN_F("got unknown message type as reply");
714                         //retval->type = DBUS_TYPE_STRING;
715                         //retval->value.s = g_strdup("Invalid return value");
716                         //XXX to GError?! 
717                         dbus_message_unref (reply);
718                         return FALSE;
719         }
720
721         g_debug ("%s: message return", __FUNCTION__);
722
723         dbus_message_iter_init (reply, &iter);
724         arg_type = dbus_message_iter_get_arg_type (&iter);
725         
726         dbus_message_iter_recurse (&iter, &child);
727         *hits = NULL;
728
729         do {
730                 ModestSearchHit *hit;
731
732                 hit = modest_dbus_message_iter_get_search_hit (&child);
733
734                 if (hit) {
735                         *hits = g_list_prepend (*hits, hit);    
736                 }
737
738         } while (dbus_message_iter_next (&child));
739
740         dbus_message_unref (reply);
741
742
743         /* TODO: This is from osso source, do we need it? */
744 #if 0
745         /* Tell TaskNavigator to show "launch banner" */
746         msg = dbus_message_new_method_call (TASK_NAV_SERVICE,
747                                             APP_LAUNCH_BANNER_METHOD_PATH,
748                                             APP_LAUNCH_BANNER_METHOD_INTERFACE,
749                                             APP_LAUNCH_BANNER_METHOD);
750
751         if (msg == NULL) {
752                 g_warn ("dbus_message_new_method_call failed");
753         }
754
755
756
757         dbus_message_append_args (msg,
758                                   DBUS_TYPE_STRING,
759                                   &service,
760                                   DBUS_TYPE_INVALID);
761
762         b = dbus_connection_send (conn, msg, NULL);
763
764         if (b == NULL) {
765                 ULOG_WARN_F("dbus_connection_send failed");
766         }
767
768         dbus_message_unref (msg);
769 #endif
770
771         return TRUE;
772 }
773
774
775 static void
776 modest_folder_result_free (ModestFolderResult *item)
777 {
778         g_free (item->folder_name);
779         g_free (item->folder_uri);
780         g_slice_free (ModestFolderResult, item);
781 }
782
783 void
784 modest_folder_result_list_free (GList *list)
785 {
786         GList *iter;
787
788         if (list == NULL) {
789                 return;
790         }
791
792         for (iter = list; iter; iter = iter->next) {
793                 modest_folder_result_free ((ModestFolderResult *) iter->data);
794         }
795
796         g_list_free (list);
797 }
798
799
800 /** Get the values from the complex type (GET_FOLDERS_RESULT_DBUS_TYPE)
801  * in the D-Bus return message. */
802 static ModestFolderResult *
803 modest_dbus_message_iter_get_folder_item (DBusMessageIter *parent)
804 {
805         gboolean error = FALSE;
806         ModestFolderResult *item = g_slice_new0 (ModestFolderResult);
807
808         int arg_type = dbus_message_iter_get_arg_type (parent);
809
810         if (arg_type != 'r') {
811                 return NULL;
812         }
813
814         DBusMessageIter  child;
815         dbus_message_iter_recurse (parent, &child);
816         
817         /* folder name: */
818         arg_type = dbus_message_iter_get_arg_type (&child);
819
820         if (arg_type != DBUS_TYPE_STRING) {
821                 error = TRUE;
822                 goto out;
823         }
824
825         item->folder_name = _dbus_iter_get_string_or_null (&child);
826         
827         
828         dbus_bool_t res = dbus_message_iter_next (&child);
829         if (res == FALSE) {
830                 error = TRUE;
831                 goto out;
832         }
833
834         /* folder URI:  */
835         arg_type = dbus_message_iter_get_arg_type (&child);
836
837         if (arg_type != DBUS_TYPE_STRING) {
838                 error = TRUE;
839                 goto out;
840         }
841
842         item->folder_uri = _dbus_iter_get_string_or_null (&child);
843
844
845 out:
846         if (error) {
847                 g_warning ("%s: Error during unmarshalling", __FUNCTION__);
848                 modest_folder_result_free (item);
849                 item = NULL;
850         }
851
852         return item;
853 }
854
855 /**
856  * libmodest_dbus_client_get_folders:
857  * @osso_ctx: A valid #osso_context_t object.
858  * @folders: A pointer to a valid GList pointer that will contain the folder items
859  * (ModestFolderResult). The list and the items must be freed by the caller 
860  * with modest_folder_result_list_free().
861  *
862  * This method will obtain a list of folders in the default account.
863  *
864  * Upon success TRUE is returned and @folders will include the folders or the list
865  * might be empty if there are no folders. The returned
866  * list must be freed with modest_folder_result_list_free ().
867  *
868  * NOTE: A folder will only be retrieved if it was previously downloaded by
869  * modest. This function does also not attempt do to remote refreshes (i.e. IMAP).
870  * 
871  * Return value: TRUE if the request succeded or FALSE for an error.
872  **/
873 gboolean
874 libmodest_dbus_client_get_folders (osso_context_t          *osso_ctx,
875                               GList                  **folders)
876 {
877         /* Initialize output argument: */
878         if (folders)
879                 *folders = NULL;
880         else
881                 return FALSE;
882
883         DBusConnection *con = osso_get_dbus_connection (osso_ctx);
884
885         if (con == NULL) {
886                 g_warning ("Could not get dbus connection\n");
887                 return FALSE;
888
889         }
890
891         DBusMessage *msg = dbus_message_new_method_call (MODEST_DBUS_SERVICE,
892                 MODEST_DBUS_OBJECT,
893                 MODEST_DBUS_IFACE,
894                 MODEST_DBUS_METHOD_GET_FOLDERS);
895
896     if (msg == NULL) {
897         //ULOG_ERR_F("dbus_message_new_method_call failed");
898                 return OSSO_ERROR;
899     }
900
901         dbus_message_set_auto_start (msg, TRUE);
902
903         /* Use a long timeout (2 minutes) because the search currently 
904          * gets folders from the servers. */
905         gint timeout = 120000;
906         //osso_rpc_get_timeout (osso_ctx, &timeout);
907
908         DBusError err;
909         dbus_error_init (&err);
910         DBusMessage *reply = dbus_connection_send_with_reply_and_block (con,
911                                                            msg, 
912                                                            timeout,
913                                                            &err);
914
915         dbus_message_unref (msg);
916         msg = NULL;
917
918         if (reply == NULL) {
919                 g_warning("%s: dbus_connection_send_with_reply_and_block() error:\n   %s", 
920                         __FUNCTION__, err.message);
921                 return FALSE;
922         }
923
924         switch (dbus_message_get_type (reply)) {
925
926                 case DBUS_MESSAGE_TYPE_ERROR:
927                         dbus_set_error_from_message (&err, reply);
928                         //XXX to GError?!
929                         dbus_error_free (&err);
930                         dbus_message_unref (reply);
931                         return FALSE;
932
933                 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
934                         /* ok we are good to go
935                          * lets drop outa here and handle that */
936                         break;
937                 default:
938                         //ULOG_WARN_F("got unknown message type as reply");
939                         //retval->type = DBUS_TYPE_STRING;
940                         //retval->value.s = g_strdup("Invalid return value");
941                         //XXX to GError?! 
942                         dbus_message_unref (reply);
943                         return FALSE;
944         }
945
946         g_debug ("%s: message return", __FUNCTION__);
947
948         DBusMessageIter iter;
949         dbus_message_iter_init (reply, &iter);
950         /* int arg_type = dbus_message_iter_get_arg_type (&iter); */
951         
952         DBusMessageIter child;
953         dbus_message_iter_recurse (&iter, &child);
954
955         do {
956                 ModestFolderResult *item = modest_dbus_message_iter_get_folder_item (&child);
957
958                 if (item) {
959                         *folders = g_list_append (*folders, item);      
960                 }
961
962         } while (dbus_message_iter_next (&child));
963
964         dbus_message_unref (reply);
965
966
967         /* TODO: This is from osso source, do we need it? */
968 #if 0
969         /* Tell TaskNavigator to show "launch banner" */
970         msg = dbus_message_new_method_call (TASK_NAV_SERVICE,
971                                             APP_LAUNCH_BANNER_METHOD_PATH,
972                                             APP_LAUNCH_BANNER_METHOD_INTERFACE,
973                                             APP_LAUNCH_BANNER_METHOD);
974
975         if (msg == NULL) {
976                 g_warn ("dbus_message_new_method_call failed");
977         }
978
979
980
981         dbus_message_append_args (msg,
982                                   DBUS_TYPE_STRING,
983                                   &service,
984                                   DBUS_TYPE_INVALID);
985
986         b = dbus_connection_send (conn, msg, NULL);
987
988         if (b == NULL) {
989                 ULOG_WARN_F("dbus_connection_send failed");
990         }
991
992         dbus_message_unref (msg);
993 #endif
994
995         return TRUE;
996 }
997
998