|
1 /* |
|
2 * Copyright (c) 2002-2004 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: Utility. Creates progress info string from given data. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include "ProgressInfoCreator.h" |
|
22 #include "DMgrUiLibPanic.h" |
|
23 #include "UiLibLogger.h" |
|
24 #include <DownloadMgrUiLib.rsg> |
|
25 #include <StringLoader.h> |
|
26 #include <AknUtils.h> |
|
27 |
|
28 // LOCAL CONSTANTS AND MACROS |
|
29 _LIT( KCharSpace, " " ); |
|
30 _LIT( KBracketLeft, "(" ); |
|
31 _LIT( KBracketRight, ")" ); |
|
32 LOCAL_C const TInt32 KKiloExchangeRate = 1000; // not 1024, because of usability... |
|
33 LOCAL_C const TInt32 KBytesInOneMB = KKiloExchangeRate*KKiloExchangeRate; |
|
34 |
|
35 // ============================ MEMBER FUNCTIONS =============================== |
|
36 |
|
37 // ----------------------------------------------------------------------------- |
|
38 // CProgressInfoCreator::CProgressInfoCreator |
|
39 // ----------------------------------------------------------------------------- |
|
40 // |
|
41 CProgressInfoCreator::CProgressInfoCreator() |
|
42 : iCoeEnv( *CCoeEnv::Static() ) |
|
43 { |
|
44 } |
|
45 |
|
46 // ----------------------------------------------------------------------------- |
|
47 // CProgressInfoCreator::ConstructL |
|
48 // ----------------------------------------------------------------------------- |
|
49 // |
|
50 void CProgressInfoCreator::ConstructL() |
|
51 { |
|
52 // Load often used resource strings for progress indication. |
|
53 iUnitKbFormatString = iCoeEnv.AllocReadResourceL( R_DMUL_UNIT_KB ); |
|
54 iUnitMbFormatString = iCoeEnv.AllocReadResourceL( R_DMUL_UNIT_MB ); |
|
55 HBufC* percentFormatBaseString = |
|
56 iCoeEnv.AllocReadResourceLC( R_DMUL_UNIT_PERCENT ); |
|
57 // Insert brackets |
|
58 iUnitPcFormatString = HBufC::NewMaxL( percentFormatBaseString->Length() + |
|
59 KBracketLeft().Length() + |
|
60 KBracketRight().Length() ); |
|
61 iUnitPcFormatString->Des().Copy( KBracketLeft ); |
|
62 iUnitPcFormatString->Des().Append( *percentFormatBaseString ); |
|
63 iUnitPcFormatString->Des().Append( KBracketRight ); |
|
64 CleanupStack::PopAndDestroy( percentFormatBaseString ); // percentFormatBaseString |
|
65 } |
|
66 |
|
67 // ----------------------------------------------------------------------------- |
|
68 // CProgressInfoCreator::NewL |
|
69 // ----------------------------------------------------------------------------- |
|
70 // |
|
71 CProgressInfoCreator* CProgressInfoCreator::NewL() |
|
72 { |
|
73 CProgressInfoCreator* self = new ( ELeave ) CProgressInfoCreator(); |
|
74 CleanupStack::PushL( self ); |
|
75 self->ConstructL(); |
|
76 CleanupStack::Pop(); |
|
77 return self; |
|
78 } |
|
79 |
|
80 // Destructor |
|
81 CProgressInfoCreator::~CProgressInfoCreator() |
|
82 { |
|
83 delete iUnitPcFormatString; |
|
84 delete iUnitMbFormatString; |
|
85 delete iUnitKbFormatString; |
|
86 } |
|
87 |
|
88 // ----------------------------------------------------------------------------- |
|
89 // CProgressInfoCreator::ProgressInfo |
|
90 // ----------------------------------------------------------------------------- |
|
91 // |
|
92 void CProgressInfoCreator::ProgressInfo( TInt32 aSize, TDes& aResult ) |
|
93 { |
|
94 __ASSERT_DEBUG( aResult.MaxSize()>=7, Panic( EUiLibPanSmallDivBuffer ) ); |
|
95 |
|
96 // Indicate only the size. |
|
97 aResult.Zero(); |
|
98 |
|
99 // aSize is in bytes. |
|
100 // Format string is: "%N kB" or "%U MB". %N and %U is 1-4 characters long. |
|
101 // So aResult must be at least 7 (=4+3) characters long. |
|
102 |
|
103 if ( aSize < KBytesInOneMB ) |
|
104 { |
|
105 // The unit must be "kB". Apply integer division. No decimals are shown. |
|
106 // With the exchange rate 1000 between kB and MB, the following |
|
107 // division's result must be < 1000, so less than 3 characters: |
|
108 TInt sizeInKiloBytes = aSize / KKiloExchangeRate; |
|
109 if ( 0 < aSize && sizeInKiloBytes == 0 ) |
|
110 { |
|
111 // Don't let progress show 0kB when there is already something |
|
112 // downloaded (0 < aSize): |
|
113 sizeInKiloBytes = 1; |
|
114 } |
|
115 StringLoader::Format |
|
116 ( aResult, *iUnitKbFormatString, KErrNotFound, sizeInKiloBytes ); |
|
117 } |
|
118 else |
|
119 { |
|
120 // Size is 1 MB or more. |
|
121 // It is always shown in 3 numbers. |
|
122 Division( aSize, KBytesInOneMB, iFormattedMegaBytes ); |
|
123 AknTextUtils::DisplayTextLanguageSpecificNumberConversion( iFormattedMegaBytes ); |
|
124 StringLoader::Format |
|
125 ( aResult, *iUnitMbFormatString, KErrNotFound, iFormattedMegaBytes ); |
|
126 } |
|
127 } |
|
128 |
|
129 // ----------------------------------------------------------------------------- |
|
130 // CProgressInfoCreator::ProgressInfo |
|
131 // ----------------------------------------------------------------------------- |
|
132 // |
|
133 void CProgressInfoCreator::ProgressInfo |
|
134 ( TInt32 aPartialSize, TInt32 aFullSize, TDes& aResult ) |
|
135 { |
|
136 __ASSERT_DEBUG( aFullSize!=0, Panic( EUiLibPanDivisionWithZero ) ); |
|
137 |
|
138 // Indicate pecentage & downloaded size. |
|
139 aResult.Zero(); |
|
140 |
|
141 // Percentage value is shown in 1-2 numbers, so must be less than 100. |
|
142 // Apply integer division. |
|
143 // Note that we have to use 64-bit integers to avoid overflow when |
|
144 // aPartialSize is multiplied with 100, because 100*KMaxTInt (the max |
|
145 // value of aPartialSize) would not fit into TUint32, thus it would cause overflow. |
|
146 iTUint64Helper1 = aPartialSize; |
|
147 iTUint64Helper2 = aFullSize; |
|
148 TInt percent = (TInt) ((TUint64(100)*iTUint64Helper1) / iTUint64Helper2); |
|
149 if ( percent >= 100 ) |
|
150 { |
|
151 percent = 99; |
|
152 } |
|
153 // OK. Insert the percentage. |
|
154 StringLoader::Format( aResult, *iUnitPcFormatString, KErrNotFound, percent ); |
|
155 |
|
156 // Append space separator. |
|
157 aResult.Append( KCharSpace ); |
|
158 |
|
159 // Construct and append partal size with unit. |
|
160 ProgressInfo( aPartialSize, iPartialSizeString ); |
|
161 aResult.Append( iPartialSizeString ); |
|
162 } |
|
163 |
|
164 // ----------------------------------------------------------------------------- |
|
165 // CProgressInfoCreator::Division |
|
166 // ----------------------------------------------------------------------------- |
|
167 // |
|
168 void CProgressInfoCreator::Division |
|
169 ( TInt aNumerator,TInt aDenominator, TDes& aResult ) |
|
170 { |
|
171 __ASSERT_DEBUG( aDenominator!=0, Panic( EUiLibPanDivisionWithZero ) ); |
|
172 __ASSERT_DEBUG( aResult.MaxSize()>=4, Panic( EUiLibPanSmallDivBuffer ) ); |
|
173 |
|
174 // Reset the result buffer. |
|
175 aResult.Zero(); |
|
176 iDivResult.Zero(); |
|
177 TLocale locale; |
|
178 |
|
179 TInt quotient = aNumerator / aDenominator; |
|
180 TInt remainder = aNumerator % aDenominator; |
|
181 |
|
182 // Only 3 numbers needed, so if 999 < quotient, then show only 999. |
|
183 if ( 999 < quotient ) |
|
184 { |
|
185 quotient = 999; |
|
186 } |
|
187 |
|
188 // Copy the integer part of the division. |
|
189 iDivResult.AppendNum( quotient ); |
|
190 //Copy the decimal separator. |
|
191 iDivResult.Append( locale.DecimalSeparator() ); |
|
192 |
|
193 // Copy the fraction part, we need 2 decimal fractions. |
|
194 // Example: 2.00 or 3.50... |
|
195 for ( TInt i=0;i<2;i++ ) |
|
196 { |
|
197 TInt newRemainder = ( remainder * 10 ) % aDenominator; |
|
198 remainder = ( remainder * 10 ) / aDenominator; |
|
199 iDivResult.AppendNum( remainder ); |
|
200 remainder = newRemainder; |
|
201 } |
|
202 |
|
203 // Only 3 numbers needed in the string. So, if the integer part is bigger |
|
204 // than 99, i.e. the decimel separator is at the 4. place, |
|
205 // we chop the remainder part... |
|
206 // Example "100.34" will be "100", "158.65" will be "158", |
|
207 // because of the specification. |
|
208 |
|
209 //If the integer value is bigger than 99, then chop... |
|
210 if ( ( TChar )iDivResult[3] == locale.DecimalSeparator() ) |
|
211 { |
|
212 aResult.Copy( iDivResult.Left( 3 ) ); |
|
213 } |
|
214 // Or simply copy to the result... |
|
215 else |
|
216 { |
|
217 aResult.Copy( iDivResult.Left( 4 ) ); |
|
218 } |
|
219 } |
|
220 |
|
221 /* End of file. */ |
|
222 |