phonebookengines/VirtualPhonebook/VPbkCntModel/src/CFindInTextDefOperation.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
       
    15 *                CContactDataBase::FindInTextDefAsyncL
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "CFindInTextDefOperation.h"
       
    21 
       
    22 // VPbkCntModel internal
       
    23 #include "CContactStore.h"
       
    24 #include "CFieldFactory.h"
       
    25 #include "CContactLink.h"
       
    26 
       
    27 // Virtual Phonebook
       
    28 #include <MVPbkFieldType.h>
       
    29 #include <MVPbkContactFindFromStoresObserver.h>
       
    30 #include <CVPbkContactLinkArray.h>
       
    31 
       
    32 // Other system includes
       
    33 #include <cntfield.h>
       
    34 
       
    35 namespace VPbkCntModel {
       
    36 
       
    37 // Set a limit to the contact id conversion. If for some reason
       
    38 // the result is huge.
       
    39 const TInt KMaxConversionInOneStep = 100;
       
    40 const TInt KContinueIdle = 1;
       
    41 const TInt KDontContinueIdle = 0;
       
    42 const TInt KGranularity( 2 );
       
    43 
       
    44 // ======== MEMBER FUNCTIONS ========
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // CFindInTextDefOperation::CFindInTextDefOperation
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 CFindInTextDefOperation::CFindInTextDefOperation( CContactStore& aContactStore,
       
    51         MVPbkContactFindFromStoresObserver& aObserver,
       
    52         const TCallBack& aWordParserCallBack )
       
    53         :   iStore( aContactStore ),
       
    54             iObserver( aObserver ),
       
    55             iWordParserCallBack( aWordParserCallBack )
       
    56     {
       
    57 
       
    58     }
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // CFindInTextDefOperation::ConstructL
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 void CFindInTextDefOperation::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 = KGranularity;
       
    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 fieldtypes to contact model text definitions
       
    83     iTextDefs = CContactTextDef::NewL();
       
    84     const TInt typeCount = aFieldTypes.FieldTypeCount();
       
    85     for ( TInt j = 0; j < typeCount; ++j )
       
    86         {
       
    87         const MVPbkFieldType& fieldType = aFieldTypes.FieldTypeAt( j );
       
    88         const CContactItemField* field =
       
    89             iStore.FieldFactory().FindField( fieldType );
       
    90         if ( field )
       
    91             {
       
    92             iTextDefs->AppendL(
       
    93                 TContactTextDefItem( field->ContentType().FieldType(0) ) );
       
    94             }
       
    95         }
       
    96 
       
    97     iIdle = CIdle::NewL( CActive::EPriorityStandard );
       
    98     iResults = CVPbkContactLinkArray::NewL();
       
    99     }
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // CFindInTextDefOperation::NewL
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 CFindInTextDefOperation* CFindInTextDefOperation::NewL(
       
   106         CContactStore& aContactStore,
       
   107         const MDesC16Array& aSearchStrings,
       
   108         const MVPbkFieldTypeList& aFieldTypes,
       
   109         MVPbkContactFindFromStoresObserver& aObserver,
       
   110         const TCallBack& aWordParserCallBack )
       
   111     {
       
   112     CFindInTextDefOperation* self = new( ELeave ) CFindInTextDefOperation(
       
   113         aContactStore, aObserver, aWordParserCallBack );
       
   114     CleanupStack::PushL( self );
       
   115     self->ConstructL( aSearchStrings, aFieldTypes );
       
   116     CleanupStack::Pop( self );
       
   117     return self;
       
   118     }
       
   119 
       
   120 // ---------------------------------------------------------------------------
       
   121 // CFindInTextDefOperation::~CFindInTextDefOperation
       
   122 // ---------------------------------------------------------------------------
       
   123 //
       
   124 CFindInTextDefOperation::~CFindInTextDefOperation()
       
   125     {
       
   126     Cancel();
       
   127     delete iAsyncFinder;
       
   128     delete iIdle;
       
   129     delete iResults;
       
   130     delete iContactIds;
       
   131     delete iSearchStrings;
       
   132     delete iTextDefs;
       
   133     }
       
   134 
       
   135 // ---------------------------------------------------------------------------
       
   136 // From class MVPbkContactOperation.
       
   137 // CFindInTextDefOperation::StartL
       
   138 // ---------------------------------------------------------------------------
       
   139 //
       
   140 void CFindInTextDefOperation::StartL()
       
   141     {
       
   142     if ( iTextDefs->Count() == 0 || iSearchStrings->Count() == 0 )
       
   143         {
       
   144         iIdle->Start( TCallBack( ConvertResults, this ) );
       
   145         }
       
   146     else
       
   147         {
       
   148         iAsyncFinder = iStore.NativeDatabase().FindInTextDefAsyncL(
       
   149             *iSearchStrings, iTextDefs, this, iWordParserCallBack );
       
   150         }
       
   151     }
       
   152 
       
   153 // ---------------------------------------------------------------------------
       
   154 // From class MVPbkContactOperation.
       
   155 // CFindInTextDefOperation::Cancel
       
   156 // ---------------------------------------------------------------------------
       
   157 //
       
   158 void CFindInTextDefOperation::Cancel()
       
   159     {
       
   160     if ( iAsyncFinder )
       
   161         {
       
   162         iAsyncFinder->Cancel();
       
   163         delete iAsyncFinder;
       
   164         iAsyncFinder = NULL;
       
   165         }
       
   166 
       
   167     if ( iIdle )
       
   168         {
       
   169         iIdle->Cancel();
       
   170         }
       
   171     }
       
   172 
       
   173 // ---------------------------------------------------------------------------
       
   174 // From class MIdleFindObserver.
       
   175 // CFindInTextDefOperation::IdleFindCallback
       
   176 // ---------------------------------------------------------------------------
       
   177 //
       
   178 void CFindInTextDefOperation::IdleFindCallback()
       
   179     {
       
   180     if ( iAsyncFinder->IsComplete() )
       
   181         {
       
   182         iContactIds = iAsyncFinder->TakeContactIds();
       
   183         iIdle->Start( TCallBack( ConvertResults, this ) );
       
   184         }
       
   185     }
       
   186 
       
   187 // ---------------------------------------------------------------------------
       
   188 // CFindInTextDefOperation::ConvertResults
       
   189 // ---------------------------------------------------------------------------
       
   190 //
       
   191 TInt CFindInTextDefOperation::ConvertResults( TAny* aThis )
       
   192     {
       
   193     CFindInTextDefOperation* self =
       
   194         static_cast<CFindInTextDefOperation*>( aThis );
       
   195     TInt continueIdle = KDontContinueIdle;
       
   196     TRAPD( res, continueIdle = self->ConvertResultsL() );
       
   197     if ( res != KErrNone )
       
   198         {
       
   199         self->HandleError( res );
       
   200         }
       
   201     return continueIdle;
       
   202     }
       
   203 
       
   204 // ---------------------------------------------------------------------------
       
   205 // CFindInTextDefOperation::ConvertResultsL
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 TInt CFindInTextDefOperation::ConvertResultsL()
       
   209     {
       
   210     TInt convertedIds = 0;
       
   211     const TInt firstContact = 0;
       
   212     // Convert only reasonable amount of ids in one cycle
       
   213     if ( iContactIds )
       
   214         {
       
   215         while ( iContactIds->Count() > 0 &&
       
   216             convertedIds < KMaxConversionInOneStep )
       
   217             {
       
   218             CContactLink* link = CContactLink::NewLC( iStore,
       
   219                 (*iContactIds)[firstContact] );
       
   220             iContactIds->Remove( firstContact );
       
   221             iResults->AppendL( link );
       
   222             CleanupStack::Pop(); // link
       
   223             ++convertedIds;
       
   224             }
       
   225         }
       
   226 
       
   227     // iContactIds not necessary exists if find wasn't done in StartL
       
   228     if ( !iContactIds || iContactIds->Count() == 0 )
       
   229         {
       
   230         // Results are ready.
       
   231         MVPbkContactLinkArray* results = iResults;
       
   232         iResults = NULL;
       
   233         iObserver.FindFromStoreSucceededL( iStore, results );
       
   234         iObserver.FindFromStoresOperationComplete();
       
   235         return KDontContinueIdle;
       
   236         }
       
   237     return KContinueIdle;
       
   238     }
       
   239 
       
   240 // ---------------------------------------------------------------------------
       
   241 // CFindInTextDefOperation::HandleError
       
   242 // ---------------------------------------------------------------------------
       
   243 //
       
   244 void CFindInTextDefOperation::HandleError( TInt aError )
       
   245     {
       
   246     if ( aError != KErrNone )
       
   247         {
       
   248         iObserver.FindFromStoreFailed( iStore, aError );
       
   249         iObserver.FindFromStoresOperationComplete();
       
   250         }
       
   251     }
       
   252 } // namespace VPbkCntModel