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

📄 cmmsssendhandler.cpp

📁 Symbian 手机发送彩信程序 mmssend
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    HBufC16* aText1 = StringLoader::LoadLC( R_MMSSEND_TEXT_ATTACHMENT1_TEXT );
    appText1.Append( aText1->Des() );
    CleanupStack::PopAndDestroy( aText1 );
    HBufC16* aName1 = StringLoader::LoadLC( R_MMSSEND_TEXT_ATTACHMENT1_NAME );
    appName1.Append( aName1->Des() );
    CleanupStack::PopAndDestroy( aName1 );
    TMsvId attachmentId1( KMsvLocalServiceIndexEntryId );
    TPtrC8 aType1( KMmsTextPlain );      // Text type Plain text
    const TUint8 aCode1( KMmsUsAscii );  // USAscii coded text
    AddTextAttachmentL( attachmentId1, aType1, aCode1, appText1, appName1 );

    // Add 2. KMmmsUsAscii coded text attchment to the message
    TBuf<50>  appText2;
    TBuf<12>  appName2;
    HBufC16* aText2 = StringLoader::LoadLC( R_MMSSEND_TEXT_ATTACHMENT2_TEXT );
    appText2.Append( aText2->Des() );
    CleanupStack::PopAndDestroy( aText2 );
    HBufC16* aName2 = StringLoader::LoadLC( R_MMSSEND_TEXT_ATTACHMENT2_NAME );
    appName2.Append( aName2->Des() );
    CleanupStack::PopAndDestroy( aName2 );
    TMsvId attachmentId2( KMsvLocalServiceIndexEntryId );
    TPtrC8 aType2( KMmsTextPlain );      // Text type Plain text
    const TUint8 aCode2( KMmsUtf8 );     // Utf8 coded text
    AddTextAttachmentL( attachmentId2, aType2, aCode2, appText2, appName2 );

    // Add 1. picture *.jpeg picture to the MMS message (mmsconst.h)
    // The attachment name
    HBufC16* aFileName1 = StringLoader::LoadLC( R_MMSSEND_MMS_OBJ1_NAME );
    appFileName.Append( aFileName1->Des() ); // = "pic2a.jpg"
    CleanupStack::PopAndDestroy( aFileName1 );
    // Use BaflUtils to get the drive an path information
    appDriveAndPath = BaflUtils::DriveAndPathFromFullName( iAppFullPath );
    // Append the filename for the drive and the path
	appDriveAndPath.Append( appFileName );
    // We need to make type conversion for the *.jpg
    const TUint8 KAssignedNumberJpeg( 0x1E );
    TPtrC8 jpegPtr( KContentTypeTable[KAssignedNumberJpeg] );
	TMsvId attachmentId3( KMsvLocalServiceIndexEntryId );
    AddMultimediaAttachment2L( attachmentId3, jpegPtr, appDriveAndPath );
    appFileName.Delete( 0, appFileName.Length() );
    appDriveAndPath.Delete( 0, appDriveAndPath.Length() );

    // Add audio *.wav to the message
    // This introduces the second alternative way to add attachments to MMS message
    // Get the names and types for the attachments (*.jpeg, *.gif*,e*.vaw)
    // See supported typed for the messages (mmsconst.h)
    // The attachment name
    HBufC16* aFileName3 = StringLoader::LoadLC( R_MMSSEND_MMS_OBJ3_NAME );
    appFileName.Append( aFileName3->Des() ); // = "sound1.wav";
    CleanupStack::PopAndDestroy( aFileName3 );
    // Use BaflUtils to get the drive an path information
    appDriveAndPath = BaflUtils::DriveAndPathFromFullName( iAppFullPath );
    // Append the filename for the drive and the path
	appDriveAndPath.Append( appFileName );
    TMsvId attachmentId5( KMsvLocalServiceIndexEntryId );
    // We need to make type conversion for the *.wav file
    _LIT8(KAudioWav,"audio/x-wav");
    TPtrC8 audioPtr( KAudioWav );
	AddMultimediaAttachment2L( attachmentId5, audioPtr, appDriveAndPath );
    appFileName.Delete( 0, appFileName.Length() );
    appDriveAndPath.Delete( 0, appDriveAndPath.Length() );

    // If all have gone fine set recipient
    // details and prepare message for sending
    aMsvEntry.iDetails.Set( iRecipientText );
    aMsvEntry.SetInPreparation( EFalse );
    aMsvEntry.SetVisible( ETrue );

    // We could set priority to our message through the TMsvEntry or
    // by using CMmsClientMtm API method SetDefaultMessagePriority()
    // aMsvEntry.SetPriority(EMsvHighPriority);

	// Set message sending state
    aMsvEntry.SetSendingState( KMsvSendStateWaiting );
    aMsvEntry.iDate.HomeTime();
    }

