|
1 /* |
|
2 * Copyright (c) 2008 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: ?Description |
|
15 |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include <liwcommon.h> |
|
21 #include <s32mem.h> |
|
22 |
|
23 #include "cpnotificationhandler.h" |
|
24 #include "cpdebug.h" |
|
25 #include "cpliwmap.h" |
|
26 #include "cpglobals.h" |
|
27 #include "cpserverdef.h" |
|
28 |
|
29 // ======== MEMBER FUNCTIONS ======== |
|
30 |
|
31 // --------------------------------------------------------------------------- |
|
32 // |
|
33 // --------------------------------------------------------------------------- |
|
34 // |
|
35 CCPNotificationHandler::CCPNotificationHandler() |
|
36 { |
|
37 |
|
38 } |
|
39 |
|
40 // --------------------------------------------------------------------------- |
|
41 // |
|
42 // --------------------------------------------------------------------------- |
|
43 // |
|
44 void CCPNotificationHandler::ConstructL( RPointerArray<CLiwDefaultList>& |
|
45 aNotifications) |
|
46 { |
|
47 iChangeInfoList = CLiwGenericParamList::NewL( ); |
|
48 for ( TInt i(0); i< aNotifications.Count( ); i++ ) |
|
49 { |
|
50 iNotifications.AppendL(aNotifications[i]); |
|
51 aNotifications[i]->IncRef(); |
|
52 } |
|
53 } |
|
54 |
|
55 // --------------------------------------------------------------------------- |
|
56 // |
|
57 // --------------------------------------------------------------------------- |
|
58 // |
|
59 CCPNotificationHandler* CCPNotificationHandler::NewL( |
|
60 RPointerArray<CLiwDefaultList>& aNotifications ) |
|
61 { |
|
62 CCPNotificationHandler* self = CCPNotificationHandler::NewLC( |
|
63 aNotifications ); |
|
64 CleanupStack::Pop( self ); |
|
65 return self; |
|
66 } |
|
67 |
|
68 // --------------------------------------------------------------------------- |
|
69 // |
|
70 // --------------------------------------------------------------------------- |
|
71 // |
|
72 CCPNotificationHandler* CCPNotificationHandler::NewLC( |
|
73 RPointerArray<CLiwDefaultList>& aNotifications ) |
|
74 { |
|
75 CCPNotificationHandler* self = new( ELeave ) CCPNotificationHandler; |
|
76 CleanupStack::PushL( self ); |
|
77 self->ConstructL( aNotifications ); |
|
78 return self; |
|
79 } |
|
80 |
|
81 // --------------------------------------------------------------------------- |
|
82 // |
|
83 // --------------------------------------------------------------------------- |
|
84 // |
|
85 CCPNotificationHandler::~CCPNotificationHandler() |
|
86 { |
|
87 THashMapIter<TInt32, CCPLiwMap*> iter( iFilters ); |
|
88 |
|
89 const TInt32* transaction = iter.NextKey(); |
|
90 while( transaction ) |
|
91 { |
|
92 CCPLiwMap** filter = iFilters.Find( *transaction );// leaves if not found |
|
93 if ( filter ) |
|
94 { |
|
95 (*filter)->Reset(); |
|
96 (*filter)->Close(); |
|
97 } |
|
98 transaction = iter.NextKey( ); |
|
99 } |
|
100 iFilters.Close(); |
|
101 Reset( ); |
|
102 for ( TInt i(0); i< iNotifications.Count( ); i++ ) |
|
103 { |
|
104 iNotifications[i]->Close( ); |
|
105 } |
|
106 iNotifications.Close( ); |
|
107 delete iChangeInfoList; |
|
108 } |
|
109 |
|
110 // --------------------------------------------------------------------------- |
|
111 // |
|
112 // --------------------------------------------------------------------------- |
|
113 // |
|
114 void CCPNotificationHandler::SaveMessageL( const RMessage2& aMessage ) |
|
115 { |
|
116 iIsReadyToSend = ETrue; |
|
117 iMessage = aMessage; |
|
118 |
|
119 TInt32 transactionId = aMessage.Int3(); |
|
120 if ( transactionId != KErrAlreadyExists ) |
|
121 { |
|
122 TInt deslen = aMessage.GetDesLengthL( KDescriptorPosition ); |
|
123 if( deslen > 0 ) |
|
124 { |
|
125 HBufC8* buffer = HBufC8::NewLC( deslen ); |
|
126 TPtr8 tempDes = buffer->Des( ); |
|
127 aMessage.Read( KDescriptorPosition, tempDes ); |
|
128 RDesReadStream datastrm( *buffer); |
|
129 CleanupClosePushL( datastrm ); |
|
130 CCPLiwMap* filterMap = CCPLiwMap::NewL( datastrm ); |
|
131 CleanupStack::PushL( filterMap ); |
|
132 iFilters.InsertL( transactionId, filterMap ); |
|
133 CleanupStack::Pop( filterMap ); |
|
134 CleanupStack::PopAndDestroy( &datastrm ); |
|
135 CleanupStack::PopAndDestroy( buffer ); |
|
136 } |
|
137 } |
|
138 if ( iNotifications.Count( ) ) |
|
139 { |
|
140 SendNotificationL( NULL ); |
|
141 } |
|
142 } |
|
143 |
|
144 // --------------------------------------------------------------------------- |
|
145 // |
|
146 // --------------------------------------------------------------------------- |
|
147 // |
|
148 void CCPNotificationHandler::AddObserverL( const RMessage2& aMessage ) |
|
149 { |
|
150 TInt32 transactionId = aMessage.Int3(); |
|
151 TInt deslen = aMessage.GetDesLength( KDescriptorPosition ); |
|
152 if( deslen > 0 ) |
|
153 { |
|
154 HBufC8* buffer = HBufC8::NewLC( deslen ); |
|
155 TPtr8 tempDes = buffer->Des( ); |
|
156 aMessage.Read( KDescriptorPosition, tempDes ); |
|
157 RDesReadStream datastrm( *buffer); |
|
158 CleanupClosePushL( datastrm ); |
|
159 CCPLiwMap* filterMap = CCPLiwMap::NewL( datastrm ); |
|
160 CleanupStack::PushL( filterMap ); |
|
161 iFilters.InsertL( transactionId, filterMap ); |
|
162 CleanupStack::Pop( filterMap ); |
|
163 CleanupStack::PopAndDestroy( &datastrm ); |
|
164 CleanupStack::PopAndDestroy( buffer ); |
|
165 } |
|
166 } |
|
167 |
|
168 // --------------------------------------------------------------------------- |
|
169 // |
|
170 // --------------------------------------------------------------------------- |
|
171 // |
|
172 void CCPNotificationHandler::RemoveObserverL( const RMessage2& aMessage ) |
|
173 { |
|
174 TInt32 transactionId = aMessage.Int3(); |
|
175 CCPLiwMap* filter = iFilters.FindL( transactionId );// leaves if not found |
|
176 filter->Reset(); |
|
177 filter->Close(); |
|
178 iFilters.Remove( transactionId ); |
|
179 } |
|
180 |
|
181 // ----------------------------------------------------------------------------- |
|
182 // |
|
183 // ----------------------------------------------------------------------------- |
|
184 // |
|
185 TBool CCPNotificationHandler::IsProperForFilterL( const CLiwDefaultMap& aMap, |
|
186 const CCPLiwMap& aFilter ) |
|
187 { |
|
188 CP_DEBUG( _L8("CCPNotificationHandler::IsProperForFilter()") ); |
|
189 TBool result( EFalse ); |
|
190 if( CheckRegistryTypeL( aMap, aFilter ) && |
|
191 CheckOperationTypeL( aMap, aFilter ) ) |
|
192 { |
|
193 if( CheckIdL( aMap, aFilter ) ) |
|
194 { |
|
195 result= ETrue; |
|
196 } |
|
197 else if( CheckPropertiesL( aMap, aFilter ) ) |
|
198 { |
|
199 result = ETrue; |
|
200 } |
|
201 } |
|
202 return result; |
|
203 } |
|
204 |
|
205 // ---------------------------------------------------------------------------- |
|
206 // |
|
207 // ---------------------------------------------------------------------------- |
|
208 // |
|
209 void CCPNotificationHandler::SendNotificationL( CLiwDefaultList* aListOfMaps ) |
|
210 { |
|
211 CP_DEBUG( _L8("CCPNotificationHandler::SendNotificationL()") ); |
|
212 if ( iIsReadyToSend ) |
|
213 { |
|
214 //notification can be send |
|
215 if ( iNotifications.Count( ) ) |
|
216 { |
|
217 //at least one notification is waiting to be sent |
|
218 TBool sent; |
|
219 sent = SendChangeInfoListL( iNotifications[0] ); |
|
220 iNotifications[0]->Close( ); |
|
221 iNotifications.Remove( 0 ); |
|
222 if ( !sent ) |
|
223 { |
|
224 //if notification from array wasn't sent because it didn't |
|
225 //match the filter, try to send the next one |
|
226 SendNotificationL( NULL ); |
|
227 } |
|
228 else |
|
229 { |
|
230 //message was send |
|
231 iIsReadyToSend = EFalse; |
|
232 } |
|
233 } |
|
234 else |
|
235 { |
|
236 //no notification is waiting, so send the new one |
|
237 if ( aListOfMaps && SendChangeInfoListL( aListOfMaps ) ) |
|
238 { |
|
239 iIsReadyToSend = EFalse; |
|
240 } |
|
241 } |
|
242 } |
|
243 else |
|
244 { |
|
245 //notification cannot be sent in this moment |
|
246 aListOfMaps->IncRef( ); |
|
247 iNotifications.AppendL( aListOfMaps ); |
|
248 } |
|
249 } |
|
250 |
|
251 // ---------------------------------------------------------------------------- |
|
252 // |
|
253 // ---------------------------------------------------------------------------- |
|
254 // |
|
255 |
|
256 const CLiwGenericParamList* CCPNotificationHandler::GetPointerToChangeInfoList() |
|
257 { |
|
258 return iChangeInfoList; |
|
259 } |
|
260 |
|
261 // ---------------------------------------------------------------------------- |
|
262 // |
|
263 // ---------------------------------------------------------------------------- |
|
264 // |
|
265 void CCPNotificationHandler::Reset() |
|
266 { |
|
267 iIsReadyToSend = EFalse; |
|
268 iChangeInfoList->Reset( ); |
|
269 } |
|
270 |
|
271 // ---------------------------------------------------------------------------- |
|
272 // |
|
273 // ---------------------------------------------------------------------------- |
|
274 // |
|
275 void CCPNotificationHandler::ErrorComplete( TInt aErrCode ) |
|
276 { |
|
277 if( iIsReadyToSend && iMessage.IsNull() == EFalse ) |
|
278 { |
|
279 iMessage.Complete( aErrCode ); |
|
280 iIsReadyToSend = EFalse; |
|
281 } |
|
282 } |
|
283 |
|
284 // ---------------------------------------------------------------------------- |
|
285 // |
|
286 // ---------------------------------------------------------------------------- |
|
287 // |
|
288 TBool CCPNotificationHandler::SendChangeInfoListL( |
|
289 const CLiwDefaultList* aListOfMaps ) |
|
290 { |
|
291 CP_DEBUG( _L8("CCPNotificationHandler::SendChangeInfoListL()") ); |
|
292 iChangeInfoList->Reset( ); |
|
293 TBool sent(EFalse); |
|
294 //for every observer in session |
|
295 THashMapIter<TInt32, CCPLiwMap*> iter( iFilters ); |
|
296 const CCPLiwMap*const* filter = iter.NextValue( ); |
|
297 while( filter ) |
|
298 { |
|
299 |
|
300 CLiwDefaultList* listOfMatchingMaps = CLiwDefaultList::NewLC( ); |
|
301 |
|
302 //for every item in the input list |
|
303 for ( TInt j = 0; j < aListOfMaps->Count( ); j++ ) |
|
304 { |
|
305 CLiwDefaultMap* map = CLiwDefaultMap::NewLC( ); |
|
306 TLiwVariant variant; |
|
307 variant.PushL( ); |
|
308 aListOfMaps->AtL( j, variant ); |
|
309 variant.Get( *map ); |
|
310 if ( IsProperForFilterL( *map, **filter ) ) |
|
311 { |
|
312 listOfMatchingMaps->AppendL( TLiwVariant( map ) ); |
|
313 } |
|
314 CleanupStack::PopAndDestroy( &variant ); |
|
315 CleanupStack::PopAndDestroy( map ); |
|
316 } |
|
317 if ( listOfMatchingMaps->Count( ) ) |
|
318 { |
|
319 //append transaction id |
|
320 listOfMatchingMaps->AppendL( TLiwVariant( *iter.CurrentKey() )); |
|
321 iChangeInfoList->AppendL( TLiwGenericParam( KChangeInfo, |
|
322 TLiwVariant( listOfMatchingMaps ) ) ); |
|
323 } |
|
324 CleanupStack::PopAndDestroy( listOfMatchingMaps ); |
|
325 filter = iter.NextValue( ); |
|
326 } |
|
327 |
|
328 if( iChangeInfoList->Count( ) ) |
|
329 { |
|
330 TPckgBuf<TInt> sizeDes( iChangeInfoList->Size( ) ); |
|
331 TInt err = iMessage.Write( KReturnPosition, sizeDes ); |
|
332 iMessage.Complete( err ); |
|
333 sent = ETrue; |
|
334 } |
|
335 return sent; |
|
336 } |
|
337 |
|
338 // ---------------------------------------------------------------------------- |
|
339 // |
|
340 // ---------------------------------------------------------------------------- |
|
341 // |
|
342 void CCPNotificationHandler::GetPropertyL( const CLiwDefaultMap& aMap, |
|
343 const TDesC8& aProperty, RBuf& aResult ) |
|
344 { |
|
345 TLiwVariant value; |
|
346 value.PushL( ); |
|
347 if ( aMap.FindL( aProperty, value ) ) |
|
348 { |
|
349 TPtrC result( KNullDesC ); |
|
350 value.Get( result ); |
|
351 aResult.CreateL( result ); |
|
352 } |
|
353 CleanupStack::PopAndDestroy( &value ); |
|
354 } |
|
355 |
|
356 // ---------------------------------------------------------------------------- |
|
357 // |
|
358 // ---------------------------------------------------------------------------- |
|
359 // |
|
360 TBool CCPNotificationHandler::CheckIdL( const CLiwDefaultMap& aMap, |
|
361 const CCPLiwMap& aFilter ) |
|
362 { |
|
363 TBool result = EFalse; |
|
364 TInt32 id( 0); |
|
365 if ( aFilter.GetProperty( KId, id ) ) |
|
366 { |
|
367 TLiwVariant value; |
|
368 value.PushL( ); |
|
369 if ( aMap.FindL( KId, value ) ) |
|
370 { |
|
371 TUint idToCompare( 0); |
|
372 if ( value.Get( idToCompare ) ) |
|
373 { |
|
374 if ( id == idToCompare ) |
|
375 { |
|
376 result = ETrue; |
|
377 } |
|
378 } |
|
379 } |
|
380 CleanupStack::PopAndDestroy( &value ); |
|
381 } |
|
382 return result; |
|
383 } |
|
384 |
|
385 // ---------------------------------------------------------------------------- |
|
386 // |
|
387 // ---------------------------------------------------------------------------- |
|
388 // |
|
389 TBool CCPNotificationHandler::CheckPropertiesL( const CLiwDefaultMap& aMap, |
|
390 const CCPLiwMap& aFilter ) |
|
391 { |
|
392 TBool result( EFalse ); |
|
393 TBool publisherName( ETrue ); |
|
394 TBool contentType( ETrue ); |
|
395 TBool contentId( ETrue ); |
|
396 RBuf publisher; |
|
397 publisher.CleanupClosePushL(); |
|
398 |
|
399 if ( aFilter.GetPropertyL( KPublisherId, publisher ) ) |
|
400 { |
|
401 if ( publisher != KAll ) |
|
402 { |
|
403 RBuf str2compare; |
|
404 str2compare.CleanupClosePushL(); |
|
405 GetPropertyL( aMap, KPublisherId, str2compare ); |
|
406 if ( publisher!=str2compare ) |
|
407 { |
|
408 publisherName = EFalse; |
|
409 } |
|
410 CleanupStack::PopAndDestroy( &str2compare ); |
|
411 } |
|
412 } |
|
413 RBuf content; |
|
414 content.CleanupClosePushL(); |
|
415 if ( aFilter.GetPropertyL( KContentType, content ) ) |
|
416 { |
|
417 if ( content != KAll ) |
|
418 { |
|
419 RBuf str2compare; |
|
420 str2compare.CleanupClosePushL(); |
|
421 GetPropertyL( aMap, KContentType, str2compare ); |
|
422 if ( content!=str2compare ) |
|
423 { |
|
424 contentType = EFalse; |
|
425 } |
|
426 CleanupStack::PopAndDestroy( &str2compare ); |
|
427 } |
|
428 } |
|
429 RBuf content_id; |
|
430 content_id.CleanupClosePushL(); |
|
431 if ( (publisher != KAll)||(content != KAll) ) |
|
432 { |
|
433 if ( aFilter.GetPropertyL( KContentId, content_id ) ) |
|
434 { |
|
435 if ( content_id != KAll ) |
|
436 { |
|
437 RBuf str2compare; |
|
438 str2compare.CleanupClosePushL(); |
|
439 GetPropertyL( aMap, KContentId, str2compare ); |
|
440 if ( content_id!=str2compare ) |
|
441 { |
|
442 contentId = EFalse; |
|
443 } |
|
444 CleanupStack::PopAndDestroy( &str2compare ); |
|
445 } |
|
446 } |
|
447 } |
|
448 if ( publisherName && contentType && contentId ) |
|
449 { |
|
450 result = ETrue; |
|
451 } |
|
452 CleanupStack::PopAndDestroy( &content_id ); |
|
453 CleanupStack::PopAndDestroy( &content ); |
|
454 CleanupStack::PopAndDestroy( &publisher ); |
|
455 return result; |
|
456 } |
|
457 |
|
458 // ---------------------------------------------------------------------------- |
|
459 // |
|
460 // ---------------------------------------------------------------------------- |
|
461 // |
|
462 TBool CCPNotificationHandler::CheckOperationTypeL( const CLiwDefaultMap& aMap, |
|
463 const CCPLiwMap& aFilter ) |
|
464 { |
|
465 TBool result = ETrue; |
|
466 RBuf operation; |
|
467 operation.CleanupClosePushL(); |
|
468 if ( aFilter.GetPropertyL( KOperation, operation ) ) |
|
469 { |
|
470 result = EFalse; |
|
471 TLiwVariant value; |
|
472 value.PushL( ); |
|
473 if ( aMap.FindL( KOperation, value ) ) |
|
474 { |
|
475 TPtrC operationToCompare( KNullDesC ); |
|
476 if ( value.Get( operationToCompare ) ) |
|
477 { |
|
478 if ( operation.Find( operationToCompare ) != KErrNotFound ) |
|
479 { |
|
480 result = ETrue; |
|
481 } |
|
482 } |
|
483 } |
|
484 CleanupStack::PopAndDestroy( &value ); |
|
485 } |
|
486 CleanupStack::PopAndDestroy( &operation ); |
|
487 return result; |
|
488 } |
|
489 |
|
490 // ---------------------------------------------------------------------------- |
|
491 // |
|
492 // ---------------------------------------------------------------------------- |
|
493 // |
|
494 TBool CCPNotificationHandler::CheckRegistryTypeL( const CLiwDefaultMap& aMap, |
|
495 const CCPLiwMap& aFilter ) |
|
496 { |
|
497 TBool result = ETrue; |
|
498 RBuf type; |
|
499 type.CleanupClosePushL(); |
|
500 if ( aFilter.GetPropertyL( KType, type ) ) |
|
501 { |
|
502 result = EFalse; |
|
503 TLiwVariant value; |
|
504 value.PushL( ); |
|
505 if ( aMap.FindL( KType, value ) ) |
|
506 { |
|
507 TPtrC typeToCompare( KNullDesC ); |
|
508 if ( value.Get( typeToCompare ) ) |
|
509 { |
|
510 if ( type.Find( typeToCompare ) != KErrNotFound ) |
|
511 { |
|
512 result = ETrue; |
|
513 } |
|
514 } |
|
515 } |
|
516 CleanupStack::PopAndDestroy( &value ); |
|
517 } |
|
518 CleanupStack::PopAndDestroy( &type ); |
|
519 return result; |
|
520 } |