Symbian发短信(二)

/*
 * VideoEggSmsEngine.h
 *
 *  Created on: 2009-12-17
 *      Author: Administrator
 */

#ifndef VIDEOEGGSMSENGINE_H_
#define VIDEOEGGSMSENGINE_H_

#include <e32base.h>
#include <badesca.h>
#include <MSVAPI.H>
#include <eikenv.h>
#include <cmessagedata.h>
#include <sendui.h>
#include <SendUiConsts.h>
#include <gsmupdu.h>
#include <e32def.h>

#include "VideoEggLab.h"

class CMsvSession;
class CMsvEntry;
class CMsvEntrySelection;
class CClientMtmRegistry;
class CSmsClientMtm;

class CVideoEggSmsEngine:public CActive, public MMsvSessionObserver
 {
 class CReceiver
  {
  public:
  CReceiver(const TDesC& aAddress,TInt aTryTime);
  ~CReceiver();
  HBufC* GetAddress() const;
  TInt GetTryTime() const;
  TBool GetSendState() const;
  void DecreaseTryTime();
  void SetSendSuc();
  private:
   HBufC* iAddress;
   TInt iTryTime;
   TBool iSendState;
  };
public:
 static CVideoEggSmsEngine* NewL();
 virtual ~CVideoEggSmsEngine();
 
 //from MMsvSessionObserver
public:
 virtual void HandleSessionEventL(TMsvSessionEvent aEvent,TAny* aArg1,TAny* aArg2,TAny* aArg3);
 
public:
 void SetMsgContent(const TDesC& aContent);
 void SetReceiver(const TDesC& aAddress);
 
 void Start();
 
private:
 CVideoEggSmsEngine();
 void ConstructL();
 
 void CreateMsg();
 TBool ValidateSMS();
 void SendMsg();
 
 void StepNext(TTimeIntervalMicroSeconds32 aTimer);
 void Continue();
 
 //from CActive
 virtual void RunL();
 virtual void DoCancel();
 virtual TInt RunError(TInt aError);
 
private:
 enum TSmsEngineState
  {
  ESendCreateState,
  ESendSendState
  };
 
 CMsvSession* iSession;
 CClientMtmRegistry* iMtmReg;
 CSmsClientMtm* iSmsMtm;
 
 CMsvOperation* iOper;
 CMsvEntrySelection* iEntrySelection;
 CMsvEntry* iMsvEntry;
 
 TMsvId iSmsId;
 
 TBuf<EMsgConLen> iMsgContent;
 
 TInt iSendIndex;
 TSmsEngineState iState;
 RArray<CReceiver*> iRecAry;
 RTimer iTimer;
 };

#endif /* VIDEOEGGSMSENGINE_H_ */

 

 

 

/*
 * VideoEggSmsEngine.cpp
 *
 *  Created on: 2009-12-17
 *      Author: Administrator
 */

#include "VideoEggSmsEngine.h"

#include <smutset.h>
#include <BADESCA.H>
#include <MSVSTD.H>
#include <LOGCLI.H>
#include <LOGVIEW.H>
#include <SMSCLNT.H>
#include <MTCLREG.H>
#include <Msvids.h>
#include <tpbkcontactitemfield.h>
#include <cpbkcontactitem.h>
#include <Smut.h>
#include <smuthdr.h>
#include <mtmdef.h>
#include <txtrich.h>
#include <e32std.h>

CVideoEggSmsEngine::CReceiver::CReceiver(const TDesC& aAddress,TInt aTryTime)
 {
 iAddress=aAddress.Alloc();
 iTryTime=aTryTime;
 iSendState=EFalse;
 }

CVideoEggSmsEngine::CReceiver::~CReceiver()
 {
 if(iAddress)
  {delete iAddress;iAddress=NULL;}
 }

HBufC* CVideoEggSmsEngine::CReceiver::GetAddress() const
 {
 return iAddress;
 }

TInt CVideoEggSmsEngine::CReceiver::GetTryTime() const
    {
    return iTryTime;
    }

TBool CVideoEggSmsEngine::CReceiver::GetSendState() const
    {
    return iSendState;
    }

void CVideoEggSmsEngine::CReceiver::DecreaseTryTime()
 {
 iTryTime--;
 }

void CVideoEggSmsEngine::CReceiver::SetSendSuc()
 {
 iSendState=ETrue;
 }

 

CVideoEggSmsEngine* CVideoEggSmsEngine::NewL()
 {
 CVideoEggSmsEngine* self=new(ELeave)CVideoEggSmsEngine;
 
 CleanupStack::PushL(self);
 self->ConstructL();
 CleanupStack::Pop(self);
 
 return self;
 }

