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

📄 dbmsexamplecontainer.cpp

📁 SymbianOS的DBMS实例 详细的代码揭示SymbianOS数据库管理系统的使用 很规范
💻 CPP
字号:
/*
* ============================================================================
*  Name     : CDBMSexampleContainer from DBMSexampleContainer.h
*  Part of  : DBMSexample
*  Created  : 02/28/2003 by Forum Nokia
*  Implementation notes:
*  Version  :
*  Copyright: Nokia Corporation, 2003
* ============================================================================
*/

// INCLUDE FILES
#include "DBMSexampleContainer.h"

#include <eiklabel.h>  			// for example label control
#include <aknutils.h>  			// LatinBold19()
#include <dbmsexample.rsg>
#include <aknappui.h>
#include <aknlists.h> 
#include  <AknGlobalNote.h>
#include "DBMSengine.h"
#include "DBMSexample.hrh"
#include "DBMSexampleForm.h"
#include "DBMSexampleAppUi.h"

/*
-----------------------------------------------------------------------------

    CDBMSexampleContainer::ConstructL(const TRect& aRect)

	Description: EPOC two phased constructor

	Comments:

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CDBMSexampleContainer::ConstructL(const TRect& aRect)
    {
    CreateWindowL();

	DisplayRecordsL();

	// Create an enginer here
	iEngine = new(ELeave) CDBMSengine();

    iCba = CEikButtonGroupContainer::Current();

    SetRect( aRect );
    ActivateL();
    }

/*
-----------------------------------------------------------------------------

    CDBMSexampleContainer::~CDBMSexampleContainer()

	Description: Destructor

	Comments:

    Return values: N/A

-----------------------------------------------------------------------------
*/
CDBMSexampleContainer::~CDBMSexampleContainer()
    {
	delete iEngine;
	
	if ( iListBox )
		iAvkonAppUi->RemoveFromStack(iListBox);
	delete iListBox;
    }


/*
-----------------------------------------------------------------------------

    CDBMSexampleContainer::SizeChanged()

	Description: Called by framework when the view size is changed

	Comments:

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CDBMSexampleContainer::SizeChanged()
    {
    // control resizing
	if ( iListBox )
		iListBox->SetExtent(TPoint(0,0),TSize(176,160));
    }

/*
-----------------------------------------------------------------------------

    CDBMSexampleContainer::CountComponentControls() const

	Description: Called by framework to return number of controls in the current
				 container
	Comments:

    Return values: number of controls in the container

-----------------------------------------------------------------------------
*/
TInt CDBMSexampleContainer::CountComponentControls() const
    {
    // return nbr of controls inside this container
	if ( iListBox )
		return 1;
	else
		return 0;
    }

/*
-----------------------------------------------------------------------------

	TKeyResponse CDBMSexampleContainer::OfferKeyEventL(
		const TKeyEvent& aKeyEvent,TEventCode aType)

	Description: Called by the framework to process the key input

	Comments:

    Return values: whether the key is processed or not

-----------------------------------------------------------------------------
*/
TKeyResponse CDBMSexampleContainer::OfferKeyEventL(
    const TKeyEvent& aKeyEvent,TEventCode aType)
    {
    // See if we have a selection
    TInt code = aKeyEvent.iCode;
    switch(code)
        {
		// is navigator button pressed
    	case EKeyOK:
			{
			CDBMSexampleAppUi* appUi = STATIC_CAST(CDBMSexampleAppUi*, iAvkonAppUi);
	        DisplayFormL( R_MANAGE_DATABASE_DIALOG, EDBMSexampleCmdUpdateRecord, appUi );
			appUi->SetAppTitleTextL( R_RECORD_INFO_TEXT );
            return ( EKeyWasConsumed );
            break;
			}

		case EKeyBackspace:
			{
			CAknQueryDialog* dlg = CAknQueryDialog::NewL(CAknQueryDialog::ENoTone);

			dlg->PrepareLC( R_CONFIRMATION_QUERY_DELETE );
			if ( dlg->RunLD() )
				{
				// If the answer is yes, delete the record, and go ahead.
				TInt index = iListBox->CurrentItemIndex();
		
				if ( iEngine->GetRecordByIndexL( index ) )
					{
					// Remove the record from database
					iEngine->DeleteRecordL();
					DisplayRecordsL();
					}			
				}
            return ( EKeyWasConsumed );			
			break;
			}

        default:
            // Let Listbox take care of its key handling
			if ( iListBox )
				return iListBox->OfferKeyEventL(aKeyEvent, aType);
			else
				return (EKeyWasNotConsumed );
            break;
        }
    }

