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

📄 ccontcontainer.cpp

📁 实现好友名单。可以今天添加、删除以及增加等操作
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/* Copyright (c) 2003, Nokia. All rights reserved */

// INCLUDE FILES
#include <aknquerydialog.h>
#include <aknnotewrappers.h>
#include <cntdb.h>
#include <cntitem.h>
#include <cntfldst.h>
#include <contacts.rsg>

#include <aknlists.h>
#include <avkon.hrh>
#include <CPbkContactEngine.h>
#include <CPbkViewState.h>
#include <CPbkContactEditorDlg.h>
#include <CPbkContactItem.h>
#include <f32file.h>
#include <rpbkviewresourcefile.h>
#include <stringloader.h>

#include "CContContainer.h"
#include "contacts.hrh"


// ================= MEMBER FUNCTIONS =======================
//
// ---------------------------------------------------------
// CContContainer::CContContainer()
// constructor
// ---------------------------------------------------------
//
CContContainer::CContContainer()
    {
    iContactDb = NULL;
    iSelectedItemId = -1;
    }

// ---------------------------------------------------------
// CContContainer::ConstructL()
// Two phased constructor
// ---------------------------------------------------------
//
void CContContainer::ConstructL( const TRect& aRect )
    {
    CreateWindowL();

    iContactArray = new (ELeave) CDesCArrayFlat( KArraySize );
    iContactGroupArray = new (ELeave) CDesCArrayFlat( KArraySize );

    // Create Listbox
    iListBox = new (ELeave) CAknSingleStyleListBox;

    if ( iListBox )
        {
        iListBox->ConstructL( NULL, EAknListBoxSelectionList );
        iListBox->Model()->SetItemTextArray( iContactArray );
        iListBox->CreateScrollBarFrameL();
        iListBox->Model()->SetOwnershipType( ELbmDoesNotOwnItemArray );
        iListBox->SetListBoxObserver( this );
        iListBox->HandleItemAdditionL();
        }
    SizeChanged();

    SetRect( aRect );

    ActivateL();
    }

// ---------------------------------------------------------
// CContContainer::~CContContainer ()
// Destructor
// ---------------------------------------------------------
//
CContContainer::~CContContainer()
    {
    if ( iListBox )
        {
        delete iListBox;
        }

    if ( iContactDb )
        {
        iContactDb->CloseTables();
        delete iContactDb;
        }

    if ( iContactArray )
        {
        delete iContactArray;
        }

    if ( iContactGroupArray )
        {
        delete iContactGroupArray;
        }
    }

// ---------------------------------------------------------
// CContContainer::SizeChanged()
// Called by framework when the view size is changed.
// ---------------------------------------------------------
//
void CContContainer::SizeChanged()
    {
    // We want the list box to begin from the X = 0, Y = 45
    // and fill at least its minimum size.
    iListBox->SetExtent( TPoint(0,45), iListBox->MinimumSize() );
    }

// ---------------------------------------------------------
// CContContainer::CountComponentControls()
// Return the number of controls inside this container. We
// have only the list box.
// ---------------------------------------------------------
//
TInt CContContainer::CountComponentControls() const
    {
    return 1; // return nbr of controls inside this container
    }

// ---------------------------------------------------------
// CContContainer::ComponentControl()
// ---------------------------------------------------------
//
CCoeControl* CContContainer::ComponentControl(TInt aIndex) const
    {
    switch ( aIndex )
        {
        case 0:
            if ( iListBox )
                {
                return iListBox;
                }

        default:
            return NULL;
        }
    }

// ---------------------------------------------------------
// CContContainer::Draw()
// ---------------------------------------------------------
//
void CContContainer::Draw( const TRect& aRect ) const
    {
    CWindowGc& gc = SystemGc();
    gc.SetPenStyle( CGraphicsContext::ENullPen );
    gc.SetBrushColor( KRgbWhite );
    gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
    gc.DrawRect( aRect );
    }

// ----------------------------------------------------------------------------
// CContContainer::OfferKeyEventL()
// Handles the key events.
// ----------------------------------------------------------------------------
//
TKeyResponse CContContainer::OfferKeyEventL( const TKeyEvent& aKeyEvent,
                                              TEventCode aType )
	{
    if ( aType != EEventKey )
        {
        return EKeyWasNotConsumed;
        }

	if ( iListBox )
        {
        return iListBox->OfferKeyEventL( aKeyEvent, aType );
        }

	return EKeyWasNotConsumed;

    }

