imstutils/imconversationview/imcvuiapp/src/cimcvappsmileyutil.cpp
changeset 0 5e5d6b214f4f
equal deleted inserted replaced
-1:000000000000 0:5e5d6b214f4f
       
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Utility for selecting smile icon and converting
       
    15 *                between smile id and smile string.
       
    16 *
       
    17 */
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include 	"cimcvappsmileyutil.h"
       
    21 #include 	"cimcvapppicture.h"
       
    22 #include    "cimcvappsmileyinformation.h"
       
    23 
       
    24 #include	<PUAcodes.hrh>
       
    25 
       
    26 #include	<gulicon.h>
       
    27 #include 	<txtrich.h>
       
    28 #include 	<gdi.h>
       
    29 #include    <eikrted.h>
       
    30 #include	<frmtlay.h>
       
    31 #include 	"imcvlogger.h"
       
    32 #include    "imcvuiliterals.h"
       
    33 #include    "cimcvengine.h"
       
    34 //CONSTANT
       
    35 const TInt KChatSmileyLength( 3 );
       
    36 _LIT(KSpace, " ");
       
    37 
       
    38 // ================= MEMBER FUNCTIONS =======================
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CIMCVAppSmileyUtil::NewL
       
    42 // Two-phased constructor.
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CIMCVAppSmileyUtil* CIMCVAppSmileyUtil::NewL(
       
    46 							 MIMCVUiAppSkinVariant& aSkinVariant, 
       
    47                              MIMCVAppUi& aAppUi, TMessageExtensionTypes aType,
       
    48                              CIMCVEngine& aServiceEngine)
       
    49     {
       
    50     CIMCVAppSmileyUtil* self = new (ELeave) CIMCVAppSmileyUtil( aType );
       
    51     CleanupStack::PushL( self );
       
    52     self->ConstructL( aSkinVariant, aAppUi,aServiceEngine );
       
    53     CleanupStack::Pop();
       
    54     return self;
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CIMCVAppSmileyUtil::~CIMCVAppSmileyUtil
       
    59 // Destructor
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 CIMCVAppSmileyUtil::~CIMCVAppSmileyUtil()
       
    63     {
       
    64     iConvertArray.ResetAndDestroy();
       
    65 	delete iSmileUtil;
       
    66     }
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CIMCVAppSmileyUtil::CIMCVAppSmileyUtil
       
    70 // C++ constructor can NOT contain any code, that
       
    71 // might leave.
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 CIMCVAppSmileyUtil::CIMCVAppSmileyUtil( TMessageExtensionTypes aType ) 
       
    75 : MIMCVAppMessageExtension( aType)
       
    76     {
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CIMCVAppSmileyUtil::ConstructL
       
    81 // ConstructL
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 void CIMCVAppSmileyUtil::ConstructL(
       
    85 							 MIMCVUiAppSkinVariant& aSkinVariant, 
       
    86                              MIMCVAppUi& aAppUi,
       
    87 							 CIMCVEngine& aServiceEngine )
       
    88     {
       
    89     iServiceId = aServiceEngine.GetServiceId();
       
    90 	iSmileUtil = CIMCVAppSmileIconUtility::NewL(aSkinVariant, aAppUi, aServiceEngine );
       
    91     }
       
    92 
       
    93 
       
    94 // ---------------------------------------------------------
       
    95 // CIMCVAppSmileyUtil::InsertExtensionL
       
    96 // (other items were commented in a header).
       
    97 // ---------------------------------------------------------
       
    98 //
       
    99 void CIMCVAppSmileyUtil::InsertExtensionL( CEikRichTextEditor& aEditor,
       
   100                                       TBool& aCancelled, TSize asize )
       
   101     {
       
   102     IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::InsertExtensionL() start") );
       
   103 	TInt bitmapId( KErrNone );
       
   104 	TInt retVal = iSmileUtil->LaunchSmileIconDialogL( bitmapId );	
       
   105 	iSmileUtil->ResizeIcons(asize);		
       
   106 	if( retVal )
       
   107 		{
       
   108 		// check if there is room for icon, if not return
       
   109 		if( ( aEditor.MaxLength() - aEditor.TextLength() ) < KChatSmileyLength )
       
   110 			{
       
   111 			return;
       
   112 			}		
       
   113         //we don't own this
       
   114 		const TDesC& iconString = iSmileUtil->GetSmileString(bitmapId);		
       
   115         TInt cursorPos = aEditor.CursorPos();        
       
   116         HBufC* msgBuf = aEditor.GetTextInHBufL();
       
   117 		CleanupStack::PushL( msgBuf );
       
   118 		if(msgBuf)
       
   119 		    {
       
   120 		    TPtr msgPtr( msgBuf->Des() );		    
       
   121 		    aEditor.RichText()->InsertL(cursorPos,iconString);
       
   122 		    cursorPos = aEditor.TextLength();
       
   123 		    aEditor.SetCursorPosL( cursorPos, EFalse);		    
       
   124 		    }
       
   125 		else
       
   126 		    {
       
   127 		    aEditor.RichText()->InsertL(cursorPos,iconString);
       
   128 		    cursorPos = aEditor.TextLength(); 
       
   129 		    aEditor.SetCursorPosL( cursorPos, EFalse);		  
       
   130 		    }
       
   131 		CleanupStack::PopAndDestroy( msgBuf ); // msgBuf		
       
   132 		if( &aEditor != iEditor )
       
   133 			{
       
   134 			iEditor = &aEditor;
       
   135 			iCurrentCount = 1;
       
   136 			}
       
   137 		else
       
   138 			{
       
   139 			++iCurrentCount;
       
   140 			}
       
   141 			
       
   142 		aCancelled = EFalse;
       
   143 		}
       
   144     else
       
   145         {
       
   146         // apply formatting even in case of cancel, as the sizes of icons would have changed and when
       
   147 		// shifted from portrait to landscape and vice versa, alignment differs.
       
   148         TCharFormat charFormat;
       
   149         TCharFormatMask charFormatMask;
       
   150 		
       
   151 		IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::InsertExtensionL() before applying format") );
       
   152         charFormat.iFontPresentation.iPictureAlignment = TFontPresentation::EAlignCentered;
       
   153         charFormatMask.SetAttrib( EAttFontPictureAlignment );
       
   154         aEditor.RichText()->ApplyCharFormatL( charFormat, charFormatMask, 0,
       
   155                                               aEditor.TextLength() );
       
   156 		IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::InsertExtensionL() after applying format") );
       
   157 		aEditor.HandleTextChangedL ();
       
   158         aCancelled = ETrue;
       
   159         }
       
   160     IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::InsertExtensionL() end") );
       
   161 	}
       
   162 
       
   163 // ---------------------------------------------------------
       
   164 // CIMCVAppSmileyUtil::ExtensionToStringL
       
   165 // (other items were commented in a header).
       
   166 // ---------------------------------------------------------
       
   167 //
       
   168 void CIMCVAppSmileyUtil::ExtensionToStringL( CEikRichTextEditor& anEditor, TDes& aDes )
       
   169 	{
       
   170 	IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::ExtensionToStringL() start") );
       
   171 	RArray<TInt> iconIndArray;
       
   172 	CleanupClosePushL( iconIndArray );
       
   173 	
       
   174 	CPicture* editorPic;
       
   175 	TInt i;
       
   176 	TInt limit( 0 );
       
   177 
       
   178 	// for loop gets icon positions to array
       
   179     TInt length = aDes.Length();
       
   180 	for( i = 0; i < length; ++i )
       
   181 		{
       
   182 		if( aDes[ i ] ==  CEditableText::EPictureCharacter )
       
   183 			{
       
   184 			User::LeaveIfError( iconIndArray.Append( i ) );
       
   185 			}
       
   186 		}
       
   187 
       
   188 	// real count of pictures
       
   189 	TInt count = anEditor.RichText()->PictureCount();
       
   190 	TInt indCount = iconIndArray.Count();
       
   191 	
       
   192 	if( &anEditor != iEditor )
       
   193 		{
       
   194         Reset();
       
   195 		}
       
   196 
       
   197 	// whisper message
       
   198 	if( indCount > iCurrentCount )
       
   199 		{
       
   200 		limit = indCount - iCurrentCount;
       
   201 		}
       
   202 
       
   203     if( indCount > 0 )
       
   204         {
       
   205         for( i = count; i > limit; --i )
       
   206     		{
       
   207     		editorPic = anEditor.RichText()->PictureHandleL(
       
   208     							iconIndArray[ i - 1 ], MLayDoc::EForceLoadTrue );
       
   209     		if ( !editorPic )
       
   210     			{
       
   211     			User::Leave( KErrBadHandle );
       
   212     			}
       
   213 
       
   214     		CIMCVAppPicture* icon = static_cast<CIMCVAppPicture*> ( editorPic );
       
   215 
       
   216             TInt oldLen = aDes.Length();
       
   217             //delete padding char
       
   218 		    aDes.Delete( iconIndArray[ i - 1 ], 1 ); // 1 = one char
       
   219 		    //insert string smiley		    
       
   220 		    aDes.Insert( iconIndArray[ i - 1 ],
       
   221 		                 iSmileUtil->GetSmileString( icon->Index() ) );
       
   222     		}
       
   223         }
       
   224     CleanupStack::PopAndDestroy(); // iconIndArray.Close()
       
   225     IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::ExtensionToStringL() end") );
       
   226 	}
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // CIMCVAppSmileyUtil::ConvertSelectionToExtensionL
       
   230 // Convert selection smileys to icons.
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 void CIMCVAppSmileyUtil::ConvertSelectionToExtensionL( CEikRichTextEditor& aEditor )
       
   234     {
       
   235     IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::ConvertSelectionToExtensionL() start") );
       
   236     TInt selectAdd( 0 );
       
   237 
       
   238     // for loop gets icon positions to array
       
   239     TInt start = aEditor.Selection().LowerPos();
       
   240     TInt end = aEditor.Selection().HigherPos();
       
   241 
       
   242     // Clear convert array, it has something, but its not correct
       
   243     iConvertArray.ResetAndDestroy();
       
   244 	
       
   245 	const TInt KMaxLength( KDefaultMsgLength );
       
   246 	HBufC* des = HBufC::NewLC( KMaxLength );
       
   247 	TPtr desText( des->Des() );
       
   248 
       
   249     aEditor.GetText( desText );
       
   250     TInt txtLen = desText.Length();
       
   251     if ( txtLen < end )
       
   252         {
       
   253         end = txtLen;
       
   254         }
       
   255     desText.Delete( end, txtLen - end );
       
   256     desText.Delete( 0, start );
       
   257    
       
   258     iSmileUtil->SearchSmilesL( desText, iConvertArray, NULL, start );
       
   259 
       
   260 	#ifdef _DEBUG
       
   261 		TPtrC myDebug( aEditor.Text()->Read( 0 ) ); 
       
   262 	#endif
       
   263 
       
   264     for( TInt a( iConvertArray.Count() - 1 ); a >= 0; --a )
       
   265         {
       
   266         if( iConvertArray[ a ]->Position() >= start && iConvertArray[ a ]->Position() < end ) // do we convert or not
       
   267             {                
       
   268             TPictureHeader picHeader;
       
   269             // Insert icon in place
       
   270 
       
   271 		    //we don't own this
       
   272 		    CGulIcon* icon = const_cast<CGulIcon*>(
       
   273 		        iSmileUtil->GetSmileIcon( iConvertArray[ a ]->Index() ) );
       
   274 
       
   275 		    //create CIMCVAppPicture from selected icon
       
   276 		    CIMCVAppPicture* smileIcon = new ( ELeave ) CIMCVAppPicture(
       
   277 									    *CCoeEnv::Static()->SystemGc().Device(),
       
   278 									    icon, 
       
   279 									    iConvertArray[ a ]->Index() );
       
   280 
       
   281 		    picHeader.iPicture = TSwizzle<CPicture>( smileIcon );
       
   282             aEditor.Text()->DeleteL( iConvertArray[ a ]->Position(), 
       
   283             							iConvertArray[ a ]->SmileyString().Length() ); 
       
   284 
       
   285 	#ifdef _DEBUG
       
   286 			myDebug.Set( aEditor.Text()->Read( 0 ) );
       
   287 			
       
   288 	#endif
       
   289 
       
   290 			aEditor.RichText()->InsertL( iConvertArray[ a ]->Position(), picHeader );
       
   291 			
       
   292 	#ifdef _DEBUG
       
   293 			myDebug.Set( aEditor.Text()->Read( 0 ) );
       
   294 			
       
   295 	#endif
       
   296 
       
   297             selectAdd -= iConvertArray[ a ]->SmileyString().Length() - 1;
       
   298 
       
   299 		    // icon is inserted, and it should be converted to text
       
   300 		    // before sending, so it needs extra chars according to smiley string length
       
   301 		    TInt textLimit =
       
   302 		        aEditor.MaxLength() - iConvertArray[ a ]->SmileyString().Length();
       
   303             TInt currentlength = aEditor.Text()->DocumentLength();            
       
   304 		    aEditor.SetTextLimit( Max( textLimit, currentlength ) );
       
   305             
       
   306 		    if( &aEditor != iEditor )
       
   307 			    {
       
   308 			    iEditor = &aEditor;
       
   309 			    iCurrentCount = 1;
       
   310 			    }
       
   311 		    else
       
   312 			    {
       
   313 			    ++iCurrentCount;
       
   314 			    }
       
   315             }
       
   316         }
       
   317     
       
   318     // Apply formatting
       
   319     TInt selectionLen = end + selectAdd - start;
       
   320     TCharFormat charFormat;
       
   321 	TCharFormatMask charFormatMask;
       
   322 	charFormat.iFontPresentation.iPictureAlignment = 
       
   323 	    TFontPresentation::EAlignCentered;
       
   324 	charFormatMask.SetAttrib( EAttFontPictureAlignment );
       
   325 	aEditor.RichText()->ApplyCharFormatL( charFormat, charFormatMask,
       
   326 	                                      start, selectionLen );
       
   327     
       
   328     // Handle changes in editor
       
   329     aEditor.TextView()->HandleGlobalChangeL();
       
   330     
       
   331     // Set selection to correct after adding images
       
   332     aEditor.SetSelectionL( start, end + selectAdd );
       
   333 
       
   334     // Clear convert array
       
   335     iConvertArray.ResetAndDestroy();
       
   336     
       
   337     CleanupStack::PopAndDestroy( des );
       
   338     IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::ConvertSelectionToExtensionL() end") );
       
   339     }
       
   340     
       
   341 // -----------------------------------------------------------------------------
       
   342 // CIMCVAppSmileyUtil::ConvertSelectionToExtensionL
       
   343 // Convert selection smileys to icons.
       
   344 // -----------------------------------------------------------------------------
       
   345 //
       
   346 void CIMCVAppSmileyUtil::ConvertSelectionToExtensionL( CRichText& aRichText, 
       
   347 														TCursorSelection& aSelection )
       
   348     {
       
   349     IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::ConvertSelectionToExtensionL() start") );
       
   350     TInt start( aSelection.LowerPos() );
       
   351     TInt length( aSelection.Length() );
       
   352 
       
   353     // Clear convert array and fetch the text
       
   354     iConvertArray.ResetAndDestroy();	
       
   355     HBufC* text = HBufC::NewMaxLC( aRichText.DocumentLength() );
       
   356     TPtr desText( text->Des() );	   
       
   357     aRichText.Extract( desText, start, length );
       
   358     
       
   359     iSmileUtil->SearchSmilesL( desText, iConvertArray );
       
   360 
       
   361     for( TInt a( iConvertArray.Count() - 1 ); a >= 0; --a )
       
   362         {
       
   363         TPictureHeader picHeader;
       
   364         // Insert icon in place
       
   365         //we don't own this
       
   366 	    CGulIcon* icon = const_cast<CGulIcon*>( 
       
   367 	        iSmileUtil->GetSmileIcon( iConvertArray[ a ]->Index()) );			
       
   368 
       
   369 	    //create CIMCVAppPicture from selected icon
       
   370 	    CIMCVAppPicture* smileIcon = new ( ELeave ) CIMCVAppPicture(
       
   371 								    *CCoeEnv::Static()->SystemGc().Device(),
       
   372 								    icon, 
       
   373 								    iConvertArray[ a ]->Index());
       
   374 
       
   375 	    picHeader.iPicture = TSwizzle<CPicture>( smileIcon );
       
   376         aRichText.DeleteL( iConvertArray[ a ]->Position() + start,
       
   377                            iConvertArray[ a ]->SmileyString().Length() );
       
   378 
       
   379 		aRichText.InsertL( iConvertArray[ a ]->Position() + start, picHeader );
       
   380         }
       
   381 
       
   382     // clean up
       
   383     iConvertArray.ResetAndDestroy();
       
   384     CleanupStack::PopAndDestroy( text );    
       
   385     IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::ConvertSelectionToExtensionL() end") );
       
   386     }
       
   387 
       
   388 // -----------------------------------------------------------------------------
       
   389 // CIMCVAppSmileyUtil::ConvertSelectionToStringL
       
   390 // Convert selection smileys to strings
       
   391 // -----------------------------------------------------------------------------
       
   392 //
       
   393 void CIMCVAppSmileyUtil::ConvertSelectionToStringL( CEikRichTextEditor& aEditor, TBool aPreserve )
       
   394     {
       
   395     IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::ConvertSelectionToStringL() start") );
       
   396     // Clear convert array
       
   397     iConvertArray.ResetAndDestroy();
       
   398 
       
   399     HBufC* text = HBufC::NewMaxLC( KDefaultMsgLength );
       
   400     TPtr textCopy( text->Des() );
       
   401     aEditor.GetText( textCopy );
       
   402 
       
   403 	// for loop gets icon positions to array
       
   404     TInt start = 0;
       
   405     TInt end = aEditor.Text()->DocumentLength();
       
   406     //aEditor.Selection().HigherPos();
       
   407 	
       
   408 	TInt i;
       
   409     for( i = start; i < end; ++i )
       
   410 		{
       
   411 		if( textCopy[ i ] ==  CEditableText::EPictureCharacter )
       
   412 			{
       
   413             CIMCVAppSmileyInformation* smileInfo = CIMCVAppSmileyInformation::NewL(); 
       
   414             CleanupStack::PushL( smileInfo );
       
   415             smileInfo->SetPosition( i );
       
   416             smileInfo->SetIcon( ETrue );
       
   417 			User::LeaveIfError( iConvertArray.Append( smileInfo ) );
       
   418 			CleanupStack::Pop( smileInfo );
       
   419 			}
       
   420         }
       
   421 
       
   422     TInt count = iConvertArray.Count();
       
   423 	for( i = count - 1; i >= 0; --i )
       
   424 		{
       
   425 		CPicture* editorPic = aEditor.RichText()->PictureHandleL( 
       
   426             iConvertArray[ i ]->Position(), MLayDoc::EForceLoadTrue );
       
   427         
       
   428 		if ( !editorPic )
       
   429 			{
       
   430 			User::Leave( KErrBadHandle );
       
   431 			}
       
   432 
       
   433 		CIMCVAppPicture* icon = static_cast<CIMCVAppPicture*> ( editorPic );
       
   434 
       
   435         const TDesC& iconString = iSmileUtil->GetSmileString( icon->Index() );
       
   436         iConvertArray[ i ]->SetSmileyStringL( iconString );
       
   437         iConvertArray[ i ]->SetIndex( icon->Index() );
       
   438 		}
       
   439 
       
   440     textCopy.Delete( end, textCopy.Length() - end );
       
   441     textCopy.Delete( 0, start );
       
   442     iSmileUtil->SearchSmilesL( textCopy, iConvertArray, NULL, start );
       
   443 
       
   444     // Change icons to strings
       
   445 	TPtrC myDebug( aEditor.Text()->Read( 0 ) ); 
       
   446 
       
   447     TInt selectAdd( 0 );
       
   448     count = iConvertArray.Count();
       
   449     for( i = count - 1; i >= 0; --i )
       
   450         {
       
   451         if( iConvertArray[ i ]->IsIcon() ) // Icon
       
   452             {
       
   453             TCursorSelection deleteSel( iConvertArray[ i ]->Position(),
       
   454                                         iConvertArray[ i ]->Position() + 1 );
       
   455                                         
       
   456             aEditor.InsertDeleteCharsL( iConvertArray[ i ]->Position(),
       
   457                                         iConvertArray[ i ]->SmileyString(),
       
   458                                         deleteSel );
       
   459             selectAdd += iConvertArray[ i ]->SmileyString().Length() - 1;
       
   460             //aEditor.SetMaxLength( aEditor.MaxLength() + selectAdd );
       
   461             }
       
   462         }
       
   463 	myDebug.Set( aEditor.Text()->Read( 0 ) );
       
   464 	
       
   465 
       
   466     aEditor.SetSelectionL( start, end + selectAdd );
       
   467     aEditor.TextView()->HandleGlobalChangeL();
       
   468 
       
   469     if( !aPreserve )
       
   470         {
       
   471 	    iConvertArray.ResetAndDestroy();
       
   472         }
       
   473         
       
   474     CleanupStack::PopAndDestroy( text );
       
   475     IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::ConvertSelectionToStringL() end") );
       
   476     }
       
   477 
       
   478 // ---------------------------------------------------------
       
   479 // CIMCVAppSmileyUtil::DeleteExtensionL
       
   480 // (other items were commented in a header).
       
   481 // ---------------------------------------------------------
       
   482 //
       
   483 void CIMCVAppSmileyUtil::DeleteExtensionL( CEikRichTextEditor& aEditor, TInt aPos )
       
   484     {
       
   485     IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::DeleteExtensionL() start") );
       
   486     // Set cursor position before deletion.
       
   487     // Deletion may cause editor size to change and if
       
   488     // cursor pos is not updated before that ETEXT 12 panic
       
   489     // is raised.
       
   490     aEditor.SetCursorPosL( aPos, EFalse );
       
   491     aEditor.RichText()->DeleteL( aPos, 1 );
       
   492 	aEditor.HandleTextChangedL();
       
   493 	// Ugly hack. Needed to keep alignment of
       
   494 	// cursor correct.
       
   495 	aEditor.SetCursorPosL( aPos, EFalse );
       
   496 	
       
   497 	// update editors length, it grows by 2 when icon is deleted
       
   498 	TInt textLimit = aEditor.MaxLength() + 2;		
       
   499 	aEditor.SetTextLimit( textLimit );
       
   500 	
       
   501 	--iCurrentCount;
       
   502 	IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::DeleteExtensionL() end") );
       
   503 	}
       
   504 
       
   505 // -----------------------------------------------------------------------------
       
   506 // CIMCVAppSmileyUtil::Reset
       
   507 // Reset extension
       
   508 // -----------------------------------------------------------------------------
       
   509 //
       
   510 void CIMCVAppSmileyUtil::Reset()
       
   511     {
       
   512     iCurrentCount = 0;
       
   513     }
       
   514 
       
   515 // -----------------------------------------------------------------------------
       
   516 // CIMCVAppSmileyUtil::SizeChanged
       
   517 // Reset extension
       
   518 // -----------------------------------------------------------------------------
       
   519 //
       
   520 void CIMCVAppSmileyUtil::SizeChanged( TSize& aSize )
       
   521     {
       
   522     IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::SizeChanged() start") );
       
   523     iSmileUtil->ResizeIcons( aSize );
       
   524     IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::SizeChanged() end") );
       
   525     }
       
   526 
       
   527 // -----------------------------------------------------------------------------
       
   528 // CIMCVAppSmileyUtil::ConvertSelectionToStringL
       
   529 // Convert selection smileys to strings
       
   530 // -----------------------------------------------------------------------------
       
   531 //
       
   532 void CIMCVAppSmileyUtil::ConvertSelectionToStringL(
       
   533     CEikRichTextEditor& aEditor,
       
   534     TDes& aResultString,
       
   535     TCursorSelection& aSelectionAfterConversion,
       
   536     TBool aPreserve )
       
   537     {
       
   538     IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::ConvertSelectionToStringL() start") );
       
   539     // Clear convert array
       
   540     iConvertArray.ResetAndDestroy();
       
   541 	
       
   542     // Get text from editor
       
   543     HBufC* text = HBufC::NewMaxLC( KDefaultMsgLength );
       
   544     TPtr textCopy( text->Des() );
       
   545     aEditor.GetText( textCopy );
       
   546 
       
   547 	// For loop gets icon positions to array
       
   548     TInt start = aEditor.Selection().LowerPos();
       
   549     TInt end = aEditor.Selection().HigherPos();
       
   550 	
       
   551 	// Copy text to result string
       
   552 	aResultString.Append( textCopy );
       
   553 	
       
   554 	TInt i = 0;
       
   555     for( i = start; i < end; ++i )
       
   556         {
       
   557 		if ( textCopy[i] ==  CEditableText::EPictureCharacter )
       
   558 			{
       
   559             CIMCVAppSmileyInformation* smileInfo = CIMCVAppSmileyInformation::NewL(); 
       
   560             CleanupStack::PushL( smileInfo );
       
   561             smileInfo->SetPosition( i );
       
   562             smileInfo->SetIcon( ETrue );
       
   563 			User::LeaveIfError( iConvertArray.Append( smileInfo ) );
       
   564 			CleanupStack::Pop( smileInfo );
       
   565 			}	
       
   566         }
       
   567 
       
   568     TInt count = iConvertArray.Count();
       
   569 	for( i = count - 1; i >= 0; --i )
       
   570 		{
       
   571 		CPicture* editorPic = NULL;
       
   572 		
       
   573 		editorPic = aEditor.RichText()->PictureHandleL( 
       
   574                 iConvertArray[i]->Position(), MLayDoc::EForceLoadTrue );
       
   575         
       
   576 		if ( !editorPic )
       
   577 			{
       
   578 			User::Leave( KErrBadHandle );
       
   579 			}
       
   580 
       
   581 		CIMCVAppPicture* icon = static_cast<CIMCVAppPicture*> ( editorPic );
       
   582 
       
   583         const TDesC& iconString = iSmileUtil->GetSmileString( icon->Index() );
       
   584         iConvertArray[i]->SetSmileyStringL( iconString );
       
   585         iConvertArray[i]->SetIndex( icon->Index() );
       
   586 		}
       
   587 
       
   588     textCopy.Delete( end, textCopy.Length() - end );
       
   589     textCopy.Delete( 0, start );
       
   590     iSmileUtil->SearchSmilesL( textCopy, iConvertArray, NULL, start );
       
   591 
       
   592     // Change icons to strings
       
   593     TInt selectAdd = 0;
       
   594     count = iConvertArray.Count();
       
   595     for( i = count - 1; i >= 0; --i )
       
   596         {
       
   597         if( iConvertArray[ i ]->IsIcon() ) // Icon
       
   598             {
       
   599             // Append smiley string to result
       
   600             aResultString.Delete( iConvertArray[i]->Position(), 1 );
       
   601             aResultString.Insert( iConvertArray[i]->Position(), iConvertArray[i]->SmileyString() );
       
   602             selectAdd += iConvertArray[ i ]->SmileyString().Length() - 1;
       
   603             }
       
   604         }
       
   605 
       
   606     if( !aPreserve )
       
   607         {
       
   608 	    iConvertArray.ResetAndDestroy();
       
   609         }
       
   610     
       
   611     // Set correct selection
       
   612     aSelectionAfterConversion.SetSelection( end + selectAdd, start );
       
   613         
       
   614     CleanupStack::PopAndDestroy( text );
       
   615     IM_CV_LOGS(TXT("CIMCVAppSmileyUtil::ConvertSelectionToStringL() end") );
       
   616     }
       
   617 // -----------------------------------------------------------------------------
       
   618 // CIMCVAppSmileyUtil::ServiceId
       
   619 // Convert selection smileys to strings
       
   620 // -----------------------------------------------------------------------------
       
   621 //
       
   622 TInt CIMCVAppSmileyUtil::ServiceId() const
       
   623     {
       
   624     return iServiceId;
       
   625     }
       
   626         
       
   627 // End of File