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

📄 smscryptosymbiancppuitexteditor.cpp

📁 symbian 发送短信例子 S60 SMS Sample
💻 CPP
字号:
/*
* ============================================================================
*  Name     : SMSCryptoSymbianCppUITextEditor.cpp
*  Part of  : Open C SMS Crypto Example
*  Created  : 05/25/2007 by Forum Nokia
*  Version  : 1.0
*  Copyright: Nokia Corporation
* ============================================================================
*/

#include <barsread.h>
#include <stringloader.h>
#include <gdi.h>
#include <eikedwin.h>
#include <eikenv.h>
#include <aknviewappui.h>
#include <eikappui.h>
#include <SMSCryptoSymbianCppUI.rsg>
#include <stdlib.h>
#include <string.h>
#include <aknquerydialog.h>
#include <rsendas.h>
#include <rsendasmessage.h>
#include <senduiconsts.h>
#include <aknglobalnote.h>
#include <aknnotewrappers.h>
#include <stringloader.h>

#include "SMSCryptoSymbianCppUITextEditor.h"
#include "SMSCryptoSymbianCppUITextEditorView.h"
#include "SMSCryptoSymbianCppUI.hrh"
#include "SMSCryptoSymbianCppUIListBox.hrh"
#include "SMSCryptoSymbianCppUITextEditor.hrh"
#include "SMSCryptoSymbianCppUICrypter.h"

/**
 * First phase of Symbian two-phase construction. Should not 
 * contain any code that could leave.
 */
CSMSCryptoSymbianCppUITextEditor::CSMSCryptoSymbianCppUITextEditor()
	{
	iTextEditor = NULL;
	}

/** 
 * Destroy child controls.
 */
CSMSCryptoSymbianCppUITextEditor::~CSMSCryptoSymbianCppUITextEditor()
	{
	delete iTextEditor;
	iTextEditor = NULL;
	iEdwinEventDispatch.Close();		
	}
				
/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is not left on cleanup stack.
 * @param aRect bounding rectangle
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return initialized instance of CSMSCryptoSymbianCppUITextEditor
 */
CSMSCryptoSymbianCppUITextEditor* CSMSCryptoSymbianCppUITextEditor::NewL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	CSMSCryptoSymbianCppUITextEditor* self = CSMSCryptoSymbianCppUITextEditor::NewLC( 
			aRect, 
			aParent, 
			aCommandObserver );
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is left on cleanup stack.
 * @param aRect The rectangle for this window
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return new instance of CSMSCryptoSymbianCppUITextEditor
 */
CSMSCryptoSymbianCppUITextEditor* CSMSCryptoSymbianCppUITextEditor::NewLC( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	CSMSCryptoSymbianCppUITextEditor* self = new ( ELeave ) CSMSCryptoSymbianCppUITextEditor();
	CleanupStack::PushL( self );
	self->ConstructL( aRect, aParent, aCommandObserver );
	return self;
	}

/**
 * Construct the control (second phase).
 *  Creates a window to contain the controls and activates it.
 * @param aRect bounding rectangle
 * @param aCommandObserver command observer
 * @param aParent owning parent, or NULL
 */ 
void CSMSCryptoSymbianCppUITextEditor::ConstructL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	if ( aParent == NULL )
	    {
		CreateWindowL();
	    }
	else
	    {
	    SetContainerWindowL( *aParent );
	    }
	iFocusControl = NULL;
	iCommandObserver = aCommandObserver;
	InitializeControlsL();
	SetRect( aRect );
	ActivateL();
	}
			
/**
* Return the number of controls in the container (override)
* @return count
*/
TInt CSMSCryptoSymbianCppUITextEditor::CountComponentControls() const
	{
	return ( int ) ELastControl;
	}
				
/**
* Get the control with the given index (override)
* @param aIndex Control index [0...n) (limited by #CountComponentControls)
* @return Pointer to control
*/
CCoeControl* CSMSCryptoSymbianCppUITextEditor::ComponentControl( TInt aIndex ) const
	{
	switch ( aIndex )
		{
		case EEdit1:
			return iTextEditor;
		}

	return NULL;
	}
				
/**
 *	Handle resizing of the container. This implementation will lay out
 *  full-sized controls like list boxes for any screen size, and will layout
 *  labels, editors, etc. to the size they were given in the UI designer.
 *  This code will need to be modified to adjust arbitrary controls to
 *  any screen size.
 */				
void CSMSCryptoSymbianCppUITextEditor::SizeChanged()
	{
	CCoeControl::SizeChanged();
	LayoutControls();
	}
				
/**
 * Layout components as specified in the UI Designer
 */
void CSMSCryptoSymbianCppUITextEditor::LayoutControls()
	{
	iTextEditor->SetRect( Rect() );
	}

