voiceui/vcommand/src/uiarraysgenerator.cpp
changeset 13 57b735022c18
parent 1 b13cd05eeb2f
equal deleted inserted replaced
1:b13cd05eeb2f 13:57b735022c18
     1 /*
       
     2 * Copyright (c) 2007 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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include "uiarraysgenerator.h"
       
    22 
       
    23 #include "vcmodel.h"
       
    24 #include <badesca.h>
       
    25 #include <gulicon.h>
       
    26 #include <AknIconArray.h>
       
    27 #include <e32debug.h>
       
    28 #include <mmfcontrollerpluginresolver.h>  // for CleanupResetAndDestroyPushL
       
    29 
       
    30 
       
    31 /**
       
    32  * @param aIcon. Ownership is tranfered. I.e. the icon will be destroyed,
       
    33  *        when this object is destroyed, unless icon has been already removed by ReleaseIcon
       
    34  * @param aFirstLine
       
    35  * @param aSecondLine
       
    36  */
       
    37 CUiEntryData* CUiEntryData::NewL( CGulIcon* aIcon, const TDesC& aFirstLine,
       
    38                             const TDesC& aSecondLine )
       
    39     {
       
    40     CUiEntryData* self = NewLC( aIcon, aFirstLine, aSecondLine );
       
    41     CleanupStack::Pop( self );
       
    42     return self;
       
    43     }
       
    44 
       
    45 CUiEntryData* CUiEntryData::NewLC( CGulIcon* aIcon, const TDesC& aFirstLine,
       
    46                             const TDesC& aSecondLine )
       
    47     {
       
    48     CUiEntryData* self = new (ELeave) CUiEntryData( aIcon, aFirstLine, aSecondLine );
       
    49     CleanupStack::PushL( self );
       
    50     return self;
       
    51     }
       
    52 
       
    53 CUiEntryData::CUiEntryData( CGulIcon* aIcon, const TDesC& aFirstLine, 
       
    54                             const TDesC& aSecondLine ) :
       
    55     iIcon( aIcon), iFirstLine( aFirstLine ), iSecondLine( aSecondLine )
       
    56     {
       
    57     }
       
    58                             
       
    59 CUiEntryData::~CUiEntryData()
       
    60     {
       
    61     delete iIcon;
       
    62     }
       
    63     
       
    64 CGulIcon* CUiEntryData::ReleaseIcon()
       
    65     {
       
    66     CGulIcon* result = iIcon;
       
    67     iIcon = NULL;
       
    68     return result;
       
    69     }
       
    70         
       
    71 const TDesC& CUiEntryData::FirstLine() const
       
    72     {
       
    73     return iFirstLine;
       
    74     }
       
    75 
       
    76 const TDesC& CUiEntryData::SecondLine() const
       
    77     {
       
    78     return iSecondLine;
       
    79     }
       
    80     
       
    81 TInt CUiEntryData::CompareByFirstLine( const CUiEntryData& aAnotherEntry ) const
       
    82     {
       
    83     return iFirstLine.CompareC( aAnotherEntry.FirstLine() );
       
    84     }
       
    85     
       
    86 TInt CUiEntryData::CompareByFirstLine( const CUiEntryData& entry1, const CUiEntryData& entry2 )
       
    87     {
       
    88     return entry1.CompareByFirstLine( entry2 );
       
    89     }
       
    90     
       
    91 TDesC* CUiEntryData::ConstructListboxItemStringLC( TInt aIconIndex ) const
       
    92     {
       
    93     _LIT( KListboxStringPattern, "%d\t%S\t%S");
       
    94     const TInt KPatternOverheadLength = 2; // two characters for tabs
       
    95     const TInt KMaxIconIndexLength = 5;  // 5 characters for the decimal number represenation
       
    96     HBufC* result = HBufC::NewLC( KPatternOverheadLength + KMaxIconIndexLength +
       
    97                     FirstLine().Length() + SecondLine().Length() );
       
    98     result->Des().Format( KListboxStringPattern, aIconIndex, &FirstLine(), &SecondLine() );
       
    99     return result;                    
       
   100     }
       
   101     
       
   102 CUiArraysGenerator* CUiArraysGenerator::NewLC()
       
   103 	{
       
   104 	CUiArraysGenerator* self = new (ELeave) CUiArraysGenerator;
       
   105 	CleanupStack::PushL( self );
       
   106 	return self;
       
   107 	}
       
   108 	
       
   109 /**
       
   110  * Create entries for all the subfolders in the given folder and sort the entries
       
   111  * alphabetically
       
   112  * @param aSource Model to parse
       
   113  * @param aCurrentFolderTitle KNullDesC for the main folder
       
   114  * @return The array of entries pushed to CS two times. First PopAndDestroy
       
   115  *         will ResetAndDestroy, the second one will delete the array itself
       
   116  */               
       
   117 RPointerArray<CUiEntryData>* CUiArraysGenerator::ExtractFoldersAndSortLC2( const CVCModel& aSource,
       
   118             const TDesC& aCurrentFolderTitle )
       
   119     {
       
   120     RPointerArray<CUiEntryData>* folderEntries = new ( ELeave ) RPointerArray<CUiEntryData>;
       
   121     CleanupStack::PushL( folderEntries );
       
   122     CleanupResetAndDestroyPushL( *folderEntries );
       
   123     if( aCurrentFolderTitle != KNullDesC() )
       
   124         {
       
   125         return folderEntries; // no multilevel folders
       
   126         }
       
   127         
       
   128     for( TInt i = 0; i < aSource.Count(); i++ )
       
   129         {
       
   130         if ( aSource.At(i).FolderTitle() != KNullDesC() )
       
   131             {
       
   132             CUiEntryData* entry = CUiEntryData::NewL( aSource.At(i).FolderIconLC(), 
       
   133                                         aSource.At(i).FolderListedName(), KNullDesC() );
       
   134             CleanupStack::Pop(); // icon
       
   135             CleanupStack::PushL( entry );
       
   136             TLinearOrder<CUiEntryData> order( CUiEntryData::CompareByFirstLine );
       
   137             // does not allow duplicates
       
   138             TInt err = folderEntries->InsertInOrder( entry, order );  
       
   139             if( err != KErrNone )
       
   140                 {
       
   141                 CleanupStack::PopAndDestroy( entry );
       
   142                 }
       
   143             else
       
   144                 {
       
   145                 CleanupStack::Pop( entry );
       
   146                 }
       
   147             }
       
   148         }
       
   149     return folderEntries;
       
   150     }
       
   151 
       
   152 /**
       
   153  * Create entries for all the subfolders in the given folder and sort the entries
       
   154  * alphabetically
       
   155  * @param aSource Model to parse
       
   156  * @param aCurrentFolderTitle KNullDesC for the main folder
       
   157  * @return The array of entries pushed to CS two times. First PopAndDestroy
       
   158  *         will ResetAndDestroy, the second one will delete the array itself
       
   159  */               
       
   160 RPointerArray<CUiEntryData>* CUiArraysGenerator::ExtractCommandsAndSortLC2( 
       
   161                 const CVCModel& aSource, const TDesC& aCurrentFolderTitle )
       
   162     {
       
   163     RPointerArray<CUiEntryData>* commandEntries = new ( ELeave ) RPointerArray<CUiEntryData>;
       
   164     CleanupStack::PushL( commandEntries );
       
   165     CleanupResetAndDestroyPushL( *commandEntries );
       
   166         
       
   167     for( TInt i = 0; i < aSource.Count(); i++ )
       
   168         {
       
   169         if ( aSource.At(i).FolderTitle() == aCurrentFolderTitle )
       
   170             {
       
   171             CUiEntryData* entry = CUiEntryData::NewL( aSource.At(i).IconLC(), 
       
   172                       aSource.At(i).WrittenText(), aSource.At(i).AlternativeSpokenText() );
       
   173             CleanupStack::Pop(); // icon
       
   174             CleanupStack::PushL( entry );
       
   175             TLinearOrder<CUiEntryData> order( CUiEntryData::CompareByFirstLine );
       
   176             commandEntries->InsertInOrderAllowRepeatsL( entry, order );  
       
   177             CleanupStack::Pop( entry );
       
   178             }
       
   179         }
       
   180     return commandEntries;
       
   181     }
       
   182             	
       
   183 /**
       
   184  * Parses the model into a set of arrays required by the VCommand app ui
       
   185  * I.e. basing on the "current folder" prepares the arrays for the listbox
       
   186  * If "current" folder is the main one, it means including all the sub-folders and main
       
   187  * folder commands. Otherwise, it means including all the current folder commands only
       
   188  * 
       
   189  * Both folders and commands are alphabetially sorted.
       
   190  * All the target arrays are ResetAndDestroyed before the parsing
       
   191  * 
       
   192  * @param aSource Model to parse
       
   193  * @param aCurrentFolderTitle KNullDesC if "curent" folder is the main one
       
   194  * @param aIconArray icons to display in the listbox. Order is not specified
       
   195  * @param aFolderTitles List of folder titles in the alphabetical order
       
   196  * @param aItemIsFolder List of boolean values for all the listbox elements from top 
       
   197  * 		  to bottom. ETrue for every folder, EFalse for every command
       
   198  * @param aItemArray List of strings in the listbox expected 
       
   199  * 		  format ( @see LB_SINGLE_GRAPHIC_HEADING ). Includes:
       
   200  * 		  * a refence to the icon - zero-based index from the aIconArray
       
   201  *        * written text of the command
       
   202  *        * user-specified alternative text or KNullDesC if
       
   203  * 		
       
   204  */
       
   205 void CUiArraysGenerator::FillArraysL( const CVCModel& aSource, const TDesC& aCurrentFolderTitle,
       
   206 				  CAknIconArray& aIconArray, CDesC16ArrayFlat& aFolderTitles, 
       
   207 				  RArray<TBool>& aItemIsFolder, CDesC16ArrayFlat& aItemArray )
       
   208 	{
       
   209 	aIconArray.ResetAndDestroy();
       
   210 	aFolderTitles.Reset();
       
   211 	aItemIsFolder.Reset();
       
   212 	aItemArray.Reset();
       
   213     // Get folder entries
       
   214 
       
   215     RPointerArray<CUiEntryData>* entries = 
       
   216             ExtractFoldersAndSortLC2( aSource, aCurrentFolderTitle );
       
   217 
       
   218     FillFolderTitlesL( aItemIsFolder, aFolderTitles, aSource, *entries );
       
   219 
       
   220     // get command entries            
       
   221     RPointerArray<CUiEntryData>* ordinaryCommands = 
       
   222             ExtractCommandsAndSortLC2( aSource, aCurrentFolderTitle );
       
   223     
       
   224     // append commands to folder list            
       
   225     while( ordinaryCommands->Count() > 0 )
       
   226         {
       
   227         entries->AppendL( (*ordinaryCommands)[0] );
       
   228         aItemIsFolder.AppendL( EFalse );
       
   229         ordinaryCommands->Remove( 0 );
       
   230         }
       
   231             
       
   232     CleanupStack::PopAndDestroy( ordinaryCommands );  // ResetAndDestroy
       
   233     CleanupStack::PopAndDestroy( ordinaryCommands );  // delete    
       
   234     
       
   235     for( TInt i = 0; i < entries->Count(); i++ )
       
   236         {
       
   237         aIconArray.AppendL( (*entries)[i]->ReleaseIcon() );
       
   238         TDesC* string = (*entries)[i]->ConstructListboxItemStringLC( i );
       
   239         aItemArray.AppendL( *string );
       
   240         CleanupStack::PopAndDestroy( string );
       
   241         }
       
   242     
       
   243     CleanupStack::PopAndDestroy( entries );  // ResetAndDestroy
       
   244     CleanupStack::PopAndDestroy( entries );  // delete    
       
   245 	}
       
   246 
       
   247 /**
       
   248  * Updates a set of arrays required by the VCommand app ui to match current folder names
       
   249  * I.e. basing on the "current folder" prepares the arrays for the listbox
       
   250  * If "current" folder is the main one, it means including all the sub-folders and main
       
   251  * folder commands. Otherwise, it means including all the current folder commands only
       
   252  * 
       
   253  * Both folders and commands are alphabetially sorted.
       
   254  * 
       
   255  * @param aSource Model to parse
       
   256  * @param aCurrentFolderTitle KNullDesC if "curent" folder is the main one
       
   257  * @param aIconArray icons to display in the listbox. Order is not specified
       
   258  * @param aFolderTitles List of folder titles in the alphabetical order
       
   259  * @param aItemIsFolder List of boolean values for all the listbox elements from top 
       
   260  * 		  to bottom. ETrue for every folder, EFalse for every command
       
   261  * @param aItemArray List of strings in the listbox expected 
       
   262  * 		  format ( @see LB_SINGLE_GRAPHIC_HEADING ). Includes:
       
   263  * 		  * a refence to the icon - zero-based index from the aIconArray
       
   264  *        * written text of the command
       
   265  *        * user-specified alternative text or KNullDesC if
       
   266  * 		
       
   267  */	
       
   268 void CUiArraysGenerator::UpdateFolderArraysL( const CVCModel& aSource, const TDesC& aCurrentFolderTitle,
       
   269 				  CAknIconArray& aIconArray, CDesC16ArrayFlat& aFolderTitles, 
       
   270 				  RArray<TBool>& aItemIsFolder, CDesC16ArrayFlat& aItemArray )
       
   271 	{
       
   272     // Get folder entries
       
   273     RPointerArray<CUiEntryData>* entries = 
       
   274             ExtractFoldersAndSortLC2( aSource, aCurrentFolderTitle );
       
   275 
       
   276     for( TInt i = aItemIsFolder.Count() - 1; i >= 0 ; i-- )
       
   277         {
       
   278         if ( aItemIsFolder[i] )
       
   279             {
       
   280             aFolderTitles.Delete(i);            
       
   281             aItemArray.Delete(i);            
       
   282             aItemIsFolder.Remove(i);
       
   283             }
       
   284         }
       
   285         
       
   286     for( TInt i = 0; i < entries->Count(); i++ )
       
   287         {
       
   288         aItemIsFolder.InsertL( ETrue, i );
       
   289         
       
   290         TBool titleForCurrentListedNameFound = EFalse;
       
   291         for( TInt j = 0; j < aSource.Count(); j++ )
       
   292             {
       
   293             if( aSource.At(j).FolderListedName() == (*entries)[i]->FirstLine() )
       
   294                 {
       
   295                 aFolderTitles.InsertL( i, aSource.At(j).FolderTitle() );
       
   296                 titleForCurrentListedNameFound = ETrue;
       
   297                 break;
       
   298                 }
       
   299             }
       
   300         if (!titleForCurrentListedNameFound)
       
   301             {
       
   302             User::Leave( KErrCorrupt );
       
   303             }
       
   304             
       
   305         aIconArray.AppendL( (*entries)[i]->ReleaseIcon() );
       
   306         
       
   307         TDesC* string = (*entries)[i]->ConstructListboxItemStringLC( aIconArray.Count() - 1 );
       
   308         aItemArray.InsertL( i, *string );
       
   309         CleanupStack::PopAndDestroy( string );
       
   310         }
       
   311     
       
   312     CleanupStack::PopAndDestroy( entries );  // ResetAndDestroy
       
   313     CleanupStack::PopAndDestroy( entries );  // delete    
       
   314 	}
       
   315     
       
   316 /**
       
   317  * 		
       
   318  */	
       
   319 void CUiArraysGenerator::FillFolderTitlesL( RArray<TBool>& aItemIsFolder, 
       
   320                     CDesC16ArrayFlat& aFolderTitles, const CVCModel& aModel, 
       
   321                     const RPointerArray<CUiEntryData>& aEntries )
       
   322     {
       
   323     // fill folder titles            
       
   324     for( TInt i = 0; i < aEntries.Count(); i++ )
       
   325         {
       
   326         aItemIsFolder.AppendL( ETrue );
       
   327         TBool titleForCurrentListedNameFound = EFalse;
       
   328         for( TInt j = 0; j < aModel.Count(); j++ )
       
   329             {
       
   330             if( aModel.At(j).FolderListedName() == aEntries[i]->FirstLine() )
       
   331                 {
       
   332                 aFolderTitles.AppendL( aModel.At(j).FolderTitle() );
       
   333                 titleForCurrentListedNameFound = ETrue;
       
   334                 break;
       
   335                 }  // if
       
   336             }  // for j
       
   337         if (!titleForCurrentListedNameFound)
       
   338             {
       
   339             User::Leave( KErrCorrupt );
       
   340             }
       
   341         }  // for i
       
   342     }    
       
   343     
       
   344 // End of File