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