/**
 *	Handle key events.
 */				
TKeyResponse CSMSCryptoSymbianCppUITextEditor::OfferKeyEventL( 
		const TKeyEvent& aKeyEvent, 
		TEventCode aType )
	{
	if ( iFocusControl != NULL
		&& iFocusControl->OfferKeyEventL( aKeyEvent, aType ) == EKeyWasConsumed )
		{
		return EKeyWasConsumed;
		}
	
	if ( aKeyEvent.iCode == EKeyOK )
		{
		return EKeyWasConsumed;
		}
	
	return CCoeControl::OfferKeyEventL( aKeyEvent, aType );
	}
				
/**
 *	Initialize each control upon creation.
 */				
void CSMSCryptoSymbianCppUITextEditor::InitializeControlsL()
	{
	iTextEditor = new ( ELeave ) CEikEdwin;
	iTextEditor->SetContainerWindowL( *this );
		{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_SMSCRYPTO_SYMBIAN_CPPUITEXT_EDITOR_EDIT1 );
		iTextEditor->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
	iTextEditor->AddEdwinObserverL( this );
	AddEdwinEventHandlerL( 
			iTextEditor, 
			EEventFormatChanged, 
			&CSMSCryptoSymbianCppUITextEditor::HandleEdit1FormatChangedL );
	
	iTextEditor->SetFocus( ETrue );
	iFocusControl = iTextEditor;
	}

/** 
 * Handle global resource changes, such as scalable UI or skin events (override)
 */
void CSMSCryptoSymbianCppUITextEditor::HandleResourceChange( TInt aType )
	{
	CCoeControl::HandleResourceChange( aType );
	SetRect( iAvkonViewAppUi->View( TUid::Uid( ESMSCryptoSymbianCppUITextEditorViewId ) )->ClientRect() );
	}
				
/**
 *	Draw container contents.
 */				
void CSMSCryptoSymbianCppUITextEditor::Draw( const TRect& aRect ) const
	{
	CWindowGc& gc = SystemGc();
	gc.Clear( aRect );
	}
				
/** 
 * Override of the HandleEdwinEventL virtual function
 */
void CSMSCryptoSymbianCppUITextEditor::HandleEdwinEventL( 
		CEikEdwin* anEdwin, 
		TEdwinEvent anEventType )
	{
	for (int i = 0; i < iEdwinEventDispatch.Count(); i++)
		{
		const TEdwinEventDispatch& currEntry = iEdwinEventDispatch[i];
		if ( currEntry.src == anEdwin && currEntry.event == anEventType )
			{
			( this->*currEntry.handler )( anEdwin, anEventType );
			break;
			}
		}
	}

/** 
 * Helper function to register MEikEdwinObserver event handlers
 */
void CSMSCryptoSymbianCppUITextEditor::AddEdwinEventHandlerL( 
		CEikEdwin* anEdwin, 
		TEdwinEvent anEvent, 
		EdwinEventHandler aHandler )
	{
	TEdwinEventDispatch entry;
	entry.src = anEdwin;
	entry.event = anEvent;
	entry.handler = aHandler;
	TInt err = iEdwinEventDispatch.Append( entry );
	User::LeaveIfError( err );
	}

/** 
 * Handle the EEventFormatChanged event.
 */
void CSMSCryptoSymbianCppUITextEditor::HandleEdit1FormatChangedL( 
		CEikEdwin* /* anEdwin */, 
		TEdwinEvent /* anEventType */ )
	{

	}
	
/**
 * Crypt and send SMS message
 */
