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

📄 taskmanagerengine.cpp

📁 symbian s60 end to end socket程序源码 基于第二版何第三版的sdk
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
* ============================================================================
*  Name     : CTaskManagerEngine from TaskManagerEngine.cpp
*  Part of  : TaskManager
*  Created  : 08/31/2005 by Forum Nokia
*  Version  : 1.0
*  Copyright: Nokia Corporation
* ============================================================================
*/

// INCLUDE FILES
#include "TimeOutTimer.h"
#include "Response.h"
#include "Request.h"
#include "TaskManagerEngine.h"
#include "TaskManagerEngineReader.h"
#include "TaskManager.pan"

#include <msvids.h>				// KMsvGlobalInBoxIndexEntryId
#include <txtrich.h>			// CRichText
#include <mtclreg.h>			// CClientMtmRegistry
#include <mtclbase.h>			// CBaseMtm
#include <commdb.h>				// CCommsDatabase
#include <commdbconnpref.h>		// TCommDbConnPref
#include <es_enum.h>			// TConnectionInfoV2Buf
#include <in_sock.h>			// TInetAddr
#include <eikenv.h>
#include <s32mem.h>				// TDesBuf
#include <e32std.h>				// TLex
#include <securesocket.h>       // CSecureSocket
#include <SocketTaskManager.rsg>

// CONSTANTS

_LIT( KSSL3, "SSL3.0" );

_LIT( KGPRSStartError, "Error starting GPRS service" );
_LIT( KLookupError, "Error looking up server IP");
_LIT( KConnectionError, "Error in forming the connection" );
_LIT( KSSLConnError, "Error forming SSL link between client and server" );
_LIT( KWriteError, "Error writing data to server" );
_LIT( KErroneousPackage, "Package received was erroneous" );

_LIT(KUserNotSet, "Set username and/or password");
_LIT(KIapNotSet, "IAP not selected");
_LIT(KServerNotSet, "Server name not set");
_LIT(KSmsUpdateMessage, "TaskManagerUpdate");

const TInt CTaskManagerEngine::KTimeOut = 30000000; // 30 seconds time-out

// ================= MEMBER FUNCTIONS =======================

// constructor
CTaskManagerEngine::CTaskManagerEngine(MTransactionObserver& aObserver)
	: CActive( EPriorityStandard ), iState( ENotConnected ),
	iOperation( TRequest::EFetchTasks ), iMarkId( 0 ),
	iTransactionObserver( aObserver )
	
	{
	}
	
// destructor
CTaskManagerEngine::~CTaskManagerEngine()
	{
	Cancel(); // Closes the socket and resolver if engine is active

    if( iReader )
        {
        iReader->Cancel();
        }
	delete iReader;
	
	delete iTimer;
	delete iSecureSocket;

	iResolver.Close();	
	iConnection.Close();
	iSocketServer.Close();
	
	delete iMsvEntry;
	delete iMsvSession;
	iIAPs.Close();
	}

// ----------------------------------------------------
// CTaskManagerEngine::NewL()
// Two-phased constructor.
// ----------------------------------------------------
//	
CTaskManagerEngine* CTaskManagerEngine::NewL(MTransactionObserver& aObserver)
	{
	CTaskManagerEngine* self = new (ELeave) CTaskManagerEngine(aObserver);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(self);
	return self;
	}

// ----------------------------------------------------
// CTaskManagerEngine::ConstructL()
// Symbian OS default constructor can leave.
// ----------------------------------------------------
//	
void CTaskManagerEngine::ConstructL()
	{
	User::LeaveIfError(iSocketServer.Connect());
	User::LeaveIfError(iConnection.Open(iSocketServer));
	
	// instantiate a timer for connection, sockethread etc. timeouts 
	iTimer = CTimeOutTimer::NewL( EPriorityHigh, *this );
	
    iReader = CTaskManagerEngineReader::NewL( *this );
    
    CActiveScheduler::Add( this );
	
	iMsvSession = CMsvSession::OpenAsyncL(*this);
	
	LoadIapsL();
	
	// no IAPs defined in the device.
	if (iIAPs.Count() == 0)
		{
		CEikonEnv::Static()->LeaveWithInfoMsg(R_TASKMANAGER_NO_IAPS_DEFINED);
		}
	
	}

// ----------------------------------------------------
// CTaskManagerEngine::SetConnectionSettingsL()
// Sets all connections settings. 
// ----------------------------------------------------
//		
void CTaskManagerEngine::SetConnectionSettingsL(const TDesC& aServerName, 
												const TInt& aPort,
												const TDesC& aUsername, 
												const TDesC& aPassword)
	{
	iUsername.Copy(aUsername);
	iPassword.Copy(aPassword);
	iServer.Copy(aServerName);
	iPort = aPort;
	}

// ----------------------------------------------------
// CTaskManagerEngine::SetIap()
// Sets the IAP to be used in the connections.
// ----------------------------------------------------
//	
void CTaskManagerEngine::SetIap(const TUint32& aId)
	{
	iIap = aId;
	}
	
// ----------------------------------------------------
// CTaskManagerEngine::IapSet()
// Returns ETrue if the IAP is set, EFalse if not.
// ----------------------------------------------------
//	
TBool CTaskManagerEngine::IapSet() const
	{
	if (iIap == 0)
		{
		return EFalse;
		}
	else
		{
		return ETrue;
		}
	}

// ----------------------------------------------------
// CTaskManagerEngine::CheckAndReportErrorsL()
// Checks that all necessary connections settings are 
// set and reports from missing data.
// ----------------------------------------------------
//	
TBool CTaskManagerEngine::CheckAndReportErrorsL()
	{
	if (!IapSet())
		{
		iTransactionObserver.ErrorL(KIapNotSet);
		return ETrue;
		}
		
	if (iServer.Length() == 0)
		{
		iTransactionObserver.ErrorL(KServerNotSet);
		return ETrue;
		}

	return EFalse;
	}

// ----------------------------------------------------
// CTaskManagerEngine::FetchTasksL()
// Starts fetching tasks from the server.
// Called from UI.
// ----------------------------------------------------
//		
void CTaskManagerEngine::FetchTasksL()
	{
	iOperation = TRequest::EFetchTasks;
	
    iTransactionObserver.ConnectingToServerL( ETrue );
	
	iRunning = ETrue;
	GPRSConnectL();
	}
	
// ----------------------------------------------------
// CTaskManagerEngine::MarkTaskDoneL()
// Informs the server that a task has been completed.
// Server updates task info in the database.
// ----------------------------------------------------
//	
void CTaskManagerEngine::MarkTaskDoneL(const TInt& aTaskId)
	{
	iOperation = TRequest::ETaskDone;
	iMarkId.Num( aTaskId );
	
	iRunning = ETrue;
	GPRSConnectL();
	}

// ----------------------------------------------------
// CTaskManagerEngine::CancelTransaction()
// Can be used for cancelling a transaction at 
// any time.
// ----------------------------------------------------
//		
void CTaskManagerEngine::CancelTransaction()
	{
	Cancel();
	
	iRunning = EFalse;
	
	TRAPD( error, iTransactionObserver.CancelledL() );
	if( error != KErrNone )
	    {
	    Panic( error );
	    }
	
	}

// ----------------------------------------------------
// CTaskManagerEngine::CheckRefreshL()
// An SMS update message may have been received while 
// a transaction was running. If so, tasks will 
// be fetched.
// ----------------------------------------------------
//
void CTaskManagerEngine::CheckRefreshL()
	{
	if (iRunning)
	    {
	    return;
	    }

	if (iDoRefresh)
		{
		iDoRefresh = EFalse;
		FetchTasksL();
		}
	}

// ----------------------------------------------------
// CTaskManagerEngine::SetAutomaticUpdateL()
// Defines whether tasks are downloaded automatically 
// when an update SMS message arrives or not.
// ----------------------------------------------------
//
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 '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 a transaction is running or program is in the background (paused) 
					// don't fetch tasks yet.
					if (iRunning || iAutomaticUpdate)
						{
						iDoRefresh = ETrue;
						}
					// 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|ECommDbBearerLAN,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() 

⌨️ 快捷键说明

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