layouts/cdl/CdlEngine/src/CCdlEngine.cpp
changeset 0 05e9090e2422
child 10 08459e712984
equal deleted inserted replaced
-1:000000000000 0:05e9090e2422
       
     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:
       
    15 *
       
    16 */
       
    17 #include "CCdlEngine.h"
       
    18 #include <CdlEngDef.h>
       
    19 #include "CdlRefs.h"
       
    20 #include "CdlServEng.h"
       
    21 
       
    22 
       
    23 
       
    24 //
       
    25 // CCdlEngineBase
       
    26 //
       
    27 
       
    28 CCdlEngineBase* CCdlEngineBase::Static()
       
    29 	{
       
    30 	return static_cast<CCdlEngineBase*>(Dll::Tls());
       
    31 	}
       
    32 
       
    33 CCdlEngineBase* CCdlEngineBase::NewLC()
       
    34 	{
       
    35 	CCdlEngineBase* self = new(ELeave) CCdlEngineBase;
       
    36 	CleanupStack::PushL(self);
       
    37 	self->ConstructL();
       
    38 	return self;
       
    39 	}
       
    40 
       
    41 CCdlEngineBase* CCdlEngineBase::InstanceLC(TBool& aPushed)
       
    42 	{
       
    43 	aPushed = EFalse;
       
    44 	CCdlEngineBase* eng = CCdlEngineBase::Static();
       
    45 	if (!eng)
       
    46 		{
       
    47 		eng = CCdlEngineBase::NewLC();
       
    48 		aPushed = ETrue;
       
    49 		}
       
    50 	return eng;
       
    51 	}
       
    52 
       
    53 CCdlEngineBase::~CCdlEngineBase()
       
    54 	{
       
    55 	delete iLibMan;
       
    56 	Dll::SetTls(0);
       
    57 	}
       
    58 
       
    59 void CCdlEngineBase::IncRefCount()
       
    60 	{
       
    61 	iRefCount++;
       
    62 	}
       
    63 
       
    64 void CCdlEngineBase::DecRefCount()
       
    65 	{
       
    66 	iRefCount--;
       
    67 	if (iRefCount == 0)
       
    68 		delete this;
       
    69 	}
       
    70 
       
    71 CCdlEngineBase::CCdlEngineBase()
       
    72 	{
       
    73 	Dll::SetTls(this);
       
    74 	}
       
    75 
       
    76 void CCdlEngineBase::ConstructL()
       
    77 	{
       
    78 	iLibMan = new(ELeave) CCdlLibManager;
       
    79 	iLibMan->ConstructL();
       
    80 	}
       
    81 
       
    82 CCdlRefCollection* CCdlEngineBase::FileContentsLC(const TDesC& aLibName)
       
    83 	{
       
    84 	return CCdlLibraryContentsImp::NewLC(aLibName, this);
       
    85 	}
       
    86 
       
    87 CCdlLibManager* CCdlEngineBase::LibMan() const
       
    88 	{
       
    89 	return iLibMan;
       
    90 	}
       
    91 
       
    92 TInt CCdlEngineBase::IsPluginInRom(const TDesC& /*aFileName*/, TBool& /*aIsInRom*/) const
       
    93 	{
       
    94 	return KErrNotSupported;
       
    95 	}
       
    96 
       
    97 TInt CCdlEngineBase::PluginDrive(const TDesC& /*aFileName*/, TDriveUnit& /*aDrive*/) const
       
    98     {
       
    99 	return KErrNotSupported;
       
   100     }
       
   101 
       
   102 
       
   103 //
       
   104 // CCdlEngine
       
   105 //
       
   106 
       
   107 CCdlEngine* CCdlEngine::Static()
       
   108 	{
       
   109 	return static_cast<CCdlEngine*>(Dll::Tls());
       
   110 	}
       
   111 
       
   112 CCdlEngine* CCdlEngine::NewLC()
       
   113 	{
       
   114 	CCdlEngine* self = new(ELeave) CCdlEngine;
       
   115 	CleanupStack::PushL(self);
       
   116 	self->ConstructL();
       
   117 	return self;
       
   118 	}
       
   119 
       
   120 CCdlEngine* CCdlEngine::InstanceLC(TBool& aPushed)
       
   121 	{
       
   122 	aPushed = EFalse;
       
   123 	CCdlEngine* eng = CCdlEngine::Static();
       
   124 	if (!eng)
       
   125 		{
       
   126 		eng = CCdlEngine::NewLC();
       
   127 		aPushed = ETrue;
       
   128 		}
       
   129 	return eng;
       
   130 	}
       
   131 
       
   132 CCdlEngine& CCdlEngine::Instance()
       
   133 	{
       
   134 	CCdlEngine* eng = CCdlEngine::Static();
       
   135 	__ASSERT_ALWAYS(eng, Panic(ECdlEngPanic_NoEngine));
       
   136 	return *eng;
       
   137 	}
       
   138 
       
   139 
       
   140 CCdlEngine::~CCdlEngine()
       
   141 	{
       
   142 	delete iChangeMonitor;
       
   143 	// Release the hash table
       
   144 	CCdlCustomisations::TIter iterator(iCustomisations);
       
   145 	while(iterator.NextKey())
       
   146 	{
       
   147 		CCdlCustomisationStack* stack = *(iterator.CurrentValue());
       
   148 		delete stack;
       
   149 	}
       
   150 	
       
   151 	iCustomisations.Close();
       
   152 	iServer.Close();
       
   153 	}
       
   154 
       
   155 TAny* CCdlEngine::GetData(TUid aCdlUid, TInt aApiId) const
       
   156 	{
       
   157 	return LookupP(aCdlUid).GetData(aApiId);
       
   158 	}
       
   159 
       
   160 TCdlEngineFunc* CCdlEngine::GetFunction(TUid aCdlUid, TInt aApiId) const
       
   161 	{
       
   162 	return LookupP(aCdlUid).GetFunction(aApiId);
       
   163 	}
       
   164 
       
   165 CCdlEngine::CCdlEngine()
       
   166 	{
       
   167 	}
       
   168 
       
   169 void CCdlEngine::ConstructL()
       
   170 	{
       
   171 	CCdlEngineBase::ConstructL();
       
   172 	User::LeaveIfError(iServer.Connect());
       
   173 	iChangeMonitor = new(ELeave) CCdlChangeMonitor(iServer, this);
       
   174 	}
       
   175 
       
   176 CCdlCustomisationStack* CCdlEngine::LookupOrCreateL(TUid aCdlUid)
       
   177 	{
       
   178 	CCdlCustomisationStack* stack = Lookup(aCdlUid);
       
   179 	if (!stack)
       
   180 		{
       
   181 		stack = CCdlCustomisationStack::NewLC(aCdlUid, this);
       
   182 		iCustomisations.InsertL(aCdlUid.iUid, stack);
       
   183 		CleanupStack::Pop(stack);
       
   184 		}
       
   185 	return stack;
       
   186 	}
       
   187 
       
   188 void CCdlEngine::RequireCustomisationL(const SCdlInterface* aInterfaceParams)
       
   189 	{
       
   190 	LookupOrCreateL(aInterfaceParams->iUid)->RequireCustomisationL(aInterfaceParams);
       
   191 	}
       
   192 
       
   193 void CCdlEngine::LoadCustomisationL(const TCdlRef& aRef)
       
   194 	{
       
   195 	LookupOrCreateL(aRef.iUid)->LoadCustomisationL(aRef);
       
   196 	}
       
   197 
       
   198 TBool CCdlEngine::IsCustomisationStarted(const SCdlInterface* aInterfaceParams) const
       
   199 	{
       
   200 	CCdlCustomisationStack* stack = Lookup(aInterfaceParams->iUid);
       
   201 	if (!stack)
       
   202 		return EFalse;
       
   203 	return stack->IsCustomisationStarted(aInterfaceParams);
       
   204 	}
       
   205 
       
   206 const CCdlInstance& CCdlEngine::CustomisationInstance(TUid aCdlUid) const
       
   207 	{
       
   208 	return LookupP(aCdlUid).Top();
       
   209 	}
       
   210 
       
   211 CCdlCustomisationStack* CCdlEngine::Lookup(TUid aCdlUid) const
       
   212 	{
       
   213 	CCdlCustomisationStack * const * stack = iCustomisations.Find(aCdlUid.iUid);
       
   214 	if(stack)
       
   215 		{
       
   216 		// Found from the hash map
       
   217 		return *stack;
       
   218 		}
       
   219 		
       
   220 	return NULL;
       
   221 	}
       
   222 
       
   223 CCdlCustomisationStack& CCdlEngine::LookupP(TUid aCdlUid) const
       
   224 	{
       
   225 	CCdlCustomisationStack* stack = Lookup(aCdlUid);
       
   226 	__ASSERT_ALWAYS(stack, Panic(ECdlEngPanic_CustomisationNotPresent));
       
   227 	return *stack;
       
   228 	}
       
   229 
       
   230 const CCdlInstance* CCdlEngine::LastAccessedInstance(TUid aCdlUid) const
       
   231 	{
       
   232 	return LookupP(aCdlUid).LastAccessedInstance();
       
   233 	}
       
   234 
       
   235 const TCdlRef& CCdlEngine::LastAccessedRef(TUid aCdlUid) const
       
   236 	{
       
   237 	return LastAccessedInstance(aCdlUid)->Ref();
       
   238 	}
       
   239 
       
   240 void CCdlEngine::FileNameRelativeToLastAccessedInstance(TUid aCdlUid, TFileName& aFileName) const
       
   241 	{
       
   242 	LastAccessedInstance(aCdlUid)->FileNameRelativeToLastAccessedInstance(aFileName);
       
   243 	}
       
   244 
       
   245 TInt CCdlEngine::LastApiId() const
       
   246 	{
       
   247 	return iLastApiId;
       
   248 	}
       
   249 
       
   250 CCdlRefs* CCdlEngine::FindInstancesLC(TUid aCdlUid) const
       
   251 	{
       
   252 	return iServer.FindInstancesLC(aCdlUid);
       
   253 	}
       
   254 
       
   255 CCdlRefCollection* CCdlEngine::FileContentsLC(const TDesC& aLibName)
       
   256 	{
       
   257 	return iServer.FileContentsLC(aLibName);
       
   258 	}
       
   259 
       
   260 CCdlNames* CCdlEngine::FindCustomisationFilesLC() const
       
   261 	{
       
   262 	return iServer.FindCustomisationFilesLC();
       
   263 	}
       
   264 
       
   265 CCdlRefs* CCdlEngine::AllAvailableRefsLC() const
       
   266 	{
       
   267 	return iServer.AllAvailableRefsLC();
       
   268 	}
       
   269 
       
   270 TInt CCdlEngine::IsPluginInRom(const TDesC& aFileName, TBool& aIsInRom) const
       
   271 	{
       
   272 	return iServer.IsPluginInRom(aFileName, aIsInRom);
       
   273 	}
       
   274 
       
   275 TInt CCdlEngine::PluginDrive(const TDesC& aFileName, TDriveUnit& aDrive) const
       
   276     {
       
   277 	return iServer.PluginDrive(aFileName, aDrive);
       
   278     }
       
   279 
       
   280 TInt* CCdlEngine::LastApiRefL(TUid aCdlUid)
       
   281 	{
       
   282 	CCdlCustomisationStack* stack = LookupOrCreateL(aCdlUid);
       
   283 	return stack->LastApiRef();
       
   284 	}
       
   285 
       
   286 void CCdlEngine::SetLocalStateL(const CCdlRefs& aState)
       
   287 	{
       
   288 	CCdlUids* uids = CCdlUids::NewLC();
       
   289 	TRAPD(err, DoSetLocalStateL(aState, *uids));
       
   290 	iObservers.HandleCustomisationChangeL(*uids);
       
   291 	CleanupStack::PopAndDestroy();
       
   292 	User::LeaveIfError(err);
       
   293 	}
       
   294 
       
   295 void CCdlEngine::DoSetLocalStateL(const CCdlRefs& aState, CCdlUids& aChangedUids)
       
   296 	{
       
   297 	CCdlUids* uids = aState.UidsLC();
       
   298 	TInt uidCount = uids->Count();
       
   299 	aChangedUids.ResizeL(uidCount, TUid::Uid(0));
       
   300 	for (TInt ii=0; ii<uidCount; ++ii)
       
   301 		{
       
   302 		TUid uid = uids->At(ii);
       
   303 		CCdlCustomisationStack* stack = LookupOrCreateL(uid);
       
   304 		stack->ClearOverrides();
       
   305 		aChangedUids[ii] = uid;
       
   306 		for (CCdlRefs::TIterator pRef = aState.Begin(uid); pRef != aState.End(); ++pRef)
       
   307 			{
       
   308 			stack->LoadCustomisationL(*pRef);
       
   309 			}
       
   310 		}
       
   311 	CleanupStack::PopAndDestroy(uids);
       
   312 	}
       
   313 
       
   314 void CCdlEngine::SetGlobalStateL(const CCdlRefs& aState)
       
   315 	{
       
   316 	HBufC8* buf = aState.ExportLC();
       
   317 	iServer.SetCustL(*buf);
       
   318 	CleanupStack::PopAndDestroy(buf);
       
   319 	}
       
   320 
       
   321 CCdlRefs* CCdlEngine::LocalStateLC(const CCdlUids& aCdlUids) const
       
   322 	{
       
   323 	CCdlRefs* state = CCdlRefs::NewLC();
       
   324 	TInt count = aCdlUids.Count();
       
   325 	for (TInt ii=0; ii<count; ii++)
       
   326 		{
       
   327 		TUid uid = aCdlUids[ii];
       
   328 		AddLocalStateToRefs(uid, state);
       
   329 		}
       
   330 	return state;
       
   331 	}
       
   332 
       
   333 void CCdlEngine::AddLocalStateToRefs(TUid aUid, CCdlRefs* aState) const
       
   334 	{
       
   335 	CCdlCustomisationStack* stack = Lookup(aUid);
       
   336 	if (stack)
       
   337 		{
       
   338 		const CCdlCustomisationStack::CStack& insts = stack->Stack();
       
   339 		TInt iCount = insts.Count();
       
   340 		for (TInt jj=0; jj<iCount; jj++)
       
   341 			{
       
   342 			aState->AppendL(insts[jj]->Ref());
       
   343 			}
       
   344 		}
       
   345 	}
       
   346 
       
   347 CCdlRefs* CCdlEngine::GlobalStateLC(const CCdlUids& aCdlUids) const
       
   348 	{
       
   349 	TInt size;
       
   350 	iServer.RequestGetCustL(aCdlUids, size);
       
   351 	return GlobalStateLC(size);
       
   352 	}
       
   353 
       
   354 CCdlRefs* CCdlEngine::GlobalStateLC(TInt aSize) const
       
   355 	{
       
   356 	const TInt KMaxRetries = 10;
       
   357 	CCdlRefs* state = CCdlRefs::NewLC();
       
   358 	HBufC8* buf = HBufC8::NewMaxLC(aSize);
       
   359 	TInt ii;
       
   360 	for (ii=0; ii<KMaxRetries; ii++)
       
   361 		{
       
   362 		TPtr8 ptr = buf->Des();
       
   363 		TInt err = iServer.GetCust(ptr, aSize);
       
   364 		if (err == KErrNone)
       
   365 			{
       
   366 			state->ImportL(ptr);
       
   367 			CleanupStack::PopAndDestroy(buf);
       
   368 			break;
       
   369 			}
       
   370 		else if (err == KCdlRefsSizeChanged)
       
   371 			{
       
   372 			CleanupStack::PopAndDestroy(buf);	// makes ptr invalid
       
   373 			buf = HBufC8::NewMaxLC(aSize);
       
   374 			}
       
   375 		else
       
   376 			{
       
   377 			User::Leave(err);
       
   378 			}
       
   379 		}
       
   380 
       
   381 	if (ii == KMaxRetries)
       
   382 		User::Leave(KErrNotReady);
       
   383 
       
   384 	return state;
       
   385 	}
       
   386 
       
   387 void CCdlEngine::EnableGlobalCustomisationL(const CCdlUids& aCdlUids, TBool aEnabled)
       
   388 	{
       
   389 	TInt count = aCdlUids.Count();
       
   390 	for (TInt ii=0; ii<count; ii++)
       
   391 		{
       
   392 		TUid uid = aCdlUids[ii];
       
   393 		CCdlCustomisationStack* stack = Lookup(uid);
       
   394 		if (stack)
       
   395 			{
       
   396 			stack->EnableGlobalCustomisation(aEnabled);
       
   397 			}
       
   398 		}
       
   399 	StartGlobalCustomisationL();
       
   400 	}
       
   401 
       
   402 void CCdlEngine::StartGlobalCustomisationL()
       
   403 	{
       
   404 	CCdlUids* add = CCdlUids::NewLC();
       
   405 	
       
   406 	CCdlCustomisations::TIter iterator(iCustomisations);
       
   407 	while(iterator.NextKey())
       
   408 		{
       
   409 		CCdlCustomisationStack* stack = *(iterator.CurrentValue());
       
   410 		TUid uid = stack->Uid();
       
   411 		TBool isEnabled = (iGloballyCustomised.FindIndex(uid) != KErrNotFound);
       
   412 		TBool reqEnabled = stack->GlobalCustomisationEnabled();
       
   413 		if (isEnabled && !reqEnabled)			// enabled, but not required -> remove
       
   414 			iGloballyCustomised.Remove(uid);
       
   415 		if (!isEnabled && reqEnabled)			// not enabled, but required -> add
       
   416 			add->AppendL(uid);
       
   417 		// other combinations of enabled & required need no action.
       
   418 		}
       
   419 
       
   420 	if (add->Count())
       
   421 		{
       
   422 		CCdlRefs* state = GlobalStateLC(*add);
       
   423 		SetLocalStateL(*state);
       
   424 		CleanupStack::PopAndDestroy(state);
       
   425 		iGloballyCustomised.AddL(*add);
       
   426 		}
       
   427 
       
   428 	iServer.SetUidsToNotifyL(iGloballyCustomised);
       
   429 
       
   430 	CleanupStack::PopAndDestroy(add);
       
   431 	}
       
   432 
       
   433 void CCdlEngine::HandleGlobalCustomisationChangeL(TInt aSize)
       
   434 	{
       
   435 	CCdlRefs* state = GlobalStateLC(aSize);
       
   436 	SetLocalStateL(*state);
       
   437 	CleanupStack::PopAndDestroy(state);
       
   438 	}
       
   439 
       
   440 void CCdlEngine::HandleAvailableRefsChangeL()
       
   441 	{
       
   442 	iObservers.HandleAvailableRefsChangeL();
       
   443 	}
       
   444 
       
   445 void CCdlEngine::AddCustomisationChangeObserverL(MCdlChangeObserver* aObserver, TUid aUid)
       
   446 	{
       
   447 	iObservers.AddCustomisationChangeObserverL(aObserver, aUid);
       
   448 	}
       
   449 
       
   450 void CCdlEngine::AddGeneralChangeObserverL(MCdlChangeObserver* aObserver)
       
   451 	{
       
   452 	iObservers.AddGeneralChangeObserverL(aObserver);
       
   453 	}
       
   454 
       
   455 void CCdlEngine::RemoveChangeObserver(MCdlChangeObserver* aObserver)
       
   456 	{
       
   457 	iObservers.RemoveChangeObserver(aObserver);
       
   458 	}
       
   459 
       
   460 
       
   461 //
       
   462 // CCdlLibraryContentsImp
       
   463 //
       
   464 
       
   465 CCdlLibraryContentsImp* CCdlLibraryContentsImp::NewLC(const TDesC& aLibName, CCdlEngineBase* aEngine)
       
   466 	{
       
   467 	CCdlLibraryContentsImp* self = new(ELeave) CCdlLibraryContentsImp();
       
   468 	CleanupStack::PushL(self);
       
   469 	self->ConstructL(aEngine, aLibName);
       
   470 	return self;
       
   471 	}
       
   472 
       
   473 CCdlLibraryContentsImp::~CCdlLibraryContentsImp()
       
   474 	{
       
   475 	iLib.Close();
       
   476 	}
       
   477 
       
   478 TInt CCdlLibraryContentsImp::CountRefs() const
       
   479 	{
       
   480 	return iContent.Count();
       
   481 	}
       
   482 
       
   483 TCdlRef CCdlLibraryContentsImp::Ref(TInt aIndex) const
       
   484 	{
       
   485 	const SCdlCustomisation& cust = iContent[aIndex];
       
   486 	TCdlRef ref = 
       
   487 		{
       
   488 		cust.iId, 
       
   489 		{ cust.iInterface->iUid.iUid }, 
       
   490 		iLib.Lib()->FullName()
       
   491 		};
       
   492 	return ref;
       
   493 	}
       
   494 
       
   495 void CCdlLibraryContentsImp::ConstructL(CCdlEngineBase* aEngine, const TDesC& aLibName)
       
   496 	{
       
   497 	CCdlLibManager* libMan = aEngine->LibMan();
       
   498     RCdlLibrary* lib = libMan->FindLib(aLibName);
       
   499 	iLib = libMan->LoadL(aLibName, lib, EFalse);
       
   500 	iContent = iLib.Lib()->ContentsL();
       
   501 	}
       
   502 
       
   503 
       
   504 
       
   505 
       
   506