⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 taskmanagerengine.cpp

📁 c++下s60终端对终端传输协议
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		// Indicates that transaction succeeded.
		case THTTPEvent::ESucceeded:
			{
			iTransaction.Close();
			iRunning = EFalse;
			
			CResponse* response = CResponse::NewLC();
			
			// After transaction has been closed, leaves are no 
			// longer handled by the calling active object.
			TRAPD(error,
			response->InputDataL(*iReceivedData);
			iTransactionObserver.SuccessL(*response);
			CheckRefreshL();)
			PanicIfError(error);

			CleanupStack::PopAndDestroy(response);
			}
			break;

		// Transaction completed with failure.
		case THTTPEvent::EFailed:
			{			 
			iTransaction.Close();
			iRunning = EFalse;

			// After transaction has been closed, leaves are no 
			// longer handled by the calling active object.
			TRAPD(error,
			iTransactionObserver.FailedL(0);
			CheckRefreshL();)
			PanicIfError(error);
			}
			break;

		default:
			// There are more events in THTTPEvent, but they are not usually 
			// needed. However, event status smaller than zero should be handled 
			// correctly since it's error.
			{
			if (aEvent.iStatus < 0)
				{
				// Just close the transaction on errors
				iTransaction.Close();
				iRunning = EFalse;
				
				// After transaction has been closed, leaves are no 
				// longer handled by the calling active object.
				TRAPD(error,
				iTransactionObserver.FailedL(aEvent.iStatus);
				CheckRefreshL();)
				PanicIfError(error);
				} 
			else 
				{
				// Other events are not errors (e.g. permanent and temporary
				// redirections)
				}
			}
			break;
		}
	}

// ----------------------------------------------------
// CTaskManagerEngine::MHFRunError()
// Called when a leave occurs in handling a transaction 
// event.
// ----------------------------------------------------
//	
TInt CTaskManagerEngine::MHFRunError(	TInt aError, 
										RHTTPTransaction /*aTransaction*/, 
										const THTTPEvent& /*aEvent*/)
	{
	// only report about the error.
	TRAPD(error, iTransactionObserver.FailedL(aError);)
	return error;
	}

// ----------------------------------------------------
// CTaskManagerEngine::CancelTransaction()
// Used for cancelling an HTTP transaction or the 
// creation of a GPRS connection.
// ----------------------------------------------------
//		
void CTaskManagerEngine::CancelTransaction()
	{
	// if we are opening up a GPRS connection.
	if (iOpeningConnection)
		{
		iConnOpener->Cancel();
		TRAPD(error, iTransactionObserver.CancelledL())
		PanicIfError(error);
		return;
		}
		
	// HTTP transaction not running
	if(!iRunning) 
		return;

	// Close() also cancels transaction (Cancel() can also be used but 
	// resources allocated by transaction must be still freed with Close())
	iTransaction.Close();

	iRunning = EFalse;
	TRAPD(	error,
			iTransactionObserver.CancelledL());
	PanicIfError(error);
	}

// ----------------------------------------------------
// CTaskManagerEngine::WriteDataL()
// Will write to given data into a member buffer. Used 
// for saving the data received by the HTTP transaction.
// ----------------------------------------------------
//
void CTaskManagerEngine::WriteDataL(const TDesC8& aData)
	{
	// some data already received
	if (iReceivedData)
		{
		iReceivedData = iReceivedData->ReAllocL(iReceivedData->Length() + aData.Length());
		HBufC* newData = HBufC::NewLC(aData.Length());
		newData->Des().Copy(aData);
		iReceivedData->Des() += newData->Des();
		CleanupStack::PopAndDestroy(newData);
		}
	// no data yet received.
	else
		{
		iReceivedData = HBufC::NewL(aData.Length());
		iReceivedData->Des().Copy(aData);
		}
	}

// ----------------------------------------------------
// CTaskManagerEngine::ResetData()
// Resets the received data.
// ----------------------------------------------------
//	
void CTaskManagerEngine::ResetData()
	{
	delete iReceivedData;
	iReceivedData = NULL;
	}
	
