📄 smsexamplemtmsengine.cpp
字号:
/*
* ============================================================================
* Name : CSMSExampleMtmsEngine from SMSExampleMtmsEngine.h
* Part of : SMSExample
* Created : 12.03.2005 by Forum Nokia
* Version : 1.0
* Copyright: Nokia Corporation
* ============================================================================
*/
// INCLUDE FILES
#include "SMSExampleMtmsEngine.h"
// SYSTEM FILES
#include <f32file.h> // TParsePtrC
#include <mmsclient.h> // CMmsClientMtm
#include <mtclreg.h> // CClientMtmRegistry
#include <mtmdef.h> // KMsvMessagePartBody
#include <smsclnt.h> // CSmsClientMtm
#include <smscmds.h> // ESmsMtmCommandScheduleCopy
#include <smuthdr.h> // CSmsHeader
#include <smutset.h> // CSmsSettings
#include <txtrich.h> // CRichText
#include <eikenv.h>
const TInt KMessageAddressLength = 100;
const TInt KMessageBodySize = 512;
// ----------------------------------------------------------------------------
// CSMSExampleMtmsEngine::NewL(MSMSExampleMtmsEngineObserver& aObserver)
//
// Symbian OS 2 phase constructor.
// ----------------------------------------------------------------------------
CSMSExampleMtmsEngine* CSMSExampleMtmsEngine::NewL(MSMSExampleMtmsEngineObserver& aObserver)
{
CSMSExampleMtmsEngine* self = new (ELeave) CSMSExampleMtmsEngine(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
// ----------------------------------------------------------------------------
// CSMSExampleMtmsEngine::CSMSExampleMtmsEngine(MSMSExampleMtmsEngineObserver&
// aObserver)
//
// Symbian OS 2 phase constructor.
// ----------------------------------------------------------------------------
CSMSExampleMtmsEngine::CSMSExampleMtmsEngine(MSMSExampleMtmsEngineObserver& aObserver)
: CActive(0),
iObserver(aObserver),
iSmsId(KMsvNullIndexEntryId)
{
}
// ----------------------------------------------------------------------------
// CSMSExampleMtmsEngine::ConstructL()
//
// Symbian OS 2nd phase constructor.
// ----------------------------------------------------------------------------
void CSMSExampleMtmsEngine::ConstructL()
{
CActiveScheduler::Add(this);
// iEntrySelection encapsulates an array of entry IDs
iEntrySelection = new (ELeave) CMsvEntrySelection;
// Represents a channel of communication between a client thread (Client-side MTM, User
// Interface MTM, or message client application) and the Message Server thread.
// Session is opened asynchorously. CreateMtmClientL() is called afterwards.
// Another possibility is use OpenSyncL which is synchronous.
iSession = CMsvSession::OpenAsyncL(*this);
}
// ----------------------------------------------------------------------------
// CSMSExampleMtmsEngine::CreateMtmClientL()
//
// Create a CSmsClientMtm after session has been opened.
// ----------------------------------------------------------------------------
void CSMSExampleMtmsEngine::CreateMtmClientL()
{
// Client-side MTM registry.
iClientMtmReg = CClientMtmRegistry::NewL(*iSession);
// Get the SMS Mtm client from the registry
iSmsMtm = static_cast<CSmsClientMtm*>(iClientMtmReg->NewMtmL(KUidMsgTypeSMS));
}
// ----------------------------------------------------------------------------
// CSMSExampleMtmsEngine::~CSMSExampleMtmsEngine()
//
// Destructor.
// ----------------------------------------------------------------------------
CSMSExampleMtmsEngine::~CSMSExampleMtmsEngine()
{
Cancel();
delete iMsvOper;
delete iEntrySelection;
delete iSmsMtm;
delete iClientMtmReg;
delete iSession;
}
// ----------------------------------------------------------------------------
// CSMSExampleMtmsEngine::DoCancel()
//
// From CActive, cancel CMsvOperation
// ----------------------------------------------------------------------------
void CSMSExampleMtmsEngine::DoCancel()
{
if (iMsvOper)
{
iMsvOper->Cancel();
delete iMsvOper;
iMsvOper = NULL;
}
}
// ----------------------------------------------------------------------------
// CSMSExampleMtmsEngine::RunL()
//
// Sending SMS is asynchronous operation.
// ----------------------------------------------------------------------------
void CSMSExampleMtmsEngine::RunL()
{
iObserver.HandleMessageSentL(iStatus.Int());
}
// ----------------------------------------------------------------------------
// CSMSExampleMtmsEngine::CreateSMSMessageL(const TDesC& aAddress,
// const TDesC& aMessage)
//
// Create an SMS message
// ----------------------------------------------------------------------------
void CSMSExampleMtmsEngine::CreateSMSMessageL(const TDesC& aAddress, const TDesC& aMessage)
{
// Set SMS parameters
TMsvEntry indexEntry;
indexEntry.iDate.HomeTime();
indexEntry.SetInPreparation(ETrue);
// This is an SMS message
indexEntry.iMtm = KUidMsgTypeSMS;
indexEntry.iType = KUidMsvMessageEntry;
//Gets the ID of the current SMS service.
indexEntry.iServiceId = iSmsMtm->ServiceId();
// Create entry to drafts
iSmsMtm->SwitchCurrentEntryL(KMsvDraftEntryId);
// Creates a new child entry owned by the context synchronously.
iSmsMtm->Entry().CreateL(indexEntry);
// Set the MTM's active context to the new message
iSmsId = indexEntry.Id();
iSmsMtm->SwitchCurrentEntryL(iSmsId);
// Add message body. Body is set twice because index entry keeps a copy
// of some summary information. Index entry and full stored entry
// must be in sync.
CRichText& body = iSmsMtm->Body();
body.Reset();
body.InsertL(0, aMessage);
indexEntry.iDescription.Set(aMessage);
// Add destination address (recipient). Copy address also to the index entry
iSmsMtm->AddAddresseeL(aAddress);
indexEntry.iDetails.Set(aAddress);
// Commit changes because index entry is only a local variable
iSmsMtm->Entry().ChangeL(indexEntry);
// Save full message data to the store
iSmsMtm->SaveMessageL();
}
// ----------------------------------------------------------------------------
// CSMSExampleMtmsEngine::ValidateCreatedSMS()
//
// Validate message. This should be done before sending the SMS.
// ----------------------------------------------------------------------------
TBool CSMSExampleMtmsEngine::ValidateCreatedSMS()
{
TMsvPartList partsToBeChecked = KMsvMessagePartBody | KMsvMessagePartRecipient |
KMsvMessagePartOriginator | KMsvMessagePartDate;
// ValidateMessage return KErrNone if message is valid.
TMsvPartList failedParts = iSmsMtm->ValidateMessage(partsToBeChecked);
if (failedParts == KMsvMessagePartNone)
{
return ETrue;
}
else
{
return EFalse;
}
}
// ----------------------------------------------------------------------------
// CSMSExampleMtmsEngine::SendSMSL()
//
// Before sending sms message should be created CreateSMSMessageL() and
// Validated ValidateCreatedSMS().
// ----------------------------------------------------------------------------
void CSMSExampleMtmsEngine::SendSMSL()
{
// Changes the entry on which later actions are performed to the entry with the
// specified TMsvId.
iSmsMtm->SwitchCurrentEntryL(iSmsId);
// Load the created message
iSmsMtm->LoadMessageL();
// Gets the current SMS service settings
CSmsSettings& serviceSettings = iSmsMtm->ServiceSettings();
// Gets the number of service centre addresses stored in this object.
const TInt numSCAddresses = serviceSettings.NumSCAddresses();
// There should always be a service center number
if (numSCAddresses > 0)
{
CSmsNumber* serviceCentreNumber = NULL;
// get the service center number
if ((serviceSettings.DefaultSC() >= 0) && (serviceSettings.DefaultSC() < numSCAddresses))
serviceCentreNumber = &(serviceSettings.SCAddress(serviceSettings.DefaultSC()));
else
serviceCentreNumber = &(serviceSettings.SCAddress(0));
iSmsMtm->SmsHeader().SetServiceCenterAddressL(serviceCentreNumber->Address());
}
else
{
// Leave if there is no service center number
User::Leave(0);
}
iSmsMtm->SaveMessageL();
// Index entry must be Updated
TMsvEntry indexEntry = iSmsMtm->Entry().Entry();
// Set in-preparation flag
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -