phonebookengines/VirtualPhonebook/VPbkSimStore/src/CRemoteStore.cpp
changeset 0 e686773b3f54
child 9 0d28c1c5b6dd
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     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 remote sim store implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CRemoteStore.h"
       
    22 
       
    23 // From Virtual Phonebook
       
    24 #include "VPbkSimStoreError.h"
       
    25 #include "CRemoteView.h"
       
    26 
       
    27 #include <MVPbkSimStoreObserver.h>
       
    28 #include <MVPbkSimContactObserver.h>
       
    29 #include <MVPbkSimFindObserver.h>
       
    30 #include <VPbkSimServerOpCodes.h>
       
    31 #include <VPbkSimStoreTemplateFunctions.h>
       
    32 #include <CVPbkSimContactBuf.h>
       
    33 #include <MVPbkSimPhoneObserver.h>
       
    34 #include <CVPbkETelCntConverter.h>
       
    35 #include <CVPbkSimFieldTypeFilter.h>
       
    36 #include <MVPbkSimStoreOperation.h>
       
    37 #include <MVPbkSimCntView.h>
       
    38 #include <VPbkSimStoreCommonUtil.h>
       
    39 #include <CVPbkAsyncOperation.h>
       
    40 #include <CVPbkAsyncCallback.h>
       
    41 
       
    42 #include <VPbkDebug.h>
       
    43 
       
    44 // System includes
       
    45 #include <s32mem.h>
       
    46 
       
    47 namespace VPbkSimStore {
       
    48 
       
    49 // CONSTANTS
       
    50 // The initial buffer size for number match results, 10 indexes
       
    51 const TInt KInitSimIndexBufSize = 10 * sizeof(TInt);
       
    52 // The maximum buffer size for number match results if server behaves badly
       
    53 const TInt KMaxSimIndexBufSize = 100 * KInitSimIndexBufSize;
       
    54 
       
    55 // ============================= LOCAL FUNCTIONS ============================
       
    56 
       
    57 // --------------------------------------------------------------------------
       
    58 // SendStoreEventMessage
       
    59 // --------------------------------------------------------------------------
       
    60 //
       
    61 template<class Function, class Param>
       
    62 inline void SendStoreEventMessage(
       
    63     RPointerArray<MVPbkSimStoreObserver>& aObservers, Function aFunction,
       
    64     Param& aParam )
       
    65     {
       
    66     const TInt count = aObservers.Count();
       
    67     for ( TInt i = count - 1; i >= 0; --i )
       
    68         {
       
    69         ( aObservers[i]->*aFunction )( aParam );
       
    70         }
       
    71     }
       
    72 
       
    73 // --------------------------------------------------------------------------
       
    74 // SendStoreErrorMessage
       
    75 // --------------------------------------------------------------------------
       
    76 //
       
    77 inline void SendStoreErrorMessage( 
       
    78     RPointerArray<MVPbkSimStoreObserver>& aObservers, MVPbkSimCntStore& aStore,
       
    79     TInt aError )
       
    80     {
       
    81     const TInt count = aObservers.Count();
       
    82     for ( TInt i = count - 1; i >= 0; --i )
       
    83         {
       
    84         aObservers[i]->StoreError( aStore, aError );
       
    85         }
       
    86     }
       
    87     
       
    88 // --------------------------------------------------------------------------
       
    89 // SendStoreEventMessage
       
    90 // --------------------------------------------------------------------------
       
    91 //
       
    92 inline void SendStoreEventMessage(
       
    93     RPointerArray<MVPbkSimStoreObserver>& aObservers,
       
    94     void (MVPbkSimStoreObserver::*aEvent)(MVPbkSimStoreObserver::TEvent, TInt),
       
    95     MVPbkSimStoreObserver::TEvent aEventType,
       
    96     TInt aParam1)
       
    97     {
       
    98     for (TInt i = aObservers.Count() - 1; i >= 0; --i)
       
    99         {
       
   100         (aObservers[i]->*aEvent)( aEventType, aParam1 );
       
   101         }    
       
   102     }    
       
   103 
       
   104 // ============================ MEMBER FUNCTIONS ============================
       
   105 
       
   106 // --------------------------------------------------------------------------
       
   107 // CAsyncSave::CAsyncSave
       
   108 // C++ default constructor can NOT contain any code, that
       
   109 // might leave.
       
   110 // --------------------------------------------------------------------------
       
   111 //
       
   112 CAsyncSave::CAsyncSave( RVPbkSimStore& aStore )
       
   113 :   CActive( EPriorityStandard ),
       
   114     iStore( aStore )
       
   115     {
       
   116     CActiveScheduler::Add( this );
       
   117     }
       
   118 
       
   119 // Destructor
       
   120 CAsyncSave::~CAsyncSave()
       
   121     {
       
   122     Cancel();
       
   123     }
       
   124 
       
   125 // --------------------------------------------------------------------------
       
   126 // CAsyncSave::DoRequestL
       
   127 // --------------------------------------------------------------------------
       
   128 //
       
   129 void CAsyncSave::DoRequestL( const TDesC8& aData, 
       
   130     TInt& aSimIndex, MVPbkSimContactObserver& aObserver )
       
   131     {
       
   132     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   133         "CAsyncSave::DoRequestL: aSimIndex %d"),
       
   134         aSimIndex );
       
   135     
       
   136     iObserver = &aObserver;
       
   137     iStore.SaveL( iStatus, aData, aSimIndex );
       
   138     SetActive();
       
   139     }
       
   140 
       
   141 // --------------------------------------------------------------------------
       
   142 // CAsyncSave::RunL
       
   143 // --------------------------------------------------------------------------
       
   144 //
       
   145 void CAsyncSave::RunL()
       
   146     {
       
   147     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   148         "CAsyncSave::RunL: iStatus %d"),
       
   149         iStatus.Int() );
       
   150     
       
   151     TInt result = iStatus.Int();
       
   152     if ( result == KErrNone )
       
   153         {
       
   154         iObserver->ContactEventComplete( MVPbkSimContactObserver::ESave, NULL );
       
   155         }
       
   156     else
       
   157         {
       
   158         iObserver->ContactEventError( MVPbkSimContactObserver::ESave, NULL, 
       
   159             result );
       
   160         }
       
   161     }
       
   162 
       
   163 // --------------------------------------------------------------------------
       
   164 // CAsyncSave::DoCancel
       
   165 // --------------------------------------------------------------------------
       
   166 //
       
   167 void CAsyncSave::DoCancel()
       
   168     {
       
   169     iStore.CancelAsyncRequest( EVPbkSimSrvSaveContact );
       
   170     }
       
   171 
       
   172 // --------------------------------------------------------------------------
       
   173 // CAsyncDelete::CAsyncDelete
       
   174 // C++ default constructor can NOT contain any code, that
       
   175 // might leave.
       
   176 // --------------------------------------------------------------------------
       
   177 //
       
   178 CAsyncDelete::CAsyncDelete( RVPbkSimStore& aStore )
       
   179 :   CActive( EPriorityStandard ),
       
   180     iStore( aStore )
       
   181     {
       
   182     CActiveScheduler::Add( this );
       
   183     }
       
   184 
       
   185 // Destructor
       
   186 CAsyncDelete::~CAsyncDelete()
       
   187     {
       
   188     Cancel();
       
   189     }
       
   190 
       
   191 // --------------------------------------------------------------------------
       
   192 // CAsyncDelete::DoRequestL
       
   193 // --------------------------------------------------------------------------
       
   194 //
       
   195 void CAsyncDelete::DoRequestL( 
       
   196         RVPbkStreamedIntArray& aSimIndexes, 
       
   197         MVPbkSimContactObserver& aObserver )
       
   198     {
       
   199     iObserver = &aObserver;
       
   200     iStore.DeleteL( iStatus, aSimIndexes );
       
   201     SetActive();
       
   202     }
       
   203 
       
   204 // --------------------------------------------------------------------------
       
   205 // CAsyncDelete::RunL
       
   206 // --------------------------------------------------------------------------
       
   207 //
       
   208 void CAsyncDelete::RunL()
       
   209     {
       
   210     TInt result = iStatus.Int();
       
   211     if ( result == KErrNone )
       
   212         {
       
   213         iObserver->ContactEventComplete( MVPbkSimContactObserver::EDelete, 
       
   214             NULL );
       
   215         }
       
   216     else
       
   217         {
       
   218         iObserver->ContactEventError( MVPbkSimContactObserver::EDelete, 
       
   219             NULL, result );
       
   220         }
       
   221     }
       
   222 
       
   223 // --------------------------------------------------------------------------
       
   224 // CAsyncDelete::DoCancel
       
   225 // --------------------------------------------------------------------------
       
   226 //
       
   227 void CAsyncDelete::DoCancel()
       
   228     {
       
   229     iStore.CancelAsyncRequest( EVPbkSimSrvDeleteContact );
       
   230     }
       
   231 
       
   232 // --------------------------------------------------------------------------
       
   233 // CRemoteStore::CNumberMatchOperation::CNumberMatchOperation
       
   234 // C++ default constructor can NOT contain any code, that
       
   235 // might leave.
       
   236 // --------------------------------------------------------------------------
       
   237 //
       
   238 CRemoteStore::CNumberMatchOperation::CNumberMatchOperation( 
       
   239     CRemoteStore& aStore, const TDesC& aPhoneNumber, 
       
   240     TInt aMaxMatchDigits, MVPbkSimFindObserver& aObserver )
       
   241     :   CActive( EPriorityStandard ),
       
   242         iStore( aStore ),
       
   243         iPhoneNumber( aPhoneNumber ),
       
   244         iMaxMatchDigits( aMaxMatchDigits ),
       
   245         iObserver( aObserver ),
       
   246         iSimIndexBufPtr( NULL, 0 )
       
   247     {
       
   248     }
       
   249 
       
   250 // --------------------------------------------------------------------------
       
   251 // CRemoteStore::CNumberMatchOperation::ConstructL
       
   252 // Symbian 2nd phase constructor can leave.
       
   253 // --------------------------------------------------------------------------
       
   254 //
       
   255 void CRemoteStore::CNumberMatchOperation::ConstructL()
       
   256     {
       
   257     CActiveScheduler::Add( this );
       
   258     iSimIndexBuf = HBufC8::NewL( KInitSimIndexBufSize );
       
   259     iSimIndexBufPtr.Set( iSimIndexBuf->Des() );
       
   260     }
       
   261 
       
   262 // --------------------------------------------------------------------------
       
   263 // CRemoteStore::CNumberMatchOperation::NewL
       
   264 // Two-phased constructor.
       
   265 // --------------------------------------------------------------------------
       
   266 //
       
   267 CRemoteStore::CNumberMatchOperation* CRemoteStore::CNumberMatchOperation::NewL( 
       
   268     CRemoteStore& aStore, const TDesC& aPhoneNumber, 
       
   269     TInt aMaxMatchDigits, MVPbkSimFindObserver& aObserver )
       
   270     {
       
   271     CNumberMatchOperation* self = new( ELeave ) CNumberMatchOperation( aStore, 
       
   272         aPhoneNumber, aMaxMatchDigits, aObserver );
       
   273     CleanupStack::PushL( self );
       
   274     self->ConstructL();
       
   275     CleanupStack::Pop( self );
       
   276     return self;
       
   277     }
       
   278 
       
   279 // Destructor
       
   280 CRemoteStore::CNumberMatchOperation::~CNumberMatchOperation()
       
   281     {
       
   282     Cancel();
       
   283     delete iSimIndexBuf;
       
   284     }
       
   285 
       
   286 // --------------------------------------------------------------------------
       
   287 // CRemoteStore::CNumberMatchOperation::Activate
       
   288 // --------------------------------------------------------------------------
       
   289 //
       
   290 void CRemoteStore::CNumberMatchOperation::Activate()
       
   291     {
       
   292     iStore.StoreSession().MatchPhoneNumber( iStatus, iPhoneNumber, 
       
   293         iMaxMatchDigits, iSimIndexBufPtr );
       
   294     SetActive();
       
   295     }
       
   296 
       
   297 // --------------------------------------------------------------------------
       
   298 // CRemoteStore::CNumberMatchOperation::RunL
       
   299 // --------------------------------------------------------------------------
       
   300 //
       
   301 void CRemoteStore::CNumberMatchOperation::RunL()
       
   302     {
       
   303     RVPbkStreamedIntArray matchedIndexes;
       
   304     CleanupClosePushL( matchedIndexes );
       
   305 
       
   306     TInt result = iStatus.Int();
       
   307     switch ( result )
       
   308         {
       
   309         case KErrNone:
       
   310             {
       
   311             RDesReadStream stream( iSimIndexBufPtr );
       
   312             CleanupClosePushL( stream );
       
   313             stream >> matchedIndexes;
       
   314             CleanupStack::PopAndDestroy(); // stream
       
   315             iObserver.FindCompleted( iStore, matchedIndexes );
       
   316             break;
       
   317             }
       
   318         case KErrNotFound:
       
   319             {
       
   320             iObserver.FindCompleted( iStore, matchedIndexes );
       
   321             break;
       
   322             }
       
   323         case KErrOverflow:
       
   324             {
       
   325             TInt newLength = 2 * iSimIndexBufPtr.MaxLength();
       
   326             if ( newLength > KMaxSimIndexBufSize )
       
   327                 {
       
   328                 iObserver.FindError( iStore, result );
       
   329                 }
       
   330             else
       
   331                 {
       
   332                 VPbkSimStoreImpl::CheckAndUpdateBufferSizeL( iSimIndexBuf, 
       
   333                     iSimIndexBufPtr, newLength );
       
   334                 Activate();
       
   335                 }
       
   336             break;
       
   337             }
       
   338         default:
       
   339             {
       
   340             iObserver.FindError( iStore, result );
       
   341             break;
       
   342             }
       
   343         }
       
   344     CleanupStack::PopAndDestroy(); // matchedIndexes
       
   345     }
       
   346   
       
   347 // --------------------------------------------------------------------------
       
   348 // CRemoteStore::CNumberMatchOperation::DoCancel
       
   349 // --------------------------------------------------------------------------
       
   350 //
       
   351 void CRemoteStore::CNumberMatchOperation::DoCancel()
       
   352     {
       
   353     iStore.StoreSession().CancelAsyncRequest( EVPbkSimSrvMatchPhoneNumber );
       
   354     }
       
   355 
       
   356 // --------------------------------------------------------------------------
       
   357 // CRemoteStore::CNumberMatchOperation::RunError
       
   358 // --------------------------------------------------------------------------
       
   359 //
       
   360 TInt CRemoteStore::CNumberMatchOperation::RunError( TInt aError )
       
   361     {
       
   362     iObserver.FindError( iStore, aError );
       
   363     return KErrNone;
       
   364     }
       
   365 
       
   366 // --------------------------------------------------------------------------
       
   367 // CRemoteStore::CFindOperation::CFindOperation
       
   368 // C++ default constructor can NOT contain any code, that
       
   369 // might leave.
       
   370 // --------------------------------------------------------------------------
       
   371 //
       
   372 CRemoteStore::CFindOperation::CFindOperation( CRemoteStore& aStore,
       
   373     const TDesC& aStringToFind, MVPbkSimFindObserver& aObserver )
       
   374 :   CActive( EPriorityStandard ),
       
   375     iStore( aStore ),
       
   376     iStringToFind( aStringToFind ),
       
   377     iObserver( aObserver ),
       
   378     iSimIndexBufPtr( NULL, 0 )
       
   379     {
       
   380     }
       
   381 
       
   382 // --------------------------------------------------------------------------
       
   383 // CRemoteStore::CFindOperation::ConstructL
       
   384 // Symbian 2nd phase constructor can leave.
       
   385 // --------------------------------------------------------------------------
       
   386 //
       
   387 void CRemoteStore::CFindOperation::ConstructL( 
       
   388     RVPbkSimFieldTypeArray& aFieldTypes )
       
   389     {
       
   390     CActiveScheduler::Add( this );
       
   391     
       
   392     iFieldTypes = ExternalizeArrayLC( aFieldTypes );
       
   393     CleanupStack::Pop( iFieldTypes );
       
   394     iSimIndexBuf = HBufC8::NewL( KInitSimIndexBufSize );
       
   395     iSimIndexBufPtr.Set( iSimIndexBuf->Des() );
       
   396     }
       
   397 
       
   398 // --------------------------------------------------------------------------
       
   399 // CRemoteStore::CFindOperation::NewL
       
   400 // Two-phased constructor.
       
   401 // --------------------------------------------------------------------------
       
   402 //
       
   403 CRemoteStore::CFindOperation* CRemoteStore::CFindOperation::NewL( 
       
   404     CRemoteStore& aStore, const TDesC& aStringToFind, 
       
   405     MVPbkSimFindObserver& aObserver, RVPbkSimFieldTypeArray& aFieldTypes )
       
   406     {
       
   407     CFindOperation* self = 
       
   408         new( ELeave ) CFindOperation( aStore, aStringToFind, aObserver );
       
   409     CleanupStack::PushL( self );
       
   410     self->ConstructL( aFieldTypes );
       
   411     CleanupStack::Pop( self );
       
   412     return self;
       
   413     }
       
   414 
       
   415 // Destructor
       
   416 CRemoteStore::CFindOperation::~CFindOperation()
       
   417     {
       
   418     Cancel();
       
   419     delete iSimIndexBuf;
       
   420     delete iFieldTypes;
       
   421     }
       
   422 
       
   423 // --------------------------------------------------------------------------
       
   424 // CRemoteStore::CFindOperation::Activate
       
   425 // --------------------------------------------------------------------------
       
   426 //
       
   427 void CRemoteStore::CFindOperation::Activate()
       
   428     {
       
   429     iStore.StoreSession().Find( iStatus, iStringToFind, 
       
   430         *iFieldTypes, iSimIndexBufPtr );
       
   431     SetActive();
       
   432     }
       
   433 
       
   434 // --------------------------------------------------------------------------
       
   435 // CRemoteStore::CFindOperation::RunL
       
   436 // --------------------------------------------------------------------------
       
   437 //
       
   438 void CRemoteStore::CFindOperation::RunL()
       
   439     {
       
   440     RVPbkStreamedIntArray matchedIndexes;
       
   441     CleanupClosePushL( matchedIndexes );
       
   442 
       
   443     TInt result = iStatus.Int();
       
   444     switch ( result )
       
   445         {
       
   446         case KErrNone:
       
   447             {
       
   448             RDesReadStream stream( iSimIndexBufPtr );
       
   449             CleanupClosePushL( stream );
       
   450             stream >> matchedIndexes;
       
   451             CleanupStack::PopAndDestroy(); // stream
       
   452             iObserver.FindCompleted( iStore, matchedIndexes );
       
   453             break;
       
   454             }
       
   455         case KErrNotFound:
       
   456             {
       
   457             iObserver.FindCompleted( iStore, matchedIndexes );
       
   458             break;
       
   459             }
       
   460         case KErrOverflow:
       
   461             {
       
   462             TInt newLength = 2 * iSimIndexBufPtr.MaxLength();
       
   463             if ( newLength > KMaxSimIndexBufSize )
       
   464                 {
       
   465                 iObserver.FindError( iStore, result );
       
   466                 }
       
   467             else
       
   468                 {
       
   469                 VPbkSimStoreImpl::CheckAndUpdateBufferSizeL( iSimIndexBuf, 
       
   470                     iSimIndexBufPtr, newLength );
       
   471                 Activate();
       
   472                 }
       
   473             break;
       
   474             }
       
   475         default:
       
   476             {
       
   477             iObserver.FindError( iStore, result );
       
   478             break;
       
   479             }
       
   480         }
       
   481     CleanupStack::PopAndDestroy(); // matchedIndexes
       
   482     }
       
   483   
       
   484 // --------------------------------------------------------------------------
       
   485 // CRemoteStore::CFindOperation::DoCancel
       
   486 // --------------------------------------------------------------------------
       
   487 //
       
   488 void CRemoteStore::CFindOperation::DoCancel()
       
   489     {
       
   490     iStore.StoreSession().CancelAsyncRequest( EVPbkSimSrvFind );
       
   491     }
       
   492 
       
   493 // --------------------------------------------------------------------------
       
   494 // CRemoteStore::CFindOperation::RunError
       
   495 // --------------------------------------------------------------------------
       
   496 //
       
   497 TInt CRemoteStore::CFindOperation::RunError( TInt aError )
       
   498     {
       
   499     iObserver.FindError( iStore, aError );
       
   500     return KErrNone;
       
   501     }
       
   502     
       
   503 // --------------------------------------------------------------------------
       
   504 // CRemoteStore::CRemoteStore
       
   505 // C++ default constructor can NOT contain any code, that
       
   506 // might leave.
       
   507 // --------------------------------------------------------------------------
       
   508 //
       
   509 CRemoteStore::CRemoteStore( TVPbkSimStoreIdentifier aIdentifier )
       
   510 :   CActive( EPriorityStandard ),
       
   511     iIdentifier( aIdentifier ),
       
   512     iCurrentState( EVPbkSimUnknown )
       
   513     {
       
   514     }
       
   515 
       
   516 // --------------------------------------------------------------------------
       
   517 // CRemoteStore::ConstructL
       
   518 // Symbian 2nd phase constructor can leave.
       
   519 // --------------------------------------------------------------------------
       
   520 //
       
   521 void CRemoteStore::ConstructL()
       
   522     {
       
   523     CActiveScheduler::Add( this );
       
   524     iContactConverter = CVPbkETelCntConverter::NewL();
       
   525     iCurrentETelContact = CVPbkSimContactBuf::NewL( *this );
       
   526     iCurrentContact = CVPbkSimContactBuf::NewL( *this );
       
   527     iSystemPhoneNumberMaxLength = 
       
   528         VPbkSimStoreCommonUtil::SystemMaxPhoneNumberLengthL();
       
   529     iAsyncOpenOp = 
       
   530         CVPbkAsyncObjectOperation<MVPbkSimStoreObserver>::NewL();
       
   531     }
       
   532 
       
   533 // --------------------------------------------------------------------------
       
   534 // CRemoteStore::NewL
       
   535 // Two-phased constructor.
       
   536 // --------------------------------------------------------------------------
       
   537 //
       
   538 CRemoteStore* CRemoteStore::NewL( TVPbkSimStoreIdentifier aIdentifier )
       
   539     {
       
   540     CRemoteStore* self = new( ELeave ) CRemoteStore( aIdentifier );
       
   541     CleanupStack::PushL( self );
       
   542     self->ConstructL();
       
   543     CleanupStack::Pop( self );
       
   544     return self;
       
   545     }
       
   546 
       
   547     
       
   548 // Destructor
       
   549 CRemoteStore::~CRemoteStore()
       
   550     {
       
   551     delete iAsyncOpenOp;
       
   552     iObservers.Close();
       
   553     delete iCurrentContact;
       
   554     delete iCurrentETelContact;
       
   555     delete iContactConverter;
       
   556     ResetAndCloseStore();
       
   557     }
       
   558 
       
   559 // --------------------------------------------------------------------------
       
   560 // CRemoteStore::AddObserverL
       
   561 // --------------------------------------------------------------------------
       
   562 //
       
   563 void CRemoteStore::AddObserverL( MVPbkSimStoreObserver& aObserver )
       
   564     {
       
   565     // adds aObserver to list of observers if it isn't there already, thus
       
   566     // avoids clients registering multiple times
       
   567     if ( iObservers.Find( &aObserver ) == KErrNotFound )
       
   568         {
       
   569         iObservers.AppendL( &aObserver );
       
   570         }
       
   571     }
       
   572 
       
   573 // --------------------------------------------------------------------------
       
   574 // CRemoteStore::RemoveObserver
       
   575 // --------------------------------------------------------------------------
       
   576 //
       
   577 void CRemoteStore::RemoveObserver( MVPbkSimStoreObserver& aObserver )
       
   578     {
       
   579     TInt index = iObservers.Find( &aObserver );
       
   580     if ( index != KErrNotFound )
       
   581         {
       
   582         iObservers.Remove( index );
       
   583         }
       
   584     }
       
   585 
       
   586 // --------------------------------------------------------------------------
       
   587 // CRemoteStore::IsOpen
       
   588 // --------------------------------------------------------------------------
       
   589 //
       
   590 TBool CRemoteStore::IsOpen() const
       
   591     {
       
   592     return iCurrentState == EVPbkSimStoreOpen;
       
   593     }
       
   594 
       
   595 // --------------------------------------------------------------------------
       
   596 // CRemoteStore::RunL
       
   597 // --------------------------------------------------------------------------
       
   598 //
       
   599 void CRemoteStore::RunL()
       
   600     {
       
   601     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   602         "CRemoteStore::RunL: result %d, handle %d, id %d"),
       
   603         iStatus.Int(), iSimStore.Handle(), iIdentifier );
       
   604     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   605         "CRemoteStore::RunL: event %d, eventData %d, eventOpData %d"),
       
   606         iStoreEvent.iEvent, iStoreEvent.iData, iStoreEvent.iOpData);
       
   607         
       
   608     if ( iStatus == KErrNone )
       
   609         {
       
   610         switch ( iStoreEvent.iEvent )
       
   611             {
       
   612             case EVPbkSimPhoneOpen:
       
   613                 {
       
   614                 // Open the store only first time. It's possible
       
   615                 // that phone and store can change unavailable<->open
       
   616                 // later.
       
   617                 if ( iCurrentState == EVPbkSimUnknown )
       
   618                     {
       
   619                     if( iSecurityInfo )
       
   620                         {
       
   621                         iSimStore.OpenL( *iSecurityInfo, iIdentifier );
       
   622                         }
       
   623                     else
       
   624                         {
       
   625                         // OpenL invoked with MVPbkSimCntStore::OpenL
       
   626                         // CRemoteStore::OpenL( secInfo, observer ) should be used instead.
       
   627                         User::Leave( KErrPermissionDenied );
       
   628                         }
       
   629                     }
       
   630                 break;
       
   631                 }
       
   632             case EVPbkSimStoreOpen:
       
   633                 {
       
   634                 iCurrentState = iStoreEvent.iEvent;
       
   635                 iAsyncOpenOp->Purge();
       
   636                 SendStoreEventMessage( iObservers, 
       
   637                     &MVPbkSimStoreObserver::StoreReady, *this );
       
   638                 break;
       
   639                 }
       
   640             case EVPbkSimStoreNotAvailable:
       
   641                 {
       
   642                 iCurrentState = iStoreEvent.iEvent;
       
   643                 iAsyncOpenOp->Purge();
       
   644                 SendStoreEventMessage( iObservers, 
       
   645                     &MVPbkSimStoreObserver::StoreNotAvailable, *this );
       
   646                 break;
       
   647                 }
       
   648             case EVPbkSimContactAdded:
       
   649                 {
       
   650                 SendStoreEventMessage(iObservers, &MVPbkSimStoreObserver::StoreContactEvent, 
       
   651                           MVPbkSimStoreObserver::EContactAdded, 
       
   652                           iStoreEvent.iOpData );
       
   653                 break;
       
   654                 }
       
   655             case EVPbkSimContactDeleted:
       
   656                 {
       
   657                 SendStoreEventMessage(iObservers, &MVPbkSimStoreObserver::StoreContactEvent, 
       
   658                           MVPbkSimStoreObserver::EContactDeleted, 
       
   659                           iStoreEvent.iOpData );
       
   660                 break;
       
   661                 }
       
   662             case EVPbkSimContactChanged:
       
   663                 {
       
   664                 SendStoreEventMessage(iObservers, &MVPbkSimStoreObserver::StoreContactEvent, 
       
   665                           MVPbkSimStoreObserver::EContactChanged, 
       
   666                           iStoreEvent.iOpData );
       
   667                 break;
       
   668                 }
       
   669             // Store is not interested in these events
       
   670             case EVPbkSimPhoneServiceTableUpdated: // FALLTHROUGH
       
   671             case EVPbkSimPhoneFdnStatusChanged:
       
   672                 {
       
   673                 break;
       
   674                 }
       
   675             case EVPbkSimPhoneError:
       
   676                 {
       
   677                 iAsyncOpenOp->Purge();
       
   678                 TInt id = iStoreEvent.iData;
       
   679                 // If not SIM card or BT SAP is active then store is not available
       
   680                 if ( id == MVPbkSimPhoneObserver::ESimCardNotInserted ||
       
   681                      id == MVPbkSimPhoneObserver::EBtSapActive )
       
   682                     {
       
   683                     SendStoreEventMessage( iObservers, 
       
   684                         &MVPbkSimStoreObserver::StoreNotAvailable, *this );
       
   685                     }
       
   686                 else
       
   687                     {
       
   688                     SendStoreErrorMessage( iObservers, *this, iStoreEvent.iOpData );    
       
   689                     }
       
   690                 break;
       
   691                 }
       
   692             case EVPbkSimStoreError: // FALLTHROUGH
       
   693                 {
       
   694                 iCurrentState = iStoreEvent.iEvent;
       
   695                 iAsyncOpenOp->Purge();
       
   696                 SendStoreErrorMessage( iObservers, *this, iStoreEvent.iOpData );
       
   697                 break;
       
   698                 }
       
   699             default:
       
   700                 {
       
   701                 __ASSERT_DEBUG(EFalse, Panic(EUnknownSimStoreEvent));
       
   702                 iAsyncOpenOp->Purge();
       
   703                 SendStoreErrorMessage( iObservers, *this, iStoreEvent.iOpData );
       
   704                 break;
       
   705                 }
       
   706             }
       
   707         }
       
   708     else
       
   709         {
       
   710         // Error case.
       
   711         iCurrentState = EVPbkSimStoreNotAvailable;
       
   712         iAsyncOpenOp->Purge();
       
   713         SendStoreErrorMessage( iObservers, *this, iStatus.Int() );
       
   714         }
       
   715         
       
   716     // check session, it may have been closed during message send
       
   717     if ( iSimStore.Handle() )
       
   718         {
       
   719         // Listen to store event always.
       
   720         iSimStore.ListenToStoreEvents( iStatus, iStoreEvent );
       
   721         SetActive();
       
   722         }
       
   723     }
       
   724 
       
   725 // --------------------------------------------------------------------------
       
   726 // CRemoteStore::DoCancel
       
   727 // --------------------------------------------------------------------------
       
   728 //
       
   729 void CRemoteStore::DoCancel()
       
   730     {
       
   731     iSimStore.CancelAsyncRequest( EVPbkSimSrvStoreEventNotification );
       
   732     }
       
   733 
       
   734 // --------------------------------------------------------------------------
       
   735 // CRemoteStore::RunError
       
   736 // --------------------------------------------------------------------------
       
   737 //
       
   738 TInt CRemoteStore::RunError( TInt aError )
       
   739     {
       
   740     SendStoreErrorMessage( iObservers, *this, aError );
       
   741     return KErrNone;
       
   742     }
       
   743     
       
   744 // --------------------------------------------------------------------------
       
   745 // CRemoteStore::Identifier
       
   746 // --------------------------------------------------------------------------
       
   747 //
       
   748 TVPbkSimStoreIdentifier CRemoteStore::Identifier() const
       
   749     {
       
   750     return iIdentifier;
       
   751     }
       
   752 
       
   753 // --------------------------------------------------------------------------
       
   754 // CRemoteStore::OpenL
       
   755 // --------------------------------------------------------------------------
       
   756 //
       
   757 void CRemoteStore::OpenL( MVPbkSimStoreObserver& aObserver )
       
   758     {
       
   759     if ( iSimStore.Handle() )
       
   760         {
       
   761         CVPbkAsyncObjectCallback<MVPbkSimStoreObserver>* callback =
       
   762                 VPbkEngUtils::CreateAsyncObjectCallbackLC(
       
   763                     *this, 
       
   764                     CRemoteStore::DoOpenL, 
       
   765                     CRemoteStore::DoOpenError, 
       
   766                     aObserver);
       
   767         iAsyncOpenOp->CallbackL( callback );
       
   768         CleanupStack::Pop( callback );
       
   769         }
       
   770     else
       
   771         {
       
   772         // Connect to sim store server
       
   773         iSimStore.ConnectToServerL();
       
   774         // Start listening to store events
       
   775         iSimStore.ListenToStoreEvents( iStatus, iStoreEvent );
       
   776         SetActive();
       
   777         // Open the phone, after phone is open we can open store
       
   778         iSimStore.OpenPhoneL();
       
   779         }
       
   780     
       
   781     AddObserverL( aObserver );
       
   782     }
       
   783 
       
   784 // --------------------------------------------------------------------------
       
   785 // CRemoteStore::OpenL
       
   786 // --------------------------------------------------------------------------
       
   787 //
       
   788 void CRemoteStore::OpenL( const TSecurityInfo& aSecurityInfo, 
       
   789                           MVPbkSimStoreObserver& aObserver )
       
   790     {
       
   791     OpenL( aObserver );
       
   792     iSecurityInfo = &aSecurityInfo;
       
   793     }
       
   794 
       
   795 // --------------------------------------------------------------------------
       
   796 // CRemoteStore::Close
       
   797 // --------------------------------------------------------------------------
       
   798 //
       
   799 void CRemoteStore::Close( MVPbkSimStoreObserver& aObserver )
       
   800     {
       
   801     iAsyncOpenOp->CancelCallback( &aObserver );
       
   802     RemoveObserver( aObserver );
       
   803     ResetAndCloseStore();
       
   804     }
       
   805 
       
   806 // --------------------------------------------------------------------------
       
   807 // CRemoteStore::CreateViewL
       
   808 // --------------------------------------------------------------------------
       
   809 //
       
   810 MVPbkSimCntView* CRemoteStore::CreateViewL(
       
   811         const RVPbkSimFieldTypeArray& aSortOrder, 
       
   812         TVPbkSimViewConstructionPolicy aConstructionPolicy,
       
   813         const TDesC& aViewName,
       
   814         CVPbkSimFieldTypeFilter* aFilter )
       
   815     {
       
   816     // Create always a remote view which is shared in the server
       
   817     // side.
       
   818     return CRemoteView::NewL( *this, aSortOrder, aConstructionPolicy, 
       
   819             aViewName, aFilter );
       
   820     }
       
   821       
       
   822 // --------------------------------------------------------------------------
       
   823 // CRemoteStore::ReadLC
       
   824 // --------------------------------------------------------------------------
       
   825 //
       
   826 const TDesC8* CRemoteStore::AtL( TInt aSimIndex )
       
   827     {
       
   828     HBufC8* cnt = iSimStore.ReadLC( aSimIndex );
       
   829     if ( cnt )
       
   830         {
       
   831         iCurrentETelContact->SetL( *cnt );
       
   832         CleanupStack::PopAndDestroy( cnt );
       
   833         return &iCurrentETelContact->ETelContactL();
       
   834         }
       
   835     return NULL;
       
   836     }
       
   837 
       
   838 // --------------------------------------------------------------------------
       
   839 // CRemoteStore::ContactAtL
       
   840 // --------------------------------------------------------------------------
       
   841 //
       
   842 const MVPbkSimContact* CRemoteStore::ContactAtL( TInt aSimIndex )
       
   843     {
       
   844     HBufC8* cnt = iSimStore.ReadLC( aSimIndex );
       
   845     if ( cnt )
       
   846         {
       
   847         iCurrentContact->SetL( *cnt );
       
   848         CleanupStack::PopAndDestroy( cnt );
       
   849         return iCurrentContact;
       
   850         }
       
   851     return NULL;
       
   852     }
       
   853     
       
   854 // --------------------------------------------------------------------------
       
   855 // CRemoteStore::SaveL
       
   856 // --------------------------------------------------------------------------
       
   857 //
       
   858 MVPbkSimStoreOperation* CRemoteStore::SaveL( const TDesC8& aData, 
       
   859         TInt& aSimIndex, MVPbkSimContactObserver& aObserver )
       
   860     {
       
   861     CAsyncSave* op = new( ELeave ) CAsyncSave( iSimStore );
       
   862     CleanupStack::PushL( op );
       
   863     op->DoRequestL( aData, aSimIndex, aObserver );
       
   864     CleanupStack::Pop( op );
       
   865     return op;
       
   866     }
       
   867 
       
   868 // --------------------------------------------------------------------------
       
   869 // CRemoteStore::DeleteL
       
   870 // --------------------------------------------------------------------------
       
   871 //
       
   872 MVPbkSimStoreOperation* CRemoteStore::DeleteL( 
       
   873         RVPbkStreamedIntArray& aSimIndexes, 
       
   874         MVPbkSimContactObserver& aObserver )
       
   875     {    
       
   876     CAsyncDelete* op = new( ELeave ) CAsyncDelete( iSimStore );
       
   877     CleanupStack::PushL( op );
       
   878     op->DoRequestL( aSimIndexes, aObserver );
       
   879     CleanupStack::Pop( op );
       
   880     return op;
       
   881     }
       
   882     
       
   883 // --------------------------------------------------------------------------
       
   884 // CRemoteStore::GetGsmStoreProperties
       
   885 // --------------------------------------------------------------------------
       
   886 //
       
   887 TInt CRemoteStore::GetGsmStoreProperties( 
       
   888     TVPbkGsmStoreProperty& aGsmProperties ) const
       
   889     {
       
   890     return iSimStore.GetGsmStoreProperties( aGsmProperties );
       
   891     }
       
   892 
       
   893 // --------------------------------------------------------------------------
       
   894 // CRemoteStore::GetUSimStoreProperties
       
   895 // --------------------------------------------------------------------------
       
   896 //
       
   897 TInt CRemoteStore::GetUSimStoreProperties( 
       
   898     TVPbkUSimStoreProperty& aUSimProperties ) const
       
   899     {
       
   900     return iSimStore.GetUSimStoreProperties( aUSimProperties );
       
   901     }
       
   902 
       
   903 // --------------------------------------------------------------------------
       
   904 // CRemoteStore::ContactConverter
       
   905 // --------------------------------------------------------------------------
       
   906 //
       
   907 const CVPbkETelCntConverter& CRemoteStore::ContactConverter() const
       
   908     {
       
   909     return *iContactConverter;
       
   910     }
       
   911 
       
   912 // --------------------------------------------------------------------------
       
   913 // CRemoteStore::CreateMatchPhoneNumberOperationL
       
   914 // --------------------------------------------------------------------------
       
   915 //
       
   916 MVPbkSimStoreOperation* CRemoteStore::CreateMatchPhoneNumberOperationL( 
       
   917     const TDesC& aPhoneNumber, TInt aMaxMatchDigits, 
       
   918     MVPbkSimFindObserver& aObserver )
       
   919     {
       
   920     CNumberMatchOperation* op = CNumberMatchOperation::NewL( *this, 
       
   921         aPhoneNumber, aMaxMatchDigits, aObserver );
       
   922     op->Activate();
       
   923     return op;
       
   924     }
       
   925 
       
   926 // --------------------------------------------------------------------------
       
   927 // CRemoteStore::CreateFindOperationL
       
   928 // --------------------------------------------------------------------------
       
   929 //
       
   930 MVPbkSimStoreOperation* CRemoteStore::CreateFindOperationL( 
       
   931     const TDesC& aStringToFind, RVPbkSimFieldTypeArray& aFieldTypes,
       
   932     MVPbkSimFindObserver& aObserver )
       
   933     {
       
   934     CFindOperation* op = 
       
   935         CFindOperation::NewL( *this, aStringToFind, aObserver, aFieldTypes );
       
   936     op->Activate();
       
   937     return op;
       
   938     }
       
   939 
       
   940 // --------------------------------------------------------------------------
       
   941 // CRemoteStore::SystemPhoneNumberMaxLength
       
   942 // --------------------------------------------------------------------------
       
   943 //
       
   944 TInt CRemoteStore::SystemPhoneNumberMaxLength() const
       
   945     {
       
   946     return iSystemPhoneNumberMaxLength;
       
   947     }
       
   948     
       
   949 // --------------------------------------------------------------------------
       
   950 // CRemoteStore::ResetAndCloseStore
       
   951 // --------------------------------------------------------------------------
       
   952 //
       
   953 void CRemoteStore::ResetAndCloseStore()
       
   954     {
       
   955     if ( iObservers.Count() == 0 )
       
   956         {
       
   957         // Close session if no observers
       
   958         Cancel();
       
   959         iSimStore.Close();
       
   960         iCurrentState = EVPbkSimUnknown;
       
   961         }
       
   962     }
       
   963 
       
   964 // --------------------------------------------------------------------------
       
   965 // CRemoteStore::DoOpenL
       
   966 // --------------------------------------------------------------------------
       
   967 //
       
   968 void CRemoteStore::DoOpenL( MVPbkSimStoreObserver& aObserver )
       
   969     {
       
   970     switch ( iCurrentState )
       
   971         {
       
   972         case EVPbkSimStoreOpen:
       
   973             {
       
   974             aObserver.StoreReady( *this );
       
   975             break;
       
   976             }
       
   977         case EVPbkSimUnknown:
       
   978             {
       
   979             // Wait that store receives its state from server.
       
   980             break;
       
   981             }
       
   982         default:
       
   983             {
       
   984             aObserver.StoreNotAvailable( *this );
       
   985             break;
       
   986             }
       
   987         }
       
   988     }
       
   989 
       
   990 // --------------------------------------------------------------------------
       
   991 // CRemoteStore::DoOpenError
       
   992 // --------------------------------------------------------------------------
       
   993 //
       
   994 void CRemoteStore::DoOpenError( MVPbkSimStoreObserver& aObserver, 
       
   995         TInt aError )
       
   996     {
       
   997     aObserver.StoreError( *this, aError );
       
   998     }
       
   999 } // namespace VPbkSimStore
       
  1000 //  End of File