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

📄 helloview.cpp

📁 symbian电话簿引擎的经典例子
💻 CPP
字号:
/*
* ============================================================================
*  Name     : CHelloView from CAknView
*  Part of  : Hello
*  Copyright (c) 2003 Nokia. All rights reserved.
* ============================================================================
*/

// INCLUDE FILES
#include <aknviewappui.h>
#include <avkon.hrh>

#include <LDN02.rsg>
#include <eikaufty.h>
#include <eikmenup.h>
#include "common.h"
#include "LDN02.hrh"
#include "HelloView.h"
#include "BookEngine.h"
#include "HelloContainer.h" 
#include "LDN02AppUi.h"
#include "WorldView.h"
#include <LDN02.mbg> //from mbm
#include <akncontext.h>
#include <akntitle.h>
#include <aknnotewrappers.h>

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

// C++ default constructor can NOT contain any code, that
// might leave.
//
CHelloView* CHelloView::NewL()
{
    CHelloView* self = NewLC();
    CleanupStack::Pop(self);
    return self;
}

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

CHelloView::CHelloView()
    {
	m_pBookEngine = NULL;

    }

// EPOC default constructor can leave.
void CHelloView::ConstructL()
    {
   	BaseConstructL(R_HELLO_VIEW);
    }

// Destructor
CHelloView::~CHelloView()
    {
		
    }

// ---------------------------------------------------------
// TUid CHelloView::Id()
// Returns Id of the view.
// ---------------------------------------------------------
TUid CHelloView::Id() const
    {
    return KViewId2;
    }
//----------------------------------------------------------
//   M class application
//----------------------------------------------------------

void CHelloView::SendData(TDesC& aSurName,TDesC& aName,TDesC& aNum)
{
	m_SurName.Copy(aSurName);
	m_Name.Copy(aName);
	m_Number.Copy(aNum);
}
// ---------------------------------------------------------
// CHelloView::HandleCommandL(TInt aCommand)
// Handles commands
// ---------------------------------------------------------
void CHelloView::HandleCommandL(TInt aCommand)
    {   
		switch(aCommand)
		{
 		case ELDN02HELLOCmdNew: //新建
 			{
				TBuf8<1> buf;
				buf.Copy(_L("1"));
				AppUi()->ActivateLocalViewL(KViewId3,KViewId2,buf);
 				break;
 			}
		case ELDN02HELLOCmdFind: //查看
 			{
				if (iContainer->GetSelectedIndexL() >= 0) //当记录为空时加判断,不然崩溃   ??为什么单独提取成一个函数时不行
				{
					TViewPara info;
					info.mFlag = 2;//1表示新建 2表示查看 3表示修改
					info.m_SurName.Copy(m_pBookInfoArray[iContainer->GetSelectedIndexL()]->GetFirstName()->Des());
					info.m_Name.Copy(m_pBookInfoArray[iContainer->GetSelectedIndexL()]->GetLastName()->Des());
					info.m_Number.Copy(m_pBookInfoArray[iContainer->GetSelectedIndexL()]->GetTelephone()->Des());
					TPckgBuf< TViewPara > package(info); //定义包类型
					TBuf8<100> bufTemp;
					bufTemp.Copy( package );
					AppUi()->ActivateLocalViewL(KViewId3,KViewId2,bufTemp);
				}
			}
 				break;
		case ELDN02HELLOCmdModify: //修改
 			{
			if (iContainer->GetSelectedIndexL() >= 0) //当记录为空时加判断,不然崩溃
				{
					TViewPara info;
					info.mFlag = 3;//1表示新建 2表示查看 3表示修改
					info.m_SurName.Copy(m_pBookInfoArray[iContainer->GetSelectedIndexL()]->GetFirstName()->Des());
					info.m_Name.Copy(m_pBookInfoArray[iContainer->GetSelectedIndexL()]->GetLastName()->Des());
					info.m_Number.Copy(m_pBookInfoArray[iContainer->GetSelectedIndexL()]->GetTelephone()->Des());
					TPckgBuf< TViewPara > package(info); //定义包类型
					TBuf8<100> bufTemp;
					bufTemp.Copy( package );
					AppUi()->ActivateLocalViewL(KViewId3,KViewId2,bufTemp);
				}
 			}
			break;
		case ELDN02HELLOCmdDel:
 			{
			TInt nLstIndex = iContainer->GetSelectedIndexL();
			if (nLstIndex >= 0)
			{
				DeleteContact();
			}
 				//AppUi()->ActivateLocalViewL(KViewId2);
 				break;
 			}

		default:
			{
				AppUi()->HandleCommandL(aCommand);
			}
			break;

		}
	}
