idlehomescreen/sapiwrapper/cpswrapper/src/cpsobserver.cpp
changeset 0 f72a12da539e
child 1 5315654608de
equal deleted inserted replaced
-1:000000000000 0:f72a12da539e
       
     1 /*
       
     2 * Copyright (c) 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:   Implements an observer to CPS
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <LiwCommon.h>
       
    21 #include <LiwServiceHandler.h>
       
    22 #include <LiwVariant.h>
       
    23 #include <LiwGenericParam.h>
       
    24 
       
    25 #include "cpsobserver.h"
       
    26 #include "cpswrapper.h"
       
    27 #include "cpsconst.h"
       
    28 
       
    29 namespace cpswrapper
       
    30     {
       
    31 	
       
    32 	// =============================================================================
       
    33 	// --------------------------------------------------------------------------
       
    34 	// Cleanup utility for RPointerArray
       
    35 	// --------------------------------------------------------------------------
       
    36 	//
       
    37 	static void DoCleanup( TAny* aPtr )
       
    38 		{
       
    39 		__ASSERT_DEBUG( aPtr, User::Invariant() );
       
    40 		static_cast< RCriteriaArray* >( aPtr )->ResetAndDestroy();
       
    41 		}
       
    42 	
       
    43 	// ============================ MEMBER FUNCTIONS ==============================
       
    44 	
       
    45 	// ----------------------------------------------------------------------------
       
    46 	// Constructor
       
    47 	// ----------------------------------------------------------------------------
       
    48 	//
       
    49 	CCpsObserver::CCpsObserver( CCpsWrapper* aCpsWrapper )
       
    50 		: iCpsWrapper( aCpsWrapper )
       
    51 		{
       
    52 		}
       
    53 	
       
    54 	// ----------------------------------------------------------------------------
       
    55 	// Two-phased constructor.
       
    56 	// ----------------------------------------------------------------------------
       
    57 	//
       
    58 	CCpsObserver* CCpsObserver::NewLC( CCpsWrapper* aCpsWrapper )
       
    59 		{
       
    60 		CCpsObserver* self( 
       
    61 			new( ELeave ) CCpsObserver( aCpsWrapper ) );
       
    62 			
       
    63 		CleanupStack::PushL( self );
       
    64 		self->ConstructL();
       
    65 		return self;
       
    66 		}
       
    67 	
       
    68 	// ---------------------------------------------------------------------------
       
    69 	// Destructor
       
    70 	// ---------------------------------------------------------------------------
       
    71 	//
       
    72 	CCpsObserver::~CCpsObserver()
       
    73 		{
       
    74 		ReleaseL();
       
    75 		ReleaseLiw();
       
    76 		}
       
    77 	
       
    78 	// ----------------------------------------------------------------------------
       
    79 	// Symbian 2nd phase constructor can leave.
       
    80 	// ----------------------------------------------------------------------------
       
    81 	//
       
    82 	void CCpsObserver::ConstructL()
       
    83 		{
       
    84 		InitLiwL();
       
    85 		}
       
    86 	
       
    87 	// ---------------------------------------------------------------------------
       
    88 	// CCpsObserver::HandleNotifyL
       
    89 	//
       
    90 	// Handles Published content notification
       
    91 	// ---------------------------------------------------------------------------
       
    92 	//
       
    93 	TInt CCpsObserver::HandleNotifyL( 
       
    94 		TInt /*aErrorCode */, 
       
    95 		TInt /*aEventId*/,
       
    96 		CLiwGenericParamList& aEventParamList, 
       
    97 		const CLiwGenericParamList& /*aInParamList*/ )
       
    98 		{
       
    99 	    TInt pos( 0 );
       
   100 
       
   101 		aEventParamList.FindFirst( pos, KChangeInfo );
       
   102 
       
   103 		if ( pos != KErrNotFound )
       
   104 			{
       
   105 			// Get list of maps
       
   106 			TLiwVariant variant( (aEventParamList)[pos].Value() );
       
   107 			variant.PushL();
       
   108 
       
   109 			const CLiwList* changeMapsList( variant.AsList() );
       
   110 
       
   111 			// Iter through list content
       
   112 			for ( TInt i = 0; i < changeMapsList->Count(); ++i )
       
   113 				{
       
   114 				TLiwVariant changeMap;
       
   115 				changeMap.PushL();
       
   116 				if ( changeMapsList->AtL( i, changeMap ) )
       
   117 					{
       
   118 					const CLiwMap* map = changeMap.AsMap();
       
   119 
       
   120 					if ( map->FindL( KContentType, changeMap ) )
       
   121 						{
       
   122 						HBufC* publisher = NULL;
       
   123 						HBufC* contentType = NULL;
       
   124 						
       
   125 						contentType = changeMap.AsDes().AllocLC();
       
   126 						if ( KHSTemplate() == contentType &&
       
   127 							 map->FindL( KPublisher, changeMap ) )
       
   128 							{
       
   129                             publisher = changeMap.AsDes().AllocLC();
       
   130 
       
   131                             if ( map->FindL( KOperation, changeMap ) )
       
   132                                 {
       
   133                                 HBufC* operation = changeMap.AsDes().AllocLC();
       
   134                                 
       
   135                                 // unregitered publisher's widget
       
   136                                 // from home screen if exist or add
       
   137                                 // new widget to widgetmanager
       
   138                                 iCpsWrapper->HandleChangedPublisherL( *publisher,
       
   139                                         *operation );
       
   140                                 CleanupStack::PopAndDestroy( operation );
       
   141                                 }
       
   142 							CleanupStack::PopAndDestroy( publisher );
       
   143 							}
       
   144 						else if( KWRTTemplate() == contentType  &&
       
   145 								  map->FindL( KContentId, changeMap ) )
       
   146 							 {
       
   147                              publisher = changeMap.AsDes().AllocLC();
       
   148                              if ( map->FindL( KOperation, changeMap ) )
       
   149                                  {
       
   150                                  HBufC* operation = changeMap.AsDes().AllocLC();
       
   151                                  
       
   152                                  // unregitered publisher's widget
       
   153                                  // from home screen if exist or add
       
   154                                  // new widget to widgetmanager
       
   155                                  iCpsWrapper->HandleChangedPublisherL( *publisher,
       
   156                                          *operation );
       
   157                                  CleanupStack::PopAndDestroy( operation );
       
   158                                  }
       
   159                              CleanupStack::PopAndDestroy( publisher );
       
   160 							 }
       
   161 						CleanupStack::PopAndDestroy( contentType );
       
   162 						}
       
   163 					}
       
   164 				CleanupStack::PopAndDestroy( &changeMap );
       
   165 				}
       
   166 
       
   167 			CleanupStack::PopAndDestroy( &variant );
       
   168 			}
       
   169 		return KErrNone;
       
   170 		}
       
   171 	
       
   172 	// ---------------------------------------------------------------------------
       
   173 	// CCpsObserver::RegisterL
       
   174 	//
       
   175 	// Register for notifications
       
   176 	// ---------------------------------------------------------------------------
       
   177 	//
       
   178 	void CCpsObserver::RegisterL( CLiwDefaultMap* aFilter, const TDesC& aRegistry )
       
   179 		{
       
   180 		CLiwGenericParamList* inParamList( CLiwGenericParamList::NewLC() );
       
   181 		CLiwGenericParamList* outParamList( CLiwGenericParamList::NewLC() );
       
   182 		
       
   183 		// Fill in input list for RequestNotification command
       
   184 		inParamList->AppendL(TLiwGenericParam(KType,TLiwVariant( aRegistry )));
       
   185 		inParamList->AppendL(TLiwGenericParam(KFilter ,TLiwVariant( aFilter )));
       
   186 			
       
   187 		iInterface->ExecuteCmdL( KRequestNotification,  *inParamList,
       
   188 											 *outParamList, 0, this );
       
   189 		
       
   190 		CleanupStack::PopAndDestroy( outParamList );
       
   191 		CleanupStack::PopAndDestroy( inParamList ); 
       
   192 		}
       
   193 	
       
   194 	// ---------------------------------------------------------------------------
       
   195 	// CCpsObserver::ReleaseL
       
   196 	//
       
   197 	// Sing off to notification
       
   198 	// ---------------------------------------------------------------------------
       
   199 	//
       
   200 	void CCpsObserver::ReleaseL()
       
   201 		{
       
   202 		if( iInterface )
       
   203 			{
       
   204 			CLiwGenericParamList* inParamList = CLiwGenericParamList::NewL();
       
   205 			CleanupStack::PushL( inParamList );
       
   206 			CLiwGenericParamList* outParamList = CLiwGenericParamList::NewL();
       
   207 			CleanupStack::PushL( outParamList );
       
   208 			
       
   209 			iInterface->ExecuteCmdL( KRequestNotification, *inParamList, *outParamList,
       
   210 													 KLiwOptCancel, this );
       
   211 	
       
   212 			CleanupStack::PopAndDestroy( outParamList );
       
   213 			CleanupStack::PopAndDestroy( inParamList );
       
   214 			}
       
   215 		}
       
   216 	
       
   217 	
       
   218 	// ------------------------------------------------------------------------
       
   219 	// CCpsObserver::InitLiwL
       
   220 	//
       
   221 	// Init LIW interface.
       
   222 	// ------------------------------------------------------------------------
       
   223 	void CCpsObserver::InitLiwL()
       
   224 		{
       
   225 		if ( !iLiwHandler )
       
   226 			{
       
   227 			TInt error( KErrNone );
       
   228 			CLiwGenericParamList* in( NULL );
       
   229 			CLiwGenericParamList* out( NULL );
       
   230 			CLiwCriteriaItem* criterion( NULL );
       
   231 	
       
   232 			RCriteriaArray array( 1 );
       
   233 			TCleanupItem cleanup( DoCleanup, &array );
       
   234 			CleanupStack::PushL( cleanup );
       
   235 	
       
   236 			// Do the basic initialization.
       
   237 			iLiwHandler = CLiwServiceHandler::NewL();
       
   238 	
       
   239 			// Get iCPSIface.
       
   240 			in = CLiwGenericParamList::NewLC();
       
   241 			out = CLiwGenericParamList::NewLC();
       
   242 	
       
   243 			criterion = CLiwCriteriaItem::NewL( 
       
   244 				KLiwCmdAsStr,
       
   245 				KCPSConfigurationIf, 
       
   246 				KCPS );
       
   247 				
       
   248 			error = array.Append( criterion );
       
   249 			if ( error )
       
   250 				{
       
   251 				delete criterion; 
       
   252 				criterion = NULL;
       
   253 				User::Leave( error );
       
   254 				}
       
   255 	
       
   256 			criterion->SetServiceClass( TUid::Uid( KLiwClassBase ) );
       
   257 				
       
   258 			iLiwHandler->AttachL( array );
       
   259 			iLiwHandler->ExecuteServiceCmdL( *criterion, *in, *out ); 
       
   260 	
       
   261 			out->FindFirst( error, KCPSConfigurationIf );
       
   262 			User::LeaveIfError( error );
       
   263 	
       
   264 			iInterface = (*out)[ error ].Value().AsInterface();
       
   265 	
       
   266 			CleanupStack::PopAndDestroy( 3 );
       
   267 			}
       
   268 		}
       
   269 	
       
   270 	// ---------------------------------------------------------------------------
       
   271 	// CCpsObserver::ReleaseLiw
       
   272 	// ---------------------------------------------------------------------------
       
   273 	//
       
   274 	void CCpsObserver::ReleaseLiw()
       
   275 		{    
       
   276 		if ( iInterface )
       
   277 			{
       
   278 			iInterface->Close();
       
   279 			iInterface = NULL;
       
   280 			}
       
   281 			
       
   282 		delete iLiwHandler;
       
   283 		iLiwHandler = NULL;
       
   284 		}
       
   285 }
       
   286  //  End of File
       
   287