|
1 /* |
|
2 * Copyright (c) 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 "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: Key - value container. |
|
15 * |
|
16 */ |
|
17 |
|
18 // INCLUDE FILES |
|
19 #include <utf.h> // for unicode character conversions |
|
20 #include <centralrepository.h> |
|
21 #include "e32std.h" |
|
22 #include "CIMPSSAPKeyValuePair.h" |
|
23 #include "IMPSSAPSettingsStoreDefinitions.h" |
|
24 #include "IMPSSAPSettingsStorePanics.h" |
|
25 |
|
26 //MACROS |
|
27 |
|
28 //Helper macro to check value type in data set |
|
29 #define REQUIRE_SET_VALUE_TYPE( aType ) \ |
|
30 { \ |
|
31 TBool _settable = ( iValueType == EValueTypeNone ) || \ |
|
32 ( iValueType == aType ); \ |
|
33 if( !_settable )\ |
|
34 {\ |
|
35 return KErrGeneral;\ |
|
36 }\ |
|
37 } \ |
|
38 |
|
39 //Helper macro to check value type in data get |
|
40 #define REQUIRE_GET_VALUE_TYPE( aType ) \ |
|
41 { \ |
|
42 if( iValueType == EValueTypeNone ) \ |
|
43 { \ |
|
44 return KErrNotFound; \ |
|
45 } \ |
|
46 TBool _readable = ( iValueType == aType ); \ |
|
47 if( !_readable ) \ |
|
48 { \ |
|
49 return KErrGeneral; \ |
|
50 } \ |
|
51 } \ |
|
52 |
|
53 |
|
54 // ================= MEMBER FUNCTIONS ======================= |
|
55 |
|
56 // ----------------------------------------------------------------------------- |
|
57 // CIMPSSAPKeyValuePair::New() |
|
58 // ----------------------------------------------------------------------------- |
|
59 // |
|
60 CIMPSSAPKeyValuePair* CIMPSSAPKeyValuePair::New( const TDesC16& aKey ) |
|
61 { |
|
62 //Non leaving two phased constructor!!! |
|
63 |
|
64 CIMPSSAPKeyValuePair* self = new CIMPSSAPKeyValuePair; // CSI: 62 # |
|
65 if ( self ) |
|
66 { |
|
67 self->iKey = aKey.Alloc(); |
|
68 if ( !self->iKey ) |
|
69 { |
|
70 delete self; |
|
71 self = NULL; |
|
72 } |
|
73 } |
|
74 |
|
75 return self; |
|
76 } |
|
77 |
|
78 |
|
79 // ----------------------------------------------------------------------------- |
|
80 // CIMPSSAPKeyValuePair::NewLC() |
|
81 // ----------------------------------------------------------------------------- |
|
82 // |
|
83 CIMPSSAPKeyValuePair* CIMPSSAPKeyValuePair::NewLC( RReadStream& aStream ) |
|
84 { |
|
85 CIMPSSAPKeyValuePair* self = new ( ELeave ) CIMPSSAPKeyValuePair; |
|
86 CleanupStack::PushL( self ); |
|
87 self->ConstructL( aStream ); |
|
88 return self; |
|
89 } |
|
90 |
|
91 // ----------------------------------------------------------------------------- |
|
92 // CIMPSSAPKeyValuePair::NewLC() |
|
93 // ----------------------------------------------------------------------------- |
|
94 // |
|
95 CIMPSSAPKeyValuePair* CIMPSSAPKeyValuePair::NewLC( const TDesC& aKeyValuePairFlat ) |
|
96 { |
|
97 CIMPSSAPKeyValuePair* self = new ( ELeave ) CIMPSSAPKeyValuePair; |
|
98 CleanupStack::PushL( self ); |
|
99 self->ParseFlatKeyValuePairL( aKeyValuePairFlat ); |
|
100 return self; |
|
101 } |
|
102 |
|
103 // ----------------------------------------------------------------------------- |
|
104 // CIMPSSAPKeyValuePair::~CIMPSSAPKeyValuePair() |
|
105 // Destructor |
|
106 // ----------------------------------------------------------------------------- |
|
107 // |
|
108 CIMPSSAPKeyValuePair::~CIMPSSAPKeyValuePair() |
|
109 { |
|
110 Reset(); |
|
111 } |
|
112 |
|
113 |
|
114 // ----------------------------------------------------------------------------- |
|
115 // CIMPSSAPKeyValuePair::CIMPSSAPKeyValuePair() |
|
116 // C++ default constructor can NOT contain any code, that |
|
117 // might leave. |
|
118 // ----------------------------------------------------------------------------- |
|
119 // |
|
120 CIMPSSAPKeyValuePair::CIMPSSAPKeyValuePair() |
|
121 : iValueType( EValueTypeNone ) |
|
122 { |
|
123 } |
|
124 |
|
125 |
|
126 |
|
127 // ----------------------------------------------------------------------------- |
|
128 // CIMPSSAPKeyValuePair::ConstructL() |
|
129 // ----------------------------------------------------------------------------- |
|
130 // |
|
131 void CIMPSSAPKeyValuePair::ConstructL( RReadStream& aStream ) |
|
132 { |
|
133 //Note! This implementation must be in sync with |
|
134 //ExternalizeL() |
|
135 |
|
136 //Extension space |
|
137 aStream.ReadInt32L(); |
|
138 |
|
139 //Default data |
|
140 TInt keyLength = aStream.ReadInt32L(); |
|
141 iKey = HBufC::NewL( keyLength ); |
|
142 TPtr keyPtr( iKey->Des() ); |
|
143 aStream.ReadL( keyPtr, keyLength ); |
|
144 |
|
145 iValueType = static_cast< TValueType > ( aStream.ReadInt8L() ); |
|
146 switch ( iValueType ) |
|
147 { |
|
148 case EValueTypeInt: |
|
149 { |
|
150 iInt = aStream.ReadInt32L(); |
|
151 break; |
|
152 } |
|
153 |
|
154 case EValueTypeDesC8: |
|
155 { |
|
156 TInt descLength = aStream.ReadInt32L(); |
|
157 iBuf8 = HBufC8::NewL( descLength ); |
|
158 TPtr8 descPtr8( iBuf8->Des() ); |
|
159 aStream.ReadL( descPtr8, descLength ); |
|
160 break; |
|
161 } |
|
162 |
|
163 |
|
164 case EValueTypeDesC16: |
|
165 { |
|
166 TInt descLength = aStream.ReadInt32L(); |
|
167 iBuf16 = HBufC16::NewL( descLength ); |
|
168 TPtr16 descPtr16( iBuf16->Des() ); |
|
169 aStream.ReadL( descPtr16, descLength ); |
|
170 break; |
|
171 } |
|
172 |
|
173 //FLOW THROUGH |
|
174 case EValueTypeNone: |
|
175 default: |
|
176 { |
|
177 break; |
|
178 } |
|
179 }; |
|
180 } |
|
181 |
|
182 |
|
183 |
|
184 // ----------------------------------------------------------------------------- |
|
185 // CIMPSSAPKeyValuePair::Reset() |
|
186 // ----------------------------------------------------------------------------- |
|
187 // |
|
188 void CIMPSSAPKeyValuePair::Reset() |
|
189 { |
|
190 switch ( iValueType ) |
|
191 { |
|
192 case EValueTypeDesC8: |
|
193 { |
|
194 delete iBuf8; |
|
195 iBuf8 = NULL; |
|
196 break; |
|
197 } |
|
198 |
|
199 |
|
200 case EValueTypeDesC16: |
|
201 { |
|
202 delete iBuf16; |
|
203 iBuf16 = NULL; |
|
204 break; |
|
205 } |
|
206 |
|
207 //FLOW THROUGH |
|
208 case EValueTypeInt: |
|
209 case EValueTypeNone: |
|
210 default: |
|
211 { |
|
212 break; |
|
213 } |
|
214 }; |
|
215 |
|
216 iValueType = EValueTypeNone; |
|
217 |
|
218 delete iKey; |
|
219 iKey = NULL; |
|
220 } |
|
221 |
|
222 |
|
223 // ----------------------------------------------------------------------------- |
|
224 // CIMPSSAPKeyValuePair::SetValue() |
|
225 // ----------------------------------------------------------------------------- |
|
226 // |
|
227 TInt CIMPSSAPKeyValuePair::SetValue( TInt aValue ) |
|
228 { |
|
229 REQUIRE_SET_VALUE_TYPE( EValueTypeInt ) |
|
230 |
|
231 TInt oldValue( iInt ); |
|
232 iInt = aValue; |
|
233 iValueType = EValueTypeInt; |
|
234 |
|
235 TBool dataTooBig( IsDataTooBig() ); |
|
236 TInt err( KErrNone ); |
|
237 if ( dataTooBig ) |
|
238 { |
|
239 iInt = oldValue; |
|
240 err = KErrTooBig; |
|
241 } |
|
242 __ASSERT_DEBUG( !err, Panic( EIMPSInvalidKeyValue ) ); |
|
243 return err; |
|
244 } |
|
245 |
|
246 |
|
247 // ----------------------------------------------------------------------------- |
|
248 // CIMPSSAPKeyValuePair::Key() |
|
249 // ----------------------------------------------------------------------------- |
|
250 // |
|
251 const TDesC& CIMPSSAPKeyValuePair::Key() const |
|
252 { |
|
253 return *iKey; |
|
254 } |
|
255 |
|
256 |
|
257 // ----------------------------------------------------------------------------- |
|
258 // CIMPSSAPKeyValuePair::SetValue() |
|
259 // ----------------------------------------------------------------------------- |
|
260 // |
|
261 TInt CIMPSSAPKeyValuePair::SetValue( const TDesC8& aValue ) |
|
262 { |
|
263 REQUIRE_SET_VALUE_TYPE( EValueTypeDesC8 ) |
|
264 |
|
265 // Allocate memory for the new value |
|
266 HBufC8* tmp = aValue.Alloc(); |
|
267 if ( !tmp ) |
|
268 { |
|
269 return KErrNoMemory; |
|
270 } |
|
271 |
|
272 // Swap pointers |
|
273 HBufC8* tmp2( NULL ); |
|
274 tmp2 = iBuf8; |
|
275 iBuf8 = tmp; |
|
276 iValueType = EValueTypeDesC8; |
|
277 |
|
278 TBool dataTooBig( IsDataTooBig() ); |
|
279 TInt err( KErrNone ); |
|
280 |
|
281 // Rollback if data too bog |
|
282 if ( dataTooBig ) |
|
283 { |
|
284 iBuf8 = tmp2; |
|
285 delete tmp; |
|
286 err = KErrTooBig; |
|
287 } |
|
288 // Otherwise delete the old value |
|
289 else |
|
290 { |
|
291 delete tmp2; |
|
292 } |
|
293 |
|
294 |
|
295 __ASSERT_DEBUG( !err, Panic( EIMPSInvalidKeyValue ) ); |
|
296 return err; |
|
297 } |
|
298 |
|
299 |
|
300 // ----------------------------------------------------------------------------- |
|
301 // CIMPSSAPKeyValuePair::SetValue() |
|
302 // ----------------------------------------------------------------------------- |
|
303 // |
|
304 TInt CIMPSSAPKeyValuePair::SetValue( const TDesC16& aValue ) |
|
305 { |
|
306 REQUIRE_SET_VALUE_TYPE( EValueTypeDesC16 ) |
|
307 |
|
308 // Allocate memory for the new value |
|
309 HBufC16* tmp = aValue.Alloc(); |
|
310 if ( !tmp ) |
|
311 { |
|
312 return KErrNoMemory; |
|
313 } |
|
314 |
|
315 // Swap pointers |
|
316 HBufC16* tmp2( NULL ); |
|
317 tmp2 = iBuf16; |
|
318 iBuf16 = tmp; |
|
319 iValueType = EValueTypeDesC16; |
|
320 |
|
321 TBool dataTooBig( IsDataTooBig() ); |
|
322 TInt err( KErrNone ); |
|
323 |
|
324 // Rollback if data too bog |
|
325 if ( dataTooBig ) |
|
326 { |
|
327 iBuf16 = tmp2; |
|
328 delete tmp; |
|
329 err = KErrTooBig; |
|
330 } |
|
331 // Otherwise delete the old value |
|
332 else |
|
333 { |
|
334 delete tmp2; |
|
335 } |
|
336 |
|
337 __ASSERT_DEBUG( !err, Panic( EIMPSInvalidKeyValue ) ); |
|
338 return err; |
|
339 } |
|
340 |
|
341 |
|
342 // ----------------------------------------------------------------------------- |
|
343 // CIMPSSAPKeyValuePair::GetValue() |
|
344 // ----------------------------------------------------------------------------- |
|
345 // |
|
346 TInt CIMPSSAPKeyValuePair::GetValue( TInt& aValue ) const |
|
347 { |
|
348 REQUIRE_GET_VALUE_TYPE( EValueTypeInt ) |
|
349 |
|
350 aValue = iInt; |
|
351 return KErrNone; |
|
352 } |
|
353 |
|
354 |
|
355 // ----------------------------------------------------------------------------- |
|
356 // CIMPSSAPKeyValuePair::GetValue() |
|
357 // ----------------------------------------------------------------------------- |
|
358 // |
|
359 TInt CIMPSSAPKeyValuePair::GetValue( TPtrC8& aValue ) const |
|
360 { |
|
361 REQUIRE_GET_VALUE_TYPE( EValueTypeDesC8 ) |
|
362 |
|
363 aValue.Set( *iBuf8 ); |
|
364 return KErrNone; |
|
365 } |
|
366 |
|
367 |
|
368 // ----------------------------------------------------------------------------- |
|
369 // CIMPSSAPKeyValuePair::GetValue() |
|
370 // ----------------------------------------------------------------------------- |
|
371 // |
|
372 TInt CIMPSSAPKeyValuePair::GetValue( TPtrC16& aValue ) const |
|
373 { |
|
374 REQUIRE_GET_VALUE_TYPE( EValueTypeDesC16 ) |
|
375 |
|
376 aValue.Set( *iBuf16 ); |
|
377 return KErrNone; |
|
378 } |
|
379 |
|
380 |
|
381 // ----------------------------------------------------------------------------- |
|
382 // CIMPSSAPKeyValuePair::ExternalizeL() |
|
383 // ----------------------------------------------------------------------------- |
|
384 // |
|
385 void CIMPSSAPKeyValuePair::ExternalizeL( RWriteStream& aStream ) const |
|
386 { |
|
387 //Extension space |
|
388 aStream.WriteInt32L( 0 ); |
|
389 |
|
390 //Default data |
|
391 aStream.WriteInt32L( iKey->Length() ); |
|
392 aStream.WriteL( *iKey ); |
|
393 aStream.WriteInt8L( iValueType ); |
|
394 |
|
395 switch ( iValueType ) |
|
396 { |
|
397 case EValueTypeInt: |
|
398 { |
|
399 aStream.WriteInt32L( iInt ); |
|
400 break; |
|
401 } |
|
402 |
|
403 case EValueTypeDesC8: |
|
404 { |
|
405 aStream.WriteInt32L( iBuf8->Length() ); |
|
406 aStream.WriteL( *iBuf8 ); |
|
407 break; |
|
408 } |
|
409 |
|
410 |
|
411 case EValueTypeDesC16: |
|
412 { |
|
413 aStream.WriteInt32L( iBuf16->Length() ); |
|
414 aStream.WriteL( *iBuf16 ); |
|
415 break; |
|
416 } |
|
417 |
|
418 //FLOW THROUGH |
|
419 case EValueTypeNone: |
|
420 default: |
|
421 { |
|
422 break; |
|
423 } |
|
424 }; |
|
425 } |
|
426 |
|
427 |
|
428 // ----------------------------------------------------------------------------- |
|
429 // CIMPSSAPKeyValuePair::DataSize() |
|
430 // ----------------------------------------------------------------------------- |
|
431 // |
|
432 TInt CIMPSSAPKeyValuePair::DataSize() const |
|
433 { |
|
434 // extension space == 4 bytes |
|
435 // keyLength == 4 bytes |
|
436 // valueType == 1 bytes |
|
437 // ==> 9 bytes |
|
438 TInt dataSize = 9; |
|
439 |
|
440 dataSize += iKey->Length(); |
|
441 |
|
442 switch ( iValueType ) |
|
443 { |
|
444 case EValueTypeInt: |
|
445 { |
|
446 //Integer == 4 bytes |
|
447 dataSize += 4; |
|
448 break; |
|
449 } |
|
450 |
|
451 case EValueTypeDesC8: |
|
452 { |
|
453 //desc8 length == 4 bytes |
|
454 dataSize += 4; |
|
455 dataSize += iBuf8->Size(); |
|
456 break; |
|
457 } |
|
458 |
|
459 case EValueTypeDesC16: |
|
460 { |
|
461 //desc16 length == 4 bytes |
|
462 dataSize += 4; |
|
463 dataSize += iBuf16->Size(); |
|
464 break; |
|
465 } |
|
466 |
|
467 //FLOW THROUGH |
|
468 case EValueTypeNone: |
|
469 default: |
|
470 { |
|
471 break; |
|
472 } |
|
473 }; |
|
474 |
|
475 return dataSize; |
|
476 } |
|
477 |
|
478 // ----------------------------------------------------------------------------- |
|
479 // CIMPSSAPKeyValuePair::DataSizeDec() |
|
480 // ----------------------------------------------------------------------------- |
|
481 // |
|
482 TInt CIMPSSAPKeyValuePair::DataSizeDec() const |
|
483 { |
|
484 |
|
485 TInt dataSize( iKey->Length() ); |
|
486 |
|
487 switch ( iValueType ) |
|
488 { |
|
489 case EValueTypeInt: |
|
490 { |
|
491 dataSize += KTIntMaxLengthTextual; |
|
492 break; |
|
493 } |
|
494 |
|
495 case EValueTypeDesC8: |
|
496 { |
|
497 dataSize += iBuf8->Length(); |
|
498 break; |
|
499 } |
|
500 |
|
501 case EValueTypeDesC16: |
|
502 { |
|
503 dataSize += iBuf16->Length(); |
|
504 break; |
|
505 } |
|
506 |
|
507 //FLOW THROUGH |
|
508 case EValueTypeNone: |
|
509 default: |
|
510 { |
|
511 break; |
|
512 } |
|
513 }; |
|
514 |
|
515 return dataSize; |
|
516 } |
|
517 |
|
518 // ----------------------------------------------------------------------------- |
|
519 // CIMPSSAPKeyValuePair::IsDataTooBig() |
|
520 // ----------------------------------------------------------------------------- |
|
521 // |
|
522 TBool CIMPSSAPKeyValuePair::IsDataTooBig() const |
|
523 { |
|
524 return ( DataSizeDec() > ( NCentralRepositoryConstants::KMaxUnicodeStringLength - |
|
525 2*KKeyValuePairFieldSeparator().Length() - 1 ) ); |
|
526 } |
|
527 |
|
528 // ----------------------------------------------------------------------------- |
|
529 // CIMPSSAPKeyValuePair::KeyValuePairFlatLC() |
|
530 // ----------------------------------------------------------------------------- |
|
531 // |
|
532 |
|
533 HBufC* CIMPSSAPKeyValuePair::KeyValuePairFlatLC() |
|
534 { |
|
535 |
|
536 // reserve memory for 2 field separators |
|
537 HBufC* flatBuf = HBufC::NewLC( NCentralRepositoryConstants::KMaxUnicodeStringLength ); |
|
538 |
|
539 TPtr flatBufPtr( flatBuf->Des() ); |
|
540 |
|
541 flatBufPtr.Append( *iKey ); |
|
542 flatBufPtr.Append( KKeyValuePairFieldSeparator ); |
|
543 flatBufPtr.AppendNum( iValueType, EDecimal ); |
|
544 flatBufPtr.Append( KKeyValuePairFieldSeparator ); |
|
545 |
|
546 switch ( iValueType ) |
|
547 { |
|
548 case EValueTypeInt: |
|
549 { |
|
550 flatBufPtr.AppendNum( iInt ); |
|
551 break; |
|
552 } |
|
553 |
|
554 case EValueTypeDesC8: |
|
555 { |
|
556 //convert to 16-bit |
|
557 TPtrC16 dataPtr16( ( TUint16* ) iBuf8->Ptr(), ( iBuf8->Size() / 2 ) ); |
|
558 flatBufPtr.Append( dataPtr16 ); |
|
559 break; |
|
560 } |
|
561 |
|
562 case EValueTypeDesC16: |
|
563 { |
|
564 flatBufPtr.Append( *iBuf16 ); |
|
565 break; |
|
566 } |
|
567 |
|
568 //FLOW THROUGH |
|
569 case EValueTypeNone: |
|
570 default: |
|
571 { |
|
572 break; |
|
573 } |
|
574 }; |
|
575 return flatBuf; |
|
576 } |
|
577 |
|
578 |
|
579 // ----------------------------------------------------------------------------- |
|
580 // CIMPSSAPKeyValuePair::ParseFlatKeyValuePairL() |
|
581 // ----------------------------------------------------------------------------- |
|
582 // |
|
583 |
|
584 void CIMPSSAPKeyValuePair::ParseFlatKeyValuePairL( const TDesC& aKeyValuePairFlat ) |
|
585 { |
|
586 TInt offset( aKeyValuePairFlat.Find( KKeyValuePairFieldSeparator ) ); |
|
587 TInt offset2( aKeyValuePairFlat.Mid( offset + |
|
588 KKeyValuePairFieldSeparator().Length() ).Find( |
|
589 KKeyValuePairFieldSeparator ) |
|
590 ); |
|
591 |
|
592 if ( ( offset == KErrNotFound ) || ( offset2 == KErrNotFound ) ) |
|
593 { |
|
594 //Incorrect format, leave |
|
595 User::Leave( KErrNotFound ); |
|
596 } |
|
597 |
|
598 TPtrC key( aKeyValuePairFlat.Left( offset ) ); |
|
599 |
|
600 iKey = key.AllocL(); |
|
601 |
|
602 TPtrC valueType( aKeyValuePairFlat.Mid( offset + |
|
603 KKeyValuePairFieldSeparator().Length(), offset2 ) ); |
|
604 TLex lexer( valueType ); |
|
605 TInt valueTypeInt( 0 ); |
|
606 User::LeaveIfError( lexer.Val( valueTypeInt ) ); |
|
607 TPtrC value( aKeyValuePairFlat.Mid( offset + offset2 + |
|
608 2*KKeyValuePairFieldSeparator().Length() ) ); // skip 2 separators |
|
609 switch ( valueTypeInt ) |
|
610 { |
|
611 case EValueTypeInt: |
|
612 { |
|
613 TLex lexer( value ); |
|
614 TInt intValue( 0 ); |
|
615 User::LeaveIfError( lexer.Val( intValue ) ); |
|
616 User::LeaveIfError( SetValue( intValue ) ); |
|
617 break; |
|
618 } |
|
619 case EValueTypeDesC8: |
|
620 { |
|
621 //convert to 8-bit |
|
622 TPtrC8 dataPtr8( ( TUint8* ) value.Ptr(), value.Size() ); |
|
623 User::LeaveIfError( SetValue( dataPtr8 ) ); |
|
624 break; |
|
625 } |
|
626 case EValueTypeDesC16: |
|
627 { |
|
628 User::LeaveIfError( SetValue( value ) ); |
|
629 break; |
|
630 } |
|
631 |
|
632 //FLOW THROUGH |
|
633 case EValueTypeNone: |
|
634 default: |
|
635 { |
|
636 break; |
|
637 } |
|
638 } |
|
639 } |
|
640 |
|
641 |
|
642 |
|
643 |
|
644 |
|
645 |
|
646 |
|
647 |
|
648 |
|
649 |
|
650 // End of File |
|
651 |