harvester/common/src/propertywatcher.cpp
changeset 0 c53acadfccc6
child 8 6752808b2036
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 CPropertyWatcher class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // SYSTEM INCLUDE
       
    20 #include <e32base.h>
       
    21 #include <e32std.h>  // For DLL.
       
    22 
       
    23 // USER INCLUDE
       
    24 #include "propertywatcher.h"
       
    25 #include "harvesterlog.h" // For debugging.
       
    26 #include "listener.h" 
       
    27 
       
    28 // ============================ MEMBER FUNCTIONS ===============================
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // CPropertyWatcher::GetInstanceL
       
    32 // ---------------------------------------------------------------------------
       
    33 //
       
    34 EXPORT_C CPropertyWatcher* CPropertyWatcher::GetInstanceL() 
       
    35     {
       
    36     WRITELOG( "CPropertyWatcher::GetInstanceL() - begin" ); 
       
    37   
       
    38     CPropertyWatcher* me = NULL;
       
    39     me = static_cast<CPropertyWatcher*>( Dll::Tls() );
       
    40     if ( !me )
       
    41         {
       
    42         CPropertyWatcher* self = new( ELeave ) CPropertyWatcher();
       
    43         User::LeaveIfError( Dll::SetTls( self ) );
       
    44     
       
    45         WRITELOG( "CPropertyWatcher::GetInstanceL() - end returning self" ); 
       
    46         return self;	
       
    47         }
       
    48     else
       
    49         {
       
    50         WRITELOG( "CPropertyWatcher::GetInstanceL() - end returning me" ); 
       
    51         return me;	
       
    52         }			    
       
    53     }
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // CPropertyWatcher::Delete
       
    57 // ---------------------------------------------------------------------------
       
    58 //	
       
    59 EXPORT_C void CPropertyWatcher::Delete()
       
    60     {
       
    61     WRITELOG( "CPropertyWatcher::Delete() - begin" ); 
       
    62     
       
    63     CPropertyWatcher *me = NULL;
       
    64     me = static_cast<CPropertyWatcher*>( Dll::Tls() );
       
    65     
       
    66     if ( me )
       
    67         {
       
    68         delete me; 
       
    69         Dll::SetTls( NULL ); // Free TLS.
       
    70         }
       
    71     
       
    72     WRITELOG( "CPropertyWatcher::Delete() - end" ); 
       
    73     }
       
    74 
       
    75 // ---------------------------------------------------------------------------
       
    76 // CPropertyWatcher::ListenKeyChangesL
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 EXPORT_C void CPropertyWatcher::ListenKeyChangesL( 
       
    80 		const TUid aPropertyCategory,
       
    81 		const TUint aKey,
       
    82 		const MKeyObserver* aValueObserver )
       
    83     {
       
    84 	WRITELOG( "CPropertyWatcher::ListenKeyChanges() - begin" ); 
       
    85 
       
    86 	const TInt listenerIndex = FindListener ( aPropertyCategory, aKey ); // Finds listener.
       
    87 	if ( listenerIndex >=  0 )
       
    88 		{
       
    89 		iListenersArray[listenerIndex]->RegisterNewClientForKeyValueL( aValueObserver );
       
    90 		return;
       
    91 		}
       
    92     
       
    93 	// Create new listener.
       
    94 	CListener* listener = CListener::NewLC( 
       
    95 			 aPropertyCategory,
       
    96 			 aKey
       
    97 			 ); 
       
    98 	 
       
    99 	listener->RegisterNewClientForKeyValueL( aValueObserver );
       
   100 	
       
   101 	// Add creted listener to array.
       
   102 	User::LeaveIfError( iListenersArray.Append( listener ) ); // Transfer ownership.
       
   103 	
       
   104 	CleanupStack::Pop( listener );
       
   105 
       
   106 	WRITELOG( "CPropertyWatcher::ListenKeyChanges() - end" ); 
       
   107     }
       
   108 
       
   109 // ---------------------------------------------------------------------------
       
   110 // CPropertyWatcher::ListenKeyAndStatusChanges
       
   111 // ---------------------------------------------------------------------------
       
   112 //
       
   113 EXPORT_C void CPropertyWatcher::ListenKeyAndStatusChangesL( 
       
   114 		const TUid aPropertyCategory,
       
   115 		const TUint aKey,
       
   116 		const MKeyAndStatusObserver* aStatusObserver )
       
   117     {
       
   118 	WRITELOG( "CPropertyWatcher::ListenKeyAndStatusChanges - begin" ); 
       
   119 
       
   120 	const TInt listenerIndex = FindListener ( aPropertyCategory, aKey ); // Finds listener.
       
   121 	if ( listenerIndex >= 0 )
       
   122 		{
       
   123 		iListenersArray[listenerIndex]->RegisterNewClientForKeyAndStatusL( aStatusObserver );
       
   124 		return;
       
   125 		}
       
   126     
       
   127 	// Create new listener.
       
   128 	CListener* listener = CListener::NewLC( 
       
   129 			 aPropertyCategory,
       
   130 			 aKey
       
   131 			 ); 
       
   132 	 
       
   133 	listener->RegisterNewClientForKeyAndStatusL( aStatusObserver );
       
   134 	
       
   135 	// Add created listener to array.
       
   136 	User::LeaveIfError( iListenersArray.Append( listener ) ); // Transfer ownership.
       
   137 	
       
   138 	CleanupStack::Pop( listener );
       
   139 	
       
   140 	WRITELOG( "CPropertyWatcher::ListenKeyChanges() - end" ); 
       
   141     }
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // CPropertyWatcher::StopListeningKeyChanges
       
   145 // ---------------------------------------------------------------------------
       
   146 //	
       
   147 EXPORT_C void CPropertyWatcher::StopListeningKeyChanges( 
       
   148 		const TUid aPropertyCategory,
       
   149 		const TUint aKey,
       
   150 		const MKeyObserver* aKeyObserver )
       
   151     {
       
   152     WRITELOG( "CPropertyWatcher::StopListeningKeyChangesL() - begin" ); 
       
   153 	
       
   154 	const TInt listenerIndex = FindListener ( aPropertyCategory, aKey ); // Finds listener.
       
   155 	if ( listenerIndex >= 0 )
       
   156 		{
       
   157 		iListenersArray[listenerIndex]->UnregisterKeyClient( aKeyObserver );
       
   158 		}
       
   159 	
       
   160 	WRITELOG( "CPropertyWatcher::StopListeningKeyChangesL() - end" ); 
       
   161     }
       
   162 
       
   163 // ---------------------------------------------------------------------------
       
   164 // CPropertyWatcher::StopListeningKeyAndStatusChanges
       
   165 // ---------------------------------------------------------------------------
       
   166 //	
       
   167 EXPORT_C void CPropertyWatcher::StopListeningKeyAndStatusChanges( 
       
   168 		const TUid aPropertyCategory,
       
   169 		const TUint aKey,
       
   170 		MKeyAndStatusObserver* aStatusObserver )
       
   171     {
       
   172     WRITELOG( "CPropertyWatcher::StopListeningKeyAndStatusChanges() - begin" ); 
       
   173 	
       
   174 	const TInt listenerIndex = FindListener ( aPropertyCategory, aKey ); // Finds listener.
       
   175 	if ( listenerIndex >= 0 )
       
   176 		{
       
   177 		iListenersArray[listenerIndex]->UnregisterKeyAndStatusClient( aStatusObserver );
       
   178 		}
       
   179 	
       
   180 	WRITELOG( "CPropertyWatcher::StopListeningKeyAndStatusChanges() - end" ); 
       
   181     }
       
   182 
       
   183 // ---------------------------------------------------------------------------
       
   184 // CPropertyWatcher::~CPropertyWatcher
       
   185 // ---------------------------------------------------------------------------
       
   186 //	
       
   187 CPropertyWatcher::~CPropertyWatcher()
       
   188 	{
       
   189 	WRITELOG( "CPropertyWatcher::~CPropertyWatcher() - begin" ); 
       
   190 	
       
   191 	iListenersArray.ResetAndDestroy(); // Clean array and objects.
       
   192 	
       
   193 	WRITELOG( "CPropertyWatcher::~CPropertyWatcher() - end" ); 
       
   194 	}
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // CPropertyWatcher::FindListener
       
   198 // ---------------------------------------------------------------------------
       
   199 //	
       
   200 TInt CPropertyWatcher::FindListener( const TUid aPropertyCategory, const TUint aKey )	const
       
   201     {
       
   202     WRITELOG( "CPropertyWatcher::FindListener() - begin" ); 
       
   203 	
       
   204     TInt listenerIndex ( KErrNotFound );
       
   205     
       
   206     for ( TInt i = iListenersArray.Count(); --i >= 0; )
       
   207     	{
       
   208     	CListener& listenerItem = *iListenersArray[i];
       
   209     	
       
   210     	if ( aKey == listenerItem.ObservedKey() &&
       
   211 			aPropertyCategory == listenerItem.ObservedCategory() ) 
       
   212 			{
       
   213 			listenerIndex =  i; 
       
   214 			}
       
   215 		}
       
   216 	
       
   217 	WRITELOG( "CPropertyWatcher::FindListener() - end" ); 
       
   218 	return listenerIndex;
       
   219     }
       
   220 
       
   221 // End of file.