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:
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
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.
52 * Return value: Whether or not the rpc call to modest
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)
61 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
63 MODEST_DBUS_METHOD_SEND_MAIL, &retval,
66 DBUS_TYPE_STRING, bcc,
67 DBUS_TYPE_STRING, subject,
68 DBUS_TYPE_STRING, body,
72 printf("debug: osso_rpc_run() failed.\n");
75 printf("debug: osso_rpc_run() succeeded.\n");
78 osso_rpc_free_val(&retval);
84 libmodest_dbus_client_mail_to (osso_context_t *osso_context, const gchar *mailto_uri)
87 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
89 MODEST_DBUS_METHOD_MAIL_TO, &retval,
90 DBUS_TYPE_STRING, mailto_uri,
94 printf("debug: osso_rpc_run() failed.\n");
97 printf("debug: osso_rpc_run() succeeded.\n");
100 osso_rpc_free_val(&retval);
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)
110 gchar *attachments_str = NULL;
114 attachments_str = g_strdup( (gchar *) attachments->data );
116 for (next = g_slist_next(attachments); next != NULL; next = g_slist_next(next))
118 tmp = g_strconcat(attachments_str, ",", (gchar *) (next->data), NULL);
119 g_free(attachments_str);
120 attachments_str = tmp;
121 if (attachments_str == NULL) {
126 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
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,
137 if (ret != OSSO_OK) {
138 printf("debug: osso_rpc_run() failed.\n");
141 printf("debug: osso_rpc_run() succeeded.\n");
144 osso_rpc_free_val(&retval);
150 * libmodest_dbus_client_dopen_message:
151 * @osso_context: a valid #osso_context_t object.
152 * @msg_uri: A valid url to a mail
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
159 * Return value: TRUE on successs, FALSE on error
162 libmodest_dbus_client_open_message (osso_context_t *osso_context, const gchar *mail_uri)
165 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
167 MODEST_DBUS_METHOD_OPEN_MESSAGE, &retval,
168 DBUS_TYPE_STRING, mail_uri,
171 if (ret != OSSO_OK) {
172 printf("debug: osso_rpc_run() failed.\n");
175 printf("debug: osso_rpc_run() succeeded.\n");
178 osso_rpc_free_val(&retval);
184 libmodest_dbus_client_send_and_receive (osso_context_t *osso_context)
187 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
189 MODEST_DBUS_METHOD_SEND_RECEIVE, &retval,
192 if (ret != OSSO_OK) {
193 printf("debug: osso_rpc_run() failed.\n");
196 printf("debug: osso_rpc_run() succeeded.\n");
199 osso_rpc_free_val(&retval);
205 libmodest_dbus_client_open_default_inbox (osso_context_t *osso_context)
208 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
210 MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX, &retval,
213 if (ret != OSSO_OK) {
214 printf("debug: osso_rpc_run() failed.\n");
217 printf("debug: osso_rpc_run() succeeded.\n");
220 osso_rpc_free_val(&retval);
226 * libmodest_dbus_client_delete_message:
227 * @osso_context: a valid #osso_context_t object.
228 * @msg_uri: A valid url to a mail
230 * This method will try to find the message supplied
231 * by @msg_uri and if found delete it. It will use
232 * remote procedure calls (rpc) over dbus to do so.
234 * Return value: TRUE on successs, FALSE on error
237 libmodest_dbus_client_delete_message (osso_context_t *osso_ctx,
243 ret = osso_rpc_run_with_defaults (osso_ctx,
245 MODEST_DBUS_METHOD_DELETE_MESSAGE, &retval,
246 DBUS_TYPE_STRING, msg_uri,
249 if (ret != OSSO_OK) {
250 g_debug ("debug: osso_rpc_run() failed.\n");
252 g_debug ("debug: osso_rpc_run() succeeded.\n");
255 osso_rpc_free_val (&retval);
257 return ret == OSSO_OK;
261 modest_search_hit_free (ModestSearchHit *hit)
264 g_slice_free (ModestSearchHit, hit);
268 modest_search_hit_list_free (GList *hits)
276 for (iter = hits; iter; iter = iter->next) {
277 modest_search_hit_free ((ModestSearchHit *) iter->data);
284 _dbus_iter_get_string_or_null (DBusMessageIter *iter)
289 dbus_message_iter_get_basic (iter, &string);
292 if (string && strlen (string)) {
293 ret = g_strdup (string);
300 _dbus_iter_get_uint64 (DBusMessageIter *iter)
306 dbus_message_iter_get_basic (iter, &ui64v);
308 ret = (guint64) ui64v;
315 _dbus_iter_get_int64 (DBusMessageIter *iter)
321 dbus_message_iter_get_basic (iter, &i64v);
329 _dbus_iter_get_boolean (DBusMessageIter *iter)
336 dbus_message_iter_get_basic (iter, &val);
338 ret = (gboolean) val;
344 static ModestSearchHit *
345 dbus_message_iter_get_search_hit (DBusMessageIter *parent)
347 ModestSearchHit *hit;
348 DBusMessageIter child;
354 hit = g_slice_new0 (ModestSearchHit);
356 arg_type = dbus_message_iter_get_arg_type (parent);
358 if (arg_type != 'r') {
362 dbus_message_iter_recurse (parent, &child);
365 arg_type = dbus_message_iter_get_arg_type (&child);
367 if (arg_type != DBUS_TYPE_STRING) {
372 hit->msgid = _dbus_iter_get_string_or_null (&child);
374 res = dbus_message_iter_next (&child);
381 arg_type = dbus_message_iter_get_arg_type (&child);
383 if (arg_type != DBUS_TYPE_STRING) {
388 hit->subject = _dbus_iter_get_string_or_null (&child);
390 res = dbus_message_iter_next (&child);
397 arg_type = dbus_message_iter_get_arg_type (&child);
399 if (arg_type != DBUS_TYPE_STRING) {
404 hit->folder = _dbus_iter_get_string_or_null (&child);
406 res = dbus_message_iter_next (&child);
413 arg_type = dbus_message_iter_get_arg_type (&child);
415 if (arg_type != DBUS_TYPE_STRING) {
420 hit->sender = _dbus_iter_get_string_or_null (&child);
422 res = dbus_message_iter_next (&child);
429 arg_type = dbus_message_iter_get_arg_type (&child);
431 if (arg_type != DBUS_TYPE_UINT64) {
436 hit->msize = _dbus_iter_get_uint64 (&child);
438 res = dbus_message_iter_next (&child);
445 arg_type = dbus_message_iter_get_arg_type (&child);
447 if (arg_type != DBUS_TYPE_BOOLEAN) {
452 hit->has_attachment = _dbus_iter_get_boolean (&child);
454 res = dbus_message_iter_next (&child);
461 arg_type = dbus_message_iter_get_arg_type (&child);
463 if (arg_type != DBUS_TYPE_BOOLEAN) {
468 hit->is_unread = _dbus_iter_get_boolean (&child);
470 res = dbus_message_iter_next (&child);
477 arg_type = dbus_message_iter_get_arg_type (&child);
479 if (arg_type != DBUS_TYPE_INT64) {
484 hit->timestamp = _dbus_iter_get_int64 (&child);
486 res = dbus_message_iter_next (&child);
494 g_warning ("Error during unmarshaling");
495 modest_search_hit_free (hit);
503 * libmodest_dbus_client_search:
504 * @osso_ctx: A valid #osso_context_t object.
505 * @query: The term to search for.
506 * @folder: An url to specific folder or %NULL to search everywhere.
507 * @start_date: Search hits before this date will be ignored.
508 * @end_date: Search hits after this date will be ignored.
509 * @min_size: Messagers smaller then this size will be ingored.
510 * @flags: A list of flags where to search so the documentation
511 * of %ModestDBusSearchFlags for details.
512 * @hits: A pointer to a valid GList pointer that will contain the search
513 * hits (must be freed by the caller).
515 * This method will search the folder specified by a valid url in @folder or all
516 * known accounts (local and remote) if %NULL for matches of the search term(s)
517 * specified in @query. It is legal to specify 0 in @start_date, @end_date and
518 * @min_size to ignore these parameters during the search otherwise those message
519 * that do not meet the specifed dates or size will be ignored.
520 * Where to search, be it subject, sender or the whole body can be specified by
521 * the @flags parameter.
523 * Upon success TRUE is returned and @hits will include the search hits or the list
524 * migh be empty if none of the messages matched the search criteria. The returned
525 * list must be freed with modest_search_hit_list_free (). It is save to pass
526 * %NULL to this function so you can call this function on the result list no matter
527 * if a hit was found or not (means the list is empty - i.e. %NULL)
528 * FALSE will only be return if an error during the remote procedure call (rpc)
529 * occured or if the specified folder could not be found.
531 * NOTE: The body of a message can only be searched if it was previously downloaded by
532 * modest. This function does also not attempt do to remote searches (i.e. IMAP search).
534 * Example to search every account for message containing "no":
535 * <informalexample><programlisting>
536 * ModestDBusSearchFlags flags;
537 * osso_context_t *osso_context;
542 * [...] Initialize osso context [...]
544 * res = libmodest_dbus_client_search (osso_context,
553 * for (iter = hits; iter; iter = iter->next) {
554 * ModestSearchHit *hit = (ModestSearchHit *) iter->data;
556 * [...] Do something with the hit [...]
560 * modest_search_hit_list_free (hits);
561 * </programlisting></informalexample>
563 * Return value: TRUE if the search succeded or FALSE for an error during the search
566 libmodest_dbus_client_search (osso_context_t *osso_ctx,
572 ModestDBusSearchFlags flags,
580 DBusMessageIter iter;
581 DBusMessageIter child;
582 DBusMessage *reply = NULL;
587 dbus_int32_t flags_v;
588 dbus_uint32_t size_v;
594 con = osso_get_dbus_connection (osso_ctx);
597 g_warning ("Could not get dbus connection\n");
603 msg = dbus_message_new_method_call (MODEST_DBUS_SERVICE,
606 MODEST_DBUS_METHOD_SEARCH);
609 //ULOG_ERR_F("dbus_message_new_method_call failed");
613 if (folder == NULL) {
619 flags_v = (dbus_int32_t) flags;
620 size_v = (dbus_uint32_t) min_size;
622 res = dbus_message_append_args (msg,
623 DBUS_TYPE_STRING, &query,
624 DBUS_TYPE_STRING, &folder,
625 DBUS_TYPE_INT64, &sd_v,
626 DBUS_TYPE_INT64, &ed_v,
627 DBUS_TYPE_INT32, &flags_v,
628 DBUS_TYPE_UINT32, &size_v,
631 dbus_message_set_auto_start (msg, TRUE);
633 dbus_error_init (&err);
635 timeout = 1000; //XXX
636 osso_rpc_get_timeout (osso_ctx, &timeout);
638 reply = dbus_connection_send_with_reply_and_block (con,
643 dbus_message_unref (msg);
647 //ULOG_ERR_F("dbus_connection_send_with_reply_and_block error: %s", err.message);
652 switch (dbus_message_get_type (reply)) {
654 case DBUS_MESSAGE_TYPE_ERROR:
655 dbus_set_error_from_message (&err, reply);
657 dbus_error_free (&err);
658 dbus_message_unref (reply);
661 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
662 /* ok we are good to go
663 * lets drop outa here and handle that */
666 //ULOG_WARN_F("got unknown message type as reply");
667 //retval->type = DBUS_TYPE_STRING;
668 //retval->value.s = g_strdup("Invalid return value");
670 dbus_message_unref (reply);
674 g_debug ("message return");
676 dbus_message_iter_init (reply, &iter);
677 arg_type = dbus_message_iter_get_arg_type (&iter);
679 dbus_message_iter_recurse (&iter, &child);
683 ModestSearchHit *hit;
685 hit = dbus_message_iter_get_search_hit (&child);
688 *hits = g_list_prepend (*hits, hit);
691 } while (dbus_message_iter_next (&child));
693 dbus_message_unref (reply);
696 /* TODO: This is from osso source, do we need it? */
698 /* Tell TaskNavigator to show "launch banner" */
699 msg = dbus_message_new_method_call (TASK_NAV_SERVICE,
700 APP_LAUNCH_BANNER_METHOD_PATH,
701 APP_LAUNCH_BANNER_METHOD_INTERFACE,
702 APP_LAUNCH_BANNER_METHOD);
705 g_warn ("dbus_message_new_method_call failed");
710 dbus_message_append_args (msg,
715 b = dbus_connection_send (conn, msg, NULL);
718 ULOG_WARN_F("dbus_connection_send failed");
721 dbus_message_unref (msg);