apengine/apeng/src/ActiveApDb.cpp
changeset 0 5a93021fdf25
child 66 ed07dcc72692
equal deleted inserted replaced
-1:000000000000 0:5a93021fdf25
       
     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:  Implementation of CActiveApDb.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include <ActiveApDb.h>
       
    22 #include "ActiveApDbNotifier.h"
       
    23 #include <ApEngineConsts.h>
       
    24 #include "ApEngineCommons.h"
       
    25 #include "ApEngineLogger.h"
       
    26 
       
    27 const TInt KObserverArrayGranularity = 1;
       
    28 
       
    29 // ================= MEMBER FUNCTIONS =======================
       
    30 
       
    31 // ---------------------------------------------------------
       
    32 // CActiveApDb::NewL
       
    33 // ---------------------------------------------------------
       
    34 //
       
    35 EXPORT_C CActiveApDb* CActiveApDb::NewL( TCommDbDatabaseType /*aType*/ )
       
    36     {
       
    37     CLOG( ( EActiveDb, 0, _L( "-> CActiveApDb::NewL" ) ) );
       
    38 
       
    39     CActiveApDb* db = new ( ELeave ) CActiveApDb;
       
    40     CleanupStack::PushL( db );
       
    41     db->ConstructL();
       
    42     CleanupStack::Pop( db ); // db
       
    43 
       
    44     CLOG( ( EActiveDb, 1, _L( "<- CActiveApDb::NewL" ) ) );
       
    45     return db;
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------
       
    49 // CActiveApDb::~CActiveApDb
       
    50 // ---------------------------------------------------------
       
    51 //
       
    52 EXPORT_C CActiveApDb::~CActiveApDb()
       
    53     {
       
    54     CLOG( ( EActiveDb, 0, _L( "-> CActiveApDb::~CActiveApDb" ) ) );
       
    55 
       
    56     delete iDbNotifier;
       
    57     delete iObservers;
       
    58     delete iDb;
       
    59 
       
    60     CLOG( ( EActiveDb, 1, _L( "<- CActiveApDb::~CActiveApDb" ) ) );
       
    61     }
       
    62 
       
    63 
       
    64 // ---------------------------------------------------------
       
    65 // CActiveApDb::CActiveApDb
       
    66 // ---------------------------------------------------------
       
    67 //
       
    68 CActiveApDb::CActiveApDb()
       
    69     {
       
    70     }
       
    71 
       
    72 
       
    73 // ---------------------------------------------------------
       
    74 // CActiveApDb::ConstructL
       
    75 // ---------------------------------------------------------
       
    76 //
       
    77 void CActiveApDb::ConstructL()
       
    78     {
       
    79     iDb = CCommsDatabase::NewL( );
       
    80     iObservers = new ( ELeave ) CArrayPtrFlat<MActiveApDbObserver>
       
    81                         ( KObserverArrayGranularity );
       
    82     iDbNotifier = new ( ELeave ) CActiveApDbNotifier( *this );
       
    83 
       
    84     iState = EReady;
       
    85     }
       
    86 
       
    87 
       
    88 // ---------------------------------------------------------
       
    89 // CActiveApDb::AddObserverL
       
    90 // ---------------------------------------------------------
       
    91 //
       
    92 EXPORT_C void CActiveApDb::AddObserverL( MActiveApDbObserver* anObserver )
       
    93     {
       
    94     CLOG( ( EActiveDb, 0, _L( "-> CActiveApDb::AddObserverL" ) ) );
       
    95 
       
    96     if ( !anObserver )
       
    97         {
       
    98         User::Leave( KErrNullPointerPassed );
       
    99         }
       
   100     iObservers->AppendL( anObserver );
       
   101     iDbNotifier->Start();
       
   102 
       
   103     CLOG( ( EActiveDb, 1, _L( "<- CActiveApDb::AddObserverL" ) ) );
       
   104     }
       
   105 
       
   106 
       
   107 // ---------------------------------------------------------
       
   108 // CActiveApDb::RemoveObserver
       
   109 // ---------------------------------------------------------
       
   110 //
       
   111 EXPORT_C void CActiveApDb::RemoveObserver( MActiveApDbObserver* anObserver )
       
   112     {
       
   113     CLOG( ( EActiveDb, 0, _L( "-> CActiveApDb::RemoveObserver" ) ) );
       
   114 
       
   115     __ASSERT_DEBUG( anObserver, ApCommons::Panic( ENullPointer ) );
       
   116     TInt count = iObservers->Count();
       
   117     for ( TInt i = 0; i < count; i++ )
       
   118         {
       
   119         if ( iObservers->At( i ) == anObserver )
       
   120             {
       
   121             iObservers->Delete( i );
       
   122             // leave cycle for faster processing, can not return because
       
   123             // we may have to stop() if it was the last one...
       
   124             break;
       
   125             }
       
   126         }
       
   127     if ( !iObservers->Count() )
       
   128         { // no more observers waiting so stop notification...
       
   129         iDbNotifier->Stop();
       
   130         }
       
   131     // for some reason, observer was not found->
       
   132     // Someone has already removed it
       
   133     // simply ignore it
       
   134     CLOG( ( EActiveDb, 1, _L( "<- CActiveApDb::RemoveObserver" ) ) );
       
   135 
       
   136     }
       
   137 
       
   138 
       
   139 // ---------------------------------------------------------
       
   140 // CActiveApDb::Database
       
   141 // ---------------------------------------------------------
       
   142 //
       
   143 EXPORT_C CCommsDatabase*  CActiveApDb::Database()
       
   144     {
       
   145     CLOG( ( EActiveDb, 0, _L( "<-> CActiveApDb::Database" ) ) );
       
   146 
       
   147     return iDb;
       
   148     }
       
   149 
       
   150 // ---------------------------------------------------------
       
   151 // CActiveApDb::HandleDbEventL
       
   152 // ---------------------------------------------------------
       
   153 //
       
   154 void CActiveApDb::HandleDbEventL( TInt anEvent )
       
   155     {
       
   156     CLOG( ( EActiveDb, 0, _L( "-> CActiveApDb::HandleDbEventL" ) ) );
       
   157 
       
   158     // possible events: EClose,EUnlock,ECommit,ERollback,ERecover
       
   159     switch ( anEvent )
       
   160         {
       
   161         case RDbNotifier::EUnlock:
       
   162             {
       
   163             CLOG( ( EActiveDb, 2, _L( "Notifier Unlock" ) ) );
       
   164             NotifyObserversL( MActiveApDbObserver::EDbAvailable );
       
   165             break;
       
   166             }
       
   167         case RDbNotifier::ECommit:
       
   168             {
       
   169             CLOG( ( EActiveDb, 2, _L( "Notifier Commit" ) ) );
       
   170             NotifyObserversL( MActiveApDbObserver::EDbChanged );
       
   171             break;
       
   172             }
       
   173         case RDbNotifier::EClose:
       
   174             {
       
   175             CLOG( ( EActiveDb, 2, _L( "Notifier Close/Rollbac/Recover" ) ) );
       
   176             NotifyObserversL( MActiveApDbObserver::EDbClosing );
       
   177             // Use base class' method to keep the notifier alive.
       
   178             // We need to know when all clients has released the db.
       
   179             break;
       
   180             }
       
   181         case RDbNotifier::ERollback:            
       
   182             {
       
   183             CLOG( ( EActiveDb, 2, _L( "Notifier Close/Rollbac/Recover" ) ) );
       
   184             NotifyObserversL( MActiveApDbObserver::EDbAvailable );
       
   185             break;
       
   186             }
       
   187         case RDbNotifier::ERecover:
       
   188             {
       
   189             CLOG( ( EActiveDb, 2, _L( "Notifier Close/Rollbac/Recover" ) ) );
       
   190             NotifyObserversL( MActiveApDbObserver::EDbClosing );
       
   191             // Use base class' method to keep the notifier alive.
       
   192             // We need to know when all clients has released the db.
       
   193             break;
       
   194             }
       
   195         default:
       
   196             {
       
   197             // Don't know what's happened, but instead of
       
   198             // Panicking, it's better to 'simulate' a change.
       
   199             // 'Defensive' programming.
       
   200             CLOG( ( EActiveDb, 2, _L( "Notifier UNKNOWN" ) ) );
       
   201             NotifyObserversL( MActiveApDbObserver::EDbChanged );
       
   202             break;
       
   203             }
       
   204         }
       
   205     CLOG( ( EActiveDb, 1, _L( "<- CActiveApDb::HandleDbEventL" ) ) );
       
   206 
       
   207     }
       
   208 
       
   209 
       
   210 
       
   211 
       
   212 // ---------------------------------------------------------
       
   213 // CActiveApDb::NotifyObserversL
       
   214 // ---------------------------------------------------------
       
   215 //
       
   216 void CActiveApDb::NotifyObserversL( MActiveApDbObserver::TEvent anEvent )
       
   217     {
       
   218 
       
   219     TInt i;
       
   220     TInt count = iObservers->Count();
       
   221     // one hazard is that if one client leaves,
       
   222     // the rest of the clients will not be notified this time...
       
   223     for ( i = 0; i < count; i++ )
       
   224         {
       
   225         TRAP_IGNORE( iObservers->At( i )->HandleApDbEventL( anEvent ) );
       
   226         }
       
   227     }
       
   228 
       
   229 // End of File