|
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: Contains CNcdFavoriteManagerProxy class |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <s32mem.h> |
|
20 |
|
21 #include "ncdfavoritemanagerproxy.h" |
|
22 #include "catalogsdebug.h" |
|
23 #include "catalogsinterfaceidentifier.h" |
|
24 #include "catalogsclientserver.h" |
|
25 #include "ncdnodefunctionids.h" |
|
26 #include "ncdnodeidentifier.h" |
|
27 #include "catalogsconstants.h" |
|
28 #include "ncdnodemanagerproxy.h" |
|
29 #include "ncdnodeproxy.h" |
|
30 #include "catalogsutils.h" |
|
31 #include "ncdnodedisclaimerproxy.h" |
|
32 #include "ncdnodeidentifiereditor.h" |
|
33 #include "ncdnodeidentifierutils.h" |
|
34 |
|
35 CNcdFavoriteManagerProxy* CNcdFavoriteManagerProxy::NewL( |
|
36 MCatalogsClientServer& aSession, |
|
37 TInt aHandle, |
|
38 CCatalogsInterfaceBase* aParent, |
|
39 CNcdNodeManagerProxy& aNodeManager ) |
|
40 { |
|
41 CNcdFavoriteManagerProxy* self = NewLC( |
|
42 aSession, aHandle, aParent, aNodeManager ); |
|
43 CleanupStack::Pop( self ); |
|
44 return self; |
|
45 } |
|
46 |
|
47 CNcdFavoriteManagerProxy* CNcdFavoriteManagerProxy::NewLC( |
|
48 MCatalogsClientServer& aSession, |
|
49 TInt aHandle, |
|
50 CCatalogsInterfaceBase* aParent, |
|
51 CNcdNodeManagerProxy& aNodeManager ) |
|
52 { |
|
53 CNcdFavoriteManagerProxy* self = |
|
54 new( ELeave ) CNcdFavoriteManagerProxy( |
|
55 aSession, aHandle, aParent, aNodeManager ); |
|
56 CleanupStack::PushL( self ); |
|
57 self->ConstructL(); |
|
58 return self; |
|
59 } |
|
60 |
|
61 CNcdFavoriteManagerProxy::CNcdFavoriteManagerProxy( |
|
62 MCatalogsClientServer& aSession, |
|
63 TInt aHandle, |
|
64 CCatalogsInterfaceBase* aParent, |
|
65 CNcdNodeManagerProxy& aNodeManager ) |
|
66 : CNcdInterfaceBaseProxy( aSession, aHandle, aParent ), |
|
67 iNodeManager( aNodeManager ) |
|
68 { |
|
69 } |
|
70 |
|
71 CNcdFavoriteManagerProxy::~CNcdFavoriteManagerProxy() |
|
72 { |
|
73 DLTRACEIN(("")); |
|
74 iFavoriteNodes.ResetAndDestroy(); |
|
75 } |
|
76 |
|
77 |
|
78 TBool CNcdFavoriteManagerProxy::IsFavoriteL( |
|
79 const CNcdNodeIdentifier& aNodeIdentifier ) const |
|
80 { |
|
81 DLTRACEIN(("")); |
|
82 |
|
83 // If the metadata is favorite, the node itself is favorite. |
|
84 CNcdNodeIdentifier* actualFavId = ActualFavoriteIdentifierL( aNodeIdentifier ); |
|
85 return actualFavId != NULL; |
|
86 } |
|
87 |
|
88 |
|
89 void CNcdFavoriteManagerProxy::AddToFavoritesL( |
|
90 const CNcdNodeIdentifier& aNodeIdentifier, TBool aRemoveOnDisconnect ) |
|
91 { |
|
92 DLTRACEIN(("")); |
|
93 |
|
94 // Check if the metadata is already added as favorite. |
|
95 CNcdNodeIdentifier* metadataId = |
|
96 NcdNodeIdentifierEditor::CreateMetaDataIdentifierLC( aNodeIdentifier ); |
|
97 if ( FavoriteNodeByMetaDataL( *metadataId ) ) |
|
98 { |
|
99 // The metadata is already favorite, do nothing. |
|
100 CleanupStack::PopAndDestroy( metadataId ); |
|
101 return; |
|
102 } |
|
103 |
|
104 CBufBase* buf = CBufFlat::NewL( KBufExpandSize ); |
|
105 CleanupStack::PushL( buf ); |
|
106 |
|
107 RBufWriteStream stream( *buf ); |
|
108 CleanupClosePushL( stream ); |
|
109 |
|
110 aNodeIdentifier.ExternalizeL( stream ); |
|
111 stream.WriteInt8L( aRemoveOnDisconnect ); |
|
112 |
|
113 CleanupStack::PopAndDestroy( &stream ); |
|
114 |
|
115 TInt output; |
|
116 |
|
117 // Send the message to server side. |
|
118 User::LeaveIfError( |
|
119 ClientServerSession().SendSync( |
|
120 NcdNodeFunctionIds::ENcdFavoriteManagerAddFavorite, |
|
121 buf->Ptr( 0 ), |
|
122 output, |
|
123 Handle() ) ); |
|
124 |
|
125 CleanupStack::PopAndDestroy( buf ); |
|
126 |
|
127 // Add the identifier to proxy side list too. |
|
128 CNcdNodeIdentifier* copy = CNcdNodeIdentifier::NewLC( aNodeIdentifier ); |
|
129 iFavoriteNodes.AppendL( copy ); |
|
130 CleanupStack::Pop( copy ); |
|
131 CleanupStack::PopAndDestroy( metadataId ); |
|
132 |
|
133 // In principle, the other node proxys having the same metadata should |
|
134 // be provided with the MNcdNodeFavorite interface as well. |
|
135 } |
|
136 |
|
137 |
|
138 void CNcdFavoriteManagerProxy::RemoveFromFavoritesL( |
|
139 const CNcdNodeIdentifier& aNodeIdentifier ) |
|
140 { |
|
141 DLTRACEIN(("")); |
|
142 |
|
143 // The actual node to remove may have different id but same metadata, |
|
144 // get the actual favorite node. |
|
145 CNcdNodeIdentifier* actualIdentifier = ActualFavoriteIdentifierL( aNodeIdentifier ); |
|
146 if ( !actualIdentifier ) |
|
147 { |
|
148 // Nothing to do, the node is not favourite. |
|
149 return; |
|
150 } |
|
151 |
|
152 CBufBase* buf = CBufFlat::NewL( KBufExpandSize ); |
|
153 CleanupStack::PushL( buf ); |
|
154 |
|
155 RBufWriteStream stream( *buf ); |
|
156 CleanupClosePushL( stream ); |
|
157 |
|
158 actualIdentifier->ExternalizeL( stream ); |
|
159 |
|
160 CleanupStack::PopAndDestroy( &stream ); |
|
161 |
|
162 TInt output; |
|
163 |
|
164 // Send the message to server side. |
|
165 User::LeaveIfError( |
|
166 ClientServerSession().SendSync( |
|
167 NcdNodeFunctionIds::ENcdFavoriteManagerRemoveFavorite, |
|
168 buf->Ptr( 0 ), |
|
169 output, |
|
170 Handle() ) ); |
|
171 |
|
172 CleanupStack::PopAndDestroy( buf ); |
|
173 |
|
174 // Remove the identifier from proxy side list too. |
|
175 TInt nodeCount = iFavoriteNodes.Count(); |
|
176 for ( TInt i = 0; i < nodeCount; i++ ) |
|
177 { |
|
178 if ( iFavoriteNodes[i]->Equals( *actualIdentifier ) ) |
|
179 { |
|
180 delete iFavoriteNodes[i]; |
|
181 iFavoriteNodes.Remove( i ); |
|
182 break; |
|
183 } |
|
184 } |
|
185 |
|
186 // In principle, the other node proxys having the same metadata should |
|
187 // not have the MNcdNodeFavorite interface after this, so it should be removed. |
|
188 } |
|
189 |
|
190 |
|
191 void CNcdFavoriteManagerProxy::SetNodeDisclaimerL( |
|
192 const CNcdNodeIdentifier& aFavoriteNodeIdentifier, |
|
193 MNcdNode* aDisclaimerOwner ) |
|
194 { |
|
195 DLTRACEIN(("")); |
|
196 |
|
197 // If the node is not favorite, leave |
|
198 if ( !IsFavoriteL( aFavoriteNodeIdentifier ) ) |
|
199 { |
|
200 User::Leave( KErrArgument ); |
|
201 } |
|
202 |
|
203 CNcdNodeIdentifier* actualFavIdentifier = |
|
204 ActualFavoriteIdentifierL( aFavoriteNodeIdentifier ); |
|
205 DASSERT( actualFavIdentifier ); |
|
206 |
|
207 if ( aDisclaimerOwner ) |
|
208 { |
|
209 CNcdNodeIdentifier* disclaimerOwnerId = |
|
210 CNcdNodeIdentifier::NewLC( |
|
211 aDisclaimerOwner->Namespace(), aDisclaimerOwner->Id(), |
|
212 aFavoriteNodeIdentifier.ClientUid() ); |
|
213 |
|
214 RCatalogsBufferWriter writer; |
|
215 writer.OpenLC(); |
|
216 actualFavIdentifier->ExternalizeL( writer() ); |
|
217 writer().WriteInt32L( ETrue ); |
|
218 disclaimerOwnerId->ExternalizeL( writer() ); |
|
219 |
|
220 TInt output; |
|
221 |
|
222 // Send the message to server side. |
|
223 User::LeaveIfError( |
|
224 ClientServerSession().SendSync( |
|
225 NcdNodeFunctionIds::ENcdFavoriteManagerSetDisclaimer, |
|
226 writer.PtrL(), |
|
227 output, |
|
228 Handle() ) ); |
|
229 |
|
230 CleanupStack::PopAndDestroy( &writer ); |
|
231 CleanupStack::PopAndDestroy( disclaimerOwnerId ); |
|
232 } |
|
233 else |
|
234 { |
|
235 // NULL node was given, remove the disclaimer. |
|
236 |
|
237 RCatalogsBufferWriter writer; |
|
238 writer.OpenLC(); |
|
239 actualFavIdentifier->ExternalizeL( writer() ); |
|
240 writer().WriteInt32L( EFalse ); |
|
241 |
|
242 TInt output; |
|
243 |
|
244 // Send the message to server side. |
|
245 User::LeaveIfError( |
|
246 ClientServerSession().SendSync( |
|
247 NcdNodeFunctionIds::ENcdFavoriteManagerSetDisclaimer, |
|
248 writer.PtrL(), |
|
249 output, |
|
250 Handle() ) ); |
|
251 |
|
252 CleanupStack::PopAndDestroy( &writer ); |
|
253 } |
|
254 } |
|
255 |
|
256 |
|
257 CNcdNodeDisclaimerProxy* CNcdFavoriteManagerProxy::NodeDisclaimerL( |
|
258 const CNcdNodeIdentifier& aNodeIdentifier ) |
|
259 { |
|
260 DLTRACEIN(("")); |
|
261 |
|
262 // If the node is not favorite, leave |
|
263 if ( !IsFavoriteL( aNodeIdentifier ) ) |
|
264 { |
|
265 User::Leave( KErrArgument ); |
|
266 } |
|
267 |
|
268 CNcdNodeIdentifier* actualFavIdentifier = |
|
269 ActualFavoriteIdentifierL( aNodeIdentifier ); |
|
270 DASSERT( actualFavIdentifier ); |
|
271 |
|
272 // Get the disclaimer handle at first. |
|
273 |
|
274 RCatalogsBufferWriter writer; |
|
275 writer.OpenLC(); |
|
276 actualFavIdentifier->ExternalizeL( writer() ); |
|
277 |
|
278 TInt disclaimerHandle; |
|
279 TInt handleError = |
|
280 ClientServerSession().SendSync( |
|
281 NcdNodeFunctionIds::ENcdFavoriteManagerDisclaimerHandle, |
|
282 writer.PtrL(), |
|
283 disclaimerHandle, |
|
284 Handle() ); |
|
285 |
|
286 CleanupStack::PopAndDestroy( &writer ); |
|
287 |
|
288 if ( handleError == KErrNotFound ) |
|
289 { |
|
290 // Disclaimer did not exist in server side, return NULL. |
|
291 return NULL; |
|
292 } |
|
293 |
|
294 User::LeaveIfError( handleError ); |
|
295 |
|
296 CNcdNodeDisclaimerProxy* disclaimerProxy = |
|
297 CNcdNodeDisclaimerProxy::NewL( |
|
298 ClientServerSession(), disclaimerHandle, NULL ); |
|
299 |
|
300 return disclaimerProxy; |
|
301 } |
|
302 |
|
303 |
|
304 CNcdNodeProxy* CNcdFavoriteManagerProxy::FavoriteNodeByMetaDataL( |
|
305 const CNcdNodeIdentifier& aMetaDataIdentifier ) const |
|
306 { |
|
307 DLTRACEIN(("")); |
|
308 TInt count = iFavoriteNodes.Count(); |
|
309 for ( TInt i = 0; i < count; i++ ) |
|
310 { |
|
311 CNcdNodeIdentifier* metaDataId = |
|
312 NcdNodeIdentifierEditor::CreateMetaDataIdentifierL( *iFavoriteNodes[ i ] ); |
|
313 if ( metaDataId->Equals( aMetaDataIdentifier ) ) |
|
314 { |
|
315 delete metaDataId; |
|
316 return &iNodeManager.NodeL( *iFavoriteNodes[ i ] ); |
|
317 } |
|
318 delete metaDataId; |
|
319 } |
|
320 |
|
321 // Node was not found, return NULL. |
|
322 return NULL; |
|
323 } |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 TInt CNcdFavoriteManagerProxy::FavoriteNodeCount() const |
|
329 { |
|
330 DLTRACEIN(("")); |
|
331 return iFavoriteNodes.Count(); |
|
332 } |
|
333 |
|
334 |
|
335 MNcdNode* CNcdFavoriteManagerProxy::FavoriteNodeL( TInt aIndex ) const |
|
336 { |
|
337 DLTRACEIN(("")); |
|
338 |
|
339 if ( aIndex < 0 || aIndex >= iFavoriteNodes.Count() ) |
|
340 { |
|
341 User::Leave( KErrArgument ); |
|
342 } |
|
343 |
|
344 // The favorite list is in order where the previously added item is last. |
|
345 // Return the nodes in the opposite order. |
|
346 |
|
347 TInt realIndex = iFavoriteNodes.Count() - 1 - aIndex; |
|
348 CNcdNodeIdentifier* nodeId = iFavoriteNodes[ realIndex ]; |
|
349 MNcdNode* node = &iNodeManager.NodeL( *nodeId ); |
|
350 |
|
351 // Increase the reference count by one. |
|
352 node->AddRef(); |
|
353 |
|
354 return node; |
|
355 } |
|
356 |
|
357 |
|
358 TInt CNcdFavoriteManagerProxy::FavoriteIndexL( |
|
359 const TDesC& aNamespace, const TDesC& aId ) const |
|
360 { |
|
361 DLTRACEIN(("")); |
|
362 |
|
363 if ( iFavoriteNodes.Count() == 0 ) |
|
364 { |
|
365 User::Leave( KErrNotFound ); |
|
366 } |
|
367 |
|
368 CNcdNodeIdentifier* nodeId = CNcdNodeIdentifier::NewLC( |
|
369 aNamespace, aId, iFavoriteNodes[ 0 ]->ClientUid() ); |
|
370 |
|
371 // Get the actual favorite node identifier. It may be different, since |
|
372 // there is only one node per metadata as favorite node. |
|
373 // Note, the ownership of the returned object is not transferred. |
|
374 CNcdNodeIdentifier* actualId = ActualFavoriteIdentifierL( *nodeId ); |
|
375 |
|
376 if ( actualId == NULL ) |
|
377 { |
|
378 User::Leave( KErrNotFound ); |
|
379 } |
|
380 |
|
381 TInt index = NcdNodeIdentifierUtils::IdentifierIndex( |
|
382 *actualId, iFavoriteNodes ); |
|
383 DASSERT( index != KErrNotFound ); |
|
384 |
|
385 CleanupStack::PopAndDestroy( nodeId ); |
|
386 |
|
387 // Since the list is in opposite order, the index must be converted. |
|
388 TInt correctIndex = iFavoriteNodes.Count() - 1 - index; |
|
389 DLINFO(("returning index: %d", correctIndex )); |
|
390 return correctIndex; |
|
391 } |
|
392 |
|
393 |
|
394 void CNcdFavoriteManagerProxy::ClearFavoritesL() |
|
395 { |
|
396 DLTRACEIN(("")); |
|
397 while ( iFavoriteNodes.Count() ) |
|
398 { |
|
399 MNcdNode* node = FavoriteNodeL( 0 ); |
|
400 CleanupReleasePushL( *node ); |
|
401 |
|
402 MNcdNodeFavorite* favorite = node->QueryInterfaceLC< MNcdNodeFavorite >(); |
|
403 DASSERT( favorite != NULL ); |
|
404 favorite->RemoveFromFavoritesL(); |
|
405 |
|
406 CleanupStack::PopAndDestroy( favorite ); |
|
407 CleanupStack::PopAndDestroy( node ); |
|
408 } |
|
409 } |
|
410 |
|
411 |
|
412 void CNcdFavoriteManagerProxy::ConstructL() |
|
413 { |
|
414 DLTRACEIN(("")); |
|
415 |
|
416 // Register the interface |
|
417 MNcdFavoriteManager* interface( this ); |
|
418 AddInterfaceL( |
|
419 CCatalogsInterfaceIdentifier::NewL( |
|
420 interface, this, MNcdFavoriteManager::KInterfaceUid ) ); |
|
421 |
|
422 // Internalize the state from server side object |
|
423 InternalizeL(); |
|
424 } |
|
425 |
|
426 void CNcdFavoriteManagerProxy::InternalizeL() |
|
427 { |
|
428 DLTRACEIN(("")); |
|
429 |
|
430 HBufC8* data( NULL ); |
|
431 |
|
432 // Internalize the node list from server side object |
|
433 User::LeaveIfError( |
|
434 ClientServerSession().SendSyncAlloc( |
|
435 NcdNodeFunctionIds::ENcdInternalize, |
|
436 KNullDesC8(), |
|
437 data, |
|
438 Handle(), |
|
439 0 ) ); |
|
440 |
|
441 CleanupStack::PushL( data ); |
|
442 |
|
443 // Read the data from the stream and insert it to the memeber variables |
|
444 RDesReadStream stream( *data ); |
|
445 CleanupClosePushL( stream ); |
|
446 |
|
447 InternalizeDataL( stream ); |
|
448 |
|
449 // Closes the stream |
|
450 CleanupStack::PopAndDestroy( &stream ); |
|
451 CleanupStack::PopAndDestroy( data ); |
|
452 } |
|
453 |
|
454 void CNcdFavoriteManagerProxy::InternalizeDataL( RReadStream& aStream ) |
|
455 { |
|
456 DLTRACEIN(("")); |
|
457 iFavoriteNodes.ResetAndDestroy(); |
|
458 |
|
459 TInt identifierCount = aStream.ReadInt32L(); |
|
460 for ( TInt i = 0; i < identifierCount; i++ ) |
|
461 { |
|
462 CNcdNodeIdentifier* nodeId = CNcdNodeIdentifier::NewLC( aStream ); |
|
463 iFavoriteNodes.AppendL( nodeId ); |
|
464 CleanupStack::Pop( nodeId ); |
|
465 } |
|
466 } |
|
467 |
|
468 CNcdNodeIdentifier* CNcdFavoriteManagerProxy::ActualFavoriteIdentifierL( |
|
469 const CNcdNodeIdentifier& aNodeIdentifier ) const |
|
470 { |
|
471 DLTRACEIN(("")); |
|
472 CNcdNodeIdentifier* metadataId = |
|
473 NcdNodeIdentifierEditor::CreateMetaDataIdentifierLC( aNodeIdentifier ); |
|
474 CNcdNodeIdentifier* actualId = FavoriteIdentifierByMetaDataL( *metadataId ); |
|
475 CleanupStack::PopAndDestroy( metadataId ); |
|
476 return actualId; |
|
477 } |
|
478 |
|
479 CNcdNodeIdentifier* CNcdFavoriteManagerProxy::FavoriteIdentifierByMetaDataL( |
|
480 const CNcdNodeIdentifier& aMetaDataIdentifier ) const |
|
481 { |
|
482 DLTRACEIN(("")); |
|
483 TInt count = iFavoriteNodes.Count(); |
|
484 for ( TInt i = 0; i < count; i++ ) |
|
485 { |
|
486 CNcdNodeIdentifier* metaId = NcdNodeIdentifierEditor::CreateMetaDataIdentifierL( |
|
487 *iFavoriteNodes[ i ] ); |
|
488 if ( metaId->Equals( aMetaDataIdentifier ) ) |
|
489 { |
|
490 delete metaId; |
|
491 return iFavoriteNodes[ i ]; |
|
492 } |
|
493 delete metaId; |
|
494 } |
|
495 |
|
496 return NULL; |
|
497 } |
|
498 |