/**
  * get imap backend and folder (and select folder)
  *
  * @param string                    $_folderId
  * @param Expressomail_Backend_Folder &$_folder
  * @param boolean                   $_select
  * @param Expressomail_Backend_ImapProxy   $_imapBackend
  * @throws Expressomail_Exception_IMAPServiceUnavailable
  * @throws Expressomail_Exception_IMAPFolderNotFound
  * @return Expressomail_Backend_ImapProxy
  */
 protected function _getBackendAndSelectFolder($_folderId = NULL, &$_folder = NULL, $_select = TRUE, Expressomail_Backend_ImapProxy $_imapBackend = NULL)
 {
     if ($_folder === NULL || empty($_folder)) {
         $folderBackend = new Expressomail_Backend_Folder();
         $_folder = $folderBackend->get($_folderId);
     }
     try {
         $imapBackend = $_imapBackend === NULL ? Expressomail_Backend_ImapFactory::factory($_folder->account_id) : $_imapBackend;
         if ($_select) {
             if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
                 Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Select folder ' . $_folder->globalname);
             }
             $backendFolderValues = $imapBackend->selectFolder(Expressomail_Model_Folder::encodeFolderName($_folder->globalname));
         }
     } catch (Zend_Mail_Storage_Exception $zmse) {
         // @todo remove the folder from cache if it could not be found on the IMAP server?
         throw new Expressomail_Exception_IMAPFolderNotFound($zmse->getMessage());
     } catch (Zend_Mail_Protocol_Exception $zmpe) {
         throw new Expressomail_Exception_IMAPServiceUnavailable($zmpe->getMessage());
     }
     return $imapBackend;
 }
 /**
  * move messages on imap server
  * 
  * @param array $_uids
  * @param string $_targetFolderName
  * @param Expressomail_Backend_ImapProxy $_imap
  * 
  * @todo perhaps we should check the existance of the messages on the imap instead of catching the exceptions here
  */
 protected function _moveBatchOfMessages($_uids, $_targetFolderName, Expressomail_Backend_ImapProxy $_imap)
 {
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Move ' . count($_uids) . ' messages to folder ' . $_targetFolderName . ' on imap server');
     }
     try {
         $_imap->copyMessage($_uids, Expressomail_Model_Folder::encodeFolderName($_targetFolderName));
         $_imap->addFlags($_uids, array(Zend_Mail_Storage::FLAG_DELETED));
     } catch (Zend_Mail_Storage_Exception $zmse) {
         if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) {
             Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' ' . $zmse);
         }
     } catch (Expressomail_Exception_IMAP $fei) {
         if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) {
             Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' ' . $fei);
         }
     }
 }
 /**
  * delete all messages in one folder -> be careful, they are completly removed and not moved to trash
  * -> delete subfolders if param set
  *
  * @param string $_folderId
  * @param boolean $_deleteSubfolders
  * @return Expressomail_Model_Folder
  * @throws Expressomail_Exception_IMAPServiceUnavailable
  */
 public function emptyFolder($_folderId, $_deleteSubfolders = FALSE)
 {
     $folder = $this->_backend->get($_folderId);
     $account = Expressomail_Controller_Account::getInstance()->get($folder->account_id);
     if ($folder) {
         $cache = Tinebase_Core::getCache();
         $cacheKey = 'Expressomail_Model_Folder_' . $folder->id;
         $cache->remove($cacheKey);
     }
     $imap = Expressomail_Backend_ImapFactory::factory($account);
     try {
         // try to delete messages in imap folder
         Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Delete all messages in folder ' . $folder->globalname);
         $imap->emptyFolder(Expressomail_Model_Folder::encodeFolderName($folder->globalname));
     } catch (Zend_Mail_Protocol_Exception $zmpe) {
         Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' ' . $zmpe->getMessage());
         throw new Expressomail_Exception_IMAPServiceUnavailable($zmpe->getMessage());
     } catch (Zend_Mail_Storage_Exception $zmse) {
         Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Folder could be empty (' . $zmse->getMessage() . ')');
     }
     if ($_deleteSubfolders) {
         $this->deleteSubfolders($folder);
     }
     return $folder;
 }
 /**
  * Creates new entry
  *
  * @param   Tinebase_Record_Interface $_record
  * @return  Tinebase_Record_Interface
  * @throws  Tinebase_Exception_InvalidArgument
  * @throws  Tinebase_Exception_UnexpectedValue
  *
  * @todo    remove autoincremental ids later
  */
 public function create(Tinebase_Record_Interface $_record)
 {
     $folder = $_record->toArray();
     $return = $this->get($this->encodeFolderUid(Expressomail_Model_Folder::encodeFolderName($folder['globalname']), $folder['account_id']), FALSE);
     return $return;
 }
 /**
  * Get Ids for filter
  * @param array $imapFilters
  * @param Tinebase_Model_Pagination $_pagination
  * @return array
  *
  * @todo pass the search parameters
  */
 protected function _getIds(array $_imapFilters, Tinebase_Model_Pagination $_pagination = NULL)
 {
     $messages = array();
     if (empty($_imapFilters['paths'])) {
         $_imapFilters['paths'] = $this->_getAllFolders();
     }
     $sort = $this->_getImapSortParams($_pagination);
     // do a search for each path on $imapFilters
     foreach ($_imapFilters['paths'] as $folderId => $path) {
         list($accountId, $mailbox) = $path;
         $imap = Expressomail_Backend_ImapFactory::factory($accountId);
         $imap->selectFolder(Expressomail_Model_Folder::encodeFolderName($mailbox));
         // TODO: pass the search parameter too.
         $messages[$folderId] = $imap->sort((array) $sort, (array) $_imapFilters['filters']);
     }
     return $messages;
 }
 /**
  * Tears down the fixture
  * This method is called after a test is executed.
  *
  * @access protected
  */
 protected function tearDown()
 {
     if (count($this->_createdFolders) > 0) {
         foreach ($this->_createdFolders as $folderName) {
             //echo "delete $folderName\n";
             try {
                 $this->_imap->removeFolder(Expressomail_Model_Folder::encodeFolderName($folderName));
             } catch (Zend_Mail_Storage_Exception $zmse) {
                 // already deleted
             }
         }
         //No necessary with Expressomail
         //           Expressomail_Controller_Cache_Folder::getInstance()->clear($this->_account);
     }
     if (!empty($this->_foldersToClear)) {
         foreach ($this->_foldersToClear as $folderName) {
             // delete test messages from given folders on imap server (search by special header)
             $this->_imap->selectFolder($folderName);
             $result = $this->_imap->search(array('HEADER X-Tine20TestMessage jsontest'));
             //print_r($result);
             foreach ($result as $messageUid) {
                 $this->_imap->removeMessage($messageUid);
             }
             // clear message cache
             $folder = Expressomail_Controller_Folder::getInstance()->getByBackendAndGlobalName($this->_account->getId(), $folderName);
             //No necessary with Expressomail
             //              Expressomail_Controller_Cache_Message::getInstance()->clear($folder);
         }
     }
     // sieve cleanup
     if ($this->_testSieveScriptName !== NULL) {
         Expressomail_Controller_Sieve::getInstance()->setScriptName($this->_testSieveScriptName);
         try {
             Expressomail_Controller_Sieve::getInstance()->deleteScript($this->_account->getId());
         } catch (Zend_Mail_Protocol_Exception $zmpe) {
             // do not delete script if active
         }
         Expressomail_Controller_Account::getInstance()->setVacationActive($this->_account, $this->_oldSieveVacationActiveState);
         if ($this->_oldSieveData !== NULL) {
             $this->_oldSieveData->save();
         }
     }
     if ($this->_oldActiveSieveScriptName !== NULL) {
         Expressomail_Controller_Sieve::getInstance()->setScriptName($this->_oldActiveSieveScriptName);
         Expressomail_Controller_Sieve::getInstance()->activateScript($this->_account->getId());
     }
     // vfs cleanup
     foreach ($this->_pathsToDelete as $path) {
         $webdavRoot = new Sabre_DAV_ObjectTree(new Tinebase_WebDav_Root());
         //echo "delete $path";
         $webdavRoot->delete($path);
     }
 }