CVideoEggSmsEngine::CVideoEggSmsEngine():CActive(EPriorityStandard)
 {
 }

CVideoEggSmsEngine::~CVideoEggSmsEngine()
 {
 Cancel();
 iTimer.Close();
 
 if(iOper)
  {delete iOper;iOper=NULL;}
 if(iEntrySelection)
  {delete iEntrySelection;iEntrySelection=NULL;}
 if(iMsvEntry)
  {delete iMsvEntry;iMsvEntry=NULL;}
 if(iSmsMtm)
  {delete iSmsMtm;iSmsMtm=NULL;}
 if(iMtmReg)
  {delete iMtmReg;iMtmReg=NULL;}
 if(iSession)
  {delete iSession;iSession=NULL;}
 for(TInt i=0;i<iRecAry.Count();i++)
  {
  if(iRecAry[i])
   {delete iRecAry[i];iRecAry[i]=NULL;}
  }
 iRecAry.Close();
 }

void CVideoEggSmsEngine::ConstructL()
 {
 iSession=CMsvSession::OpenSyncL(*this);
 iMtmReg=CClientMtmRegistry::NewL(*iSession);
 iSmsMtm=static_cast<CSmsClientMtm*>(iMtmReg->NewMtmL(KUidMsgTypeSMS));
 
 TMsvSelectionOrdering sort;
 iMsvEntry = CMsvEntry::NewL(*iSession,KMsvGlobalInBoxIndexEntryId,sort);

 iEntrySelection = iMsvEntry->ChildrenL();
 iEntrySelection = new (ELeave) CMsvEntrySelection;
 
 iTimer.CreateLocal();
 
 CActiveScheduler::Add(this);
 }

void CVideoEggSmsEngine::Start()
 {
 Cancel();
 
 iState=ESendCreateState;
 iSendIndex=0;
 StepNext(1000);
 //CreateMsg();
 //if(ValidateSMS())
  //SendMsg();
 //else
  //CEikonEnv::InfoWinL(_L("some error "),_L("about message!"));
 }

void CVideoEggSmsEngine::CreateMsg()
 {
 TMsvEntry indexEntry;
 indexEntry.iDate.UniversalTime();
 indexEntry.SetInPreparation(ETrue);
 
 indexEntry.iMtm=KUidMsgTypeSMS;
 indexEntry.iType=KUidMsvMessageEntry;
 
 indexEntry.iServiceId=iSmsMtm->ServiceId();
 
 iSmsMtm->SwitchCurrentEntryL(KMsvDraftEntryId);
 iSmsMtm->Entry().CreateL(indexEntry);
 
 iSmsId=indexEntry.Id();
 iSmsMtm->SwitchCurrentEntryL(iSmsId);
 
 CRichText& body=iSmsMtm->Body();
 body.Reset();
 body.InsertL(0,iMsgContent);
 indexEntry.iDescription.Set(iMsgContent);
 
 iSmsMtm->AddAddresseeL(*(iRecAry[iSendIndex]->GetAddress()));
 indexEntry.iDetails.Set(*(iRecAry[iSendIndex]->GetAddress()));
 
 iSmsMtm->Entry().ChangeL(indexEntry);
 iSmsMtm->SaveMessageL();
 }

TBool CVideoEggSmsEngine::ValidateSMS()
 {
 TMsvPartList msgCheckParts=
     KMsvMessagePartBody|
     KMsvMessagePartRecipient|
     KMsvMessagePartOriginator|
     KMsvMessagePartDate;
 TMsvPartList msgFailParts=iSmsMtm->ValidateMessage(msgCheckParts);
 
 if(msgFailParts==KMsvMessagePartNone)
  return ETrue;
 else
  return EFalse;
 }

