harvester/common/src/listener.cpp
changeset 0 c53acadfccc6
child 15 3cebc1a84278
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2008-2009 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:  This implements CListener class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "listener.h"
       
    21 #include "mpropertyobserver.h"
       
    22 #include "harvesterlog.h" // For debugging.
       
    23 
       
    24 // ============================ MEMBER FUNCTIONS ===============================
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // CListener::NewL
       
    28 // Two-phased constructor.
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 CListener* CListener::NewL( const TUid aPropertyCategory,
       
    32 		const TUint aKey )
       
    33     {
       
    34     WRITELOG( "CListener::NewL() - begin" );
       
    35     
       
    36     CListener* self = CListener::NewLC( aPropertyCategory, aKey );
       
    37     CleanupStack::Pop( self );
       
    38     
       
    39     WRITELOG( "CListener::NewL() - end" );
       
    40     return self;
       
    41     }
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CListener::NewLC
       
    45 // Two-phased constructor.
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 CListener* CListener::NewLC( const TUid aPropertyCategory,
       
    49 		const TUint aKey )
       
    50     {
       
    51     WRITELOG( "CHarvesterAO::NewLC() - begin" );
       
    52     
       
    53     CListener* self = new ( ELeave ) CListener( aPropertyCategory, aKey );
       
    54     CleanupStack::PushL( self );
       
    55     self->ConstructL();
       
    56     
       
    57     WRITELOG( "CListener::NewLC - end" );
       
    58     return self;
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CListener::~CListener
       
    63 // C++ default destructor
       
    64 // -----------------------------------------------------------------------------
       
    65 //    
       
    66 CListener::~CListener()
       
    67     {
       
    68     WRITELOG( "CListener::~CListener - begin" );
       
    69     
       
    70     Cancel(); // Cancels any existing active object orders for iStatus.
       
    71     
       
    72     iProperty.Close();
       
    73     
       
    74     iArrayForKeyValue.Reset();
       
    75     iArrayForKeyValue.Close();
       
    76     
       
    77     iArrayForKeyAndStatus.Reset();
       
    78     iArrayForKeyAndStatus.Close();
       
    79 
       
    80     WRITELOG( "CListener::~CListener - end" );   
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CListener::ObservedKey
       
    85 // Returns observed key.
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 TUint CListener::ObservedKey() const
       
    89     {
       
    90     WRITELOG( "CListener::ObservedKey" );
       
    91     
       
    92     return iKey; 
       
    93     }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // CListener::ObservedCategory
       
    97 // Returns observed key's Category.
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 TUid CListener::ObservedCategory() const
       
   101     {
       
   102     WRITELOG( "CListener::ObservedCategory" );
       
   103     
       
   104     return iPropertyCategory; 
       
   105     }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // CListener::RegisterNewClientForKeyValueL
       
   109 // 
       
   110 // -----------------------------------------------------------------------------
       
   111 //
       
   112 void CListener::RegisterNewClientForKeyValueL( const MKeyObserver* aKeyObserver )
       
   113     {
       
   114     WRITELOG( "CListener::RegisterNewClientForKeyValue" );
       
   115     
       
   116     User::LeaveIfError( iArrayForKeyValue.Append( aKeyObserver ) );
       
   117     }
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 // CListener::RegisterNewClientForKeyAndStatusL
       
   121 // 
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 void CListener::RegisterNewClientForKeyAndStatusL( 
       
   125 		const MKeyAndStatusObserver* aKeyAndStatusObserver )
       
   126     {
       
   127     WRITELOG( "CListener::RegisterNewClientForKeyAndStatus" );
       
   128     
       
   129     User::LeaveIfError( iArrayForKeyAndStatus.Append( aKeyAndStatusObserver ) );
       
   130     }
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // CListener::UnregisterKeyClient
       
   134 //
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 void CListener::UnregisterKeyClient( const MKeyObserver* aKeyObserver )
       
   138     {
       
   139     WRITELOG( "CListener::RemoveClient - begin" ); 
       
   140     
       
   141     const TInt listIndex = iArrayForKeyValue.Find( aKeyObserver );
       
   142     if( listIndex != KErrNotFound )
       
   143     	{
       
   144     	iArrayForKeyValue.Remove( listIndex );
       
   145     	}
       
   146  
       
   147     WRITELOG( "CListener::RemoveClient - end" );   
       
   148     }
       
   149 
       
   150 // -----------------------------------------------------------------------------
       
   151 // CListener::UnregisterKeyAndStatusClient
       
   152 //
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 void CListener::UnregisterKeyAndStatusClient( const MKeyAndStatusObserver* aKeyAndStatusObserver )
       
   156     {
       
   157     WRITELOG( "CListener::UnregisterKeyAndStatusClient - begin" ); 
       
   158     
       
   159     const TInt listIndex = iArrayForKeyAndStatus.Find( aKeyAndStatusObserver );
       
   160     if( listIndex != KErrNotFound )
       
   161     	{
       
   162     	iArrayForKeyAndStatus.Remove( listIndex );
       
   163     	}
       
   164  
       
   165     WRITELOG( "CListener::UnregisterKeyAndStatusClient - end" );   
       
   166     }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CListener::CListener
       
   170 // C++ default constructor can NOT contain any code, that
       
   171 // might leave.
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 CListener::CListener( const TUid aPropertyCategory, const TUint aKey ) :
       
   175 		CActive( CActive::EPriorityUserInput ),
       
   176 		iPropertyCategory( aPropertyCategory ),
       
   177 		iKey( aKey )
       
   178  	{
       
   179  	// No implementation required.
       
   180  	}
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // CListener::ConstructL
       
   184 // Construct member variable that is to access the
       
   185 // kernel. 
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 void CListener::ConstructL()
       
   189     {
       
   190     WRITELOG( "CListener::ConstructL - begin" );
       
   191     
       
   192     CActiveScheduler::Add( this );
       
   193     
       
   194 	// Attach to key.
       
   195  	User::LeaveIfError( iProperty.Attach( 
       
   196  			iPropertyCategory,
       
   197  			iKey,
       
   198  			EOwnerThread ) );
       
   199  	
       
   200  	// Sets object active.
       
   201 	StartListening();
       
   202     
       
   203     WRITELOG( "CListener::ConstructL - end" );
       
   204     }
       
   205 
       
   206 // -----------------------------------------------------------------------------
       
   207 // CListener::RunL
       
   208 // This function will be called upon a change in the watched key.
       
   209 // From class CActive.
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 void CListener::RunL()
       
   213     {
       
   214 #ifdef _DEBUG
       
   215     WRITELOG1( "CListener::RunL - begin, iStatus: %d",  iStatus.Int() ); 
       
   216 #endif
       
   217     
       
   218     if( iStatus.Int() == KErrNone )
       
   219     	{
       
   220     	TInt keyValue( KErrNone );
       
   221     	User::LeaveIfError( iProperty.Get( keyValue ) );
       
   222     	SendNotificationsL( keyValue, iStatus );
       
   223     	}
       
   224     
       
   225     // Start listening again.
       
   226     StartListening();
       
   227     
       
   228     WRITELOG( "CListener::RunL - end" );
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // CListener::RunError
       
   233 // Standard active object error function. From class CActive.
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 #ifdef _DEBUG
       
   237 TInt CListener::RunError( TInt aError )
       
   238 #else
       
   239 TInt CListener::RunError( TInt /*aError*/ )
       
   240 #endif
       
   241     {
       
   242     WRITELOG1( "CListener::RunError, aError: %d",  aError );
       
   243     
       
   244     Cancel();
       
   245     
       
   246     return KErrNone;
       
   247     }
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CListener::DoCancel
       
   251 // Standard active object cancellation function. From class CActive.
       
   252 // -----------------------------------------------------------------------------
       
   253 //
       
   254 void CListener::DoCancel()
       
   255     {
       
   256     WRITELOG( "CListener::DoCancel" );
       
   257     
       
   258     iProperty.Cancel();	
       
   259     }
       
   260 
       
   261 // -----------------------------------------------------------------------------
       
   262 // CListener::StartListeningL
       
   263 // Then start listening of the changes in the key.
       
   264 // -----------------------------------------------------------------------------
       
   265 //
       
   266 void CListener::StartListening()
       
   267     {
       
   268     WRITELOG( "CListener::StartListeningL - begin" ); 
       
   269     
       
   270     if( IsActive() )
       
   271         {
       
   272         return;   
       
   273         }
       
   274 
       
   275 	iProperty.Subscribe( iStatus );
       
   276 	SetActive();
       
   277  
       
   278    WRITELOG( "CListener::StartListeningL - end" );   
       
   279    }
       
   280 
       
   281 // -----------------------------------------------------------------------------
       
   282 // CListener::SendNotificationsL
       
   283 // Send notifications to clients.
       
   284 // -----------------------------------------------------------------------------
       
   285 //
       
   286 void CListener::SendNotificationsL( const TUint aKeyValue,
       
   287 		TRequestStatus& aStatus ) const 
       
   288     {
       
   289     WRITELOG( "CListener::SendNotifications - begin" ); 
       
   290     
       
   291     // For value only.
       
   292     for ( TInt i = iArrayForKeyValue.Count(); --i >= 0; ) 
       
   293     	{
       
   294         iArrayForKeyValue[i]->NotifyKeyL(
       
   295         		aKeyValue,
       
   296         		iPropertyCategory,
       
   297         		iKey );
       
   298         }
       
   299     
       
   300     // For status and value.
       
   301     for ( TInt i = iArrayForKeyAndStatus.Count(); --i >= 0; ) 
       
   302     	{
       
   303         iArrayForKeyAndStatus[i]->NotifyKeyAndStatusL( 
       
   304         		aKeyValue,
       
   305         		aStatus,
       
   306         		iPropertyCategory,
       
   307         		iKey );
       
   308         }
       
   309 
       
   310     WRITELOG( "CListener::SendNotifications - end" );   
       
   311     }
       
   312 
       
   313 // End of file.