predictivesearch/PcsAlgorithm/Algorithm2/src/CPcsAlgorithm2Utils.cpp
changeset 0 e686773b3f54
child 32 2828b4d142c0
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     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: Supports initial search feature. 
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDES
       
    19 #include "CPcsAlgorithm2Utils.h"
       
    20 #include "CPsData.h"
       
    21 #include "CPcsDefs.h"
       
    22 #include "CPcsCache.h"
       
    23 #include <collate.h>
       
    24 // CONSTANTS
       
    25 _LIT(KSpace, ' ');
       
    26 // Search contacts in a group URI template
       
    27 _LIT(KGroupIdUri, "cntdb://c:contacts.gdb?id=");
       
    28 
       
    29 // ============================== MEMBER FUNCTIONS ============================
       
    30 
       
    31 // ----------------------------------------------------------------------------
       
    32 // CPcsAlgorithm2Utils::FormCompleteSearchResultsL()
       
    33 // Merges all the respective data store result sets to single set in sorted order.
       
    34 // ----------------------------------------------------------------------------
       
    35 void CPcsAlgorithm2Utils::FormCompleteSearchResultsL(RPointerArray<CPSDATA_R_PTR_ARRAY>& aSearchResultsArr,
       
    36                                                      RPointerArray<CPsData>& SearchResults)
       
    37     {
       
    38     TInt maxIndex = 0;
       
    39     TInt maxValue = aSearchResultsArr[maxIndex]->Count();
       
    40     TLinearOrder<CPsData> rule(CPcsAlgorithm2Utils::CompareDataBySortOrder);
       
    41 
       
    42     // Find the largest array in aSearchResultsArr
       
    43     for (TInt i = 1; i < aSearchResultsArr.Count(); i++)
       
    44         {
       
    45         if (aSearchResultsArr[i]->Count() > maxValue)
       
    46             {
       
    47             maxIndex = i;
       
    48             maxValue = aSearchResultsArr[i]->Count();
       
    49             }
       
    50         }
       
    51 
       
    52     // Assign the largets array to searchresults 
       
    53     for (TInt i = 0; i < aSearchResultsArr[maxIndex]->Count(); i++)
       
    54         {
       
    55         SearchResults.Append((*(aSearchResultsArr[maxIndex]))[i]);
       
    56         }
       
    57 
       
    58     // Merge the remaining result arrays to the largest array in sequential order
       
    59     for (TInt i = 0; i < aSearchResultsArr.Count(); i++)
       
    60         {
       
    61         // Check if we are not copying again the largest array
       
    62         if ((i != maxIndex) && ((aSearchResultsArr[i])->Count() != 0))
       
    63             {
       
    64             TInt numElements = (aSearchResultsArr[i])->Count();
       
    65             for (TInt j = 0; j < numElements; j++)
       
    66                 {
       
    67                 SearchResults.InsertInOrderAllowRepeatsL((*(aSearchResultsArr[i]))[j], rule);
       
    68                 }
       
    69             }
       
    70         }
       
    71     }
       
    72 
       
    73 // ----------------------------------------------------------------------------
       
    74 // CPcsAlgorithm2Utils::MyCompareC()
       
    75 // Lang specific MyCompareC
       
    76 // ----------------------------------------------------------------------------
       
    77 TInt CPcsAlgorithm2Utils::MyCompareC(const TDesC& aLeft, const TDesC& aRight)
       
    78     {
       
    79     // Get the current language
       
    80     TLanguage lang = User::Language();
       
    81 
       
    82     // Get the standard method
       
    83     TCollationMethod meth = *Mem::CollationMethodByIndex(0);
       
    84     meth.iFlags |= TCollationMethod::EIgnoreNone;
       
    85     meth.iFlags |= TCollationMethod::EFoldCase;
       
    86 
       
    87     if (lang == ELangHindi || lang == ELangMarathi)
       
    88         {
       
    89         meth.iFlags |= TCollationMethod::EIgnoreCombining;
       
    90         }
       
    91 
       
    92     // Collation level 3 is used
       
    93     TInt comparison(aLeft.CompareC(aRight, 3, &meth));
       
    94     return comparison;
       
    95     }
       
    96 
       
    97 // ----------------------------------------------------------------------------
       
    98 // CPcsAlgorithm2Utils::CompareDataBySortOrder()
       
    99 // TLinearOrder rule for comparison of data objects
       
   100 // ----------------------------------------------------------------------------
       
   101 TInt CPcsAlgorithm2Utils::CompareDataBySortOrder(const CPsData& aObject1,
       
   102                                                  const CPsData& aObject2)
       
   103     {
       
   104     _LIT(KSpace, " ");
       
   105 
       
   106     // Fetch the cache list stored in TLS to recover the sort order
       
   107     typedef RPointerArray<CPcsCache> PTR;
       
   108     PTR* pcsCache = static_cast<PTR*> (Dll::Tls());
       
   109 
       
   110     // Data1
       
   111     TBuf<255> data1(KNullDesC);
       
   112     TInt uriId1 = aObject1.UriId();
       
   113     CPcsCache* cache = (*pcsCache)[uriId1];
       
   114 
       
   115     RArray<TInt> indexOrder;
       
   116 
       
   117     // Get the index order based on sort order from the cache
       
   118     cache->GetIndexOrder(indexOrder);
       
   119 
       
   120     // Append sort order elements first
       
   121     for (int i = 0; i < indexOrder.Count(); i++)
       
   122         {
       
   123         TInt index = indexOrder[i];
       
   124         if (index < aObject1.DataElementCount() && aObject1.Data(index))
       
   125             {
       
   126             // Trim the unnecessary white spaces/special chars before we compare
       
   127             TBuf<255> str(KNullDesC);
       
   128 
       
   129             str = aObject1.Data(index)->Des();
       
   130             CPcsAlgorithm2Utils::MyTrim(str);
       
   131 
       
   132             data1 += str;
       
   133             data1 += KSpace;
       
   134             }
       
   135         }
       
   136 
       
   137     /* --- NOT SURE IF THIS BEHAVIOR IS REQUIRED ---
       
   138      // Append remaining elements in order
       
   139      for ( int i = 0; i < aObject1.DataElementCount(); i++ )
       
   140      {
       
   141      if ( indexOrder.Find(i) == KErrNone && aObject1.Data(i) )
       
   142      {
       
   143      data1 += aObject1.Data(i)->Des();
       
   144      data1 += KSpace;
       
   145      }	     
       
   146      }
       
   147      */
       
   148 
       
   149     // Data2
       
   150     TBuf<255> data2(KNullDesC);
       
   151     TInt uriId2 = aObject2.UriId();
       
   152     cache = (*pcsCache)[uriId2];
       
   153 
       
   154     indexOrder.Reset();
       
   155 
       
   156     // Get the index order based on sort order from the cache
       
   157     cache->GetIndexOrder(indexOrder);
       
   158 
       
   159     // Append sort order elements first
       
   160     for (int i = 0; i < indexOrder.Count(); i++)
       
   161         {
       
   162         TInt index = indexOrder[i];
       
   163         if (index < aObject2.DataElementCount() && aObject2.Data(index))
       
   164             {
       
   165             // Trim the unnecessary white spaces/special chars before we compare
       
   166             TBuf<255> str(KNullDesC);
       
   167 
       
   168             str = aObject2.Data(index)->Des();
       
   169             CPcsAlgorithm2Utils::MyTrim(str);
       
   170 
       
   171             data2 += str;
       
   172             data2 += KSpace;
       
   173             }
       
   174         }
       
   175 
       
   176     /* --- NOT SURE IF THIS BEHAVIOR IS REQUIRED ---
       
   177      // Append remaining elements in order
       
   178      for ( int i = 0; i < aObject2.DataElementCount(); i++ )
       
   179      {
       
   180      if ( indexOrder.Find(i) == KErrNone && aObject2.Data(i) )
       
   181      {
       
   182      data2 += aObject2.Data(i)->Des();
       
   183      data2 += KSpace;
       
   184      }	     
       
   185      }
       
   186      */
       
   187 
       
   188     indexOrder.Reset();
       
   189     data1.TrimAll();
       
   190     data2.TrimAll();
       
   191     return (CPcsAlgorithm2Utils::MyCompareC(data1, data2));
       
   192     }
       
   193 
       
   194 // ----------------------------------------------------------------------------
       
   195 // CPcsAlgorithm2Utils::MyTrim()
       
   196 // Trim off all white spaces and special characters
       
   197 // This behavior is required to mimic the current phonebook sort sequence
       
   198 // ----------------------------------------------------------------------------
       
   199 void CPcsAlgorithm2Utils::MyTrim(TDes& aString)
       
   200     {
       
   201     for (TInt i = aString.Length(); --i >= 0;)
       
   202         {
       
   203         TChar c = (TChar) aString[i];
       
   204 
       
   205         if (!c.IsAlphaDigit())
       
   206             {
       
   207             aString.Replace(i, 1, KSpace);
       
   208             }
       
   209         }
       
   210 
       
   211     aString.TrimAll();
       
   212     }
       
   213 
       
   214 // ----------------------------------------------------------------------------
       
   215 // CPcsAlgorithm2Utils::IsGroupUri()
       
   216 // Check if the input URI is of contact search in a group template form
       
   217 // ----------------------------------------------------------------------------                         
       
   218 TBool CPcsAlgorithm2Utils::IsGroupUri(TDesC& aURI)
       
   219     {
       
   220     TBuf<255> uri(aURI);
       
   221     uri.LowerCase();
       
   222 
       
   223     TInt index = uri.FindF(KGroupIdUri);
       
   224 
       
   225     if (index == KErrNotFound)
       
   226         {
       
   227         return EFalse;
       
   228         }
       
   229 
       
   230     return ETrue;
       
   231     }
       
   232 
       
   233 // End of File
       
   234