76054f615f603d2f83ea8e878a7ab2667f818e11
[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 gboolean
39 libmodest_dbus_client_send_mail (osso_context_t *osso_context, const gchar *to, const gchar *cc, 
40         const gchar *bcc, const gchar* subject, const gchar* body, GSList *attachments)
41 {
42         osso_rpc_t retval;
43         const osso_return_t ret = osso_rpc_run_with_defaults(osso_context, 
44                    MODEST_DBUS_NAME, 
45                    MODEST_DBUS_METHOD_SEND_MAIL, &retval, 
46                    DBUS_TYPE_STRING, to, 
47                    DBUS_TYPE_STRING, cc, 
48                    DBUS_TYPE_STRING, bcc, 
49                    DBUS_TYPE_STRING, subject, 
50                    DBUS_TYPE_STRING, body, 
51                    DBUS_TYPE_INVALID);
52                 
53         if (ret != OSSO_OK) {
54                 printf("debug: osso_rpc_run() failed.\n");
55                 return FALSE;
56         } else {
57                 printf("debug: osso_rpc_run() succeeded.\n");
58         }
59         
60         osso_rpc_free_val(&retval);
61         
62         return TRUE;
63 }
64         
65 gboolean 
66 libmodest_dbus_client_mail_to (osso_context_t *osso_context, const gchar *mailto_uri)
67 {
68         osso_rpc_t retval;
69         const osso_return_t ret = osso_rpc_run_with_defaults(osso_context, 
70                    MODEST_DBUS_NAME, 
71                    MODEST_DBUS_METHOD_MAIL_TO, &retval, 
72                    DBUS_TYPE_STRING, mailto_uri, 
73                    DBUS_TYPE_INVALID);
74                 
75         if (ret != OSSO_OK) {
76                 printf("debug: osso_rpc_run() failed.\n");
77                 return FALSE;
78         } else {
79                 printf("debug: osso_rpc_run() succeeded.\n");
80         }
81         
82         osso_rpc_free_val(&retval);
83         
84         return TRUE;
85 }
86
87 gboolean
88 libmodest_dbus_client_compose_mail (osso_context_t *osso_context, const gchar *to, const gchar *cc, 
89         const gchar *bcc, const gchar* subject, const gchar* body, GSList *attachments)
90 {
91         osso_rpc_t retval;
92         gchar *attachments_str = NULL;
93         gchar *tmp = NULL;
94         GSList *next = NULL;
95         
96         attachments_str = g_strdup( (gchar *) attachments->data );
97         
98         for (next = g_slist_next(attachments); next != NULL; next = g_slist_next(next))
99         {
100                 tmp = g_strconcat(attachments_str, ",", (gchar *) (next->data), NULL);
101                 g_free(attachments_str);
102                 attachments_str = tmp;
103                 if (attachments_str == NULL) {
104                         return OSSO_ERROR;
105                 }
106         }
107
108         const osso_return_t ret = osso_rpc_run_with_defaults(osso_context, 
109                    MODEST_DBUS_NAME, 
110                    MODEST_DBUS_METHOD_COMPOSE_MAIL, &retval, 
111                    DBUS_TYPE_STRING, to, 
112                    DBUS_TYPE_STRING, cc, 
113                    DBUS_TYPE_STRING, bcc, 
114                    DBUS_TYPE_STRING, subject, 
115                    DBUS_TYPE_STRING, body,
116                    DBUS_TYPE_STRING, attachments_str,
117                    DBUS_TYPE_INVALID);
118                 
119         if (ret != OSSO_OK) {
120                 printf("debug: osso_rpc_run() failed.\n");
121                 return FALSE;
122         } else {
123                 printf("debug: osso_rpc_run() succeeded.\n");
124         }
125         
126         osso_rpc_free_val(&retval);
127         
128         return TRUE;
129 }
130
131 gboolean 
132 libmodest_dbus_client_open_message (osso_context_t *osso_context, const gchar *mail_uri)
133 {
134         osso_rpc_t retval;
135         const osso_return_t ret = osso_rpc_run_with_defaults(osso_context, 
136                    MODEST_DBUS_NAME, 
137                    MODEST_DBUS_METHOD_OPEN_MESSAGE, &retval, 
138                    DBUS_TYPE_STRING, mail_uri, 
139                    DBUS_TYPE_INVALID);
140                 
141         if (ret != OSSO_OK) {
142                 printf("debug: osso_rpc_run() failed.\n");
143                 return FALSE;
144         } else {
145                 printf("debug: osso_rpc_run() succeeded.\n");
146         }
147         
148         osso_rpc_free_val(&retval);
149         
150         return TRUE;
151 }
152
153 gboolean 
154 libmodest_dbus_client_send_and_receive (osso_context_t *osso_context)
155 {
156         osso_rpc_t retval;
157         const osso_return_t ret = osso_rpc_run_with_defaults(osso_context, 
158                    MODEST_DBUS_NAME, 
159                    MODEST_DBUS_METHOD_SEND_RECEIVE, &retval, 
160                    DBUS_TYPE_INVALID);
161                 
162         if (ret != OSSO_OK) {
163                 printf("debug: osso_rpc_run() failed.\n");
164                 return FALSE;
165         } else {
166                 printf("debug: osso_rpc_run() succeeded.\n");
167         }
168         
169         osso_rpc_free_val(&retval);
170         
171         return TRUE;
172 }
173
174 gboolean
175 libmodest_dbus_client_delete_message (osso_context_t   *osso_ctx,
176                                       const char       *msg_uri)
177 {
178         osso_rpc_t    retval;
179         osso_return_t ret;
180        
181         ret = osso_rpc_run_with_defaults (osso_ctx, 
182                                           MODEST_DBUS_NAME, 
183                                           MODEST_DBUS_METHOD_DELETE_MESSAGE, &retval, 
184                                           DBUS_TYPE_STRING, msg_uri, 
185                                           DBUS_TYPE_INVALID);
186                 
187         if (ret != OSSO_OK) {
188                 g_debug ("debug: osso_rpc_run() failed.\n");
189         } else {
190                 g_debug ("debug: osso_rpc_run() succeeded.\n");
191         }
192         
193         osso_rpc_free_val (&retval);
194
195         return ret == OSSO_OK;
196 }
197
198 void
199 modest_search_hit_free (ModestSearchHit *hit)
200 {
201         g_free (hit->msgid);
202         g_slice_free (ModestSearchHit, hit);
203 }
204
205 void
206 modest_search_hit_list_free (GList *hits)
207 {
208         GList *iter;
209
210         if (hits == NULL) {
211                 return;
212         }
213
214         for (iter = hits; iter; iter = iter->next) {
215                 modest_search_hit_free ((ModestSearchHit *) iter->data);
216         }
217
218         g_list_free (hits);
219 }
220
221 static char *
222 _dbus_iter_get_string_or_null (DBusMessageIter *iter)
223 {
224         const char *string;
225         char       *ret;
226
227         dbus_message_iter_get_basic (iter, &string);
228         
229         ret = NULL;
230         if (string && strlen (string)) {
231                 ret = g_strdup (string);
232         }
233
234         return ret;
235 }
236
237 static guint64
238 _dbus_iter_get_uint64 (DBusMessageIter *iter)
239 {
240         dbus_uint64_t ui64v;
241         guint64       ret;
242
243         ui64v = 0;
244         dbus_message_iter_get_basic (iter, &ui64v);
245
246         ret = (guint64) ui64v;
247
248         return ret;
249 }
250
251
252 static gint64
253 _dbus_iter_get_int64 (DBusMessageIter *iter)
254 {
255         dbus_int64_t i64v;
256         gint64       ret;
257
258         i64v = 0;
259         dbus_message_iter_get_basic (iter, &i64v);
260
261         ret = (gint64) i64v;
262
263         return ret;
264 }
265
266 static gboolean
267 _dbus_iter_get_boolean (DBusMessageIter *iter)
268
269 {
270         dbus_bool_t  val;
271         gboolean     ret;
272
273         val = FALSE;
274         dbus_message_iter_get_basic (iter, &val);
275
276         ret = (gboolean) val;
277
278         return ret;
279 }
280
281
282 static ModestSearchHit *
283 dbus_message_iter_get_search_hit (DBusMessageIter *parent)
284 {
285         ModestSearchHit *hit;
286         DBusMessageIter  child;
287         dbus_bool_t      res;
288         int              arg_type;
289         gboolean         error;
290
291         error = FALSE;
292         hit = g_slice_new0 (ModestSearchHit);
293
294         arg_type = dbus_message_iter_get_arg_type (parent);
295
296         if (arg_type != 'r') {
297                 return NULL;
298         }
299
300         dbus_message_iter_recurse (parent, &child);
301         
302         /* msgid  */
303         arg_type = dbus_message_iter_get_arg_type (&child);
304
305         if (arg_type != DBUS_TYPE_STRING) {
306                 error = TRUE;
307                 goto out;
308         }
309
310         hit->msgid = _dbus_iter_get_string_or_null (&child);
311
312         res = dbus_message_iter_next (&child);
313         if (res == FALSE) {
314                 error = TRUE;
315                 goto out;
316         }
317
318         /* subject  */
319         arg_type = dbus_message_iter_get_arg_type (&child);
320
321         if (arg_type != DBUS_TYPE_STRING) {
322                 error = TRUE;
323                 goto out;
324         }
325
326         hit->subject = _dbus_iter_get_string_or_null (&child);
327
328         res = dbus_message_iter_next (&child);
329         if (res == FALSE) {
330                 error = TRUE;
331                 goto out;
332         }
333
334         /* folder  */
335         arg_type = dbus_message_iter_get_arg_type (&child);
336
337         if (arg_type != DBUS_TYPE_STRING) {
338                 error = TRUE;
339                 goto out;
340         }
341
342         hit->folder = _dbus_iter_get_string_or_null (&child);
343
344         res = dbus_message_iter_next (&child);
345         if (res == FALSE) {
346                 error = TRUE;
347                 goto out;
348         }
349
350         /* sender  */
351         arg_type = dbus_message_iter_get_arg_type (&child);
352
353         if (arg_type != DBUS_TYPE_STRING) {
354                 error = TRUE;
355                 goto out;
356         }
357
358         hit->sender = _dbus_iter_get_string_or_null (&child);
359
360         res = dbus_message_iter_next (&child);
361         if (res == FALSE) {
362                 error = TRUE;
363                 goto out;
364         }
365
366         /* msize  */
367         arg_type = dbus_message_iter_get_arg_type (&child);
368
369         if (arg_type != DBUS_TYPE_UINT64) {
370                 error = TRUE;
371                 goto out;
372         }
373
374         hit->msize = _dbus_iter_get_uint64 (&child);
375
376         res = dbus_message_iter_next (&child);
377         if (res == FALSE) {
378                 error = TRUE;
379                 goto out;
380         }
381
382         /* has_attachment  */
383         arg_type = dbus_message_iter_get_arg_type (&child);
384
385         if (arg_type != DBUS_TYPE_BOOLEAN) {
386                 error = TRUE;
387                 goto out;
388         }
389
390         hit->has_attachment = _dbus_iter_get_boolean (&child); 
391
392         res = dbus_message_iter_next (&child);
393         if (res == FALSE) {
394                 error = TRUE;
395                 goto out;
396         }
397
398         /* is_unread  */
399         arg_type = dbus_message_iter_get_arg_type (&child);
400
401         if (arg_type != DBUS_TYPE_BOOLEAN) {
402                 error = TRUE;
403                 goto out;
404         }
405
406         hit->is_unread = _dbus_iter_get_boolean (&child);  
407
408         res = dbus_message_iter_next (&child);
409         if (res == FALSE) {
410                 error = TRUE;
411                 goto out;
412         }
413
414         /* msize  */
415         arg_type = dbus_message_iter_get_arg_type (&child);
416
417         if (arg_type != DBUS_TYPE_INT64) {
418                 error = TRUE;
419                 goto out;
420         }
421
422         hit->timestamp = _dbus_iter_get_int64 (&child); 
423
424         res = dbus_message_iter_next (&child);
425         if (res == TRUE) {
426                 error = TRUE;
427                 goto out;
428         }       
429
430 out:
431         if (error) {
432                 g_warning ("Error during unmarshaling");
433                 modest_search_hit_free (hit);
434                 hit = NULL;
435         }
436
437         return hit;
438 }
439
440
441 gboolean
442 libmodest_dbus_client_search (osso_context_t          *osso_ctx,
443                               const gchar             *query,
444                               const gchar             *folder,
445                               time_t                   start_date,
446                               time_t                   end_date,
447                               guint32                  min_size,
448                               ModestDBusSearchFlags    flags,
449                               GList                  **hits)
450 {
451
452         DBusMessage *msg;
453         dbus_bool_t res;
454         DBusError err;
455         DBusConnection *con;
456         DBusMessageIter iter;
457         DBusMessageIter child;
458         DBusMessage *reply = NULL;
459         gint timeout;
460         int          arg_type;
461         dbus_int64_t sd_v;
462         dbus_int64_t ed_v;
463         dbus_int32_t flags_v;
464         dbus_uint32_t size_v;
465
466
467         con = osso_get_dbus_connection (osso_ctx);
468
469         if (con == NULL) {
470                 g_warning ("Could not get dbus connection\n");
471                 return FALSE;
472
473         }
474
475
476         msg = dbus_message_new_method_call (MODEST_DBUS_SERVICE,
477                                             MODEST_DBUS_OBJECT,
478                                             MODEST_DBUS_IFACE,
479                                             MODEST_DBUS_METHOD_SEARCH);
480
481         if (msg == NULL) {
482                 //ULOG_ERR_F("dbus_message_new_method_call failed");
483                 return OSSO_ERROR;
484         }
485
486         sd_v = start_date;
487         ed_v = end_date;
488         flags_v = (dbus_int32_t) flags;
489         size_v = (dbus_uint32_t) min_size;
490
491         res  = dbus_message_append_args (msg,
492                                          DBUS_TYPE_STRING, &query,
493                                          DBUS_TYPE_STRING, &folder,
494                                          DBUS_TYPE_INT64, &sd_v,
495                                          DBUS_TYPE_INT64, &ed_v,
496                                          DBUS_TYPE_INT32, &flags_v,
497                                          DBUS_TYPE_UINT32, &size_v,
498                                          DBUS_TYPE_INVALID);
499
500         dbus_message_set_auto_start (msg, TRUE);
501
502         dbus_error_init (&err);
503
504         timeout = 1000; //XXX
505         osso_rpc_get_timeout (osso_ctx, &timeout);
506
507         reply = dbus_connection_send_with_reply_and_block (con,
508                                                            msg, 
509                                                            timeout,
510                                                            &err);
511
512         dbus_message_unref (msg);
513
514
515         if (reply == NULL) {
516             //ULOG_ERR_F("dbus_connection_send_with_reply_and_block error: %s", err.message);
517             //XXX to GError?! 
518             return FALSE;
519         }
520
521         switch (dbus_message_get_type (reply)) {
522
523                 case DBUS_MESSAGE_TYPE_ERROR:
524                         dbus_set_error_from_message (&err, reply);
525                         //XXX to GError?!
526                         dbus_error_free (&err);
527                         dbus_message_unref (reply);
528                         return FALSE;
529
530                 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
531                         /* ok we are good to go
532                          * lets drop outa here and handle that */
533                         break;
534                 default:
535                         //ULOG_WARN_F("got unknown message type as reply");
536                         //retval->type = DBUS_TYPE_STRING;
537                         //retval->value.s = g_strdup("Invalid return value");
538                         //XXX to GError?! 
539                         dbus_message_unref (reply);
540                         return FALSE;
541         }
542
543         g_debug ("message return");
544
545         dbus_message_iter_init (reply, &iter);
546         arg_type = dbus_message_iter_get_arg_type (&iter);
547         
548         dbus_message_iter_recurse (&iter, &child);
549
550         do {
551                 ModestSearchHit *hit;
552
553                 hit = dbus_message_iter_get_search_hit (&child);
554
555                 if (hit) {
556                         *hits = g_list_prepend (*hits, hit);    
557                 }
558
559         } while (dbus_message_iter_next (&child));
560
561         dbus_message_unref (reply);
562
563
564         /* TODO: This is from osso source, do we need it? */
565 #if 0
566         /* Tell TaskNavigator to show "launch banner" */
567         msg = dbus_message_new_method_call (TASK_NAV_SERVICE,
568                                             APP_LAUNCH_BANNER_METHOD_PATH,
569                                             APP_LAUNCH_BANNER_METHOD_INTERFACE,
570                                             APP_LAUNCH_BANNER_METHOD);
571
572         if (msg == NULL) {
573                 g_warn ("dbus_message_new_method_call failed");
574         }
575
576
577
578         dbus_message_append_args (msg,
579                                   DBUS_TYPE_STRING,
580                                   &service,
581                                   DBUS_TYPE_INVALID);
582
583         b = dbus_connection_send (conn, msg, NULL);
584
585         if (b == NULL) {
586                 ULOG_WARN_F("dbus_connection_send failed");
587         }
588
589         dbus_message_unref (msg);
590 #endif
591
592         return TRUE;
593 }
594