phonebookengines/VirtualPhonebook/VPbkCntModel/src/CMatchPhoneNumberOperation.cpp
branchRCL_3
changeset 20 f4a778e096c2
parent 0 e686773b3f54
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2004-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:  Virtual Phonebook Contact model phone number match operation.
       
    15 *
       
    16 */
       
    17  
       
    18 
       
    19 #include "CMatchPhoneNumberOperation.h"
       
    20 #include "CContactLink.h"
       
    21 #include "CContactStore.h"
       
    22 #include <cntdb.h>
       
    23 
       
    24 #include <MVPbkContactFindObserver.h>
       
    25 
       
    26 namespace VPbkCntModel {
       
    27 
       
    28 CMatchPhoneNumberOperation::CMatchPhoneNumberOperation(
       
    29         CContactStore& aContactStore,
       
    30         TInt aMaxMatchDigits,
       
    31         MVPbkContactFindObserver& aObserver) :
       
    32     CActive(CActive::EPriorityIdle),
       
    33     iContactStore(aContactStore),
       
    34     iObserver(aObserver),
       
    35     iMaxMatchDigits(aMaxMatchDigits)
       
    36     {
       
    37     CActiveScheduler::Add(this);
       
    38     }
       
    39 
       
    40 inline void CMatchPhoneNumberOperation::ConstructL(const TDesC& aPhoneNumber)
       
    41     {
       
    42     iPhoneNumber = aPhoneNumber.AllocL();
       
    43     iResults = CVPbkContactLinkArray::NewL();
       
    44     }
       
    45 
       
    46 CMatchPhoneNumberOperation* CMatchPhoneNumberOperation::NewL(
       
    47         CContactStore& aContactStore,
       
    48         const TDesC& aPhoneNumber,
       
    49         TInt aMaxMatchDigits,
       
    50         MVPbkContactFindObserver& aObserver)
       
    51     {
       
    52 	CMatchPhoneNumberOperation* self = new(ELeave) CMatchPhoneNumberOperation(
       
    53         aContactStore, aMaxMatchDigits, aObserver);
       
    54     CleanupStack::PushL(self);
       
    55     self->ConstructL(aPhoneNumber);
       
    56     CleanupStack::Pop(self);
       
    57 	return self;
       
    58 	}
       
    59 
       
    60 CMatchPhoneNumberOperation::~CMatchPhoneNumberOperation()
       
    61     {
       
    62     Cancel();
       
    63 
       
    64     delete iPhoneNumber;
       
    65     delete iContactIds;
       
    66     delete iResults;
       
    67     }
       
    68 
       
    69 void CMatchPhoneNumberOperation::IssueRequest()
       
    70     {
       
    71     TRequestStatus* status = &iStatus;
       
    72     User::RequestComplete(status, KErrNone);
       
    73     SetActive();
       
    74     }
       
    75 
       
    76 void CMatchPhoneNumberOperation::DoCancel()
       
    77     {
       
    78     iState = ECancelled;
       
    79     if (!IsActive())
       
    80         {
       
    81         IssueRequest();
       
    82         }
       
    83     }
       
    84 
       
    85 void CMatchPhoneNumberOperation::RunL()
       
    86     {
       
    87     switch (iState)
       
    88         {
       
    89         case EMatch:
       
    90             {
       
    91             iContactIds = iContactStore.NativeDatabase().
       
    92                 MatchPhoneNumberL(*iPhoneNumber, iMaxMatchDigits);
       
    93             iState = EBuildLinks;
       
    94             IssueRequest();
       
    95             break;
       
    96             }
       
    97         case EBuildLinks:
       
    98             {
       
    99             if (iContactIds->Count() > 0)
       
   100                 {
       
   101                 CContactLink* link = CContactLink::NewLC(iContactStore, (*iContactIds)[0]);
       
   102                 iContactIds->Remove(0);
       
   103                 iResults->AppendL(link);
       
   104                 CleanupStack::Pop(link); // link
       
   105                 IssueRequest();
       
   106                 }
       
   107             else
       
   108                 {
       
   109                 delete iContactIds;
       
   110                 iContactIds = NULL;
       
   111                 iState = EComplete;
       
   112                 IssueRequest();
       
   113                 }
       
   114             break;
       
   115             }
       
   116         case EComplete:
       
   117             {
       
   118             // takes ownership of results
       
   119             CVPbkContactLinkArray* results = iResults;            
       
   120             iResults = NULL;
       
   121             iObserver.FindCompleteL(results);
       
   122             break;
       
   123             }
       
   124         case ECancelled:
       
   125             {
       
   126             iObserver.FindFailed(KErrCancel);
       
   127             break;
       
   128             }
       
   129         }
       
   130 
       
   131     }
       
   132 
       
   133 TInt CMatchPhoneNumberOperation::RunError(TInt aError)
       
   134     {
       
   135     iObserver.FindFailed(aError);
       
   136     return KErrNone;
       
   137     }
       
   138 
       
   139 void CMatchPhoneNumberOperation::StartL()
       
   140     {
       
   141     IssueRequest();
       
   142 	}
       
   143 
       
   144 void CMatchPhoneNumberOperation::Cancel()
       
   145     {
       
   146     CActive::Cancel();
       
   147 	}
       
   148 
       
   149 } // namespace VPbkCntModel