phonebookui/Phonebook2/Commands/src/CPbk2CommandStore.cpp
branchRCL_3
changeset 20 f4a778e096c2
child 21 9da50d567e3c
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:  Phonebook 2 command store.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "CPbk2CommandStore.h"
       
    21 #include "MPbk2CommandResourceRelease.h"
       
    22 
       
    23 // Phonebook 2
       
    24 #include <MPbk2Command.h>
       
    25 #include <MPbk2MenuCommandObserver.h>
       
    26 
       
    27 // --------------------------------------------------------------------------
       
    28 // CPbk2CommandStore::CPbk2CommandStore
       
    29 // --------------------------------------------------------------------------
       
    30 //
       
    31 inline CPbk2CommandStore::CPbk2CommandStore() :
       
    32 iInputBlocker( NULL )
       
    33     {
       
    34     }
       
    35 
       
    36 // --------------------------------------------------------------------------
       
    37 // CPbk2CommandStore::~CPbk2CommandStore
       
    38 // --------------------------------------------------------------------------
       
    39 //
       
    40 CPbk2CommandStore::~CPbk2CommandStore()
       
    41     {
       
    42     iIdleDestructableCommands.Reset();
       
    43     iIdleDestructableCommands.Close();
       
    44     iCommandObservers.Reset();
       
    45     iCommandObservers.Close();
       
    46     iCommandArray.ResetAndDestroy();
       
    47     iCommandArray.Close();
       
    48     delete iIdleDestroyer;
       
    49     delete iInputBlocker;
       
    50     }
       
    51 
       
    52 // --------------------------------------------------------------------------
       
    53 // CPbk2CommandStore::NewL
       
    54 // --------------------------------------------------------------------------
       
    55 //
       
    56 EXPORT_C CPbk2CommandStore* CPbk2CommandStore::NewL()
       
    57     {
       
    58     CPbk2CommandStore* self = new ( ELeave ) CPbk2CommandStore;
       
    59     CleanupStack::PushL( self );
       
    60     self->ConstructL();
       
    61     CleanupStack::Pop( self );
       
    62     return self;
       
    63     }
       
    64 
       
    65 // --------------------------------------------------------------------------
       
    66 // CPbk2CommandStore::ConstructL
       
    67 // --------------------------------------------------------------------------
       
    68 //
       
    69 inline void CPbk2CommandStore::ConstructL()
       
    70     {
       
    71     iIdleDestroyer = CIdle::NewL( CActive::EPriorityIdle );
       
    72     }
       
    73 
       
    74 // --------------------------------------------------------------------------
       
    75 // CPbk2CommandStore::AddMenuCommandObserver
       
    76 // --------------------------------------------------------------------------
       
    77 //
       
    78 void CPbk2CommandStore::AddMenuCommandObserver
       
    79         ( MPbk2MenuCommandObserver& aObserver )
       
    80     {
       
    81     TRAP_IGNORE( iCommandObservers.AppendL( &aObserver ) );
       
    82     }
       
    83 
       
    84 
       
    85 // --------------------------------------------------------------------------
       
    86 // CPbk2CommandStore::RemoveMenuCommandObserver
       
    87 // --------------------------------------------------------------------------
       
    88 //
       
    89 void CPbk2CommandStore::RemoveMenuCommandObserver
       
    90         ( MPbk2MenuCommandObserver& aObserver )
       
    91     {
       
    92     TInt index = iCommandObservers.Find( &aObserver );
       
    93     if ( index > KErrNotFound )
       
    94         {
       
    95         iCommandObservers.Remove( index );
       
    96         }
       
    97     }
       
    98 
       
    99 // --------------------------------------------------------------------------
       
   100 // CPbk2CommandStore::AddAndExecuteCommandL
       
   101 // --------------------------------------------------------------------------
       
   102 //
       
   103 EXPORT_C void CPbk2CommandStore::AddAndExecuteCommandL
       
   104         ( MPbk2Command* aCommand )
       
   105     {
       
   106     const TInt KEmpty( 0 );
       
   107     if ( aCommand )
       
   108         {
       
   109         // If command array is empty, execute command.
       
   110         // Otherwise delete command to avoid overlapping commands.
       
   111         if ( iCommandArray.Count() == KEmpty )
       
   112             {
       
   113             // Avoid user input during command execution
       
   114             if( iInputBlocker )
       
   115                 {
       
   116                 iInputBlocker->Cancel();
       
   117                 }
       
   118             iInputBlocker = CAknInputBlock::NewCancelHandlerLC( this );
       
   119             CleanupStack::Pop( iInputBlocker );   
       
   120             iInputBlocker->SetCancelDelete( iInputBlocker );
       
   121                         
       
   122             // notify all command observers
       
   123             for ( TInt i = 0; i < iCommandObservers.Count(); ++i )
       
   124                 {
       
   125                 iCommandObservers[i]->PreCommandExecutionL( *aCommand );
       
   126                 }
       
   127             aCommand->AddObserver( *this );
       
   128                         
       
   129             // Some command observers needs always postcommand call,
       
   130             // if ExecuteLD leaves
       
   131             TRAPD( err, aCommand->ExecuteLD() );
       
   132             if ( err != KErrNone )
       
   133                 {
       
   134                 for (TInt i = 0; i < iCommandObservers.Count(); ++i)
       
   135                     {
       
   136                     // ignore leave, we are leaving anyway after this
       
   137                     TRAP_IGNORE
       
   138                         ( iCommandObservers[i]->PostCommandExecutionL( *aCommand ) );
       
   139                     }
       
   140                 
       
   141                 // stop user input blocker
       
   142                 if( iInputBlocker )
       
   143                     {
       
   144                     iInputBlocker->Cancel();
       
   145                     }
       
   146                 
       
   147                 User::Leave( err );
       
   148                 }
       
   149             User::LeaveIfError( iCommandArray.Append( aCommand ) );
       
   150             }
       
   151          else
       
   152             {
       
   153             delete aCommand;
       
   154             aCommand = NULL;
       
   155             }
       
   156         }
       
   157     }
       
   158 
       
   159 // --------------------------------------------------------------------------
       
   160 // CPbk2CommandStore::CommandFinished
       
   161 // --------------------------------------------------------------------------
       
   162 //
       
   163 void CPbk2CommandStore::CommandFinished( const MPbk2Command& aCommand )
       
   164     {
       
   165     // notify all command observers
       
   166     for (TInt i = 0; i < iCommandObservers.Count(); ++i)
       
   167         {
       
   168         TRAP_IGNORE
       
   169             ( iCommandObservers[i]->PostCommandExecutionL( aCommand ) );
       
   170         }
       
   171     
       
   172     // stop user input blocker
       
   173     if( iInputBlocker )
       
   174         {
       
   175         iInputBlocker->Cancel();
       
   176         }
       
   177 
       
   178     // Add command to the list of idle destroyable objects
       
   179     iIdleDestructableCommands.Append( &aCommand );
       
   180     // Cancel any outstanding before starting new one.
       
   181     iIdleDestroyer->Cancel();
       
   182     // Request idle destruction. If the append above failed,
       
   183     // it doesn't matter. The command gets destructed then
       
   184     // at the Phonebook exit, when this objects destructor
       
   185     // deletes also commands in iCommandArray.
       
   186     iIdleDestroyer->Start( TCallBack(
       
   187         ( &CPbk2CommandStore::IdleDestructorCallback ),this ) );
       
   188     }
       
   189 
       
   190 // --------------------------------------------------------------------------
       
   191 // CPbk2CommandStore::AknInputBlockCancel
       
   192 // --------------------------------------------------------------------------
       
   193 //
       
   194 void CPbk2CommandStore::AknInputBlockCancel()
       
   195 	{
       
   196 	iInputBlocker = NULL;
       
   197 	}
       
   198 
       
   199 
       
   200 // --------------------------------------------------------------------------
       
   201 // CPbk2CommandStore::IdleDestructorCallback
       
   202 // --------------------------------------------------------------------------
       
   203 //
       
   204 TInt CPbk2CommandStore::IdleDestructorCallback( TAny* aSelf )
       
   205     {
       
   206     CPbk2CommandStore* self = static_cast<CPbk2CommandStore*>( aSelf );
       
   207     self->IdleDestructor();
       
   208     return EFalse;
       
   209     }
       
   210 
       
   211 // --------------------------------------------------------------------------
       
   212 // CPbk2CommandStore::IdleDestructor
       
   213 // --------------------------------------------------------------------------
       
   214 //
       
   215 void CPbk2CommandStore::IdleDestructor()
       
   216     {
       
   217     // Delete objects in idle destroyable array
       
   218     for (TInt i = 0; i < iIdleDestructableCommands.Count(); ++i)
       
   219         {
       
   220         MPbk2Command* idleDestCmd = iIdleDestructableCommands[i];
       
   221         for (TInt j = iCommandArray.Count()-1; j >= 0; --j)
       
   222             {
       
   223             MPbk2Command* arrayCmd = iCommandArray[j];
       
   224             if (idleDestCmd == arrayCmd)
       
   225                 {
       
   226                 iIdleDestructableCommands.Remove(i);
       
   227                                 
       
   228                 // Try to get MPbk2ResourceRelease interface.
       
   229                 TUid uid;
       
   230                 uid.iUid = MPbk2ResourceReleaseUID;
       
   231                 MPbk2ResourceRelease* release = static_cast<MPbk2ResourceRelease *>(idleDestCmd->CommandExtension(uid));
       
   232                 if (release)
       
   233                   {
       
   234                     // Call ReleaseResource before delete.
       
   235                     release->ReleaseResource();
       
   236                   }
       
   237               
       
   238                 // Remove from command array
       
   239                 iCommandArray.Remove(j);                
       
   240                 
       
   241                 delete idleDestCmd;
       
   242                 idleDestCmd = NULL;
       
   243                 
       
   244                 break;
       
   245                 }
       
   246             }
       
   247         }
       
   248     }
       
   249 
       
   250 
       
   251 // --------------------------------------------------------------------------
       
   252 // CPbk2CommandStore::DestroyAllCommands
       
   253 // --------------------------------------------------------------------------
       
   254 //
       
   255 void CPbk2CommandStore::DestroyAllCommands()
       
   256     {
       
   257     // Delete objects in idle commands array using CommandFinished
       
   258     for (TInt i = 0; i < iCommandArray.Count(); ++i)
       
   259         {
       
   260         MPbk2Command* destCmd = iCommandArray[i];
       
   261         CommandFinished ( *destCmd );
       
   262         }
       
   263     }
       
   264 // End of File