From 3412e50b54e3daac8745234e21ab6e72be0ed165 Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Wed, 04 Jun 2014 11:20:33 -0400
Subject: [PATCH] Fix attachment menu structure and aria-attributes

---
 program/lib/Roundcube/rcube_result_multifolder.php |  133 ++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 127 insertions(+), 6 deletions(-)

diff --git a/program/lib/Roundcube/rcube_result_multifolder.php b/program/lib/Roundcube/rcube_result_multifolder.php
index 8d7ae5d..4bbd218 100644
--- a/program/lib/Roundcube/rcube_result_multifolder.php
+++ b/program/lib/Roundcube/rcube_result_multifolder.php
@@ -28,16 +28,22 @@
 {
     public $multi = true;
     public $sets = array();
+    public $incomplete = false;
+    public $folder;
 
     protected $meta = array();
+    protected $index = array();
+    protected $folders = array();
+    protected $sorting;
     protected $order = 'ASC';
 
 
     /**
      * Object constructor.
      */
-    public function __construct()
+    public function __construct($folders = array())
     {
+        $this->folders = $folders;
         $this->meta = array('count' => 0);
     }
 
@@ -50,9 +56,41 @@
     public function add($result)
     {
         $this->sets[] = $result;
-        $this->meta['count'] += $result->count();
+
+        if ($result->count()) {
+            $this->append_result($result);
+        }
+        else if ($result->incomplete) {
+            $this->incomplete = true;
+        }
     }
 
+    /**
+     * Append message UIDs from the given result to our index
+     */
+    protected function append_result($result)
+    {
+        $this->meta['count'] += $result->count();
+
+        // append UIDs to global index
+        $folder = $result->get_parameters('MAILBOX');
+        $index = array_map(function($uid) use ($folder) { return $uid . '-' . $folder; }, $result->get());
+        $this->index = array_merge($this->index, $index);
+    }
+
+    /**
+     * Store a global index of (sorted) message UIDs
+     */
+    public function set_message_index($headers, $sort_field, $sort_order)
+    {
+        $this->index = array();
+        foreach ($headers as $header) {
+            $this->index[] = $header->uid . '-' . $header->folder;
+        }
+
+        $this->sorting = $sort_field;
+        $this->order = $sort_order;
+    }
 
     /**
      * Checks the result from IMAP command
@@ -105,6 +143,17 @@
     public function revert()
     {
         $this->order = $this->order == 'ASC' ? 'DESC' : 'ASC';
+        $this->index = array();
+
+        // revert order in all sub-sets
+        foreach ($this->sets as $set) {
+            if ($this->order != $set->get_parameters('ORDER')) {
+                $set->revert();
+            }
+            $folder = $set->get_parameters('MAILBOX');
+            $index = array_map(function($uid) use ($folder) { return $uid . '-' . $folder; }, $set->get());
+            $this->index = array_merge($this->index, $index);
+        }
     }
 
 
@@ -119,7 +168,10 @@
      */
     public function exists($msgid, $get_index = false)
     {
-        return false;
+        if (!empty($this->folder)) {
+            $msgid .= '-' . $this->folder;
+        }
+        return array_search($msgid, $this->index);
     }
 
 
@@ -141,6 +193,21 @@
     }
 
     /**
+     * Slices data set.
+     *
+     * @param $offset Offset (as for PHP's array_slice())
+     * @param $length Number of elements (as for PHP's array_slice())
+     *
+     */
+    public function slice($offset, $length)
+    {
+        $data = array_slice($this->get(), $offset, $length);
+
+        $this->index = $data;
+        $this->meta['count'] = count($data);
+    }
+
+    /**
      * Filters data set. Removes elements not listed in $ids list.
      *
      * @param array $ids List of IDs to keep.
@@ -157,7 +224,7 @@
      */
     public function get()
     {
-        return array();
+        return $this->index;
     }
 
 
@@ -179,9 +246,13 @@
      *
      * @return int Element value
      */
-    public function get_element($index)
+    public function get_element($idx)
     {
-        return null;
+        switch ($idx) {
+            case 'FIRST': return $this->index[0];
+            case 'LAST':  return end($this->index);
+            default:      return $this->index[$idx];
+        }
     }
 
 
@@ -195,9 +266,35 @@
      */
     public function get_parameters($param=null)
     {
+        $params = array(
+            'SORT' => $this->sorting,
+            'ORDER' => $this->order,
+            'MAILBOX' => $this->folders,
+        );
+
+        if ($param !== null) {
+            return $params[$param];
+        }
+
         return $params;
     }
 
+    /**
+     * Returns the stored result object for a particular folder
+     *
+     * @param string $folder  Folder name
+     * @return false|obejct rcube_result_* instance of false if none found
+     */
+    public function get_set($folder)
+    {
+        foreach ($this->sets as $set) {
+            if ($set->get_parameters('MAILBOX') == $folder) {
+                return $set;
+            }
+        }
+
+        return false;
+    }
 
     /**
      * Returns length of internal data representation
@@ -208,4 +305,28 @@
     {
         return $this->count();
     }
+
+
+    /* Serialize magic methods */
+
+    public function __sleep()
+    {
+        return array('sets','folders','sorting','order');
+    }
+
+    public function __wakeup()
+    {
+        // restore index from saved result sets
+        $this->meta = array('count' => 0);
+
+        foreach ($this->sets as $result) {
+            if ($result->count()) {
+                $this->append_result($result);
+            }
+            else if ($result->incomplete) {
+                $this->incomplete = true;
+            }
+        }
+    }
+
 }

--
Gitblit v1.9.1