📄 smscryptosymbiancppuilistbox.cpp
字号:
/*
* ============================================================================
* Name : SMSCryptoSymbianCppUIListBox.cpp
* Part of : Open C SMS Crypto Example
* Created : 05/25/2007 by Forum Nokia
* Version : 1.0
* Copyright: Nokia Corporation
* ============================================================================
*/
#include "SMSCryptoSymbianCppUIListBox.h"
#include "SMSCryptoSymbianCppUIListBoxView.h"
#include "SMSCryptoSymbianCppUI.hrh"
#include "SMSCryptoSymbianCppUIListBox.hrh"
#include "SMSCryptoSymbianCppUITextEditor.hrh"
#include <barsread.h>
#include <stringloader.h>
#include <aknlists.h>
#include <eikenv.h>
#include <akniconarray.h>
#include <eikclbd.h>
#include <aknviewappui.h>
#include <eikappui.h>
#include <SMSCryptoSymbianCppUI.rsg>
#include <smuthdr.h>
#include <smut.h>
const TInt KArrayGranularity=10;
/**
* First phase of Symbian two-phase construction. Should not
* contain any code that could leave.
*/
CSMSCryptoSymbianCppUIListBox::CSMSCryptoSymbianCppUIListBox()
{
iListBox = NULL;
}
/**
* Destroy child controls.
*/
CSMSCryptoSymbianCppUIListBox::~CSMSCryptoSymbianCppUIListBox()
{
delete iListBox;
delete iArrayAddr;
delete iArrayMsgBody;
delete iSmsMtm;
delete iClientMtmReg;
delete iSession;
}
/**
* Construct the control (first phase).
* Creates an instance and initializes it.
* Instance is not left on cleanup stack.
* @param aRect bounding rectangle
* @param aParent owning parent, or NULL
* @param aCommandObserver command observer
* @return initialized instance of CSMSCryptoSymbianCppUIListBox
*/
CSMSCryptoSymbianCppUIListBox* CSMSCryptoSymbianCppUIListBox::NewL(
const TRect& aRect,
const CCoeControl* aParent,
MEikCommandObserver* aCommandObserver )
{
CSMSCryptoSymbianCppUIListBox* self = CSMSCryptoSymbianCppUIListBox::NewLC(
aRect,
aParent,
aCommandObserver );
CleanupStack::Pop( self );
return self;
}
/**
* Construct the control (first phase).
* Creates an instance and initializes it.
* Instance is left on cleanup stack.
* @param aRect The rectangle for this window
* @param aParent owning parent, or NULL
* @param aCommandObserver command observer
* @return new instance of CSMSCryptoSymbianCppUIListBox
*/
CSMSCryptoSymbianCppUIListBox* CSMSCryptoSymbianCppUIListBox::NewLC(
const TRect& aRect,
const CCoeControl* aParent,
MEikCommandObserver* aCommandObserver )
{
CSMSCryptoSymbianCppUIListBox* self = new ( ELeave ) CSMSCryptoSymbianCppUIListBox();
CleanupStack::PushL( self );
self->ConstructL( aRect, aParent, aCommandObserver );
return self;
}
/**
* Construct the control (second phase).
* Creates a window to contain the controls and activates it.
* @param aRect bounding rectangle
* @param aCommandObserver command observer
* @param aParent owning parent, or NULL
*/
void CSMSCryptoSymbianCppUIListBox::ConstructL(
const TRect& aRect,
const CCoeControl* aParent,
MEikCommandObserver* aCommandObserver )
{
if ( aParent == NULL )
{
CreateWindowL();
}
else
{
SetContainerWindowL( *aParent );
}
iFocusControl = NULL;
iCommandObserver = aCommandObserver;
InitializeControlsL();
SetRect( aRect );
ActivateL();
iSession = CMsvSession::OpenSyncL(*this);
iClientMtmReg = CClientMtmRegistry::NewL(*iSession);
iSmsMtm = static_cast<CSmsClientMtm*>(iClientMtmReg->NewMtmL(KUidMsgTypeSMS));
iArrayAddr = new (ELeave) CDesCArrayFlat( KArrayGranularity );
iArrayMsgBody = new (ELeave) CDesCArrayFlat( KArrayGranularity );
GetSMSFromInboxL();
}
/**
* Return the number of controls in the container (override)
* @return count
*/
TInt CSMSCryptoSymbianCppUIListBox::CountComponentControls() const
{
return ( int ) ELastControl;
}
/**
* Get the control with the given index (override)
* @param aIndex Control index [0...n) (limited by #CountComponentControls)
* @return Pointer to control
*/
CCoeControl* CSMSCryptoSymbianCppUIListBox::ComponentControl( TInt aIndex ) const
{
switch ( aIndex )
{
case EListBox:
return iListBox;
}
// handle any user controls here...
return NULL;
}
/**
* Handle resizing of the container. This implementation will lay out
* full-sized controls like list boxes for any screen size, and will layout
* labels, editors, etc. to the size they were given in the UI designer.
* This code will need to be modified to adjust arbitrary controls to
* any screen size.
*/
void CSMSCryptoSymbianCppUIListBox::SizeChanged()
{
CCoeControl::SizeChanged();
LayoutControls();
}
/**
* Layout components as specified in the UI Designer
*/
void CSMSCryptoSymbianCppUIListBox::LayoutControls()
{
iListBox->SetExtent( TPoint( 0, 0 ), iListBox->MinimumSize() );
}
/**
* Handle key events.
*/
TKeyResponse CSMSCryptoSymbianCppUIListBox::OfferKeyEventL(
const TKeyEvent& aKeyEvent,
TEventCode aType )
{
if ( aKeyEvent.iCode == EKeyLeftArrow
|| aKeyEvent.iCode == EKeyRightArrow )
{
// Listbox takes all events even if it doesn't use them
return EKeyWasNotConsumed;
}
if ( aKeyEvent.iCode == EKeyOK )
{
return EKeyWasNotConsumed;
}
if ( iFocusControl != NULL
&& iFocusControl->OfferKeyEventL( aKeyEvent, aType ) == EKeyWasConsumed )
{
return EKeyWasConsumed;
}
return CCoeControl::OfferKeyEventL( aKeyEvent, aType );
}
/**
* Initialize each control upon creation.
*/
void CSMSCryptoSymbianCppUIListBox::InitializeControlsL()
{
iListBox = new ( ELeave ) CAknSingleStyleListBox;
iListBox->SetContainerWindowL( *this );
{
TResourceReader reader;
iEikonEnv->CreateResourceReaderLC( reader, R_SMSCRYPTO_SYMBIAN_CPPUILIST_BOX_LIST_BOX );
iListBox->ConstructFromResourceL( reader );
CleanupStack::PopAndDestroy(); // reader internal state
}
_LIT(KDefaultText, "SMS Crypto Symbian Cpp UI Example");
iListBox->View()->SetListEmptyTextL(KDefaultText);
// the listbox owns the items in the list and will free them
iListBox->Model()->SetOwnershipType( ELbmOwnsItemArray );
// setup the icon array so graphics-style boxes work
SetupListBoxIconsL();
iListBox->SetFocus( ETrue );
iFocusControl = iListBox;
}
/**
* Handle global resource changes, such as scalable UI or skin events (override)
*/
void CSMSCryptoSymbianCppUIListBox::HandleResourceChange( TInt aType )
{
CCoeControl::HandleResourceChange( aType );
SetRect( iAvkonViewAppUi->View( TUid::Uid( ESMSCryptoSymbianCppUIListBoxViewId ) )->ClientRect() );
}
/**
* Draw container contents.
*/
void CSMSCryptoSymbianCppUIListBox::Draw( const TRect& aRect ) const
{
CWindowGc& gc = SystemGc();
gc.Clear( aRect );
}
/**
* Add a list box item to a list.
*/
void CSMSCryptoSymbianCppUIListBox::AddListBoxItemL(
CEikTextListBox* aListBox,
const TDesC& aString)
{
CTextListBoxModel* model = aListBox->Model();
CDesCArray* itemArray = static_cast< CDesCArray* > ( model->ItemTextArray() );
itemArray->InsertL( 0, aString );
aListBox->HandleItemAdditionL();
}
/**
* Get the array of selected item indices, with respect to the list model.
* The array is sorted in ascending order.
* The array should be destroyed with two calls to CleanupStack::PopAndDestroy(),
* the first with no argument (referring to the internal resource) and the
* second with the array pointer.
* @return newly allocated array, which is left on the cleanup stack;
* or NULL for empty list.
*/
RArray< TInt >* CSMSCryptoSymbianCppUIListBox::GetSelectedListBoxItemsLC( CEikTextListBox* aListBox )
{
CAknFilteredTextListBoxModel* model =
static_cast< CAknFilteredTextListBoxModel *> ( aListBox->Model() );
if ( model->NumberOfItems() == 0 )
return NULL;
// get currently selected indices
const CListBoxView::CSelectionIndexArray* selectionIndexes =
aListBox->SelectionIndexes();
TInt selectedIndexesCount = selectionIndexes->Count();
if ( selectedIndexesCount == 0 )
return NULL;
// copy the indices and sort numerically
RArray<TInt>* orderedSelectedIndices =
new (ELeave) RArray< TInt >( selectedIndexesCount );
// push the allocated array
CleanupStack::PushL( orderedSelectedIndices );
// dispose the array resource
CleanupClosePushL( *orderedSelectedIndices );
// see if the search field is enabled
CAknListBoxFilterItems* filter = model->Filter();
if ( filter != NULL )
{
// when filtering enabled, translate indices back to underlying model
for ( TInt idx = 0; idx < selectedIndexesCount; idx++ )
{
TInt filteredItem = ( *selectionIndexes ) [ idx ];
TInt actualItem = filter->FilteredItemIndex ( filteredItem );
orderedSelectedIndices->InsertInOrder( actualItem );
}
}
else
{
// the selection indices refer directly to the model
for ( TInt idx = 0; idx < selectedIndexesCount; idx++ )
orderedSelectedIndices->InsertInOrder( ( *selectionIndexes ) [ idx ] );
}
return orderedSelectedIndices;
}
/**
* Delete the selected item or items from the list box.
*/
void CSMSCryptoSymbianCppUIListBox::DeleteSelectedListBoxItemsL( CEikTextListBox* aListBox )
{
CAknFilteredTextListBoxModel* model =
static_cast< CAknFilteredTextListBoxModel *> ( aListBox->Model() );
if ( model->NumberOfItems() == 0 )
return;
RArray< TInt >* orderedSelectedIndices = GetSelectedListBoxItemsLC( aListBox );
if ( !orderedSelectedIndices )
return;
// Delete selected items from bottom up so indices don't change on us
CDesCArray* itemArray = static_cast< CDesCArray* > ( model->ItemTextArray() );
TInt currentItem = 0;
for ( TInt idx = orderedSelectedIndices->Count(); idx-- > 0; )
{
currentItem = ( *orderedSelectedIndices )[ idx ];
itemArray->Delete ( currentItem );
}
// dispose the array resources
CleanupStack::PopAndDestroy();
// dispose the array pointer
CleanupStack::PopAndDestroy( orderedSelectedIndices );
// refresh listbox's cursor now that items are deleted
AknListBoxUtils::HandleItemRemovalAndPositionHighlightL(
aListBox, currentItem, ETrue );
}
/**
* Get the listbox.
*/
CAknSingleStyleListBox* CSMSCryptoSymbianCppUIListBox::ListBox()
{
return iListBox;
}
/**
* Create a list box item with the given column values.
*/
void CSMSCryptoSymbianCppUIListBox::CreateListBoxItemL( TDes& aBuffer,
const TDesC& aMainText )
{
_LIT ( KStringHeader, "\t%S" );
aBuffer.Format( KStringHeader(), &aMainText );
}
/**
* Add an item to the list by reading the text items from the array resource
* and setting a single image property (if available) from an index
* in the list box's icon array.
* @param aResourceId id of an ARRAY resource containing the textual
* items in the columns
*
*/
void CSMSCryptoSymbianCppUIListBox::AddListBoxResourceArrayItemL( TInt aResourceId )
{
CDesCArray* array = iCoeEnv->ReadDesCArrayResourceL( aResourceId );
CleanupStack::PushL( array );
// This is intended to be large enough, but if you get
// a USER 11 panic, consider reducing string sizes.
TBuf<512> listString;
CreateListBoxItemL( listString, ( *array ) [ 0 ] );
AddListBoxItemL( iListBox, listString );
CleanupStack::PopAndDestroy( array );
}
/**
* Set up the list's icon array.
*/
void CSMSCryptoSymbianCppUIListBox::SetupListBoxIconsL()
{
CArrayPtr< CGulIcon >* icons = NULL;
if ( icons != NULL )
{
iListBox->ItemDrawer()->ColumnData()->SetIconArray( icons );
}
}
/**
* Handle commands relating to markable lists.
*/
TBool CSMSCryptoSymbianCppUIListBox::HandleMarkableListCommandL( TInt /*aCommand*/ )
{
return EFalse;
}
/*
* Get crypted SMS messages from Messaging Inbox
* */
void CSMSCryptoSymbianCppUIListBox::GetFolderSMSMessageInformationL(CDesCArrayFlat* arrayAddr, CDesCArrayFlat* arrayMsgBody)
{
MDesCArray* textArray = iListBox->Model()->ItemTextArray();
CDesCArray* array = static_cast<CDesCArray*>(textArray);
array->Reset();
iListBox->Reset();
iSmsMtm->SwitchCurrentEntryL( KMsvGlobalInBoxIndexEntryId );
CMsvEntry& entry = iSmsMtm->Entry();
// Remember to delete this entry after no longer needed!
// Only intrested in messages. Filter out service etries.
CMsvEntrySelection* entries = entry.ChildrenWithMtmL(KUidMsgTypeSMS);
CleanupStack::PushL(entries);
TBuf<512> listString;
_LIT ( KStringHeader, "\tFrom: " );
_LIT ( KHeader, "#PyCr" );
for (TInt i = 0; i < entries->Count(); i++ )
{
// Append only SMS messages, .sis files etc. are disregarded.
// Take only beginning of SMS body, because listbox only shows
// beginning of message.
// Remember to load before using the SmsHeader
iSmsMtm->SwitchCurrentEntryL( (*entries)[i] );
iSmsMtm->LoadMessageL();
listString.Copy( iSmsMtm->Entry().Entry().iDescription );
if ( listString.Left(5) == KHeader )
{
listString.Copy( iSmsMtm->SmsHeader().FromAddress() );
listString.Insert( 0, KStringHeader );
AddListBoxItemL( iListBox, listString );
arrayMsgBody->InsertL ( 0, iSmsMtm->Entry().Entry().iDescription );
arrayAddr->AppendL ( iSmsMtm->SmsHeader().FromAddress() );
}
}
CleanupStack::PopAndDestroy(entries);
}
/*
* Wrapper function for loading SMS messages
*/
void CSMSCryptoSymbianCppUIListBox::GetSMSFromInboxL()
{
GetFolderSMSMessageInformationL(iArrayAddr, iArrayMsgBody);
}
/*
* Required by session
*/
void CSMSCryptoSymbianCppUIListBox::HandleSessionEventL(
TMsvSessionEvent /*aEvent*/,
TAny* /*aArg1*/,
TAny* /*aArg2*/,
TAny* /*aArg3*/)
{
}
/*
* Retrieve the selected crypted SMS message
*/
TBuf<bufSize> CSMSCryptoSymbianCppUIListBox::GetSMSL()
{
TInt currentItem = iListBox->CurrentItemIndex();
return iArrayMsgBody->MdcaPoint(currentItem);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -