srsf/nssvasapi/nssvasdb/nssvasdbeventnotifier/src/nssvascvasdbeventmonitor.cpp
branchRCL_3
changeset 23 e36f3802f733
parent 0 bf1d17376201
equal deleted inserted replaced
22:cad71a31b7fc 23:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2005-2006 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:  CVASDBMonitor class is an active object that is a wrapper around 
       
    15 *               the RDBNotifier class. Events from RDBNotifier are handled by 
       
    16 *               encapsulating them in a CNssVASDBEvent object and passing it to all 
       
    17 *               its observers. In addition to the events provided by RDBNotifier, 
       
    18 *               CVASDBMonitor also provides additional events when a entry is added,
       
    19 *               changed or deleted from the VAS DB. CVASDBMonitor owns the list of 
       
    20 *               observers of the VAS DB.
       
    21 *
       
    22 */
       
    23 
       
    24 
       
    25 // INCLUDE FILES
       
    26 #include "nssvascresourcehandler.h"
       
    27 #include "nssvascvasdbeventmonitor.h"
       
    28 
       
    29 
       
    30 // ================= MEMBER FUNCTIONS =======================
       
    31 
       
    32 // ---------------------------------------------------------
       
    33 // CNssVASDBEventMonitor::ConstructL
       
    34 // C++ constructor can NOT contain any code that
       
    35 // might leave.
       
    36 // ---------------------------------------------------------
       
    37 //
       
    38 CNssVASDBEventMonitor::CNssVASDBEventMonitor() : CActive( CActive::EPriorityStandard) 
       
    39     {
       
    40     }
       
    41 
       
    42 
       
    43 // ---------------------------------------------------------
       
    44 // CNssVASDBEventMonitor::ConstructL
       
    45 // EPOC constructor can leave.
       
    46 // ---------------------------------------------------------
       
    47 //
       
    48 void CNssVASDBEventMonitor::ConstructL()
       
    49     {
       
    50     iObservers = new ( ELeave ) CArrayPtrFlat<MNssVASDatabaseObserver>( 1 );
       
    51 
       
    52     TFileName serverSideFilename;
       
    53    
       
    54     User::LeaveIfError( iDbSession.Connect() );
       
    55 
       
    56     // Open the database.
       
    57     User::LeaveIfError( iDatabase.Open( iDbSession, KVasDatabaseName, KVasDatabaseFormatString ) );
       
    58 
       
    59     User::LeaveIfError( iNotifier.Open( iDatabase ) );
       
    60     StartMonitoring();
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------
       
    64 // CNssVASDBEventMonitor::NewL
       
    65 // Two-phased constructor.
       
    66 // ---------------------------------------------------------
       
    67 //
       
    68 CNssVASDBEventMonitor* CNssVASDBEventMonitor::NewL()
       
    69     {
       
    70     CNssVASDBEventMonitor* self = new (ELeave) CNssVASDBEventMonitor;
       
    71       
       
    72     CleanupStack::PushL( self );
       
    73 	CActiveScheduler::Add( self );
       
    74     self->ConstructL();
       
    75     CleanupStack::Pop( self );
       
    76     return self;
       
    77     }
       
    78 
       
    79 
       
    80 // ---------------------------------------------------------
       
    81 // CNssVASDBEventMonitor::~CNssVASDBEventMonitor
       
    82 // Destructor
       
    83 // ---------------------------------------------------------
       
    84 //
       
    85 CNssVASDBEventMonitor::~CNssVASDBEventMonitor()
       
    86     {
       
    87     Cancel();
       
    88     iNotifier.Close();
       
    89     iDatabase.Close();
       
    90     iDbSession.Close();
       
    91     if ( iObservers )
       
    92         {
       
    93         iObservers->Reset();
       
    94         delete iObservers;
       
    95         }
       
    96     }
       
    97 
       
    98 
       
    99 // ---------------------------------------------------------
       
   100 // CNssVASDBEventMonitor::StartMonitoring
       
   101 // Request function to start monitoring
       
   102 // (other items were commented in a header).
       
   103 // ---------------------------------------------------------
       
   104 //
       
   105 void CNssVASDBEventMonitor::StartMonitoring()
       
   106     {
       
   107     if ( IsActive() )
       
   108         {
       
   109 		return;
       
   110         }
       
   111 	iStatus=KRequestPending;
       
   112 	SetActive();
       
   113 	iNotifier.NotifyChange( iStatus );
       
   114     }
       
   115 
       
   116 
       
   117 // ---------------------------------------------------------
       
   118 // CNssVASDBEventMonitor::AddObserverL
       
   119 // add observer to its list
       
   120 // (other items were commented in a header).
       
   121 // ---------------------------------------------------------
       
   122 //
       
   123 void CNssVASDBEventMonitor::AddObserverL(MNssVASDatabaseObserver* aObserver)
       
   124     {
       
   125 	iObservers->AppendL( aObserver );	
       
   126     }
       
   127 
       
   128 
       
   129 // ---------------------------------------------------------
       
   130 // CNssVASDBEventMonitor::RemoveObserver
       
   131 // remove observer from its list
       
   132 // (other items were commented in a header).
       
   133 // ---------------------------------------------------------
       
   134 //
       
   135 TInt CNssVASDBEventMonitor::RemoveObserver(MNssVASDatabaseObserver* aObserver)
       
   136     {
       
   137 	TBool ret = EFalse;
       
   138 	if ( iObservers )
       
   139 	    {
       
   140 		for ( TInt i = 0;i < iObservers->Count(); i++ )
       
   141 		    {
       
   142 			if ( ( *iObservers )[i] == aObserver )
       
   143 			    {
       
   144 				iObservers->Delete( i );
       
   145 				ret = ETrue;
       
   146 				break;
       
   147 			    }
       
   148 		    }
       
   149 	    }
       
   150 	if ( ret )
       
   151     	{
       
   152 		iObservers->Compress();
       
   153 	    }
       
   154 	return ( ret? KErrNone : KErrGeneral );			
       
   155     }
       
   156 
       
   157 
       
   158 // ---------------------------------------------------------
       
   159 // CNssVASDBEventMonitor::RunL
       
   160 // called when standard symbian DBMS events occur
       
   161 // (other items were commented in a header).
       
   162 // ---------------------------------------------------------
       
   163 //
       
   164 void CNssVASDBEventMonitor::RunL()
       
   165     {
       
   166 	RDbNotifier::TEvent event = STATIC_CAST( RDbNotifier::TEvent,iStatus.Int() );
       
   167 	HandleDatabaseEventL( event );
       
   168 	StartMonitoring();
       
   169     }
       
   170 
       
   171 
       
   172 // ---------------------------------------------------------
       
   173 // CNssVASDBEventMonitor::DoCancel
       
   174 // cancel outstanding requests to RDBNotifier
       
   175 // (other items were commented in a header).
       
   176 // ---------------------------------------------------------
       
   177 //
       
   178 void CNssVASDBEventMonitor::DoCancel()
       
   179     {
       
   180 	iNotifier.Cancel();
       
   181     }
       
   182 
       
   183 
       
   184 // ---------------------------------------------------------
       
   185 // CNssVASDBEventMonitor::HandleDatabaseEventL
       
   186 // Standard Symbian DBMS events handling
       
   187 // (other items were commented in a header).
       
   188 // ---------------------------------------------------------
       
   189 //
       
   190 void CNssVASDBEventMonitor::HandleDatabaseEventL( RDbNotifier::TEvent aEvent )
       
   191     {
       
   192 	 if(!iObservers->Count())
       
   193 	    {
       
   194 		return;
       
   195 	    }
       
   196 
       
   197 	 CNssVASDBEvent::TVASDBEventType type = CNssVASDBEvent::EVASDatabaseEventNull;
       
   198 	 switch( aEvent )
       
   199     	 {
       
   200     	 case RDbNotifier::EClose: 
       
   201     	    type = CNssVASDBEvent::EVASDatabaseEventDatabaseClose;
       
   202     	    break; 
       
   203     	 case RDbNotifier::EUnlock:
       
   204 	    	 type = CNssVASDBEvent::EVASDatabaseEventDatabaseUnlock;
       
   205 		     break;
       
   206 		 case RDbNotifier::ECommit:
       
   207 	        type = CNssVASDBEvent::EVASDatabaseEventDatabaseCommit;
       
   208 		    break;
       
   209 		 case RDbNotifier::ERollback:
       
   210 		    type = CNssVASDBEvent::EVASDatabaseEventDatabaseRollback;
       
   211 		    break;
       
   212 		 case RDbNotifier::ERecover:
       
   213 		    type = CNssVASDBEvent::EVASDatabaseEventDatabaseRecover;
       
   214 		    break;
       
   215 		 default:
       
   216 		    break;
       
   217     	 }
       
   218 	 // for standard symbian DBMS events, the parameter aTag
       
   219 	 // does not have any significance, so create a dummy one
       
   220 	 MNssTag *dummyTag = NULL;
       
   221 	 CNssVASDBEvent *vasDBEvent = CNssVASDBEvent::NewL( type, dummyTag );
       
   222 
       
   223 	 for(TInt i = 0; i < iObservers->Count(); i++)
       
   224 	     {
       
   225 		 ( *iObservers )[i]->HandleVASDBEvent( vasDBEvent );
       
   226 	     }
       
   227     }
       
   228 
       
   229 
       
   230 // ---------------------------------------------------------
       
   231 // CNssVASDBEventMonitor::HandleDatabaseEventL()
       
   232 // handling of addional events ( tag added, tag deleted, tag changed)
       
   233 // wk 15 release has limited functionality where these events can be 
       
   234 // reported only when the events occur on the same thread as that 
       
   235 // of the observer (client monitoring the event).
       
   236 // (other items were commented in a header).
       
   237 // ---------------------------------------------------------
       
   238 //
       
   239 void CNssVASDBEventMonitor::HandleDatabaseEventL( 
       
   240                     CNssVASDBEvent::TVASDBEventType type, CNssTag *aTag )
       
   241 {
       
   242 	 if ( !iObservers->Count() )
       
   243 	    {
       
   244     	return;
       
   245 	    }
       
   246 
       
   247 	 for ( TInt i = 0; i < iObservers->Count(); i++ )
       
   248 	 {
       
   249 		 CNssVASDBEvent *vasDBEvent = CNssVASDBEvent::NewL( type, aTag );
       
   250 		 ( *iObservers )[i]->HandleVASDBEvent( vasDBEvent );
       
   251 	 }
       
   252 }
       
   253 
       
   254 //  End of File