// ----------------------------------------------------------------------------
// CMmssSendHandler::AddTextAttachmentL()
// Initialize the MMS message and save the changes
// ----------------------------------------------------------------------------
//
void CMmssSendHandler::AddTextAttachmentL( TMsvId& attachmentId,
                                            TPtrC8& aType, const TUint aCode,
                                            TDes& aMsgText, TDes& aMsgName )
    {
    if ( iMtm )
        {
        // Get handle to our message
	    CMmsClientMtm* mmsMtm = static_cast<CMmsClientMtm*>( iMtm );

        mmsMtm->CreateTextAttachmentL ( attachmentId, aMsgText, aMsgName );
	    mmsMtm->SetAttachmentCharsetL ( attachmentId, aCode );
	    mmsMtm->SetAttachmentTypeL ( attachmentId, aType );
	    mmsMtm->SetAttachmentNameL ( attachmentId, aMsgName );
	    mmsMtm->SetMessageRootL( attachmentId ) ;
        mmsMtm->SaveMessageL();
        }
    }

// ----------------------------------------------------------------------------
// CMmssSendHandler::AddMultimediaAttachment2L()
// Initialize the MMS message and save the changes
// (Message Server will try to solve the message type -"*.gif, *.jpeg")
// ----------------------------------------------------------------------------
//
void CMmssSendHandler::AddMultimediaAttachment2L( TMsvId& attachmentId, 
                                                  TPtrC8& aType,
                                                  TFileName aObjFullPathAndName )
    {
    if ( iMtm )
        {
        // Get handle to our message
	    CMmsClientMtm* mmsMtm = static_cast<CMmsClientMtm*>( iMtm );

        //Use RFs-object to verify that the filename (image) path is correct
	    RFs fs;
	    TParse fp;

	    TInt result( fs.Connect() );
	    if ( KErrNone != result )
	    {
		    iObserver.HandleAttachmentErrorL( MMsvObserver::EConnectionFailed );
            fs.Close();
	    }
	    TInt result2( fs.Parse( aObjFullPathAndName, fp ) );
	    if ( KErrNone != result2 )
	    {
		     iObserver.HandleAttachmentErrorL( MMsvObserver::EFileNameWrong );
             fs.Close();
	    }
	    fs.Close();

	    mmsMtm->CreateAttachment2L( attachmentId, aObjFullPathAndName );
	    mmsMtm->SetAttachmentTypeL( attachmentId, aType );
	    mmsMtm->SetMessageRootL( attachmentId );
    	mmsMtm->SaveMessageL();
        }
    }
// ----------------------------------------------------------------------------
// CMmssSendHandler::InitializeMessageL()
// Initialize the MMS message and save the changes
// ----------------------------------------------------------------------------
//
TBool CMmssSendHandler::InitializeMessageL()
    {
    ASSERT( iMtm );

    //Take message server index entry
    TMsvEntry msvEntry( iMtm->Entry().Entry() );

    // Add attachments for the message
    PopulateMessageL( msvEntry );

    // Set MMS specific Mtm fields
    if ( !SetupMmsHeaderL() )
        {
        return EFalse;
        }

    // 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.
    iMtm->AddAddresseeL( iRecipientText, msvEntry.iDetails );
	//Other possible way to add recipient is to use following method
	//which uses parameters recipient and (EMmsTo | EMmsCc | EMmsBc )
	//iMtm->AddTypedAddresseeL(iRecipientText, EMmsTo);

	// Add our subject to the list
	iMtm->SetSubjectL( iSubjectText );

	// save message
    CMsvEntry& entry = iMtm->Entry();
    entry.ChangeL( msvEntry );            // commit index changes
    iMtm->SaveMessageL();                 // commit message to message server

    return ETrue;
    }