// ---------------------------------------------------------------------------
// CContContainer::HandleListBoxEventL()
// Handles listboxevents.
// ---------------------------------------------------------------------------
//
void CContContainer::HandleListBoxEventL( CEikListBox* /*aListBox */,
    TListBoxEvent aEventType )
    {
    switch( aEventType )
        {
        case EEventEnterKeyPressed: // Flow through
        case EEventItemClicked:
            EditContactDlgL();
            break;
        default:
            break;
        }
    }

// ---------------------------------------------------------
// CContContainer::HandleControlEventL()
// ---------------------------------------------------------
//
void CContContainer::HandleControlEventL(
    CCoeControl* /*Control*/,TCoeEvent /*aEventType*/)
    {
    }

// ---------------------------------------------------------
// CContContainer::UpdateContactDbName()
// Store current database path and name.
// ---------------------------------------------------------
//
void CContContainer::UpdateContactDbName( TDesC& aDbName )
    {
    iDbName = aDbName;
    }

// ---------------------------------------------------------
// CContContainer::GetContactDbName()
// ---------------------------------------------------------
//
void CContContainer::GetContactDbName( TDesC& aDbName, TPtr aCompleteDbName )
	{
    TBuf<KMaxDatabasePathAndNameLength> contactFile( KContactFile );
    contactFile.Append( aDbName );
    contactFile.Append( KPrefix );

    aCompleteDbName = contactFile;
	}


// ----------------------------------------------------
// CContContainer::UpdateListBoxL()
// Updates listbox.
// ----------------------------------------------------
//
void CContContainer::UpdateListBoxL()
    {
    iListBox->HandleItemAdditionL();
    iListBox->SetCurrentItemIndexAndDraw( 0 );
    SizeChanged();
    DrawNow();
    }

// ----------------------------------------------------
// CContContainer::IsContactsOnListBox()
// The function returns state of the listbox.
// If contacts on the listbox has been selected
// the function returns ETrue.
// ----------------------------------------------------
//
TBool CContContainer::IsContactsOnListBox()
    {
    CDesCArray* itemArray = (CDesCArray*) iListBox->Model()->ItemTextArray();

    if ( iContactArray == itemArray )
        {
	    return ETrue;
	    }

    return EFalse;
    }
// ----------------------------------------------------
// CContContainer::ShowContactsL()
// Show contacts in the listbox.
// ----------------------------------------------------
//
void CContContainer::ShowContactsL()
    {

    if ( !IfContactDbSelected() )
        {
        ShowInformationNoteNoDbL();
        return;
        }

    if ( IsContactsOnListBox() )
        {
        return;
        }

    iListBox->Model()->SetItemTextArray( iContactArray );
    iListBox->Model()->SetOwnershipType( ELbmDoesNotOwnItemArray );
    UpdateListBoxL();
    }

// ----------------------------------------------------
// CContContainer::ShowContactGroupsL()
// Show contact groups in the listbox.
// ----------------------------------------------------
//
void CContContainer::ShowContactGroupsL()
	{

    if ( !IfContactDbSelected() )
        {
        ShowInformationNoteNoDbL();
        return;
        }

    if ( !IsContactsOnListBox() )
	    {
	    return;
	    }

    iListBox->Model()->SetItemTextArray( iContactGroupArray );
    iListBox->Model()->SetOwnershipType( ELbmDoesNotOwnItemArray );
    UpdateListBoxL();
    }



// ----------------------------------------------------
// CContContainer::ShowInformationNoteNoDbL()
// Show information not if contact database
// did not opened.
// ----------------------------------------------------
//
void CContContainer::ShowInformationNoteNoDbL()
    {
    HBufC* textResource = StringLoader::LoadLC( R_CONTACTS_NO_DB_SELECTED );
    CAknInformationNote* informationNote = new( ELeave ) CAknInformationNote;
    informationNote->ExecuteLD( textResource->Des() );
    CleanupStack::PopAndDestroy( textResource );

    }
