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