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

📄 pbengine.cpp

📁 个人手机助理套件:包含1、记事本(备忘录)、名片夹、名片夹的上传下载(异地备份)、短消息模块
💻 CPP
字号:
/*****************************************************************************
*  Name     : PBEngine.cpp
*  Part of  : 
*  Created  : 
*  Implementation notes:
*     Initial content was generated by Series 60 Application Wizard.
*  Version  : 1.0
*****************************************************************************/
#include <eikenv.h>
#include <e32def.h>
#include <charconv.h>
#include <utf.h>

#include "PBEngine.h"
#include "PBInfo.h"
#include "MEM_FREE.H"

CPBEngine* CPBEngine::NewL()
{
	CPBEngine* self = CPBEngine::NewLC();
	CleanupStack::Pop();
	return self;
}

CPBEngine* CPBEngine::NewLC()
{
	CPBEngine* self = new(ELeave) CPBEngine;
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

void CPBEngine::ConstructL()
{
	i_pPbkContactEngine = CPbkContactEngine::NewL();
}

CPBEngine::~CPBEngine()
{
	MEM_FREE(i_pDataBase)
	MEM_FREE(i_pPbkContactEngine)
}

//根据联系人名字,从名片夹数据库中获取对应电话号码
void CPBEngine::GetMobileNumberByName(const TDesC& aName, TDes& aMobile)
{
	TInt i;
	//打开名片夹数据库
	i_pDataBase = CContactDatabase::OpenL();
	const CContactIdArray* contactIDArray = i_pDataBase->SortedItemsL();
	TInt nCount (contactIDArray->Count());
	for(i=0; i<nCount; i++)
	{
		TBuf<64> sFullName;
		CContactTextField* textField = NULL;
		//通讯录条目类
		CContactItem* cntItem = i_pDataBase->ReadContactLC((*contactIDArray)[i]);
		//域类
		CContactItemFieldSet& cntFieldSet = cntItem->CardFields();
		TInt nTmpIndex = cntFieldSet.Find(KUidContactFieldFamilyName);
		if(nTmpIndex != -1)
		{
			textField = cntFieldSet[nTmpIndex].TextStorage();
			sFullName.Copy(textField->Text());
		}
		
		nTmpIndex = cntFieldSet.Find(KUidContactFieldGivenName);
		if(nTmpIndex != -1)
		{
			textField = cntFieldSet[nTmpIndex].TextStorage();
			sFullName.Append(textField->Text());
		}
		CleanupStack::PopAndDestroy(cntItem);
		
		if( sFullName.Compare(aName) == 0 )
		{
			CPbkContactItem* pbkItem = i_pPbkContactEngine->ReadContactLC((*contactIDArray)[i]) ;
			TPbkContactItemField* fld = pbkItem->FindField(EPbkFieldIdPhoneNumberMobile);
			if(fld)
			{
				aMobile.Copy(fld->Text());
			}
			CleanupStack::PopAndDestroy(pbkItem);
			break;
		}
	}
	MEM_FREE(i_pDataBase)
}

//更新名片夹
void CPBEngine::UpdatePhoneBook(TInt aFlag, RPointerArray<CPBInfo>*& aPBInfoArray)
{
	//打开名片夹数据库
	i_pDataBase = CContactDatabase::OpenL();
	
	//unicode--->utf8:
	CCnvCharacterSetConverter* converter = CCnvCharacterSetConverter::NewLC();
	converter->PrepareToConvertToOrFromL(KCharacterSetIdentifierUtf8, CEikonEnv::Static()->FsSession());
	
	if (aFlag == 0)		//从手机(数据库)读入
	{
		TBuf<256>   sTmpBuf;
		TInt		nTmpIndex	= 0;
		CPBInfo*	pPBInfo	= NULL;
		
		// 先读取手机的contacts 
		// 改为SortedItemsL,即使只写电话簿中的一项,也能读取出来
		const CContactIdArray* contactIDArray = i_pDataBase->SortedItemsL();
		for (TInt i=0; i< contactIDArray->Count(); i++)
		{
			sTmpBuf.Zero();
			nTmpIndex = 0 ;
			
			pPBInfo = CPBInfo::NewL();
			
			//通讯录条目类
			CContactItem*	 cntItem = i_pDataBase->ReadContactLC((*contactIDArray)[i]);
			CPbkContactItem* pbkItem = i_pPbkContactEngine->ReadContactLC((*contactIDArray)[i]);  //为什么要两个??
			
			//域类
			CContactItemFieldSet& cntFieldSet = cntItem->CardFields();
			TPbkContactItemField* fld = NULL;
			
			pPBInfo->SetItemID((*contactIDArray)[i]);
			//last name
			nTmpIndex = cntFieldSet.Find(KUidContactFieldFamilyName);
			if (nTmpIndex != -1)
			{
				CContactTextField* textField = cntFieldSet[nTmpIndex].TextStorage();
				pPBInfo->SetLastName(textField->Text());
			}
			//first name
			nTmpIndex = cntFieldSet.Find(KUidContactFieldGivenName);
			if (nTmpIndex != -1)
			{
				CContactTextField* textField = cntFieldSet[nTmpIndex].TextStorage();
				pPBInfo->SetFirstName(textField->Text());
			}
			//固定电话
			fld = pbkItem->FindField(EPbkFieldIdPhoneNumberGeneral);
			sTmpBuf.Zero();
			if (fld)
			{
				fld->GetTextL(sTmpBuf);
				pPBInfo->SetTelephone(sTmpBuf);
			}
			//家庭固定电话
// 			fld = pbkItem->FindField(EPbkFieldIdPhoneNumberHome);
// 			sTmpBuf.Zero();
// 			if (fld)
// 			{
// 				fld->GetTextL(sTmpBuf);
// 				pPBInfo->SetHomeTelephone(sTmpBuf);
// 			}
			//公司固定电话
// 			fld = pbkItem->FindField(EPbkFieldIdPhoneNumberWork);
// 			sTmpBuf.Zero();
// 			if (fld)
// 			{
// 				fld->GetTextL(sTmpBuf);
// 				pPBInfo->SetBusinessTelephone(sTmpBuf);
// 			}
			//手机号
			fld = pbkItem->FindField(EPbkFieldIdPhoneNumberMobile);
			sTmpBuf.Zero();
			if (fld)
			{
				fld->GetTextL(sTmpBuf);
				pPBInfo->SetHomeMobileNumber(sTmpBuf);
			}
            //Email
			fld = pbkItem->FindField(EPbkFieldIdEmailAddress);
			sTmpBuf.Zero();
			if (fld)
			{
				fld->GetTextL(sTmpBuf);
				pPBInfo->SetEmail(sTmpBuf);
			}
			aPBInfoArray->Append(pPBInfo);
			CleanupStack::PopAndDestroy(pbkItem);		//pbkItem
			CleanupStack::PopAndDestroy(cntItem);		//cntItem
		}
	}
	else if (aFlag == 1)//更新列表
	{
		//判断出哪些是新增加的,哪些是修改过的,分别处理:
		for (TInt i=0; i< aPBInfoArray->Count(); i++)
		{
			CPBInfo* pPBInfo = (*aPBInfoArray)[i];
			CPbkContactItem* contactItem;  
			if(pPBInfo->GetItemID() == -1)	//若itemid == -1, 则新增:分配空的条目
			{
				contactItem = i_pPbkContactEngine->CreateEmptyContactL(); 
			}
			else
			{
				contactItem = i_pPbkContactEngine->OpenContactL(pPBInfo->GetItemID()) ;
			}
			CleanupStack::PushL(contactItem);
			// find the phonenumber field(字段) of the contact, and add aCallInfo's telephone number
			TPbkContactItemField* fld = contactItem->FindField(EPbkFieldIdLastName);
			//向数据库写入LastName
			if(fld && (pPBInfo->GetLastName()))//如果该字段存在,则...
			{
				fld->TextStorage()->SetTextL(pPBInfo->GetLastName()->Des());//写入数据库
			}
			//向数据库写入FirstName
			fld = contactItem->FindField(EPbkFieldIdFirstName);
			if(fld && (pPBInfo->GetFirstName()))
			{
				fld->TextStorage()->SetTextL(pPBInfo->GetFirstName()->Des());
			}
			//向数据库写入电话
			fld = contactItem->FindField(EPbkFieldIdPhoneNumberGeneral);
			if(fld&&(pPBInfo->GetTelephone() ) )
			{
				fld->TextStorage()->SetTextL(pPBInfo->GetTelephone()->Des());
			}
			//向数据库写入家庭电话
// 			fld = contactItem->FindField(EPbkFieldIdPhoneNumberHome);
// 			if(fld && (pPBInfo->GetHomeTelephone()))
// 			{
// 				fld->TextStorage()->SetTextL(*(pPBInfo->GetHomeTelephone()));
// 			}
			// 			fld = contactItem->FindField(EPbkFieldIdPhoneNumberWork );
			// 			if(fld && (pPBInfo->GetBusinessTelephone()))
			// 			{
			// 				fld->TextStorage()->SetTextL(*(pPBInfo->GetBusinessTelephone()));
			// 			}
			//向数据库写入移动电话
			fld = contactItem->FindField(EPbkFieldIdPhoneNumberMobile);
			if(fld && (pPBInfo->GetHomeMobileNumber()))
			{
				fld->TextStorage()->SetTextL(*(pPBInfo->GetHomeMobileNumber()));
			}
			//向数据库写入Email
			fld = contactItem->FindField(EPbkFieldIdEmailAddress);
			if(fld && (pPBInfo->GetEmail()))
			{
				fld->TextStorage()->SetTextL(*(pPBInfo->GetEmail()));
			}
			
			if(pPBInfo->GetItemID()== -1)
			{   //如果是新增,则向引擎申请一个ID
				TContactItemId contactId = i_pPbkContactEngine->AddNewContactL(*contactItem);
				pPBInfo->SetItemID(contactId);
			} 
			else
			{
				TInt err=0;
				TRAP(err, i_pPbkContactEngine->CommitContactL(*contactItem, EFalse));
			}
			CleanupStack::PopAndDestroy(contactItem);	//contactItem
		}
		
	}	
	CleanupStack::PopAndDestroy(converter);
	MEM_FREE(i_pDataBase)
}

//从名片夹删除联系人
void CPBEngine::DeleteUserFromBook(TInt aItemID)
{
	//打开名片夹数据库
	i_pDataBase = CContactDatabase::OpenL();
	
	//删除指定ID的联系人
	i_pPbkContactEngine->DeleteContactL(aItemID);
	MEM_FREE(i_pDataBase)
}


void CPBEngine::HandleDatabaseEventL(TContactDbObserverEvent /*aEvent*/)
{
}


⌨️ 快捷键说明

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