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

📄 smsengine.cpp

📁 手机短信开发的一个案例
💻 CPP
字号:
/*
* ========================================================================
*  Name     : SmsEngine.cpp
*  Part of  : Sms Modifier
*  Created  : 20.05.2006 by Artem Marchenko
*  Description:
*  Version  : 1.0
*  Copyright: Artem Marchenko 2006
* ========================================================================
*/

// INCLUDE FILES
#include "SmsEngine.h"

#include <smsclnt.h>        // CSmsClientMtm
#include <smuthdr.h>        // CSmsHeader
#include <txtrich.h>        // CRichText
#include <mtuireg.h>		// CMtmUiRegistry
#include <flogger.h>

// ----------------------------------------------------------------------------
// CSmsEngine::NewL(MSMSEngineObserver& aObserver)
//
// Symbian OS 2 phase constructor.
// ----------------------------------------------------------------------------
CSmsEngine* CSmsEngine::NewL()
    {
    CSmsEngine* self = new (ELeave) CSmsEngine();
    CleanupStack::PushL( self );
    self->ConstructL();
    CleanupStack::Pop( self );
    return self;
    }

// ----------------------------------------------------------------------------
// CSmsEngine::ConstructL()
//
// Symbian OS 2nd phase constructor.
// ----------------------------------------------------------------------------
void CSmsEngine::ConstructL()
    {
	// Connect to the Messaging server
	// Upon successful connection HandleSessionEventL (EMsvServerReady) 
	// will be called
    iSession = CMsvSession::OpenSyncL( *this );	
    iClientMtmReg = CClientMtmRegistry::NewL(*iSession);
    // Get the SMS Mtm client from the registry
    iSmsMtm = static_cast<CSmsClientMtm*>( 
        iClientMtmReg->NewMtmL( KUidMsgTypeSMS ) );
    }

// ----------------------------------------------------------------------------
// CSmsEngine::~CSmsEngine()
//
// Destructor.
// ----------------------------------------------------------------------------
CSmsEngine::~CSmsEngine()
    {
    delete iSmsMtm;
    delete iClientMtmReg;
    delete iSession;	
    }

// ----------------------------------------------------------------------------
// CSmsEngine::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* 
// Handle the Nessaging-related events
// ----------------------------------------------------------------------------
void CSmsEngine::HandleSessionEventL( TMsvSessionEvent aEvent, TAny* aArg1, 
                                      TAny* aArg2, TAny* /*aArg3*/ )
    {
	CMsvEntrySelection* entries ( NULL );
	TMsvId* folderId ( NULL );
    TMsvEntry entry;
    switch (aEvent)
        {
		case EMsvEntriesCreated:
			// One or more entries have been created
            folderId = static_cast<TMsvId*>( aArg2 );
            // We are interested only in the Inbox messages
            if ( *folderId != KMsvGlobalInBoxIndexEntryId )
                {
                break;
                }

            entries = static_cast<CMsvEntrySelection*>( aArg1 );
            // In this simple example for the sake of simplicity
            // we process only the first entry.
            // In real-world app, you'll have to iterate over the entries
            TMsvId folder;
            User::LeaveIfError( iSession->GetEntry( entries->At( 0 ), folder, entry ) );

            // We are interested only in the sms messages
            if ( entry.iMtm != KUidMsgTypeSMS )
                {
                break;
                }

            if( entry.Id() == iLastChangedMessageId )
                {
                // We just created this message ourselves
                break;
                }

            CloneMessageL( entry );

            // Delete the original message
            iSmsMtm->SwitchCurrentEntryL( entry.Parent() );
            iSmsMtm->Entry().DeleteL( entry.Id() );            
			break;
        default:
            // Ignore all the other messages
            break;
        }
    }


