1 /* Copyright (c) 2007, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #include "libmodest-dbus-client.h"
31 #include <dbus_api/modest-dbus-api.h> /* For the API strings. */
33 //#define DBUS_API_SUBJECT_TO_CHANGE 1
34 #include <dbus/dbus.h>
35 #include <dbus/dbus-glib-lowlevel.h>
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)
43 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
45 MODEST_DBUS_METHOD_SEND_MAIL, &retval,
48 DBUS_TYPE_STRING, bcc,
49 DBUS_TYPE_STRING, subject,
50 DBUS_TYPE_STRING, body,
54 printf("debug: osso_rpc_run() failed.\n");
57 printf("debug: osso_rpc_run() succeeded.\n");
60 osso_rpc_free_val(&retval);
66 libmodest_dbus_client_mail_to (osso_context_t *osso_context, const gchar *mailto_uri)
69 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
71 MODEST_DBUS_METHOD_MAIL_TO, &retval,
72 DBUS_TYPE_STRING, mailto_uri,
76 printf("debug: osso_rpc_run() failed.\n");
79 printf("debug: osso_rpc_run() succeeded.\n");
82 osso_rpc_free_val(&retval);
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)
92 gchar *attachments_str = NULL;
96 attachments_str = g_strdup( (gchar *) attachments->data );
98 for (next = g_slist_next(attachments); next != NULL; next = g_slist_next(next))
100 tmp = g_strconcat(attachments_str, ",", (gchar *) (next->data), NULL);
101 g_free(attachments_str);
102 attachments_str = tmp;
103 if (attachments_str == NULL) {
108 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
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,
119 if (ret != OSSO_OK) {
120 printf("debug: osso_rpc_run() failed.\n");
123 printf("debug: osso_rpc_run() succeeded.\n");
126 osso_rpc_free_val(&retval);
132 libmodest_dbus_client_open_message (osso_context_t *osso_context, const gchar *mail_uri)
135 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
137 MODEST_DBUS_METHOD_OPEN_MESSAGE, &retval,
138 DBUS_TYPE_STRING, mail_uri,
141 if (ret != OSSO_OK) {
142 printf("debug: osso_rpc_run() failed.\n");
145 printf("debug: osso_rpc_run() succeeded.\n");
148 osso_rpc_free_val(&retval);
154 libmodest_dbus_client_send_and_receive (osso_context_t *osso_context)
157 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
159 MODEST_DBUS_METHOD_SEND_RECEIVE, &retval,
162 if (ret != OSSO_OK) {
163 printf("debug: osso_rpc_run() failed.\n");
166 printf("debug: osso_rpc_run() succeeded.\n");
169 osso_rpc_free_val(&retval);
175 modest_search_hit_free (ModestSearchHit *hit)
179 g_slice_free (ModestSearchHit, hit);
184 _dbus_iter_get_string_or_null (DBusMessageIter *iter)
189 dbus_message_iter_get_basic (iter, &string);
192 if (string && strlen (string)) {
193 ret = g_strdup (string);
200 _dbus_iter_get_uint64 (DBusMessageIter *iter)
206 dbus_message_iter_get_basic (iter, &ui64v);
208 ret = (guint64) ui64v;
215 _dbus_iter_get_int64 (DBusMessageIter *iter)
221 dbus_message_iter_get_basic (iter, &i64v);
229 _dbus_iter_get_boolean (DBusMessageIter *iter)
236 dbus_message_iter_get_basic (iter, &val);
238 ret = (gboolean) val;
244 static ModestSearchHit *
245 dbus_message_iter_get_search_hit (DBusMessageIter *parent)
247 ModestSearchHit *hit;
248 DBusMessageIter child;
254 hit = g_slice_new0 (ModestSearchHit);
256 arg_type = dbus_message_iter_get_arg_type (parent);
258 if (arg_type != 'r') {
262 dbus_message_iter_recurse (parent, &child);
265 arg_type = dbus_message_iter_get_arg_type (&child);
267 if (arg_type != DBUS_TYPE_STRING) {
272 hit->msgid = _dbus_iter_get_string_or_null (&child);
274 res = dbus_message_iter_next (&child);
281 arg_type = dbus_message_iter_get_arg_type (&child);
283 if (arg_type != DBUS_TYPE_STRING) {
288 hit->subject = _dbus_iter_get_string_or_null (&child);
290 res = dbus_message_iter_next (&child);
297 arg_type = dbus_message_iter_get_arg_type (&child);
299 if (arg_type != DBUS_TYPE_STRING) {
304 hit->folder = _dbus_iter_get_string_or_null (&child);
306 res = dbus_message_iter_next (&child);
313 arg_type = dbus_message_iter_get_arg_type (&child);
315 if (arg_type != DBUS_TYPE_STRING) {
320 hit->sender = _dbus_iter_get_string_or_null (&child);
322 res = dbus_message_iter_next (&child);
329 arg_type = dbus_message_iter_get_arg_type (&child);
331 if (arg_type != DBUS_TYPE_UINT64) {
336 hit->msize = _dbus_iter_get_uint64 (&child);
338 res = dbus_message_iter_next (&child);
345 arg_type = dbus_message_iter_get_arg_type (&child);
347 if (arg_type != DBUS_TYPE_BOOLEAN) {
352 hit->has_attachment = _dbus_iter_get_boolean (&child);
354 res = dbus_message_iter_next (&child);
361 arg_type = dbus_message_iter_get_arg_type (&child);
363 if (arg_type != DBUS_TYPE_BOOLEAN) {
368 hit->is_unread = _dbus_iter_get_boolean (&child);
370 res = dbus_message_iter_next (&child);
377 arg_type = dbus_message_iter_get_arg_type (&child);
379 if (arg_type != DBUS_TYPE_INT64) {
384 hit->timestamp = _dbus_iter_get_int64 (&child);
386 res = dbus_message_iter_next (&child);
394 g_warning ("Error during unmarshaling");
395 modest_search_hit_free (hit);
404 libmodest_dbus_client_search (osso_context_t *osso_ctx,
410 ModestDBusSearchFlags flags,
418 DBusMessageIter iter;
419 DBusMessageIter child;
420 DBusMessage *reply = NULL;
425 dbus_int32_t flags_v;
426 dbus_uint32_t size_v;
429 con = osso_get_dbus_connection (osso_ctx);
432 g_warning ("Could not get dbus connection\n");
438 msg = dbus_message_new_method_call (MODEST_DBUS_SERVICE,
441 MODEST_DBUS_METHOD_SEARCH);
444 //ULOG_ERR_F("dbus_message_new_method_call failed");
450 flags_v = (dbus_int32_t) flags;
451 size_v = (dbus_uint32_t) min_size;
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,
462 dbus_message_set_auto_start (msg, TRUE);
464 dbus_error_init (&err);
466 timeout = 1000; //XXX
467 osso_rpc_get_timeout (osso_ctx, &timeout);
469 reply = dbus_connection_send_with_reply_and_block (con,
474 dbus_message_unref (msg);
478 //ULOG_ERR_F("dbus_connection_send_with_reply_and_block error: %s", err.message);
483 switch (dbus_message_get_type (reply)) {
485 case DBUS_MESSAGE_TYPE_ERROR:
486 dbus_set_error_from_message (&err, reply);
488 dbus_error_free (&err);
489 dbus_message_unref (reply);
492 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
493 /* ok we are good to go
494 * lets drop outa here and handle that */
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");
501 dbus_message_unref (reply);
505 g_debug ("message return");
507 dbus_message_iter_init (reply, &iter);
508 arg_type = dbus_message_iter_get_arg_type (&iter);
510 dbus_message_iter_recurse (&iter, &child);
513 ModestSearchHit *hit;
515 hit = dbus_message_iter_get_search_hit (&child);
518 *hits = g_list_prepend (*hits, hit);
521 } while (dbus_message_iter_next (&child));
523 dbus_message_unref (reply);
526 /* TODO: This is from osso source, do we need it? */
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);
535 g_warn ("dbus_message_new_method_call failed");
540 dbus_message_append_args (msg,
545 b = dbus_connection_send (conn, msg, NULL);
548 ULOG_WARN_F("dbus_connection_send failed");
551 dbus_message_unref (msg);