|
1 /* |
|
2 * Copyright (c) 2006-2009 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 the License "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: |
|
15 * cpackageinfo.cpp |
|
16 * This file provides implementation for CIoHandler class |
|
17 * CIoHandler: This class handles all the I/O activities like reading from |
|
18 * console, writing to a log file, etc. |
|
19 * @internalComponent |
|
20 * |
|
21 */ |
|
22 |
|
23 #include <e32cons.h> // ConsoleBase |
|
24 |
|
25 // User includes |
|
26 #include "ciohandler.h" |
|
27 #include "clogger.h" |
|
28 #include "cpreferencehandler.h" |
|
29 #include "swiconsoleerrors.h" |
|
30 |
|
31 // Constants and literals |
|
32 const TInt KProgressBarFactor = 3; |
|
33 const TInt KCursorOffset = 2; |
|
34 const TInt KProgressOffset = 1; |
|
35 const TInt KDateAdjustment = 1; |
|
36 const TInt KAllocationBlock = 50; |
|
37 |
|
38 _LIT(KCurProgressSymbol, "#"); |
|
39 _LIT(KCompleteProgressSymbol, "-"); |
|
40 _LIT(KDateFormatString, "%d/%d/%d"); |
|
41 _LIT(KConsoleName, "SwiConsole"); |
|
42 |
|
43 |
|
44 CIoHandler::CIoHandler(): |
|
45 iIsLoggingEnabled(EFalse), |
|
46 iIsProgressBarEnabled(EFalse) |
|
47 { |
|
48 } |
|
49 |
|
50 CIoHandler::~CIoHandler() |
|
51 { |
|
52 iResourceFile.Close(); |
|
53 iFileSession.Close(); |
|
54 delete iLogger; |
|
55 delete iConsole; |
|
56 } |
|
57 |
|
58 // Second phase constructor |
|
59 void CIoHandler::ConstructL() |
|
60 { |
|
61 iConsole = Console::NewL(KConsoleName,TSize(KConsFullScreen,KConsFullScreen)); |
|
62 iProgressBarLen = iConsole->ScreenSize().iWidth; |
|
63 iProgressBarLen -= iProgressBarLen/KProgressBarFactor; |
|
64 |
|
65 User::LeaveIfError(iFileSession.Connect()); |
|
66 _LIT(KResourceFile, "swiconsole.rsc"); |
|
67 TFileName privatePath; |
|
68 iFileSession.PrivatePath(privatePath); |
|
69 |
|
70 TFileName resourceFilename(_L("Z:")); |
|
71 resourceFilename.Append(privatePath); |
|
72 resourceFilename.Append(KResourceFile()); |
|
73 |
|
74 iResourceFile.OpenL(iFileSession, resourceFilename); |
|
75 } |
|
76 |
|
77 CIoHandler* CIoHandler::NewL() |
|
78 { |
|
79 CIoHandler* self = NewLC(); |
|
80 CleanupStack::Pop(self); |
|
81 return self; |
|
82 } |
|
83 |
|
84 CIoHandler* CIoHandler::NewLC() |
|
85 { |
|
86 CIoHandler* self = new (ELeave) CIoHandler(); |
|
87 CleanupStack::PushL(self); |
|
88 self->ConstructL(); |
|
89 return self; |
|
90 } |
|
91 |
|
92 HBufC* CIoHandler::GetStringFromResourceLC(TInt aStringUid) const |
|
93 { |
|
94 HBufC8* dataBuffer = iResourceFile.AllocReadLC(aStringUid); |
|
95 |
|
96 TResourceReader resourceReader; |
|
97 resourceReader.SetBuffer(dataBuffer); |
|
98 |
|
99 TInt bufferLength = dataBuffer->Length(); |
|
100 |
|
101 HBufC* stringBuffer = HBufC::NewL(bufferLength); |
|
102 TPtr ptrString = stringBuffer->Des(); |
|
103 ptrString.Copy(resourceReader.ReadTPtrC()); |
|
104 |
|
105 // clean up data buffer |
|
106 CleanupStack::PopAndDestroy(); // finished with dataBuffer |
|
107 CleanupStack::PushL(stringBuffer); |
|
108 |
|
109 return stringBuffer; |
|
110 } |
|
111 |
|
112 |
|
113 void CIoHandler::GetStringFromResourceL(TDes& aString, TInt aStringUid) const |
|
114 { |
|
115 HBufC8* dataBuffer = iResourceFile.AllocReadLC(aStringUid); |
|
116 |
|
117 TResourceReader resourceReader; |
|
118 resourceReader.SetBuffer(dataBuffer); |
|
119 |
|
120 __ASSERT_DEBUG(resourceReader.ReadTPtrC().Length() < aString.MaxLength(), |
|
121 User::Panic(KGeneralPanicString, KInvalidArgument)); |
|
122 |
|
123 aString.Copy(resourceReader.ReadTPtrC()); |
|
124 |
|
125 // clean up data buffer |
|
126 CleanupStack::PopAndDestroy(); // finished with dataBuffer |
|
127 } |
|
128 |
|
129 void CIoHandler::WriteToConsoleL(const TDesC& aString) const |
|
130 { |
|
131 if( iIsProgressBarEnabled) |
|
132 { |
|
133 // This piece of code is used to redraw progress bar |
|
134 TSize screenSize = iConsole->ScreenSize(); |
|
135 |
|
136 if(iConsole->WhereY() == screenSize.iHeight - KProgressOffset) |
|
137 { |
|
138 // Clear the current progress bar |
|
139 ClearLine(); |
|
140 // write the string |
|
141 iConsole->Printf(aString); |
|
142 // Save the cursor position |
|
143 TPoint curPos = iConsole->CursorPos(); |
|
144 |
|
145 // If the string passed is not a line breaker |
|
146 // then add line breaker |
|
147 if(aString.Compare(KLineBreaker)) |
|
148 { |
|
149 iConsole->Printf(KLineBreaker); |
|
150 --curPos.iY; |
|
151 } |
|
152 |
|
153 RedrawProgressBarL(); |
|
154 iConsole->SetPos(curPos.iX, curPos.iY); |
|
155 return; |
|
156 } |
|
157 } |
|
158 iConsole->Printf(aString); |
|
159 } |
|
160 |
|
161 |
|
162 void CIoHandler::WriteL(const TDesC& aString, TIoType aIoType) const |
|
163 { |
|
164 switch(aIoType) |
|
165 { |
|
166 case EIoBoth: |
|
167 case EIoConsoleType: |
|
168 { |
|
169 WriteToConsoleL(aString); |
|
170 if(EIoBoth != aIoType) |
|
171 { |
|
172 break; |
|
173 } |
|
174 } |
|
175 case EIoLogType: |
|
176 { |
|
177 if(!iIsLoggingEnabled) |
|
178 { |
|
179 break; |
|
180 } |
|
181 if(!aString.Compare(KLineBreaker)) |
|
182 { |
|
183 iLogger->FlushLog(); |
|
184 break; |
|
185 } |
|
186 iLogger->WriteL(aString); |
|
187 break; |
|
188 } |
|
189 default: |
|
190 { |
|
191 User::Leave(KSwiInvalidSwitchCase); |
|
192 } |
|
193 } |
|
194 } |
|
195 |
|
196 void CIoHandler::WriteLineL(const TDesC& aString, TIoType aIoType) const |
|
197 { |
|
198 switch(aIoType) |
|
199 { |
|
200 case EIoBoth: |
|
201 case EIoConsoleType: |
|
202 { |
|
203 WriteToConsoleL(aString); |
|
204 WriteToConsoleL(KLineBreaker); |
|
205 if(EIoBoth != aIoType) |
|
206 { |
|
207 break; |
|
208 } |
|
209 } |
|
210 case EIoLogType: |
|
211 { |
|
212 if(!iIsLoggingEnabled) |
|
213 { |
|
214 break; |
|
215 } |
|
216 iLogger->WriteAndFlushL(aString); |
|
217 break; |
|
218 } |
|
219 default: |
|
220 { |
|
221 User::Leave(KSwiInvalidSwitchCase); |
|
222 } |
|
223 } |
|
224 } |
|
225 |
|
226 void CIoHandler::WriteToPageL(const TDesC& aString, TBool aInsertLineBreak) const |
|
227 { |
|
228 TSize screenSize = iConsole->ScreenSize(); |
|
229 if(iConsole->WhereY() >= screenSize.iHeight - KCursorOffset) |
|
230 { |
|
231 iConsole->Printf(KLineBreaker); |
|
232 if(!iPreferenceHandler || ESwiNormalMode == iPreferenceHandler->GetOperationMode()) |
|
233 { |
|
234 PauseScreenL(); |
|
235 } |
|
236 ClearConsoleL(); |
|
237 } |
|
238 WriteToConsoleL(aString); |
|
239 if(aInsertLineBreak) |
|
240 { |
|
241 WriteToConsoleL(KLineBreaker); |
|
242 } |
|
243 } |
|
244 |
|
245 |
|
246 void CIoHandler::WriteL(TInt aResourceID, TIoType aIoType) const |
|
247 { |
|
248 HBufC *string = GetStringFromResourceLC(aResourceID); |
|
249 WriteL(*string, aIoType); |
|
250 CleanupStack::PopAndDestroy(string); |
|
251 } |
|
252 |
|
253 void CIoHandler::WriteLineL(TInt aResourceID, TIoType aIoType) const |
|
254 { |
|
255 HBufC *string = GetStringFromResourceLC(aResourceID); |
|
256 WriteLineL(*string, aIoType); |
|
257 CleanupStack::PopAndDestroy(string); |
|
258 } |
|
259 |
|
260 void CIoHandler::WriteToPageL(TInt aResourceID, TBool aInsertLineBreak) const |
|
261 { |
|
262 TSize screenSize = iConsole->ScreenSize(); |
|
263 if(iConsole->WhereY() >= screenSize.iHeight - KCursorOffset) |
|
264 { |
|
265 if(!iPreferenceHandler || ESwiNormalMode == iPreferenceHandler->GetOperationMode()) |
|
266 { |
|
267 PauseScreenL(); |
|
268 } |
|
269 ClearConsoleL(); |
|
270 } |
|
271 HBufC *string = GetStringFromResourceLC(aResourceID); |
|
272 WriteToConsoleL(*string); |
|
273 if(aInsertLineBreak) |
|
274 { |
|
275 WriteToConsoleL(KLineBreaker); |
|
276 } |
|
277 CleanupStack::PopAndDestroy(string); |
|
278 } |
|
279 |
|
280 |
|
281 void CIoHandler::WriteL(const TDateTime& aDate, TIoType aIoType) const |
|
282 { |
|
283 TBuf<KDateStringLen> dateString; |
|
284 dateString.Format(KDateFormatString, aDate.Day() + KDateAdjustment, aDate.Month() + KDateAdjustment, aDate.Year()); |
|
285 WriteL(dateString, aIoType); |
|
286 } |
|
287 |
|
288 void CIoHandler::WriteLineL(const TDateTime& aDate, TIoType aIoType) const |
|
289 { |
|
290 TBuf<KDateStringLen> dateString; |
|
291 dateString.Format(KDateFormatString, aDate.Day() + KDateAdjustment, aDate.Month() + KDateAdjustment, aDate.Year()); |
|
292 WriteLineL(dateString, aIoType); |
|
293 } |
|
294 |
|
295 void CIoHandler::WriteErrorMsgL(const TDesC& aString) const |
|
296 { |
|
297 ClearLine(); |
|
298 iConsole->Printf(aString); |
|
299 TInt initialPos = iConsole->WhereY(); |
|
300 iConsole->Printf(KLineBreaker); |
|
301 TInt finalPos = iConsole->WhereY(); |
|
302 ClearLine(); |
|
303 HBufC *string = GetStringFromResourceLC(R_PRESS_ANY_KEY_MSG); |
|
304 iConsole->Printf(*string); |
|
305 CleanupStack::PopAndDestroy(string); |
|
306 iConsole->Getch(); |
|
307 if(initialPos == finalPos) |
|
308 { |
|
309 --initialPos; |
|
310 } |
|
311 ClearLine(); |
|
312 RedrawProgressBarL(); |
|
313 iConsole->SetPos(0, initialPos); |
|
314 ClearLine(); |
|
315 } |
|
316 |
|
317 void CIoHandler::WriteErrorMsgL(TInt aResourceID) const |
|
318 { |
|
319 HBufC *string = GetStringFromResourceLC(aResourceID); |
|
320 WriteErrorMsgL(*string); |
|
321 CleanupStack::PopAndDestroy(string); |
|
322 } |
|
323 |
|
324 TBool CIoHandler::ReadStringFromConsoleL(TDes& aString, TBool aLimitedLength) |
|
325 { |
|
326 // This infinte loop terminates when user hits an "enter" key |
|
327 // Or the maximum size of the buffer is hit |
|
328 FOREVER |
|
329 { |
|
330 // Get a key(character) from the console |
|
331 TKeyCode ch = iConsole->Getch(); |
|
332 |
|
333 switch(ch) |
|
334 { |
|
335 case EKeyEnter: |
|
336 { |
|
337 return EFalse; |
|
338 } |
|
339 case EKeyBackspace: |
|
340 { |
|
341 if(0 != aString.Length()) |
|
342 { |
|
343 // Back-space only takes the cursor one position back |
|
344 // So to delete a character blank-space is inserted at |
|
345 // that position and later cursor is again adjusted. |
|
346 iConsole->Printf(_L("%c%c%c"), EKeyBackspace, |
|
347 EKeySpace, |
|
348 EKeyBackspace); |
|
349 // Delete the character from the target string also. |
|
350 aString.Delete(aString.Length() - 1, 1); |
|
351 } |
|
352 break; |
|
353 } |
|
354 case EKeyEscape: |
|
355 { |
|
356 User::Leave(KErrCancel); |
|
357 } |
|
358 default: |
|
359 { |
|
360 // IsCharacterKey will return true if ch is a displayable |
|
361 // character else it will return false. |
|
362 if(IsCharacterKey(ch)) |
|
363 { |
|
364 TInt maxBufferLength = aString.MaxLength(); |
|
365 if(aString.Length() == maxBufferLength) |
|
366 { |
|
367 continue; |
|
368 } |
|
369 iConsole->Printf(_L("%c"), ch); |
|
370 aString.Append(ch); |
|
371 if(aString.Length() == maxBufferLength && !aLimitedLength) |
|
372 { |
|
373 return ETrue; |
|
374 } |
|
375 } |
|
376 } |
|
377 } |
|
378 } |
|
379 } |
|
380 |
|
381 void CIoHandler::ReadStringL(TDes& aString) |
|
382 { |
|
383 aString.Zero(); |
|
384 ReadStringFromConsoleL(aString); |
|
385 } |
|
386 |
|
387 |
|
388 void CIoHandler::ReadStringL(RBuf& aString) |
|
389 { |
|
390 aString.Zero(); |
|
391 while(ReadStringFromConsoleL(aString, EFalse)) |
|
392 { |
|
393 aString.ReAllocL(aString.MaxLength() + KAllocationBlock); |
|
394 } |
|
395 } |
|
396 |
|
397 TInt CIoHandler::ReadIntL(TInt& aValue) |
|
398 { |
|
399 TBuf<KIntStringLen> string; |
|
400 ReadStringL(string); |
|
401 TLex lexObj(string); |
|
402 return lexObj.Val(aValue); |
|
403 } |
|
404 |
|
405 void CIoHandler::ReadIntArrayL(RArray<TInt>& aIntArr, TBool aAllowDuplicates) |
|
406 { |
|
407 RBuf string; |
|
408 string.Create(KIntArrayStringLen); |
|
409 CleanupClosePushL(string); |
|
410 ReadStringL(string); |
|
411 string.TrimLeft(); |
|
412 |
|
413 if(0 == string.Length()) |
|
414 { |
|
415 return; |
|
416 } |
|
417 |
|
418 TBool bFlag = ETrue; |
|
419 |
|
420 do |
|
421 { |
|
422 TInt pos = string.Find(_L(" ")); |
|
423 |
|
424 if(KErrNotFound == pos) |
|
425 { |
|
426 pos = string.Length(); |
|
427 bFlag = EFalse; |
|
428 } |
|
429 |
|
430 TInt number = 0; |
|
431 TLex lexObj(string); |
|
432 User::LeaveIfError(lexObj.Val(number)); |
|
433 |
|
434 if(aAllowDuplicates) |
|
435 { |
|
436 aIntArr.Append(number); |
|
437 } |
|
438 else |
|
439 { |
|
440 aIntArr.InsertInOrder(number); |
|
441 } |
|
442 |
|
443 string.Delete(0, pos); |
|
444 |
|
445 string.TrimLeft(); |
|
446 }while(bFlag && string.Length() > 0); |
|
447 |
|
448 CleanupStack::PopAndDestroy(&string); |
|
449 } |
|
450 |
|
451 void CIoHandler::InitLoggingL(TDesC& aLogFile) |
|
452 { |
|
453 __ASSERT_DEBUG(!iIsLoggingEnabled, User::Panic(KGeneralPanicString, KErrArgument)); |
|
454 iIsLoggingEnabled = ETrue; |
|
455 iLogger = CLogger::NewL(aLogFile); |
|
456 |
|
457 HBufC *string = GetStringFromResourceLC(R_LOG_LINE_MSG); |
|
458 iLogger->WriteAndFlushL(*string); |
|
459 CleanupStack::PopAndDestroy(string); |
|
460 } |
|
461 |
|
462 void CIoHandler::SetLoggingState(TBool aEnableLogging) |
|
463 { |
|
464 iIsLoggingEnabled = aEnableLogging; |
|
465 } |
|
466 |
|
467 void CIoHandler::ClearConsoleL() const |
|
468 { |
|
469 iConsole->ClearScreen(); |
|
470 RedrawProgressBarL(); |
|
471 } |
|
472 |
|
473 void CIoHandler::ClearLine() const |
|
474 { |
|
475 iConsole->SetPos(0); |
|
476 iConsole->ClearToEndOfLine(); |
|
477 } |
|
478 |
|
479 |
|
480 void CIoHandler::PauseScreenL() const |
|
481 { |
|
482 HBufC *string = GetStringFromResourceLC(R_PRESS_ANY_KEY_MSG); |
|
483 WriteL(*string, EIoConsoleType); |
|
484 CleanupStack::PopAndDestroy(string); |
|
485 iConsole->Getch(); |
|
486 iConsole->Printf(KLineBreaker); |
|
487 } |
|
488 |
|
489 TInt CIoHandler::ProgressBarLength() const |
|
490 { |
|
491 return (iCurProgressValue * iProgressBarLen)/iFinalProgressValue; |
|
492 } |
|
493 void CIoHandler::SetFinalProgressValueL(TInt aFinalValue) |
|
494 { |
|
495 if(0 == aFinalValue) |
|
496 { |
|
497 return; |
|
498 } |
|
499 iFinalProgressValue = aFinalValue; |
|
500 iCurProgressValue = iProgressCursor = 0; |
|
501 iIsProgressBarEnabled = ETrue; |
|
502 |
|
503 TPoint curPos = iConsole->CursorPos(); |
|
504 TSize screenSize = iConsole->ScreenSize(); |
|
505 |
|
506 iConsole->SetPos(0, screenSize.iHeight - KProgressOffset); |
|
507 |
|
508 HBufC *string = GetStringFromResourceLC(R_PROGRESS_INFO); |
|
509 iConsole->Printf(*string); |
|
510 CleanupStack::PopAndDestroy(string); |
|
511 |
|
512 iProgressStartPos = iConsole->WhereX(); |
|
513 for(TInt i = iProgressBarLen; i >= 0; --i) |
|
514 { |
|
515 iConsole->Printf(KCompleteProgressSymbol); |
|
516 } |
|
517 |
|
518 iConsole->SetPos(curPos.iX, curPos.iY); |
|
519 } |
|
520 |
|
521 void CIoHandler::UpdateProgressBar(TInt aUpdateValue) |
|
522 { |
|
523 if(!iIsProgressBarEnabled) |
|
524 { |
|
525 return; |
|
526 } |
|
527 TPoint curPos = iConsole->CursorPos(); |
|
528 TSize screenSize = iConsole->ScreenSize(); |
|
529 iCurProgressValue += aUpdateValue; |
|
530 TInt progessBarLen = ProgressBarLength(); |
|
531 if(progessBarLen > iProgressCursor) |
|
532 { |
|
533 iConsole->SetPos(iProgressStartPos + iProgressCursor, screenSize.iHeight - KProgressOffset); |
|
534 for(TInt i = progessBarLen - iProgressCursor; i > 0; --i) |
|
535 { |
|
536 iConsole->Printf(KCurProgressSymbol); |
|
537 } |
|
538 iProgressCursor = progessBarLen; |
|
539 } |
|
540 iConsole->SetPos(curPos.iX, curPos.iY); |
|
541 } |
|
542 |
|
543 void CIoHandler::RedrawProgressBarL() const |
|
544 { |
|
545 if(!iIsProgressBarEnabled) |
|
546 { |
|
547 return; |
|
548 } |
|
549 TSize screenSize = iConsole->ScreenSize(); |
|
550 iConsole->SetPos(0, screenSize.iHeight - KProgressOffset); |
|
551 |
|
552 HBufC *string = GetStringFromResourceLC(R_PROGRESS_INFO); |
|
553 iConsole->Printf(*string); |
|
554 CleanupStack::PopAndDestroy(string); |
|
555 |
|
556 TInt progessBarLen = ProgressBarLength(); |
|
557 |
|
558 for(TInt i = 0; i < iProgressBarLen; ++i) |
|
559 { |
|
560 if(i < progessBarLen) |
|
561 { |
|
562 iConsole->Printf(KCurProgressSymbol); |
|
563 } |
|
564 else |
|
565 { |
|
566 iConsole->Printf(KCompleteProgressSymbol); |
|
567 } |
|
568 } |
|
569 } |
|
570 |
|
571 void CIoHandler::SetPreferenceHandler(CPreferenceHandler* aPreferenceHandler) |
|
572 { |
|
573 iPreferenceHandler = aPreferenceHandler; |
|
574 } |
|
575 |
|
576 TBool CIoHandler::IsDisplayableInPage(TInt aLineCount) |
|
577 { |
|
578 if(iPreferenceHandler && ESwiNormalMode != iPreferenceHandler->GetOperationMode() ) |
|
579 { |
|
580 return ETrue; |
|
581 } |
|
582 TSize screenSize = iConsole->ScreenSize(); |
|
583 if(iConsole->WhereY() + aLineCount >= screenSize.iHeight - KCursorOffset) |
|
584 { |
|
585 return EFalse; |
|
586 } |
|
587 |
|
588 return ETrue; |
|
589 } |
|
590 |
|
591 |