os/graphics/graphicsdeviceinterface/gdi/sgdi/PRINTGDI.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/graphicsdeviceinterface/gdi/sgdi/PRINTGDI.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,906 @@
     1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <gdi.h>
    1.20 +#include <f32file.h>
    1.21 +#include <s32file.h>
    1.22 +#include <bautils.h>
    1.23 +#include <barsc.h>
    1.24 +#include <barsread.h>
    1.25 +#include "GDIPANIC.h"
    1.26 +
    1.27 +_LIT(KPdrExtension,"*.PDR"); // must be capitalized
    1.28 +_LIT(KPdlExtension,".PDL"); // must be capitalized
    1.29 +_LIT(KUdlExtension,".UDL"); // must be capitalized
    1.30 +_LIT(KRscExtension,".RSC");
    1.31 +_LIT(KGdiPrintPanic,"GDI - PRINT");
    1.32 +
    1.33 +_LIT(KGDIPanicDesc1, "GDI Pdr internal Panic %S, in file %S @ line %i");
    1.34 +_LIT(KGDIPanicDesc2, "Assert condition = \"%S\"");
    1.35 +
    1.36 +enum TPdrStorePanic
    1.37 +	{
    1.38 +	EPdrModelIndexOutOfRange,
    1.39 +	EPdrModelUidNotFound,
    1.40 +	EPdrDirectoryIndexOutOfRange,
    1.41 +	EPdrFileIndexOutOfRange,
    1.42 +	EPdrPrinterDeviceExists,
    1.43 +	EPdrPrinterDeviceDoesNotExist,
    1.44 +	};
    1.45 +
    1.46 +void Panic(TPdrStorePanic aPanic)
    1.47 +	{
    1.48 +	User::Panic(KGdiPrintPanic,aPanic);
    1.49 +	}
    1.50 +	
    1.51 +void PanicWithCondAndInfo(TPdrStorePanic aError, const TDesC& aCondition, const TDesC& aFileName, const TDesC& aPanicName, TInt aLine)
    1.52 +	{
    1.53 +	TBuf<256> buf;
    1.54 +	buf.Format(KGDIPanicDesc1, &aPanicName, &aFileName, aLine);
    1.55 +	RDebug::Print(buf);
    1.56 +
    1.57 +	buf.Format(KGDIPanicDesc2, &aCondition);
    1.58 +	RDebug::Print(buf);
    1.59 +	Panic(aError);
    1.60 +	}
    1.61 +
    1.62 +//
    1.63 +// TPrinterModelEntry
    1.64 +//
    1.65 +
    1.66 +
    1.67 +EXPORT_C void TPrinterModelEntry::InternalizeL(RReadStream& aStream)
    1.68 +
    1.69 +/** Internalises a printer model entry from a read stream. 
    1.70 +
    1.71 +The presence of this function means that the standard templated stream operator>>(), 
    1.72 +defined in s32strm.h, is available to internalise objects of this class.
    1.73 +
    1.74 +@param aStream The read stream. */
    1.75 +    {
    1.76 +	aStream >> iModelName;
    1.77 +	iRequiresPrinterPort=aStream.ReadUint8L();
    1.78 +	aStream >> iUid;
    1.79 +	}
    1.80 +
    1.81 +
    1.82 +EXPORT_C void TPrinterModelEntry::ExternalizeL(RWriteStream& aStream) const
    1.83 +/** Externalises the printer model entry to a write stream.
    1.84 +
    1.85 +The presence of this function means that the standard templated stream operator<<(), 
    1.86 +defined in s32strm.h, is available to externalise objects of this class.
    1.87 +
    1.88 +@param aStream The write stream. */
    1.89 +	{
    1.90 +	aStream << iModelName;
    1.91 +	aStream.WriteUint8L((TUint8) iRequiresPrinterPort);
    1.92 +	aStream << iUid;
    1.93 +	}
    1.94 +
    1.95 +//
    1.96 +// TPrinterModelHeader
    1.97 +//
    1.98 +
    1.99 +
   1.100 +EXPORT_C void TPrinterModelHeader::InternalizeL(RReadStream& aStream)
   1.101 +/** Internalises a printer model header from a read stream. 
   1.102 +
   1.103 +The presence of this function means that the standard templated stream operator>>(), 
   1.104 +defined in s32strm.h, is available to internalise objects of this class.
   1.105 +
   1.106 +@param aStream The read stream. */
   1.107 + 	{
   1.108 +	aStream >> iEntry;
   1.109 +	aStream >> iModelDataStreamId;
   1.110 +	}
   1.111 +
   1.112 +
   1.113 +EXPORT_C void TPrinterModelHeader::ExternalizeL(RWriteStream& aStream) const
   1.114 + 
   1.115 +/** Externalises the printer model header to a write stream.
   1.116 +
   1.117 +The presence of this function means that the standard templated stream operator<<(), 
   1.118 +defined in s32strm.h, is available to externalise objects of this class.
   1.119 +@param aStream The write stream. */
   1.120 +    {
   1.121 +	aStream << iEntry;
   1.122 +	aStream << iModelDataStreamId;
   1.123 +	}
   1.124 +
   1.125 +//
   1.126 +// CPrinterDevice
   1.127 +//
   1.128 +
   1.129 +EXPORT_C CPrinterDevice::CPrinterDevice():
   1.130 +	CGraphicsDevice(),
   1.131 +	iCurrentPageSpecInTwips()
   1.132 +/** Standard constructor. */
   1.133 +	{}
   1.134 +
   1.135 +
   1.136 +EXPORT_C CPrinterDevice::~CPrinterDevice()
   1.137 +
   1.138 +/** Destructor.
   1.139 +It frees all resources owned by the object, prior to its destruction. */
   1.140 +	{
   1.141 +	delete iControl;
   1.142 +	}
   1.143 +
   1.144 +
   1.145 +EXPORT_C void CPrinterDevice::SelectPageSpecInTwips(const TPageSpec& aPageSpecInTwips)
   1.146 +/** Sets the page specification in twips.
   1.147 +@param  aPageSpec The page specification in twips. */	
   1.148 +    {
   1.149 +	iCurrentPageSpecInTwips=aPageSpecInTwips;
   1.150 +	}
   1.151 +
   1.152 +
   1.153 +EXPORT_C TRect CPrinterDevice::PrintablePageInPixels() const
   1.154 +/** Gets the dimensions of the area to which the printer device can print.
   1.155 +
   1.156 +These dimensions are normally less than those returned by TPageSpec::OrientedPageSize() 
   1.157 +because a margin exists between the boundary of the printable page and the 
   1.158 +absolute extent of the page.
   1.159 +
   1.160 +@return The dimensions of the printer device area in pixels, respecting the 
   1.161 +page orientation */
   1.162 +	{
   1.163 +	return TRect(SizeInPixels());
   1.164 +	}
   1.165 +
   1.166 +
   1.167 +EXPORT_C void CPrinterDevice::DeleteControl()
   1.168 + /** Deletes the printer control owned by this object.
   1.169 +
   1.170 +The function sets the iControl member to NULL. */
   1.171 +    {
   1.172 +	delete iControl;
   1.173 +	iControl=NULL;
   1.174 +	}
   1.175 +
   1.176 +
   1.177 +EXPORT_C void CPrinterDevice::RestorePropertiesL()
   1.178 +/** Restores printer properties. */	
   1.179 +    {
   1.180 +	_LIT(KSystemIniFileNameSpec,"Z:\\System\\System.ini");
   1.181 +
   1.182 +	RFs fs;
   1.183 +	User::LeaveIfError(fs.Connect());
   1.184 +	CleanupClosePushL(fs);
   1.185 +	
   1.186 +	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));
   1.187 +	TParse systemIniFileName;
   1.188 +	systemIniFileName.Set(drive.Name(), &KSystemIniFileNameSpec, NULL);
   1.189 +	
   1.190 +	TUint atts = 0;
   1.191 +	TInt ret = fs.Att(systemIniFileName.FullName(), atts);
   1.192 +	if (ret == KErrNone)
   1.193 +		{
   1.194 +		CDictionaryStore* dictionarystore = NULL;
   1.195 +		TRAPD(err,dictionarystore = CDictionaryFileStore::SystemL(fs));
   1.196 +		if (err == KErrNone)
   1.197 +			{
   1.198 +			CleanupStack::PushL(dictionarystore);
   1.199 +			if (dictionarystore->IsPresentL(Model().iUid))
   1.200 +				{
   1.201 +				RDictionaryReadStream stream;
   1.202 +				stream.OpenLC(*dictionarystore,Model().iUid);
   1.203 +				InternalizePropertiesL(stream);
   1.204 +				CleanupStack::PopAndDestroy(); // stream
   1.205 +				}
   1.206 +			CleanupStack::PopAndDestroy(); // dictionarystore
   1.207 +			}
   1.208 +		}
   1.209 +	CleanupStack::PopAndDestroy(); // fs
   1.210 +	}
   1.211 +
   1.212 +
   1.213 +EXPORT_C void CPrinterDevice::StorePropertiesL() const
   1.214 +/**  Stores the printer properties. */
   1.215 +    {
   1.216 +	RFs fs;
   1.217 +	User::LeaveIfError(fs.Connect());
   1.218 +	CleanupClosePushL(fs);
   1.219 +	CDictionaryStore* dictionarystore = CDictionaryFileStore::SystemLC(fs);
   1.220 +	RDictionaryWriteStream stream;
   1.221 +	stream.AssignLC(*dictionarystore,Model().iUid);
   1.222 +	ExternalizePropertiesL(stream);
   1.223 +	stream.CommitL();
   1.224 +	CleanupStack::PopAndDestroy(); // stream
   1.225 +	dictionarystore->CommitL();
   1.226 +	CleanupStack::PopAndDestroy(2); // dictionarystore, fs
   1.227 +	}
   1.228 +
   1.229 +//
   1.230 +// CPrinterControl
   1.231 +//
   1.232 +
   1.233 +
   1.234 +EXPORT_C CPrinterControl::~CPrinterControl()
   1.235 +/**  Destructor.
   1.236 +
   1.237 +It frees all resources owned by the object, prior to its destruction. */
   1.238 + {
   1.239 +	delete iPrinterPort;
   1.240 +	}
   1.241 +
   1.242 +EXPORT_C CPrinterControl::CPrinterControl(CPrinterPort* aPrinterPort):
   1.243 +	CBase(),
   1.244 +	iState(ENotPrinting),
   1.245 +	iPrinterPort(aPrinterPort)
   1.246 +	{}
   1.247 +
   1.248 +//
   1.249 +// TPageSpec
   1.250 +//
   1.251 +
   1.252 +
   1.253 +EXPORT_C TPageSpec::TPageSpec():
   1.254 +	iPortraitPageSize(TSize(0,0)),
   1.255 +	iOrientation(EPortrait)
   1.256 +/** Default constructor. 
   1.257 +
   1.258 +Initialises the page orientation to portrait and the page height and width 
   1.259 +to zero. */
   1.260 +	{}
   1.261 +
   1.262 +
   1.263 +EXPORT_C TPageSpec::TPageSpec(TPageOrientation anOrientation,const TSize& aSize):
   1.264 +	iPortraitPageSize(aSize),
   1.265 +	iOrientation(anOrientation)
   1.266 +/** Constructor with page orientation and size. 
   1.267 +
   1.268 +@param aOrientation Specifies the page orientation. 
   1.269 +@param aSize Specifies the page size. */
   1.270 + 	{}
   1.271 +
   1.272 +
   1.273 +EXPORT_C void TPageSpec::InternalizeL(RReadStream& aStream)
   1.274 +/** Internalises a page specification object from a read stream. 
   1.275 +
   1.276 +The presence of this function means that the standard templated stream operator>>(), 
   1.277 +defined in s32strm.h, is available to internalise objects of this class.
   1.278 +
   1.279 +@param aStream The read stream. */
   1.280 + 	{
   1.281 +	iPortraitPageSize.iWidth = aStream.ReadInt32L();
   1.282 +	iPortraitPageSize.iHeight = aStream.ReadInt32L();
   1.283 +	iOrientation=(TPageSpec::TPageOrientation)aStream.ReadInt8L();
   1.284 +	}
   1.285 +
   1.286 +
   1.287 +EXPORT_C void TPageSpec::ExternalizeL(RWriteStream& aStream) const
   1.288 +/** Externalises the page specification object to a write stream.
   1.289 +
   1.290 +The presence of this function means that the standard templated stream operator<<(), 
   1.291 +defined in s32strm.h, is available to externalise objects of this class.
   1.292 +@param aStream The write stream. */
   1.293 + 	{
   1.294 +	aStream.WriteInt32L(iPortraitPageSize.iWidth);
   1.295 +	aStream.WriteInt32L(iPortraitPageSize.iHeight);
   1.296 +	aStream.WriteInt8L(iOrientation);
   1.297 +	}
   1.298 +
   1.299 +
   1.300 +EXPORT_C TSize TPageSpec::OrientedPageSize()const
   1.301 +/** Gets the oriented page size.
   1.302 +
   1.303 +The oriented page size is the absolute width and height of the page, respecting 
   1.304 +the page orientation.
   1.305 +@return The oriented page size (in pixels or twips). */
   1.306 +  	{
   1.307 +	if(iOrientation==EPortrait)
   1.308 +		return(iPortraitPageSize);
   1.309 +	return(TSize(iPortraitPageSize.iHeight,iPortraitPageSize.iWidth));
   1.310 +	}
   1.311 +
   1.312 +
   1.313 +EXPORT_C TBool TPageSpec::operator==(const TPageSpec& aPageSpec) const
   1.314 +/** Equality operator. 
   1.315 +
   1.316 +This operator compares page specifications for equality. Two page specifications 
   1.317 +are equal if both their orientations and portrait page sizes are equal.
   1.318 +
   1.319 +@param aPageSpec Page specification to be compared.
   1.320 +@return ETrue, if the page specifications are equal; EFalse, otherwise. */
   1.321 +   {
   1.322 +	return(iPortraitPageSize==aPageSpec.iPortraitPageSize &&
   1.323 +		iOrientation==aPageSpec.iOrientation);
   1.324 +	}
   1.325 +
   1.326 + 
   1.327 +EXPORT_C TBool TPageSpec::operator!=(const TPageSpec& aPageSpec) const
   1.328 +/** Inequality operator.
   1.329 +
   1.330 +This operator compares two page specifications for inequality. Two page specifications 
   1.331 +are unequal if one or both of their orientations and portrait page sizes differ.
   1.332 +
   1.333 +@param aPageSpec Page specification to be compared.
   1.334 +@return ETrue, if the page specifications differ; EFalse, otherwise. */
   1.335 +	{
   1.336 +	return(!(*this==aPageSpec));
   1.337 +	}
   1.338 +
   1.339 +
   1.340 +// TMargins
   1.341 +
   1.342 +EXPORT_C void TMargins::InternalizeL(RReadStream& aStream)
   1.343 +/** Internalises a set of margins from a read stream.
   1.344 +
   1.345 +The presence of this function means that the standard templated stream operator>>() 
   1.346 +is available to internalise objects of this class.
   1.347 +
   1.348 +@param aStream Stream from which the object is internalised. */
   1.349 +	{
   1.350 +	iLeft = aStream.ReadInt32L();
   1.351 +	iRight = aStream.ReadInt32L();
   1.352 +	iTop = aStream.ReadInt32L();
   1.353 +	iBottom = aStream.ReadInt32L();
   1.354 +	}
   1.355 +
   1.356 +EXPORT_C void TMargins::ExternalizeL(RWriteStream& aStream) const
   1.357 +/** Externalises a set of margins to a write stream.
   1.358 +
   1.359 +The presence of this function means that the standard templated stream operator<<() 
   1.360 +is available to externalise objects of this class.
   1.361 +
   1.362 +@param aStream Stream to which the object is externalised. */
   1.363 +	{
   1.364 +	aStream.WriteInt32L(iLeft);
   1.365 +	aStream.WriteInt32L(iRight);
   1.366 +	aStream.WriteInt32L(iTop);
   1.367 +	aStream.WriteInt32L(iBottom);
   1.368 +	}
   1.369 +
   1.370 +EXPORT_C TBool TMargins::operator==(const TMargins& aMargins) const
   1.371 +/** Tests margins for equality.
   1.372 +
   1.373 +@param aMargins The margin to be compared with this margin. 
   1.374 +@return ETrue, if the margins are equal; EFalse, otherwise. */
   1.375 +	{
   1.376 +	return(iLeft==aMargins.iLeft && iRight==aMargins.iRight &&
   1.377 +		iTop==aMargins.iTop && iBottom==aMargins.iBottom);
   1.378 +	}
   1.379 +
   1.380 +EXPORT_C TBool TMargins::operator!=(const TMargins& aMargins) const
   1.381 +/** Tests margins for inequality.
   1.382 +
   1.383 +@param aMargins The margin to be compared with this margin. 
   1.384 +@return ETrue, if the margins are unequal; EFalse, otherwise. */
   1.385 +	{
   1.386 +	return(!(*this==aMargins));
   1.387 +	}
   1.388 +
   1.389 +//
   1.390 +// CPrinterDriverUI
   1.391 +//
   1.392 +
   1.393 +EXPORT_C CPrinterDriverUI::CPrinterDriverUI()
   1.394 +	{
   1.395 +	__DECLARE_NAME(_S("CPrinterDriverUI"));
   1.396 +	}
   1.397 +
   1.398 +
   1.399 +EXPORT_C TBool CPrinterDriverUI::BeforePrintL()
   1.400 + /** Provides an opportunity for a dialog to be put up before printing begins.
   1.401 +
   1.402 +@return ETrue, if printing is to continue; EFalse, if printing is to be cancelled. 
   1.403 +The default implementation returns ETrue. */
   1.404 +  	{
   1.405 +	return ETrue;
   1.406 +	}
   1.407 +
   1.408 +
   1.409 +EXPORT_C void CPrinterDriverUI::AfterPrintL()
   1.410 + /** Provides an opportunity for a dialog to be put up after printing is complete.
   1.411 +The default implementation is empty. */
   1.412 +	{
   1.413 +	}
   1.414 +
   1.415 +
   1.416 +EXPORT_C void CPrinterDriverUI::SetPropertiesL()
   1.417 +/** Provides an opportunity for a dialog to be put up to capture or change printer 
   1.418 +properties.
   1.419 +The default implementation is empty. */
   1.420 +	{
   1.421 +	}
   1.422 +
   1.423 +
   1.424 +EXPORT_C TBool CPrinterDriverUI::CanSetProperties()
   1.425 +/** Tests whether printer properties can be set.
   1.426 +
   1.427 +@return ETrue, if printer properties can be set; EFalse, otherwise. The default 
   1.428 +implementation returns EFalse. */
   1.429 +    {
   1.430 +	return EFalse;
   1.431 +	}
   1.432 +
   1.433 +//
   1.434 +// CPrinterDriver
   1.435 +//
   1.436 +
   1.437 +
   1.438 +EXPORT_C CPrinterDriver* CPrinterDriver::NewL()
   1.439 +/** Constructs, and returns a pointer to a new instance for accessing a printer 
   1.440 +specification data store.
   1.441 +
   1.442 +@return Pointer to the object for accessing a printer specification data store. */
   1.443 +	{
   1.444 +	CPrinterDriver* printerdriver=new(ELeave) CPrinterDriver;
   1.445 +	CleanupStack::PushL(printerdriver);
   1.446 +	User::LeaveIfError(printerdriver->iFs.Connect());
   1.447 +	CleanupStack::Pop();
   1.448 +	return printerdriver;
   1.449 +	}
   1.450 +
   1.451 +
   1.452 +EXPORT_C CPrinterDriver::~CPrinterDriver()
   1.453 +/** Destructor.
   1.454 +
   1.455 +It frees all resources owned by the object, prior to its destruction. In particular, 
   1.456 +it closes the printer specification data store and any open session with the file server.  */	
   1.457 +    {
   1.458 +	Close();
   1.459 +	iFs.Close();
   1.460 +	}
   1.461 +
   1.462 +
   1.463 +EXPORT_C void CPrinterDriver::OpenPdrL(const TDesC &aName)
   1.464 +/**  Opens the specified printer specification data store.
   1.465 +
   1.466 +@return  The name of the printer specification data store. This must be a 
   1.467 +valid printer specification data store,otherwise the function leaves with
   1.468 +KErrNotSupported. */	
   1.469 +    {
   1.470 +	Close();
   1.471 +	TRAPD(ret,DoOpenPdrL(aName));
   1.472 +	if (ret!=KErrNone)
   1.473 +		{
   1.474 +		Close();
   1.475 +		User::Leave(ret);
   1.476 +		}
   1.477 +	}
   1.478 +
   1.479 +EXPORT_C void CPrinterDriver::Close()
   1.480 +/** Closes the printer specification data store and frees resources.
   1.481 +
   1.482 +An open session with the file server remains open. */
   1.483 +	{
   1.484 +	delete iPdrStore,
   1.485 +	iPdrStore=NULL;
   1.486 +	iNumModels=0;
   1.487 +	delete[] iModelList;
   1.488 +	iModelList=NULL;
   1.489 +	DeletePrinterDevice();
   1.490 +	}
   1.491 +
   1.492 +
   1.493 +EXPORT_C TInt CPrinterDriver::NumModels() const
   1.494 + /** Gets the number of printer models defined by the printer specification.
   1.495 +
   1.496 +@return The number of printer models. */
   1.497 + 	{
   1.498 +	return iNumModels;
   1.499 +	}
   1.500 +
   1.501 +
   1.502 +EXPORT_C TPrinterModelEntry CPrinterDriver::Model(TInt aNum) const
   1.503 +/** Gets the specified printer model.
   1.504 +
   1.505 +@param aNum An index into the list of printer models defined in the printer 
   1.506 +specification data.
   1.507 +@return Model specific information. */
   1.508 + 	{
   1.509 +	GDI_ASSERT_DEBUG(aNum>=0,EPdrModelIndexOutOfRange);
   1.510 +	GDI_ASSERT_DEBUG(aNum<iNumModels,EPdrModelIndexOutOfRange);
   1.511 +	return iModelList[aNum].iEntry;
   1.512 +	}
   1.513 +
   1.514 +
   1.515 +EXPORT_C CPrinterDevice* CPrinterDriver::CreatePrinterDeviceL(TUid aModelUid)
   1.516 +/** Creates the physical graphics device to be used for printing.
   1.517 +
   1.518 +@param aModelUid The UID of a specific model which is defined in the printer 
   1.519 +specification data.
   1.520 +@return The physical graphics device to be used for printing.  */	
   1.521 +    {
   1.522 +	GDI_ASSERT_DEBUG(!iPrinterDevice,EPdrPrinterDeviceExists);
   1.523 +	TRAPD(ret,DoCreatePrinterDeviceL(aModelUid));
   1.524 +	if (ret!=KErrNone)
   1.525 +		{
   1.526 +		DeletePrinterDevice();
   1.527 +		User::Leave(ret);
   1.528 +		}
   1.529 +	return iPrinterDevice;
   1.530 +	}
   1.531 +
   1.532 +void CPrinterDriver::LoadLibraryL(RLibrary& aLibrary,const TDesC& aExt,TUid aUid2)
   1.533 +	{
   1.534 +	TFileName filename=iPdlName;
   1.535 +	filename.Append(aExt);
   1.536 +
   1.537 +	User::LeaveIfError(aLibrary.Load(filename));
   1.538 +	TUidType type=aLibrary.Type();
   1.539 +	if (type[1]!=aUid2 && type[2]!=iPdlUid)
   1.540 +		{
   1.541 +		aLibrary.Close();
   1.542 +		User::Leave(KErrNotSupported);
   1.543 +		}
   1.544 +	if (type[1]!=aUid2)
   1.545 +		{
   1.546 +		aLibrary.Close();
   1.547 +		User::Leave(KErrNotFound);
   1.548 +		}
   1.549 +	}
   1.550 +
   1.551 +
   1.552 +EXPORT_C CPrinterDriverUI* CPrinterDriver::CreatePrinterDriverUIL()
   1.553 +/** Constructs a printer specific user interface.
   1.554 +
   1.555 +The user interface object is optional, but if it exists, it is implemented 
   1.556 +as part of a UDL (i.e. a UI DLL).
   1.557 +
   1.558 +@return A pointer to the printer specific user interface, or NULL if there is 
   1.559 +none. */
   1.560 +    {
   1.561 +	GDI_ASSERT_DEBUG(iPrinterDevice,EPdrPrinterDeviceDoesNotExist);
   1.562 +	if (iUdlLibrary.Handle()==0)
   1.563 +		{
   1.564 +		TRAPD(ret,LoadLibraryL(iUdlLibrary,KUdlExtension,TUid::Uid(KUdlUidVal)));
   1.565 +		if (ret==KErrNotFound)
   1.566 +			return NULL;
   1.567 +		else
   1.568 +			User::LeaveIfError(ret);
   1.569 +		}
   1.570 +	TLibraryFunction f = iUdlLibrary.Lookup(1);
   1.571 +	CPrinterDriverUI* printerdriverui=(CPrinterDriverUI*)((*f)());
   1.572 +	CleanupStack::PushL(printerdriverui);
   1.573 +	User::LeaveIfError(printerdriverui->SetPrinterDevice(iPrinterDevice));
   1.574 +	CleanupStack::Pop();
   1.575 +	return printerdriverui;
   1.576 +	}
   1.577 +
   1.578 +CPrinterDriver::CPrinterDriver()
   1.579 +	{}
   1.580 +
   1.581 +void CPrinterDriver::DeletePrinterDevice()
   1.582 +	{
   1.583 +	iUdlLibrary.Close();
   1.584 +	delete iPrinterDevice;
   1.585 +	iPrinterDevice=NULL;
   1.586 +	iPdlLibrary.Close();
   1.587 +	}
   1.588 +
   1.589 +void CPrinterDriver::DoOpenPdrL(const TDesC &aName)
   1.590 +	{
   1.591 +	Close();
   1.592 +	iPdrStore=CDirectFileStore::OpenL(iFs,aName,EFileStream|EFileRead|EFileShareReadersOnly);
   1.593 +	if (iPdrStore->Type()[1]!=TUid::Uid(KPdrStoreFileUidVal))
   1.594 +		User::Leave(KErrNotSupported);
   1.595 +	TStreamId headerid = iPdrStore->Root();
   1.596 +	RStoreReadStream stream;
   1.597 +	stream.OpenLC(*iPdrStore,headerid);
   1.598 +		stream >> iPdlName;
   1.599 +	stream >> iPdlUid;
   1.600 +	iNumModels = stream.ReadInt32L();
   1.601 +	iModelList = new(ELeave) TPrinterModelHeader[iNumModels];
   1.602 +	for (TInt i=0; i<iNumModels; i++)
   1.603 +		iModelList[i].InternalizeL(stream);
   1.604 +	CleanupStack::PopAndDestroy();
   1.605 +	}
   1.606 +
   1.607 +void CPrinterDriver::DoCreatePrinterDeviceL(TUid aModelUid)
   1.608 +	{
   1.609 +	if (!iPdlName.Length())
   1.610 +		User::Leave(KErrGeneral); // !! find a better error number
   1.611 +	LoadLibraryL(iPdlLibrary,KPdlExtension,TUid::Uid(KPdlUidVal));
   1.612 +	TLibraryFunction f = iPdlLibrary.Lookup(1);
   1.613 +	iPrinterDevice=(CPrinterDevice*)((*f)());
   1.614 +	TInt i;
   1.615 +	for (i=0; (i<iNumModels) && (aModelUid!=iModelList[i].iEntry.iUid); i++)
   1.616 +		{
   1.617 +		}
   1.618 +	GDI_ASSERT_DEBUG(i<iNumModels,EPdrModelUidNotFound);
   1.619 +	User::LeaveIfError(iPrinterDevice->SetModel(iModelList[i],*iPdrStore));
   1.620 +	iPrinterDevice->RestorePropertiesL();
   1.621 +	}
   1.622 +
   1.623 +//
   1.624 +// CPdrModelList
   1.625 +//
   1.626 +
   1.627 +
   1.628 +EXPORT_C CPdrModelList* CPdrModelList::NewL()
   1.629 +/** Constructs, and returns a pointer to a new instance of the printer model 
   1.630 +list interface.
   1.631 +@return Pointer to the new printer model list interface object. */
   1.632 +    {
   1.633 +	CPdrModelList* modellist=new(ELeave) CPdrModelList();
   1.634 +	CleanupStack::PushL(modellist);
   1.635 +	modellist->ConstructL();
   1.636 +	CleanupStack::Pop();
   1.637 +	return modellist;
   1.638 +	}
   1.639 +
   1.640 +
   1.641 +EXPORT_C CPdrModelList::~CPdrModelList()
   1.642 +/** Virtual destructor.
   1.643 +Frees resources owned by the object, prior to its destruction. */
   1.644 +	{
   1.645 +	delete iModelArray;
   1.646 +	delete iFileArray;
   1.647 +	if (iDirectoryArray)
   1.648 +		{// delete all the HBufC's
   1.649 +		for (TInt i=iDirectoryArray->Count()-1 ; i>=0 ; i--)
   1.650 +			delete (*iDirectoryArray)[i];
   1.651 +		delete iDirectoryArray;
   1.652 +		}
   1.653 +	iFileServer.Close();
   1.654 +	}
   1.655 +
   1.656 +
   1.657 +EXPORT_C TInt CPdrModelList::ModelCount() const
   1.658 +/** Gets the number of printer models in the printer model list.
   1.659 +@return The number of printer models. */
   1.660 +    {
   1.661 +	return iModelArray->Count();
   1.662 +	}
   1.663 +
   1.664 +
   1.665 +EXPORT_C const TPrinterModelEntry CPdrModelList::operator [] (TInt anIndex)
   1.666 +/** Gets printer model name.
   1.667 +
   1.668 +This is the name of the printer model at the specified index within the list 
   1.669 +of printer models.
   1.670 +
   1.671 +@param anIndex The index of the printer model within the array of printer 
   1.672 +models. Note that this number must be between zero and ModelCount(). 
   1.673 +
   1.674 +@return Name of printer model, up to 32 characters long */
   1.675 + 	{
   1.676 +	GDI_ASSERT_DEBUG(anIndex>=0,EPdrModelIndexOutOfRange);
   1.677 +	GDI_ASSERT_DEBUG(anIndex<iModelArray->Count(),EPdrModelIndexOutOfRange);
   1.678 +
   1.679 +	return (*iModelArray)[anIndex].iEntry;
   1.680 +	}
   1.681 +
   1.682 +
   1.683 +EXPORT_C TInt CPdrModelList::UidToNum(TUid aModelUid) const
   1.684 +/** Gets a printer model's index within the model list from its UID.
   1.685 +
   1.686 +@param aModelUid The UID of the printer model.
   1.687 +@return The index of the printer model within the array of printer models if 
   1.688 +found; KErrNotFound, otherwise. */
   1.689 +    {
   1.690 +	TInt i,count=iModelArray->Count();
   1.691 +	for (i=0; (i<count) && (aModelUid!=(*iModelArray)[i].iEntry.iUid); i++)
   1.692 +		{
   1.693 +		}
   1.694 +
   1.695 +	if (i==count)
   1.696 +		i=KErrNotFound;
   1.697 +
   1.698 +	return i;
   1.699 +	}
   1.700 +
   1.701 +
   1.702 +EXPORT_C void CPdrModelList::AddDirectoryL(const TDesC& aDir)
   1.703 +/** Adds a directory to the list of directories to be scanned for printer models.
   1.704 +
   1.705 +@param aDir The directory to be added to the list. */
   1.706 +	{
   1.707 +	HBufC* buf = HBufC::NewL(aDir.Length());
   1.708 +	CleanupStack::PushL(buf);
   1.709 +	*buf = aDir;
   1.710 +	iDirectoryArray->AppendL(buf);
   1.711 +	CleanupStack::Pop(); //buf
   1.712 +	}
   1.713 +
   1.714 +LOCAL_C void DereferenceAndDeleteHBufC8(TAny* aPointerToPointerToHBufC8)
   1.715 +	{
   1.716 +	delete *STATIC_CAST(HBufC8**, aPointerToPointerToHBufC8);
   1.717 +	}
   1.718 +
   1.719 +
   1.720 +EXPORT_C CPrinterModelList* CPdrModelList::ScanForModelsL()
   1.721 +/** Scans through through the list of directories for all .pdr files and generates 
   1.722 +a list of printer models.
   1.723 +
   1.724 +@return The list of model names. */
   1.725 +	{
   1.726 +	iModelArray->Reset();
   1.727 +	iFileArray->Reset();
   1.728 +	// check that there is at least one directory to parse?
   1.729 +	// get a list of *.pdr files in all directories specified (using AddDirectory())
   1.730 +	for (TInt index=iDirectoryArray->Count()-1 ; index>=0 ; index--)
   1.731 +		ScanDirectoryL(index);
   1.732 + 	// then go through the files one at a time, adding all models to the list
   1.733 +	TParse* parser = new(ELeave) TParse;
   1.734 +	CleanupStack::PushL(parser);
   1.735 +	TFileName* nameOfLoadedResourceFile=new(ELeave) TFileName;
   1.736 +	CleanupStack::PushL(nameOfLoadedResourceFile);
   1.737 +	TFileName* tempFileName=new(ELeave) TFileName;
   1.738 +	CleanupStack::PushL(tempFileName);
   1.739 +	RResourceFile resourceFile;
   1.740 +	CleanupClosePushL(resourceFile);
   1.741 +	HBufC8* resource=NULL;
   1.742 +	CleanupStack::PushL(TCleanupItem(DereferenceAndDeleteHBufC8, &resource));
   1.743 +	for (TInt fileNum=iFileArray->Count()-1 ; fileNum>=0 ; fileNum--)
   1.744 +		ListModelsL(fileNum, *parser, *nameOfLoadedResourceFile, *tempFileName, resourceFile, resource);
   1.745 +	CleanupStack::PopAndDestroy(5); // resource, resourceFile, tempFileName, nameOfLoadedResourceFile and parser
   1.746 +	// return a handle to the array of model names
   1.747 +	return this;
   1.748 +	}
   1.749 +
   1.750 +
   1.751 +EXPORT_C CPrinterDriver* CPdrModelList::CreatePrinterDriverL(TInt anIndex)
   1.752 +/** Creates an object for accessing the specified store that contains printer specification 
   1.753 +data.
   1.754 +
   1.755 +@param anIndex An index into a list of files containing printer specification 
   1.756 +data. The files are the complete set of .pdr files found in the directories 
   1.757 +known to this object.
   1.758 +@return A pointer to the object representing the store containing the printer 
   1.759 +specification data. */
   1.760 + 	{
   1.761 +	GDI_ASSERT_DEBUG(anIndex>=0,EPdrModelIndexOutOfRange);
   1.762 +	GDI_ASSERT_DEBUG(anIndex<iModelArray->Count(),EPdrModelIndexOutOfRange);
   1.763 +
   1.764 +	CPrinterDriver* driver = CPrinterDriver::NewL();
   1.765 +	CleanupStack::PushL(driver);
   1.766 +	HBufC* file = NewPathBufL(*((*iModelArray)[anIndex].iFile));
   1.767 +	CleanupStack::PushL(file);
   1.768 +	driver->OpenPdrL(*file);
   1.769 +	driver->CreatePrinterDeviceL((*iModelArray)[anIndex].iEntry.iUid);
   1.770 +	CleanupStack::PopAndDestroy();
   1.771 +	CleanupStack::Pop();
   1.772 +	return driver;
   1.773 +	}
   1.774 +
   1.775 + 
   1.776 + CPdrModelList::CPdrModelList():
   1.777 +	iModelArray(NULL),
   1.778 +	iFileArray(NULL),
   1.779 +	iDirectoryArray(NULL)
   1.780 +	{
   1.781 +	}
   1.782 + 
   1.783 + void CPdrModelList::ConstructL()
   1.784 +	{
   1.785 +	__DECLARE_NAME(_S("CPdrModelList"));
   1.786 +	iModelArray = new(ELeave) CArrayFixSeg<TModelEntry>(5);
   1.787 +	iFileArray = new(ELeave) CArrayFixFlat<TFileEntry>(5);
   1.788 +	iDirectoryArray = new(ELeave) CArrayFixFlat<HBufC*>(1);
   1.789 +	User::LeaveIfError(iFileServer.Connect());
   1.790 +	}
   1.791 +
   1.792 +void CPdrModelList::ScanDirectoryL(TInt aDirIndex)
   1.793 +/** Scans the given directory, parsing all files found
   1.794 + If a file is of the correct type it is appended to the file list*/
   1.795 +	{
   1.796 +	GDI_ASSERT_DEBUG(aDirIndex>=0,EPdrDirectoryIndexOutOfRange);
   1.797 +	GDI_ASSERT_DEBUG(aDirIndex<iDirectoryArray->Count(),EPdrDirectoryIndexOutOfRange);
   1.798 +
   1.799 +	TDesC* dir = (*iDirectoryArray)[aDirIndex];
   1.800 +	TParse path;
   1.801 +	path.Set(KPdrExtension,dir,NULL);
   1.802 +	CDir* fileList;
   1.803 +	TInt ret = iFileServer.GetDir(path.FullName(),KEntryAttNormal,ESortByName,fileList);
   1.804 +	if (ret == KErrNone)
   1.805 +		{
   1.806 +		CleanupStack::PushL(fileList);
   1.807 +		for (TInt i=fileList->Count()-1 ; i>=0 ; i--) 
   1.808 +			{
   1.809 +			TFileEntry& entry = iFileArray->ExtendL();
   1.810 +			entry.iFileName = (*fileList)[i].iName;
   1.811 +			entry.iDirectory = dir;
   1.812 +			}
   1.813 +		CleanupStack::PopAndDestroy(); // fileList
   1.814 +		}
   1.815 +	else if (ret == KErrNoMemory) // Ignore errors other than KErrNoMemory
   1.816 +		User::LeaveNoMemory();
   1.817 +	}
   1.818 +
   1.819 +void CPdrModelList::ListModelsL(TInt aFileIndex, TParse& aParser, TFileName& aNameOfLoadedResourceFile, TFileName& aTempFileName, RResourceFile& aResourceFile, HBufC8*& aResource)
   1.820 +/** given a pdr file list all the models it contains in the model array */
   1.821 +	{
   1.822 +	GDI_ASSERT_DEBUG(aFileIndex>=0,EPdrFileIndexOutOfRange);
   1.823 +	GDI_ASSERT_DEBUG(aFileIndex<iFileArray->Count(),EPdrFileIndexOutOfRange);
   1.824 +
   1.825 +	CPrinterDriver* driver = CPrinterDriver::NewL() ;
   1.826 +	CleanupStack::PushL(driver);
   1.827 +	// open the file
   1.828 +	HBufC* fullPath = NewPathBufL((*iFileArray)[aFileIndex]);
   1.829 +	TRAPD(ret,driver->OpenPdrL(*fullPath));
   1.830 +	delete fullPath;
   1.831 +	if ((ret!=KErrNone) && (ret!=KErrNotSupported))
   1.832 +		User::Leave(ret);
   1.833 +	// get info on the models one by one and insert them into the array
   1.834 +	if (ret==KErrNone)
   1.835 +		{
   1.836 +		TModelEntry modelentry;
   1.837 +		modelentry.iFile = &(*iFileArray)[aFileIndex]; // set the file pointer for the entry
   1.838 +		const TInt numberOfModels = driver->NumModels();
   1.839 +		for (TInt i=0 ; i<numberOfModels ; i++)
   1.840 +			{
   1.841 +			modelentry.iEntry=driver->Model(i);
   1.842 +			if (UidToNum(modelentry.iEntry.iUid)==KErrNotFound)
   1.843 +				{
   1.844 +				User::LeaveIfError(aParser.SetNoWild(KRscExtension, &modelentry.iFile->iFileName, modelentry.iFile->iDirectory));
   1.845 +				aTempFileName=aParser.FullName();
   1.846 +				BaflUtils::NearestLanguageFile(iFileServer, aTempFileName);
   1.847 +				if (aNameOfLoadedResourceFile.CompareF(aTempFileName)!=0)
   1.848 +					{
   1.849 +					if (!BaflUtils::FileExists(iFileServer, aTempFileName))
   1.850 +						{
   1.851 +						iModelArray->AppendL(modelentry); // no resource file found, so reverting to old behaviour (i.e. where the model-name is set from the PDR file)
   1.852 +						continue;
   1.853 +						}
   1.854 +					aNameOfLoadedResourceFile=KNullDesC;
   1.855 +					aResourceFile.OpenL(iFileServer, aTempFileName);
   1.856 +					HBufC8* resource=aResourceFile.AllocReadL(aResourceFile.Offset()+2); // read the first resource after the RSS_SIGNATURE resource
   1.857 +					delete aResource;
   1.858 +					aResource=resource;
   1.859 +					aNameOfLoadedResourceFile=aTempFileName;
   1.860 +					}
   1.861 +				TResourceReader resourceReader;
   1.862 +				resourceReader.SetBuffer(aResource);
   1.863 +				for (TInt j=resourceReader.ReadUint16()-1; ; --j)
   1.864 +					{
   1.865 +					if (j<0)
   1.866 +						{
   1.867 +						iModelArray->AppendL(modelentry); // no matching uid found in the resource file, so reverting to old behaviour (i.e. where the model-name is set from the PDR file)
   1.868 +						break;
   1.869 +						}
   1.870 +					TInt uid=resourceReader.ReadInt32();
   1.871 +					TPtrC name=resourceReader.ReadTPtrC();
   1.872 +					if (uid==modelentry.iEntry.iUid.iUid)
   1.873 +						{
   1.874 +						if (name.Length()>0)
   1.875 +							{
   1.876 +							modelentry.iEntry.iModelName=name;
   1.877 +							iModelArray->AppendL(modelentry);
   1.878 +							}
   1.879 +						break;
   1.880 +						}
   1.881 +					}
   1.882 +				}
   1.883 +			}
   1.884 +		}
   1.885 +	CleanupStack::PopAndDestroy(); // driver
   1.886 +	}
   1.887 +
   1.888 +
   1.889 +HBufC* CPdrModelList::NewPathBufL(const TFileEntry& aFileEntry)  
   1.890 +/** Create a buf of the right length and... 
   1.891 + set its contents to the full filename of model aModel */
   1.892 +	{
   1.893 +	// Create a buf of the right length
   1.894 +	HBufC* buf = HBufC::NewL(aFileEntry.iFileName.Length()+aFileEntry.iDirectory->Length());
   1.895 +	// Insert the file path
   1.896 +	TPtr filePtr = buf->Des();
   1.897 +	filePtr.Append(*aFileEntry.iDirectory);
   1.898 +	filePtr.Append(aFileEntry.iFileName);
   1.899 +	return buf;
   1.900 +	}
   1.901 +
   1.902 +// !!!!
   1.903 +// Retained for binary compatibility only: remove if we make a binary incompatible release
   1.904 +//
   1.905 +
   1.906 +IMPORT_C void GDI_Reserved();
   1.907 +EXPORT_C void GDI_Reserved()
   1.908 +	{}
   1.909 +