?? mtmsengine.cpp
字號:
/*
* ============================================================================
* Name : CMtmsEngine from SMSExampleMtmsEngine.h
* Part of : SMSExample
* Created : 12.03.2005 by Forum Nokia
* Version : 1.0
* Copyright: Nokia Corporation
* ============================================================================
*/
// INCLUDE FILES
#include "MtmsEngine.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>
#include "common.h"
#include <aknnotewrappers.h>
#include <EveryDay.rsg>
const TInt KMessageAddressLength = 32;
const TInt KMessageBodySize = 256;
const TInt KListInfoLength = 512;
#define MEMORY_FREE_EX(a) if(a) {delete a; a = NULL;}
// ----------------------------------------------------------------------------
// CMtmsEngine::NewL(MSMSExampleMtmsEngineObserver& aObserver)
//
// Symbian OS 2 phase constructor.
// ----------------------------------------------------------------------------
CMtmsEngine* CMtmsEngine::NewL(MMtmsEngineObserver& aObserver)
{
CMtmsEngine* self = new (ELeave) CMtmsEngine(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
// ----------------------------------------------------------------------------
// CMtmsEngine::CMtmsEngine(MSMSExampleMtmsEngineObserver&
// aObserver)
//
// Symbian OS 2 phase constructor.
// ----------------------------------------------------------------------------
CMtmsEngine::CMtmsEngine(MMtmsEngineObserver& aObserver)
: CActive(0),
iObserver(aObserver),
iIdArray(NULL),
iSmsId(KMsvNullIndexEntryId)
{
}
// ----------------------------------------------------------------------------
// CMtmsEngine::ConstructL()
//
// Symbian OS 2nd phase constructor.
// ----------------------------------------------------------------------------
void CMtmsEngine::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);
}
// ----------------------------------------------------------------------------
// CMtmsEngine::CreateMtmClientL()
//
// Create a CSmsClientMtm after session has been opened.
// ----------------------------------------------------------------------------
void CMtmsEngine::CreateMtmClientL()
{
// Client-side MTM registry.
iClientMtmReg = CClientMtmRegistry::NewL(*iSession);
// Get the SMS Mtm client from the registry
iSmsMtm = static_cast<CSmsClientMtm*>(iClientMtmReg->NewMtmL(KUidMsgTypeSMS));
}
// ----------------------------------------------------------------------------
// CMtmsEngine::~CMtmsEngine()
//
// Destructor.
// ----------------------------------------------------------------------------
CMtmsEngine::~CMtmsEngine()
{
Cancel();
delete iMsvOper;
delete iEntrySelection;
delete iSmsMtm;
delete iClientMtmReg;
delete iSession;
if(iIdArray)
{
iIdArray->Reset();
delete iIdArray;
}
}
// ----------------------------------------------------------------------------
// CMtmsEngine::DoCancel()
//
// From CActive, cancel CMsvOperation
// ----------------------------------------------------------------------------
void CMtmsEngine::DoCancel()
{
if (iMsvOper)
{
iMsvOper->Cancel();
delete iMsvOper;
iMsvOper = NULL;
}
}
// ----------------------------------------------------------------------------
// CMtmsEngine::RunL()
//
// Sending SMS is asynchronous operation.
// ----------------------------------------------------------------------------
void CMtmsEngine::RunL()
{
iObserver.HandleMessageSentL(iStatus.Int());
}
// ----------------------------------------------------------------------------
// CMtmsEngine::CreateSMSMessageL(const TDesC& aAddress,
// const TDesC& aMessage)
//
// Create an SMS message
// ----------------------------------------------------------------------------
TMsvEntry CMtmsEngine::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);
//modify by tommy
CSmsHeader& header = iSmsMtm->SmsHeader();
CSmsSettings* sendOptions = CSmsSettings::NewL();
CleanupStack::PushL(sendOptions);
sendOptions->CopyL(iSmsMtm->ServiceSettings()); // restore existing settings
// set send options
sendOptions->SetDelivery(ESmsDeliveryImmediately); // set to be delivered immediately
// here we modified the character set
sendOptions->SetCharacterSet(TSmsDataCodingScheme::ESmsAlphabetUCS2) ;
// end
header.SetSmsSettingsL(*sendOptions);
CleanupStack::PopAndDestroy(sendOptions);
// 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();
return indexEntry;
}
// ----------------------------------------------------------------------------
// CMtmsEngine::ValidateCreatedSMS()
//
// Validate message. This should be done before sending the SMS.
// ----------------------------------------------------------------------------
TBool CMtmsEngine::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;
}
}
// ----------------------------------------------------------------------------
// CMtmsEngine::SendSMSL()
//
// Before sending sms message should be created CreateSMSMessageL() and
// Validated ValidateCreatedSMS().
// ----------------------------------------------------------------------------
void CMtmsEngine::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
indexEntry.SetInPreparation(EFalse);
// Sets the sending state
indexEntry.SetSendingState(KMsvSendStateWaiting);
iSmsMtm->Entry().ChangeL(indexEntry);
// Time to send the message
Cancel(); // prepare iMsvOper for use
iEntrySelection->Reset();
iEntrySelection->AppendL(iSmsId);
TBuf8<1> dummyParam;
// There is also InvokeSyncFunctionL which is synchronous.
iMsvOper = iSmsMtm->InvokeAsyncFunctionL(ESmsMtmCommandScheduleCopy, *iEntrySelection, dummyParam, iStatus);
SetActive();
}
// ----------------------------------------------------------------------------
// CMtmsEngine::MoveToFolderL( TMsvId aMessageId, TMsvId aFolder )
//
// Move message to folder. Notice that the iSmsMtm points to the parent, not
// to the message itself. If you move messages from inbox to drafts, those
// messages cannot be edited because their complete flag is true.
// ----------------------------------------------------------------------------
void CMtmsEngine::MoveToFolderL(TInt aIndex, TMsvId aFolder)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -