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

📄 ccontcontainer.cpp

📁 基于symbian OS的IP前缀添加器.可以在通话前为电话加一个IP的功能.
💻 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 <barsread.h> // TResource Reader
#include "CContContainer.h"
#include "contacts.hrh"

#include <eikmenub.h> // CEikMenuBar
#include "uikon.hrh" // TKeyCode #defines





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



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();
	OpenDefaultDatabaseL();
    
    SetRect( aRect );

    ActivateL();
    }


CContContainer::~CContContainer()
    {
    if ( iListBox )
        {
        delete iListBox;
        }

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

    if ( iContactArray )
        {
        delete iContactArray;
        }
}

// ---------------------------------------------------------
// 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 )
{
	TBool selectKeyPressed = (aType == EEventKey) && (aKeyEvent.iCode == EKeyOK);
	TBool savedGameListNotEmpty = (iListBox) && (iListBox->Model()->NumberOfItems() > 0);

    //if ( aType != EEventKey )
        {
       // return EKeyWasNotConsumed;
        }

	if (selectKeyPressed && savedGameListNotEmpty)
	{
		CEikMenuBar* parentMenuBar = iEikonEnv->AppUiFactory()->MenuBar();
		parentMenuBar->SetMenuTitleResourceId(R_CONTACTS_MENU_BAR);
		if (parentMenuBar)
		{
			parentMenuBar->TryDisplayMenuBarL();
			parentMenuBar->SetMenuTitleResourceId(R_CONTACTS_CONTACTS_MENUBAR);
			return EKeyWasConsumed;
		}
		else
			return iListBox->OfferKeyEventL(aKeyEvent,aType);
	}
	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::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::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 );

   
    ReadContactsFromDbL();
    UpdateListBoxL();
    }

⌨️ 快捷键说明

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