|
1 /* |
|
2 * Copyright (c) 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: Implements CNcdNodeIcon class |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "ncdnodeiconimpl.h" |
|
20 #include "catalogssession.h" |
|
21 #include "catalogsbasemessage.h" |
|
22 #include "ncdnodemanager.h" |
|
23 #include "ncdnodemetadataimpl.h" |
|
24 #include "ncdnodefunctionids.h" |
|
25 #include "ncdnodeclassids.h" |
|
26 #include "ncd_pp_icon.h" |
|
27 #include "catalogsconstants.h" |
|
28 #include "ncd_pp_dataentity.h" |
|
29 #include "ncd_cp_query.h" |
|
30 #include "catalogsutils.h" |
|
31 #include "catalogsdebug.h" |
|
32 #include "ncdnodeidentifier.h" |
|
33 #include "ncderrors.h" |
|
34 #include "ncdutils.h" |
|
35 |
|
36 |
|
37 _LIT( KNcdPurchaseDetailsIconIdPrefix, "*PD_1c0n*"); |
|
38 |
|
39 CNcdNodeIcon* CNcdNodeIcon::NewL( |
|
40 CNcdNodeManager& aNodeManager, |
|
41 const CNcdNodeMetaData& aParentMetaData, |
|
42 TBool aUsePurchaseDetailsIcon ) |
|
43 { |
|
44 CNcdNodeIcon* self = |
|
45 CNcdNodeIcon::NewLC( aNodeManager, aParentMetaData, |
|
46 aUsePurchaseDetailsIcon ); |
|
47 CleanupStack::Pop( self ); |
|
48 return self; |
|
49 } |
|
50 |
|
51 CNcdNodeIcon* CNcdNodeIcon::NewLC( |
|
52 CNcdNodeManager& aNodeManager, |
|
53 const CNcdNodeMetaData& aParentMetaData, |
|
54 TBool aUsePurchaseDetailsIcon ) |
|
55 { |
|
56 CNcdNodeIcon* self = |
|
57 new( ELeave ) CNcdNodeIcon( |
|
58 NcdNodeClassIds::ENcdNodeIconClassId, |
|
59 aNodeManager, |
|
60 aParentMetaData, |
|
61 aUsePurchaseDetailsIcon ); |
|
62 CleanupClosePushL( *self ); |
|
63 self->ConstructL(); |
|
64 return self; |
|
65 } |
|
66 |
|
67 |
|
68 CNcdNodeIcon::CNcdNodeIcon( |
|
69 NcdNodeClassIds::TNcdNodeClassId aClassId, |
|
70 CNcdNodeManager& aNodeManager, |
|
71 const CNcdNodeMetaData& aParentMetaData, |
|
72 TBool aUsePurchaseDetailsIcon ) |
|
73 : CNcdCommunicable(), |
|
74 iNodeManager( aNodeManager ), |
|
75 iParentMetaData( aParentMetaData ), |
|
76 iClassId( aClassId ), |
|
77 iIconDataReady ( ETrue ), |
|
78 iUsePurchaseDetailsIcon( aUsePurchaseDetailsIcon ) |
|
79 { |
|
80 } |
|
81 |
|
82 void CNcdNodeIcon::ConstructL() |
|
83 { |
|
84 iDataBlock = KNullDesC().AllocL(); |
|
85 if( iUsePurchaseDetailsIcon ) |
|
86 { |
|
87 iIconId = HBufC::NewL( KNcdPurchaseDetailsIconIdPrefix().Length() + |
|
88 iParentMetaData.Identifier().NodeId().Length() + |
|
89 iParentMetaData.Identifier().NodeNameSpace().Length() ); |
|
90 iIconId->Des().Append( KNcdPurchaseDetailsIconIdPrefix ); |
|
91 iIconId->Des().Append( iParentMetaData.Identifier().NodeId() ); |
|
92 iIconId->Des().Append( iParentMetaData.Identifier().NodeNameSpace() ); |
|
93 } |
|
94 else |
|
95 { |
|
96 iIconId = KNullDesC().AllocL(); |
|
97 } |
|
98 iUri = KNullDesC().AllocL(); |
|
99 } |
|
100 |
|
101 CNcdNodeIcon::~CNcdNodeIcon() |
|
102 { |
|
103 DLTRACEIN(("")); |
|
104 |
|
105 // Delete member variables here |
|
106 |
|
107 delete iDataBlock; |
|
108 iDataBlock = NULL; |
|
109 |
|
110 delete iIconId; |
|
111 iIconId = NULL; |
|
112 |
|
113 delete iUri; |
|
114 iUri = NULL; |
|
115 |
|
116 DLTRACEOUT(("")); |
|
117 } |
|
118 |
|
119 NcdNodeClassIds::TNcdNodeClassId CNcdNodeIcon::ClassId() const |
|
120 { |
|
121 return iClassId; |
|
122 } |
|
123 |
|
124 |
|
125 // Internalization from the protocol |
|
126 |
|
127 void CNcdNodeIcon::InternalizeL( MNcdPreminetProtocolDataEntity& aData ) |
|
128 { |
|
129 DLTRACEIN(("")); |
|
130 |
|
131 iUsePurchaseDetailsIcon = EFalse; |
|
132 if ( ! aData.Icon() ) |
|
133 { |
|
134 // Icon data not present. |
|
135 User::Leave( KErrNotFound ); |
|
136 } |
|
137 |
|
138 // First create the new values |
|
139 |
|
140 HBufC* tmpDataBlock = aData.Icon()->DataBlock().AllocLC(); |
|
141 DLINFO((_L("datablock: %S"), tmpDataBlock)); |
|
142 |
|
143 HBufC* tmpId = aData.Icon()->Id().AllocLC(); |
|
144 DLINFO((_L("id: %S"), tmpId)); |
|
145 |
|
146 HBufC* tmpUri = aData.Icon()->Uri().AllocLC(); |
|
147 DLINFO((_L("uri: %S"), tmpUri)); |
|
148 |
|
149 |
|
150 // Now, that we are sure we have correctly created new values and the |
|
151 // code cannot leave here, set the tmp values to the member variables. |
|
152 |
|
153 delete iUri; |
|
154 iUri = tmpUri; |
|
155 CleanupStack::Pop( tmpUri ); |
|
156 |
|
157 delete iIconId; |
|
158 iIconId = tmpId; |
|
159 CleanupStack::Pop( tmpId ); |
|
160 |
|
161 delete iDataBlock; |
|
162 iDataBlock = tmpDataBlock; |
|
163 CleanupStack::Pop( tmpDataBlock ); |
|
164 |
|
165 // If icon data is obtained inline, save the data to database |
|
166 if ( aData.Icon()->Data() != KNullDesC8 ) |
|
167 { |
|
168 CNcdNodeIdentifier* iconIdentifier = |
|
169 CNcdNodeIdentifier::NewLC( iParentMetaData.Identifier().NodeNameSpace(), |
|
170 *iIconId, |
|
171 Uri(), |
|
172 iParentMetaData.Identifier().ClientUid() ); |
|
173 |
|
174 iNodeManager.DbSaveIconDataL( *iconIdentifier, |
|
175 aData.Icon()->Data() ); |
|
176 |
|
177 CleanupStack::PopAndDestroy( iconIdentifier ); |
|
178 } |
|
179 |
|
180 DLTRACEOUT(("")); |
|
181 } |
|
182 |
|
183 |
|
184 // Internalization from and externalization to the database |
|
185 |
|
186 void CNcdNodeIcon::ExternalizeL( RWriteStream& aStream ) |
|
187 { |
|
188 DLTRACEIN(("")); |
|
189 |
|
190 // Set all the membervariable values to the stream. So, |
|
191 // that the stream may be used later to create a new |
|
192 // object. |
|
193 |
|
194 // First insert data that the creator of this class object will use to |
|
195 // create this class object. Class id informs what class object |
|
196 // will be created. |
|
197 |
|
198 aStream.WriteInt32L( iClassId ); |
|
199 |
|
200 |
|
201 // Write the data that will be used when internalize function |
|
202 // is called. catalogsutils.h contains good internalize and |
|
203 // externalize functions. |
|
204 // Make sure that this matches to the order that is used |
|
205 // when this stream is internalized. |
|
206 |
|
207 ExternalizeDesL( *iDataBlock, aStream ); |
|
208 ExternalizeDesL( *iIconId, aStream ); |
|
209 ExternalizeDesL( *iUri, aStream ); |
|
210 aStream.WriteInt32L( iUsePurchaseDetailsIcon ); |
|
211 |
|
212 DLTRACEOUT(("")); |
|
213 } |
|
214 |
|
215 |
|
216 void CNcdNodeIcon::InternalizeL( RReadStream& aStream ) |
|
217 { |
|
218 DLTRACEIN(("")); |
|
219 |
|
220 // Read the class id first because it is set to the stream in internalize |
|
221 // function and it is not read from the stream anywhere else. |
|
222 TInt classId( aStream.ReadInt32L() ); |
|
223 if ( classId != ClassId() ) |
|
224 { |
|
225 DLERROR(("Wrong class id")); |
|
226 DASSERT( EFalse ); |
|
227 // Leave because the stream does not match this class object |
|
228 User::Leave( KErrCorrupt ); |
|
229 } |
|
230 |
|
231 InternalizeDesL( iDataBlock, aStream ); |
|
232 InternalizeDesL( iIconId, aStream ); |
|
233 InternalizeDesL( iUri, aStream ); |
|
234 iUsePurchaseDetailsIcon = aStream.ReadInt32L(); |
|
235 |
|
236 DLTRACEOUT(("")); |
|
237 } |
|
238 |
|
239 |
|
240 |
|
241 void CNcdNodeIcon::ReceiveMessage( MCatalogsBaseMessage* aMessage, |
|
242 TInt aFunctionNumber ) |
|
243 { |
|
244 DLTRACEIN(("")); |
|
245 |
|
246 DASSERT( aMessage ); |
|
247 |
|
248 // Now, we can be sure that rest of the time iMessage exists. |
|
249 // This member variable is set for the CounterPartLost function. |
|
250 iMessage = aMessage; |
|
251 |
|
252 TInt trapError( KErrNone ); |
|
253 |
|
254 // Check which function is called by the proxy side object. |
|
255 // Function number are located in ncdnodefunctinoids.h file. |
|
256 switch( aFunctionNumber ) |
|
257 { |
|
258 case NcdNodeFunctionIds::ENcdInternalize: |
|
259 // Internalize the proxy side according to the data |
|
260 // of this object. |
|
261 TRAP( trapError, InternalizeRequestL( *aMessage ) ); |
|
262 break; |
|
263 |
|
264 case NcdNodeFunctionIds::ENcdIconData: |
|
265 // Get icon data. |
|
266 TRAP( trapError, IconDataRequestL( *aMessage ) ); |
|
267 break; |
|
268 |
|
269 case NcdNodeFunctionIds::ENcdRelease: |
|
270 // The proxy does not want to use this object anymore. |
|
271 // So, release the handle from the session. |
|
272 ReleaseRequest( *aMessage ); |
|
273 break; |
|
274 |
|
275 default: |
|
276 DLERROR(("Unidentified function request")); |
|
277 DASSERT( EFalse ); |
|
278 break; |
|
279 } |
|
280 |
|
281 if ( trapError != KErrNone ) |
|
282 { |
|
283 // Because something went wrong, the complete has not been |
|
284 // yet called for the message. |
|
285 // So, inform the client about the error if the |
|
286 // message is still available. |
|
287 aMessage->CompleteAndRelease( trapError ); |
|
288 } |
|
289 |
|
290 // Because the message should not be used after this, set it NULL. |
|
291 // So, CounterPartLost function will know that no messages are |
|
292 // waiting the response at the moment. |
|
293 iMessage = NULL; |
|
294 |
|
295 DLTRACEOUT(("")); |
|
296 } |
|
297 |
|
298 void CNcdNodeIcon::CounterPartLost( const MCatalogsSession& aSession ) |
|
299 { |
|
300 // This function may be called whenever -- when the message is waiting |
|
301 // response or when the message does not exist. |
|
302 // iMessage may be NULL here, because in the end of the |
|
303 // ReceiveMessage it is set to NULL. The life time of the message |
|
304 // ends shortly after CompleteAndRelease is called. |
|
305 if ( iMessage != NULL ) |
|
306 { |
|
307 iMessage->CounterPartLost( aSession ); |
|
308 } |
|
309 } |
|
310 |
|
311 const TDesC& CNcdNodeIcon::DataBlock() const |
|
312 { |
|
313 DASSERT( iDataBlock ); |
|
314 return *iDataBlock; |
|
315 } |
|
316 |
|
317 const TDesC& CNcdNodeIcon::IconId() const |
|
318 { |
|
319 DASSERT( iIconId ); |
|
320 return *iIconId; |
|
321 } |
|
322 |
|
323 void CNcdNodeIcon::SetIconIdL( const TDesC& aIconId ) |
|
324 { |
|
325 HBufC* newId = aIconId.AllocL(); |
|
326 delete iIconId; |
|
327 iIconId = newId; |
|
328 } |
|
329 |
|
330 const TDesC& CNcdNodeIcon::Uri() const |
|
331 { |
|
332 DASSERT( iUri ); |
|
333 return *iUri; |
|
334 } |
|
335 |
|
336 void CNcdNodeIcon::SetUriL( const TDesC& aUri ) |
|
337 { |
|
338 HBufC* newUri = aUri.AllocL(); |
|
339 delete iUri; |
|
340 iUri = newUri; |
|
341 } |
|
342 |
|
343 TBool CNcdNodeIcon::IconDataReady() const |
|
344 { |
|
345 return iIconDataReady; |
|
346 } |
|
347 |
|
348 void CNcdNodeIcon::SetIconDataReady( TBool aValue ) |
|
349 { |
|
350 iIconDataReady = aValue; |
|
351 } |
|
352 |
|
353 |
|
354 void CNcdNodeIcon::InternalizeRequestL( MCatalogsBaseMessage& aMessage ) const |
|
355 { |
|
356 DLTRACEIN(("")); |
|
357 |
|
358 CBufBase* buf = CBufFlat::NewL( KBufExpandSize ); |
|
359 CleanupStack::PushL( buf ); |
|
360 |
|
361 RBufWriteStream stream( *buf ); |
|
362 CleanupClosePushL( stream ); |
|
363 |
|
364 |
|
365 // Include all the necessary data to the stream. |
|
366 ExternalizeDataForRequestL( stream ); |
|
367 |
|
368 |
|
369 // Commits data to the stream when closing. |
|
370 CleanupStack::PopAndDestroy( &stream ); |
|
371 |
|
372 |
|
373 // If this leaves, ReceiveMessage will complete the message. |
|
374 // NOTE: that here we expect that the buffer contains at least |
|
375 // some data. So, make sure that ExternalizeDataForRequestL inserts |
|
376 // something to the buffer. |
|
377 aMessage.CompleteAndReleaseL( buf->Ptr( 0 ), KErrNone ); |
|
378 |
|
379 |
|
380 DLINFO(("Deleting the buf")); |
|
381 CleanupStack::PopAndDestroy( buf ); |
|
382 |
|
383 DLTRACEOUT(("")); |
|
384 } |
|
385 |
|
386 |
|
387 void CNcdNodeIcon::ExternalizeDataForRequestL( RWriteStream& aStream ) const |
|
388 { |
|
389 DLTRACEIN(("")); |
|
390 |
|
391 if ( IsObsolete() ) |
|
392 { |
|
393 DLINFO(("Set as obsolete. This means that server has removed the object.")); |
|
394 User::Leave( KNcdErrorObsolete ); |
|
395 } |
|
396 |
|
397 // Icon existed. So, insert info that meta data was found. |
|
398 aStream.WriteInt32L( ClassId() ); |
|
399 |
|
400 // Add additional content to the stream. |
|
401 // Make sure that this matches to the order that is used in the proxy |
|
402 // side when this stream is internalized. |
|
403 // NOTE: Be careful with the 8- and 16-bit descriptors. Remember to check |
|
404 // if the proxy wants the data in 16 or 8 bits? |
|
405 |
|
406 ExternalizeDesL( *iIconId, aStream ); |
|
407 |
|
408 DLTRACEOUT(("")); |
|
409 } |
|
410 |
|
411 void CNcdNodeIcon::IconDataRequestL( MCatalogsBaseMessage& aMessage ) const |
|
412 { |
|
413 DLTRACEIN(("")); |
|
414 |
|
415 // If this leaves, ReceiveMessage will complete the message. |
|
416 |
|
417 if ( ! iIconId ) |
|
418 { |
|
419 User::Leave( KErrNotFound ); |
|
420 } |
|
421 |
|
422 HBufC8* icon = IconDataL(); |
|
423 CleanupStack::PushL( icon ); |
|
424 |
|
425 aMessage.CompleteAndReleaseL( *icon, KErrNone ); |
|
426 |
|
427 CleanupStack::PopAndDestroy( icon ); |
|
428 |
|
429 DLTRACEOUT(("")); |
|
430 } |
|
431 |
|
432 void CNcdNodeIcon::ReleaseRequest( MCatalogsBaseMessage& aMessage ) const |
|
433 { |
|
434 DLTRACEIN(("")); |
|
435 |
|
436 // Decrease the reference count for this object. |
|
437 // When the reference count reaches zero, this object will be destroyed |
|
438 // and removed from the session. |
|
439 MCatalogsSession& requestSession( aMessage.Session() ); |
|
440 TInt handle( aMessage.Handle() ); |
|
441 |
|
442 // Send complete information back to proxy. |
|
443 aMessage.CompleteAndRelease( KErrNone ); |
|
444 |
|
445 // Remove this object from the session. |
|
446 requestSession.RemoveObject( handle ); |
|
447 |
|
448 DLTRACEOUT(("")); |
|
449 } |
|
450 |
|
451 HBufC8* CNcdNodeIcon::IconDataL() const |
|
452 { |
|
453 if( iUsePurchaseDetailsIcon ) |
|
454 { |
|
455 // Get purchase details with icon. |
|
456 CNcdPurchaseDetails* purchaseDetails = |
|
457 iParentMetaData.PurchaseDetailsLC( ETrue ); |
|
458 HBufC8* iconData = purchaseDetails->GetIconData(); |
|
459 CleanupStack::PopAndDestroy( purchaseDetails ); |
|
460 return iconData; |
|
461 } |
|
462 else |
|
463 { |
|
464 CNcdNodeIdentifier* iconIdentifier = |
|
465 CNcdNodeIdentifier::NewLC( iParentMetaData.Identifier().NodeNameSpace(), |
|
466 *iIconId, |
|
467 Uri(), |
|
468 iParentMetaData.Identifier().ClientUid() ); |
|
469 HBufC8* icon = iNodeManager.DbIconDataLC( *iconIdentifier ); |
|
470 CleanupStack::Pop( icon ); |
|
471 CleanupStack::PopAndDestroy( iconIdentifier ); |
|
472 return icon; |
|
473 } |
|
474 } |