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>
56 #include <tny-merge-folder.h>
76 } ComposeMailIdleData;
79 * @uri An escaped URI. URIs should always be escaped.
80 * @len The length of the @uri string, or -1 if the string is null terminated.
82 * Decode a URI, or URI fragment, as per RFC 1738.
83 * http://www.ietf.org/rfc/rfc1738.txt
85 * Return value: An unescaped string. This should be freed with g_free().
87 static gchar* uri_unescape(const gchar* uri, size_t len)
95 /* Allocate an extra string so we can be sure that it is null-terminated,
96 * so we can use gnome_vfs_unescape_string().
97 * This is not efficient. */
98 gchar * escaped_nullterminated = g_strndup (uri, len);
99 gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
100 g_free (escaped_nullterminated);
105 /** uri_parse_mailto:
106 * @mailto A mailto URI, with the mailto: prefix.
107 * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings,
108 * with each name item being followed by a value item. This list should be freed with g_slist_free) after
109 * all the string items have been freed. This parameter may be NULL.
110 * Parse a mailto URI as per RFC2368.
111 * http://www.ietf.org/rfc/rfc2368.txt
113 * Return value: The to address, unescaped. This should be freed with g_free().
115 static gchar* uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
117 const gchar* start_to = NULL;
118 /* Remove the mailto: prefix:
119 * 7 is the length of "mailto:": */
120 if (strncmp (mailto, "mailto:", 7) == 0) {
121 start_to = mailto + 7;
127 /* Look for ?, or the end of the string, marking the end of the to address: */
128 const size_t len_to = strcspn (start_to, "?");
129 gchar* result_to = uri_unescape (start_to, len_to);
130 printf("debug: result_to=%s\n", result_to);
132 /* Get any other items: */
133 const size_t len_mailto = strlen (start_to);
134 const gchar* p = start_to + len_to + 1; /* parsed so far. */
135 const gchar* end = start_to + len_mailto;
136 /* GSList *items = NULL; */
137 const gchar* start_item_name = p;
138 size_t len_item_name = 0;
139 const gchar* start_item_value = NULL;
142 /* Looking for the end of a name; */
143 if (start_item_name) {
144 const size_t len = strcspn (p, "="); /* Returns whole string if none found. */
146 /* This marks the end of a name and the start of the value: */
149 /* Skip over the name and mark the start of the value: */
150 p += (len + 1); /* Skip over the = */
151 start_item_value = p;
155 /* Looking for the end of a value: */
156 if (start_item_value) {
157 const size_t len = strcspn (p, "?"); /* Returns whole string if none found. */
158 /* ? marks the start of a new item: */
160 if (start_item_name && len_item_name) {
161 /* Finish the previously-started item: */
162 gchar *item_value = uri_unescape (start_item_value, len);
163 gchar *item_name = g_strndup (start_item_name, len_item_name);
164 /* printf ("debug: item name=%s, value=%s\n", item_name, item_value); */
166 /* Append the items to the list */
167 if(list_items_and_values) {
168 *list_items_and_values = g_slist_append (*list_items_and_values, item_name);
169 *list_items_and_values = g_slist_append (*list_items_and_values, item_value);
173 /* Skip over the value and mark the start of a possible new name/value pair: */
174 p += (len + 1); /* Skip over the ? */
177 start_item_value = NULL;
187 on_idle_mail_to(gpointer user_data)
189 /* This is based on the implemenation of main.c:start_uil(): */
191 gchar *uri = (gchar*)user_data;
192 GSList *list_names_and_values = NULL;
193 gchar *to = uri_parse_mailto (uri, &list_names_and_values);
195 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
196 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
197 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
199 g_printerr ("modest: no account found\n");
202 TnyAccount *account = NULL;
204 account = modest_tny_account_store_get_transport_account_for_open_connection (
205 modest_runtime_get_account_store(), account_name);
209 g_printerr ("modest: failed to get tny account folder'\n", account_name);
211 gchar * from = modest_account_mgr_get_from_string (account_mgr,
214 g_printerr ("modest: no from address for account '%s'\n", account_name);
216 const gchar *cc = NULL;
217 const gchar *bcc = NULL;
218 const gchar *subject = NULL;
219 const gchar *body = NULL;
221 /* Get the relevant items from the list: */
222 GSList *list = list_names_and_values;
224 const gchar * name = (const gchar*)list->data;
225 GSList *list_value = g_slist_next (list);
226 const gchar * value = (const gchar*)list_value->data;
228 if (strcmp (name, "cc") == 0) {
230 } else if (strcmp (name, "bcc") == 0) {
232 } else if (strcmp (name, "subject") == 0) {
234 } else if (strcmp (name, "body") == 0) {
238 /* Go to the next pair: */
240 list = g_slist_next (list_value);
245 /* Create the message: */
246 TnyMsg *msg = modest_tny_msg_new (to, from,
247 cc, bcc, subject, body,
248 NULL /* attachments */);
251 g_printerr ("modest: failed to create message\n");
254 /* Add the message to a folder and show its UI for editing: */
255 TnyFolder *folder = modest_tny_account_get_special_folder (account,
256 TNY_FOLDER_TYPE_DRAFTS);
258 g_printerr ("modest: failed to find Drafts folder\n");
261 tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
262 gdk_threads_enter ();
264 ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
265 modest_window_mgr_register_window (modest_runtime_get_window_mgr (), win);
266 gtk_widget_show_all (GTK_WIDGET (win));
268 gdk_threads_leave ();
270 g_object_unref (G_OBJECT(folder));
271 g_object_unref (win);
274 g_object_unref (G_OBJECT(msg));
277 g_object_unref (G_OBJECT(account));
281 g_free (account_name);
283 /* Free the list, as required by the uri_parse_mailto() documentation: */
284 if (list_names_and_values)
285 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
286 g_slist_free (list_names_and_values);
292 return FALSE; /* Do not call this callback again. */
295 static gint on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
297 if (arguments->len != MODEST_DBUS_MAIL_TO_ARGS_COUNT)
300 /* Use g_idle to context-switch into the application's thread: */
302 /* Get the arguments: */
303 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_MAIL_TO_ARG_URI);
304 gchar *uri = g_strdup (val.value.s);
306 /* printf(" debug: to=%s\n", idle_data->to); */
307 g_idle_add(on_idle_mail_to, (gpointer)uri);
309 /* Note that we cannot report failures during sending,
310 * because that would be asynchronous. */
316 on_idle_compose_mail(gpointer user_data)
318 ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
322 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
323 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
324 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
326 g_printerr ("modest: no account found.\n");
328 /* TODO: If the call to this D-Bus method caused the application to start
329 * then the new-account wizard will now be shown, and we need to wait
330 * until the account exists instead of just failing.
334 TnyAccount *account = NULL;
335 if (account_name && account_mgr) {
336 account = modest_tny_account_store_get_transport_account_for_open_connection (
337 modest_runtime_get_account_store(), account_name);
341 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
343 gchar * from = modest_account_mgr_get_from_string (account_mgr,
346 g_printerr ("modest: no from address for account '%s'\n", account_name);
349 /* Create the message: */
350 TnyMsg *msg = modest_tny_msg_new (idle_data->to, from,
351 idle_data->cc, idle_data->bcc, idle_data->subject, idle_data->body,
352 NULL); /* NULL because m_t_m_n doesn't use it */
355 g_printerr ("modest: failed to create message\n");
358 /* Add the message to a folder and show its UI for editing: */
359 TnyFolder *folder = modest_tny_account_get_special_folder (account,
360 TNY_FOLDER_TYPE_DRAFTS);
362 g_printerr ("modest: failed to find Drafts folder\n");
365 tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
367 gdk_threads_enter ();
369 ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
371 /* it seems Sketch at least sends a leading ',' -- take that into account,
373 if (idle_data->attachments && idle_data->attachments[0]==',') {
374 gchar *tmp = g_strdup (idle_data->attachments + 1);
375 g_free(idle_data->attachments);
376 idle_data->attachments = tmp;
379 list = g_strsplit(idle_data->attachments, ",", 0);
380 for (i=0; list[i] != NULL; i++) {
381 modest_msg_edit_window_attach_file_noninteractive(
382 (ModestMsgEditWindow *)win, list[i]);
386 modest_window_mgr_register_window (modest_runtime_get_window_mgr (), win);
387 gtk_widget_show_all (GTK_WIDGET (win));
389 gdk_threads_leave ();
391 g_object_unref (G_OBJECT(folder));
392 g_object_unref (win);
395 g_object_unref (G_OBJECT(msg));
398 g_object_unref (G_OBJECT(account));
402 /* Free the idle data: */
403 g_free (idle_data->to);
404 g_free (idle_data->cc);
405 g_free (idle_data->bcc);
406 g_free (idle_data->subject);
407 g_free (idle_data->body);
408 g_free (idle_data->attachments);
411 g_free (account_name);
413 return FALSE; /* Do not call this callback again. */
416 static gint on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
418 if (arguments->len != MODEST_DBUS_COMPOSE_MAIL_ARGS_COUNT)
421 /* Use g_idle to context-switch into the application's thread: */
422 ComposeMailIdleData *idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
424 /* Get the arguments: */
425 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_TO);
426 idle_data->to = g_strdup (val.value.s);
428 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_CC);
429 idle_data->cc = g_strdup (val.value.s);
431 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BCC);
432 idle_data->bcc = g_strdup (val.value.s);
434 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_SUBJECT);
435 idle_data->subject = g_strdup (val.value.s);
437 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BODY);
438 idle_data->body = g_strdup (val.value.s);
440 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
441 idle_data->attachments = g_strdup (val.value.s);
443 g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
445 /* Note that we cannot report failures during sending,
446 * because that would be asynchronous. */
452 find_message_by_url (const char *uri, TnyAccount **ac_out)
454 ModestTnyAccountStore *astore;
463 astore = modest_runtime_get_account_store ();
465 if (astore == NULL) {
469 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (astore),
472 if (account == NULL) {
473 g_debug ("%s: tny_account_store_find_account() failed for\n uri=%s\n",
478 g_debug ("%s: Found account.\n", __FUNCTION__);
480 if ( ! TNY_IS_STORE_ACCOUNT (account)) {
484 g_debug ("%s: Account is store account.\n", __FUNCTION__);
487 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account),
491 if (folder == NULL) {
492 g_debug ("%s: tny_store_account_find_folder() failed for\n account=%s, uri=%s.\n", __FUNCTION__,
493 tny_account_get_id (TNY_ACCOUNT(account)), uri);
497 g_debug ("%s: Found folder. (%s)\n", __FUNCTION__, uri);
499 msg = tny_folder_find_msg (folder, uri, &err);
502 g_debug ("%s: tny_folder_find_msg() failed for folder %s\n with error=%s.\n",
503 __FUNCTION__, tny_folder_get_id (folder), err->message);
510 if (account && !msg) {
511 g_object_unref (account);
516 g_object_unref (folder);
522 on_idle_open_message (gpointer user_data)
525 TnyAccount *account = NULL;
526 TnyHeader *header = NULL;
527 const char *msg_uid = NULL;
529 ModestWindowMgr *win_mgr = NULL;
530 TnyFolder *folder = NULL;
532 uri = (char *) user_data;
534 /* g_debug ("modest: %s: Trying to find msg by url: %s", __FUNCTION__, uri); */
535 msg = find_message_by_url (uri, &account);
539 g_debug ("modest: %s: message not found.", __FUNCTION__);
542 g_debug ("modest: %s: Found message.", __FUNCTION__);
545 folder = tny_msg_get_folder (msg);
547 /* Drafts will be opened in the editor, instead of the viewer, as per the UI spec: */
548 gboolean is_draft = FALSE;
549 if (folder && modest_tny_folder_is_local_folder (folder) &&
550 (modest_tny_folder_get_local_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS)) {
554 header = tny_msg_get_header (msg);
556 /* TODO: The modest_tny_folder_get_header_unique_id() documentation warns against
557 * using it with tny_msg_get_header(), and there is a
558 * " camel_folder_get_full_name: assertion `CAMEL_IS_FOLDER (folder)' failed" runtime warning,
559 * but it seems to work.
561 msg_uid = modest_tny_folder_get_header_unique_id(header);
563 win_mgr = modest_runtime_get_window_mgr ();
565 gdk_threads_enter ();
567 gboolean already_opened = FALSE;
568 ModestWindow *msg_view = NULL;
569 if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
571 g_debug ("modest: %s: A window for this message is open already: type=%s",
572 __FUNCTION__, G_OBJECT_TYPE_NAME (msg_view));
576 g_debug ("modest_window_mgr_find_registered_header(): Returned TRUE, but msg_view is NULL");
577 else if (!MODEST_IS_MSG_VIEW_WINDOW (msg_view) && !MODEST_IS_MSG_EDIT_WINDOW (msg_view))
578 g_debug (" DEBUG: But the window is not a msg view or edit window.");
580 gtk_window_present (GTK_WINDOW(msg_view));
581 already_opened = TRUE;
585 if (!already_opened) {
586 /* g_debug ("creating new window for this msg"); */
587 modest_window_mgr_register_header (win_mgr, header);
589 const gchar *modest_account_name =
590 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
592 /* Drafts will be opened in the editor, and others will be opened in the viewer,
593 * as per the UI spec: */
595 /* TODO: Maybe the msg_uid should be registered for edit windows too,
596 * so we can open the same window again next time: */
597 msg_view = modest_msg_edit_window_new (msg, modest_account_name);
599 msg_view = modest_msg_view_window_new (msg, modest_account_name,
603 modest_window_mgr_register_window (win_mgr, msg_view);
604 gtk_widget_show_all (GTK_WIDGET (msg_view));
607 gdk_threads_leave ();
609 g_object_unref (header);
610 g_object_unref (account);
611 g_object_unref (folder);
613 return FALSE; /* Do not call this callback again. */
616 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
618 if (arguments->len != MODEST_DBUS_OPEN_MESSAGE_ARGS_COUNT)
621 /* Use g_idle to context-switch into the application's thread: */
623 /* Get the arguments: */
624 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_OPEN_MESSAGE_ARG_URI);
625 gchar *uri = g_strdup (val.value.s);
627 /* printf(" debug: to=%s\n", idle_data->to); */
628 g_idle_add(on_idle_open_message, (gpointer)uri);
630 /* Note that we cannot report failures during sending,
631 * because that would be asynchronous. */
636 on_idle_delete_message (gpointer user_data)
638 TnyList *headers = NULL;
639 TnyFolder *folder = NULL;
640 TnyIterator *iter = NULL;
641 TnyHeader *header = NULL;
642 TnyHeader *msg_header = NULL;
644 TnyAccount *account = NULL;
645 GError *error = NULL;
646 const char *uri = NULL;
647 const char *uid = NULL;
650 uri = (char *) user_data;
652 /* g_debug ("modest: %s Searching for message (delete message)"); */
654 msg = find_message_by_url (uri, &account);
660 g_debug ("modest: %s: Found message", __FUNCTION__);
662 msg_header = tny_msg_get_header (msg);
663 uid = tny_header_get_uid (msg_header);
664 folder = tny_msg_get_folder (msg);
667 /* tny_msg_get_header () flaw:
668 * From tinythingy doc: You can't use the returned instance with the
669 * TnyFolder operations
671 * To get a header instance that will work with these folder methods,
672 * you can use tny_folder_get_headers.
674 * Ok, we will do so then. Sigh.
676 headers = tny_simple_list_new ();
678 tny_folder_get_headers (folder, headers, TRUE, NULL);
679 iter = tny_list_create_iterator (headers);
682 /* g_debug ("Searching header for msg in folder"); */
683 while (!tny_iterator_is_done (iter)) {
684 const char *cur_id = NULL;
686 header = TNY_HEADER (tny_iterator_get_current (iter));
688 cur_id = tny_header_get_uid (header);
690 if (cur_id && uid && g_str_equal (cur_id, uid)) {
691 /* g_debug ("Found corresponding header from folder"); */
696 g_object_unref (header);
700 tny_iterator_next (iter);
703 g_object_unref (iter);
705 g_object_unref (headers);
708 g_object_unref (msg_header);
710 g_object_unref (msg);
713 if (header == NULL) {
715 g_object_unref (folder);
723 gdk_threads_enter ();
724 ModestWindow *win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
725 modest_do_message_delete (header, win);
729 g_error_free (error);
734 ModestWindowMgr *win_mgr = modest_runtime_get_window_mgr ();
735 ModestWindow *msg_view = NULL;
736 if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
737 if (MODEST_IS_MSG_VIEW_WINDOW (msg_view))
738 modest_ui_actions_refresh_message_window_after_delete (MODEST_MSG_VIEW_WINDOW (msg_view));
741 gdk_threads_leave ();
744 g_object_unref (header);
747 /* Trick: do a poke status in order to speed up the signaling
749 A delete via the menu does this, in do_headers_action(),
750 but it does not seem to solve the stricken-out-but-not-removed problem here.
752 tny_folder_poke_status (folder);
754 g_object_unref (folder);
758 /* Do a refresh so that stricken-out emails are really removed from the treeview.
759 * TODO: Why is this necessary here, but not when doing a delete from the menu or toolbar? */
760 const gchar *modest_account_name =
761 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
762 modest_ui_actions_do_send_receive (modest_account_name, MODEST_WINDOW (win));
764 g_object_unref (account);
774 on_delete_message (GArray *arguments, gpointer data, osso_rpc_t *retval)
776 if (arguments->len != MODEST_DBUS_DELETE_MESSAGE_ARGS_COUNT)
779 /* Use g_idle to context-switch into the application's thread: */
781 /* Get the arguments: */
782 osso_rpc_t val = g_array_index (arguments,
784 MODEST_DBUS_DELETE_MESSAGE_ARG_URI);
785 gchar *uri = g_strdup (val.value.s);
787 /* printf(" debug: to=%s\n", idle_data->to); */
788 g_idle_add(on_idle_delete_message, (gpointer)uri);
790 /* Note that we cannot report failures during sending,
791 * because that would be asynchronous. */
796 on_idle_send_receive(gpointer user_data)
800 gdk_threads_enter ();
802 /* Pick the main window if it exists */
803 win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
805 /* Send & receive all if "Update automatically" is set */
806 /* TODO: check the auto-update parameter in the configuration */
807 modest_ui_actions_do_send_receive_all (win);
809 gdk_threads_leave ();
811 return FALSE; /* Do not call this callback again. */
814 static gint on_send_receive(GArray * arguments, gpointer data, osso_rpc_t * retval)
816 /* Use g_idle to context-switch into the application's thread: */
818 /* This method has no arguments. */
820 /* printf(" debug: to=%s\n", idle_data->to); */
821 g_idle_add(on_idle_send_receive, NULL);
823 /* Note that we cannot report failures during send/receive,
824 * because that would be asynchronous. */
828 static gboolean on_idle_top_application (gpointer user_data);
831 on_idle_open_default_inbox(gpointer user_data)
833 gdk_threads_enter ();
836 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
838 /* Get the folder view */
839 GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
840 MODEST_WIDGET_TYPE_FOLDER_VIEW);
841 modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (folder_view));
843 gdk_threads_leave ();
845 /* This D-Bus method is obviously meant to result in the UI being visible,
846 * so show it, by calling this idle handler directly: */
847 on_idle_top_application(user_data);
849 return FALSE; /* Do not call this callback again. */
852 static gint on_open_default_inbox(GArray * arguments, gpointer data, osso_rpc_t * retval)
854 /* Use g_idle to context-switch into the application's thread: */
856 /* This method has no arguments. */
858 g_idle_add(on_idle_open_default_inbox, NULL);
860 /* Note that we cannot report failures during send/receive,
861 * because that would be asynchronous. */
866 static gboolean on_idle_top_application (gpointer user_data)
868 gdk_threads_enter ();
871 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
873 /* Ideally, we would just use gtk_widget_show(),
874 * but this widget is not coded correctly to support that: */
875 gtk_widget_show_all (GTK_WIDGET (win));
876 gtk_window_present (GTK_WINDOW (win));
879 gdk_threads_leave ();
881 return FALSE; /* Do not call this callback again. */
884 static gint on_top_application(GArray * arguments, gpointer data, osso_rpc_t * retval)
886 /* Use g_idle to context-switch into the application's thread: */
888 /* This method has no arguments. */
890 g_idle_add(on_idle_top_application, NULL);
895 /* Callback for normal D-BUS messages */
896 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
897 GArray * arguments, gpointer data,
901 /* g_debug ("debug: %s\n", __FUNCTION__); */
902 g_debug ("debug: %s: method received: %s\n", __FUNCTION__, method);
904 if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
905 return on_mail_to (arguments, data, retval);
906 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
907 return on_open_message (arguments, data, retval);
908 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
909 return on_send_receive (arguments, data, retval);
910 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
911 return on_compose_mail (arguments, data, retval);
912 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_DELETE_MESSAGE) == 0) {
913 return on_delete_message (arguments,data, retval);
914 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX) == 0) {
915 return on_open_default_inbox (arguments, data, retval);
916 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_TOP_APPLICATION) == 0) {
917 return on_top_application (arguments, data, retval);
920 /* We need to return INVALID here so
921 * libosso will return DBUS_HANDLER_RESULT_NOT_YET_HANDLED,
922 * so that our modest_dbus_req_filter will then be tried instead.
928 /* A complex D-Bus type (like a struct),
929 * used to return various information about a search hit.
931 #define SEARCH_HIT_DBUS_TYPE \
932 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
933 DBUS_TYPE_STRING_AS_STRING /* msgid */ \
934 DBUS_TYPE_STRING_AS_STRING /* subject */ \
935 DBUS_TYPE_STRING_AS_STRING /* folder */ \
936 DBUS_TYPE_STRING_AS_STRING /* sender */ \
937 DBUS_TYPE_UINT64_AS_STRING /* msize */ \
938 DBUS_TYPE_BOOLEAN_AS_STRING /* has_attachment */ \
939 DBUS_TYPE_BOOLEAN_AS_STRING /* is_unread */ \
940 DBUS_TYPE_INT64_AS_STRING /* timestamp */ \
941 DBUS_STRUCT_END_CHAR_AS_STRING
944 search_result_to_message (DBusMessage *reply,
947 DBusMessageIter iter;
948 DBusMessageIter array_iter;
951 dbus_message_iter_init_append (reply, &iter);
952 dbus_message_iter_open_container (&iter,
954 SEARCH_HIT_DBUS_TYPE,
957 for (hit_iter = hits; hit_iter; hit_iter = hit_iter->next) {
958 DBusMessageIter struct_iter;
959 ModestSearchHit *hit;
965 gboolean has_attachment;
969 hit = (ModestSearchHit *) hit_iter->data;
971 msg_url = hit->msgid;
972 subject = hit->subject;
973 folder = hit->folder;
974 sender = hit->sender;
976 has_attachment = hit->has_attachment;
977 is_unread = hit->is_unread;
980 g_debug ("DEBUG: %s: Adding hit: %s", __FUNCTION__, msg_url);
982 dbus_message_iter_open_container (&array_iter,
987 dbus_message_iter_append_basic (&struct_iter,
991 dbus_message_iter_append_basic (&struct_iter,
995 dbus_message_iter_append_basic (&struct_iter,
999 dbus_message_iter_append_basic (&struct_iter,
1003 dbus_message_iter_append_basic (&struct_iter,
1007 dbus_message_iter_append_basic (&struct_iter,
1011 dbus_message_iter_append_basic (&struct_iter,
1015 dbus_message_iter_append_basic (&struct_iter,
1019 dbus_message_iter_close_container (&array_iter,
1022 g_free (hit->msgid);
1023 g_free (hit->subject);
1024 g_free (hit->folder);
1025 g_free (hit->sender);
1027 g_slice_free (ModestSearchHit, hit);
1030 dbus_message_iter_close_container (&iter, &array_iter);
1037 on_dbus_method_search (DBusConnection *con, DBusMessage *message)
1039 ModestDBusSearchFlags dbus_flags;
1040 DBusMessage *reply = NULL;
1044 dbus_int32_t flags_v;
1045 dbus_uint32_t size_v;
1053 dbus_error_init (&error);
1058 res = dbus_message_get_args (message,
1060 DBUS_TYPE_STRING, &query,
1061 DBUS_TYPE_STRING, &folder, /* e.g. "INBOX/drafts": TODO: Use both an ID and a display name. */
1062 DBUS_TYPE_INT64, &sd_v,
1063 DBUS_TYPE_INT64, &ed_v,
1064 DBUS_TYPE_INT32, &flags_v,
1065 DBUS_TYPE_UINT32, &size_v,
1068 dbus_flags = (ModestDBusSearchFlags) flags_v;
1069 start_date = (time_t) sd_v;
1070 end_date = (time_t) ed_v;
1072 ModestSearch search;
1073 memset (&search, 0, sizeof (search));
1075 /* Remember what folder we are searching in:
1077 * Note that we don't copy the strings,
1078 * because this struct will only be used for the lifetime of this function.
1080 search.folder = folder;
1082 /* Remember the text to search for: */
1083 #ifdef MODEST_HAVE_OGS
1084 search.query = query;
1087 /* Other criteria: */
1088 search.before = start_date;
1089 search.after = end_date;
1092 /* Text to serach for in various parts of the message: */
1093 if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
1094 search.flags |= MODEST_SEARCH_SUBJECT;
1095 search.subject = query;
1098 if (dbus_flags & MODEST_DBUS_SEARCH_SENDER) {
1099 search.flags |= MODEST_SEARCH_SENDER;
1100 search.from = query;
1103 if (dbus_flags & MODEST_DBUS_SEARCH_RECIPIENT) {
1104 search.flags |= MODEST_SEARCH_RECIPIENT;
1105 search.recipient = query;
1108 if (dbus_flags & MODEST_DBUS_SEARCH_BODY) {
1109 search.flags |= MODEST_SEARCH_BODY;
1110 search.body = query;
1114 search.flags |= MODEST_SEARCH_BEFORE;
1115 search.before = start_date;
1119 search.flags |= MODEST_SEARCH_AFTER;
1120 search.after = end_date;
1124 search.flags |= MODEST_SEARCH_SIZE;
1125 search.minsize = size_v;
1128 #ifdef MODEST_HAVE_OGS
1129 search.flags |= MODEST_SEARCH_USE_OGS;
1130 g_debug ("%s: Starting search for %s", __FUNCTION__, search.query);
1133 /* Note that this currently gets folders and messages from the servers,
1134 * which can take a long time. libmodest_dbus_client_search() can timeout,
1135 * reporting no results, if this takes a long time: */
1136 hits = modest_search_all_accounts (&search);
1138 reply = dbus_message_new_method_return (message);
1140 search_result_to_message (reply, hits);
1142 if (reply == NULL) {
1143 g_warning ("%s: Could not create reply.", __FUNCTION__);
1147 dbus_uint32_t serial = 0;
1148 dbus_connection_send (con, reply, &serial);
1149 dbus_connection_flush (con);
1150 dbus_message_unref (reply);
1157 /* A complex D-Bus type (like a struct),
1158 * used to return various information about a folder.
1160 #define GET_FOLDERS_RESULT_DBUS_TYPE \
1161 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1162 DBUS_TYPE_STRING_AS_STRING /* Folder Name */ \
1163 DBUS_TYPE_STRING_AS_STRING /* Folder URI */ \
1164 DBUS_STRUCT_END_CHAR_AS_STRING
1166 static DBusMessage *
1167 get_folders_result_to_message (DBusMessage *reply,
1170 DBusMessageIter iter;
1171 dbus_message_iter_init_append (reply, &iter);
1173 DBusMessageIter array_iter;
1174 dbus_message_iter_open_container (&iter,
1176 GET_FOLDERS_RESULT_DBUS_TYPE,
1179 GList *list_iter = folder_ids;
1180 for (list_iter = folder_ids; list_iter; list_iter = list_iter->next) {
1182 const gchar *folder_name = (const gchar*)list_iter->data;
1184 /* g_debug ("DEBUG: %s: Adding folder: %s", __FUNCTION__, folder_name); */
1186 DBusMessageIter struct_iter;
1187 dbus_message_iter_open_container (&array_iter,
1193 dbus_message_iter_append_basic (&struct_iter,
1195 &folder_name); /* The string will be copied. */
1197 /* URI: This is maybe not needed by osso-global-search: */
1198 const gchar *folder_uri = "TODO:unimplemented";
1199 dbus_message_iter_append_basic (&struct_iter,
1201 &folder_uri); /* The string will be copied. */
1203 dbus_message_iter_close_container (&array_iter,
1208 dbus_message_iter_close_container (&iter, &array_iter);
1214 add_single_folder_to_list (TnyFolder *folder, GList** list)
1219 if (TNY_IS_MERGE_FOLDER (folder)) {
1220 /* Ignore these because their IDs ares
1221 * a) not always unique or sensible.
1222 * b) not human-readable, and currently need a human-readable
1223 * ID here, because the osso-email-interface API does not allow
1224 * us to return both an ID and a display name.
1226 * This is actually the merged outbox folder.
1227 * We could hack our D-Bus API to understand "outbox" as the merged outboxes,
1228 * but that seems unwise. murrayc.
1233 /* Add this folder to the list: */
1235 const gchar * folder_name = tny_folder_get_name (folder);
1237 *list = g_list_append(*list, g_strdup (folder_name));
1240 /* osso-global-search only uses one string,
1241 * so ID is the only thing that could possibly identify a folder.
1242 * TODO: osso-global search should probably be changed to
1243 * take an ID and a Name.
1245 const gchar * id = tny_folder_get_id (folder);
1246 if (id && strlen(id))
1247 *list = g_list_append(*list, g_strdup (id));
1250 g_warning ("DEBUG: %s: folder has no name or ID.\n", __FUNCTION__);
1258 add_folders_to_list (TnyFolderStore *folder_store, GList** list)
1263 /* Add this folder to the list: */
1264 if (TNY_IS_FOLDER (folder_store)) {
1265 add_single_folder_to_list (TNY_FOLDER (folder_store), list);
1268 /* Recurse into child folders: */
1270 /* Get the folders list: */
1272 TnyFolderStoreQuery *query = tny_folder_store_query_new ();
1273 tny_folder_store_query_add_item (query, NULL,
1274 TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
1276 TnyList *all_folders = tny_simple_list_new ();
1277 tny_folder_store_get_folders (folder_store,
1282 TnyIterator *iter = tny_list_create_iterator (all_folders);
1283 while (!tny_iterator_is_done (iter)) {
1285 /* Do not recurse, because the osso-global-search UI specification
1286 * does not seem to want the sub-folders, though that spec seems to
1287 * be generally unsuitable for Modest.
1289 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
1291 add_single_folder_to_list (TNY_FOLDER (folder), list);
1294 if (TNY_IS_FOLDER_STORE (folder))
1295 add_folders_to_list (TNY_FOLDER_STORE (folder), list);
1297 add_single_folder_to_list (TNY_FOLDER (folder), list);
1301 /* tny_iterator_get_current() gave us a reference. */
1302 g_object_unref (folder);
1305 tny_iterator_next (iter);
1307 g_object_unref (G_OBJECT (iter));
1311 on_dbus_method_get_folders (DBusConnection *con, DBusMessage *message)
1313 DBusMessage *reply = NULL;
1316 /* Get the TnyStoreAccount so we can get the folders: */
1317 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
1318 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
1319 if (!account_name) {
1320 g_printerr ("modest: no account found\n");
1323 TnyAccount *account = NULL;
1325 account = modest_tny_account_store_get_server_account (
1326 modest_runtime_get_account_store(), account_name,
1327 TNY_ACCOUNT_TYPE_STORE);
1331 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
1334 printf("DEBUG: %s: Getting folders for account name=%s\n", __FUNCTION__, account_name);
1335 g_free (account_name);
1336 account_name = NULL;
1338 GList *folder_names = NULL;
1339 add_folders_to_list (TNY_FOLDER_STORE (account), &folder_names);
1341 g_object_unref (account);
1344 /* Also add the folders from the local folders account,
1345 * because they are (currently) used with all accounts:
1346 * TODO: This is not working. It seems to get only the Merged Folder (with an ID of "" (not NULL)).
1348 TnyAccount *account_local =
1349 modest_tny_account_store_get_local_folders_account (
1350 TNY_ACCOUNT_STORE (modest_runtime_get_account_store()));
1351 add_folders_to_list (TNY_FOLDER_STORE (account_local), &folder_names);
1353 g_object_unref (account_local);
1354 account_local = NULL;
1357 /* Put the result in a DBus reply: */
1358 reply = dbus_message_new_method_return (message);
1360 get_folders_result_to_message (reply, folder_names);
1362 if (reply == NULL) {
1363 g_warning ("%s: Could not create reply.", __FUNCTION__);
1367 dbus_uint32_t serial = 0;
1368 dbus_connection_send (con, reply, &serial);
1369 dbus_connection_flush (con);
1370 dbus_message_unref (reply);
1373 g_list_foreach (folder_names, (GFunc)g_free, NULL);
1374 g_list_free (folder_names);
1378 /** This D-Bus handler is used when the main osso-rpc
1379 * D-Bus handler has not handled something.
1380 * We use this for D-Bus methods that need to use more complex types
1381 * than osso-rpc supports.
1384 modest_dbus_req_filter (DBusConnection *con,
1385 DBusMessage *message,
1388 printf ("DEBUG: %s\n", __FUNCTION__);
1389 gboolean handled = FALSE;
1391 if (dbus_message_is_method_call (message,
1393 MODEST_DBUS_METHOD_SEARCH)) {
1394 printf (" DEBUG1: %s\n", __FUNCTION__);
1395 on_dbus_method_search (con, message);
1397 } else if (dbus_message_is_method_call (message,
1399 MODEST_DBUS_METHOD_GET_FOLDERS)) {
1400 printf (" DEBUG2: %s\n", __FUNCTION__);
1401 on_dbus_method_get_folders (con, message);
1405 g_debug (" debug: %s: Unexpected D-Bus method\n", __FUNCTION__);
1409 DBUS_HANDLER_RESULT_HANDLED :
1410 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1415 modest_osso_cb_hw_state_handler(osso_hw_state_t *state, gpointer data)
1418 /* printf("%s()\n", __PRETTY_FUNCTION__); */
1420 if(state->system_inactivity_ind)
1423 else if(state->save_unsaved_data_ind)
1431 /* printf("debug: %s(): return\n", __PRETTY_FUNCTION__); */