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

📄 mmsengine.cpp

📁 Symbian c++ 的彩信发送接收实例
💻 CPP
字号:
/*
============================================================================
 Name        : CMmsEngine.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CMmsEngine implementation
============================================================================
*/

#include <msvids.h>
#include <mtclbase.h>
#include <pathinfo.h>
#include <coemain.h>
#include <CMsvMimeHeaders.h>
#include <mmsclient.h>
#include <mtclreg.h>
#include <avkon.hrh>
#include <psvariables.h>
#include <eikenv.h>

#include "MmsEngine.h"
#include "MsvObserver.h"

#define KDirPictures1		PathInfo::ImagesPath()
#define KPhoneRootPath1		PathInfo::PhoneMemoryRootPath()
_LIT( KFileName1,			"mmsexample.jpg" ); 	//Attachment file name

CMmsEngine* CMmsEngine::NewL( MMsvObserver& aObserver )
	{
	CMmsEngine* self = CMmsEngine::NewLC( aObserver );
	CleanupStack::Pop( self );
	return self;
	}

CMmsEngine* CMmsEngine::NewLC( MMsvObserver& aObserver )
	{
	CMmsEngine* self = new (ELeave) CMmsEngine( aObserver );
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
	}

CMmsEngine::CMmsEngine( MMsvObserver& aObserver ) : 
	CMsvHandler( aObserver ), iPhase( EIdle )
	{
	}

void CMmsEngine::ConstructL()
	{
    CMsvHandler::ConstructL();
	}

CMmsEngine::~CMmsEngine()
	{
	}

void CMmsEngine::RunL()
	{
	}

void CMmsEngine::HandleSessionEventL( TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* /*aArg3*/)
    {
    switch (aEvent)
        {
		case EMsvEntriesChanged:
			{
			break;
			}

        // This event tells us that the session has been opened
        case EMsvServerReady:
			{
            CompleteConstructL();  // Construct the MTM registry & MMS MTM
			iMtm = static_cast<CMmsClientMtm*>(iMtmRegistry->NewMtmL( KUidMsgTypeMultimedia ));
            break;
			}
			
		case EMsvEntriesCreated:
			{
			TMsvId* entryId = static_cast<TMsvId*>(aArg2);  // entry id from the session event

			//Listening when new message is arrived in inbox
            if ( aArg2 &&  ( *entryId == KMsvGlobalInBoxIndexEntryId ) )
                {
                CMsvEntrySelection* entries = static_cast<CMsvEntrySelection*>(aArg1);
                if( entries->Count() >= 1 )
                    {
                    }
                else
                    {
                    }
                }
			else if ( aArg2 && ( *entryId == KMsvSentEntryId ) )
				{
				}

			break;
			}

		case EMsvCloseSession:
			iSession->CloseMessageServer();
			break;

		case EMsvServerTerminated:
			iSession->CloseMessageServer();
			break;

		case EMsvGeneralError:
		case EMsvServerFailedToStart:
			{
			iObserver.HandleError( MMsvObserver::EFatalServerError );
			break;
			}

		default:
            break;
        }
    }

TBool CMmsEngine::IsIdle()
    {
    if ( CMsvHandler::IsIdle() && ( iPhase == EIdle ) )
        {
        return ETrue;
        }
    else
		{
        return EFalse;
        }
    }

void CMmsEngine::SendToL(const TDesC& aRecipient, const TDesC& aText )
    {
	iRecipientsTelNum = aRecipient;
	iText = aText;

	if ( CreateNewMessageL() )
		{
		SendMessageL();
		}
    }

