From ee9ee7f66fae218afe24942166ba833eb490ada3 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Sun, 29 Jan 2012 08:49:49 -0500
Subject: [PATCH] Backported r5820 and r5842

---
 program/include/rcube_imap_cache.php |   37 +++++++++++++++++++++++++++++--------
 1 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/program/include/rcube_imap_cache.php b/program/include/rcube_imap_cache.php
index d304386..acff146 100644
--- a/program/include/rcube_imap_cache.php
+++ b/program/include/rcube_imap_cache.php
@@ -813,9 +813,16 @@
         }
 
         // Folder is empty but cache isn't
-        if (empty($mbox_data['EXISTS']) && (!empty($index['seq']) || !empty($index['tree']))) {
-            $this->clear($mailbox);
-            $exists = false;
+        if (empty($mbox_data['EXISTS'])) {
+            if (!empty($index['seq']) || !empty($index['tree'])) {
+                $this->clear($mailbox);
+                $exists = false;
+                return false;
+            }
+        }
+        // Folder is not empty but cache is
+        else if (empty($index['seq']) && empty($index['tree'])) {
+            unset($this->icache[$mailbox][$is_thread ? 'thread' : 'index']);
             return false;
         }
 
@@ -846,7 +853,7 @@
         // @TODO: find better validity check for threaded index
         if ($is_thread) {
             // check messages number...
-            if ($mbox_data['EXISTS'] != max(array_keys($index['depth']))) {
+            if (!$this->skip_deleted && $mbox_data['EXISTS'] != @max(array_keys($index['depth']))) {
                 return false;
             }
             return true;
@@ -880,7 +887,7 @@
                     rcube_imap_generic::compressMessageSet($index['uid']));
 
                 if (!empty($ids)) {
-                    $index = null; // cache invalid
+                    return false;
                 }
             }
         }
@@ -996,12 +1003,22 @@
             !empty($uids) ? $uids : '1:*', true, array('FLAGS'),
             $index['modseq'], $qresync);
 
+        $invalidated = false;
+
         if (!empty($result)) {
             foreach ($result as $id => $msg) {
                 $uid = $msg->uid;
                 // Remove deleted message
                 if ($this->skip_deleted && !empty($msg->flags['DELETED'])) {
                     $this->remove_message($mailbox, $uid);
+
+                    if (!$invalidated) {
+                        $invalidated = true;
+                        // Invalidate thread indexes (?)
+                        $this->remove_thread($mailbox);
+                        // Invalidate index
+                        $index['valid'] = false;
+                    }
                     continue;
                 }
 
@@ -1036,6 +1053,8 @@
 
                     // Invalidate thread indexes (?)
                     $this->remove_thread($mailbox);
+                    // Invalidate index
+                    $index['valid'] = false;
                 }
             }
         }
@@ -1089,10 +1108,12 @@
     private function build_thread_data($data, &$depth, &$children, $level = 0)
     {
         foreach ((array)$data as $key => $val) {
-            $children[$key] = !empty($val);
-            $depth[$key] = $level;
-            if (!empty($val))
+            $empty          = empty($val) || !is_array($val);
+            $children[$key] = !$empty;
+            $depth[$key]    = $level;
+            if (!$empty) {
                 $this->build_thread_data($val, $depth, $children, $level + 1);
+            }
         }
     }
 

--
Gitblit v1.9.1