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