64
|
1 |
/*
|
|
2 |
* Copyright (c) 2005 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: Freestyle Email application attachment download info mediator
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
// INCLUDE FILES
|
|
20 |
#include "emailtrace.h"
|
|
21 |
#include <e32svr.h>
|
|
22 |
//<cmail>
|
|
23 |
#include "cfsmailclient.h"
|
|
24 |
//</cmail>
|
|
25 |
#include <StringLoader.h>
|
|
26 |
#include <FreestyleEmailUi.rsg>
|
|
27 |
|
|
28 |
#include "FreestyleEmailUiConstants.h"
|
|
29 |
#include "FreestyleEmailUiLiterals.h"
|
|
30 |
#include "FreestyleEmailUiUtilities.h"
|
|
31 |
#include "FreestyleEmailDownloadInformationMediator.h"
|
|
32 |
#include "freestyleemailcenrephandler.h"
|
|
33 |
#include "FreestyleEmailUiAppui.h"
|
|
34 |
#include "FSEmail.pan"
|
|
35 |
|
|
36 |
|
|
37 |
// ============================ MEMBER FUNCTIONS ===============================
|
|
38 |
|
|
39 |
// ----------------------------------------------------
|
|
40 |
// CFSEmailDownloadInfoMediator::CFSEmailDownloadInfoMediator
|
|
41 |
|
|
42 |
// Default class constructor.
|
|
43 |
// ----------------------------------------------------
|
|
44 |
//
|
|
45 |
CFSEmailDownloadInfoMediator::CFSEmailDownloadInfoMediator( CFSMailClient& aMailClient )
|
|
46 |
: CActive( EPriorityNormal ), iMailClient(aMailClient),
|
|
47 |
iDownloadArray( KArrayGranularity, _FOFF(TDownload, iRequestId) ), iDownloadsStarted ( EFalse ),
|
|
48 |
iNotificationsInProgress( 0 ), iObserverDeleted( EFalse )
|
|
49 |
{
|
|
50 |
FUNC_LOG;
|
|
51 |
CActiveScheduler::Add( this );
|
|
52 |
}
|
|
53 |
|
|
54 |
// ----------------------------------------------------
|
|
55 |
// CFSEmailDownloadInfoMediator::ConstructL
|
|
56 |
// Second phase class constructor.
|
|
57 |
// ----------------------------------------------------
|
|
58 |
//
|
|
59 |
void CFSEmailDownloadInfoMediator::ConstructL()
|
|
60 |
{
|
|
61 |
FUNC_LOG;
|
|
62 |
}
|
|
63 |
|
|
64 |
// ----------------------------------------------------
|
|
65 |
// CFSEmailDownloadInfoMediator::NewL
|
|
66 |
// Two-phased class constructor.
|
|
67 |
// Singleton
|
|
68 |
// ----------------------------------------------------
|
|
69 |
//
|
|
70 |
CFSEmailDownloadInfoMediator* CFSEmailDownloadInfoMediator::NewL( CFSMailClient& aMailClient )
|
|
71 |
{
|
|
72 |
FUNC_LOG;
|
|
73 |
|
|
74 |
CFSEmailDownloadInfoMediator* singleton = NULL;
|
|
75 |
|
|
76 |
// Check Thread Local Storage for instance pointer
|
|
77 |
singleton = static_cast<CFSEmailDownloadInfoMediator*>( UserSvr::DllTls( KTlsHandleDownloadInfo ) );
|
|
78 |
if ( !singleton )
|
|
79 |
{
|
|
80 |
singleton = new ( ELeave ) CFSEmailDownloadInfoMediator( aMailClient );
|
|
81 |
CleanupStack::PushL( singleton );
|
|
82 |
singleton->ConstructL();
|
|
83 |
CleanupStack::Pop( singleton );
|
|
84 |
|
|
85 |
// Store a pointer of a new instance in Thread Local Storage
|
|
86 |
TInt err = UserSvr::DllSetTls( KTlsHandleDownloadInfo, singleton );
|
|
87 |
if ( err )
|
|
88 |
{
|
|
89 |
delete singleton;
|
|
90 |
singleton = NULL;
|
|
91 |
User::Leave( err );
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
return singleton;
|
|
96 |
}
|
|
97 |
|
|
98 |
|
|
99 |
void CFSEmailDownloadInfoMediator::AddObserver( MFSEmailDownloadInformationObserver* aObserver, TFSMailMsgId aMessageId )
|
|
100 |
{
|
|
101 |
FUNC_LOG;
|
|
102 |
TRequestObserver newObserver = { aObserver, aMessageId };
|
|
103 |
//To avoid adding duplicate observers
|
|
104 |
for ( TInt i=0; i<iObserverArray.Count(); i++ )
|
|
105 |
{
|
|
106 |
if ( newObserver.iObserver == iObserverArray[i].iObserver && newObserver.iMessageId == iObserverArray[i].iMessageId )
|
|
107 |
{
|
|
108 |
return;
|
|
109 |
}
|
|
110 |
}
|
|
111 |
iObserverArray.Append( newObserver );
|
|
112 |
}
|
|
113 |
|
|
114 |
void CFSEmailDownloadInfoMediator::AddObserver( MFSEmailDownloadInformationObserver* aObserver )
|
|
115 |
{
|
|
116 |
//To avoid adding duplicate observers
|
|
117 |
for ( TInt i=0; i<iAllObserverArray.Count(); i++ )
|
|
118 |
{
|
|
119 |
if ( aObserver == iAllObserverArray[i] )
|
|
120 |
{
|
|
121 |
return;
|
|
122 |
}
|
|
123 |
}
|
|
124 |
iAllObserverArray.Append(aObserver);
|
|
125 |
}
|
|
126 |
|
|
127 |
// ----------------------------------------------------
|
|
128 |
// CFSEmailDownloadInfoMediator::~CFSEmailDownloadInfoMediator
|
|
129 |
// Destructor of CFSEmailDownloadInfoMediator class.
|
|
130 |
// ----------------------------------------------------
|
|
131 |
//
|
|
132 |
CFSEmailDownloadInfoMediator::~CFSEmailDownloadInfoMediator()
|
|
133 |
{
|
|
134 |
FUNC_LOG;
|
|
135 |
iAllObserverArray.Close();
|
|
136 |
iObserverArray.Close();
|
|
137 |
iDownloadArray.Close();
|
|
138 |
iDownloadCountArray.Close();
|
|
139 |
Cancel();
|
|
140 |
}
|
|
141 |
|
|
142 |
|
|
143 |
void CFSEmailDownloadInfoMediator::RequestResponseL(
|
|
144 |
TFSProgress aEvent, TInt aRequestId )
|
|
145 |
{
|
|
146 |
FUNC_LOG;
|
|
147 |
// Find the correct download structure
|
|
148 |
TDownload download( aRequestId, TPartData(), KNullDesC(), EFalse );
|
|
149 |
TInt idx = iDownloadArray.Find( download );
|
|
150 |
|
|
151 |
if ( idx >= 0 && idx < iDownloadArray.Count() )
|
|
152 |
{
|
|
153 |
// save the progress info from status events
|
|
154 |
if ( aEvent.iProgressStatus == TFSProgress::EFSStatus_Status )
|
|
155 |
{
|
|
156 |
iDownloadArray[idx].iCounter = aEvent.iCounter;
|
|
157 |
iDownloadArray[idx].iMaxCount = aEvent.iMaxCount;
|
|
158 |
}
|
|
159 |
|
|
160 |
// Make a local copy of the download object as the object may be
|
|
161 |
// removed from the array before we are finished
|
|
162 |
download = iDownloadArray[idx];
|
|
163 |
|
|
164 |
// In case of error, show a note. Note that these notes should not
|
|
165 |
// be displayed synchronously, since otherwise other events can
|
|
166 |
// occur that delete objects that are still in use waiting for this
|
|
167 |
// method to complete (see PDOO-7X93JP).
|
|
168 |
if ( aEvent.iError && aEvent.iError != KErrCancel )
|
|
169 |
{
|
|
170 |
// Download failed, show error note
|
|
171 |
if ( aEvent.iError == KErrCouldNotConnect ||
|
|
172 |
aEvent.iError == KErrConnectionTerminated )
|
|
173 |
{ // connection error
|
|
174 |
TFsEmailUiUtility::ShowErrorNoteL(
|
|
175 |
R_FREESTYLE_EMAIL_ERROR_GENERAL_CONNECTION_ERROR,
|
|
176 |
EFalse );
|
|
177 |
}
|
|
178 |
else // other error
|
|
179 |
{
|
|
180 |
TFsEmailUiUtility::ShowErrorNoteL(
|
|
181 |
R_FREESTYLE_EMAIL_ERROR_GENERAL_UNABLE_TO_COMPLETE,
|
|
182 |
EFalse );
|
|
183 |
}
|
|
184 |
}
|
|
185 |
|
|
186 |
// Remove download from array if it has been cancelled or failed
|
|
187 |
if ( aEvent.iProgressStatus ==
|
|
188 |
TFSProgress::EFSStatus_RequestCancelled || aEvent.iError )
|
|
189 |
{
|
|
190 |
iDownloadArray.Remove(idx);
|
|
191 |
}
|
|
192 |
// Mark download as completed if download just finished
|
|
193 |
else if ( aEvent.iProgressStatus ==
|
|
194 |
TFSProgress::EFSStatus_RequestComplete )
|
|
195 |
{
|
|
196 |
if ( !iDownloadArray[idx].iMaxCount )
|
|
197 |
{
|
|
198 |
// Set max count to 1 if no single progress event has arrived
|
|
199 |
// before download is finished.
|
|
200 |
iDownloadArray[idx].iMaxCount = 1;
|
|
201 |
}
|
|
202 |
iDownloadArray[idx].iCounter = iDownloadArray[idx].iMaxCount;
|
|
203 |
}
|
|
204 |
|
|
205 |
TInt completedDownloadsToNotify = 0;
|
|
206 |
|
|
207 |
// Find and update the count array entry
|
|
208 |
TDownloadCount countObject( download.iPartData.iMessageId );
|
|
209 |
GetAndUpdateDownloadCountL( countObject, aEvent );
|
|
210 |
|
|
211 |
// Notification may be given if all ongoing downloads from message
|
|
212 |
// have finished and there are some properly completed downloads
|
|
213 |
if ( download.iNotifyComplete &&
|
|
214 |
countObject.iDownloadsCompletedCount &&
|
|
215 |
countObject.iDownloadsCompletedCount ==
|
|
216 |
countObject.iDownloadsStartedCount )
|
|
217 |
{
|
|
218 |
completedDownloadsToNotify = countObject.iDownloadsCompletedCount;
|
|
219 |
}
|
|
220 |
|
|
221 |
// if download is complete
|
|
222 |
if ( aEvent.iProgressStatus ==
|
|
223 |
TFSProgress::EFSStatus_RequestComplete && !aEvent.iError )
|
|
224 |
{
|
|
225 |
// if file was set to be saved after download
|
|
226 |
if ( download.iSaveFileName.CompareC( KNullDesC ) )
|
|
227 |
{
|
|
228 |
CFSMailMessage* mailMessage = iMailClient.GetMessageByUidL(
|
|
229 |
download.iPartData.iMailBoxId,
|
|
230 |
download.iPartData.iFolderId,
|
|
231 |
download.iPartData.iMessageId,
|
|
232 |
EFSMsgDataEnvelope );
|
|
233 |
CleanupStack::PushL( mailMessage );
|
|
234 |
CFSMailMessagePart* attachment = mailMessage->ChildPartL(
|
|
235 |
download.iPartData.iMessagePartId );
|
|
236 |
CleanupStack::PopAndDestroy( mailMessage );
|
|
237 |
CleanupStack::PushL( attachment );
|
|
238 |
if ( TFsEmailUiUtility::OkToSaveFileL( download.iSaveFileName, *attachment ) )
|
|
239 |
{
|
|
240 |
attachment->CopyContentFileL( download.iSaveFileName );
|
|
241 |
}
|
|
242 |
CleanupStack::PopAndDestroy( attachment );
|
|
243 |
}
|
|
244 |
}
|
|
245 |
|
|
246 |
// relay the event to observers
|
|
247 |
NotifyObserversL( aEvent, download.iPartData );
|
|
248 |
|
|
249 |
// <cmail> moved completion note after notifying so that observers can
|
|
250 |
// clear menus etc. before note is displayed
|
|
251 |
// if download is complete
|
|
252 |
if ( aEvent.iProgressStatus ==
|
|
253 |
TFSProgress::EFSStatus_RequestComplete && !aEvent.iError )
|
|
254 |
{
|
|
255 |
// Notification of saved attachments may be given if all downloads of the given message has been completed.
|
|
256 |
if ( download.iNotifyComplete && countObject.iDownloadsCompletedCount &&
|
|
257 |
countObject.iDownloadsCompletedCount == countObject.iDownloadsStartedCount &&
|
|
258 |
countObject.iSaveRequestedCount > 0 )
|
|
259 |
{
|
|
260 |
TFsEmailUiUtility::SetDownloadSave( ETrue );
|
|
261 |
TFsEmailUiUtility::ShowFilesSavedToFolderNoteL( countObject.iDownloadsCompletedCount /*iSaveRequestedCount*/ ); // Fix: ESLX-84ACJ9
|
|
262 |
}
|
|
263 |
// Show "Download completed" if necessary
|
|
264 |
else if ( CompletionNotesInUseL() && completedDownloadsToNotify )
|
|
265 |
{
|
|
266 |
OpenAttachmentL( download.iPartData, completedDownloadsToNotify );
|
|
267 |
}
|
|
268 |
else if ( download.iNotifyComplete &&
|
|
269 |
countObject.iSaveRequestedCount == 0 )
|
|
270 |
{
|
|
271 |
TFsEmailUiUtility::OpenAttachmentL( download.iPartData );
|
|
272 |
}
|
|
273 |
}
|
|
274 |
// </cmail>
|
|
275 |
}
|
|
276 |
}
|
|
277 |
|
|
278 |
TBool CFSEmailDownloadInfoMediator::IsAnyAttachmentDownloads()
|
|
279 |
{
|
|
280 |
FUNC_LOG;
|
|
281 |
return iDownloadsStarted;
|
|
282 |
}
|
|
283 |
|
|
284 |
|
|
285 |
TBool CFSEmailDownloadInfoMediator::IsAnyAttachmentDownloads( TFSMailMsgId aMessageId )
|
|
286 |
{
|
|
287 |
FUNC_LOG;
|
|
288 |
for ( TInt i=0; i<iDownloadArray.Count(); i++ )
|
|
289 |
{
|
|
290 |
if ( iDownloadArray[i].iPartData.iMessageId == aMessageId )
|
|
291 |
{
|
|
292 |
return ETrue;
|
|
293 |
}
|
|
294 |
}
|
|
295 |
return EFalse;
|
|
296 |
}
|
|
297 |
|
|
298 |
TInt CFSEmailDownloadInfoMediator::GetDownloadPercentageL( TFSMailMsgId aMessageId )
|
|
299 |
{
|
|
300 |
FUNC_LOG;
|
|
301 |
TInt count = 0;
|
|
302 |
TInt maxCount = 0;
|
|
303 |
|
|
304 |
for ( TInt i=0; i<iDownloadArray.Count(); i++ )
|
|
305 |
{
|
|
306 |
if ( iDownloadArray[i].iPartData.iMessageId == aMessageId )
|
|
307 |
{
|
|
308 |
// In case we find an attachment for which maxCount is still missing,
|
|
309 |
// update all the missing maxCounts to match the file size of
|
|
310 |
// the attachment.
|
|
311 |
if ( iDownloadArray[i].iMaxCount <= 0 )
|
|
312 |
{
|
|
313 |
// This should happen at most once per call to GetDownloadPercentageL().
|
|
314 |
UpdateAttachmentSizesL( iDownloadArray[i].iPartData );
|
|
315 |
}
|
|
316 |
|
|
317 |
count += iDownloadArray[i].iCounter;
|
|
318 |
maxCount += iDownloadArray[i].iMaxCount;
|
|
319 |
}
|
|
320 |
}
|
|
321 |
|
|
322 |
if ( maxCount <= 0 )
|
|
323 |
{
|
|
324 |
return KErrNotFound;
|
|
325 |
}
|
|
326 |
else
|
|
327 |
{
|
|
328 |
return 100*count/maxCount;
|
|
329 |
}
|
|
330 |
}
|
|
331 |
|
|
332 |
void CFSEmailDownloadInfoMediator::UpdateAttachmentSizesL( const TPartData& aMessageData )
|
|
333 |
{
|
|
334 |
FUNC_LOG;
|
|
335 |
CFSMailMessage* mailMessage = iMailClient.GetMessageByUidL( aMessageData.iMailBoxId,
|
|
336 |
aMessageData.iFolderId,
|
|
337 |
aMessageData.iMessageId,
|
|
338 |
EFSMsgDataEnvelope );
|
|
339 |
CleanupStack::PushL( mailMessage );
|
|
340 |
RPointerArray<CFSMailMessagePart> attachments;
|
|
341 |
CleanupResetAndDestroyClosePushL( attachments );
|
|
342 |
mailMessage->AttachmentListL( attachments );
|
|
343 |
|
|
344 |
for ( TInt i=0; i<iDownloadArray.Count(); i++ )
|
|
345 |
{
|
|
346 |
if ( iDownloadArray[i].iPartData.iMessageId == aMessageData.iMessageId )
|
|
347 |
{
|
|
348 |
if ( iDownloadArray[i].iMaxCount <= 0 )
|
|
349 |
{
|
|
350 |
// Find the matching attachment object and retrieve the attachment size
|
|
351 |
for ( TInt j=0 ; j<attachments.Count() ; ++j )
|
|
352 |
{
|
|
353 |
if ( attachments[j]->GetPartId() == iDownloadArray[i].iPartData.iMessagePartId )
|
|
354 |
{
|
|
355 |
iDownloadArray[i].iMaxCount = attachments[j]->ContentSize();
|
|
356 |
break;
|
|
357 |
}
|
|
358 |
}
|
|
359 |
}
|
|
360 |
}
|
|
361 |
}
|
|
362 |
|
|
363 |
CleanupStack::PopAndDestroy( &attachments );
|
|
364 |
CleanupStack::PopAndDestroy( mailMessage );
|
|
365 |
}
|
|
366 |
|
|
367 |
TBool CFSEmailDownloadInfoMediator::IsDownloadableL( TPartData aPart )
|
|
368 |
{
|
|
369 |
FUNC_LOG;
|
|
370 |
TBool ret(EFalse);
|
|
371 |
// if part is downloading, no more checking is needed
|
|
372 |
if ( !IsDownloading( aPart.iMessagePartId ) )
|
|
373 |
{
|
|
374 |
CFSMailMessage* mailMessage = iMailClient.GetMessageByUidL(
|
|
375 |
aPart.iMailBoxId, aPart.iFolderId,
|
|
376 |
aPart.iMessageId, EFSMsgDataEnvelope );
|
|
377 |
if ( mailMessage )
|
|
378 |
{
|
|
379 |
CleanupStack::PushL( mailMessage );
|
|
380 |
CFSMailMessagePart* mailMessagePart =
|
|
381 |
mailMessage->ChildPartL( aPart.iMessagePartId );
|
|
382 |
// if the file is fetched full
|
|
383 |
if ( mailMessagePart &&
|
|
384 |
mailMessagePart->FetchLoadState() == EFSFull )
|
|
385 |
{
|
|
386 |
ret = EFalse;
|
|
387 |
}
|
|
388 |
else
|
|
389 |
{
|
|
390 |
ret = ETrue;
|
|
391 |
}
|
|
392 |
delete mailMessagePart;
|
|
393 |
CleanupStack::PopAndDestroy( mailMessage );
|
|
394 |
}
|
|
395 |
}
|
|
396 |
return ret;
|
|
397 |
}
|
|
398 |
|
|
399 |
|
|
400 |
void CFSEmailDownloadInfoMediator::Destroy()
|
|
401 |
{
|
|
402 |
FUNC_LOG;
|
|
403 |
CFSEmailDownloadInfoMediator* self =
|
|
404 |
static_cast<CFSEmailDownloadInfoMediator*>( UserSvr::DllTls( KTlsHandleDownloadInfo ) );
|
|
405 |
UserSvr::DllFreeTls( KTlsHandleDownloadInfo );
|
|
406 |
delete self;
|
|
407 |
}
|
|
408 |
|
|
409 |
void CFSEmailDownloadInfoMediator::StopObserving( MFSEmailDownloadInformationObserver* aObserver, TFSMailMsgId aMessageId )
|
|
410 |
{
|
|
411 |
FUNC_LOG;
|
|
412 |
for (TInt i=iObserverArray.Count()-1; i>=0; i--)
|
|
413 |
{
|
|
414 |
if ( iObserverArray[i].iObserver == aObserver && iObserverArray[i].iMessageId == aMessageId )
|
|
415 |
{
|
|
416 |
RemoveObserver(i);
|
|
417 |
}
|
|
418 |
}
|
|
419 |
}
|
|
420 |
|
|
421 |
void CFSEmailDownloadInfoMediator::StopObserving( MFSEmailDownloadInformationObserver* aObserver )
|
|
422 |
{
|
|
423 |
FUNC_LOG;
|
|
424 |
for ( TInt i=iObserverArray.Count()-1; i>=0; i-- )
|
|
425 |
{
|
|
426 |
if ( iObserverArray[i].iObserver == aObserver )
|
|
427 |
{
|
|
428 |
RemoveObserver(i);
|
|
429 |
}
|
|
430 |
}
|
|
431 |
for ( TInt i=iAllObserverArray.Count()-1; i>=0; i-- )
|
|
432 |
{
|
|
433 |
if ( iAllObserverArray[i] == aObserver )
|
|
434 |
{
|
|
435 |
iAllObserverArray.Remove(i);
|
|
436 |
}
|
|
437 |
}
|
|
438 |
}
|
|
439 |
|
|
440 |
void CFSEmailDownloadInfoMediator::RemoveObserver( TInt aIdx )
|
|
441 |
{
|
|
442 |
if ( iNotificationsInProgress == 0 )
|
|
443 |
{
|
|
444 |
// If we're not currently iterating over the array, remove the entry
|
|
445 |
// immediately.
|
|
446 |
iObserverArray.Remove( aIdx );
|
|
447 |
}
|
|
448 |
else
|
|
449 |
{
|
|
450 |
// We're currently iterating over the array: mark the entry as
|
|
451 |
// deleted and flag that we've done so.
|
|
452 |
iObserverArray[aIdx].iDeleted = ETrue;
|
|
453 |
iObserverDeleted = ETrue;
|
|
454 |
}
|
|
455 |
}
|
|
456 |
|
|
457 |
void CFSEmailDownloadInfoMediator::DownloadL( TPartData aPart, TBool aCompleteNote )
|
|
458 |
{
|
|
459 |
FUNC_LOG;
|
|
460 |
TFileName emptyFileName;
|
|
461 |
DownloadAndSaveL( aPart, emptyFileName, aCompleteNote );
|
|
462 |
}
|
|
463 |
|
|
464 |
void CFSEmailDownloadInfoMediator::DownloadAndSaveL( TPartData aPart, const TDesC& aSaveFileName, TBool aCompleteNote )
|
|
465 |
{
|
|
466 |
FUNC_LOG;
|
|
467 |
// now there is at least one download started
|
|
468 |
iDownloadsStarted = ETrue;
|
|
469 |
TFsEmailUiUtility::SetSaveSelect( ETrue );
|
|
470 |
// fetch message part
|
|
471 |
CFSMailMessage* mailMessage = iMailClient.GetMessageByUidL( aPart.iMailBoxId, aPart.iFolderId, aPart.iMessageId, EFSMsgDataEnvelope );
|
|
472 |
CleanupStack::PushL( mailMessage );
|
|
473 |
CFSMailMessagePart* messagePart = mailMessage->ChildPartL( aPart.iMessagePartId );
|
|
474 |
CleanupStack::PopAndDestroy( mailMessage );
|
|
475 |
|
|
476 |
// Panic in udeb builds, the condition !messagePart should not be possible
|
|
477 |
ASSERT( messagePart );
|
|
478 |
|
|
479 |
// to be on the safer side only leave in urel builds
|
|
480 |
if ( !messagePart )
|
|
481 |
{
|
|
482 |
User::Leave( KErrNotFound );
|
|
483 |
}
|
|
484 |
|
|
485 |
CleanupStack::PushL( messagePart );
|
|
486 |
// start download and get request id
|
|
487 |
TInt requestId = messagePart->FetchMessagePartL( aPart.iMessagePartId, *this, 0 );
|
|
488 |
|
|
489 |
// store download information
|
|
490 |
TDownload newDownload( requestId, aPart, aSaveFileName, aCompleteNote );
|
|
491 |
AppendDownloadToInternalArraysL( newDownload );
|
|
492 |
CleanupStack::PopAndDestroy( messagePart );
|
|
493 |
|
|
494 |
// It may take some time before the first progress event is received from
|
|
495 |
// the protocol plug-in. This is true at least with the current Intellisync
|
|
496 |
// plug-in implementation. Send artificial "0% event" to the UI to ensure that
|
|
497 |
// the UI shows that the file is being downloaded.
|
|
498 |
TFSProgress zeroEvent = { TFSProgress::EFSStatus_Waiting, 0, 0, KErrNone };
|
|
499 |
NotifyObserversL( zeroEvent, aPart );
|
|
500 |
}
|
|
501 |
|
|
502 |
void CFSEmailDownloadInfoMediator::CancelDownloadL(
|
|
503 |
TFSMailMsgId aMessagePartId )
|
|
504 |
{
|
|
505 |
FUNC_LOG;
|
|
506 |
// Find the corresponding download object
|
|
507 |
TDownload downloadToCancel;
|
|
508 |
downloadToCancel.iPartData.iMessagePartId = aMessagePartId;
|
|
509 |
TInt idx = iDownloadArray.Find( downloadToCancel,
|
|
510 |
TIdentityRelation<TDownload>(EqualMessagePartId) );
|
|
511 |
// Cancel if download object was found
|
|
512 |
if ( idx >= 0 && idx < iDownloadArray.Count() )
|
|
513 |
{
|
|
514 |
iMailClient.CancelL( iDownloadArray[idx].iRequestId );
|
|
515 |
}
|
|
516 |
}
|
|
517 |
|
|
518 |
void CFSEmailDownloadInfoMediator::CancelAllDownloadsL( TFSMailMsgId aMailBoxId )
|
|
519 |
{
|
|
520 |
// Cancel all downloads within mailbox indicated with aMailBoxId
|
|
521 |
for (TInt i=0 ; i < iDownloadArray.Count() ; i ++ )
|
|
522 |
{
|
|
523 |
if ( iDownloadArray[i].iPartData.iMailBoxId == aMailBoxId )
|
|
524 |
{
|
|
525 |
iMailClient.CancelL( iDownloadArray[i].iRequestId );
|
|
526 |
}
|
|
527 |
}
|
|
528 |
}
|
|
529 |
|
|
530 |
TBool CFSEmailDownloadInfoMediator::IsDownloading(
|
|
531 |
TFSMailMsgId aMessagePartId )
|
|
532 |
{
|
|
533 |
FUNC_LOG;
|
|
534 |
TInt dlArrayCount = iDownloadArray.Count();
|
|
535 |
for ( TInt i = 0; i < dlArrayCount; i++ )
|
|
536 |
{
|
|
537 |
const TDownload& downloadObject = iDownloadArray[i];
|
|
538 |
if ( downloadObject.iPartData.iMessagePartId == aMessagePartId )
|
|
539 |
{
|
|
540 |
if ( downloadObject.iCounter < downloadObject.iMaxCount ||
|
|
541 |
downloadObject.iMaxCount <= 0 )
|
|
542 |
{
|
|
543 |
// Only return ETrue if the corresponding download entry is
|
|
544 |
// found and it's not yet finished.
|
|
545 |
return ETrue;
|
|
546 |
}
|
|
547 |
}
|
|
548 |
}
|
|
549 |
return EFalse;
|
|
550 |
}
|
|
551 |
|
|
552 |
void CFSEmailDownloadInfoMediator::NotifyObserversIfAttachmentsDownloadedL(
|
|
553 |
TPartData aMessageId )
|
|
554 |
{
|
|
555 |
FUNC_LOG;
|
|
556 |
CFSMailMessage* mailMessage = iMailClient.GetMessageByUidL(
|
|
557 |
aMessageId.iMailBoxId,
|
|
558 |
aMessageId.iFolderId,
|
|
559 |
aMessageId.iMessageId,
|
|
560 |
EFSMsgDataEnvelope );
|
|
561 |
CleanupStack::PushL( mailMessage );
|
|
562 |
|
|
563 |
RPointerArray<CFSMailMessagePart> attachments;
|
|
564 |
CleanupResetAndDestroyClosePushL( attachments );
|
|
565 |
mailMessage->AttachmentListL( attachments );
|
|
566 |
|
|
567 |
for ( TInt i=0 ; i<attachments.Count() ; ++i )
|
|
568 |
{
|
|
569 |
TInt fetched = attachments[i]->FetchedContentSize();
|
|
570 |
TInt totalSize = attachments[i]->ContentSize();
|
|
571 |
|
|
572 |
if ( fetched == totalSize )
|
|
573 |
{
|
|
574 |
// download has happened => Download Manager should be available in menus
|
|
575 |
iDownloadsStarted = ETrue;
|
|
576 |
|
|
577 |
// notify observers about completed attachment download
|
|
578 |
TFSProgress completeEvent =
|
|
579 |
{
|
|
580 |
TFSProgress::EFSStatus_RequestComplete,
|
|
581 |
fetched,
|
|
582 |
totalSize,
|
|
583 |
KErrNone
|
|
584 |
};
|
|
585 |
aMessageId.iMessagePartId = attachments[i]->GetPartId();
|
|
586 |
NotifyObserversL( completeEvent, aMessageId );
|
|
587 |
}
|
|
588 |
}
|
|
589 |
|
|
590 |
CleanupStack::PopAndDestroy( &attachments );
|
|
591 |
CleanupStack::PopAndDestroy( mailMessage );
|
|
592 |
}
|
|
593 |
|
|
594 |
void CFSEmailDownloadInfoMediator::AppendDownloadToInternalArraysL( const TDownload& aNewDownload )
|
|
595 |
{
|
|
596 |
FUNC_LOG;
|
|
597 |
// In case of some unexpected errors, it's possible that we have an existing download object
|
|
598 |
// with the same request ID as the new download. In that case, the original download has failed
|
|
599 |
// and needs to be removed.
|
|
600 |
TInt foundIdx = iDownloadArray.Find( aNewDownload );
|
|
601 |
if ( foundIdx >= 0 )
|
|
602 |
{
|
|
603 |
TFSProgress cancelEvent = { TFSProgress::EFSStatus_RequestCancelled, 0, 0, KErrUnknown };
|
|
604 |
TDownload obsoleteDownload = iDownloadArray[foundIdx];
|
|
605 |
TDownloadCount countObject( obsoleteDownload.iPartData.iMessageId );
|
|
606 |
|
|
607 |
iDownloadArray.Remove( foundIdx );
|
|
608 |
GetAndUpdateDownloadCountL( countObject, cancelEvent );
|
|
609 |
NotifyObserversL( cancelEvent, obsoleteDownload.iPartData );
|
|
610 |
}
|
|
611 |
|
|
612 |
// Add the download object to array now when we know that there's no duplicate IDs.
|
|
613 |
iDownloadArray.Append( aNewDownload );
|
|
614 |
|
|
615 |
// Add the download to the download count array
|
|
616 |
TDownloadCount newCountObject( aNewDownload.iPartData.iMessageId );
|
|
617 |
TInt idx = iDownloadCountArray.Find( newCountObject,
|
|
618 |
TIdentityRelation<TDownloadCount>(EqualMessageId) );
|
|
619 |
if ( idx >= 0 && idx < iDownloadCountArray.Count() )
|
|
620 |
{
|
|
621 |
// if there are already downloads going on on the same message
|
|
622 |
iDownloadCountArray[idx].iDownloadsStartedCount++;
|
|
623 |
if ( aNewDownload.iSaveFileName.CompareC( KNullDesC ) )
|
|
624 |
{
|
|
625 |
iDownloadCountArray[idx].iSaveRequestedCount++;
|
|
626 |
}
|
|
627 |
}
|
|
628 |
else
|
|
629 |
{
|
|
630 |
newCountObject.iDownloadsStartedCount = 1;
|
|
631 |
if ( aNewDownload.iSaveFileName.CompareC( KNullDesC ) )
|
|
632 |
{
|
|
633 |
newCountObject.iSaveRequestedCount=1;
|
|
634 |
}
|
|
635 |
iDownloadCountArray.Append( newCountObject );
|
|
636 |
}
|
|
637 |
}
|
|
638 |
|
|
639 |
TBool CFSEmailDownloadInfoMediator::CompletionNotesInUseL() const
|
|
640 |
{
|
|
641 |
FUNC_LOG;
|
|
642 |
// singleton, do not destroy
|
|
643 |
CFSEmailCRHandler* cenRepHandler = CFSEmailCRHandler::InstanceL();
|
|
644 |
|
|
645 |
return cenRepHandler->DownloadNotifications();
|
|
646 |
}
|
|
647 |
|
|
648 |
void CFSEmailDownloadInfoMediator::OpenAttachmentL( const TPartData& aPart,
|
|
649 |
const TInt aCompletedCount )
|
|
650 |
{
|
|
651 |
iPopupLaunchData = aPart;
|
|
652 |
if (aCompletedCount > 1)
|
|
653 |
{
|
|
654 |
iPopupLaunchData.iMessagePartId.SetNullId();
|
|
655 |
}
|
|
656 |
TRequestStatus* status = &iStatus;
|
|
657 |
User::RequestComplete(status, KErrNone);
|
|
658 |
SetActive();
|
|
659 |
}
|
|
660 |
|
|
661 |
void CFSEmailDownloadInfoMediator::LaunchDownloadCompleteNoteL(
|
|
662 |
const TPartData& aPart, TInt aCompletedCount )
|
|
663 |
{
|
|
664 |
FUNC_LOG;
|
|
665 |
|
|
666 |
Cancel(); // deletes previous iGlobalMsgQuery
|
|
667 |
|
|
668 |
iPopupLaunchData = aPart;
|
|
669 |
|
|
670 |
// fetch message part
|
|
671 |
CFSMailMessage* mailMessage = iMailClient.GetMessageByUidL(
|
|
672 |
aPart.iMailBoxId,
|
|
673 |
aPart.iFolderId,
|
|
674 |
aPart.iMessageId,
|
|
675 |
EFSMsgDataEnvelope );
|
|
676 |
|
|
677 |
CleanupStack::PushL( mailMessage );
|
|
678 |
|
|
679 |
// From:
|
|
680 |
HBufC* from = StringLoader::LoadLC(
|
|
681 |
R_FSE_VIEWER_ATTACHMENTS_TEXT_FROM );
|
|
682 |
|
|
683 |
CFSMailAddress* senderData = mailMessage->GetSender(); // NOT OWNED
|
|
684 |
|
|
685 |
TDesC& mailSender = senderData->GetDisplayName();
|
|
686 |
|
|
687 |
// Subject:
|
|
688 |
HBufC* mailSubject =
|
|
689 |
TFsEmailUiUtility::CreateSubjectTextLC( mailMessage );
|
|
690 |
|
|
691 |
HBufC* subj = StringLoader::LoadLC(
|
|
692 |
R_FSE_VIEWER_ATTACHMENTS_TEXT_SUBJECT );
|
|
693 |
|
|
694 |
// Attachment name or count
|
|
695 |
HBufC* attachmentText = NULL;
|
|
696 |
|
|
697 |
CFSMailMessagePart* messagePart =
|
|
698 |
mailMessage->ChildPartL( aPart.iMessagePartId );
|
|
699 |
|
|
700 |
if ( messagePart )
|
|
701 |
{
|
|
702 |
// create popup text
|
|
703 |
|
|
704 |
if ( aCompletedCount == 1 )
|
|
705 |
{
|
|
706 |
CleanupStack::PushL( messagePart );
|
|
707 |
|
|
708 |
attachmentText = messagePart->AttachmentNameL().AllocL();
|
|
709 |
|
|
710 |
CleanupStack::PopAndDestroy( messagePart );
|
|
711 |
|
|
712 |
CleanupStack::PushL( attachmentText );
|
|
713 |
}
|
|
714 |
else if ( aCompletedCount > 1 )
|
|
715 |
{
|
|
716 |
attachmentText = StringLoader::LoadLC(
|
|
717 |
R_FSE_VIEWER_ATTACHMENTS_DOWNLOADED, aCompletedCount );
|
|
718 |
iPopupLaunchData.iMessagePartId.SetNullId();
|
|
719 |
}
|
|
720 |
else
|
|
721 |
{
|
|
722 |
__ASSERT_DEBUG( EFalse, Panic(EFSEmailUiUnexpectedValue) );
|
|
723 |
attachmentText = KNullDesC().AllocLC();
|
|
724 |
}
|
|
725 |
|
|
726 |
}
|
|
727 |
else
|
|
728 |
{
|
|
729 |
attachmentText = StringLoader::LoadLC(
|
|
730 |
R_FSE_VIEWER_ATTACHMENTS_DOWNLOADED, aCompletedCount );
|
|
731 |
iPopupLaunchData.iMessagePartId.SetNullId();
|
|
732 |
}
|
|
733 |
|
|
734 |
// Combine the text parts into one descriptor
|
|
735 |
TInt textLength = from->Length() +
|
|
736 |
KSpace().Length() +
|
|
737 |
mailSender.Length() +
|
|
738 |
KLineFeed().Length() +
|
|
739 |
subj->Length() +
|
|
740 |
KSpace().Length() +
|
|
741 |
mailSubject->Length() +
|
|
742 |
KLineFeed().Length() +
|
|
743 |
attachmentText->Length();
|
|
744 |
|
|
745 |
HBufC* popupText = HBufC::NewL( textLength );
|
|
746 |
TPtr textPtr = popupText->Des();
|
|
747 |
|
|
748 |
textPtr.Append( *from );
|
|
749 |
textPtr.Append( KSpace );
|
|
750 |
textPtr.Append( mailSender );
|
|
751 |
textPtr.Append( KLineFeed );
|
|
752 |
textPtr.Append( *subj );
|
|
753 |
textPtr.Append( KSpace );
|
|
754 |
textPtr.Append( *mailSubject );
|
|
755 |
textPtr.Append( KLineFeed );
|
|
756 |
textPtr.Append( *attachmentText );
|
|
757 |
|
|
758 |
CleanupStack::PopAndDestroy( attachmentText );
|
|
759 |
CleanupStack::PopAndDestroy( subj );
|
|
760 |
CleanupStack::PopAndDestroy( mailSubject );
|
|
761 |
CleanupStack::PopAndDestroy( from );
|
|
762 |
|
|
763 |
CleanupStack::PopAndDestroy( mailMessage );
|
|
764 |
|
|
765 |
CleanupStack::PushL( popupText );
|
|
766 |
|
|
767 |
HBufC* popupHeader = StringLoader::LoadLC(
|
|
768 |
R_FSE_VIEWER_NOTE_ATTACHMENTS_DOWNLOAD_COMPLETE );
|
|
769 |
|
|
770 |
iGlobalMsgQuery = CAknGlobalMsgQuery::NewL();
|
|
771 |
|
|
772 |
iGlobalMsgQuery->ShowMsgQueryL(
|
|
773 |
iStatus,
|
|
774 |
*popupText,
|
|
775 |
R_AVKON_SOFTKEYS_OPEN_CLOSE,
|
|
776 |
*popupHeader,
|
|
777 |
KNullDesC );
|
|
778 |
|
|
779 |
CleanupStack::PopAndDestroy( popupHeader );
|
|
780 |
|
|
781 |
CleanupStack::PopAndDestroy( popupText );
|
|
782 |
|
|
783 |
SetActive();
|
|
784 |
}
|
|
785 |
|
|
786 |
void CFSEmailDownloadInfoMediator::NotifyObserversL( const TFSProgress& aEvent, const TPartData& aPart )
|
|
787 |
{
|
|
788 |
FUNC_LOG;
|
|
789 |
|
|
790 |
// notify observers of this particular message part
|
|
791 |
iNotificationsInProgress++;
|
|
792 |
TRAPD( error, NotifyPartObserversL( aEvent, aPart ) );
|
|
793 |
if ( --iNotificationsInProgress == 0 )
|
|
794 |
{
|
|
795 |
CleanUpObservers();
|
|
796 |
}
|
|
797 |
if ( error )
|
|
798 |
{
|
|
799 |
User::Leave( error );
|
|
800 |
}
|
|
801 |
|
|
802 |
// send response to every 'all observer'
|
|
803 |
for ( TInt i=iAllObserverArray.Count()-1; i>=0; i-- )
|
|
804 |
{
|
|
805 |
iAllObserverArray[i]->RequestResponseL( aEvent, aPart );
|
|
806 |
}
|
|
807 |
}
|
|
808 |
|
|
809 |
void CFSEmailDownloadInfoMediator::NotifyPartObserversL( const TFSProgress& aEvent, const TPartData& aPart )
|
|
810 |
{
|
|
811 |
FUNC_LOG;
|
|
812 |
// go through all observers
|
|
813 |
for ( TInt j=iObserverArray.Count()-1; j>=0; j-- )
|
|
814 |
{
|
|
815 |
// if observer is observing this message
|
|
816 |
if ( !iObserverArray[j].iDeleted && iObserverArray[j].iMessageId == aPart.iMessageId )
|
|
817 |
{
|
|
818 |
// send response to observer
|
|
819 |
iObserverArray[j].iObserver->RequestResponseL( aEvent, aPart );
|
|
820 |
}
|
|
821 |
}
|
|
822 |
}
|
|
823 |
|
|
824 |
void CFSEmailDownloadInfoMediator::CleanUpObservers()
|
|
825 |
{
|
|
826 |
FUNC_LOG;
|
|
827 |
// If one or more observers have been marked for deletion, go through
|
|
828 |
// the observer array and remove them.
|
|
829 |
if ( iObserverDeleted )
|
|
830 |
{
|
|
831 |
for ( TInt j=iObserverArray.Count()-1; j>=0; j-- )
|
|
832 |
{
|
|
833 |
if ( iObserverArray[j].iDeleted )
|
|
834 |
{
|
|
835 |
iObserverArray.Remove(j);
|
|
836 |
}
|
|
837 |
}
|
|
838 |
iObserverDeleted = EFalse;
|
|
839 |
}
|
|
840 |
}
|
|
841 |
|
|
842 |
void CFSEmailDownloadInfoMediator::RunL()
|
|
843 |
{
|
|
844 |
FUNC_LOG;
|
|
845 |
// no close button pressed
|
|
846 |
if (iStatus.Int() != EAknSoftkeyClose )
|
|
847 |
{
|
|
848 |
// open attachment
|
|
849 |
if ( !iPopupLaunchData.iMessagePartId.IsNullId() )
|
|
850 |
{
|
|
851 |
// Force FsEmailUI to foreground because global completion note may appear
|
|
852 |
// while some other application is active and our local error notes are not shown
|
|
853 |
// in that case. (This wouldn't be necessary in case of succesful file launching.)
|
|
854 |
TFsEmailUiUtility::BringFsEmailToForeground();
|
|
855 |
TFsEmailUiUtility::OpenAttachmentL( iPopupLaunchData );
|
|
856 |
}
|
|
857 |
}
|
|
858 |
}
|
|
859 |
|
|
860 |
void CFSEmailDownloadInfoMediator::DoCancel()
|
|
861 |
{
|
|
862 |
}
|
|
863 |
|
|
864 |
void CFSEmailDownloadInfoMediator::Cancel()
|
|
865 |
{
|
|
866 |
FUNC_LOG;
|
|
867 |
if ( iGlobalMsgQuery )
|
|
868 |
{
|
|
869 |
iGlobalMsgQuery->CancelMsgQuery();
|
|
870 |
delete iGlobalMsgQuery;
|
|
871 |
iGlobalMsgQuery = NULL;
|
|
872 |
}
|
|
873 |
if ( IsActive() )
|
|
874 |
{
|
|
875 |
CActive::Cancel();
|
|
876 |
}
|
|
877 |
}
|
|
878 |
|
|
879 |
TInt CFSEmailDownloadInfoMediator::RunError( TInt /*aError*/ )
|
|
880 |
{
|
|
881 |
return KErrNone;
|
|
882 |
}
|
|
883 |
|
|
884 |
void CFSEmailDownloadInfoMediator::GetAndUpdateDownloadCountL( TDownloadCount& aCountObject,
|
|
885 |
const TFSProgress& aEvent )
|
|
886 |
{
|
|
887 |
FUNC_LOG;
|
|
888 |
TInt idx = iDownloadCountArray.Find( aCountObject,
|
|
889 |
TIdentityRelation<TDownloadCount>(EqualMessageId) );
|
|
890 |
if ( idx >= 0 && idx < iDownloadCountArray.Count() )
|
|
891 |
{
|
|
892 |
// Set started save requests, it does not change depending on the event received
|
|
893 |
aCountObject.iDownloadsStartedCount = iDownloadCountArray[idx].iDownloadsStartedCount;
|
|
894 |
|
|
895 |
// If download completed succesfully, increase the completed downlods count
|
|
896 |
if ( aEvent.iProgressStatus == TFSProgress::EFSStatus_RequestComplete &&
|
|
897 |
!aEvent.iError )
|
|
898 |
{
|
|
899 |
iDownloadCountArray[idx].iDownloadsCompletedCount++;
|
|
900 |
}
|
|
901 |
|
|
902 |
// If download was cancelled or failed, decrease started downloads count
|
|
903 |
else if ( aEvent.iProgressStatus == TFSProgress::EFSStatus_RequestCancelled ||
|
|
904 |
aEvent.iError )
|
|
905 |
{
|
|
906 |
iDownloadCountArray[idx].iDownloadsStartedCount--;
|
|
907 |
}
|
|
908 |
else
|
|
909 |
{
|
|
910 |
// do nothing with other request status values
|
|
911 |
}
|
|
912 |
aCountObject = iDownloadCountArray[idx];
|
|
913 |
|
|
914 |
if ( aCountObject.iDownloadsCompletedCount ==
|
|
915 |
aCountObject.iDownloadsStartedCount )
|
|
916 |
{
|
|
917 |
// The count entry can be removed from the array when all ongoing
|
|
918 |
// downloads have been completed or cancelled.
|
|
919 |
iDownloadCountArray.Remove(idx);
|
|
920 |
|
|
921 |
// Remove also all the connected download entries from the iDownloadArray.
|
|
922 |
for ( TInt i = iDownloadArray.Count()-1 ; i >=0 ; --i )
|
|
923 |
{
|
|
924 |
if ( iDownloadArray[i].iPartData.iMessageId == aCountObject.iMessageId )
|
|
925 |
{
|
|
926 |
iDownloadArray.Remove(i);
|
|
927 |
}
|
|
928 |
}
|
|
929 |
}
|
|
930 |
}
|
|
931 |
}
|
|
932 |
|
|
933 |
TBool CFSEmailDownloadInfoMediator::EqualMessageId( const TDownloadCount& aFirst, const TDownloadCount& aSecond )
|
|
934 |
{
|
|
935 |
return (aFirst.iMessageId == aSecond.iMessageId);
|
|
936 |
}
|
|
937 |
|
|
938 |
TBool CFSEmailDownloadInfoMediator::EqualMessagePartId( const TDownload& aFirst, const TDownload& aSecond )
|
|
939 |
{
|
|
940 |
return (aFirst.iPartData.iMessagePartId == aSecond.iPartData.iMessagePartId);
|
|
941 |
}
|
|
942 |
|