// ---------------------------------------------------------
// CHelloView::HandleClientRectChange()
// Handles client rect change.
// ---------------------------------------------------------
void CHelloView::HandleClientRectChange()
    {
    if (iContainer)
        {
        iContainer->SetRect(ClientRect());
        }
    }

// ---------------------------------------------------------
// CHelloView::DoActivateL(...)
// Creates the Container class object.
// ---------------------------------------------------------
void CHelloView::DoActivateL(const TVwsViewId& /*aPrevViewId*/,
                                    TUid aCustomMessageId,
                                    const TDesC8& /*aCustomMessage*/)
    {

	iUid = aCustomMessageId;
	TBuf<KMaxPath> pathMbm;
#ifdef __WINS__
	pathMbm.Copy(_L("z:\\system\\apps\\LDN02\\LDN02.mbm"));
#else
	CLDN02AppUi* pApp = (CLDN02AppUi*)CEikonEnv::Static()->AppUi();
	pApp->GetAppPath(pathMbm);
	pathMbm.Append(_L("LDN02.mbm"));
#endif
	
	TUid contextPaneUid;
	contextPaneUid.iUid = EEikStatusPaneUidContext;	
	CEikStatusPane* statusPane = StatusPane();
	
	//set icon
	CAknContextPane* contextPane = (CAknContextPane*) statusPane->ControlL(TUid::Uid(EEikStatusPaneUidContext));
	CFbsBitmap* bitmap = iEikonEnv->CreateBitmapL(pathMbm, EMbmLdn02Ldn02icon1);
	CFbsBitmap* bitmapMask = iEikonEnv->CreateBitmapL(pathMbm, EMbmLdn02Ldn02icon1mask);
	
	contextPane->SetPicture(bitmap, bitmapMask);


		TBuf<32> sTmpTitle;
	CEikonEnv::Static()->ReadResource(sTmpTitle, R_QTN_LD_HELLO_TITLE);

	CAknTitlePane* titlePane = (CAknTitlePane*) statusPane->ControlL(TUid::Uid(EEikStatusPaneUidTitle));
	titlePane->SetTextL(sTmpTitle);

	iContainer = new (ELeave) CHelloContainer;
    iContainer->SetMopParent(this);
	
	//define a rectangle, the size and point is the same of clientrect;
	TRect rc;
	rc.SetRect(ClientRect().iTl, TSize(176, 144));
	
	// initialize container instance by the second phase construction
    iContainer->ConstructL(rc);
	
	m_pBookEngine = CBookEngine::NewL();
	//更新动态数组中的名片
	GetContactFromPB();
	iContainer->SetItemToListBox(m_pBookInfoArray);
	
	if (iUid.iUid == 2 || iUid.iUid == 3 )//注意不是添加功能时要传别的UID  2表示新建 3表示修改
	{
		CreateNewContact(m_SurName,m_Name,m_Number);//调用创建函数
	}
	//Adds a control to the control stack
	AppUi()->AddToStackL(*this, iContainer);
    }

// ---------------------------------------------------------
// CHelloView::DoDeactivate()
// Deletes the Container class object.
// ---------------------------------------------------------
void CHelloView::DoDeactivate()
    {
    if (iContainer)
    {
		AppUi()->RemoveFromStack(iContainer);
		MEM_FREE(iContainer);
    }
	
	MEM_FREE(m_pBookEngine);
	//动态数组重置并销毁
	m_pBookInfoArray.ResetAndDestroy();
}

