|
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 |
|
418 // Calculate size for buffer |
|
419 TInt bufSize = 0; |
|
420 if(!printer->Cached()) |
|
421 { |
|
422 TBuf8<10> id; |
|
423 id.AppendNum(printer->Id()); |
|
424 |
|
425 TBuf8<10> vendor; |
|
426 vendor.AppendNum(printer->Vendor()); |
|
427 vendor.Append(_L8(" ")); |
|
428 |
|
429 bufSize += id.Length(); |
|
430 bufSize += KUPnPComma().Length(); |
|
431 bufSize += printer->DisplayName().Length(); |
|
432 bufSize += KUPnPComma().Length(); |
|
433 bufSize += vendor.Length(); |
|
434 bufSize += KUPnPComma().Length(); |
|
435 bufSize += printer->UId().Length(); |
|
436 bufSize += KUPnPLineFeed().Length(); |
|
437 |
|
438 // Create buffer |
|
439 HBufC8* fileBuffer = HBufC8::NewL(bufSize); |
|
440 CleanupStack::PushL(fileBuffer); |
|
441 |
|
442 fileBuffer->Des().AppendNum(printer->Id()); |
|
443 fileBuffer->Des().Append(KUPnPComma()); |
|
444 fileBuffer->Des().Append(printer->DisplayName()); |
|
445 fileBuffer->Des().Append(KUPnPComma()); |
|
446 fileBuffer->Des().Append(vendor); |
|
447 fileBuffer->Des().Append(KUPnPComma()); |
|
448 fileBuffer->Des().Append(printer->UId()); |
|
449 fileBuffer->Des().Append(KUPnPLineFeed()); |
|
450 |
|
451 |
|
452 RFile cacheFile; |
|
453 CleanupClosePushL(cacheFile); |
|
454 TInt fSize = 0; |
|
455 // Find end of file to write if exists |
|
456 TInt err = cacheFile.Open( iFsSession, KUPnPPrinterCache, EFileWrite); |
|
457 if (err == KErrNone) |
|
458 { |
|
459 cacheFile.Size(fSize); |
|
460 ++fSize; |
|
461 } |
|
462 else |
|
463 { |
|
464 // Write buffer to file |
|
465 User::LeaveIfError(cacheFile.Replace( iFsSession, KUPnPPrinterCache, EFileWrite)); |
|
466 } |
|
467 User::LeaveIfError(cacheFile.Write(fSize, fileBuffer->Des())); |
|
468 printer->SetCached(ETrue); |
|
469 |
|
470 CleanupStack::PopAndDestroy(2); // fileBuffer, cacheFile |
|
471 } |
|
472 } |
|
473 |
|
474 //-------------------------------------------------------------------------------------------- |
|
475 // |
|
476 // CUPPrinterContainer::GetDisappearedPrinters |
|
477 // |
|
478 //-------------------------------------------------------------------------------------------- |
|
479 void CUPPrinterContainer::GetDisappearedPrinters(RPointerArray<CUPPrinter>& aArray) |
|
480 { |
|
481 for (TInt i=0; i < iPrinters.Count(); i++) |
|
482 { |
|
483 if (iPrinters[i]->IsDisappeared()) |
|
484 { |
|
485 aArray.Append(iPrinters[i]); |
|
486 } |
|
487 } |
|
488 } |
|
489 |
|
490 /////////////////////////// |
|
491 // Protected functions |
|
492 /////////////////////////// |
|
493 //-------------------------------------------------------------------------------------------- |
|
494 // |
|
495 // CUPPrinterContainer::ConstructL |
|
496 // |
|
497 //-------------------------------------------------------------------------------------------- |
|
498 void CUPPrinterContainer::ConstructL() |
|
499 { |
|
500 iCacheBuffer = NULL; |
|
501 |
|
502 // Init file server session |
|
503 User::LeaveIfError( iFsSession.Connect() ); |
|
504 |
|
505 TInt err = iFsSession.MkDirAll( KUPnPDataPath ); |
|
506 if (err != KErrNone && err != KErrAlreadyExists) |
|
507 { |
|
508 User::Leave(err); |
|
509 } |
|
510 |
|
511 User::LeaveIfError(iFsSession.SetSessionPath( KUPnPDataPath )); |
|
512 } |
|
513 |
|
514 /////////////////////////// |
|
515 // Private functions |
|
516 /////////////////////////// |
|
517 //-------------------------------------------------------------------------------------------- |
|
518 // |
|
519 // CUPPrinterContainer::CUPPrinterContainer |
|
520 // |
|
521 //-------------------------------------------------------------------------------------------- |
|
522 CUPPrinterContainer::CUPPrinterContainer() |
|
523 { |
|
524 iIdCounter = 0; |
|
525 } |
|
526 |
|
527 |
|
528 //-------------------------------------------------------------------------------------------- |
|
529 // |
|
530 // CUPPrinterContainer::ToCDevice |
|
531 // |
|
532 //-------------------------------------------------------------------------------------------- |
|
533 CUpnpDevice* CUPPrinterContainer::ToCDevice(TInt aIndex) |
|
534 { |
|
535 if (aIndex >= iPrinters.Count() || aIndex < 0 || iPrinters[aIndex]->IsDisappeared()) |
|
536 return NULL; |
|
537 return iPrinters[aIndex]->Device(); |
|
538 } |
|
539 |
|
540 //-------------------------------------------------------------------------------------------- |
|
541 // |
|
542 // CUPPrinterContainer::ReadCacheL |
|
543 // |
|
544 //-------------------------------------------------------------------------------------------- |
|
545 void CUPPrinterContainer::ReadCacheL() |
|
546 { |
|
547 LOG("[CUPPrinterContainer::ReadCacheL]\t"); |
|
548 |
|
549 ReadCacheFileL(); |
|
550 |
|
551 TPtr8 bufferPtr( iCacheBuffer->Des() ); |
|
552 |
|
553 // Add cached devices. |
|
554 while (bufferPtr.Find(KUPnPLineFeed8()) >= 0) |
|
555 { |
|
556 // Read id |
|
557 TInt separPos = bufferPtr.Find(KUPnPComma()); |
|
558 TInt lfPos = bufferPtr.Find(KUPnPLineFeed8()); |
|
559 |
|
560 if (lfPos < 1 || lfPos < separPos) |
|
561 { |
|
562 break; // No id could be read |
|
563 } |
|
564 |
|
565 TPtrC8 idStr = bufferPtr.Left(separPos); |
|
566 TInt id; |
|
567 TLex8 lex(idStr); |
|
568 TInt error = lex.Val(id); |
|
569 User::LeaveIfError( error ); |
|
570 |
|
571 bufferPtr = bufferPtr.Right(bufferPtr.Length()-separPos-1); |
|
572 |
|
573 // Read display name |
|
574 separPos = bufferPtr.Find(KUPnPComma()); |
|
575 lfPos = bufferPtr.Find(KUPnPLineFeed8()); |
|
576 |
|
577 if (lfPos < 1 || lfPos < separPos) |
|
578 { |
|
579 break; // No name could be read |
|
580 } |
|
581 |
|
582 TPtrC8 name = bufferPtr.Left(separPos); |
|
583 TBuf8<100> dispName; |
|
584 dispName.Copy(name); |
|
585 bufferPtr = bufferPtr.Right(bufferPtr.Length()-separPos-1); |
|
586 |
|
587 // Read vendor |
|
588 separPos = bufferPtr.Find(KUPnPComma()); |
|
589 lfPos = bufferPtr.Find(KUPnPLineFeed8()); |
|
590 |
|
591 if (lfPos < 1 || lfPos < separPos) |
|
592 { |
|
593 break; // No vendor could be read |
|
594 } |
|
595 |
|
596 TPtrC8 vendorStr = bufferPtr.Left(separPos); |
|
597 TInt vendor; |
|
598 TLex8 lexV(vendorStr); |
|
599 TInt err = lexV.Val(vendor); |
|
600 |
|
601 // FIX: if error reading vendor, use default 0 and return err to none |
|
602 if ( err != KErrNone ) |
|
603 { |
|
604 vendor = 0; |
|
605 err = KErrNone; |
|
606 } |
|
607 |
|
608 bufferPtr = bufferPtr.Right(bufferPtr.Length()-separPos-1); |
|
609 |
|
610 //Read uid |
|
611 separPos = bufferPtr.Find(KUPnPComma()); |
|
612 lfPos = bufferPtr.Find(KUPnPLineFeed8()); |
|
613 |
|
614 if ( (lfPos < 1) || (lfPos > separPos && separPos >= 0 ) ) |
|
615 { |
|
616 break; // No uid could be read |
|
617 } |
|
618 |
|
619 TPtrC8 uid = bufferPtr.Left(lfPos); |
|
620 |
|
621 // Add cached printer |
|
622 if (err == KErrNone) |
|
623 { |
|
624 AddCachedPrinterL(id, uid, dispName, vendor); |
|
625 } |
|
626 |
|
627 bufferPtr = bufferPtr.Right(bufferPtr.Length()-lfPos-1); |
|
628 |
|
629 } |
|
630 } |
|
631 |
|
632 //-------------------------------------------------------------------------------------------- |
|
633 // |
|
634 // CUPPrinterContainer::FindCachedPrinterL |
|
635 // |
|
636 //-------------------------------------------------------------------------------------------- |
|
637 TPtrC8 CUPPrinterContainer::FindCachedPrinterL(TInt aId, TInt& aStartPos, TInt& aEndPos) |
|
638 { |
|
639 LOG("[CUPPrinterContainer::FindCachedPrinterL]\t"); |
|
640 |
|
641 User::LeaveIfNull(iCacheBuffer); |
|
642 TPtrC8 bufferPtr( iCacheBuffer->Des() ); |
|
643 |
|
644 TBuf8<10> reqId; |
|
645 reqId.AppendNum(aId); |
|
646 |
|
647 aStartPos = 0; |
|
648 aEndPos = 0; |
|
649 |
|
650 // Find cached device. |
|
651 while (bufferPtr.Find(KUPnPLineFeed8()) >= 0) |
|
652 { |
|
653 aStartPos = aEndPos; |
|
654 |
|
655 // Read id |
|
656 TInt separPos = bufferPtr.Find(KUPnPComma()); |
|
657 TInt lfPos = bufferPtr.Find(KUPnPLineFeed8()); |
|
658 |
|
659 if (lfPos < 1 || lfPos < separPos) |
|
660 { |
|
661 break; // No id could be read |
|
662 } |
|
663 |
|
664 aEndPos += lfPos+1; |
|
665 |
|
666 LOG81("[CUPPrinterContainer::FindCachedPrinterL]\t bufferPtr: %S", &bufferPtr); |
|
667 |
|
668 //Find next printer if id doesn't match |
|
669 if(0 != reqId.Compare(bufferPtr.Left(separPos))) |
|
670 { |
|
671 bufferPtr.Set(bufferPtr.Right(bufferPtr.Length()-lfPos-1)); |
|
672 continue; |
|
673 } |
|
674 else |
|
675 { |
|
676 return bufferPtr.Left(aEndPos); |
|
677 } |
|
678 } |
|
679 |
|
680 return NULL; |
|
681 } |
|
682 |
|
683 //-------------------------------------------------------------------------------------------- |
|
684 // |
|
685 // CUPPrinterContainer::ReadCacheFileLC |
|
686 // |
|
687 //-------------------------------------------------------------------------------------------- |
|
688 void CUPPrinterContainer::ReadCacheFileL() |
|
689 { |
|
690 LOG("[CUPPrinterContainer::ReadCacheFileL]\t"); |
|
691 |
|
692 RFile cacheFile; |
|
693 CleanupClosePushL(cacheFile); |
|
694 |
|
695 TInt err = cacheFile.Open( iFsSession, KUPnPPrinterCache, EFileRead ); |
|
696 |
|
697 if (err == KErrNone) |
|
698 { |
|
699 |
|
700 TInt fileSize = 0; |
|
701 err = cacheFile.Size(fileSize); |
|
702 if(err != KErrNone) |
|
703 fileSize = KMaxCacheFileSize; |
|
704 |
|
705 iCacheBuffer = HBufC8::NewL(fileSize); |
|
706 |
|
707 TPtr8 bufferPtr( iCacheBuffer->Des() ); |
|
708 bufferPtr.Zero(); |
|
709 |
|
710 err = cacheFile.Read(bufferPtr); |
|
711 } |
|
712 |
|
713 if (err != KErrNone) |
|
714 { |
|
715 // Problem in opening or reading cache file |
|
716 if (iCacheBuffer) |
|
717 { |
|
718 delete iCacheBuffer; |
|
719 iCacheBuffer = NULL; |
|
720 } |
|
721 iCacheBuffer = HBufC8::NewL(0); |
|
722 // printer not found from cache, leave! |
|
723 User::Leave( KErrNotFound ); |
|
724 } |
|
725 |
|
726 CleanupStack::PopAndDestroy(); //cacheFile |
|
727 } |
|
728 |
|
729 |
|
730 |
|
731 //-------------------------------------------------------------------------------------------- |
|
732 // |
|
733 // CUPPrinterContainer::GenerateIdL |
|
734 // |
|
735 //-------------------------------------------------------------------------------------------- |
|
736 TInt CUPPrinterContainer::GenerateId() |
|
737 { |
|
738 TInt id = -1; |
|
739 for (TInt i=1; i < iPrinters.Count()+2; i++) |
|
740 { |
|
741 TInt j; |
|
742 for (j=0; j < iPrinters.Count(); j++) |
|
743 { |
|
744 if (iPrinters[j]->Id() == i) |
|
745 { |
|
746 break; |
|
747 } |
|
748 } |
|
749 |
|
750 if (j == iPrinters.Count()) |
|
751 { |
|
752 id = i; |
|
753 break; |
|
754 } |
|
755 } |
|
756 |
|
757 return id; |
|
758 } |
|
759 |
|
760 //-------------------------------------------------------------------------------------------- |
|
761 // |
|
762 // CUPPrinterContainer::AddCachedPrinterL |
|
763 // |
|
764 //-------------------------------------------------------------------------------------------- |
|
765 void CUPPrinterContainer::AddCachedPrinterL(TInt aId, TDesC8& aUPnPUId, TDesC8& aDisplayName, TInt aVendor) |
|
766 { |
|
767 LOG("[CUPPrinterContainer::AddCachedPrinterL]"); |
|
768 CUPPrinter* newPrinter = CUPPrinter::NewL(aId, aUPnPUId, aDisplayName, aVendor); |
|
769 newPrinter->SetCached(ETrue); |
|
770 CleanupStack::PushL(newPrinter); |
|
771 iPrinters.AppendL(newPrinter); |
|
772 CleanupStack::Pop(newPrinter); |
|
773 } |
|
774 |
|
775 // End of File |