predictivesearch/PcsAlgorithm/Algorithm2/src/CPcsAlgorithm2.cpp
branchRCL_3
changeset 85 38bb213f60ba
parent 74 6b5524b4f673
equal deleted inserted replaced
74:6b5524b4f673 85:38bb213f60ba
    14 * Description: Predictive Contact Search Algorithm 1 main class
    14 * Description: Predictive Contact Search Algorithm 1 main class
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 // INCLUDES
    18 // INCLUDES
    19 #include <vpbkeng.rsg>
    19 #include <VPbkEng.rsg>
    20 #include <PtiEngine.h>
    20 #include <PtiEngine.h>
    21 #include <centralrepository.h>
    21 #include <centralrepository.h>
    22 #include <AknFepInternalCRKeys.h>
    22 #include <AknFepInternalCRKeys.h>
    23 
    23 
    24 #include "CPcsAlgorithm2.h"
    24 #include "CPcsAlgorithm2.h"
    77     iCacheStatus = ECachingNotStarted; // Starting status
    77     iCacheStatus = ECachingNotStarted; // Starting status
    78     iCacheError = KErrNone; // No error
    78     iCacheError = KErrNone; // No error
    79     iCacheCount = 0; // No data
    79     iCacheCount = 0; // No data
    80 
    80 
    81     iFindUtilECE = CFindUtilChineseECE::NewL(this);
    81     iFindUtilECE = CFindUtilChineseECE::NewL(this);
       
    82     iFindUtil = CFindUtil::NewL();
    82     
    83     
    83     iPluginLauncher = CIdle::NewL( CActive::EPriorityStandard );
    84     iPluginLauncher = CIdle::NewL( CActive::EPriorityStandard );
    84 
    85 
    85     // Define cache status property used to inform clients about the caching status.
    86     // Define cache status property used to inform clients about the caching status.
    86     DefinePropertyL( EPsKeyCacheStatus );
    87     DefinePropertyL( EPsKeyCacheStatus );
    93     DefinePropertyL( EPsKeyContactModifiedCounter );
    94     DefinePropertyL( EPsKeyContactModifiedCounter );
    94     DefinePropertyL( EPsKeyContactAddedCounter );
    95     DefinePropertyL( EPsKeyContactAddedCounter );
    95 
    96 
    96     // Initialize key map and pti engine
    97     // Initialize key map and pti engine
    97     TInt keyMapErr = KErrNone;
    98     TInt keyMapErr = KErrNone;
    98     TRAP( keyMapErr, iKeyMap = CPcsKeyMap::NewL( this ) );
    99     TRAP( keyMapErr, iKeyMap = CPcsKeyMap::NewL( this ));
    99     if (keyMapErr != KErrNone)
   100     if (keyMapErr != KErrNone)
   100         {
   101         {
   101         PRINT ( _L("**********************************************."));
   102         PRINT ( _L("**********************************************."));
   102         PRINT1 ( _L("CPcsAlgorithm2::ConstructL() KeyMap construction error. The keymap crashed with error code %d."), keyMapErr );
   103         PRINT1 ( _L("CPcsAlgorithm2::ConstructL() KeyMap construction error. The keymap crashed with error code %d."), keyMapErr );
   103         PRINT ( _L("Please check the keypad/language for which keymap got crashed.") );
   104         PRINT ( _L("Please check the keypad/language for which keymap got crashed.") );
   104         PRINT ( _L("**********************************************."));
   105         PRINT ( _L("**********************************************."));
   105         User::Leave( keyMapErr ); // we can't go on without a key map; constructing cache needs it
       
   106         }
   106         }
   107     
   107     
   108     // Initialize helpers
   108     // Initialize helpers
   109     iHelper = CPcsAlgorithm2Helper::NewL(this);
   109     iHelper = CPcsAlgorithm2Helper::NewL(this);
   110     iMultiSearchHelper = CPcsAlgorithm2MultiSearchHelper::NewL(this);
   110     iMultiSearchHelper = CPcsAlgorithm2MultiSearchHelper::NewL(this);
   138     // Cleanup helpers
   138     // Cleanup helpers
   139     delete iHelper;
   139     delete iHelper;
   140     delete iMultiSearchHelper;
   140     delete iMultiSearchHelper;
   141 
   141 
   142     delete iFindUtilECE;
   142     delete iFindUtilECE;
       
   143     delete iFindUtil;
   143     
   144     
   144     delete iPluginLauncher;
   145     delete iPluginLauncher;
   145 
   146 
   146     PRINT ( _L("End CPcsAlgorithm2::~CPcsAlgorithm2") );
   147     PRINT ( _L("End CPcsAlgorithm2::~CPcsAlgorithm2") );
   147     }
   148     }
   162         User::LeaveIfError(err);
   163         User::LeaveIfError(err);
   163         }
   164         }
   164     }
   165     }
   165 
   166 
   166 // ----------------------------------------------------------------------------
   167 // ----------------------------------------------------------------------------
   167 // CPcsAlgorithm2::RemoveSpacesL
       
   168 // Remove leading and trailing spaces of search query
       
   169 // ----------------------------------------------------------------------------
       
   170 void  CPcsAlgorithm2::RemoveSpacesL(CPsQuery& aQuery)
       
   171     {
       
   172     PRINT ( _L("Enter CPcsAlgorithm2::RemoveSpacesL") );
       
   173 
       
   174     // Remove all leading " "
       
   175     while ( aQuery.Count() > 0 )
       
   176         {
       
   177         CPsQueryItem& item = aQuery.GetItemAtL(0); // First
       
   178         if ( !item.Character().IsSpace() )
       
   179             {
       
   180             break;
       
   181             }
       
   182         aQuery.Remove(0);
       
   183         }
       
   184 
       
   185     // Remove all trailing " "
       
   186     while ( aQuery.Count() > 0 )
       
   187         {
       
   188         CPsQueryItem& item = aQuery.GetItemAtL(aQuery.Count()-1); // Last
       
   189         if ( !item.Character().IsSpace() )
       
   190             {
       
   191             break;
       
   192             }
       
   193         aQuery.Remove(aQuery.Count()-1);
       
   194         }
       
   195     
       
   196     PRINT ( _L("End CPcsAlgorithm2::RemoveSpacesL") );
       
   197     }    
       
   198 
       
   199 // ----------------------------------------------------------------------------
       
   200 // CPcsAlgorithm2::ReplaceZeroWithSpaceL
   168 // CPcsAlgorithm2::ReplaceZeroWithSpaceL
   201 // Replace first occurance of '0' in a sequence of '0's in ITU-T with space
   169 // Replace first occurance of '0' in a sequence of '0's in ITU-T with space
   202 // ----------------------------------------------------------------------------
   170 // ----------------------------------------------------------------------------
   203 TBool CPcsAlgorithm2::ReplaceZeroWithSpaceL(CPsQuery& aQuery)
   171 TBool CPcsAlgorithm2::ReplaceZeroWithSpaceL(CPsQuery& aQuery)
   204     {
   172     {
   205     PRINT ( _L("Enter CPcsAlgorithm2::ReplaceZeroWithSpaceL") );
   173     TChar space(KSpace); // ascii value for space
   206 
   174 
   207        //PRINTQUERY ( _L("CPcsAlgorithm2::ReplaceZeroWithSpaceL (BEFORE): "), aQuery );
   175     TBool queryModified = EFalse;
   208 
   176 
   209        TBool queryModified = EFalse;    
   177     // Skip initial zeros in query
   210 
   178     TInt index = 0;
   211        /* In phones like E52 and E55, where the "0" and the " " characters are on
   179     for (index = 0; index < aQuery.Count()
   212         * the same key, the "0"s have to be considered as possible separators.
   180             && aQuery.GetItemAtL(index).Character().GetNumericValue() == 0; ++index)
   213         *
   181         {
   214         * In phones like N97 and E72, where the "0" and the " " characters are on
   182         }
   215         * different keys, the "0"s must not be considered as possible separators.
   183 
   216         */
   184     if (aQuery.KeyboardModeL() != EQwerty)
   217 
   185         {
   218        // Skip initial "0"s, they are not replaced into spaces
   186         for (TInt beg = index; beg < aQuery.Count(); ++beg)
   219        TInt skipIndex = 0;
   187             {
   220        while ( (skipIndex < aQuery.Count()) && 
   188             CPsQueryItem& item = aQuery.GetItemAtL(beg);
   221                (aQuery.GetItemAtL(skipIndex).Character().GetNumericValue() == 0) )
   189 
   222        {
   190             if (item.Character().GetNumericValue() == 0 && item.Mode()== EItut)
   223            skipIndex++;
   191                 {
   224        }
   192                 if (beg != 0)
   225        
   193                     {
   226        // Replace remaining "0"s into spaces in case they are entered with a keyboard
   194                     CPsQueryItem& item1 = aQuery.GetItemAtL(beg - 1);
   227        // that has "0" and " " on the same key.
   195                     if (item1.Character().GetNumericValue() != 0
   228        const TInt queryCount = aQuery.Count();
   196                             && !item1.Character().IsSpace())
   229        for ( TInt index = skipIndex; index < queryCount; index++ )
   197                         {
   230        {
   198                         item.SetCharacter(space);
   231            CPsQueryItem& item = aQuery.GetItemAtL(index);
   199                         queryModified = ETrue;
   232 
   200                         }
   233            if ( iKeyMap->GetSpaceAndZeroOnSameKey( item.Mode() ) &&
   201                     }
   234                 item.Character().GetNumericValue() == 0 )
   202                 else
   235            {
   203                     {
   236                item.SetCharacter(KSpace);
   204                     item.SetCharacter(space);
   237                queryModified = ETrue;
   205                     queryModified = ETrue;
   238            }
   206                     }
   239        }
   207                 }
   240        
   208             }
   241        //PRINTQUERY ( _L("CPcsAlgorithm2::ReplaceZeroWithSpaceL (AFTER): "), aQuery );
   209         }
   242 
   210 
   243        PRINT1 ( _L("CPcsAlgorithm2::ReplaceZeroWithSpaceL: Query modified (0=not, 1=yes): %d"), queryModified );
   211     return queryModified;
   244 
       
   245        PRINT ( _L("End CPcsAlgorithm2::ReplaceZeroWithSpaceL") );
       
   246 
       
   247        return  queryModified;
       
   248     }
   212     }
   249 
   213 
   250 // ----------------------------------------------------------------------------
   214 // ----------------------------------------------------------------------------
   251 // CPcsAlgorithm2::PerformSearchL
   215 // CPcsAlgorithm2::PerformSearchL
   252 // Search function for cache
   216 // Search function for cache
   253 // ----------------------------------------------------------------------------
   217 // ----------------------------------------------------------------------------
   254 void CPcsAlgorithm2::PerformSearchL(const CPsSettings& aSettings, 
   218 void CPcsAlgorithm2::PerformSearchL(const CPsSettings& aSettings, CPsQuery& aQuery, 
   255                                     CPsQuery& aQuery, 
       
   256                                     RPointerArray<CPsClientData>& aSearchResults,
   219                                     RPointerArray<CPsClientData>& aSearchResults,
   257                                     RPointerArray<CPsPattern>& aSearchSeqs)
   220                                     RPointerArray<CPsPattern>& aSearchSeqs)
   258     {
   221     {
   259     PRINT ( _L("Enter CPcsAlgorithm2::PerformSearchL") );
   222     PRINT ( _L("Enter CPcsAlgorithm2::PerformSearchL") );
   260 
   223 
   261     //__LATENCY_MARK ( _L("CPcsAlgorithm2::PerformSearchL") );
   224     __LATENCY_MARK ( _L("CPcsAlgorithm2::PerformSearchL") );
   262     
   225 
   263     // Check aSettings   
   226     // Get the current language
   264     RPointerArray<TDesC> searchUris;
   227     TLanguage lang = User::Language();
   265     CleanupResetAndDestroyPushL( searchUris );
   228 
   266     aSettings.SearchUrisL(searchUris);
   229     // Check if the language is supported and the keyboard mode is not qwerty.
   267          
       
   268     if ( searchUris.Count() <= 0)
       
   269     {
       
   270         PRINT ( _L("searchUris.Count() <= 0, Leave from CPcsAlgorithm2::PerformSearchL") );
       
   271         User::Leave(KErrArgument); 
       
   272     }
       
   273     CleanupStack::PopAndDestroy( &searchUris ); // ResetAndDestroy
       
   274 
   230 
   275     // Local arrays to hold the search results 
   231     // Local arrays to hold the search results 
   276     RPointerArray<CPsData> tempSearchResults;
   232     RPointerArray<CPsData> tempSearchResults;
   277     CleanupClosePushL( tempSearchResults );
       
   278     RPointerArray<CPsData> tempSearchResults1;
   233     RPointerArray<CPsData> tempSearchResults1;
   279     CleanupClosePushL( tempSearchResults1 );
       
   280 
   234 
   281     // -------------------- Perform the basic search --------------------------
   235     // -------------------- Perform the basic search --------------------------
   282 
   236 
   283     RemoveSpacesL(aQuery);
       
   284     PRINTQUERY ( _L("CPcsAlgorithm2::PerformSearchL: 1st search query: "), aQuery );
       
   285     DoSearchL(aSettings, aQuery, tempSearchResults, aSearchSeqs);
   237     DoSearchL(aSettings, aQuery, tempSearchResults, aSearchSeqs);
   286 
   238 
   287     // ------------------------------------------------------------------------
   239     // ------------------------------------------------------------------------
   288 
   240 
   289     // ---- Perform new search after "0" replacement if query is not empty ----
   241     // ------------------- Perform advanced search if needed ------------------
   290     /* Examples:
   242     // Substitute "0" with space
   291      * - If the original search string is "Abc0" then we will search again with "Abc".
       
   292      * - If the original search string is "00" then we will not search again.
       
   293      */
       
   294     TBool queryModified = ReplaceZeroWithSpaceL(aQuery);
   243     TBool queryModified = ReplaceZeroWithSpaceL(aQuery);
   295     RemoveSpacesL(aQuery);
   244 
   296     // Perform query again if query got modified. 
   245     // If query got modified and the search query translated to more than 1 query
       
   246     // perform a multi search again
   297     if (queryModified)
   247     if (queryModified)
   298         {
   248         {
   299         PRINTQUERY ( _L("CPcsAlgorithm2::PerformSearchL: 2nd search query: "), aQuery );
   249         // Split query
   300         DoSearchL(aSettings, aQuery, tempSearchResults1, aSearchSeqs);
   250         RPointerArray<CPsQuery> queryList = iMultiSearchHelper->MultiQueryL(aQuery);
   301 
   251 
       
   252         //  Perform seach again
       
   253         if (queryList.Count() > 1)
       
   254             {
       
   255             DoSearchL(aSettings, aQuery, tempSearchResults1, aSearchSeqs);
       
   256             }
   302 
   257 
   303         // Sort rule        
   258         // Sort rule        
   304         TLinearOrder<CPsData> rule(CPcsAlgorithm2Utils::CompareDataBySortOrderL);
   259         TLinearOrder<CPsData> rule(CPcsAlgorithm2Utils::CompareDataBySortOrder);
   305 
   260 
   306         // Avoid duplicates and add new results
   261         // Avoid duplicates and add new results
   307         TIdentityRelation<CPsData> identityRule(CPsData::CompareById);
   262         TIdentityRelation<CPsData> identityRule(CPsData::CompareById);
   308         const TInt tempSearchResults1Count = tempSearchResults1.Count();
       
   309         if (aSettings.GetSortType() != EAlphabetical)
   263         if (aSettings.GetSortType() != EAlphabetical)
   310             {
   264             {
   311             TInt insertPos = 0;
   265             TInt insertPos = 0;
   312             for (TInt i = 0; i < tempSearchResults1Count; i++)
   266             for (int i = 0; i < tempSearchResults1.Count(); i++)
   313                 {
   267                 {
   314                 if (tempSearchResults.Find(tempSearchResults1[i],
   268                 if (tempSearchResults.Find(tempSearchResults1[i],
   315                                            identityRule) == KErrNotFound)
   269                                            identityRule) == KErrNotFound)
   316                     {
   270                     {
   317                     tempSearchResults.Insert(tempSearchResults1[i], insertPos);
   271                     tempSearchResults.Insert(tempSearchResults1[i], insertPos);
   318                     insertPos++;
   272                     insertPos++;
   319                     }
   273                     }
       
   274 
   320                 }
   275                 }
       
   276 
   321             }
   277             }
   322         else
   278         else
   323             {
   279             {
   324             for (TInt i = 0; i < tempSearchResults1Count; i++)
   280 
       
   281             for (int i = 0; i < tempSearchResults1.Count(); i++)
   325                 {
   282                 {
   326                 if (tempSearchResults.Find(tempSearchResults1[i],
   283                 if (tempSearchResults.Find(tempSearchResults1[i],
   327                                            identityRule) == KErrNotFound)
   284                                            identityRule) == KErrNotFound)
   328                     {
   285                     {
   329                     tempSearchResults.InsertInOrderAllowRepeats(tempSearchResults1[i], 
   286                     tempSearchResults.InsertInOrderAllowRepeats(tempSearchResults1[i], 
   330                                                                 rule);
   287                                                                 rule);
   331                     }
   288                     }
   332                 }
   289                 }
   333             }
   290             }
       
   291 
       
   292         queryList.ResetAndDestroy();
   334         }
   293         }
   335     // ------------------------------------------------------------------------
   294     // ------------------------------------------------------------------------
   336 
   295 
   337     // ---------------------- Write result objects to the stream --------------
   296     // ---------------------- Write result objects to the stream --------------
   338     // Truncate the result set if required
   297     // Truncate the result set if required
   339     TInt maxNumToDisplay = aSettings.MaxResults();
   298     TInt numToDisplay = aSettings.MaxResults();
   340     TInt resultSetCount = tempSearchResults.Count();
   299     TInt resultSet = tempSearchResults.Count();
   341     TInt numToDisplay = 0;
   300 
   342     if ( maxNumToDisplay == -1 )
   301     if (resultSet > numToDisplay && numToDisplay != -1)
   343         {
   302         {
   344         numToDisplay = resultSetCount;
   303         // Copy the top N contents from tempSearchResults to the results stream
       
   304         for (int i = 0; i < numToDisplay; i++)
       
   305             {
       
   306             aSearchResults.Append(WriteClientDataL(*(tempSearchResults[i])));
       
   307             }
       
   308 
   345         }
   309         }
   346     else
   310     else
   347         {
   311         {
   348         numToDisplay = Min( maxNumToDisplay, resultSetCount );
   312         // Copy all the contents from tempSearchResults to the results stream
   349         }
   313         for (int i = 0; i < resultSet; i++)
   350 
   314             {
   351     // Copy desired number of results from tempSearchResults to the results stream
   315             aSearchResults.Append(WriteClientDataL(*(tempSearchResults[i])));
   352     for (TInt i = 0; i < numToDisplay; i++)
   316             }
   353         {
       
   354         aSearchResults.Append(WriteClientDataL(*(tempSearchResults[i])));
       
   355         }
   317         }
   356     // ------------------------------------------------------------------------
   318     // ------------------------------------------------------------------------
   357 
   319 
   358     // Cleanup local results array
   320     // Cleanup local results array
   359     CleanupStack::PopAndDestroy( &tempSearchResults1 ); // Close, don't destroy
   321     tempSearchResults.Reset(); // Don't destroy
   360     CleanupStack::PopAndDestroy( &tempSearchResults );  // Close, don't destroy
   322     tempSearchResults1.Reset(); // Don't destroy
   361 
   323 
   362     // __LATENCY_MARKEND ( _L("CPcsAlgorithm2::PerformSearchL") );
   324     __LATENCY_MARKEND ( _L("CPcsAlgorithm2::PerformSearchL") );
   363 
   325 
   364     PRINT ( _L("End CPcsAlgorithm2::PerformSearchL") );
   326     PRINT ( _L("End CPcsAlgorithm2::PerformSearchL") );
   365     }
   327     }
   366 
   328 
   367 // ----------------------------------------------------------------------------
   329 // ----------------------------------------------------------------------------
   368 // CPcsAlgorithm2::SearchInputL
   330 // CPcsAlgorithm2::SearchInputL
   369 // Search function for input string
   331 // Search function for input string
   370 // ----------------------------------------------------------------------------
   332 // ----------------------------------------------------------------------------
   371 void CPcsAlgorithm2::SearchInputL(CPsQuery& aQuery, 
   333 void CPcsAlgorithm2::SearchInputL(CPsQuery& aQuery, TDesC& aData,
   372                                   TDesC& aData,
       
   373                                   RPointerArray<TDesC>& aMatchSet,
   334                                   RPointerArray<TDesC>& aMatchSet,
   374                                   RArray<TPsMatchLocation>& aMatchLocation)
   335                                   RArray<TPsMatchLocation>& aMatchLocation)
   375     {
   336     {
   376     // __LATENCY_MARK ( _L("CPcsAlgorithm2::SearchInputL: ") );
       
   377     PRINT ( _L("Enter CPcsAlgorithm2::SearchInputL") );
   337     PRINT ( _L("Enter CPcsAlgorithm2::SearchInputL") );
   378 
   338 
       
   339     // Get the current language
       
   340     TLanguage lang = User::Language();
       
   341 
       
   342     // Check if the language is supported and the keyboard mode is not qwerty.
       
   343 
   379     // Print input query for debug
   344     // Print input query for debug
   380     PRINTQUERY ( _L("CPcsAlgorithm2::SearchInputL: Search query: "), aQuery );
   345     aQuery.PrintQuery();
   381 
   346 
   382     // Print received search data
   347     // Print received search data
   383     PRINT1 ( _L("Search data received = %S"), &aData);
   348     PRINT1 ( _L("Search data received = %S"), &aData);
   384 
   349 
   385 
       
   386     // -------------------- Perform the basic search --------------------------
   350     // -------------------- Perform the basic search --------------------------
   387 
   351 
   388     RemoveSpacesL(aQuery);
       
   389     DoSearchInputL(aQuery, aData, aMatchSet, aMatchLocation);
   352     DoSearchInputL(aQuery, aData, aMatchSet, aMatchLocation);
   390 
   353 
   391     // ------------------------------------------------------------------------   
   354     // ------------------------------------------------------------------------   
   392 
   355 
   393     // ---- Perform new search after "0" replacement if query is not empty ----
   356     // ------------------- Perform advanced search if needed ------------------
   394     /* Examples:
   357     // Substitute "0" with space
   395      * - If the original search string is "Abc0" then we will search again with "Abc".
       
   396      * - If the original search string is "00" then we will not search again.
       
   397      */
       
   398     TBool queryModified = ReplaceZeroWithSpaceL(aQuery);
   358     TBool queryModified = ReplaceZeroWithSpaceL(aQuery);
   399     RemoveSpacesL(aQuery);
   359 
   400     // If query got modified and the search query still contains something
   360     // If query got modified and the search query translated to more than 1 query
   401     // perform a multi search again
   361     // perform a multi search again
   402     if (queryModified && aQuery.Count() > 0 && aMatchSet.Count() == 0  && aMatchLocation.Count() == 0 )
   362     if (queryModified)
   403         {
   363         {
   404         DoSearchInputL(aQuery, aData, aMatchSet, aMatchLocation);
   364         RPointerArray<CPsQuery> queryList = iMultiSearchHelper->MultiQueryL(aQuery);
       
   365 
       
   366         if (queryList.Count() > 1)
       
   367             {
       
   368             DoSearchInputL(aQuery, aData, aMatchSet, aMatchLocation);
       
   369             }
       
   370 
       
   371         queryList.ResetAndDestroy();
   405         }
   372         }
   406     // ------------------------------------------------------------------------
   373     // ------------------------------------------------------------------------
   407 
       
   408     // --- Remove overlapping items from aMatchLocation ---
       
   409     TInt i = 0;
       
   410     TBool incrementFirstCursor;
       
   411     while ( i < aMatchLocation.Count() )
       
   412         {
       
   413         incrementFirstCursor = ETrue;
       
   414         TInt j = i+1;
       
   415         while ( j < aMatchLocation.Count() )
       
   416             {
       
   417             if ( CPcsAlgorithm2Utils::MatchesOverlap( aMatchLocation[j], aMatchLocation[i] ) )
       
   418                 {
       
   419                 // Remove match location item with smallest length if 2 items have same index
       
   420                 if ( aMatchLocation[j].length <= aMatchLocation[i].length )
       
   421                     {
       
   422                     aMatchLocation.Remove(j);
       
   423                     continue; // Do not increment j
       
   424                     }
       
   425                 else
       
   426                     {
       
   427                     aMatchLocation.Remove(i);
       
   428                     incrementFirstCursor = EFalse; // Do not increment i
       
   429                     break;
       
   430                     }
       
   431                 }
       
   432             j++;
       
   433             }
       
   434         if ( incrementFirstCursor )
       
   435             {
       
   436             i++;
       
   437             }
       
   438         }
       
   439 
       
   440     // --- Remove from aMatchSet items which no longer have corresponding item in aMatchLocation ---
       
   441     HBufC* dataUpper = HBufC::NewLC(aData.Length());
       
   442     dataUpper->Des().Copy(aData);
       
   443     dataUpper->Des().UpperCase(); // Get uppercase, as aMatchSet is in upper case
       
   444 
       
   445     TInt k = 0;
       
   446     while ( k < aMatchSet.Count() )
       
   447         {
       
   448         TBool keepMatch = EFalse;
       
   449         TInt startCursor = -1;
       
   450 
       
   451         TInt offsetCursor;
       
   452         while ( KErrNotFound != (offsetCursor = dataUpper->Mid(startCursor + 1).Find(*aMatchSet[k])) )
       
   453             {
       
   454             // startCursor = index of match item *aMatchSet[k] into search string aData
       
   455             startCursor = offsetCursor + startCursor + 1;
       
   456             const TInt matchLocationCount = aMatchLocation.Count();
       
   457             for ( TInt i = 0; i < matchLocationCount; i++ )
       
   458                 {
       
   459                 // If match item was found in the location list, then keep it
       
   460                 if ( (aMatchLocation[i].index == startCursor) &&
       
   461                      (aMatchLocation[i].length == aMatchSet[k]->Length()) )
       
   462                     {
       
   463                     keepMatch = ETrue;
       
   464                     break;
       
   465                     }
       
   466                 }
       
   467             }
       
   468 
       
   469         if ( keepMatch )
       
   470             {
       
   471             k++;
       
   472             }
       
   473         else
       
   474             {
       
   475             aMatchSet.Remove(k); // Do not increment k
       
   476             }
       
   477         }
       
   478     CleanupStack::PopAndDestroy( dataUpper );
       
   479     // --- Remove items End ---
       
   480 
   374 
   481     // Sort match set
   375     // Sort match set
   482     iHelper->SortSearchSeqsL(aMatchSet);
   376     iHelper->SortSearchSeqsL(aMatchSet);
   483 
   377 
   484     PRINT ( _L("End CPcsAlgorithm2::SearchInputL") );
   378     PRINT ( _L("End CPcsAlgorithm2::SearchInputL") );
   485     //__LATENCY_MARKEND ( _L("CPcsAlgorithm2::SearchInputL") );
       
   486     }
   379     }
   487 
   380 
   488 // ----------------------------------------------------------------------------
   381 // ----------------------------------------------------------------------------
   489 // CPcsAlgorithm2::SearchMatchStringL
   382 // CPcsAlgorithm2::SearchMatchStringL
   490 // Search function for input string, result also as string
   383 // Search function for input string, result also as string
   491 // ----------------------------------------------------------------------------
   384 // ----------------------------------------------------------------------------
   492 void CPcsAlgorithm2::SearchMatchStringL( CPsQuery& aSearchQuery,
   385 void CPcsAlgorithm2::SearchMatchStringL( CPsQuery& /*aSearchQuery*/,
   493                                          TDesC& aSearchData,
   386                                          TDesC& /*aSearchData*/,
   494                                          TDes& aMatch )
   387                                          TDes& /*aMatch*/ )
   495     {
   388     {
   496     PRINT ( _L("Enter CPcsAlgorithm2::SearchMatchStringL") );
   389     //NOT IMPLEMENTED YET
   497 
       
   498     __LATENCY_MARK ( _L("CPcsAlgorithm2::SearchMatchStringL") ); 
       
   499     
       
   500     RemoveSpacesL(aSearchQuery);
       
   501     // ---------------------- Perform the initial search ----------------------
       
   502     iMultiSearchHelper->LookupMatchL( aSearchQuery, aSearchData, aMatch );
       
   503     PRINTQUERY ( _L("CPcsAlgorithm2::SearchMatchStringL: 1st search: "), aSearchQuery );
       
   504     PRINT1     ( _L("CPcsAlgorithm2::SearchMatchStringL: 1st search: Search Data: %S"), &aSearchData );
       
   505     PRINT1     ( _L("CPcsAlgorithm2::SearchMatchStringL: 1st search: Result: %S"), &aMatch );
       
   506     // ------------------------------------------------------------------------
       
   507     
       
   508     // ---- Perform new search after "0" replacement if query is not empty ----
       
   509     /* Examples:
       
   510      * - If the original search string is "Abc0" then we will search again with "Abc".
       
   511      * - If the original search string is "00" then we will not search again.
       
   512      */
       
   513     if ( aMatch.Length() <= 0 )
       
   514     {
       
   515         TBool isQueryModified = ReplaceZeroWithSpaceL(aSearchQuery);
       
   516         RemoveSpacesL(aSearchQuery);
       
   517         if ( isQueryModified && (aSearchQuery.Count() > 0) )
       
   518         {
       
   519             iMultiSearchHelper->LookupMatchL( aSearchQuery, aSearchData, aMatch );
       
   520             PRINTQUERY ( _L("CPcsAlgorithm2::SearchMatchStringL: 2nd search: "), aSearchQuery );
       
   521             PRINT1     ( _L("CPcsAlgorithm2::SearchMatchStringL: 2nd search: Search Data: %S"), &aSearchData );
       
   522             PRINT1     ( _L("CPcsAlgorithm2::SearchMatchStringL: 2nd search: Result: %S"), &aMatch );            
       
   523         }
       
   524     }
       
   525     __LATENCY_MARKEND ( _L("CPcsAlgorithm2::SearchMatchStringL") );
       
   526 
       
   527     PRINT ( _L("End CPcsAlgorithm2::SearchMatchStringL") );
       
   528     }
   390     }
   529 
   391 
   530 // ----------------------------------------------------------------------------
   392 // ----------------------------------------------------------------------------
   531 // CPcsAlgorithm2::DoSearchL
   393 // CPcsAlgorithm2::DoSearchL
   532 // Search function helper
   394 // Search function helper
   533 // ----------------------------------------------------------------------------
   395 // ----------------------------------------------------------------------------
   534 void CPcsAlgorithm2::DoSearchL( const CPsSettings& aSettings, 
   396 void CPcsAlgorithm2::DoSearchL(const CPsSettings& aSettings, CPsQuery& aQuery, 
   535                                 CPsQuery& aQuery, 
   397                                RPointerArray<CPsData>& searchResults,
   536                                 RPointerArray<CPsData>& aSearchResults,
   398                                RPointerArray<CPsPattern>& searchSeqs)
   537                                 RPointerArray<CPsPattern>& aSearchSeqs )
       
   538     {
   399     {
   539     PRINT ( _L("Enter CPcsAlgorithm2::DoSearchL") );
   400     PRINT ( _L("Enter CPcsAlgorithm2::DoSearchL") );
   540 
   401 
   541     //__LATENCY_MARK ( _L("CPcsAlgorithm2::DoSearchL") ); 
   402     // Print query for debug
   542     
   403     aQuery.PrintQuery();
       
   404 
   543     // -(0)----------------- Check if group search is required ---------------    
   405     // -(0)----------------- Check if group search is required ---------------    
   544     RArray<TInt> contactsInGroup;
   406     RArray<TInt> contactsInGroup;
   545     CleanupClosePushL( contactsInGroup );
       
   546     RArray<TInt> groupIdArray;
   407     RArray<TInt> groupIdArray;
   547     CleanupClosePushL( groupIdArray );
       
   548 
   408 
   549     // Create a new settings instance
   409     // Create a new settings instance
   550     CPsSettings* tempSettings = aSettings.CloneL();
   410     CPsSettings *tempSettings = aSettings.CloneL();
   551     CleanupStack::PushL( tempSettings );
       
   552 
   411 
   553     TBool isGroupSearch = IsGroupSearchL(*tempSettings, groupIdArray);
   412     TBool isGroupSearch = IsGroupSearchL(*tempSettings, groupIdArray);
   554 
   413 
   555     if (isGroupSearch)
   414     if (isGroupSearch)
   556         {
   415         {
   559 
   418 
   560         // List of contacts in this group	
   419         // List of contacts in this group	
   561         GetContactsInGroupL(groupIdArray[0], contactsInGroup);
   420         GetContactsInGroupL(groupIdArray[0], contactsInGroup);
   562         }
   421         }
   563 
   422 
       
   423     groupIdArray.Close();
       
   424 
   564     // -----------------------------------------------------------------------
   425     // -----------------------------------------------------------------------
   565 
   426 
   566 
   427 
   567     // Extract query list. 
   428     // Extract query list. 
   568     RPointerArray<CPsQuery> queryList = iMultiSearchHelper->MultiQueryL(aQuery);
   429     RPointerArray<CPsQuery> queryList = iMultiSearchHelper->MultiQueryL(aQuery);
   569     CleanupResetAndDestroyPushL( queryList );
   430 
   570 
   431     // -(1)--------------------- No query return all contacts ----------------    
   571     // (1)-------------------- No query return all contacts -------------------
       
   572     if (queryList.Count() == 0)
   432     if (queryList.Count() == 0)
   573         {
   433         {
   574         GetAllContentsL(*tempSettings, aSearchResults);
   434         GetAllContentsL(*tempSettings, searchResults);
   575 
   435 
   576         if (isGroupSearch)
   436         if (isGroupSearch)
   577             {
   437             {
   578             FilterSearchResultsForGroupsL(contactsInGroup, aSearchResults);
   438             FilterSearchResultsForGroupsL(contactsInGroup, searchResults);
   579             }
   439             }
   580         }
   440         }
   581     // ------------------------------------------------------------------------
   441     // ------------------------------------------------------------------------
   582 
   442 
   583     // (2)-------------------- Perform a single query search ------------------
   443 
   584     else if (queryList.Count() == 1)
   444     // -(2)------------------------ Perform a single query search -------------
   585         {
   445     if (queryList.Count() == 1)
       
   446         {
       
   447         TInt mode = aQuery.KeyboardModeL();
       
   448 
   586         CPsQuery* query = queryList[0];
   449         CPsQuery* query = queryList[0];
   587 
   450 
   588         // Search results
   451         iFindUtilECE->SetKeyboardMode(mode);
   589         iHelper->SearchSingleL(*tempSettings, *query, isGroupSearch,
   452 
   590                                contactsInGroup, aSearchResults, aSearchSeqs);
   453         switch (mode)
       
   454             {
       
   455             case EItut:
       
   456 
       
   457                 PRINT ( _L("Query received is in ITU-T mode") );
       
   458 
       
   459                 // Search results
       
   460                 iHelper->SearchITUL(*tempSettings, *query, isGroupSearch,
       
   461                                     contactsInGroup, searchResults, searchSeqs);
       
   462 
       
   463                 break;
       
   464 
       
   465             case EQwerty:
       
   466 
       
   467                 PRINT ( _L("Query received is in QWERTY mode") );
       
   468 
       
   469                 // Search results
       
   470                 iHelper->SearchQWERTYL(*tempSettings, *query, isGroupSearch,
       
   471                                        contactsInGroup, searchResults, searchSeqs);
       
   472 
       
   473                 break;
       
   474 
       
   475             case EModeUndefined:
       
   476 
       
   477                 PRINT ( _L("Query received is in Mixed mode. Keyboard swap happened.") );
       
   478 
       
   479                 // Search results
       
   480                 iHelper->SearchMixedL(*tempSettings, *query, isGroupSearch,
       
   481                                       contactsInGroup, searchResults, searchSeqs);
       
   482 
       
   483                 break;
       
   484             }
       
   485 
   591         }
   486         }
   592     // ------------------------------------------------------------------------
   487     // ------------------------------------------------------------------------
   593 
   488 
   594     // (3)-------------------- Perform a multi query search -------------------
   489 
   595     else // multiple query
   490     // -(3)---------------------------- Perform a multi query search ----------
       
   491     if (queryList.Count() > 1) // multiple query
   596         {
   492         {
   597         PRINT ( _L("Query received is in multiple. Performing a multi search.") );
   493         PRINT ( _L("Query received is in multiple. Performing a multi search.") );
       
   494 
       
   495         TInt mode = aQuery.KeyboardModeL();
       
   496         iFindUtilECE->SetKeyboardMode(mode);
       
   497 
       
   498         for (int i = 0; i < queryList.Count(); i++)
       
   499             {
       
   500             TPtrC queryPtr = queryList[i]->QueryAsStringLC();
       
   501             PRINT2 ( _L("Received Query, index = %d; value = %S"), i, &queryPtr );
       
   502             CleanupStack::PopAndDestroy();
       
   503             }
   598 
   504 
   599         // Search results
   505         // Search results
   600         iMultiSearchHelper->SearchMultiL(*tempSettings, queryList, isGroupSearch, 
   506         iMultiSearchHelper->SearchMultiL(*tempSettings, queryList, isGroupSearch, 
   601                                          contactsInGroup, aSearchResults, aSearchSeqs);
   507                                          contactsInGroup, searchResults, searchSeqs, 
       
   508                                          mode);
   602         }
   509         }
   603     // -------------------------------------------------------------------------	
   510     // -------------------------------------------------------------------------	
   604 
   511 
   605     // Cleanup
   512     // Cleanup
   606     
   513     delete tempSettings;
   607     CleanupStack::PopAndDestroy( &queryList ); // ResetAndDestroy
   514     tempSettings = NULL;
   608     CleanupStack::PopAndDestroy( tempSettings );
   515 
   609     CleanupStack::PopAndDestroy( &groupIdArray ); // Close
   516     groupIdArray.Close();
   610     CleanupStack::PopAndDestroy( &contactsInGroup ); // Close
   517     contactsInGroup.Close();
   611 
   518     queryList.ResetAndDestroy();
   612 	//__LATENCY_MARKEND ( _L("CPcsAlgorithm2::DoSearchL") );
       
   613 
   519 
   614     PRINT ( _L("End CPcsAlgorithm2::DoSearchL") );
   520     PRINT ( _L("End CPcsAlgorithm2::DoSearchL") );
   615     }
   521     }
   616 
   522 
   617 // ----------------------------------------------------------------------------
   523 // ----------------------------------------------------------------------------
   618 // CPcsAlgorithm2::DoSearchInputL
   524 // CPcsAlgorithm2::DoSearchInputL
   619 // Search function helper
   525 // Search function helper
   620 // ----------------------------------------------------------------------------
   526 // ----------------------------------------------------------------------------
   621 void CPcsAlgorithm2::DoSearchInputL(CPsQuery& aQuery, 
   527 void CPcsAlgorithm2::DoSearchInputL(CPsQuery& aQuery, TDesC& aData,
   622                                     const TDesC& aData,
       
   623                                     RPointerArray<TDesC>& aMatchSet,
   528                                     RPointerArray<TDesC>& aMatchSet,
   624                                     RArray<TPsMatchLocation>& aMatchLocation)
   529                                     RArray<TPsMatchLocation>& aMatchLocation)
   625     {
   530     {
   626 
   531 
   627     //__LATENCY_MARK ( _L("CPcsAlgorithm2::SearchInputL: ") );
       
   628     PRINT ( _L("Enter CPcsAlgorithm2::DoSearchInputL") );
   532     PRINT ( _L("Enter CPcsAlgorithm2::DoSearchInputL") );
   629 
   533 
   630     // Check if any seperator is there in the query
   534     // Check if any seperator is there in the query
   631     RPointerArray<CPsQuery> queryList = iMultiSearchHelper->MultiQueryL(aQuery);
   535     RPointerArray<CPsQuery> queryList = iMultiSearchHelper->MultiQueryL(aQuery);
   632     CleanupResetAndDestroyPushL( queryList );
       
   633 
   536 
   634     // No query    
   537     // No query    
   635     if (queryList.Count() == 0)
   538     if (queryList.Count() == 0)
   636         {
   539         {
   637         PRINT ( _L("Query received is empty") );
   540         PRINT ( _L("Query received is empty") );
   638         CleanupStack::PopAndDestroy( &queryList ); // ResetAndDestroy
   541         return;
   639         return;
   542         }
   640         }
   543 
       
   544     RPointerArray<HBufC> convertedQuery;
       
   545     iMultiSearchHelper->ConvertQueryToListL(queryList, convertedQuery);
   641 
   546 
   642     // Single query
   547     // Single query
   643     if (queryList.Count() == 1)
   548     if (queryList.Count() == 1)
   644         {
   549         {
   645         iHelper->SearchMatchSeqL(aQuery, aData, aMatchSet, aMatchLocation);
   550         iHelper->SearchMatchSeqL(convertedQuery[0], aData, aMatchSet, aQuery,
       
   551                                  aMatchLocation);
       
   552 
   646         }
   553         }
   647 
   554 
   648     if (queryList.Count() > 1) // multiple query
   555     if (queryList.Count() > 1) // multiple query
   649         {
   556         {
   650         PRINT ( _L("Query received is in multiple. Performing a multi search.") );
   557         PRINT ( _L("Query received is in multiple. Performing a multi search.") );
   651 
   558 
       
   559         for (int i = 0; i < queryList.Count(); i++)
       
   560             {
       
   561             TPtrC queryPtr = queryList[i]->QueryAsStringLC();
       
   562             PRINT2 ( _L("Rceived Query, index = %d; value = %S"), i, &queryPtr );
       
   563             CleanupStack::PopAndDestroy();
       
   564             }
       
   565 
   652         // Search results
   566         // Search results
   653         iMultiSearchHelper->SearchMatchSeqMultiL(queryList, 
   567         iMultiSearchHelper->SearchMatchSeqMultiL(queryList, aData, aMatchSet,
   654                                                  aData, 
       
   655                                                  aMatchSet,
       
   656                                                  aMatchLocation);
   568                                                  aMatchLocation);
       
   569 
   657         }
   570         }
   658 
   571 
   659     // Delete all the query elements
   572     // Delete all the query elements
   660     CleanupStack::PopAndDestroy( &queryList ); // ResetAndDestroy
   573     queryList.ResetAndDestroy();
       
   574     convertedQuery.ResetAndDestroy();
       
   575 
   661     PRINT ( _L("End CPcsAlgorithm2::DoSearchInputL") );
   576     PRINT ( _L("End CPcsAlgorithm2::DoSearchInputL") );
   662     //__LATENCY_MARKEND ( _L("CPcsAlgorithm2::SearchInputL") );
       
   663 
       
   664     }
   577     }
   665 
   578 
   666 // ----------------------------------------------------------------------------
   579 // ----------------------------------------------------------------------------
   667 // CPcsAlgorithm2::AddData
   580 // CPcsAlgorithm2::AddData
   668 // Add a data element to the pool
   581 // Add a data element to the pool
   725 
   638 
   726     if (dataStoreIndex < 0)
   639     if (dataStoreIndex < 0)
   727         return;
   640         return;
   728 
   641 
   729     CPcsCache* cache = iPcsCache[dataStoreIndex];
   642     CPcsCache* cache = iPcsCache[dataStoreIndex];
   730     cache->RemoveAllFromCache();
   643     TRAPD(err, cache->RemoveAllFromCacheL());
       
   644 
       
   645     if (err != KErrNone)
       
   646         {
       
   647         SetCachingError(aDataStore, err);
       
   648         }
   731     }
   649     }
   732 
   650 
   733 // ----------------------------------------------------------------------------
   651 // ----------------------------------------------------------------------------
   734 // CPcsAlgorithm2::GetCacheIndex
   652 // CPcsAlgorithm2::GetCacheIndex
   735 // Return the cache index for a data store
   653 // Return the cache index for a data store
   736 // ----------------------------------------------------------------------------
   654 // ----------------------------------------------------------------------------
   737 TInt CPcsAlgorithm2::GetCacheIndex(const TDesC& aDataStore)
   655 TInt CPcsAlgorithm2::GetCacheIndex(TDesC& aDataStore)
   738     {
   656     {
   739     const TInt pcsCacheCount = iPcsCache.Count();
   657     for (int i = 0; i < iPcsCache.Count(); i++)
   740     for (int i = 0; i <pcsCacheCount; i++)
       
   741         {
   658         {
   742         CPcsCache* cache = iPcsCache[i];
   659         CPcsCache* cache = iPcsCache[i];
   743 
   660 
   744         if (cache->GetURI().CompareC(aDataStore) == 0)
   661         if (cache->GetURI().CompareC(aDataStore) == 0)
   745             return i;
   662             return i;
   819 // CPcsAlgorithm2::RemoveDataStore
   736 // CPcsAlgorithm2::RemoveDataStore
   820 // Removes an existing data store
   737 // Removes an existing data store
   821 // ----------------------------------------------------------------------------
   738 // ----------------------------------------------------------------------------
   822 void CPcsAlgorithm2::RemoveDataStore(TDesC& aDataStore)
   739 void CPcsAlgorithm2::RemoveDataStore(TDesC& aDataStore)
   823     {
   740     {
   824 
       
   825     for (int i = 0; i < iPcsCache.Count(); i++)
   741     for (int i = 0; i < iPcsCache.Count(); i++)
   826         {
   742         {
   827         CPcsCache* cache = iPcsCache[i];
   743         CPcsCache* cache = iPcsCache[i];
   828 
   744 
   829         if (cache->GetURI().CompareC(aDataStore) == 0)
   745         if (cache->GetURI().CompareC(aDataStore) == 0)
   846 
   762 
   847 // ----------------------------------------------------------------------------
   763 // ----------------------------------------------------------------------------
   848 // CPcsAlgorithm2::GetUriForIdL
   764 // CPcsAlgorithm2::GetUriForIdL
   849 // Get the URI string for this internal id
   765 // Get the URI string for this internal id
   850 // ----------------------------------------------------------------------------
   766 // ----------------------------------------------------------------------------
   851 const TDesC& CPcsAlgorithm2::GetUriForIdL(TUint8 aUriId)
   767 TDesC& CPcsAlgorithm2::GetUriForIdL(TUint8 aUriId)
   852     {
   768     {
   853     TBool found = EFalse;
   769     TBool found = EFalse;
   854     TInt i = 0;
   770     TInt i = 0;
   855     const TInt pcsCacheCount = iPcsCache.Count();
   771 
   856     for (i = 0; i < pcsCacheCount; i++)
   772     for (i = 0; i < iPcsCache.Count(); i++)
   857         {
   773         {
   858         if (iPcsCache[i]->GetUriId() == aUriId)
   774         if (iPcsCache[i]->GetUriId() == aUriId)
   859             {
   775             {
   860             found = ETrue;
   776             found = ETrue;
   861             break;
   777             break;
   872 
   788 
   873 // ----------------------------------------------------------------------------
   789 // ----------------------------------------------------------------------------
   874 // CPcsAlgorithm2::FindStoreUri
   790 // CPcsAlgorithm2::FindStoreUri
   875 // Checks if this store exists
   791 // Checks if this store exists
   876 // ----------------------------------------------------------------------------
   792 // ----------------------------------------------------------------------------
   877 TInt CPcsAlgorithm2::FindStoreUri(const TDesC& aDataStore)
   793 TInt CPcsAlgorithm2::FindStoreUri(TDesC& aDataStore)
   878     {
   794     {
   879     const TInt pcsCacheCount = iPcsCache.Count();
   795     for (int i = 0; i < iPcsCache.Count(); i++)
   880     for ( TInt i = 0; i < pcsCacheCount; i++ )
   796         {
   881         {
   797         if (aDataStore.CompareC(*(iPcsCache[i]->GetUri())) == 0)
   882         if ( aDataStore.CompareC(*(iPcsCache[i]->GetUri())) == 0 )
       
   883             {
   798             {
   884             return i;
   799             return i;
   885             }
   800             }
   886         }
   801         }
   887 
   802 
   888     return KErrNotFound;
   803     return -1;
   889     }
   804     }
   890 
   805 
   891 // ----------------------------------------------------------------------------
   806 // ----------------------------------------------------------------------------
   892 // CPcsAlgorithm2::UpdateCachingStatus
   807 // CPcsAlgorithm2::UpdateCachingStatus
   893 // Update caching status
   808 // Update caching status
   894 // ----------------------------------------------------------------------------
   809 // ----------------------------------------------------------------------------
   895 void CPcsAlgorithm2::UpdateCachingStatus(TDesC& aDataStore, TInt aStatus)
   810 void CPcsAlgorithm2::UpdateCachingStatus(TDesC& aDataStore, TInt aStatus)
   896 {
   811     {
   897     PRINT ( _L("Enter CPcsAlgorithm2::UpdateCachingStatus") );
   812     PRINT ( _L("Enter CPcsAlgorithm2::UpdateCachingStatus") );
   898     PRINT2 ( _L("CPcsAlgorithm2::UpdateCachingStatus: Request received for URI=%S with status=%d"),
       
   899              &aDataStore, aStatus );
       
   900 
   813 
   901     // Handle data store update events
   814     // Handle data store update events
   902     if ( aStatus == ECacheUpdateContactRemoved ||
   815     if ( aStatus == ECacheUpdateContactRemoved ||
   903          aStatus == ECacheUpdateContactModified ||
   816          aStatus == ECacheUpdateContactModified ||
   904          aStatus == ECacheUpdateContactAdded )
   817          aStatus == ECacheUpdateContactAdded )
   907         return;
   820         return;
   908         }
   821         }
   909 
   822 
   910     // If not a cache update event, then this event is related to the initial
   823     // If not a cache update event, then this event is related to the initial
   911     // cache construction.
   824     // cache construction.
   912 
   825     TInt index = FindStoreUri(aDataStore);
   913     // Check if any error occurred and update the cache error
   826     iPcsCache[index]->UpdateCacheStatus(aStatus);
   914     if ( aStatus < 0 )
   827 
   915     {
   828     // Check if any error occurred
       
   829     // If so, update the cache status, Set the property and return
       
   830     if (aStatus < 0)
       
   831         {
   916         SetCachingError(aDataStore, aStatus);
   832         SetCachingError(aDataStore, aStatus);
   917     }
   833         //return;
   918     else
   834         }
   919     {
   835 
   920         TInt index = FindStoreUri(aDataStore);
   836     //store the index for firstname and lastname
   921         iPcsCache[index]->UpdateCacheStatus(aStatus);
   837     if (aStatus == ECachingComplete)
   922     }
   838         {
   923 
   839         RArray<TInt> dataFields;
   924     TCachingStatus status = ECachingNotStarted;
   840         iPcsCache[index]->GetDataFields(dataFields);
   925     TUint countNotStarted = 0;
   841 
   926     TUint countInProgress = 0;
   842         for (int i = 0; i < dataFields.Count(); i++)
   927     TUint countCompleted = 0;
   843             {
   928     TUint countCompletedWithErrors = 0;
   844             if (dataFields[i] == R_VPBK_FIELD_TYPE_FIRSTNAME)
   929     TInt cacheCount = iPcsCache.Count();
   845                 {
   930     for ( TInt i = 0; i < cacheCount; i++ )
   846                 iFirstNameIndex = i;
   931     {
   847                 }
   932         PRINT3 ( _L("CPcsAlgorithm2::UpdateCachingStatus: URI[%d]=%S, cache status=%d"),
   848             else if (dataFields[i] == R_VPBK_FIELD_TYPE_LASTNAME)
   933                  i, &iPcsCache[i]->GetURI(), iPcsCache[i]->GetCacheStatus() );
   849                 {
   934 
   850                 iLastNameIndex = i;
   935         switch ( iPcsCache[i]->GetCacheStatus() )
   851                 }
   936         {
   852             }
   937             case ECachingNotStarted:         
   853         }
   938             {
   854 
   939                 countNotStarted++;          
   855     // No error occurred
   940                 break;
   856     TCachingStatus status = ECachingComplete;
   941             }
   857     TBool atLeastOneStoreCachingCompleteWithErrors(EFalse);
   942             case ECachingInProgress:         
   858     for (TInt i = 0; i < iPcsCache.Count(); i++)
   943             {
   859         {
   944                 countInProgress++;         
   860         if (iPcsCache[i]->GetCacheStatus() == ECachingComplete)
   945                 break;
   861             {
   946             }
   862             continue;
   947             case ECachingComplete:           
   863             }
   948             {
   864         else if (iPcsCache[i]->GetCacheStatus() == ECachingCompleteWithErrors)
   949                 countCompleted++;           
   865             {
   950                 break;
   866             atLeastOneStoreCachingCompleteWithErrors = ETrue;
   951             }
   867             continue;
   952             case ECachingCompleteWithErrors: 
   868             }
   953             {
   869         else
   954                 countCompletedWithErrors++; 
   870             {
   955                 break;
   871             status = ECachingInProgress;
   956             }
   872             break;
   957             default:                         
   873             }
   958             { 
   874         }
   959                 // Default completed state
   875 
   960                 countCompleted++;           
   876     if (status == ECachingComplete)
   961                 break;
   877         {
   962             }
   878         // See if any error occurred while caching
   963         }
   879         // If so, change the status to ECachingCompleteWithErrors
   964     }
   880         if ((iCacheError != KErrNone) || (atLeastOneStoreCachingCompleteWithErrors))
   965 
   881             {
   966     // Calculate cumulative status according to single caches statuses
   882             status = ECachingCompleteWithErrors;
   967     if ( countCompleted > 0 && ( countCompleted + countNotStarted ) == cacheCount )
   883             }
   968     {
   884         }
   969         // If at least one caching is finished
       
   970         // set status to ECachingComplete or ECachingCompleteWithErrors
       
   971         // according to iCacheError
       
   972         status = ( iCacheError == KErrNone ) ? ECachingComplete : ECachingCompleteWithErrors;
       
   973     }
       
   974     else if ( countInProgress > 0 )
       
   975     {
       
   976         // Else if at least one caching is in progress,
       
   977         // set status to ECachingInProgress
       
   978         status = ECachingInProgress;
       
   979     }
       
   980     else if ( countCompletedWithErrors > 0 )
       
   981     {
       
   982         // Else if at least one caching is completed with errors, 
       
   983         //set status to ECachingCompleteWithErrors
       
   984         status = ECachingCompleteWithErrors;
       
   985     }
       
   986     else
       
   987     {
       
   988         // countNotStarted == cacheCount
       
   989         // status is set to default ECachingNotStarted
       
   990     }
       
   991 
       
   992     PRINT1 ( _L("CPcsAlgorithm2::UpdateCachingStatus: Cumulative caching status is %d"),
       
   993              status );
       
   994 
   885 
   995     // Check if status changed
   886     // Check if status changed
   996     if ( status != iCacheStatus )
   887     if (status != iCacheStatus)
   997     {
   888         {
   998         PRINT2 ( _L("CPcsAlgorithm2::UpdateCachingStatus: Cumulative caching changed: %d -> %d"),
       
   999                  iCacheStatus, status );
       
  1000 
       
  1001         iCacheStatus = status;
   889         iCacheStatus = status;
  1002         RProperty::Set(KPcsInternalUidCacheStatus, EPsKeyCacheStatus, iCacheStatus );
   890         RProperty::Set(KPcsInternalUidCacheStatus, EPsKeyCacheStatus, iCacheStatus );
  1003     }
   891         }
  1004 
   892 
  1005     PRINT( _L("End CPcsAlgorithm2::UpdateCachingStatus") );
   893     PRINT ( _L("End CPcsAlgorithm2::UpdateCachingStatus") );
  1006 }
   894     }
  1007 
       
  1008 
   895 
  1009 // ----------------------------------------------------------------------------
   896 // ----------------------------------------------------------------------------
  1010 // CPcsAlgorithm2::SetCachingError
   897 // CPcsAlgorithm2::SetCachingError
  1011 // Updates cachinge error
   898 // Updates cachinge error
  1012 // ----------------------------------------------------------------------------
   899 // ----------------------------------------------------------------------------
  1013 void CPcsAlgorithm2::SetCachingError(const TDesC& aDataStore, TInt aError)
   900 void CPcsAlgorithm2::SetCachingError(TDesC& aDataStore, TInt aError)
  1014     {
   901     {
  1015     PRINT2 ( _L("SetCachingError::URI %S ERROR %d"), &aDataStore, aError );
   902     PRINT2 ( _L("SetCachingError::URI %S ERROR %d"), &aDataStore, aError );
  1016 
   903 
  1017     iCacheError = aError;
   904     iCacheError = aError;
  1018     RProperty::Set( KPcsInternalUidCacheStatus, EPsKeyCacheError, iCacheError );
   905     RProperty::Set( KPcsInternalUidCacheStatus, EPsKeyCacheError, iCacheError );
  1023 // Returns all the contents of a store
   910 // Returns all the contents of a store
  1024 // ----------------------------------------------------------------------------
   911 // ----------------------------------------------------------------------------
  1025 void CPcsAlgorithm2::GetAllContentsL(const CPsSettings& aSettings,
   912 void CPcsAlgorithm2::GetAllContentsL(const CPsSettings& aSettings,
  1026                                      RPointerArray<CPsData>& aResults)
   913                                      RPointerArray<CPsData>& aResults)
  1027     {
   914     {
  1028     //__LATENCY_MARK ( _L("CPcsAlgorithm2::GetAllContentsL") );
   915     __LATENCY_MARK ( _L("CPcsAlgorithm2::GetAllContentsL") );
  1029 
   916 
  1030     PRINT ( _L("Enter CPcsAlgorithm2::GetAllContentsL") );
   917     PRINT ( _L("Enter CPcsAlgorithm2::GetAllContentsL") );
       
   918 
       
   919     // Get the data stores
       
   920     RPointerArray<TDesC> aDataStores;
       
   921     aSettings.SearchUrisL(aDataStores);
  1031 
   922 
  1032     // To hold array of results from different data stores
   923     // To hold array of results from different data stores
  1033     typedef RPointerArray<CPsData> CPSDATA_R_PTR_ARRAY;
   924     typedef RPointerArray<CPsData> CPSDATA_R_PTR_ARRAY;
  1034     RPointerArray<CPSDATA_R_PTR_ARRAY> searchResultsArr;
   925     RPointerArray<CPSDATA_R_PTR_ARRAY> iSearchResultsArr;
  1035     CleanupResetAndDestroyPushL( searchResultsArr );
       
  1036     // TODO: Here's still a potential memory leak if a leave happens. The child
       
  1037     // arrays of searchResultsArr are not Reset in that case. The CPsData objects
       
  1038     // may leak as well. Handling this safely is somewhat complicated because some of
       
  1039     // the CPsData objects may already be transferred to ownership of aResults array
       
  1040     // at the time the leave happens, and those items must not be deleted.
       
  1041     
       
  1042     // Get the data stores
       
  1043     RPointerArray<TDesC> dataStores;
       
  1044     CleanupResetAndDestroyPushL( dataStores );
       
  1045     aSettings.SearchUrisL(dataStores);
       
  1046 
   926 
  1047     // Get all contacts for each data store
   927     // Get all contacts for each data store
  1048     const TInt dataStoresCount = dataStores.Count(); 
   928     for (int dsIndex = 0; dsIndex < aDataStores.Count(); dsIndex++)
  1049     for (TInt dsIndex = 0; dsIndex < dataStoresCount; dsIndex++)
       
  1050         {
   929         {
  1051         RPointerArray<CPsData> *temp = new (ELeave) RPointerArray<CPsData> ();
   930         RPointerArray<CPsData> *temp = new (ELeave) RPointerArray<CPsData> ();
  1052         searchResultsArr.Append(temp);
   931         iSearchResultsArr.Append(temp);
  1053 
   932 
  1054         TInt arrayIndex = GetCacheIndex(*(dataStores[dsIndex]));
   933         TInt arrayIndex = GetCacheIndex(*(aDataStores[dsIndex]));
  1055         if (arrayIndex < 0)
   934         if (arrayIndex < 0)
  1056             {
   935             {
  1057             continue;
   936             continue;
  1058             }
   937             }
  1059 
   938 
  1060         CPcsCache* cache = GetCache(arrayIndex);
   939         CPcsCache* cache = GetCache(arrayIndex);
  1061 
   940 
  1062         cache->GetAllContentsL(*(searchResultsArr[dsIndex]));
   941         cache->GetAllContentsL(*(iSearchResultsArr[dsIndex]));
  1063         }
   942         }
  1064 
   943 
  1065     CleanupStack::PopAndDestroy( &dataStores ); // ResetAndDestroy
   944     aDataStores.ResetAndDestroy();
  1066 
   945 
  1067     // Merge the results from different data stores
   946     // Merge the results from different data stores
  1068     CPcsAlgorithm2Utils::FormCompleteSearchResultsL(searchResultsArr, aResults);
   947     CPcsAlgorithm2Utils::FormCompleteSearchResultsL(iSearchResultsArr, aResults);
  1069 
   948 
  1070     // Cleanup the local arrays
   949     // Cleanup the local arrays
  1071     const TInt seaerchResultsArrCount = searchResultsArr.Count(); 
   950     for (TInt i = 0; i < iSearchResultsArr.Count(); i++)
  1072     for (TInt i = 0; i < seaerchResultsArrCount; i++)
   951         {
  1073         {
   952         iSearchResultsArr[i]->Reset();
  1074         searchResultsArr[i]->Reset();
   953         delete iSearchResultsArr[i];
  1075         }
   954         iSearchResultsArr[i] = NULL;
  1076 
   955         }
  1077     CleanupStack::PopAndDestroy( &searchResultsArr ); // ResetAndDestroy
   956 
       
   957     iSearchResultsArr.Reset();
  1078 
   958 
  1079     PRINT1 ( _L("Number of results = %d"), aResults.Count() );
   959     PRINT1 ( _L("Number of results = %d"), aResults.Count() );
  1080 
   960 
  1081     PRINT ( _L("End CPcsAlgorithm2::GetAllContentsL") );
   961     PRINT ( _L("End CPcsAlgorithm2::GetAllContentsL") );
  1082 
   962 
  1083     //__LATENCY_MARKEND ( _L("CPcsAlgorithm2::GetAllContentsL") );
   963     __LATENCY_MARKEND ( _L("CPcsAlgorithm2::GetAllContentsL") );
  1084     }
   964     }
  1085 
   965 
  1086 // ----------------------------------------------------------------------------
   966 // ----------------------------------------------------------------------------
  1087 // CPcsAlgorithm2::IsGroupSearchL
   967 // CPcsAlgorithm2::IsGroupSearchL
  1088 // Checks if a group search is required
   968 // Checks if a group search is required
  1095     // Get the groupIds in the seach settings
   975     // Get the groupIds in the seach settings
  1096     aSettings.GetGroupIdsL(aGroupIdArray);
   976     aSettings.GetGroupIdsL(aGroupIdArray);
  1097 
   977 
  1098     // Get the current URIs defined in settings    
   978     // Get the current URIs defined in settings    
  1099     RPointerArray<TDesC> searchUris;
   979     RPointerArray<TDesC> searchUris;
  1100     CleanupResetAndDestroyPushL( searchUris );
       
  1101     aSettings.SearchUrisL(searchUris);
   980     aSettings.SearchUrisL(searchUris);
  1102 
   981 
  1103     if (aGroupIdArray.Count() && (searchUris.Count() > aGroupIdArray.Count()))
   982     if (aGroupIdArray.Count() && (searchUris.Count() > aGroupIdArray.Count()))
  1104         {
   983         {
  1105         // There is an error, either there are more than one groups
   984         // There is an error, either there are more than one groups
  1106         // or the settings contain a combination of group/non-group Uris
   985         // or the settings contain a combination of group/non-group Uris
       
   986         searchUris.ResetAndDestroy();
  1107         aGroupIdArray.Close();
   987         aGroupIdArray.Close();
  1108         User::Leave(KErrArgument);
   988         User::Leave(KErrArgument);
  1109         }
   989         }
  1110 
   990 
  1111     CleanupStack::PopAndDestroy( &searchUris ); // ResetAndDestroy
   991     searchUris.ResetAndDestroy();
  1112 
   992 
  1113     PRINT ( _L("End CPcsAlgorithm2::IsGroupSearchL") );
   993     PRINT ( _L("End CPcsAlgorithm2::IsGroupSearchL") );
  1114 
   994 
  1115     if (aGroupIdArray.Count() == 1)
   995     if (aGroupIdArray.Count() == 1)
  1116         return ETrue;
   996         return ETrue;
  1123 // Replace groups uri to contacts uri
  1003 // Replace groups uri to contacts uri
  1124 // ----------------------------------------------------------------------------
  1004 // ----------------------------------------------------------------------------
  1125 void CPcsAlgorithm2::ReplaceGroupsUriL(CPsSettings& aSettings)
  1005 void CPcsAlgorithm2::ReplaceGroupsUriL(CPsSettings& aSettings)
  1126     {
  1006     {
  1127     RPointerArray<TDesC> uri;
  1007     RPointerArray<TDesC> uri;
  1128     CleanupResetAndDestroyPushL( uri );
       
  1129 
  1008 
  1130     // Set contacts db uri
  1009     // Set contacts db uri
  1131     HBufC* cntdb = KVPbkDefaultCntDbURI().AllocLC();
  1010     HBufC* cntdb = HBufC::NewL(KBufferMaxLen);
  1132     uri.AppendL(cntdb);
  1011     cntdb->Des().Copy(KVPbkDefaultCntDbURI);
  1133     CleanupStack::Pop( cntdb ); // ownership transferred
  1012     uri.Append(cntdb);
  1134     aSettings.SetSearchUrisL(uri);
  1013     aSettings.SetSearchUrisL(uri);
  1135 
  1014 
  1136     // Cleanup
  1015     // Cleanup
  1137     CleanupStack::PopAndDestroy( &uri ); // ResetAndDestroy
  1016     uri.ResetAndDestroy();
  1138     }
  1017     }
  1139 
  1018 
  1140 // ----------------------------------------------------------------------------
  1019 // ----------------------------------------------------------------------------
  1141 // CPcsAlgorithm2::FilterSearchResultsForGroupsL
  1020 // CPcsAlgorithm2::FilterSearchResultsForGroupsL
  1142 // Filters the results that belong to a group
  1021 // Filters the results that belong to a group
  1174 // ----------------------------------------------------------------------------
  1053 // ----------------------------------------------------------------------------
  1175 void CPcsAlgorithm2::GetContactsInGroupL(TInt aGroupId, RArray<TInt>& aGroupContactIds)
  1054 void CPcsAlgorithm2::GetContactsInGroupL(TInt aGroupId, RArray<TInt>& aGroupContactIds)
  1176     {
  1055     {
  1177     // Clear results array
  1056     // Clear results array
  1178     aGroupContactIds.Reset();
  1057     aGroupContactIds.Reset();
  1179     
  1058 
  1180     // Cache Index for group database
  1059     // Groups URI
  1181     TInt cacheIndex = GetCacheIndex(KVPbkDefaultGrpDbURI);
  1060     HBufC* groupURI = HBufC::NewL(50);
  1182     
  1061     groupURI->Des().Copy(KVPbkDefaultGrpDbURI);
       
  1062 
       
  1063     // Cache Index   
       
  1064     TInt cacheIndex = GetCacheIndex(*groupURI);
       
  1065 
       
  1066     // Cleanup
       
  1067     delete groupURI;
       
  1068     groupURI = NULL;
       
  1069 
  1183     // Get the groups contact ids 
  1070     // Get the groups contact ids 
  1184     if (cacheIndex != -1)
  1071     if (cacheIndex != -1)
  1185         {
  1072         {
  1186         RPointerArray<CPsData> groups;
  1073         RPointerArray<CPsData> groups;
  1187         CleanupClosePushL( groups );
       
  1188 
  1074 
  1189         // Get all groups
  1075         // Get all groups
  1190         iPcsCache[cacheIndex]->GetAllContentsL(groups);
  1076         iPcsCache[cacheIndex]->GetAllContentsL(groups);
  1191 
  1077 
  1192         // Get all contacts in group
  1078         // Get all contacts in group
  1193         const TInt groupsCount = groups.Count(); 
  1079         for (TInt i = 0; i < groups.Count(); i++)
  1194         for (TInt i = 0; i < groupsCount; i++)
       
  1195             {
  1080             {
  1196             if (groups[i]->Id() == aGroupId)
  1081             if (groups[i]->Id() == aGroupId)
  1197                 {
  1082                 {
  1198                 groups[i]->IntDataExt(aGroupContactIds); // All contacts in group
  1083                 groups[i]->IntDataExt(aGroupContactIds); // All contacts in group
  1199                 break;
  1084                 break;
  1200                 }
  1085                 }
  1201             }
  1086             }
  1202 
  1087 
  1203         CleanupStack::PopAndDestroy( &groups ); // Close
  1088         groups.Reset();
  1204         }
  1089         }
  1205     }
  1090     }
  1206 
  1091 
  1207 // ----------------------------------------------------------------------------
  1092 // ----------------------------------------------------------------------------
  1208 // CPcsAlgorithm2::GetDataOrderL
  1093 // CPcsAlgorithm2::GetDataOrderL
  1215     TInt arrayIndex = -1;
  1100     TInt arrayIndex = -1;
  1216 
  1101 
  1217     if (CPcsAlgorithm2Utils::IsGroupUri(aURI))
  1102     if (CPcsAlgorithm2Utils::IsGroupUri(aURI))
  1218         {
  1103         {
  1219         // If search in a group uri, use contacts db
  1104         // If search in a group uri, use contacts db
  1220         arrayIndex = GetCacheIndex(KVPbkDefaultCntDbURI);
  1105         TBuf<255> cntdb(KVPbkDefaultCntDbURI);
       
  1106         arrayIndex = GetCacheIndex(cntdb);
  1221         }
  1107         }
  1222     else
  1108     else
  1223         {
  1109         {
  1224         arrayIndex = GetCacheIndex(aURI);
  1110         arrayIndex = GetCacheIndex(aURI);
  1225         }
  1111         }
  1250     TInt arrayIndex = -1;
  1136     TInt arrayIndex = -1;
  1251 
  1137 
  1252     if (CPcsAlgorithm2Utils::IsGroupUri(aURI))
  1138     if (CPcsAlgorithm2Utils::IsGroupUri(aURI))
  1253         {
  1139         {
  1254         // If search in a group uri, use contacts db
  1140         // If search in a group uri, use contacts db
  1255         arrayIndex = GetCacheIndex(KVPbkDefaultCntDbURI);
  1141         TBuf<255> cntdb(KVPbkDefaultCntDbURI);
       
  1142         arrayIndex = GetCacheIndex(cntdb);
  1256         }
  1143         }
  1257     else
  1144     else
  1258         {
  1145         {
  1259         arrayIndex = GetCacheIndex(aURI);
  1146         arrayIndex = GetCacheIndex(aURI);
  1260         }
  1147         }
  1308     cache->GetSortOrder(mySortOrder);
  1195     cache->GetSortOrder(mySortOrder);
  1309 
  1196 
  1310     if (aSortOrder.Count() == mySortOrder.Count())
  1197     if (aSortOrder.Count() == mySortOrder.Count())
  1311         {
  1198         {
  1312         TBool same = ETrue;
  1199         TBool same = ETrue;
  1313         const TInt mySourtOrderCount = mySortOrder.Count(); 
  1200         for (int i = 0; i < mySortOrder.Count(); i++)
  1314         for ( TInt i = 0; i < mySourtOrderCount ; i++ )
       
  1315             {
  1201             {
  1316             if (mySortOrder[i] != aSortOrder[i])
  1202             if (mySortOrder[i] != aSortOrder[i])
  1317                 {
  1203                 {
  1318                 same = EFalse;
  1204                 same = EFalse;
  1319                 break;
  1205                 break;
  1342     // Request for data again
  1228     // Request for data again
  1343     TInt err = KErrNone;
  1229     TInt err = KErrNone;
  1344     TRAP(err, cache->ResortdataInPoolsL());
  1230     TRAP(err, cache->ResortdataInPoolsL());
  1345     if (err != KErrNone)
  1231     if (err != KErrNone)
  1346         {
  1232         {
  1347         PRINT ( _L("CPcsAlgorithm2::ChangeSortOrderL() Set Caching Error ") );
  1233         PRINT ( _L("CPcsAlgorithm1::ChangeSortOrderL() Set Caching Error ") );
  1348         SetCachingError(aURI, err);
  1234         SetCachingError(aURI, err);
  1349         UpdateCachingStatus(aURI, ECachingCompleteWithErrors);
  1235         UpdateCachingStatus(aURI, ECachingCompleteWithErrors);
  1350         return;
  1236         return;
  1351         }
  1237         }
  1352 
  1238 
  1353     PRINT ( _L("End CPcsAlgorithm2::ChangeSortOrderL.") );
  1239     PRINT ( _L("End CPcsAlgorithm2::ChangeSortOrderL.") );
       
  1240     }
       
  1241 
       
  1242 // ----------------------------------------------------------------------------
       
  1243 // CPcsAlgorithm2::GetAdaptiveGridL
       
  1244 // 
       
  1245 // ----------------------------------------------------------------------------
       
  1246 void CPcsAlgorithm2::GetAdaptiveGridL( const MDesCArray& /*aURIs*/,
       
  1247                                        const TBool /*aCompanyName*/,
       
  1248                                        TDes& /*aAdaptiveGrid*/ )
       
  1249     {
       
  1250     //NOT IMPLEMENTED YET
  1354     }
  1251     }
  1355 
  1252 
  1356 // ---------------------------------------------------------------------------------
  1253 // ---------------------------------------------------------------------------------
  1357 // Read the persisted sort order from the central repository
  1254 // Read the persisted sort order from the central repository
  1358 // Persisted sort order is of form URI Field1 Field2 Field3 .. FieldN (space delimited)
  1255 // Persisted sort order is of form URI Field1 Field2 Field3 .. FieldN (space delimited)
  1359 // ---------------------------------------------------------------------------------
  1256 // ---------------------------------------------------------------------------------
  1360 void CPcsAlgorithm2::ReadSortOrderFromCenRepL(const TDesC& aURI, RArray<TInt>& aSortOrder)
  1257 void CPcsAlgorithm2::ReadSortOrderFromCenRepL(TDesC& aURI, RArray<TInt>& aSortOrder)
  1361     {
  1258     {
  1362     PRINT ( _L("Enter CPcsAlgorithm2::ReadSortOrderFromCenRepL.") );
  1259     PRINT ( _L("Enter CPcsAlgorithm2::ReadSortOrderFromCenRepL.") );
  1363 
  1260 
  1364     aSortOrder.Reset();
  1261     aSortOrder.Reset();
  1365 
  1262 
  1366     CRepository *repository = CRepository::NewL(KCRUidPSSortOrder);
  1263     CRepository *repository = CRepository::NewL(KCRUidPSSortOrder);
  1367 
  1264 
  1368     // Read the sort order from cenrep
  1265     // Read the sort order from cenrep
  1369     TBuf<KCRMaxLen> str;
  1266     TBuf<KCRMaxLen> str;
  1370 
  1267 
  1371     for ( TInt i(KCenrepFieldsStartKey); 
  1268     for (TInt i(KCenrepFieldsStartKey); i < KCenrepFieldsStartKey
  1372           i < KCenrepFieldsStartKey + KCenrepNumberOfFieldsCount; 
  1269             + KCenrepNumberOfFieldsCount; i++)
  1373           i++ )
       
  1374         {
  1270         {
  1375         TInt err = repository->Get(i, str);
  1271         TInt err = repository->Get(i, str);
  1376 
  1272 
  1377         if (KErrNone != err)
  1273         if (KErrNone != err)
  1378             {
  1274             {
  1420 
  1316 
  1421 // ---------------------------------------------------------------------------------
  1317 // ---------------------------------------------------------------------------------
  1422 // Write the sort order into the central repository
  1318 // Write the sort order into the central repository
  1423 // Persisted sort order is of form URI Field1 Field2 Field3 .. FieldN (space delimited)
  1319 // Persisted sort order is of form URI Field1 Field2 Field3 .. FieldN (space delimited)
  1424 // ---------------------------------------------------------------------------------
  1320 // ---------------------------------------------------------------------------------
  1425 void CPcsAlgorithm2::WriteSortOrderToCenRepL(const TDesC& aURI, RArray<TInt>& aSortOrder)
  1321 void CPcsAlgorithm2::WriteSortOrderToCenRepL(TDesC& aURI, RArray<TInt>& aSortOrder)
  1426     {
  1322     {
  1427     PRINT ( _L("Enter CPcsAlgorithm2::WriteSortOrderToCenRepL.") );
  1323     PRINT ( _L("Enter CPcsAlgorithm2::WriteSortOrderToCenRepL.") );
  1428 
  1324 
  1429     CRepository* repository = CRepository::NewLC(KCRUidPSSortOrder);
  1325     CRepository *repository = CRepository::NewL(KCRUidPSSortOrder);
  1430 
  1326 
  1431     // Check if there an entry for this URI in cenrep
  1327     // Check if there an entry for this URI in cenrep
  1432     TBuf<KCRMaxLen> str;
  1328     TBuf<KCRMaxLen> str;
  1433     TInt keyIndex = -1;
  1329     TInt keyIndex = -1;
  1434 
  1330 
  1435     for ( TInt i(KCenrepFieldsStartKey); 
  1331     for (TInt i(KCenrepFieldsStartKey); i < KCenrepFieldsStartKey
  1436           i < KCenrepFieldsStartKey + KCenrepNumberOfFieldsCount; 
  1332             + KCenrepNumberOfFieldsCount; i++)
  1437           i++ )
       
  1438         {
  1333         {
  1439         TInt err = repository->Get(i, str);
  1334         TInt err = repository->Get(i, str);
  1440 
  1335 
  1441         if (KErrNone != err)
  1336         if (KErrNone != err)
  1442             {
  1337             {
  1462     // No entry for this URI in cenrep
  1357     // No entry for this URI in cenrep
  1463     // Find the next free location in cenrep
  1358     // Find the next free location in cenrep
  1464     if (keyIndex == -1)
  1359     if (keyIndex == -1)
  1465         {
  1360         {
  1466         // Find the next free key index
  1361         // Find the next free key index
  1467         for ( TInt i(KCenrepFieldsStartKey); 
  1362         for (TInt i(KCenrepFieldsStartKey); i < KCenrepFieldsStartKey
  1468               i < KCenrepFieldsStartKey + KCenrepNumberOfFieldsCount; 
  1363                 + KCenrepNumberOfFieldsCount; i++)
  1469               i++ )
       
  1470             {
  1364             {
  1471             TInt err = repository->Get(i, str);
  1365             TInt err = repository->Get(i, str);
  1472 
  1366 
  1473             if (KErrNone != err)
  1367             if (KErrNone != err)
  1474                 {
  1368                 {
  1489         PRINT ( _L("CPcsAlgorithm2::WriteSortOrderToCenRepL. Persist limit violated.") );
  1383         PRINT ( _L("CPcsAlgorithm2::WriteSortOrderToCenRepL. Persist limit violated.") );
  1490         return;
  1384         return;
  1491         }
  1385         }
  1492 
  1386 
  1493     // Persist the sort order
  1387     // Persist the sort order
  1494     HBufC* str1 = HBufC::NewLC(KCRMaxLen);
  1388     HBufC* str1 = HBufC::NewL(KCRMaxLen);
  1495     TPtr ptr(str1->Des());
  1389     TPtr ptr(str1->Des());
  1496 
  1390 
  1497     // Append the URI
  1391     // Append the URI
  1498     ptr.Append(aURI);
  1392     ptr.Append(aURI);
  1499     ptr.Append(KSpace);
  1393     ptr.Append(KSpace);
  1500 
  1394 
  1501     // Append the sort order fields
  1395     // Append the sort order fields
  1502     const TInt sortOrderCount =  aSortOrder.Count();
  1396     for (int j = 0; j < aSortOrder.Count(); j++)
  1503     for (TInt j = 0; j < sortOrderCount; j++)
       
  1504         {
  1397         {
  1505         ptr.AppendNum(aSortOrder[j]);
  1398         ptr.AppendNum(aSortOrder[j]);
  1506         ptr.Append(KSpace);
  1399         ptr.Append(KSpace);
  1507         }
  1400         }
  1508 
  1401 
  1509     // Write to persistent store
  1402     // Write to persistent store
  1510     TInt err = repository->Set(keyIndex, ptr);
  1403     TInt err = repository->Set(keyIndex, ptr);
  1511 
  1404 
  1512     User::LeaveIfError(err);
  1405     User::LeaveIfError(err);
  1513 
  1406 
  1514     CleanupStack::PopAndDestroy( str1 );
  1407     delete str1;
  1515 
  1408 
  1516     CleanupStack::PopAndDestroy( repository );
  1409     delete repository;
  1517 
  1410 
  1518     PRINT ( _L("End CPcsAlgorithm2::WriteSortOrderToCenRepL.") );
  1411     PRINT ( _L("End CPcsAlgorithm2::WriteSortOrderToCenRepL.") );
  1519     }
  1412     }
  1520 
  1413 
  1521 // ---------------------------------------------------------------------------------
  1414 // ---------------------------------------------------------------------------------
  1532 
  1425 
  1533     // set Uri
  1426     // set Uri
  1534     clientData->SetUriL(GetUriForIdL(aPsData.UriId()));
  1427     clientData->SetUriL(GetUriForIdL(aPsData.UriId()));
  1535 
  1428 
  1536     // set pointer to the each data element
  1429     // set pointer to the each data element
  1537     const TInt dataElementCount = aPsData.DataElementCount(); 
  1430     for (TInt i = 0; i < aPsData.DataElementCount(); i++)
  1538     for (TInt i = 0; i <dataElementCount; i++)
       
  1539         {
  1431         {
  1540         clientData->SetDataL(i, *(aPsData.Data(i)));
  1432         clientData->SetDataL(i, *(aPsData.Data(i)));
  1541         }
  1433         }
  1542 
  1434 
  1543     // set data extension
  1435     // set data extension
  1572             break;
  1464             break;
  1573             
  1465             
  1574         default:
  1466         default:
  1575             break;
  1467             break;
  1576         }
  1468         }
  1577     
  1469 
  1578     // Increment the relevant counter in P&S by one to signal the clients about
  1470     if ( psKey != KErrNotFound )
  1579     // the cache update.
  1471         {
  1580     if( psKey != KErrNotFound )
  1472         // Increment the relevant counter in P&S by one to signal the clients about
  1581         {
  1473         // the cache update.
  1582         TInt counter( KErrNotFound );
  1474         TInt counter( KErrNotFound );
  1583         TInt err = RProperty::Get( KPcsInternalUidCacheStatus, psKey, counter );
  1475         TInt err = RProperty::Get( KPcsInternalUidCacheStatus, psKey, counter );
  1584         if ( !err )
  1476         if ( !err )
  1585             {
  1477             {
  1586             counter++;
  1478             counter++;
  1601     if (err != KErrNone)
  1493     if (err != KErrNone)
  1602         {    
  1494         {    
  1603         PRINT1 ( _L("keyMap ReconstructKeymapL, err =%d"),err );
  1495         PRINT1 ( _L("keyMap ReconstructKeymapL, err =%d"),err );
  1604         }
  1496         }
  1605     
  1497     
  1606     for (TInt index = 0; index < iCacheCount; index++)
  1498     for (int index = 0; index < iCacheCount; index++)
  1607         {
  1499         {
  1608         CPcsCache* cache = iPcsCache[index];
  1500         CPcsCache* cache = iPcsCache[index];
  1609 
  1501 
  1610         HBufC* uri = cache->GetUri();
  1502         HBufC * uri = cache->GetUri();
  1611         // Clear the cache
  1503         // Clear the cache
  1612         cache->RemoveAllFromCache();
  1504         TRAP(err, cache->RemoveAllFromCacheL());   
       
  1505         PRINT1 ( _L("cache RemoveAllFromCacheL, err =%d"),err );
  1613 
  1506 
  1614         if (err != KErrNone)
  1507         if (err != KErrNone)
  1615             {
  1508             {
  1616             SetCachingError(*uri, err);
  1509             SetCachingError(*uri, err);
  1617             }
  1510             }
  1618         //Update the caching status as ECachingInProgress, since now the caching
  1511         //Update the caching status as ECachingInProgress, since now the caching
  1619         // would be started again
  1512         // would be strated again
  1620         UpdateCachingStatus(*uri, ECachingInProgress);
  1513         UpdateCachingStatus(*uri, ECachingInProgress);
  1621 
  1514 
  1622         // Request for data again
  1515         // Request for data again
  1623         TRAP(err, iPsDataPluginInterface->RequestForDataL(*uri));
  1516         TRAP(err, iPsDataPluginInterface->RequestForDataL(*uri));        
  1624         PRINT1 ( _L("iPsDataPluginInterface->RequestForDataL, err =%d"),err );
  1517         PRINT1 ( _L("iPsDataPluginInterface->RequestForDataL, err =%d"),err );
  1625 
  1518 
  1626         if (err != KErrNone)
  1519         if (err != KErrNone)
  1627             {
  1520             {
  1628             SetCachingError(*uri, err);
  1521             SetCachingError(*uri, err);
  1629             }
  1522             }
       
  1523         }
       
  1524     }
       
  1525 
       
  1526 void CPcsAlgorithm2::Converter(const TDesC& aSourStr, TDes& aDestStr)
       
  1527     {
       
  1528     if (iKeyMap)
       
  1529         {
       
  1530         iKeyMap->GetNumericKeyString(aSourStr, aDestStr);
  1630         }
  1531         }
  1631     }
  1532     }
  1632 
  1533 
  1633 // ---------------------------------------------------------------------------------
  1534 // ---------------------------------------------------------------------------------
  1634 // DoLaunchPluginsL.
  1535 // DoLaunchPluginsL.
  1658     // to see how this is being used.
  1559     // to see how this is being used.
  1659     User::LeaveIfError(Dll::SetTls(&iPcsCache));
  1560     User::LeaveIfError(Dll::SetTls(&iPcsCache));
  1660 
  1561 
  1661     // Initialize cache
  1562     // Initialize cache
  1662     RPointerArray<TDesC> dataStores;
  1563     RPointerArray<TDesC> dataStores;
  1663     CleanupClosePushL( dataStores );
       
  1664 
  1564 
  1665     iPsDataPluginInterface->GetAllSupportedDataStoresL(dataStores);
  1565     iPsDataPluginInterface->GetAllSupportedDataStoresL(dataStores);
  1666 
  1566 
  1667     const TInt dataStoresCount = dataStores.Count();
  1567     for (int dIndex = 0; dIndex < dataStores.Count(); dIndex++)
  1668     for (TInt dIndex = 0; dIndex < dataStoresCount; dIndex++)
       
  1669         {
  1568         {
  1670         AddDataStore(*(dataStores[dIndex]));
  1569         AddDataStore(*(dataStores[dIndex]));
  1671         }
  1570         }
  1672 
  1571 
  1673     CleanupStack::PopAndDestroy( &dataStores ); // Close
  1572     dataStores.Reset();
  1674     }
       
  1675 
       
  1676 /**
       
  1677 * Returns the Adaptive Grid for one or more URI
       
  1678 * 
       
  1679 */
       
  1680  void CPcsAlgorithm2::GetAdaptiveGridL( const MDesCArray& /*aURIs*/,
       
  1681                                const TBool /*aCompanyName*/,
       
  1682                                TDes& /*aAdaptiveGrid*/ )
       
  1683     {
       
  1684      PRINT ( _L("Enter CPcsAlgorithm2::GetAdaptiveGridL") );
       
  1685 
       
  1686 
       
  1687      PRINT ( _L("End CPcsAlgorithm2::GetAdaptiveGridL") );
       
  1688 
       
  1689     }
  1573     }
  1690 // End of file
  1574 // End of file
  1691 
  1575