From 11074801923807a9448f7427299ddba76e235e42 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Sun, 25 Nov 2007 15:40:10 -0500
Subject: [PATCH] Only show new messages if they match the current search (#1484176)

---
 program/js/list.js                  |    3 +--
 CHANGELOG                           |    2 +-
 program/steps/mail/list.inc         |    3 ++-
 program/steps/mail/check_recent.inc |   14 +++++++++++---
 program/js/app.js                   |    4 +++-
 program/include/rcube_imap.inc      |   16 +++++++++++++++-
 program/steps/mail/move_del.inc     |    2 +-
 7 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index f646061..131b720 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -12,7 +12,7 @@
 - Added Georgian localization by Zaza Zviadadze
 - Updated Russian localization
 - Fixed some potential security risks (audited by Andris)
-
+- Only show new messages if they match the current search (#1484176)
 
 2007/11/20 (tomekp)
 ----------
diff --git a/program/include/rcube_imap.inc b/program/include/rcube_imap.inc
index 5a9994c..56859b3 100644
--- a/program/include/rcube_imap.inc
+++ b/program/include/rcube_imap.inc
@@ -441,7 +441,7 @@
       $mailbox = $this->mailbox;
       
     // count search set
-    if ($this->search_string && $mailbox == $this->mailbox && $mode == 'ALL')
+    if ($this->search_string && $mailbox == $this->mailbox && $mode == 'ALL' && !$force)
       return count((array)$this->search_set);
 
     $a_mailbox_cache = $this->get_cache('messagecount');
@@ -934,6 +934,20 @@
       
     return $this->get_search_set();
     }
+  
+  
+  /**
+   * Check if the given message ID is part of the current search set
+   *
+   * @return True on match or if no search request is stored
+   */
+  function in_searchset($msgid)
+  {
+    if (!empty($this->search_string))
+      return in_array("$msgid", (array)$this->search_set, true);
+    else
+      return true;
+  }
 
 
   /**
diff --git a/program/js/app.js b/program/js/app.js
index 5b9265b..9bb8568 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -1143,6 +1143,8 @@
       this.command('show');
     else if (list.key_pressed == list.DELETE_KEY)
       this.command('delete');
+    else
+      list.shiftkey = false;
     };
 
 
@@ -3483,7 +3485,7 @@
       }
 
     this.set_busy(true, 'checkingmail');
-    this.http_request('check-recent', '_t='+(new Date().getTime()), true);
+    this.http_request('check-recent', (this.env.search_request ? '_search='+this.env.search_request+'&' : '') + '_t='+(new Date().getTime()), true);
     };
 
 
diff --git a/program/js/list.js b/program/js/list.js
index de4d7c8..06c3554 100644
--- a/program/js/list.js
+++ b/program/js/list.js
@@ -535,8 +535,6 @@
   if (this.focused != true) 
     return true;
 
-  this.shiftkey = e.shiftKey;
-
   var keyCode = document.layers ? e.which : document.all ? event.keyCode : document.getElementById ? e.keyCode : 0;
   var mod_key = rcube_event.get_modifier(e);
   switch (keyCode)
@@ -547,6 +545,7 @@
       break;
 
     default:
+      this.shiftkey = e.shiftKey;
       this.key_pressed = keyCode;
       this.trigger_event('keypress');
   }
diff --git a/program/steps/mail/check_recent.inc b/program/steps/mail/check_recent.inc
index a2100f1..a392cd0 100644
--- a/program/steps/mail/check_recent.inc
+++ b/program/steps/mail/check_recent.inc
@@ -27,18 +27,26 @@
     {
     if ($recent_count = $IMAP->messagecount(NULL, 'RECENT', TRUE))
       {
-      $count = $IMAP->messagecount(NULL, 'ALL', TRUE);
+      // refresh saved search set
+      if (($search_request = get_input_value('_search', RCUBE_INPUT_GPC)) && $IMAP->search_set)
+        $_SESSION['search'][$search_request] = $IMAP->refresh_search();
+        
+      $count_all = $IMAP->messagecount(NULL, 'ALL', TRUE);
       $unread_count = $IMAP->messagecount(NULL, 'UNSEEN', TRUE);
 
-      $OUTPUT->set_env('messagecount', $count);
+      $OUTPUT->set_env('messagecount', $IMAP->messagecount());
       $OUTPUT->command('set_unread_count', $mbox_name, $unread_count, true);
       $OUTPUT->command('set_rowcount', rcmail_get_messagecount_text());
       $OUTPUT->command('set_quota', $IMAP->get_quota());
 
       // add new message headers to list
       $a_headers = array();
-      for ($i=$recent_count, $id=$count-$recent_count+1; $i>0; $i--, $id++)
+      for ($i=$recent_count, $id=$count_all-$recent_count+1; $i>0; $i--, $id++)
         {
+        // skip message if it does not match the current search
+        if (!$IMAP->in_searchset($id))
+          continue;
+        
         $header = $IMAP->get_headers($id, NULL, FALSE);
         if ($header->recent)
           $a_headers[] = $header;
diff --git a/program/steps/mail/list.inc b/program/steps/mail/list.inc
index 1961442..8caf4c0 100644
--- a/program/steps/mail/list.inc
+++ b/program/steps/mail/list.inc
@@ -40,9 +40,10 @@
 $mbox_name = $IMAP->get_mailbox_name();
 
 // fetch message headers
-if ($count = $IMAP->messagecount($mbox_name, 'ALL', !empty($_REQUEST['_refresh'])))
+if ($IMAP->messagecount($mbox_name, 'ALL', !empty($_REQUEST['_refresh'])))
   $a_headers = $IMAP->list_headers($mbox_name, NULL, $sort_col, $sort_order);
 
+$count = $IMAP->messagecount($mbox_name);
 $unseen = $IMAP->messagecount($mbox_name, 'UNSEEN', !empty($_REQUEST['_refresh']));
 
 // update message count display
diff --git a/program/steps/mail/move_del.inc b/program/steps/mail/move_del.inc
index ff6117d..c78d6db 100644
--- a/program/steps/mail/move_del.inc
+++ b/program/steps/mail/move_del.inc
@@ -60,7 +60,7 @@
 else
   exit;
 
-// refresh saved seach set after moving some messages
+// refresh saved search set after moving some messages
 if (($search_request = get_input_value('_search', RCUBE_INPUT_GPC)) && $IMAP->search_set)
   $_SESSION['search'][$search_request] = $IMAP->refresh_search();
 

--
Gitblit v1.9.1