layouts/cdl/CdlServer/src/CdlEcomReg.cpp
changeset 0 05e9090e2422
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 <barsc2.h>
       
    18 #include <barsread2.h>
       
    19 #include "CdlEcomReg.h"
       
    20 
       
    21 void CCdlDllsWatcherBase::NewL(CCdlDllsWatcherBase*& iWatcher, RFs& aFs, MCdlDllsObserver* aObserver)
       
    22 	{
       
    23 	CCdlEcomWatcher* self = new(ELeave) CCdlEcomWatcher(aFs, aObserver);
       
    24 	iWatcher = self;
       
    25 	self->ConstructL();
       
    26 	}
       
    27 
       
    28 CCdlRefCollection* CCdlDllsWatcherBase::FileContentsLC(const TDesC& aFileName)
       
    29 	{
       
    30 	CCdlRefs* refs = CCdlRefs::NewLC();
       
    31 
       
    32 	TParsePtrC parse(aFileName);
       
    33 	TPtrC name(parse.Name());
       
    34 	TPtrC drive(parse.Drive());
       
    35 
       
    36 	_LIT(KRes, "%S\\resource\\cdl\\%S_cdl_detail.rsc");
       
    37 	TBuf<64> fileName;
       
    38 	fileName.Format(KRes, &drive, &name);
       
    39 	
       
    40 	CResourceFile* resFile = CResourceFile::NewLC(iFs, fileName, 0, 0);
       
    41 	RResourceReader reader;
       
    42 	reader.OpenLC(resFile, 1);
       
    43 
       
    44 	TInt count = reader.ReadInt16L();
       
    45 	for (TInt ii=0; ii<count; ii++)
       
    46 		{
       
    47 		TCdlRef ref;
       
    48 		ref.iName = &aFileName;
       
    49 		ref.iUid = TUid::Uid(reader.ReadInt32L());
       
    50 		ref.iId = reader.ReadInt32L();
       
    51 		refs->AppendL(ref);
       
    52 		}
       
    53 
       
    54 	CleanupStack::PopAndDestroy(2);		// reader, resFile
       
    55 
       
    56 	return refs;
       
    57 	}
       
    58 
       
    59 
       
    60 CCdlEcomWatcher::CCdlEcomWatcher(RFs& aFs, MCdlDllsObserver* aObserver)
       
    61 : CCdlDllsWatcherBase(aFs), iObserver(aObserver)
       
    62 	{
       
    63 	}
       
    64 
       
    65 CCdlEcomWatcher::~CCdlEcomWatcher()
       
    66 	{
       
    67 	Cancel();
       
    68 	if (iImplArray)
       
    69 		iImplArray->ResetAndDestroy();
       
    70 	delete iImplArray;
       
    71 	if (iEcom)
       
    72 		{
       
    73 		iEcom->Close();
       
    74 		REComSession::FinalClose();
       
    75 		}
       
    76 	}
       
    77 
       
    78 void CCdlEcomWatcher::ConstructL()
       
    79 	{
       
    80 	iImplArray = new(ELeave) RImplInfoPtrArray;
       
    81 	iEcom = &REComSession::OpenL();
       
    82 	TRAP_IGNORE(GetListL());
       
    83 	Queue();
       
    84 	}
       
    85 
       
    86 void CCdlEcomWatcher::DoCancel()
       
    87 	{
       
    88 	iEcom->CancelNotifyOnChange(iStatus);
       
    89 	}
       
    90 
       
    91 void CCdlEcomWatcher::RunL()
       
    92 	{
       
    93 	TRAP_IGNORE(GetListL());
       
    94 	Queue();
       
    95 	}
       
    96 
       
    97 void CleanImplArray(TAny* aArray)
       
    98 	{
       
    99 	RImplInfoPtrArray* array = static_cast<RImplInfoPtrArray*>(aArray);
       
   100 	array->ResetAndDestroy();
       
   101 	delete array;
       
   102 	}
       
   103 
       
   104 TInt ImplUidLessThan(const CImplementationInformation& aLhs, const CImplementationInformation& aRhs)
       
   105 	{
       
   106 	return aLhs.ImplementationUid().iUid - aRhs.ImplementationUid().iUid;
       
   107 	}
       
   108 
       
   109 void CCdlEcomWatcher::GetListL()
       
   110 	{
       
   111 	RImplInfoPtrArray* implArray = new(ELeave) RImplInfoPtrArray;
       
   112 	CleanupStack::PushL(TCleanupItem(CleanImplArray, implArray));
       
   113 	REComSession::ListImplementationsL(TUid::Uid(0x101f8243), *implArray);
       
   114 
       
   115 	implArray->Sort(&ImplUidLessThan);
       
   116 
       
   117 	HandleNewListL(*implArray);
       
   118 
       
   119 	iImplArray->ResetAndDestroy();
       
   120 	delete iImplArray;
       
   121 	iImplArray = implArray;
       
   122 
       
   123 	CleanupStack::Pop(); // implArray
       
   124 	}
       
   125 
       
   126 void CCdlEcomWatcher::Queue()
       
   127 	{
       
   128 	iEcom->NotifyOnChange(iStatus);
       
   129 	SetActive();
       
   130 	}
       
   131 
       
   132 void CCdlEcomWatcher::HandleNewListL(RImplInfoPtrArray& aImplArray)
       
   133 	{
       
   134 	RImplInfoPtrArray& implArray = *iImplArray;
       
   135 	TInt ixNew = 0;
       
   136 	TInt ixCur = 0;
       
   137 	TInt countNew = aImplArray.Count();
       
   138 	TInt countCur = implArray.Count();
       
   139 	while (ixNew < countNew || ixCur < countCur)
       
   140 		{
       
   141 		if (ixCur == countCur)				// at end of current list, any UIDs left in new list should be added
       
   142 			{
       
   143 			AddNewToObserverL(*aImplArray[ixNew]);
       
   144 			ixNew++;
       
   145 			}
       
   146 		else if (ixNew == countNew)					// at end of new list, any UIDs left in current list should be removed
       
   147 			{
       
   148 			RemoveCurrentFromObserverL(*implArray[ixCur]);
       
   149 			ixCur++;
       
   150 			}
       
   151 		else if (implArray[ixCur]->ImplementationUid() == aImplArray[ixNew]->ImplementationUid())				// UIDs are the same, skip to the next one
       
   152 			{
       
   153 			ixNew++;
       
   154 			ixCur++;
       
   155 			}
       
   156 		else if (implArray[ixCur]->ImplementationUid().iUid < aImplArray[ixNew]->ImplementationUid().iUid)				// current UID does not appear in new list, remove it
       
   157 			{
       
   158 			RemoveCurrentFromObserverL(*implArray[ixCur]);
       
   159 			ixCur++;
       
   160 			}
       
   161 		else									// new UID does not appear in current list, add it
       
   162 			{
       
   163 			AddNewToObserverL(*aImplArray[ixNew]);
       
   164 			ixNew++;
       
   165 			}
       
   166 		}
       
   167 
       
   168 	if (iTellingObserver)
       
   169 		{
       
   170 		iTellingObserver = EFalse;
       
   171 		iObserver->McdoChangesCompleteL();
       
   172 		}
       
   173 	}
       
   174 
       
   175 void CCdlEcomWatcher::AddNewToObserverL(CImplementationInformation& aImpl)
       
   176 	{
       
   177 	TFileName fileName;
       
   178 	ImplFileName(aImpl, fileName);
       
   179 	StartTellingObserverIfNecessaryL();
       
   180 	TRAP_IGNORE(iObserver->McdoFileAddedL(fileName));
       
   181 	}
       
   182 
       
   183 void CCdlEcomWatcher::RemoveCurrentFromObserverL(CImplementationInformation& aImpl)
       
   184 	{
       
   185 	TFileName fileName;
       
   186 	ImplFileName(aImpl, fileName);
       
   187 	StartTellingObserverIfNecessaryL();
       
   188 	TRAP_IGNORE(iObserver->McdoFileRemovedL(fileName));
       
   189 	}
       
   190 
       
   191 void CCdlEcomWatcher::ImplFileName(CImplementationInformation& aImpl, TFileName& aFileName)
       
   192 	{
       
   193 	TDriveName drive = aImpl.Drive().Name();
       
   194 	aFileName.Format(_L("%S%08x.dll"), &drive, aImpl.ImplementationUid());
       
   195 	}
       
   196 
       
   197 void CCdlEcomWatcher::StartTellingObserverIfNecessaryL()
       
   198 	{
       
   199 	if (!iTellingObserver)
       
   200 		iObserver->McdoPrepareForChangesL();
       
   201 	iTellingObserver = ETrue;
       
   202 	}
       
   203 
       
   204 TInt ImplArrayUidSearchPred(const TUid* aUid, const CImplementationInformation& aImpl)
       
   205 	{
       
   206 	return aUid->iUid - aImpl.ImplementationUid().iUid;
       
   207 	}
       
   208 
       
   209 TInt CCdlEcomWatcher::FindPlugin(const TDesC& aFileName) const
       
   210 	{
       
   211 	// get the implementation UID from the file name
       
   212 	TParsePtrC parse(aFileName);
       
   213 	TPtrC name(parse.Name());
       
   214 	TLex lexer(name);
       
   215 	TUint32 uidVal;
       
   216 	TInt r = lexer.Val(uidVal, EHex);
       
   217 	if (r!=KErrNone)
       
   218 		return r;
       
   219 	
       
   220 	// Search for an implementation with the UID in the implementation array
       
   221 	TUid uid = { uidVal };
       
   222 	return iImplArray->FindInOrder(uid, &ImplArrayUidSearchPred);
       
   223 	}
       
   224 
       
   225 TInt CCdlEcomWatcher::IsPluginInRom(const TDesC& aFileName, TBool& aIsInRom) const
       
   226     {
       
   227     TInt index = FindPlugin(aFileName);
       
   228 	if (index<=KErrNotFound)
       
   229 		return index;
       
   230 	
       
   231 	// Is this implementation ROM based or not?
       
   232 	aIsInRom = (*iImplArray)[index]->RomBased();
       
   233 	
       
   234 	return KErrNone;
       
   235 	}
       
   236 
       
   237 TInt CCdlEcomWatcher::PluginDrive(const TDesC& aFileName, TDriveUnit& aDrive) const
       
   238     {
       
   239     TInt index = FindPlugin(aFileName);
       
   240 	if (index<=KErrNotFound)
       
   241 		return index;
       
   242 	
       
   243 	// Get the drive unit
       
   244 	aDrive = (*iImplArray)[index]->Drive();
       
   245 	
       
   246 	return KErrNone;
       
   247     }