Bug #8913: Improve auto-draft deletion behavior
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 17 Mar 2010 20:11:18 +0000 (14:11 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 17 Mar 2010 20:11:18 +0000 (14:11 -0600)
imp/compose-mimp.php
imp/compose.php
imp/lib/Ajax/Application.php
imp/lib/Compose.php

index 90d8dcf..b81e60f 100644 (file)
@@ -242,7 +242,7 @@ case _("Send"):
         try {
             $notification->push($imp_compose->saveDraft($header, $message, Horde_Nls::getCharset(), false), 'horde.success');
             if ($prefs->getValue('close_draft')) {
-                $imp_compose->destroy();
+                $imp_compose->destroy('save_draft');
                 require IMP_BASE . '/mailbox-mimp.php';
                 exit;
             }
@@ -265,7 +265,7 @@ case _("Send"):
 
         try {
             if ($imp_compose->buildAndSendMessage($message, $header, Horde_Nls::getEmailCharset(), false, $options)) {
-                $imp_compose->destroy();
+                $imp_compose->destroy('send');
 
                 $notification->push(_("Message sent successfully."), 'horde.success');
                 require IMP_BASE . '/mailbox-mimp.php';
@@ -279,7 +279,7 @@ case _("Send"):
     break;
 
 case _("Cancel"):
-    $imp_compose->destroy(false);
+    $imp_compose->destroy('cancel');
     require IMP_BASE . '/mailbox-mimp.php';
     exit;
 }
index 22bcfe3..5352406 100644 (file)
@@ -321,7 +321,7 @@ case 'redirect_send':
 
     try {
         $imp_ui->redirectMessage($f_to, $imp_compose, $imp_contents);
-        $imp_compose->destroy();
+        $imp_compose->destroy('send');
         if ($isPopup) {
             if ($prefs->getValue('compose_confirm')) {
                 $notification->push(_("Message redirected successfully."), 'horde.success');
@@ -381,7 +381,7 @@ case 'send_message':
                 if ($vars->actionID == 'save_draft') {
                     if ($isPopup) {
                         if ($prefs->getValue('close_draft')) {
-                            $imp_compose->destroy();
+                            $imp_compose->destroy('save_draft');
                             echo Horde::wrapInlineScript(array('window.close();'));
                             exit;
                         } else {
@@ -390,7 +390,7 @@ case 'send_message':
                     } else {
                         $notification->push($result, 'horde.success');
                         if ($prefs->getValue('close_draft')) {
-                            $imp_compose->destroy();
+                            $imp_compose->destroy('save_draft');
                             header('Location: ' . $imp_ui->mailboxReturnUrl(false));
                             exit;
                         }
@@ -432,7 +432,7 @@ case 'send_message':
 
     try {
         $sent = $imp_compose->buildAndSendMessage($message, $header, $charset, $rtemode, $options);
-        $imp_compose->destroy();
+        $imp_compose->destroy('send');
     } catch (IMP_Compose_Exception $e) {
         $get_sig = false;
         $code = $e->getCode();
@@ -483,7 +483,7 @@ case 'fwd_digest':
     break;
 
 case 'cancel_compose':
-    $imp_compose->destroy(false);
+    $imp_compose->destroy('cancel');
     if ($isPopup) {
         echo Horde::wrapInlineScript(array('window.close();'));
     } else {
index 18b5f27..428dabd 100644 (file)
@@ -1047,7 +1047,7 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
     public function cancelCompose()
     {
         $imp_compose = IMP_Compose::singleton($this->_vars->imp_compose);
-        $imp_compose->destroy(false);
+        $imp_compose->destroy('cancel');
 
         return true;
     }
@@ -1064,14 +1064,7 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
      */
     public function deleteDraft()
     {
-        $imp_compose = IMP_Compose::singleton($this->_vars->imp_compose);
-        $imp_compose->destroy(false);
-
-        if ($draft_uid = $imp_compose->getMetadata('draft_uid')) {
-            $idx_array = array($draft_uid . IMP::IDX_SEP . IMP::folderPref($GLOBALS['prefs']->getValue('drafts_folder'), true));
-            $GLOBALS['injector']->getInstance('IMP_Message')->delete($idx_array, array('nuke' => true));
-        }
-
+        IMP_Compose::singleton($this->_vars->imp_compose)->destroy('cancel');
         return true;
     }
 
@@ -1400,7 +1393,7 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
             }
         }
 
-        $imp_compose->destroy();
+        $imp_compose->destroy('send');
 
         $result->mailbox = $this->_getMailboxResponse($imptree);
 
@@ -1492,7 +1485,7 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
             } else {
                 $GLOBALS['notification']->push($res);
                 if ($GLOBALS['prefs']->getValue('close_draft')) {
-                    $imp_compose->destroy();
+                    $imp_compose->destroy('save_draft');
                 }
             }
         } catch (IMP_Compose_Exception $e) {
index e342526..6e7aaaf 100644 (file)
@@ -146,19 +146,33 @@ class IMP_Compose
     /**
      * Destroys an IMP_Compose instance.
      *
-     * @param boolean $success  Was the message sent successfully?
+     * @param string $action  The action performed to cause the end of this
+     *                        instance.  Either 'cancel', 'save_draft', or
+     *                        'send'.
      */
-    public function destroy($success = true)
+    public function destroy($action)
     {
-        /* Delete draft if we were auto saving and this wasn't a resume
-         * draft request, -or- if this was a resume draft request,
-         * auto_delete_drafts was true, and we successfully sent the
-         * message. */
-        if ((($GLOBALS['prefs']->getValue('auto_save_drafts') &&
-              !$this->getMetadata('resume')) ||
-             ($success && $GLOBALS['prefs']->getValue('auto_delete_drafts'))) &&
-            ($uid = $this->getMetadata('draft_uid'))) {
-            $GLOBALS['injector']->getInstance('IMP_Message')->delete(array($uid . IMP::IDX_SEP . IMP::folderPref($GLOBALS['prefs']->getValue('drafts_folder'), true)), array('nuke' => true));
+        $uids = array();
+
+        switch ($action) {
+        case 'save_draft':
+            /* Don't delete any drafts. */
+            break;
+
+        case 'send':
+            /* Delete the auto-draft and the original resumed draft. */
+            $uids[] = $this->getMetadata('draft_uid_resume');
+            // Fall-through
+
+        case 'cancel':
+            /* Delete the auto-draft, but save the original resume draft. */
+            $uids[] = $this->getMetadata('draft_uid');
+            $uids = array_filter($uids);
+            break;
+        }
+
+        if (!empty($uids)) {
+            $GLOBALS['injector']->getInstance('IMP_Message')->delete($uids, array('nuke' => true));
         }
 
         $this->deleteAllAttachments();
@@ -320,26 +334,21 @@ class IMP_Compose
         /* Get the message ID. */
         $headers = Horde_Mime_Headers::parseHeaders($data);
 
+        $drafts_mbox = IMP::folderPref($GLOBALS['prefs']->getValue('drafts_folder'), true);
+        $old_uid = $this->getMetadata('draft_uid');
+
         /* Add the message to the mailbox. */
         try {
-            $old_uid = $this->getMetadata('autodraft')
-                ? $this->getMetadata('draft_uid')
-                : null;
-
             $ids = $GLOBALS['imp_imap']->ob()->append($drafts_mbox, array(array('data' => $data, 'flags' => $append_flags, 'messageid' => $headers->getValue('message-id'))));
 
             if ($old_uid) {
-                $idx_array = array($old_uid . IMP::IDX_SEP . IMP::folderPref($GLOBALS['prefs']->getValue('drafts_folder'), true));
-                $GLOBALS['injector']->getInstance('IMP_Message')->delete($idx_array, array('nuke' => true));
+                $GLOBALS['injector']->getInstance('IMP_Message')->delete(array($old_uid), array('nuke' => true));
             }
 
-            $this->_metadata['draft_uid'] = reset($ids);
-            $this->_metadata['autodraft'] = true;
+            $this->_metadata['draft_uid'] = reset($ids) . IMP::IDX_SEP . $drafts_mbox;
             $this->_modified = true;
             return sprintf(_("The draft has been saved to the \"%s\" folder."), IMP::displayFolder($drafts_mbox));
         } catch (Horde_Imap_Client_Exception $e) {
-            unset($this->_metadata['draft_uid']);
-            $this->_modified = true;
             return _("The draft was not successfully saved.");
         }
     }
@@ -435,8 +444,7 @@ class IMP_Compose
             } catch (Horde_Exception $e) {}
         }
 
-        list($this->_metadata['draft_uid'],) = explode(IMP::IDX_SEP, $uid);
-        $this->_metadata['resume'] = 1;
+        $this->_metadata['draft_uid_resume'] = $uid;
         $this->_modified = true;
 
         return array(