phonebookui/Phonebook2/UIPolicy/src/CPbk2ContactViewSortPolicy.cpp
changeset 0 e686773b3f54
child 18 d4f567ce2e7c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2002-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:  Phonebook 2 contact view sort policy.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2ContactViewSortPolicy.h"
       
    20 
       
    21 // Phonebook2
       
    22 #include "CPbk2SortKeyArray.h"
       
    23 #include "RPbk2LocalizedResourceFile.h"
       
    24 #include "Pbk2DataCaging.hrh"
       
    25 #include <Pbk2UIPolicyRes.rsg>
       
    26 
       
    27 // Virtual Phonebook
       
    28 #include <MVPbkFieldType.h>
       
    29 #include <MVPbkViewContact.h>
       
    30 #include <MVPbkBaseContactField.h>
       
    31 #include <MVPbkContactFieldTextData.h>
       
    32 #include <MVPbkBaseContactFieldCollection.h>
       
    33 #include <CVPbkContactManager.h>
       
    34 #include <CVPbkFieldTypeSelector.h>
       
    35 #include <CVPbkFieldFilter.h>
       
    36 #include <CVPbkSortOrder.h>
       
    37 
       
    38 // System includes
       
    39 #include <coemain.h>
       
    40 #include <barsread.h>
       
    41 #include <SortUtil.h>
       
    42 
       
    43 // Debugging headers
       
    44 #include <Pbk2Profile.h>
       
    45 
       
    46 
       
    47 /// Unnamed namespace for local definitions
       
    48 namespace {
       
    49 
       
    50 _LIT(KResourceFile, "Pbk2UIPolicyRes.rsc");
       
    51 const TInt KNegative = -1;
       
    52 const TInt KPositive = 1;
       
    53 
       
    54 /**
       
    55  * Sort key array.
       
    56  */
       
    57 class TPbk2SortKeyArray : public MSortKeyArray
       
    58     {
       
    59     public: // Interface
       
    60 
       
    61         /**
       
    62          * Constructor.
       
    63          *
       
    64          * @param aArray    Array.
       
    65          */
       
    66         TPbk2SortKeyArray(
       
    67                 const MVPbkSortKeyArray& aArray );
       
    68 
       
    69         /**
       
    70          * Returns the count of the keys.
       
    71          *
       
    72          * @return  Count of the keys.
       
    73          */
       
    74         TInt SortKeyCount() const
       
    75             {
       
    76             return iArray.Count();
       
    77             }
       
    78 
       
    79         /**
       
    80          * Returns the key at aIndex.
       
    81          *
       
    82          * @param aIndex    Index to query.
       
    83          * @return  Key at queried index.
       
    84          */
       
    85         TSortKey SortKeyAt(
       
    86                 TInt aIndex ) const;
       
    87 
       
    88     private: // Data
       
    89         /// Ref: Array
       
    90         const MVPbkSortKeyArray& iArray;
       
    91 
       
    92     };
       
    93 
       
    94 // --------------------------------------------------------------------------
       
    95 // TPbk2SortKeyArray::TPbk2SortKeyArray
       
    96 // --------------------------------------------------------------------------
       
    97 //
       
    98 TPbk2SortKeyArray::TPbk2SortKeyArray( const MVPbkSortKeyArray& aArray ) :
       
    99         iArray( aArray )
       
   100     {
       
   101     }
       
   102 
       
   103 // --------------------------------------------------------------------------
       
   104 // TPbk2SortKeyArray::SortKeyAt
       
   105 // --------------------------------------------------------------------------
       
   106 //
       
   107 TSortKey TPbk2SortKeyArray::SortKeyAt( TInt aIndex ) const
       
   108     {
       
   109     TSortKeyType sortKeyType( ESortKeyBasic );
       
   110     if ( iArray.SortKeyAt( aIndex ).Type() == EVPbkSortKeyPronounciation )
       
   111         {
       
   112         sortKeyType = ESortKeyPronounciation;
       
   113         }
       
   114 
       
   115     return TSortKey( iArray.SortKeyAt( aIndex ).Text(), sortKeyType );
       
   116     }
       
   117 
       
   118 } /// namespace
       
   119 
       
   120 
       
   121 // --------------------------------------------------------------------------
       
   122 // CPbk2ContactViewSortPolicy::CPbk2ContactViewSortPolicy
       
   123 // --------------------------------------------------------------------------
       
   124 //
       
   125 CPbk2ContactViewSortPolicy::CPbk2ContactViewSortPolicy()
       
   126     {
       
   127     }
       
   128 
       
   129 // --------------------------------------------------------------------------
       
   130 // CPbk2ContactViewSortPolicy::~CPbk2ContactViewSortPolicy
       
   131 // --------------------------------------------------------------------------
       
   132 //
       
   133 CPbk2ContactViewSortPolicy::~CPbk2ContactViewSortPolicy()
       
   134     {
       
   135     delete iPronunciationFieldSelector;
       
   136     delete iRightSortKeyArray;
       
   137     delete iLeftSortKeyArray;
       
   138     delete iSortUtil;
       
   139     delete iSortOrder;
       
   140     }
       
   141 
       
   142 // --------------------------------------------------------------------------
       
   143 // CPbk2ContactViewSortPolicy::NewL
       
   144 // --------------------------------------------------------------------------
       
   145 //
       
   146 CPbk2ContactViewSortPolicy* CPbk2ContactViewSortPolicy::NewL
       
   147         ( TParam* aParam )
       
   148     {
       
   149     CPbk2ContactViewSortPolicy* self =
       
   150         new ( ELeave ) CPbk2ContactViewSortPolicy;
       
   151     CleanupStack::PushL( self );
       
   152     self->ConstructL( aParam );
       
   153     CleanupStack::Pop( self );
       
   154     return self;
       
   155     }
       
   156 
       
   157 // --------------------------------------------------------------------------
       
   158 // CPbk2ContactViewSortPolicy::ConstructL
       
   159 // --------------------------------------------------------------------------
       
   160 //
       
   161 void CPbk2ContactViewSortPolicy::ConstructL( TParam* aParam )
       
   162     {
       
   163     iSortUtil = CSortUtil::NewL();
       
   164 
       
   165     if ( aParam )
       
   166         {
       
   167         iSortPolicyParameterReceived = ETrue;
       
   168 
       
   169         iLeftSortKeyArray = CPbk2SortKeyArray::NewL();
       
   170         iRightSortKeyArray = CPbk2SortKeyArray::NewL();
       
   171 
       
   172         iSortOrder = CVPbkSortOrder::NewL(aParam->iSortOrder);
       
   173         iFieldMapper.SetSortOrder(*iSortOrder);
       
   174 
       
   175         RPbk2LocalizedResourceFile resFile(aParam->iFs);
       
   176         resFile.OpenLC( KPbk2RomFileDrive, KDC_RESOURCE_FILES_DIR,
       
   177             KResourceFile );
       
   178 
       
   179         HBufC8* selectorBuf = resFile.AllocReadLC
       
   180             ( R_PRONUNCIATION_FIELD_SELECTOR );
       
   181         TResourceReader reader;
       
   182         reader.SetBuffer( selectorBuf );
       
   183         iPronunciationFieldSelector = CVPbkFieldTypeSelector::NewL
       
   184             ( reader, aParam->iFieldTypes );
       
   185 
       
   186         CleanupStack::PopAndDestroy( 2 ); // buffer, resFile
       
   187 
       
   188         }
       
   189     }
       
   190 
       
   191 // --------------------------------------------------------------------------
       
   192 // CPbk2ContactViewSortPolicy::SetSortOrderL
       
   193 // --------------------------------------------------------------------------
       
   194 //
       
   195 void CPbk2ContactViewSortPolicy::SetSortOrderL
       
   196         ( const MVPbkFieldTypeList& aSortOrder )
       
   197     {
       
   198     if ( !iSortPolicyParameterReceived )
       
   199         {
       
   200         // This class has been initialized without TParam
       
   201         User::Leave( KErrNotSupported );
       
   202         }
       
   203 
       
   204     CVPbkSortOrder* sortOrder = CVPbkSortOrder::NewL( aSortOrder );
       
   205     delete iSortOrder;
       
   206     iSortOrder = sortOrder;
       
   207     iFieldMapper.SetSortOrder( *iSortOrder );
       
   208     }
       
   209 
       
   210 // --------------------------------------------------------------------------
       
   211 // CPbk2ContactViewSortPolicy::SortStartL
       
   212 // --------------------------------------------------------------------------
       
   213 //
       
   214 void CPbk2ContactViewSortPolicy::SortStartL()
       
   215     {
       
   216     if ( !iSortPolicyParameterReceived )
       
   217         {
       
   218         // This class has been initialized without TParam
       
   219         User::Leave( KErrNotSupported );
       
   220         }
       
   221 
       
   222     iLeftSortKeyArray->Reset();
       
   223     iRightSortKeyArray->Reset();
       
   224 
       
   225     const TInt typeCount( iSortOrder->FieldTypeCount() );
       
   226     for ( TInt i( 0 ); i < typeCount; ++i )
       
   227         {
       
   228         const MVPbkFieldType& fieldType = iSortOrder->FieldTypeAt( i );
       
   229         if ( iPronunciationFieldSelector->IsFieldTypeIncluded( fieldType ) )
       
   230             {
       
   231             iLeftSortKeyArray->AppendL(
       
   232                 TSortKey( KNullDesC, ESortKeyPronounciation ) );
       
   233             iRightSortKeyArray->AppendL(
       
   234                 TSortKey( KNullDesC, ESortKeyPronounciation ) );
       
   235             }
       
   236         else
       
   237             {
       
   238             iLeftSortKeyArray->AppendL(
       
   239                 TSortKey( KNullDesC, ESortKeyBasic ) );
       
   240             iRightSortKeyArray->AppendL(
       
   241                 TSortKey( KNullDesC, ESortKeyBasic ) );
       
   242             }
       
   243         }
       
   244     }
       
   245 
       
   246 // --------------------------------------------------------------------------
       
   247 // CPbk2ContactViewSortPolicy::SortCompleted
       
   248 // --------------------------------------------------------------------------
       
   249 //
       
   250 void CPbk2ContactViewSortPolicy::SortCompleted()
       
   251     {
       
   252     iLeftSortKeyArray->Reset();
       
   253     iRightSortKeyArray->Reset();
       
   254     }
       
   255 
       
   256 // --------------------------------------------------------------------------
       
   257 // CPbk2ContactViewSortPolicy::CompareContacts
       
   258 // --------------------------------------------------------------------------
       
   259 //
       
   260 TInt CPbk2ContactViewSortPolicy::CompareContacts
       
   261         ( const MVPbkViewContact& aLhs, const MVPbkViewContact& aRhs )
       
   262     {
       
   263     TInt ret = 0;
       
   264 
       
   265     if ( iSortPolicyParameterReceived )
       
   266         {
       
   267         // Change the text in the sortkeys to correspond
       
   268         // fields in the contacts
       
   269 
       
   270         TInt i = 0;
       
   271 
       
   272 PBK2_PROFILE_START( Pbk2Profile::ESortPolicyCompareContacts );
       
   273 
       
   274         iFieldMapper.SetContactFields( aLhs.Fields() );
       
   275         const TInt lhsCount = iFieldMapper.FieldCount();
       
   276         for ( i = 0; i < lhsCount; ++i )
       
   277             {
       
   278             const MVPbkBaseContactField* lhsField =
       
   279                 iFieldMapper.FieldAt( i );
       
   280             if ( lhsField )
       
   281                 {
       
   282                 iLeftSortKeyArray->SetText( MVPbkContactFieldTextData::Cast
       
   283                     ( lhsField->FieldData() ).Text(), i );
       
   284                 }
       
   285             else
       
   286                 {
       
   287                 iLeftSortKeyArray->SetText( KNullDesC, i );
       
   288                 }
       
   289             }
       
   290 
       
   291         iFieldMapper.SetContactFields( aRhs.Fields() );
       
   292         const TInt rhsCount = iFieldMapper.FieldCount();
       
   293         for ( i = 0; i < rhsCount; ++i )
       
   294             {
       
   295             const MVPbkBaseContactField* rhsField =
       
   296                 iFieldMapper.FieldAt( i );
       
   297             if ( rhsField )
       
   298                 {
       
   299                 iRightSortKeyArray->SetText( MVPbkContactFieldTextData::Cast
       
   300                     ( rhsField->FieldData() ).Text(), i );
       
   301                 }
       
   302             else
       
   303                 {
       
   304                 iRightSortKeyArray->SetText( KNullDesC, i );
       
   305                 }
       
   306             }
       
   307 
       
   308 PBK2_PROFILE_END( Pbk2Profile::ESortPolicyCompareContacts );
       
   309 
       
   310 
       
   311 PBK2_PROFILE_START( Pbk2Profile::ESortPolicySortUtil );
       
   312 
       
   313         TInt result( iSortUtil->Interface()->CompareItems
       
   314             ( *iLeftSortKeyArray, *iRightSortKeyArray ) );
       
   315 
       
   316 PBK2_PROFILE_END( Pbk2Profile::ESortPolicySortUtil );
       
   317 
       
   318         ret = PostProcessResult( result );
       
   319         }
       
   320 
       
   321     return ret;
       
   322     }
       
   323 
       
   324 // --------------------------------------------------------------------------
       
   325 // CPbk2ContactViewSortPolicy::CompareItems
       
   326 // --------------------------------------------------------------------------
       
   327 //
       
   328 TInt CPbk2ContactViewSortPolicy::CompareItems
       
   329         ( const MVPbkSortKeyArray& aLhs,
       
   330           const MVPbkSortKeyArray& aRhs ) const
       
   331     {
       
   332     const TPbk2SortKeyArray lhsKeyArray( aLhs );
       
   333     const TPbk2SortKeyArray rhsKeyArray( aRhs );
       
   334     return iSortUtil->Interface()->CompareItems( lhsKeyArray, rhsKeyArray );
       
   335     }
       
   336 
       
   337 // --------------------------------------------------------------------------
       
   338 // CPbk2ContactViewSortPolicy::PostProcessResult
       
   339 // --------------------------------------------------------------------------
       
   340 //
       
   341 TInt CPbk2ContactViewSortPolicy::PostProcessResult( TInt aSortUtilResult )
       
   342     {
       
   343     // With this function we correct sort util result because it does not
       
   344     // give right answers if contact is "Unnamed".
       
   345     // So here is clarified how this function corrects the result.
       
   346     // If rightside has no data left is less (-1) otherwise greater (1).
       
   347     // "<Unnamed>"  vs. "<Unnamed>" = -1,   leftside comes before rightside
       
   348     // "a"          vs. "<Unnamed>" = -1,   leftside comes before rightside
       
   349     // "<Unnamed>"  vs. "a"         = 1     rightside comes before leftside
       
   350     //
       
   351     TInt result = aSortUtilResult;
       
   352     const TInt rhsCount( iRightSortKeyArray->SortKeyCount() );
       
   353     TBool rhsHasData( EFalse );
       
   354     for ( TInt i = 0; i < rhsCount; ++i )
       
   355         {
       
   356         TSortKey key = iRightSortKeyArray->SortKeyAt( i );
       
   357         if ( key.Text().Length() > 0 )
       
   358             {
       
   359             rhsHasData = ETrue;
       
   360             break;
       
   361             }
       
   362         }
       
   363 
       
   364     if ( !rhsHasData )
       
   365         {
       
   366         // Here we have situation "something" vs "<Unnamed>" .
       
   367         // We want leftside to come before rightside.
       
   368         result = KNegative;
       
   369         }
       
   370     else
       
   371         {
       
   372         TBool lhsHasData( EFalse );
       
   373         const TInt lhsCount( iLeftSortKeyArray->SortKeyCount() );
       
   374         for ( TInt i = 0; i < lhsCount; ++i )
       
   375             {
       
   376             TSortKey key = iLeftSortKeyArray->SortKeyAt( i );
       
   377             if ( key.Text().Length() > 0 )
       
   378                 {
       
   379                 lhsHasData = ETrue;
       
   380                 break;
       
   381                 }
       
   382             }
       
   383 
       
   384         if ( !lhsHasData )
       
   385             {
       
   386             // Here we have situation "<Unnamed>" vs. "something".
       
   387             // We want rightside to come before leftside.
       
   388             result = KPositive;
       
   389             }
       
   390         }
       
   391 
       
   392     return result;
       
   393     }
       
   394 
       
   395 //  End of File