|
1 /* |
|
2 * Copyright (c) 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: cbselement.cpp |
|
15 * |
|
16 */ |
|
17 |
|
18 // INCLUDE FILES |
|
19 |
|
20 #include <utf.h> |
|
21 #include "e32base.h" |
|
22 #include "s32strm.h" |
|
23 #include "cbselement.h" |
|
24 #include "cbsbitmap.h" |
|
25 |
|
26 |
|
27 CBSElement* CBSElement::NewL( |
|
28 const TDesC8& aElementId, |
|
29 TInt aData ) |
|
30 { |
|
31 |
|
32 CBSElement* self = new ( ELeave ) CBSElement() ; |
|
33 CleanupStack::PushL( self ); |
|
34 self->ConstructL( EBSInt, |
|
35 aElementId, |
|
36 aData, |
|
37 ETrue, |
|
38 NULL, |
|
39 KNullDesC, |
|
40 KNullDesC8, |
|
41 NULL ); |
|
42 CleanupStack::Pop( self ); //self |
|
43 return self; |
|
44 } |
|
45 |
|
46 |
|
47 CBSElement* CBSElement::NewL( |
|
48 const TDesC8& aElementId, |
|
49 const TDesC& aData, |
|
50 TBSElementType aType /*= EText*/ ) |
|
51 { |
|
52 if( aType != EBSFile && aType != EBSText ) |
|
53 { |
|
54 User::Leave( KErrArgument ); |
|
55 } |
|
56 |
|
57 CBSElement* self = new ( ELeave ) CBSElement() ; |
|
58 CleanupStack::PushL( self ); |
|
59 self->ConstructL( aType, |
|
60 aElementId, |
|
61 0, |
|
62 EFalse, |
|
63 NULL, |
|
64 aData, |
|
65 KNullDesC8, |
|
66 NULL); |
|
67 CleanupStack::Pop( self ); //self |
|
68 return self; |
|
69 } |
|
70 |
|
71 |
|
72 CBSElement* CBSElement::NewL( |
|
73 const TDesC8& aElementId, |
|
74 const TDesC8& aData ) |
|
75 { |
|
76 CBSElement* self = new ( ELeave ) CBSElement() ; |
|
77 CleanupStack::PushL( self ); |
|
78 self->ConstructL( EBSBuffer, |
|
79 aElementId, |
|
80 0, |
|
81 EFalse, |
|
82 NULL, |
|
83 KNullDesC, |
|
84 aData, |
|
85 NULL); |
|
86 CleanupStack::Pop( self ); //self |
|
87 return self; |
|
88 } |
|
89 |
|
90 CBSElement* CBSElement::NewL( |
|
91 const TDesC8& aElementId, |
|
92 RBSObjOwningPtrArray<MBSElement>& aStructure ) |
|
93 { |
|
94 CBSElement* self = new ( ELeave ) CBSElement(); |
|
95 CleanupStack::PushL( self ); |
|
96 self->ConstructL( EBSList, |
|
97 aElementId, |
|
98 0, |
|
99 EFalse, |
|
100 NULL, |
|
101 KNullDesC, |
|
102 KNullDesC8, |
|
103 &aStructure ); |
|
104 CleanupStack::Pop( self ); //self |
|
105 return self; |
|
106 } |
|
107 |
|
108 CBSElement* CBSElement::NewL( |
|
109 const TDesC8& aElementId, |
|
110 CBSBitmap* aBitmap ) |
|
111 { |
|
112 CBSElement* self = new ( ELeave ) CBSElement() ; |
|
113 CleanupStack::PushL( self ); |
|
114 self->ConstructL( EBSBitmap, |
|
115 aElementId, |
|
116 0, |
|
117 EFalse, |
|
118 aBitmap, |
|
119 KNullDesC, |
|
120 KNullDesC8, |
|
121 NULL ); |
|
122 |
|
123 if( !aBitmap ) |
|
124 { |
|
125 User::Leave( KErrArgument ); |
|
126 } |
|
127 |
|
128 CleanupStack::Pop( self ); //self |
|
129 return self; |
|
130 } |
|
131 |
|
132 void CBSElement::ConstructL( |
|
133 TBSElementType aElementType, |
|
134 const TDesC8& aElementId, |
|
135 TInt aIntData, |
|
136 TBool aIntDataInitialized, |
|
137 CBSBitmap* aBitmap, |
|
138 const TDesC& aTextData, |
|
139 const TDesC8& aBufferData, |
|
140 RBSObjOwningPtrArray<MBSElement>* aStructure ) |
|
141 { |
|
142 iElementType = aElementType; |
|
143 |
|
144 iElementId = aElementId.AllocL(); |
|
145 |
|
146 iIntData = aIntData; |
|
147 iIntDataInitialized = aIntDataInitialized; |
|
148 |
|
149 iBitmapData = aBitmap; |
|
150 |
|
151 if( 0 != aTextData.Compare( KNullDesC() ) ) |
|
152 { |
|
153 iTextData = aTextData.AllocL(); |
|
154 } |
|
155 if( 0 != aBufferData.Compare( KNullDesC8() ) ) |
|
156 { |
|
157 iBufferData = aBufferData.AllocL(); |
|
158 } |
|
159 if( aStructure ) |
|
160 { |
|
161 iArrayInitialized = ETrue; |
|
162 iArray = *aStructure; |
|
163 } |
|
164 } |
|
165 |
|
166 |
|
167 CBSElement::~CBSElement() |
|
168 { |
|
169 delete iTextData; |
|
170 delete iBufferData; |
|
171 delete iElementId; |
|
172 |
|
173 iArray.Close(); |
|
174 delete iBitmapData; |
|
175 } |
|
176 |
|
177 |
|
178 CBSElement::CBSElement() |
|
179 { |
|
180 } |
|
181 |
|
182 void CBSElement::Close() |
|
183 { |
|
184 delete this; |
|
185 } |
|
186 |
|
187 |
|
188 TBSElementType CBSElement::ElementType() |
|
189 { |
|
190 return iElementType; |
|
191 } |
|
192 |
|
193 const TDesC8& CBSElement::ElementId() |
|
194 { |
|
195 return *iElementId; |
|
196 } |
|
197 |
|
198 TInt CBSElement::IntDataL() |
|
199 { |
|
200 if( !iIntDataInitialized ) |
|
201 { |
|
202 User::Leave( KErrArgument ); |
|
203 } |
|
204 return iIntData; |
|
205 } |
|
206 |
|
207 const TDesC& CBSElement::TextDataL() |
|
208 { |
|
209 if( !iTextData ) |
|
210 { |
|
211 User::Leave( KErrArgument ); |
|
212 } |
|
213 return *iTextData; |
|
214 } |
|
215 |
|
216 const TDesC8& CBSElement::BufferDataL() |
|
217 { |
|
218 if( !iBufferData ) |
|
219 { |
|
220 User::Leave( KErrArgument ); |
|
221 } |
|
222 return *iBufferData; |
|
223 } |
|
224 |
|
225 const CBSBitmap& CBSElement::BitmapDataL() |
|
226 { |
|
227 if( !iBitmapData ) |
|
228 { |
|
229 User::Leave( KErrArgument ); |
|
230 } |
|
231 return *iBitmapData; |
|
232 } |
|
233 |
|
234 TArray<MBSElement*> CBSElement::GetStructureL() |
|
235 { |
|
236 if( !iArrayInitialized ) |
|
237 { |
|
238 User::Leave( KErrArgument ); |
|
239 } |
|
240 |
|
241 return iArray.Array(); |
|
242 } |
|
243 |
|
244 const MBSElement& CBSElement::FindSubEntryL( const TDesC& aElementId ) |
|
245 { |
|
246 if ( !iArray.Count() ) |
|
247 { |
|
248 User::Leave( KErrNotFound ); |
|
249 } |
|
250 |
|
251 MBSElement* element = NULL; |
|
252 HBufC8* id = CnvUtfConverter::ConvertFromUnicodeToUtf8L( aElementId ); |
|
253 User::LeaveIfNull( id ); |
|
254 CleanupStack::PushL( id ); |
|
255 for ( TInt i( 0 ) ; i < iArray.Count() ; i++ ) |
|
256 { |
|
257 if ( iArray[ i ] ) |
|
258 { |
|
259 if ( !iArray[ i ]->ElementId().CompareC( *id ) ) |
|
260 { |
|
261 // Match found |
|
262 element = iArray[ i ]; |
|
263 i = iArray.Count();// Exit loop. |
|
264 } |
|
265 } |
|
266 } |
|
267 CleanupStack::PopAndDestroy( id ); |
|
268 |
|
269 if ( !element ) |
|
270 { |
|
271 User::Leave( KErrNotFound ); |
|
272 } |
|
273 |
|
274 return *element; |
|
275 } |
|
276 |
|
277 void CBSElement::ExternalizeL( RWriteStream& aWriteStream ) |
|
278 { |
|
279 // Write common header for all elements |
|
280 aWriteStream.WriteInt16L( iElementType ); |
|
281 aWriteStream.WriteInt16L( iElementId->Length() ); |
|
282 if( iElementId->Length() > 0 ) |
|
283 { |
|
284 // Write ID only if it's defined |
|
285 aWriteStream.WriteL( *iElementId ); |
|
286 } |
|
287 |
|
288 // Write element type specific data |
|
289 switch( iElementType ) |
|
290 { |
|
291 case EBSInt: |
|
292 { |
|
293 aWriteStream.WriteInt16L( iIntData ); |
|
294 break; |
|
295 } |
|
296 case EBSText: // flowthrough |
|
297 case EBSFile: |
|
298 { |
|
299 aWriteStream.WriteInt16L( iTextData->Length() ); |
|
300 aWriteStream.WriteL( *iTextData ); |
|
301 break; |
|
302 } |
|
303 case EBSList: |
|
304 { |
|
305 TInt count = iArray.Count(); |
|
306 aWriteStream.WriteInt16L( count ); |
|
307 // Write all subitems to stream |
|
308 for( TInt i = 0; i < count; i++ ) |
|
309 { |
|
310 MBSElement* element = iArray[i]; |
|
311 element->ExternalizeL( aWriteStream ); |
|
312 } |
|
313 break; |
|
314 } |
|
315 case EBSBitmap: |
|
316 { |
|
317 TPtrC8 ptr = iBitmapData->BitmapFileId(); |
|
318 aWriteStream.WriteInt16L( ptr.Length() ); |
|
319 aWriteStream.WriteL( ptr ); |
|
320 |
|
321 aWriteStream.WriteInt16L( iBitmapData->BitmapId() ); |
|
322 aWriteStream.WriteInt16L( iBitmapData->BitmapMaskId() ); |
|
323 aWriteStream.WriteInt16L( iBitmapData->SkinIdMajor() ); |
|
324 aWriteStream.WriteInt16L( iBitmapData->SkinIdMinor() ); |
|
325 |
|
326 break; |
|
327 } |
|
328 case EBSBuffer: |
|
329 { |
|
330 aWriteStream.WriteInt16L( iBufferData->Length() ); |
|
331 aWriteStream.WriteL( *iBufferData ); |
|
332 break; |
|
333 } |
|
334 default: |
|
335 { |
|
336 // unknown type! |
|
337 User::Leave( KErrCorrupt ); |
|
338 break; |
|
339 } |
|
340 } |
|
341 } |
|
342 |
|
343 |
|
344 // END OF FILE |