phonebookengines/VirtualPhonebook/VPbkSimStoreImpl/src/CSingleReadCmd.cpp
branchRCL_3
changeset 20 f4a778e096c2
parent 0 e686773b3f54
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2002-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 class that reads one contact at a time using ETel
       
    15 *                RMobilePhoneStore Read
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "CSingleReadCmd.h"
       
    23 
       
    24 #include "CStoreBase.h"
       
    25 #include "CVPbkSimContact.h"
       
    26 #include "CVPbkSimCntField.h"
       
    27 #include "CContactArray.h"
       
    28 #include "VPbkSimStoreImplError.h"
       
    29 
       
    30 // From VPbkEng
       
    31 #include "VPbkDebug.h"
       
    32 
       
    33 namespace VPbkSimStoreImpl {
       
    34 
       
    35 
       
    36 // ============================ MEMBER FUNCTIONS ===============================
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CSingleReadBase::CSingleReadBase
       
    40 // C++ default constructor can NOT contain any code, that
       
    41 // might leave.
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 template<class ETelContact>
       
    45 CSingleReadBase<ETelContact>::CSingleReadBase( 
       
    46     CStoreBase& aStore, TInt aIndexToRead )
       
    47     :   CActive( EPriorityStandard ),
       
    48         iStore( aStore ),
       
    49         iETelContactPckg( iETelContact ),
       
    50         iCurrentIndex( aIndexToRead ),
       
    51         iLastIndexToRead( aIndexToRead ),
       
    52         iSimIndexRef( KVPbkSimStoreInvalidETelIndex )
       
    53     {
       
    54     CActiveScheduler::Add( this );
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CSingleReadBase::CSingleReadBase
       
    59 // C++ default constructor can NOT contain any code, that
       
    60 // might leave.
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 template<class ETelContact>
       
    64 CSingleReadBase<ETelContact>::CSingleReadBase( CStoreBase& aStore )
       
    65     :   CActive( EPriorityStandard ),
       
    66         iStore( aStore ),
       
    67         iETelContactPckg( iETelContact ),
       
    68         iCurrentIndex( KVPbkSimStoreFirstETelIndex ),
       
    69         iLastIndexToRead( KVPbkSimStoreInvalidETelIndex ),
       
    70         iSimIndexRef( KVPbkSimStoreInvalidETelIndex )
       
    71     {
       
    72     CActiveScheduler::Add( this );
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CSingleReadBase::CSingleReadBase
       
    77 // C++ default constructor can NOT contain any code, that
       
    78 // might leave.
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 template<class ETelContact>
       
    82 CSingleReadBase<ETelContact>::CSingleReadBase( 
       
    83         TInt& aIndexToReadRef, CStoreBase& aStore )
       
    84     :   CActive( EPriorityStandard ),
       
    85         iStore( aStore ),
       
    86         iETelContactPckg( iETelContact ),
       
    87         iCurrentIndex( KVPbkSimStoreInvalidETelIndex ),
       
    88         iLastIndexToRead( KVPbkSimStoreInvalidETelIndex ),
       
    89         iSimIndexRef( aIndexToReadRef )
       
    90     {
       
    91     CActiveScheduler::Add( this );
       
    92     }
       
    93     
       
    94 // -----------------------------------------------------------------------------
       
    95 // CSingleReadBase::Execute
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 template<class ETelContact>
       
    99 void CSingleReadBase<ETelContact>::Execute()
       
   100     {
       
   101     __ASSERT_DEBUG( iObserver,
       
   102         VPbkSimStoreImpl::Panic( EPreCondCSingleReadBaseExecute ) );
       
   103         
       
   104     if ( iSimIndexRef != KVPbkSimStoreInvalidETelIndex )
       
   105         {
       
   106         iCurrentIndex = iSimIndexRef;
       
   107         iLastIndexToRead = iSimIndexRef;
       
   108         }
       
   109     else if ( iLastIndexToRead == KVPbkSimStoreInvalidETelIndex )
       
   110         {
       
   111         iLastIndexToRead = iStore.ETelStoreInfo().iTotalEntries;
       
   112         }
       
   113 
       
   114     // If noting to read then make a dummy request to complete command
       
   115     // asynchronously
       
   116     if ( !ReadNext() )
       
   117         {
       
   118         TRequestStatus* status = &iStatus;
       
   119         User::RequestComplete( status, KErrNone );
       
   120         SetActive();
       
   121         }
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CSingleReadBase::AddObserverL
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 template<class ETelContact>
       
   129 void CSingleReadBase<ETelContact>::AddObserverL( 
       
   130         MVPbkSimCommandObserver& aObserver )
       
   131     {
       
   132     __ASSERT_DEBUG( !iObserver,
       
   133         VPbkSimStoreImpl::Panic( EPreCondCSingleReadBaseAddObserverL ) );
       
   134     iObserver = &aObserver;
       
   135     }
       
   136 
       
   137 // -----------------------------------------------------------------------------
       
   138 // CSingleReadBase::CancelCmd
       
   139 // -----------------------------------------------------------------------------
       
   140 //
       
   141 template<class ETelContact>
       
   142 void CSingleReadBase<ETelContact>::CancelCmd()
       
   143     {
       
   144     CActive::Cancel();
       
   145     }
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // CSingleReadBase::RunL
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 template<class ETelContact>
       
   152 void CSingleReadBase<ETelContact>::RunL()
       
   153     {
       
   154     TInt result = iStatus.Int();
       
   155 
       
   156     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   157         "VPbkSimStoreImpl: RMobilePhoneStore::Read h%d complete %d, index %d"),
       
   158         iStore.ETelStoreBase().SubSessionHandle(),result, 
       
   159         iETelContact.iIndex);
       
   160         
       
   161     HandleRunL();
       
   162     TBool readNext = ETrue;
       
   163     switch ( result )
       
   164         {
       
   165         case KErrNone: // FALLTHROUGH
       
   166         case KErrNotFound:
       
   167             {
       
   168             readNext = ReadNext();
       
   169             break;
       
   170             }
       
   171         default:
       
   172             {
       
   173             iObserver->CommandError( *this, result );
       
   174             break;
       
   175             }
       
   176         }
       
   177 
       
   178     if ( !readNext )
       
   179         {
       
   180         iObserver->CommandDone( *this );
       
   181         }
       
   182     }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CSingleReadBase::DoCancel
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 template<class ETelContact>
       
   189 void CSingleReadBase<ETelContact>::DoCancel()
       
   190     {
       
   191     iStore.ETelStoreBase().CancelAsyncRequest( EMobilePhoneStoreRead );
       
   192     }
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // CSingleReadBase::RunError
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 template<class ETelContact>
       
   199 TInt CSingleReadBase<ETelContact>::RunError( TInt aError )
       
   200     {
       
   201     iObserver->CommandError( *this, aError );
       
   202     return KErrNone;
       
   203     }
       
   204     
       
   205 // -----------------------------------------------------------------------------
       
   206 // CSingleReadBase::ReadNext
       
   207 // -----------------------------------------------------------------------------
       
   208 //
       
   209 template<class ETelContact>
       
   210 TBool CSingleReadBase<ETelContact>::ReadNext()
       
   211     {
       
   212     if ( iCurrentIndex <= iLastIndexToRead )
       
   213         {
       
   214         VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   215             "VPbkSimStoreImpl: RMobilePhoneStore::Read h%d index %d"),
       
   216             iStore.ETelStoreBase().SubSessionHandle(),iCurrentIndex);
       
   217         iETelContact.iIndex = iCurrentIndex;
       
   218         iStore.ETelStoreBase().Read( iStatus, iETelContactPckg );
       
   219         ++iCurrentIndex;
       
   220         SetActive();
       
   221         return ETrue;
       
   222         }
       
   223     return EFalse;
       
   224     }
       
   225 
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CSingleONReadCmd::CSingleONReadCmd
       
   229 // C++ default constructor can NOT contain any code, that
       
   230 // might leave.
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 CSingleONReadCmd::CSingleONReadCmd( CStoreBase& aStoreBase, 
       
   234         TInt aIndexToRead )
       
   235         :   CSingleReadBase<RMobileONStore::TMobileONEntryV1>(
       
   236                 aStoreBase, aIndexToRead )
       
   237     {
       
   238     }
       
   239 
       
   240 // -----------------------------------------------------------------------------
       
   241 // CSingleONReadCmd::CSingleONReadCmd
       
   242 // C++ default constructor can NOT contain any code, that
       
   243 // might leave.
       
   244 // -----------------------------------------------------------------------------
       
   245 //
       
   246 CSingleONReadCmd::CSingleONReadCmd( CStoreBase& aStoreBase )
       
   247         :   CSingleReadBase<RMobileONStore::TMobileONEntryV1>( aStoreBase )
       
   248     {
       
   249     }
       
   250 
       
   251 // -----------------------------------------------------------------------------
       
   252 // CSingleONReadCmd::CSingleONReadCmd
       
   253 // C++ default constructor can NOT contain any code, that
       
   254 // might leave.
       
   255 // -----------------------------------------------------------------------------
       
   256 //
       
   257 CSingleONReadCmd::CSingleONReadCmd( TInt& aIndexToReadRef, 
       
   258         CStoreBase& aStoreBase )
       
   259         :   CSingleReadBase<RMobileONStore::TMobileONEntryV1>(
       
   260                 aIndexToReadRef, aStoreBase)
       
   261     {
       
   262     }
       
   263 
       
   264 // Destructor
       
   265 CSingleONReadCmd::~CSingleONReadCmd()
       
   266     {
       
   267     CancelCmd();
       
   268     }
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // CSingleONReadCmd::HandleRunL
       
   272 // -----------------------------------------------------------------------------
       
   273 //
       
   274 void CSingleONReadCmd::HandleRunL()
       
   275     {
       
   276     TInt result = iStatus.Int();
       
   277     if ( result == KErrNone )
       
   278         {
       
   279         AddContactL();
       
   280         }
       
   281     }
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // CSingleONReadCmd::AddContactL
       
   285 // -----------------------------------------------------------------------------
       
   286 //
       
   287 void CSingleONReadCmd::AddContactL()
       
   288     {
       
   289     CVPbkSimContact* cnt = CVPbkSimContact::NewLC( iStore );
       
   290     cnt->SetSimIndex( iETelContact.iIndex );
       
   291 
       
   292     if ( iETelContact.iText.Length() > 0 )
       
   293         {
       
   294         CVPbkSimCntField* field = cnt->CreateFieldLC( EVPbkSimName );
       
   295         field->SetDataL( iETelContact.iText );
       
   296         cnt->AddFieldL( field );
       
   297         CleanupStack::Pop( field );
       
   298         }
       
   299 
       
   300     if ( iETelContact.iNumber.iTelNumber.Length() > 0 )
       
   301         {
       
   302         CVPbkSimCntField* field = cnt->CreateFieldLC( EVPbkSimGsmNumber );
       
   303         field->SetDataL( iETelContact.iNumber.iTelNumber );
       
   304         cnt->AddFieldL( field );
       
   305         CleanupStack::Pop( field );
       
   306         }
       
   307 
       
   308     if ( cnt->FieldArray().Count() > 0 )
       
   309         {
       
   310         iStore.Contacts().AddOrReplaceL( cnt );
       
   311         CleanupStack::Pop( cnt );
       
   312         }
       
   313     else
       
   314         {
       
   315         CleanupStack::PopAndDestroy( cnt );
       
   316         }
       
   317     }
       
   318 } // namespace VPbkSimStoreImpl
       
   319 //  End of File