1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef nsAutoSyncState_h__
6 #define nsAutoSyncState_h__
7 
8 #include "MailNewsTypes.h"
9 #include "nsIAutoSyncState.h"
10 #include "nsIUrlListener.h"
11 #include "nsIWeakReferenceUtils.h"
12 #include "nsTHashtable.h"
13 #include "nsHashKeys.h"
14 #include "nsTArray.h"
15 #include "prlog.h"
16 
17 class nsImapMailFolder;
18 class nsIAutoSyncMsgStrategy;
19 class nsIMsgDatabase;
20 
21 /**
22  * An adaptor class to make msg strategy nsTArray.Sort()
23  * compatible.
24  */
25 class MsgStrategyComparatorAdaptor {
26  public:
27   MsgStrategyComparatorAdaptor(nsIAutoSyncMsgStrategy* aStrategy,
28                                nsIMsgFolder* aFolder,
29                                nsIMsgDatabase* aDatabase);
30 
31   /** @return True if the elements are equals; false otherwise. */
32   bool Equals(const nsMsgKey& a, const nsMsgKey& b) const;
33 
34   /** @return True if (a < b); false otherwise. */
35   bool LessThan(const nsMsgKey& a, const nsMsgKey& b) const;
36 
37  private:
38   MsgStrategyComparatorAdaptor();
39 
40  private:
41   nsIAutoSyncMsgStrategy* mStrategy;
42   nsIMsgFolder* mFolder;
43   nsIMsgDatabase* mDatabase;
44 };
45 
46 /**
47  * Facilitates auto-sync capabilities for imap folders.
48  */
49 class nsAutoSyncState final : public nsIAutoSyncState, public nsIUrlListener {
50  public:
51   NS_DECL_ISUPPORTS
52   NS_DECL_NSIAUTOSYNCSTATE
53   NS_DECL_NSIURLLISTENER
54 
55   explicit nsAutoSyncState(nsImapMailFolder* aOwnerFolder,
56                            PRTime aLastSyncTime = 0UL);
57 
58   /// Called by owner folder when new headers are fetched from the server
59   void OnNewHeaderFetchCompleted(const nsTArray<nsMsgKey>& aMsgKeyList);
60 
61   /// Sets the last sync time in lower precision (seconds)
62   void SetLastSyncTimeInSec(int32_t aLastSyncTime);
63 
64   /// Manages storage space for auto-sync operations
65   nsresult ManageStorageSpace();
66 
67   void SetServerCounts(int32_t total, int32_t recent, int32_t unseen,
68                        int32_t nextUID);
69 
70  private:
71   ~nsAutoSyncState();
72 
73   nsresult PlaceIntoDownloadQ(const nsTArray<nsMsgKey>& aMsgKeyList);
74   nsresult SortQueueBasedOnStrategy(nsTArray<nsMsgKey>& aQueue);
75   nsresult SortSubQueueBasedOnStrategy(nsTArray<nsMsgKey>& aQueue,
76                                        uint32_t aStartingOffset);
77 
78   void LogOwnerFolderName(const char* s);
79   void LogQWithSize(nsTArray<nsMsgKey>& q, uint32_t toOffset = 0);
80   void LogQWithSize(nsTArray<RefPtr<nsIMsgDBHdr>> const& q,
81                     uint32_t toOffset = 0);
82 
83  private:
84   int32_t mSyncState;
85   nsWeakPtr mOwnerFolder;
86   uint32_t mOffset;
87   uint32_t mLastOffset;
88 
89   // used to tell if the Server counts have changed.
90   int32_t mLastServerTotal;
91   int32_t mLastServerRecent;
92   int32_t mLastServerUnseen;
93   int32_t mLastNextUID;
94 
95   PRTime mLastSyncTime;
96   PRTime mLastUpdateTime;
97   uint32_t mProcessPointer;
98   bool mIsDownloadQChanged;
99   uint32_t mRetryCounter;
100   nsTHashtable<nsUint32HashKey> mDownloadSet;
101   nsTArray<nsMsgKey> mDownloadQ;
102   nsTArray<nsMsgKey> mExistingHeadersQ;
103 };
104 
105 #endif
106