phonebookengines/VirtualPhonebook/VPbkSimStoreImpl/src/CVPbkSimCommandStore.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 command store for handling lifetime of the commands
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CVPbkSimCommandStore.h"
       
    22 #include "VPbkSimStoreImplError.h"
       
    23 #include "MVPbkSimCommand.h"
       
    24 
       
    25 // DATA TYPES
       
    26 
       
    27 namespace
       
    28 {
       
    29 
       
    30 /**
       
    31 * The state of the command
       
    32 */
       
    33 enum TCommandState
       
    34     {
       
    35     /// command is active
       
    36     EExecuting,
       
    37     /// command is waiting to be executed
       
    38     EWaiting,
       
    39     /// command is wating to be deleted
       
    40     EToBeDeleted
       
    41     };
       
    42 }
       
    43 
       
    44 // ============================ MEMBER FUNCTIONS ===============================
       
    45 
       
    46 /**
       
    47 * A simple helper class to know which commands should be deleted
       
    48 */
       
    49 NONSHARABLE_CLASS( CVPbkSimCommandStore::CCmd ): public CBase
       
    50     {
       
    51     public:
       
    52         CCmd( MVPbkSimCommand* aCommand );
       
    53         ~CCmd();
       
    54 
       
    55     public: // Data
       
    56         MVPbkSimCommand* iCommand;
       
    57         TCommandState iState;
       
    58     };
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CVPbkSimCommandStore::CCmd::CCmd
       
    62 // C++ default constructor can NOT contain any code, that
       
    63 // might leave.
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 CVPbkSimCommandStore::CCmd::CCmd( MVPbkSimCommand* aCommand )
       
    67 :   iCommand( aCommand )
       
    68     {
       
    69     }
       
    70 
       
    71 CVPbkSimCommandStore::CCmd::~CCmd()
       
    72     {
       
    73     delete iCommand;
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CVPbkSimCommandStore::CVPbkSimCommandStore
       
    78 // C++ default constructor can NOT contain any code, that
       
    79 // might leave.
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 CVPbkSimCommandStore::CVPbkSimCommandStore( TBool aExecuteOneAtTime )
       
    83 :   iExecuteOneAtTime( aExecuteOneAtTime )
       
    84     {
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CVPbkSimCommandStore::ConstructL
       
    89 // Symbian 2nd phase constructor can leave.
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 void CVPbkSimCommandStore::ConstructL()
       
    93     {
       
    94     iIdleDestroyer = CIdle::NewL( CActive::EPriorityIdle );
       
    95     iIdleCmdRunner = CIdle::NewL( CActive::EPriorityStandard );
       
    96     }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CVPbkSimCommandStore::NewL
       
   100 // Two-phased constructor.
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 EXPORT_C CVPbkSimCommandStore* CVPbkSimCommandStore::NewL( 
       
   104     TBool aExecuteOneAtTime )
       
   105     {
       
   106     CVPbkSimCommandStore* self = 
       
   107         new( ELeave ) CVPbkSimCommandStore( aExecuteOneAtTime );
       
   108     CleanupStack::PushL( self );
       
   109     self->ConstructL();
       
   110     CleanupStack::Pop( self );
       
   111     return self;
       
   112     }
       
   113 
       
   114 // Destructor
       
   115 CVPbkSimCommandStore::~CVPbkSimCommandStore()
       
   116     {
       
   117     delete iIdleCmdRunner;
       
   118     delete iIdleDestroyer;
       
   119     iCmdArray.ResetAndDestroy();
       
   120     iCmdArray.Close();
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // CVPbkSimCommandStore::AddAndExecuteCommandL
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 EXPORT_C void CVPbkSimCommandStore::AddAndExecuteCommandL( 
       
   128     MVPbkSimCommand* aCommand )
       
   129     {
       
   130     CleanupDeletePushL( aCommand );
       
   131     CCmd* cmd = new( ELeave ) CCmd( aCommand );
       
   132     CleanupStack::Pop( aCommand ); 
       
   133     CleanupStack::PushL( cmd );
       
   134     iCmdArray.AppendL( cmd );
       
   135     CleanupStack::Pop( cmd );
       
   136     
       
   137     aCommand->AddObserverL( *this );
       
   138 
       
   139     if ( iExecuteOneAtTime )
       
   140         {
       
   141         cmd->iState = EWaiting;
       
   142         RunNextCommand();
       
   143         }
       
   144     else
       
   145         {
       
   146         cmd->iState = EExecuting;
       
   147         aCommand->Execute();
       
   148         }
       
   149     }
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CVPbkSimCommandStore::CancelAll
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 EXPORT_C void CVPbkSimCommandStore::CancelAll()
       
   156     {
       
   157     const TInt count = iCmdArray.Count();
       
   158     for ( TInt i = 0; i < count; ++i )
       
   159         {
       
   160         if ( iCmdArray[i]->iState == EExecuting ||
       
   161              iCmdArray[i]->iState == EWaiting )
       
   162             {
       
   163             iCmdArray[i]->iCommand->CancelCmd();
       
   164             }
       
   165         iCmdArray[i]->iState = EToBeDeleted;
       
   166         }
       
   167 
       
   168     StartAsyncDelete();
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CVPbkSimCommandStore::CancelCommand
       
   173 // -----------------------------------------------------------------------------
       
   174 //
       
   175 EXPORT_C void CVPbkSimCommandStore::CancelCommand( MVPbkSimCommand& aCommand )
       
   176     {
       
   177     TInt pos = FindCommandPosition( aCommand );
       
   178     if ( pos >= 0 )
       
   179         {
       
   180         if ( iCmdArray[pos]->iState == EExecuting )
       
   181             {
       
   182             /// Run next waiting command if exists
       
   183             StartAsyncCommandRunner();
       
   184             }
       
   185         
       
   186         if ( iCmdArray[pos]->iState != EToBeDeleted )
       
   187             {
       
   188             // Cancel it.
       
   189             iCmdArray[pos]->iCommand->CancelCmd();
       
   190             // Set command to delete state...
       
   191             iCmdArray[pos]->iState = EToBeDeleted;
       
   192             // ...and start idle deletion
       
   193             StartAsyncDelete();
       
   194             }
       
   195         }
       
   196     }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CVPbkSimCommandStore::Executing
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 TBool CVPbkSimCommandStore::Executing() const
       
   203     {
       
   204     TInt count = iCmdArray.Count();
       
   205     for ( TInt i = 0; i < count; ++i )
       
   206         {
       
   207         if ( iCmdArray[i]->iState == EExecuting )
       
   208             {
       
   209             return ETrue;
       
   210             }
       
   211         }
       
   212     return EFalse;
       
   213     }
       
   214 
       
   215 // -----------------------------------------------------------------------------
       
   216 // CVPbkSimCommandStore::CommandDone
       
   217 // -----------------------------------------------------------------------------
       
   218 //
       
   219 void CVPbkSimCommandStore::CommandDone( MVPbkSimCommand& aCommand )
       
   220     {
       
   221     HandleCommandComplete( aCommand );
       
   222     }
       
   223 
       
   224 // -----------------------------------------------------------------------------
       
   225 // CVPbkSimCommandStore::CommandError
       
   226 // -----------------------------------------------------------------------------
       
   227 //
       
   228 void CVPbkSimCommandStore::CommandError( MVPbkSimCommand& aCommand, 
       
   229     TInt /*aError*/ )
       
   230     {
       
   231     HandleCommandComplete( aCommand );
       
   232     }
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // CVPbkSimCommandStore::RunNextCommand
       
   236 // -----------------------------------------------------------------------------
       
   237 //
       
   238 void CVPbkSimCommandStore::RunNextCommand()
       
   239     {
       
   240     CVPbkSimCommandStore::CCmd* firstWaitingCmd = NULL;
       
   241     TInt count = iCmdArray.Count();
       
   242     for ( TInt i = 0; i < count; ++i )
       
   243         {
       
   244         if ( iCmdArray[i]->iState == EExecuting )
       
   245             {
       
   246             // Active command found -> don't start next command
       
   247             return;
       
   248             }
       
   249         else if ( iCmdArray[i]->iState == EWaiting )
       
   250             {
       
   251             firstWaitingCmd = iCmdArray[i];
       
   252             /// first waiting command found -> break the loop and execute it
       
   253             break;
       
   254             }
       
   255         }
       
   256 
       
   257     if ( firstWaitingCmd )
       
   258         {
       
   259         firstWaitingCmd->iState = EExecuting;
       
   260         firstWaitingCmd->iCommand->Execute();
       
   261         }
       
   262     }
       
   263 
       
   264 // -----------------------------------------------------------------------------
       
   265 // CVPbkSimCommandStore::HandleCommandComplete
       
   266 // -----------------------------------------------------------------------------
       
   267 //
       
   268 void CVPbkSimCommandStore::HandleCommandComplete( MVPbkSimCommand& aCommand )
       
   269     {
       
   270     TInt pos = FindCommandPosition( aCommand );
       
   271     if ( pos >= 0 )
       
   272         {
       
   273         iCmdArray[pos]->iState = EToBeDeleted;
       
   274         }
       
   275 
       
   276     StartAsyncDelete();
       
   277     StartAsyncCommandRunner();
       
   278     }
       
   279 
       
   280 // -----------------------------------------------------------------------------
       
   281 // CVPbkSimCommandStore::StartAsyncDelete
       
   282 // -----------------------------------------------------------------------------
       
   283 //
       
   284 void CVPbkSimCommandStore::StartAsyncDelete()
       
   285     {
       
   286     iIdleDestroyer->Cancel();
       
   287     iIdleDestroyer->Start( 
       
   288         TCallBack( (&CVPbkSimCommandStore::IdleDestroyerCallback), this ) );
       
   289     }
       
   290 
       
   291 // -----------------------------------------------------------------------------
       
   292 // CVPbkSimCommandStore::StartAsyncCommandRunner
       
   293 // -----------------------------------------------------------------------------
       
   294 //
       
   295 void CVPbkSimCommandStore::StartAsyncCommandRunner()
       
   296     {
       
   297     iIdleCmdRunner->Cancel();
       
   298     iIdleCmdRunner->Start(
       
   299         TCallBack( ( &CmdRunnerCallback ), this ) );
       
   300     }
       
   301     
       
   302 // -----------------------------------------------------------------------------
       
   303 // CVPbkSimCommandStore::IdleDestroyerCallback
       
   304 // -----------------------------------------------------------------------------
       
   305 //
       
   306 TInt CVPbkSimCommandStore::IdleDestroyerCallback( TAny* aSelf )
       
   307     {
       
   308     static_cast<CVPbkSimCommandStore*>( aSelf )->DestroyCommands();
       
   309     return EFalse;
       
   310     }
       
   311 
       
   312 // -----------------------------------------------------------------------------
       
   313 // CVPbkSimCommandStore::DestroyCommands
       
   314 // -----------------------------------------------------------------------------
       
   315 //
       
   316 void CVPbkSimCommandStore::DestroyCommands()
       
   317     {
       
   318     TInt count = iCmdArray.Count();
       
   319     for ( TInt i = count - 1; i >= 0; --i )
       
   320         {
       
   321         if ( iCmdArray[i]->iState == EToBeDeleted )
       
   322             {
       
   323             delete iCmdArray[i];
       
   324             iCmdArray.Remove( i );
       
   325             }
       
   326         }
       
   327     }
       
   328 
       
   329 // -----------------------------------------------------------------------------
       
   330 // CVPbkSimCommandStore::CmdRunnerCallback
       
   331 // -----------------------------------------------------------------------------
       
   332 //
       
   333 TInt CVPbkSimCommandStore::CmdRunnerCallback( TAny* aSelf )
       
   334     {
       
   335     static_cast<CVPbkSimCommandStore*>( aSelf )->RunNextCommand();
       
   336     return EFalse;
       
   337     }
       
   338 
       
   339 // -----------------------------------------------------------------------------
       
   340 // CVPbkSimCommandStore::FindCommandPosition
       
   341 // -----------------------------------------------------------------------------
       
   342 //
       
   343 TInt CVPbkSimCommandStore::FindCommandPosition( MVPbkSimCommand& aCommand )
       
   344     {
       
   345     TInt count = iCmdArray.Count();
       
   346     for ( TInt i = 0; i < count; ++i )
       
   347         {
       
   348         if ( iCmdArray[i]->iCommand == &aCommand )
       
   349             {
       
   350             return i;
       
   351             }
       
   352         }
       
   353     return KErrNotFound;
       
   354     }
       
   355 
       
   356 //  End of File