void CSmsEngine::CloneMessageL( TMsvEntry& aEntry )
    {
    iSmsMtm->SwitchCurrentEntryL( aEntry.Id() );
    CMsvEntry& cEntry( iSmsMtm->Entry() );
    TMsvEntry tEntry = cEntry.Entry();
    
    // tEntry caches the CMsvEntry data, but descriptor fields are special-
    // tEntry contains only pointers to the actual text. To preserve it, we need
    // to save it ourselves
    HBufC* details = HBufC::NewLC( tEntry.iDetails.Length() );
    *details = tEntry.iDetails;
    HBufC* description = HBufC::NewLC( tEntry.iDescription.Length() );
    *description = tEntry.iDescription;
    
    // Store the original message text
    CParaFormatLayer* pf = CParaFormatLayer::NewL();
	CleanupStack::PushL( pf );
	CCharFormatLayer* cf = CCharFormatLayer::NewL();
	CleanupStack::PushL( cf );
	CRichText* richText = CRichText::NewL( pf, cf,
	                                       CEditableText::EFlatStorage );
    CleanupStack::PopAndDestroy( cf );
    CleanupStack::PopAndDestroy( pf );
    CleanupStack::PushL( richText );
	CMsvStore* store = cEntry.ReadStoreL();
	CleanupStack::PushL( store );
		store->RestoreBodyTextL( *richText );
	CleanupStack::PopAndDestroy( store );
    // Read the original message content into the richText

    // Demonstrate how the message can be modified. 
    // Append some text to the original content
    richText->InsertL( richText->DocumentLength(), _L( ".Changed" ) );	

    // Create new message
    iSmsMtm->SwitchCurrentEntryL( aEntry.Parent() ); 
    iSmsMtm->CreateMessageL( KUidMsgTypeSMS.iUid );

    // Set the index entry details to be equal to the original message details
    // In the real world application, you might need to copy more details
    TMsvEntry entry = iSmsMtm->Entry().Entry();
    iLastChangedMessageId = entry.Id();
    entry.SetInPreparation( tEntry.InPreparation() );
    entry.SetVisible( tEntry.Visible() );
    entry.iDate = tEntry.iDate;
    // In real app, you might have to regenerate description if the old
    // description doesn't match the changed text
    entry.iDescription.Set( *description );
    entry.iDetails.Set( *details );
    entry.SetUnread( tEntry.Unread() );    
    iSmsMtm->Entry().ChangeL( entry );
    iSmsMtm->SaveMessageL();   

    // Now save the original message text
    CMsvStore* messageStore = iSmsMtm->Entry().EditStoreL();
    CleanupStack::PushL( messageStore );

    // To make programmatically created message display "From", not "To"
    // in the Messaging application, it is needed to store the ESmsDeliver PDU
    // into the message header
    CSmsHeader* header = CSmsHeader::NewL( CSmsPDU::ESmsDeliver, *richText );
    CleanupStack::PushL( header );
    header->StoreL( *messageStore );
    CleanupStack::PopAndDestroy( header );
    messageStore->StoreBodyTextL( *richText );
	messageStore->CommitL();
    
    CleanupStack::PopAndDestroy( messageStore );
    CleanupStack::PopAndDestroy( richText );
    CleanupStack::PopAndDestroy( description );
    CleanupStack::PopAndDestroy( details );
    }

//////////////////////////////////////////////////////////////////////////
// void CSmsEngine::CreateLocalMessageL(const TDesC &aAddress, 
//                       const TDesC &aDescription, const TDesC &aMessage)
//
// Create a new sms message in folder
//////////////////////////////////////////////////////////////////////////
void CSmsEngine::CreateLocalMessageL( TMsvId aFolderId, const TDesC &aAddress, 
                             const TDesC &aDescription, const TDesC &aMsgText )
	{
    // Switch to the given folder
    iSmsMtm->SwitchCurrentEntryL( aFolderId ); 
    iSmsMtm->CreateMessageL( KUidMsgTypeSMS.iUid );

    CMsvStore* aMessageStore = iSmsMtm->Entry().EditStoreL();
    CleanupStack::PushL( aMessageStore );
    CleanupStack::PopAndDestroy( aMessageStore );

    CRichText& body = iSmsMtm->Body();    
    body.Reset();
    body.InsertL( 0, aMsgText );

    TMsvEntry entry = iSmsMtm->Entry().Entry();
    entry.SetInPreparation( EFalse );
    entry.SetVisible( ETrue );
    entry.iDate.HomeTime();
    entry.iDescription.Set( aDescription );
    entry.iDetails.Set( aAddress );
    entry.SetUnread( ETrue );

    iSmsMtm->Entry().ChangeL(entry);
    iSmsMtm->SaveMessageL();   
	}

// End of file

⌨️ 快捷键说明

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