|
1 /** @file |
|
2 * Copyright (c) 2005-2006 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: UPnP container class implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include <e32base.h> |
|
21 #include "upnpobject.h" |
|
22 #include "upnpobjectlist.h" |
|
23 #include "upnpitem.h" |
|
24 #include "upnpcontainer.h" |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 // ============================ MEMBER FUNCTIONS =============================== |
|
35 |
|
36 // ----------------------------------------------------------------------------- |
|
37 // CUpnpObjectList::CUpnpObjectList |
|
38 // C++ default constructor can NOT contain any code, that |
|
39 // might leave. |
|
40 // ----------------------------------------------------------------------------- |
|
41 // |
|
42 CUpnpObjectList::CUpnpObjectList() |
|
43 { |
|
44 } |
|
45 |
|
46 // ----------------------------------------------------------------------------- |
|
47 // CUpnpObjectList::ConstructL |
|
48 // Symbian 2nd phase constructor can leave. |
|
49 // ----------------------------------------------------------------------------- |
|
50 // |
|
51 void CUpnpObjectList::ConstructL( ) |
|
52 { |
|
53 } |
|
54 |
|
55 // ----------------------------------------------------------------------------- |
|
56 // CUpnpObjectList::NewL |
|
57 // Two-phased constructor. |
|
58 // ----------------------------------------------------------------------------- |
|
59 // |
|
60 EXPORT_C CUpnpObjectList* CUpnpObjectList::NewL() |
|
61 { |
|
62 CUpnpObjectList* self = NewLC(); |
|
63 CleanupStack::Pop(self); |
|
64 return self; |
|
65 } |
|
66 |
|
67 // ----------------------------------------------------------------------------- |
|
68 // CUpnpObjectList::NewL |
|
69 // Two-phased constructor. |
|
70 // ----------------------------------------------------------------------------- |
|
71 // |
|
72 EXPORT_C CUpnpObjectList* CUpnpObjectList::NewLC() |
|
73 { |
|
74 CUpnpObjectList* self = new( ELeave ) CUpnpObjectList; |
|
75 CleanupStack::PushL( self ); |
|
76 self->ConstructL( ); |
|
77 return self; |
|
78 } |
|
79 |
|
80 // Destructor |
|
81 CUpnpObjectList::~CUpnpObjectList() |
|
82 { |
|
83 iObjects.ResetAndDestroy(); |
|
84 } |
|
85 |
|
86 // ----------------------------------------------------------------------------- |
|
87 // CUpnpObjectList::AppendObjectL |
|
88 // Appends object to internal array. |
|
89 // (other items were commented in a header). |
|
90 // ----------------------------------------------------------------------------- |
|
91 // |
|
92 EXPORT_C void CUpnpObjectList::AppendObjectL( CUpnpObject& aNewObject ) |
|
93 { |
|
94 iObjects.AppendL( &aNewObject ); |
|
95 } |
|
96 |
|
97 // ----------------------------------------------------------------------------- |
|
98 // CUpnpObjectList::RemoveAndDestroy |
|
99 // Removes and destroys object from internal array according to ID. |
|
100 // (other items were commented in a header). |
|
101 // ----------------------------------------------------------------------------- |
|
102 // |
|
103 EXPORT_C void CUpnpObjectList::RemoveAndDestroy( const TDesC8& aId ) |
|
104 { |
|
105 TInt idx = FindObjectIndex(aId); |
|
106 if(idx != KErrNotFound) |
|
107 { |
|
108 delete iObjects[idx]; |
|
109 iObjects.Remove(idx); |
|
110 } |
|
111 } |
|
112 // ----------------------------------------------------------------------------- |
|
113 // CUpnpObjectList::Remove |
|
114 // Detaches object from internal array according to ID and resturns its address. |
|
115 // (other items were commented in a header). |
|
116 // ----------------------------------------------------------------------------- |
|
117 // |
|
118 EXPORT_C CUpnpObject* CUpnpObjectList::Remove( const TDesC8& aId ) |
|
119 { |
|
120 CUpnpObject* ret = NULL; |
|
121 TInt idx = FindObjectIndex(aId); |
|
122 if(idx != KErrNotFound) |
|
123 { |
|
124 ret = iObjects[idx]; |
|
125 iObjects.Remove(idx); |
|
126 } |
|
127 return ret; |
|
128 } |
|
129 |
|
130 // ----------------------------------------------------------------------------- |
|
131 // CUpnpObjectList::FindObject |
|
132 // (other items were commented in a header). |
|
133 // ----------------------------------------------------------------------------- |
|
134 // |
|
135 EXPORT_C CUpnpObject* CUpnpObjectList::FindObject( const TDesC8& aId ) |
|
136 { |
|
137 CUpnpObject* ret = NULL; |
|
138 TInt idx = FindObjectIndex(aId); |
|
139 if(idx != KErrNotFound) |
|
140 { |
|
141 ret = iObjects[idx]; |
|
142 } |
|
143 return ret; |
|
144 } |
|
145 |
|
146 // ----------------------------------------------------------------------------- |
|
147 // CUpnpObjectList::ObjectCount |
|
148 // (other items were commented in a header). |
|
149 // ----------------------------------------------------------------------------- |
|
150 // |
|
151 TInt CUpnpObjectList::FindObjectIndex(const TDesC8& aObjId) const |
|
152 { |
|
153 TInt ret = KErrNotFound; |
|
154 |
|
155 // try to find the object with given id |
|
156 for(TInt i = 0; i < iObjects.Count(); i++) |
|
157 { |
|
158 if(iObjects[i]->Id() == aObjId) |
|
159 { |
|
160 ret = i; |
|
161 // stop searching |
|
162 break; |
|
163 } |
|
164 } |
|
165 |
|
166 return ret; |
|
167 } |
|
168 // ----------------------------------------------------------------------------- |
|
169 // CUpnpObjectList::ObjectCount |
|
170 // Counts objects |
|
171 // (other items were commented in a header). |
|
172 // ----------------------------------------------------------------------------- |
|
173 // |
|
174 EXPORT_C TInt CUpnpObjectList::ObjectCount( ) const |
|
175 { |
|
176 return iObjects.Count(); |
|
177 } |
|
178 |
|
179 // ----------------------------------------------------------------------------- |
|
180 // CUpnpObjectList::GetObjectL |
|
181 // Returns object from object array according to index |
|
182 // (other items were commented in a header). |
|
183 // ----------------------------------------------------------------------------- |
|
184 // |
|
185 EXPORT_C CUpnpObject* CUpnpObjectList::operator[]( TInt aIndex ) const |
|
186 { |
|
187 return iObjects[aIndex]; |
|
188 } |
|
189 |
|
190 // ----------------------------------------------------------------------------- |
|
191 // CUpnpObjectList::ExternalizeL |
|
192 // Writes the content to stream. |
|
193 // (other items were commented in a header). |
|
194 // ----------------------------------------------------------------------------- |
|
195 // |
|
196 EXPORT_C void CUpnpObjectList::ExternalizeL( RWriteStream& aStream ) const |
|
197 { |
|
198 // Let's write the count of items to stream first |
|
199 aStream.WriteInt16L( iObjects.Count() ); |
|
200 for ( TInt i = 0; i < iObjects.Count(); i++ ) |
|
201 { |
|
202 // Need to put the type of the object to stream |
|
203 TPckg<TUPnPObjectType> type( iObjects[i]->ObjectType() ); |
|
204 aStream << type; |
|
205 // Then the object itself |
|
206 iObjects[i]->ExternalizeL( aStream ); |
|
207 } |
|
208 } |
|
209 // ----------------------------------------------------------------------------- |
|
210 // CUpnpObjectList::InternalizeL |
|
211 // Fills container information from stream |
|
212 // (other items were commented in a header). |
|
213 // ----------------------------------------------------------------------------- |
|
214 // |
|
215 EXPORT_C void CUpnpObjectList::InternalizeL( RReadStream& aStream ) |
|
216 { |
|
217 // internalize the objects |
|
218 iObjects.ResetAndDestroy(); |
|
219 |
|
220 // First the count of individual objects |
|
221 TInt itemCount = aStream.ReadInt16L(); |
|
222 |
|
223 TUPnPObjectType type = EUPnPItem; |
|
224 TPckg<TUPnPObjectType> typePkg( type ); |
|
225 |
|
226 // Then internalize them from the stream one by one |
|
227 for (TInt index = 0; index < itemCount; index++ ) |
|
228 { |
|
229 aStream >> typePkg; |
|
230 |
|
231 switch ( type ) |
|
232 { |
|
233 case EUPnPItem: |
|
234 { |
|
235 CUpnpItem* newItem = CUpnpItem::NewL(); |
|
236 CleanupStack::PushL( newItem ); |
|
237 newItem->InternalizeL( aStream ); |
|
238 AppendObjectL( *newItem ); |
|
239 CleanupStack::Pop(); |
|
240 break; |
|
241 } |
|
242 case EUPnPContainer: |
|
243 { |
|
244 CUpnpContainer* newObj = CUpnpContainer::NewL(); |
|
245 CleanupStack::PushL( newObj ); |
|
246 newObj->InternalizeL( aStream ); |
|
247 AppendObjectL( *newObj ); |
|
248 CleanupStack::Pop(); |
|
249 break; |
|
250 } |
|
251 default: |
|
252 { |
|
253 break; |
|
254 } |
|
255 } |
|
256 } |
|
257 } |
|
258 // ----------------------------------------------------------------------------- |
|
259 // CUpnpObjectList::ToDes8L |
|
260 // (other items were commented in a header). |
|
261 // ----------------------------------------------------------------------------- |
|
262 // |
|
263 EXPORT_C HBufC8* CUpnpObjectList::ToDes8L() const |
|
264 { |
|
265 // serialize object |
|
266 CBufFlat* buf = CBufFlat::NewL(KDefBufferGranularity); |
|
267 CleanupStack::PushL(buf); |
|
268 RBufWriteStream stream(*buf); |
|
269 CleanupClosePushL(stream); |
|
270 |
|
271 stream << *this; |
|
272 |
|
273 // create heap descriptor |
|
274 HBufC8* hbuf = HBufC8::NewLC(buf->Size()); |
|
275 TPtr8 ptr(hbuf->Des()); |
|
276 buf->Read(0, ptr, buf->Size()); |
|
277 |
|
278 // clean up |
|
279 CleanupStack::Pop(hbuf); |
|
280 CleanupStack::PopAndDestroy(&stream); |
|
281 CleanupStack::PopAndDestroy(buf); |
|
282 |
|
283 return hbuf; |
|
284 } |
|
285 // ----------------------------------------------------------------------------- |
|
286 // CUpnpObjectList::ToDes8L |
|
287 // (other items were commented in a header). |
|
288 // ----------------------------------------------------------------------------- |
|
289 // |
|
290 EXPORT_C HBufC8* CUpnpObjectList::IdsToDes8L() const |
|
291 { |
|
292 // serialize object |
|
293 CBufFlat* buf = CBufFlat::NewL(KDefBufferGranularity); |
|
294 CleanupStack::PushL(buf); |
|
295 RBufWriteStream stream(*buf); |
|
296 CleanupClosePushL(stream); |
|
297 |
|
298 // number of elements |
|
299 TInt n = ObjectCount(); |
|
300 stream.WriteInt32L(n); |
|
301 |
|
302 // write each element |
|
303 for(TInt i = 0; i < n; i++) |
|
304 { |
|
305 stream << ( iObjects[i]->Id() ); |
|
306 } |
|
307 |
|
308 // create heap descriptor |
|
309 HBufC8* hbuf = HBufC8::NewLC(buf->Size()); |
|
310 TPtr8 ptr(hbuf->Des()); |
|
311 buf->Read(0, ptr, buf->Size()); |
|
312 |
|
313 // clean up |
|
314 CleanupStack::Pop(hbuf); |
|
315 CleanupStack::PopAndDestroy(&stream); |
|
316 CleanupStack::PopAndDestroy(buf); |
|
317 |
|
318 return hbuf; |
|
319 } |
|
320 |
|
321 // End of File |