//更新动态数组中的名片
void CHelloView::GetContactFromPB()
{
	m_pBookInfoArray.ResetAndDestroy();	//删除后重置
	m_pBookEngine->UpdatePhoneBook(0,m_pBookInfoArray);

}

//创建新名片
void CHelloView::CreateNewContact(TDesC& aSurName,TDesC& aName,TDesC& aNum)
//void CHelloView::CreateNewContact()
{
//	TBuf<16> aSurName;
//	TBuf<16> aName;
//	TBuf<16> aNum;
	
	// 	iContainer->GetFirstName(firstname);
	// 	iContainer->GetLastName(lastname);
	// 	iContainer->GetPhoneNumber(phonenumber);
//	aSurName.Copy(_L("Good"));
//	aName.Copy(_L("Mao"));
//	aNum.Copy(_L("13812341234"));
	
	//若firstname、lastname、phonenumber同时都为空,则不添加新名片.
	if ((aSurName.Length()==0) && (aName.Length()==0) && (aNum.Length())==0)
	{
		//AppUi()->ActivateLocalViewL(TUid::Uid(8));
		return;
	}
	
	//若firstname、lastname、phonenumber不同时为空,则添加新名片.

	//CBookEngine* pEngine;
	//pEngine = CBookEngine::NewL();	//名片夹引擎
	RPointerArray<CBookInfo> szBookInfoArray;	//动态数组
	
	//创建一个名片对象,即一个条目/项.
	CBookInfo* pBookInfo = CBookInfo::NewL();
	//赋值
	pBookInfo->SetFirstName(aSurName);
	pBookInfo->SetLastName(aName);
	pBookInfo->SetTelephone(aNum);
	//此处应做处理,当功能为修改时保证其ID不为-1
	if (iUid.iUid == 2)//为2时表示添加
	{
		pBookInfo->SetItemID(-1);
	}
	if (iUid.iUid ==3)//为3时表示修改  
	{
		pBookInfo->SetItemID(m_pBookInfoArray[iContainer->GetSelectedIndexL()]->GetItemID());
	}
	

	//向动态数组添加新的名片
	szBookInfoArray.Append(pBookInfo);
	
	//更新名片夹
	m_pBookEngine->UpdatePhoneBook(1, szBookInfoArray);
	
	//MEM_FREE(pEngine);
	for (TInt i=0; i<szBookInfoArray.Count(); i++)
	{
		delete szBookInfoArray[i];
	}
	szBookInfoArray.Close();

 	//更新动态数组中的名片
 	GetContactFromPB();
 	//刷新显示内容
 	RefreshListBox(0);
}


//删除名片
void CHelloView::DeleteContact()
{
	if (m_pBookInfoArray.Count() <= 0) 
	{
		return;
	}

	TInt nResult;
	iContainer->GetDeleteResult(nResult);

	if (nResult) 
	{
		TInt nIndex = iContainer->m_pListBox->CurrentItemIndex();
		
		if (nIndex >= 0)
		{
			//从数据库中删除一个名片
			m_pBookEngine->DeleteUserFromBook(m_pBookInfoArray[nIndex]->GetItemID());
			//更新动态数组中的名片
			GetContactFromPB();
			//刷新显示内容
			RefreshListBox(nIndex);
		}
	}
}

//刷新显示内容
void CHelloView::RefreshListBox(TInt aIndex)
{
	if (m_pBookInfoArray.Count() != 0)
	{		
		iContainer->SetItemToListBox(m_pBookInfoArray);
		
		if (aIndex != 0)
		{
			iContainer->m_pListBox->SetCurrentItemIndexAndDraw(aIndex-1);
		}
		else
		{
			iContainer->m_pListBox->SetCurrentItemIndexAndDraw(0);
			iContainer->ShowNoCard();
		}
	}
	else
	{
		iContainer->SetItemToListBox(m_pBookInfoArray);
	}
	iContainer->m_pListBox->DrawNow();
}

// End of File

⌨️ 快捷键说明

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