📄 ccontcontainer.cpp
字号:
if ( !IsContactsOnListBox() )
{
return;
}
// The caller does not take ownership of this object.
// so do not push it onto the CleanupStack
TInt currentItemIndex( iListBox->CurrentItemIndex() );
TInt count( iContactArray->Count() );
if (currentItemIndex < 0 || currentItemIndex >= count )
{
HBufC* textResource = StringLoader::LoadLC( R_CONTACTS_NO_CONTACT_SELECTED );
CAknInformationNote* informationNote = new( ELeave ) CAknInformationNote;
informationNote->ExecuteLD( textResource->Des() );
CleanupStack::PopAndDestroy( textResource );
return;
}
// Ask about deletion
if ( !ShowDeleteConfirmationQueryL( R_CONTACTS_DELETE_CONTACT_MESSAGE ) )
{
return;
}
// Sets the type of contact items to be included in
// sorted views of the database
iContactDb->SetDbViewContactType( KUidContactCard );
const CContactIdArray* contacts = iContactDb->SortedItemsL();
TContactItemId itemId( ( (*contacts)[currentItemIndex] ) );
CPbkContactEngine * pbkContactEng =
CPbkContactEngine::NewL( iDbName,
EFalse,
&iCoeEnv->FsSession() );
CleanupStack::PushL( pbkContactEng );
pbkContactEng->DeleteContactL( itemId );
CleanupStack::PopAndDestroy( pbkContactEng );
iContactDb->CloseTables();
delete iContactDb;
iContactDb = NULL;
iContactDb = CContactDatabase::OpenL( iDbName );
// Update iContactArray and iContactGroupArray
ReadContactsFromDbL();
UpdateListBoxL();
}
// ----------------------------------------------------
// CContContainer::CreateContactTemplateL()
// Creates new contact template to contact database.
// ----------------------------------------------------
//
void CContContainer::CreateContactTemplateL()
{
if ( !IfContactDbSelected() )
{
ShowInformationNoteNoDbL();
return;
}
HBufC* templateName = HBufC::NewLC( KMaxTemplateNameLength );
TPtr ptr( templateName->Des() );
if( !OpenTextBoxQueryL( R_CONTACTS_CREATE_TEMPLATE_MESSAGE,
ptr,
KMaxTextLength ) )
{
CleanupStack::PopAndDestroy( 1 ); // templateName
return;
}
// The caller takes ownership of this object.
// so push it onto the CleanupStack
CContactItem* contactTemplate =
iContactDb->CreateContactCardTemplateL( templateName->Des() );
CleanupStack::PushL( contactTemplate );
CContactItemField* givenNameField =
CContactItemField::NewLC( KStorageTypeText,
KUidContactFieldGivenName );
CContactItemField* familyNameField =
CContactItemField::NewLC(KStorageTypeText,
KUidContactFieldFamilyName );
CContactItemField* phoneNumberField =
CContactItemField::NewLC(KStorageTypeText,
KUidContactFieldPhoneNumber );
// Add data to fields
givenNameField->TextStorage()->SetTextL( KFirstName );
givenNameField->SetMapping( KUidContactFieldVCardMapUnusedN );
familyNameField->TextStorage()->SetTextL( KLastName );
familyNameField->SetMapping( KUidContactFieldVCardMapUnusedN );
phoneNumberField->TextStorage()->SetTextL( KPhoneNumber );
phoneNumberField->SetMapping( KUidContactFieldVCardMapTEL );
// Add fields to a contact template
contactTemplate->AddFieldL( *givenNameField );
contactTemplate->AddFieldL( *familyNameField );
contactTemplate->AddFieldL( *phoneNumberField );
// Item takes the ownership of fields
CleanupStack::Pop( 3 ); // givenNameField, familyNameField, phoneNumberField
CleanupStack::PopAndDestroy( 2 );// templateName, contactTemplate
// Update iContactArray and iContactGroupArray
ReadContactsFromDbL();
UpdateListBoxL();
}
// ----------------------------------------------------
// CContContainer::CreateContactGroupL()
// Create new contact group.
// ----------------------------------------------------
//
void CContContainer::CreateContactGroupL()
{
if ( !IfContactDbSelected() )
{
ShowInformationNoteNoDbL();
return;
}
if ( IsContactsOnListBox() )
{
return;
}
CPbkContactEngine * pbkContactEng =
CPbkContactEngine::NewL( iDbName,
EFalse,
&iCoeEnv->FsSession() );
CleanupStack::PushL( pbkContactEng );
// Open dialog
HBufC* groupName = HBufC::NewLC( KMaxGroupNameLength );
TPtr ptr( groupName->Des() );
if ( OpenTextBoxQueryL( R_CONTACTS_CREATE_GROUP_MESSAGE,
ptr,
KMaxTextLength ) )
{
//Create contactGroup
CContactGroup* contactGroup =
pbkContactEng->CreateContactGroupL( groupName->Des() );
delete contactGroup;
}
CleanupStack::PopAndDestroy( 2 ); // pbkContactEng, groupName
iContactDb->CloseTables();
delete iContactDb;
iContactDb = NULL;
iContactDb = CContactDatabase::OpenL( iDbName );
// Update iContactArray and iContactGroupArray
ReadContactsFromDbL();
UpdateListBoxL();
}
// ----------------------------------------------------
// CContContainer::DeleteContactGroupL()
// Delete contact group from contact database.
// ----------------------------------------------------
//
void CContContainer::DeleteContactGroupL()
{
if ( !IfContactDbSelected() )
{
ShowInformationNoteNoDbL();
return;
}
if ( IsContactsOnListBox() )
{
return;
}
TInt currentItemIndex( iListBox->CurrentItemIndex() );
TInt count( iContactGroupArray->Count() );
if ( currentItemIndex < 0 || currentItemIndex >= count )
{
return;
}
// The caller takes ownership of the returned object.
// groupIdList is NULL if there are no groups in the database.
CContactIdArray* groupIdList = iContactDb->GetGroupIdListL();
if ( !groupIdList )
{
return;
}
CleanupStack::PushL( groupIdList );
CPbkContactEngine* pbkContactEng =
CPbkContactEngine::NewL( iDbName,
EFalse,
&iCoeEnv->FsSession() );
CleanupStack::PushL(pbkContactEng);
TContactItemId itemId = ((*groupIdList)[currentItemIndex]);
// Ask about deletion
if (ShowDeleteConfirmationQueryL( R_CONTACTS_DELETE_GROUP_MESSAGE ) )
{
pbkContactEng->DeleteContactGroupL( itemId );
}
CleanupStack::PopAndDestroy( 2 ); //pbkContactEng , groupIdList
iContactDb->CloseTables();
delete iContactDb;
iContactDb = NULL;
iContactDb = CContactDatabase::OpenL( iDbName );
// Update iContactArray and iContactGroupArray
ReadContactsFromDbL();
UpdateListBoxL();
}
// ---------------------------------------------------------
// CContContainer::ReadContactsFromDbL()
// Read contacts from contact database
// and sort contacts by Family Name and
// Given Name. Add sorted items to
// iContactArray.
// ---------------------------------------------------------
//
void CContContainer::ReadContactsFromDbL()
{
ReadContactGroupsFromDbL();
// Delete old contact from listbox
TInt count( iContactArray->Count() );
if ( count )
{
// Clear array. Start to index 0
iContactArray->Delete( 0, count );
}
if ( !IfContactDbSelected() )
{
return;
}
iContactDb->SetDbViewContactType( KUidContactCard );
TFieldType aFieldType1( KUidContactFieldFamilyName );
TFieldType aFieldType2( KUidContactFieldGivenName );
CContactDatabase::TSortPref sortPref1( aFieldType1 );
CContactDatabase::TSortPref sortPref2( aFieldType2 );
// Sort contacts by Family and Given Name
CArrayFixFlat<CContactDatabase::TSortPref>* aSortOrder =
new (ELeave) CArrayFixFlat<CContactDatabase::TSortPref>(2);
CleanupStack::PushL( aSortOrder );
aSortOrder->AppendL( sortPref1 );
aSortOrder->AppendL( sortPref2 );
// The database takes ownership of the sort order array passed in
iContactDb->SortL( aSortOrder );
// The caller does not take ownership of this object.
// so do not push it onto the CleanupStack
const CContactIdArray* contacts = iContactDb->SortedItemsL();
// Go through each contact item and
// make items for listbox
const TInt nc( contacts->Count() );
for ( TInt i( nc-1 ); i >= 0; i-- ) //For each ContactId
{
CContactItem* contact = NULL;
// The caller takes ownership of the returned object.
// So push it onto the CleanupStack
contact = iContactDb->OpenContactL( (*contacts)[i] );
CleanupStack::PushL( contact );
CContactItemFieldSet& fieldSet = contact->CardFields();
HBufC* firstNameBuf = NULL;
HBufC* lastNameBuf = NULL;
// Get first name
TInt findpos( fieldSet.Find( KUidContactFieldGivenName ) );
// Check that the first name field is actually there.
if ( (findpos > -1) || (findpos >= fieldSet.Count()) )
{
CContactItemField& firstNameField = fieldSet[findpos];
CContactTextField* firstName = firstNameField.TextStorage();
firstNameBuf = firstName->Text().AllocLC();
}
// Get last name
findpos = fieldSet.Find( KUidContactFieldFamilyName );
// Check that the last name field is actually there.
if ( (findpos > -1) || (findpos >= fieldSet.Count()) )
{
CContactItemField& lastNameField = fieldSet[ findpos ];
CContactTextField* lastName = lastNameField.TextStorage();
lastNameBuf = lastName->Text().AllocLC();
}
TInt len( 0 );
if ( firstNameBuf )
{
len+=firstNameBuf->Length();
}
if ( lastNameBuf )
{
len+=lastNameBuf->Length();
}
// Create new buffer and add space for formatters
HBufC* combinedDes = HBufC::NewLC( len + KFormattersSpace );
if ( ( firstNameBuf ) && ( lastNameBuf ) )
{
combinedDes->Des().Format( KListItemFormatter,
lastNameBuf,
firstNameBuf );
}
else
{
if ( firstNameBuf )
{
combinedDes->Des().Format( KListOItemFormatter, firstNameBuf );
}
else if ( lastNameBuf )
{
combinedDes->Des().Format( KListOItemFormatter, lastNameBuf );
}
}
// Insert a contacts name into the array at the specified position.
// If the specified position is the same as the current number of
// descriptor elements in the array, this has the effect of
// appending the element.
iContactArray->InsertL( 0, *combinedDes );
CleanupStack::PopAndDestroy( combinedDes );
if ( lastNameBuf )
{
CleanupStack::PopAndDestroy( lastNameBuf );
}
if ( firstNameBuf )
{
CleanupStack::PopAndDestroy( firstNameBuf );
}
iContactDb->CloseContactL( contact->Id() );
CleanupStack::PopAndDestroy( contact );
}
CleanupStack::Pop( aSortOrder );
}
// ---------------------------------------------------------
// CContContainer::ReadContactGroupsFromDbL()
// Read contact groups from contact database.
// Do not sort contact groups. Add group items
// to iContactGroupArray.
// ---------------------------------------------------------
//
void CContContainer::ReadContactGroupsFromDbL()
{
TInt count( iContactGroupArray->Count() );
if ( count )
{
// Clear array. Start to index 0
iContactGroupArray->Delete( 0, count );
}
if ( !IfContactDbSelected() )
{
return;
}
//The caller takes ownership of the returned object.
// groupIdList is NULL if there are no groups in the database.
CContactIdArray* groupIdList = iContactDb->GetGroupIdListL();
if( !groupIdList )
{
return;
}
CleanupStack::PushL( groupIdList );
CContactGroup* contactGroup = NULL;
const TInt nc( groupIdList->Count() );
for ( TInt i( nc-1 ); i >= 0; i-- ) //For each Contact Group Id
{
CContactItem* contact=NULL;
// The caller takes ownership of the returned object.
// So push it onto the CleanupStack
contact = iContactDb->OpenContactL( ((*groupIdList)[i]) );
CleanupStack::PushL( contact );
contactGroup = (CContactGroup*) contact;
TInt len( contactGroup->GetGroupLabelL().Length() );
HBufC* groupLabelBuf = HBufC::NewLC( len + KFormattersSpace );
HBufC* combinedDes = HBufC::NewLC( len + KFormattersSpace );
*groupLabelBuf = contactGroup->GetGroupLabelL();
combinedDes->Des().Format( KListOItemFormatter, groupLabelBuf );
// Insert a group name into the array at the specified position.
// If the specified position is the same as the current number of
// descriptor elements in the array, this has the effect of
// appending the element.
iContactGroupArray->InsertL( 0, *combinedDes );
CleanupStack::PopAndDestroy( combinedDes );
CleanupStack::PopAndDestroy( groupLabelBuf );
iContactDb->CloseContactL( contact->Id() );
CleanupStack::PopAndDestroy( contact );
}
CleanupStack::PopAndDestroy( groupIdList );
}
// ---------------------------------------------------------
// CContContainer::DatabaseExists()
// Check if contact database is created.
// ---------------------------------------------------------
//
TBool CContContainer::DatabaseExists( TDesC& aDbName )
{
HBufC* contactFile = HBufC::NewLC( KMaxDatabasePathAndNameLength );
TPtr ptr( contactFile->Des() );
// Resolve path for contact database file
GetContactDbName( aDbName, ptr );
CContactDatabase* contactDb = NULL;
TBool retValue = ETrue;
TRAPD(err, contactDb = CContactDatabase::OpenL( contactFile->Des() ););
// Check if database already exist
if ( err == KErrNotFound )
{
retValue = EFalse;
}
else
{
contactDb->CloseTables();
delete contactDb;
}
CleanupStack::PopAndDestroy( contactFile );
return retValue;
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -