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

📄 mtmsexampleengine.cpp

📁 symbian s60 第3版下的短信发送程序,实现了(发送,接收等功能)
💻 CPP
字号:
/*
============================================================================
 Name        : MtmsExampleEngine.cpp
 Author      : Lion
 Copyright   : Your copyright notice
 Description : Application Engine implementation
============================================================================
*/
// INCLUDE FILES

// Class include
#include "MtmsExampleEngine.h"

// System includes
#include <aknutils.h>       // CompleteWithAppPath()
#include <f32file.h>        // TParsePtrC
#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


// String constants
_LIT(KSMSBody, "Example SMS body text");
_LIT(KMMSSubject, "Penguins");
_LIT(KMMSText, "Look! Some penguins!");
_LIT(KMMSTextFilename, "hello.txt");


// Filenames
_LIT(KMMSImageFilename, "image.jpg");
_LIT(KMMSSmilFilename,  "simple.smil");


// ================= MEMBER FUNCTIONS =======================


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

CMtmsExampleEngine::CMtmsExampleEngine(MMtmsExampleEngineObserver& aObserver)
: CActive(0),
  iObserver(aObserver),
  iReady(EFalse),
  iSmsId(KMsvNullIndexEntryId),
  iMmsId(KMsvNullIndexEntryId)
    {
    }

/**
* Symbian OS 2nd phase constructor.
*/  
void CMtmsExampleEngine::ConstructL()
    {
    CActiveScheduler::Add(this);

    // Create iEntrySelection
    iEntrySelection = new (ELeave) CMsvEntrySelection;

    // Create CMsvSession
    iSession = CMsvSession::OpenAsyncL(*this);
    // New session is opened asynchronously
    // CompleteConstructL() is called when async call is finished.
    }


void CMtmsExampleEngine::CompleteConstructL()
    {
    // Construct the client MTM registry
    iMtmReg = CClientMtmRegistry::NewL(*iSession);

    // Obtain MTMs from the MTM registry
    iSmsMtm = static_cast<CSmsClientMtm*>(iMtmReg->NewMtmL(KUidMsgTypeSMS));
    iMmsMtm = static_cast<CMmsClientMtm*>(iMtmReg->NewMtmL(KUidMsgTypeMultimedia));

    iReady = ETrue;
    }

/**
* Destructor.
*/  
CMtmsExampleEngine::~CMtmsExampleEngine()
    {
    Cancel();
    delete iOp;
    delete iEntrySelection;
    delete iSmsMtm;
    delete iMmsMtm;
    delete iMtmReg;
    delete iSession;
    }


void CMtmsExampleEngine::DoCancel()
    {
    if (iOp)
        {
        iOp->Cancel();
        delete iOp;
        iOp = NULL;
        }
    }


void CMtmsExampleEngine::RunL()
    {
    iObserver.HandleMessageSentL(iStatus.Int());
    }


//
// SMS Functions
//

void CMtmsExampleEngine::CreateDraftSMSL(const TDesC& aAddress)
    {
    // Set attributes on index entry
    TMsvEntry indexEntry;
    indexEntry.SetInPreparation(ETrue);
    indexEntry.iMtm = KUidMsgTypeSMS;
    indexEntry.iType = KUidMsvMessageEntry;
    indexEntry.iServiceId = iSmsMtm->ServiceId();
    indexEntry.iDate.HomeTime();

    // Create entry from this index entry
    iSmsMtm->SwitchCurrentEntryL(KMsvDraftEntryId);
    iSmsMtm->Entry().CreateL(indexEntry);

    // Set the MTM's active context to the new message
    iSmsId = indexEntry.Id();
    iSmsMtm->SwitchCurrentEntryL(iSmsId);

    // Add body
    CRichText& body = iSmsMtm->Body();
    body.Reset();
    body.InsertL(0, KSMSBody);
    indexEntry.iDescription.Set(KSMSBody);

    // Add addressee
    iSmsMtm->AddAddresseeL(aAddress);
    indexEntry.iDetails.Set(aAddress);

    // Update index entry
    iSmsMtm->Entry().ChangeL(indexEntry);

    // Update store entry
    iSmsMtm->SaveMessageL();
    }


TBool CMtmsExampleEngine::ValidateSMS()
    {
    TMsvPartList msgCheckParts =
        KMsvMessagePartBody |
        KMsvMessagePartRecipient |
        KMsvMessagePartOriginator |
        KMsvMessagePartDate;
    TMsvPartList msgFailParts = iSmsMtm->ValidateMessage(msgCheckParts);
    return msgFailParts == KMsvMessagePartNone;
    }


void CMtmsExampleEngine::SendSMSL()
    {
    // Set context to the SMS message
    iSmsMtm->SwitchCurrentEntryL(iSmsId);

    // Load the message
    iSmsMtm->LoadMessageL();

    // Set the SMS service centre address
    CSmsSettings& settings = iSmsMtm->ServiceSettings();
    
   
    const TInt numSCAddresses = settings.ServiceCenterCount();
    if (numSCAddresses > 0)
		{
		CSmsServiceCenter* serviceCentreNumber = NULL;
        // Get the default SC address, if valid, Otherwise just get the first from the list.
        if ((settings.DefaultServiceCenter() >= 0)  &&  (settings.DefaultServiceCenter() < numSCAddresses))
            serviceCentreNumber = &(settings.GetServiceCenter(settings.DefaultServiceCenter()));
        else
            serviceCentreNumber = &(settings.GetServiceCenter(0));
		
        iSmsMtm->SmsHeader().SetServiceCenterAddressL(serviceCentreNumber->Address());
        
        }
    else
    	{
		 // report panic, programming error - there should never be a missing service number,
		}

    // Save the message
    iSmsMtm->SaveMessageL();


    // Update the index entry
    TMsvEntry indexEntry = iSmsMtm->Entry().Entry();
    indexEntry.SetInPreparation(EFalse);
    indexEntry.SetSendingState(KMsvSendStateWaiting);
    iSmsMtm->Entry().ChangeL(indexEntry);


    // Now send
    Cancel(); // prepare iOp for use
	iEntrySelection->Reset();
	iEntrySelection->AppendL(iSmsId);
    TBuf8<1> dummyParams;
	iOp = iSmsMtm->InvokeAsyncFunctionL(ESmsMtmCommandScheduleCopy, *iEntrySelection, dummyParams, iStatus);
    SetActive();
    }


//
// MMS Functions
//

void CMtmsExampleEngine::CreateDraftMMSL(const TDesC& aAddress)
    {
    // Set the context of the MMS MTM to Drafts
    iMmsMtm->SwitchCurrentEntryL(KMsvDraftEntryId);

    // Create a new message using the default service,
    // and store its ID for later reference
    iMmsMtm->CreateMessageL(iMmsMtm->DefaultServiceL());
    iMmsId = iMmsMtm->Entry().EntryId();
    
    // Add any phone number and email addressees
    iMmsMtm->AddAddresseeL(aAddress);
    
    // Add attachments (also known as "media objects")
    AddMMSAttachmentsL();
    
    // Set subject
    iMmsMtm->SetSubjectL(KMMSSubject);
    
    // Finally, save the message to commit changes to the store
    iMmsMtm->SaveMessageL();
    }


void CMtmsExampleEngine::AddMMSAttachmentsL()
    {
    TMsvId attachmentId = KMsvNullIndexEntryId;

    // Add a JPEG attachment
    TFileName attachmentName(KMMSImageFilename);
    User::LeaveIfError(CompleteWithAppPath(attachmentName));
  //  iMmsMtm->CreateAttachment2L(attachmentId, attachmentName);

    // Add the text attachment
//    iMmsMtm->CreateTextAttachmentL(attachmentId, KMMSText, KMMSTextFilename);

    // Add the SMIL part
    attachmentName = KMMSSmilFilename;
    User::LeaveIfError(CompleteWithAppPath(attachmentName));
 //   iMmsMtm->CreateAttachment2L(attachmentId, attachmentName);
    iMmsMtm->SetMessageRootL(attachmentId);
    }


TBool CMtmsExampleEngine::ValidateMMS()
    {
    TMsvPartList msgCheckParts =
        KMsvMessagePartBody | KMsvMessagePartRecipient |
        KMsvMessagePartOriginator | KMsvMessagePartDate |
        KMsvMessagePartAttachments | KMsvMessagePartDescription;
    TMsvPartList msgFailParts = iMmsMtm->ValidateMessage(msgCheckParts);

    return msgFailParts == KMsvMessagePartNone;
    }


void CMtmsExampleEngine::SendMMSL()
    {
    iMmsMtm->SwitchCurrentEntryL(iMmsId);

    // Mark the message as complete
    TMsvEntry indexEntry = iMmsMtm->Entry().Entry();
    indexEntry.SetInPreparation(EFalse);
    indexEntry.SetVisible(ETrue);

    // Commit changes to index entry
    iMmsMtm->Entry().ChangeL(indexEntry);

    // Now send
    Cancel(); // prepare iOp for use
	iOp = iMmsMtm->SendL(iStatus);
    SetActive();
    }


void CMtmsExampleEngine::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* /*aArg1*/, TAny* /*aArg2*/, TAny* /*aArg3*/)
    {
    switch (aEvent)
        {
        // This event tells us that the session has been opened
        case EMsvServerReady:
            CompleteConstructL();
            break;

        default:
            // do nothing
            break;
        }
    }


// End of File  

⌨️ 快捷键说明

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