29bbbefe9d6a077f4eada4e76b84931caa1e26b9
[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  * libmodest_dbus_client_send_mail:
40  * @osso_context: a valid #osso_context_t object.
41  * @to: The Recipients (From: line)
42  * @cc: Recipients for carbon copies
43  * @bcc: Recipients for blind carbon copies
44  * @subject: Subject line
45  * @body: The actual body of the mail to send
46  * @attachments: Additional list of attachments
47  * 
48  * This function will try to do a remote procedure call (rpc)
49  * into modest (or start it if necessary) and open a composer
50  * window with the supplied parameters prefilled.
51  *
52  * Return value: Whether or not the rpc call to modest
53  * was successfull
54  **/
55
56 gboolean
57 libmodest_dbus_client_send_mail (osso_context_t *osso_context, const gchar *to, const gchar *cc, 
58         const gchar *bcc, const gchar* subject, const gchar* body, GSList *attachments)
59 {
60         osso_rpc_t retval;
61         const osso_return_t ret = osso_rpc_run_with_defaults(osso_context, 
62                    MODEST_DBUS_NAME, 
63                    MODEST_DBUS_METHOD_SEND_MAIL, &retval, 
64                    DBUS_TYPE_STRING, to, 
65                    DBUS_TYPE_STRING, cc, 
66                    DBUS_TYPE_STRING, bcc, 
67                    DBUS_TYPE_STRING, subject, 
68                    DBUS_TYPE_STRING, body, 
69                    DBUS_TYPE_INVALID);
70                 
71         if (ret != OSSO_OK) {
72                 printf("debug: osso_rpc_run() failed.\n");
73                 return FALSE;
74         } else {
75                 printf("debug: osso_rpc_run() succeeded.\n");
76         }
77         
78         osso_rpc_free_val(&retval);
79         
80         return TRUE;
81 }
82         
83 gboolean 
84 libmodest_dbus_client_mail_to (osso_context_t *osso_context, const gchar *mailto_uri)
85 {
86         osso_rpc_t retval;
87         const osso_return_t ret = osso_rpc_run_with_defaults(osso_context, 
88                    MODEST_DBUS_NAME, 
89                    MODEST_DBUS_METHOD_MAIL_TO, &retval, 
90                    DBUS_TYPE_STRING, mailto_uri, 
91                    DBUS_TYPE_INVALID);
92                 
93         if (ret != OSSO_OK) {
94                 printf("debug: osso_rpc_run() failed.\n");
95                 return FALSE;
96         } else {
97                 printf("debug: osso_rpc_run() succeeded.\n");
98         }
99         
100         osso_rpc_free_val(&retval);
101         
102         return TRUE;
103 }
104
105 gboolean
106 libmodest_dbus_client_compose_mail (osso_context_t *osso_context, const gchar *to, const gchar *cc, 
107         const gchar *bcc, const gchar* subject, const gchar* body, GSList *attachments)
108 {
109         osso_rpc_t retval;
110         gchar *attachments_str = NULL;
111         gchar *tmp = NULL;
112         GSList *next = NULL;
113         
114         attachments_str = g_strdup( (gchar *) attachments->data );
115         
116         for (next = g_slist_next(attachments); next != NULL; next = g_slist_next(next))
117         {
118                 tmp = g_strconcat(attachments_str, ",", (gchar *) (next->data), NULL);
119                 g_free(attachments_str);
120                 attachments_str = tmp;
121                 if (attachments_str == NULL) {
122                         return OSSO_ERROR;
123                 }
124         }
125
126         const osso_return_t ret = osso_rpc_run_with_defaults(osso_context, 
127                    MODEST_DBUS_NAME, 
128                    MODEST_DBUS_METHOD_COMPOSE_MAIL, &retval, 
129                    DBUS_TYPE_STRING, to, 
130                    DBUS_TYPE_STRING, cc, 
131                    DBUS_TYPE_STRING, bcc, 
132                    DBUS_TYPE_STRING, subject, 
133                    DBUS_TYPE_STRING, body,
134                    DBUS_TYPE_STRING, attachments_str,
135                    DBUS_TYPE_INVALID);
136                 
137         if (ret != OSSO_OK) {
138                 printf("debug: osso_rpc_run() failed.\n");
139                 return FALSE;
140         } else {
141                 printf("debug: osso_rpc_run() succeeded.\n");
142         }
143         
144         osso_rpc_free_val(&retval);
145         
146         return TRUE;
147 }
148
149 /**
150  * libmodest_dbus_client_dopen_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: osso_rpc_run() failed.\n");
173                 return FALSE;
174         } else {
175                 printf("debug: osso_rpc_run() succeeded.\n");
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: osso_rpc_run() failed.\n");
194                 return FALSE;
195         } else {
196                 printf("debug: osso_rpc_run() succeeded.\n");
197         }
198         
199         osso_rpc_free_val(&retval);
200         
201         return TRUE;
202 }
203
204 /**
205  * libmodest_dbus_client_delete_message:
206  * @osso_context: a valid #osso_context_t object.
207  * @msg_uri: A valid url to a mail 
208  *
209  * This method will try to find the message supplied
210  * by @msg_uri and if found delete it. It will use
211  * remote procedure calls (rpc) over dbus to do so.
212  * 
213  * Return value: TRUE on successs, FALSE on error
214  **/
215 gboolean
216 libmodest_dbus_client_delete_message (osso_context_t   *osso_ctx,
217                                       const char       *msg_uri)
218 {
219         osso_rpc_t    retval;
220         osso_return_t ret;
221        
222         ret = osso_rpc_run_with_defaults (osso_ctx, 
223                                           MODEST_DBUS_NAME, 
224                                           MODEST_DBUS_METHOD_DELETE_MESSAGE, &retval, 
225                                           DBUS_TYPE_STRING, msg_uri, 
226                                           DBUS_TYPE_INVALID);
227                 
228         if (ret != OSSO_OK) {
229                 g_debug ("debug: osso_rpc_run() failed.\n");
230         } else {
231                 g_debug ("debug: osso_rpc_run() succeeded.\n");
232         }
233         
234         osso_rpc_free_val (&retval);
235
236         return ret == OSSO_OK;
237 }
238
239 void
240 modest_search_hit_free (ModestSearchHit *hit)
241 {
242         g_free (hit->msgid);
243         g_slice_free (ModestSearchHit, hit);
244 }
245
246 void
247 modest_search_hit_list_free (GList *hits)
248 {
249         GList *iter;
250
251         if (hits == NULL) {
252                 return;
253         }
254
255         for (iter = hits; iter; iter = iter->next) {
256                 modest_search_hit_free ((ModestSearchHit *) iter->data);
257         }
258
259         g_list_free (hits);
260 }
261
262 static char *
263 _dbus_iter_get_string_or_null (DBusMessageIter *iter)
264 {
265         const char *string;
266         char       *ret;
267
268         dbus_message_iter_get_basic (iter, &string);
269         
270         ret = NULL;
271         if (string && strlen (string)) {
272                 ret = g_strdup (string);
273         }
274
275         return ret;
276 }
277
278 static guint64
279 _dbus_iter_get_uint64 (DBusMessageIter *iter)
280 {
281         dbus_uint64_t ui64v;
282         guint64       ret;
283
284         ui64v = 0;
285         dbus_message_iter_get_basic (iter, &ui64v);
286
287         ret = (guint64) ui64v;
288
289         return ret;
290 }
291
292
293 static gint64
294 _dbus_iter_get_int64 (DBusMessageIter *iter)
295 {
296         dbus_int64_t i64v;
297         gint64       ret;
298
299         i64v = 0;
300         dbus_message_iter_get_basic (iter, &i64v);
301
302         ret = (gint64) i64v;
303
304         return ret;
305 }
306
307 static gboolean
308 _dbus_iter_get_boolean (DBusMessageIter *iter)
309
310 {
311         dbus_bool_t  val;
312         gboolean     ret;
313
314         val = FALSE;
315         dbus_message_iter_get_basic (iter, &val);
316
317         ret = (gboolean) val;
318
319         return ret;
320 }
321
322
323 static ModestSearchHit *
324 dbus_message_iter_get_search_hit (DBusMessageIter *parent)
325 {
326         ModestSearchHit *hit;
327         DBusMessageIter  child;
328         dbus_bool_t      res;
329         int              arg_type;
330         gboolean         error;
331
332         error = FALSE;
333         hit = g_slice_new0 (ModestSearchHit);
334
335         arg_type = dbus_message_iter_get_arg_type (parent);
336
337         if (arg_type != 'r') {
338                 return NULL;
339         }
340
341         dbus_message_iter_recurse (parent, &child);
342         
343         /* msgid  */
344         arg_type = dbus_message_iter_get_arg_type (&child);
345
346         if (arg_type != DBUS_TYPE_STRING) {
347                 error = TRUE;
348                 goto out;
349         }
350
351         hit->msgid = _dbus_iter_get_string_or_null (&child);
352
353         res = dbus_message_iter_next (&child);
354         if (res == FALSE) {
355                 error = TRUE;
356                 goto out;
357         }
358
359         /* subject  */
360         arg_type = dbus_message_iter_get_arg_type (&child);
361
362         if (arg_type != DBUS_TYPE_STRING) {
363                 error = TRUE;
364                 goto out;
365         }
366
367         hit->subject = _dbus_iter_get_string_or_null (&child);
368
369         res = dbus_message_iter_next (&child);
370         if (res == FALSE) {
371                 error = TRUE;
372                 goto out;
373         }
374
375         /* folder  */
376         arg_type = dbus_message_iter_get_arg_type (&child);
377
378         if (arg_type != DBUS_TYPE_STRING) {
379                 error = TRUE;
380                 goto out;
381         }
382
383         hit->folder = _dbus_iter_get_string_or_null (&child);
384
385         res = dbus_message_iter_next (&child);
386         if (res == FALSE) {
387                 error = TRUE;
388                 goto out;
389         }
390
391         /* sender  */
392         arg_type = dbus_message_iter_get_arg_type (&child);
393
394         if (arg_type != DBUS_TYPE_STRING) {
395                 error = TRUE;
396                 goto out;
397         }
398
399         hit->sender = _dbus_iter_get_string_or_null (&child);
400
401         res = dbus_message_iter_next (&child);
402         if (res == FALSE) {
403                 error = TRUE;
404                 goto out;
405         }
406
407         /* msize  */
408         arg_type = dbus_message_iter_get_arg_type (&child);
409
410         if (arg_type != DBUS_TYPE_UINT64) {
411                 error = TRUE;
412                 goto out;
413         }
414
415         hit->msize = _dbus_iter_get_uint64 (&child);
416
417         res = dbus_message_iter_next (&child);
418         if (res == FALSE) {
419                 error = TRUE;
420                 goto out;
421         }
422
423         /* has_attachment  */
424         arg_type = dbus_message_iter_get_arg_type (&child);
425
426         if (arg_type != DBUS_TYPE_BOOLEAN) {
427                 error = TRUE;
428                 goto out;
429         }
430
431         hit->has_attachment = _dbus_iter_get_boolean (&child); 
432
433         res = dbus_message_iter_next (&child);
434         if (res == FALSE) {
435                 error = TRUE;
436                 goto out;
437         }
438
439         /* is_unread  */
440         arg_type = dbus_message_iter_get_arg_type (&child);
441
442         if (arg_type != DBUS_TYPE_BOOLEAN) {
443                 error = TRUE;
444                 goto out;
445         }
446
447         hit->is_unread = _dbus_iter_get_boolean (&child);  
448
449         res = dbus_message_iter_next (&child);
450         if (res == FALSE) {
451                 error = TRUE;
452                 goto out;
453         }
454
455         /* msize  */
456         arg_type = dbus_message_iter_get_arg_type (&child);
457
458         if (arg_type != DBUS_TYPE_INT64) {
459                 error = TRUE;
460                 goto out;
461         }
462
463         hit->timestamp = _dbus_iter_get_int64 (&child); 
464
465         res = dbus_message_iter_next (&child);
466         if (res == TRUE) {
467                 error = TRUE;
468                 goto out;
469         }       
470
471 out:
472         if (error) {
473                 g_warning ("Error during unmarshaling");
474                 modest_search_hit_free (hit);
475                 hit = NULL;
476         }
477
478         return hit;
479 }
480
481 /**
482  * libmodest_dbus_client_search:
483  * @osso_ctx: A valid #osso_context_t object.
484  * @query: The term to search for.
485  * @folder: An url to specific folder or %NULL to search everywhere.
486  * @start_date: Search hits before this date will be ignored.
487  * @end_date: Search hits after this date will be ignored.
488  * @min_size: Messagers smaller then this size will be ingored.
489  * @flags: A list of flags where to search so the documentation 
490  * of %ModestDBusSearchFlags for details.
491  * @hits: A pointer to a valid GList pointer that will contain the search
492  * hits (must be freed by the caller).
493  *
494  * This method will search the folder specified by a valid url in @folder or all
495  * known accounts (local and remote) if %NULL for matches of the search term(s)
496  * specified in @query. It is legal to specify 0 in @start_date, @end_date and
497  * @min_size to ignore these parameters during the search otherwise those message
498  * that do not meet the specifed dates or size will be ignored.
499  * Where to search, be it subject, sender or the whole body can be specified by
500  * the @flags parameter.
501  *
502  * Upon success TRUE is returned and @hits will include the search hits or the list
503  * migh be empty if none of the messages matched the search criteria. The returned
504  * list must be freed with modest_search_hit_list_free (). It is save to pass
505  * %NULL to this function so you can call this function on the result list no matter
506  * if a hit was found or not (means the list is empty - i.e. %NULL)
507  * FALSE will only be return if an error during the remote procedure call (rpc) 
508  * occured or if the specified folder could not be found.
509  *
510  * NOTE: The body of a message can only be searched if it was previously downloaded by
511  * modest. This function does also not attempt do to remote searches (i.e. IMAP search).
512  *
513  * Example to search every account for message containing "no":
514  * <informalexample><programlisting>
515  * ModestDBusSearchFlags  flags;
516  * osso_context_t        *osso_ctx;
517  * GList                 *hit;
518  * GList                 *iter;
519  * gboolean               res;
520  * 
521  * [...] Initialize osso context [...]
522  *
523  * res = libmodest_dbus_client_search (osso_context,
524  *                                     "no",
525  *                                     NULL,
526  *                                     0,
527  *                                     0,
528  *                                     0,
529  *                                     flags,
530  *                                     &hits);
531  * 
532  * for (iter = hits; iter; iter = iter->next) {
533  *      ModestSearchHit *hit = (ModestSearchHit *) iter->data;
534  *      
535  *      [...] Do something with the hit [...]
536  *
537  *      }
538  *
539  *      modest_search_hit_list_free (hits);
540  * </programlisting></informalexample>
541  * 
542  * Return value: TRUE if the search succeded or FALSE for an error during the search
543  **/
544 gboolean
545 libmodest_dbus_client_search (osso_context_t          *osso_ctx,
546                               const gchar             *query,
547                               const gchar             *folder,
548                               time_t                   start_date,
549                               time_t                   end_date,
550                               guint32                  min_size,
551                               ModestDBusSearchFlags    flags,
552                               GList                  **hits)
553 {
554
555         DBusMessage *msg;
556         dbus_bool_t res;
557         DBusError err;
558         DBusConnection *con;
559         DBusMessageIter iter;
560         DBusMessageIter child;
561         DBusMessage *reply = NULL;
562         gint timeout;
563         int          arg_type;
564         dbus_int64_t sd_v;
565         dbus_int64_t ed_v;
566         dbus_int32_t flags_v;
567         dbus_uint32_t size_v;
568
569         if (query == NULL) {
570                 return FALSE;
571         }
572
573         con = osso_get_dbus_connection (osso_ctx);
574
575         if (con == NULL) {
576                 g_warning ("Could not get dbus connection\n");
577                 return FALSE;
578
579         }
580
581
582         msg = dbus_message_new_method_call (MODEST_DBUS_SERVICE,
583                                             MODEST_DBUS_OBJECT,
584                                             MODEST_DBUS_IFACE,
585                                             MODEST_DBUS_METHOD_SEARCH);
586
587         if (msg == NULL) {
588                 //ULOG_ERR_F("dbus_message_new_method_call failed");
589                 return OSSO_ERROR;
590         }
591
592         if (folder == NULL) {
593                 folder = "";
594         }
595
596         sd_v = start_date;
597         ed_v = end_date;
598         flags_v = (dbus_int32_t) flags;
599         size_v = (dbus_uint32_t) min_size;
600
601         res  = dbus_message_append_args (msg,
602                                          DBUS_TYPE_STRING, &query,
603                                          DBUS_TYPE_STRING, &folder,
604                                          DBUS_TYPE_INT64, &sd_v,
605                                          DBUS_TYPE_INT64, &ed_v,
606                                          DBUS_TYPE_INT32, &flags_v,
607                                          DBUS_TYPE_UINT32, &size_v,
608                                          DBUS_TYPE_INVALID);
609
610         dbus_message_set_auto_start (msg, TRUE);
611
612         dbus_error_init (&err);
613
614         timeout = 1000; //XXX
615         osso_rpc_get_timeout (osso_ctx, &timeout);
616
617         reply = dbus_connection_send_with_reply_and_block (con,
618                                                            msg, 
619                                                            timeout,
620                                                            &err);
621
622         dbus_message_unref (msg);
623
624
625         if (reply == NULL) {
626             //ULOG_ERR_F("dbus_connection_send_with_reply_and_block error: %s", err.message);
627             //XXX to GError?! 
628             return FALSE;
629         }
630
631         switch (dbus_message_get_type (reply)) {
632
633                 case DBUS_MESSAGE_TYPE_ERROR:
634                         dbus_set_error_from_message (&err, reply);
635                         //XXX to GError?!
636                         dbus_error_free (&err);
637                         dbus_message_unref (reply);
638                         return FALSE;
639
640                 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
641                         /* ok we are good to go
642                          * lets drop outa here and handle that */
643                         break;
644                 default:
645                         //ULOG_WARN_F("got unknown message type as reply");
646                         //retval->type = DBUS_TYPE_STRING;
647                         //retval->value.s = g_strdup("Invalid return value");
648                         //XXX to GError?! 
649                         dbus_message_unref (reply);
650                         return FALSE;
651         }
652
653         g_debug ("message return");
654
655         dbus_message_iter_init (reply, &iter);
656         arg_type = dbus_message_iter_get_arg_type (&iter);
657         
658         dbus_message_iter_recurse (&iter, &child);
659         *hits = NULL;
660
661         do {
662                 ModestSearchHit *hit;
663
664                 hit = dbus_message_iter_get_search_hit (&child);
665
666                 if (hit) {
667                         *hits = g_list_prepend (*hits, hit);    
668                 }
669
670         } while (dbus_message_iter_next (&child));
671
672         dbus_message_unref (reply);
673
674
675         /* TODO: This is from osso source, do we need it? */
676 #if 0
677         /* Tell TaskNavigator to show "launch banner" */
678         msg = dbus_message_new_method_call (TASK_NAV_SERVICE,
679                                             APP_LAUNCH_BANNER_METHOD_PATH,
680                                             APP_LAUNCH_BANNER_METHOD_INTERFACE,
681                                             APP_LAUNCH_BANNER_METHOD);
682
683         if (msg == NULL) {
684                 g_warn ("dbus_message_new_method_call failed");
685         }
686
687
688
689         dbus_message_append_args (msg,
690                                   DBUS_TYPE_STRING,
691                                   &service,
692                                   DBUS_TYPE_INVALID);
693
694         b = dbus_connection_send (conn, msg, NULL);
695
696         if (b == NULL) {
697                 ULOG_WARN_F("dbus_connection_send failed");
698         }
699
700         dbus_message_unref (msg);
701 #endif
702
703         return TRUE;
704 }
705