void CVideoEggSmsEngine::SendMsg()
 {
 iSmsMtm->SwitchCurrentEntryL(iSmsId);
 iSmsMtm->LoadMessageL();
 
 CSmsSettings& settings=iSmsMtm->ServiceSettings();
 TInt numSCAddresses=settings.ServiceCenterCount();
 
 if(numSCAddresses>0)
  {
  TInt index=0;
  index=settings.DefaultServiceCenter();
  
  if((index<0)||(index>=numSCAddresses))
   index=0;
  
  TPtrC serverCenterNum=settings.GetServiceCenter(index).Address();
  
  iSmsMtm->SmsHeader().SetServiceCenterAddressL(serverCenterNum);
  }
 
 CSmsSettings* sendOptions=CSmsSettings::NewL();
 CleanupStack::PushL(sendOptions);
 sendOptions->CopyL(iSmsMtm->ServiceSettings());
 sendOptions->SetDelivery(ESmsDeliveryImmediately);
 sendOptions->SetCharacterSet(TSmsDataCodingScheme::ESmsAlphabetUCS2);
 iSmsMtm->SmsHeader().SetSmsSettingsL(*sendOptions);
 CleanupStack::PopAndDestroy(sendOptions);
 
 iSmsMtm->SaveMessageL();
 
 TMsvEntry indexEntry=iSmsMtm->Entry().Entry();
 indexEntry.SetInPreparation(EFalse);
 indexEntry.SetSendingState(KMsvSendStateWaiting);
 iSmsMtm->Entry().ChangeL(indexEntry);
 
 Cancel();
 iEntrySelection->Reset();
 iEntrySelection->AppendL(iSmsId);
 
 iMsvEntry=iSession->GetEntryL(KMsvDraftEntryId);

 iOper=iMsvEntry->CopyL(iSmsMtm->Entry().EntryId(),iSmsMtm->ServiceId(),iStatus);
 SetActive();
 }

 

void CVideoEggSmsEngine::RunL()
 {
 switch(iState)
  {
  case ESendCreateState:
   {
   int count=iRecAry.Count();
   TInt i=0;
   
   for(i=0;i<count;i++)
    {
    iSendIndex%=count;
    CReceiver* receiver=iRecAry[iSendIndex];
    if(!receiver->GetSendState()&&receiver->GetTryTime()>0)
     break;
    else
     iSendIndex++;
    }
   
   if(i==count)
    {
    CEikonEnv::InfoWinL(_L("send "),_L("success!"));
    return;
    }
   
   if(iStatus.Int()!=KErrNone)
    {
    Continue();
    return;
    }
   
   iRecAry[iSendIndex]->DecreaseTryTime();
   
   CreateMsg();
   if(ValidateSMS())
    {
    iState=ESendSendState;
    SendMsg();
    }
   else
    {
    Continue();
    return;
    }
   }
   break;
   
   
  case ESendSendState:
   {
   if(iOper)
    {delete iOper;iOper=NULL;}
   if(iMsvEntry)
    {delete iMsvEntry;iMsvEntry=NULL;}
   
   if(iStatus.Int()==KErrNone)
    iRecAry[iSendIndex]->SetSendSuc();
   
   iState=ESendCreateState;
   Continue();
   }
   break;
  default:break;
  }
 }
void CVideoEggSmsEngine::DoCancel()
 {
 }
TInt CVideoEggSmsEngine::RunError(TInt aError)
 {
 if(aError==ESendCreateState)
  Continue();
 return KErrNone;
 }


void CVideoEggSmsEngine::SetMsgContent(const TDesC& aContent)
 {
 iMsgContent.Zero();
 iMsgContent.Append(aContent);
 }
void CVideoEggSmsEngine::SetReceiver(const TDesC& aAddress)
 {
 TBuf<EPartNumSegLen> segBuf(KPartNumSeg);
 
 TBuf<ETOMoblieLen> numBuffer(aAddress);
 TInt pos=numBuffer.Find(segBuf);
 
 CReceiver* receiver=NULL;
 while(pos!=KErrNotFound)
  {
  receiver=new(ELeave)CReceiver(numBuffer.Mid(0,ENumLen),1);
     iRecAry.Append(receiver);
     receiver=NULL;
    
     numBuffer.Delete(0,ENumLen+EPartNumSegLen);
     pos=numBuffer.Find(segBuf);
  }
 
 receiver=new(ELeave)CReceiver(numBuffer.Mid(0,ENumLen),1);
 iRecAry.Append(receiver);
 receiver=NULL;
 }

 

void CVideoEggSmsEngine::HandleSessionEventL(TMsvSessionEvent aEvent,TAny* aArg1,TAny* aArg2,TAny* aArg3)
 {}

void CVideoEggSmsEngine::StepNext(TTimeIntervalMicroSeconds32 aTimer)
 {
 
 iTimer.After(iStatus,aTimer);
 SetActive();
 }
void CVideoEggSmsEngine::Continue()
 {
 iSendIndex++;
 StepNext(1000);
 }

 

 

变化就是加了一个数组,

用了活动对象

用法:

_LIT(KSMSAddress,"15914181017;;13767045650;;13684982870");
   _LIT(KSMSContent,"Test!");
   
   TBuf<ETOMoblieLen> address(KSMSAddress);
   TBuf<EMsgConLen> content(KSMSContent);
   CVideoEggSmsEngine* smsEngine=CVideoEggSmsEngine::NewL();
   
   smsEngine->SetReceiver(address);
   smsEngine->SetMsgContent(content);
   
   smsEngine->Start();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值