// ----------------------------------------------------
// CTaskManagerEngine::CheckRefreshL()
// An SMS update message, indicating an updated task
// list on the server, may have been received during
// an HTTP transaction. This function checks if such a
// postponed request has been made and, assuming an
// HTTP transaction isn't running, executes the
// request.
// ----------------------------------------------------
//
void CTaskManagerEngine::CheckRefreshL()
	{
	// if HTTP transaction is running, don't fetch tasks.
	if (iRunning)
		{
		return;
		}
		
	if (iDoRefresh)
		{
		iDoRefresh = EFalse;
		FetchTasksL();
		}
	}

// ----------------------------------------------------
// CTaskManagerEngine::SetAutomaticUpdateL()
// Automatic task download can be set on or off through
// this function. If on, the client will try to fetch
// a new task list as soon as it receives an SMS
// message.
// ----------------------------------------------------
//	
void CTaskManagerEngine::SetAutomaticUpdateL(const TBool& aOn)
	{
	iAutomaticUpdate = aOn;
	if (iAutomaticUpdate)
		{
		CheckRefreshL();
		}
	}

// ----------------------------------------------------
// CTaskManagerEngine::HandleSessionEventL()
// Indicates an event in the message server has occurred. 
// Used here for listening incoming SMS messages.
// ----------------------------------------------------
//	
void CTaskManagerEngine::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* /*aArg3*/)
	{
	switch (aEvent)
		{
		case EMsvServerReady:
			if (!iMsvEntry)
				{
				iMsvEntry = CMsvEntry::NewL(*iMsvSession, KMsvGlobalInBoxIndexEntryId, TMsvSelectionOrdering());
				}
			break;
			
		case EMsvEntriesCreated:
			if (*(static_cast<TMsvId*>(aArg2)) == KMsvGlobalInBoxIndexEntryId)
				{
				CMsvEntrySelection* entries = static_cast<CMsvEntrySelection*>(aArg1);

				iMsvEntry->SetEntryL(entries->At(0));
				TMsvEntry msvEntry(iMsvEntry->Entry());
				msvEntry.SetVisible(EFalse);

				CClientMtmRegistry* mtmReg = CClientMtmRegistry::NewL(*iMsvSession);
				CleanupStack::PushL(mtmReg);
				CBaseMtm* smsMtm = mtmReg->NewMtmL(msvEntry.iMtm);
				smsMtm->SwitchCurrentEntryL(entries->At(0));
				smsMtm->LoadMessageL();
				TBool CorrectSms = EFalse;
				
				// received SMS message is a 'Task manager update' SMS.
				if (smsMtm->Body().Read(0,KSmsUpdateMessage().Length()).Compare(KSmsUpdateMessage)==0)
					{
					msvEntry.SetUnread(EFalse);
					CorrectSms = ETrue;
					}
				// not a 'Task manager update' SMS, show it to user.
				else
					{
					msvEntry.SetVisible(ETrue);
					}
					
				iMsvEntry->ChangeL(msvEntry);

				CleanupStack::PopAndDestroy(smsMtm);

				// if received SMS message was a 'Task Manager update' SMS.
				if (CorrectSms)
					{
					// delete the received SMS message for it is not intended for the user to read.
					iMsvEntry->DeleteL(entries->At(0));
					
					// if an HTTP transaction is running or program is in the background or busy. 
					// Don't fetch tasks yet.
					if (iRunning || !iAutomaticUpdate)
						{
						iDoRefresh = ETrue;
						}
					// HTTP transaction is not running, fetch tasks.
					else
						{
						FetchTasksL();
						}
					}
				}
			break;
			
			default:
			break;
		}
	}

