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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
25 #include <banamedplugins.h>
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.
34 @param aParameters The parameters for the list of plug-in names.
35 @return The new list of plug-in names. */
37 CBaNamedPlugins* const namedPlugIns=NewLC(aParameters);
38 CleanupStack::Pop(namedPlugIns);
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.
48 @param aParameters The parameters for the list of plug-in names.
49 @return The new list of plug-in names. */
51 CBaNamedPlugins* const namedPlugIns=new(ELeave) CBaNamedPlugins(Max(aParameters.iArrayOfResourceFiles->Count(), 1));
52 CleanupStack::PushL(namedPlugIns);
53 namedPlugIns->ConstructL(aParameters);
57 EXPORT_C CBaNamedPlugins::~CBaNamedPlugins()
58 /** Destructor. Deletes all resources owned by the object prior to its
61 for (TInt i=iArrayOfNamedPlugIns.Count()-1; i>=0; --i)
63 delete iArrayOfNamedPlugIns[i].iName;
64 delete iArrayOfNamedPlugIns[i].iIdentifier;
66 iArrayOfNamedPlugIns.Close();
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.
73 @param aUid A plug-in UID to search for. Its value must not be KNullUid or
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. */
78 __ASSERT_ALWAYS(aUid.iUid!=KNullUid.iUid, Panic(EBafPanicNullUid));
79 for (TInt i=iArrayOfNamedPlugIns.Count()-1; i>=0; --i)
81 if (iArrayOfNamedPlugIns[i].iUid.iUid==aUid.iUid)
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.
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. */
99 for (TInt i=iArrayOfNamedPlugIns.Count()-1; i>=0; --i)
101 const TDesC* const identifier=iArrayOfNamedPlugIns[i].iIdentifier;
102 if ((identifier!=NULL) && (*aEquivalentIdentifiers)(*identifier, aIdentifier))
110 EXPORT_C TUid CBaNamedPlugins::UidAtIndex(TInt aIndex) const
111 /** Gets the UID of the plug-in at the specified index into the MDesCArray.
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. */
117 return iArrayOfNamedPlugIns[aIndex].iUid;
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.
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. */
127 return iArrayOfNamedPlugIns[aIndex].iIdentifier;
130 EXPORT_C TInt CBaNamedPlugins::MdcaCount() const
131 /** Gets the number of plug-ins in the list.
133 @return The number of plug-ins in the list. */
135 return iArrayOfNamedPlugIns.Count();
138 EXPORT_C TPtrC CBaNamedPlugins::MdcaPoint(TInt aIndex) const
139 /** Returns a TPtrC for the name of the plug-in at the given index.
141 @param aIndex The index into the list. Must be within the bounds of the array,
143 @return The name of the plug-in at the given index. */
145 return *iArrayOfNamedPlugIns[aIndex].iName;
148 CBaNamedPlugins::CBaNamedPlugins(TInt aGranularity)
149 :iArrayOfNamedPlugIns(aGranularity)
153 void CBaNamedPlugins::ConstructL(const CParameters& aParameters)
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)
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))
171 if (aParameters.iFallBackName==NULL)
173 namedPlugIn.iName=TParsePtrC(*resourceFileData.iFullFileName).Name().AllocLC();
177 namedPlugIn.iName=aParameters.iFallBackName->FallBackNameL(*resourceFileData.iFullFileName);
178 CleanupStack::PushL(namedPlugIn.iName);
180 User::LeaveIfError(iArrayOfNamedPlugIns.Append(namedPlugIn));
181 CleanupStack::Pop(namedPlugIn.iName);
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)
190 case TResourceFile::EFormatTbuf:
192 const TPtrC name(REINTERPRET_CAST(const TText*, resource->Ptr()), resource->Length()/sizeof(TText));
195 namedPlugIn.iName=name.AllocLC();
196 User::LeaveIfError(iArrayOfNamedPlugIns.Append(namedPlugIn));
197 CleanupStack::Pop(namedPlugIn.iName);
201 case TResourceFile::EFormatArrayOfUidNamePairs:
203 TResourceReader resourceReader;
204 resourceReader.SetBuffer(resource);
205 for (TInt j=resourceReader.ReadUint16()-1; j>=0; --j)
207 namedPlugIn.iUid=TUid::Uid(resourceReader.ReadUint32());
208 const TPtrC name(resourceReader.ReadTPtrC());
211 namedPlugIn.iName=name.AllocLC();
212 User::LeaveIfError(iArrayOfNamedPlugIns.Append(namedPlugIn));
213 CleanupStack::Pop(namedPlugIn.iName);
219 Panic(EBafPanicBadResourceFileFormat);
222 CleanupStack::PopAndDestroy(resource);
224 if (namedPlugIn.iIdentifier!=NULL)
226 CleanupStack::Pop(namedPlugIn.iIdentifier);
229 CleanupStack::PopAndDestroy(2, fileName);
230 iArrayOfNamedPlugIns.Sort(TLinearOrder<TNamedPlugIn>(CompareNamedPlugIns));
231 if (aParameters.iTextForNone!=NULL)
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)
240 case EArrayPositionFirst:
241 User::LeaveIfError(iArrayOfNamedPlugIns.Insert(namedPlugIn, 0));
243 case EArrayPositionLast:
244 User::LeaveIfError(iArrayOfNamedPlugIns.Append(namedPlugIn));
247 Panic(EBafPanicBadArrayPosition);
250 CleanupStack::Pop(namedPlugIn.iName);
254 TInt CBaNamedPlugins::CompareNamedPlugIns(const TNamedPlugIn& aNamedPlugIn1, const TNamedPlugIn& aNamedPlugIn2)
256 __ASSERT_DEBUG((aNamedPlugIn1.iCompareNames!=NULL) && (aNamedPlugIn1.iCompareNames==aNamedPlugIn2.iCompareNames), Panic(EBafPanicBadCompareNames));
257 return (*aNamedPlugIn1.iCompareNames)(*aNamedPlugIn1.iName, *aNamedPlugIn2.iName);
260 TInt CBaNamedPlugins::DefaultAlgorithmToCompareNames(const TDesC& aName1, const TDesC& aName2)
262 return aName1.CompareC(aName2);
265 // CBaNamedPlugins::MFallBackName
269 EXPORT_C void CBaNamedPlugins::MFallBackName::MFallBackName_Reserved_1()
275 EXPORT_C void CBaNamedPlugins::MFallBackName::MFallBackName_Reserved_2()
279 // CBaNamedPlugins::CParameters
281 EXPORT_C CBaNamedPlugins::CParameters* CBaNamedPlugins::CParameters::NewL(RFs& aFileServerSession, const TArray<TResourceFile>& aArrayOfResourceFiles)
282 /** Allocates and constructs a new parameters object.
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. */
294 CParameters* const parameters=NewLC(aFileServerSession, aArrayOfResourceFiles);
295 CleanupStack::Pop(parameters);
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
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. */
313 CParameters* const parameters=new(ELeave) CParameters(aFileServerSession);
314 CleanupStack::PushL(parameters);
315 parameters->ConstructL(aArrayOfResourceFiles);
320 EXPORT_C CBaNamedPlugins::CParameters::~CParameters()
321 /** Destructor. Deletes all resources owned by the object. */
323 delete iArrayOfResourceFiles;
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.
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). */
337 iFallBackName=&aFallBackName;
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
346 @param aCompareNames A function that compares two plug-in names for sorting. */
348 iCompareNames=aCompareNames;
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.
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. */
362 HBufC* const textForNone=aTextForNone.AllocL();
364 iTextForNone=textForNone;
365 iArrayPositionOfTextForNone=aArrayPositionOfTextForNone;
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.
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. */
383 iTextForNone=aTextForNone;
384 iArrayPositionOfTextForNone=aArrayPositionOfTextForNone;
387 CBaNamedPlugins::CParameters::CParameters(RFs& aFileServerSession)
388 :iFileServerSession(aFileServerSession),
389 iArrayOfResourceFiles(NULL),
393 iArrayPositionOfTextForNone(STATIC_CAST(TArrayPosition, -1))
397 void CBaNamedPlugins::CParameters::ConstructL(const TArray<TResourceFile>& aArrayOfResourceFiles)
399 iArrayOfResourceFiles=new(ELeave) TArray<TResourceFile>(aArrayOfResourceFiles);