Skip to content

Commit

Permalink
Move mailbox specific actions from IMP_Message to IMP_Mailbox
Browse files Browse the repository at this point in the history
  • Loading branch information
slusarz committed Nov 13, 2014
1 parent dc63f95 commit 8e36501
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 163 deletions.
10 changes: 5 additions & 5 deletions imp/lib/Ajax/Application/Handler/Dynamic.php
Expand Up @@ -231,7 +231,7 @@ public function emptyMailbox()

$mbox = IMP_Mailbox::formFrom($this->vars->mbox);

$GLOBALS['injector']->getInstance('IMP_Message')->emptyMailbox(array($mbox));
$mbox->emptyMailbox();

$this->_base->queue->poll($mbox);

Expand Down Expand Up @@ -263,7 +263,7 @@ public function flagAll()

$mbox = IMP_Mailbox::formFrom($this->vars->mbox);

if (!$GLOBALS['injector']->getInstance('IMP_Message')->flagAllInMailbox($flags, array($mbox), $this->vars->add)) {
if (!$mbox->flagAll($flags, $this->vars->add)) {
return false;
}

Expand Down Expand Up @@ -835,8 +835,6 @@ public function deleteAttach()
*/
public function purgeDeleted()
{
global $injector;

$change = $this->_base->changed(true);
if (is_null($change)) {
return false;
Expand All @@ -846,7 +844,9 @@ public function purgeDeleted()
$change = ($this->_base->indices->mailbox->getSort()->sortby == Horde_Imap_Client::SORT_THREAD);
}

$expunged = $injector->getInstance('IMP_Message')->expungeMailbox(array(strval($this->_base->indices->mailbox) => 1), array('list' => true));
$expunged = $this->_base->indices->mailbox->expunge(null, array(
'list' => true
));

if (!($expunge_count = count($expunged))) {
return false;
Expand Down
4 changes: 1 addition & 3 deletions imp/lib/Indices.php
Expand Up @@ -510,9 +510,7 @@ public function delete(array $opts = array())
));

if ($expunge_now) {
$this->expungeMailbox(
$imp_indices->indices()
);
$ob->mbox->expunge($ids_ob);
} else {
$ajax_queue->flag(
$del_flags,
Expand Down
158 changes: 158 additions & 0 deletions imp/lib/Mailbox.php
Expand Up @@ -1275,6 +1275,164 @@ public function createMailboxName($new)
return self::get($new);
}

/**
* Adds or removes flag(s) for all messages in the mailbox.
* This function works with IMAP only, not POP3.
*
* @param array $flags The IMAP flag(s) to add or remove.
* @param boolean $action If true, add the flag(s); otherwise, remove the
* flag(s).
*
* @return boolean True if successful, false if not.
*/
public function flagAll($flags, $action = true)
{
$action_array = $action
? array('add' => $flags)
: array('remove' => $flags);

try {
/* Grab list of UIDs before flagging, to make sure we determine
* the exact subset that has been flagged. */
$mailbox_list = $this->list_ob->getIndicesOb();
$this->imp_imap->store($this, $action_array);
$GLOBALS['injector']->getInstance('IMP_Ajax_Queue')
->flag(reset($action_array), $action, $mailbox_list);
} catch (IMP_Imap_Exception $e) {
return false;
}

return true;
}

/**
* Expunges all deleted messages.
*
* @param array $to_expunge An optional array of indices to delete.
* If empty, all messages flagged as deleted in
* the mailbox will be deleted.
* @param array $opts Additional options:
* <pre>
* - list: (boolean) Return a list of messages expunged.
* DEFAULT: false
* </pre>
*
* @return IMP_Indices If 'list' option is true, an indices object
* containing the messages that have been expunged.
*/
public function expunge($to_expunge, array $opts = array())
{
$msg_list = !empty($opts['list']);
$process_list = $update_list = array();

if ($this->access_expunge) {
$ids = $this->imp_imap->getIdsOb(
$to_expunge ?: Horde_Imap_Client_Ids::ALL
);

if ($this->search) {
foreach ($this->getSearchOb()->mboxes as $skey) {
$process_list[] = array($skey, $ids);
}
} else {
$process_list[] = array($key, $ids);
}
}

// [0] = IMP_Mailbox object, [1] = Horde_Imap_Client_Ids object
foreach ($process_list as $val) {
/* If expunging a particular UID list, need to check
* UIDVALIDITY. */
if (!$val[1]->all) {
try {
$val[0]->uidvalid;
} catch (IMP_Exception $e) {
continue;
}
}

try {
$update_list[strval($val[0])] = $val[0]->imp_imap->expunge($val[0], array(
'ids' => $val[1],
'list' => $msg_list
));
} catch (IMP_Imap_Exception $e) {}
}

if ($msg_list) {
return new IMP_Indices($update_list);
}
}

/**
* Empties the entire mailbox.
*/
public function emptyMailbox()
{
global $notification, $prefs;

if (!$this->access_empty) {
$notification->push(
sprintf(
_("Could not delete messages from %s. This mailbox is read-only."),
$this->display
),
'horde.error'
);
return;
}

if ($this->vtrash) {
foreach (IMP_Mailbox::get($this->getSearchOb()->mboxes) as $val) {
$val->expunge();
}
$notification->push(
_("Emptied all messages from Virtual Trash Folder."),
'horde.success'
);
return;
}

/* Make sure there is at least 1 message before attempting to
* delete. */
try {
$imp_imap = $this->imp_imap;
$status = $imp_imap->status($this, Horde_Imap_Client::STATUS_MESSAGES);
if (empty($status['messages'])) {
$notification->push(
sprintf(
_("The mailbox %s is already empty."),
$this->display
),
'horde.message'
);
return;
}

$trash = $prefs->getValue('use_trash')
? IMP_Mailbox::getPref(IMP_Mailbox::MBOX_TRASH)
: null;

if (!$trash || ($trash == $this)) {
$imp_imap->store($this, array(
'add' => array(Horde_Imap_Client::FLAG_DELETED)
));
$this->expunge();
} else {
$ret = $imp_imap->search($this);
$this->getIndicesOb($ret['match'])->delete();
}

$notification->push(
sprintf(
_("Emptied all messages from %s."),
$this->display
),
'horde.success'
);
} catch (IMP_Imap_Exception $e) {}
}

/* Static methods. */

/**
Expand Down
155 changes: 0 additions & 155 deletions imp/lib/Message.php
Expand Up @@ -273,159 +273,4 @@ public function flag(array $action, IMP_Indices $indices,
return $ret;
}

/**
* Adds or removes flag(s) for all messages in a list of mailboxes.
* This function works with IMAP only, not POP3.
*
* @param array $flags The IMAP flag(s) to add or remove.
* @param array $mboxes The list of mailboxes to flag.
* @param boolean $action If true, add the flag(s), otherwise, remove the
* flag(s).
*
* @return boolean True if successful, false if not.
*/
public function flagAllInMailbox($flags, $mboxes, $action = true)
{
if (empty($mboxes) || !is_array($mboxes)) {
return false;
}

$action_array = $action
? array('add' => $flags)
: array('remove' => $flags);
$ajax_queue = $GLOBALS['injector']->getInstance('IMP_Ajax_Queue');

$ajax_queue->poll($mboxes);

foreach (IMP_Mailbox::get($mboxes) as $val) {
try {
/* Grab list of UIDs before flagging, to make sure we
* determine the exact subset that has been flagged. */
$mailbox_list = $val->list_ob->getIndicesOb();
$val->imp_imap->store($val, $action_array);
$ajax_queue->flag(reset($action_array), $action, $mailbox_list);
} catch (IMP_Imap_Exception $e) {
return false;
}
}

return true;
}

/**
* Expunges all deleted messages from the list of mailboxes.
*
* @param array $mbox_list The list of mailboxes to empty as keys; an
* optional array of indices to delete as values.
* If the value is not an array, all messages
* flagged as deleted in the mailbox will be
* deleted.
* @param array $opts Additional options:
* - list: (boolean) Return a list of messages expunged.
* DEFAULT: false
*
* @return IMP_Indices If 'list' option is true, an indices object
* containing the messages that have been expunged.
*/
public function expungeMailbox($mbox_list, array $opts = array())
{
$msg_list = !empty($opts['list']);

if (empty($mbox_list)) {
return $msg_list ? new IMP_Indices() : null;
}

$process_list = $update_list = array();

foreach ($mbox_list as $key => $val) {
$key = IMP_Mailbox::get($key);

if ($key->access_expunge) {
$ids = $key->imp_imap->getIdsOb(is_array($val) ? $val : Horde_Imap_Client_Ids::ALL);

if ($key->search) {
foreach ($key->getSearchOb()->mboxes as $skey) {
$process_list[] = array($skey, $ids);
}
} else {
$process_list[] = array($key, $ids);
}
}
}

// [0] = IMP_Mailbox object, [1] = Horde_Imap_Client_Ids object
foreach ($process_list as $val) {
/* If expunging a particular UID list, need to check
* UIDVALIDITY. */
if (!$val[1]->all) {
try {
$val[0]->uidvalid;
} catch (IMP_Exception $e) {
continue;
}
}

try {
$update_list[strval($val[0])] = $val[0]->imp_imap->expunge($val[0], array(
'ids' => $val[1],
'list' => $msg_list
));
} catch (IMP_Imap_Exception $e) {}
}

if ($msg_list) {
return new IMP_Indices($update_list);
}
}

/**
* Empties an entire mailbox.
*
* @param array $mbox_list The list of mailboxes to empty.
*/
public function emptyMailbox($mbox_list)
{
global $notification, $prefs;

$trash = ($prefs->getValue('use_trash'))
? IMP_Mailbox::getPref(IMP_Mailbox::MBOX_TRASH)
: null;

foreach (IMP_Mailbox::get($mbox_list) as $mbox) {
if (!$mbox->access_empty) {
$notification->push(sprintf(_("Could not delete messages from %s. This mailbox is read-only."), $mbox->display), 'horde.error');
continue;
}

if ($mbox->vtrash) {
$this->expungeMailbox(array_flip($mbox->getSearchOb()->mboxes));
$notification->push(_("Emptied all messages from Virtual Trash Folder."), 'horde.success');
continue;
}

/* Make sure there is at least 1 message before attempting to
* delete. */
try {
$imp_imap = $mbox->imp_imap;
$status = $imp_imap->status($mbox, Horde_Imap_Client::STATUS_MESSAGES);
if (empty($status['messages'])) {
$notification->push(sprintf(_("The mailbox %s is already empty."), $mbox->display), 'horde.message');
continue;
}

if (!$trash || ($trash == $mbox)) {
$imp_imap->store($mbox, array(
'add' => array(Horde_Imap_Client::FLAG_DELETED)
));
$this->expungeMailbox(array(strval($mbox) => 1));
} else {
$ret = $imp_imap->search($mbox);
$mbox->getIndicesOb($ret['match'])->delete();
}

$notification->push(sprintf(_("Emptied all messages from %s."), $mbox->display), 'horde.success');
} catch (IMP_Imap_Exception $e) {}
}
}

}

0 comments on commit 8e36501

Please sign in to comment.