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

📄 nqmtmsengine.cpp

📁 本例是SYMBIAN C++一个创建短信的例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 * CNqMtmsEngine class
 * Contact Engine for NetQin
 * Date: 2008.06.11
 * Author: Tomken
 */

#include "NqMtmsEngine.h"

#include <mmsclient.h>      // CMmsClientMtm
#include <mmsconst.h>       // KMmsIso10646Ucs2 etc.
#include <mtclreg.h>        // CClientMtmRegistry 
#include <mtmdef.h>         // KMsvMessagePartBody etc.
#include <smsclnt.h>        // CSmsClientMtm
#include <smscmds.h>        // ESmsMtmCommandScheduleCopy
#include <smuthdr.h>        // CSmsHeader
#include <smutset.h>        // CSmsSettings
#include <txtrich.h>        // CRichText

#include <coeutils.h>

#include <eikdef.h>
#include <eikenv.h>

CNqMtmsEngine* CNqMtmsEngine::NewL(MNqMtmsEngineObserver& aObserver)
{
	CNqMtmsEngine* self = new (ELeave) CNqMtmsEngine(aObserver);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(self);
	return self;
}

CNqMtmsEngine::CNqMtmsEngine(MNqMtmsEngineObserver& aObserver) : CActive(0), iObserver(aObserver)
{
	iReady = EFalse;
}

CNqMtmsEngine::~CNqMtmsEngine()
{
	Cancel();
	
	delete iOperation;;
	delete iSmsMtm;
	delete iMtmReg;
	delete iSession;
	delete iSendTimer;
	
#ifdef  EKA2
	iTz.Close();
#endif
}

void CNqMtmsEngine::ConstructL()
{
	CActiveScheduler::Add(this);
	
#ifdef  EKA2
	iTz.Connect();
#endif
	
	iState = EWaitingForNothing;
	iSendTimer = CNQTimer::NewL(0, *this, 0);
	TRAPD(err, (iSession = CMsvSession::OpenAsyncL(*this)));
	if(err != KErrNone)
	{
		User::Leave(err);
	}
}

void CNqMtmsEngine::CompleteConstructL()
{
    iMtmReg = CClientMtmRegistry::NewL(*iSession);
    iSmsMtm = static_cast<CSmsClientMtm*>(iMtmReg->NewMtmL(KUidMsgTypeSMS));

    iReady = ETrue;
	iObserver.HandleMtmsEngineInitialized();
}

TBool CNqMtmsEngine::SendSMSL(const TDesC& aNumber, const TDesC& aAliasName, const TDesC& aBody)
{
	// Current entry is the Draft folder.
    iSmsMtm->SwitchCurrentEntryL( KMsvDraftEntryId );

    // Create a new SMS message entry as a child of the current context.
    iSmsMtm->CreateMessageL( KUidMsgTypeSMS.iUid );

    CMsvEntry& serverEntry = iSmsMtm->Entry();
    TMsvEntry entry( serverEntry.Entry() );

    CRichText& body = iSmsMtm->Body();   // the body of the message
    body.Reset();
    // Insert the message text gotten as input from user.
    body.InsertL( 0, aBody );

    // Message will be sent immediately.
    entry.SetSendingState( KMsvSendStateWaiting );
    
#ifdef EKA2
    entry.iDate.UniversalTime(); // insert current time
#else
    entry.iDate.HomeTime(); // insert current time
#endif

    // Set the SMS message settings for the message.
    CSmsHeader& header = iSmsMtm->SmsHeader();
    CSmsSettings* settings = CSmsSettings::NewL();
    CleanupStack::PushL( settings );

    settings->CopyL( iSmsMtm->ServiceSettings() );    // restore settings
    settings->SetDelivery( ESmsDeliveryImmediately ); // to be delivered immediately
	settings->SetCharacterSet(TSmsDataCodingScheme::ESmsAlphabetUCS2) ; 
    header.SetSmsSettingsL( *settings );              // new settings

#ifndef __WINS__
	// Let's check if there is a service center address.
	if ( header.Message().ServiceCenterAddress().Length() == 0 )
	{
		// No, there isn't. We assume there is at least one service center
		// number set and use the default service center number.
		CSmsSettings* serviceSettings = &( iSmsMtm->ServiceSettings() );

		// Check if number of service center addresses in the list is null.
#ifndef EKA2
		if (!serviceSettings->NumSCAddresses())
#else
		if (!serviceSettings->ServiceCenterCount())
#endif
		{
			// Remember to pop settings even if this branch is taken.
			CleanupStack::PopAndDestroy( settings );
			return EFalse;     // quit creating the message
		}

		else
		{
			// Set service center address to default.
			// The caller does not take ownership of the returned object.
#ifndef EKA2
			CSmsNumber* sc = 0;
			sc = &(serviceSettings->SCAddress(serviceSettings->DefaultSC()));
			header.Message().SetServiceCenterAddressL(sc->Address());
#else
			CSmsServiceCenter &sc=serviceSettings->GetServiceCenter(serviceSettings->DefaultServiceCenter());
			header.Message().SetServiceCenterAddressL(sc.Address());
#endif
		}
	}
#endif // __WINS__

	CleanupStack::PopAndDestroy( settings );

    // Recipient number is displayed also as the recipient alias.
    entry.iDetails.Set( aAliasName );
    // Add addressee.
    iSmsMtm->AddAddresseeL( aNumber, entry.iDetails );

    // Validate message.
    if ( !ValidateL() )
	{
		return EFalse;
	}

    entry.SetVisible( ETrue );          // set message as visible
    entry.SetInPreparation( EFalse );   // set together with the visibility flag
    serverEntry.ChangeL( entry );       // commit changes
    iSmsMtm->SaveMessageL();            // save message

	iSendQueue.Append(entry.Id());

	if(iState == EWaitingForNothing)
		iSendTimer->After(500);

    return ETrue;
}

