os/ossrv/lowlevellibsandfws/apputils/src/BANAMEDPLUGINS.CPP
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200 (2012-06-15)
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include <e32std.h>
    17 #include <e32base.h>
    18 #include <f32file.h>
    19 #include <charconv.h>
    20 #include <bamdesca.h>
    21 #include <bautils.h>
    22 #include <barsc.h>
    23 #include <barsread.h>
    24 #include <baflpan.h>
    25 #include <banamedplugins.h>
    26 
    27 // CBaNamedPlugins
    28 
    29 EXPORT_C CBaNamedPlugins* CBaNamedPlugins::NewL(const CParameters& aParameters)
    30 /** Allocates and constructs a new list of plug-in names. The list is populated 
    31 using the CParameters object passed into the function. The CParameters object 
    32 can be destroyed once the CBaNamedPlugins object has been created.
    33 
    34 @param aParameters The parameters for the list of plug-in names. 
    35 @return The new list of plug-in names. */
    36 	{
    37 	CBaNamedPlugins* const namedPlugIns=NewLC(aParameters);
    38 	CleanupStack::Pop(namedPlugIns);
    39 	return namedPlugIns;
    40 	}
    41 
    42 EXPORT_C CBaNamedPlugins* CBaNamedPlugins::NewLC(const CParameters& aParameters)
    43 /** Allocates and constructs a new list of plug-in names. The list is populated 
    44 using the CParameters object passed into the function. The list is left on 
    45 the cleanup stack. The CParameters object can be destroyed once the 
    46 CBaNamedPlugins object has been created.
    47 
    48 @param aParameters The parameters for the list of plug-in names.
    49 @return The new list of plug-in names. */
    50 	{
    51 	CBaNamedPlugins* const namedPlugIns=new(ELeave) CBaNamedPlugins(Max(aParameters.iArrayOfResourceFiles->Count(), 1));
    52 	CleanupStack::PushL(namedPlugIns);
    53 	namedPlugIns->ConstructL(aParameters);
    54 	return namedPlugIns;
    55 	}
    56 
    57 EXPORT_C CBaNamedPlugins::~CBaNamedPlugins()
    58 /** Destructor. Deletes all resources owned by the object prior to its 
    59 destruction. */
    60 	{
    61 	for (TInt i=iArrayOfNamedPlugIns.Count()-1; i>=0; --i)
    62 		{
    63 		delete iArrayOfNamedPlugIns[i].iName;
    64 		delete iArrayOfNamedPlugIns[i].iIdentifier;
    65 		}
    66 	iArrayOfNamedPlugIns.Close();
    67 	}
    68 
    69 EXPORT_C TInt CBaNamedPlugins::IndexOfUid(TUid aUid) const
    70 /** Gets the index into the sorted list (i.e. the index into the MDesCArray) of 
    71 the plug-in associated with the UID specified.
    72 
    73 @param aUid A plug-in UID to search for. Its value must not be KNullUid or 
    74 a panic occurs.
    75 @return The index into the list of the plug-in with the UID specified, or 
    76 KErrNotFound if no plug-in has the specified UID. */
    77 	{
    78 	__ASSERT_ALWAYS(aUid.iUid!=KNullUid.iUid, Panic(EBafPanicNullUid));
    79 	for (TInt i=iArrayOfNamedPlugIns.Count()-1; i>=0; --i)
    80 		{
    81 		if (iArrayOfNamedPlugIns[i].iUid.iUid==aUid.iUid)
    82 			{
    83 			return i;
    84 			}
    85 		}
    86 	return KErrNotFound;
    87 	}
    88 
    89 EXPORT_C TInt CBaNamedPlugins::IndexOfIdentifier(const TDesC& aIdentifier, TEquivalentIdentifiers aEquivalentIdentifiers) const
    90 /** Gets the index into the sorted list (i.e. the index into the MDesCArray) of 
    91 the plug-in associated with the identifier specified.
    92 
    93 @param aIdentifier The plug-in identifier to search for.
    94 @param aEquivalentIdentifiers A function which tests whether two plug-in 
    95 identifiers are the same, returning true or false.
    96 @return The index into the list of the plug-in with the identifier specified, 
    97 or KErrNotFound if no plug-in has the specified identifier. */
    98 	{
    99 	for (TInt i=iArrayOfNamedPlugIns.Count()-1; i>=0; --i)
   100 		{
   101 		const TDesC* const identifier=iArrayOfNamedPlugIns[i].iIdentifier;
   102 		if ((identifier!=NULL) && (*aEquivalentIdentifiers)(*identifier, aIdentifier))
   103 			{
   104 			return i;
   105 			}
   106 		}
   107 	return KErrNotFound;
   108 	}
   109 
   110 EXPORT_C TUid CBaNamedPlugins::UidAtIndex(TInt aIndex) const
   111 /** Gets the UID of the plug-in at the specified index into the MDesCArray.
   112 
   113 @param aIndex The index into the MDesCArray. Must be within the bounds of 
   114 the array, or a panic occurs.
   115 @return The UID of the plug-in at the specified index. */
   116 	{
   117 	return iArrayOfNamedPlugIns[aIndex].iUid;
   118 	}
   119 
   120 EXPORT_C const TDesC* CBaNamedPlugins::IdentifierAtIndex(TInt aIndex) const
   121 /** Gets the identifier of the plug-in at the specified index into the MDesCArray.
   122 
   123 @param aIndex The index into the MDesCArray. Must be within the bounds of 
   124 the array, or a panic occurs.
   125 @return The identifier of the plug-in at the specified index. */
   126 	{
   127 	return iArrayOfNamedPlugIns[aIndex].iIdentifier;
   128 	}
   129 
   130 EXPORT_C TInt CBaNamedPlugins::MdcaCount() const
   131 /** Gets the number of plug-ins in the list.
   132 
   133 @return The number of plug-ins in the list. */
   134 	{
   135 	return iArrayOfNamedPlugIns.Count();
   136 	}
   137 
   138 EXPORT_C TPtrC CBaNamedPlugins::MdcaPoint(TInt aIndex) const
   139 /** Returns a TPtrC for the name of the plug-in at the given index.
   140 
   141 @param aIndex The index into the list. Must be within the bounds of the array, 
   142 or a panic occurs.
   143 @return The name of the plug-in at the given index. */
   144 	{
   145 	return *iArrayOfNamedPlugIns[aIndex].iName;
   146 	}
   147 
   148 CBaNamedPlugins::CBaNamedPlugins(TInt aGranularity)
   149 	:iArrayOfNamedPlugIns(aGranularity)
   150 	{
   151 	}
   152 
   153 void CBaNamedPlugins::ConstructL(const CParameters& aParameters)
   154 	{
   155 	TFileName* const fileName=new(ELeave) TFileName;
   156 	CleanupStack::PushL(fileName);
   157 	RResourceFile resourceFile;
   158 	CleanupClosePushL(resourceFile);
   159 	const TCompareNames compareNames=(aParameters.iCompareNames!=NULL)? aParameters.iCompareNames: DefaultAlgorithmToCompareNames;
   160 	for (TInt i=aParameters.iArrayOfResourceFiles->Count()-1; i>=0; --i)
   161 		{
   162 		const TResourceFile& resourceFileData=(*aParameters.iArrayOfResourceFiles)[i];
   163 		*fileName=*resourceFileData.iFullFileName;
   164 		BaflUtils::NearestLanguageFile(aParameters.iFileServerSession, *fileName);
   165 		TNamedPlugIn namedPlugIn;
   166 		namedPlugIn.iIdentifier=(resourceFileData.iIdentifier==NULL)? NULL: resourceFileData.iIdentifier->AllocLC();
   167 		namedPlugIn.iUid=resourceFileData.iUid;
   168 		namedPlugIn.iCompareNames=compareNames;
   169 		if (!BaflUtils::FileExists(aParameters.iFileServerSession, *fileName))
   170 			{
   171 			if (aParameters.iFallBackName==NULL)
   172 				{
   173 				namedPlugIn.iName=TParsePtrC(*resourceFileData.iFullFileName).Name().AllocLC();
   174 				}
   175 			else
   176 				{
   177 				namedPlugIn.iName=aParameters.iFallBackName->FallBackNameL(*resourceFileData.iFullFileName);
   178 				CleanupStack::PushL(namedPlugIn.iName);
   179 				}
   180 			User::LeaveIfError(iArrayOfNamedPlugIns.Append(namedPlugIn));
   181 			CleanupStack::Pop(namedPlugIn.iName);
   182 			}
   183 		else
   184 			{
   185 			resourceFile.Close();
   186 			resourceFile.OpenL(aParameters.iFileServerSession, *fileName);
   187 			HBufC8* const resource=resourceFile.AllocReadLC(resourceFile.Offset()+2); // read the first resource after the RSS_SIGNATURE resource
   188 			switch (resourceFileData.iFormat)
   189 				{
   190 			case TResourceFile::EFormatTbuf:
   191 				{
   192 				const TPtrC name(REINTERPRET_CAST(const TText*, resource->Ptr()), resource->Length()/sizeof(TText));
   193 				if (name.Length()>0)
   194 					{
   195 					namedPlugIn.iName=name.AllocLC();
   196 					User::LeaveIfError(iArrayOfNamedPlugIns.Append(namedPlugIn));
   197 					CleanupStack::Pop(namedPlugIn.iName);
   198 					}
   199 				}
   200 				break;
   201 			case TResourceFile::EFormatArrayOfUidNamePairs:
   202 				{
   203 				TResourceReader resourceReader;
   204 				resourceReader.SetBuffer(resource);
   205 				for (TInt j=resourceReader.ReadUint16()-1; j>=0; --j)
   206 					{
   207 					namedPlugIn.iUid=TUid::Uid(resourceReader.ReadUint32());
   208 					const TPtrC name(resourceReader.ReadTPtrC());
   209 					if (name.Length()>0)
   210 						{
   211 						namedPlugIn.iName=name.AllocLC();
   212 						User::LeaveIfError(iArrayOfNamedPlugIns.Append(namedPlugIn));
   213 						CleanupStack::Pop(namedPlugIn.iName);
   214 						}
   215 					}
   216 				}
   217 				break;
   218 			default:
   219 				Panic(EBafPanicBadResourceFileFormat);
   220 				break;
   221 				}
   222 			CleanupStack::PopAndDestroy(resource);
   223 			}
   224 		if (namedPlugIn.iIdentifier!=NULL)
   225 			{
   226 			CleanupStack::Pop(namedPlugIn.iIdentifier);
   227 			}
   228 		}
   229 	CleanupStack::PopAndDestroy(2, fileName);
   230 	iArrayOfNamedPlugIns.Sort(TLinearOrder<TNamedPlugIn>(CompareNamedPlugIns));
   231 	if (aParameters.iTextForNone!=NULL)
   232 		{
   233 		TNamedPlugIn namedPlugIn;
   234 		namedPlugIn.iName=aParameters.iTextForNone->AllocLC();
   235 		namedPlugIn.iIdentifier=NULL;
   236 		namedPlugIn.iUid=KNullUid;
   237 		namedPlugIn.iCompareNames=NULL;
   238 		switch (aParameters.iArrayPositionOfTextForNone)
   239 			{
   240 		case EArrayPositionFirst:
   241 			User::LeaveIfError(iArrayOfNamedPlugIns.Insert(namedPlugIn, 0));
   242 			break;
   243 		case EArrayPositionLast:
   244 			User::LeaveIfError(iArrayOfNamedPlugIns.Append(namedPlugIn));
   245 			break;
   246 		default:
   247 			Panic(EBafPanicBadArrayPosition);
   248 			break;
   249 			}
   250 		CleanupStack::Pop(namedPlugIn.iName);
   251 		}
   252 	}
   253 
   254 TInt CBaNamedPlugins::CompareNamedPlugIns(const TNamedPlugIn& aNamedPlugIn1, const TNamedPlugIn& aNamedPlugIn2)
   255 	{
   256 	__ASSERT_DEBUG((aNamedPlugIn1.iCompareNames!=NULL) && (aNamedPlugIn1.iCompareNames==aNamedPlugIn2.iCompareNames), Panic(EBafPanicBadCompareNames));
   257 	return (*aNamedPlugIn1.iCompareNames)(*aNamedPlugIn1.iName, *aNamedPlugIn2.iName);
   258 	}
   259 
   260 TInt CBaNamedPlugins::DefaultAlgorithmToCompareNames(const TDesC& aName1, const TDesC& aName2)
   261 	{
   262 	return aName1.CompareC(aName2);
   263 	}
   264 
   265 // CBaNamedPlugins::MFallBackName
   266 /**
   267 @internalAll
   268 */
   269 EXPORT_C void CBaNamedPlugins::MFallBackName::MFallBackName_Reserved_1()
   270 	{
   271 	}
   272 /**
   273 @internalAll
   274 */
   275 EXPORT_C void CBaNamedPlugins::MFallBackName::MFallBackName_Reserved_2()
   276 	{
   277 	}
   278 
   279 // CBaNamedPlugins::CParameters
   280 
   281 EXPORT_C CBaNamedPlugins::CParameters* CBaNamedPlugins::CParameters::NewL(RFs& aFileServerSession, const TArray<TResourceFile>& aArrayOfResourceFiles)
   282 /** Allocates and constructs a new parameters object. 
   283 	
   284 @param aFileServerSession A connected session with the file server. This is 
   285 required to search the file sytem for the localised resource files, and to 
   286 open them for reading.
   287 @param aArrayOfResourceFiles Array of TResourceFile objects. Each object 
   288 contains information about a single plug-in, if its iFormat attribute is set 
   289 to EFormatTbuf or potentially multiple plug-ins if its iFormat attribute is 
   290 set to EFormatArrayOfUidNamePairs. This information includes the filename of 
   291 its resource file. The CParameters object takes a copy of this array.
   292 @return The new parameters object. */
   293 	{
   294 	CParameters* const parameters=NewLC(aFileServerSession, aArrayOfResourceFiles);
   295 	CleanupStack::Pop(parameters);
   296 	return parameters;
   297 	}
   298 
   299 EXPORT_C CBaNamedPlugins::CParameters* CBaNamedPlugins::CParameters::NewLC(RFs& aFileServerSession, const TArray<TResourceFile>& aArrayOfResourceFiles)
   300 /** Allocates and constructs a new parameters object. The object is left on the 
   301 cleanup stack. 
   302 
   303 @param aFileServerSession A connected session with the file server. This is 
   304 required to search the file sytem for the localised resource files and to 
   305 open them for reading.
   306 @param aArrayOfResourceFiles Array of TResourceFile objects. Each object 
   307 contains information about a single plug-in, if its iFormat attribute is set 
   308 to EFormatTbuf or potentially multiple plug-ins if its iFormat attribute is 
   309 set to EFormatArrayOfUidNamePairs. This information includes the filename of 
   310 its resource file. The CParameters object takes a copy of this array.
   311 @return The new parameters object. */
   312 	{
   313 	CParameters* const parameters=new(ELeave) CParameters(aFileServerSession);
   314 	CleanupStack::PushL(parameters);
   315 	parameters->ConstructL(aArrayOfResourceFiles);
   316 	return parameters;
   317 	}
   318 
   319 
   320 EXPORT_C CBaNamedPlugins::CParameters::~CParameters()
   321 /** Destructor. Deletes all resources owned by the object. */
   322 	{
   323 	delete iArrayOfResourceFiles;
   324 	delete iTextForNone;
   325 	}
   326 
   327 EXPORT_C void CBaNamedPlugins::CParameters::SetFallBackName(const MFallBackName& aFallBackName)
   328 /** Sets a function that generates a fallback name for plug-ins for which no 
   329 resource file could be found. If SetFallBackName() is not called, then by 
   330 default the fallback name used for plug-ins is simply the filename of the 
   331 resource file without the drive, directory path or extension. 
   332 
   333 @param aFallBackName An instance of an MFallBackName-derived class. This should 
   334 implement a function which creates a name for plug-ins for which there is 
   335 no resource available (the "fallback" name). */
   336 	{
   337 	iFallBackName=&aFallBackName;
   338 	}
   339 
   340 EXPORT_C void CBaNamedPlugins::CParameters::SetCompareNames(TCompareNames aCompareNames)
   341 /** Sets a function that compares two plug-in names for sorting. The plug-in 
   342 names list is sorted after it has been fully populated, using this algorithm. 
   343 If SetCompareNames() is not called, collation takes place by default using 
   344 TDesC::CompareC().
   345 	
   346 @param aCompareNames A function that compares two plug-in names for sorting. */
   347 	{
   348 	iCompareNames=aCompareNames;
   349 	}
   350 
   351 EXPORT_C void CBaNamedPlugins::CParameters::SetTextForNoneL(const TDesC& aTextForNone, TArrayPosition aArrayPositionOfTextForNone)
   352 /** Sets a text string, representing the choice of no plug-in and the array 
   353 position at which to insert it. This function increases the length of the 
   354 plug-in names list by one because it creates and adds an item to the array 
   355 which is empty except for the text string specified.
   356 
   357 @param aTextForNone The string whose meaning is "none", i.e. no plug-in. It 
   358 is assumed that this descriptor has already been localised.
   359 @param aArrayPositionOfTextForNone Whether the string should be inserted at 
   360 the start or appended to the end of the array. */
   361 	{
   362 	HBufC* const textForNone=aTextForNone.AllocL();
   363 	delete iTextForNone;
   364 	iTextForNone=textForNone;
   365 	iArrayPositionOfTextForNone=aArrayPositionOfTextForNone;
   366 	}
   367 
   368 
   369 EXPORT_C void CBaNamedPlugins::CParameters::SetTextForNone(HBufC* aTextForNone, TArrayPosition aArrayPositionOfTextForNone)
   370 /** Sets a text string, representing the choice of no plug-in and the array 
   371 position at which to insert it. This function increases the length of the 
   372 plug-in names list by one because it creates and adds an item to the array 
   373 which is empty except for the text string specified. The function cannot 
   374 leave because nothing is allocated ownership of aTextForNone is passed to 
   375 the CParameters object. 
   376 
   377 @param aTextForNone The string whose meaning is "none", i.e. no plug-in. It 
   378 is assumed that this descriptor has already been localised.
   379 @param aArrayPositionOfTextForNone Whether the string should be inserted at 
   380 the start or appended to the end of the array. */
   381 	{
   382 	delete iTextForNone;
   383 	iTextForNone=aTextForNone;
   384 	iArrayPositionOfTextForNone=aArrayPositionOfTextForNone;
   385 	}
   386 
   387 CBaNamedPlugins::CParameters::CParameters(RFs& aFileServerSession)
   388 	:iFileServerSession(aFileServerSession),
   389 	 iArrayOfResourceFiles(NULL),
   390 	 iFallBackName(NULL),
   391 	 iCompareNames(NULL),
   392 	 iTextForNone(NULL),
   393 	 iArrayPositionOfTextForNone(STATIC_CAST(TArrayPosition, -1))
   394 	{
   395 	}
   396 
   397 void CBaNamedPlugins::CParameters::ConstructL(const TArray<TResourceFile>& aArrayOfResourceFiles)
   398 	{
   399 	iArrayOfResourceFiles=new(ELeave) TArray<TResourceFile>(aArrayOfResourceFiles);
   400 	}
   401