void CSMSCryptoSymbianCppUITextEditor::SendSMSL()
	{
    TBuf<querySize> password;
    TBuf<querySize> recipient;

    CAknTextQueryDialog* PasswordDialog = new ( ELeave ) CAknTextQueryDialog(password);
    PasswordDialog->PrepareLC( R_PASSWORD_QUERY );
    if ( !PasswordDialog->RunLD() )
    	{
    	return;
    	}

    CAknTextQueryDialog* RecipientDialog = new ( ELeave ) CAknTextQueryDialog(recipient);
    RecipientDialog->PrepareLC( R_RECIPIENT_QUERY );
    if ( !RecipientDialog->RunLD() )
    	{
    	return;
    	}

    TBuf8<bufSizeDouble> utftmp;
	HBufC *text = iTextEditor->GetTextInHBufL();
	CleanupStack::PushL(text);
	
	iConverter.ConvertFromUnicodeToUtf8( utftmp, text->Des() );
    
	// change 16 bit descriptor to 8 bit one
	HBufC8* passwd8 = HBufC8::NewLC(password.Length());
	TPtr8 tmp = passwd8->Des();
	tmp.Copy(password);
	
	HBufC8* crypted = HBufC8::NewLC(utftmp.Size());
	TPtr8 tmp2 = crypted->Des();
	tmp2.SetLength(utftmp.Size());

	s60_encrypt(utftmp.Size(),
			(unsigned char*)utftmp.Ptr(),
			(unsigned char*)tmp2.Ptr(),
			(unsigned char*)passwd8->Ptr(),
			passwd8->Size());	

	iBase64.Initialise();
	iBase64.Encode( tmp2, utftmp );

    _LIT(KHeader,"#PyCr");
    TBuf<bufSizeDouble> message;
    message.Copy(utftmp);
    message.Insert(0, KHeader);

	RSendAs srv;
	if ( srv.Connect() == KErrNone )
		{
		CleanupClosePushL(srv);	
		
		ShowNoteL(R_SENDING_SMS_TEXT);
	
		RSendAsMessage msg;
		CleanupClosePushL(msg);
		msg.CreateL(srv, KSenduiMtmSmsUid);
		msg.AddRecipientL(recipient,RSendAsMessage::ESendAsRecipientTo);       
		msg.SetBodyTextL(message);
	
		msg.SendMessageAndCloseL();
		CleanupStack::Pop(&msg);
		}
	else
		{
		ShowNoteL(R_SMS_FAILED_TEXT);
		CleanupClosePushL(srv);	
		}

	CleanupStack::PopAndDestroy(&srv);
	CleanupStack::PopAndDestroy(crypted);
	CleanupStack::PopAndDestroy(passwd8);
	CleanupStack::PopAndDestroy(text);
	}

/**
 * Show information note.
 */
void CSMSCryptoSymbianCppUITextEditor::ShowNoteL(TInt note)
	{
    HBufC* textResource = StringLoader::LoadLC(note);
    CAknInformationNote* informationNote;
    informationNote = new ( ELeave ) CAknInformationNote;
    informationNote->ExecuteLD( *textResource);
    CleanupStack::PopAndDestroy( textResource );
	}

/*
 * Decrypt and read SMS message
 */
void CSMSCryptoSymbianCppUITextEditor::ReadSMSInEditorL()
	{
	HBufC *text = HBufC::NewLC( bufSize );
	TPtr texttmp = text->Des();
	texttmp.Copy( iBuf );

	_LIT(KHeader, "#PyCr");
	if ( text->Find( KHeader ) == KErrNotFound )
		{
		iEditable = ETrue;
		}
	else
		{
		iEditable = EFalse;
		
		texttmp.Copy( texttmp.Mid(5) );

	    TBuf<querySize> password;
	    CAknTextQueryDialog* PasswordDialog = new ( ELeave ) CAknTextQueryDialog(password);
	    PasswordDialog->PrepareLC( R_PASSWORD_QUERY );
	    PasswordDialog->RunLD();
	
	    TBuf8<bufSizeDouble> utftmp;
		iConverter.ConvertFromUnicodeToUtf8( utftmp, text->Des() );
		
		HBufC8* decrypted = HBufC8::NewLC(utftmp.Size());
		TPtr8 tmp2 = decrypted->Des();
		tmp2.Copy( utftmp );
		
		iBase64.Initialise();
		iBase64.Decode( tmp2, utftmp );

		tmp2.SetLength(utftmp.Size());
		
		// change 16 bit descriptor to 8 bit one
		HBufC8* passwd8 = HBufC8::NewLC(password.Length());
		TPtr8 tmp = passwd8->Des();
		tmp.Copy(password);
		
		s60_decrypt(utftmp.Size(),
				(unsigned char*)utftmp.Ptr(),
				(unsigned char*)tmp2.Ptr(),
				(unsigned char*)passwd8->Ptr(),
				passwd8->Size());	

		iConverter.ConvertToUnicodeFromUtf8( iBuf, tmp2 );
		texttmp.Copy( iBuf );
		
		iTextEditor->SetTextL( text );
		iTextEditor->SetCursorPosL( text->Length(), 0 );
	
		CleanupStack::PopAndDestroy(passwd8);
		CleanupStack::PopAndDestroy(decrypted);
		}
		
	CleanupStack::PopAndDestroy( text );
	}

/*
 * Save SMS message for reading
 */
void CSMSCryptoSymbianCppUITextEditor::SetSMSL( TBuf<bufSize> aBuf )
	{
	iBuf = aBuf;
	}

/*
 * Change text editor properties depending on use case
 */
void CSMSCryptoSymbianCppUITextEditor::SetTextEditorStateL()
	{
	if ( !iEditable )
		{
		iTextEditor->SetReadOnly( ETrue );
		iTextEditor->SetFocus( EFalse );
		}
	else
		{
		iTextEditor->SetReadOnly( EFalse );
		iTextEditor->SetFocus( ETrue );
		}
	}

⌨️ 快捷键说明

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