predictivesearch/PcsAlgorithm/Algorithm2/src/CPcsAlgorithm2Utils.cpp
branchRCL_3
changeset 21 9da50d567e3c
parent 20 f4a778e096c2
equal deleted inserted replaced
20:f4a778e096c2 21:9da50d567e3c
    36 void CPcsAlgorithm2Utils::FormCompleteSearchResultsL(RPointerArray<CPSDATA_R_PTR_ARRAY>& aSearchResultsArr,
    36 void CPcsAlgorithm2Utils::FormCompleteSearchResultsL(RPointerArray<CPSDATA_R_PTR_ARRAY>& aSearchResultsArr,
    37                                                      RPointerArray<CPsData>& aSearchResults)
    37                                                      RPointerArray<CPsData>& aSearchResults)
    38     {
    38     {
    39     TInt maxIndex = 0;
    39     TInt maxIndex = 0;
    40     TInt maxValue = aSearchResultsArr[maxIndex]->Count();
    40     TInt maxValue = aSearchResultsArr[maxIndex]->Count();
    41     TLinearOrder<CPsData> rule(CPcsAlgorithm2Utils::CompareDataBySortOrder);
    41     TLinearOrder<CPsData> rule(CPcsAlgorithm2Utils::CompareDataBySortOrderL);
    42 
    42 
    43     // Find the largest array in aSearchResultsArr
    43     // Find the largest array in aSearchResultsArr
    44     const TInt searchResultsArrCount = aSearchResultsArr.Count(); 
    44     const TInt searchResultsArrCount = aSearchResultsArr.Count(); 
    45     for (TInt i = 1; i < searchResultsArrCount; i++)
    45     for (TInt i = 1; i < searchResultsArrCount; i++)
    46         {
    46         {
    97     TInt comparison(aLeft.CompareC(aRight, 3, &meth));
    97     TInt comparison(aLeft.CompareC(aRight, 3, &meth));
    98     return comparison;
    98     return comparison;
    99     }
    99     }
   100 
   100 
   101 // ----------------------------------------------------------------------------
   101 // ----------------------------------------------------------------------------
   102 // CPcsAlgorithm2Utils::CompareDataBySortOrder()
   102 // CPcsAlgorithm2Utils::CompareDataBySortOrderL()
   103 // TLinearOrder rule for comparison of data objects
   103 // TLinearOrder rule for comparison of data objects
   104 // ----------------------------------------------------------------------------
   104 // ----------------------------------------------------------------------------
   105 TInt CPcsAlgorithm2Utils::CompareDataBySortOrder(const CPsData& aObject1,
   105 TInt CPcsAlgorithm2Utils::CompareDataBySortOrderL(const CPsData& aObject1,
   106                                                  const CPsData& aObject2)
   106                                                   const CPsData& aObject2)
   107     {
   107     {
   108     _LIT(KSpace, " ");
   108     TInt compareRes = 0;
       
   109 
       
   110     if( CPsData::CompareById(aObject1, aObject2) )
       
   111         {
       
   112         return compareRes;
       
   113         }
   109 
   114 
   110     // Fetch the cache list stored in TLS to recover the sort order
   115     // Fetch the cache list stored in TLS to recover the sort order
   111     typedef RPointerArray<CPcsCache> PTR;
   116     typedef RPointerArray<CPcsCache> PTR;
   112     PTR* pcsCache = static_cast<PTR*> (Dll::Tls());
   117     PTR* pcsCache = static_cast<PTR*>(Dll::Tls());
   113 
   118     User::LeaveIfNull(pcsCache);
   114     // Data1
   119 
   115     TBuf<255> data1(KNullDesC);
   120     CPcsCache* cache1 = (*pcsCache)[aObject1.UriId()];
   116     TInt uriId1 = aObject1.UriId();
   121     CPcsCache* cache2 = (*pcsCache)[aObject2.UriId()];
   117     CPcsCache* cache = (*pcsCache)[uriId1];
   122     RArray<TInt> indexOrder1;
   118 
   123     RArray<TInt> indexOrder2;
   119     RArray<TInt> indexOrder;
       
   120 
   124 
   121     // Get the index order based on sort order from the cache
   125     // Get the index order based on sort order from the cache
   122     cache->GetIndexOrder(indexOrder);
   126     cache1->GetIndexOrder(indexOrder1);
   123 
   127     CleanupClosePushL(indexOrder1);
   124     // Append sort order elements first
   128     cache2->GetIndexOrder(indexOrder2);
   125     const TInt indexOrderCount1 = indexOrder.Count();
   129     CleanupClosePushL(indexOrder2);
   126     for (int i = 0; i < indexOrderCount1; i++)
   130 
   127         {
   131     // Check if Sort Order is identical (it must be for same cache)
   128         TInt index = indexOrder[i];
   132     TBool sameIndexOrder = ETrue;
   129         if (index < aObject1.DataElementCount() && aObject1.Data(index))
   133     if ( indexOrder1.Count() != indexOrder2.Count() )
   130             {
   134         {
   131             // Trim the unnecessary white spaces/special chars before we compare
   135         sameIndexOrder = EFalse;
   132             TBuf<255> str(KNullDesC);
   136         }
   133 
   137     else
   134             str = aObject1.Data(index)->Des();
   138         {
   135             CPcsAlgorithm2Utils::MyTrim(str);
   139         for ( TInt i = 0; i < indexOrder1.Count(); i++ )
   136 
   140             {
   137             data1 += str;
   141             if (indexOrder1[i] != indexOrder2[i])
   138             data1 += KSpace;
   142                 {
   139             }
   143                 sameIndexOrder = EFalse;
   140         }
   144                 break;
   141 
   145                 }
   142     /* --- NOT SURE IF THIS BEHAVIOR IS REQUIRED ---
   146             }
   143      // Append remaining elements in order
   147         }
   144      for ( int i = 0; i < aObject1.DataElementCount(); i++ )
   148 
   145      {
   149     // Sort Orders among diffent caches should be the same, anyway
   146      if ( indexOrder.Find(i) == KErrNone && aObject1.Data(i) )
   150     // if caches and SO are different we compare by cache URI index
   147      {
   151     if (!sameIndexOrder)
   148      data1 += aObject1.Data(i)->Des();
   152         {
   149      data1 += KSpace;
   153         compareRes = aObject1.UriId() - aObject2.UriId();
   150      }	     
   154 
   151      }
   155         CleanupStack::PopAndDestroy(&indexOrder2);
   152      */
   156         CleanupStack::PopAndDestroy(&indexOrder1);
   153 
   157         return compareRes;
   154     // Data2
   158         }
   155     TBuf<255> data2(KNullDesC);
   159 
   156     TInt uriId2 = aObject2.UriId();
   160     // The comparison between contacts data is done for the sort order
   157     cache = (*pcsCache)[uriId2];
   161     // fields skipping the ones that are empty
   158 
   162     TInt indexCount = indexOrder1.Count();
   159     indexOrder.Reset();
   163     TInt idx1 = 0;
   160 
   164     TInt idx2 = 0;
   161     // Get the index order based on sort order from the cache
   165     while ( compareRes == 0 && idx1 < indexCount && idx2 < indexCount )
   162     cache->GetIndexOrder(indexOrder);
   166         {
   163     const TInt indexOrderCount2 = indexOrder.Count();
   167         // Get contact field of 1st contact
   164 
   168         TInt object1Idx = indexOrder1[idx1];
   165     // Append sort order elements first
   169         HBufC* strCompare1 = aObject1.Data(object1Idx)->Des().AllocLC();
   166     for (int i = 0; i < indexOrderCount2; i++)
   170         TPtr strCompare1Ptr( strCompare1->Des() );
   167         {
   171         CPcsAlgorithm2Utils::MyTrim( strCompare1Ptr );
   168         TInt index = indexOrder[i];
   172 
   169         if (index < aObject2.DataElementCount() && aObject2.Data(index))
   173         // Get contact field of 2nd contact
   170             {
   174         TInt object2Idx = indexOrder2[idx2];
   171             // Trim the unnecessary white spaces/special chars before we compare
   175         HBufC* strCompare2 = aObject2.Data(object2Idx)->Des().AllocLC();
   172             TBuf<255> str(KNullDesC);
   176         TPtr strCompare2Ptr( strCompare2->Des() );
   173 
   177         CPcsAlgorithm2Utils::MyTrim( strCompare2Ptr );
   174             str = aObject2.Data(index)->Des();
   178 
   175             CPcsAlgorithm2Utils::MyTrim(str);
   179         if ( strCompare1->Length() > 0 && strCompare2->Length() > 0 )
   176 
   180             {
   177             data2 += str;
   181             compareRes = CPcsAlgorithm2Utils::MyCompareC(*strCompare1, *strCompare2);
   178             data2 += KSpace;
   182             idx1++;
   179             }
   183             idx2++;
   180         }
   184             }
   181 
   185         else // Increment only the index of the contact with empty field
   182     /* --- NOT SURE IF THIS BEHAVIOR IS REQUIRED ---
   186             {
   183      // Append remaining elements in order
   187             if ( strCompare1->Length() == 0 )
   184      for ( int i = 0; i < aObject2.DataElementCount(); i++ )
   188                 idx1++;
   185      {
   189             if ( strCompare2->Length() == 0 )
   186      if ( indexOrder.Find(i) == KErrNone && aObject2.Data(i) )
   190                 idx2++;
   187      {
   191             }
   188      data2 += aObject2.Data(i)->Des();
   192 
   189      data2 += KSpace;
   193         CleanupStack::PopAndDestroy(strCompare2);
   190      }	     
   194         CleanupStack::PopAndDestroy(strCompare1);
   191      }
   195         }
   192      */
   196 
   193 
   197     // We do not return that contacts are equal by SO
   194     indexOrder.Reset();
   198     if ( compareRes == 0 )
   195     data1.TrimAll();
   199         {
   196     data2.TrimAll();
   200         if ( idx1 != idx2 )
   197     return (CPcsAlgorithm2Utils::MyCompareC(data1, data2));
   201             {
       
   202             // Compare by index position
       
   203             // If idx1 > idx2 and SO is "FN LN" it means for instance that:
       
   204             //     Contact1=[FN:"",      LN:"Smith"],  idx1=2
       
   205             //     Contact2=[FN:"Smith", LN:"Donald"], idx2=1
       
   206             // Therefore Contact1="Smith" is < than Contact2="Smith Donald"
       
   207             // and the return value of this method has to be < 0 (idx2-idx1)
       
   208 
       
   209             compareRes = idx2 - idx1;
       
   210             }
       
   211         else
       
   212             {
       
   213             // Compare by URI ID as 1st choice and Contact ID as 2nd choice
       
   214 
       
   215             compareRes == ( aObject1.UriId() != aObject2.UriId() ) ?
       
   216                 aObject1.UriId() - aObject2.UriId() : aObject1.Id() - aObject2.Id();
       
   217             }
       
   218         }
       
   219 
       
   220     CleanupStack::PopAndDestroy(&indexOrder2);
       
   221     CleanupStack::PopAndDestroy(&indexOrder1);
       
   222 
       
   223     return compareRes;
   198     }
   224     }
   199 
   225 
   200 // ----------------------------------------------------------------------------
   226 // ----------------------------------------------------------------------------
   201 // CPcsAlgorithm2Utils::CompareExact()
   227 // CPcsAlgorithm2Utils::CompareExact()
   202 // 
   228 //