|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: This file implements class CMessageIterator. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "emailmessage.h" |
|
19 #include "messageiterator.h" |
|
20 #include "emailapiutils.h" |
|
21 #include "cfsmailplugin.h" |
|
22 #include "emailapiutils.h" |
|
23 #include "emailclientapi.hrh" |
|
24 #include "mfsmailiterator.h" |
|
25 |
|
26 // number of messages in chunk to retrive from protocol plugin. Actual chunk |
|
27 // size is one less because last element is used for reference to next chunk |
|
28 // retrieval. See ReadNextChunkL() for details. |
|
29 const TInt KMessageChunkSize = 5; |
|
30 |
|
31 const TInt KUndefined = -1; |
|
32 |
|
33 // ----------------------------------------------------------------------------- |
|
34 // |
|
35 // ----------------------------------------------------------------------------- |
|
36 CMessageIterator* CMessageIterator::NewL( |
|
37 MFSMailIterator* aIterator, |
|
38 CPluginData& aPluginData, |
|
39 TUint aCount ) |
|
40 { |
|
41 CMessageIterator* iter = new ( ELeave ) |
|
42 CMessageIterator( aIterator, aPluginData, aCount ); |
|
43 return iter; |
|
44 } |
|
45 |
|
46 // ----------------------------------------------------------------------------- |
|
47 // |
|
48 // ----------------------------------------------------------------------------- |
|
49 CMessageIterator::~CMessageIterator() |
|
50 { |
|
51 iPluginData.ReleaseInstance(); |
|
52 TInt count = iMessageArray.Count(); |
|
53 iMessageArray.ResetAndDestroy(); |
|
54 for ( TInt i = count; i < iFsMessageArray.Count(); i++ ) |
|
55 { |
|
56 delete iFsMessageArray[i]; |
|
57 } |
|
58 iFsMessageArray.Reset(); |
|
59 delete iIterator; |
|
60 } |
|
61 |
|
62 // ----------------------------------------------------------------------------- |
|
63 // |
|
64 // ----------------------------------------------------------------------------- |
|
65 TEmailTypeId CMessageIterator::InterfaceId() const |
|
66 { |
|
67 return KEmailIFUidMessageIterator; |
|
68 } |
|
69 |
|
70 // ----------------------------------------------------------------------------- |
|
71 // |
|
72 // ----------------------------------------------------------------------------- |
|
73 void CMessageIterator::Release() |
|
74 { |
|
75 delete this; |
|
76 } |
|
77 |
|
78 // ----------------------------------------------------------------------------- |
|
79 // Returns next message |
|
80 // Messages are retrieved from protocol plugin in chunks of x messages and |
|
81 // when NextL has be called for x times, next chunk is retrieved. This |
|
82 // is trade-off between RAM consumption and speed. |
|
83 // ----------------------------------------------------------------------------- |
|
84 MEmailMessage* CMessageIterator::NextL() |
|
85 { |
|
86 if ( iCursor == KUndefined ) |
|
87 { |
|
88 // Buffer empty, read the first chunk |
|
89 if ( !ReadNextChunkL() ) |
|
90 { |
|
91 // No messages found, |
|
92 // Reset the cursor and return NULL |
|
93 iCursor = KUndefined; |
|
94 iStartMsgId = TFSMailMsgId(); |
|
95 return NULL; |
|
96 } |
|
97 // Items found, return the first item in the buffer |
|
98 iCursor = 0; |
|
99 } |
|
100 else |
|
101 { |
|
102 // Iterate to the next item |
|
103 iCursor++; |
|
104 } |
|
105 return ReadFromChunkL(); |
|
106 } |
|
107 |
|
108 // ----------------------------------------------------------------------------- |
|
109 // |
|
110 // ----------------------------------------------------------------------------- |
|
111 MEmailMessage* CMessageIterator::PreviousL() |
|
112 { |
|
113 if ( iCursor == KUndefined ) |
|
114 { |
|
115 iStartMsgId = TFSMailMsgId(); |
|
116 // Buffer empty, client should first call NextL |
|
117 return NULL; |
|
118 } |
|
119 // Iterate to the previous item |
|
120 iCursor--; |
|
121 return ReadFromChunkL(); |
|
122 } |
|
123 |
|
124 // ----------------------------------------------------------------------------- |
|
125 // |
|
126 // ----------------------------------------------------------------------------- |
|
127 TBool CMessageIterator::ReadNextChunkL() |
|
128 { |
|
129 User::LeaveIfNull( iIterator ); |
|
130 |
|
131 TBool chunkFound = EFalse; |
|
132 |
|
133 // Delete previous items if exist |
|
134 CleanCache(); |
|
135 iHasMoreNextItems = iIterator->NextL( |
|
136 iStartMsgId, |
|
137 KMessageChunkSize, |
|
138 iFsMessageArray ); |
|
139 if ( iFsMessageArray.Count() > 0 ) |
|
140 { |
|
141 chunkFound = ETrue; |
|
142 // Set the cursor to the first item |
|
143 iCursor = 0; |
|
144 iHasMorePrevItems = ETrue; |
|
145 } |
|
146 return chunkFound; |
|
147 } |
|
148 |
|
149 // ----------------------------------------------------------------------------- |
|
150 // |
|
151 // ----------------------------------------------------------------------------- |
|
152 TBool CMessageIterator::ReadPreviousChunkL() |
|
153 { |
|
154 User::LeaveIfNull( iIterator ); |
|
155 |
|
156 TBool chunkFound = EFalse; |
|
157 |
|
158 // Delete previous items if exist |
|
159 CleanCache(); |
|
160 RPointerArray<CFSMailMessage> tmp; |
|
161 iHasMorePrevItems = iIterator->PreviousL( |
|
162 iStartMsgId, |
|
163 KMessageChunkSize, |
|
164 tmp ); |
|
165 TInt count = tmp.Count(); |
|
166 if ( count > 0 ) |
|
167 { |
|
168 // It seems that items have been found |
|
169 // Set the cursor to the end of array |
|
170 iCursor = count - 1; |
|
171 iHasMoreNextItems = ETrue; |
|
172 chunkFound = ETrue; |
|
173 // Revise the order of the buffer. |
|
174 // Create complete cache, so it is easier to iterate, |
|
175 // as now we have to start from the end of cache |
|
176 for ( TInt i = count; i > 0; i-- ) |
|
177 { |
|
178 CFSMailMessage* msg = tmp[i-1]; |
|
179 iFsMessageArray.AppendL( msg ); |
|
180 AddToCacheL( msg ); |
|
181 } |
|
182 } |
|
183 |
|
184 return chunkFound; |
|
185 } |
|
186 |
|
187 // ----------------------------------------------------------------------------- |
|
188 // |
|
189 // ----------------------------------------------------------------------------- |
|
190 MEmailMessage* CMessageIterator::ReadFromChunkL() |
|
191 { |
|
192 if ( iCursor >= KMessageChunkSize ) |
|
193 { |
|
194 if ( iHasMoreNextItems ) |
|
195 { |
|
196 iStartMsgId = iFsMessageArray[iCursor-1]->GetMessageId(); |
|
197 if ( !ReadNextChunkL() ) |
|
198 { |
|
199 // No more items found, |
|
200 // set the cursor to the previous item, |
|
201 // and return NULL. |
|
202 iCursor--; |
|
203 return NULL; |
|
204 } |
|
205 } |
|
206 else |
|
207 { |
|
208 // No more items found, |
|
209 // set the cursor to the previous item, |
|
210 // and return NULL. |
|
211 iCursor--; |
|
212 return NULL; |
|
213 } |
|
214 } |
|
215 else if ( iCursor < 0 ) |
|
216 { |
|
217 iStartMsgId = iFsMessageArray[0]->GetMessageId(); |
|
218 if ( iHasMorePrevItems ) |
|
219 { |
|
220 if ( !ReadPreviousChunkL() ) |
|
221 { |
|
222 // Buffer is empty now, client should call nextL, |
|
223 // and start reading from the start of the buffer |
|
224 iCursor = KUndefined; |
|
225 return NULL; |
|
226 } |
|
227 } |
|
228 else |
|
229 { |
|
230 // Buffer is empty now, client should call nextL, |
|
231 // and start reading from the start of the buffer |
|
232 iCursor = KUndefined; |
|
233 return NULL; |
|
234 } |
|
235 } |
|
236 |
|
237 if ( iCursor < iFsMessageArray.Count() ) |
|
238 { |
|
239 /* There are items to read in the cache */ |
|
240 if ( iCursor >= iMessageArray.Count() ) |
|
241 { |
|
242 AddToCacheL( iFsMessageArray[iCursor] ); |
|
243 } |
|
244 return iMessageArray[iCursor]; |
|
245 } |
|
246 else |
|
247 { |
|
248 // No more items found, |
|
249 // set the cursor to the previous item, |
|
250 // and return NULL. |
|
251 iCursor--; |
|
252 return NULL; |
|
253 } |
|
254 } |
|
255 |
|
256 // ----------------------------------------------------------------------------- |
|
257 // |
|
258 // ----------------------------------------------------------------------------- |
|
259 void CMessageIterator::AddToCacheL( CFSMailMessage* aFsMsg ) |
|
260 { |
|
261 CEmailMessage* message = CEmailMessage::NewL( iPluginData, aFsMsg, EAPIOwns ); |
|
262 iMessageArray.AppendL( message ); |
|
263 } |
|
264 |
|
265 // ----------------------------------------------------------------------------- |
|
266 // |
|
267 // ----------------------------------------------------------------------------- |
|
268 void CMessageIterator::CleanCache() |
|
269 { |
|
270 // This is strange loop indeed |
|
271 // Both arrays has items that are deleted, iMessageArray has objects that point to iFsMessageArray |
|
272 // Index in both arrays goes syncronously |
|
273 // To prevent double destruction, first delete iMessageArray onjects and then the rest of the iFsMessageArray |
|
274 TInt count = iMessageArray.Count(); |
|
275 iMessageArray.ResetAndDestroy(); |
|
276 for ( TInt i = count; i < iFsMessageArray.Count(); i++ ) |
|
277 { |
|
278 iFsMessageArray.Remove( count ); |
|
279 } |
|
280 iFsMessageArray.Reset(); |
|
281 } |
|
282 // ----------------------------------------------------------------------------- |
|
283 // |
|
284 // ----------------------------------------------------------------------------- |
|
285 TUint CMessageIterator::Count() const |
|
286 { |
|
287 return iCount; |
|
288 } |
|
289 |
|
290 // ----------------------------------------------------------------------------- |
|
291 // |
|
292 // ----------------------------------------------------------------------------- |
|
293 void CMessageIterator::ConstructL() |
|
294 { |
|
295 iPlugin = iPluginData.ClaimInstanceL(); |
|
296 |
|
297 } |
|
298 |
|
299 // ----------------------------------------------------------------------------- |
|
300 // |
|
301 // ----------------------------------------------------------------------------- |
|
302 CMessageIterator::CMessageIterator( |
|
303 MFSMailIterator* aIterator, |
|
304 CPluginData& aPluginData, |
|
305 TUint aCount ) : |
|
306 iIterator( aIterator ), |
|
307 iPluginData( aPluginData ), |
|
308 iStartMsgId( TFSMailMsgId() ), |
|
309 iState( EReadNextMessageChunk ), |
|
310 iFsMessageArray( KMessageChunkSize ), |
|
311 iCount( aCount ), |
|
312 iCursor ( KUndefined ), |
|
313 iFirstMsgId( TFSMailMsgId() ), |
|
314 iHasMoreNextItems( ETrue ), |
|
315 iHasMorePrevItems( ETrue ) |
|
316 { |
|
317 } |
|
318 |
|
319 // End of file |