Fix first page determination.
authorMichael M Slusarz <slusarz@curecanti.org>
Mon, 24 Nov 2008 05:36:42 +0000 (22:36 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Mon, 24 Nov 2008 05:37:27 +0000 (22:37 -0700)
Clean up some details regarding the API for unseenMessages() and
newMessages() also.

imp/lib/Mailbox.php
imp/mailbox.php
imp/rss.php

index fbc8793..0fc3fba 100644 (file)
@@ -277,42 +277,43 @@ class IMP_Mailbox
      * Get the list of new messages in the mailbox (IMAP RECENT flag, with
      * UNDELETED if we're hiding deleted messages).
      *
-     * @param boolean $count  Return a count of new messages, rather than
-     *                        the entire message list?
+     * @param integer $results  A Horde_Imap_Client::SORT_* constant that
+     *                          indicates the desired return type.
      *
-     * @return integer  The number of new messages in the mailbox.
+     * @return mixed  Whatever is requested in $results.
      */
-    public function newMessages($count = false)
+    public function newMessages($results)
     {
-        return $this->_msgFlagSearch('recent', $count);
+        return $this->_msgFlagSearch('recent', $results);
     }
 
     /**
      * Get the list of unseen messages in the mailbox (IMAP UNSEEN flag, with
      * UNDELETED if we're hiding deleted messages).
      *
-     * @param boolean $count  Return a count of unseen messages, rather than
-     *                        the entire message list?
+     * @param integer $results  A Horde_Imap_Client::SORT_RESULTS_* constant
+     *                          that indicates the desired return type.
      *
-     * @return array  The list of unseen messages.
+     * @return mixed  Whatever is requested in $results.
      */
-    public function unseenMessages($count = false)
+    public function unseenMessages($results)
     {
-        return $this->_msgFlagSearch('unseen', $count);
+        return $this->_msgFlagSearch('unseen', $results);
     }
 
     /**
      * Do a search on a mailbox in the most efficient way available.
      *
-     * @param string $type    The search type - either 'recent' or 'unseen'.
-     * @param boolean $count  Return a count of unseen messages, rather than
-     *                        the entire message list?
+     * @param string $type      The search type - either 'recent' or 'unseen'.
+     * @param integer $results  A Horde_Imap_Client::SORT_RESULTS_* constant
+     *                          that indicates the desired return type.
      *
-     * @return mixed  If $count is true, the number of messages.  If $count is
-     *                false, a list of message UIDs.
+     * @return mixed  Whatever is requested in $results.
      */
-    protected function _msgFlagSearch($type, $count)
+    protected function _msgFlagSearch($type, $results)
     {
+        $count = $results == Horde_Imap_Client::SORT_RESULTS_COUNT;
+
         if ($this->_searchmbox || empty($this->_sorted)) {
             return $count ? 0 : array();
         }
@@ -331,18 +332,14 @@ class IMP_Mailbox
         }
 
         if ($type == 'recent') {
-            $criteria->flag('\\recent');
+            $criteria->flag('\\recent', true);
         } else {
             $criteria->flag('\\seen', false);
         }
 
-        $results = $count
-            ? array(Horde_Imap_Client::SORT_RESULTS_COUNT)
-            : array(Horde_Imap_Client::SORT_RESULTS_MATCH);
-
         try {
-            $res = $GLOBALS['imp_imap']->ob->search($this->_mailbox, $criteria, array('results' => $results));
-            return $count ? $res['count'] : $res['match'];
+            $res = $GLOBALS['imp_imap']->ob->search($this->_mailbox, $criteria, array('results' => array($results)));
+            return $count ? $res['count'] : $res;
         } catch (Horde_Imap_Client_Exception $e) {
             return $count ? 0 : array();
         }
@@ -398,15 +395,12 @@ class IMP_Mailbox
     {
         $index = $this->_arrayIndex + $offset;
 
-        /* If the offset would put us out of array index, return now. */
-        if (!isset($this->_sorted[$index])) {
-            return array();
-        }
-
-        return array(
-            'index' => $this->_sorted[$index],
-            'mailbox' => ($this->_searchmbox) ? $this->_sortedInfo[$index]['m'] : $this->_mailbox
-        );
+        return isset($this->_sorted[$index])
+            ? array(
+                  'index' => $this->_sorted[$index],
+                  'mailbox' => ($this->_searchmbox) ? $this->_sortedInfo[$index]['m'] : $this->_mailbox
+              )
+            : array();
     }
 
     /**
@@ -435,7 +429,7 @@ class IMP_Mailbox
     {
         $this->_buildMailbox();
 
-        $ret = array('msgcount' => $this->getMessageCount());
+        $ret = array('msgcount' => count($this->_sorted));
 
         if (is_null($page_size) &&
             ($page_size != $GLOBALS['prefs']->getValue('max_msgs'))) {
@@ -454,9 +448,13 @@ class IMP_Mailbox
                     /* Search for the last visited page first. */
                     if (isset($_SESSION['imp']['cache']['mbox_page'][$this->_mailbox])) {
                         $page = $_SESSION['imp']['cache']['mbox_page'][$this->_mailbox];
+                    } elseif ($this->_searchmbox) {
+                        $page = 1;
                     } else {
+                        $page_uid = null;
                         $startpage = $GLOBALS['prefs']->getValue('mailbox_start');
-                        switch ($startpage) {
+
+                        switch ($GLOBALS['prefs']->getValue('mailbox_start')) {
                         case IMP::MAILBOX_START_FIRSTPAGE:
                             $page = 1;
                             break;
@@ -466,29 +464,35 @@ class IMP_Mailbox
                             break;
 
                         case IMP::MAILBOX_START_FIRSTUNSEEN:
-                            // TODO - Use status()
-
-                        case IMP::MAILBOX_START_LASTUNSEEN:
                             $sortpref = IMP::getSort($this->_mailbox);
-                            if (!$sortpref['limit'] &&
-                                !$this->_searchmbox &&
-                                ($query = $this->unseenMessages())) {
-                                $sortednew = array_keys(array_intersect($this->_sorted, $query));
-                                $first_new = ($startpage == IMP::MAILBOX_START_FIRSTUNSEEN) ?
-                                    array_shift($sortednew) :
-                                    array_pop($sortednew);
-                                $page = ceil(($first_new + 1) / $page_size);
+
+                            /* Optimization: if sorting by arrival then first
+                             * unseen information is returned via a
+                             * SELECT/EXAMINE call. */
+                            if ($sortpref['by'] == Horde_Imap_Client::SORT_ARRIVAL) {
+                                try {
+                                    $res = $GLOBALS['imp_imap']->ob->status($this->_mailbox, Horde_Imap_Client::STATUS_FIRSTUNSEEN);
+                                    $page_uid = $this->_sorted[$res['firstunseen'] - 1];
+                                } catch (Horde_Imap_Client_Exception $e) {}
+                            } else {
+                                $unseen_msgs = $this->unseenMessages(Horde_Imap_Client::SORT_RESULTS_MIN);
+                                $page_uid = $unseen_msgs['min'];
                             }
                             break;
+
+                        case IMP::MAILBOX_START_LASTUNSEEN:
+                            $unseen_msgs = $this->unseenMessages(Horde_Imap_Client::SORT_RESULTS_MAX);
+                            $page_uid = $unseen_msgs['max'];
+                            break;
                         }
                     }
                 }
 
+                print_r($page_uid);
                 if (empty($page)) {
-                    if (!isset($sortpref)) {
-                        $sortpref = IMP::getSort($this->_mailbox);
-                    }
-                    $page = $sortpref['dir'] ? 1 : $ret['pagecount'];
+                    $page = is_null($page_uid)
+                        ? 1
+                        : ceil((array_search($page_uid, $this->_sorted) + 1) / $page_size);
                 }
             }
 
index 5a267f6..2c255a3 100644 (file)
@@ -345,7 +345,7 @@ if ($search_mbox) {
         $vtrash = $imp_search->createSearchID($search_mbox);
     }
 } else {
-    $unread = $imp_mailbox->unseenMessages(true);
+    $unread = $imp_mailbox->unseenMessages(Horde_Imap_Client::SORT_RESULTS_COUNT);
 }
 if ($browser->isBrowser('konqueror')) {
     $notification->push('if (window.fluid) window.fluid.setDockBadge("' . ($unread ? $unread : '') . '");', 'javascript');
@@ -354,7 +354,7 @@ if ($browser->isBrowser('konqueror')) {
 /* Get the recent message count. */
 $newmsgs = 0;
 if ($prefs->getValue('nav_popup') || $prefs->getValue('nav_audio')) {
-    $newmsgs = $imp_mailbox->newMessages(true);
+    $newmsgs = $imp_mailbox->newMessages(Horde_Imap_Client::SORT_RESULTS_COUNT);
 }
 
 $pagetitle = $rawtitle = $title = IMP::getLabel($imp_mbox['mailbox']);
index a0f9a4e..171878b 100644 (file)
@@ -51,7 +51,7 @@ $imp_mailbox = &IMP_Mailbox::singleton($mailbox);
 $total_num = $imp_mailbox->getMessageCount();
 $unseen_num = ($imp_search->isVINBOXFolder($mailbox))
     ? $imp_mailbox->getMessageCount()
-    : $imp_mailbox->unseenMessages(true);
+    : $imp_mailbox->unseenMessages(Horde_Imap_Client::SORT_RESULTS_COUNT);
 
 $query = new Horde_Imap_Client_Search_Query();
 if ($new_mail) {