From a5fc70f55b34276108ac04fe5a4acdebcd98a65c Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <machniak@hosted05.klab.cc>
Date: Tue, 15 May 2012 02:59:42 -0400
Subject: [PATCH] Add public list_folder_direct() and list_folders_subscribed_direct()

---
 program/include/rcube_imap.php |  256 +++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 198 insertions(+), 58 deletions(-)

diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index fa3f60c..571ffb9 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -212,6 +212,8 @@
 
     /**
      * Check connection state, connect if not connected.
+     *
+     * @return bool Connection state.
      */
     public function check_connection()
     {
@@ -564,8 +566,8 @@
     {
         $mode = strtoupper($mode);
 
-        // count search set
-        if ($this->search_string && $folder == $this->folder && ($mode == 'ALL' || $mode == 'THREADS') && !$force) {
+        // count search set, assume search set is always up-to-date (don't check $force flag)
+        if ($this->search_string && $folder == $this->folder && ($mode == 'ALL' || $mode == 'THREADS')) {
             if ($mode == 'ALL') {
                 return $this->search_set->count_messages();
             }
@@ -1211,7 +1213,9 @@
         }
         // use message index sort as default sorting
         else if (!$sort_field) {
+            // use search result from count() if possible
             if ($this->options['skip_deleted'] && !empty($this->icache['undeleted_idx'])
+                && $this->icache['undeleted_idx']->get_parameters('ALL') !== null
                 && $this->icache['undeleted_idx']->get_parameters('MAILBOX') == $folder
             ) {
                 $index = $this->icache['undeleted_idx'];
@@ -1344,21 +1348,21 @@
      *
      * @return rcube_result_index  Search result (UIDs)
      */
-    public function search_once($mailbox = null, $str = 'ALL')
+    public function search_once($folder = null, $str = 'ALL')
     {
         if (!$str) {
             return 'ALL';
         }
 
-        if (!strlen($mailbox)) {
-            $mailbox = $this->mailbox;
+        if (!strlen($folder)) {
+            $folder = $this->folder;
         }
 
         if (!$this->check_connection()) {
             return new rcube_result_index();
         }
 
-        $index = $this->conn->search($mailbox, $str, true);
+        $index = $this->conn->search($folder, $str, true);
 
         return $index;
     }
@@ -2464,7 +2468,16 @@
             return $a_mboxes;
         }
 
-        $a_mboxes = $this->_list_folders_subscribed($root, $name, $filter, $rights);
+        // Give plugins a chance to provide a list of folders
+        $data = rcmail::get_instance()->plugins->exec_hook('storage_folders',
+            array('root' => $root, 'name' => $name, 'filter' => $filter, 'mode' => 'LSUB'));
+
+        if (isset($data['folders'])) {
+            $a_mboxes = $data['folders'];
+        }
+        else {
+            $a_mboxes = $this->list_folders_subscribed_direct($root, $name);
+        }
 
         if (!is_array($a_mboxes)) {
             return array();
@@ -2493,66 +2506,57 @@
 
 
     /**
-     * protected method for folders listing (LSUB)
+     * Method for direct folders listing (LSUB)
      *
      * @param   string  $root   Optional root folder
      * @param   string  $name   Optional name pattern
-     * @param   mixed   $filter Optional filter
-     * @param   string  $rights Optional ACL requirements
      *
      * @return  array   List of subscribed folders
      * @see     rcube_imap::list_folders_subscribed()
      */
-    protected function _list_folders_subscribed($root='', $name='*', $filter=null, $rights=null)
+    public function list_folders_subscribed_direct($root='', $name='*')
     {
-        $a_defaults = $a_out = array();
-
-        // Give plugins a chance to provide a list of folders
-        $data = rcmail::get_instance()->plugins->exec_hook('storage_folders',
-            array('root' => $root, 'name' => $name, 'filter' => $filter, 'mode' => 'LSUB'));
-
-        if (isset($data['folders'])) {
-            $a_folders = $data['folders'];
-        }
-        else if (!$this->check_connection()) {
+        if (!$this->check_connection()) {
            return null;
         }
-        else {
-            // Server supports LIST-EXTENDED, we can use selection options
-            $config = rcmail::get_instance()->config;
-            // #1486225: Some dovecot versions returns wrong result using LIST-EXTENDED
-            if (!$config->get('imap_force_lsub') && $this->get_capability('LIST-EXTENDED')) {
-                // This will also set folder options, LSUB doesn't do that
-                $a_folders = $this->conn->listMailboxes($root, $name,
-                    NULL, array('SUBSCRIBED'));
 
-                // unsubscribe non-existent folders, remove from the list
-                if (is_array($a_folders) && $name == '*') {
-                    foreach ($a_folders as $idx => $folder) {
-                        if ($this->conn->data['LIST'] && ($opts = $this->conn->data['LIST'][$folder])
-                            && in_array('\\NonExistent', $opts)
-                        ) {
-                            $this->conn->unsubscribe($folder);
-                            unset($a_folders[$idx]);
-                        }
+        $config = rcmail::get_instance()->config;
+
+        // Server supports LIST-EXTENDED, we can use selection options
+        // #1486225: Some dovecot versions returns wrong result using LIST-EXTENDED
+        if (!$config->get('imap_force_lsub') && $this->get_capability('LIST-EXTENDED')) {
+            // This will also set folder options, LSUB doesn't do that
+            $a_folders = $this->conn->listMailboxes($root, $name,
+                NULL, array('SUBSCRIBED'));
+
+            // unsubscribe non-existent folders, remove from the list
+            // we can do this only when LIST response is available
+            if (is_array($a_folders) && $name == '*' && !empty($this->conn->data['LIST'])) {
+                foreach ($a_folders as $idx => $folder) {
+                    if (($opts = $this->conn->data['LIST'][$folder])
+                        && in_array('\\NonExistent', $opts)
+                    ) {
+                        $this->conn->unsubscribe($folder);
+                        unset($a_folders[$idx]);
                     }
                 }
             }
-            // retrieve list of folders from IMAP server using LSUB
-            else {
-                $a_folders = $this->conn->listSubscribed($root, $name);
+        }
+        // retrieve list of folders from IMAP server using LSUB
+        else {
+            $a_folders = $this->conn->listSubscribed($root, $name);
 
-                // unsubscribe non-existent folders, remove from the list
-                if (is_array($a_folders) && $name == '*') {
-                    foreach ($a_folders as $idx => $folder) {
-                        if ($this->conn->data['LIST'] && ($opts = $this->conn->data['LIST'][$folder])
-                            && in_array('\\Noselect', $opts)
-                        ) {
-                            // Some servers returns \Noselect for existing folders
-                            if (!$this->folder_exists($folder)) {
-                                $this->conn->unsubscribe($folder);
-                                unset($a_folders[$idx]);
-                            }
+            // unsubscribe non-existent folders, remove them from the list,
+            // we can do this only when LIST response is available
+            if (is_array($a_folders) && $name == '*' && !empty($this->conn->data['LIST'])) {
+                foreach ($a_folders as $idx => $folder) {
+                    if (!isset($this->conn->data['LIST'][$folder])
+                        || in_array('\\Noselect', $this->conn->data['LIST'][$folder])
+                    ) {
+                        // Some servers returns \Noselect for existing folders
+                        if (!$this->folder_exists($folder)) {
+                            $this->conn->unsubscribe($folder);
+                            unset($a_folders[$idx]);
                         }
                     }
                 }
@@ -2602,7 +2606,7 @@
         }
         else {
             // retrieve list of folders from IMAP server
-            $a_mboxes = $this->_list_folders($root, $name);
+            $a_mboxes = $this->list_folders_direct($root, $name);
         }
 
         if (!is_array($a_mboxes)) {
@@ -2637,7 +2641,7 @@
 
 
     /**
-     * protected method for folders listing (LIST)
+     * Method for direct folders listing (LIST)
      *
      * @param   string  $root   Optional root folder
      * @param   string  $name   Optional name pattern
@@ -2645,7 +2649,7 @@
      * @return  array   List of folders
      * @see     rcube_imap::list_folders()
      */
-    protected function _list_folders($root='', $name='*')
+    public function list_folders_direct($root='', $name='*')
     {
         if (!$this->check_connection()) {
             return null;
@@ -2913,7 +2917,7 @@
                 if (strpos($c_mbox, $folder.$delm) === 0) {
                     $this->conn->unsubscribe($c_mbox);
                     if ($this->conn->deleteFolder($c_mbox)) {
-	                    $this->clear_message_cache($c_mbox);
+                        $this->clear_message_cache($c_mbox);
                     }
                 }
             }
@@ -3527,7 +3531,8 @@
     {
         if ($this->caching && !$this->cache) {
             $rcmail = rcmail::get_instance();
-            $ttl = $rcmail->config->get('message_cache_lifetime', '10d') - mktime();
+            $ttl = $rcmail->config->get('message_cache_lifetime', '10d');
+            $ttl = get_offset_time($ttl) - time();
             $this->cache = $rcmail->get_cache('IMAP', $this->caching, $ttl);
         }
 
@@ -3850,4 +3855,139 @@
         write_log('imap', $message);
     }
 
-}  // end class rcube_imap
+
+    /**
+     * Deprecated methods (to be removed)
+     */
+
+    public function decode_address_list($input, $max = null, $decode = true, $fallback = null)
+    {
+        return rcube_mime::decode_address_list($input, $max, $decode, $fallback);
+    }
+
+    public function decode_header($input, $fallback = null)
+    {
+        return rcube_mime::decode_mime_string((string)$input, $fallback);
+    }
+
+    public static function decode_mime_string($input, $fallback = null)
+    {
+        return rcube_mime::decode_mime_string($input, $fallback);
+    }
+
+    public function mime_decode($input, $encoding = '7bit')
+    {
+        return rcube_mime::decode($input, $encoding);
+    }
+
+    public static function explode_header_string($separator, $str, $remove_comments = false)
+    {
+        return rcube_mime::explode_header_string($separator, $str, $remove_comments);
+    }
+
+    public function select_mailbox($mailbox)
+    {
+        // do nothing
+    }
+
+    public function set_mailbox($folder)
+    {
+        $this->set_folder($folder);
+    }
+
+    public function get_mailbox_name()
+    {
+        return $this->get_folder();
+    }
+
+    public function list_headers($folder='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $slice=0)
+    {
+        return $this->list_messages($folder, $page, $sort_field, $sort_order, $slice);
+    }
+
+    public function mailbox_status($folder = null)
+    {
+        return $this->folder_status($folder);
+    }
+
+    public function message_index($folder = '', $sort_field = NULL, $sort_order = NULL)
+    {
+        return $this->index($folder, $sort_field, $sort_order);
+    }
+
+    public function message_index_direct($folder, $sort_field = null, $sort_order = null, $skip_cache = true)
+    {
+        return $this->index_direct($folder, $sort_field, $sort_order, $skip_cache);
+    }
+
+    public function list_mailboxes($root='', $name='*', $filter=null, $rights=null, $skip_sort=false)
+    {
+        return $this->list_folders_subscribed($root, $name, $filter, $rights, $skip_sort);
+    }
+
+    public function list_unsubscribed($root='', $name='*', $filter=null, $rights=null, $skip_sort=false)
+    {
+        return $this->list_folders($root, $name, $filter, $rights, $skip_sort);
+    }
+
+    public function get_mailbox_size($folder)
+    {
+        return $this->folder_size($folder);
+    }
+
+    public function create_mailbox($folder, $subscribe=false)
+    {
+        return $this->create_folder($folder, $subscribe);
+    }
+
+    public function rename_mailbox($folder, $new_name)
+    {
+        return $this->rename_folder($folder, $new_name);
+    }
+
+    function delete_mailbox($folder)
+    {
+        return $this->delete_folder($folder);
+    }
+
+    public function mailbox_exists($folder, $subscription=false)
+    {
+        return $this->folder_exists($folder, $subscription);
+    }
+
+    public function mailbox_namespace($folder)
+    {
+        return $this->folder_namespace($folder);
+    }
+
+    public function mod_mailbox($folder, $mode = 'out')
+    {
+        return $this->mod_folder($folder, $mode);
+    }
+
+    public function mailbox_attributes($folder, $force=false)
+    {
+        return $this->folder_attributes($folder, $force);
+    }
+
+    public function mailbox_data($folder)
+    {
+        return $this->folder_data($folder);
+    }
+
+    public function mailbox_info($folder)
+    {
+        return $this->folder_info($folder);
+    }
+
+    public function mailbox_sync($folder)
+    {
+        return $this->folder_sync($folder);
+    }
+
+    public function expunge($folder='', $clear_cache=true)
+    {
+        return $this->expunge_folder($folder, $clear_cache);
+    }
+
+}

--
Gitblit v1.9.1