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