/*
-----------------------------------------------------------------------------

	CDBMSexampleContainer::ComponentControl(TInt aIndex) const
	
	Description: Called by framework to return the control pointer according
				 to the index

	Comments:

    Return values: Control pointer at the index

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

        default:
            return NULL;
        }
    }


/*
-----------------------------------------------------------------------------

	CDBMSexampleContainer::Draw(const TRect& aRect) const
	
	Description: Called by framework to draw the current control

	Comments:

    Return values: N/A

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


/*
-----------------------------------------------------------------------------

    CDBMSexampleContainer::HandleControlEventL(CCoeControl* aControl,TCoeEvent 
											   aEventType)

	Description: Handles control events

	Comments:

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CDBMSexampleContainer::HandleControlEventL(
    CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
    {
    }

/*
-----------------------------------------------------------------------------

    CDBMSexampleContainer::DisplayTheListboxL(CDesC16Array &aList)

	Description: Displays a listbox control according to the list input

	Comments:

    Return values: ETrue if the operation is successful
				   EFalse if the operation is not successful

-----------------------------------------------------------------------------
*/
TBool CDBMSexampleContainer::DisplayTheListboxL(CDesC16Array &aList)
{
    // check parameter length and return if zero
    TInt count = aList.Count();
    if(count == 0)
        {
        // if list is empty we return. Further, if the list obj is not empty
		// we should not leave it as it be. Clear the list content as well.
		if ( iListBox )
			ClearContentsInListBox();
        return EFalse;
        }

    if ( !iListBox )
	    {
		// If no list box is created, construct it.
		iListBox = new( ELeave ) CAknDoubleStyleListBox();
		iListBox->SetContainerWindowL( *this );
		iListBox->ConstructL( this, EAknListBoxSelectionList);
		iListBox->CreateScrollBarFrameL(ETrue);
		iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
	
		iAvkonAppUi->AddToStackL( iListBox );
		}

	//link against bafl.DLL
	MDesCArray* itemList = iListBox->Model()->ItemTextArray();
	CDesCArray* itemArray = (CDesCArray*) itemList;

	// Clear the contents in the list
	itemArray->Reset();

    // read message items from aList to itemArray
    // we read 2 items from aList to form a single item in itemArray
    TInt i = 0;
    TBuf<100> tempBuf;

    if(count%2 == 0) // check validity of our aList item count
        {
        while(i < count)
            {
            // clear buffer
            tempBuf.Delete(0, tempBuf.Length());
            // catenate the two lines(details & description) into one
            tempBuf.Append(_L("\t"));
            tempBuf.Append(aList[i]);
            i++;
            tempBuf.Append(_L("\t"));
            tempBuf.Append(aList[i]);
            // increment for the next round evaluation
            i++;
            // add created buffer to our list
            itemArray->AppendL(tempBuf);
            }
        }

	iListBox->HandleItemAdditionL( );
	// iListBox->HandleItemRemovalL( );
	iListBox->SetCurrentItemIndex( 0 );

	iListBox->SetRect( Rect() );

	iListBox->ActivateL();
	iListBox->DrawNow();

    return ETrue;
}

/*
-----------------------------------------------------------------------------

    CDBMSexampleContainer::DisplayRecordsL()

	Description: Displays all the records into a listbox control

	Comments:

    Return values: ETrue if the operation is successful
				   EFalse if the operation is not successful

-----------------------------------------------------------------------------
*/
TBool CDBMSexampleContainer::DisplayRecordsL()
{
	// Revert back to the normal CBA
	iCba = CEikButtonGroupContainer::Current();
	if( iCba) iCba->SetCommandSetL( R_AVKON_SOFTKEYS_OPTIONS_EXIT );

    CDesC16ArrayFlat* recordDescriptors = new( ELeave )CDesC16ArrayFlat(5);
    CleanupStack::PushL(recordDescriptors);

	// Read in the records into the descriptor
	if ( !iEngine->ReadRecordTitlesL( *recordDescriptors ) )
		{

		// Check if the list is empty or not. If it is empty, and check whether
		// the listbox has been created. If yes, clear everything in the listbox
		if ( (recordDescriptors->Count() == 0) && iListBox )
			{
			ClearContentsInListBox();
			}

		CleanupStack::PopAndDestroy(); // recordDescriptors
		return EFalse;
		}
	
	// Now display the records in the container
    // launch selection list
    if( DisplayTheListboxL( *recordDescriptors ) )
        {
        DrawNow();
        }
    else  // could not open the list (messageDescriptors was empty)
        {
		TBuf<64> des;
		CEikonEnv::Static()->ReadResource( des, R_NO_RECORD_IN_DATABASE );
        // _LIT(KNote, "No records in database");
        CAknGlobalNote* globalNote = CAknGlobalNote::NewLC();
	    globalNote->ShowNoteL( EAknGlobalInformationNote, des );
        CleanupStack::PopAndDestroy(); // GlobalNote
        }

    CleanupStack::PopAndDestroy(); // recordDescriptors
	return ETrue;
}


