phonebookui/Phonebook/PbkUI/src/CPbkCommandStore.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2002 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: 
       
    15 *       Component which owns all phonebook command objects.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <CPbkCommandStore.h>
       
    22 #include <MPbkCommand.h>
       
    23 
       
    24 // ================= MEMBER FUNCTIONS =======================
       
    25 
       
    26 inline CPbkCommandStore::CPbkCommandStore()
       
    27     {
       
    28     }
       
    29 
       
    30 inline void CPbkCommandStore::ConstructL()
       
    31     {
       
    32 	iIdleDestroyer = CIdle::NewL(CActive::EPriorityIdle);
       
    33     }
       
    34 
       
    35 EXPORT_C CPbkCommandStore* CPbkCommandStore::NewL()
       
    36     {
       
    37     CPbkCommandStore* self = new(ELeave) CPbkCommandStore;
       
    38     CleanupStack::PushL(self);
       
    39     self->ConstructL();
       
    40     CleanupStack::Pop(self);
       
    41     return self;
       
    42     }
       
    43 
       
    44 CPbkCommandStore::~CPbkCommandStore()
       
    45     {
       
    46     iIdleDestructableCommands.Reset();
       
    47     iIdleDestructableCommands.Close();
       
    48 	iCommandArray.ResetAndDestroy();
       
    49     iCommandArray.Close();
       
    50 	delete iIdleDestroyer;
       
    51     }
       
    52 
       
    53 EXPORT_C void CPbkCommandStore::AddAndExecuteCommandL
       
    54         (MPbkCommand* aCommand)
       
    55     {
       
    56     if (aCommand)
       
    57         {
       
    58         aCommand->AddObserver(*this);
       
    59         aCommand->ExecuteLD();
       
    60         User::LeaveIfError(iCommandArray.Append(aCommand));
       
    61         }
       
    62     }
       
    63 
       
    64 void CPbkCommandStore::CommandFinished
       
    65         (const MPbkCommand& aCommand)
       
    66     {
       
    67     // Add command to the list of idle destroyable objects
       
    68     iIdleDestructableCommands.Append(&aCommand);
       
    69     if (iIdleDestroyer->IsActive())
       
    70         {
       
    71         iIdleDestroyer->Cancel();
       
    72         }
       
    73     // Request idle destruction. If the append above failed,
       
    74     // it doesn't matter. The command gets destructed then
       
    75     // at the Phonebook exit, when this objects destructor
       
    76     // deletes also commands in iCommandArray.
       
    77     iIdleDestroyer->Start(TCallBack((&CPbkCommandStore::IdleDestructorCallback),this));
       
    78     }
       
    79 
       
    80 TInt CPbkCommandStore::IdleDestructorCallback(TAny* aSelf)
       
    81     {
       
    82     CPbkCommandStore* self = static_cast<CPbkCommandStore*>(aSelf);
       
    83     self->IdleDestructor();
       
    84     return EFalse;
       
    85     }
       
    86 
       
    87 void CPbkCommandStore::IdleDestructor()
       
    88     {
       
    89     // Delete objects in idle destroyable array
       
    90     for (TInt i = 0; i < iIdleDestructableCommands.Count(); ++i)
       
    91         {
       
    92         MPbkCommand* idleDestCmd = iIdleDestructableCommands[i];
       
    93         for (TInt j = 0; j < iCommandArray.Count(); ++j)
       
    94             {
       
    95             MPbkCommand* arrayCmd = iCommandArray[j];
       
    96             if (idleDestCmd == arrayCmd)
       
    97                 { 
       
    98                 // Remove from command array
       
    99                 iCommandArray.Remove(j);
       
   100                 iIdleDestructableCommands.Remove(i);
       
   101                 delete idleDestCmd;
       
   102                 idleDestCmd = NULL;
       
   103                 }
       
   104             }
       
   105         }
       
   106     }
       
   107 
       
   108 // End of File