+static gboolean
+idle_create_msg_cb (gpointer idle_data)
+{
+ CreateMsgIdleInfo *info = (CreateMsgIdleInfo *) idle_data;
+
+ gdk_threads_enter ();
+ info->callback (info->mail_op, info->msg, info->userdata);
+ gdk_threads_leave ();
+ g_object_unref (info->mail_op);
+ if (info->msg)
+ g_object_unref (info->msg);
+ g_slice_free (CreateMsgIdleInfo, info);
+
+ return FALSE;
+}
+
+static gpointer
+create_msg_thread (gpointer thread_data)
+{
+ CreateMsgInfo *info = (CreateMsgInfo *) thread_data;
+ TnyMsg *new_msg = NULL;
+ ModestMailOperationPrivate *priv;
+
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE(info->mail_op);
+ if (info->html_body == NULL) {
+ new_msg = modest_tny_msg_new (info->to, info->from, info->cc,
+ info->bcc, info->subject, info->plain_body,
+ info->attachments_list); /* FIXME: attachments */
+ } else {
+ new_msg = modest_tny_msg_new_html_plain (info->to, info->from, info->cc,
+ info->bcc, info->subject, info->html_body,
+ info->plain_body, info->attachments_list);
+ }
+
+ if (new_msg) {
+ TnyHeader *header;
+ /* Set priority flags in message */
+ header = tny_msg_get_header (new_msg);
+ if (info->priority_flags != 0)
+ tny_header_set_flags (header, info->priority_flags);
+ if (info->attachments_list != NULL) {
+ tny_header_set_flags (header, TNY_HEADER_FLAG_ATTACHMENTS);
+ }
+ g_object_unref (G_OBJECT(header));
+ } else {
+ priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
+ g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
+ MODEST_MAIL_OPERATION_ERROR_INSTANCE_CREATION_FAILED,
+ "modest: failed to create a new msg\n");
+ }
+
+
+ g_free (info->to);
+ g_free (info->from);
+ g_free (info->cc);
+ g_free (info->bcc);
+ g_free (info->plain_body);
+ g_free (info->html_body);
+ g_free (info->subject);
+ g_list_foreach (info->attachments_list, (GFunc) g_object_unref, NULL);
+ g_list_free (info->attachments_list);
+
+ if (info->callback) {
+ CreateMsgIdleInfo *idle_info;
+ idle_info = g_slice_new0 (CreateMsgIdleInfo);
+ idle_info->mail_op = info->mail_op;
+ g_object_ref (info->mail_op);
+ idle_info->msg = new_msg;
+ if (new_msg)
+ g_object_ref (new_msg);
+ idle_info->callback = info->callback;
+ idle_info->userdata = info->userdata;
+ g_idle_add (idle_create_msg_cb, idle_info);
+ } else {
+ g_idle_add (idle_notify_queue, g_object_ref (info->mail_op));
+ }
+
+ g_object_unref (info->mail_op);
+ g_slice_free (CreateMsgInfo, info);
+ return NULL;
+}
+
+void
+modest_mail_operation_create_msg (ModestMailOperation *self,
+ const gchar *from, const gchar *to,
+ const gchar *cc, const gchar *bcc,
+ const gchar *subject, const gchar *plain_body,
+ const gchar *html_body,
+ const GList *attachments_list,
+ TnyHeaderFlags priority_flags,
+ ModestMailOperationCreateMsgCallback callback,
+ gpointer userdata)
+{
+ CreateMsgInfo *info = NULL;
+
+ info = g_slice_new0 (CreateMsgInfo);
+ info->mail_op = self;
+ g_object_ref (self);
+
+ info->from = g_strdup (from);
+ info->to = g_strdup (to);
+ info->cc = g_strdup (cc);
+ info->subject = g_strdup (subject);
+ info->plain_body = g_strdup (plain_body);
+ info->html_body = g_strdup (html_body);
+ info->attachments_list = g_list_copy ((GList *) attachments_list);
+ g_list_foreach (info->attachments_list, (GFunc) g_object_ref, NULL);
+ info->priority_flags = priority_flags;
+
+ info->callback = callback;
+ info->userdata = userdata;
+
+ g_thread_create (create_msg_thread, info, FALSE, NULL);
+}
+
+typedef struct
+{
+ TnyTransportAccount *transport_account;
+ TnyMsg *draft_msg;
+} SendNewMailInfo;
+
+static void
+modest_mail_operation_send_new_mail_cb (ModestMailOperation *self,
+ TnyMsg *msg,
+ gpointer userdata)
+{
+ SendNewMailInfo *info = (SendNewMailInfo *) userdata;
+ TnyFolder *folder;
+ TnyHeader *header;
+
+ if (!msg) {
+ goto end;
+ }
+
+ /* Call mail operation */
+ modest_mail_operation_send_mail (self, info->transport_account, msg);
+
+ folder = modest_tny_account_get_special_folder (TNY_ACCOUNT (info->transport_account), TNY_FOLDER_TYPE_DRAFTS);
+ if (folder) {
+ if (info->draft_msg != NULL) {
+ header = tny_msg_get_header (info->draft_msg);
+ /* Note: This can fail (with a warning) if the message is not really already in a folder,
+ * because this function requires it to have a UID. */
+ tny_folder_remove_msg (folder, header, NULL);
+ tny_header_set_flags (header, TNY_HEADER_FLAG_DELETED);
+ tny_header_set_flags (header, TNY_HEADER_FLAG_SEEN);
+ g_object_unref (header);
+ }
+ }
+
+end:
+ if (info->draft_msg)
+ g_object_unref (info->draft_msg);
+ if (info->transport_account)
+ g_object_unref (info->transport_account);
+ g_slice_free (SendNewMailInfo, info);
+ modest_mail_operation_notify_end (self);
+}
+