00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "InternetEmailEngine.h"
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 CInternetEmailEngine* CInternetEmailEngine::NewL( MInternetEmailEngineObserver& aObserver )
00044 {
00045 CInternetEmailEngine* engine=new(ELeave) CInternetEmailEngine( aObserver );
00046 CleanupStack::PushL(engine);
00047 engine->ConstructL();
00048 CleanupStack::Pop(engine);
00049 return engine;
00050 }
00051
00052
00053
00054
00055
00056
00057
00058
00059 CInternetEmailEngine::CInternetEmailEngine( MInternetEmailEngineObserver& aObserver )
00060 : CActive(CActive::EPriorityStandard), iObserver( aObserver )
00061 {
00062
00063 CActiveScheduler::Add(this);
00064 }
00065
00066
00067
00068
00069
00070
00071 CInternetEmailEngine::~CInternetEmailEngine()
00072 {
00073 if( IsActive() )
00074 {
00075 Cancel();
00076 }
00077 if(iMsvOp)
00078 {
00079 iMsvOp->Cancel();
00080 }
00081 delete iMsvOp;
00082 iMsvOp = NULL;
00083
00084 delete iEntry;
00085 iEntry = NULL;
00086
00087 delete iMtm;
00088 iMtm = NULL;
00089
00090 delete iRemoteEntries;
00091 iRemoteEntries = NULL;
00092
00093 delete iUiReg;
00094 iUiReg = NULL;
00095
00096 delete iClientReg;
00097 iClientReg = NULL;
00098
00099 delete iMsvSession;
00100 iMsvSession = NULL;
00101 }
00102
00103
00104
00105
00106
00107
00108 void CInternetEmailEngine::ConstructL()
00109 {
00110
00111
00112
00113 iMsvSession=CMsvSession::OpenAsyncL(*this);
00114
00115
00116 iProtocolType=EProtocolNULL;
00117 iProtocolUid=KUidMsgTypeSMTP;
00118 iIsProtocolSet=EFalse;
00119 iHasImapInbox=EFalse;
00120
00121
00122 iServiceId=KMsvRootIndexEntryId;
00123 iId=KMsvRootIndexEntryId;
00124 iRemoteEntries = new(ELeave) CMsvEntrySelection();
00125
00126
00127 iState=EInitialising;
00128 }
00129
00130
00131
00132
00133
00134
00135 void CInternetEmailEngine::CompleteConstructL()
00136 {
00137 iClientReg = CClientMtmRegistry::NewL( *iMsvSession );
00138 iUiReg = CMtmUiRegistry::NewL( *iMsvSession );
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148 void CInternetEmailEngine::RunL()
00149 {
00150
00151 TBuf8<1> null;
00152
00153 switch( iState )
00154 {
00155
00156
00157
00158
00159
00160 case EConnecting:
00161 {
00162
00163 if( iProtocolType==EProtocolPop3 )
00164 {
00165 MailCommandL(KPOP3MTMConnect, null);
00166 }
00167 else
00168 {
00169
00170
00171
00172
00173 MailCommandL(KIMAP4MTMConnectAndSynchronise, null);
00174 }
00175
00176 iState=EFetching;
00177 SetActive();
00178 }
00179 break;
00180
00181
00182
00183
00184
00185 case EFetching:
00186 {
00187
00188
00189 if( iProtocolType==EProtocolPop3 )
00190 {
00191 TImPop3GetMailInfo Pop3GetMailInfo;
00192
00193 Pop3GetMailInfo.iMaxEmailSize = KMaxTUint;
00194
00195 Pop3GetMailInfo.iDestinationFolder = iServiceId;
00196
00197 TPckgBuf<TImPop3GetMailInfo> package(Pop3GetMailInfo);
00198
00199 MailCommandL(KPOP3MTMCopyAllMailWhenAlreadyConnected,package);
00200 }
00201 else
00202 {
00203
00204
00205
00206 MailCommandL(KIMAP4MTMWaitForBackground,null);
00207 }
00208
00209 iState=EDisconnecting;
00210 SetActive();
00211 }
00212 break;
00213
00214
00215
00216
00217
00218
00219
00220 case EDisconnecting:
00221 {
00222
00223 if( iProtocolType==EProtocolPop3 )
00224 {
00225 MailCommandL(KPOP3MTMDisconnect,null);
00226 }
00227 else
00228 {
00229
00230
00231
00232
00233 MailCommandL(KIMAP4MTMDisconnect,null);
00234 }
00235
00236 iState=EDone;
00237 SetActive();
00238 }
00239 break;
00240
00241
00242
00243
00244
00245 case EDone:
00246 {
00247 UpdateRemoteCountL();
00248 iState = EReady;
00249 if(iMsvOp)
00250 {
00251 iMsvOp->Cancel();
00252 }
00253 delete iMsvOp;
00254 iMsvOp=NULL;
00255 }
00256 break;
00257
00258
00259
00260
00261
00262 case ECanceling:
00263 {
00264 UpdateRemoteCountL();
00265 iState= EReady;
00266 delete iMsvOp;
00267 iMsvOp=NULL;
00268 }
00269 break;
00270
00271 default:
00272
00273
00274 break;
00275 }
00276 }
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 void CInternetEmailEngine::MailCommandL( const TInt aCommand, TDes8& aParams)
00287 {
00288
00289 if( iServiceId != KMsvLocalServiceIndexEntryId )
00290 {
00291 iRemoteEntries->Reset();
00292 iRemoteEntries->AppendL(iServiceId);
00293
00294
00295 CMsvEntry* service = iMsvSession->GetEntryL(iServiceId);
00296 CleanupStack::PushL(service);
00297
00298
00299 if(service->Entry().iMtm == iProtocolUid)
00300 {
00301
00302 if(iMsvOp)
00303 {
00304 iMsvOp->Cancel();
00305 }
00306 delete iMsvOp;
00307 iMsvOp=NULL;
00308 iMsvOp=iMtm->InvokeAsyncFunctionL(aCommand,*iRemoteEntries,aParams,iStatus);
00309 if( iStatus != KRequestPending )
00310 {
00311 User::Leave( iStatus.Int() );
00312 }
00313 }
00314 CleanupStack::PopAndDestroy(service);
00315 }
00316 else
00317 {
00318 User::Leave(KErrNotFound);
00319 }
00320 }
00321
00322
00323
00324
00325
00326
00327
00328
00329 void CInternetEmailEngine::HandleSessionEventL(TMsvSessionEvent aEvent,
00330 TAny* ,TAny* ,TAny* )
00331 {
00332 switch(aEvent)
00333 {
00334
00335 case MMsvSessionObserver::EMsvServerReady:
00336 {
00337
00338
00339
00340
00341
00342 if(iState==EInitialising)
00343 {
00344 iState=EReadyButNeedsProtocol;
00345 CompleteConstructL();
00346 }
00347 }
00348 break;
00349
00350 case MMsvSessionObserver::EMsvEntriesCreated:
00351 case MMsvSessionObserver::EMsvEntriesChanged:
00352 case MMsvSessionObserver::EMsvEntriesDeleted:
00353 case MMsvSessionObserver::EMsvEntriesMoved:
00354
00355
00356 case MMsvSessionObserver::EMsvMtmGroupInstalled:
00357 case MMsvSessionObserver::EMsvMtmGroupDeInstalled:
00358 case MMsvSessionObserver::EMsvGeneralError:
00359 case MMsvSessionObserver::EMsvServerFailedToStart:
00360 case MMsvSessionObserver::EMsvCorruptedIndexRebuilt:
00361 case MMsvSessionObserver::EMsvCloseSession:
00362 case MMsvSessionObserver::EMsvServerTerminated:
00363 case MMsvSessionObserver::EMsvMediaChanged:
00364 case MMsvSessionObserver::EMsvMediaUnavailable:
00365 case MMsvSessionObserver::EMsvMediaAvailable:
00366 case MMsvSessionObserver::EMsvMediaIncorrect:
00367 case MMsvSessionObserver::EMsvCorruptedIndexRebuilding:
00368 break;
00369 }
00370 }
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 void CInternetEmailEngine::DisplayMessageL( const TMsvId &aId )
00381 {
00382
00383 TMsvEntry indexEntry;
00384 TMsvId serviceId;
00385 User::LeaveIfError( iMsvSession->GetEntry(aId, serviceId, indexEntry));
00386 CBaseMtm* mtm = iClientReg->NewMtmL(indexEntry.iMtm);
00387 CleanupStack::PushL(mtm);
00388
00389
00390 CBaseMtmUi* uiMtm = iUiReg->NewMtmUiL(*mtm);
00391 CleanupStack::PushL(uiMtm);
00392
00393
00394 uiMtm->BaseMtm().SwitchCurrentEntryL(indexEntry.Id());
00395 CMsvOperationWait* waiter=CMsvOperationWait::NewLC();
00396 waiter->Start();
00397 CMsvOperation* op = uiMtm->OpenL(waiter->iStatus);
00398 CleanupStack::PushL(op);
00399 CActiveScheduler::Start();
00400
00401
00402 CleanupStack::PopAndDestroy(4);
00403 }
00404
00405
00406
00407
00408
00409
00410
00411
00412 void CInternetEmailEngine::CancelOperation()
00413 {
00414 iState= ECanceling;
00415
00416 if( iMsvOp )
00417 {
00418 iMsvOp->Cancel();
00419 }
00420 }
00421
00422
00423
00424
00425
00426
00427 void CInternetEmailEngine::DoCancel()
00428 {
00429
00430 }
00431
00432
00433
00434
00435
00436
00437 TInt CInternetEmailEngine::RunError( const TInt aError)
00438 {
00439 return aError;
00440 }
00441
00442
00443
00444
00445
00446
00447 void CInternetEmailEngine::LoadMtmL()
00448 {
00449 if(iId == KMsvRootIndexEntryId)
00450 {
00451 User::Leave(KErrNotFound);
00452 }
00453
00454 TMsvEntry tmp;
00455 User::LeaveIfError(iMsvSession->GetEntry(iId,iServiceId,tmp));
00456 iMtm =InstantiateClientMtmL(iServiceId,iProtocolUid);
00457 }
00458
00459
00460
00461
00462
00463
00464
00465 CBaseMtm* CInternetEmailEngine::InstantiateClientMtmL(TMsvId aService, const TUid aType)
00466 {
00467
00468 CBaseMtm* newMtm = iClientReg->NewMtmL(aType);
00469 CleanupStack::PushL(newMtm);
00470
00471 CMsvEntry* entry = iMsvSession->GetEntryL(aService);
00472 CleanupStack::PushL(entry);
00473
00474
00475 newMtm->SetCurrentEntryL(entry);
00476 CleanupStack::Pop(2);
00477 return newMtm;
00478 }
00479
00480
00481
00482
00483
00484
00485
00486
00487 void CInternetEmailEngine::Queue()
00488 {
00489 if(!IsActive())
00490 {
00491 SetActive();
00492 }
00493
00494 TRequestStatus* st= &iStatus;
00495 User::RequestComplete(st,KErrNone);
00496 }
00497
00498
00499
00500
00501
00502
00503 TBool CInternetEmailEngine::CheckIfExistsL( const TInt aTypeEnum )
00504 {
00505 TUid temp = ( aTypeEnum == EProtocolImap4 ) ? KUidMsgTypeIMAP4 : KUidMsgTypePOP3;
00506 TMsvId id=FindServiceL(temp);
00507 return ( id == KMsvRootIndexEntryId ) ? EFalse : ETrue;
00508 }
00509
00510
00511
00512
00513
00514
00515
00516 TPtrC CInternetEmailEngine::RemoteEmailTextL( const TInt aIndex )
00517 {
00518 if( iState == ECanceling )
00519 {
00520 User::Leave( KErrCancel );
00521 }
00522
00523 TMsvId entryId=(*iRemoteEntries)[aIndex];
00524 if(iEntry==NULL || iEntry->Entry().Id()!=entryId)
00525 {
00526 delete iEntry;
00527 iEntry=NULL;
00528 iEntry=iMsvSession->GetEntryL(entryId);
00529 }
00530 return iEntry->Entry().iDescription;
00531 }
00532
00533
00534
00535
00536
00537
00538 TPtrC CInternetEmailEngine::RemoteEmailSenderL( const TInt aIndex )
00539 {
00540 if( iState == ECanceling )
00541 {
00542 User::Leave( KErrCancel );
00543 }
00544
00545 TMsvId entryId=(*iRemoteEntries)[aIndex];
00546 if(iEntry==NULL || iEntry->Entry().Id()!=entryId)
00547 {
00548 delete iEntry;
00549 iEntry=NULL;
00550 iEntry=iMsvSession->GetEntryL(entryId);
00551 }
00552 return iEntry->Entry().iDetails;
00553 }
00554
00555
00556
00557
00558
00559
00560 TInt CInternetEmailEngine::RemoteEmailCount()
00561 {
00562 return iRemoteCount;
00563 }
00564
00565
00566
00567
00568
00569
00570 void CInternetEmailEngine::RemoteOpenEmailL( TInt aIndex )
00571 {
00572 TMsvId entryId=(*iRemoteEntries)[aIndex];
00573 if(aIndex<0 || aIndex>=iRemoteCount)
00574 {
00575 User::Leave(KErrGeneral);
00576 }
00577 iState=EReady;
00578 DisplayMessageL(entryId);
00579 }
00580
00581
00582
00583
00584
00585
00586 void CInternetEmailEngine::RemoteFetchL()
00587 {
00588 iState=EConnecting;
00589 Queue();
00590 }
00591
00592
00593
00594
00595
00596
00597
00598 CMsvEntrySelection* CInternetEmailEngine::UpdateEntriesL(
00599 const TMsvId& aServiceId)
00600 {
00601 CMsvEntry* entry=iMsvSession->GetEntryL(aServiceId);
00602 CleanupStack::PushL(entry);
00603 CMsvEntrySelection* entries=entry->ChildrenL();
00604 CleanupStack::PopAndDestroy(entry);
00605 return entries;
00606 }
00607
00608
00609
00610
00611
00612
00613
00614
00615 void CInternetEmailEngine::UpdateRemoteCountL()
00616 {
00617
00618 iRemoteCount=0;
00619 delete iRemoteEntries;
00620 iRemoteEntries=NULL;
00621
00622
00623 if( iProtocolType == EProtocolImap4 && !iHasImapInbox )
00624 {
00625 iId=FindFolderThenServiceL(iProtocolUid);
00626 }
00627
00628
00629
00630
00631
00632
00633 iRemoteEntries=UpdateEntriesL(iId);
00634
00635
00636
00637 if(iRemoteEntries->Count()!=iRemoteCount)
00638 {
00639 iRemoteCount=iRemoteEntries->Count();
00640 iObserver.HandleEngineChangedEventL(ERemoteCountChanged);
00641 }
00642 }
00643
00644
00645
00646
00647
00648
00649 TBool CInternetEmailEngine::IsProtocolSet()
00650 {
00651 return iIsProtocolSet;
00652 }
00653
00654
00655
00656
00657
00658
00659 TBool CInternetEmailEngine::IsEngineReady()
00660 {
00661 return (iClientReg!=NULL)?(ETrue):(EFalse);
00662 }
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673 void CInternetEmailEngine::SetProtocolL( const TInt aType )
00674 {
00675 iProtocolType=aType;
00676 iProtocolUid=(iProtocolType==EProtocolPop3)?KUidMsgTypePOP3:KUidMsgTypeIMAP4;
00677 iIsProtocolSet=ETrue;
00678 iState=EReady;
00679 iId=FindServiceL(iProtocolUid);
00680 LoadMtmL();
00681
00682 if( iProtocolType!=EProtocolPop3)
00683 {
00684 TRAPD( err, iId=FindFolderThenServiceL(iProtocolUid));
00685 if( err != KErrNone )
00686 {
00687
00688 return;
00689 }
00690 }
00691
00692 UpdateRemoteCountL();
00693
00694
00695 }
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705 TMsvId CInternetEmailEngine::FindServiceL(TUid aType)
00706 {
00707
00708 CMsvEntry* currentEntry = iMsvSession->GetEntryL(KMsvRootIndexEntryId);
00709 CleanupStack::PushL(currentEntry);
00710
00711
00712 currentEntry->SetSortTypeL(TMsvSelectionOrdering(KMsvNoGrouping,EMsvSortByNone, ETrue));
00713
00714 TMsvId rc = KMsvRootIndexEntryId;
00715 TInt count=currentEntry->Count();
00716
00717 for(TInt i = 0;i<count;i++)
00718 {
00719 const TMsvEntry& child = (*currentEntry)[i];
00720
00721
00722 if (child.iMtm == aType)
00723 {
00724 rc=child.Id();
00725 break;
00726 }
00727 }
00728
00729 CleanupStack::PopAndDestroy(currentEntry);
00730
00731
00732 return rc;
00733 }
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744 TMsvId CInternetEmailEngine::FindFolderThenServiceL(TUid aType)
00745 {
00746
00747
00748 CMsvEntry* currentEntry = iMsvSession->GetEntryL(KMsvRootIndexEntryId);
00749 CleanupStack::PushL(currentEntry);
00750
00751
00752 currentEntry->SetSortTypeL(TMsvSelectionOrdering(KMsvNoGrouping,EMsvSortByNone, ETrue));
00753
00754 TMsvId rc = KMsvRootIndexEntryId;
00755 TInt count=currentEntry->Count();
00756
00757 for(TInt i = 0;i<count;i++)
00758 {
00759 const TMsvEntry& child = (*currentEntry)[i];
00760
00761
00762 if (child.iMtm == aType)
00763 {
00764
00765 CMsvEntry* firstEntry = iMsvSession->GetEntryL(child.Id());
00766 CleanupStack::PushL(firstEntry);
00767 TInt innercount=firstEntry->Count();
00768 if( innercount )
00769 {
00770 const TMsvEntry& folder = (*firstEntry)[0];
00771 rc=folder.Id();
00772 iHasImapInbox=ETrue;
00773 }
00774 else
00775 {
00776 iHasImapInbox=EFalse;
00777 }
00778 CleanupStack::PopAndDestroy(firstEntry);
00779 break;
00780 }
00781 }
00782
00783 CleanupStack::PopAndDestroy(currentEntry);
00784
00785 if( !iHasImapInbox )
00786 {
00787 User::Leave( KErrNotFound );
00788 }
00789
00790
00791 return rc;
00792 }
00793
00794