ImagePrint/ImagePrintEngine/DeviceProtocols/upnpprotocolfw2/src/cupprintercontainer.cpp
branchRCL_3
changeset 27 159fc2f68139
parent 21 26673e532f65
child 28 d59c248c9d36
equal deleted inserted replaced
21:26673e532f65 27:159fc2f68139
     1 /*
       
     2 * Copyright (c) 2002-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Declares CUPPrinterContainer class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "cupprintercontainer.h"
       
    20 #include "cuplogger.h"
       
    21 #include "upconsts.h"
       
    22 
       
    23 _LIT(KUPnPPrinterCache, "printers.txt");
       
    24 
       
    25 #define KMaxCacheFileSize 512
       
    26 
       
    27 // Public functions
       
    28 
       
    29 //--------------------------------------------------------------------------------------------
       
    30 //
       
    31 // CUPPrinterContainer::~CUPPrinterContainer
       
    32 //
       
    33 //--------------------------------------------------------------------------------------------
       
    34 CUPPrinterContainer::~CUPPrinterContainer()
       
    35 {
       
    36 	iPrinters.ResetAndDestroy();
       
    37 	iPrinters.Close();
       
    38 	iFsSession.Close();
       
    39 	if(iCacheBuffer)
       
    40 		delete iCacheBuffer;
       
    41 }
       
    42 
       
    43 //--------------------------------------------------------------------------------------------
       
    44 //
       
    45 // CUPPrinterContainer::NewL
       
    46 //
       
    47 //--------------------------------------------------------------------------------------------
       
    48 CUPPrinterContainer* CUPPrinterContainer::NewL()
       
    49 {
       
    50 	CUPPrinterContainer* self = new (ELeave) CUPPrinterContainer();
       
    51 	CleanupStack::PushL(self);
       
    52 	self->ConstructL();
       
    53 	CleanupStack::Pop();	// self
       
    54 	return self;
       
    55 }
       
    56 
       
    57 //--------------------------------------------------------------------------------------------
       
    58 //
       
    59 // CUPPrinterContainer::Printer
       
    60 //
       
    61 //--------------------------------------------------------------------------------------------
       
    62 CUPPrinter* CUPPrinterContainer::Printer(CUpnpDevice& aDevice)
       
    63 {
       
    64 	TInt index = PrinterIndex(aDevice);
       
    65 	if(KErrNotFound != index)
       
    66 	{
       
    67 		return PrinterByIndex(index);
       
    68 	}
       
    69 	else
       
    70 		return NULL;
       
    71 }
       
    72 
       
    73 //--------------------------------------------------------------------------------------------
       
    74 //
       
    75 // CUPPrinterContainer::Printer
       
    76 //
       
    77 //--------------------------------------------------------------------------------------------
       
    78 CUPPrinter* CUPPrinterContainer::Printer(TInt aId)
       
    79 {
       
    80 	TInt index = PrinterIndex(aId);
       
    81 	if(KErrNotFound != index)
       
    82 	{
       
    83 		return PrinterByIndex(index);
       
    84 	}
       
    85 	else
       
    86 		return NULL;
       
    87 }
       
    88 
       
    89 
       
    90 //--------------------------------------------------------------------------------------------
       
    91 //
       
    92 // CUPPrinterContainer::ToTPrinter
       
    93 //
       
    94 //--------------------------------------------------------------------------------------------
       
    95 TPrinter CUPPrinterContainer::ToTPrinter(CUpnpDevice& aDevice)
       
    96 {
       
    97 	TInt index = PrinterIndex(aDevice);
       
    98 	return ToTPrinter(index);
       
    99 }
       
   100 
       
   101 //--------------------------------------------------------------------------------------------
       
   102 //
       
   103 // CUPPrinterContainer::ToTPrinter
       
   104 //
       
   105 //--------------------------------------------------------------------------------------------
       
   106 TPrinter CUPPrinterContainer::ToTPrinter(TInt aIndex)
       
   107 {
       
   108 	if (aIndex >= iPrinters.Count() )
       
   109 	{
       
   110 		TPrinter nullPrinter;
       
   111 		nullPrinter.iPrinterID = KErrNotFound;
       
   112 		nullPrinter.iProtocol = 0;
       
   113 		nullPrinter.iProperties = TPrinter::Cached;
       
   114 		return nullPrinter;
       
   115 	}
       
   116 
       
   117 	return iPrinters[aIndex]->ToTPrinter();
       
   118 }
       
   119 
       
   120 //--------------------------------------------------------------------------------------------
       
   121 //
       
   122 // CUPPrinterContainer::PrinterCount
       
   123 //
       
   124 //--------------------------------------------------------------------------------------------
       
   125 TInt CUPPrinterContainer::PrinterCount()
       
   126 {
       
   127 	return iPrinters.Count();
       
   128 }
       
   129 
       
   130 
       
   131 //--------------------------------------------------------------------------------------------
       
   132 //
       
   133 // CUPPrinterContainer::AddPrinterL
       
   134 //
       
   135 //--------------------------------------------------------------------------------------------
       
   136 void CUPPrinterContainer::AddPrinterL(CUpnpDevice& aDevice, TInt& aId)
       
   137 {
       
   138 	aId = KErrNotFound;
       
   139 	
       
   140 	// Check if the discovered printer is cached/ disappeared
       
   141 	for (TInt i=0; i < iPrinters.Count(); i++)
       
   142 	{
       
   143 		if (iPrinters[i]->UId().Compare(aDevice.Uuid()) == 0)
       
   144 		{
       
   145 			// Printer already in array - update pointer
       
   146 			iPrinters[i]->SetDevice(&aDevice);
       
   147 			if (iPrinters[i]->IsDisappeared())
       
   148 			{
       
   149 				// remove disappeared flag
       
   150 				iPrinters[i]->SetDisappeared(EFalse);
       
   151 			}
       
   152 			aId = iPrinters[i]->Id();
       
   153 			User::Leave(KErrAlreadyExists);
       
   154 		}
       
   155 	}
       
   156 
       
   157 	// New printer - append to list
       
   158 	TInt id = GenerateId();
       
   159 	CUPPrinter* newPrinter = CUPPrinter::NewL(aDevice, id);
       
   160 	CleanupStack::PushL(newPrinter);
       
   161 	iPrinters.AppendL(newPrinter);
       
   162 	CleanupStack::Pop(newPrinter);
       
   163 	aId = id;
       
   164 }
       
   165 
       
   166 
       
   167 //--------------------------------------------------------------------------------------------
       
   168 //
       
   169 // CUPPrinterContainer::RemovePrinter
       
   170 //
       
   171 //--------------------------------------------------------------------------------------------
       
   172 TInt CUPPrinterContainer::RemovePrinter(CUpnpDevice& aDevice)
       
   173 {
       
   174 	TInt index = PrinterIndex(aDevice);
       
   175 	if(KErrNotFound != index)
       
   176 	{
       
   177 		return RemovePrinter(index);
       
   178 	}
       
   179 	else
       
   180 		return index;
       
   181 }
       
   182 
       
   183 //--------------------------------------------------------------------------------------------
       
   184 //
       
   185 // CUPPrinterContainer::RemovePrinter
       
   186 //
       
   187 //--------------------------------------------------------------------------------------------
       
   188 TInt CUPPrinterContainer::RemovePrinter(TInt aIndex)
       
   189 {
       
   190 	if (aIndex >= iPrinters.Count() || aIndex < 0)
       
   191 		return KErrArgument;
       
   192 	CUPPrinter* temp = iPrinters[aIndex];
       
   193 	iPrinters.Remove(aIndex);
       
   194 	if (temp)
       
   195 	{
       
   196 		delete temp;
       
   197 		temp = NULL;	
       
   198 	}
       
   199 	return KErrNone;
       
   200 }
       
   201 //--------------------------------------------------------------------------------------------
       
   202 //
       
   203 // CUPPrinterContainer::RemovePrinter
       
   204 //
       
   205 //--------------------------------------------------------------------------------------------
       
   206 TInt CUPPrinterContainer::RemoveCachedDeviceL(TInt aDeviceID)
       
   207 {
       
   208 	TInt err = KErrNone;
       
   209 	TBool cached = EFalse;
       
   210 
       
   211 	for (TInt i=0; i < iPrinters.Count(); i++)
       
   212 	{
       
   213 		if (iPrinters[i]->Id() == aDeviceID)
       
   214 		{
       
   215 			cached = iPrinters[i]->Cached();
       
   216 			iPrinters[i]->SetCached(EFalse);
       
   217 			break;
       
   218 		}
       
   219 	}
       
   220 
       
   221 	if(!cached)
       
   222 		return KErrNone;
       
   223 
       
   224 	ReadCacheFileL();
       
   225 
       
   226 	TInt startPos = 0;
       
   227 	TInt endPos = 0;
       
   228 
       
   229 	FindCachedPrinterL(aDeviceID, startPos, endPos);
       
   230 
       
   231 	LOG1("[CUPPrinterContainer::RemoveCachedDeviceL]\t startPos:\t%d", startPos);
       
   232 	LOG1("[CUPPrinterContainer::RemoveCachedDeviceL]\t endPos:\t%d", endPos);
       
   233 
       
   234 	if(0 < endPos)
       
   235 	{
       
   236 		//Remove printer form the buffer
       
   237 		iCacheBuffer->Des().Delete(startPos, endPos-startPos);
       
   238 
       
   239 		//Write buffer in file
       
   240 		RFile cacheFile;
       
   241 		CleanupClosePushL(cacheFile);
       
   242 		TInt err = cacheFile.Replace( iFsSession, KUPnPPrinterCache, EFileWrite);
       
   243 		if (err == KErrNone)
       
   244 		{
       
   245 			err = cacheFile.Write(iCacheBuffer->Des());
       
   246 		}
       
   247 		CleanupStack::PopAndDestroy(&cacheFile);
       
   248 	}
       
   249 
       
   250 	delete iCacheBuffer;
       
   251 	iCacheBuffer = NULL;
       
   252 
       
   253 	return err;
       
   254 }
       
   255 
       
   256 //--------------------------------------------------------------------------------------------
       
   257 //
       
   258 // CUPPrinterContainer::Printer
       
   259 //
       
   260 //--------------------------------------------------------------------------------------------
       
   261 CUPPrinter* CUPPrinterContainer::PrinterByIndex(TInt aIndex)
       
   262 {
       
   263 	if (aIndex >= iPrinters.Count() || aIndex < 0)	
       
   264 		return NULL;
       
   265 	return iPrinters[aIndex];
       
   266 }
       
   267 
       
   268 
       
   269 //--------------------------------------------------------------------------------------------
       
   270 //
       
   271 // CUPPrinterContainer::PrinterIndex
       
   272 //
       
   273 //--------------------------------------------------------------------------------------------
       
   274 TInt CUPPrinterContainer::PrinterIndex(CUpnpDevice& aDevice)
       
   275 {
       
   276 	TPtrC8 uuid = aDevice.Uuid();
       
   277 	TInt count = iPrinters.Count();
       
   278 	for (TInt i=0; i<count; i++)
       
   279 	{
       
   280 		TPtrC8 tempUuid = KNullDesC8();
       
   281 		if(!iPrinters[i]->IsDisappeared()) 
       
   282 			tempUuid.Set( (iPrinters[i])->Device()->Uuid() );
       
   283 			
       
   284 		if (uuid.Compare(tempUuid) == 0)
       
   285 			return i;
       
   286 	}
       
   287 	return KErrNotFound;
       
   288 }
       
   289 
       
   290 
       
   291 //--------------------------------------------------------------------------------------------
       
   292 //
       
   293 // CUPPrinterContainer::PrinterIndex
       
   294 //
       
   295 //--------------------------------------------------------------------------------------------
       
   296 TInt CUPPrinterContainer::PrinterIndex(TInt aId)
       
   297 {
       
   298 	TInt count = iPrinters.Count();
       
   299 	for (TInt i=0; i<count; i++)
       
   300 	{
       
   301 		if (aId == iPrinters[i]->Id())
       
   302 			return i;
       
   303 	}
       
   304 	
       
   305 	return KErrNotFound;
       
   306 }
       
   307 
       
   308 
       
   309 //--------------------------------------------------------------------------------------------
       
   310 //
       
   311 // CUPPrinterContainer::PrinterDisappeared
       
   312 //
       
   313 //--------------------------------------------------------------------------------------------
       
   314 void CUPPrinterContainer::PrinterDisappeared(CUpnpDevice& aDevice)
       
   315 {
       
   316   // Check if device is found from iPrinters array. If so add to disappeared array.
       
   317   TInt index = PrinterIndex(aDevice);
       
   318   if ( index >=0 )
       
   319   {
       
   320   	iPrinters[index]->SetDisappeared(ETrue);
       
   321   }
       
   322 }
       
   323 
       
   324 
       
   325 //--------------------------------------------------------------------------------------------
       
   326 //
       
   327 // CUPPrinterContainer::SyncPrinterArray
       
   328 //
       
   329 //--------------------------------------------------------------------------------------------
       
   330 void CUPPrinterContainer::SyncPrinterArray()
       
   331 {
       
   332 	for(TInt i=0; i<iPrinters.Count(); i++)
       
   333 	{
       
   334 		if (iPrinters[i]->IsDisappeared())
       
   335 		{
       
   336 			RemovePrinter(i);
       
   337 			i--;
       
   338 		}
       
   339 	}
       
   340 }
       
   341 
       
   342 //--------------------------------------------------------------------------------------------
       
   343 //
       
   344 // CUPPrinterContainer::UpdateCache
       
   345 //
       
   346 //--------------------------------------------------------------------------------------------
       
   347 void CUPPrinterContainer::UpdateCacheL()
       
   348 {
       
   349 	// Calculate size for buffer
       
   350 	TInt bufSize = 0;
       
   351 	for (TInt i=0; i < iPrinters.Count(); i++)
       
   352 	{
       
   353 		bufSize += 3; // Separators and line feed
       
   354 	
       
   355 		TBuf8<10> id;
       
   356 		id.AppendNum(iPrinters[i]->Id());
       
   357 		TBuf8<10> vendor;
       
   358 		vendor.AppendNum(iPrinters[i]->Vendor());
       
   359 		
       
   360 		bufSize += id.Length();
       
   361 		bufSize += iPrinters[i]->DisplayName().Length();
       
   362 		bufSize += vendor.Length();
       
   363 		bufSize += iPrinters[i]->UId().Length();
       
   364 	}
       
   365 	
       
   366 	// Create buffer
       
   367 	if(iCacheBuffer)
       
   368 	{
       
   369 		delete iCacheBuffer;
       
   370 		iCacheBuffer = NULL;
       
   371 	}
       
   372 
       
   373 	iCacheBuffer = HBufC8::NewL(bufSize);
       
   374 
       
   375 	// Write buffer
       
   376 	for (TInt i=0; i < iPrinters.Count(); i++)
       
   377 	{
       
   378 		iCacheBuffer->Des().AppendNum(iPrinters[i]->Id());
       
   379 		iCacheBuffer->Des().Append(KUPnPComma());
       
   380 		iCacheBuffer->Des().Append(iPrinters[i]->DisplayName());
       
   381 		iCacheBuffer->Des().Append(KUPnPComma());
       
   382 		iCacheBuffer->Des().AppendNum(iPrinters[i]->Vendor());
       
   383 		iCacheBuffer->Des().Append(KUPnPComma());
       
   384 		iCacheBuffer->Des().Append(iPrinters[i]->UId());
       
   385 		iCacheBuffer->Des().Append(KUPnPLineFeed8());
       
   386 	}
       
   387 
       
   388 	// Write buffer to file
       
   389 	RFile cacheFile;
       
   390 	TInt err = cacheFile.Replace( iFsSession, KUPnPPrinterCache, EFileWrite);
       
   391 	if (err == KErrNone)
       
   392 	{
       
   393 		err = cacheFile.Write(iCacheBuffer->Des());
       
   394 	}
       
   395 	cacheFile.Close();
       
   396 	User::LeaveIfError(err);
       
   397 }
       
   398 
       
   399 //--------------------------------------------------------------------------------------------
       
   400 //
       
   401 // CUPPrinterContainer::UpdateCache
       
   402 //
       
   403 //--------------------------------------------------------------------------------------------
       
   404 void CUPPrinterContainer::UpdateCacheL(TInt aDeviceId)
       
   405 {
       
   406 
       
   407 	// Get correct printer
       
   408 	CUPPrinter* printer = NULL;
       
   409 	for (TInt i=0; i < iPrinters.Count(); i++)
       
   410 	{
       
   411 		if(iPrinters[i]->Id() == aDeviceId)
       
   412 		{
       
   413 			printer = iPrinters[i];
       
   414 			break;
       
   415 		}
       
   416 	}
       
   417 	User::LeaveIfNull(printer);
       
   418 
       
   419 	// Calculate size for buffer
       
   420 	TInt bufSize = 0;
       
   421 	if(!printer->Cached())
       
   422 	{
       
   423 		TBuf8<10> id;
       
   424 		id.AppendNum(printer->Id());
       
   425 
       
   426 		TBuf8<10> vendor;
       
   427 		vendor.AppendNum(printer->Vendor());
       
   428 		vendor.Append(_L8(" "));
       
   429 
       
   430 		bufSize += id.Length();
       
   431 		bufSize += KUPnPComma().Length();
       
   432 		bufSize += printer->DisplayName().Length();
       
   433 		bufSize += KUPnPComma().Length();
       
   434 		bufSize += vendor.Length();
       
   435 		bufSize += KUPnPComma().Length();
       
   436 		bufSize += printer->UId().Length();
       
   437 		bufSize += KUPnPLineFeed().Length();
       
   438 
       
   439 		// Create buffer
       
   440 		HBufC8* fileBuffer = HBufC8::NewL(bufSize);
       
   441 		CleanupStack::PushL(fileBuffer);
       
   442 
       
   443 		fileBuffer->Des().AppendNum(printer->Id());
       
   444 		fileBuffer->Des().Append(KUPnPComma());
       
   445 		fileBuffer->Des().Append(printer->DisplayName());
       
   446 		fileBuffer->Des().Append(KUPnPComma());
       
   447 		fileBuffer->Des().Append(vendor);
       
   448 		fileBuffer->Des().Append(KUPnPComma());
       
   449 		fileBuffer->Des().Append(printer->UId());
       
   450 		fileBuffer->Des().Append(KUPnPLineFeed());
       
   451 
       
   452 
       
   453 		RFile cacheFile;
       
   454 		CleanupClosePushL(cacheFile);
       
   455 		TInt fSize = 0;
       
   456 		// Find end of file to write if exists
       
   457 		TInt err = cacheFile.Open( iFsSession, KUPnPPrinterCache, EFileWrite);
       
   458 		if (err == KErrNone)
       
   459 		{
       
   460 	    	cacheFile.Size(fSize);
       
   461 	    	++fSize;
       
   462 		}
       
   463 		else
       
   464 		{
       
   465 			// Write buffer to file
       
   466 			User::LeaveIfError(cacheFile.Replace( iFsSession, KUPnPPrinterCache, EFileWrite));
       
   467 		}		
       
   468 		User::LeaveIfError(cacheFile.Write(fSize, fileBuffer->Des()));
       
   469 		printer->SetCached(ETrue);
       
   470 
       
   471 		CleanupStack::PopAndDestroy(2); // fileBuffer, cacheFile
       
   472 	}
       
   473 }
       
   474 
       
   475 //--------------------------------------------------------------------------------------------
       
   476 //
       
   477 // CUPPrinterContainer::GetDisappearedPrinters
       
   478 //
       
   479 //--------------------------------------------------------------------------------------------
       
   480 void CUPPrinterContainer::GetDisappearedPrinters(RPointerArray<CUPPrinter>& aArray)
       
   481 {
       
   482 	for (TInt i=0; i < iPrinters.Count(); i++)
       
   483 	{
       
   484 		if (iPrinters[i]->IsDisappeared())
       
   485 		{
       
   486 			aArray.Append(iPrinters[i]);
       
   487 		}
       
   488 	}
       
   489 }
       
   490 
       
   491 ///////////////////////////
       
   492 // Protected functions
       
   493 ///////////////////////////
       
   494 //--------------------------------------------------------------------------------------------
       
   495 //
       
   496 // CUPPrinterContainer::ConstructL
       
   497 //
       
   498 //--------------------------------------------------------------------------------------------
       
   499 void CUPPrinterContainer::ConstructL()
       
   500 {
       
   501 	iCacheBuffer = NULL;
       
   502 
       
   503 	// Init file server session
       
   504 	User::LeaveIfError( iFsSession.Connect() );
       
   505 
       
   506 	TInt err = iFsSession.MkDirAll( KUPnPDataPath );
       
   507 	if (err != KErrNone && err != KErrAlreadyExists)
       
   508 	{
       
   509 	  User::Leave(err);
       
   510 	}
       
   511 	
       
   512 	User::LeaveIfError(iFsSession.SetSessionPath( KUPnPDataPath ));
       
   513 }
       
   514 
       
   515 ///////////////////////////
       
   516 // Private functions
       
   517 ///////////////////////////
       
   518 //--------------------------------------------------------------------------------------------
       
   519 //
       
   520 // CUPPrinterContainer::CUPPrinterContainer
       
   521 //
       
   522 //--------------------------------------------------------------------------------------------
       
   523 CUPPrinterContainer::CUPPrinterContainer()
       
   524 {
       
   525 	iIdCounter = 0;
       
   526 }
       
   527 
       
   528 
       
   529 //--------------------------------------------------------------------------------------------
       
   530 //
       
   531 // CUPPrinterContainer::ToCDevice
       
   532 //
       
   533 //--------------------------------------------------------------------------------------------
       
   534 CUpnpDevice* CUPPrinterContainer::ToCDevice(TInt aIndex)
       
   535 {
       
   536 	if (aIndex >= iPrinters.Count() || aIndex < 0 || iPrinters[aIndex]->IsDisappeared())	
       
   537 		return NULL;
       
   538 	return iPrinters[aIndex]->Device();
       
   539 }
       
   540 
       
   541 //--------------------------------------------------------------------------------------------
       
   542 //
       
   543 // CUPPrinterContainer::ReadCacheL
       
   544 //
       
   545 //--------------------------------------------------------------------------------------------
       
   546 void CUPPrinterContainer::ReadCacheL()
       
   547 {
       
   548 	LOG("[CUPPrinterContainer::ReadCacheL]\t");
       
   549 
       
   550 	ReadCacheFileL();
       
   551 
       
   552 	TPtr8 bufferPtr( iCacheBuffer->Des() );
       
   553 
       
   554 	// Add cached devices.
       
   555 	while (bufferPtr.Find(KUPnPLineFeed8()) >= 0)
       
   556 	{
       
   557 		// Read id
       
   558 		TInt separPos = bufferPtr.Find(KUPnPComma());
       
   559 		TInt lfPos = bufferPtr.Find(KUPnPLineFeed8());
       
   560 
       
   561 		if (lfPos < 1 || lfPos < separPos)
       
   562 		{
       
   563 			break; // No id could be read
       
   564 		}
       
   565 
       
   566 		TPtrC8 idStr = bufferPtr.Left(separPos);
       
   567 		TInt id;
       
   568 		TLex8 lex(idStr);
       
   569 		TInt error = lex.Val(id);
       
   570 		User::LeaveIfError( error );
       
   571 		
       
   572 		bufferPtr = bufferPtr.Right(bufferPtr.Length()-separPos-1);
       
   573 
       
   574 		// Read display name
       
   575 		separPos = bufferPtr.Find(KUPnPComma());
       
   576 		lfPos = bufferPtr.Find(KUPnPLineFeed8());
       
   577 
       
   578 		if (lfPos < 1 || lfPos < separPos)
       
   579 		{
       
   580 			break; // No name could be read
       
   581 		}
       
   582 
       
   583 		TPtrC8 name = bufferPtr.Left(separPos);
       
   584 		TBuf8<100> dispName;
       
   585 		dispName.Copy(name);
       
   586 		bufferPtr = bufferPtr.Right(bufferPtr.Length()-separPos-1);
       
   587 
       
   588 		// Read vendor
       
   589 		separPos = bufferPtr.Find(KUPnPComma());
       
   590 		lfPos = bufferPtr.Find(KUPnPLineFeed8());
       
   591 
       
   592 		if (lfPos < 1 || lfPos < separPos)
       
   593 		{
       
   594 			break; // No vendor could be read
       
   595 		}
       
   596 
       
   597 		TPtrC8 vendorStr = bufferPtr.Left(separPos);
       
   598 		TInt vendor;
       
   599 		TLex8 lexV(vendorStr);
       
   600 		TInt err = lexV.Val(vendor);
       
   601 		
       
   602 		// FIX: if error reading vendor, use default 0 and return err to none
       
   603     if ( err != KErrNone )
       
   604     {
       
   605       vendor = 0;
       
   606       err = KErrNone;
       
   607     }
       
   608 		
       
   609 		bufferPtr = bufferPtr.Right(bufferPtr.Length()-separPos-1);
       
   610 
       
   611 		//Read uid
       
   612 		separPos = bufferPtr.Find(KUPnPComma());
       
   613 		lfPos = bufferPtr.Find(KUPnPLineFeed8());
       
   614 
       
   615 		if ( (lfPos < 1) || (lfPos > separPos && separPos >= 0 ) )
       
   616 		{
       
   617 			break; // No uid could be read
       
   618 		}
       
   619 
       
   620 		TPtrC8 uid = bufferPtr.Left(lfPos);
       
   621 
       
   622 		// Add cached printer
       
   623 		if (err == KErrNone)
       
   624 		{
       
   625 			AddCachedPrinterL(id, uid, dispName, vendor);
       
   626 		}
       
   627 
       
   628 		bufferPtr = bufferPtr.Right(bufferPtr.Length()-lfPos-1);
       
   629 
       
   630 	}
       
   631 }
       
   632 
       
   633 //--------------------------------------------------------------------------------------------
       
   634 //
       
   635 // CUPPrinterContainer::FindCachedPrinterL
       
   636 //
       
   637 //--------------------------------------------------------------------------------------------
       
   638 TPtrC8 CUPPrinterContainer::FindCachedPrinterL(TInt aId, TInt& aStartPos, TInt& aEndPos)
       
   639 {
       
   640 	LOG("[CUPPrinterContainer::FindCachedPrinterL]\t");
       
   641 
       
   642 	User::LeaveIfNull(iCacheBuffer);
       
   643 	TPtrC8 bufferPtr( iCacheBuffer->Des() );
       
   644 
       
   645 	TBuf8<10> reqId;
       
   646 	reqId.AppendNum(aId);
       
   647 
       
   648 	aStartPos = 0;
       
   649 	aEndPos = 0;
       
   650 
       
   651 	// Find cached device.
       
   652 	while (bufferPtr.Find(KUPnPLineFeed8()) >= 0)
       
   653 	{
       
   654 		aStartPos = aEndPos;
       
   655 
       
   656 		// Read id
       
   657 		TInt separPos = bufferPtr.Find(KUPnPComma());
       
   658 		TInt lfPos = bufferPtr.Find(KUPnPLineFeed8());
       
   659 
       
   660 		if (lfPos < 1 || lfPos < separPos)
       
   661 		{
       
   662 			break; // No id could be read
       
   663 		}
       
   664 
       
   665 		aEndPos += lfPos+1;
       
   666 
       
   667 		LOG81("[CUPPrinterContainer::FindCachedPrinterL]\t bufferPtr: %S", &bufferPtr);
       
   668 
       
   669 		//Find next printer if id doesn't match
       
   670 		if(0 != reqId.Compare(bufferPtr.Left(separPos)))
       
   671 		{
       
   672 			bufferPtr.Set(bufferPtr.Right(bufferPtr.Length()-lfPos-1));
       
   673 			continue;
       
   674 		}
       
   675 		else
       
   676 		{
       
   677 			return bufferPtr.Left(aEndPos);
       
   678 		}
       
   679 	}
       
   680 
       
   681 	return NULL;
       
   682 }
       
   683 
       
   684 //--------------------------------------------------------------------------------------------
       
   685 //
       
   686 // CUPPrinterContainer::ReadCacheFileLC
       
   687 //
       
   688 //--------------------------------------------------------------------------------------------
       
   689 void CUPPrinterContainer::ReadCacheFileL()
       
   690 {
       
   691 	LOG("[CUPPrinterContainer::ReadCacheFileL]\t");
       
   692 
       
   693 	RFile cacheFile;
       
   694 	CleanupClosePushL(cacheFile);
       
   695 
       
   696 	TInt err = cacheFile.Open( iFsSession, KUPnPPrinterCache, EFileRead );
       
   697 
       
   698 	if (err == KErrNone)
       
   699 	{
       
   700 	
       
   701 		TInt fileSize = 0;
       
   702 		err = cacheFile.Size(fileSize);
       
   703 		if(err != KErrNone)
       
   704 			fileSize = KMaxCacheFileSize;
       
   705 	
       
   706 		iCacheBuffer = HBufC8::NewL(fileSize);
       
   707 	
       
   708 		TPtr8 bufferPtr( iCacheBuffer->Des() );
       
   709 		bufferPtr.Zero();
       
   710 	
       
   711 		err = cacheFile.Read(bufferPtr);
       
   712 	}
       
   713 
       
   714   if (err != KErrNone)
       
   715 	{
       
   716 		// Problem in opening or reading cache file
       
   717 		if (iCacheBuffer)
       
   718 		{
       
   719 				delete iCacheBuffer;
       
   720 				iCacheBuffer = NULL;
       
   721 		}
       
   722 		iCacheBuffer = HBufC8::NewL(0);
       
   723 		// printer not found from cache, leave!
       
   724 		User::Leave( KErrNotFound );
       
   725 	}
       
   726 
       
   727 	CleanupStack::PopAndDestroy(); //cacheFile
       
   728 }
       
   729 
       
   730 
       
   731 
       
   732 //--------------------------------------------------------------------------------------------
       
   733 //
       
   734 // CUPPrinterContainer::GenerateIdL
       
   735 //
       
   736 //--------------------------------------------------------------------------------------------
       
   737 TInt CUPPrinterContainer::GenerateId()
       
   738 {
       
   739 	TInt id = -1;
       
   740 	for (TInt i=1; i < iPrinters.Count()+2; i++)
       
   741 	{
       
   742 		TInt j;
       
   743 		for (j=0; j < iPrinters.Count(); j++)
       
   744 		{
       
   745 			if (iPrinters[j]->Id() == i)
       
   746 			{
       
   747 				break;
       
   748 			}
       
   749 		}
       
   750 
       
   751 		if (j == iPrinters.Count())
       
   752 		{
       
   753 			id = i;
       
   754 			break;
       
   755 		}
       
   756 	}
       
   757 
       
   758 	return id;
       
   759 }
       
   760 
       
   761 //--------------------------------------------------------------------------------------------
       
   762 //
       
   763 // CUPPrinterContainer::AddCachedPrinterL
       
   764 //
       
   765 //--------------------------------------------------------------------------------------------
       
   766 void CUPPrinterContainer::AddCachedPrinterL(TInt aId, TDesC8& aUPnPUId, TDesC8& aDisplayName, TInt aVendor)
       
   767 {
       
   768 	LOG("[CUPPrinterContainer::AddCachedPrinterL]");
       
   769 	CUPPrinter* newPrinter = CUPPrinter::NewL(aId, aUPnPUId, aDisplayName, aVendor);
       
   770 	newPrinter->SetCached(ETrue);
       
   771 	CleanupStack::PushL(newPrinter);
       
   772 	iPrinters.AppendL(newPrinter);
       
   773 	CleanupStack::Pop(newPrinter);
       
   774 }
       
   775 
       
   776 //  End of File