709b3bb67fe989422e54b29017b459dc47b9c71a
[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 static void
175 modest_search_hit_free (ModestSearchHit *hit)
176 {
177         g_free (hit->msgid);
178
179         g_slice_free (ModestSearchHit, hit);
180 }
181
182
183 static char *
184 _dbus_iter_get_string_or_null (DBusMessageIter *iter)
185 {
186         const char *string;
187         char       *ret;
188
189         dbus_message_iter_get_basic (iter, &string);
190         
191         ret = NULL;
192         if (string && strlen (string)) {
193                 ret = g_strdup (string);
194         }
195
196         return ret;
197 }
198
199 static guint64
200 _dbus_iter_get_uint64 (DBusMessageIter *iter)
201 {
202         dbus_uint64_t ui64v;
203         guint64       ret;
204
205         ui64v = 0;
206         dbus_message_iter_get_basic (iter, &ui64v);
207
208         ret = (guint64) ui64v;
209
210         return ret;
211 }
212
213
214 static gint64
215 _dbus_iter_get_int64 (DBusMessageIter *iter)
216 {
217         dbus_int64_t i64v;
218         gint64       ret;
219
220         i64v = 0;
221         dbus_message_iter_get_basic (iter, &i64v);
222
223         ret = (gint64) i64v;
224
225         return ret;
226 }
227
228 static gboolean
229 _dbus_iter_get_boolean (DBusMessageIter *iter)
230
231 {
232         dbus_bool_t  val;
233         gboolean     ret;
234
235         val = FALSE;
236         dbus_message_iter_get_basic (iter, &val);
237
238         ret = (gboolean) val;
239
240         return ret;
241 }
242
243
244 static ModestSearchHit *
245 dbus_message_iter_get_search_hit (DBusMessageIter *parent)
246 {
247         ModestSearchHit *hit;
248         DBusMessageIter  child;
249         dbus_bool_t      res;
250         int              arg_type;
251         gboolean         error;
252
253         error = FALSE;
254         hit = g_slice_new0 (ModestSearchHit);
255
256         arg_type = dbus_message_iter_get_arg_type (parent);
257
258         if (arg_type != 'r') {
259                 return NULL;
260         }
261
262         g_debug ("Umarshalling hit (%d)", dbus_message_iter_get_arg_type (parent));
263
264         dbus_message_iter_recurse (parent, &child);
265         
266         /* msgid  */
267         arg_type = dbus_message_iter_get_arg_type (&child);
268
269         if (arg_type != DBUS_TYPE_STRING) {
270                 error = TRUE;
271                 goto out;
272         }
273
274         hit->msgid = _dbus_iter_get_string_or_null (&child);
275
276         res = dbus_message_iter_next (&child);
277         if (res == FALSE) {
278                 error = TRUE;
279                 goto out;
280         }
281
282         /* subject  */
283         arg_type = dbus_message_iter_get_arg_type (&child);
284
285         if (arg_type != DBUS_TYPE_STRING) {
286                 error = TRUE;
287                 goto out;
288         }
289
290         hit->subject = _dbus_iter_get_string_or_null (&child);
291
292         res = dbus_message_iter_next (&child);
293         if (res == FALSE) {
294                 error = TRUE;
295                 goto out;
296         }
297
298         /* folder  */
299         arg_type = dbus_message_iter_get_arg_type (&child);
300
301         if (arg_type != DBUS_TYPE_STRING) {
302                 error = TRUE;
303                 goto out;
304         }
305
306         hit->folder = _dbus_iter_get_string_or_null (&child);
307
308         res = dbus_message_iter_next (&child);
309         if (res == FALSE) {
310                 error = TRUE;
311                 goto out;
312         }
313
314         /* sender  */
315         arg_type = dbus_message_iter_get_arg_type (&child);
316
317         if (arg_type != DBUS_TYPE_STRING) {
318                 error = TRUE;
319                 goto out;
320         }
321
322         hit->sender = _dbus_iter_get_string_or_null (&child);
323
324         res = dbus_message_iter_next (&child);
325         if (res == FALSE) {
326                 error = TRUE;
327                 goto out;
328         }
329
330         /* msize  */
331         arg_type = dbus_message_iter_get_arg_type (&child);
332
333         if (arg_type != DBUS_TYPE_UINT64) {
334                 error = TRUE;
335                 goto out;
336         }
337
338         hit->msize = _dbus_iter_get_uint64 (&child);
339
340         res = dbus_message_iter_next (&child);
341         if (res == FALSE) {
342                 error = TRUE;
343                 goto out;
344         }
345
346         /* has_attachment  */
347         arg_type = dbus_message_iter_get_arg_type (&child);
348
349         if (arg_type != DBUS_TYPE_BOOLEAN) {
350                 error = TRUE;
351                 goto out;
352         }
353
354         hit->has_attachment = _dbus_iter_get_boolean (&child); 
355
356         res = dbus_message_iter_next (&child);
357         if (res == FALSE) {
358                 error = TRUE;
359                 goto out;
360         }
361
362         /* is_unread  */
363         arg_type = dbus_message_iter_get_arg_type (&child);
364
365         if (arg_type != DBUS_TYPE_BOOLEAN) {
366                 error = TRUE;
367                 goto out;
368         }
369
370         hit->is_unread = _dbus_iter_get_boolean (&child);  
371
372         res = dbus_message_iter_next (&child);
373         if (res == FALSE) {
374                 error = TRUE;
375                 goto out;
376         }
377
378         /* msize  */
379         arg_type = dbus_message_iter_get_arg_type (&child);
380
381         if (arg_type != DBUS_TYPE_INT64) {
382                 error = TRUE;
383                 goto out;
384         }
385
386         hit->timestamp = _dbus_iter_get_int64 (&child); 
387
388         res = dbus_message_iter_next (&child);
389         if (res == TRUE) {
390                 error = TRUE;
391                 goto out;
392         }       
393
394 out:
395         if (error) {
396                 g_warning ("Error during unmarshaling");
397                 modest_search_hit_free (hit);
398                 hit = NULL;
399         }
400
401         return hit;
402 }
403
404
405 gboolean
406 libmodest_dbus_client_search (osso_context_t          *osso_ctx,
407                               const gchar             *query,
408                               const gchar             *folder,
409                               time_t                   start_date,
410                               time_t                   end_date,
411                               guint32                  min_size,
412                               ModestDBusSearchFlags    flags,
413                               GList                  **hits)
414 {
415
416         DBusMessage *msg;
417         dbus_bool_t res;
418         DBusError err;
419         DBusConnection *con;
420         DBusMessageIter iter;
421         DBusMessageIter child;
422         DBusMessage *reply = NULL;
423         gint timeout;
424         int          arg_type;
425         dbus_int64_t sd_v;
426         dbus_int64_t ed_v;
427         dbus_int32_t flags_v;
428         dbus_uint32_t size_v;
429
430
431         con = osso_get_dbus_connection (osso_ctx);
432
433         if (con == NULL) {
434                 g_warning ("Could not get dbus connection\n");
435                 return FALSE;
436
437         }
438
439
440         msg = dbus_message_new_method_call (MODEST_DBUS_SERVICE,
441                                             MODEST_DBUS_OBJECT,
442                                             MODEST_DBUS_IFACE,
443                                             MODEST_DBUS_METHOD_SEARCH);
444
445         if (msg == NULL) {
446                 //ULOG_ERR_F("dbus_message_new_method_call failed");
447                 return OSSO_ERROR;
448         }
449
450         sd_v = start_date;
451         ed_v = end_date;
452         flags_v = (dbus_int32_t) flags;
453         size_v = (dbus_uint32_t) min_size;
454
455         res  = dbus_message_append_args (msg,
456                                          DBUS_TYPE_STRING, &query,
457                                          DBUS_TYPE_STRING, &folder,
458                                          DBUS_TYPE_INT64, &sd_v,
459                                          DBUS_TYPE_INT64, &ed_v,
460                                          DBUS_TYPE_INT32, &flags_v,
461                                          DBUS_TYPE_UINT32, &size_v,
462                                          DBUS_TYPE_INVALID);
463
464         dbus_message_set_auto_start (msg, TRUE);
465
466         dbus_error_init (&err);
467
468         timeout = 1000; //XXX
469         osso_rpc_get_timeout (osso_ctx, &timeout);
470
471         reply = dbus_connection_send_with_reply_and_block (con,
472                                                            msg, 
473                                                            timeout,
474                                                            &err);
475
476         dbus_message_unref (msg);
477
478
479         if (reply == NULL) {
480             //ULOG_ERR_F("dbus_connection_send_with_reply_and_block error: %s", err.message);
481             //XXX to GError?! 
482             return FALSE;
483         }
484
485         switch (dbus_message_get_type (reply)) {
486
487                 case DBUS_MESSAGE_TYPE_ERROR:
488                         dbus_set_error_from_message (&err, reply);
489                         //XXX to GError?!
490                         dbus_error_free (&err);
491                         dbus_message_unref (reply);
492                         return FALSE;
493
494                 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
495                         /* ok we are good to go
496                          * lets drop outa here and handle that */
497                         break;
498                 default:
499                         //ULOG_WARN_F("got unknown message type as reply");
500                         //retval->type = DBUS_TYPE_STRING;
501                         //retval->value.s = g_strdup("Invalid return value");
502                         //XXX to GError?! 
503                         dbus_message_unref (reply);
504                         return FALSE;
505         }
506
507         g_debug ("message return");
508
509         dbus_message_iter_init (reply, &iter);
510         arg_type = dbus_message_iter_get_arg_type (&iter);
511         
512         g_debug ("iter type: %d", arg_type);
513         dbus_message_iter_recurse (&iter, &child);
514         g_debug ("recursed");
515
516         do {
517                 ModestSearchHit *hit;
518
519                 hit = dbus_message_iter_get_search_hit (&child);
520
521                 if (hit) {
522                         *hits = g_list_prepend (*hits, hit);    
523                 }
524
525         } while (dbus_message_iter_next (&child));
526
527         dbus_message_unref (reply);
528
529         g_debug ("Done unmarshalling message");
530 #if 0
531         /* Tell TaskNavigator to show "launch banner" */
532         msg = dbus_message_new_method_call (TASK_NAV_SERVICE,
533                                             APP_LAUNCH_BANNER_METHOD_PATH,
534                                             APP_LAUNCH_BANNER_METHOD_INTERFACE,
535                                             APP_LAUNCH_BANNER_METHOD);
536
537         if (msg == NULL) {
538                 g_warn ("dbus_message_new_method_call failed");
539         }
540
541
542
543         dbus_message_append_args (msg,
544                                   DBUS_TYPE_STRING,
545                                   &service,
546                                   DBUS_TYPE_INVALID);
547
548         b = dbus_connection_send (conn, msg, NULL);
549
550         if (b == NULL) {
551                 ULOG_WARN_F("dbus_connection_send failed");
552         }
553
554         dbus_message_unref (msg);
555 #endif
556
557         return TRUE;
558 }
559