TBool CNqMtmsEngine::ValidateL()
{
	// Empty part list to hold the result.
	TMsvPartList result( KMsvMessagePartNone );

	// Validate message body.
	result = iSmsMtm->ValidateMessage( KMsvMessagePartBody );

	if ( result != KMsvMessagePartNone )
	{
		return EFalse;
	}

	// Validate recipient.
	result = iSmsMtm->ValidateMessage( KMsvMessagePartRecipient );

	if ( result != KMsvMessagePartNone )
	{
		return EFalse;
	}

	return ETrue;
}

void CNqMtmsEngine::ScheduleL()
{
	CMsvEntrySelection* selection = new ( ELeave ) CMsvEntrySelection;
	CleanupStack::PushL( selection );
	selection->AppendL( iSmsMtm->Entry().EntryId() ); // add message to selection
#ifdef EKA2
	CMsvEntry* entry = iSession->GetEntryL(KMsvGlobalOutBoxIndexEntryId);
	TRAPD(err, iOperation = entry->CopyL(iSmsMtm->Entry().EntryId(), iSmsMtm->ServiceId(), iStatus) );
#else
	// Add entry to task scheduler.
	TBuf8<1> dummyParams;   // dummy parameters needed for InvokeAsyncFunctionL
	TRAPD(err, iOperation = iSmsMtm->InvokeAsyncFunctionL( ESmsMtmCommandScheduleCopy, *selection, dummyParams, iStatus ));
#endif
	CleanupStack::PopAndDestroy( selection );

	if(err == KErrNone)
	{
		iState = EWaitingForScheduling;
		SetActive();
	}
	else
	{
		iState = EWaitingForNothing;
		iSendTimer->After(500);
	}
}

void CNqMtmsEngine::DoCancel()
{
	if ( iOperation )
	{
		iOperation->Cancel();
	}
}

void CNqMtmsEngine::RunL()
{
	User::LeaveIfError( iStatus != KErrNone );

	// Determine the current operations progress.
    // ProgressL returns an 8 bit descriptor.
	TBufC8<KMsvProgressBufferLength> progress( iOperation->ProgressL() );
    _LIT8( KCompare, "KErrNone" );
    User::LeaveIfError( !progress.Compare( KCompare ) );

	// The pointer to the current CMsvOperation object is no longer needed.
    delete iOperation;
    iOperation = NULL;

	switch ( iState )
	{
		case EWaitingForMoving:
			{
				ScheduleL();
			}
            break;
		case EWaitingForScheduling:
			{
				TMsvEntry entry( iSmsMtm->Entry().Entry() );
				TInt state( entry.SendingState() );

				if ( state == KMsvSendStateWaiting || state == KMsvSendStateScheduled)
				{
				}

				iState = EWaitingForNothing;
				iSendTimer->After(500);
			}
            break;
		default:
			break;
	}
}

void CNqMtmsEngine::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* aArg3)
{
    switch (aEvent)
    {
        case EMsvServerReady:
            CompleteConstructL();
            break;
		case EMsvEntriesMoved:
		case EMsvEntriesChanged:
		case EMsvEntriesCreated:
		case EMsvEntriesDeleted:
		default:
			iObserver.HandleMtmsEngineSessionEventL(aEvent, aArg1, aArg2, aArg3);
			break;
    }
}

void CNqMtmsEngine::GetFolderMessageInfo(TMsvId aFolderID, CMsvEntrySelection*& aArray)
{
	iSmsMtm->SwitchCurrentEntryL( aFolderID );
	CMsvEntry& entry = iSmsMtm->Entry();

	aArray = entry.ChildrenWithMtmL(KUidMsgTypeSMS);
}

void CNqMtmsEngine::GetMessageTime(TMsvId aId, TTime& aTime)
{
	TRAPD(err, iSmsMtm->SwitchCurrentEntryL( aId ));
	if(err == KErrNone)
	{
		TMsvEntry * iEntry = (TMsvEntry *)&(iSmsMtm->Entry().Entry());
		aTime = iEntry->iDate;
		
#ifdef EKA2
		iTz.ConvertToLocalTime(aTime);
#endif
	}
}

void CNqMtmsEngine::GetMessageInfo(TMsvId aId, TTime& aTime, TBool& aUnRead, TDes& aAddress, TDes& aBody)
{
	TRAPD(err, iSmsMtm->SwitchCurrentEntryL( aId ));

	if(err == KErrNone)
	{
		TMsvEntry * iEntry = (TMsvEntry *)&(iSmsMtm->Entry().Entry());

		aUnRead = iEntry->Unread();
		aTime = iEntry->iDate;
		
#ifdef EKA2
		iTz.ConvertToLocalTime(aTime);
#endif

		GetMessageFromAddressL(aId, aAddress);
		GetMessageTextL(aId, aBody);
	}
}

⌨️ 快捷键说明

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