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>
40 /** Get a comma-separated list of attachement URI strings,
41 * from a list of strings.
43 static gchar* get_attachments_string (GSList *attachments)
48 gchar *attachments_str = g_strdup("");
50 GSList *iter = attachments;
54 gchar *tmp = g_strconcat(attachments_str, ",", (gchar *) (iter->data), NULL);
55 g_free(attachments_str);
56 attachments_str = tmp;
59 iter = g_slist_next(iter);
62 return attachments_str;
65 /* TODO: Is this actually used by anything?
66 * I guess that everything uses *_compose_mail() instead. murrayc.
70 * libmodest_dbus_client_send_mail:
71 * @osso_context: a valid #osso_context_t object.
72 * @to: The Recipients (From: line)
73 * @cc: Recipients for carbon copies
74 * @bcc: Recipients for blind carbon copies
75 * @subject: Subject line
76 * @body: The actual body of the mail to send
77 * @attachments: Additional list of attachments
79 * This function will try to do a remote procedure call (rpc)
80 * into modest (or start it if necessary) and send a new
81 * email with the supplied parameters.
83 * Return value: Whether or not the rpc call to modest
87 libmodest_dbus_client_send_mail (osso_context_t *osso_context, const gchar *to, const gchar *cc,
88 const gchar *bcc, const gchar* subject, const gchar* body, GSList *attachments)
90 gchar *attachments_str = get_attachments_string(attachments);
93 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
95 MODEST_DBUS_METHOD_SEND_MAIL, &retval,
98 DBUS_TYPE_STRING, bcc,
99 DBUS_TYPE_STRING, subject,
100 DBUS_TYPE_STRING, body,
101 DBUS_TYPE_STRING, attachments_str,
104 if (ret != OSSO_OK) {
105 printf("debug: %s: osso_rpc_run() failed.\n", __FUNCTION__);
108 printf("debug: %s: osso_rpc_run() succeeded.\n", __FUNCTION__);
111 osso_rpc_free_val(&retval);
113 g_free (attachments_str);
119 libmodest_dbus_client_mail_to (osso_context_t *osso_context, const gchar *mailto_uri)
122 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
124 MODEST_DBUS_METHOD_MAIL_TO, &retval,
125 DBUS_TYPE_STRING, mailto_uri,
128 if (ret != OSSO_OK) {
129 printf("debug: %s: osso_rpc_run() failed.\n", __FUNCTION__);
132 printf("debug: %s: osso_rpc_run() succeeded.\n", __FUNCTION__);
135 osso_rpc_free_val(&retval);
141 * libmodest_dbus_client_compose_mail:
142 * @osso_context: a valid #osso_context_t object.
143 * @to: The Recipients (From: line)
144 * @cc: Recipients for carbon copies
145 * @bcc: Recipients for blind carbon copies
146 * @subject: Subject line
147 * @body: The actual body of the mail to send
148 * @attachments: Additional list of attachments
150 * This function will try to do a remote procedure call (rpc)
151 * into modest (or start it if necessary) and open a composer
152 * window with the supplied parameters prefilled.
154 * Return value: Whether or not the rpc call to modest
158 libmodest_dbus_client_compose_mail (osso_context_t *osso_context, const gchar *to, const gchar *cc,
159 const gchar *bcc, const gchar* subject, const gchar* body, GSList *attachments)
163 gchar *attachments_str = get_attachments_string(attachments);
165 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
167 MODEST_DBUS_METHOD_COMPOSE_MAIL, &retval,
168 DBUS_TYPE_STRING, to,
169 DBUS_TYPE_STRING, cc,
170 DBUS_TYPE_STRING, bcc,
171 DBUS_TYPE_STRING, subject,
172 DBUS_TYPE_STRING, body,
173 DBUS_TYPE_STRING, attachments_str,
176 if (ret != OSSO_OK) {
177 printf("debug: %s: osso_rpc_run() failed.\n", __FUNCTION__);
180 printf("debug: %s: osso_rpc_run() succeeded.\n", __FUNCTION__);
183 osso_rpc_free_val(&retval);
185 g_free (attachments_str);
191 * libmodest_dbus_client_open_message:
192 * @osso_context: a valid #osso_context_t object.
193 * @msg_uri: A valid url to a mail
195 * This method will try to find the message supplied
196 * by @msg_uri and open it for display if found.
197 * It will use remote procedure calls (rpc) over
200 * Return value: TRUE on successs, FALSE on error
203 libmodest_dbus_client_open_message (osso_context_t *osso_context, const gchar *mail_uri)
206 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
208 MODEST_DBUS_METHOD_OPEN_MESSAGE, &retval,
209 DBUS_TYPE_STRING, mail_uri,
212 if (ret != OSSO_OK) {
213 printf("debug: %s: osso_rpc_run() failed.\n", __FUNCTION__);
216 printf("debug: %s: osso_rpc_run() succeeded.\n", __FUNCTION__);
219 osso_rpc_free_val(&retval);
225 libmodest_dbus_client_send_and_receive (osso_context_t *osso_context)
228 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
230 MODEST_DBUS_METHOD_SEND_RECEIVE, &retval,
233 if (ret != OSSO_OK) {
234 printf("debug: %s: osso_rpc_run() failed.\n", __FUNCTION__);
237 printf("debug: %s: osso_rpc_run() succeeded.\n", __FUNCTION__);
240 osso_rpc_free_val(&retval);
246 libmodest_dbus_client_open_default_inbox (osso_context_t *osso_context)
249 const osso_return_t ret = osso_rpc_run_with_defaults(osso_context,
251 MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX, &retval,
254 if (ret != OSSO_OK) {
255 printf("debug: %s: osso_rpc_run() failed.\n", __FUNCTION__);
258 printf("debug: %s: osso_rpc_run() succeeded.\n", __FUNCTION__);
261 osso_rpc_free_val(&retval);
267 * libmodest_dbus_client_delete_message:
268 * @osso_context: a valid #osso_context_t object.
269 * @msg_uri: A valid url to a mail
271 * This method will try to find the message supplied
272 * by @msg_uri and if found delete it. It will use
273 * remote procedure calls (rpc) over dbus to do so.
275 * Return value: TRUE on successs, FALSE on error
278 libmodest_dbus_client_delete_message (osso_context_t *osso_ctx,
284 ret = osso_rpc_run_with_defaults (osso_ctx,
286 MODEST_DBUS_METHOD_DELETE_MESSAGE, &retval,
287 DBUS_TYPE_STRING, msg_uri,
290 if (ret != OSSO_OK) {
291 g_debug ("debug: osso_rpc_run() failed.\n");
293 g_debug ("debug: osso_rpc_run() succeeded.\n");
296 osso_rpc_free_val (&retval);
298 return ret == OSSO_OK;
302 modest_search_hit_free (ModestSearchHit *hit)
305 g_slice_free (ModestSearchHit, hit);
309 modest_search_hit_list_free (GList *hits)
317 for (iter = hits; iter; iter = iter->next) {
318 modest_search_hit_free ((ModestSearchHit *) iter->data);
325 _dbus_iter_get_string_or_null (DBusMessageIter *iter)
327 const char *string = NULL;
330 dbus_message_iter_get_basic (iter, &string);
332 if (string && strlen (string)) {
333 ret = g_strdup (string);
340 _dbus_iter_get_uint64 (DBusMessageIter *iter)
346 dbus_message_iter_get_basic (iter, &ui64v);
348 ret = (guint64) ui64v;
355 _dbus_iter_get_int64 (DBusMessageIter *iter)
361 dbus_message_iter_get_basic (iter, &i64v);
369 _dbus_iter_get_boolean (DBusMessageIter *iter)
376 dbus_message_iter_get_basic (iter, &val);
378 ret = (gboolean) val;
384 static ModestSearchHit *
385 dbus_message_iter_get_search_hit (DBusMessageIter *parent)
387 ModestSearchHit *hit;
388 DBusMessageIter child;
394 hit = g_slice_new0 (ModestSearchHit);
396 arg_type = dbus_message_iter_get_arg_type (parent);
398 if (arg_type != 'r') {
402 dbus_message_iter_recurse (parent, &child);
405 arg_type = dbus_message_iter_get_arg_type (&child);
407 if (arg_type != DBUS_TYPE_STRING) {
412 hit->msgid = _dbus_iter_get_string_or_null (&child);
414 res = dbus_message_iter_next (&child);
421 arg_type = dbus_message_iter_get_arg_type (&child);
423 if (arg_type != DBUS_TYPE_STRING) {
428 hit->subject = _dbus_iter_get_string_or_null (&child);
430 res = dbus_message_iter_next (&child);
437 arg_type = dbus_message_iter_get_arg_type (&child);
439 if (arg_type != DBUS_TYPE_STRING) {
444 hit->folder = _dbus_iter_get_string_or_null (&child);
446 res = dbus_message_iter_next (&child);
453 arg_type = dbus_message_iter_get_arg_type (&child);
455 if (arg_type != DBUS_TYPE_STRING) {
460 hit->sender = _dbus_iter_get_string_or_null (&child);
462 res = dbus_message_iter_next (&child);
469 arg_type = dbus_message_iter_get_arg_type (&child);
471 if (arg_type != DBUS_TYPE_UINT64) {
476 hit->msize = _dbus_iter_get_uint64 (&child);
478 res = dbus_message_iter_next (&child);
485 arg_type = dbus_message_iter_get_arg_type (&child);
487 if (arg_type != DBUS_TYPE_BOOLEAN) {
492 hit->has_attachment = _dbus_iter_get_boolean (&child);
494 res = dbus_message_iter_next (&child);
501 arg_type = dbus_message_iter_get_arg_type (&child);
503 if (arg_type != DBUS_TYPE_BOOLEAN) {
508 hit->is_unread = _dbus_iter_get_boolean (&child);
510 res = dbus_message_iter_next (&child);
517 arg_type = dbus_message_iter_get_arg_type (&child);
519 if (arg_type != DBUS_TYPE_INT64) {
524 hit->timestamp = _dbus_iter_get_int64 (&child);
526 res = dbus_message_iter_next (&child);
534 g_warning ("Error during unmarshaling");
535 modest_search_hit_free (hit);
543 * libmodest_dbus_client_search:
544 * @osso_ctx: A valid #osso_context_t object.
545 * @query: The term to search for.
546 * @folder: An url to specific folder or %NULL to search everywhere.
547 * @start_date: Search hits before this date will be ignored.
548 * @end_date: Search hits after this date will be ignored.
549 * @min_size: Messagers smaller then this size will be ingored.
550 * @flags: A list of flags where to search so the documentation
551 * of %ModestDBusSearchFlags for details.
552 * @hits: A pointer to a valid GList pointer that will contain the search
553 * hits (ModestSearchHit). The list and the items must be freed by the caller
554 * with modest_search_hit_list_free().
556 * This method will search the folder specified by a valid url in @folder or all
557 * known accounts (local and remote) if %NULL for matches of the search term(s)
558 * specified in @query. It is legal to specify 0 in @start_date, @end_date and
559 * @min_size to ignore these parameters during the search otherwise those message
560 * that do not meet the specifed dates or size will be ignored.
561 * Where to search, be it subject, sender or the whole body can be specified by
562 * the @flags parameter.
564 * Upon success TRUE is returned and @hits will include the search hits or the list
565 * migh be empty if none of the messages matched the search criteria. The returned
566 * list must be freed with modest_search_hit_list_free (). It is save to pass
567 * %NULL to this function so you can call this function on the result list no matter
568 * if a hit was found or not (means the list is empty - i.e. %NULL)
569 * FALSE will only be return if an error during the remote procedure call (rpc)
570 * occured or if the specified folder could not be found.
572 * NOTE: The body of a message can only be searched if it was previously downloaded by
573 * modest. This function does also not attempt do to remote searches (i.e. IMAP search).
575 * Example to search every account for message containing "no":
576 * <informalexample><programlisting>
577 * ModestDBusSearchFlags flags;
578 * osso_context_t *osso_context;
583 * [...] Initialize osso context [...]
585 * res = libmodest_dbus_client_search (osso_context,
594 * for (iter = hits; iter; iter = iter->next) {
595 * ModestSearchHit *hit = (ModestSearchHit *) iter->data;
597 * [...] Do something with the hit [...]
601 * modest_search_hit_list_free (hits);
602 * </programlisting></informalexample>
604 * Return value: TRUE if the search succeded or FALSE for an error during the search
607 libmodest_dbus_client_search (osso_context_t *osso_ctx,
613 ModestDBusSearchFlags flags,
621 DBusMessageIter iter;
622 DBusMessageIter child;
623 DBusMessage *reply = NULL;
628 dbus_int32_t flags_v;
629 dbus_uint32_t size_v;
635 con = osso_get_dbus_connection (osso_ctx);
638 g_warning ("Could not get dbus connection\n");
644 msg = dbus_message_new_method_call (MODEST_DBUS_SERVICE,
647 MODEST_DBUS_METHOD_SEARCH);
650 //ULOG_ERR_F("dbus_message_new_method_call failed");
654 if (folder == NULL) {
660 flags_v = (dbus_int32_t) flags;
661 size_v = (dbus_uint32_t) min_size;
663 res = dbus_message_append_args (msg,
664 DBUS_TYPE_STRING, &query,
665 DBUS_TYPE_STRING, &folder,
666 DBUS_TYPE_INT64, &sd_v,
667 DBUS_TYPE_INT64, &ed_v,
668 DBUS_TYPE_INT32, &flags_v,
669 DBUS_TYPE_UINT32, &size_v,
672 dbus_message_set_auto_start (msg, TRUE);
674 dbus_error_init (&err);
676 timeout = 1000; //XXX
677 osso_rpc_get_timeout (osso_ctx, &timeout);
679 reply = dbus_connection_send_with_reply_and_block (con,
684 dbus_message_unref (msg);
688 //ULOG_ERR_F("dbus_connection_send_with_reply_and_block error: %s", err.message);
693 switch (dbus_message_get_type (reply)) {
695 case DBUS_MESSAGE_TYPE_ERROR:
696 dbus_set_error_from_message (&err, reply);
698 dbus_error_free (&err);
699 dbus_message_unref (reply);
702 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
703 /* ok we are good to go
704 * lets drop outa here and handle that */
707 //ULOG_WARN_F("got unknown message type as reply");
708 //retval->type = DBUS_TYPE_STRING;
709 //retval->value.s = g_strdup("Invalid return value");
711 dbus_message_unref (reply);
715 g_debug ("message return");
717 dbus_message_iter_init (reply, &iter);
718 arg_type = dbus_message_iter_get_arg_type (&iter);
720 dbus_message_iter_recurse (&iter, &child);
724 ModestSearchHit *hit;
726 hit = dbus_message_iter_get_search_hit (&child);
729 *hits = g_list_prepend (*hits, hit);
732 } while (dbus_message_iter_next (&child));
734 dbus_message_unref (reply);
737 /* TODO: This is from osso source, do we need it? */
739 /* Tell TaskNavigator to show "launch banner" */
740 msg = dbus_message_new_method_call (TASK_NAV_SERVICE,
741 APP_LAUNCH_BANNER_METHOD_PATH,
742 APP_LAUNCH_BANNER_METHOD_INTERFACE,
743 APP_LAUNCH_BANNER_METHOD);
746 g_warn ("dbus_message_new_method_call failed");
751 dbus_message_append_args (msg,
756 b = dbus_connection_send (conn, msg, NULL);
759 ULOG_WARN_F("dbus_connection_send failed");
762 dbus_message_unref (msg);