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-tny-folder.h"
36 #include "modest-ui-actions.h"
38 #include "modest-search.h"
39 #include "widgets/modest-msg-edit-window.h"
40 #include "modest-tny-msg.h"
41 #include <libmodest-dbus-client/libmodest-dbus-client.h>
42 #include <libgnomevfs/gnome-vfs-utils.h>
45 #include <glib/gstdio.h>
46 #ifdef MODEST_HAVE_HILDON0_WIDGETS
47 #include <libgnomevfs/gnome-vfs-mime-utils.h>
49 #include <libgnomevfs/gnome-vfs-mime.h>
51 #include <tny-fs-stream.h>
54 #include <tny-iterator.h>
55 #include <tny-simple-list.h>
75 } ComposeMailIdleData;
78 * @uri An escaped URI. URIs should always be escaped.
79 * @len The length of the @uri string, or -1 if the string is null terminated.
81 * Decode a URI, or URI fragment, as per RFC 1738.
82 * http://www.ietf.org/rfc/rfc1738.txt
84 * Return value: An unescaped string. This should be freed with g_free().
86 static gchar* uri_unescape(const gchar* uri, size_t len)
94 /* Allocate an extra string so we can be sure that it is null-terminated,
95 * so we can use gnome_vfs_unescape_string().
96 * This is not efficient. */
97 gchar * escaped_nullterminated = g_strndup (uri, len);
98 gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
99 g_free (escaped_nullterminated);
104 /** uri_parse_mailto:
105 * @mailto A mailto URI, with the mailto: prefix.
106 * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings,
107 * with each name item being followed by a value item. This list should be freed with g_slist_free) after
108 * all the string items have been freed. This parameter may be NULL.
109 * Parse a mailto URI as per RFC2368.
110 * http://www.ietf.org/rfc/rfc2368.txt
112 * Return value: The to address, unescaped. This should be freed with g_free().
114 static gchar* uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
116 const gchar* start_to = NULL;
117 /* Remove the mailto: prefix:
118 * 7 is the length of "mailto:": */
119 if (strncmp (mailto, "mailto:", 7) == 0) {
120 start_to = mailto + 7;
126 /* Look for ?, or the end of the string, marking the end of the to address: */
127 const size_t len_to = strcspn (start_to, "?");
128 gchar* result_to = uri_unescape (start_to, len_to);
129 printf("debug: result_to=%s\n", result_to);
131 /* Get any other items: */
132 const size_t len_mailto = strlen (start_to);
133 const gchar* p = start_to + len_to + 1; /* parsed so far. */
134 const gchar* end = start_to + len_mailto;
135 /* GSList *items = NULL; */
136 const gchar* start_item_name = p;
137 size_t len_item_name = 0;
138 const gchar* start_item_value = NULL;
141 /* Looking for the end of a name; */
142 if (start_item_name) {
143 const size_t len = strcspn (p, "="); /* Returns whole string if none found. */
145 /* This marks the end of a name and the start of the value: */
148 /* Skip over the name and mark the start of the value: */
149 p += (len + 1); /* Skip over the = */
150 start_item_value = p;
154 /* Looking for the end of a value: */
155 if (start_item_value) {
156 const size_t len = strcspn (p, "?"); /* Returns whole string if none found. */
157 /* ? marks the start of a new item: */
159 if (start_item_name && len_item_name) {
160 /* Finish the previously-started item: */
161 gchar *item_value = uri_unescape (start_item_value, len);
162 gchar *item_name = g_strndup (start_item_name, len_item_name);
163 /* printf ("debug: item name=%s, value=%s\n", item_name, item_value); */
165 /* Append the items to the list */
166 if(list_items_and_values) {
167 *list_items_and_values = g_slist_append (*list_items_and_values, item_name);
168 *list_items_and_values = g_slist_append (*list_items_and_values, item_value);
172 /* Skip over the value and mark the start of a possible new name/value pair: */
173 p += (len + 1); /* Skip over the ? */
176 start_item_value = NULL;
186 on_idle_mail_to(gpointer user_data)
188 /* This is based on the implemenation of main.c:start_uil(): */
190 gchar *uri = (gchar*)user_data;
191 GSList *list_names_and_values = NULL;
192 gchar *to = uri_parse_mailto (uri, &list_names_and_values);
194 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
195 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
196 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
198 g_printerr ("modest: no account found\n");
201 TnyAccount *account = NULL;
203 account = modest_tny_account_store_get_transport_account_for_open_connection (
204 modest_runtime_get_account_store(), account_name);
208 g_printerr ("modest: failed to get tny account folder'\n", account_name);
210 gchar * from = modest_account_mgr_get_from_string (account_mgr,
213 g_printerr ("modest: no from address for account '%s'\n", account_name);
215 const gchar *cc = NULL;
216 const gchar *bcc = NULL;
217 const gchar *subject = NULL;
218 const gchar *body = NULL;
220 /* Get the relevant items from the list: */
221 GSList *list = list_names_and_values;
223 const gchar * name = (const gchar*)list->data;
224 GSList *list_value = g_slist_next (list);
225 const gchar * value = (const gchar*)list_value->data;
227 if (strcmp (name, "cc") == 0) {
229 } else if (strcmp (name, "bcc") == 0) {
231 } else if (strcmp (name, "subject") == 0) {
233 } else if (strcmp (name, "body") == 0) {
237 /* Go to the next pair: */
239 list = g_slist_next (list_value);
244 /* Create the message: */
245 TnyMsg *msg = modest_tny_msg_new (to, from,
246 cc, bcc, subject, body,
247 NULL /* attachments */);
250 g_printerr ("modest: failed to create message\n");
253 /* Add the message to a folder and show its UI for editing: */
254 TnyFolder *folder = modest_tny_account_get_special_folder (account,
255 TNY_FOLDER_TYPE_DRAFTS);
257 g_printerr ("modest: failed to find Drafts folder\n");
260 tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
261 gdk_threads_enter ();
263 ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
264 modest_window_mgr_register_window (modest_runtime_get_window_mgr (), win);
265 gtk_widget_show_all (GTK_WIDGET (win));
267 gdk_threads_leave ();
269 g_object_unref (G_OBJECT(folder));
270 g_object_unref (win);
273 g_object_unref (G_OBJECT(msg));
276 g_object_unref (G_OBJECT(account));
280 g_free (account_name);
282 /* Free the list, as required by the uri_parse_mailto() documentation: */
283 if (list_names_and_values)
284 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
285 g_slist_free (list_names_and_values);
291 return FALSE; /* Do not call this callback again. */
294 static gint on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
296 if (arguments->len != MODEST_DBUS_MAIL_TO_ARGS_COUNT)
299 /* Use g_idle to context-switch into the application's thread: */
301 /* Get the arguments: */
302 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_MAIL_TO_ARG_URI);
303 gchar *uri = g_strdup (val.value.s);
305 /* printf(" debug: to=%s\n", idle_data->to); */
306 g_idle_add(on_idle_mail_to, (gpointer)uri);
308 /* Note that we cannot report failures during sending,
309 * because that would be asynchronous. */
318 on_idle_compose_mail(gpointer user_data)
320 ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
324 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
325 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
326 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
328 g_printerr ("modest: no account found\n");
331 TnyAccount *account = NULL;
333 account = modest_tny_account_store_get_transport_account_for_open_connection (
334 modest_runtime_get_account_store(), account_name);
338 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
340 gchar * from = modest_account_mgr_get_from_string (account_mgr,
343 g_printerr ("modest: no from address for account '%s'\n", account_name);
346 /* Create the message: */
347 TnyMsg *msg = modest_tny_msg_new (idle_data->to, from,
348 idle_data->cc, idle_data->bcc, idle_data->subject, idle_data->body,
349 NULL); /* NULL because m_t_m_n doesn't use it */
352 g_printerr ("modest: failed to create message\n");
355 /* Add the message to a folder and show its UI for editing: */
356 TnyFolder *folder = modest_tny_account_get_special_folder (account,
357 TNY_FOLDER_TYPE_DRAFTS);
359 g_printerr ("modest: failed to find Drafts folder\n");
362 tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
364 gdk_threads_enter ();
366 ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
368 /* it seems Sketch at least sends a leading ',' -- take that into account,
370 if (idle_data->attachments && idle_data->attachments[0]==',') {
371 gchar *tmp = g_strdup (idle_data->attachments + 1);
372 g_free(idle_data->attachments);
373 idle_data->attachments = tmp;
376 list = g_strsplit(idle_data->attachments, ",", 0);
377 for (i=0; list[i] != NULL; i++) {
378 modest_msg_edit_window_attach_file_noninteractive(
379 (ModestMsgEditWindow *)win, list[i]);
383 modest_window_mgr_register_window (modest_runtime_get_window_mgr (), win);
384 gtk_widget_show_all (GTK_WIDGET (win));
386 gdk_threads_leave ();
388 g_object_unref (G_OBJECT(folder));
389 g_object_unref (win);
392 g_object_unref (G_OBJECT(msg));
395 g_object_unref (G_OBJECT(account));
399 /* Free the idle data: */
400 g_free (idle_data->to);
401 g_free (idle_data->cc);
402 g_free (idle_data->bcc);
403 g_free (idle_data->subject);
404 g_free (idle_data->body);
405 g_free (idle_data->attachments);
408 g_free (account_name);
410 return FALSE; /* Do not call this callback again. */
413 static gint on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
415 if (arguments->len != MODEST_DBUS_COMPOSE_MAIL_ARGS_COUNT)
418 /* Use g_idle to context-switch into the application's thread: */
419 ComposeMailIdleData *idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
421 /* Get the arguments: */
422 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_TO);
423 idle_data->to = g_strdup (val.value.s);
425 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_CC);
426 idle_data->cc = g_strdup (val.value.s);
428 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BCC);
429 idle_data->bcc = g_strdup (val.value.s);
431 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_SUBJECT);
432 idle_data->subject = g_strdup (val.value.s);
434 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BODY);
435 idle_data->body = g_strdup (val.value.s);
437 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
438 idle_data->attachments = g_strdup (val.value.s);
440 g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
442 /* Note that we cannot report failures during sending,
443 * because that would be asynchronous. */
449 find_message_by_url (const char *uri, TnyAccount **ac_out)
451 ModestTnyAccountStore *astore;
460 astore = modest_runtime_get_account_store ();
462 if (astore == NULL) {
466 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (astore),
469 if (account == NULL) {
470 g_debug ("%s: tny_account_store_find_account() failed.\n", __FUNCTION__);
474 g_debug ("%s: Found account.\n", __FUNCTION__);
476 if ( ! TNY_IS_STORE_ACCOUNT (account)) {
480 g_debug ("%s: Account is store account.\n", __FUNCTION__);
483 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account),
487 if (folder == NULL) {
488 g_debug ("%s: tny_store_account_find_folder() failed\naccount=%s, uri=%s.\n", __FUNCTION__,
489 tny_account_get_id (TNY_ACCOUNT(account)), uri);
493 g_debug ("%s: Found folder. (%s)\n", __FUNCTION__, uri);
495 msg = tny_folder_find_msg (folder, uri, &err);
498 g_debug ("%s: tny_folder_find_msg() failed for folder %s\n with error=%s.\n",
499 __FUNCTION__, tny_folder_get_id (folder), err->message);
506 if (account && !msg) {
507 g_object_unref (account);
512 g_object_unref (folder);
518 on_idle_open_message (gpointer user_data)
520 ModestWindow *msg_view = NULL;
522 TnyAccount *account = NULL;
525 const char *account_name;
527 ModestWindowMgr *win_mgr;
528 TnyFolder *folder = NULL;
530 uri = (char *) user_data;
532 /* g_debug ("modest: %s: Trying to find msg by url: %s", __FUNCTION__, uri); */
533 msg = find_message_by_url (uri, &account);
537 g_debug ("modest: %s: message not found.", __FUNCTION__);
540 g_debug ("modest: %s: Found message.", __FUNCTION__);
542 folder = tny_msg_get_folder (msg);
543 if (modest_tny_folder_get_local_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS) {
544 g_debug ("TODO: draft messages should be opened in edit mode... ");
547 header = tny_msg_get_header (msg);
548 account_name = tny_account_get_name (account);
549 msg_uid = modest_tny_folder_get_header_unique_id(header);
550 /* FIXME: modest_tny_folder_get_header_unique_id warns against this */
551 win_mgr = modest_runtime_get_window_mgr ();
553 gdk_threads_enter ();
555 if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
556 g_debug ("modest: %s: A window for this messsage is open already.", __FUNCTION__);
557 if (!MODEST_IS_MSG_VIEW_WINDOW(msg_view))
558 g_debug (" DEBUG: But the window is not a msg view");
560 gtk_window_present (GTK_WINDOW(msg_view));
563 /* g_debug ("creating new window for this msg"); */
564 modest_window_mgr_register_header (win_mgr, header);
565 msg_view = modest_msg_view_window_new (msg,account_name,
567 modest_window_mgr_register_window (win_mgr, msg_view);
568 gtk_widget_show_all (GTK_WIDGET (msg_view));
571 gdk_threads_leave ();
573 g_object_unref (header);
574 g_object_unref (account);
575 g_object_unref (folder);
577 return FALSE; /* Do not call this callback again. */
580 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
582 if (arguments->len != MODEST_DBUS_OPEN_MESSAGE_ARGS_COUNT)
585 /* Use g_idle to context-switch into the application's thread: */
587 /* Get the arguments: */
588 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_OPEN_MESSAGE_ARG_URI);
589 gchar *uri = g_strdup (val.value.s);
591 /* printf(" debug: to=%s\n", idle_data->to); */
592 g_idle_add(on_idle_open_message, (gpointer)uri);
594 /* Note that we cannot report failures during sending,
595 * because that would be asynchronous. */
600 on_idle_delete_message (gpointer user_data)
606 TnyHeader *msg_header;
614 uri = (char *) user_data;
616 /* g_debug ("modest: %s Searching for message (delete message)"); */
618 msg = find_message_by_url (uri, &account);
624 g_debug ("modest: %s: Found message", __FUNCTION__);
626 msg_header = tny_msg_get_header (msg);
627 uid = tny_header_get_uid (msg_header);
628 folder = tny_msg_get_folder (msg);
631 /* tny_msg_get_header () flaw:
632 * From tinythingy doc: You can't use the returned instance with the
633 * TnyFolder operations
635 * To get a header instance that will work with these folder methods,
636 * you can use tny_folder_get_headers.
638 * Ok, we will do so then. Sigh.
640 headers = tny_simple_list_new ();
642 tny_folder_get_headers (folder, headers, TRUE, NULL);
643 iter = tny_list_create_iterator (headers);
646 /* g_debug ("Searching header for msg in folder"); */
647 while (!tny_iterator_is_done (iter)) {
650 header = TNY_HEADER (tny_iterator_get_current (iter));
651 cur_id = tny_header_get_uid (header);
653 if (cur_id && uid && g_str_equal (cur_id, uid)) {
654 /* g_debug ("Found corresponding header from folder"); */
659 g_object_unref (header);
660 tny_iterator_next (iter);
663 g_object_unref (iter);
665 g_object_unref (headers);
668 g_object_unref (msg_header);
670 g_object_unref (msg);
673 if (header == NULL) {
674 g_object_unref (folder);
680 tny_folder_remove_msg (folder, header, &error);
681 tny_header_set_flags (header, TNY_HEADER_FLAG_SEEN);
685 g_error_free (error);
688 gdk_threads_enter ();
690 ModestWindowMgr *win_mgr = modest_runtime_get_window_mgr ();
691 ModestWindow *msg_view = NULL;
692 if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
693 if (MODEST_IS_MSG_VIEW_WINDOW (msg_view))
694 modest_ui_actions_refresh_message_window_after_delete (MODEST_MSG_VIEW_WINDOW (msg_view));
697 gdk_threads_leave ();
699 g_object_unref (header);
700 g_object_unref (folder);
705 on_delete_message (GArray *arguments, gpointer data, osso_rpc_t *retval)
707 if (arguments->len != MODEST_DBUS_DELETE_MESSAGE_ARGS_COUNT)
710 /* Use g_idle to context-switch into the application's thread: */
712 /* Get the arguments: */
713 osso_rpc_t val = g_array_index (arguments,
715 MODEST_DBUS_DELETE_MESSAGE_ARG_URI);
716 gchar *uri = g_strdup (val.value.s);
718 /* printf(" debug: to=%s\n", idle_data->to); */
719 g_idle_add(on_idle_delete_message, (gpointer)uri);
721 /* Note that we cannot report failures during sending,
722 * because that would be asynchronous. */
727 on_idle_send_receive(gpointer user_data)
731 gdk_threads_enter ();
733 /* Pick the main window if it exists */
734 win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
736 /* Send & receive all if "Update automatically" is set */
737 /* TODO: check the auto-update parameter in the configuration */
738 modest_ui_actions_do_send_receive_all (win);
740 gdk_threads_leave ();
742 return FALSE; /* Do not call this callback again. */
745 static gint on_send_receive(GArray * arguments, gpointer data, osso_rpc_t * retval)
747 /* Use g_idle to context-switch into the application's thread: */
749 /* This method has no arguments. */
751 /* printf(" debug: to=%s\n", idle_data->to); */
752 g_idle_add(on_idle_send_receive, NULL);
754 /* Note that we cannot report failures during send/receive,
755 * because that would be asynchronous. */
760 on_idle_open_default_inbox(gpointer user_data)
762 gdk_threads_enter ();
765 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
767 /* Get the folder view */
768 GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
769 MODEST_WIDGET_TYPE_FOLDER_VIEW);
770 modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (folder_view));
772 gdk_threads_leave ();
774 return FALSE; /* Do not call this callback again. */
777 static gint on_open_default_inbox(GArray * arguments, gpointer data, osso_rpc_t * retval)
779 /* Use g_idle to context-switch into the application's thread: */
781 /* This method has no arguments. */
783 g_idle_add(on_idle_open_default_inbox, NULL);
785 /* Note that we cannot report failures during send/receive,
786 * because that would be asynchronous. */
791 static gboolean on_idle_top_application (gpointer user_data)
793 gdk_threads_enter ();
796 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
798 /* Ideally, we would just use gtk_widget_show(),
799 * but this widget is not coded correctly to support that: */
800 gtk_widget_show_all (GTK_WIDGET (win));
801 gtk_window_present (GTK_WINDOW (win));
804 gdk_threads_leave ();
806 return FALSE; /* Do not call this callback again. */
809 static gint on_top_application(GArray * arguments, gpointer data, osso_rpc_t * retval)
811 /* Use g_idle to context-switch into the application's thread: */
813 /* This method has no arguments. */
815 g_idle_add(on_idle_top_application, NULL);
820 /* Callback for normal D-BUS messages */
821 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
822 GArray * arguments, gpointer data,
826 /* g_debug ("debug: %s\n", __FUNCTION__); */
827 g_debug ("debug: %s: method received: %s\n", __FUNCTION__, method);
829 if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
830 return on_mail_to (arguments, data, retval);
831 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
832 return on_open_message (arguments, data, retval);
833 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
834 return on_send_receive (arguments, data, retval);
835 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
836 return on_compose_mail (arguments, data, retval);
837 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_DELETE_MESSAGE) == 0) {
838 return on_delete_message (arguments,data, retval);
839 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX) == 0) {
840 return on_open_default_inbox (arguments, data, retval);
841 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_TOP_APPLICATION) == 0) {
842 return on_top_application (arguments, data, retval);
845 g_debug (" debug: %s: Unexpected D-Bus method: %s\n", __FUNCTION__, method);
847 /* We need to return INVALID here so
848 * libosso will return DBUS_HANDLER_RESULT_NOT_YET_HANDLED,
849 * so that our modest_dbus_req_filter will then be tried instead.
855 /* A complex D-Bus type (like a struct),
856 * used to return various information about a search hit.
858 #define SEARCH_HIT_DBUS_TYPE \
859 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
860 DBUS_TYPE_STRING_AS_STRING /* msgid */ \
861 DBUS_TYPE_STRING_AS_STRING /* subject */ \
862 DBUS_TYPE_STRING_AS_STRING /* folder */ \
863 DBUS_TYPE_STRING_AS_STRING /* sender */ \
864 DBUS_TYPE_UINT64_AS_STRING /* msize */ \
865 DBUS_TYPE_BOOLEAN_AS_STRING /* has_attachment */ \
866 DBUS_TYPE_BOOLEAN_AS_STRING /* is_unread */ \
867 DBUS_TYPE_INT64_AS_STRING /* timestamp */ \
868 DBUS_STRUCT_END_CHAR_AS_STRING
871 search_result_to_message (DBusMessage *reply,
874 DBusMessageIter iter;
875 DBusMessageIter array_iter;
878 dbus_message_iter_init_append (reply, &iter);
879 dbus_message_iter_open_container (&iter,
881 SEARCH_HIT_DBUS_TYPE,
884 for (hit_iter = hits; hit_iter; hit_iter = hit_iter->next) {
885 DBusMessageIter struct_iter;
886 ModestSearchHit *hit;
892 gboolean has_attachment;
896 hit = (ModestSearchHit *) hit_iter->data;
898 msg_url = hit->msgid;
899 subject = hit->subject;
900 folder = hit->folder;
901 sender = hit->sender;
903 has_attachment = hit->has_attachment;
904 is_unread = hit->is_unread;
907 g_debug ("DEBUG: %s: Adding hit: %s", __FUNCTION__, msg_url);
909 dbus_message_iter_open_container (&array_iter,
914 dbus_message_iter_append_basic (&struct_iter,
918 dbus_message_iter_append_basic (&struct_iter,
922 dbus_message_iter_append_basic (&struct_iter,
926 dbus_message_iter_append_basic (&struct_iter,
930 dbus_message_iter_append_basic (&struct_iter,
934 dbus_message_iter_append_basic (&struct_iter,
938 dbus_message_iter_append_basic (&struct_iter,
942 dbus_message_iter_append_basic (&struct_iter,
946 dbus_message_iter_close_container (&array_iter,
950 g_free (hit->subject);
951 g_free (hit->folder);
952 g_free (hit->sender);
954 g_slice_free (ModestSearchHit, hit);
957 dbus_message_iter_close_container (&iter, &array_iter);
964 on_dbus_method_search (DBusConnection *con, DBusMessage *message)
966 ModestDBusSearchFlags dbus_flags;
967 DBusMessage *reply = NULL;
971 dbus_int32_t flags_v;
972 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, /* e.g. "INBOX/drafts": TODO: Use both an ID and a display name. */
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;
1000 memset (&search, 0, sizeof (search));
1002 /* Remember what folder we are searching in:
1004 * Note that we don't copy the strings,
1005 * because this struct will only be used for the lifetime of this function.
1007 search.folder = folder;
1009 /* Remember the text to search for: */
1010 #ifdef MODEST_HAVE_OGS
1011 search.query = query;
1014 /* Other criteria: */
1015 search.before = start_date;
1016 search.after = end_date;
1019 /* Text to serach for in various parts of the message: */
1020 if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
1021 search.flags |= MODEST_SEARCH_SUBJECT;
1022 search.subject = query;
1025 if (dbus_flags & MODEST_DBUS_SEARCH_SENDER) {
1026 search.flags |= MODEST_SEARCH_SENDER;
1027 search.from = query;
1030 if (dbus_flags & MODEST_DBUS_SEARCH_RECIPIENT) {
1031 search.flags |= MODEST_SEARCH_RECIPIENT;
1032 search.recipient = query;
1035 if (dbus_flags & MODEST_DBUS_SEARCH_BODY) {
1036 search.flags |= MODEST_SEARCH_BODY;
1037 search.body = query;
1041 search.flags |= MODEST_SEARCH_BEFORE;
1042 search.before = start_date;
1046 search.flags |= MODEST_SEARCH_AFTER;
1047 search.after = end_date;
1051 search.flags |= MODEST_SEARCH_SIZE;
1052 search.minsize = size_v;
1055 #ifdef MODEST_HAVE_OGS
1056 search.flags |= MODEST_SEARCH_USE_OGS;
1057 g_debug ("%s: Starting search for %s", __FUNCTION__, search.query);
1060 /* Note that this currently gets folders and messages from the servers,
1061 * which can take a long time. libmodest_dbus_client_search() can timeout,
1062 * reporting no results, if this takes a long time: */
1063 hits = modest_search_all_accounts (&search);
1065 reply = dbus_message_new_method_return (message);
1067 search_result_to_message (reply, hits);
1069 if (reply == NULL) {
1070 g_warning ("%s: Could not create reply.", __FUNCTION__);
1074 dbus_uint32_t serial = 0;
1075 dbus_connection_send (con, reply, &serial);
1076 dbus_connection_flush (con);
1077 dbus_message_unref (reply);
1084 /* A complex D-Bus type (like a struct),
1085 * used to return various information about a folder.
1087 #define GET_FOLDERS_RESULT_DBUS_TYPE \
1088 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1089 DBUS_TYPE_STRING_AS_STRING /* Folder Name */ \
1090 DBUS_TYPE_STRING_AS_STRING /* Folder URI */ \
1091 DBUS_STRUCT_END_CHAR_AS_STRING
1093 static DBusMessage *
1094 get_folders_result_to_message (DBusMessage *reply,
1097 DBusMessageIter iter;
1098 dbus_message_iter_init_append (reply, &iter);
1100 DBusMessageIter array_iter;
1101 dbus_message_iter_open_container (&iter,
1103 GET_FOLDERS_RESULT_DBUS_TYPE,
1106 GList *list_iter = folder_ids;
1107 for (list_iter = folder_ids; list_iter; list_iter = list_iter->next) {
1109 const gchar *folder_name = (const gchar*)list_iter->data;
1111 /* g_debug ("DEBUG: %s: Adding folder: %s", __FUNCTION__, folder_name); */
1113 DBusMessageIter struct_iter;
1114 dbus_message_iter_open_container (&array_iter,
1120 dbus_message_iter_append_basic (&struct_iter,
1122 &folder_name); /* The string will be copied. */
1124 /* URI: This is maybe not needed by osso-global-search: */
1125 const gchar *folder_uri = "TODO:unimplemented";
1126 dbus_message_iter_append_basic (&struct_iter,
1128 &folder_uri); /* The string will be copied. */
1130 dbus_message_iter_close_container (&array_iter,
1135 dbus_message_iter_close_container (&iter, &array_iter);
1141 add_single_folder_to_list (TnyFolder *folder, GList** list)
1146 /* Add this folder to the list: */
1148 const gchar * folder_name = tny_folder_get_name (folder);
1150 *list = g_list_append(*list, g_strdup (folder_name));
1153 /* osso-global-search only uses one string,
1154 * so ID is the only thing that could possibly identify a folder.
1155 * TODO: osso-global search should probably be changed to
1156 * take an ID and a Name.
1158 const gchar * id = tny_folder_get_id (folder);
1159 if (id && strlen(id))
1160 *list = g_list_append(*list, g_strdup (id));
1163 g_warning ("DEBUG: %s: folder has no name or ID.\n", __FUNCTION__);
1171 add_folders_to_list (TnyFolderStore *folder_store, GList** list)
1176 /* Add this folder to the list: */
1177 if (TNY_IS_FOLDER (folder_store)) {
1178 add_single_folder_to_list (TNY_FOLDER (folder_store), list);
1181 /* Recurse into child folders: */
1183 /* Get the folders list: */
1185 TnyFolderStoreQuery *query = tny_folder_store_query_new ();
1186 tny_folder_store_query_add_item (query, NULL,
1187 TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
1189 TnyList *all_folders = tny_simple_list_new ();
1190 tny_folder_store_get_folders (folder_store,
1195 TnyIterator *iter = tny_list_create_iterator (all_folders);
1196 while (!tny_iterator_is_done (iter)) {
1198 /* Do not recurse, because the osso-global-search UI specification
1199 * does not seem to want the sub-folders, though that spec seems to
1200 * be generally unsuitable for Modest.
1202 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
1203 add_single_folder_to_list (TNY_FOLDER (folder), list);
1206 if (TNY_IS_FOLDER_STORE (folder))
1207 add_folders_to_list (TNY_FOLDER_STORE (folder), list);
1209 add_single_folder_to_list (TNY_FOLDER (folder), list);
1213 tny_iterator_next (iter);
1215 g_object_unref (G_OBJECT (iter));
1219 on_dbus_method_get_folders (DBusConnection *con, DBusMessage *message)
1221 DBusMessage *reply = NULL;
1224 /* Get the TnyStoreAccount so we can get the folders: */
1225 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
1226 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
1227 if (!account_name) {
1228 g_printerr ("modest: no account found\n");
1231 TnyAccount *account = NULL;
1233 account = modest_tny_account_store_get_server_account (
1234 modest_runtime_get_account_store(), account_name,
1235 TNY_ACCOUNT_TYPE_STORE);
1239 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
1242 printf("DEBUG: %s: Getting folders for account name=%s\n", __FUNCTION__, account_name);
1243 g_free (account_name);
1244 account_name = NULL;
1246 GList *folder_names = NULL;
1247 add_folders_to_list (TNY_FOLDER_STORE (account), &folder_names);
1249 g_object_unref (account);
1253 /* Also add the folders from the local folders account,
1254 * because they are (currently) used with all accounts:
1255 * TODO: This is not working. It seems to get only the Merged Folder (with an ID of "" (not NULL)).
1257 TnyAccount *account_local =
1258 modest_tny_account_store_get_local_folders_account (
1259 TNY_ACCOUNT_STORE (modest_runtime_get_account_store()));
1260 add_folders_to_list (TNY_FOLDER_STORE (account_local), &folder_names);
1262 g_object_unref (account_local);
1263 account_local = NULL;
1266 /* Put the result in a DBus reply: */
1267 reply = dbus_message_new_method_return (message);
1269 get_folders_result_to_message (reply, folder_names);
1271 if (reply == NULL) {
1272 g_warning ("%s: Could not create reply.", __FUNCTION__);
1276 dbus_uint32_t serial = 0;
1277 dbus_connection_send (con, reply, &serial);
1278 dbus_connection_flush (con);
1279 dbus_message_unref (reply);
1282 g_list_foreach (folder_names, (GFunc)g_free, NULL);
1283 g_list_free (folder_names);
1287 /** This D-Bus handler is used when the main osso-rpc
1288 * D-Bus handler has not handled something.
1289 * We use this for D-Bus methods that need to use more complex types
1290 * than osso-rpc supports.
1293 modest_dbus_req_filter (DBusConnection *con,
1294 DBusMessage *message,
1297 gboolean handled = FALSE;
1299 if (dbus_message_is_method_call (message,
1301 MODEST_DBUS_METHOD_SEARCH)) {
1302 on_dbus_method_search (con, message);
1304 } else if (dbus_message_is_method_call (message,
1306 MODEST_DBUS_METHOD_GET_FOLDERS)) {
1307 on_dbus_method_get_folders (con, message);
1312 DBUS_HANDLER_RESULT_HANDLED :
1313 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1318 modest_osso_cb_hw_state_handler(osso_hw_state_t *state, gpointer data)
1321 /* printf("%s()\n", __PRETTY_FUNCTION__); */
1323 if(state->system_inactivity_ind)
1326 else if(state->save_unsaved_data_ind)
1334 /* printf("debug: %s(): return\n", __PRETTY_FUNCTION__); */