phonebookengines/VirtualPhonebook/VPbkSimStore/src/CFindWithParserOperation.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2006-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:  A find operation that uses word parser function from client
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CFindWithParserOperation.h"
       
    20 
       
    21 #include "CContactStore.h"
       
    22 #include "CFieldTypeMappings.h"
       
    23 #include "CContactLink.h"
       
    24 
       
    25 #include <MVPbkFieldType.h>
       
    26 #include <MVPbkContactStoreInfo.h>
       
    27 #include <CVPbkContactLinkArray.h>
       
    28 #include <MVPbkContactFindFromStoresObserver.h>
       
    29 #include <TVPbkWordParserCallbackParam.h>
       
    30 #include <CVPbkContactFindPolicy.h>
       
    31 #include <CVPbkSimContactBuf.h>
       
    32 #include <CVPbkSimCntField.h>
       
    33 #include <MVPbkSimCntStore.h>
       
    34 
       
    35 namespace VPbkSimStore {
       
    36 
       
    37 // Max amount of synchrnous contact reads from sim server
       
    38 // at one step
       
    39 const TInt KMaxContactReadInOneStep = 50;
       
    40 const TInt KContinueIdle = 1;
       
    41 const TInt KDontContinueIdle = 0;
       
    42 
       
    43 // ======== MEMBER FUNCTIONS ========
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // CFindWithParserOperation::CFindWithParserOperation
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 CFindWithParserOperation::CFindWithParserOperation( CContactStore& aStore,
       
    50         MVPbkContactFindFromStoresObserver& aObserver, 
       
    51         const TCallBack& aWordParserCallBack )
       
    52         :   iStore( aStore ),
       
    53             iSimStore( aStore.NativeStore() ),
       
    54             iObserver( aObserver ),
       
    55             iWordParserCallBack( aWordParserCallBack )
       
    56     {
       
    57     }
       
    58 
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // CFindWithParserOperation::ConstructL
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 void CFindWithParserOperation::ConstructL( const MDesC16Array& aSearchStrings,
       
    65         const MVPbkFieldTypeList& aFieldTypes )
       
    66     {
       
    67     // Copy search strings
       
    68     const TInt stringCount = aSearchStrings.MdcaCount();
       
    69     TInt granularity = stringCount;
       
    70     if ( stringCount == 0 )
       
    71         {
       
    72         // default granularity
       
    73         granularity = 2;
       
    74         }
       
    75     
       
    76     iSearchStrings = new( ELeave ) CDesCArrayFlat( granularity );
       
    77     for ( TInt i = 0; i < stringCount; ++i )
       
    78         {
       
    79         iSearchStrings->AppendL( aSearchStrings.MdcaPoint( i ) );
       
    80         }
       
    81     
       
    82     // Convert field types
       
    83     const TInt fieldTypeCount = aFieldTypes.FieldTypeCount();
       
    84     for ( TInt j = 0; j < fieldTypeCount; ++j )
       
    85         {
       
    86         TVPbkSimCntFieldType simType = 
       
    87             iStore.FieldTypeMappings().Match( aFieldTypes.FieldTypeAt( j ) );
       
    88         if ( simType != EVPbkSimUnknownType )
       
    89             {
       
    90             iSimFieldTypes.AppendL( simType );
       
    91             }
       
    92         }
       
    93     
       
    94     iIdle = CIdle::NewL( CActive::EPriorityStandard );
       
    95     iResults = CVPbkContactLinkArray::NewL();
       
    96     iCurSimContact = CVPbkSimContactBuf::NewL( iSimStore );
       
    97     granularity = 4;
       
    98     iParsedWords = new( ELeave ) CDesCArrayFlat( granularity );
       
    99     // Assumes that find policy implementation exists
       
   100     iFindPolicy = CVPbkContactFindPolicy::NewL();
       
   101     }
       
   102 
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 // CFindWithParserOperation::NewL
       
   106 // ---------------------------------------------------------------------------
       
   107 //
       
   108 CFindWithParserOperation* CFindWithParserOperation::NewL(
       
   109         CContactStore& aStore,
       
   110         const MDesC16Array& aSearchStrings,
       
   111         const MVPbkFieldTypeList& aFieldTypes,
       
   112         MVPbkContactFindFromStoresObserver& aObserver, 
       
   113         const TCallBack& aWordParserCallBack )
       
   114     {
       
   115     CFindWithParserOperation* self = new( ELeave ) CFindWithParserOperation( 
       
   116         aStore, aObserver, aWordParserCallBack );
       
   117     CleanupStack::PushL( self );
       
   118     self->ConstructL( aSearchStrings, aFieldTypes );
       
   119     CleanupStack::Pop( self );
       
   120     return self;
       
   121     }
       
   122 
       
   123 // ---------------------------------------------------------------------------
       
   124 // CFindWithParserOperation::~CFindWithParserOperation
       
   125 // ---------------------------------------------------------------------------
       
   126 //
       
   127 CFindWithParserOperation::~CFindWithParserOperation()
       
   128     {
       
   129     Cancel();
       
   130     delete iFindPolicy;
       
   131     delete iParsedWords;
       
   132     delete iCurSimContact;
       
   133     delete iResults;
       
   134     delete iIdle;
       
   135     delete iSearchStrings;
       
   136     iSimFieldTypes.Close();
       
   137     }
       
   138 
       
   139 // ---------------------------------------------------------------------------
       
   140 // From class MVPbkContactOperation
       
   141 // CFindWithParserOperation::StartL
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 void CFindWithParserOperation::StartL()
       
   145     {
       
   146     iTotalSimSlots = iStore.StoreInfo().MaxNumberOfContactsL();
       
   147     // SIM indexes start from 1
       
   148     iCurSimIndex = 1;
       
   149     
       
   150     if ( iSearchStrings->Count() == 0 || iSimFieldTypes.Count() == 0 )
       
   151         {
       
   152         // Set to zero so that find will stop immediately in callback
       
   153         iTotalSimSlots = 0;    
       
   154         }
       
   155         
       
   156     iIdle->Start( TCallBack( LoopContacts, this ) );
       
   157     }
       
   158 
       
   159 // ---------------------------------------------------------------------------
       
   160 // From class MVPbkContactOperation
       
   161 // CFindWithParserOperation::Cancel
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 void CFindWithParserOperation::Cancel()
       
   165     {
       
   166     if ( iIdle )
       
   167         {
       
   168         iIdle->Cancel();
       
   169         }
       
   170     }
       
   171 
       
   172 // ---------------------------------------------------------------------------
       
   173 // CFindWithParserOperation::LoopContacts
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 TInt CFindWithParserOperation::LoopContacts( TAny* aThis )
       
   177     {
       
   178     CFindWithParserOperation* self = 
       
   179         static_cast<CFindWithParserOperation*>( aThis );
       
   180     TInt continueIdle = KDontContinueIdle;
       
   181     TRAPD( res, continueIdle = self->LoopContactsL() );
       
   182     if ( res != KErrNone )
       
   183         {
       
   184         self->HandleError( res );
       
   185         }
       
   186     return continueIdle;
       
   187     }
       
   188 
       
   189 // ---------------------------------------------------------------------------
       
   190 // CFindWithParserOperation::LoopContacts
       
   191 // ---------------------------------------------------------------------------
       
   192 //
       
   193 TInt CFindWithParserOperation::LoopContactsL()
       
   194     {
       
   195     TInt contactsRead = 0;
       
   196     
       
   197     // Implementation comment: contact are read from server one contact
       
   198     // at a time. If this is too slow with big USIM cards then there are
       
   199     // couple of possibilities: 1) read many contacts in one request,
       
   200     // 2) do a find to SIM store that matches contacts that have search
       
   201     // strings anywhere in the field data. Then refine the result here.
       
   202     while ( iCurSimIndex <= iTotalSimSlots &&
       
   203             contactsRead <= KMaxContactReadInOneStep )
       
   204         {
       
   205         const TDesC8* etelCnt = iSimStore.AtL( iCurSimIndex );
       
   206         if ( etelCnt )
       
   207             {
       
   208             // reuse iCurSimContact
       
   209             iCurSimContact->SetL( *etelCnt );
       
   210             if ( IsCurrentContactMatchL() )
       
   211                 {
       
   212                 iResults->AppendL( 
       
   213                     CContactLink::NewLC( iStore, iCurSimIndex ) );
       
   214                 CleanupStack::Pop(); // link
       
   215                 }
       
   216             }
       
   217         ++iCurSimIndex;
       
   218         ++contactsRead;
       
   219         }
       
   220     
       
   221     if ( contactsRead == 0 )
       
   222         {
       
   223         // Results are ready.
       
   224         MVPbkContactLinkArray* results = iResults;
       
   225         iResults = NULL;
       
   226         iObserver.FindFromStoreSucceededL( iStore, results );
       
   227         iObserver.FindFromStoresOperationComplete();
       
   228         return KDontContinueIdle;
       
   229         }
       
   230     return KContinueIdle;
       
   231     }
       
   232 
       
   233 // ---------------------------------------------------------------------------
       
   234 // CFindWithParserOperation::IsCurrentContactMatchL
       
   235 // ---------------------------------------------------------------------------
       
   236 //    
       
   237 TBool CFindWithParserOperation::IsCurrentContactMatchL()
       
   238     {
       
   239     TBool fieldMatched = EFalse;
       
   240     const TInt fieldCount = iCurSimContact->FieldCount();
       
   241     const TInt searchStringCount = iSearchStrings->Count();
       
   242     for ( TInt i = 0; i < fieldCount && !fieldMatched; ++i )
       
   243         {
       
   244         const CVPbkSimCntField& field = iCurSimContact->ConstFieldAt( i );
       
   245         // Check that is the type of the field in the given types
       
   246         if ( iSimFieldTypes.Find( field.Type() ) != KErrNotFound )
       
   247             {
       
   248             // Call clients parser function. It separates field data
       
   249             // into words and appends them into iParsedWords
       
   250             iParsedWords->Reset();
       
   251             TVPbkWordParserCallbackParam param( &field.Data(), iParsedWords );
       
   252             iWordParserCallBack.iFunction( &param );
       
   253             
       
   254             // All the search strings must match. If there are more
       
   255             // search strings than the words then it's not a match
       
   256             if ( searchStringCount <= iParsedWords->Count() )
       
   257                 {
       
   258                 TInt matchedWords = 0;
       
   259                 for ( TInt j = 0; j < searchStringCount; ++j )
       
   260                     {
       
   261                     const TInt wordCount = iParsedWords->Count();
       
   262                     for ( TInt k = 0; k < wordCount; ++k )
       
   263                         {
       
   264                         if ( iFindPolicy->MatchRefineL( (*iParsedWords)[k], 
       
   265                             (*iSearchStrings)[j] ))
       
   266                             {
       
   267                             // Remove word that matched so that it's not
       
   268                             // matched again
       
   269                             iParsedWords->Delete( k );
       
   270                             ++matchedWords;
       
   271                             break;                           
       
   272                             }
       
   273                         }
       
   274                     }
       
   275                 
       
   276                 // If all words matched then the field data matched
       
   277                 if ( matchedWords == searchStringCount )
       
   278                     {
       
   279                     fieldMatched = ETrue;
       
   280                     }
       
   281                 }
       
   282             }
       
   283         }
       
   284     return fieldMatched;
       
   285     }
       
   286     
       
   287 // ---------------------------------------------------------------------------
       
   288 // CFindWithParserOperation::HandleError
       
   289 // ---------------------------------------------------------------------------
       
   290 //    
       
   291 void CFindWithParserOperation::HandleError( TInt aError )
       
   292     {
       
   293     if ( aError != KErrNone )
       
   294         {
       
   295         iObserver.FindFromStoreFailed( iStore, aError );
       
   296         iObserver.FindFromStoresOperationComplete();
       
   297         }
       
   298     }
       
   299 } // namespace VPbkSimStore