+static gboolean
+idle_create_msg_cb (gpointer idle_data)
+{
+ CreateMsgIdleInfo *info = (CreateMsgIdleInfo *) idle_data;
+
+ /* This is a GDK lock because we are an idle callback and
+ * info->callback can contain Gtk+ code */
+
+ gdk_threads_enter (); /* CHECKED */
+ info->callback (info->mail_op, info->msg, info->userdata);
+
+ g_object_unref (info->mail_op);
+ if (info->msg)
+ g_object_unref (info->msg);
+ g_slice_free (CreateMsgIdleInfo, info);
+ gdk_threads_leave (); /* CHECKED */
+
+ 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);
+ } 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,
+ info->images_list);
+ }
+
+ if (new_msg) {
+ TnyHeader *header;
+ TnyHeaderFlags flags = 0;
+
+ /* Set priority flags in message */
+ header = tny_msg_get_header (new_msg);
+ if (info->priority_flags != 0)
+ flags |= info->priority_flags;
+
+ /* Set attachment flags in message */
+ if (info->attachments_list != NULL)
+ flags |= TNY_HEADER_FLAG_ATTACHMENTS;
+
+ tny_header_set_flag (header, flags);
+ 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);
+ g_list_foreach (info->images_list, (GFunc) g_object_unref, NULL);
+ g_list_free (info->images_list);
+
+ if (info->callback) {
+ CreateMsgIdleInfo *idle_info;
+ idle_info = g_slice_new0 (CreateMsgIdleInfo);
+ idle_info->mail_op = g_object_ref (info->mail_op);
+ idle_info->msg = (new_msg) ? g_object_ref (new_msg) : NULL;
+ 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,
+ const GList *images_list,
+ TnyHeaderFlags priority_flags,
+ ModestMailOperationCreateMsgCallback callback,
+ gpointer userdata)
+{
+ CreateMsgInfo *info = NULL;
+
+ info = g_slice_new0 (CreateMsgInfo);
+ info->mail_op = g_object_ref (self);
+
+ info->from = g_strdup (from);
+ info->to = g_strdup (to);
+ info->cc = g_strdup (cc);
+ info->bcc = g_strdup (bcc);
+ 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->images_list = g_list_copy ((GList *) images_list);
+ g_list_foreach (info->images_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 *draft_folder = NULL;
+ TnyFolder *outbox_folder = NULL;
+ TnyHeader *header;
+ GError *err = NULL;
+
+ if (!msg) {
+ goto end;
+ }
+
+ /* Call mail operation */
+ modest_mail_operation_send_mail (self, info->transport_account, msg);
+
+ /* Remove old mail from its source folder */
+ draft_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT (info->transport_account),
+ TNY_FOLDER_TYPE_DRAFTS);
+ outbox_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT (info->transport_account),
+ TNY_FOLDER_TYPE_OUTBOX);
+ if (info->draft_msg != NULL) {
+ TnyFolder *folder = NULL;
+ TnyFolder *src_folder = NULL;
+ TnyFolderType folder_type;
+ folder = tny_msg_get_folder (info->draft_msg);
+ if (folder == NULL) goto end;
+ folder_type = modest_tny_folder_guess_folder_type (folder);
+
+ if (folder_type == TNY_FOLDER_TYPE_INVALID)
+ g_warning ("%s: BUG: folder of type TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
+
+ if (folder_type == TNY_FOLDER_TYPE_OUTBOX)
+ src_folder = outbox_folder;
+ else
+ src_folder = draft_folder;
+
+ /* 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. */
+ header = tny_msg_get_header (info->draft_msg);
+ tny_folder_remove_msg (src_folder, header, NULL);
+
+ tny_folder_sync (folder, TRUE, &err); /* FALSE --> don't expunge */
+/* tny_folder_sync_async (src_folder, TRUE, NULL, NULL, NULL); /\* expunge *\/ */
+
+ g_object_unref (header);
+ g_object_unref (folder);
+ }
+
+end:
+ if (err != NULL)
+ g_error_free(err);
+ if (info->draft_msg)
+ g_object_unref (info->draft_msg);
+ if (draft_folder)
+ g_object_unref (draft_folder);
+ if (outbox_folder)
+ g_object_unref (outbox_folder);
+ if (info->transport_account)
+ g_object_unref (info->transport_account);
+ g_slice_free (SendNewMailInfo, info);
+ modest_mail_operation_notify_end (self);
+}
+