phonebookengines/VirtualPhonebook/VPbkSimStoreImpl/src/CDeleteCmd.cpp
branchRCL_3
changeset 20 f4a778e096c2
parent 0 e686773b3f54
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2005-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 command that deletes a contact from the (U)SIM using ETel
       
    15 *                RMobilePhoneStore Delete
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "CDeleteCmd.h"
       
    23 
       
    24 #include "VPbkSimStoreImplError.h"
       
    25 #include "CStoreBase.h"
       
    26 #include "CContactArray.h"
       
    27 #include "CETelStoreNotification.h"
       
    28 
       
    29 #include <MVPbkSimContactObserver.h>
       
    30 #include <RVPbkStreamedIntArray.h>
       
    31 #include <MVPbkSimCommand.h>
       
    32 
       
    33 #include <VPbkDebug.h>
       
    34 
       
    35 namespace VPbkSimStoreImpl {
       
    36 
       
    37 // ============================ MEMBER FUNCTIONS ===============================
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CDeleteCmd::CDeleteCmd
       
    41 // C++ default constructor can NOT contain any code, that
       
    42 // might leave.
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CDeleteCmd::CDeleteCmd( CStoreBase& aStore, 
       
    46         MVPbkSimContactObserver& aObserver  )
       
    47         :   CActive( EPriorityStandard ),
       
    48             iStore( aStore ),
       
    49             iObserver( aObserver )
       
    50     {
       
    51     CActiveScheduler::Add( this );
       
    52     }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // CDeleteCmd::ConstructL
       
    56 // Symbian 2nd phase constructor can leave.
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 void CDeleteCmd::ConstructL( RVPbkStreamedIntArray& aIndexArray )
       
    60     {
       
    61     TInt lastSimIndex = iStore.Contacts().Size();
       
    62     const TInt indexCount = aIndexArray.Count();
       
    63     for ( TInt i = 0; i < indexCount; ++i )
       
    64         {
       
    65         TInt simIndex = aIndexArray[i];
       
    66         // Check the validity of SIM index and append only indexes that
       
    67         // contain a contact.
       
    68         if ( simIndex > 0 && simIndex <= lastSimIndex )
       
    69             {
       
    70             iIndexArray.AppendL( simIndex );
       
    71             }
       
    72         }
       
    73         
       
    74     if ( iStore.ETelNotification() )
       
    75         {
       
    76         // listen to store events to consume the event if it's caused
       
    77         // by this command
       
    78         iStore.ETelNotification()->SetHighPriorityObserver( *this );
       
    79         }
       
    80     }
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CDeleteCmd::NewL
       
    84 // Two-phased constructor.
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 CDeleteCmd* CDeleteCmd::NewL( CStoreBase& aStore, 
       
    88                               RVPbkStreamedIntArray& aIndexArray,
       
    89                               MVPbkSimContactObserver& aObserver )
       
    90     {
       
    91     CDeleteCmd* self = 
       
    92         new( ELeave ) CDeleteCmd( aStore, aObserver );
       
    93     CleanupStack::PushL( self );
       
    94     self->ConstructL( aIndexArray );
       
    95     CleanupStack::Pop( self );
       
    96     return self;
       
    97     }
       
    98     
       
    99 // Destructor
       
   100 CDeleteCmd::~CDeleteCmd()
       
   101     {
       
   102     Cancel();
       
   103     RemoveNotificationObserver();
       
   104     delete iUpdateCommand;
       
   105     iIndexArray.Close();
       
   106     }
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CDeleteCmd::Execute
       
   110 // -----------------------------------------------------------------------------
       
   111 //
       
   112 void CDeleteCmd::Execute()
       
   113     {
       
   114     if ( iIndexArray.Count() == 0 )
       
   115         {
       
   116         // There was no valid indexes in client's array. Use KErrArgument and
       
   117         // Complete asynchronously.
       
   118         CompleteRequest( KErrArgument );
       
   119         }
       
   120     else
       
   121         {
       
   122         TRAPD( res, DeleteNextL() );
       
   123         if ( res != KErrNone )
       
   124             {
       
   125             // Complete asynchronously
       
   126             CompleteRequest( res );
       
   127             }
       
   128         }
       
   129     }
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // CDeleteCmd::RunL
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 void CDeleteCmd::RunL()
       
   136     {
       
   137     TInt result = iStatus.Int();
       
   138 
       
   139     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   140         "VPbkSimStoreImpl: RMobilePhoneStore::Delete h%d complete %d"),
       
   141         iStore.ETelStoreBase().SubSessionHandle(),result);
       
   142 
       
   143     switch ( result )
       
   144         {
       
   145         case KErrNone:
       
   146             {
       
   147             iUpdateCommand->Execute();                
       
   148             break;
       
   149             }
       
   150         case KErrNotFound:
       
   151             {
       
   152             // Contact was not there -> it's ok because this is deleting
       
   153             // Try to delete next one.
       
   154             TInt simIndex = NextContactIndex();
       
   155             if ( simIndex != KErrNotFound )
       
   156                 {
       
   157                 DeleteNextL( simIndex );
       
   158                 }
       
   159             else
       
   160                 {
       
   161                 CompleteCommand();
       
   162                 }
       
   163             break;
       
   164             }
       
   165         default:
       
   166             {
       
   167             HandleError( result );
       
   168             break;
       
   169             }
       
   170         }
       
   171     }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // CDeleteCmd::DoCancel
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 void CDeleteCmd::DoCancel()
       
   178     {
       
   179     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   180         "VPbkSimStoreImpl: RMobilePhoneStore::Delete h%d Cancel"),
       
   181         iStore.ETelStoreBase().SubSessionHandle());
       
   182     iStore.ETelStoreBase().CancelAsyncRequest( EMobilePhoneStoreDelete );
       
   183     }
       
   184 
       
   185 // -----------------------------------------------------------------------------
       
   186 // CDeleteCmd::DoCancel
       
   187 // -----------------------------------------------------------------------------
       
   188 //
       
   189 TInt CDeleteCmd::RunError( TInt aError )
       
   190     {
       
   191     HandleError( aError );
       
   192     return KErrNone;
       
   193     }
       
   194     
       
   195 // -----------------------------------------------------------------------------
       
   196 // CDeleteCmd::ETelStoreChanged
       
   197 // Consume the notification if it's same index that is being modified by
       
   198 // this command. Otherwise the event must not be consumed because some
       
   199 // other component caused the event by using ETel API and not VPbk API
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 TBool CDeleteCmd::ETelStoreChanged( TInt aSimIndex, TUint32 /*aEvents*/ )
       
   203     {
       
   204     if ( iCurSimIndex == aSimIndex )
       
   205         {
       
   206         return ETrue;
       
   207         }
       
   208     return EFalse;
       
   209     }
       
   210   
       
   211 // -----------------------------------------------------------------------------
       
   212 // CDeleteCmd::ETelStoreChangeError
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 TBool CDeleteCmd::ETelStoreChangeError( TInt /*aError*/ )
       
   216     {
       
   217     return EFalse;
       
   218     }
       
   219 
       
   220 // -----------------------------------------------------------------------------
       
   221 // CDeleteCmd::CommandDone
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 void CDeleteCmd::CommandDone( MVPbkSimCommand& /*aCommand*/ )
       
   225     {
       
   226     TRAPD( res, DeleteNextL() );
       
   227     if ( res != KErrNone )
       
   228         {
       
   229         HandleError( res );
       
   230         }
       
   231     }
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 // CDeleteCmd::CommandError
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 void CDeleteCmd::CommandError( MVPbkSimCommand& /*aCommand*/, TInt aError )
       
   238     {
       
   239     HandleError( aError );
       
   240     }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // CDeleteCmd::DeleteNextL
       
   244 // -----------------------------------------------------------------------------
       
   245 //
       
   246 void CDeleteCmd::DeleteNextL()
       
   247     {
       
   248     // Index validity has been checked in ConstructL. Find first slot
       
   249     // that contains a contact
       
   250     TInt simIndexToDelete = NextContactIndex();
       
   251     if ( simIndexToDelete != KErrNotFound )
       
   252         {
       
   253         DeleteNextL( simIndexToDelete );
       
   254         }
       
   255     else
       
   256         {
       
   257         // If nothing to delete then this operation has done it's job
       
   258         // successfully.
       
   259         CompleteRequest( KErrNotFound );
       
   260         }
       
   261     }
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CDeleteCmd::DeleteNextL
       
   265 // -----------------------------------------------------------------------------
       
   266 //
       
   267 void CDeleteCmd::DeleteNextL( TInt aSimIndex )
       
   268     {
       
   269     // Delete the previous update command
       
   270     delete iUpdateCommand;
       
   271     iUpdateCommand = NULL;
       
   272     // Reserve memory for an update command that will read the slot 
       
   273     // and update other store information after deletion
       
   274     iUpdateCommand = iStore.CreateUpdateCommandL( aSimIndex );
       
   275     // Add observer so that the this command will be notified
       
   276     // after the update is done.
       
   277     iUpdateCommand->AddObserverL( *this );
       
   278     
       
   279     // Finally the contact can be deleted
       
   280     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   281         "VPbkSimStoreImpl: RMobilePhoneStore::Delete h%d slot %d"), 
       
   282         iStore.ETelStoreBase().SubSessionHandle(), aSimIndex);
       
   283     iStore.ETelStoreBase().Delete( iStatus, aSimIndex );
       
   284     iCurSimIndex = aSimIndex;
       
   285     SetActive();
       
   286     }
       
   287 
       
   288 // -----------------------------------------------------------------------------
       
   289 // CDeleteCmd::NextContactIndex
       
   290 // -----------------------------------------------------------------------------
       
   291 //
       
   292 TInt CDeleteCmd::NextContactIndex()
       
   293     {
       
   294     // Index validity has been checked in ConstructL. Find first slot
       
   295     // that contains a contact
       
   296     TInt simIndexToDelete = KErrNotFound;
       
   297     while ( simIndexToDelete == KErrNotFound && iIndexArray.Count() > 0 )
       
   298         {
       
   299         if ( iStore.Contacts().At( iIndexArray[0] ) )
       
   300             {
       
   301             simIndexToDelete = iIndexArray[0];
       
   302             }
       
   303         iIndexArray.Remove( 0 );
       
   304         }
       
   305     return simIndexToDelete;
       
   306     }
       
   307     
       
   308 // -----------------------------------------------------------------------------
       
   309 // CDeleteCmd::CompleteRequest
       
   310 // -----------------------------------------------------------------------------
       
   311 //
       
   312 void CDeleteCmd::CompleteRequest( TInt aRequestResult )
       
   313     {
       
   314     // Delete request was not done so complete with internal result
       
   315     VPBK_DEBUG_PRINT(VPBK_DEBUG_STRING(
       
   316         "VPbkSimStoreImpl: CDeleteCmd::CompleteRequest dummy request %d"), 
       
   317         aRequestResult );
       
   318     TRequestStatus* status = &iStatus;
       
   319     User::RequestComplete( status, aRequestResult );
       
   320     SetActive();
       
   321     }
       
   322     
       
   323 // -----------------------------------------------------------------------------
       
   324 // CDeleteCmd::CompleteCommand
       
   325 // -----------------------------------------------------------------------------
       
   326 //
       
   327 void CDeleteCmd::CompleteCommand()
       
   328     {
       
   329     RemoveNotificationObserver();
       
   330     iObserver.ContactEventComplete( MVPbkSimContactObserver::EDelete, NULL );
       
   331     }
       
   332     
       
   333 // -----------------------------------------------------------------------------
       
   334 // CDeleteCmd::HandleError
       
   335 // -----------------------------------------------------------------------------
       
   336 //
       
   337 void CDeleteCmd::HandleError( TInt aError )
       
   338     {
       
   339     RemoveNotificationObserver();
       
   340     iObserver.ContactEventError( MVPbkSimContactObserver::EDelete, NULL, 
       
   341         aError );
       
   342     }
       
   343 
       
   344 // -----------------------------------------------------------------------------
       
   345 // CDeleteCmd::RemoveNotificationObserver
       
   346 // -----------------------------------------------------------------------------
       
   347 //
       
   348 void CDeleteCmd::RemoveNotificationObserver()
       
   349     {
       
   350     if ( iStore.ETelNotification() )
       
   351         {
       
   352         iStore.ETelNotification()->RemoveHighPriorityObserver( *this );
       
   353         }
       
   354     }
       
   355 
       
   356 } // namespace VPbkSimStoreImpl
       
   357 //  End of File