// ----------------------------------------------------------------------------
// CMmssSendHandler::MoveMessageEntryL()
// Changes the message entry
// ----------------------------------------------------------------------------
//
TBool CMmssSendHandler::MoveMessageEntryL( TMsvId aTarget )
    {
    ASSERT( iMtm );

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

    if ( msvEntry.Parent() != aTarget )
        {
        TMsvSelectionOrdering sort;
        // we want to also handle the invisible entries
        sort.SetShowInvisibleEntries( ETrue );    
        // Take a handle to the parent entry
        CMsvEntry* parentEntry = CMsvEntry::NewL( iMtm->Session(), 
                                                  msvEntry.Parent(), sort );
        CleanupStack::PushL( parentEntry );

        // Move original from the parent to the new location
        iOperation = parentEntry->MoveL( msvEntry.Id(), aTarget, iStatus );

        CleanupStack::PopAndDestroy( parentEntry );
        SetActive();

        return ETrue;
        }
    return EFalse;
    }

// ----------------------------------------------------------------------------
// CMmssSendHandler::SetScheduledSendingStateL()
// Validate message and schedule it before sending it to recipient
// ----------------------------------------------------------------------------
//
void CMmssSendHandler::SetScheduledSendingStateL( CMsvEntrySelection& aSelection )
    {
    ASSERT( iMtm );

	CMmsClientMtm* mmsMtm = static_cast<CMmsClientMtm*>( iMtm );

    // Check MMS messag part before sending it
	TMsvPartList invalidParts( mmsMtm->ValidateMessage(
                                                KMsvMessagePartRecipient |
										        KMsvMessagePartOriginator |
										        KMsvMessagePartAttachments ) );

	if ( invalidParts & KMsvMessagePartRecipient ||
		invalidParts & KMsvMessagePartOriginator ||
		invalidParts & KMsvMessagePartAttachments )
	    {
		User::Leave( KErrCorrupt );
	    }

    // Let see what is the maximum message size, so that we don't send too
    // big message, which isn't allowed. Max. MMS message is limited to 100KB!
	TInt32 maxSendSize( mmsMtm->MaximumSendSize() );
	TInt32 msgSize( mmsMtm->MessageSize() );
    TInt32 maxReceiveSize( mmsMtm->MaximumReceiveSize() );
	if ( msgSize > maxSendSize || msgSize > maxReceiveSize )
	    {
		User::Leave( KErrCorrupt );
	    }

    // TTime aSendingTime(0) == send message as fast as possible
    // Note: Using following way SendL, we don't have to use
    // CMsvOperationWait class.
	TTime aSendingTime(0);
	iOperation = mmsMtm->SendL( aSelection, iStatus, aSendingTime );

    // Request is outstanding
	SetActive();
	}

// ----------------------------------------------------------------------------
// CMmssSendHandler::HandleSessionEventL()
// Framework calls this method to trigger the events
// ----------------------------------------------------------------------------
//
void CMmssSendHandler::HandleSessionEventL( TMsvSessionEvent aEvent,
						TAny* /*aArg1*/, TAny* /*aArg2*/, TAny* /*aArg3*/ )
    {
    switch ( aEvent )
        {
        // flowthrough, not used in this example application
        case EMsvEntriesCreated:
        case EMsvEntriesChanged:
        case EMsvEntriesDeleted:
        case EMsvEntriesMoved:
            break;
            // This event tells us that the session has been opened
        case EMsvServerReady:
			// Construct the mtm registry
            CompleteConstructL();
			iObserver.HandleServerStatusChange( MMsvObserver::EServerOpen );
            break;
        case EMsvCloseSession:
             // Handle close session
            // Note: If program gets in this part, then it will call to shutdown
            // the Message Server, which affects to all clients. The Message
            // Server will stop acting with clients. This part should be
            // avoided in all cases ( ref. CloseMessageServer() ).
            // iSession->CloseMessageServer();
			iObserver.HandleServerStatusChange( MMsvObserver::EServerClose );
            iObserver.ExitFromProgram();
            break;
        case EMsvServerTerminated:
            // Handle server terminated
            // Note: If program gets in this part, then it will call to shutdown
            // the Message Server, which affects to all clients. The Message
            // Server will stop acting with clients. This part should be
            // avoided in all cases ( ref. CloseMessageServer() ).
            // iSession->CloseMessageServer();
			iObserver.HandleServerStatusChange( MMsvObserver::EServerTerminate );
            iObserver.ExitFromProgram();
            break;
        case EMsvGeneralError:
        case EMsvServerFailedToStart:
            // A major problem has occurred
            iObserver.HandleErrorL( MMsvObserver::EFatalServerError );
            iObserver.ExitFromProgram();
            break;
        default:
            // All other events are ignored
            break;
        }
    }

// End of File

⌨️ 快捷键说明

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