📄 phonebookengine.cpp
字号:
/**
*
* @brief Definition of CPhoneBookEngine
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/
// INCLUDES
// Class include
#include "PhoneBookEngine.h"
// System includes
#include <CallSummary.rsg> // R_CALLVIEW_MENU_PANE
#include <CPbkContactEngine.h> // CPbkContactEngine
#include <CPbkContactItem.h> // CPbkContactItem
#include <CPbkdatasaveappui.h> // CPbkDataSaveAppUi
#include <CPbkFieldInfo.h> // CPbkFieldInfo
#include <CPbkFieldsInfo.h> // CPbkFieldsInfo
#include <eikmenup.h> // CEikMenuPane
#include <RPbkViewResourceFile.h> // RPbkViewResourceFile
#include <spdiacontrol.h> // CSpdiaControl
// User includes
#include "CallSummary.hrh" // TCallSummaryViewNumber
#include "CallInfo.h" // TCallInfo
// CONSTANTS
const TInt KPhoneMatchLengthFromRight = 10;
const TInt KDefaultGranularity = 6;
// ================= MEMBER FUNCTIONS =======================
/**
* C++ constructor
*/
CPhoneBookEngine::CPhoneBookEngine()
{
}
/**
* Destructor. Frees up memory for the class.
*/
CPhoneBookEngine::~CPhoneBookEngine()
{
if (iPbkResourceFile)
{
iPbkResourceFile->Close();
}
delete iPbkResourceFile;
delete iFieldInfoArray; // Doesn抰 own it's pointed-to objects, they are owned by the contact engine.
delete iPbkDataSaveAppUi;
delete iPbkContactEngine;
}
/**
* Symbian OS 2 phase constructor.
* Constructs the CPhoneBookEngine using the NewLC method, popping
* the constructed object from the CleanupStack before returning it.
*
* @return The newly constructed CPhoneBookEngine
*/
CPhoneBookEngine* CPhoneBookEngine::NewL()
{
CPhoneBookEngine* self = CPhoneBookEngine::NewLC();
CleanupStack::Pop(self);
return self;
}
/**
* Symbian OS 2 phase constructor.
* Constructs the CPhoneBookEngine using the constructor and ConstructL
* method, leaving the constructed object on the CleanupStack before returning it.
*
* @return The newly constructed CPhoneBookEngine
*/
CPhoneBookEngine* CPhoneBookEngine::NewLC()
{
CPhoneBookEngine* self = new (ELeave) CPhoneBookEngine();
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
/**
* Symbian OS 2nd phase constructor.
*/
void CPhoneBookEngine::ConstructL()
{
//Create a new Phonebook engine object and connects to the default contact database
iPbkContactEngine = CPbkContactEngine::NewL();
// Load the phonebook resource file
iPbkResourceFile = new (ELeave) RPbkViewResourceFile(*CCoeEnv::Static());
iPbkResourceFile->OpenL();
// Create a CPbkDataSaveAppUi using the opened contactengine object
iPbkDataSaveAppUi = CPbkDataSaveAppUi::NewL(*iPbkContactEngine);
// create an array of pointers to those CPbkFieldInfo objects, which are
// phone number fields. The CPbkFieldInfo objects are owned by iPbkContactEngine.
iFieldInfoArray = new (ELeave) CArrayPtrFlat<CPbkFieldInfo>(KDefaultGranularity);
const CPbkFieldsInfo& fieldsInfo = iPbkContactEngine->FieldsInfo();
TInt numberOfFields = fieldsInfo.Count();
for (TInt i = 0; i < numberOfFields; i++)
{
CPbkFieldInfo* fieldInfo = fieldsInfo[i];
if (fieldInfo->IsPhoneNumberField())
{
iFieldInfoArray->AppendL((fieldsInfo[i]));
}
}
}
/**
* Updates Phonebook data save menu items. Called from CCallView
* DynInitMenuPaneL.
*
* @param aResourceId resource id of the menu pane
* @param aMenuPane menu pane
*/
void CPhoneBookEngine::DynInitMenuPaneL(TInt aResourceId, CEikMenuPane* aMenuPane)
{
iPbkDataSaveAppUi->DynInitMenuPaneL(aResourceId, aMenuPane);
if (aResourceId == R_CALLVIEW_MENU_PANE)
{
// Adds Phonebook data save menu items to the menu pane
iPbkDataSaveAppUi->AddMenuItemsL(aMenuPane, ECallSummaryAddContactDB);
}
}
/**
* Handles a menu command inserted in the menu
* by CPbkDataSaveAppUi::AddMenuItemsL(), the command
* is either EPbkCmdDataSaveCreateNew or EPbkCmdDataSaveAddToExisting
*
* @param aCommandId command to execute.
* @param aNumber telphone number to add
* @return ETrue if the command was executed and the data added,
* EFalse if the operation was canceled in any stage.
*/
TBool CPhoneBookEngine::HandlePhoneBookCommandL(TInt aCommand, const TDesC& aNumber)
{
return iPbkDataSaveAppUi->HandleCommandL(aCommand, *iFieldInfoArray, aNumber);
}
/**
* Launchs the speed dial control dialog,
* @param aId the id of the contact to added to speeddial
* @param aPhoneNumber the phone number of the contact to added to speeddial
* @return Was the speed dial added successfully
*/
TBool CPhoneBookEngine::AssignSpeedDialDialogL(TContactItemId aId, const TDesC& aPhoneNumber)
{
// Create the speeddial control.
CSpdiaControl* speedDialControl = CSpdiaControl::NewL(*iPbkContactEngine);
// Get a copy of the contact details and find the required phone number.
CPbkContactItem* contact = iPbkContactEngine->ReadContactLC(aId);
// Search for contact using phone number.
TInt index = -1;
// ** Note: since the second paramater in the following call (number of digits) is 0,
// ** a full comparison is performed. This may not find a match if, for example,
// ** international dialing codes are used in the phone number field, but not in the
// ** log. In this case the speed dial will not be assigned! This is left as an exercise
// ** for the reader! ;)
if (!contact->FindNextFieldWithPhoneNumber(aPhoneNumber, 0, index))
{
// Contact not found.
CleanupStack::PopAndDestroy(contact);
return EFalse;
}
CleanupStack::PopAndDestroy(contact);
// Launch the speed dial with the selected contact and phone number field Id.
return (speedDialControl->ExecuteLD(aId, index) == KErrNone);
}
/**
*Finds the name asscociated with a contact
*@param aName filled with contacts name
*@param id of contact to find name of
*/
void CPhoneBookEngine::GetContactNameL(TDes& aName, TInt aContactId)
{
// open contact for reading
CPbkContactItem* contact = iPbkContactEngine->ReadContactLC(aContactId);
// get the contacts name
HBufC* name = contact->GetContactTitleL();
// copy HBufC to aName, making sure we don't excede the length of the descriptor.
if (name->Length() > aName.MaxLength())
{
aName.Copy(name->Ptr(), aName.MaxLength());
}
else
{
aName = *name;
}
delete name;
CleanupStack::PopAndDestroy(contact);
}
/**
* Adds a contact to the phonebook database, using the telephone number in the TCallInfo
* @param aCallInfo contains the number to be added to the phonebook
* @return the id of the newly created contact
*/
TContactItemId CPhoneBookEngine::AddContactEntryL(TCallInfo& aCallInfo)
{
// create a new contact
CPbkContactItem* contactItem = iPbkContactEngine->CreateEmptyContactL();
CleanupStack::PushL(contactItem);
// find the phonenumber field of the contact, and add aCallInfo's telephone number
TPbkContactItemField* phoneNumber = contactItem->FindField(EPbkFieldIdPhoneNumberGeneral);
phoneNumber->TextStorage()->SetTextL(aCallInfo.iTelephoneNumber);
// add the new contact to the phonebook.
TContactItemId contactId = iPbkContactEngine->AddNewContactL(*contactItem);
CleanupStack::PopAndDestroy(contactItem);
aCallInfo.iContactId = contactId;
return contactId;
}
/**
* Searchs the phonebook database for the contact containing aTelephone.
* @param aTelephoneNumber telephone number to be searched for
* @return the id of the contact found or -1 if not found
*/
TContactItemId CPhoneBookEngine::GetContactIdL(const TDesC& aTelephoneNumber) const
{
CContactIdArray* contactIdArray = iPbkContactEngine->MatchPhoneNumberL(aTelephoneNumber,KPhoneMatchLengthFromRight);
TContactItemId id;
if (contactIdArray->Count() > 0)
{
id = (*contactIdArray)[0];
}
else
{
id = -1;
}
delete contactIdArray;
return id; // return the first contact that matchs or -1
}
/**
* Retrieve the contact using the PbkContactEngine->ReadContactLC method
* @param aContactId contains the contact id of the contact item to retrieve
* @return a copy of the contact item. Ownership is passed out.
*/
CPbkContactItem* CPhoneBookEngine::ReadContactLC(TContactItemId aContactId)
{
return iPbkContactEngine->ReadContactLC(aContactId);
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -