|
1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #ifndef __CONSYNC_H__ |
|
17 #define __CONSYNC_H__ |
|
18 |
|
19 #include <e32base.h> |
|
20 #include <msvapi.h> |
|
21 |
|
22 #include "ImapConnectCompletionStates.H" |
|
23 |
|
24 class CClientMtmRegistry; |
|
25 class CBaseMtm; |
|
26 class CMtmUiRegistry; |
|
27 class CBaseMtmUi; |
|
28 class CImapConnectAndSyncOp; |
|
29 class MMsvImapConnectionObserver; |
|
30 |
|
31 |
|
32 class CProgressTimer : public CTimer |
|
33 /** |
|
34 @internalComponent |
|
35 @released |
|
36 */ |
|
37 { |
|
38 public: |
|
39 static CProgressTimer* NewL(CImapConnectAndSyncOp& aOperation); |
|
40 |
|
41 private: // methods from CActive, via CTimer |
|
42 virtual void RunL(); |
|
43 virtual TInt RunError(TInt /*aError*/); |
|
44 private: |
|
45 CProgressTimer(CImapConnectAndSyncOp& aOperation); |
|
46 private: |
|
47 CImapConnectAndSyncOp& iOperation; |
|
48 |
|
49 }; |
|
50 |
|
51 class CRefreshTimer : public CTimer |
|
52 { |
|
53 public: |
|
54 static CRefreshTimer* NewL(CImapConnectAndSyncOp& aOperation); |
|
55 private: // methods from CActive, via CTimer |
|
56 virtual void RunL(); |
|
57 virtual TInt RunError(TInt /*aError*/); |
|
58 private: |
|
59 CRefreshTimer(CImapConnectAndSyncOp& aOperation); |
|
60 private: |
|
61 CImapConnectAndSyncOp& iOperation; |
|
62 }; |
|
63 |
|
64 |
|
65 class CImapConnectAndSyncOp : public CMsvOperation, public MMsvEntryObserver |
|
66 /** |
|
67 The CImapConnectAndSyncOp is an operation handles connection and |
|
68 synchronisation to an IMAP account. |
|
69 |
|
70 The operation has three different completion stages. After connection has |
|
71 occurred with the IMAP server, after the full sync has completed, or after |
|
72 disconnecting from the IMAP server. With the last option the operation does |
|
73 intermitent refreshes until connection timeout is exceeded. The sync is done |
|
74 as a background task. |
|
75 |
|
76 The refresh rate is set by the CImImap4Settings class. It is obtained from |
|
77 the settings on creation of the object and that will persist for the lifetime |
|
78 of the object. The timeout value is provided by iMtmData1 component of the |
|
79 IMAP service entry index. |
|
80 |
|
81 NOTE - this value is obtained from the commdb by the server MTM. |
|
82 |
|
83 This class contains two timers - a refresh and a progress timer. The refresh |
|
84 timer is used to refresh the inbox as described earlier. The progress timer |
|
85 has two roles. Whilst the sync takes place the progress timer is fired every |
|
86 KImapDefaultProgressRate microseconds - this is currently set to approx 1sec. |
|
87 When fired it calls the ProgressL() function. This updates the state and the |
|
88 operation observer as necessary. Once the sync has completed the progress |
|
89 timer acts as the timeout timer. If the timeout value is zero or negative |
|
90 then no timeout actually occurs even though the timer is used. |
|
91 |
|
92 This class sets itself as an observer to the store entry for the IMAP service. |
|
93 It uses the notification of changes to the entry to push the state machine |
|
94 through the sync states. These sync states are concerned with the first |
|
95 sync to the IMAP acconut and NOT any subsequent inbox refresh syncs. The |
|
96 first sync is broken into three stages - syncing the inbox then the folder |
|
97 list and finally the folders themselves. |
|
98 |
|
99 The state machine starts in the NotStarted state. Here it invokes an async |
|
100 command on the server MTM - KIMAP4MTMConnectAndSynchronise. The class sets |
|
101 itself active and moves to the Connecting state. |
|
102 |
|
103 In the Connecting state, the RunL() checks the completion criteria. If |
|
104 completion is when the connection to the IMAP server is established then the |
|
105 observer is notified (completed) and the operation ends. The sync continues |
|
106 in the background. If the completion criteria is for the other options then |
|
107 the state moves to the FirstSyncingUpdatingInbox state. The progress timer is |
|
108 reset. |
|
109 |
|
110 NOTE - the class is SetActive() even though no async request has been made. |
|
111 In order for the scheduler not to call its RunL() the iStatus is set to |
|
112 KRequestPending. This is done in order to make the operation appear as if it |
|
113 is not complete. Therefore care must be taken when changing code not to call |
|
114 SetActive() again. |
|
115 |
|
116 During the first sync to the IMAP account the state machine is driven by |
|
117 calls to ProgressL(). This may be either by the progress timer or by the |
|
118 owner/observer of this operation. |
|
119 |
|
120 In state FirstSyncingUpdatingInbox the sync progress is obtained from the |
|
121 server MTM. If the progress has moved on from ESyncInbox then the state is |
|
122 changed to FirstSyncingUpdatingFolderList. The observer is notified and the |
|
123 state machine moves onto handling this next state. |
|
124 |
|
125 In state FirstSyncingUpdatingFolderList the sync progress is again obtained |
|
126 from the server MTM. If the progress has moved on from ESyncFolderTree then |
|
127 the state is changed to FirstSyncingUpdatingFolders. The observer is notified |
|
128 and the state machine moves onto handling this next state. |
|
129 |
|
130 In state FirstSyncingUpdatingFolders the sync progress is again obtained |
|
131 from the server MTM. If the progress is not Idle then nothing happens. |
|
132 Otherwise if the completion criteria is for after sync with the IMAP server |
|
133 then the state moves to CompletingSelf and the active object self-completes. |
|
134 This ensures that its RunL() is called asap. If the completion criteria is |
|
135 for when disconnection from the IMAP server occurs then the state moves to |
|
136 Waiting and the refresh timer is started and the observer is notified. |
|
137 |
|
138 The behaviour in the Waiting state depends on which function has been called. |
|
139 In ProgressL() the progress timer changes its role to the idle timeoout timer. |
|
140 If the timeout time has not been set then it is set, although if the interval |
|
141 is zero or negative it cannot be set. If the timeout time has been set then |
|
142 a check is made to see if the current time has passed it. If so the class |
|
143 invokes the async function KIMAP4MTMDisconnect on the server MTM and the state |
|
144 moves to DisconnectingOnTimeout. The observer is notified. If the current time |
|
145 has not exceeded the timeout time or the timeout time has been set then the |
|
146 progress timer is started - the value depending on whether a timeout time |
|
147 exists or not. |
|
148 |
|
149 When the refresh timer fires the DoRefreshInboxL() function is called. The state |
|
150 will be Waiting. If the server MTM is active then the refresh timer is re-started. |
|
151 If the server MTM is not active then the async function KIMAP4MTMInboxNewSync |
|
152 is called and the state moves to the ForcedSyncing state. |
|
153 |
|
154 In the ForcedSyncing state the RunL() re-starts the refresh timer and the |
|
155 state is moved back to Waiting. |
|
156 |
|
157 NOTE - the active object is SetActive() again as described before. |
|
158 |
|
159 In the DisconnectingOnTimeout state the RunL() completes the observer. The |
|
160 operation is finished and the final progress value is set to KErrTimeOut. |
|
161 |
|
162 In the CompletingSelf state the RunL() completes the observer. The operation |
|
163 is finished. |
|
164 |
|
165 @internalComponent |
|
166 @released |
|
167 */ |
|
168 { |
|
169 public: |
|
170 static CImapConnectAndSyncOp* NewL( |
|
171 CMsvSession& aSession, |
|
172 const CMsvEntrySelection& aSelection, |
|
173 CBaseMtm& aBaseMtm, |
|
174 TInt aPriority, |
|
175 TRequestStatus& aStatus, |
|
176 TImapConnectionCompletionState aCompletionState, |
|
177 MMsvImapConnectionObserver* aConnectionObserver=NULL |
|
178 ); |
|
179 |
|
180 virtual ~CImapConnectAndSyncOp(); |
|
181 |
|
182 void DoRefreshInboxL(); |
|
183 |
|
184 public: // methods from CMsvOperation |
|
185 |
|
186 virtual const TDesC8& ProgressL(); |
|
187 |
|
188 public: // methods from MMsvEntryObserver |
|
189 |
|
190 void HandleEntryEventL(TMsvEntryEvent aEvent, TAny* /*aArg1*/, TAny* /*aArg2*/, TAny* /*aArg3*/); |
|
191 |
|
192 private: // methods from CActive |
|
193 |
|
194 virtual void RunL(); |
|
195 virtual void DoCancel(); |
|
196 virtual TInt RunError(TInt aError); |
|
197 |
|
198 private: |
|
199 CImapConnectAndSyncOp( |
|
200 CMsvSession& aSession, |
|
201 const CMsvEntrySelection& aSelection, |
|
202 CBaseMtm& aBaseMtm, |
|
203 TInt aPriority, |
|
204 TRequestStatus& aStatus, |
|
205 TImapConnectionCompletionState aCompletionState, |
|
206 MMsvImapConnectionObserver* aConnectionObserver |
|
207 ); |
|
208 void ConstructL(const CMsvEntrySelection& aSelection); |
|
209 |
|
210 void Completed(TInt aError); |
|
211 TInt GetServiceProgress(); |
|
212 void UpdateObserver() const; |
|
213 void ResetProgressTimer(); |
|
214 void ResetRefreshTimer(); |
|
215 |
|
216 private: |
|
217 enum TImapConnState |
|
218 { |
|
219 ENotStarted=0, |
|
220 EConnecting, |
|
221 EFirstSyncingUpdatingInbox, |
|
222 EFirstSyncingUpdatingFolderList, |
|
223 EFirstSyncingUpdatingFolders, |
|
224 EWaiting, |
|
225 EForcedSyncing, |
|
226 EDisconnectingOnTimeout, |
|
227 ECompletingSelf, |
|
228 ECompleted |
|
229 }; |
|
230 |
|
231 TImapConnState iState; |
|
232 CBaseMtm& iBaseMtm; |
|
233 CMsvOperation* iOperation; |
|
234 CMsvEntrySelection* iSelection; |
|
235 TPckgBuf<TImap4CompoundProgress> iProgress; |
|
236 TPckgBuf<TImap4SyncProgress> iSyncProgress; |
|
237 CRefreshTimer* iRefreshTimer; |
|
238 CProgressTimer* iProgressTimer; |
|
239 MMsvImapConnectionObserver* iConnectionObserver; |
|
240 TBool iForcedCancel; |
|
241 TImapConnectionCompletionState iCompletionState; |
|
242 CMsvEntry* iServiceEntry; |
|
243 |
|
244 // Following member used for timeout and refresh functionality |
|
245 TBool iTimeout; |
|
246 TTime iTimeoutAt; |
|
247 TTimeIntervalSeconds iIdleTimeout; |
|
248 TTimeIntervalSeconds iRefreshRate; |
|
249 }; |
|
250 |
|
251 #endif // __CONSYNC_H__ |