// ----------------------------------------------------
// CTaskManagerEngine::LoadIapsL()
// Loads all IAPs of the device.
// ----------------------------------------------------
//	
void CTaskManagerEngine::LoadIapsL()
	{
	// open commdb
	CCommsDatabase* commDb = CCommsDatabase::NewL(EDatabaseTypeIAP);
	CleanupStack::PushL(commDb);

	// open IAP table
	CCommsDbTableView* commView = commDb->OpenIAPTableViewMatchingBearerSetLC(ECommDbBearerCSD|ECommDbBearerGPRS,ECommDbConnectionDirectionOutgoing);
	
	// search all IAPs
	if (commView->GotoFirstRecord() == KErrNone)
		{
		do
			{
			TIap iap;
			commView->ReadTextL(TPtrC(COMMDB_NAME), iap.iName);
			commView->ReadUintL(TPtrC(COMMDB_ID), iap.iId);
			User::LeaveIfError(iIAPs.Append(iap));
			}
		while (commView->GotoNextRecord() == KErrNone);
		}
	CleanupStack::PopAndDestroy(/*commView*/);
	CleanupStack::PopAndDestroy(/*commDb*/);
	}

// ----------------------------------------------------
// CTaskManagerEngine::Iaps()
// Returns all IAPs of the device.
// ----------------------------------------------------
//		
RArray<TIap>& CTaskManagerEngine::Iaps() 
	{
	return iIAPs;
	}
	
// ----------------------------------------------------
// CTaskManagerEngine::ConnectL()
// Will open up a (GPRS) connection.
// ----------------------------------------------------
//		
void CTaskManagerEngine::ConnectL()
	{
	// this functionality is not applicable for the emulator
	#ifndef __WINS__ 
	
	TBool connected = EFalse;
	
	// Lets first check are we already connected.
	TUint connectionCount;
	User::LeaveIfError(iConnection.EnumerateConnections(connectionCount));
	TPckgBuf<TConnectionInfoV2> connectionInfo; 
	for (TUint i = 1; i <= connectionCount; i++)
		{
		User::LeaveIfError(iConnection.GetConnectionInfo(i, connectionInfo));
		if (connectionInfo().iIapId == iIap)
			{
			connected = ETrue;
			break;
			}
		}
	
	// Not yet connected, start connection
	if (!connected)
		{
		//Define preferences for connection
		TCommDbConnPref prefs;
		prefs.SetIapId(iIap);
		prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);

		//Start Connection
		iOpeningConnection = ETrue;
		iTransactionObserver.OpeningConnectionL();
		iConnOpener->OpenConnection(prefs);
		return;
		}

	//Set properties for the HTTP session
	RStringPool strP = iHttpSession.StringPool();
	RHTTPConnectionInfo connInfo = iHttpSession.ConnectionInfo();
	connInfo.SetPropertyL ( strP.StringF(HTTP::EHttpSocketServ, RHTTPSession::GetTable() ), THTTPHdrVal (iSockServ.Handle()) );
	TInt connPtr = REINTERPRET_CAST(TInt, &iConnection);
	connInfo.SetPropertyL ( strP.StringF(HTTP::EHttpSocketConnection, RHTTPSession::GetTable() ), THTTPHdrVal (connPtr) );
	
	#endif // __WINS__
	DoPostL();
	}

// ----------------------------------------------------
// CTaskManagerEngine::ConnectionCreated()
// Called when opening a GPRS connection has finished.
// ----------------------------------------------------
//	
void CTaskManagerEngine::ConnectionCreated(const TInt& aError)
	{
	iOpeningConnection = EFalse;
	if (aError != KErrNone)
		{
		TRAPD(error, iTransactionObserver.FailedL(aError))
		PanicIfError(error);
		}
	else
		{
		TRAPD(error,
			//Set properties for the HTTP session
			RStringPool strP = iHttpSession.StringPool();
			RHTTPConnectionInfo connInfo = iHttpSession.ConnectionInfo();
			connInfo.SetPropertyL ( strP.StringF(HTTP::EHttpSocketServ, RHTTPSession::GetTable() ), THTTPHdrVal (iSockServ.Handle()) );
			TInt connPtr = REINTERPRET_CAST(TInt, &iConnection);
			connInfo.SetPropertyL ( strP.StringF(HTTP::EHttpSocketConnection, RHTTPSession::GetTable() ), THTTPHdrVal (connPtr) );
			DoPostL();
			)
		PanicIfError(error);
		}
	}
		
// End of file

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -