2007-07-31 Philip Van Hoof <pvanhoof@gnome.org>
[modest] / src / modest-mail-operation.c
index faec82a..9d48dae 100644 (file)
@@ -417,8 +417,8 @@ modest_mail_operation_cancel (ModestMailOperation *self)
        /* Cancel the mail operation. We need to wrap it between this
           start/stop operations to allow following calls to the
           account */
-       if (priv->account)
-               tny_account_cancel (priv->account);
+       g_return_val_if_fail (priv->account, FALSE);
+       tny_account_cancel (priv->account);
 
        return canceled;
 }
@@ -672,6 +672,7 @@ modest_mail_operation_create_msg (ModestMailOperation *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);
@@ -697,7 +698,8 @@ modest_mail_operation_send_new_mail_cb (ModestMailOperation *self,
                                        gpointer userdata)
 {
        SendNewMailInfo *info = (SendNewMailInfo *) userdata;
-       TnyFolder *folder;
+       TnyFolder *draft_folder = NULL;
+       TnyFolder *outbox_folder = NULL;
        TnyHeader *header;
 
        if (!msg) {
@@ -707,23 +709,38 @@ modest_mail_operation_send_new_mail_cb (ModestMailOperation *self,
        /* 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);
-                       g_object_unref (folder);
-               }
+       /* 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_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_header_set_flags (header, TNY_HEADER_FLAG_DELETED);
+               tny_header_set_flags (header, TNY_HEADER_FLAG_SEEN);
+               g_object_unref (header);
+               g_object_unref (folder);
        }
 
 end:
        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);
@@ -783,6 +800,7 @@ modest_mail_operation_save_to_drafts_cb (ModestMailOperation *self,
                                         TnyMsg *msg,
                                         gpointer userdata)
 {
+       TnyFolder *src_folder = NULL;
        TnyFolder *folder = NULL;
        TnyHeader *header = NULL;
        ModestMailOperationPrivate *priv = NULL;
@@ -808,11 +826,12 @@ modest_mail_operation_save_to_drafts_cb (ModestMailOperation *self,
 
        if (info->draft_msg != NULL) {
                header = tny_msg_get_header (info->draft_msg);
+               src_folder = tny_header_get_folder (header); 
                /* Remove the old draft expunging it */
-               tny_folder_remove_msg (folder, header, NULL);
+               tny_folder_remove_msg (src_folder, header, NULL);
                tny_header_set_flags (header, TNY_HEADER_FLAG_DELETED);
                tny_header_set_flags (header, TNY_HEADER_FLAG_SEEN);
-               tny_folder_sync (folder, FALSE, &(priv->error));  /* FALSE --> don't expunge */
+               tny_folder_sync (src_folder, TRUE, &(priv->error));  /* expunge */
                g_object_unref (header);
        }
        
@@ -831,6 +850,8 @@ modest_mail_operation_save_to_drafts_cb (ModestMailOperation *self,
 end:
        if (folder)
                g_object_unref (G_OBJECT(folder));
+       if (src_folder)
+               g_object_unref (G_OBJECT(src_folder));
        if (info->edit_window)
                g_object_unref (G_OBJECT(info->edit_window));
        if (info->draft_msg)
@@ -1158,11 +1179,17 @@ update_account_thread (gpointer thr_user_data)
        priv->account = g_object_ref (info->account);
 
        /*
-        * for POP3, we do a logout-login upon send/receive -- many POP-servers (like Gmail) do not
-        * show any updates unless we do that
+        * Previousl, we did this for POP3, to do a logout-login upon send/receive, 
+        * because many POP-servers (like Gmail) do not
+        * show any updates unless we do that.
+        * But that didn't work with gmail anyway, 
+        * and tinymail now takes care of this itself by disconnecting 
+        * automatically after using the connection.
         */
+       /*
        if (!first_time && TNY_IS_CAMEL_POP_STORE_ACCOUNT (priv->account)) 
                tny_camel_pop_store_account_reconnect (TNY_CAMEL_POP_STORE_ACCOUNT(priv->account));
+       */
 
        /* Get all the folders. We can do it synchronously because
           we're already running in a different thread than the UI */
@@ -1725,8 +1752,8 @@ new_name_valid_if_local_account (ModestMailOperationPrivate *priv,
 {
        if (TNY_IS_ACCOUNT (into) && 
            modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (into)) &&
-           modest_tny_local_folders_account_extra_folder_exists (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (into),
-                                                                 new_name)) {
+           modest_tny_local_folders_account_folder_name_in_use (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (into),
+                                                                new_name)) {
                priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
                g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
                             MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
@@ -1763,7 +1790,9 @@ modest_mail_operation_xfer_folder (ModestMailOperation *self,
                parent_rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
        
        /* The moveable restriction is applied also to copy operation */
-       if ((!TNY_IS_FOLDER_STORE (parent)) || (rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE)) {
+       if ((gpointer) parent == (gpointer) folder ||
+           (!TNY_IS_FOLDER_STORE (parent)) || 
+           (rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE)) {
                printf("DEBUG: %s: Not allowing the move.\n", __FUNCTION__);
                /* Set status failed and set an error */
                priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
@@ -2607,7 +2636,6 @@ on_refresh_folder (TnyFolder   *folder,
        }
 
        /* Free */
-/*     g_object_unref (helper->mail_op); */
        g_slice_free   (RefreshAsyncHelper, helper);
 
        /* Notify about operation end */
@@ -2666,7 +2694,7 @@ modest_mail_operation_refresh_folder  (ModestMailOperation *self,
 
        /* Create the helper */
        helper = g_slice_new0 (RefreshAsyncHelper);
-       helper->mail_op = g_object_ref (self);
+       helper->mail_op = self;
        helper->user_callback = user_callback;
        helper->user_data = user_data;