homescreensrv_plat/sapi_contentpublishing/src/cpclientactivenotifier.cpp
changeset 93 82b66994846c
parent 92 782e3408c2ab
child 94 dbb8300717f7
equal deleted inserted replaced
92:782e3408c2ab 93:82b66994846c
     1 /*
       
     2 * Copyright (c) 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:  
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <s32mem.h>
       
    20 #include <liwcommon.h>
       
    21 #include "cpclientactivenotifier.h"
       
    22 #include "ccontentmap.h"
       
    23 #include "cpdebug.h"
       
    24 #include "cpglobals.h"
       
    25 #include "cpserverdef.h"
       
    26 #include "cdatasourceinterface.h"
       
    27 
       
    28 using namespace LIW;
       
    29 
       
    30 // ======== MEMBER FUNCTIONS ========
       
    31 
       
    32 // ----------------------------------------------------------------------------
       
    33 //
       
    34 // ----------------------------------------------------------------------------
       
    35 //
       
    36 CCPActiveNotifier* CCPActiveNotifier::NewL( RCPServerClient& aServerClient )
       
    37     {
       
    38     CCPActiveNotifier* self = new( ELeave ) 
       
    39             CCPActiveNotifier ( aServerClient );
       
    40     CleanupStack::PushL( self );
       
    41     self->ConstructL( );
       
    42     CleanupStack::Pop( self );
       
    43     return self;
       
    44     }
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // destructor
       
    48 // ---------------------------------------------------------------------------
       
    49 CCPActiveNotifier::~CCPActiveNotifier()
       
    50     {
       
    51     CP_DEBUG( _L8("CCPActiveNotifier::CCPActiveNotifier()" ) );
       
    52     Cancel( );
       
    53     iObservers.Close( );
       
    54     delete iObserverBuf;
       
    55     delete iSizeDes;
       
    56     }
       
    57 
       
    58 // ----------------------------------------------------------------------------
       
    59 //
       
    60 // ----------------------------------------------------------------------------
       
    61 //
       
    62 void CCPActiveNotifier::RegisterL( MLiwNotifyCallback* aObserver, 
       
    63 		TInt32 aTransactionId, CCPLiwMap* aMap )
       
    64     {
       
    65     CP_DEBUG( _L8("CCPActiveNotifier::RegisterL()" ) );
       
    66     TIpcArgs args;
       
    67     
       
    68    	if(iObservers.Find( aTransactionId ))
       
    69    		{
       
    70    		User::Leave( KErrInUse );
       
    71    		}
       
    72     
       
    73    	iObservers.InsertL( aTransactionId, aObserver );
       
    74 	delete iObserverBuf;
       
    75 	iObserverBuf = NULL;
       
    76  	iObserverBuf = aMap->PackForServerLC( );
       
    77    	CleanupStack::Pop( iObserverBuf );
       
    78    	args.Set( KDescriptorPosition, &*iObserverBuf );
       
    79 	args.Set( KTransactionPosition, aTransactionId );
       
    80     
       
    81     if ( !IsActive( ) )
       
    82         {
       
    83         iSizeDes = new (ELeave) TPckgBuf<TInt>( );
       
    84         args.Set( KReturnPosition, iSizeDes );
       
    85         iServerClient.RegisterObserverL( iStatus, args );
       
    86         SetActive( );
       
    87         }
       
    88     else
       
    89         {
       
    90         iServerClient.AddObserver( args );
       
    91         }
       
    92     }
       
    93 
       
    94 
       
    95 // ----------------------------------------------------------------------------
       
    96 //
       
    97 // ----------------------------------------------------------------------------
       
    98 //
       
    99 void CCPActiveNotifier::UnregisterL()
       
   100     {
       
   101     CP_DEBUG( _L8("CCPActiveNotifier::UnregisterL()" ) );
       
   102     if ( !IsActive( ) )
       
   103         {
       
   104         User::Leave( KErrNotFound );
       
   105         }
       
   106     else
       
   107         {
       
   108         Cancel( );
       
   109         delete iSizeDes;
       
   110         iSizeDes = NULL;
       
   111         }
       
   112     }
       
   113 
       
   114 // ----------------------------------------------------------------------------
       
   115 //
       
   116 // ----------------------------------------------------------------------------
       
   117 //
       
   118 TBool CCPActiveNotifier::UnregisterL( TInt32 aTransactionId )
       
   119     {
       
   120     TBool last(EFalse);
       
   121     CP_DEBUG( _L8("CCPActiveNotifier::UnregisterL()" ) );
       
   122 
       
   123     MLiwNotifyCallback** observer = iObservers.Find(aTransactionId);
       
   124     if (observer)
       
   125         {
       
   126         CLiwGenericParamList* changeInfoList = 
       
   127                  CLiwGenericParamList::NewL( );
       
   128         CleanupStack::PushL( changeInfoList );
       
   129         CLiwGenericParamList* inParamList = CLiwGenericParamList::NewL( );
       
   130         CleanupStack::PushL( inParamList );
       
   131         
       
   132         changeInfoList->AppendL(TLiwGenericParam(EGenericParamError,
       
   133                 TLiwVariant(CDataSourceInterface::ErrCodeConversion(KErrNone))));
       
   134         changeInfoList->AppendL(TLiwGenericParam(KErrorCode, TLiwVariant(
       
   135                 CDataSourceInterface::ErrCodeConversion(KErrNone))));
       
   136         
       
   137 		(*observer)->HandleNotifyL( aTransactionId, KLiwEventCanceled, *changeInfoList,
       
   138             *inParamList );     
       
   139         
       
   140         CleanupStack::PopAndDestroy( inParamList );
       
   141         CleanupStack::PopAndDestroy( changeInfoList );
       
   142         }
       
   143     else
       
   144         {
       
   145         User::Leave(KErrNotFound);
       
   146         }
       
   147     
       
   148     TIpcArgs args;
       
   149 	args.Set( KTransactionPosition, aTransactionId );
       
   150     iServerClient.RemoveObserver( args );
       
   151 
       
   152     iObservers.Remove( aTransactionId );
       
   153         
       
   154 	if( !iObservers.Count() )
       
   155 		{
       
   156 		UnregisterL();
       
   157 		last = ETrue;
       
   158 		}
       
   159 	return last;
       
   160     }
       
   161 
       
   162 // ----------------------------------------------------------------------------
       
   163 //
       
   164 // ----------------------------------------------------------------------------
       
   165 //
       
   166 void CCPActiveNotifier::UnregisterAllL(  )
       
   167     {
       
   168     CP_DEBUG( _L8("CCPActiveNotifier::UnregisterAllL()" ) );
       
   169     if (!iObservers.Count())
       
   170     	{
       
   171     	User::Leave( KErrNotFound );
       
   172     	}
       
   173     THashMapIter<TInt32, MLiwNotifyCallback*> iter( iObservers );
       
   174     const TInt32* transaction = iter.NextKey();
       
   175     while( transaction )
       
   176         {
       
   177         UnregisterL( *transaction );
       
   178         transaction = iter.NextKey( );
       
   179         }    
       
   180     }
       
   181 
       
   182 // ----------------------------------------------------------------------------
       
   183 //
       
   184 // ----------------------------------------------------------------------------
       
   185 //
       
   186 void CCPActiveNotifier::RunL()
       
   187     {
       
   188     CP_DEBUG( _L8("CCPActiveNotifier::RunL()" ) );
       
   189     CLiwGenericParamList* eventParamList = CLiwGenericParamList::NewL();
       
   190     CleanupStack::PushL(eventParamList);
       
   191 
       
   192     TInt error = iStatus.Int();
       
   193     if (KErrNone == error)
       
   194         {
       
   195         RBuf8 outbuf;
       
   196         outbuf.CleanupClosePushL();
       
   197         outbuf.CreateL((*iSizeDes)());
       
   198         error = iServerClient.GetChangeInfoData(outbuf);
       
   199         RDesReadStream stream(outbuf);
       
   200         CleanupClosePushL(stream);
       
   201         eventParamList->InternalizeL(stream);
       
   202         CleanupStack::PopAndDestroy(&stream);
       
   203         delete iSizeDes;
       
   204         iSizeDes = NULL;
       
   205         CleanupStack::PopAndDestroy(&outbuf);
       
   206         }
       
   207     RegisterAgainL();
       
   208 
       
   209     NotifyObserversL(error, eventParamList);
       
   210     CleanupStack::PopAndDestroy(eventParamList);
       
   211     }
       
   212 
       
   213 // ----------------------------------------------------------------------------
       
   214 //
       
   215 // ----------------------------------------------------------------------------
       
   216 //
       
   217 void CCPActiveNotifier::NotifyObserversL( TInt aErrorCode, 
       
   218 		CLiwGenericParamList* aEventParamList  )
       
   219 	{
       
   220     TInt event( KLiwEventInProgress );
       
   221     if ( aErrorCode == KErrCancel )
       
   222         {
       
   223         event = KLiwEventCanceled;
       
   224         }
       
   225     else if ( aErrorCode != KErrNone )
       
   226         {
       
   227         event = KLiwEventStopped;
       
   228         }	
       
   229  
       
   230     for ( TInt i = 0; i<aEventParamList->Count(); i++ )
       
   231     	{
       
   232     	TLiwGenericParam param;
       
   233     	param.PushL();
       
   234     	TLiwVariant mapsV;
       
   235     	mapsV.PushL();
       
   236     	aEventParamList->AtL( i, param );
       
   237     	mapsV = param.Value();
       
   238     	CLiwDefaultList* listOfMatchingMaps = CLiwDefaultList::NewLC( );
       
   239     	if (mapsV.Get( *listOfMatchingMaps ))
       
   240     		{
       
   241     		TLiwVariant transV;
       
   242     		transV.PushL();
       
   243     		TInt transIndex (listOfMatchingMaps->Count()-1);
       
   244     		listOfMatchingMaps->AtL(transIndex ,transV);
       
   245     		TInt32 transactionId ( KErrNotFound );
       
   246     		transV.Get( transactionId );
       
   247     		MLiwNotifyCallback** observer = iObservers.Find( transactionId );
       
   248     		
       
   249     		if ( observer )
       
   250     			{
       
   251     			listOfMatchingMaps->Remove( transIndex );
       
   252     			CLiwGenericParamList* changeInfoList = 
       
   253 											CLiwGenericParamList::NewL( );
       
   254     			CleanupStack::PushL( changeInfoList );
       
   255     		    CLiwGenericParamList* inParamList = CLiwGenericParamList::NewL( );
       
   256     		    CleanupStack::PushL( inParamList );
       
   257     			
       
   258     			changeInfoList->AppendL( TLiwGenericParam( KChangeInfo,
       
   259     							TLiwVariant( listOfMatchingMaps ) ) );
       
   260     			
       
   261     			changeInfoList->AppendL( TLiwGenericParam( EGenericParamError,
       
   262     			    TLiwVariant( CDataSourceInterface::ErrCodeConversion( aErrorCode ) ) ) );
       
   263     			changeInfoList->AppendL( TLiwGenericParam( KErrorCode,
       
   264     			    TLiwVariant( CDataSourceInterface::ErrCodeConversion( aErrorCode ) ) ) );
       
   265     			(*observer)->HandleNotifyL( transactionId, event, *changeInfoList,
       
   266     			        *inParamList );
       
   267     			
       
   268     		    CleanupStack::PopAndDestroy( inParamList );
       
   269     		    CleanupStack::PopAndDestroy( changeInfoList );
       
   270     			}//if
       
   271 
       
   272     		CleanupStack::PopAndDestroy( &transV );
       
   273     		}//if
       
   274     	CleanupStack::PopAndDestroy( listOfMatchingMaps );
       
   275     	CleanupStack::PopAndDestroy( &mapsV );
       
   276     	CleanupStack::PopAndDestroy( &param );
       
   277     	}//for
       
   278 	}
       
   279 
       
   280 
       
   281 // ----------------------------------------------------------------------------
       
   282 //
       
   283 // ----------------------------------------------------------------------------
       
   284 //
       
   285 void CCPActiveNotifier::DoCancel()
       
   286     {
       
   287     CP_DEBUG( _L8("CCPActiveNotifier::DoCancel()" ) );
       
   288     iServerClient.UnregisterObserver( );
       
   289     }
       
   290 
       
   291 // ----------------------------------------------------------------------------
       
   292 //
       
   293 // ----------------------------------------------------------------------------
       
   294 //
       
   295 TInt CCPActiveNotifier::RunError( TInt /*aError*/)
       
   296     {
       
   297     TRAP_IGNORE( RegisterAgainL( ) );
       
   298     return KErrNone;
       
   299     }
       
   300 // ----------------------------------------------------------------------------
       
   301 //
       
   302 // ----------------------------------------------------------------------------
       
   303 //
       
   304 CCPActiveNotifier::CCPActiveNotifier( RCPServerClient& aServerClient ) :
       
   305     CActive(EPriorityStandard)
       
   306     {
       
   307     iServerClient = aServerClient;
       
   308     CActiveScheduler::Add( this );
       
   309     }
       
   310 
       
   311 // ----------------------------------------------------------------------------
       
   312 //
       
   313 // ----------------------------------------------------------------------------
       
   314 //
       
   315 void CCPActiveNotifier::ConstructL()
       
   316     {
       
   317 
       
   318     }
       
   319 
       
   320 // ----------------------------------------------------------------------------
       
   321 //
       
   322 // ----------------------------------------------------------------------------
       
   323 //
       
   324 void CCPActiveNotifier::RegisterAgainL()
       
   325     {
       
   326     CP_DEBUG( _L8("CCPActiveNotifier::RegisterAgainL()" ) );
       
   327 
       
   328     if (!IsActive())
       
   329         {
       
   330         TIpcArgs args;
       
   331         args.Set(KTransactionPosition, KErrAlreadyExists);
       
   332         if (iSizeDes)
       
   333             {
       
   334             delete iSizeDes;
       
   335             iSizeDes = NULL;
       
   336             }
       
   337         iSizeDes = new (ELeave) TPckgBuf<TInt> ();
       
   338         args.Set(KReturnPosition, iSizeDes);
       
   339         iServerClient.RegisterObserverL(iStatus, args);
       
   340         SetActive();
       
   341         }
       
   342     }