imstutils/imconversationview/imcvuiapp/src/cimcvappsmileiconutility.cpp
branchRCL_3
changeset 29 9a48e301e94b
child 34 e7f34e614544
equal deleted inserted replaced
28:3104fc151679 29:9a48e301e94b
       
     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 
       
    20 // INCLUDE FILES
       
    21 #include "cimcvappsmileiconutility.h"
       
    22 #include "cimcvappsmileicondialog.h"
       
    23 #include "cimcvappsmilestring.h"
       
    24 #include "cimcvappsmileyinformation.h"
       
    25 #include "mimcvuiappskinvariant.h"
       
    26 #include "cimcvengine.h"
       
    27 #include    "cimcvappbitmapmapping.h"     // defines the skinned bitmap ids
       
    28 
       
    29 #include 	"mimcvappui.h"
       
    30 #include 	<uniutils.rsg>
       
    31 #include 	<msgeditorappui.rsg>
       
    32 #include 	<imcvuiapp.rsg>
       
    33 #include 	<bautils.h>
       
    34 #include 	<PUAcodes.hrh>
       
    35 #include 	<barsread.h> // RResourceReader
       
    36 #include 	<coemain.h>
       
    37 #include 	<eikenv.h>
       
    38 #include 	<gulicon.h>
       
    39 
       
    40 #include	<aknappui.h>
       
    41 #include	<AknsSkinInstance.h>	// skins 
       
    42 #include	<AknsUtils.h>
       
    43 #include	<AknsDrawUtils.h>
       
    44 #include	<AknsBasicBackgroundControlContext.h>
       
    45 #include    <AknIconUtils.h>
       
    46 #include 	"imcvlogger.h"
       
    47 #include    <mbsaccess.h>
       
    48 #include    "cimcvuiappiconfileprovider.h"
       
    49 #include    "conversations.h"
       
    50 #include    "imcvuiapp.h"
       
    51 #include 	<utf.h>
       
    52 
       
    53 
       
    54 // ================= MEMBER FUNCTIONS =======================
       
    55 
       
    56 // Two-phased constructor.
       
    57 CIMCVAppSmileIconUtility* CIMCVAppSmileIconUtility::NewL(MIMCVUiAppSkinVariant& aSkinVariant,
       
    58                              MIMCVAppUi& aAppUi,
       
    59                              CIMCVEngine& aServiceEngine  )
       
    60     {
       
    61     IM_CV_LOGS(TXT("CIMCVAppSmileIconUtility::NewL() start") );
       
    62     CIMCVAppSmileIconUtility* self = new (ELeave) CIMCVAppSmileIconUtility(aSkinVariant, aAppUi);
       
    63     CleanupStack::PushL( self );
       
    64     self->ConstructL(aServiceEngine);
       
    65     CleanupStack::Pop( self );
       
    66     IM_CV_LOGS(TXT("CIMCVAppSmileIconUtility::NewL() end") );
       
    67     return self;
       
    68     }
       
    69 
       
    70 // Destructor
       
    71 CIMCVAppSmileIconUtility::~CIMCVAppSmileIconUtility()
       
    72     {
       
    73     iAppUi.RemoveResourceChangeObserver( this );  
       
    74 	iStringArray.ResetAndDestroy();
       
    75 	iSmileArray.ResetAndDestroy();
       
    76     iIconToSmileyPointers.Close();   
       
    77     iSmileDlgIconArray.ResetAndDestroy();
       
    78     iFile.Close();
       
    79     }
       
    80 
       
    81 // C++ constructor can NOT contain any code, that
       
    82 // might leave.
       
    83 //
       
    84 CIMCVAppSmileIconUtility::CIMCVAppSmileIconUtility(MIMCVUiAppSkinVariant& aSkinVariant, 
       
    85                              MIMCVAppUi& aAppUi )
       
    86     :iSkinVariant( aSkinVariant),                       
       
    87     iAppUi( aAppUi )
       
    88     {
       
    89     }
       
    90 
       
    91 // ConstructL
       
    92 //
       
    93 void CIMCVAppSmileIconUtility::ConstructL(CIMCVEngine& aServiceEngine )
       
    94     {
       
    95     IM_CV_LOGS(TXT("CIMCVAppSmileIconUtility::ConstructL() start") );
       
    96 	// we need to be resource aware
       
    97 	iAppUi.AddResourceChangeObserver( this );
       
    98 	ConstructSmileDialogIconArrayL( aServiceEngine );
       
    99     IM_CV_LOGS(TXT("CIMCVAppSmileIconUtility::ConstructL() end") );
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------
       
   103 // CIMCVAppSmileIconUtility::ParseAllSmileyStringL
       
   104 // (other items were commented in a header).
       
   105 // ---------------------------------------------------------
       
   106 // 
       
   107 void CIMCVAppSmileIconUtility::ParseAllSmileyStringL(RPointerArray<HBufC> aAllSmileyString)
       
   108     {       
       
   109     TInt iconCount  = aAllSmileyString.Count();
       
   110     //make sure everything is fresh
       
   111     iStringArray.ResetAndDestroy();
       
   112     for(TInt i=0; i<iconCount; i++)
       
   113         {
       
   114         CIMCVAppSmileString* smileArray = CIMCVAppSmileString::NewL();
       
   115         CleanupStack::PushL( smileArray );
       
   116      
       
   117         HBufC* smileString = aAllSmileyString[i];
       
   118         //set longest smiley lenth
       
   119         if( smileString->Length() > iLongestSmileyLength )
       
   120             {
       
   121             iLongestSmileyLength = smileString->Length();
       
   122             }
       
   123         // ownership is transferred
       
   124         smileArray->Set( smileString, i );
       
   125         User::LeaveIfError( iStringArray.Append( smileArray ) );            
       
   126         CleanupStack::Pop( smileArray );
       
   127         }    
       
   128     }
       
   129 // ---------------------------------------------------------
       
   130 // CIMCVAppSmileIconUtility::ConstructSmileDialogIconArrayL
       
   131 // (other items were commented in a header).
       
   132 // ---------------------------------------------------------
       
   133 //
       
   134 void CIMCVAppSmileIconUtility::ConstructSmileDialogIconArrayL(CIMCVEngine& aServiceEngine )
       
   135     {   
       
   136     RPointerArray<HBufC> smileyString = aServiceEngine.GetSmileStringArray(); 
       
   137     CleanupClosePushL( smileyString );
       
   138     ParseAllSmileyStringL(smileyString); // all object ownership transfered
       
   139     smileyString.Reset();
       
   140     CleanupStack::Pop( &smileyString ); // no objects alive
       
   141     
       
   142     iSmileDlgIconArray = aServiceEngine.GetSmileyIconArray();
       
   143  
       
   144     // Sort smileys according to their string length, so that searching
       
   145     // for smileys is done from the longest smiley to the shortest. This
       
   146     // prevents the chat from detecting smaller smileys inside the big ones.
       
   147     TLinearOrder< CIMCVAppSmileString > 
       
   148     order( CIMCVAppSmileIconUtility::CompareSmileyLengthReversed );
       
   149     iStringArray.Sort( order );
       
   150     // Precalculate an ref table from icon to smiley.        
       
   151     iIconToSmileyPointers.Reset();   
       
   152     TInt count = iSmileDlgIconArray.Count();
       
   153     for( TInt i = 0; i < count; ++i )
       
   154         {
       
   155         User::LeaveIfError( 
       
   156                 iIconToSmileyPointers.Append( GetFirstSmileyIndex( i ) ) );        
       
   157         }  
       
   158     }    
       
   159 
       
   160 // ---------------------------------------------------------
       
   161 // CIMCVAppSmileIconUtility::LaunchSmileIconDialogL
       
   162 // (other items were commented in a header).
       
   163 // ---------------------------------------------------------
       
   164 //code scaner warning can be ignored since ExecuteLD is leaving and we need to return dialogVal.
       
   165 TInt CIMCVAppSmileIconUtility::LaunchSmileIconDialogL( TInt& aSelectedIconId )
       
   166     {
       
   167     if( iSmileDlgIconArray.Count() <= 0 )
       
   168         {
       
   169         User::Leave(KErrArgument);
       
   170         }
       
   171 	CIMCVAppSmileIconDialog* dialog = CIMCVAppSmileIconDialog::NewL( iSmileDlgIconArray,
       
   172 														   aSelectedIconId );
       
   173 	TInt dialogVal = dialog->ExecuteLD( R_SELECT_SMILE_DIALOG );
       
   174 	IM_CV_LOGS(TXT("CIMCVAppSmileIconUtility::LaunchSmileIconDialogL() end") );
       
   175 	return dialogVal;
       
   176     }
       
   177 
       
   178 // ---------------------------------------------------------
       
   179 // CIMCVAppSmileIconUtility::SmileIconCount
       
   180 // (other items were commented in a header).
       
   181 // ---------------------------------------------------------
       
   182 //
       
   183 TInt CIMCVAppSmileIconUtility::SmileIconCount() const
       
   184 	{
       
   185 	return iSmileDlgIconArray.Count();
       
   186 	}
       
   187 
       
   188 // ---------------------------------------------------------
       
   189 // CIMCVAppSmileIconUtility::GetSmileIcon
       
   190 // (other items were commented in a header).
       
   191 // ---------------------------------------------------------
       
   192 //
       
   193 const CGulIcon* CIMCVAppSmileIconUtility::GetSmileIcon( TInt aIconId ) const
       
   194 	{
       
   195 	return iSmileDlgIconArray[ aIconId ];
       
   196 	}
       
   197 
       
   198 // ---------------------------------------------------------
       
   199 // CIMCVAppSmileIconUtility::GetSmileString
       
   200 // (other items were commented in a header).
       
   201 // ---------------------------------------------------------
       
   202 //
       
   203 const TDesC& CIMCVAppSmileIconUtility::GetSmileString( TInt aIconId ) const
       
   204 	{
       
   205 	return iStringArray[ iIconToSmileyPointers[ aIconId ] ]->SmileString();
       
   206 	}
       
   207 
       
   208 // ---------------------------------------------------------
       
   209 // CIMCVAppSmileIconUtility::SearchSmilesL
       
   210 // (other items were commented in a header).
       
   211 // ---------------------------------------------------------
       
   212 //
       
   213 void CIMCVAppSmileIconUtility::SearchSmilesL( const TDesC& aStr, TDes& aFixedStr,
       
   214 										 RArray<TInt>* aArray )
       
   215 	{
       
   216 	CleanupClosePushL(*aArray);
       
   217 	
       
   218 	IM_CV_LOGS(TXT("CIMCVAppSmileIconUtility::SearchSmilesL() start") );
       
   219 	TInt i;
       
   220     iSmileArray.ResetAndDestroy();
       
   221     SearchSmilesL( aStr, iSmileArray, &aFixedStr );
       
   222 
       
   223 	aArray->Reset();
       
   224     TInt arrayCount( iSmileArray.Count() );
       
   225 
       
   226     for ( i = 0; i < arrayCount; ++i )
       
   227 		{
       
   228 		User::LeaveIfError( aArray->Append( iSmileArray[ i ]->Index() ) );
       
   229 		}
       
   230     
       
   231 	iSmileArray.ResetAndDestroy();
       
   232 	
       
   233 	CleanupStack::Pop(aArray);
       
   234 	
       
   235 	IM_CV_LOGS(TXT("CIMCVAppSmileIconUtility::SearchSmilesL() end") );
       
   236 	}
       
   237 
       
   238 // -----------------------------------------------------------------------------
       
   239 // CIMCVAppSmileIconUtility::SearchSmilesL
       
   240 // Search smileys from string and sort them in linear order.
       
   241 // -----------------------------------------------------------------------------
       
   242 //
       
   243 void CIMCVAppSmileIconUtility::SearchSmilesL( const TDesC& aStr, 
       
   244                          RPointerArray< CIMCVAppSmileyInformation >& aSmileyInfoArray, 
       
   245                          TDes* aFixedStr /*= NULL */,
       
   246                          TInt aStartPos /* = 0 */) const
       
   247     {
       
   248 	CleanupClosePushL(aSmileyInfoArray);
       
   249 	
       
   250 	IM_CV_LOGS(TXT("CIMCVAppSmileIconUtility::SearchSmilesL() start") );
       
   251 	HBufC* text = HBufC::NewMaxLC( aStr.Length() );
       
   252 	TPtr fixedText( text->Des() );
       
   253 	fixedText.Copy( aStr.Left(fixedText.MaxLength()) );
       
   254     
       
   255     HBufC* replaceString = HBufC::NewMaxLC( iLongestSmileyLength );
       
   256 	TPtr replace( replaceString->Des() );
       
   257 	if ( aFixedStr )
       
   258 		{
       
   259 		replace.Fill( KPuaCodeSmileyIconPadding, 1 );
       
   260 		}
       
   261 	else
       
   262 		{
       
   263 		//we dont need fixed, but we do need correct positions
       
   264 		replace.Fill( KPuaCodeSmileyIconPadding, iLongestSmileyLength );
       
   265 		}
       
   266 
       
   267     // order to sort array by the position
       
   268 	TLinearOrder< CIMCVAppSmileyInformation > order( CIMCVAppSmileIconUtility::Compare );
       
   269 
       
   270 	// for i-loop goes throug every smile string
       
   271 	// while-loop searches smiles from given string
       
   272     TInt arrayCount = iStringArray.Count(); 
       
   273 	TInt index = 0;
       
   274 	for ( TInt i = 0; i < arrayCount; ++i )
       
   275 		{
       
   276 		const TDesC& smileString( iStringArray[ i ]->SmileString() );
       
   277 		index = fixedText.Find( smileString );
       
   278 		while ( index != KErrNotFound )
       
   279 			{
       
   280             CIMCVAppSmileyInformation* smileInfo = CIMCVAppSmileyInformation::NewL();
       
   281             CleanupStack::PushL( smileInfo );
       
   282             
       
   283 			smileInfo->SetPosition( index + aStartPos );
       
   284 			smileInfo->SetIndex( iStringArray[ i ]->SmileIconIndex() );
       
   285             smileInfo->SetSmileyStringL( smileString );
       
   286             smileInfo->SetIcon( EFalse );
       
   287             
       
   288 			User::LeaveIfError( aSmileyInfoArray.Append( smileInfo ) );
       
   289             CleanupStack::Pop( smileInfo );
       
   290             
       
   291 			TInt smilyLength = smileString.Length();
       
   292             // Only replace the same amount of chars than in smiley, so we 
       
   293             // don't mess up the length or index table.
       
   294             fixedText.Replace( index, 
       
   295                                smilyLength, 
       
   296                                replace.Left( smilyLength ) );
       
   297 			index = fixedText.Find( smileString );
       
   298 			} // while
       
   299 		} // for i
       
   300     
       
   301 	aSmileyInfoArray.Sort( order );
       
   302     if( aFixedStr )
       
   303         {
       
   304         *aFixedStr = fixedText;
       
   305         }
       
   306         
       
   307     CleanupStack::PopAndDestroy( 2, text ); // replaceString, text
       
   308     CleanupStack::Pop(&aSmileyInfoArray);
       
   309     
       
   310 	IM_CV_LOGS(TXT("CIMCVAppSmileIconUtility::SearchSmilesL() end") );
       
   311     }
       
   312 
       
   313 // ---------------------------------------------------------
       
   314 // CIMCVAppSmileIconUtility::Compare
       
   315 // (other items were commented in a header).
       
   316 // ---------------------------------------------------------
       
   317 //
       
   318 TInt CIMCVAppSmileIconUtility::Compare( const CIMCVAppSmileyInformation& aFirst,
       
   319                                    const CIMCVAppSmileyInformation& aSecond )
       
   320     {
       
   321     return ( aFirst.Position() - aSecond.Position() );
       
   322     }
       
   323 
       
   324 // -----------------------------------------------------------------------------
       
   325 // CIMCVAppSmileIconUtility::CompareSmileyLengthReversed
       
   326 // (other items were commented in a header).
       
   327 // -----------------------------------------------------------------------------
       
   328 //
       
   329 TInt CIMCVAppSmileIconUtility::CompareSmileyLengthReversed( 
       
   330                                             const CIMCVAppSmileString& aFirst,
       
   331                                             const CIMCVAppSmileString& aSecond )
       
   332     {
       
   333     return ( aSecond.SmileString().Length() - aFirst.SmileString().Length() );
       
   334     }
       
   335 
       
   336 // ---------------------------------------------------------
       
   337 // CIMCVAppSmileIconUtility::ResizeIcons
       
   338 // (other items were commented in a header).
       
   339 // ---------------------------------------------------------
       
   340 //
       
   341 void CIMCVAppSmileIconUtility::ResizeIcons( const TSize& aSize )
       
   342 	{
       
   343 	IM_CV_LOGS(TXT("CIMCVAppSmileIconUtility::ResizeIcons() start") );
       
   344 	TInt count( SmileIconCount() );
       
   345 	for( TInt i(0); i < count; i++) 
       
   346 		{
       
   347 		AknIconUtils::SetSize( iSmileDlgIconArray[ i ]->Bitmap(), aSize );
       
   348 		}
       
   349 	IM_CV_LOGS(TXT("CIMCVAppSmileIconUtility::ResizeIcons() end") );
       
   350 	}
       
   351 
       
   352 // -----------------------------------------------------------------------------
       
   353 // CIMCVAppSmileIconUtility::GetFirstSmileyIndex
       
   354 // -----------------------------------------------------------------------------
       
   355 //
       
   356 TInt CIMCVAppSmileIconUtility::GetFirstSmileyIndex( TInt aIconIndex ) const
       
   357     {
       
   358     IM_CV_LOGS(TXT("CIMCVAppSmileIconUtility::GetFirstSmileyIndex() start") );
       
   359 	TInt count = iStringArray.Count();
       
   360 	
       
   361 	for( TInt i = 0; i < count; ++i )
       
   362 	    {
       
   363 	    if( iStringArray[ i ]->SmileIconIndex() == aIconIndex )
       
   364 	        {
       
   365 	        return i;
       
   366 	        }
       
   367 	    }
       
   368 	IM_CV_LOGS(TXT("CIMCVAppSmileIconUtility::GetFirstSmileyIndex() end") );
       
   369 	// Icon was not found.	
       
   370 	return KErrNotFound;
       
   371     }
       
   372 
       
   373 // -----------------------------------------------------------------------------
       
   374 // CIMCVAppSmileIconUtility::ResourceChangedL
       
   375 // -----------------------------------------------------------------------------
       
   376 //
       
   377 void CIMCVAppSmileIconUtility::ResourceChangedL()
       
   378     {
       
   379     IM_CV_LOGS(TXT("CIMCVAppSmileIconUtility::ResourceChangedL() start") );		
       
   380 	IM_CV_LOGS(TXT("CIMCVAppSmileIconUtility::ResourceChangedL() end") );
       
   381     }
       
   382 
       
   383 
       
   384 // End of File
       
   385