/*
-----------------------------------------------------------------------------

    CDBMSexampleContainer::DisplayFormL(TInt aResourceId, TInt aType, 
								   MDBMSobserver* aObserver )

	Description: Displays a form to present a record

	Comments:

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CDBMSexampleContainer::DisplayFormL(TInt aResourceId, TInt aType, 
								   MDBMSobserver* aObserver )
{
    CDBMSexampleForm* form = CDBMSexampleForm::NewL( aType, iEngine, aObserver );

	if ( aType == EDBMSexampleCmdUpdateRecord || aType == EDBMSexampleCmdDeleteRecord ||
		 aType == EDBMSexampleCmdRetrieveRecord )
		{
		// Get the current index of selection in the list box.
		TInt index = iListBox->CurrentItemIndex();
		
		if ( iEngine->GetRecordByIndexL( index ) )
			{
			TBuf<40 > name;
			TBuf< KDbBirthday > birthday;
			iEngine->GetBirthday( birthday );
			TTime time( birthday );
			
			iEngine->GetName( name );
			form->SetName( name );

			form->SetSex( iEngine->GetSex() );
			form->GetBirthday() = time;
			form->SetPersonalId( iEngine->GetPersonalId() );
			}
		else
			{
			delete form;
			return;
			}
		}
	else
		{
		// It is create record function. Create a default record first.

		// Preset some values for the form.
		_LIT( KName, "xxx" );
		// YYYYMMDD:HHMMSS.MMMMMM
		TTime time( _L("19640504:000000.000000"));
		TInt  id = 1234;

		TBuf<40> name;
		name.Copy( KName );
		form->SetName( name );
		form->SetSex( 0 );
		form->GetBirthday() = time;
		form->SetPersonalId( id );
		}
    
	form->ExecuteLD( aResourceId );
		
	// According to different kinds of form, display different CBAs
	iCba = CEikButtonGroupContainer::Current();

	// According to different kinds of form, display different CBAs
	switch ( aType )
		{
		case EDBMSexampleCmdUpdateRecord:
		case EDBMSexampleCmdRetrieveRecord:
			{
			if( iCba) iCba->SetCommandSetL( R_DBMS_UPDATE_CANCEL );
			form->DrawNow();
			break;
			}

		case EDBMSexampleCmdDeleteRecord:
			{
			if( iCba) iCba->SetCommandSetL( R_DBMS_DELETE_CANCEL );
			break;
			}
		}
	}

/*
-----------------------------------------------------------------------------

    CDBMSexampleContainer::GetEngine()

	Description: Get the created engine pointer

	Comments:

    Return values: engine pointer

-----------------------------------------------------------------------------
*/
CDBMSengine* CDBMSexampleContainer::GetEngine()
{
	return iEngine;
}

/*
-----------------------------------------------------------------------------

    CDBMSexampleContainer::ClearContentsInListBox()

	Description: clears all the contents in the listbox control if the listbox
				 has been created before.

	Comments:

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CDBMSexampleContainer::ClearContentsInListBox()
{

    if ( iListBox )
		{

		//link against bafl.DLL
		MDesCArray* itemList = iListBox->Model()->ItemTextArray();
		CDesCArray* itemArray = (CDesCArray*) itemList;

		// Clear the contents in the list
		itemArray->Reset();

		// read message items from aList to itemArray
		// we read 2 items from aList to form a single item in itemArray
		iListBox->HandleItemAdditionL( );

		iListBox->SetRect( Rect() );

		iListBox->ActivateL();
		iListBox->DrawNow();
	}
}

/*
-----------------------------------------------------------------------------

    CDBMSexampleContainer::IsListBoxConstructed()

	Description: Returns whether a ListBox control has been constructed or not

	Comments:

    Return values: ETrue if ListBox is constructed, else returns EFalse

-----------------------------------------------------------------------------
*/
TBool CDBMSexampleContainer::IsListBoxConstructed()
{
	if ( iListBox )
		return ETrue;
	else
		return EFalse;
}

⌨️ 快捷键说明

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