📄 smsexampleappui.cpp
字号:
/*
* ============================================================================
* Name : CSMSExampleAppUi from SMSExampleAppui.h
* Part of : SMSExample
* Created : 08.02.2005 by Forum Nokia
* Implementation notes:
* Initial content was generated by Series 60 Application Wizard.
* Version : 1.0
* Copyright: Nokia Corporation
* ============================================================================
*/
// INCLUDE FILES
#include "SMSExampleAppui.h"
#include <SMSExample.rsg>
#include "SMSExample.hrh"
#include "SMSExampleMtmsEngine.h"
#include "SMSExampleLogView.h"
#include "SMSExampleListboxView.h"
#include <avkon.hrh>
#include <AknQueryDialog.h>
#include <msvids.h> // Folder Ids
#include <txtrich.h> // CRichText
#include <smut.h>
#ifdef __WINS__
const TMsvId KObservedFolderId = KMsvDraftEntryId;
#else
const TMsvId KObservedFolderId = KMsvGlobalInBoxIndexEntryId;;
#endif
const TMsvId KInbox = KMsvGlobalInBoxIndexEntryId;
const TMsvId KOutbox = KMsvGlobalOutBoxIndexEntryId;
const TMsvId KDrafts = KMsvDraftEntryId;
_LIT(KCouldNotSendSMS, "Message unsent");
_LIT(KInvalidMessageResend, "The message was invalid. Please resend.");
_LIT(KSentMessageToAddress, "Sent message to address: ");
_LIT(KNoBodyText, "No body text");
_LIT(KAutomaticDeletedMesssageText, "Deleted arrived message!");
_LIT(KAutomaticDeleteActivatedText, "Automatic delete activated");
_LIT(KAutomaticDeleteDeActivatedText, "Automatic delete deactivated");
// ------------------------------------------------------------------------------
// CSMSExampleAppUi::ConstructL()
//
// Symbian OS 2nd phase constructor.
// ------------------------------------------------------------------------------
void CSMSExampleAppUi::ConstructL()
{
BaseConstructL();
iAutomaticDelete = EFalse;
iEngine = CSMSExampleMtmsEngine::NewL(*this);
iLogView = CLogView::NewL();
iListboxView = CListboxView::NewL(iEngine, iLogView);
AddViewL(iLogView); // Transfer ownership to base class
AddViewL(iListboxView); // Transfer ownership to base class
SetDefaultViewL(*iLogView); // Start with log view.
// SMS automatic receiving needs a session to the messaging server
iMsvSession = CMsvSession::OpenAsyncL(*this);
// Message body parser
iParser = CSMSExampleParser::NewL();
}
// ------------------------------------------------------------------------------
// CSMSExampleAppUi::~CSMSExampleAppUi()
// Destructor
// Frees reserved resources
// ------------------------------------------------------------------------------
CSMSExampleAppUi::~CSMSExampleAppUi()
{
delete iMsvEntry;
iMsvEntry = 0;
delete iMsvSession;
iMsvSession = 0;
delete iParser;
iParser = 0;
delete iEngine;
iEngine = 0;
}
// ------------------------------------------------------------------------------
// CSMSExampleAppUi::DynInitMenuPaneL(TInt aResourceId,CEikMenuPane* aMenuPane)
// This function is called by the EIKON framework just before it displays
// a menu pane. Its default implementation is empty, and by overriding it,
// the application can set the state of menu items dynamically according
// to the state of application data.
// ------------------------------------------------------------------------------
void CSMSExampleAppUi::DynInitMenuPaneL(
TInt /*aResourceId*/,CEikMenuPane* /*aMenuPane*/)
{
}
// ------------------------------------------------------------------------------
// CSMSExampleAppUi::HandleKeyEventL(
// const TKeyEvent& aKeyEvent,TEventCode /*aType*/)
// takes care of key event handling
// ------------------------------------------------------------------------------
TKeyResponse CSMSExampleAppUi::HandleKeyEventL(
const TKeyEvent& /*aKeyEvent*/,TEventCode /*aType*/)
{
return EKeyWasNotConsumed;
}
// ------------------------------------------------------------------------------
// CSMSExampleAppUi::GetAddressL()
//
// Query message recipient from the user.
// ------------------------------------------------------------------------------
TInt CSMSExampleAppUi::GetAddressL()
{
CAknTextQueryDialog* dlg = new (ELeave) CAknTextQueryDialog(iAddress,
CAknQueryDialog::ENoTone);
return dlg->ExecuteLD(R_MTMS_EXAMPLE_ADDRESS_QUERY);
}
// ------------------------------------------------------------------------------
// CSMSExampleAppUi::QueryMessageL()
//
// Query message body from the user.
// ------------------------------------------------------------------------------
TInt CSMSExampleAppUi::QueryMessageL()
{
CAknTextQueryDialog* dlg = new (ELeave) CAknTextQueryDialog(iMessage,
CAknQueryDialog::ENoTone);
return dlg->ExecuteLD(R_MTMS_EXAMPLE_MESSAGE_QUERY);
}
// ------------------------------------------------------------------------------
// CSMSExampleAppUi::HandleCommandL(TInt aCommand)
// Takes care of command handling.
// ------------------------------------------------------------------------------
void CSMSExampleAppUi::HandleCommandL(TInt aCommand)
{
switch ( aCommand )
{
case EAknSoftkeyExit:
case EEikCmdExit:
{
Exit();
break;
}
// Send a new message
case ESMSExampleCmdSendMessage:
{
// Ask recipient from the user
GetAddressL();
// Query message body
QueryMessageL();
iLogView->LogEventBeginningL();
// Create a new SMS to drafts
iEngine->CreateSMSMessageL( iAddress, iMessage );
// Validate it before sending
if ( iEngine->ValidateCreatedSMS() )
{
// Now send
iEngine->SendSMSL();
}
else
{
iLogView->DrawTextL( KInvalidMessageResend );
}
break;
}
// DELETE MESSAGE COMMANDS. Target folder is not needed.
case ESMSExampleCmdDeleteMessageFromDrafts:
{
QueryMessagesAndExecute( ESMSExampleDelete, KDrafts, 0 );
break;
}
case ESMSExampleCmdDeleteMessageFromOutbox:
{
QueryMessagesAndExecute( ESMSExampleDelete, KOutbox, 0 );
break;
}
case ESMSExampleCmdDeleteMessageFromInbox:
{
QueryMessagesAndExecute(ESMSExampleDelete, KInbox, 0 );
break;
}
//COPY MESSAGES
case ESMSExampleCmdCopyMessageFromDraftsToInbox:
{
QueryMessagesAndExecute( ESMSExampleModeCopy, KDrafts, KInbox );
break;
}
case ESMSExampleCmdCopyMessageFromDraftsToOutbox:
{
QueryMessagesAndExecute( ESMSExampleModeCopy, KDrafts, KOutbox );
break;
}
case ESMSExampleCmdCopyMessageFromInboxToOutbox:
{
QueryMessagesAndExecute( ESMSExampleModeCopy, KInbox, KOutbox );
break;
}
case ESMSExampleCmdCopyMessageFromInboxToDrafts:
{
QueryMessagesAndExecute( ESMSExampleModeCopy, KInbox, KDrafts );
break;
}
case ESMSExampleCmdCopyMessageFromOutboxToInbox:
{
QueryMessagesAndExecute( ESMSExampleModeCopy, KOutbox, KInbox );
break;
}
case ESMSExampleCmdCopyMessageFromOutboxToDrafts:
{
QueryMessagesAndExecute( ESMSExampleModeCopy, KOutbox, KDrafts );
break;
}
//MOVE FROM FOLDER TO NEW FOLDER
case ESMSExampleCmdMoveMessageFromInboxToDrafts:
{
QueryMessagesAndExecute( ESMSExampleMove, KInbox, KDrafts );
break;
}
case ESMSExampleCmdMoveMessageFromInboxToOutbox:
{
QueryMessagesAndExecute( ESMSExampleMove, KInbox, KOutbox );
break;
}
case ESMSExampleCmdMoveMessageFromDraftsToInbox:
{
QueryMessagesAndExecute( ESMSExampleMove, KDrafts, KInbox );
break;
}
case ESMSExampleCmdMoveMessageFromDraftsToOutbox:
{
QueryMessagesAndExecute( ESMSExampleMove, KDrafts, KOutbox );
break;
}
case ESMSExampleCmdMoveMessageFromOutboxToInbox:
{
QueryMessagesAndExecute( ESMSExampleMove, KOutbox, KInbox );
break;
}
case ESMSExampleCmdMoveMessageFromOutboxToDrafts:
{
QueryMessagesAndExecute( ESMSExampleMove, KOutbox, KDrafts );
break;
}
// SETTINGS
case ESMSExampleListBoxSetAutomaticDeleteOn:
{
CAknQueryDialog* confirmation = CAknQueryDialog::NewL( CAknQueryDialog::EWarningTone );
if ( confirmation->ExecuteLD( R_AUTOMATIC_DELETE_CONFIRMATION_QUERY_DIALOG ) )
{
iLogView->LogEventBeginningL();
iLogView->DrawTextL( KAutomaticDeleteActivatedText );
iAutomaticDelete = ETrue;
}
break;
}
case ESMSExampleListBoxSetAutomaticDeleteOff:
{
iLogView->LogEventBeginningL();
iLogView->DrawTextL( KAutomaticDeleteDeActivatedText );
iAutomaticDelete = EFalse;
break;
}
default:
break;
}
}
// ------------------------------------------------------------------------------
// CSMSExampleAppUi::QueryMessagesAndExecute(TInt aMode,
// TMsvId aOriginFolderID,
// TMsvId aDestinationFolderID)
//
// Set ListBox mode and set folders. Operation is executed when the
// ActivateLocalView activates ListBox.
// ------------------------------------------------------------------------------
void CSMSExampleAppUi::QueryMessagesAndExecute(TInt aMode, TMsvId aOriginFolderID,
TMsvId aDestinationFolderID)
{
iListboxView->SetListBoxMode( aMode );
iListboxView->SetFolderID( aOriginFolderID );
iListboxView->SetTargetFolderID( aDestinationFolderID );
// Change to another view, function takes id of the view as a parameter
// (ActivateLocalViewL ( TUid aViewId ) ). enum TMultiViewsViewNumber in SMSExample.hrh
// defines these ids.
ActivateLocalViewL(TUid::Uid(EListboxViewId));
}
// ------------------------------------------------------------------------------
// CSMSExampleAppUi::HandleMessageSentL(TInt aError)
//
// Observes SMS Engine.
// ------------------------------------------------------------------------------
void CSMSExampleAppUi::HandleMessageSentL(TInt aError)
{
if (aError == KErrNone)
{
TBuf<KSmsMessageLength> messageText;
messageText.Append( KSentMessageToAddress );
messageText.Append( iAddress );
iLogView->DrawTextL( messageText );
}
else // If there was some error sending the SMS
{
iLogView->DrawTextL( KCouldNotSendSMS );
}
}
// ------------------------------------------------------------------------------
// CSMSExampleAppUi::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1,
// TAny* aArg2, TAny* /*aArg3*/)
//
// Handle session events. Observe received messages.
// ------------------------------------------------------------------------------
void CSMSExampleAppUi::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* /*aArg3*/)
{
switch (aEvent)
{
case EMsvServerReady:
// Initialise iMsvEntry
if (!iMsvEntry)
{
iMsvEntry = CMsvEntry::NewL(*iMsvSession, KInbox, TMsvSelectionOrdering());
}
break;
case EMsvEntriesCreated:
// Only look for changes in the Inbox
if (*(static_cast<TMsvId*>(aArg2)) == KObservedFolderId)
{
CMsvEntrySelection* entries = static_cast<CMsvEntrySelection*>(aArg1);
iNewMessageId = entries->At(0);
}
break;
case EMsvEntriesChanged:
// Look for changes. When using the emulator observed folder is drafts, otherwise inbox.
if (*(static_cast<TMsvId*>(aArg2)) == KObservedFolderId)
{
CMsvEntrySelection* entries = static_cast<CMsvEntrySelection*>(aArg1);
if (iNewMessageId == entries->At(0))
{
// Set entry context to the new message
iMsvEntry->SetEntryL(iNewMessageId);
// Check the type of the arrived message and that the message is complete.
// only SMS are our consern
if ( iMsvEntry->Entry().iMtm != KUidMsgTypeSMS || !iMsvEntry->Entry().Complete() ) {
return;
}
// Read-only store.
CMsvStore* store = iMsvEntry->ReadStoreL();
CleanupStack::PushL(store);
// Get address of received message.
iLogView->LogEventBeginningL();
TBuf<KSmsMessageLength> address ( iMsvEntry->Entry().iDetails );
iLogView->DrawTextL( address );
if (store->HasBodyTextL())
{
CRichText* richText = CRichText::NewL(
iEikonEnv->SystemParaFormatLayerL(),
iEikonEnv->SystemCharFormatLayerL());
CleanupStack::PushL(richText);
store->RestoreBodyTextL(*richText);
const TInt length = richText->DocumentLength();
TBuf<KSmsMessageLength> number;
TPtrC ptr = richText->Read(0, length);
// Try to find a number that has 10 or more digits from message body.
// Underline found number.
if ( iParser->ParseMessage( ptr, number ))
{
// Search number position in the message body
TInt pos = richText->Read(0, length).Find( number );
iLogView->DrawTextWithoutCarriageL( richText->Read(0, pos) );
iLogView->DrawUnderlinedTextL( richText->Read(pos , number.Length()) );
iLogView->DrawTextWithoutCarriageL( richText->Read(pos + number.Length() , length));
}
else
{
iLogView->DrawTextL( richText->Read(0, length) );
}
CleanupStack::PopAndDestroy(richText);
}
else
{
iLogView->DrawTextL( KNoBodyText );
}
CleanupStack::PopAndDestroy(store);
// Automatic delete setting
if ( iAutomaticDelete )
{
// Ncnlist seems to panic if there is no time to play arrived
// message tone before deletion.
User::After(1000*3000);
iLogView->LogEventBeginningL();
iLogView->DrawTextL( KAutomaticDeletedMesssageText );
iMsvSession->RemoveEntry(iNewMessageId);
}
}
}
break;
default:
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -