+ TnyAccount *account = NULL;
+ if (account_mgr) {
+ account = modest_tny_account_store_get_transport_account_for_open_connection (
+ modest_runtime_get_account_store(), account_name);
+ }
+
+ if (!account) {
+ g_printerr ("modest: failed to get tny account folder'\n", account_name);
+ } else {
+ gchar * from = modest_account_mgr_get_from_string (account_mgr,
+ account_name);
+ if (!from) {
+ g_printerr ("modest: no from address for account '%s'\n", account_name);
+ } else {
+
+ /* Create the message: */
+ TnyMsg *msg = modest_tny_msg_new (idle_data->to, from,
+ idle_data->cc, idle_data->bcc, idle_data->subject, idle_data->body,
+ idle_data->attachments);
+
+ if (!msg) {
+ g_printerr ("modest: failed to create message\n");
+ } else
+ {
+ /* Add the message to a folder and show its UI for editing: */
+ TnyFolder *folder = modest_tny_account_get_special_folder (account,
+ TNY_FOLDER_TYPE_DRAFTS);
+ if (!folder) {
+ g_printerr ("modest: failed to find Drafts folder\n");
+ } else {
+
+ tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
+
+ ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
+ gtk_widget_show_all (GTK_WIDGET (win));
+
+ g_object_unref (G_OBJECT(folder));
+ }
+
+ g_object_unref (G_OBJECT(msg));
+ }
+
+ g_object_unref (G_OBJECT(account));
+ }
+ }
+
+ /* Free the idle data: */
+ g_free (idle_data->to);
+ g_free (idle_data->cc);
+ g_free (idle_data->bcc);
+ g_free (idle_data->subject);
+ g_free (idle_data->body);
+ g_free (idle_data->attachments);
+ g_free (idle_data);
+
+ g_free (account_name);
+ return FALSE; /* Do not call this callback again. */
+}
+
+static gint on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
+{
+ gchar **list = NULL;
+ gint i = 0;
+
+ if (arguments->len != MODEST_DEBUS_COMPOSE_MAIL_ARGS_COUNT)
+ return OSSO_ERROR;
+
+ /* Use g_idle to context-switch into the application's thread: */
+ ComposeMailIdleData *idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
+
+ /* Get the arguments: */
+ osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_TO);
+ idle_data->to = g_strdup (val.value.s);
+
+ val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_CC);
+ idle_data->cc = g_strdup (val.value.s);
+
+ val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_BCC);
+ idle_data->bcc = g_strdup (val.value.s);
+
+ val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_SUBJECT);
+ idle_data->subject = g_strdup (val.value.s);
+
+ val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_BODY);
+ idle_data->body = g_strdup (val.value.s);
+
+ val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
+ gchar *attachments_str = g_strdup (val.value.s);
+
+ list = g_strsplit(attachments_str, ",", 0);
+ for (i=0; list[i] != NULL; i++) {
+ idle_data->attachments = g_slist_append(idle_data->attachments, g_strdup(list[i]));
+ }
+ g_strfreev(list);
+
+
+ /* printf(" debug: to=%s\n", idle_data->to); */
+ g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
+
+ /* Note that we cannot report failures during sending,
+ * because that would be asynchronous. */
+ return OSSO_OK;
+}
+
+
+static TnyMsg *
+find_message_by_url (const char *uri, TnyAccount **ac_out)
+{
+
+ ModestTnyAccountStore *astore;
+ TnyAccount *account;
+ TnyFolder *folder;
+ TnyMsg *msg;
+
+ account = NULL;
+ msg = NULL;
+ folder = NULL;
+
+ astore = modest_runtime_get_account_store ();
+
+ if (astore == NULL) {
+ return NULL;
+ }
+
+ g_debug ("Got AccountStore, lets go");
+
+ account = tny_account_store_find_account (TNY_ACCOUNT_STORE (astore),
+ uri);
+
+ if (account == NULL) {
+ return NULL;
+ }
+
+ g_debug ("Found account");
+
+ if ( ! TNY_IS_STORE_ACCOUNT (account)) {
+ goto out;
+ }
+
+ g_debug ("Account is store account");
+
+ *ac_out = account;
+
+ folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account),
+ uri,
+ NULL);
+
+ if (folder == NULL) {
+ goto out;
+ }
+ g_debug ("Found folder");
+
+
+ msg = tny_folder_find_msg (folder, uri, NULL);
+
+out:
+ if (account && !msg) {
+ g_object_unref (account);
+ *ac_out = NULL;
+ }
+
+ if (folder) {
+ g_object_unref (folder);
+ }
+
+ return msg;
+}
+
+static gboolean
+on_idle_open_message (gpointer user_data)
+{
+ ModestWindow *msg_view;
+ TnyMsg *msg;
+ TnyAccount *account;
+ TnyHeader *header;
+ const char *msg_uid;
+ const char *account_name;
+ char *uri;
+
+ uri = (char *) user_data;
+
+ g_debug ("Trying to find msg by url: %s", uri);
+ msg = find_message_by_url (uri, &account);
+ g_free (uri);
+
+ if (msg == NULL) {
+ return FALSE;
+ }
+ g_debug ("Found message");
+
+ header = tny_msg_get_header (msg);
+ account_name = tny_account_get_name (account);
+ msg_uid = tny_header_get_uid (header);
+
+ msg_view = modest_msg_view_window_new (msg,
+ account_name,
+ msg_uid);
+ /* TODO: does that leak the msg_view ?! */
+
+ gtk_widget_show_all (GTK_WIDGET (msg_view));
+
+ g_object_unref (header);
+ g_object_unref (account);