|
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: IM Service Connection subscription item base implementation. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "ximpimsubscriptionitembase.h" |
|
20 #include "ximpimitemparent.h" |
|
21 #include "ximppanics.h" |
|
22 //#include "ximppsccontext.h" |
|
23 //#include "ximpdatasubscriptionstateimp.h" |
|
24 //#include "ximpobjecthelpers.h" |
|
25 |
|
26 //#include "ximptrace.h" |
|
27 |
|
28 const TInt KXIMPContentAutoExpiryTime = 15; // Seconds |
|
29 |
|
30 |
|
31 // --------------------------------------------------------------------------- |
|
32 // CXIMPIMSubscriptionItemBase::OrderOfContexts() |
|
33 // --------------------------------------------------------------------------- |
|
34 // |
|
35 TInt CXIMPIMSubscriptionItemBase::OrderOfContexts( const MXIMPPscContext& aFirst, |
|
36 const MXIMPPscContext& aSecond ) |
|
37 { |
|
38 if( &aFirst < &aSecond ) |
|
39 { |
|
40 return -1; |
|
41 } |
|
42 else if( &aFirst == &aSecond ) |
|
43 { |
|
44 return 0; |
|
45 } |
|
46 else |
|
47 { |
|
48 return 1; |
|
49 } |
|
50 } |
|
51 |
|
52 // --------------------------------------------------------------------------- |
|
53 // CXIMPIMSubscriptionItemBase::OrderOfItems() |
|
54 // --------------------------------------------------------------------------- |
|
55 // |
|
56 TInt CXIMPIMSubscriptionItemBase::OrderOfItems( const CXIMPIMSubscriptionItemBase& aFirst, |
|
57 const CXIMPIMSubscriptionItemBase& aSecond ) |
|
58 { |
|
59 if( &aFirst < &aSecond ) |
|
60 { |
|
61 return -1; |
|
62 } |
|
63 else if( &aFirst == &aSecond ) |
|
64 { |
|
65 return 0; |
|
66 } |
|
67 else |
|
68 { |
|
69 return 1; |
|
70 } |
|
71 } |
|
72 |
|
73 |
|
74 // --------------------------------------------------------------------------- |
|
75 // CXIMPIMSubscriptionItemBase::CXIMPIMSubscriptionItemBase() |
|
76 // --------------------------------------------------------------------------- |
|
77 // |
|
78 EXPORT_C CXIMPIMSubscriptionItemBase::CXIMPIMSubscriptionItemBase( MXIMPIMItemParent& aParent ) |
|
79 : iParent( aParent ) |
|
80 { |
|
81 } |
|
82 |
|
83 // --------------------------------------------------------------------------- |
|
84 // CXIMPIMSubscriptionItemBase::BaseConstructL() |
|
85 // --------------------------------------------------------------------------- |
|
86 // |
|
87 EXPORT_C void CXIMPIMSubscriptionItemBase::BaseConstructL() |
|
88 { |
|
89 iSubscriptionState = CXIMPDataSubscriptionStateImp::NewL(); |
|
90 } |
|
91 |
|
92 // --------------------------------------------------------------------------- |
|
93 // CXIMPIMSubscriptionItemBase::~CXIMPIMSubscriptionItemBase() |
|
94 // --------------------------------------------------------------------------- |
|
95 // |
|
96 EXPORT_C CXIMPIMSubscriptionItemBase::~CXIMPIMSubscriptionItemBase() |
|
97 { |
|
98 __ASSERT_ALWAYS( !CountOfSubscribers(), |
|
99 User::Panic( NXIMPPrivPanic::KCategory, NXIMPPrivPanic::EArrayCountNotValid ) ); |
|
100 iContexts.Close(); |
|
101 iPreContexts.Close(); |
|
102 iItems.Close(); |
|
103 iPreItems.Close(); |
|
104 |
|
105 delete iSubscriptionState; |
|
106 } |
|
107 |
|
108 // --------------------------------------------------------------------------- |
|
109 // CXIMPIMSubscriptionItemBase::SubscriptionStatus() |
|
110 // --------------------------------------------------------------------------- |
|
111 // |
|
112 EXPORT_C CXIMPIMSubscriptionItemBase::TSubscriptionStatus |
|
113 CXIMPIMSubscriptionItemBase::SubscriptionStatus( MXIMPPscContext* aContext ) |
|
114 { |
|
115 // Calculate situation |
|
116 TInt count = iContexts.Count() + iItems.Count(); |
|
117 |
|
118 if( count == 0 ) |
|
119 { |
|
120 return ENotSubscribedAtAll; |
|
121 } |
|
122 if( IsContext( aContext ) ) |
|
123 { |
|
124 if( count == 1 ) |
|
125 { |
|
126 return ESubscribedForCtxOnly; |
|
127 } |
|
128 return ESubscribedForCtxAndOthers; |
|
129 } |
|
130 return ESubscribedForOtherCtxOnly; |
|
131 } |
|
132 |
|
133 // --------------------------------------------------------------------------- |
|
134 // CXIMPIMSubscriptionItemBase::AddSubscriberL() |
|
135 // --------------------------------------------------------------------------- |
|
136 // |
|
137 EXPORT_C void CXIMPIMSubscriptionItemBase::AddSubscriberL( MXIMPPscContext* aContext ) |
|
138 { |
|
139 TLinearOrder< MXIMPPscContext > order( OrderOfContexts ); |
|
140 if( IsContext( aContext ) ) |
|
141 { |
|
142 User::Leave( KErrAlreadyExists ); |
|
143 } |
|
144 TInt index = iPreContexts.FindInOrder( aContext, order ); |
|
145 if( index != KErrNotFound ) |
|
146 { |
|
147 User::Leave( KErrAlreadyExists ); |
|
148 } |
|
149 |
|
150 iPreContexts.InsertInOrderL( aContext, order ); |
|
151 User::LeaveIfError( Open() ); |
|
152 ChangeStatusAfterAdd(); |
|
153 } |
|
154 |
|
155 // --------------------------------------------------------------------------- |
|
156 // CXIMPIMSubscriptionItemBase::RemoveSubscriber() |
|
157 // --------------------------------------------------------------------------- |
|
158 // |
|
159 EXPORT_C void CXIMPIMSubscriptionItemBase::RemoveSubscriber( MXIMPPscContext* aContext ) |
|
160 { |
|
161 TLinearOrder< MXIMPPscContext > order( OrderOfContexts ); |
|
162 TInt index = iContexts.FindInOrder( aContext, order ); |
|
163 TInt preIndex = iPreContexts.FindInOrder( aContext, order ); |
|
164 if( index >= 0 ) |
|
165 { |
|
166 iContexts.Remove( index ); |
|
167 ChangeStatusAfterRemove(); |
|
168 Close(); |
|
169 } |
|
170 else if( preIndex >= 0 ) |
|
171 { |
|
172 iPreContexts.Remove( preIndex ); |
|
173 ChangeStatusAfterRemove(); |
|
174 Close(); |
|
175 } |
|
176 else |
|
177 { |
|
178 iParent.UnregisterExpiringItem( this ); |
|
179 } |
|
180 } |
|
181 |
|
182 // --------------------------------------------------------------------------- |
|
183 // CXIMPIMSubscriptionItemBase::AddSubscriberL() |
|
184 // --------------------------------------------------------------------------- |
|
185 // |
|
186 EXPORT_C void CXIMPIMSubscriptionItemBase::AddSubscriberL( CXIMPIMSubscriptionItemBase* aItem ) |
|
187 { |
|
188 TLinearOrder< CXIMPIMSubscriptionItemBase > order( OrderOfItems ); |
|
189 TInt index = iItems.FindInOrder( aItem, order ); |
|
190 if( index != KErrNotFound ) |
|
191 { |
|
192 return; |
|
193 } |
|
194 index = iPreItems.FindInOrder( aItem, order ); |
|
195 if( index != KErrNotFound ) |
|
196 { |
|
197 return; |
|
198 } |
|
199 |
|
200 iPreItems.InsertInOrderL( aItem, order ); |
|
201 User::LeaveIfError( Open() ); |
|
202 ChangeStatusAfterAdd(); |
|
203 } |
|
204 |
|
205 // --------------------------------------------------------------------------- |
|
206 // CXIMPIMSubscriptionItemBase::RemoveSubscriber() |
|
207 // --------------------------------------------------------------------------- |
|
208 // |
|
209 EXPORT_C void CXIMPIMSubscriptionItemBase::RemoveSubscriber( CXIMPIMSubscriptionItemBase* aItem ) |
|
210 { |
|
211 TLinearOrder< CXIMPIMSubscriptionItemBase > order( OrderOfItems ); |
|
212 TInt index = iItems.FindInOrder( aItem, order ); |
|
213 TInt preIndex = iPreItems.FindInOrder( aItem, order ); |
|
214 if( index >= 0 ) |
|
215 { |
|
216 iItems.Remove( index ); |
|
217 ChangeStatusAfterRemove(); |
|
218 Close(); |
|
219 } |
|
220 else if( preIndex >= 0 ) |
|
221 { |
|
222 iPreItems.Remove( index ); |
|
223 ChangeStatusAfterRemove(); |
|
224 Close(); |
|
225 } |
|
226 else |
|
227 { |
|
228 iParent.UnregisterExpiringItem( this ); |
|
229 } |
|
230 } |
|
231 |
|
232 // --------------------------------------------------------------------------- |
|
233 // CXIMPIMSubscriptionItemBase::ForceClose() |
|
234 // --------------------------------------------------------------------------- |
|
235 // |
|
236 EXPORT_C void CXIMPIMSubscriptionItemBase::ForceClose() |
|
237 { |
|
238 TInt count = iContexts.Count(); |
|
239 for( TInt a = 0; a < count; ++a ) |
|
240 { |
|
241 // RemoveSubscriber removes first one every time. |
|
242 RemoveSubscriber( iContexts[ 0 ] ); |
|
243 } |
|
244 count = iItems.Count(); |
|
245 for( TInt a = 0; a < count; ++a ) |
|
246 { |
|
247 RemoveSubscriber( iItems[ 0 ] ); |
|
248 } |
|
249 } |
|
250 |
|
251 // --------------------------------------------------------------------------- |
|
252 // CXIMPIMSubscriptionItemBase::CleanIfExpired() |
|
253 // --------------------------------------------------------------------------- |
|
254 // |
|
255 EXPORT_C TBool CXIMPIMSubscriptionItemBase::CleanIfExpired() |
|
256 { |
|
257 TTime timeNow; |
|
258 timeNow.UniversalTime(); |
|
259 TBool expired = |
|
260 ( iExpireTime + TTimeIntervalSeconds( KXIMPContentAutoExpiryTime ) ) < timeNow; |
|
261 if( expired ) |
|
262 { |
|
263 CleanExpired(); |
|
264 } |
|
265 return expired; |
|
266 } |
|
267 |
|
268 // --------------------------------------------------------------------------- |
|
269 // CXIMPIMSubscriptionItemBase::UpdateSubscriptionStateL() |
|
270 // --------------------------------------------------------------------------- |
|
271 // |
|
272 EXPORT_C void CXIMPIMSubscriptionItemBase::UpdateSubscriptionStateL( TSubscriptionEvent aUpdateState ) |
|
273 { |
|
274 switch( aUpdateState ) |
|
275 { |
|
276 case ECompleteDataReceived: |
|
277 { |
|
278 if( CountOfSubscribers() == 0 ) |
|
279 { |
|
280 UpdateExpirationL(); |
|
281 } |
|
282 break; |
|
283 } |
|
284 default: |
|
285 { |
|
286 User::Panic( NXIMPPrivPanic::KCategory, NXIMPPrivPanic::ESubscriptionItemLogicError ); |
|
287 break; |
|
288 } |
|
289 } |
|
290 } |
|
291 |
|
292 |
|
293 // --------------------------------------------------------------------------- |
|
294 // CXIMPIMSubscriptionItemBase::DataSubscriptionState() |
|
295 // --------------------------------------------------------------------------- |
|
296 // |
|
297 EXPORT_C const MXIMPDataSubscriptionState& CXIMPIMSubscriptionItemBase::DataSubscriptionState() |
|
298 { |
|
299 return *iSubscriptionState; |
|
300 } |
|
301 |
|
302 // --------------------------------------------------------------------------- |
|
303 // CXIMPIMSubscriptionItemBase::ContextCount() |
|
304 // --------------------------------------------------------------------------- |
|
305 // |
|
306 EXPORT_C TInt CXIMPIMSubscriptionItemBase::ContextCount() const |
|
307 { |
|
308 return iContexts.Count(); |
|
309 } |
|
310 |
|
311 // --------------------------------------------------------------------------- |
|
312 // CXIMPIMSubscriptionItemBase::Context() |
|
313 // --------------------------------------------------------------------------- |
|
314 // |
|
315 EXPORT_C MXIMPPscContext& CXIMPIMSubscriptionItemBase::Context( TInt aIndex ) |
|
316 { |
|
317 return *iContexts[ aIndex ]; |
|
318 } |
|
319 |
|
320 // --------------------------------------------------------------------------- |
|
321 // CXIMPIMSubscriptionItemBase::Context() |
|
322 // --------------------------------------------------------------------------- |
|
323 // |
|
324 EXPORT_C TBool CXIMPIMSubscriptionItemBase::IsContext( MXIMPPscContext* aContext ) const |
|
325 { |
|
326 TLinearOrder< MXIMPPscContext > order( OrderOfContexts ); |
|
327 return iContexts.FindInOrder( aContext, order ) != KErrNotFound; |
|
328 } |
|
329 |
|
330 // --------------------------------------------------------------------------- |
|
331 // CXIMPIMSubscriptionItemBase::SynthesiseSubscriptionEventToAllCtxsL() |
|
332 // --------------------------------------------------------------------------- |
|
333 // |
|
334 EXPORT_C void CXIMPIMSubscriptionItemBase::SynthesiseSubscriptionEventToAllCtxsL() |
|
335 { |
|
336 //TRACE( _L("CXIMPIMSubscriptionItemBase::SynthesiseSubscriptionEventToAllCtxsL()" ) ); |
|
337 RPointerArray< MXIMPPscContext > contextArray; |
|
338 CleanupClosePushL( contextArray ); |
|
339 TLinearOrder< MXIMPPscContext > order( OrderOfContexts ); |
|
340 |
|
341 TInt contextCount = iContexts.Count(); |
|
342 for( TInt a = 0; a < contextCount; ++a ) |
|
343 { |
|
344 contextArray.InsertInOrderL( iContexts[ a ], order ); |
|
345 } |
|
346 |
|
347 TInt itemCount = iItems.Count(); |
|
348 for( TInt a = 0; a < itemCount; ++a ) |
|
349 { |
|
350 TInt contextCount = iItems[ a ]->ContextCount(); |
|
351 for( TInt b = 0; b < contextCount; ++b ) |
|
352 { |
|
353 TInt error = contextArray.InsertInOrder( &iItems[ a ]->Context( b ), order ); |
|
354 if( error != KErrNotFound ) |
|
355 { |
|
356 User::LeaveIfError( error ); |
|
357 } |
|
358 } |
|
359 } |
|
360 |
|
361 TInt count = contextArray.Count(); |
|
362 for( TInt a = 0; a < count; ++a ) |
|
363 { |
|
364 SynthesiseSubscriptionEventToL( contextArray[ a ], ETrue ); |
|
365 } |
|
366 CleanupStack::PopAndDestroy(); // contextArray |
|
367 } |
|
368 |
|
369 // --------------------------------------------------------------------------- |
|
370 // CXIMPIMSubscriptionItemBase::ActivateSubscriberL() |
|
371 // --------------------------------------------------------------------------- |
|
372 // |
|
373 EXPORT_C void CXIMPIMSubscriptionItemBase::ActivateSubscriberL( MXIMPPscContext* aContext ) |
|
374 { |
|
375 TLinearOrder< MXIMPPscContext > order( OrderOfContexts ); |
|
376 TInt index = iPreContexts.FindInOrder( aContext, order ); |
|
377 if( index != KErrNotFound && index < iPreContexts.Count()) |
|
378 { |
|
379 iContexts.InsertInOrderL( iPreContexts[ index ], order ); |
|
380 iPreContexts.Remove( index ); |
|
381 } |
|
382 } |
|
383 |
|
384 // --------------------------------------------------------------------------- |
|
385 // CXIMPIMSubscriptionItemBase::ActivateSubscriberL() |
|
386 // --------------------------------------------------------------------------- |
|
387 // |
|
388 EXPORT_C void CXIMPIMSubscriptionItemBase::ActivateSubscriberL( CXIMPIMSubscriptionItemBase* aItem ) |
|
389 { |
|
390 TLinearOrder< CXIMPIMSubscriptionItemBase > order( OrderOfItems ); |
|
391 TInt index = iPreItems.FindInOrder( aItem, order ); |
|
392 if( index != KErrNotFound && index < iPreItems.Count()) |
|
393 { |
|
394 iItems.InsertInOrderL( iPreItems[ index ], order ); |
|
395 iPreItems.Remove( index ); |
|
396 } |
|
397 } |
|
398 |
|
399 // --------------------------------------------------------------------------- |
|
400 // CXIMPIMSubscriptionItemBase::Status() |
|
401 // --------------------------------------------------------------------------- |
|
402 // |
|
403 EXPORT_C void CXIMPIMSubscriptionItemBase::SetDataSubscriptionStateL( CXIMPDataSubscriptionStateImp& aNewState ) |
|
404 { |
|
405 // Some handle logic for terminating subscribers, data expiration etc. missing. |
|
406 delete iSubscriptionState; |
|
407 iSubscriptionState = &aNewState; |
|
408 } |
|
409 |
|
410 // --------------------------------------------------------------------------- |
|
411 // CXIMPIMSubscriptionItemBase::Status() |
|
412 // --------------------------------------------------------------------------- |
|
413 // |
|
414 EXPORT_C CXIMPDataSubscriptionStateImp* CXIMPIMSubscriptionItemBase::StatusLC( MXIMPPscContext* /*aContext*/ ) const |
|
415 { |
|
416 // not implemented as of now |
|
417 } |
|
418 |
|
419 // --------------------------------------------------------------------------- |
|
420 // CXIMPIMSubscriptionItemBase::UpdateExpirationL() |
|
421 // --------------------------------------------------------------------------- |
|
422 // |
|
423 EXPORT_C void CXIMPIMSubscriptionItemBase::UpdateExpirationL() |
|
424 { |
|
425 if( CountOfSubscribers() == 0 ) |
|
426 { |
|
427 iParent.RegisterExpiringItemL( this ); |
|
428 } |
|
429 iExpireTime.UniversalTime(); |
|
430 } |
|
431 |
|
432 |
|
433 // --------------------------------------------------------------------------- |
|
434 // CXIMPIMSubscriptionItemBase::ChangeStatusAfterRemove() |
|
435 // --------------------------------------------------------------------------- |
|
436 // |
|
437 EXPORT_C void CXIMPIMSubscriptionItemBase::ChangeStatusAfterRemove() |
|
438 { |
|
439 // not implemented as of now |
|
440 } |
|
441 |
|
442 |
|
443 // --------------------------------------------------------------------------- |
|
444 // CXIMPIMSubscriptionItemBase::ChangeStatusAfterAdd() |
|
445 // --------------------------------------------------------------------------- |
|
446 // |
|
447 EXPORT_C void CXIMPIMSubscriptionItemBase::ChangeStatusAfterAdd() |
|
448 { |
|
449 // not implemented as of now |
|
450 } |
|
451 |
|
452 // --------------------------------------------------------------------------- |
|
453 // CXIMPIMSubscriptionItemBase::CountOfSubscribers() |
|
454 // --------------------------------------------------------------------------- |
|
455 // |
|
456 EXPORT_C TInt CXIMPIMSubscriptionItemBase::CountOfSubscribers() |
|
457 { |
|
458 return iItems.Count() + iPreItems.Count() + iContexts.Count() + iPreContexts.Count(); |
|
459 } |
|
460 // End of file |