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