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
00032 #include "TimeOutTimer.h"
00033 #include "Response.h"
00034 #include "Request.h"
00035 #include "TaskManagerEngine.h"
00036 #include "TaskManagerEngineReader.h"
00037 #include "TaskManager.pan"
00038
00039 #include <msvids.h>
00040 #include <txtrich.h>
00041 #include <mtclreg.h>
00042 #include <mtclbase.h>
00043 #include <commdb.h>
00044 #include <commdbconnpref.h>
00045 #include <es_enum.h>
00046 #include <in_sock.h>
00047 #include <eikenv.h>
00048 #include <s32mem.h>
00049 #include <e32std.h>
00050 #include <securesocket.h>
00051 #include <SocketTaskManager.rsg>
00052 #include <ssl_internal.h>
00053
00054
00055
00056 _LIT( KSSL3, "SSL3.0" );
00057
00058 _LIT( KGPRSStartError, "Error starting GPRS service" );
00059 _LIT( KLookupError, "Error looking up server IP");
00060 _LIT( KConnectionError, "Error in forming the connection" );
00061 _LIT( KSSLConnError, "Error forming SSL link between client and server" );
00062 _LIT( KWriteError, "Error writing data to server" );
00063 _LIT( KErroneousPackage, "Package received was erroneous" );
00064
00065 _LIT(KIapNotSet, "IAP not selected");
00066 _LIT(KServerNotSet, "Server name not set");
00067 _LIT(KSmsUpdateMessage, "TaskManagerUpdate");
00068
00069 const TInt CTaskManagerEngine::KTimeOut = 30000000;
00070
00071
00072
00073
00074 CTaskManagerEngine::CTaskManagerEngine(MTransactionObserver& aObserver)
00075 : CActive( EPriorityStandard ), iState( ENotConnected ),
00076 iOperation( TRequest::EFetchTasks ), iMarkId( 0 ),
00077 iTransactionObserver( aObserver )
00078
00079 {
00080 }
00081
00082
00083 CTaskManagerEngine::~CTaskManagerEngine()
00084 {
00085 Cancel();
00086
00087 if( iReader )
00088 {
00089 iReader->Cancel();
00090 }
00091 delete iReader;
00092
00093 delete iTimer;
00094 delete iSecureSocket;
00095
00096 iConnection.Close();
00097 iSocketServer.Close();
00098
00099 delete iMsvEntry;
00100 delete iMsvSession;
00101 iIAPs.Close();
00102 }
00103
00104
00105
00106
00107
00108
00109 CTaskManagerEngine* CTaskManagerEngine::NewL(MTransactionObserver& aObserver)
00110 {
00111 CTaskManagerEngine* self = new (ELeave) CTaskManagerEngine(aObserver);
00112 CleanupStack::PushL(self);
00113 self->ConstructL();
00114 CleanupStack::Pop(self);
00115 return self;
00116 }
00117
00118
00119
00120
00121
00122
00123 void CTaskManagerEngine::ConstructL()
00124 {
00125 User::LeaveIfError(iSocketServer.Connect());
00126 User::LeaveIfError(iConnection.Open(iSocketServer));
00127
00128
00129 iTimer = CTimeOutTimer::NewL( EPriorityHigh, *this );
00130
00131 iReader = CTaskManagerEngineReader::NewL( *this );
00132
00133 CActiveScheduler::Add( this );
00134
00135 iMsvSession = CMsvSession::OpenAsyncL(*this);
00136
00137 LoadIapsL();
00138
00139
00140 if (iIAPs.Count() == 0)
00141 {
00142 CEikonEnv::Static()->LeaveWithInfoMsg(R_TASKMANAGER_NO_IAPS_DEFINED);
00143 }
00144
00145 }
00146
00147
00148
00149
00150
00151
00152 void CTaskManagerEngine::SetConnectionSettings(const TDesC& aServerName,
00153 const TInt& aPort,
00154 const TDesC& aUsername,
00155 const TDesC& aPassword)
00156 {
00157 iUsername.Copy(aUsername);
00158 iPassword.Copy(aPassword);
00159 iServer.Copy(aServerName);
00160 iPort = aPort;
00161 }
00162
00163
00164
00165
00166
00167
00168 void CTaskManagerEngine::SetIap(const TUint32& aId)
00169 {
00170 iIap = aId;
00171 }
00172
00173
00174
00175
00176
00177
00178 TBool CTaskManagerEngine::IapSet() const
00179 {
00180 if (iIap == 0)
00181 {
00182 return EFalse;
00183 }
00184 else
00185 {
00186 return ETrue;
00187 }
00188 }
00189
00190
00191
00192
00193
00194
00195
00196 TBool CTaskManagerEngine::CheckAndReportErrorsL()
00197 {
00198 if (!IapSet())
00199 {
00200 iTransactionObserver.ErrorL(KIapNotSet);
00201 return ETrue;
00202 }
00203
00204 if (iServer.Length() == 0)
00205 {
00206 iTransactionObserver.ErrorL(KServerNotSet);
00207 return ETrue;
00208 }
00209
00210 return EFalse;
00211 }
00212
00213
00214
00215
00216
00217
00218
00219 void CTaskManagerEngine::FetchTasksL()
00220 {
00221 iOperation = TRequest::EFetchTasks;
00222
00223 iTransactionObserver.ConnectingToServerL( ETrue );
00224
00225 iRunning = ETrue;
00226 GPRSConnectL();
00227 }
00228
00229
00230
00231
00232
00233
00234
00235 void CTaskManagerEngine::MarkTaskDoneL(const TInt& aTaskId)
00236 {
00237 iOperation = TRequest::ETaskDone;
00238 iMarkId.Num( aTaskId );
00239
00240 iRunning = ETrue;
00241 GPRSConnectL();
00242 }
00243
00244
00245
00246
00247
00248
00249
00250 void CTaskManagerEngine::CancelTransaction()
00251 {
00252 Cancel();
00253
00254 iRunning = EFalse;
00255
00256 TRAPD( error, iTransactionObserver.CancelledL() );
00257 if( error != KErrNone )
00258 {
00259 Panic( error );
00260 }
00261
00262 }
00263
00264
00265
00266
00267
00268
00269
00270
00271 void CTaskManagerEngine::CheckRefreshL()
00272 {
00273 if (iRunning)
00274 {
00275 return;
00276 }
00277
00278 if (iDoRefresh)
00279 {
00280 iDoRefresh = EFalse;
00281 FetchTasksL();
00282 }
00283 }
00284
00285
00286
00287
00288
00289
00290
00291 void CTaskManagerEngine::SetAutomaticUpdateL(const TBool& aOn)
00292 {
00293 iAutomaticUpdate = aOn;
00294 if (iAutomaticUpdate)
00295 {
00296 CheckRefreshL();
00297 }
00298 }
00299
00300
00301
00302
00303
00304
00305
00306 void CTaskManagerEngine::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* )
00307 {
00308 switch (aEvent)
00309 {
00310 case EMsvServerReady:
00311 if (!iMsvEntry)
00312 {
00313 iMsvEntry = CMsvEntry::NewL(*iMsvSession, KMsvGlobalInBoxIndexEntryId, TMsvSelectionOrdering());
00314 }
00315 break;
00316
00317 case EMsvEntriesCreated:
00318 if (*(static_cast<TMsvId*>(aArg2)) == KMsvGlobalInBoxIndexEntryId)
00319 {
00320 CMsvEntrySelection* entries = static_cast<CMsvEntrySelection*>(aArg1);
00321
00322 iMsvEntry->SetEntryL(entries->At(0));
00323 TMsvEntry msvEntry(iMsvEntry->Entry());
00324 msvEntry.SetVisible(EFalse);
00325
00326 CClientMtmRegistry* mtmReg = CClientMtmRegistry::NewL(*iMsvSession);
00327 CleanupStack::PushL(mtmReg);
00328 CBaseMtm* smsMtm = mtmReg->NewMtmL(msvEntry.iMtm);
00329 smsMtm->SwitchCurrentEntryL(entries->At(0));
00330 smsMtm->LoadMessageL();
00331 TBool CorrectSms = EFalse;
00332
00333
00334 if (smsMtm->Body().Read(0,KSmsUpdateMessage().Length()).Compare(KSmsUpdateMessage)==0)
00335 {
00336 msvEntry.SetUnread(EFalse);
00337 CorrectSms = ETrue;
00338 }
00339
00340 else
00341 {
00342 msvEntry.SetVisible(ETrue);
00343 }
00344
00345 iMsvEntry->ChangeL(msvEntry);
00346
00347 CleanupStack::PopAndDestroy(smsMtm);
00348
00349
00350 if (CorrectSms)
00351 {
00352
00353 iMsvEntry->DeleteL(entries->At(0));
00354
00355
00356
00357 if (iRunning || iAutomaticUpdate)
00358 {
00359 iDoRefresh = ETrue;
00360 }
00361
00362 else
00363 {
00364 FetchTasksL();
00365 }
00366 }
00367 }
00368 break;
00369
00370 default:
00371 break;
00372 }
00373 }
00374
00375
00376
00377
00378
00379
00380 void CTaskManagerEngine::LoadIapsL()
00381 {
00382
00383 CCommsDatabase* commDb = CCommsDatabase::NewL(EDatabaseTypeIAP);
00384 CleanupStack::PushL(commDb);
00385
00386
00387 CCommsDbTableView* commView = commDb->OpenIAPTableViewMatchingBearerSetLC(ECommDbBearerCSD|ECommDbBearerGPRS,ECommDbConnectionDirectionOutgoing);
00388
00389
00390 if (commView->GotoFirstRecord() == KErrNone)
00391 {
00392 do
00393 {
00394 TIap iap;
00395 commView->ReadTextL(TPtrC(COMMDB_NAME), iap.iName);
00396 commView->ReadUintL(TPtrC(COMMDB_ID), iap.iId);
00397 User::LeaveIfError(iIAPs.Append(iap));
00398 }
00399 while (commView->GotoNextRecord() == KErrNone);
00400 }
00401 CleanupStack::PopAndDestroy(commView);
00402 CleanupStack::PopAndDestroy(commDb);
00403 }
00404
00405
00406
00407
00408
00409
00410 RArray<TIap>& CTaskManagerEngine::Iaps()
00411 {
00412 return iIAPs;
00413 }
00414
00415
00416
00417
00418
00419
00420
00421 void CTaskManagerEngine::ConnectL()
00422 {
00423 if( iState == ENotConnected )
00424 {
00425 TInetAddr addr;
00426
00427
00428 if( addr.Input( iServer ) == KErrNone )
00429 {
00430 ConnectL( addr.Address() );
00431 }
00432 else
00433 {
00434
00435 User::LeaveIfError( iResolver.Open( iSocketServer, KAfInet, KProtocolInetUdp) );
00436
00437
00438 iResolver.GetByName( iServer, iNameEntry, iStatus );
00439
00440 iState = ELookingUp;
00441 iTimer->After( KTimeOut );
00442 SetActive();
00443 }
00444 }
00445 }
00446
00447
00448
00449
00450
00451
00452
00453 void CTaskManagerEngine::ConnectL( TUint32 aAddr )
00454 {
00455 if( iState == ENotConnected )
00456 {
00457
00458 #ifdef __WINS__
00459
00460 User::LeaveIfError( iSocket.Open( iSocketServer,
00461 KAfInet,
00462 KSockStream,
00463 KProtocolInetTcp ) );
00464 #else
00465 User::LeaveIfError( iSocket.Open( iSocketServer,
00466 KAfInet,
00467 KSockStream,
00468 KProtocolInetTcp,
00469 iConnection ) );
00470 #endif
00471
00472 iAddress.SetPort( iPort );
00473 iAddress.SetAddress( aAddr );
00474
00475
00476 iSocket.Connect( iAddress, iStatus );
00477 iState = EConnecting;
00478
00479 iTimer->After( KTimeOut );
00480
00481 SetActive();
00482 }
00483 }
00484
00485
00486
00487
00488
00489
00490 void CTaskManagerEngine::DoCancel()
00491 {
00492 iTimer->Cancel();
00493
00494 switch( iState )
00495 {
00496 case EStartingGPRS:
00497 {
00498 iConnection.Stop();
00499 break;
00500 }
00501 case ELookingUp:
00502 {
00503 iResolver.Cancel();
00504 iResolver.Close();
00505 iState = ENotConnected;
00506 break;
00507 }
00508 case EConnecting:
00509 {
00510 iSocket.CancelConnect();
00511 iSocket.Close();
00512 iState = ENotConnected;
00513 break;
00514 }
00515 case ESSLConnecting:
00516 {
00517 iSecureSocket->CancelHandshake();
00518
00519 EndConnection();
00520 break;
00521 }
00522 case EWriting:
00523 {
00524 iSecureSocket->CancelSend();
00525 iReader->Cancel();
00526
00527 EndConnection();
00528 break;
00529 }
00530 default:
00531 {
00532 Panic( ETaskManagerInvalidState );
00533 break;
00534 }
00535 };
00536 }
00537
00538
00539
00540
00541
00542
00543
00544 void CTaskManagerEngine::RunL()
00545 {
00546 iTimer->Cancel();
00547
00548 switch( iState )
00549 {
00550
00551 case EStartingGPRS:
00552 {
00553 iState = ENotConnected;
00554
00555 if( iStatus == KErrNone )
00556 {
00557 iOpeningConnection = EFalse;
00558 iTransactionObserver.ConnectingToServerL( (iOperation == TRequest::EFetchTasks ) );
00559 ConnectL();
00560 }
00561 else
00562 {
00563 iTransactionObserver.ErrorL( KGPRSStartError );
00564 }
00565 break;
00566 }
00567
00568 case ELookingUp:
00569 {
00570 iResolver.Close();
00571 iState = ENotConnected;
00572
00573 if( iStatus == KErrNone )
00574 {
00575 iNameRecord = iNameEntry();
00576
00577 TBuf<15> ipAddr;
00578 TInetAddr::Cast( iNameRecord.iAddr ).Output( ipAddr );
00579
00580 ConnectL( TInetAddr::Cast( iNameRecord.iAddr ).Address() );
00581 }
00582 else
00583 {
00584 iTransactionObserver.ErrorL( KLookupError );
00585 }
00586
00587 break;
00588 }
00589
00590 case EConnecting:
00591 {
00592
00593 if( iStatus == KErrNone )
00594 {
00595 iState = ESSLConnecting;
00596 iSecureSocket = CSecureSocket::NewL( iSocket, KSSL3 );
00597
00598 #ifdef __SERIES60_30__
00599
00600
00601
00602 TBuf8<KMaxServerNameLength> tempName;
00603 tempName.Copy(iServer);
00604 iSecureSocket->SetOpt(KSoSSLDomainName, KSolInetSSL, tempName);
00605 #endif
00606
00607 iSecureSocket->StartClientHandshake( iStatus );
00608 iTimer->After( KTimeOut );
00609 SetActive();
00610 }
00611 else
00612 {
00613 iSocket.Close();
00614 iTransactionObserver.ErrorL( KConnectionError );
00615 iState = ENotConnected;
00616 }
00617
00618 break;
00619 }
00620
00621 case ESSLConnecting:
00622 {
00623 if( iStatus == KErrNone )
00624 {
00625 iState = EConnected;
00626 iReader->SetSecureSocket( iSecureSocket );
00627
00628
00629 TRequest::GetMessage( iUsername, iPassword, iOperation, iMarkId, iWriteBuffer );
00630
00631
00632 iReader->Start();
00633
00634
00635 iSecureSocket->Send( iWriteBuffer, iStatus );
00636 iState = EWriting;
00637
00638
00639 iTimer->After( KTimeOut );
00640 SetActive();
00641 }
00642 else
00643 {
00644
00645
00646 iSecureSocket->Close();
00647 delete iSecureSocket;
00648 iSecureSocket = NULL;
00649
00650 iState = ENotConnected;
00651 iTransactionObserver.ErrorL( KSSLConnError );
00652 }
00653 break;
00654 }
00655 case EWriting:
00656 {
00657 if( iStatus == KErrNone )
00658 {
00659 iTimer->After( KTimeOut );
00660 iState = EReading;
00661 }
00662 else
00663 {
00664 iState = EConnected;
00665
00666 iTransactionObserver.ErrorL( KWriteError );
00667 }
00668 break;
00669 }
00670 default:
00671 {
00672 User::Leave( ETaskManagerInvalidState );
00673 }
00674 };
00675 }
00676
00677 TInt CTaskManagerEngine::RunError( TInt )
00678 {
00679 return KErrNone;
00680 }
00681
00682
00683
00684
00685
00686
00687
00688 void CTaskManagerEngine::GPRSConnectL()
00689 {
00690
00691 #ifndef __WINS__
00692 TBool connected = EFalse;
00693
00694
00695 TUint connectionCount;
00696 User::LeaveIfError(iConnection.EnumerateConnections(connectionCount));
00697 TPckgBuf<TConnectionInfo> connectionInfo;
00698 for (TUint i = 1; i <= connectionCount; i++)
00699 {
00700 User::LeaveIfError(iConnection.GetConnectionInfo(i, connectionInfo));
00701 if (connectionInfo().iIapId == iIap)
00702 {
00703 connected = ETrue;
00704 break;
00705 }
00706 }
00707
00708
00709 if (!connected)
00710 {
00711
00712 TCommDbConnPref prefs;
00713 prefs.SetIapId(iIap);
00714 prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
00715
00716
00717 iTransactionObserver.OpeningConnectionL();
00718
00719
00720 iOpeningConnection = ETrue;
00721 iConnection.Start(prefs, iStatus);
00722
00723 iState = EStartingGPRS;
00724 iTimer->After( KTimeOut );
00725 SetActive();
00726
00727 return;
00728 }
00729 #endif // __WINS__
00730
00731 ConnectL();
00732 }
00733
00734
00735
00736
00737
00738
00739
00740
00741 TBool CTaskManagerEngine::PackageReceivedL( const TDesC8& aData )
00742 {
00743
00744 iTimer->Cancel();
00745
00746 if( !iResponse )
00747 {
00748 iResponse = CResponse::NewL();
00749 }
00750
00751 iResponse->InputDataL( aData );
00752
00753 switch( iResponse->GetState() )
00754 {
00755 case CResponse::ENotComplete:
00756 {
00757 iTimer->After( KTimeOut );
00758 return ETrue;
00759 }
00760 case CResponse::EError:
00761 {
00762 iTransactionObserver.ErrorL( KErroneousPackage );
00763 }
00764 case CResponse::EComplete:
00765 {
00766 iTransactionObserver.SuccessL( *iResponse );
00767 CheckRefreshL();
00768 }
00769 }
00770
00771 EndConnection();
00772 return EFalse;
00773 }
00774
00775
00776
00777
00778
00779
00780 void CTaskManagerEngine::EndConnection()
00781 {
00782 delete iResponse;
00783 iResponse = NULL;
00784
00785 if( iSecureSocket )
00786 {
00787 iSecureSocket->Close();
00788 }
00789 else
00790 {
00791 iSocket.Close();
00792 }
00793
00794 delete iSecureSocket;
00795 iSecureSocket = NULL;
00796
00797 iRunning = EFalse;
00798 iState = ENotConnected;
00799 }
00800
00801
00802
00803
00804
00805
00806
00807 void CTaskManagerEngine::TimerExpired()
00808 {
00809 TRAPD( err, iTransactionObserver.FailedL( 1 ) );
00810
00811 if( err != KErrNone )
00812 {
00813 Panic( err );
00814 }
00815 Cancel();
00816 }
00817
00818