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 "modest-dbus-callbacks.h"
31 #include "modest-runtime.h"
32 #include "modest-account-mgr.h"
33 #include "modest-account-mgr-helpers.h"
34 #include "modest-tny-account.h"
35 #include "modest-ui-actions.h"
37 #include "modest-search.h"
38 #include "widgets/modest-msg-edit-window.h"
39 #include "modest-tny-msg.h"
40 #include <libmodest-dbus-client/libmodest-dbus-client.h>
41 #include <libgnomevfs/gnome-vfs-utils.h>
44 #include <glib/gstdio.h>
45 #include <libgnomevfs/gnome-vfs-mime.h>
46 #include <tny-fs-stream.h>
49 #include <tny-iterator.h>
50 #include <tny-simple-list.h>
70 } ComposeMailIdleData;
73 on_idle_send_mail(gpointer user_data)
75 SendMailIdleData *idle_data = (SendMailIdleData*)user_data;
77 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
78 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
79 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
81 g_printerr ("modest: no account found\n");
84 TnyTransportAccount *transport_account = NULL;
86 transport_account = TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_transport_account_for_open_connection
87 (modest_runtime_get_account_store(),
91 if (!transport_account) {
92 g_printerr ("modest: no transport account found for '%s'\n", account_name);
95 /* Create the mail operation: */
96 if (transport_account) {
97 /* Use the mail operation: */
98 gchar * from = modest_account_mgr_get_from_string (account_mgr,
101 g_printerr ("modest: no from address for account '%s'\n", account_name);
103 ModestMailOperation *mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_SEND, NULL);
104 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
106 modest_mail_operation_send_new_mail (mail_operation,
110 idle_data->to, idle_data->cc, idle_data->bcc, idle_data->subject,
111 idle_data->body, /* plain_body */
112 NULL, /* html_body */
113 NULL, /* attachments_list, GSList of TnyMimePart. */
117 g_object_unref (G_OBJECT (mail_operation));
120 g_object_unref (G_OBJECT (transport_account));
123 g_free (account_name);
125 /* Free the idle data: */
126 g_free (idle_data->to);
127 g_free (idle_data->cc);
128 g_free (idle_data->bcc);
129 g_free (idle_data->subject);
130 g_free (idle_data->body);
131 g_free (idle_data->attachments);
134 return FALSE; /* Do not call this callback again. */
137 /* TODO: Is this actually used by anything?
138 * I guess that everything uses *_compose_mail() instead. murrayc.
140 static gint on_send_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
142 if (arguments->len != MODEST_DEBUS_SEND_MAIL_ARGS_COUNT)
145 /* Use g_idle to context-switch into the application's thread: */
146 SendMailIdleData *idle_data = g_new0(SendMailIdleData, 1); /* Freed in the idle callback. */
148 /* Get the arguments: */
149 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_TO);
150 idle_data->to = g_strdup (val.value.s);
152 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_CC);
153 idle_data->cc = g_strdup (val.value.s);
155 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BCC);
156 idle_data->bcc = g_strdup (val.value.s);
158 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_SUBJECT);
159 idle_data->subject = g_strdup (val.value.s);
161 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BODY);
162 idle_data->body = g_strdup (val.value.s);
164 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_ATTACHMENTS);
165 idle_data->attachments = g_strdup (val.value.s);
167 /* printf(" debug: to=%s\n", idle_data->to); */
168 g_idle_add(on_idle_send_mail, (gpointer)idle_data);
170 /* Note that we cannot report failures during sending,
171 * because that would be asynchronous. */
176 * @uri An escaped URI. URIs should always be escaped.
177 * @len The length of the @uri string, or -1 if the string is null terminated.
179 * Decode a URI, or URI fragment, as per RFC 1738.
180 * http://www.ietf.org/rfc/rfc1738.txt
182 * Return value: An unescaped string. This should be freed with g_free().
184 static gchar* uri_unescape(const gchar* uri, size_t len)
192 /* Allocate an extra string so we can be sure that it is null-terminated,
193 * so we can use gnome_vfs_unescape_string().
194 * This is not efficient. */
195 gchar * escaped_nullterminated = g_strndup (uri, len);
196 gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
197 g_free (escaped_nullterminated);
202 /** uri_parse_mailto:
203 * @mailto A mailto URI, with the mailto: prefix.
204 * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings,
205 * with each name item being followed by a value item. This list should be freed with g_slist_free) after
206 * all the string items have been freed. This parameter may be NULL.
207 * Parse a mailto URI as per RFC2368.
208 * http://www.ietf.org/rfc/rfc2368.txt
210 * Return value: The to address, unescaped. This should be freed with g_free().
212 static gchar* uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
214 const gchar* start_to = NULL;
215 /* Remove the mailto: prefix:
216 * 7 is the length of "mailto:": */
217 if (strncmp (mailto, "mailto:", 7) == 0) {
218 start_to = mailto + 7;
224 /* Look for ?, or the end of the string, marking the end of the to address: */
225 const size_t len_to = strcspn (start_to, "?");
226 gchar* result_to = uri_unescape (start_to, len_to);
227 printf("debug: result_to=%s\n", result_to);
229 /* Get any other items: */
230 const size_t len_mailto = strlen (start_to);
231 const gchar* p = start_to + len_to + 1; /* parsed so far. */
232 const gchar* end = start_to + len_mailto;
233 /* GSList *items = NULL; */
234 const gchar* start_item_name = p;
235 size_t len_item_name = 0;
236 const gchar* start_item_value = NULL;
239 /* Looking for the end of a name; */
240 if (start_item_name) {
241 const size_t len = strcspn (p, "="); /* Returns whole string if none found. */
243 /* This marks the end of a name and the start of the value: */
246 /* Skip over the name and mark the start of the value: */
247 p += (len + 1); /* Skip over the = */
248 start_item_value = p;
252 /* Looking for the end of a value: */
253 if (start_item_value) {
254 const size_t len = strcspn (p, "?"); /* Returns whole string if none found. */
255 /* ? marks the start of a new item: */
257 if (start_item_name && len_item_name) {
258 /* Finish the previously-started item: */
259 gchar *item_value = uri_unescape (start_item_value, len);
260 gchar *item_name = g_strndup (start_item_name, len_item_name);
261 /* printf ("debug: item name=%s, value=%s\n", item_name, item_value); */
263 /* Append the items to the list */
264 if(list_items_and_values) {
265 *list_items_and_values = g_slist_append (*list_items_and_values, item_name);
266 *list_items_and_values = g_slist_append (*list_items_and_values, item_value);
270 /* Skip over the value and mark the start of a possible new name/value pair: */
271 p += (len + 1); /* Skip over the ? */
274 start_item_value = NULL;
285 on_idle_mail_to(gpointer user_data)
287 /* This is based on the implemenation of main.c:start_uil(): */
289 gchar *uri = (gchar*)user_data;
290 GSList *list_names_and_values = NULL;
291 gchar *to = uri_parse_mailto (uri, &list_names_and_values);
293 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
294 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
295 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
297 g_printerr ("modest: no account found\n");
300 TnyAccount *account = NULL;
302 account = modest_tny_account_store_get_transport_account_for_open_connection (
303 modest_runtime_get_account_store(), account_name);
307 g_printerr ("modest: failed to get tny account folder'\n", account_name);
309 gchar * from = modest_account_mgr_get_from_string (account_mgr,
312 g_printerr ("modest: no from address for account '%s'\n", account_name);
314 const gchar *cc = NULL;
315 const gchar *bcc = NULL;
316 const gchar *subject = NULL;
317 const gchar *body = NULL;
319 /* Get the relevant items from the list: */
320 GSList *list = list_names_and_values;
322 const gchar * name = (const gchar*)list->data;
323 GSList *list_value = g_slist_next (list);
324 const gchar * value = (const gchar*)list_value->data;
326 if (strcmp (name, "cc") == 0) {
328 } else if (strcmp (name, "bcc") == 0) {
330 } else if (strcmp (name, "subject") == 0) {
332 } else if (strcmp (name, "body") == 0) {
336 /* Go to the next pair: */
338 list = g_slist_next (list_value);
343 /* Create the message: */
344 TnyMsg *msg = modest_tny_msg_new (to, from,
345 cc, bcc, subject, body,
346 NULL /* attachments */);
349 g_printerr ("modest: failed to create message\n");
352 /* Add the message to a folder and show its UI for editing: */
353 TnyFolder *folder = modest_tny_account_get_special_folder (account,
354 TNY_FOLDER_TYPE_DRAFTS);
356 g_printerr ("modest: failed to find Drafts folder\n");
359 tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
361 ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
362 gtk_widget_show_all (GTK_WIDGET (win));
364 g_object_unref (G_OBJECT(folder));
367 g_object_unref (G_OBJECT(msg));
370 g_object_unref (G_OBJECT(account));
374 g_free (account_name);
376 /* Free the list, as required by the uri_parse_mailto() documentation: */
377 if (list_names_and_values)
378 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
379 g_slist_free (list_names_and_values);
385 return FALSE; /* Do not call this callback again. */
388 static gint on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
390 if (arguments->len != MODEST_DEBUS_MAIL_TO_ARGS_COUNT)
393 /* Use g_idle to context-switch into the application's thread: */
395 /* Get the arguments: */
396 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_MAIL_TO_ARG_URI);
397 gchar *uri = g_strdup (val.value.s);
399 /* printf(" debug: to=%s\n", idle_data->to); */
400 g_idle_add(on_idle_mail_to, (gpointer)uri);
402 /* Note that we cannot report failures during sending,
403 * because that would be asynchronous. */
412 on_idle_compose_mail(gpointer user_data)
414 ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
418 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
419 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
420 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
422 g_printerr ("modest: no account found\n");
425 TnyAccount *account = NULL;
427 account = modest_tny_account_store_get_transport_account_for_open_connection (
428 modest_runtime_get_account_store(), account_name);
432 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
434 gchar * from = modest_account_mgr_get_from_string (account_mgr,
437 g_printerr ("modest: no from address for account '%s'\n", account_name);
440 /* Create the message: */
441 TnyMsg *msg = modest_tny_msg_new (idle_data->to, from,
442 idle_data->cc, idle_data->bcc, idle_data->subject, idle_data->body,
443 NULL); /* NULL because m_t_m_n doesn't use it */
446 g_printerr ("modest: failed to create message\n");
449 /* Add the message to a folder and show its UI for editing: */
450 TnyFolder *folder = modest_tny_account_get_special_folder (account,
451 TNY_FOLDER_TYPE_DRAFTS);
453 g_printerr ("modest: failed to find Drafts folder\n");
456 tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
458 ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
460 list = g_strsplit(idle_data->attachments, ",", 0);
461 for (i=0; list[i] != NULL; i++) {
462 modest_msg_edit_window_attach_file_noninteractive(
463 (ModestMsgEditWindow *)win, list[i]);
467 gtk_widget_show_all (GTK_WIDGET (win));
469 g_object_unref (G_OBJECT(folder));
472 g_object_unref (G_OBJECT(msg));
475 g_object_unref (G_OBJECT(account));
479 /* Free the idle data: */
480 g_free (idle_data->to);
481 g_free (idle_data->cc);
482 g_free (idle_data->bcc);
483 g_free (idle_data->subject);
484 g_free (idle_data->body);
485 g_free (idle_data->attachments);
488 g_free (account_name);
489 return FALSE; /* Do not call this callback again. */
492 static gint on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
496 if (arguments->len != MODEST_DEBUS_COMPOSE_MAIL_ARGS_COUNT)
499 /* Use g_idle to context-switch into the application's thread: */
500 ComposeMailIdleData *idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
502 /* Get the arguments: */
503 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_TO);
504 idle_data->to = g_strdup (val.value.s);
506 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_CC);
507 idle_data->cc = g_strdup (val.value.s);
509 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_BCC);
510 idle_data->bcc = g_strdup (val.value.s);
512 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_SUBJECT);
513 idle_data->subject = g_strdup (val.value.s);
515 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_BODY);
516 idle_data->body = g_strdup (val.value.s);
518 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
519 idle_data->attachments = g_strdup (val.value.s);
521 g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
523 /* Note that we cannot report failures during sending,
524 * because that would be asynchronous. */
530 find_message_by_url (const char *uri, TnyAccount **ac_out)
532 ModestTnyAccountStore *astore;
541 astore = modest_runtime_get_account_store ();
543 if (astore == NULL) {
547 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (astore),
550 if (account == NULL) {
551 g_debug ("%s: tny_account_store_find_account() failed.\n", __FUNCTION__);
555 g_debug ("%s: Found account.\n", __FUNCTION__);
557 if ( ! TNY_IS_STORE_ACCOUNT (account)) {
561 g_debug ("%s: Account is store account.\n", __FUNCTION__);
565 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account),
569 if (folder == NULL) {
570 g_debug ("%s: tny_store_account_find_folder() failed\naccount=%s, uri=%s.\n", __FUNCTION__,
571 tny_account_get_id (TNY_ACCOUNT(account)), uri);
574 g_debug ("%s: Found folder. (%s)\n", __FUNCTION__, uri);
577 msg = tny_folder_find_msg (folder, uri, &err);
580 g_debug ("%s: tny_folder_find_msg() failed for folder %s\n with error=%s.\n",
581 __FUNCTION__, tny_folder_get_id (folder), err->message);
588 if (account && !msg) {
589 g_object_unref (account);
594 g_object_unref (folder);
601 on_idle_open_message (gpointer user_data)
603 ModestWindow *msg_view;
608 const char *account_name;
611 uri = (char *) user_data;
613 g_debug ("%s: Trying to find msg by url: %s", __FUNCTION__, uri);
614 msg = find_message_by_url (uri, &account);
618 g_debug (" %s: message not found.", __FUNCTION__);
621 g_debug (" %s: Found message.", __FUNCTION__);
623 header = tny_msg_get_header (msg);
624 account_name = tny_account_get_name (account);
625 msg_uid = tny_header_get_uid (header);
627 msg_view = modest_msg_view_window_new (msg,
630 /* TODO: does that leak the msg_view ?! */
632 gtk_widget_show_all (GTK_WIDGET (msg_view));
634 g_object_unref (header);
635 g_object_unref (account);
637 return FALSE; /* Do not call this callback again. */
640 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
642 if (arguments->len != MODEST_DEBUS_OPEN_MESSAGE_ARGS_COUNT)
645 /* Use g_idle to context-switch into the application's thread: */
647 /* Get the arguments: */
648 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_OPEN_MESSAGE_ARG_URI);
649 gchar *uri = g_strdup (val.value.s);
651 /* printf(" debug: to=%s\n", idle_data->to); */
652 g_idle_add(on_idle_open_message, (gpointer)uri);
654 /* Note that we cannot report failures during sending,
655 * because that would be asynchronous. */
661 on_delete_message (GArray *arguments, gpointer data, osso_rpc_t *retval)
667 TnyHeader *msg_header;
676 if (arguments->len != MODEST_DEBUS_DELETE_MESSAGE_ARGS_COUNT) {
680 val = g_array_index (arguments,
682 MODEST_DEBUS_DELETE_MESSAGE_ARG_URI);
684 uri = (const char *) val.value.s;
686 g_debug ("Searching message (delete message)");
688 msg = find_message_by_url (uri, &account);
694 g_debug ("Found message");
696 msg_header = tny_msg_get_header (msg);
697 uid = tny_header_get_uid (msg_header);
698 folder = tny_msg_get_folder (msg);
701 /* tny_msg_get_header () flaw:
702 * From tinythingy doc: You can't use the returned instance with the
703 * TnyFolder operations
705 * To get a header instance that will work with these folder methods,
706 * you can use tny_folder_get_headers.
708 * Ok, we will do so then. Sigh.
710 headers = tny_simple_list_new ();
712 tny_folder_get_headers (folder, headers, TRUE, NULL);
713 iter = tny_list_create_iterator (headers);
716 g_debug ("Searching header for msg in folder");
717 while (!tny_iterator_is_done (iter)) {
720 header = TNY_HEADER (tny_iterator_get_current (iter));
721 cur_id = tny_header_get_uid (header);
723 if (cur_id && uid && g_str_equal (cur_id, uid)) {
724 g_debug ("Found correspoding header from folder");
729 g_object_unref (header);
730 tny_iterator_next (iter);
733 g_object_unref (iter);
734 g_object_unref (headers);
736 g_object_unref (msg_header);
737 g_object_unref (msg);
739 if (header == NULL) {
740 g_object_unref (folder);
747 tny_folder_remove_msg (folder, header, &error);
751 g_error_free (error);
754 g_object_unref (folder);
759 on_idle_send_receive(gpointer user_data)
763 /* Pick the main window if it exists */
764 win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
766 /* Send & receive all if "Update automatically" is set */
767 /* TODO: check the auto-update parameter in the configuration */
768 modest_ui_actions_do_send_receive_all (win);
770 return FALSE; /* Do not call this callback again. */
773 static gint on_send_receive(GArray * arguments, gpointer data, osso_rpc_t * retval)
775 /* Use g_idle to context-switch into the application's thread: */
777 /* This method has no arguments. */
779 /* printf(" debug: to=%s\n", idle_data->to); */
780 g_idle_add(on_idle_send_receive, NULL);
782 /* Note that we cannot report failures during send/receive,
783 * because that would be asynchronous. */
788 on_idle_open_default_inbox(gpointer user_data)
791 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
793 /* Get the folder view */
794 GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
795 MODEST_WIDGET_TYPE_FOLDER_VIEW);
796 modest_folder_view_select_first_inbox_or_local (
797 MODEST_FOLDER_VIEW (folder_view));
799 return FALSE; /* Do not call this callback again. */
802 static gint on_open_default_inbox(GArray * arguments, gpointer data, osso_rpc_t * retval)
804 /* Use g_idle to context-switch into the application's thread: */
806 /* This method has no arguments. */
808 g_idle_add(on_idle_open_default_inbox, NULL);
810 /* Note that we cannot report failures during send/receive,
811 * because that would be asynchronous. */
815 /* Callback for normal D-BUS messages */
816 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
817 GArray * arguments, gpointer data,
821 g_debug ("debug: %s\n", __FUNCTION__);
822 g_debug ("debug: %s: method received: %s\n", __FUNCTION__, method);
824 if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_MAIL) == 0) {
825 return on_send_mail (arguments, data, retval);
826 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
827 return on_mail_to (arguments, data, retval);
828 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
829 return on_open_message (arguments, data, retval);
830 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
831 return on_send_receive (arguments, data, retval);
832 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
833 return on_compose_mail (arguments, data, retval);
834 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_DELETE_MESSAGE) == 0) {
835 return on_delete_message (arguments,data, retval);
836 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX) == 0) {
837 return on_open_default_inbox (arguments, data, retval);
840 /* We need to return INVALID here so
841 * osso is returning DBUS_HANDLER_RESULT_NOT_YET_HANDLED
842 * so our modest_dbus_req_filter can kick in!
848 #define SEARCH_HIT_DBUS_TYPE \
849 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
850 DBUS_TYPE_STRING_AS_STRING \
851 DBUS_TYPE_STRING_AS_STRING \
852 DBUS_TYPE_STRING_AS_STRING \
853 DBUS_TYPE_STRING_AS_STRING \
854 DBUS_TYPE_UINT64_AS_STRING \
855 DBUS_TYPE_BOOLEAN_AS_STRING \
856 DBUS_TYPE_BOOLEAN_AS_STRING \
857 DBUS_TYPE_INT64_AS_STRING \
858 DBUS_STRUCT_END_CHAR_AS_STRING
861 search_result_to_message (DBusMessage *reply,
864 DBusMessageIter iter;
865 DBusMessageIter array_iter;
868 dbus_message_iter_init_append (reply, &iter);
869 dbus_message_iter_open_container (&iter,
871 SEARCH_HIT_DBUS_TYPE,
874 for (hit_iter = hits; hit_iter; hit_iter = hit_iter->next) {
875 DBusMessageIter struct_iter;
876 ModestSearchHit *hit;
882 gboolean has_attachment;
886 hit = (ModestSearchHit *) hit_iter->data;
888 msg_url = hit->msgid;
889 subject = hit->subject;
890 folder = hit->folder;
891 sender = hit->sender;
893 has_attachment = hit->has_attachment;
894 is_unread = hit->is_unread;
897 g_debug ("DEBUG: %s: Adding hit: %s", __FUNCTION__, msg_url);
899 dbus_message_iter_open_container (&array_iter,
904 dbus_message_iter_append_basic (&struct_iter,
908 dbus_message_iter_append_basic (&struct_iter,
912 dbus_message_iter_append_basic (&struct_iter,
916 dbus_message_iter_append_basic (&struct_iter,
920 dbus_message_iter_append_basic (&struct_iter,
924 dbus_message_iter_append_basic (&struct_iter,
928 dbus_message_iter_append_basic (&struct_iter,
932 dbus_message_iter_append_basic (&struct_iter,
936 dbus_message_iter_close_container (&array_iter,
940 g_free (hit->subject);
941 g_free (hit->folder);
942 g_free (hit->sender);
944 g_slice_free (ModestSearchHit, hit);
947 dbus_message_iter_close_container (&iter, &array_iter);
953 modest_dbus_req_filter (DBusConnection *con,
954 DBusMessage *message,
957 gboolean handled = FALSE;
960 if (dbus_message_is_method_call (message,
962 MODEST_DBUS_METHOD_SEARCH)) {
963 ModestDBusSearchFlags dbus_flags;
965 DBusMessage *reply = NULL;
969 dbus_int32_t flags_v;
970 dbus_uint32_t serial;
971 dbus_uint32_t size_v;
980 dbus_error_init (&error);
985 res = dbus_message_get_args (message,
987 DBUS_TYPE_STRING, &query,
988 DBUS_TYPE_STRING, &folder,
989 DBUS_TYPE_INT64, &sd_v,
990 DBUS_TYPE_INT64, &ed_v,
991 DBUS_TYPE_INT32, &flags_v,
992 DBUS_TYPE_UINT32, &size_v,
995 dbus_flags = (ModestDBusSearchFlags) flags_v;
996 start_date = (time_t) sd_v;
997 end_date = (time_t) ed_v;
999 memset (&search, 0, sizeof (search));
1000 #ifdef MODEST_HAVE_OGS
1001 search.query = query;
1003 search.before = start_date;
1004 search.after = end_date;
1007 if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
1008 search.flags |= MODEST_SEARCH_SUBJECT;
1009 search.subject = query;
1012 if (dbus_flags & MODEST_DBUS_SEARCH_SENDER) {
1013 search.flags |= MODEST_SEARCH_SENDER;
1014 search.from = query;
1017 if (dbus_flags & MODEST_DBUS_SEARCH_RECIPIENT) {
1018 search.flags |= MODEST_SEARCH_RECIPIENT;
1019 search.recipient = query;
1022 if (dbus_flags & MODEST_DBUS_SEARCH_BODY) {
1023 search.flags |= MODEST_SEARCH_BODY;
1024 search.subject = query;
1028 search.flags |= MODEST_SEARCH_BEFORE;
1029 search.before = start_date;
1033 search.flags |= MODEST_SEARCH_AFTER;
1034 search.after = end_date;
1038 search.flags |= MODEST_SEARCH_SIZE;
1039 search.minsize = size_v;
1042 #ifdef MODEST_HAVE_OGS
1043 search.flags |= MODEST_SEARCH_USE_OGS;
1044 g_debug ("Starting search for %s", search.query);
1046 hits = modest_search_all_accounts (&search);
1048 reply = dbus_message_new_method_return (message);
1050 search_result_to_message (reply, hits);
1052 if (reply == NULL) {
1053 g_warning ("Could not create reply");
1057 dbus_connection_send (con, reply, &serial);
1058 dbus_connection_flush (con);
1059 dbus_message_unref (reply);
1068 DBUS_HANDLER_RESULT_HANDLED :
1069 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1074 modest_osso_cb_hw_state_handler(osso_hw_state_t *state, gpointer data)
1077 /* printf("%s()\n", __PRETTY_FUNCTION__); */
1079 if(state->system_inactivity_ind)
1082 else if(state->save_unsaved_data_ind)
1090 /* printf("debug: %s(): return\n", __PRETTY_FUNCTION__); */