revert autosync changes
authorDavid Bienvenu <bienvenu@nventure.com>
Thu, 12 Aug 2010 15:41:05 -0700
changeset 6148 fe9a71fcf37c84af486897ab0f14e16ed4e9a4b5
parent 6147 142f3807100c6938ac3c61bc4547ac4aa53705b0
child 6149 ec76ca68cbcee0b6874cb3f9f9c34467baec50da
push idunknown
push userunknown
push dateunknown
revert autosync changes
mailnews/imap/public/nsIAutoSyncManager.idl
mailnews/imap/public/nsIAutoSyncState.idl
mailnews/imap/src/nsAutoSyncManager.cpp
mailnews/imap/src/nsAutoSyncManager.h
mailnews/imap/src/nsAutoSyncState.cpp
mailnews/imap/src/nsAutoSyncState.h
mailnews/imap/src/nsImapMailFolder.cpp
mailnews/imap/src/nsImapMailFolder.h
--- a/mailnews/imap/public/nsIAutoSyncManager.idl
+++ b/mailnews/imap/public/nsIAutoSyncManager.idl
@@ -108,44 +108,44 @@ interface nsIAutoSyncMgrListener : nsISu
   /**
    * It is called on the listener after the auto-sync manager updates the given folder 
    * (mostly for debugging purposes)
    */
   void onAutoSyncInitiated(in nsIMsgFolder aFolder);
 };
 
 
