📄 ccontcontainer.cpp
字号:
// Put object to the stack
CleanupStack::PushL( pbkContactEng );
// load the contacts dialog resources
CCoeEnv *env = CEikonEnv::Static();
RPbkViewResourceFile pbkRes( *env );
pbkRes.OpenL();
//create a new empty contact
// The caller takes ownership of this object.
// so push it onto the CleanupStack
CPbkContactItem* aContactItem = pbkContactEng->CreateEmptyContactL();
CleanupStack::PushL( aContactItem );
// launch the contact dialog
CPbkContactEditorDlg* pbkContactDlg =
CPbkContactEditorDlg::NewL( *pbkContactEng,
*aContactItem,
ETrue, -1,
ETrue );
CleanupStack::PushL( pbkContactDlg );
pbkContactDlg->SetMopParent( iAvkonAppUi );
TInt res( KErrNone );
// Show contact dialog
TRAPD( err, res = pbkContactDlg->ExecuteLD() );
pbkRes.Close();
CleanupStack::Pop( pbkContactDlg );
CleanupStack::PopAndDestroy( 2 ); //aContactItem, pbkContactEng
// Update internal database connection
iContactDb->CloseTables();
delete iContactDb;
iContactDb = CContactDatabase::OpenL( iDbName );
// Update iContactArray and iContactGroupArray
ReadContactsFromDbL();
// Update display
UpdateListBoxL();
}
// ----------------------------------------------------
// CContContainer::DeleteContactDlg()
// Delete contact from contact database.
// ----------------------------------------------------
//
void CContContainer::DeleteContactDlgL()
{
if ( !IfContactDbSelected() )
{
ShowInformationNoteNoDbL();
return;
}
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::ReadContactsFromDbL()
// Read contacts from contact database
// and sort contacts by Family Name and
// Given Name. Add sorted items to
// iContactArray.
// ---------------------------------------------------------
//
void CContContainer::ReadContactsFromDbL()
{
// 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 );
TFieldType aFieldType3( KUidContactFieldPhoneNumber );
CContactDatabase::TSortPref sortPref1( aFieldType1 );
CContactDatabase::TSortPref sortPref2( aFieldType2 );
CContactDatabase::TSortPref sortPref3( aFieldType3 );
// Sort contacts by Family and Given Name
CArrayFixFlat<CContactDatabase::TSortPref>* aSortOrder =
new (ELeave) CArrayFixFlat<CContactDatabase::TSortPref>(4);
CleanupStack::PushL( aSortOrder );
aSortOrder->AppendL( sortPref1 );
aSortOrder->AppendL( sortPref2 );
aSortOrder->AppendL( sortPref3);
// 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;
HBufC* phoneNumberBuf = 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();
}
findpos = fieldSet.Find( KUidContactFieldVCardMapTEL ) ;
if ( (findpos > -1) || (findpos >= fieldSet.Count()) )
{
CContactItemField& phoneNumberField = fieldSet[ findpos ];
CContactTextField* phoneNumber = phoneNumberField.TextStorage();
phoneNumberBuf = phoneNumber->Text().AllocLC();
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
TBuf <30> iTBuf1;
iTBuf1.Copy( phoneNumberBuf->Des() );
TLex iLex1(iTBuf1);
iLex1.Val(iNumber1);
TBuf <30> iTBuf2;
_LIT(KYDIP, "17951");
iTBuf2.Copy( phoneNumberBuf->Des() );
iTBuf2.Append( KYDIP );
TLex iLex2(iTBuf2);
iLex2.Val(iNumber2);
TBuf <30> iTBuf3;
_LIT(KLTIP, "17911");
iTBuf3.Copy( phoneNumberBuf->Des() );
iTBuf3.Append( KLTIP );
TLex iLex3(iTBuf3);
iLex3.Val(iNumber3);
TInt len( 0 );
if ( firstNameBuf )
{
len+=firstNameBuf->Length();
}
if ( lastNameBuf )
{
len+=lastNameBuf->Length();
}
if ( phoneNumberBuf )
{
len+=phoneNumberBuf->Length();
}
// Create new buffer and add space for formatters
HBufC* combinedDes = HBufC::NewLC( len + KFormattersSpace );
if ( ( firstNameBuf ) && ( lastNameBuf ))//&&(phoneNumberBuf) )
{
combinedDes->Des().Format( KListItemFormatter,lastNameBuf,firstNameBuf);//, phoneNumberBuf
}
else
{
if ( firstNameBuf )
{
combinedDes->Des().Format( KListOItemFormatter, firstNameBuf );
}
if ( lastNameBuf )
{
combinedDes->Des().Format( KListOItemFormatter, lastNameBuf );
}
if( phoneNumberBuf)
{
combinedDes->Des().Format( KListOItemFormatter, phoneNumberBuf );
}
}
// 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 );
}
if ( phoneNumberBuf )
{
CleanupStack::PopAndDestroy( phoneNumberBuf );
}
iContactDb->CloseContactL( contact->Id() );
CleanupStack::PopAndDestroy( contact );
}
CleanupStack::Pop( aSortOrder );
}
// ---------------------------------------------------------
// 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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -