appfw/apparchitecture/apgrfx/APGICNFL.CPP
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 1997-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 // apgicnfl.cpp
       
    15 //
       
    16 
       
    17 #include <e32std.h>
       
    18 #include <s32file.h>
       
    19 #include <s32ucmp.h>
       
    20 #include <bautils.h>
       
    21 #include <barsread.h>
       
    22 #include <barsc.h>
       
    23 #include <barsread2.h>
       
    24 #include <barsc2.h>
       
    25 #include "APFDEF.H"
       
    26 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    27 #if !defined(__APGICNFL_PARTNER_H__)
       
    28 #include "apgicnflpartner.h"
       
    29 #endif
       
    30 #endif //SYMBIAN_ENABLE_SPLIT_HEADERS
       
    31 
       
    32 
       
    33 #ifdef _DEBUG
       
    34 #include "APGSTD.H" // panic codes
       
    35 #endif // _DEBUG
       
    36 
       
    37 #include "APGICNFL.H"
       
    38 
       
    39 //
       
    40 // Try and reduce the bitmap mask depth to 1bpp (2 colours)
       
    41 //
       
    42 LOCAL_D CFbsBitmap* TryCompressMaskL(const CFbsBitmap& aMask)
       
    43 	{
       
    44 	CFbsBitmap* newMask=NULL;
       
    45 	if (aMask.DisplayMode()!=EGray2 && aMask.IsMonochrome())
       
    46 		{
       
    47 		newMask=new(ELeave) CFbsBitmap;
       
    48 		CleanupStack::PushL(newMask);
       
    49 		const TSize size=aMask.SizeInPixels();
       
    50 		User::LeaveIfError(newMask->Create(size,EGray2));
       
    51 		CFbsBitmapDevice* device=CFbsBitmapDevice::NewL(newMask);
       
    52 		CleanupStack::PushL(device);
       
    53 		CFbsBitGc* gc=NULL;
       
    54 		User::LeaveIfError(device->CreateContext(gc));
       
    55 		CleanupStack::PushL(gc);
       
    56 		TPoint origin(0,0);
       
    57 		gc->BitBlt(origin,&aMask);
       
    58 		CleanupStack::PopAndDestroy(2); // gc, device
       
    59 		CleanupStack::Pop(newMask);
       
    60 		}
       
    61 	return newMask;
       
    62 	}
       
    63 
       
    64 //
       
    65 // Class CApaMaskedBitmap
       
    66 //
       
    67 
       
    68 CApaMaskedBitmap::CApaMaskedBitmap()
       
    69 :CFbsBitmap()
       
    70 	{}
       
    71 
       
    72 EXPORT_C CApaMaskedBitmap::~CApaMaskedBitmap()
       
    73 /** Destructor.
       
    74 
       
    75 Frees resources owned by the object prior to its destruction. */
       
    76 	{
       
    77 	delete iMask;
       
    78 	}
       
    79 
       
    80 EXPORT_C CApaMaskedBitmap* CApaMaskedBitmap::NewLC()
       
    81 /** Creates a default application icon object.
       
    82 
       
    83 @return A pointer to the new application icon object. */
       
    84 	{
       
    85 	CApaMaskedBitmap* self=new(ELeave) CApaMaskedBitmap;
       
    86 	CleanupStack::PushL(self);
       
    87 	self->ConstructL();
       
    88 	return self;
       
    89 	}
       
    90 
       
    91 EXPORT_C CApaMaskedBitmap* CApaMaskedBitmap::NewL(const CApaMaskedBitmap* aSourceIcon)
       
    92 /** Creates a new application icon object, making a duplicate copy of an existing 
       
    93 application icon.
       
    94 
       
    95 @param aSourceIcon A pointer to an existing application icon.
       
    96 @return A pointer to the new application icon object. */
       
    97 	{
       
    98 	CApaMaskedBitmap* self=new(ELeave) CApaMaskedBitmap;
       
    99 	CleanupStack::PushL(self);
       
   100 	self->ConstructL();
       
   101 	User::LeaveIfError( self->iMask->Duplicate(aSourceIcon->Mask()->Handle()) );
       
   102 	User::LeaveIfError( self->Duplicate(aSourceIcon->Handle()) );
       
   103 	CleanupStack::Pop(self);
       
   104 	return self;
       
   105 	}
       
   106 
       
   107 void CApaMaskedBitmap::ConstructL()
       
   108 	{
       
   109 	if (!iFbs)
       
   110 		User::Leave(KErrCouldNotConnect);
       
   111 	
       
   112 	iMask=new(ELeave) CFbsBitmap;
       
   113 	}
       
   114 
       
   115 EXPORT_C CFbsBitmap* CApaMaskedBitmap::Mask() const
       
   116 /** Gets the icon's mask.
       
   117 
       
   118 @return A pointer to the mask bitmap. */
       
   119 	{
       
   120 	return iMask;
       
   121 	}
       
   122 
       
   123 EXPORT_C void CApaMaskedBitmap::InternalizeL(RReadStream& aStream)
       
   124 /** Internalizes the application icon from the read stream.
       
   125 
       
   126 @param aStream The read stream. */
       
   127 	{
       
   128 	__ASSERT_DEBUG(iMask, Panic(EPanicNullPointer));
       
   129 	CFbsBitmap::InternalizeL(aStream);
       
   130 	aStream >> *iMask;
       
   131 
       
   132 	// Try to reduce the colour depth of the bitmap mask
       
   133 	CFbsBitmap* tempMask;
       
   134 	tempMask = TryCompressMaskL(*iMask);
       
   135 	// tempMask = NULL if iMask could not be reduced
       
   136 	if (tempMask != NULL)
       
   137 		{
       
   138 		delete iMask;
       
   139 		iMask = tempMask;
       
   140 		}
       
   141 	}
       
   142 
       
   143 void CApaMaskedBitmap::SetRomBitmapL(TUint8* aRomPointer)
       
   144 	{
       
   145 	__ASSERT_DEBUG(iMask, Panic(EPanicNullPointer));
       
   146 	TInt bitmapSize = 0;
       
   147 	CFbsBitmap::SetRomBitmapL(reinterpret_cast<CBitwiseBitmap*>(aRomPointer),bitmapSize);
       
   148 
       
   149 	aRomPointer += bitmapSize;
       
   150 
       
   151 	iMask->SetRomBitmapL(reinterpret_cast<CBitwiseBitmap*>(aRomPointer),bitmapSize);
       
   152 	}
       
   153 
       
   154 EXPORT_C void CApaMaskedBitmap::ExternalizeL(RWriteStream& aStream) const
       
   155 /** Externalises the application icon to the specified stream.
       
   156 
       
   157 @param aStream The write stream. */
       
   158 	{
       
   159 	__ASSERT_DEBUG(iMask, Panic(EPanicNullPointer));
       
   160 	CFbsBitmap::ExternalizeL(aStream);
       
   161 	aStream << *iMask;
       
   162 	}
       
   163 
       
   164 
       
   165 EXPORT_C void CApaMaskedBitmap::SetMaskBitmap(CFbsBitmap* aMask)
       
   166 /**
       
   167 Sets the icon's mask
       
   168 
       
   169 @publishedAll
       
   170 @released
       
   171 @param     aMask A pointer to the mask bitmap       
       
   172 */
       
   173 	{
       
   174 	delete iMask;
       
   175 	iMask = aMask;
       
   176 	}
       
   177 
       
   178 //
       
   179 // CApaResourceFileWriterBase::MDataSink
       
   180 //
       
   181 
       
   182 void CApaResourceFileWriterBase::MDataSink::WriteBufferL(const TDesC8& aBuffer)
       
   183 	{
       
   184 	if (aBuffer.Length()>0)
       
   185 		{
       
   186 		if (iBufferSinkForCompressedUnicodeFormat!=NULL)
       
   187 			{
       
   188 			WriteInCompressedUnicodeFormatL(aBuffer.Length(), aBuffer, EFalse);
       
   189 			}
       
   190 		else
       
   191 			{
       
   192 			DoWriteBufferL(aBuffer);
       
   193 			iNumberOfBytesWhenUncompressed+=aBuffer.Length();
       
   194 			}
       
   195 		}
       
   196 	}
       
   197 
       
   198 void CApaResourceFileWriterBase::MDataSink::WriteCompressedUnicodeRunL(TInt aNumberOfBytesWhenUncompressed, const TDesC8& aTextAsCompressedUnicode)
       
   199 	{
       
   200 	WriteInCompressedUnicodeFormatL(aNumberOfBytesWhenUncompressed, aTextAsCompressedUnicode, ETrue);
       
   201 	}
       
   202 
       
   203 void CApaResourceFileWriterBase::MDataSink::WriteInCompressedUnicodeFormatL(TInt aNumberOfBytesWhenUncompressed, const TDesC8& aData, TBool aCompressedUnicode)
       
   204 	{
       
   205 	__ASSERT_DEBUG(iBufferSinkForCompressedUnicodeFormat!=NULL, Panic(EPanicWrongResourceFormat1));
       
   206 	const TInt dataLength=aData.Length();
       
   207 	__ASSERT_DEBUG(((dataLength==0) && ((aNumberOfBytesWhenUncompressed==0) || (aNumberOfBytesWhenUncompressed==1))) ||
       
   208 				   ((dataLength>0) && (aNumberOfBytesWhenUncompressed>0)), Panic(EPanicBadCompressedUnicodeRun));
       
   209 	if (dataLength>0)
       
   210 		{
       
   211 		if ((iNumberOfBytesWhenUncompressed==0) && (iBufferSinkForCompressedUnicodeFormat->iNumberOfBytesWhenUncompressed==0))
       
   212 			{
       
   213 			if (aCompressedUnicode)
       
   214 				{
       
   215 				iFlags|=EFlag_InCompressedUnicodeRun;
       
   216 				}
       
   217 			else
       
   218 				{
       
   219 				iFlags&=~EFlag_InCompressedUnicodeRun;
       
   220 				}
       
   221 			}
       
   222 		else if ((iFlags&EFlag_InCompressedUnicodeRun)^(aCompressedUnicode? EFlag_InCompressedUnicodeRun: 0)) // if we're changing the state of the EFlag_InCompressedUnicodeRun flag
       
   223 			{
       
   224 			FlushL(EFalse);
       
   225 			iFlags^=EFlag_InCompressedUnicodeRun; // toggle the EFlag_InCompressedUnicodeRun flag
       
   226 			}
       
   227 		static_cast<MDataSink*>(iBufferSinkForCompressedUnicodeFormat)->DoWriteBufferL(aData);
       
   228 		}
       
   229 	iBufferSinkForCompressedUnicodeFormat->iNumberOfBytesWhenUncompressed+=aNumberOfBytesWhenUncompressed;
       
   230 	}
       
   231 
       
   232 TInt CApaResourceFileWriterBase::MDataSink::NumberOfBytesWhenUncompressed() const
       
   233 	{
       
   234 	TInt numberOfBytesWhenUncompressed=iNumberOfBytesWhenUncompressed;
       
   235 	if (iBufferSinkForCompressedUnicodeFormat!=NULL)
       
   236 		{
       
   237 		numberOfBytesWhenUncompressed+=iBufferSinkForCompressedUnicodeFormat->iNumberOfBytesWhenUncompressed;
       
   238 		}
       
   239 	return numberOfBytesWhenUncompressed;
       
   240 	}
       
   241 
       
   242 CApaResourceFileWriterBase::MDataSink::MDataSink(RBufferSink* aBufferSinkForCompressedUnicodeFormat)
       
   243 	:iNumberOfBytesWhenUncompressed(0),
       
   244 	 iFlags(0),
       
   245 	 iBufferSinkForCompressedUnicodeFormat(aBufferSinkForCompressedUnicodeFormat)
       
   246 	{
       
   247 	}
       
   248 
       
   249 void CApaResourceFileWriterBase::MDataSink::FlushL(TBool aFinalFlush)
       
   250 	{
       
   251 	if (iBufferSinkForCompressedUnicodeFormat!=NULL)
       
   252 		{
       
   253 		RBuf8 run;
       
   254 		CleanupClosePushL(run);
       
   255 		TInt numberOfBytesInRunWhenUncompressed=0;
       
   256 		iBufferSinkForCompressedUnicodeFormat->FlushAndGetAndResetL(numberOfBytesInRunWhenUncompressed, run);
       
   257 		if (numberOfBytesInRunWhenUncompressed>0)
       
   258 			{
       
   259 			if ((iNumberOfBytesWhenUncompressed==0) && ((iFlags&EFlag_InCompressedUnicodeRun)==0))
       
   260 				{
       
   261 				WriteRunLengthL(0); // insert initial zero-length run-length as we're not starting with compressed Unicode
       
   262 				}
       
   263 			__ASSERT_DEBUG(run.Length()>0, Panic(EPanicBadRunLength));
       
   264 			WriteRunLengthL(run.Length());
       
   265 			DoWriteBufferL(run);
       
   266 			iNumberOfBytesWhenUncompressed+=numberOfBytesInRunWhenUncompressed;
       
   267 			}
       
   268 		CleanupStack::PopAndDestroy(&run);
       
   269 		if (aFinalFlush && (iNumberOfBytesWhenUncompressed==0))
       
   270 			{
       
   271 			WriteRunLengthL(0); // write a zero-length run-length as the resource is completely empty
       
   272 			}
       
   273 		}
       
   274 	}
       
   275 
       
   276 void CApaResourceFileWriterBase::MDataSink::Reset(TInt& aNumberOfBytesWhenUncompressed)
       
   277 	{
       
   278 	aNumberOfBytesWhenUncompressed=iNumberOfBytesWhenUncompressed;
       
   279 	iNumberOfBytesWhenUncompressed=0;
       
   280 	iFlags=0;
       
   281 	}
       
   282 
       
   283 void CApaResourceFileWriterBase::MDataSink::WriteRunLengthL(TInt aRunLength)
       
   284 	{
       
   285 	__ASSERT_DEBUG((aRunLength&~0x7fff)==0, Panic(EPanicBadRunLengthParameter));
       
   286 	__ASSERT_DEBUG(CompressedUnicodeFormat(), Panic(EPanicWrongResourceFormat2));
       
   287 	TBuf8<2> buffer;
       
   288 	if (aRunLength&~0x7f)
       
   289 		{
       
   290 		buffer.Append((aRunLength>>8)|0x80);
       
   291 		}
       
   292 	buffer.Append(aRunLength&0xff);
       
   293 	DoWriteBufferL(buffer); // must call DoWriteBufferL here (not WriteBufferL) so that iNumberOfBytesWhenUncompressed is not altered
       
   294 	}
       
   295 
       
   296 // CApaResourceFileWriterBase::RBufferSink
       
   297 
       
   298 CApaResourceFileWriterBase::RBufferSink::RBufferSink(RBufferSink* aBufferSinkForCompressedUnicodeFormat)
       
   299 	:MDataSink(aBufferSinkForCompressedUnicodeFormat)
       
   300 	{
       
   301 	}
       
   302 
       
   303 void CApaResourceFileWriterBase::RBufferSink::ConstructLC()
       
   304 	{
       
   305 	iBuffer.CreateL(20);
       
   306 	CleanupClosePushL(*this);
       
   307 	}
       
   308 
       
   309 void CApaResourceFileWriterBase::RBufferSink::Close()
       
   310 	{
       
   311 	iBuffer.Close();
       
   312 	}
       
   313 
       
   314 void CApaResourceFileWriterBase::RBufferSink::FlushAndGetAndResetL(TInt& aNumberOfBytesWhenUncompressed, RBuf8& aBuffer)
       
   315 	{
       
   316 	FlushL(ETrue);
       
   317 	iBuffer.Swap(aBuffer);
       
   318 	Reset(aNumberOfBytesWhenUncompressed);
       
   319 	}
       
   320 
       
   321 void CApaResourceFileWriterBase::RBufferSink::DoWriteBufferL(const TDesC8& aBuffer)
       
   322 	{
       
   323 	if (iBuffer.MaxLength()-iBuffer.Length()<aBuffer.Length())
       
   324 		{
       
   325 		iBuffer.ReAllocL(iBuffer.Length()+aBuffer.Length()+20);
       
   326 		}
       
   327 	iBuffer.Append(aBuffer);
       
   328 	}
       
   329 
       
   330 // CApaResourceFileWriterBase
       
   331 
       
   332 CApaResourceFileWriterBase::CApaResourceFileWriterBase()
       
   333 	{
       
   334 	}
       
   335 
       
   336 void CApaResourceFileWriterBase::DoGenerateFileContentsL(RBuf8& aBuffer, TUid aUid2, TUid aUid3) const
       
   337 	{
       
   338 	TInt mainResourceSizeInBytesWhenUncompressed=0;
       
   339 	RBuf8 mainResourceInFormatContainingCompressedUnicode;
       
   340 	MainResourceInCompiledFormatLC(mainResourceSizeInBytesWhenUncompressed, mainResourceInFormatContainingCompressedUnicode, ETrue);
       
   341 	TInt temp=0;
       
   342 	RBuf8 mainResourceInFormatNotContainingCompressedUnicode;
       
   343 	MainResourceInCompiledFormatLC(temp, mainResourceInFormatNotContainingCompressedUnicode, EFalse);
       
   344 	__ASSERT_DEBUG(temp==mainResourceSizeInBytesWhenUncompressed, Panic(EPanicInconsistentResourceSizeInBytes));
       
   345 	TBool mainResourceInCompressedUnicodeFormat=ETrue;
       
   346 	TPtrC8 mainResourceInSmallestFormat(mainResourceInFormatContainingCompressedUnicode);
       
   347 	if (mainResourceInSmallestFormat.Length()>=mainResourceInFormatNotContainingCompressedUnicode.Length())
       
   348 		{
       
   349 		mainResourceInCompressedUnicodeFormat=EFalse;
       
   350 		mainResourceInSmallestFormat.Set(mainResourceInFormatNotContainingCompressedUnicode);
       
   351 		}
       
   352 	TBool secondResourceInCompressedUnicodeFormat=EFalse;
       
   353 	const TDesC8* const secondResource=SecondResourceL(secondResourceInCompressedUnicodeFormat);
       
   354 	RBufferSink bufferSink(NULL);
       
   355 	bufferSink.ConstructLC();
       
   356 
       
   357 	WriteUidTypeL(bufferSink, aUid2, aUid3);
       
   358 	WriteUint8L(bufferSink, 0); // flags
       
   359 	WriteLittleEndianUint16L(bufferSink, (secondResource==NULL)? mainResourceSizeInBytesWhenUncompressed: Max(mainResourceSizeInBytesWhenUncompressed, secondResource->Length())); // the size in bytes of the largest resource in the file when uncompressed
       
   360 	TUint bitArray=0; // bit-array (one per top-level resource) indicating whether each top-level resource contains compressed unicode or not
       
   361 	if (mainResourceInCompressedUnicodeFormat)
       
   362 		{
       
   363 		bitArray|=0x01;
       
   364 		}
       
   365 	if (secondResourceInCompressedUnicodeFormat)
       
   366 		{
       
   367 		bitArray|=0x02;
       
   368 		}
       
   369 	WriteUint8L(bufferSink, bitArray);
       
   370 	WriteBufferL(bufferSink, mainResourceInSmallestFormat);
       
   371 	if (secondResource!=NULL)
       
   372 		{
       
   373 		WriteBufferL(bufferSink, *secondResource);
       
   374 		}
       
   375 	TInt filePosition=16+1+2+1;
       
   376 	WriteLittleEndianUint16L(bufferSink, filePosition);
       
   377 	filePosition+=mainResourceInSmallestFormat.Length();
       
   378 	WriteLittleEndianUint16L(bufferSink, filePosition);
       
   379 	if (secondResource!=NULL)
       
   380 		{
       
   381 		filePosition+=secondResource->Length();
       
   382 		WriteLittleEndianUint16L(bufferSink, filePosition);
       
   383 		}
       
   384 	TInt notUsed;
       
   385 	bufferSink.FlushAndGetAndResetL(notUsed, aBuffer);
       
   386 
       
   387 	CleanupStack::PopAndDestroy(3, &mainResourceInFormatContainingCompressedUnicode);
       
   388 	}
       
   389 
       
   390 void CApaResourceFileWriterBase::WriteUidTypeL(MDataSink& aDataSink, TUid aUid2, TUid aUid3) const
       
   391 	{
       
   392 	aDataSink.WriteBufferL(TCheckedUid(TUidType(TUid::Uid(0x101f4a6b), aUid2, aUid3)).Des());
       
   393 	}
       
   394 
       
   395 void CApaResourceFileWriterBase::WriteTextL(MDataSink& aDataSink, const TDesC& aText) const
       
   396 	{
       
   397 	// LTEXT
       
   398 
       
   399 	WriteUint8L(aDataSink, aText.Length());
       
   400 	if (!aDataSink.CompressedUnicodeFormat())
       
   401 		{
       
   402 		if ((aDataSink.NumberOfBytesWhenUncompressed()%2)!=0)
       
   403 			{
       
   404 			WriteUint8L(aDataSink, 0xab);
       
   405 			}
       
   406 		const TInt lengthOfTextInBytes=aText.Size();
       
   407 		WriteBufferL(aDataSink, TPtrC8(reinterpret_cast<const TUint8*>(aText.Ptr()), lengthOfTextInBytes));
       
   408 		}
       
   409 	else
       
   410 		{
       
   411 		TInt numberOfBytesWhenUncompressed=aText.Size();
       
   412 		if ((aDataSink.NumberOfBytesWhenUncompressed()%2)!=0)
       
   413 			{
       
   414 			++numberOfBytesWhenUncompressed; // for the padding-byte 0xab when it's uncompressed
       
   415 			}
       
   416 		HBufC8* const textAsCompressedUnicode=AsCompressedUnicodeLC(aText);
       
   417 		aDataSink.WriteCompressedUnicodeRunL(numberOfBytesWhenUncompressed, *textAsCompressedUnicode);
       
   418 		CleanupStack::PopAndDestroy(textAsCompressedUnicode);
       
   419 		}
       
   420 	}
       
   421 
       
   422 void CApaResourceFileWriterBase::WriteText8L(MDataSink& aDataSink, const TDesC8& aText8) const
       
   423 	{
       
   424 	// LTEXT8
       
   425 
       
   426 	WriteUint8L(aDataSink, aText8.Length());
       
   427 	WriteBufferL(aDataSink, aText8);
       
   428 	}
       
   429 
       
   430 void CApaResourceFileWriterBase::WriteUint8L(MDataSink& aDataSink, TUint aUint8) const
       
   431 	{
       
   432 	TBuf8<1> buffer;
       
   433 	buffer.Append(aUint8&0xff);
       
   434 	aDataSink.WriteBufferL(buffer);
       
   435 	}
       
   436 
       
   437 void CApaResourceFileWriterBase::WriteLittleEndianUint16L(MDataSink& aDataSink, TUint aUint16) const
       
   438 	{
       
   439 	TBuf8<2> buffer;
       
   440 	buffer.Append(aUint16&0xff);
       
   441 	buffer.Append((aUint16>>8)&0xff);
       
   442 	aDataSink.WriteBufferL(buffer);
       
   443 	}
       
   444 
       
   445 void CApaResourceFileWriterBase::WriteLittleEndianUint32L(MDataSink& aDataSink, TUint aUint32) const
       
   446 	{
       
   447 	TBuf8<4> buffer;
       
   448 	buffer.Append(aUint32&0xff);
       
   449 	buffer.Append((aUint32>>8)&0xff);
       
   450 	buffer.Append((aUint32>>16)&0xff);
       
   451 	buffer.Append((aUint32>>24)&0xff);
       
   452 	aDataSink.WriteBufferL(buffer);
       
   453 	}
       
   454 
       
   455 void CApaResourceFileWriterBase::WriteBufferL(MDataSink& aDataSink, const TDesC8& aBuffer) const
       
   456 	{
       
   457 	aDataSink.WriteBufferL(aBuffer);
       
   458 	}
       
   459 
       
   460 HBufC8* CApaResourceFileWriterBase::AsCompressedUnicodeLC(const TDesC& aUncompressedUnicode)
       
   461 	{
       
   462 	TUnicodeCompressor unicodeCompressor;
       
   463 	TMemoryUnicodeSource decompressedUnicode1(aUncompressedUnicode.Ptr());
       
   464 	TMemoryUnicodeSource decompressedUnicode2(aUncompressedUnicode.Ptr());
       
   465 	
       
   466 	// Create a buffer big enough to hold all the compressed output
       
   467 	TInt compressedUnicodeSizeInBytes = TUnicodeCompressor::CompressedSizeL(decompressedUnicode1, aUncompressedUnicode.Length());
       
   468 	HBufC8* const compressedUnicodeBuffer=HBufC8::NewLC(compressedUnicodeSizeInBytes);
       
   469 	TUint8* const compressedUnicodeBuffer_asBytePointer=const_cast<TUint8*>(compressedUnicodeBuffer->Ptr());
       
   470 	
       
   471 	// Compress the Unicode string
       
   472 	TInt numberOfInputElementsConsumed = 0;
       
   473 	TInt numberOfOutputBytes = 0;
       
   474 	unicodeCompressor.CompressL(compressedUnicodeBuffer_asBytePointer, decompressedUnicode2, compressedUnicodeSizeInBytes, aUncompressedUnicode.Length(), &numberOfOutputBytes, &numberOfInputElementsConsumed);
       
   475 	TInt temp = 0;
       
   476 	unicodeCompressor.FlushL(compressedUnicodeBuffer_asBytePointer, compressedUnicodeSizeInBytes, temp);
       
   477 	numberOfOutputBytes+=temp;
       
   478 	TPtr8 compressedUnicodeBuffer_asWritable(compressedUnicodeBuffer->Des());
       
   479 	compressedUnicodeBuffer_asWritable.SetLength(numberOfOutputBytes);
       
   480 	return compressedUnicodeBuffer;
       
   481 	}
       
   482 
       
   483 void CApaResourceFileWriterBase::MainResourceInCompiledFormatLC(TInt& aMainResourceSizeInBytesWhenUncompressed, RBuf8& aBuffer, TBool aCompressedUnicodeFormat) const
       
   484 	{
       
   485 	CleanupClosePushL(aBuffer);
       
   486 	RBufferSink bufferSinkForCompressedUnicodeFormat(NULL);
       
   487 	if (aCompressedUnicodeFormat)
       
   488 		{
       
   489 		bufferSinkForCompressedUnicodeFormat.ConstructLC();
       
   490 		}
       
   491 	RBufferSink bufferSink(aCompressedUnicodeFormat? &bufferSinkForCompressedUnicodeFormat: NULL);
       
   492 	bufferSink.ConstructLC();
       
   493 	MainResourceInCompiledFormatL(bufferSink);
       
   494 	bufferSink.FlushAndGetAndResetL(aMainResourceSizeInBytesWhenUncompressed, aBuffer);
       
   495 	CleanupStack::PopAndDestroy(&bufferSink);
       
   496 	if (aCompressedUnicodeFormat)
       
   497 		{
       
   498 		CleanupStack::PopAndDestroy(&bufferSinkForCompressedUnicodeFormat);
       
   499 		}
       
   500 	}
       
   501 
       
   502 // CApaRegistrationResourceFileWriter
       
   503 
       
   504 /** 
       
   505 Creates a new CApaRegistrationResourceFileWriter instance.
       
   506 
       
   507 @param aAppUid The UID of the application.
       
   508 @param aAppFile The name and extension of the file to generate.
       
   509 @param aAttributes The attributes of the application. See the TApaAppCapability class for more details.
       
   510 @return A pointer to the new CApaRegistrationResourceFileWriter instance.
       
   511 
       
   512 @publishedPartner
       
   513 @released
       
   514 */
       
   515 EXPORT_C CApaRegistrationResourceFileWriter* CApaRegistrationResourceFileWriter::NewL(TUid aAppUid, const TDesC& aAppFile, TUint aAttributes)
       
   516 // aAppFile does not need to have the drive set
       
   517 	{ // static
       
   518 	CApaRegistrationResourceFileWriter* const self=new(ELeave) CApaRegistrationResourceFileWriter(aAppUid, aAttributes);
       
   519 	CleanupStack::PushL(self);
       
   520 	self->ConstructL(aAppFile);
       
   521 	CleanupStack::Pop(self);
       
   522 	return self;
       
   523 	}
       
   524 
       
   525 /** 
       
   526 The destructor for the CApaRegistrationResourceFileWriter class.
       
   527 
       
   528 @publishedPartner
       
   529 @released
       
   530 */
       
   531 EXPORT_C CApaRegistrationResourceFileWriter::~CApaRegistrationResourceFileWriter()
       
   532 	{
       
   533 	delete iAppFile;
       
   534 	delete iLocalisableResourceFile;
       
   535 	delete iGroupName;
       
   536 	delete iOpaqueData;
       
   537 
       
   538 	TInt i;
       
   539 	for (i=iDataTypeList.Count()-1; i>=0; --i)
       
   540 		{
       
   541 		delete iDataTypeList[i].iType;
       
   542 		}
       
   543 	iDataTypeList.Close();
       
   544 
       
   545 	for (i=iFileOwnershipList.Count()-1; i>=0; --i)
       
   546 		{
       
   547 		delete iFileOwnershipList[i].iFileName;
       
   548 		}
       
   549 	iFileOwnershipList.Close();
       
   550 	}
       
   551 
       
   552 TUid CApaRegistrationResourceFileWriter::AppUid() const
       
   553 /** @internalComponent */
       
   554 	{
       
   555 	return iAppUid;
       
   556 	}
       
   557 
       
   558 const TUid KUidAppRegistrationFile ={0x101F8021};
       
   559 
       
   560 void CApaRegistrationResourceFileWriter::GenerateFileContentsL(RBuf8& aBuffer) const
       
   561 /** @internalComponent */
       
   562 	{
       
   563 	DoGenerateFileContentsL(aBuffer, KUidAppRegistrationFile, iAppUid);
       
   564 	}
       
   565 
       
   566 void CApaRegistrationResourceFileWriter::SetLocalisableResourceFileL(const TDesC& aLocalisableResourceFile)
       
   567 /** @internalComponent */
       
   568 	{
       
   569 	HBufC* const localisableResourceFile=aLocalisableResourceFile.AllocL();
       
   570 	delete iLocalisableResourceFile;
       
   571 	iLocalisableResourceFile=localisableResourceFile;
       
   572 	}
       
   573 
       
   574 /** 
       
   575 Sets or clears the hidden attribute. The usual purpose of the hidden attribute is to 
       
   576 decide if the application should appear in the task list or not but this could vary between
       
   577 products.
       
   578 
       
   579 @param aAppIsHidden The value of the hidden flag.
       
   580 
       
   581 @publishedPartner
       
   582 @released
       
   583 */
       
   584 EXPORT_C void CApaRegistrationResourceFileWriter::SetAppIsHiddenL(TBool aAppIsHidden)
       
   585 	{
       
   586 	iAppIsHidden=aAppIsHidden? 1: 0;
       
   587 	}
       
   588 
       
   589 /** 
       
   590 Sets the embeddability attribute. See the TApaAppCapability::TEmbeddability class for more details.
       
   591 
       
   592 @param aEmbeddability The value of the embeddability flags.
       
   593 
       
   594 @publishedPartner
       
   595 @released
       
   596 */
       
   597 EXPORT_C void CApaRegistrationResourceFileWriter::SetEmbeddabilityL(TApaAppCapability::TEmbeddability aEmbeddability)
       
   598 	{
       
   599 	iEmbeddability=aEmbeddability;
       
   600 	}
       
   601 
       
   602 /** 
       
   603 Specifies if the application supports the creation of a new file or not.
       
   604 
       
   605 @param aSupportsNewFile ETrue to specify that the application supports the creation of a new file.
       
   606 
       
   607 @publishedPartner
       
   608 @released
       
   609 */
       
   610 EXPORT_C void CApaRegistrationResourceFileWriter::SetSupportsNewFileL(TBool aSupportsNewFile)
       
   611 	{
       
   612 	iSupportsNewFile=aSupportsNewFile? 1: 0;
       
   613 	}
       
   614 
       
   615 /** 
       
   616 Specifies if the application must be launched in the background.
       
   617 
       
   618 @param aLaunchInBackground ETrue if the application must be launched in the background, EFalse otherwise.
       
   619 
       
   620 @publishedPartner
       
   621 @released
       
   622 */
       
   623 EXPORT_C void CApaRegistrationResourceFileWriter::SetLaunchInBackgroundL(TBool aLaunchInBackground)
       
   624 	{
       
   625 	iLaunchInBackground=aLaunchInBackground;
       
   626 	}
       
   627 
       
   628 /** 
       
   629 Sets the name of the application group.
       
   630 
       
   631 @param aGroupName The name of the application group.
       
   632 
       
   633 @publishedPartner
       
   634 @released
       
   635 */
       
   636 EXPORT_C void CApaRegistrationResourceFileWriter::SetGroupNameL(const TDesC& aGroupName)
       
   637 	{
       
   638 	HBufC* const groupName=aGroupName.AllocL();
       
   639 	delete iGroupName;
       
   640 	iGroupName=groupName;
       
   641 	}
       
   642 
       
   643 /** 
       
   644 Sets the default screen number. This can be used to specify the preferred screen on devices
       
   645 that support more than one screen.
       
   646 
       
   647 @param aDefaultScreenNumber The name of the default screen.
       
   648 
       
   649 @publishedPartner
       
   650 @released
       
   651 */
       
   652 EXPORT_C void CApaRegistrationResourceFileWriter::SetDefaultScreenNumberL(TInt aDefaultScreenNumber)
       
   653 	{
       
   654 	iDefaultScreenNumber=aDefaultScreenNumber;
       
   655 	}
       
   656 
       
   657 /** 
       
   658 Sets the opaque data. The opaque data is some data that is specific to the type of application.
       
   659 
       
   660 @publishedPartner
       
   661 @released
       
   662 */
       
   663 EXPORT_C void CApaRegistrationResourceFileWriter::SetOpaqueDataL(const TDesC8& aOpaqueData)
       
   664 	{
       
   665 	HBufC8* const opaqueData=aOpaqueData.AllocL();
       
   666 	delete iOpaqueData;
       
   667 	iOpaqueData=opaqueData;
       
   668 	}
       
   669 
       
   670 /** 
       
   671 Adds a datatype to the list of datatypes that the application can handle.
       
   672 
       
   673 @param aPriority The priority.
       
   674 @param aType The datatype.
       
   675 
       
   676 @publishedPartner
       
   677 @released
       
   678 */
       
   679 EXPORT_C void CApaRegistrationResourceFileWriter::AddDataTypeL(TInt aPriority, const TDesC8& aType)
       
   680 	{
       
   681 	SDataType dataType;
       
   682 	dataType.iPriority=aPriority;
       
   683 	dataType.iType=aType.AllocLC();
       
   684 	iDataTypeList.AppendL(dataType);
       
   685 	CleanupStack::Pop(dataType.iType);
       
   686 	}
       
   687 
       
   688 /** 
       
   689 Adds a file to the list of files owned by the CApaRegistrationResourceFileWriter instances. These files
       
   690 are deleted if an error occurs whil registering the new applications.
       
   691 
       
   692 @param aFileName The name of the file.
       
   693 
       
   694 @publishedPartner
       
   695 @released
       
   696 */
       
   697 EXPORT_C void CApaRegistrationResourceFileWriter::AddFileOwnershipInfoL(const TDesC& aFileName)
       
   698 	{
       
   699 	SFileOwnershipInfo fileOwnershipInfo;
       
   700 	fileOwnershipInfo.iFileName=aFileName.AllocLC();
       
   701 	iFileOwnershipList.AppendL(fileOwnershipInfo);
       
   702 	CleanupStack::Pop(fileOwnershipInfo.iFileName);
       
   703 	}
       
   704 
       
   705 CApaRegistrationResourceFileWriter::CApaRegistrationResourceFileWriter(TUid aAppUid, TUint aAttributes)
       
   706 	:iAppUid(aAppUid),
       
   707 	 iAppFile(NULL),
       
   708 	 iAttributes(aAttributes),
       
   709 	 iLocalisableResourceFile(NULL),
       
   710 	 iAppIsHidden(0),
       
   711 	 iEmbeddability(TApaAppCapability::ENotEmbeddable),
       
   712 	 iSupportsNewFile(0),
       
   713 	 iLaunchInBackground(0),
       
   714 	 iGroupName(NULL),
       
   715 	 iDefaultScreenNumber(0),
       
   716 	 iOpaqueData(NULL)
       
   717 	{
       
   718 	}
       
   719 
       
   720 void CApaRegistrationResourceFileWriter::ConstructL(const TDesC& aAppFile)
       
   721 	{
       
   722 	iAppFile=aAppFile.AllocL();
       
   723 	iLocalisableResourceFile=NULL;
       
   724 	}
       
   725 
       
   726 void CApaRegistrationResourceFileWriter::WriteDataTypeL(MDataSink& aDataSink, const SDataType& aDataType) const
       
   727 	{
       
   728 	// DATATYPE
       
   729 
       
   730 	// LONG priority
       
   731 	WriteLittleEndianUint32L(aDataSink, aDataType.iPriority);
       
   732 
       
   733 	// LTEXT8 type(KMaxDataTypeLength)
       
   734 	WriteText8L(aDataSink, *aDataType.iType);
       
   735 	}
       
   736 
       
   737 void CApaRegistrationResourceFileWriter::WriteFileOwnershipInfoL(MDataSink& aDataSink, const SFileOwnershipInfo& aFileOwnershipInfo) const
       
   738 	{
       
   739 	// FILE_OWNERSHIP_INFO
       
   740 
       
   741 	// LTEXT file_name(KMaxFileNameLength)
       
   742 	WriteTextL(aDataSink, *aFileOwnershipInfo.iFileName);
       
   743 	}
       
   744 
       
   745 void CApaRegistrationResourceFileWriter::MainResourceInCompiledFormatL(MDataSink& aDataSink) const
       
   746 	{
       
   747 	// APP_REGISTRATION_INFO
       
   748 
       
   749 	// LONG reserved_long = 0
       
   750 	WriteLittleEndianUint32L(aDataSink, 0);
       
   751 
       
   752 	// LLINK reserved_llink = 0
       
   753 	WriteLittleEndianUint32L(aDataSink, 0);
       
   754 	__ASSERT_DEBUG(iAppFile, Panic(EPanicNullPointer));
       
   755 	// LTEXT app_file(KMaxFileNameLength) = ""
       
   756 	WriteTextL(aDataSink, *iAppFile);
       
   757 
       
   758 	// LONG attributes = 0
       
   759 	WriteLittleEndianUint32L(aDataSink, iAttributes);
       
   760 
       
   761 	// LTEXT localisable_resource_file(KMaxFileNameLength) = ""
       
   762 	TPtrC localisableResourceFile(KNullDesC);
       
   763 	if (iLocalisableResourceFile!=NULL)
       
   764 		{
       
   765 		localisableResourceFile.Set(*iLocalisableResourceFile);
       
   766 		}
       
   767 	WriteTextL(aDataSink, localisableResourceFile);
       
   768 
       
   769 	// LONG localisable_resource_id = 1
       
   770 	WriteLittleEndianUint32L(aDataSink, 1);
       
   771 
       
   772 	// BYTE hidden = KAppNotHidden
       
   773 	WriteUint8L(aDataSink, iAppIsHidden);
       
   774 
       
   775 	// BYTE embeddability = KAppNotEmbeddable
       
   776 	WriteUint8L(aDataSink, iEmbeddability);
       
   777 
       
   778 	// BYTE newfile = KAppDoesNotSupportNewFile
       
   779 	WriteUint8L(aDataSink, iSupportsNewFile);
       
   780 
       
   781 	// BYTE launch = KAppLaunchInForeground
       
   782 	WriteUint8L(aDataSink, iLaunchInBackground);
       
   783 
       
   784 	// LTEXT group_name(KAppMaxGroupName) = ""
       
   785 	TPtrC groupName(KNullDesC);
       
   786 	if (iGroupName!=NULL)
       
   787 		{
       
   788 		groupName.Set(*iGroupName);
       
   789 		}
       
   790 	WriteTextL(aDataSink, groupName);
       
   791 
       
   792 	// BYTE default_screen_number = 0
       
   793 	WriteUint8L(aDataSink, iDefaultScreenNumber);
       
   794 
       
   795 	// LEN WORD STRUCT datatype_list[]
       
   796 	TInt i;
       
   797 	const TInt numberOfDataTypes=iDataTypeList.Count();
       
   798 	WriteLittleEndianUint16L(aDataSink, numberOfDataTypes);
       
   799 	for (i=0; i<numberOfDataTypes; ++i)
       
   800 		{
       
   801 		WriteDataTypeL(aDataSink, iDataTypeList[i]);
       
   802 		}
       
   803 
       
   804 	// LEN WORD STRUCT file_ownership_list[]
       
   805 	const TInt numberOfOwnershipItems=iFileOwnershipList.Count();
       
   806 	WriteLittleEndianUint16L(aDataSink, numberOfOwnershipItems);
       
   807 	for (i=0; i<numberOfOwnershipItems; ++i)
       
   808 		{
       
   809 		WriteFileOwnershipInfoL(aDataSink, iFileOwnershipList[i]);
       
   810 		}
       
   811 
       
   812 	// LEN WORD STRUCT service_list[]
       
   813 	WriteLittleEndianUint16L(aDataSink, 0);
       
   814 
       
   815 	// LLINK opaque_data = 0
       
   816 	WriteLittleEndianUint32L(aDataSink, (iOpaqueData!=NULL)? 2: 0);
       
   817 	}
       
   818 
       
   819 const TDesC8* CApaRegistrationResourceFileWriter::SecondResourceL(TBool& aSecondResourceInCompressedUnicodeFormat) const
       
   820 	{
       
   821 	aSecondResourceInCompressedUnicodeFormat=EFalse;
       
   822 	return iOpaqueData;
       
   823 	}
       
   824 
       
   825 // CApaLocalisableResourceFileWriter
       
   826 
       
   827 EXPORT_C CApaLocalisableResourceFileWriter* CApaLocalisableResourceFileWriter::NewL(const TDesC& aShortCaption, const TDesC& aCaption, TInt aNumberOfIcons, const TDesC& aGroupName)
       
   828 	{ // static
       
   829 	CApaLocalisableResourceFileWriter* const self=new(ELeave) CApaLocalisableResourceFileWriter(aNumberOfIcons);
       
   830 	CleanupStack::PushL(self);
       
   831 	self->ConstructL(aShortCaption, aCaption, aGroupName);
       
   832 	CleanupStack::Pop(self);
       
   833 	return self;
       
   834 	}
       
   835 
       
   836 EXPORT_C CApaLocalisableResourceFileWriter::~CApaLocalisableResourceFileWriter()
       
   837 	{
       
   838 	delete iShortCaption;
       
   839 	delete iCaptionAndIcon.iCaption;
       
   840 	delete iCaptionAndIcon.iIconFile;
       
   841 	delete iGroupName;
       
   842 	}
       
   843 
       
   844 void CApaLocalisableResourceFileWriter::GenerateFileContentsL(RBuf8& aBuffer) const
       
   845 /** @internalComponent */
       
   846 	{
       
   847 	DoGenerateFileContentsL(aBuffer, TUid::Null(), TUid::Null());
       
   848 	}
       
   849 
       
   850 void CApaLocalisableResourceFileWriter::SetIconFileL(const TDesC& aIconFile)
       
   851 /** @internalComponent */
       
   852 	{
       
   853 	HBufC* const iconFile=aIconFile.AllocL();
       
   854 	delete iCaptionAndIcon.iIconFile;
       
   855 	iCaptionAndIcon.iIconFile=iconFile;
       
   856 	}
       
   857 
       
   858 CApaLocalisableResourceFileWriter::CApaLocalisableResourceFileWriter(TInt aNumberOfIcons)
       
   859 	:iShortCaption(NULL),
       
   860 	 iGroupName(NULL)
       
   861 	{
       
   862 	iCaptionAndIcon.iCaption=NULL;
       
   863 	iCaptionAndIcon.iNumberOfIcons=aNumberOfIcons;
       
   864 	iCaptionAndIcon.iIconFile=NULL;
       
   865 	}
       
   866 
       
   867 void CApaLocalisableResourceFileWriter::ConstructL(const TDesC& aShortCaption, const TDesC& aCaption, const TDesC& aGroupName)
       
   868 	{
       
   869 	iShortCaption=aShortCaption.AllocL();
       
   870 	iCaptionAndIcon.iCaption=aCaption.AllocL();
       
   871 	iCaptionAndIcon.iIconFile=NULL;
       
   872 	iGroupName=aGroupName.AllocL();
       
   873 	}
       
   874 
       
   875 void CApaLocalisableResourceFileWriter::WriteCaptionAndIconInfoL(MDataSink& aDataSink, const SCaptionAndIconInfo& aCaptionAndIcon) const
       
   876 	{
       
   877 	// CAPTION_AND_ICON_INFO
       
   878 
       
   879 	// LONG reserved_long = 0
       
   880 	WriteLittleEndianUint32L(aDataSink, 0);
       
   881 
       
   882 	// LLINK reserved_llink = 0
       
   883 	WriteLittleEndianUint32L(aDataSink, 0);
       
   884 
       
   885 	// LTEXT caption(KMaxCaption) = ""
       
   886 	WriteTextL(aDataSink, *aCaptionAndIcon.iCaption);
       
   887 
       
   888 	// WORD number_of_icons = 0
       
   889 	WriteLittleEndianUint16L(aDataSink, aCaptionAndIcon.iNumberOfIcons);
       
   890 
       
   891 	// LTEXT icon_file(KMaxFileNameLength) = ""
       
   892 	TPtrC iconFile(KNullDesC);
       
   893 	if (aCaptionAndIcon.iIconFile!=NULL)
       
   894 		{
       
   895 		iconFile.Set(*aCaptionAndIcon.iIconFile);
       
   896 		}
       
   897 	WriteTextL(aDataSink, iconFile);
       
   898 	}
       
   899 
       
   900 void CApaLocalisableResourceFileWriter::MainResourceInCompiledFormatL(MDataSink& aDataSink) const
       
   901 	{
       
   902 	// LOCALISABLE_APP_INFO
       
   903 
       
   904 	// LONG reserved_long = 0
       
   905 	WriteLittleEndianUint32L(aDataSink, 0);
       
   906 
       
   907 	// LLINK reserved_llink = 0
       
   908 	WriteLittleEndianUint32L(aDataSink, 0);
       
   909 	__ASSERT_DEBUG(iShortCaption, Panic(EPanicNullPointer));
       
   910 	// LTEXT short_caption(KMaxCaption) = ""
       
   911 	WriteTextL(aDataSink, *iShortCaption);
       
   912 
       
   913 	// STRUCT caption_and_icon
       
   914 	WriteCaptionAndIconInfoL(aDataSink, iCaptionAndIcon);
       
   915 
       
   916 	// LEN WORD STRUCT view_list[]
       
   917 	WriteLittleEndianUint16L(aDataSink, 0);
       
   918 	__ASSERT_DEBUG(iGroupName, Panic(EPanicNullPointer));
       
   919 	//	LTEXT group_name(KAppMaxGroupName) = ""
       
   920 	WriteTextL(aDataSink, *iGroupName);
       
   921 	}
       
   922 
       
   923 const TDesC8* CApaLocalisableResourceFileWriter::SecondResourceL(TBool& aSecondResourceInCompressedUnicodeFormat) const
       
   924 	{
       
   925 	aSecondResourceInCompressedUnicodeFormat=EFalse;
       
   926 	return NULL;
       
   927 	}
       
   928