// ----------------------------------------------------
// CContContainer::IfContactDbSelected()
// The function checks is contact database
// has been opened.
// ----------------------------------------------------
//
TBool CContContainer::IfContactDbSelected()
    {

    if ( iContactDb )
	    {
	    return ETrue;
	    }

    return EFalse;
    }

// ----------------------------------------------------
// CContContainer::CanAddContactToGroupL()
// The function returns ETrue is contact database
// has been opened and contains at least one contact item.
// ----------------------------------------------------
//
TBool CContContainer::CanAddContactToGroupL()
    {
    if ( !IfContactDbSelected() )
	    {
        ShowInformationNoteNoDbL();
	    return EFalse;
	    }

    if ( !iContactArray->Count() )
        {
        HBufC* textResource = StringLoader::LoadLC( R_CONTACTS_NO_CONTACT_SELECTED );
        CAknInformationNote* informationNote = new( ELeave ) CAknInformationNote;
        informationNote->ExecuteLD( textResource->Des() );
        CleanupStack::PopAndDestroy( textResource );
        return EFalse;
        }

    return ETrue;
    }


// ---------------------------------------------------------
// CContContainer::CreateContactDatabaseL()
// The function creates the contact database.
// ---------------------------------------------------------
//
void CContContainer::CreateContactDatabaseL( TDesC& aDbName )
    {
    //get connection to phonebook database
    CContactDatabase* contactDb = NULL;
    HBufC* contactFile = HBufC::NewLC( KMaxDatabasePathAndNameLength );
    TPtr ptr( contactFile->Des() );

    // Resolve path for contact database file
    GetContactDbName( aDbName, ptr );

    if ( iContactDb )
        {

        // Check if database opened
        if ( !iDbName.Compare( contactFile->Des() ) )
            {
            HBufC* text = StringLoader::LoadLC( R_CONTACTS_DB_ALREADY_EXISTS );
            CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
            informationNote->ExecuteLD( *text );
            CleanupStack::PopAndDestroy( text );
            CleanupStack::PopAndDestroy( contactFile );
            return;
            }

        iContactDb->CloseTables();
        delete iContactDb;
        iContactDb = NULL;
        }

    TRAPD( err, contactDb = CContactDatabase::OpenL( contactFile->Des() ););
    // Check if database already exist
    if ( err == KErrNotFound )
        {
        // Create new contact database
        TRAPD( err, contactDb = CContactDatabase::CreateL( contactFile->Des() ););
        if ( err != KErrNone )
            {
	            contactDb = NULL;
            }
        else
            {
            // Just created the database connection crash in the emulator
            // if user tries create contact template immediately.
            // Close the database connection and open it again.
            contactDb->CloseTables ();
            delete contactDb;
            contactDb = NULL;
            TRAPD( err, contactDb = CContactDatabase::OpenL( contactFile->Des() ););
            if ( err != KErrNone)
                {
                contactDb = NULL;
                }

            }
    } // if ( err == KErrNotFound )
    else
        {
        HBufC* text = StringLoader::LoadLC( R_CONTACTS_DB_ALREADY_EXISTS );
        CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
        informationNote->ExecuteLD( *text );
        CleanupStack::PopAndDestroy( text );
        }

    if ( contactDb )
        {
        iContactDb = contactDb;
        UpdateContactDbName( *contactFile );
        }
    else
        {
        TBuf<15> contactFile( KNullDesC );
        UpdateContactDbName( contactFile );
        }

    CleanupStack::PopAndDestroy( contactFile );

    // Update iContactArray and iContactGroupArray
    ReadContactsFromDbL();
    UpdateListBoxL();
    }
// ---------------------------------------------------------
// CContContainer::DeleteContactDatabaseL()
// ---------------------------------------------------------
//
void CContContainer::DeleteContactDatabaseL( TDesC& aDbName )
    {

    HBufC* contactFile = HBufC::NewLC( KMaxDatabasePathAndNameLength );
    TPtr ptr( contactFile->Des() );

    // Resolve path for contact database file
    GetContactDbName( aDbName, ptr );

    // Check if database opened
    if ( iContactDb && ( !iDbName.Compare( contactFile->Des() ) ) )
        {
        iContactDb->CloseTables();
        delete iContactDb;
        iContactDb = NULL;

        TBuf<15> noContactFile( KNullDesC );

⌨️ 快捷键说明

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