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

📄 addressbookengine.cpp

📁 symbian v9.1 下的地址本程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
 	}

// ----------------------------------------------------------------------------
// CAddressBookEngine::SendSearchL(const TDesC& aSearchPattern)
// function is called after an user has selected Search from the GUI 
// and typed a request. Function creates a search pattern from the descriptor
// and send it to the WSP. The message from server is received through 
// HandleMessageL() or an error is received through HandleErrorL().
// ----------------------------------------------------------------------------
//
void CAddressBookEngine::SendSearchL(const TDesC& aSearchPattern)
	{	
	_LIT( KSendSearchL, "CAddressBookEngine::SendSearchL()");
	LOG(KSendSearchL);
	iErrorState = ENoError;
	if (!iConnection)
		{
		_LIT( KConnNotReady, "Connection not ready.");
		LOG(KConnNotReady);
		iObserver.ErrorL(EConnectionNotReady);
		return;
		}
	
	//Get the xml query to be sent to service
	HBufC8* stSearch= GenerateQueryRequestL(aSearchPattern);
	CleanupStack::PushL(stSearch);

	//Send the query to service
	iConnection->SendL(*stSearch);
	
	CleanupStack::PopAndDestroy(stSearch);
	}

// ----------------------------------------------------------------------------
// CAddressBookEngine::StartElementL(const TDesC8& aNsUri,
//									const TDesC8& aLocalName,
//									const TDesC8& aQName,
//									const RAttributeArray& aAttrs)
// It is called from CAddressBookEngine::HandleMessageL (ParseL(aMessage))
// every time when XML parser finds a new element tag.
// 									
// Example server response:
// <ab:QueryResponse xmlns:ab="urn:nokia:test:addrbook:2004-09">
//    <ab:Status code="ab:OK"/>
// <ab:Data>
//	<ab:Card>
//		<ab:N>
//			<ab:FAMILY>Smith</ab:FAMILY>
//			<ab:GIVEN>John</ab:GIVEN>
//		</ab:N>
//		<ab:ADR>
//			<ab:EXTADR>Room 123</ab:EXTADDR>
//			<ab:STREET>1st ST 123</ab:STREET>
//			<ab:PCODE>7698532</ab:PCODE>
//		</ab:ADR>
//		<ab:TEL>1234</ab:TEL>
//		<ab:TEL type="CELL">2122345</ab:TEL>
//	</ab:Card>
// </ab:Data>
// </ab:QueryResponse>
//
// In this case we are interested in the <Status> and <Card> elements.
// When Status is found, we compare if attribute 'Code' is OK or not.
// SenXmlUtils::LocalName() only extracts out a possible XML prefix
// (i.e. 'ab:OK').
// When local name is 'Card', we have encountered a contact-object.
// Then we make a new CAddressBookContact and delegate the parsing to it.
// ----------------------------------------------------------------------------
//
void CAddressBookEngine::StartElementL(	const TDesC8& aNsUri,
										const TDesC8& aLocalName,
										const TDesC8& aQName,
										const RAttributeArray& aAttrs)
	{
	_LIT( KStartElementL, "CAddressBookEngine::StartElementL() ***");
	_LIT( KNamespaceURI, "- namespace URI  (%S)");
	_LIT( KLocalname, "- localname      (%S)");
	_LIT( KQualName, "- qualified name (%S)");
	
	LOG( KStartElementL);
	LOG_FORMAT((KNamespaceURI, aNsUri));
	LOG_FORMAT((KLocalname, aLocalName));
	LOG_FORMAT((KQualName, aQName));

	if (aLocalName == KStatus)
		{
		
		if (SenXmlUtils::LocalName(SenXmlUtils::AttrValue(aAttrs, KCode)) != KOk)
			{
			iErrorState = ENestedError;
			delete iFaultDelegate;
			iFaultDelegate = NULL;
			iFaultDelegate = CSenDomFragment::NewL(aNsUri,
							aLocalName, aQName, aAttrs);
			DelegateParsingL(*iFaultDelegate);
			return;
			}
		}
	else if (aLocalName == KCard)
		{
		iErrorState = ENoError;

		CAddressBookContact* delegate = CAddressBookContact::NewL();
		CleanupStack::PushL(delegate);
		iContacts.Append(delegate);
		DelegateParsingL(*delegate);
		CleanupStack::Pop(delegate);
		return;
		}
	}

// ----------------------------------------------------------------------------
// CAddressBookEngine::EndElementL(const TDesC8& aNsUri,
//								  const TDesC8& aLocalName,
//								  const TDesC8& aQName)
// It is called from CAddressBookEngine::HandleMessageL (ParseL(aMessage))
// every time when XML parser finds a closing element tag.
//								  
// We are only interested about the </Status> element.
// When the </Status> is found, we check for errors found in response,
// and if so, notice the observer
// ----------------------------------------------------------------------------
//
void CAddressBookEngine::EndElementL(	const TDesC8& aNsUri,
										const TDesC8& aLocalName,
										const TDesC8& aQName)
	{
	_LIT( KEndElementL, "CAddressBookEngine::EndElementL()");
	_LIT( KNamespaceURI, "- namespace URI  (%S)");
	_LIT( KLocalname, "- localname      (%S)");
	_LIT( KQualName, "- qualified name (%S)");	

	LOG( KEndElementL);
	LOG_FORMAT((KNamespaceURI, aNsUri));
	LOG_FORMAT((KLocalname, aLocalName));
	LOG_FORMAT((KQualName, aQName));

	if(aLocalName == KStatus)
		{
		if (iErrorState == ENestedError)
			{
			iErrorState = ENoError;
			CSenElement* statusElement = NULL;
			if (iFaultDelegate)
				{
					statusElement =
						iFaultDelegate->AsElement().Element(KStatus);
				}

			if(statusElement)
				{
				const TDesC8* errorCode = statusElement->AttrValue(KCode);
				iContacts.ResetAndDestroy();
				if (errorCode)
					{
					if (SenXmlUtils::LocalName(*errorCode) == KTooManyMatches)
						{
						iObserver.ErrorL(ETooManyMatches);
						}
					else
						{
						iObserver.ErrorL(ESearchFailed); // Default error
						}
					}
				}
			else
				{
				// could not find status element:
				iObserver.ErrorL(ESearchFailed); // Default error
				}
			}
		else if (iErrorState == ESoapError)
			{
			iErrorState = ENoError;
			delete iFaultDelegate;
			iFaultDelegate = NULL;
			iContacts.ResetAndDestroy();
			iObserver.ErrorL(ESearchFailed); // Default error
			}
		}
	else
		{
		CSenBaseFragment::EndElementL(aNsUri, aLocalName, aQName);
		}
	}

// ----------------------------------------------------------------------------
// CAddressBookEngine::RegisterIdentityProviderL()
// registers the IdentityProvider and associates the FN service to it. 
// Method creates the CSenServiceManager instance and uses it to register
// Identity Provider ALSO as Authentication Service (AS) Description.
// The actual Forum Nokia's Addressbook service is then associated to this
// newly registered IdentityProvider.
// ----------------------------------------------------------------------------
//
TInt CAddressBookEngine::RegisterIdentityProviderL()
	{
	_LIT( KRegIdProvL, "CAddressBookEngine::RegisterIdentityProviderL()");
	LOG( KRegIdProvL);
	TBuf<64> buf; // for logging

	TInt retVal(KErrNone);

	if (!iManager)
		{
		_LIT( KInsNewServMng, "Instantiating new service manager");
		LOG( KInsNewServMng);
		iManager = CSenServiceManager::NewL();
		}

	CSenIdentityProvider* idp = CSenIdentityProvider::NewLC(
		KASEndPoint, KASContract);

	// Set the Provider ID
	idp->SetProviderID(KASProviderID);

	// Associate Forum Nokia's Addressbook service ID (contract)
	// to this Identity Provider
	idp->SetServiceID(KPpContract);

	// Set Liberty ID-WSF framework ID
	idp->SetFrameworkIdL(KDefaultIdWsfFrameworkID);

	// ------------------------------------------------------------------------
	// The following username/password properties will be used for
	// authentication. Please note, that use advisory authentication
	// id "IMEI" would result device ID to be directly fetched from phone.
	// ------------------------------------------------------------------------
	//
	idp->SetUserInfoL(KTestAuthzID, KNullDesC8, KTestPassword);

	_LIT( KRegIdProv, "Registering identityprovider. User info provided.");
	LOG( KRegIdProv);
	retVal = iManager->RegisterIdentityProviderL(*idp);
	if(retVal != KErrNone)
		{
		// --------------------------------------------------------------------
		// An error occurred. However, the rest of registration calls will
		// be attempted in order to provide any possible data that WSF needs.
		// --------------------------------------------------------------------
		buf.AppendNum(retVal);
		_LIT( KErrRegIdProv, "Error registering Identity Provider: %S");
		LOG_FORMAT((KErrRegIdProv, buf));
		buf.Zero();
		}
	_LIT( KRegAutServDesc, "Registering autentication service description.");
	LOG( KRegAutServDesc);
	retVal = iManager->RegisterServiceDescriptionL(*idp);
	if(retVal != KErrNone)
		{
		// --------------------------------------------------------------------
		// An error occurred. However, the rest of registration calls will
		// be attempted in order to provide any possible data that WSF needs.
		// --------------------------------------------------------------------
		buf.AppendNum(retVal);
		_LIT( KErrRegServDesc, "Error registering Service Description: %S");
		LOG_FORMAT((KErrRegServDesc, buf));
		buf.Zero();
		}

// Note, that following API call could be used to associate more services
// to some Identity Provider, which ProviderID is known.
//
//	LIT( KAssocAddressbookServ, "Associating Addressbook service (WSP) to known Identity Provider.");
//	LOG( KAssocAddressbookServ);
//	retVal = iManager->AssociateServiceL(KPpContract, KASProviderID);
//
//	if(retVal != KErrNone) 
//		{
//		// --------------------------------------------------------------------
//		// An error occurred. However, the rest of registration calls will
//		// be attempted in order to provide any possible data that WSF needs.
//		// --------------------------------------------------------------------
//		buf.AppendNum(retVal);
//		_LIT( KErrAssFNServ, "Error associating the FN service to IDP %S");
//		LOG_FORMAT( KErrAssFNServ, buf);
//		buf.Zero();
//		}

	CleanupStack::PopAndDestroy(idp); 
	return retVal;
	}

// ----------------------------------------------------------------------------
// CAddressBookEngine::ConnectL()
// Makes a connection to an ID-WSF service.
// When ConnectL() is called the first time, authentication and
// discovery services are connected and the user is therefore
// authenticated and the service is connected.
// When ConnectL() is called again and the credentials received from
// authentication and discovery services are valid, there are no need to
// connect to them again. This means that SetStatus() will be called
// almost immediately with value CONNECTED, but in device GPRS or data
// connection is not opened yet (but instead it is opened when search
// occurs).
// ----------------------------------------------------------------------------
//
void CAddressBookEngine::ConnectL()
	{
	_LIT (KConnectL, "CAddressBookEngine::ConnectL()");
	LOG( KConnectL);
	RegisterIdentityProviderL();

	CSenXmlServiceDescription* pattern = CSenXmlServiceDescription::NewLC();
	pattern->SetContractL(KPpContract());
	delete iConnection;
	iConnection = NULL;
	iConnection = CSenServiceConnection::NewL(*this, *pattern);
	CleanupStack::PopAndDestroy(pattern); 
	}
	
// ---------------------------------------------------------------------------- 
// CAddressBookEngine::Connected()
// sets a state of connection as KSenConnectionStatusReady
// ----------------------------------------------------------------------------
//
TBool CAddressBookEngine::Connected()
	{
	return (iConnectionState == KSenConnectionStatusReady);
	}

// ---------------------------------------------------------------------------- 
// CAddressBookEngine::GetContact(TInt aPosition, CAddressBookContact*& aContact)
// This function need to get a current contact (CAddressBookView::CurrentContact())
// or to open an item (CAddressBookView::OpenItemL())
// ----------------------------------------------------------------------------
// 
TInt CAddressBookEngine::GetContact(TInt aPosition, CAddressBookContact*& aContact)
	{
	_LIT( KGetContact, "CAddressBookEngine::GetContact()");
	LOG( KGetContact);
	TInt retVal(KErrNotFound);
	if (aPosition < iContacts.Count() && aPosition >= 0)
		{
		aContact = iContacts[aPosition];
		retVal = KErrNone;
		}
	return retVal;
	}
	
// ---------------------------------------------------------------------------- 
// CAddressBookEngine::Cancel()
// Close WebServices-connection if user canceled
// ----------------------------------------------------------------------------
//
void CAddressBookEngine::Cancel()
	{
	_LIT( KCancel, "CAddressBookEngine::Cancel()");
	LOG( KCancel);
	delete iConnection;
	iConnectionState = 0;	 // not connected
	iConnection = NULL;
	}

// End of File 

⌨️ 快捷键说明

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