|
1 /* |
|
2 * Copyright (c) 2007-2008 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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <coemain.h> |
|
20 #include <bautils.h> |
|
21 #include <StringLoader.h> |
|
22 |
|
23 #include "hnmdlocalization.h" |
|
24 #include "hnmdlocalizationelement.h" |
|
25 #include "hnglobals.h" |
|
26 #include "hnconvutils.h" |
|
27 #include "hnutils.h" |
|
28 |
|
29 // ======== MEMBER FUNCTIONS ======== |
|
30 |
|
31 /** Max param count */ |
|
32 static const TInt KMaxParamsCount( 10 ); |
|
33 const TInt KFileExtensionLength( 4 ); |
|
34 |
|
35 _LIT(KLocalizationOneDes, "%U"); |
|
36 _LIT(KLocalizationMoreDes, "%?U"); |
|
37 _LIT(KLocalizationOneInt, "%N"); |
|
38 _LIT(KLocalizationMoreInt, "%?N"); |
|
39 |
|
40 // --------------------------------------------------------------------------- |
|
41 // |
|
42 // --------------------------------------------------------------------------- |
|
43 // |
|
44 void CHnMdLocalization::ConstructL() |
|
45 { |
|
46 iCoeEnv = CCoeEnv::Static(); |
|
47 ASSERT( iCoeEnv ); |
|
48 } |
|
49 |
|
50 // --------------------------------------------------------------------------- |
|
51 // |
|
52 // --------------------------------------------------------------------------- |
|
53 // |
|
54 CHnMdLocalization* CHnMdLocalization::NewL() |
|
55 { |
|
56 CHnMdLocalization* self = CHnMdLocalization::NewLC(); |
|
57 CleanupStack::Pop( self ); |
|
58 return self; |
|
59 } |
|
60 |
|
61 // --------------------------------------------------------------------------- |
|
62 // |
|
63 // --------------------------------------------------------------------------- |
|
64 // |
|
65 CHnMdLocalization* CHnMdLocalization::NewLC() |
|
66 { |
|
67 CHnMdLocalization* self = new( ELeave ) CHnMdLocalization; |
|
68 CleanupStack::PushL( self ); |
|
69 self->ConstructL(); |
|
70 return self; |
|
71 } |
|
72 |
|
73 // --------------------------------------------------------------------------- |
|
74 // |
|
75 // --------------------------------------------------------------------------- |
|
76 // |
|
77 void CHnMdLocalization::AppendLocalizationsL( TXmlEngElement aElement ) |
|
78 { |
|
79 RXmlEngNodeList< TXmlEngElement > children; |
|
80 CleanupClosePushL( children ); |
|
81 aElement.GetChildElements( children ); |
|
82 |
|
83 RPointerArray<CHnMdLocalizationElement> tempArray; |
|
84 CleanupClosePushL( tempArray ); |
|
85 TInt count = children.Count(); |
|
86 for ( TInt i( 0 ); i < count; i++ ) |
|
87 { |
|
88 TXmlEngElement child = children.Next(); |
|
89 |
|
90 if ( !child.Name().Compare( KLocalizationElementName8 ) ) |
|
91 { |
|
92 RBuf8 namespac; |
|
93 CleanupClosePushL( namespac ); |
|
94 if ( child.HasAttributeL( KNameSpaceAttrName8 ) ) |
|
95 { |
|
96 HnUtils::SetAttributeL( child, KNameSpaceAttrName8, namespac ); |
|
97 if ( !IsDuplicateL( namespac ) ) |
|
98 { |
|
99 CHnMdLocalizationElement* element = |
|
100 CHnMdLocalizationElement::NewL( child ); |
|
101 |
|
102 AppendElementL( element ); // ownership transfered |
|
103 } |
|
104 } |
|
105 CleanupStack::PopAndDestroy( &namespac ); |
|
106 } |
|
107 } |
|
108 |
|
109 CleanupStack::PopAndDestroy( &tempArray ); |
|
110 CleanupStack::PopAndDestroy( &children ); |
|
111 } |
|
112 |
|
113 // --------------------------------------------------------------------------- |
|
114 // |
|
115 // --------------------------------------------------------------------------- |
|
116 // |
|
117 const CHnMdLocalizationElement* CHnMdLocalization::ElementByNamespace( |
|
118 const TDesC& aNamespace ) const |
|
119 { |
|
120 TInt count = iInternalLocalization.Count(); |
|
121 const CHnMdLocalizationElement* element = NULL; |
|
122 for ( TInt i = 0; i < count; i++ ) |
|
123 { |
|
124 const CHnMdLocalizationElement* tmp = iInternalLocalization[i]; |
|
125 if ( !tmp->Namespace().Compare( aNamespace) ) |
|
126 { |
|
127 element = tmp; |
|
128 break; |
|
129 } |
|
130 } |
|
131 return element; |
|
132 } |
|
133 |
|
134 // --------------------------------------------------------------------------- |
|
135 // |
|
136 // --------------------------------------------------------------------------- |
|
137 // |
|
138 CHnMdLocalization::CHnMdLocalization() |
|
139 { |
|
140 } |
|
141 |
|
142 // --------------------------------------------------------------------------- |
|
143 // |
|
144 // --------------------------------------------------------------------------- |
|
145 // |
|
146 CHnMdLocalization::~CHnMdLocalization() |
|
147 { |
|
148 iInternalLocalization.ResetAndDestroy(); |
|
149 for( TInt i( 0 ); i < iInternalOffset.Count(); i++ ) |
|
150 { |
|
151 iCoeEnv->DeleteResourceFile( iInternalOffset[i] ); |
|
152 } |
|
153 iInternalOffset.Close(); |
|
154 } |
|
155 |
|
156 // --------------------------------------------------------------------------- |
|
157 // |
|
158 // --------------------------------------------------------------------------- |
|
159 // |
|
160 void CHnMdLocalization::AppendElementL( CHnMdLocalizationElement* aElement ) |
|
161 { |
|
162 if( IsInternalL( aElement->Source() ) ) |
|
163 { |
|
164 iInternalLocalization.AppendL( aElement ); |
|
165 if( IsResourceFile (aElement->Source()) ) |
|
166 { |
|
167 TInt offset( 0 ); |
|
168 TRAPD( err, offset = iCoeEnv->AddResourceFileL( *(aElement->SourcePath()) ) ); |
|
169 if ( !err ) |
|
170 { |
|
171 iInternalOffset.AppendL( offset ); |
|
172 } |
|
173 } |
|
174 } |
|
175 } |
|
176 |
|
177 // --------------------------------------------------------------------------- |
|
178 // |
|
179 // --------------------------------------------------------------------------- |
|
180 // |
|
181 TBool CHnMdLocalization::IsDuplicateL( TDesC8& aNamespace ) |
|
182 { |
|
183 HBufC* namespac = HnConvUtils::Str8ToStrLC( aNamespace ); |
|
184 TBool isDuplicate( IsDuplicateL( *namespac ) ); |
|
185 CleanupStack::PopAndDestroy( namespac ); |
|
186 return isDuplicate; |
|
187 } |
|
188 |
|
189 // --------------------------------------------------------------------------- |
|
190 // |
|
191 // --------------------------------------------------------------------------- |
|
192 // |
|
193 TBool CHnMdLocalization::IsDuplicateL( TDesC& aNamespace ) |
|
194 { |
|
195 TBool isDuplicate( EFalse ); |
|
196 |
|
197 if ( ElementByNamespace( aNamespace ) != NULL ) |
|
198 { |
|
199 isDuplicate = ETrue; |
|
200 } |
|
201 |
|
202 return isDuplicate; |
|
203 } |
|
204 |
|
205 // --------------------------------------------------------------------------- |
|
206 // |
|
207 // --------------------------------------------------------------------------- |
|
208 // |
|
209 TBool CHnMdLocalization::IsInternalL( const TDesC& aName ) |
|
210 { |
|
211 TBool isInternal( EFalse ); |
|
212 _LIT( KDot, "." ); |
|
213 |
|
214 TInt offset( aName.Find( KDot ) ); |
|
215 if( offset != KErrNotFound ) |
|
216 { |
|
217 if( aName.Left( offset ).Compare( KFolderSuite ) == KErrNone ) |
|
218 { |
|
219 isInternal = ETrue; |
|
220 } |
|
221 } |
|
222 |
|
223 return isInternal; |
|
224 } |
|
225 |
|
226 // --------------------------------------------------------------------------- |
|
227 // |
|
228 // --------------------------------------------------------------------------- |
|
229 // |
|
230 TBool CHnMdLocalization::IsResourceFile( const TDesC& aFileName ) |
|
231 { |
|
232 TInt pos = aFileName.Find( KExtension ); |
|
233 if( aFileName.Right( KFileExtensionLength ).Compare( KExtensionRsc ) ) |
|
234 { |
|
235 TBuf<2> tail; |
|
236 tail.Copy(aFileName.Right(2)); |
|
237 for (TInt i = 0; i < tail.Length(); i++ ) |
|
238 { |
|
239 TChar c = tail[i]; |
|
240 if ( !c.IsDigit() ) |
|
241 { |
|
242 pos = KErrNotFound; |
|
243 break; |
|
244 } |
|
245 } |
|
246 } |
|
247 return pos == ( aFileName.Length() - KFileExtensionLength ); |
|
248 } |
|
249 |
|
250 // --------------------------------------------------------------------------- |
|
251 // |
|
252 // --------------------------------------------------------------------------- |
|
253 // |
|
254 HBufC* CHnMdLocalization::LoadL( const TDesC& aResourceName, |
|
255 const CDesC16Array* aDesParams, const CArrayFix<TInt>* aIntParams ) |
|
256 { |
|
257 TInt position = aResourceName.Locate(':'); |
|
258 HBufC* res = NULL; |
|
259 if( position!= KErrNotFound ) |
|
260 { |
|
261 TPtrC resourceName = aResourceName.Mid(position + 1); |
|
262 TPtrC locName = aResourceName.Left(position); |
|
263 |
|
264 const CHnMdLocalizationElement* localization = |
|
265 ElementByNamespace( locName ); |
|
266 // if there is not namaspace defined, check may it is file in format |
|
267 // myapp.rsc:id |
|
268 if (!localization && !IsDuplicateL( locName ) && IsResourceFile( locName )) |
|
269 { |
|
270 // let's try to add namespace |
|
271 CHnMdLocalizationElement* element = |
|
272 CHnMdLocalizationElement::NewL( locName, locName); |
|
273 AppendElementL(element); // ownership transfered |
|
274 localization = ElementByNamespace( locName ); |
|
275 } |
|
276 |
|
277 if( localization && IsResourceFile( localization->Source() ) && localization->SourceExists() ) |
|
278 { |
|
279 HBufC8* resourceName8 = HBufC8::NewLC(resourceName.Length()); |
|
280 resourceName8->Des().Copy(resourceName); |
|
281 TInt resourceId (KErrNotFound); |
|
282 TRAPD(err, resourceId = |
|
283 const_cast<CHnMdLocalizationElement*>(localization)->FindResourceIdL(resourceName8) ); |
|
284 if ( err != KErrNone ) // maybe it was only number |
|
285 { |
|
286 err = HnConvUtils::Str8ToInt(*resourceName8, resourceId); |
|
287 } |
|
288 User::LeaveIfError(err); |
|
289 if ( iCoeEnv->IsResourceAvailableL( resourceId ) ) |
|
290 { |
|
291 res = iCoeEnv->AllocReadResourceL( resourceId ); |
|
292 } |
|
293 |
|
294 CleanupStack::PopAndDestroy( resourceName8 ); |
|
295 } |
|
296 } |
|
297 |
|
298 if (!res) // it mean that we dont have localization item for it |
|
299 { |
|
300 res = aResourceName.AllocL(); |
|
301 } |
|
302 if ((aDesParams && aDesParams->Count()) || |
|
303 (aIntParams && aIntParams->Count())) |
|
304 { |
|
305 CleanupStack::PushL(res); |
|
306 HBufC* formatted = FormatTextL(*res, aDesParams, aIntParams); |
|
307 CleanupStack::PopAndDestroy(res); |
|
308 res = formatted; |
|
309 } |
|
310 |
|
311 return res; |
|
312 } |
|
313 |
|
314 // --------------------------------------------------------------------------- |
|
315 // |
|
316 // --------------------------------------------------------------------------- |
|
317 // |
|
318 HBufC* CHnMdLocalization::FormatTextL( const TDesC& aTextToFormat, |
|
319 const CDesC16Array* aDesParams, const CArrayFix<TInt>* aIntParams ) |
|
320 { |
|
321 HBufC* res = aTextToFormat.AllocL(); |
|
322 TInt noParams(0); |
|
323 |
|
324 if (aDesParams) |
|
325 noParams += aDesParams->Count(); |
|
326 if (aIntParams) |
|
327 noParams += aIntParams->Count(); |
|
328 |
|
329 if (noParams > KMaxParamsCount) |
|
330 User::Leave( KErrNotSupported ); |
|
331 |
|
332 // let's check if we have descriptors to format |
|
333 if( aDesParams && aDesParams->Count() ) |
|
334 { |
|
335 // if we have only one we match to %U |
|
336 if (aDesParams->Count() == 1) |
|
337 { |
|
338 if ( res->FindC(KLocalizationOneDes) != KErrNotFound ) |
|
339 { |
|
340 TPtrC16 param = aDesParams->MdcaPoint(0); |
|
341 HBufC* dest = HBufC::NewL( res->Length() + |
|
342 param.Length() * KMaxParamsCount ); |
|
343 TPtr destDes(dest->Des()); |
|
344 StringLoader::Format( destDes, *res, -1, param ); |
|
345 delete res; |
|
346 res = dest; |
|
347 } |
|
348 } |
|
349 // and we match for format %(index)U |
|
350 TBufC<3> descformat(KLocalizationMoreDes); |
|
351 TInt desIndexUsed(0); |
|
352 for (TInt i = 0; i < noParams && desIndexUsed < aDesParams->Count(); i++) |
|
353 { |
|
354 descformat.Des()[1] = '0'+ i; // replace index infromation |
|
355 if ( res->FindC( descformat ) != KErrNotFound ) |
|
356 { |
|
357 |
|
358 TPtrC16 param = aDesParams->MdcaPoint(desIndexUsed++); |
|
359 HBufC* dest = HBufC::NewL( res->Length() + |
|
360 param.Length() * KMaxParamsCount ); |
|
361 TPtr destDes(dest->Des()); |
|
362 StringLoader::Format( destDes, *res, i, param ); |
|
363 delete res; |
|
364 res = dest; |
|
365 } |
|
366 } |
|
367 } |
|
368 // let's check if we have numbers to format |
|
369 if( aIntParams && aIntParams->Count() ) |
|
370 { |
|
371 // if we have only one we match to %N |
|
372 if (aIntParams->Count() == 1) |
|
373 { |
|
374 if ( res->FindC(KLocalizationOneInt) != KErrNotFound ) |
|
375 { |
|
376 HBufC* dest = HBufC::NewL( res->Length() + |
|
377 sizeof(TInt) * KMaxParamsCount ); |
|
378 TPtr destDes(dest->Des()); |
|
379 StringLoader::Format( destDes, *res, -1, aIntParams->At(0) ); |
|
380 delete res; |
|
381 res = dest; |
|
382 } |
|
383 } |
|
384 // and we match for format %(index)N |
|
385 TBufC<3> format(KLocalizationMoreInt); |
|
386 TInt numberUsed(0); |
|
387 for (TInt i = 0; i < noParams && numberUsed < aIntParams->Count(); i++) |
|
388 { |
|
389 format.Des()[1] = '0'+ i; |
|
390 if ( res->FindC(format) != KErrNotFound ) |
|
391 { |
|
392 HBufC* dest = HBufC::NewL( res->Length() + |
|
393 sizeof(TInt)* KMaxParamsCount ); |
|
394 TPtr destDes(dest->Des()); |
|
395 StringLoader::Format( destDes, *res, i, aIntParams->At(numberUsed++) ); |
|
396 delete res; |
|
397 res = dest; |
|
398 } |
|
399 } |
|
400 } |
|
401 return res; |
|
402 } |
|
403 |
|
404 |