From 83576c2f1fe0d1a60a9c41748625389d0b0f6cad Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Wed, 17 Mar 2010 14:11:18 -0600 Subject: [PATCH] Bug #8913: Improve auto-draft deletion behavior --- imp/compose-mimp.php | 6 ++--- imp/compose.php | 10 ++++---- imp/lib/Ajax/Application.php | 15 ++++-------- imp/lib/Compose.php | 54 +++++++++++++++++++++++++------------------- 4 files changed, 43 insertions(+), 42 deletions(-) diff --git a/imp/compose-mimp.php b/imp/compose-mimp.php index 90d8dcf3d..b81e60f54 100644 --- a/imp/compose-mimp.php +++ b/imp/compose-mimp.php @@ -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; } diff --git a/imp/compose.php b/imp/compose.php index 22bcfe3b5..535240600 100644 --- a/imp/compose.php +++ b/imp/compose.php @@ -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 { diff --git a/imp/lib/Ajax/Application.php b/imp/lib/Ajax/Application.php index 18b5f2751..428dabdd9 100644 --- a/imp/lib/Ajax/Application.php +++ b/imp/lib/Ajax/Application.php @@ -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) { diff --git a/imp/lib/Compose.php b/imp/lib/Compose.php index e342526aa..6e7aaaf9c 100644 --- a/imp/lib/Compose.php +++ b/imp/lib/Compose.php @@ -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( -- 2.11.0