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

📄 gdsms.cpp

📁 send SMS example. send SMS example.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
            if ( *entryId == KMsvSentEntryId )   // the entry has been moved into Sent folder
                {
                // We take the moved entries into a selection
                CMsvEntrySelection* entries = static_cast<CMsvEntrySelection*>(aArg1);

                //Process each created entry, one at a time.
                for(TInt i = 0; i < entries->Count(); i++)
                    {
                    DeleteSentEntry(entries->At(i)); // this checks the entry and deletes if it is created by GDSMS app                
                    }
                }
            }
            break;

            // This event tells us that the session has been opened
        case EMsvServerReady:
            CompleteConstructL();       // Construct the mtm registry & sms mtm
            break;

        default:
            // All other events are ignored
            break;
        }

    }



/*
-----------------------------------------------------------------------------

    CGDSMSAppUi::HandleCommandL(TInt aCommand)

    Handle the commands from CBA and menu items

-----------------------------------------------------------------------------
*/
void CGDSMSAppUi::HandleCommandL(TInt aCommand)
    {
    switch (aCommand)
        {
    case EGDSMSCmdSend:
        CmdSendL();
        break;
    case EEikCmdExit: 
        CmdExitL();
        break;
    default:
        break;
        }
    }

/*
-----------------------------------------------------------------------------

    CGDSMSAppUi::CmdSendL()

    Handle send command  
    
-----------------------------------------------------------------------------
*/
void CGDSMSAppUi::CmdSendL()
    {
    if (!InitializeCommunicationsL())
        {
        iEikonEnv->InfoWinL(_L("Error"),_L("Problems in initializing\ncommunications."));
        return;
        }
    if (!SendMessageL())
        {
        iEikonEnv->InfoWinL(_L("Error"),_L("Problems in sending\nmessage."));
        return;
        }
    }


/*
-----------------------------------------------------------------------------

    CGDSMSAppUi::CmdExitL()
    
    Exit application
  
-----------------------------------------------------------------------------
*/

void CGDSMSAppUi::CmdExitL()
    {
    DeleteMessagesFromInboxL();
    Exit();
    }



/*
-----------------------------------------------------------------------------

    CGDSMSAppUi::InitializeCommunicationsL()
    
    Initialize a new message and ask the user for a recipient gsm number.

    Return values:      ETrue or EFalse

-----------------------------------------------------------------------------
*/
TBool CGDSMSAppUi::InitializeCommunicationsL()
    { 
    // first the tel number
    // we get it from our telNumDialog
    CGDSMSTelNumDialog* telNumDialog=new(ELeave)CGDSMSTelNumDialog(*iRecipient);

    if (!telNumDialog->ExecuteLD(R_GDSMS_TEL_NUMBER_DIALOG))
        return EFalse;

    // set up a new message
    iMsvId = CreateNewMessageL();
    // Set the new message to be the current entry
    SetEntryL(iMsvId);

    return ETrue;
    }



/*
-----------------------------------------------------------------------------

    CGDSMSAppUi::CreateNewMessageL()

    Creates a new message server entry and set up default values.

    Return values:      TMsvId (the id of created entry)

-----------------------------------------------------------------------------
*/
TMsvId CGDSMSAppUi::CreateNewMessageL()
	{
    TMsvEntry newEntry;              // This represents an entry in the Message Server index
    newEntry.iMtm = KUidMsgTypeSMS;                         // message type is SMS
    newEntry.iType = KUidMsvMessageEntry;                   // this defines the type of the entry: message 
    newEntry.iServiceId = KMsvLocalServiceIndexEntryId;     // ID of local service (containing the standard folders)
    newEntry.iDate.HomeTime();                              // set the date of the entry to home time
    newEntry.SetInPreparation(ETrue);                       // a flag that this message is in preparation

    //----
    //newEntry.iBioType = 0x1000ffff;                         // define a bio UID if sending a bio message over SMS bearer
    //----

    // - 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, KMsvDraftEntryIdValue ,TMsvSelectionOrdering());
    CleanupStack::PushL(entry);
    CCommandAbsorbingControl::NewLC();
    CMsvOperationWait* wait = CMsvOperationWait::NewLC();
    wait->Start();
    // We create a new entry asynchronously...
    CMsvOperation* oper = entry->CreateL(newEntry,wait->iStatus);
    CleanupStack::PushL(oper);
    CActiveScheduler::Start();

    // ...and keep track of the progress of the create operation.
    TMsvLocalOperationProgress progress = McliUtils::GetLocalProgressL(*oper);
    User::LeaveIfError(progress.iError);

    // Set our entry context to the created one
    entry->SetEntryL(progress.iId); // operation progress contains the ID of the ceated entry

    CleanupStack::PopAndDestroy(4); // entry,oper,wait, CCommandAbsorbingControl

    return progress.iId;
	}



/*
-----------------------------------------------------------------------------

    CGDSMSAppUi::SetEntryL(TMsvId aEntryId)

    Set up current message entry.

    Note: It can be useful to remember the original entry id for 
          error handling.

-----------------------------------------------------------------------------
*/

void CGDSMSAppUi::SetEntryL(TMsvId aEntryId)
    {
    // Get the server entry from our session
    CMsvEntry* entry = iSession->GetEntryL(aEntryId);
    CleanupStack::PushL(entry);
    
    // Check if our mtm is different from the mtm set to our entry
    if (iMtm == NULL || entry->Entry().iMtm != (iMtm->Entry()).Entry().iMtm)
        {
        // If so, we delete the old... 
        delete iMtm;
        iMtm = NULL;
        
        // ...and get a new one from the MtmRegistry
        iMtm = iMtmReg->NewMtmL(entry->Entry().iMtm);
        iMtm->SetCurrentEntryL(entry);  
        }
    else
        {
        // if there is no need to change our mtm,
        // we only set our entry as current.
        iMtm->SetCurrentEntryL(entry);
        }

    CleanupStack::Pop(); //entry
    entry = NULL;
    }



/* 
-----------------------------------------------------------------------------
    CGDSMSAppUi::SendMessageL()

    Prepares the message body and sends the message.

    Return values:      ETrue or EFalse

-----------------------------------------------------------------------------
*/
TBool CGDSMSAppUi::SendMessageL()
    {

    TMsvEntry msvEntry = iMtm->Entry().Entry();

    // We get the message body from Mtm and insert a bodytext
    CRichText& mtmBody = iMtm->Body();
    mtmBody.Reset();
    mtmBody.InsertL(0, KGDSMSTag);   // insert our msg tag as the body text
    
    // set iRecipient into the Details of the entry
    msvEntry.iDetails.Set(iRecipient->Des());  // set recipient info in details
    msvEntry.SetInPreparation(EFalse);         // set inPreparation to false

    msvEntry.SetSendingState(KMsvSendStateWaiting);   // set the sending state (immediately)
	msvEntry.iDate.HomeTime();                        // set time to Home Time
 

    // To handle the sms specifics we start using SmsMtm
    CSmsClientMtm* smsMtm = STATIC_CAST(CSmsClientMtm*, iMtm);


    // 
    smsMtm->RestoreServiceAndSettingsL();
 
	// CSmsHeader encapsulates data specific for sms messages,
    // like service center number and options for sending.
    CSmsHeader& header = smsMtm->SmsHeader();
    CSmsSettings* sendOptions = CSmsSettings::NewL();
    CleanupStack::PushL(sendOptions);
    sendOptions->CopyL(smsMtm->ServiceSettings()); // restore existing settings

    // set send options
    sendOptions->SetDelivery(ESmsDeliveryImmediately);      // set to be delivered immediately
    header.SetSmsSettingsL(*sendOptions);

	// let's check if there's sc address
	if (header.Message().ServiceCenterAddress().Length() == 0)
		{
		// no, there isn't. We assume there is at least one sc number set and use
		// the default SC number. 
		CSmsSettings* serviceSettings = &(smsMtm->ServiceSettings());
        
		// if number of scaddresses in the list is null
        if (!serviceSettings->NumSCAddresses())
            {
			// here there should be a dialog in which user can add sc number
			iEikonEnv->InfoWinL(_L("No service center number"),_L("cannot send this one."));
			}
		else
			{
            // set sc address to default. 
            CSmsNumber* sc = 0;
			sc = &(serviceSettings->SCAddress(serviceSettings->DefaultSC()));
			header.Message().SetServiceCenterAddressL(sc->Address());
			}
		}
	CleanupStack::PopAndDestroy(); // send options 
    
    // Add our recipient to the list, takes in two TDesCs, first is real address and second is an alias
    // works also without the alias parameter.
	smsMtm->AddAddresseeL(iRecipient->Des(),msvEntry.iDetails);
    
    // Next we mark our message so later on we know which
    // message to delete from the Sent folder
    msvEntry.iMtmData3 = KUidGDSMS.iUid;     // use our app uid as an identifier

    // save message
    CMsvEntry& entry = iMtm->Entry();
    entry.ChangeL(msvEntry);                // make sure that we are handling the right entry
	smsMtm->SaveMessageL();                 // closes the message

    // This moves the message entry to outbox, we'll schedule it for sending after this. 
    TMsvId movedId = MoveMessageEntryL( KMsvGlobalOutBoxIndexEntryId );  // move message to outbox

 
    // We must create an entry selection for message copies (although now we only have one message in selection)
    CMsvEntrySelection* selection = new (ELeave) CMsvEntrySelection;
    CleanupStack::PushL(selection);

    selection->AppendL(movedId);            // add our message to the selection
    SetScheduledSendingStateL(selection);   // schedule the sending with the active scheduler

    CleanupStack::PopAndDestroy(); // selection

    return ETrue; // at this point the message has been sent

    }

/* 
-----------------------------------------------------------------------------
    CGDSMSAppUi::MoveMessageEntryL(TMsvId aTarget) const

    Moves an entry to another parent.

    Return values:      TMsvId of the moved message

-----------------------------------------------------------------------------
*/
TMsvId CGDSMSAppUi::MoveMessageEntryL( TMsvId aTarget )

⌨️ 快捷键说明

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