uifw/EikStd/coctlsrc/smileymodel.cpp
changeset 0 2f259fa3e83a
child 15 c52421ed5f07
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 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:  smiely engine class
       
    15 *
       
    16 */
       
    17 
       
    18 //#include <streamlogger.h>
       
    19 
       
    20 #include "smileymanager.h"
       
    21 #include "smileymodel.h"
       
    22 
       
    23 const TText KSpace = ' ';
       
    24 const TInt KLinkIndexMark = 0x8000;
       
    25 
       
    26 // ======== MEMBER FUNCTIONS ========
       
    27 // ---------------------------------------------------------------------------
       
    28 // CSmileyInfo::CSmileyInfo
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 CSmileyInfo::CSmileyInfo()
       
    32     {    
       
    33     }
       
    34 
       
    35 CSmileyInfo::~CSmileyInfo()
       
    36     {
       
    37     iStrArray.Reset();
       
    38     iStrArray.Close();
       
    39     }
       
    40 
       
    41 void CSmileyInfo::Reset()
       
    42     {
       
    43     iImageInfo.Reset();
       
    44     iStrArray.Reset();
       
    45     }
       
    46 
       
    47 void CSmileyInfo::SetSmileyText( const TDesC& aText )
       
    48     {
       
    49     TInt startIndex( KErrNotFound );
       
    50     for ( TInt i( 0 ); i <= aText.Length(); i++ )
       
    51         {
       
    52         if ( ( i == aText.Length() || aText[i] == KSpace ) && 
       
    53             startIndex != KErrNotFound )
       
    54             {
       
    55             iStrArray.Append( aText.Mid( startIndex, i - startIndex ) );
       
    56             startIndex = KErrNotFound;
       
    57             }
       
    58         if ( startIndex == KErrNotFound && i < aText.Length() && 
       
    59             aText[i] != KSpace )
       
    60             {
       
    61             startIndex = i;
       
    62             }
       
    63         }
       
    64     }
       
    65     
       
    66 // ======== MEMBER FUNCTIONS ========
       
    67 // ---------------------------------------------------------------------------
       
    68 // TImageInfo::TImageInfo
       
    69 // ---------------------------------------------------------------------------
       
    70 //
       
    71 TImageInfo::TImageInfo() : iCode( 0 ), iBmpId( 0 ), iMaskId( 0 ),
       
    72     iIsAnimation( EFalse ), iStaticBmpId( 0 ), iStaticMaskId( 0 )
       
    73     {    
       
    74     }
       
    75 
       
    76 void TImageInfo::Reset()
       
    77     {
       
    78     iCode = iBmpId = iMaskId = iStaticBmpId = iStaticMaskId = 0;
       
    79     iIsAnimation = EFalse;
       
    80     }
       
    81 
       
    82 // ======== MEMBER FUNCTIONS ========
       
    83 // ---------------------------------------------------------------------------
       
    84 // CSmileyModel::TLinkToImage::TLinkToImage
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 CSmileyModel::TLinkToImage::TLinkToImage() : iStrLength( 0 ),
       
    88     iStrNode( KInvalidIndex ), iImageIndex( KInvalidIndex )
       
    89     {    
       
    90     }
       
    91     
       
    92 // ======== MEMBER FUNCTIONS ========
       
    93 // ---------------------------------------------------------------------------
       
    94 // CSmileyModel::TStrNode::TStrNode
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 CSmileyModel::TStrNode::TStrNode() : iCode(0), iSibling( KInvalidIndex ), 
       
    98     iChild( KInvalidIndex ), iParent( KInvalidIndex )
       
    99     {    
       
   100     }
       
   101 
       
   102 // ======== MEMBER FUNCTIONS ========
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 // CSmileyModel::CSmileyModel
       
   106 // ---------------------------------------------------------------------------
       
   107 //
       
   108 CSmileyModel::CSmileyModel()    
       
   109     {    
       
   110     }
       
   111     
       
   112 // ---------------------------------------------------------------------------
       
   113 // CSmileyModel::~CSmileyModel
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 CSmileyModel::~CSmileyModel()
       
   117     {
       
   118     Reset();
       
   119     iStrArray.Close();
       
   120     iImageInfoArray.Close();
       
   121     iLinkArray.Close();
       
   122     delete iFileName;  
       
   123     }
       
   124 
       
   125 // ---------------------------------------------------------------------------
       
   126 // CSmileyModel::Reset
       
   127 // ---------------------------------------------------------------------------
       
   128 //
       
   129 void CSmileyModel::Reset()
       
   130     {
       
   131     iStrArray.Reset();
       
   132     iStrArray.Compress();
       
   133     iImageInfoArray.Reset();
       
   134     iImageInfoArray.Compress();
       
   135     iLinkArray.Reset();
       
   136     iLinkArray.Compress();
       
   137     delete iFileName;
       
   138     iFileName = NULL;
       
   139     }
       
   140 
       
   141 // ---------------------------------------------------------------------------
       
   142 // CSmileyModel::SetSmileyIconFileL
       
   143 // ---------------------------------------------------------------------------
       
   144 // 
       
   145 void CSmileyModel::SetSmileyIconFileL( const TDesC& aIconFileName )
       
   146     {
       
   147     if ( aIconFileName.Length() > 0 )
       
   148         {
       
   149         delete iFileName;
       
   150         iFileName = NULL;
       
   151         iFileName = HBufC::NewL( aIconFileName.Length() );
       
   152         TPtr ptr( iFileName->Des() );
       
   153         ptr.Copy( aIconFileName );
       
   154         }
       
   155     }
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // CSmileyModel::SupportedCodesL
       
   159 // ---------------------------------------------------------------------------
       
   160 //    
       
   161 HBufC* CSmileyModel::SupportedCodesL()
       
   162     {
       
   163     HBufC* codes( NULL );
       
   164     if ( iImageInfoArray.Count() > 0 )
       
   165         {
       
   166         codes = HBufC::NewL( iImageInfoArray.Count() );
       
   167         TPtr ptr( codes->Des() );
       
   168         for ( TInt i = 0; i < iImageInfoArray.Count(); i++ )
       
   169             {
       
   170             if ( ptr.Locate( iImageInfoArray[i].iCode ) == KErrNotFound )
       
   171                 {
       
   172                 ptr.Append( iImageInfoArray[i].iCode );                                
       
   173                 }
       
   174             }
       
   175         }
       
   176     return codes;
       
   177     }
       
   178 
       
   179 // ---------------------------------------------------------------------------
       
   180 // CSmileyModel::GetImageInfo
       
   181 // ---------------------------------------------------------------------------
       
   182 //
       
   183 void CSmileyModel::GetImageInfo( TImageInfo& aImageInfo, HBufC** aFileName )
       
   184     {
       
   185     TInt count( iImageInfoArray.Count() );
       
   186     if ( count == 0 )
       
   187         {
       
   188         aFileName = NULL;
       
   189         aImageInfo.Reset();
       
   190         return;
       
   191         }
       
   192     for ( TInt i = 0; i < count; i++ )
       
   193         {
       
   194         if ( iImageInfoArray[i].iCode == aImageInfo.iCode )
       
   195             {
       
   196             aImageInfo = iImageInfoArray[i];
       
   197             *aFileName = iFileName;
       
   198             }
       
   199         }
       
   200     }
       
   201 
       
   202 // ---------------------------------------------------------------------------
       
   203 // CSmileyModel::AddSimleyL
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 void CSmileyModel::AddSimleyL( CSmileyInfo& aInfo )
       
   207     {
       
   208     if ( aInfo.iImageInfo.iCode < CSmileyManager::KSmileyCodeMin || 
       
   209         aInfo.iImageInfo.iCode > CSmileyManager::KSmileyCodeMax || 
       
   210         aInfo.iStrArray.Count() == 0 )
       
   211         {
       
   212         return;
       
   213         }
       
   214     for ( TInt i = 0; i < aInfo.iStrArray.Count(); i++ )
       
   215         {
       
   216         TInt imageIndex = AddLinkAndImageInfoL( aInfo, i );
       
   217         AddStringL( aInfo, imageIndex, i );
       
   218         }    
       
   219     }
       
   220 
       
   221 // ---------------------------------------------------------------------------
       
   222 // CSmileyModel::AddLinkAndImageInfoL
       
   223 // ---------------------------------------------------------------------------
       
   224 //
       
   225 TInt CSmileyModel::AddLinkAndImageInfoL( CSmileyInfo& aInfo, TInt aStrIndex )
       
   226     {
       
   227     TLinkToImage link;
       
   228     for ( TInt i( 0 ); i < iImageInfoArray.Count(); i++ )
       
   229         {
       
   230         if ( iImageInfoArray[i].iCode == aInfo.iImageInfo.iCode )
       
   231             {
       
   232             link.iImageIndex = i;
       
   233             break;
       
   234             }
       
   235         }
       
   236     if ( link.iImageIndex == KInvalidIndex )
       
   237         {
       
   238         link.iImageIndex = iImageInfoArray.Count();
       
   239         iImageInfoArray.Append( aInfo.iImageInfo );
       
   240         }
       
   241     link.iStrLength = aInfo.iStrArray[aStrIndex].Length();
       
   242     iLinkArray.Append( link );
       
   243     return ( iLinkArray.Count() - 1 );
       
   244     }
       
   245 
       
   246 // ---------------------------------------------------------------------------
       
   247 // CSmileyModel::AddStringL
       
   248 // ---------------------------------------------------------------------------
       
   249 //
       
   250 void CSmileyModel::AddStringL( CSmileyInfo& aInfo, TInt aLinkIndex, 
       
   251     TInt aStrIndex )
       
   252     {       
       
   253     TInt index( KInvalidIndex );    
       
   254     for ( TInt i = 0; i < aInfo.iStrArray[aStrIndex].Length(); i++ )
       
   255         {        
       
   256         TStrNode str; 
       
   257         str.iCode = aInfo.iStrArray[aStrIndex][i];
       
   258         index = AddStrNodeL( index, str );                               
       
   259         }
       
   260     iStrArray[index].iChild = ( KLinkIndexMark | aLinkIndex );
       
   261     iLinkArray[aLinkIndex].iStrNode = index;    
       
   262     }
       
   263 
       
   264 // ---------------------------------------------------------------------------
       
   265 // CSmileyModel::AddStrNodeL
       
   266 // ---------------------------------------------------------------------------
       
   267 //
       
   268 TInt CSmileyModel::AddStrNodeL( TInt aIndex, TStrNode& aNode )
       
   269     {    
       
   270     TInt count = iStrArray.Count();
       
   271     if ( count > 0 )
       
   272         {        
       
   273         TInt child( aIndex == KInvalidIndex ? 0 : 
       
   274             iStrArray[aIndex].iChild );
       
   275         if ( child != KInvalidIndex )
       
   276             {            
       
   277             TInt index = FindInSibling( child, aNode.iCode );
       
   278             if ( index != KInvalidIndex )
       
   279                 {
       
   280                 return index;
       
   281                 }
       
   282             iStrArray[child].iSibling = count;            
       
   283             }
       
   284         else
       
   285             {
       
   286             iStrArray[aIndex].iChild = count;
       
   287             }
       
   288         }
       
   289     aNode.iParent = aIndex;
       
   290     iStrArray.Append( aNode );
       
   291     return count;
       
   292     }
       
   293 
       
   294 // ---------------------------------------------------------------------------
       
   295 // CSmileyModel::FindInSibling
       
   296 // ---------------------------------------------------------------------------
       
   297 //
       
   298 TInt CSmileyModel::FindInSibling( TInt& aIndex, TText aCode )
       
   299     {
       
   300     TInt ret( KInvalidIndex );
       
   301     if ( iStrArray.Count() > 0 )
       
   302         {
       
   303         TInt index( aIndex == KInvalidIndex ? 0 : aIndex );
       
   304         while ( index != KInvalidIndex )
       
   305             {
       
   306             if ( iStrArray[index].iCode == aCode )
       
   307                 {
       
   308                 ret = index;
       
   309                 break;
       
   310                 }
       
   311             aIndex = index;
       
   312             index = iStrArray[index].iSibling;
       
   313             }        
       
   314         }
       
   315     return ret;
       
   316     }
       
   317 
       
   318 // ---------------------------------------------------------------------------
       
   319 // CSmileyModel::FindInSibling
       
   320 // ---------------------------------------------------------------------------
       
   321 //
       
   322 TBool CSmileyModel::IsFinalNode( TInt aIndex )
       
   323     {
       
   324     return ( aIndex != KInvalidIndex && 
       
   325         ( iStrArray[aIndex].iChild & KLinkIndexMark ) != 0 );
       
   326     }
       
   327 
       
   328 // ---------------------------------------------------------------------------
       
   329 // CSmileyModel::FindMatchStr
       
   330 // ---------------------------------------------------------------------------
       
   331 //
       
   332 TInt CSmileyModel::TryFindMatchNode( const TDesC& aText, TInt aTextPos )
       
   333     {
       
   334     TInt index( 0 );
       
   335     index = FindInSibling( index, aText[aTextPos] );
       
   336     TInt textPos( aTextPos + 1 );
       
   337     while ( index != KInvalidIndex && !IsFinalNode( index ) && 
       
   338         textPos < aText.Length() )
       
   339         {            
       
   340         index = iStrArray[index].iChild;
       
   341         index = FindInSibling( index, aText[textPos] );
       
   342         if ( index != KInvalidIndex )
       
   343             {
       
   344             textPos++;
       
   345             }
       
   346         }
       
   347     return index;
       
   348     }
       
   349 
       
   350 // ---------------------------------------------------------------------------
       
   351 // CSmileyModel::SmileyStringLength
       
   352 // ---------------------------------------------------------------------------
       
   353 //
       
   354 TInt CSmileyModel::SmileyStringLength( TInt aNodeIndex )
       
   355     {
       
   356     if ( IsFinalNode( aNodeIndex ) )
       
   357         {
       
   358         TInt linkIndex( iStrArray[aNodeIndex].iChild & ~KLinkIndexMark );
       
   359         return iLinkArray[linkIndex].iStrLength;
       
   360         }
       
   361     return 0;
       
   362     }
       
   363 
       
   364 // ---------------------------------------------------------------------------
       
   365 // CSmileyModel::SmileyCode
       
   366 // ---------------------------------------------------------------------------
       
   367 //
       
   368 TText CSmileyModel::SmileyCode( TInt aNodeIndex )
       
   369     {
       
   370     TInt linkIndex( iStrArray[aNodeIndex].iChild & ~KLinkIndexMark );
       
   371     TInt imageIndex( iLinkArray[linkIndex].iImageIndex );
       
   372     return iImageInfoArray[imageIndex].iCode;
       
   373     }
       
   374 
       
   375 // ---------------------------------------------------------------------------
       
   376 // CSmileyModel::ReplaceTextWithCodes
       
   377 // ---------------------------------------------------------------------------
       
   378 //
       
   379 void CSmileyModel::ReplaceTextWithCodes( TDes& aText, TInt aDocPos, 
       
   380     TInt aNodeIndex )
       
   381     {
       
   382     TInt linkIndex( iStrArray[aNodeIndex].iChild & ~KLinkIndexMark );
       
   383     TInt len( iLinkArray[linkIndex].iStrLength );
       
   384     TInt imageIndex( iLinkArray[linkIndex].iImageIndex );
       
   385     aText[aDocPos] = iImageInfoArray[imageIndex].iCode;
       
   386     aText[aDocPos + 1] = CSmileyManager::KCompensateChar;
       
   387     const TInt KThirdIndex = 2;
       
   388     for ( TInt i( KThirdIndex ); i < len; i++ )
       
   389         {
       
   390         aText[i + aDocPos] = CSmileyManager::KPlaceHolder;
       
   391         }
       
   392     }