Make IMP_Imap_Tree implement ArrayAccess
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 17 Aug 2010 18:43:55 +0000 (12:43 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 17 Aug 2010 18:52:55 +0000 (12:52 -0600)
Get rid of element() and get() functions - simply use array access to
grab an individual mailbox element.

imp/lib/Ajax/Application.php
imp/lib/Folder.php
imp/lib/Imap/Tree.php

index 95502a3..4a2e231 100644 (file)
@@ -334,9 +334,8 @@ class IMP_Ajax_Application extends Horde_Core_Ajax_Application
                 }
 
                 foreach ($val as $val2) {
-                    if (!isset($folder_list[$val2]) &&
-                        ($elt = $imptree->element($val2))) {
-                        $folder_list[$val2] = $elt;
+                    if (!isset($folder_list[$val2]) && isset($imptree[$val2])) {
+                        $folder_list[$val2] = $imptree[$val2];
                     }
                 }
             }
@@ -1886,21 +1885,11 @@ class IMP_Ajax_Application extends Horde_Core_Ajax_Application
      */
     protected function _getPollInformation($mbox)
     {
-        $imptree = $GLOBALS['injector']->getInstance('IMP_Imap_Tree');
-        $elt = $imptree->get($mbox);
-        if (!$imptree->isPolled($elt)) {
-            return array();
-        }
-
-        try {
-            $count = ($info = $GLOBALS['injector']->getInstance('IMP_Imap')->getOb()->status($mbox, Horde_Imap_Client::STATUS_UNSEEN))
-                ? intval($info['unseen'])
-                : 0;
-        } catch (Horde_Imap_Client_Exception $e) {
-            $count = 0;
-        }
+        $imaptree = $GLOBALS['injector']->getInstance('IMP_Imap_Tree');
 
-        return array($mbox => $count);
+        return $imaptree[$mbox]->polled
+            ? array($mbox => $imaptree[$mbox]->poll_info->unseen)
+            : array();
     }
 
     /**
@@ -1953,7 +1942,7 @@ class IMP_Ajax_Application extends Horde_Core_Ajax_Application
         if (!empty($changes['a'])) {
             $result['a'] = array();
             foreach ($changes['a'] as $val) {
-                $result['a'][] = $this->_createMailboxElt(is_object($val) ? $val : $imptree->element($val));
+                $result['a'][] = $this->_createMailboxElt(is_object($val) ? $val : $imptree[$val]);
             }
         }
 
@@ -1963,7 +1952,7 @@ class IMP_Ajax_Application extends Horde_Core_Ajax_Application
                 // Skip the base element, since any change there won't ever be
                 // updated on-screen.
                 if ($val != IMP_Imap_Tree::BASE_ELT) {
-                    $result['c'][] = $this->_createMailboxElt($imptree->element($val));
+                    $result['c'][] = $this->_createMailboxElt($imptree[$val]);
                 }
             }
         }
index 786b6e6..560ea4d 100644 (file)
@@ -282,9 +282,8 @@ class IMP_Folder
     public function exists($folder)
     {
         $imaptree = $GLOBALS['injector']->getInstance('IMP_Imap_Tree');
-        $elt = $imaptree->get($folder);
-        if ($elt) {
-            return !$imaptree->isContainer($elt);
+        if (isset($imaptree[$folder])) {
+            return !$imaptree[$folder]->container;
         }
 
         try {
index b3d983c..42c7eaa 100644 (file)
@@ -18,7 +18,7 @@
  * @license  http://www.fsf.org/copyleft/gpl.html GPL
  * @package  IMP
  */
-class IMP_Imap_Tree
+class IMP_Imap_Tree implements ArrayAccess
 {
     /* Constants for mailboxElt attributes. */
     const ELT_NOSELECT = 1;
@@ -565,11 +565,11 @@ class IMP_Imap_Tree
      */
     public function peek($name)
     {
-        if (!($elt = $this->get($name))) {
+        if (!($elt = $this[$name])) {
             return false;
         }
 
-        foreach (array_slice($this->_parent[$elt['p']], array_search($elt['v'], $this->_parent[$elt['p']]) + 1) as $val) {
+        foreach (array_slice($this->_parent[$elt->parent], array_search($elt->value, $this->_parent[$elt->parent]) + 1) as $val) {
             if ($this->_activeElt($this->_tree[$val])) {
                 return true;
             }
@@ -579,22 +579,6 @@ class IMP_Imap_Tree
     }
 
     /**
-     * Returns the requested element.
-     *
-     * @param string $name  The name of the tree element.
-     *
-     * @return array  Returns the requested element or false if not found.
-     */
-    public function get($name)
-    {
-        $name = $this->_convertName($name);
-
-        return isset($this->_tree[$name])
-            ? $this->_tree[$name]
-            : false;
-    }
-
-    /**
      * Insert a folder/mailbox into the tree.
      *
      * @param mixed $id  The name of the folder (or a list of folder names)
@@ -1643,7 +1627,7 @@ class IMP_Imap_Tree
                      !$this->isContainer($mailbox)) &&
                     (($mask & self::FLIST_VFOLDER) ||
                      !$this->isVFolder($mailbox))) {
-                    $ret_array[$mailbox['v']] = $this->element($mailbox);
+                    $ret_array[$mailbox['v']] = $this[$mailbox['v']];
                 }
             } while (($mailbox = $this->next($nextmask)));
         }
@@ -1679,25 +1663,6 @@ class IMP_Imap_Tree
     }
 
     /**
-     * Return extended information on an element.
-     *
-     * @param mixed $name  The name of the tree element or a tree element.
-     *
-     * @return IMP_Imap_Tree_Element  Returns the mailbox element or false if
-     *                                not found.
-     */
-    public function element($mailbox)
-    {
-        if (!is_array($mailbox)) {
-            $mailbox = $this->get($mailbox);
-        }
-
-        return $mailbox
-            ? new IMP_Imap_Tree_Element($mailbox, $this)
-            : false;
-    }
-
-    /**
      * Sort a level in the tree.
      *
      * @param string $id  The parent folder whose children need to be sorted.
@@ -1744,4 +1709,26 @@ class IMP_Imap_Tree
         return $mbox . $new;
     }
 
+    /* ArrayAccess methods. */
+
+    public function offsetExists($offset)
+    {
+        return isset($this->_tree[$this->_convertName($offset)]);
+    }
+
+    public function offsetGet($offset)
+    {
+        return new IMP_Imap_Tree_Element($this->_tree[$this->_convertName($offset)], $this);
+    }
+
+    public function offsetSet($offset, $value)
+    {
+        $this->insert($offset);
+    }
+
+    public function offsetUnset($offset)
+    {
+        $this->delete($offset);
+    }
+
 }