+ if (new_msg) {
+ TnyHeader *header;
+
+ /* Set priority flags in message */
+ header = tny_msg_get_header (new_msg);
+ tny_header_set_flag (header, info->priority_flags);
+
+ /* Set attachment flags in message */
+ if (info->attachments_list != NULL)
+ tny_header_set_flag (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);
+ 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);
+ if (new_msg) g_object_unref(new_msg);
+ 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)
+{
+ ModestMailOperationPrivate *priv;
+ CreateMsgInfo *info = NULL;
+
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
+
+ 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)
+{
+ ModestMailOperationPrivate *priv = NULL;
+ SendNewMailInfo *info = (SendNewMailInfo *) userdata;
+ TnyFolder *draft_folder = NULL;
+ TnyFolder *outbox_folder = NULL;
+ TnyHeader *header = NULL;
+ GError *err = NULL;
+
+ if (!msg) {
+ goto end;
+ }
+
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
+
+ if (priv->error) {
+ priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
+ modest_mail_operation_notify_end (self);
+ goto end;
+ }
+
+ /* Call mail operation */
+ modest_mail_operation_send_mail (self, info->transport_account, msg);
+
+ if (info->draft_msg != NULL) {
+ TnyFolder *folder = NULL;
+ TnyFolder *src_folder = NULL;
+ TnyFolderType folder_type;
+ TnyTransportAccount *transport_account = NULL;
+
+ /* To remove the old mail from its source folder, we need to get the
+ * transport account of the original draft message (the transport account
+ * might have been changed by the user) */
+ header = tny_msg_get_header (info->draft_msg);
+ transport_account = modest_tny_account_store_get_transport_account_from_outbox_header(
+ modest_runtime_get_account_store(), header);
+ if (transport_account == NULL)
+ transport_account = g_object_ref(info->transport_account);
+ draft_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT (transport_account),
+ TNY_FOLDER_TYPE_DRAFTS);
+ outbox_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT (transport_account),
+ TNY_FOLDER_TYPE_OUTBOX);
+ g_object_unref(transport_account);
+
+ if (!draft_folder) {
+ g_warning ("%s: modest_tny_account_get_special_folder(..) returned a NULL drafts folder",
+ __FUNCTION__);
+ goto end;
+ }
+ if (!outbox_folder) {
+ g_warning ("%s: modest_tny_account_get_special_folder(..) returned a NULL outbox folder",
+ __FUNCTION__);
+ goto end;
+ }
+
+ 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. */
+ 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 (folder);
+ }
+
+end:
+ if (header)
+ g_object_unref (header);
+ 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);