TBool CMmsEngine::CreateNewMessageL()
	{	
	// - CMsvEntry accesses and acts upon a particular Message Server entry.
	// - NewL() does not create a new entry, but simply a new object to access an existing entry.
	// - It takes in as parameters the client's message server session,
	//   ID of the entry to access and initial sorting order of the children of the entry. 
	CMsvEntry* entry = CMsvEntry::NewL( *iSession, KMsvGlobalOutBoxIndexEntryId ,
				TMsvSelectionOrdering() );

	CleanupStack::PushL( entry );

    // Set context to the parent folder (Outbox)
    iMtm->SwitchCurrentEntryL( entry->EntryId() );
	static_cast<CMmsClientMtm*>(iMtm)->SetDeliveryReport( EMmsYes );
    iMtm->CreateMessageL( iMtm->DefaultServiceL()  );
    
	CleanupStack::PopAndDestroy(); // entry
    
    // Setting recipients
    // use this to add the "To" recipients.
    iMtm->AddAddresseeL( iRecipientsTelNum );
    
    //Setting message subject
    _LIT( KMessageSubject, "MMS Example" );
    iMtm->SetSubjectL( KMessageSubject );
    
    // Message consists of one image
    TFileName attachmentFile( KPhoneRootPath1 );
    attachmentFile.Append( KDirPictures1 );
    TFileName fileName( KFileName1 );
    attachmentFile.Append( KFileName1 );
    
    TMsvEntry ent = iMtm->Entry().Entry();
    // Set InPreparation to false
    ent.SetInPreparation(EFalse);
    ent.SetVisible( ETrue );		 // mark as visible, after this the message can be seen in Outbox and, 
									 // after sending, in Sent folder.
    iMtm->Entry().ChangeL( ent );    // Commit changes

	//Save the changes
	iMtm->SaveMessageL();
        
	// Opening store
	CMsvStore* store = iMtm->Entry().EditStoreL();
	CleanupStack::PushL( store );

	RFile attachment;
	//open attachment file
	TInt error = attachment.Open( CCoeEnv::Static()->FsSession(), attachmentFile, 
								  EFileShareReadersOnly | EFileRead );
	CleanupClosePushL( attachment );
    	
	//Check that the attachment file exist.
	if( error != KErrNone )
		{
    	// _LIT(KFileNotFound, "Attachment file not found!");
    	// EchoL(KFileNotFound);
		// EchoL(attachmentFile);
    		
    	attachment.Close();
    	CleanupStack::PopAndDestroy(); // attachment
    	CleanupStack::PopAndDestroy(); // store
    	return EFalse;
    	}
	else
    	{
    	// Mime header
    	CMsvMimeHeaders* mimeHeaders = CMsvMimeHeaders::NewL();
    	CleanupStack::PushL( mimeHeaders );   
    	mimeHeaders->SetSuggestedFilenameL( fileName );

       	// Represents a single attachment and information about the attachment
		CMsvAttachment* attaInfo = 
    	CMsvAttachment::NewL( CMsvAttachment::EMsvFile );
    	CleanupStack::PushL( attaInfo );

		//Mime Type
    	_LIT8( KMimeType, "image/jpeg" );
    	TBufC8<10> mimeType(KMimeType);	

        TMsvAttachmentId attachId = KMsvNullIndexEntryId;
		
		//Attachment file must be an public folder (e.g. c:\Data\images)
        static_cast<CMmsClientMtm*>(iMtm)->CreateAttachment2L( *store,
                                  attachment,
                                  mimeType,
                                  *mimeHeaders,
                                  attaInfo,
                                  attachId );

        CleanupStack::Pop( attaInfo ); // attaInfo
    	CleanupStack::PopAndDestroy( mimeHeaders ); // mimeHeaders
    		
		// _LIT(KCreatingComplete, "Message created.");
		// EchoL(KCreatingComplete);
    		
    	store->CommitL();
    	attachment.Close();
    	
    	CleanupStack::PopAndDestroy(); // attachment
    	CleanupStack::PopAndDestroy(); // store
    	}
		    
    return ETrue;
	}

TBool CMmsEngine::SendMessageL()
    {
	// Start sending the message via the Server MTM to the MMS server
    CMsvOperationWait* wait = CMsvOperationWait::NewLC();
    wait->iStatus = KRequestPending;
    CMsvOperation* op = NULL;
   	op = static_cast<CMmsClientMtm*>(iMtm)->SendL( wait->iStatus );
    wait->Start();
    CleanupStack::PushL( op );
    CActiveScheduler::Start();

    // The following is to ignore the completion of other active objects. It is not
    // needed if the app has a command absorbing control.
    while( wait->iStatus == KRequestPending )
        {
        CActiveScheduler::Start();
        }

    CleanupStack::PopAndDestroy( 2 ); // op, wait

    return ETrue;
    }

⌨️ 快捷键说明

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