0e9668797da22f03ae4360ef4073692c92ab69ef
[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         dbus_message_iter_recurse (parent, &child);
263         
264         /* msgid  */
265         arg_type = dbus_message_iter_get_arg_type (&child);
266
267         if (arg_type != DBUS_TYPE_STRING) {
268                 error = TRUE;
269                 goto out;
270         }
271
272         hit->msgid = _dbus_iter_get_string_or_null (&child);
273
274         res = dbus_message_iter_next (&child);
275         if (res == FALSE) {
276                 error = TRUE;
277                 goto out;
278         }
279
280         /* subject  */
281         arg_type = dbus_message_iter_get_arg_type (&child);
282
283         if (arg_type != DBUS_TYPE_STRING) {
284                 error = TRUE;
285                 goto out;
286         }
287
288         hit->subject = _dbus_iter_get_string_or_null (&child);
289
290         res = dbus_message_iter_next (&child);
291         if (res == FALSE) {
292                 error = TRUE;
293                 goto out;
294         }
295
296         /* folder  */
297         arg_type = dbus_message_iter_get_arg_type (&child);
298
299         if (arg_type != DBUS_TYPE_STRING) {
300                 error = TRUE;
301                 goto out;
302         }
303
304         hit->folder = _dbus_iter_get_string_or_null (&child);
305
306         res = dbus_message_iter_next (&child);
307         if (res == FALSE) {
308                 error = TRUE;
309                 goto out;
310         }
311
312         /* sender  */
313         arg_type = dbus_message_iter_get_arg_type (&child);
314
315         if (arg_type != DBUS_TYPE_STRING) {
316                 error = TRUE;
317                 goto out;
318         }
319
320         hit->sender = _dbus_iter_get_string_or_null (&child);
321
322         res = dbus_message_iter_next (&child);
323         if (res == FALSE) {
324                 error = TRUE;
325                 goto out;
326         }
327
328         /* msize  */
329         arg_type = dbus_message_iter_get_arg_type (&child);
330
331         if (arg_type != DBUS_TYPE_UINT64) {
332                 error = TRUE;
333                 goto out;
334         }
335
336         hit->msize = _dbus_iter_get_uint64 (&child);
337
338         res = dbus_message_iter_next (&child);
339         if (res == FALSE) {
340                 error = TRUE;
341                 goto out;
342         }
343
344         /* has_attachment  */
345         arg_type = dbus_message_iter_get_arg_type (&child);
346
347         if (arg_type != DBUS_TYPE_BOOLEAN) {
348                 error = TRUE;
349                 goto out;
350         }
351
352         hit->has_attachment = _dbus_iter_get_boolean (&child); 
353
354         res = dbus_message_iter_next (&child);
355         if (res == FALSE) {
356                 error = TRUE;
357                 goto out;
358         }
359
360         /* is_unread  */
361         arg_type = dbus_message_iter_get_arg_type (&child);
362
363         if (arg_type != DBUS_TYPE_BOOLEAN) {
364                 error = TRUE;
365                 goto out;
366         }
367
368         hit->is_unread = _dbus_iter_get_boolean (&child);  
369
370         res = dbus_message_iter_next (&child);
371         if (res == FALSE) {
372                 error = TRUE;
373                 goto out;
374         }
375
376         /* msize  */
377         arg_type = dbus_message_iter_get_arg_type (&child);
378
379         if (arg_type != DBUS_TYPE_INT64) {
380                 error = TRUE;
381                 goto out;
382         }
383
384         hit->timestamp = _dbus_iter_get_int64 (&child); 
385
386         res = dbus_message_iter_next (&child);
387         if (res == TRUE) {
388                 error = TRUE;
389                 goto out;
390         }       
391
392 out:
393         if (error) {
394                 g_warning ("Error during unmarshaling");
395                 modest_search_hit_free (hit);
396                 hit = NULL;
397         }
398
399         return hit;
400 }
401
402
403 gboolean
404 libmodest_dbus_client_search (osso_context_t          *osso_ctx,
405                               const gchar             *query,
406                               const gchar             *folder,
407                               time_t                   start_date,
408                               time_t                   end_date,
409                               guint32                  min_size,
410                               ModestDBusSearchFlags    flags,
411                               GList                  **hits)
412 {
413
414         DBusMessage *msg;
415         dbus_bool_t res;
416         DBusError err;
417         DBusConnection *con;
418         DBusMessageIter iter;
419         DBusMessageIter child;
420         DBusMessage *reply = NULL;
421         gint timeout;
422         int          arg_type;
423         dbus_int64_t sd_v;
424         dbus_int64_t ed_v;
425         dbus_int32_t flags_v;
426         dbus_uint32_t size_v;
427
428
429         con = osso_get_dbus_connection (osso_ctx);
430
431         if (con == NULL) {
432                 g_warning ("Could not get dbus connection\n");
433                 return FALSE;
434
435         }
436
437
438         msg = dbus_message_new_method_call (MODEST_DBUS_SERVICE,
439                                             MODEST_DBUS_OBJECT,
440                                             MODEST_DBUS_IFACE,
441                                             MODEST_DBUS_METHOD_SEARCH);
442
443         if (msg == NULL) {
444                 //ULOG_ERR_F("dbus_message_new_method_call failed");
445                 return OSSO_ERROR;
446         }
447
448         sd_v = start_date;
449         ed_v = end_date;
450         flags_v = (dbus_int32_t) flags;
451         size_v = (dbus_uint32_t) min_size;
452
453         res  = dbus_message_append_args (msg,
454                                          DBUS_TYPE_STRING, &query,
455                                          DBUS_TYPE_STRING, &folder,
456                                          DBUS_TYPE_INT64, &sd_v,
457                                          DBUS_TYPE_INT64, &ed_v,
458                                          DBUS_TYPE_INT32, &flags_v,
459                                          DBUS_TYPE_UINT32, &size_v,
460                                          DBUS_TYPE_INVALID);
461
462         dbus_message_set_auto_start (msg, TRUE);
463
464         dbus_error_init (&err);
465
466         timeout = 1000; //XXX
467         osso_rpc_get_timeout (osso_ctx, &timeout);
468
469         reply = dbus_connection_send_with_reply_and_block (con,
470                                                            msg, 
471                                                            timeout,
472                                                            &err);
473
474         dbus_message_unref (msg);
475
476
477         if (reply == NULL) {
478             //ULOG_ERR_F("dbus_connection_send_with_reply_and_block error: %s", err.message);
479             //XXX to GError?! 
480             return FALSE;
481         }
482
483         switch (dbus_message_get_type (reply)) {
484
485                 case DBUS_MESSAGE_TYPE_ERROR:
486                         dbus_set_error_from_message (&err, reply);
487                         //XXX to GError?!
488                         dbus_error_free (&err);
489                         dbus_message_unref (reply);
490                         return FALSE;
491
492                 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
493                         /* ok we are good to go
494                          * lets drop outa here and handle that */
495                         break;
496                 default:
497                         //ULOG_WARN_F("got unknown message type as reply");
498                         //retval->type = DBUS_TYPE_STRING;
499                         //retval->value.s = g_strdup("Invalid return value");
500                         //XXX to GError?! 
501                         dbus_message_unref (reply);
502                         return FALSE;
503         }
504
505         g_debug ("message return");
506
507         dbus_message_iter_init (reply, &iter);
508         arg_type = dbus_message_iter_get_arg_type (&iter);
509         
510         dbus_message_iter_recurse (&iter, &child);
511
512         do {
513                 ModestSearchHit *hit;
514
515                 hit = dbus_message_iter_get_search_hit (&child);
516
517                 if (hit) {
518                         *hits = g_list_prepend (*hits, hit);    
519                 }
520
521         } while (dbus_message_iter_next (&child));
522
523         dbus_message_unref (reply);
524
525
526         /* TODO: This is from osso source, do we need it? */
527 #if 0
528         /* Tell TaskNavigator to show "launch banner" */
529         msg = dbus_message_new_method_call (TASK_NAV_SERVICE,
530                                             APP_LAUNCH_BANNER_METHOD_PATH,
531                                             APP_LAUNCH_BANNER_METHOD_INTERFACE,
532                                             APP_LAUNCH_BANNER_METHOD);
533
534         if (msg == NULL) {
535                 g_warn ("dbus_message_new_method_call failed");
536         }
537
538
539
540         dbus_message_append_args (msg,
541                                   DBUS_TYPE_STRING,
542                                   &service,
543                                   DBUS_TYPE_INVALID);
544
545         b = dbus_connection_send (conn, msg, NULL);
546
547         if (b == NULL) {
548                 ULOG_WARN_F("dbus_connection_send failed");
549         }
550
551         dbus_message_unref (msg);
552 #endif
553
554         return TRUE;
555 }
556