-[scriptable, uuid(7fe0b48e-f5d8-4747-beb7-888c9cced3a5)]
+[scriptable, uuid(E8DC3FD2-E321-400B-B52D-416E39EF5335)] 
 interface nsIAutoSyncManager : nsISupports {
 
   /** 
    * Download models
    */
   const long dmParallel = 0;
   const long dmChained = 1;
-
+  
   /**
    * Suggested minimum grouping size in bytes for message downloads.
    * Setting this attribute to 0 resets its value to the 
    * hardcoded default.
    */
   attribute unsigned long groupSize;
-
-  /**
+  
+  /** 
    * Active strategy function to prioritize
    * messages in the download queue
    */
   attribute nsIAutoSyncMsgStrategy msgStrategy;
-
-  /**
+  
+  /** 
    * Active strategy function to prioritize
    * folders in the download queue
    */
   attribute nsIAutoSyncFolderStrategy folderStrategy;
-
+  
   /**
    * Adds a listener to notify about auto-sync events
    */
   void addListener(in nsIAutoSyncMgrListener aListener);
   
   /**
    * Removes the listener from notification list
    */
@@ -192,25 +192,17 @@ interface nsIAutoSyncManager : nsISuppor
    * @see nsAutoSyncManager.h for details
    */
   readonly attribute unsigned long downloadQLength;
 
   /**
    * Active download model; Chained (serial), or Parallel
    */
   attribute long downloadModel;
-
-  /**
-   * The imap folder corresponding to aAutoSyncState has had a message
-   * added to it. Autosync may want to add this folder to the update q.
-   *
-   * @param aAutoSyncState state obj for folder needing updating
-   */
-  void onFolderHasPendingMsgs(in nsIAutoSyncState aAutoSyncState);
-
+  
   /// Pause autosync (e.g., we're downloading for offline).
   void pause();
   
   /// Resume normal autosync activities (e.g., we've come back online).
   void resume();
 };
 
 %{C++
--- a/mailnews/imap/public/nsIAutoSyncState.idl
+++ b/mailnews/imap/public/nsIAutoSyncState.idl
@@ -36,44 +36,37 @@
  
 #include "nsISupports.idl"
 
 interface nsIMsgFolder;
 interface nsIMsgWindow;
 interface nsIArray;
 interface nsIMutableArray;
 
-[scriptable, uuid(7512f927-b8f0-48c4-b101-03e859e61281)]
+[scriptable, uuid(8D4950BC-9D6F-4886-94A2-A0A551B3376E)] 
 interface nsIAutoSyncState : nsISupports {
 
   /**
    * Auto-Sync states
    */
 
   /** sync'd and no pending messages */
   const long stCompletedIdle = 0;     
 
   /** STATUS issued. Will check to see if  any counts changed since last STATUS */
   const long stStatusIssued = 1;
 
-  /**
-   * Status found new messages. Update should be issued next. Status most
-   * likely was issued by non-autosync code (e.g., check other folders for
-   * new messages).
-   */
-  const long stUpdateNeeded = 2;
-
   /** Update issued. Will figure out if there are any bodies to download */
-  const long stUpdateIssued = 3;
+  const long stUpdateIssued = 2;
 
   /** Message body download in progress */
-  const long stDownloadInProgress = 4;
+  const long stDownloadInProgress = 3;
 
   /** ready to download the next group of messages */
-  const long stReadyToDownload = 5;
+  const long stReadyToDownload = 4;
 
   /**
    * Puts the download queue offset to its previous position. 
    */
   void rollback();
   
   /**
    * Clears the download queue. Resets the offsets.
@@ -92,21 +85,16 @@ interface nsIAutoSyncState : nsISupports
   void resetRetryCounter();
   
   /**
    * Tests whether the given folder has the same imap server. 
    */
   boolean isSibling(in nsIAutoSyncState aAnotherStateObj);
 
   /**
-   * Update the folder to find new message headers to download
-   */
-  void updateFolder();
-
-  /**
    * Downloads the bodies of the given messages from the server.
    */
   void downloadMessagesForOffline(in nsIArray aMessageList);
 
   /**
    * Populates the given array with the keys of the messages that will 
    * be downloaded next.
    *
--- a/mailnews/imap/src/nsAutoSyncManager.cpp
+++ b/mailnews/imap/src/nsAutoSyncManager.cpp
@@ -215,24 +215,16 @@ NS_IMETHODIMP
 nsDefaultAutoSyncFolderStrategy::IsExcluded(nsIMsgFolder *aFolder, PRBool *aDecision)
 {
   NS_ENSURE_ARG_POINTER(aDecision);
   NS_ENSURE_ARG_POINTER(aFolder);
   PRUint32 folderFlags;
   aFolder->GetFlags(&folderFlags);
   // exclude saved search
   *aDecision = (folderFlags & nsMsgFolderFlags::Virtual);
-  if (!*aDecision)
-  {
-    // Exclude orphans
-    nsCOMPtr<nsIMsgFolder> parent;
-    aFolder->GetParent(getter_AddRefs(parent));
-    if (!parent)
-      *aDecision = PR_TRUE;
-  }
   return NS_OK;
 }
 
 #define NOTIFY_LISTENERS_STATIC(obj_, propertyfunc_, params_) \
   PR_BEGIN_MACRO \
   nsTObserverArray<nsCOMPtr<nsIAutoSyncMgrListener> >::ForwardIterator iter(obj_->mListeners); \
   nsCOMPtr<nsIAutoSyncMgrListener> listener; \
   while (iter.HasMore()) { \
@@ -313,50 +305,49 @@ void nsAutoSyncManager::TimerCallback(ns
   nsAutoSyncManager *autoSyncMgr = static_cast<nsAutoSyncManager*>(aClosure);
   if (autoSyncMgr->GetIdleState() == notIdle ||
     (autoSyncMgr->mDiscoveryQ.Count() <= 0 && autoSyncMgr->mUpdateQ.Count() <= 0))
   {
     // Idle will create a new timer automatically if discovery Q or update Q is not empty
     autoSyncMgr->StopTimer();
   }
 
-  // process folders within the discovery queue
+  // process folders within the discovery queue 
   if (autoSyncMgr->mDiscoveryQ.Count() > 0)
   {
     nsCOMPtr<nsIAutoSyncState> autoSyncStateObj(autoSyncMgr->mDiscoveryQ[0]);
     if (autoSyncStateObj)
     {
       PRUint32 leftToProcess;
       nsresult rv = autoSyncStateObj->ProcessExistingHeaders(kNumberOfHeadersToProcess, &leftToProcess);
-
+      
       nsCOMPtr<nsIMsgFolder> folder;
       autoSyncStateObj->GetOwnerFolder(getter_AddRefs(folder));
       if (folder)
         NOTIFY_LISTENERS_STATIC(autoSyncMgr, OnDiscoveryQProcessed, (folder, kNumberOfHeadersToProcess, leftToProcess));
-
+            
       if (NS_SUCCEEDED(rv) && 0 == leftToProcess)
       {
         autoSyncMgr->mDiscoveryQ.RemoveObjectAt(0);
         if (folder)
           NOTIFY_LISTENERS_STATIC(autoSyncMgr, OnFolderRemovedFromQ, (nsIAutoSyncMgrListener::DiscoveryQueue, folder));
       }
     }
   }
-
+  
   if (autoSyncMgr->mUpdateQ.Count() > 0)
   {
     if (autoSyncMgr->mUpdateState == completed)
     {
       nsCOMPtr<nsIAutoSyncState> autoSyncStateObj(autoSyncMgr->mUpdateQ[0]);
       if (autoSyncStateObj)
       {
         PRInt32 state;
         nsresult rv = autoSyncStateObj->GetState(&state);
-        if (NS_SUCCEEDED(rv) && (state == nsAutoSyncState::stCompletedIdle ||
-                                 state == nsAutoSyncState::stUpdateNeeded))
+        if (NS_SUCCEEDED(rv) && nsAutoSyncState::stCompletedIdle == state)
         {
           nsCOMPtr<nsIMsgFolder> folder; 
           autoSyncStateObj->GetOwnerFolder(getter_AddRefs(folder));
           if (folder)
           {
             nsCOMPtr <nsIMsgImapMailFolder> imapFolder = do_QueryInterface(folder, &rv);
             NS_ENSURE_SUCCESS(rv,);
             rv = imapFolder->InitiateAutoSync(autoSyncMgr);
@@ -755,18 +746,17 @@ nsresult nsAutoSyncManager::StartIdlePro
       NOTIFY_LISTENERS(OnFolderRemovedFromQ,
                       (nsIAutoSyncMgrListener::PriorityQueue, folder));
   }
     
   return AutoUpdateFolders();
 }
 
 /**
- * Updates offline imap folders that are not synchronized recently. This is
- * called whenever we're idle.
+ * Updates offline imap folders that are not synchronized recently.
  */
 nsresult nsAutoSyncManager::AutoUpdateFolders()
 {
   nsresult rv;
 
   // iterate through each imap account and update offline folders automatically
 
   nsCOMPtr<nsIMsgAccountManager> accountManager = do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
@@ -850,17 +840,17 @@ nsresult nsAutoSyncManager::AutoUpdateFo
           if (NS_FAILED(rv) || !autoSyncOfflineStores)
             continue;
         }
 
         nsCOMPtr<nsIAutoSyncState> autoSyncState;
         rv = imapFolder->GetAutoSyncStateObj(getter_AddRefs(autoSyncState));
         NS_ASSERTION(autoSyncState, "*** nsAutoSyncState shouldn't be NULL, check owner folder");
 
-        // shouldn't happen but let's be defensive here
+        // shouldn't happen but lets be defensive here
         if (!autoSyncState)
           continue;
 
         PRInt32 state;
         rv = autoSyncState->GetState(&state);
 
         if (NS_SUCCEEDED(rv) && nsAutoSyncState::stCompletedIdle == state)
         {
@@ -906,62 +896,62 @@ nsresult nsAutoSyncManager::AutoUpdateFo
 }
 
 /**
  * Places the given folder into the priority queue based on active
  * strategy function.
  */
 void nsAutoSyncManager::ScheduleFolderForOfflineDownload(nsIAutoSyncState *aAutoSyncStateObj)
 {
-  if (aAutoSyncStateObj && (mPriorityQ.IndexOf(aAutoSyncStateObj) == -1))
+  if (aAutoSyncStateObj &&  (mPriorityQ.IndexOf(aAutoSyncStateObj) == -1))
   {
     nsCOMPtr<nsIAutoSyncFolderStrategy> folStrategy;
     GetFolderStrategy(getter_AddRefs(folStrategy));
-
+        
     if (mPriorityQ.Count() <= 0)
     {
       // make sure that we don't insert a folder excluded by the given strategy
       nsCOMPtr<nsIMsgFolder> folder;
       aAutoSyncStateObj->GetOwnerFolder(getter_AddRefs(folder));
       if (folder)
       {
         PRBool excluded = PR_FALSE;
         if (folStrategy)
           folStrategy->IsExcluded(folder, &excluded);
-
+        
         if (!excluded)
         {
           mPriorityQ.AppendObject(aAutoSyncStateObj); // insert into the first spot
           NOTIFY_LISTENERS(OnFolderAddedIntoQ, (nsIAutoSyncMgrListener::PriorityQueue, folder));
         }
       }
     }
-    else
+    else 
     {
       // find the right spot for the given folder      
       PRUint32 qidx = mPriorityQ.Count();
       while (qidx > 0) 
       {
         --qidx;
-
+        
         nsCOMPtr<nsIMsgFolder> folderA, folderB;
         mPriorityQ[qidx]->GetOwnerFolder(getter_AddRefs(folderA));
         aAutoSyncStateObj->GetOwnerFolder(getter_AddRefs(folderB));
         
         PRBool excluded = PR_FALSE;
         if (folderB && folStrategy)
           folStrategy->IsExcluded(folderB, &excluded);
-
+          
         if (excluded)
           break;
-
+        
         nsAutoSyncStrategyDecisionType decision = nsAutoSyncStrategyDecisions::Same;
         if (folderA && folderB && folStrategy)
           folStrategy->Sort(folderA, folderB, &decision);
-
+                  
         if (decision == nsAutoSyncStrategyDecisions::Higher && 0 == qidx)
           mPriorityQ.InsertObjectAt(aAutoSyncStateObj, 0);
         else if (decision == nsAutoSyncStrategyDecisions::Higher)
           continue;
         else if (decision == nsAutoSyncStrategyDecisions::Lower)
           mPriorityQ.InsertObjectAt(aAutoSyncStateObj, qidx+1);
         else //  decision == nsAutoSyncStrategyDecisions::Same
           mPriorityQ.InsertObjectAt(aAutoSyncStateObj, qidx);
@@ -1167,45 +1157,45 @@ NS_IMETHODIMP nsAutoSyncManager::SetFold
   mFolderStrategyImpl = aFolderStrategy;
   return NS_OK;
 }
 
 NS_IMETHODIMP 
 nsAutoSyncManager::DoesMsgFitDownloadCriteria(nsIMsgDBHdr *aMsgHdr, PRBool *aResult)
 {
   NS_ENSURE_ARG_POINTER(aResult);
-
+  
   PRUint32 msgFlags = 0;
   aMsgHdr->GetFlags(&msgFlags);
-
+  
   // check whether this message is marked imap deleted or not 
   *aResult = !(msgFlags & nsMsgMessageFlags::IMAPDeleted);
   if (!(*aResult))
     return NS_OK;
-
+    
   PRBool shouldStoreMsgOffline = PR_TRUE;
   nsCOMPtr<nsIMsgFolder> folder;
   aMsgHdr->GetFolder(getter_AddRefs(folder));
   if (folder)
   {
     nsMsgKey msgKey;
     nsresult rv = aMsgHdr->GetMessageKey(&msgKey);
     // a cheap way to get the size limit for this folder and make
     // sure that we don't have this message offline already
     if (NS_SUCCEEDED(rv))
       folder->ShouldStoreMsgOffline(msgKey, &shouldStoreMsgOffline);
   }
-
+        
   *aResult &= shouldStoreMsgOffline;
-
+  
   return NS_OK;
 }
 
 NS_IMETHODIMP nsAutoSyncManager::OnDownloadQChanged(nsIAutoSyncState *aAutoSyncStateObj)
-{
+{  
   nsCOMPtr<nsIAutoSyncState> autoSyncStateObj(aAutoSyncStateObj);
   if (!autoSyncStateObj)
     return NS_ERROR_INVALID_ARG;
 
   if (mPaused)
     return NS_OK;
   // We want to start downloading immediately unless the folder is excluded.
   PRBool excluded = PR_FALSE;
@@ -1220,17 +1210,17 @@ NS_IMETHODIMP nsAutoSyncManager::OnDownl
 
   nsresult rv = NS_OK;
 
   if (!excluded)
   {
     // Add this folder into the priority queue.
     autoSyncStateObj->SetState(nsAutoSyncState::stReadyToDownload);
     ScheduleFolderForOfflineDownload(autoSyncStateObj);
-
+    
     // If we operate in parallel mode or if there is no sibling downloading messages at the moment,
     // we can download the first group of the messages for this folder
     if (mDownloadModel == dmParallel ||
         !DoesQContainAnySiblingOf(mPriorityQ, autoSyncStateObj, nsAutoSyncState::stDownloadInProgress))
     {
       // this will download the first group of messages immediately;
       // to ensure that we don't end up downloading a large single message in not-idle time, 
       // we enforce a limit. If there is no message fits into this limit we postpone the 
@@ -1393,52 +1383,19 @@ NS_IMETHODIMP nsAutoSyncManager::GetUpda
 /* readonly attribute unsigned long downloadQLength; */
 NS_IMETHODIMP nsAutoSyncManager::GetDownloadQLength(PRUint32 *aDownloadQLength)
 {
   NS_ENSURE_ARG_POINTER(aDownloadQLength);
   *aDownloadQLength = mPriorityQ.Count();
   return NS_OK;
 }
 
-NS_IMETHODIMP
-nsAutoSyncManager::OnFolderHasPendingMsgs(nsIAutoSyncState *aAutoSyncStateObj)
-{
-  NS_ENSURE_ARG_POINTER(aAutoSyncStateObj);
-  if (mUpdateQ.IndexOf(aAutoSyncStateObj) == -1)
-  {
-    nsCOMPtr<nsIMsgFolder> folder;
-    aAutoSyncStateObj->GetOwnerFolder(getter_AddRefs(folder));
-    // If this folder isn't the trash, add it to the update q.
-    if (folder)
-    {
-      PRBool isTrash;
-      folder->GetFlag(nsMsgFolderFlags::Trash, &isTrash);
-      if (!isTrash)
-      {
-        PRBool isSentOrArchive;
-        folder->IsSpecialFolder(nsMsgFolderFlags::SentMail|
-                                nsMsgFolderFlags::Archive,
-                                PR_TRUE, &isSentOrArchive);
-        // Sent or archive folders go to the q front, the rest to the end.
-        if (isSentOrArchive)
-          mUpdateQ.InsertObjectAt(aAutoSyncStateObj, 0);
-        else
-          mUpdateQ.AppendObject(aAutoSyncStateObj);
-        aAutoSyncStateObj->SetState(nsAutoSyncState::stUpdateNeeded);
-        NOTIFY_LISTENERS(OnFolderAddedIntoQ,
-                        (nsIAutoSyncMgrListener::UpdateQueue, folder));
-      }
-    }
-  }
-  return NS_OK;
-}
-
 void nsAutoSyncManager::SetIdleState(IdleState st) 
 { 
   mIdleState = st;
 }
-
+    
 nsAutoSyncManager::IdleState nsAutoSyncManager::GetIdleState() const 
 { 
   return mIdleState; 
 }
 
 NS_IMPL_ISUPPORTS3(nsAutoSyncManager, nsIObserver, nsIUrlListener, nsIAutoSyncManager)
--- a/mailnews/imap/src/nsAutoSyncManager.h
+++ b/mailnews/imap/src/nsAutoSyncManager.h
@@ -100,23 +100,16 @@ class nsIMsgFolder;
  *  o If the message size is zero, auto-sync ignores the message.
  *  o If the download of the message group fails for some reason, auto-sync tries to
  *    download the same group |kGroupRetryCount| times. If it still fails, continues with the
  *    next group of messages.
  *
  * Download Model:
  *  Parallel model should be used with the imap servers that do not have any "max number of sessions
  *  per IP" limit, and when the bandwidth is significantly large.
- *
- * How it really works:
- * The AutoSyncManager gets an idle notification. First it processes any
- * folders in the discovery queue (which means it schedules message download
- * for any messages it previously determined it should download). Then it sets
- * a timer, and in the timer callback, it processes the update q, by calling 
- * InitiateAutoSync on the first folder in the update q.
  */
  
 /**
  * Default strategy implementation to prioritize messages in the download queue.   
  */
 class nsDefaultAutoSyncMsgStrategy : public nsIAutoSyncMsgStrategy
 {
   static const PRUint32 kFirstPassMessageSize = 60U*1024U; // 60K
@@ -158,87 +151,85 @@ class nsAutoSyncManager : public nsIObse
   static const PRTime kAutoSyncFreq = 60UL * (PR_USEC_PER_SEC * 60UL);  // 1hr
   static const PRUint32 kDefaultUpdateInterval = 10UL;                  // 10min
   static const PRInt32 kTimerIntervalInMs = 400;
   static const PRUint32 kNumberOfHeadersToProcess = 250U;
   // enforced size of the first group that will be downloaded before idle time
   static const PRUint32 kFirstGroupSizeLimit = 60U*1024U /* 60K */; 
   static const PRInt32 kIdleTimeInSec = 10;
   static const PRUint32 kGroupRetryCount = 3;
-
+  
   enum IdleState { systemIdle, appIdle, notIdle };
   enum UpdateState { initiated, completed };
       
   public:
     NS_DECL_ISUPPORTS
     NS_DECL_NSIOBSERVER
     NS_DECL_NSIURLLISTENER
     NS_DECL_NSIAUTOSYNCMANAGER
 
     nsAutoSyncManager();
-
+    
   private:
     ~nsAutoSyncManager();
 
-    void SetIdleState(IdleState st);
+    void SetIdleState(IdleState st);    
     IdleState GetIdleState() const;
     nsresult StartIdleProcessing();
     nsresult AutoUpdateFolders(); 
     void ScheduleFolderForOfflineDownload(nsIAutoSyncState *aAutoSyncStateObj);
     nsresult DownloadMessagesForOffline(nsIAutoSyncState *aAutoSyncStateObj, PRUint32 aSizeLimit = 0);
     nsresult HandleDownloadErrorFor(nsIAutoSyncState *aAutoSyncStateObj, const nsresult error);
-
+    
     // Helper methods for priority Q operations
     static
-    void ChainFoldersInQ(const nsCOMArray<nsIAutoSyncState> &aQueue,
+    void ChainFoldersInQ(const nsCOMArray<nsIAutoSyncState> &aQueue, 
                           nsCOMArray<nsIAutoSyncState> &aChainedQ);
     static
-    nsIAutoSyncState* SearchQForSibling(const nsCOMArray<nsIAutoSyncState> &aQueue,
+    nsIAutoSyncState* SearchQForSibling(const nsCOMArray<nsIAutoSyncState> &aQueue, 
                           nsIAutoSyncState *aAutoSyncStateObj, PRInt32 aStartIdx, PRInt32 *aIndex = nsnull);
     static
     PRBool DoesQContainAnySiblingOf(const nsCOMArray<nsIAutoSyncState> &aQueue, 
                           nsIAutoSyncState *aAutoSyncStateObj, const PRInt32 aState, 
                           PRInt32 *aIndex = nsnull);
     static 
     nsIAutoSyncState* GetNextSibling(const nsCOMArray<nsIAutoSyncState> &aQueue, 
                           nsIAutoSyncState *aAutoSyncStateObj, PRInt32 *aIndex = nsnull);
     static 
     nsIAutoSyncState* GetHighestPrioSibling(const nsCOMArray<nsIAutoSyncState> &aQueue, 
                           nsIAutoSyncState *aAutoSyncStateObj, PRInt32 *aIndex = nsnull);
-
+    
     /// timer to process existing keys and updates
     void InitTimer();
     static void TimerCallback(nsITimer *aTimer, void *aClosure);
     void StopTimer();
     void StartTimerIfNeeded();
-
+    
     /// pref helpers
     PRUint32 GetUpdateIntervalFor(nsIAutoSyncState *aAutoSyncStateObj);
-
+    
   protected:
     nsCOMPtr<nsIAutoSyncMsgStrategy> mMsgStrategyImpl;
     nsCOMPtr<nsIAutoSyncFolderStrategy> mFolderStrategyImpl;
     // contains the folders that will be downloaded on background
     nsCOMArray<nsIAutoSyncState> mPriorityQ;
-    // contains the folders that will be examined for existing headers and
-    // adds the headers we don't have offline into the autosyncState
-    // object's download queue.
+    // contains the folders that will be examined for existing headers
     nsCOMArray<nsIAutoSyncState> mDiscoveryQ;
-    // contains the folders that will be checked for new messages with STATUS,
-    // and if there are any, we'll call UpdateFolder on them.
+    // contains the folders that will be updated in order
+    // (see nsImapMailFolder::UpdateFolder for update operation)
     nsCOMArray<nsIAutoSyncState> mUpdateQ;
     // this is the update state for the current folder.
     UpdateState mUpdateState;
-
+    
     // This is set if auto sync has been completely paused.
     PRBool mPaused;
     // This is set if we've finished startup and should start
     // paying attention to idle notifications.
     PRBool mStartupDone;
-
+   
   private:
     PRUint32 mGroupSize;
     IdleState mIdleState;
     PRInt32 mDownloadModel;
     nsCOMPtr<nsIIdleService> mIdleService;
     nsCOMPtr<nsITimer> mTimer;
     nsTObserverArray<nsCOMPtr<nsIAutoSyncMgrListener> > mListeners;
 };
--- a/mailnews/imap/src/nsAutoSyncState.cpp
+++ b/mailnews/imap/src/nsAutoSyncState.cpp
@@ -171,17 +171,17 @@ nsresult nsAutoSyncState::PlaceIntoDownl
           if (NS_SUCCEEDED(rv) && !excluded)
           {
             mIsDownloadQChanged = PR_TRUE;
             mDownloadQ.AppendElement(aMsgKeyList[idx]);
           }
         }
       }
     }//endfor
-
+    
     if (mIsDownloadQChanged)
     {
       LogOwnerFolderName("Download Q is created for ");
       LogQWithSize(mDownloadQ, 0);
       rv = autoSyncMgr->OnDownloadQChanged(this);
     }
     
   }
@@ -350,109 +350,95 @@ NS_IMETHODIMP nsAutoSyncState::GetNextGr
 }
 
 /**
  * Usually called by nsAutoSyncManager when the last sync time is expired.
  */
 NS_IMETHODIMP nsAutoSyncState::ProcessExistingHeaders(PRUint32 aNumOfHdrsToProcess, PRUint32 *aLeftToProcess)
 {
   NS_ENSURE_ARG_POINTER(aLeftToProcess);
-
+  
   nsresult rv;
   nsCOMPtr <nsIMsgFolder> folder = do_QueryReferent(mOwnerFolder, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
-
+  
   nsCOMPtr<nsIMsgDatabase> database;
   rv = folder->GetMsgDatabase(getter_AddRefs(database));
   if (!database)
     return NS_ERROR_FAILURE;
-
+  
   // create a queue to process existing headers for the first time
   if (mExistingHeadersQ.IsEmpty())
   {
     rv = database->ListAllKeys(mExistingHeadersQ);
     NS_ENSURE_SUCCESS(rv, rv);
     mProcessPointer = 0;
   }
-
+  
   // process the existing headers and find the messages not downloaded yet
   PRUint32 lastIdx = mProcessPointer;
   nsTArray<nsMsgKey> msgKeys;
   PRUint32 keyCount = mExistingHeadersQ.Length();
   for (; mProcessPointer < (lastIdx + aNumOfHdrsToProcess) && mProcessPointer < keyCount; mProcessPointer++)
   {
     nsCOMPtr<nsIMsgDBHdr> hdr;
     rv = database->GetMsgHdrForKey(mExistingHeadersQ[mProcessPointer], getter_AddRefs(hdr));
     if (hdr)
     {
       PRUint32 msgFlags = 0;
       hdr->GetFlags(&msgFlags);
-
+      
       if (!(msgFlags & nsMsgMessageFlags::Offline))
         msgKeys.AppendElement(mExistingHeadersQ[mProcessPointer]);
     }
   }
   if (!msgKeys.IsEmpty())
   {
     nsCString folderName;
     folder->GetURI(folderName);
     PR_LOG(gAutoSyncLog, PR_LOG_DEBUG,
           ("%d messages will be added into the download q of folder %s\n",
             msgKeys.Length(), folderName.get()));
 
     rv = PlaceIntoDownloadQ(msgKeys);
     if (NS_FAILED(rv))
       mProcessPointer = lastIdx;
   }
-
+      
   *aLeftToProcess = keyCount - mProcessPointer;
-
+    
   // cleanup if we are done processing
   if (0 == *aLeftToProcess)
   {
     mLastSyncTime = PR_Now();
     mExistingHeadersQ.Clear();
     mProcessPointer = 0;
     folder->SetMsgDatabase(nsnull);
   }
-
+  
   return rv;
 }
 
-void nsAutoSyncState::OnNewHeaderFetchCompleted(const nsTArray<nsMsgKey> &aMsgKeyList)
-{
-  SetLastUpdateTime(PR_Now());
-  if (!aMsgKeyList.IsEmpty())
-    PlaceIntoDownloadQ(aMsgKeyList);
-}
-
-NS_IMETHODIMP nsAutoSyncState::UpdateFolder()
+nsresult nsAutoSyncState::OnNewHeaderFetchCompleted(const nsTArray<nsMsgKey> &aMsgKeyList)
 {
-  nsresult rv;
-  nsCOMPtr<nsIAutoSyncManager> autoSyncMgr = do_GetService(NS_AUTOSYNCMANAGER_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
-  nsCOMPtr<nsIUrlListener> autoSyncMgrListener = do_QueryInterface(autoSyncMgr, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
-  nsCOMPtr <nsIMsgImapMailFolder> imapFolder = do_QueryReferent(mOwnerFolder, &rv);
-  SetState(nsAutoSyncState::stUpdateIssued);
-  return imapFolder->UpdateFolderWithListener(nsnull, autoSyncMgrListener);
+  return PlaceIntoDownloadQ(aMsgKeyList);
 }
 
 NS_IMETHODIMP nsAutoSyncState::OnStartRunningUrl(nsIURI* aUrl)
 {
   nsresult rv = NS_OK;
     
   // if there is a problem to start the download, set rv with the
   // corresponding error code. In that case, AutoSyncManager is going to
   // set the autosync state to nsAutoSyncState::stReadyToDownload
   // to resume downloading another time
   
   // TODO: is there a way to make sure that download started without
   // problem through nsIURI interface?
-
+   
   nsCOMPtr<nsIAutoSyncManager> autoSyncMgr = do_GetService(NS_AUTOSYNCMANAGER_CONTRACTID, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
   
   return autoSyncMgr->OnDownloadStarted(this, rv);
 }
 
 NS_IMETHODIMP nsAutoSyncState::OnStopRunningUrl(nsIURI* aUrl, nsresult aExitCode)
 {
@@ -479,17 +465,17 @@ NS_IMETHODIMP nsAutoSyncState::OnStopRun
       nsCString folderName;
       ownerFolder->GetURI(folderName);
       PR_LOG(gAutoSyncLog, PR_LOG_DEBUG,
              ("folder %s status changed serverNextUID = %lx lastNextUID = %lx\n", folderName.get(),
               serverNextUID, mLastNextUID));
       PR_LOG(gAutoSyncLog, PR_LOG_DEBUG,
              ("serverTotal = %lx lastServerTotal = %lx serverRecent = %lx lastServerRecent = %lx\n",
               serverTotal, mLastServerTotal, serverRecent, mLastServerRecent));
-      SetServerCounts(serverTotal, serverRecent, serverUnseen, serverNextUID);
+
       SetState(nsAutoSyncState::stUpdateIssued);
       return imapFolder->UpdateFolderWithListener(nsnull, autoSyncMgrListener);
     }
     else
     {
       ownerFolder->SetMsgDatabase(nsnull);
       // nothing more to do.
       SetState(nsAutoSyncState::stCompletedIdle);
--- a/mailnews/imap/src/nsAutoSyncState.h
+++ b/mailnews/imap/src/nsAutoSyncState.h
@@ -76,29 +76,29 @@ class MsgStrategyComparatorAdaptor
 };
 
 
 /**
  * Facilitates auto-sync capabilities for imap folders.
  */
 class nsAutoSyncState : public nsIAutoSyncState, public nsIUrlListener
 {
-public:
+ public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIAUTOSYNCSTATE
   NS_DECL_NSIURLLISTENER
-
+  
   nsAutoSyncState(nsImapMailFolder *aOwnerFolder, PRTime aLastSyncTime = 0UL);
-
-  /// Called by owner folder when new headers are fetched from the server
-  void OnNewHeaderFetchCompleted(const nsTArray<nsMsgKey> &aMsgKeyList);
+  
+  /// Called by owner folder when new headers are fetched form the server
+  nsresult OnNewHeaderFetchCompleted(const nsTArray<nsMsgKey> &aMsgKeyList);
 
   /// Sets the last sync time in lower precision (seconds)
   void SetLastSyncTimeInSec(PRInt32 aLastSyncTime);
-
+  
   /// Manages storage space for auto-sync operations 
   nsresult ManageStorageSpace();
 
   void SetServerCounts(PRInt32 total, PRInt32 recent, PRInt32 unseen,
                        PRInt32 nextUID);
 
  private:
   ~nsAutoSyncState();
--- a/mailnews/imap/src/nsImapMailFolder.cpp
+++ b/mailnews/imap/src/nsImapMailFolder.cpp
@@ -2031,17 +2031,17 @@ NS_IMETHODIMP nsImapMailFolder::ReadFrom
   element->GetInt32Property("aclFlags", (PRInt32 *) &m_aclFlags);
   element->GetInt32Property("serverTotal", &m_numServerTotalMessages);
   element->GetInt32Property("serverUnseen", &m_numServerUnseenMessages);
   element->GetInt32Property("serverRecent", &m_numServerRecentMessages);
   element->GetInt32Property("nextUID", &m_nextUID);
   PRInt32 lastSyncTimeInSec;
   if ( NS_FAILED(element->GetInt32Property("lastSyncTimeInSec", (PRInt32 *) &lastSyncTimeInSec)) )
     lastSyncTimeInSec = 0U;
-
+  
   // make sure that auto-sync state object is created
   InitAutoSyncState();
   m_autoSyncStateObj->SetLastSyncTimeInSec(lastSyncTimeInSec);
   
   return rv;
 }
 
 NS_IMETHODIMP nsImapMailFolder::WriteToFolderCacheElem(nsIMsgFolderCacheElement *element)
@@ -2935,19 +2935,16 @@ NS_IMETHODIMP nsImapMailFolder::UpdateIm
       SetNumNewMessages(unreadDelta);
       SetBiffState(nsMsgBiffState_NewMail);
     }
     summaryChanged = PR_TRUE;
   }
   SetPerformingBiff(PR_FALSE);
   if (m_numServerUnseenMessages != numUnread || m_numServerTotalMessages != numTotal)
   {
-    if (numUnread > m_numServerUnseenMessages ||
-        m_numServerTotalMessages > numTotal)
-      NotifyHasPendingMsgs();
     summaryChanged = PR_TRUE;
     m_numServerUnseenMessages = numUnread;
     m_numServerTotalMessages = numTotal;
   }
   if (summaryChanged)
     SummaryChanged();
 
   return NS_OK;
@@ -5419,17 +5416,17 @@ nsImapMailFolder::OnStopRunningUrl(nsIUR
   }
   return rv;
 }
 
 void nsImapMailFolder::UpdatePendingCounts()
 {
   if (m_copyState)
   {
-    ChangePendingTotal(m_copyState->m_isCrossServerOp ? 1 : m_copyState->m_totalCount);
+    ChangeNumPendingTotalMessages(m_copyState->m_isCrossServerOp ? 1 : m_copyState->m_totalCount);
 
     // count the moves that were unread
     int numUnread = m_copyState->m_unreadCount;
     if (numUnread)
     {
       m_numServerUnseenMessages += numUnread; // adjust last status count by this delta.
       ChangeNumPendingUnread(numUnread);
     }
@@ -5672,35 +5669,34 @@ nsImapMailFolder::HeaderFetchCompleted(n
         autoDownloadNewHeaders = PR_TRUE;
     }
     PRBool notifiedBodies = PR_FALSE;
     if (m_downloadingFolderForOfflineUse || autoSyncOfflineStores ||
         autoDownloadNewHeaders)
     {
       nsTArray<nsMsgKey> keysToDownload;
       GetBodysToDownload(&keysToDownload);
-      // this is the case when DownloadAllForOffline is called.
-      if (!keysToDownload.IsEmpty() && (m_downloadingFolderForOfflineUse ||
-                                        autoDownloadNewHeaders))
-      {
-        notifiedBodies = PR_TRUE;
-        aProtocol->NotifyBodysToDownload(keysToDownload.Elements(), keysToDownload.Length());
-      }
-      else
+      if (!keysToDownload.IsEmpty())
       {
-        // create auto-sync state object lazily
-        InitAutoSyncState();
-
-        // make enough room for new downloads
-        m_autoSyncStateObj->ManageStorageSpace();
-        m_autoSyncStateObj->SetServerCounts(m_numServerTotalMessages,
-                                            m_numServerRecentMessages,
-                                            m_numServerUnseenMessages,
-                                            m_nextUID);
-        m_autoSyncStateObj->OnNewHeaderFetchCompleted(keysToDownload);
+        // this is the case when DownloadAllForOffline is called.
+        if (m_downloadingFolderForOfflineUse || autoDownloadNewHeaders)
+        {
+          notifiedBodies = PR_TRUE;
+          aProtocol->NotifyBodysToDownload(keysToDownload.Elements(), keysToDownload.Length());
+        }
+        else
+        {
+          // create auto-sync state object lazily
+          InitAutoSyncState();
+        
+          // make enough room for new downloads
+          m_autoSyncStateObj->ManageStorageSpace();
+          
+          m_autoSyncStateObj->OnNewHeaderFetchCompleted(keysToDownload);
+        }
       }
     }
     if (!notifiedBodies)
       aProtocol->NotifyBodysToDownload(nsnull, 0/*keysToFetch.Length() */);
    
     nsCOMPtr <nsIURI> runningUri;
     aProtocol->GetRunningUrl(getter_AddRefs(runningUri));
     if (runningUri)
@@ -9051,30 +9047,19 @@ NS_IMETHODIMP nsImapMailFolder::GetCusto
     }
   }
   return nsMsgDBFolder::GetCustomIdentity(aIdentity);
 }
 
 NS_IMETHODIMP nsImapMailFolder::ChangePendingTotal(PRInt32 aDelta)
 {
   ChangeNumPendingTotalMessages(aDelta);
-  if (aDelta > 0)
-    NotifyHasPendingMsgs();
   return NS_OK;
 }
 
-void nsImapMailFolder::NotifyHasPendingMsgs()
-{
-  InitAutoSyncState();
-  nsresult rv;
-  nsCOMPtr<nsIAutoSyncManager> autoSyncMgr = do_GetService(NS_AUTOSYNCMANAGER_CONTRACTID, &rv);
-  if (NS_SUCCEEDED(rv)) 
-    autoSyncMgr->OnFolderHasPendingMsgs(m_autoSyncStateObj);
-}
-
 /* void changePendingUnread (in long aDelta); */
 NS_IMETHODIMP nsImapMailFolder::ChangePendingUnread(PRInt32 aDelta)
 {
   ChangeNumPendingUnread(aDelta);
   return NS_OK;
 }
 
 NS_IMETHODIMP nsImapMailFolder::GetServerRecent(PRInt32 *aServerRecent)
@@ -9103,20 +9088,20 @@ NS_IMETHODIMP nsImapMailFolder::GetServe
   NS_ENSURE_ARG_POINTER(aNextUID);
   *aNextUID = m_nextUID;
   return NS_OK;
 }
 
 NS_IMETHODIMP nsImapMailFolder::GetAutoSyncStateObj(nsIAutoSyncState **autoSyncStateObj)
 {
   NS_ENSURE_ARG_POINTER(autoSyncStateObj);
-
+  
   // create auto-sync state object lazily
   InitAutoSyncState();
-
+    
   NS_IF_ADDREF(*autoSyncStateObj = m_autoSyncStateObj);
   return NS_OK;
 }
 
 NS_IMETHODIMP nsImapMailFolder::InitiateAutoSync(nsIUrlListener *aUrlListener)
 {
   nsCString folderName;
   GetURI(folderName);
@@ -9139,34 +9124,22 @@ NS_IMETHODIMP nsImapMailFolder::Initiate
   InitAutoSyncState();
 
   // make sure we get the counts from the folder cache.
   ReadDBFolderInfo(PR_FALSE);
 
   nsresult rv = m_autoSyncStateObj->ManageStorageSpace();
   NS_ENSURE_SUCCESS(rv, rv);
 
-  PRInt32 syncState;
-  m_autoSyncStateObj->GetState(&syncState);
-  if (syncState == nsAutoSyncState::stUpdateNeeded)
-    return m_autoSyncStateObj->UpdateFolder();
-
-  // We only want to init the autosyncStateObj server counts the first time
-  // we update, and update it when the STATUS call finishes. This deals with
-  // the case where biff is doing a STATUS on a non-inbox folder, which
-  // can make autosync think the counts aren't changing.
-  PRTime lastUpdateTime;
-  m_autoSyncStateObj->GetLastUpdateTime(&lastUpdateTime);
-  if (!lastUpdateTime)
-    m_autoSyncStateObj->SetServerCounts(m_numServerTotalMessages,
-                                        m_numServerRecentMessages,
-                                        m_numServerUnseenMessages,
-                                        m_nextUID);
   // Issue a STATUS command and see if any counts changed.
   m_autoSyncStateObj->SetState(nsAutoSyncState::stStatusIssued);
+  m_autoSyncStateObj->SetServerCounts(m_numServerTotalMessages,
+                                      m_numServerRecentMessages,
+                                      m_numServerUnseenMessages,
+                                      m_nextUID);
   // The OnStopRunningUrl method of the autosync state obj
   // will check if the counts or next uid have changed,
   // and if so, will issue an UpdateFolder().
   rv = UpdateStatus(m_autoSyncStateObj, nsnull);
   NS_ENSURE_SUCCESS(rv, rv);
   
   // record the last update time
   m_autoSyncStateObj->SetLastUpdateTime(PR_Now());
--- a/mailnews/imap/src/nsImapMailFolder.h
+++ b/mailnews/imap/src/nsImapMailFolder.h
@@ -374,19 +374,16 @@ protected:
   nsresult SetupHeaderParseStream(PRUint32 size, const nsACString& content_type, nsIMailboxSpec *boxSpec);
   nsresult  ParseAdoptedHeaderLine(const char *messageLine, PRUint32 msgKey);
   nsresult  NormalEndHeaderParseStream(nsIImapProtocol *aProtocol, nsIImapUrl *imapUrl);
 
   void EndOfflineDownload();
 
   nsresult MarkMessagesImapDeleted(nsTArray<nsMsgKey> *keyArray, PRBool deleted, nsIMsgDatabase *db);
 
-  // Notifies imap autosync that it should update this folder when it
-  // gets a chance.
-  void NotifyHasPendingMsgs();
   void UpdatePendingCounts();
   void SetIMAPDeletedFlag(nsIMsgDatabase *mailDB, const nsTArray<nsMsgKey> &msgids, PRBool markDeleted);
   virtual PRBool ShowDeletedMessages();
   virtual PRBool DeleteIsMoveToTrash();
   nsresult GetFolder(const nsACString& name, nsIMsgFolder **pFolder);
   nsresult GetTrashFolder(nsIMsgFolder **pTrashFolder);
   PRBool TrashOrDescendentOfTrash(nsIMsgFolder* folder);
   nsresult GetServerKey(nsACString& serverKey);