convergedconnectionhandler/cchclientapi/src/cchclientobserver.cpp
branchRCL_3
changeset 21 f742655b05bf
parent 20 65a3ef1d5bd0
child 22 d38647835c2e
equal deleted inserted replaced
20:65a3ef1d5bd0 21:f742655b05bf
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  Cch client api
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "cchimpl.h"
       
    21 #include "cchclientobserver.h"
       
    22 #include "cchlogger.h"
       
    23 #include "cchserviceimpl.h"
       
    24 // EXTERNAL DATA STRUCTURES
       
    25 // None
       
    26 
       
    27 // EXTERNAL FUNCTION PROTOTYPES
       
    28 // None
       
    29 
       
    30 // CONSTANTS
       
    31 // None
       
    32 
       
    33 // MACROS
       
    34 // None
       
    35 
       
    36 // LOCAL CONSTANTS AND MACROS
       
    37 // None
       
    38 
       
    39 // MODULE DATA STRUCTURES
       
    40 // None
       
    41 
       
    42 // LOCAL FUNCTION PROTOTYPES
       
    43 // None
       
    44 
       
    45 // FORWARD DECLARATIONS
       
    46 // None
       
    47 
       
    48 // ============================= LOCAL FUNCTIONS =============================
       
    49 
       
    50 // ============================ MEMBER FUNCTIONS =============================
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // CCchClientObserver::CCchClientObserver
       
    54 // C++ default constructor can NOT contain any code, that might leave.
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 CCchClientObserver::CCchClientObserver( CCchServiceImpl& aCchService )
       
    58 : CActive ( EPriorityStandard )
       
    59 , iCchService( aCchService )
       
    60 , iBackwardSupportObserver( NULL )
       
    61     {
       
    62     CActiveScheduler::Add( this );
       
    63     }
       
    64     
       
    65 CCchClientObserver::~CCchClientObserver()
       
    66     {
       
    67     iObservers.Reset();
       
    68 
       
    69     Cancel();
       
    70     }
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // CCchClientObserver::ConstructL
       
    74 // Symbian 2nd phase constructor can leave.
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 void CCchClientObserver::ConstructL()
       
    78     {
       
    79     CCHLOGSTRING( "CCchClientObserver::ConstructL: IN" );
       
    80 
       
    81     CCHLOGSTRING( "CCchClientObserver::ConstructL: OUT" );
       
    82     }
       
    83 // ---------------------------------------------------------------------------
       
    84 // CCchClientObserver::NewL
       
    85 // Two-phased constructor.
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 CCchClientObserver* CCchClientObserver::NewL( CCchServiceImpl& aCchService )
       
    89     {
       
    90     CCHLOGSTRING( "CCchClientObserver::NewL: IN" );
       
    91     CCchClientObserver* self = 
       
    92         CCchClientObserver::NewLC( aCchService );
       
    93     CleanupStack::Pop( self );
       
    94     CCHLOGSTRING( "CCchClientObserver::NewL: OUT" );
       
    95     return self;
       
    96     }
       
    97 
       
    98 // ---------------------------------------------------------------------------
       
    99 // CCchClientObserver::NewLC
       
   100 // Two-phased constructor.
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 CCchClientObserver* CCchClientObserver::NewLC( CCchServiceImpl& aCchService )
       
   104     {
       
   105     CCHLOGSTRING( "CCchClientObserver::NewLC: IN" );
       
   106     CCchClientObserver* self = 
       
   107         new (ELeave) CCchClientObserver( aCchService );
       
   108     CleanupStack::PushL( self );
       
   109     self->ConstructL();
       
   110     CCHLOGSTRING( "CCchClientObserver::NewLC: OUT" );
       
   111     return self;
       
   112     }
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // CCchClientObserver::SetObserver
       
   116 // ---------------------------------------------------------------------------
       
   117 //
       
   118 void CCchClientObserver::SetObserver( MCchServiceStatusObserver& aObserver )
       
   119     {
       
   120     CCHLOGSTRING( "CCchClientObserver::SetObserver: IN - deprecated use AddObserver" );
       
   121     
       
   122     // This is for old API user support remove this when all are using new API
       
   123     if( iBackwardSupportObserver )
       
   124         {
       
   125         // remove existing observer
       
   126         CCHLOGSTRING( "CCchClientObserver::SetObserver: removing old observer" );
       
   127         RemoveObserver();
       
   128         }
       
   129     
       
   130     CCHLOGSTRING( "CCchClientObserver::SetObserver: adding new observer" );
       
   131     // This is for old API user support remove this when all are using new API
       
   132     AddObserver( aObserver );
       
   133     iBackwardSupportObserver = &aObserver;
       
   134 
       
   135     CCHLOGSTRING( "CCchClientObserver::SetObserver: OUT" );
       
   136     }
       
   137     
       
   138 // ---------------------------------------------------------------------------
       
   139 // CCchClientObserver::RemoveObserver
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 void CCchClientObserver::RemoveObserver( )
       
   143     {    
       
   144     CCHLOGSTRING( "CCchClientObserver::RemoveObserver: IN - deprecated use the one with parameter" );
       
   145     
       
   146     if( iBackwardSupportObserver )
       
   147     	{
       
   148     	// remove using observer set in SetObserver. This is for old API user support
       
   149     	// remove this when all are using new API
       
   150     	RemoveObserver( *iBackwardSupportObserver );
       
   151     	iBackwardSupportObserver = NULL;
       
   152     	}
       
   153     
       
   154     CCHLOGSTRING( "CCchClientObserver::RemoveObserver: OUT" );
       
   155     }
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // CCchClientObserver::AddObserver
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 TInt CCchClientObserver::AddObserver( 
       
   162     MCchServiceStatusObserver& aObserver )
       
   163     {
       
   164     CCHLOGSTRING2( "CCchClientObserver::AddObserver: IN count=%d", iObservers.Count() );
       
   165     
       
   166     TInt index = iObservers.Find( &aObserver );
       
   167 
       
   168     // Insert if not already observing
       
   169     if ( index == KErrNotFound )
       
   170         {
       
   171         CCHLOGSTRING( "CCchClientObserver::AddObserver: observer added" );
       
   172         iObservers.Append( &aObserver );
       
   173         }     
       
   174     else
       
   175         {
       
   176         CCHLOGSTRING( "CCchClientObserver::AddObserver: observer KErrAlreadyExists" );
       
   177         return KErrAlreadyExists;   
       
   178         }
       
   179 
       
   180     if ( iObservers.Count() )
       
   181         {
       
   182         if ( !IsActive() )
       
   183             {
       
   184             iCchService.CchImpl()->CchClient().SubscribeToEvents( 
       
   185                     TServiceSelection( iCchService.ServiceId(), ECCHUnknown ), 
       
   186                     iServiceStatus, 
       
   187                     iStatus );
       
   188             
       
   189             CCHLOGSTRING( "CCchClientObserver::AddObserver: setting active" );
       
   190             
       
   191             SetActive();
       
   192             }
       
   193         
       
   194         CCHLOGSTRING( "CCchClientObserver::AddObserver: subscrbing to events"  );
       
   195       
       
   196         }
       
   197     else
       
   198         {
       
   199         CCHLOGSTRING( "CCchClientObserver::AddObserver: already subscribed subscrbing to events - skipping"  );
       
   200         }
       
   201     
       
   202     CCHLOGSTRING2( "CCchClientObserver::AddObserver: OUT count=%d", iObservers.Count() );
       
   203     return KErrNone;
       
   204     }
       
   205 
       
   206 // ---------------------------------------------------------------------------
       
   207 // CCchClientObserver::RemoveObserver
       
   208 // ---------------------------------------------------------------------------
       
   209 //
       
   210 TInt CCchClientObserver::RemoveObserver( 
       
   211     MCchServiceStatusObserver& aObserver )
       
   212     {
       
   213     CCHLOGSTRING( "CCchClientObserver::RemoveObserver: IN" );
       
   214     CCHLOGSTRING2( "CCchClientObserver::RemoveObserver IN: COUNT=%d", iObservers.Count() );
       
   215     
       
   216     TInt index = iObservers.Find( &aObserver );
       
   217     
       
   218     if ( index == KErrNotFound )
       
   219         {
       
   220         CCHLOGSTRING("CCchClientObserver::RemoveObserver - observer NOT found KErnotFound returned" );
       
   221         return KErrNotFound;   
       
   222         }
       
   223     else
       
   224         {
       
   225         CCHLOGSTRING( "CCchClientObserver::RemoveObserver: removing observer"  );
       
   226         
       
   227         // Observer found.
       
   228         iObservers.Remove( index );
       
   229         iObservers.Compress();
       
   230         
       
   231         // Cancel subcribe to events and free up memory.
       
   232         if ( !iObservers.Count() )
       
   233             {
       
   234             CCHLOGSTRING( "CCchClientObserver::RemoveObserver: unsubscrbing from events"  );                  
       
   235             iCchService.CchImpl()->CchClient().SubscribeToEventsCancel( );      
       
   236             iObservers.Reset();
       
   237             }
       
   238         }
       
   239 
       
   240     CCHLOGSTRING2( "CCchClientObserver::RemoveObserver OUT: COUNT=%d", iObservers.Count() );
       
   241     
       
   242     CCHLOGSTRING( "CCchClientObserver::RemoveObserver: OUT" );
       
   243     return KErrNone;
       
   244     }
       
   245 
       
   246 // ---------------------------------------------------------------------------
       
   247 // CCchClientObserver::DoCancel
       
   248 // ---------------------------------------------------------------------------
       
   249 //
       
   250 void CCchClientObserver::DoCancel()
       
   251     {
       
   252     CCHLOGSTRING( "CCchClientObserver::DoCancel: IN" );
       
   253 	
       
   254 	CCHLOGSTRING( "CCchClientObserver::DoCancel: OUT" );
       
   255     }
       
   256 
       
   257 // ---------------------------------------------------------------------------
       
   258 // CCchClientObserver::RunL
       
   259 // ---------------------------------------------------------------------------
       
   260 //
       
   261 void CCchClientObserver::RunL()
       
   262     {
       
   263     TServiceStatus serviceStatus = iServiceStatus();
       
   264     CCHLOGSTRING( "CCchClientObserver::RunL: IN" );
       
   265 
       
   266     //if someone is observing and correct service is changed
       
   267     if ( iObservers.Count() && ( serviceStatus.ServiceId() == iCchService.ServiceId() ) )
       
   268     	{
       
   269     	TCchServiceStatus status;
       
   270     	
       
   271     	status.SetError( serviceStatus.iError );
       
   272     	status.SetState( serviceStatus.iState );
       
   273 
       
   274     	CCHLOGSTRING2( "CCchClientObserver::RunL: count=%d", iObservers.Count() );
       
   275     	
       
   276     	// If observer array can change while iterating it add support for that.
       
   277     	// Then there is a need to iterate starting from beginning until done and save 
       
   278     	// already notified observers etc
       
   279     	TInt count( iObservers.Count() );
       
   280     	
       
   281  	    for( TInt i=0; i < count; i++ )
       
   282             {
       
   283             CCHLOGSTRING2( "CCchClientObserver::RunL: looping observers: %d", i );
       
   284             
       
   285             // If some client removes observer in middle of iteration start from beginning of array again.
       
   286             if ( count > iObservers.Count() )
       
   287                 {
       
   288                 CCHLOGSTRING( "CCchClientObserver::RunL: Start from beginning of array" );
       
   289                 
       
   290                 i = 0;
       
   291                 count = iObservers.Count();
       
   292                 }
       
   293             
       
   294             if( iObservers[ i ] )
       
   295                 {
       
   296                 CCHLOGSTRING2( "CCchClientObserver::RunL: send service status changed to client: %d", i );
       
   297                 iObservers[ i ]->ServiceStatusChanged( iCchService.ServiceId(), 
       
   298                     serviceStatus.Type(), status );
       
   299                 }
       
   300             }
       
   301         }
       
   302     
       
   303     if ( iObservers.Count() )
       
   304         {
       
   305         if ( !IsActive() )
       
   306             {
       
   307             CCHLOGSTRING( "CCchClientObserver::RunL: Subscribing " );
       
   308             iCchService.CchImpl()->CchClient().SubscribeToEvents( 
       
   309                     TServiceSelection( iCchService.ServiceId(), ECCHUnknown ),
       
   310                     iServiceStatus,
       
   311                     iStatus );
       
   312             
       
   313             CCHLOGSTRING( "CCchClientObserver::RunL: Setting active" );
       
   314             SetActive();    
       
   315            }
       
   316         } 
       
   317 
       
   318 
       
   319     CCHLOGSTRING( "CCchClientObserver::RunL: OUT" );
       
   320     }
       
   321 
       
   322 // ---------------------------------------------------------------------------
       
   323 // CCchClientObserver::RunError
       
   324 // ---------------------------------------------------------------------------
       
   325 //
       
   326 TInt CCchClientObserver::RunError( TInt /*aError*/ )
       
   327     {
       
   328     CCHLOGSTRING( "CCchClientObserver::RunError: IN OUT" );
       
   329     return KErrNone;
       
   330     }
       
   331 
       
   332 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   333 
       
   334 //  End of File