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