homescreensrv_plat/hs_widget_publisher_api/src/hswidgetpublisherimpl.cpp
changeset 0 79c6a41cd166
child 8 d0529222e3f0
equal deleted inserted replaced
-1:000000000000 0:79c6a41cd166
       
     1 /*
       
     2 * Copyright (c) 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: CPSApiDll.cpp - main DLL source
       
    15 *
       
    16 */
       
    17 
       
    18 #include <utf.h>
       
    19 
       
    20 #include "hswidgetpublisherimpl.h"
       
    21 #include "hswidget.h"
       
    22 #include "hswidgetitem.h"
       
    23 #include "hsglobals.h"
       
    24 #include "hsexception.h"
       
    25 #include "hsserviceerrno.h"
       
    26 
       
    27 const TInt KSAPIContentNameMaxLength = 255;
       
    28 const TInt KUnicodeSize = 4;
       
    29 
       
    30 using namespace Hs;
       
    31 // ---------------------------------------------------------------------------
       
    32 // 
       
    33 // ---------------------------------------------------------------------------
       
    34 // 
       
    35 HsWidgetPublisherImpl::HsWidgetPublisherImpl()
       
    36 	{
       
    37 	mServiceInterface = 0;
       
    38 	TInt err;
       
    39 	TRAP( err,
       
    40 		mServiceHandler = CLiwServiceHandler::NewL();
       
    41 	    )
       
    42 	if( err != KErrNone )
       
    43 		{
       
    44 		throw HsException( err );
       
    45 		}
       
    46     RProcess process;
       
    47     iWidgetUid = process.Identity().iUid;
       
    48     process.Close();
       
    49 	}
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 // 
       
    53 // ---------------------------------------------------------------------------
       
    54 // 
       
    55 HsWidgetPublisherImpl::~HsWidgetPublisherImpl()
       
    56 	{
       
    57    	TRAP_IGNORE( UnegisterNotificationL() );
       
    58 
       
    59     if( mServiceInterface )
       
    60     	{
       
    61     	mServiceInterface->Close();
       
    62     	}
       
    63    	if( mServiceHandler )
       
    64     	{
       
    65     	mServiceHandler->Reset();
       
    66     	delete mServiceHandler;
       
    67     	}
       
    68    	
       
    69    	const int count = mWidgets.size();
       
    70     for( int index = 0; index < count; index++ )
       
    71         {
       
    72         HsWidget* const widget = mWidgets.at( index );
       
    73         mWidgets[index] = NULL;
       
    74         delete widget;
       
    75         }
       
    76 	}
       
    77 
       
    78 // ---------------------------------------------------------------------------
       
    79 // 
       
    80 // ---------------------------------------------------------------------------
       
    81 //
       
    82 HsWidget& HsWidgetPublisherImpl::CreateWidget( std::string& aTemplateName, 
       
    83 	std::string& aWidgetName, std::string& aIdentifier,
       
    84 	std::string& aDescription, std::string& aIconLocation)
       
    85 	{
       
    86 	if( CheckIfWidgetExist( aTemplateName, aWidgetName, aIdentifier, EFalse ) )
       
    87 		{
       
    88 		throw HsException( KErrAlreadyExists );
       
    89 		}
       
    90 	HsWidget* widget= new HsWidget( aTemplateName, aWidgetName, 
       
    91 		aIdentifier, aDescription, aIconLocation );
       
    92 	if( !widget)
       
    93 		{
       
    94 		throw HsException( KErrNoMemory );
       
    95 		}
       
    96 	mWidgets.push_back( widget);
       
    97 	TInt err( KErrNone );
       
    98 	TRAP( err, 
       
    99 		PublishWidgetActionsL( *widget );
       
   100         RegisterNotificationL( *widget );
       
   101 		)
       
   102 	if( err != KErrNone )
       
   103 		{
       
   104 		throw HsException( err );
       
   105 		}
       
   106 	
       
   107 	return *widget;
       
   108 	}
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // 
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 TInt HsWidgetPublisherImpl::HandleNotifyL(
       
   115     TInt /*aCmdId*/,
       
   116     TInt /*aEventId*/,
       
   117     CLiwGenericParamList& aEventParamList,
       
   118     const CLiwGenericParamList& /*aInParamList*/)
       
   119 	{
       
   120 	if( !mHsDataObserver )
       
   121 		 {
       
   122 		 return 1;
       
   123 		 }
       
   124 	
       
   125 	TInt pos(0);	
       
   126     aEventParamList.FindFirst(pos, KChangeInfo);
       
   127     if (pos != KErrNotFound)
       
   128         {
       
   129         // Get list of maps
       
   130         TLiwVariant variant = (aEventParamList)[pos].Value();
       
   131         variant.PushL();
       
   132         const CLiwList* changeMapsList = variant.AsList();
       
   133         
       
   134 		TBuf8<KSAPIContentNameMaxLength> operation;
       
   135 		TBuf8<KSAPIContentNameMaxLength> trigger;
       
   136         // Iter through list content
       
   137         for (TInt index = 0; index < changeMapsList->Count(); ++index)
       
   138             {
       
   139              if ( changeMapsList->AtL(index, variant) ) 
       
   140             	 {
       
   141                  const CLiwMap* map  = variant.AsMap();
       
   142                  HsWidget* widget = GetWidgetL( map );
       
   143                  if( !widget )
       
   144                 	 {
       
   145                 	 break;
       
   146                 	 }
       
   147                  if ( map->FindL(KOperation, variant) )
       
   148                      {// Check what triggered a notification
       
   149                      variant.Get(operation);
       
   150                      }
       
   151                  variant.Reset();
       
   152                  if ( operation == KOperationExecute )
       
   153                 	 {
       
   154                 	 if( map->FindL( KActionTrigger, variant) )
       
   155 						 {
       
   156 						 variant.Get(trigger );
       
   157 						 }
       
   158                 	 if( !HandleWidgetActionL( trigger, *widget ) )
       
   159                 		 {
       
   160                 		 break;
       
   161                 		 }
       
   162                 	 HandleWidgetItemActionL( trigger, *widget );
       
   163                 	 }
       
   164                  }
       
   165             }
       
   166         CleanupStack::PopAndDestroy(1);
       
   167         }
       
   168     return 0;
       
   169 	}
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // 
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 int HsWidgetPublisherImpl::HandleWidgetActionL( const TDesC8& aActionDes,
       
   176 	HsWidget& aWidget )
       
   177 	{
       
   178 	int ret( 1 );//not found
       
   179 	IHsDataObserver::EEvent action( IHsDataObserver::EUnknown );
       
   180 	TranslateObserverAction( aActionDes, action );
       
   181 	if( action == IHsDataObserver::EActivate ||
       
   182 		action == IHsDataObserver::EDeactivate ||
       
   183 		action == IHsDataObserver::EResume ||
       
   184 		action == IHsDataObserver::ESuspend )
       
   185 		{
       
   186 		TInt observerError = KErrNone;
       
   187 		try
       
   188 			{
       
   189 			mHsDataObserver->handleEvent(
       
   190 				aWidget.getWidgetName(), action );
       
   191 			}
       
   192 		catch( HsException& exception )
       
   193 			{
       
   194 			observerError = exception.getReason();
       
   195 			}
       
   196 		
       
   197 		if ( action == IHsDataObserver::EDeactivate )
       
   198 		    {
       
   199 		    // remove widget data when widget removed from screen
       
   200 		    CLiwDefaultMap* cpdatamap = CLiwDefaultMap::NewLC();
       
   201 		    InsertWidgetDataIdentifiersL( aWidget, cpdatamap );
       
   202 		    // removal may fail if the client has already removed the data
       
   203 		    TRAP_IGNORE( RemoveFromCpsL( cpdatamap, KCpData ) );
       
   204 		    mWidgetContentIds.erase( aWidget.getIdentifier() );
       
   205 		    CleanupStack::PopAndDestroy( cpdatamap );
       
   206 		    }
       
   207 		
       
   208 		User::LeaveIfError( observerError );
       
   209 		ret = 0;
       
   210 		}
       
   211 	return ret;
       
   212 	}
       
   213 
       
   214 // ---------------------------------------------------------------------------
       
   215 // 
       
   216 // ---------------------------------------------------------------------------
       
   217 //
       
   218 void HsWidgetPublisherImpl::TranslateObserverAction( const TDesC8& aActionDes,
       
   219 	IHsDataObserver::EEvent& aAction )
       
   220 	{
       
   221 	if( !aActionDes.Compare( KActive() ) )
       
   222 		{
       
   223 		aAction = IHsDataObserver::EActivate;
       
   224 		}
       
   225 	if( !aActionDes.Compare( KDeActive() ) )
       
   226 		{
       
   227 		aAction = IHsDataObserver::EDeactivate;
       
   228 		}
       
   229 	if( !aActionDes.Compare( KSuspend() ) )
       
   230 		{
       
   231 		aAction = IHsDataObserver::ESuspend;
       
   232 		}
       
   233 	if( !aActionDes.Compare( KResume() ) )
       
   234 		{
       
   235 		aAction = IHsDataObserver::EResume;
       
   236 		}
       
   237 	}
       
   238 
       
   239 // ---------------------------------------------------------------------------
       
   240 // 
       
   241 // ---------------------------------------------------------------------------
       
   242 //
       
   243 int HsWidgetPublisherImpl::HandleWidgetItemActionL( const TDesC8& aActionDes,
       
   244 		HsWidget& aWidget )
       
   245 	{
       
   246 	int ret( 1 );
       
   247 	TBuf8<KSAPIContentNameMaxLength> itemName( aActionDes );
       
   248 	std::string strItemName( (char*) itemName.PtrZ() );
       
   249 	if( aWidget.checkIfWidgetItemExist(  strItemName ) )
       
   250 		{
       
   251 		std::string widgetName( aWidget.getWidgetName() );
       
   252 		try
       
   253 			{
       
   254 			mHsDataObserver->handleItemEvent( widgetName,
       
   255 				strItemName, 
       
   256 				IHsDataObserver::ESelect );
       
   257 			}
       
   258 		catch( HsException& exception )
       
   259 			{
       
   260 			User::Leave( exception.getReason() );
       
   261 			}
       
   262 		ret = 0;
       
   263 		}
       
   264 	else if (aActionDes.Compare(KSelected) == 0)
       
   265 	    {
       
   266         std::string widgetName( aWidget.getWidgetName() );
       
   267 	    int count = aWidget.itemsCount();
       
   268 	    
       
   269         for (int index = 0; index < count; index++)
       
   270             {
       
   271             HsWidgetItem* const item = aWidget.getWidgetItem( index );
       
   272             try
       
   273                 {
       
   274                 mHsDataObserver->handleItemEvent( widgetName,
       
   275                     item->getItemName(), 
       
   276                     IHsDataObserver::ESelect );
       
   277                 }
       
   278             catch( HsException& exception )
       
   279                 {
       
   280                 User::Leave( exception.getReason() );
       
   281                 }
       
   282             }
       
   283 	        ret = 0;
       
   284 	    }
       
   285 	return ret;
       
   286 	}
       
   287 
       
   288 // ---------------------------------------------------------------------------
       
   289 // 
       
   290 // ---------------------------------------------------------------------------
       
   291 //
       
   292 MLiwInterface* HsWidgetPublisherImpl::GetServiceInterfaceL( )
       
   293 	{
       
   294 	CLiwGenericParamList* inParam = CLiwGenericParamList::NewL();
       
   295 	CleanupStack::PushL( inParam );
       
   296 	CLiwGenericParamList* outParam = CLiwGenericParamList::NewL();
       
   297 	CleanupStack::PushL( outParam );
       
   298 	CLiwCriteriaItem* crit = CLiwCriteriaItem::NewL( KLiwCmdAsStr, 
       
   299 		KCPInterface, KCPService );
       
   300 	crit->SetServiceClass( TUid::Uid(KLiwClassBase) );
       
   301 	
       
   302 	RCriteriaArray a;
       
   303 	a.AppendL(crit);    
       
   304 	mServiceHandler->AttachL(a);
       
   305 	mServiceHandler->ExecuteServiceCmdL( *crit, *inParam, *outParam ); 
       
   306 	delete crit;
       
   307 	a.Reset();
       
   308 	
       
   309 	// find service interface
       
   310 	TInt pos = 0;
       
   311 	MLiwInterface* msgInterface( 0 );
       
   312 	outParam->FindFirst( pos, KCPInterface );
       
   313 	if ( pos != KErrNotFound ) 
       
   314 	    {
       
   315 	    msgInterface = (*outParam)[pos].Value().AsInterface(); 
       
   316 	    }
       
   317 	
       
   318 	outParam->Reset();
       
   319 	inParam->Reset();
       
   320 	CleanupStack::PopAndDestroy( outParam );
       
   321 	CleanupStack::PopAndDestroy( inParam );
       
   322 	
       
   323 	return msgInterface;
       
   324 	}
       
   325 
       
   326 // ---------------------------------------------------------------------------
       
   327 // 
       
   328 // ---------------------------------------------------------------------------
       
   329 //
       
   330 void HsWidgetPublisherImpl::SetDataObserver( IHsDataObserver* aDataObserver )
       
   331 	{
       
   332 	__ASSERT_ALWAYS( aDataObserver, User::Panic( KStartUpFailed, 0) );
       
   333 	mHsDataObserver = aDataObserver;
       
   334 	TInt err( KErrNone );
       
   335 	TRAP( err,
       
   336 		mServiceInterface = GetServiceInterfaceL();
       
   337 		);
       
   338 	if( err != KErrNone )
       
   339 		{
       
   340 		throw HsException( err );
       
   341 		}
       
   342 	}
       
   343 
       
   344 // ---------------------------------------------------------------------------
       
   345 // 
       
   346 // ---------------------------------------------------------------------------
       
   347 //
       
   348 void HsWidgetPublisherImpl::RegisterNotificationL( HsWidget& aWidget )
       
   349 	{
       
   350 	CLiwGenericParamList* inParam = &(mServiceHandler->InParamListL());
       
   351 	CLiwGenericParamList* outParam = &(mServiceHandler->OutParamListL());
       
   352 	CLiwDefaultMap* reqFilter = CLiwDefaultMap::NewLC();
       
   353 	
       
   354 	HBufC* publisherName = StdStringToUnicodeLC( GetPublisherNameL( aWidget ) );
       
   355 	
       
   356 	reqFilter->InsertL( KPublisherId, TLiwVariant( *publisherName ) );
       
   357 	reqFilter->InsertL( KContentType, TLiwVariant( KAll ) );
       
   358 	reqFilter->InsertL( KContentId, TLiwVariant( KAll ));
       
   359 	reqFilter->InsertL( KOperation, TLiwVariant( KOperationExecute ));
       
   360 	
       
   361 	inParam->AppendL(TLiwGenericParam(KType, TLiwVariant( 
       
   362 		KContentAndPublisher_Registry)));
       
   363 	inParam->AppendL(TLiwGenericParam(KFilter, TLiwVariant(reqFilter)));
       
   364 	
       
   365 	mServiceInterface->ExecuteCmdL( KRequestNotification,
       
   366 		*inParam, *outParam, 0, this );
       
   367 	TInt ret = ObtainErrorCode( *outParam );
       
   368 	
       
   369 	outParam->Reset();
       
   370 	inParam->Reset();
       
   371 	CleanupStack::PopAndDestroy( publisherName );
       
   372 	CleanupStack::PopAndDestroy( reqFilter );
       
   373 	User::LeaveIfError( ret );
       
   374 	}
       
   375 
       
   376 // ---------------------------------------------------------------------------
       
   377 // 
       
   378 // ---------------------------------------------------------------------------
       
   379 //
       
   380 void HsWidgetPublisherImpl::UnegisterNotificationL()
       
   381 	{
       
   382 	CLiwGenericParamList* inParam = &(mServiceHandler->InParamListL());
       
   383 	CLiwGenericParamList* outParam = &(mServiceHandler->OutParamListL());
       
   384 
       
   385 	mServiceInterface->ExecuteCmdL( KRequestNotification,
       
   386 		*inParam, *outParam, KLiwOptCancel, this ); 
       
   387 	TInt ret= ObtainErrorCode( *outParam );
       
   388 
       
   389 	outParam->Reset();
       
   390 	inParam->Reset();	
       
   391 	User::LeaveIfError( ret );
       
   392 	}
       
   393 
       
   394 // ---------------------------------------------------------------------------
       
   395 // 
       
   396 // ---------------------------------------------------------------------------
       
   397 //
       
   398 void HsWidgetPublisherImpl::PublishWidget( HsWidget& aWidget )
       
   399 	{
       
   400 	TRAPD( err,
       
   401 		PublishWidgetDataL( aWidget );
       
   402 		);
       
   403 	if( err != KErrNone )
       
   404 		{
       
   405 		throw HsException( err );
       
   406 		}
       
   407 	}
       
   408 
       
   409 // ---------------------------------------------------------------------------
       
   410 // 
       
   411 // ---------------------------------------------------------------------------
       
   412 //
       
   413 void HsWidgetPublisherImpl::PublishWidgetDataL( HsWidget& aWidget )
       
   414 	{
       
   415 	CLiwGenericParamList* inParam = &(mServiceHandler->InParamListL());
       
   416 	CLiwGenericParamList* outParam = &(mServiceHandler->OutParamListL());
       
   417 	
       
   418 	TLiwGenericParam type(KType, TLiwVariant(KCpData));
       
   419 	inParam->AppendL(type);
       
   420 	
       
   421 	CLiwDefaultMap* cpdatamap = CLiwDefaultMap::NewLC();
       
   422 	InsertWidgetDataIdentifiersL( aWidget, cpdatamap );
       
   423    
       
   424 	CLiwDefaultMap* datamap = CLiwDefaultMap::NewLC();
       
   425 	InsertWidgetItemsL( aWidget, datamap );
       
   426 	cpdatamap->InsertL( KDataMap, TLiwVariant( datamap ) );
       
   427 	InsertWidgetACLL( cpdatamap );
       
   428 	
       
   429 	int count = aWidget.itemsCount();
       
   430 	if( count > 0 )
       
   431 		{
       
   432 		CLiwDefaultMap* triggermap = CLiwDefaultMap::NewLC();
       
   433 		InsertItemsTriggersL( aWidget, triggermap );
       
   434 		cpdatamap->InsertL( KActionMap, TLiwVariant( triggermap ) );
       
   435 		CleanupStack::PopAndDestroy( triggermap );
       
   436 		}
       
   437 
       
   438 	TLiwGenericParam item( KItem, TLiwVariant( cpdatamap ));	    
       
   439 	inParam->AppendL( item );
       
   440 	mServiceInterface->ExecuteCmdL( KAdd,
       
   441 			*inParam, *outParam);
       
   442 	TInt ret= ObtainErrorCode( *outParam );
       
   443 
       
   444 	CleanupStack::PopAndDestroy( datamap );
       
   445 	CleanupStack::PopAndDestroy( cpdatamap );
       
   446 
       
   447 	item.Reset();
       
   448 	type.Reset();	
       
   449 	outParam->Reset();
       
   450 	inParam->Reset();
       
   451 	User::LeaveIfError( ret );
       
   452 	}
       
   453 
       
   454 // ---------------------------------------------------------------------------
       
   455 // 
       
   456 // ---------------------------------------------------------------------------
       
   457 //
       
   458 void HsWidgetPublisherImpl::PublishWidgetActionsL( HsWidget& aWidget )
       
   459 	{
       
   460 	CLiwGenericParamList* inParam = &(mServiceHandler->InParamListL());
       
   461 	CLiwGenericParamList* outParam = &(mServiceHandler->OutParamListL());
       
   462 
       
   463 	TLiwGenericParam type(KType, TLiwVariant(KPublisher));
       
   464 	inParam->AppendL(type);
       
   465 	CLiwDefaultMap* cpdatamap = CLiwDefaultMap::NewLC();
       
   466 	InsertWidgetIdentifiersL( aWidget, cpdatamap );
       
   467 	
       
   468 	CLiwDefaultMap* mapAction = CLiwDefaultMap::NewLC();
       
   469 	
       
   470 	CLiwDefaultMap* activateAction = CLiwDefaultMap::NewLC();
       
   471 	activateAction->InsertL( KPluginId, TLiwVariant( KCASpaAppLauncherPlugin ) );
       
   472 	
       
   473 	CLiwDefaultMap* activate = CLiwDefaultMap::NewLC();
       
   474 	activate->InsertL( KType, TLiwVariant( KActionValueLaunchApplication ) );
       
   475 	activate->InsertL( KLaunchMethod, TLiwVariant( KLaunchMethodValueCmdLine ) );
       
   476 	activate->InsertL( KApplicationUid, TLiwVariant( iWidgetUid ) );
       
   477 	activate->InsertL( KApaCommand, TLiwVariant( KApaCommandBackground ) );
       
   478 	
       
   479 	activateAction->InsertL( KData, TLiwVariant( activate ) );
       
   480 	
       
   481 	mapAction->InsertL(KActive, TLiwVariant( activateAction ));
       
   482     mapAction->InsertL(KDeActive, TLiwVariant(KTriggerMap));
       
   483     mapAction->InsertL(KSuspend, TLiwVariant(KTriggerMap));
       
   484     mapAction->InsertL(KResume, TLiwVariant(KTriggerMap));
       
   485     mapAction->InsertL(KSelected, TLiwVariant(activateAction));
       
   486     
       
   487     cpdatamap->InsertL( KActionMap, TLiwVariant( mapAction ) );
       
   488     
       
   489 	CLiwDefaultMap* datamap = CLiwDefaultMap::NewLC();
       
   490 	InsertWidgetInfoL( aWidget, datamap );
       
   491 	cpdatamap->InsertL( KDataMap, TLiwVariant( datamap ) );
       
   492     
       
   493 	InsertWidgetACLL( cpdatamap );
       
   494 	
       
   495 	TLiwGenericParam item( KItem, TLiwVariant( cpdatamap ));	    
       
   496 	inParam->AppendL( item );
       
   497 	
       
   498 	mServiceInterface->ExecuteCmdL( KAdd, *inParam, *outParam);
       
   499 	
       
   500 	TInt ret= ObtainErrorCode( *outParam );
       
   501 
       
   502 	CleanupStack::PopAndDestroy( datamap );
       
   503 	CleanupStack::PopAndDestroy( activate );
       
   504 	CleanupStack::PopAndDestroy( activateAction );
       
   505 	CleanupStack::PopAndDestroy( mapAction );
       
   506 	CleanupStack::PopAndDestroy( cpdatamap );
       
   507 	
       
   508 	item.Reset();
       
   509 	type.Reset();	
       
   510 	outParam->Reset();
       
   511 	inParam->Reset();
       
   512 	User::LeaveIfError( ret );
       
   513 	}
       
   514 
       
   515 // ---------------------------------------------------------------------------
       
   516 // 
       
   517 // ---------------------------------------------------------------------------
       
   518 //
       
   519 void HsWidgetPublisherImpl::RemoveWidget( std::string& aTemplateName, 
       
   520 	std::string& aWidgetName,
       
   521 	std::string& aIdentifier )
       
   522 	{
       
   523 	HsWidget* ret( 0 );
       
   524 	int count = mWidgets.size();
       
   525 	for (int index = 0; index < count; index++)
       
   526 		{
       
   527 		HsWidget* const widget = mWidgets.at( index );
       
   528 		if ( !aTemplateName.compare( widget->getTemplateName() ) )
       
   529 			{
       
   530 			if( !aWidgetName.compare( widget->getWidgetName() ) )
       
   531 				{
       
   532 				if( !aIdentifier.compare( widget->getIdentifier() ) )
       
   533 					{
       
   534 					ret = widget;
       
   535 					TRAPD( removalErr, RemoveWidgetL( *widget ) );
       
   536 					if ( removalErr != KErrNone )
       
   537 					    {
       
   538 					    throw HsException( removalErr );
       
   539 					    }
       
   540 					mWidgetContentIds.erase( widget->getIdentifier() );
       
   541 					mWidgets.erase( mWidgets.begin() + index );
       
   542 					break;
       
   543 					}
       
   544 				}
       
   545 			}
       
   546 		}
       
   547 	if( !ret )
       
   548 		{
       
   549 		throw HsException( KErrNotFound );
       
   550 		}
       
   551 	delete ret;
       
   552 	}
       
   553 
       
   554 
       
   555 // ---------------------------------------------------------------------------
       
   556 // 
       
   557 // ---------------------------------------------------------------------------
       
   558 //
       
   559 void HsWidgetPublisherImpl::RemoveWidgetL( HsWidget& aWidget )
       
   560 	{
       
   561 	// remove widget data
       
   562     CLiwDefaultMap* cpdatamap = CLiwDefaultMap::NewLC();
       
   563     
       
   564     TRAPD( err,
       
   565             InsertWidgetDataIdentifiersL( aWidget, cpdatamap );
       
   566             RemoveFromCpsL( cpdatamap, KCpData ) );
       
   567     if ( err != KErrNotFound )
       
   568         {
       
   569         User::LeaveIfError( err );
       
   570         }
       
   571     CleanupStack::PopAndDestroy( cpdatamap );
       
   572     cpdatamap = NULL;
       
   573 	
       
   574 	// remove widget definition (actions and template info)
       
   575     cpdatamap = CLiwDefaultMap::NewLC();
       
   576     InsertWidgetIdentifiersL( aWidget, cpdatamap );
       
   577     RemoveFromCpsL( cpdatamap, KPublisher );
       
   578     CleanupStack::PopAndDestroy( cpdatamap );
       
   579 	}
       
   580 
       
   581 // ---------------------------------------------------------------------------
       
   582 // 
       
   583 // ---------------------------------------------------------------------------
       
   584 //
       
   585 void HsWidgetPublisherImpl::RemoveFromCpsL( CLiwDefaultMap* aCpDataMap, 
       
   586 	const TDesC& aType )
       
   587 	{
       
   588 	__ASSERT_ALWAYS( aCpDataMap, User::Invariant() );
       
   589 	CLiwGenericParamList* inParam = &(mServiceHandler->InParamListL());
       
   590 	CLiwGenericParamList* outParam = &(mServiceHandler->OutParamListL());
       
   591 	
       
   592 	TLiwGenericParam type( KType, TLiwVariant( aType ) );
       
   593 	inParam->AppendL( type );
       
   594 	
       
   595 	TLiwGenericParam item( KItem, TLiwVariant( aCpDataMap ));	    
       
   596 	inParam->AppendL( item );
       
   597 	
       
   598 	mServiceInterface->ExecuteCmdL( KDelete,
       
   599 		*inParam, *outParam);
       
   600 	TInt ret= ObtainErrorCode( *outParam );
       
   601 	
       
   602 	item.Reset();
       
   603 	type.Reset();	
       
   604 	inParam->Reset();
       
   605 	outParam->Reset();
       
   606 	User::LeaveIfError( ret );
       
   607 	}
       
   608 
       
   609 // ---------------------------------------------------------------------------
       
   610 // 
       
   611 // ---------------------------------------------------------------------------
       
   612 //
       
   613 HsWidget& HsWidgetPublisherImpl::GetWidget( std::string& aTemplateName, 
       
   614 	std::string& aWidgetName,
       
   615 	std::string& aIdentifier )
       
   616 	{
       
   617 	HsWidget* ret( 0 );
       
   618 	int count = mWidgets.size();
       
   619 	for (int index = 0; index < count; index++)
       
   620 		{
       
   621 		HsWidget* const widget = mWidgets.at( index );
       
   622 		if ( !aTemplateName.compare( widget->getTemplateName() ) )
       
   623 			{
       
   624 			if( !aWidgetName.compare( widget->getWidgetName() ) )
       
   625 				{
       
   626 				if( !aIdentifier.compare( widget->getIdentifier() ) )
       
   627 					{
       
   628 					ret = widget;
       
   629 					break;
       
   630 					}
       
   631 				}
       
   632 			}
       
   633 		}
       
   634 	if( !ret )
       
   635 		{
       
   636 		throw HsException( KErrNotFound );
       
   637 		}
       
   638 	return *ret; 
       
   639 	}
       
   640 
       
   641 // ---------------------------------------------------------------------------
       
   642 // 
       
   643 // ---------------------------------------------------------------------------
       
   644 //
       
   645 HsWidget* HsWidgetPublisherImpl::GetWidgetL( const CLiwMap* aMap )
       
   646 	{
       
   647 	// identifier length + colon char + KHsWidgetPublisher string length
       
   648 	const TInt maxPublisherNameLength =
       
   649         KSAPIContentNameMaxLength + 1 + KHsWidgetPublisher().Length();
       
   650 
       
   651 	HsWidget* ret ( 0 );
       
   652 	TLiwVariant variant;
       
   653     variant.PushL();
       
   654     
       
   655     if ( !aMap->FindL( KPublisherId, variant ) )
       
   656         {
       
   657         User::Leave( KErrNotFound );
       
   658         }
       
   659     HBufC* publisherName = HBufC::NewLC( maxPublisherNameLength );
       
   660         { // braces for scope only
       
   661         TPtr16 publisherNameDes = publisherName->Des();
       
   662         variant.Get( publisherNameDes );
       
   663         }
       
   664     variant.Reset();
       
   665     std::string strIdentifier( GetWidgetIdentifierFromPublisherNameL(
       
   666             *publisherName ) );
       
   667     CleanupStack::PopAndDestroy( publisherName );
       
   668     publisherName = 0;
       
   669     
       
   670 	TBuf<KSAPIContentNameMaxLength> contentIdDesc;
       
   671     if ( !aMap->FindL( KContentId, variant ) )
       
   672          {
       
   673          User::Leave( KErrNotFound );
       
   674          }
       
   675     variant.Get( contentIdDesc );
       
   676     CleanupStack::PopAndDestroy( &variant );
       
   677     std::wstring contentId( ToWideStringL( contentIdDesc ) );
       
   678 	
       
   679 	std::string strTemplate;
       
   680 	std::string strWidgetName;
       
   681 		
       
   682 	if( CheckIfWidgetExist(strTemplate, strWidgetName, strIdentifier, ETrue) )
       
   683 		{
       
   684 		ret = &GetWidget( strTemplate, strWidgetName, strIdentifier);
       
   685 		
       
   686 		// verify that content id does not change
       
   687 		__ASSERT_DEBUG( !mWidgetContentIds.count( ret->getIdentifier() ) ||
       
   688 		        mWidgetContentIds[ret->getIdentifier()] == contentId,
       
   689 		        User::Invariant() );
       
   690 
       
   691 		mWidgetContentIds[ret->getIdentifier()] = contentId;
       
   692 		}
       
   693 	
       
   694 	return ret;
       
   695 	}
       
   696 
       
   697 // ---------------------------------------------------------------------------
       
   698 // 
       
   699 // ---------------------------------------------------------------------------
       
   700 //
       
   701 bool HsWidgetPublisherImpl::CheckIfWidgetExist( std::string& aTemplateName, 
       
   702 	std::string& aWidgetName,
       
   703 	std::string& aIdentifier,
       
   704 	TBool aAdjustToIdentifier )
       
   705 	{
       
   706 	bool widgetExist( false );
       
   707 	int count = mWidgets.size();
       
   708 	for (int index = 0; index < count; index++)
       
   709 		{
       
   710 		HsWidget* const widget = mWidgets.at( index );
       
   711 		if( !aIdentifier.compare( widget->getIdentifier() ) )
       
   712 			{
       
   713 			if( !aTemplateName.compare( widget->getTemplateName() ) )
       
   714 				{
       
   715 				if( !aWidgetName.compare( widget->getWidgetName() ) )
       
   716 					{
       
   717 					widgetExist = true;
       
   718 					break;
       
   719 					}
       
   720 				}
       
   721 			if( aAdjustToIdentifier )
       
   722 				{
       
   723 				aTemplateName = widget->getTemplateName();
       
   724 				aWidgetName = widget->getWidgetName();
       
   725 				widgetExist = true;
       
   726 				break;
       
   727 				}
       
   728 			}
       
   729 		}
       
   730 	return widgetExist;	
       
   731 	}
       
   732 
       
   733 // ---------------------------------------------------------------------------
       
   734 // 
       
   735 // ---------------------------------------------------------------------------
       
   736 //
       
   737 std::string HsWidgetPublisherImpl::ToStringL(const TDesC& aText)
       
   738     {
       
   739     HBufC8* text = HBufC8::NewL( aText.Length() + 1 /*for ending zero*/ );
       
   740     TPtr8 dest( text->Des() );
       
   741     CnvUtfConverter::ConvertFromUnicodeToUtf8( dest, aText );    
       
   742     std::string ret((const char*)dest.PtrZ());
       
   743     delete text;
       
   744     return ret;
       
   745     }
       
   746 
       
   747 // ---------------------------------------------------------------------------
       
   748 // 
       
   749 // ---------------------------------------------------------------------------
       
   750 //
       
   751 std::wstring HsWidgetPublisherImpl::ToWideStringL( const TDesC& aText ) const
       
   752     {
       
   753     return std::wstring(
       
   754             reinterpret_cast<const wchar_t*>( aText.Ptr() ), aText.Length() );
       
   755     }
       
   756 
       
   757 // ---------------------------------------------------------------------------
       
   758 // 
       
   759 // ---------------------------------------------------------------------------
       
   760 //
       
   761 void HsWidgetPublisherImpl::InsertWidgetIdentifiersL( HsWidget& aWidget,
       
   762 	CLiwDefaultMap* aDataMap )
       
   763     {
       
   764     HBufC* publisherName = StdStringToUnicodeLC( GetPublisherNameL( aWidget ) );
       
   765     aDataMap->InsertL( KPublisherId, TLiwVariant( *publisherName ));
       
   766     aDataMap->InsertL( KContentType, TLiwVariant( KHSTemplate ));
       
   767     aDataMap->InsertL( KContentId, TLiwVariant( KAll ));
       
   768     CleanupStack::PopAndDestroy( publisherName );
       
   769     }
       
   770 
       
   771 // ---------------------------------------------------------------------------
       
   772 // 
       
   773 // ---------------------------------------------------------------------------
       
   774 //
       
   775 void HsWidgetPublisherImpl::InsertWidgetDataIdentifiersL( HsWidget& aWidget,
       
   776 	CLiwDefaultMap* aDataMap )
       
   777     {
       
   778     WidgetContentIdMapType::const_iterator contentIdIter =
       
   779             mWidgetContentIds.find( aWidget.getIdentifier() );
       
   780     if ( contentIdIter == mWidgetContentIds.end() )
       
   781         {
       
   782         User::Leave( KErrNotFound );
       
   783         }
       
   784     TPtrC16 contentId = reinterpret_cast<const TUint16*>(
       
   785             contentIdIter->second.c_str() );
       
   786     
       
   787     HBufC* publisherName = StdStringToUnicodeLC( GetPublisherNameL( aWidget ) );
       
   788     
       
   789     aDataMap->InsertL( KPublisherId, TLiwVariant( *publisherName ) );
       
   790     aDataMap->InsertL( KContentType, TLiwVariant( KAll ) );
       
   791     aDataMap->InsertL( KContentId, TLiwVariant( contentId ) );
       
   792     
       
   793     CleanupStack::PopAndDestroy( publisherName );
       
   794     }
       
   795 
       
   796 // ---------------------------------------------------------------------------
       
   797 // 
       
   798 // ---------------------------------------------------------------------------
       
   799 //
       
   800 void HsWidgetPublisherImpl::InsertWidgetInfoL( HsWidget& aWidget,
       
   801 	CLiwDefaultMap* aDataMap )
       
   802     {
       
   803     __ASSERT_ALWAYS( aDataMap, User::Invariant() );
       
   804 
       
   805     HBufC* templateType = StdStringToUnicodeLC( aWidget.getTemplateName() );
       
   806     HBufC* widgetName = StdStringToUnicodeLC( aWidget.getWidgetName() );
       
   807 	    
       
   808 	aDataMap->InsertL( KTemplateType, TLiwVariant( *templateType ) );
       
   809 	aDataMap->InsertL( KWidgetName, TLiwVariant( *widgetName ) );
       
   810     aDataMap->InsertL( KWidgetMaxInstance, TLiwVariant( 1 ) );
       
   811     
       
   812     const std::string& widgetDescriptionStd = aWidget.getDescription();
       
   813     if ( widgetDescriptionStd.length() )
       
   814         {
       
   815         HBufC* widgetDescription = StdStringToUnicodeLC( widgetDescriptionStd );
       
   816         aDataMap->InsertL( KWidgetDesc, TLiwVariant( *widgetDescription ) );
       
   817         CleanupStack::PopAndDestroy( widgetDescription );
       
   818         }
       
   819     const std::string& widgetIconLocStd = aWidget.getIconLocation();
       
   820     if ( widgetIconLocStd.length() )
       
   821         {
       
   822         HBufC* widgetIconLoc = StdStringToUnicodeLC( widgetIconLocStd );
       
   823         aDataMap->InsertL( KWidgetIcon, TLiwVariant( *widgetIconLoc ) );
       
   824         CleanupStack::PopAndDestroy( widgetIconLoc );
       
   825         }
       
   826 
       
   827 	CleanupStack::PopAndDestroy( widgetName );
       
   828 	CleanupStack::PopAndDestroy( templateType );
       
   829     }
       
   830 
       
   831 // ---------------------------------------------------------------------------
       
   832 // 
       
   833 // ---------------------------------------------------------------------------
       
   834 //
       
   835 void HsWidgetPublisherImpl::InsertWidgetItemsL ( HsWidget& aWidget,
       
   836 	CLiwDefaultMap* aDataMap )
       
   837 	{
       
   838 	int count = aWidget.itemsCount();
       
   839 	for (int index = 0; index < count; index++)
       
   840 		{
       
   841 		HsWidgetItem* const item = aWidget.getWidgetItem( index );
       
   842 		
       
   843 		TPtrC8 itemName = ((TUint8*)item->getItemName().c_str());
       
   844 		if( item->isStringValue() )
       
   845 			{
       
   846 			TPtrC8 itemValue = ((TUint8*)item->getItemValue().c_str());
       
   847 		    HBufC* value = HBufC::NewLC( KUnicodeSize * itemValue.Length() );
       
   848 		    TPtr dest( value->Des() );
       
   849 		    CnvUtfConverter::ConvertToUnicodeFromUtf8( dest, itemValue );
       
   850 		    
       
   851 		    aDataMap->InsertL( itemName, TLiwVariant(*value ));
       
   852 			CleanupStack::PopAndDestroy(value);
       
   853 			}
       
   854 		else
       
   855 			{
       
   856 			int itemValue = item->getItemValueInt();
       
   857 			aDataMap->InsertL( itemName, TLiwVariant( TInt32( itemValue ) ));
       
   858 			}
       
   859 		}
       
   860 	}
       
   861 
       
   862 // ---------------------------------------------------------------------------
       
   863 // 
       
   864 // ---------------------------------------------------------------------------
       
   865 //
       
   866 TInt HsWidgetPublisherImpl::ObtainErrorCode( 
       
   867 	const CLiwGenericParamList& aOutParamList )
       
   868 	{
       
   869     TInt errorCode( KErrNone );
       
   870     TInt32 serviceErrorCode;
       
   871     TInt posErr( 0 );
       
   872     aOutParamList.FindFirst( posErr, LIW::EGenericParamError );
       
   873 	if( posErr != KErrNotFound )
       
   874 		{
       
   875 		// error code found - extract and handle
       
   876 		aOutParamList[posErr].Value().Get( serviceErrorCode );
       
   877 		errorCode = TranslateServiceError( serviceErrorCode );
       
   878 		}
       
   879     return errorCode;
       
   880 	}
       
   881 
       
   882 // ---------------------------------------------------------------------------
       
   883 // 
       
   884 // ---------------------------------------------------------------------------
       
   885 //
       
   886 TInt HsWidgetPublisherImpl::TranslateServiceError( TInt32 aServiceErrorCode )
       
   887 	{
       
   888 	TInt ret( KErrNone );
       
   889 
       
   890     switch ( aServiceErrorCode )
       
   891         {
       
   892         case SErrNone: 
       
   893         	// KErrNone and KErrCancel are translated into SErrNone 
       
   894         	ret = KErrNone;
       
   895         	break;
       
   896 
       
   897         case SErrNotFound:
       
   898             ret = KErrNotFound;
       
   899             break;
       
   900 
       
   901         case SErrNoMemory:
       
   902             ret = KErrNoMemory;
       
   903             break;
       
   904 
       
   905         case SErrServiceInUse:
       
   906             ret = KErrInUse;
       
   907             break;
       
   908 
       
   909         case SErrServiceNotSupported:
       
   910             ret = KErrNotSupported;
       
   911             break;
       
   912 
       
   913         case SErrBadArgumentType:
       
   914             ret = KErrBadName;
       
   915             break;
       
   916 
       
   917         case SErrInvalidServiceArgument:
       
   918             ret = KErrArgument;
       
   919             break;
       
   920 
       
   921         case SErrAccessDenied:
       
   922             ret = KErrPermissionDenied;
       
   923             break;            
       
   924 
       
   925         default:
       
   926             ret = KErrGeneral;
       
   927             break;
       
   928         }
       
   929 	
       
   930 	return ret;
       
   931 	}
       
   932 
       
   933 // ---------------------------------------------------------------------------
       
   934 // 
       
   935 // ---------------------------------------------------------------------------
       
   936 //
       
   937 void HsWidgetPublisherImpl::InsertItemsTriggersL( HsWidget& aWidget,
       
   938 	CLiwDefaultMap* aTriggerMap )
       
   939 	{
       
   940 	int count = aWidget.itemsCount();
       
   941 	CLiwDefaultMap* activateAction = CLiwDefaultMap::NewLC();
       
   942 	activateAction->InsertL( KPluginId, TLiwVariant( KCASpaAppLauncherPlugin ) );
       
   943 	
       
   944 	CLiwDefaultMap* activate = CLiwDefaultMap::NewLC();
       
   945 	activate->InsertL( KType, TLiwVariant( KActionValueLaunchApplication ) );
       
   946 	activate->InsertL( KLaunchMethod, TLiwVariant( KLaunchMethodValueCmdLine ) );
       
   947 	activate->InsertL( KApplicationUid, TLiwVariant( iWidgetUid ) );
       
   948 	activate->InsertL( KApaCommand, TLiwVariant( KApaCommandBackground ) );
       
   949 	
       
   950 	activateAction->InsertL( KData, TLiwVariant( activate ) );
       
   951 	
       
   952 	for (int index = 0; index < count; index++)
       
   953 		{
       
   954 		HsWidgetItem* const item = aWidget.getWidgetItem( index );
       
   955 		TPtrC8 itemName = ((TUint8*)item->getItemName().c_str());
       
   956 		aTriggerMap->InsertL( itemName, TLiwVariant( activateAction ));
       
   957 		}
       
   958 	CleanupStack::PopAndDestroy( activate );
       
   959 	CleanupStack::PopAndDestroy( activateAction );
       
   960 	}
       
   961 
       
   962 // ---------------------------------------------------------------------------
       
   963 // 
       
   964 // ---------------------------------------------------------------------------
       
   965 //
       
   966 void HsWidgetPublisherImpl::InsertWidgetACLL( CLiwDefaultMap* aDataMap )
       
   967     {
       
   968     CLiwDefaultMap* aclMap = CLiwDefaultMap::NewLC();
       
   969     CLiwDefaultMap* writeMap = CLiwDefaultMap::NewLC();
       
   970     CLiwDefaultMap* deleteMap = CLiwDefaultMap::NewLC();
       
   971     
       
   972     RBuf widgetUid;
       
   973     widgetUid.CleanupClosePushL();
       
   974     widgetUid.CreateL(10);
       
   975     widgetUid.Append( KHexPrefix );
       
   976     widgetUid.AppendNum( (TUint)iWidgetUid, EHex );
       
   977     
       
   978     RBuf widgetCHUids;
       
   979     widgetCHUids.CleanupClosePushL();
       
   980     widgetCHUids.CreateL(21);
       
   981     widgetCHUids.Append( widgetUid );
       
   982     widgetCHUids.Append( KColon );
       
   983     widgetCHUids.Append( KContentHarvesterUid );
       
   984     
       
   985     writeMap->InsertL( KUIDs, TLiwVariant( widgetUid ) );
       
   986     deleteMap->InsertL( KUIDs, TLiwVariant( widgetCHUids ) );
       
   987     
       
   988     aclMap->InsertL( KACLWrite, TLiwVariant( writeMap ) );
       
   989     aclMap->InsertL( KACLDelete, TLiwVariant( deleteMap ) );
       
   990     
       
   991     aDataMap->InsertL( KAccessList, TLiwVariant( aclMap ) );
       
   992     
       
   993     CleanupStack::PopAndDestroy( &widgetCHUids );
       
   994     CleanupStack::PopAndDestroy( &widgetUid );
       
   995     CleanupStack::PopAndDestroy( deleteMap );
       
   996     CleanupStack::PopAndDestroy( writeMap );
       
   997     CleanupStack::PopAndDestroy( aclMap );
       
   998     }
       
   999 
       
  1000 // ---------------------------------------------------------------------------
       
  1001 // 
       
  1002 // ---------------------------------------------------------------------------
       
  1003 //
       
  1004 std::string HsWidgetPublisherImpl::GetPublisherNameL( HsWidget& aWidget )
       
  1005     {
       
  1006     return aWidget.getIdentifier() + ':' + ToStringL( KHsWidgetPublisher() );
       
  1007     }
       
  1008 
       
  1009 // ---------------------------------------------------------------------------
       
  1010 // 
       
  1011 // ---------------------------------------------------------------------------
       
  1012 //
       
  1013 std::string HsWidgetPublisherImpl::GetWidgetIdentifierFromPublisherNameL(
       
  1014         const TDesC& aPublisherName )
       
  1015     {
       
  1016     TInt colonPos = aPublisherName.LocateReverse(':');
       
  1017     User::LeaveIfError( colonPos );
       
  1018     return ToStringL( aPublisherName.Left( colonPos ) );
       
  1019     }
       
  1020 
       
  1021 // ---------------------------------------------------------------------------
       
  1022 // 
       
  1023 // ---------------------------------------------------------------------------
       
  1024 //
       
  1025 HBufC* HsWidgetPublisherImpl::Utf8ToUnicodeLC( const TDesC8& aSrcString ) const
       
  1026     {
       
  1027     HBufC* destString = HBufC::NewLC( aSrcString.Length() );
       
  1028     TPtr destStringDes = destString->Des();
       
  1029     CnvUtfConverter::ConvertToUnicodeFromUtf8( destStringDes, aSrcString );
       
  1030     return destString;
       
  1031     }
       
  1032 
       
  1033 // ---------------------------------------------------------------------------
       
  1034 // 
       
  1035 // ---------------------------------------------------------------------------
       
  1036 //
       
  1037 HBufC* HsWidgetPublisherImpl::StdStringToUnicodeLC(
       
  1038         const std::string& aSrcString ) const
       
  1039     {
       
  1040     TPtrC8 srcDescriptor = reinterpret_cast<const TUint8*>(
       
  1041             aSrcString.c_str() );
       
  1042     return Utf8ToUnicodeLC( srcDescriptor );
       
  1043     }