|
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: The cache stores a single array of this type. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <s32strm.h> |
|
20 #include <mpresencebuddyinfo2.h> |
|
21 |
|
22 #include "presencecacheservicestore.h" |
|
23 #include "presencecachebuddystore.h" |
|
24 #include "presencetrace.h" |
|
25 #include "presenceobjecthelpers.h" |
|
26 |
|
27 // --------------------------------------------------------------------------- |
|
28 // CPresenceCacheServiceStore::NewLC() |
|
29 // --------------------------------------------------------------------------- |
|
30 // |
|
31 EXPORT_C CPresenceCacheServiceStore* CPresenceCacheServiceStore::NewLC() |
|
32 { |
|
33 CPresenceCacheServiceStore* self = new( ELeave ) CPresenceCacheServiceStore; |
|
34 CleanupStack::PushL( self ); |
|
35 self->ConstructL(); |
|
36 return self; |
|
37 } |
|
38 |
|
39 // --------------------------------------------------------------------------- |
|
40 // CPresenceCacheServiceStore::NewL() |
|
41 // --------------------------------------------------------------------------- |
|
42 // |
|
43 EXPORT_C CPresenceCacheServiceStore* CPresenceCacheServiceStore::NewL() |
|
44 { |
|
45 CPresenceCacheServiceStore* self = NewLC(); |
|
46 CleanupStack::Pop( self ); |
|
47 return self; |
|
48 } |
|
49 |
|
50 // --------------------------------------------------------------------------- |
|
51 // CPresenceCacheServiceStore::~CPresenceCacheServiceStore() |
|
52 // --------------------------------------------------------------------------- |
|
53 // |
|
54 CPresenceCacheServiceStore::~CPresenceCacheServiceStore() |
|
55 { |
|
56 TInt count = iBuddyStoreCollection.Count(); |
|
57 for (TInt i=0;i<count;i++) |
|
58 { |
|
59 delete iBuddyStoreCollection[i]; |
|
60 } |
|
61 iBuddyStoreCollection.Close(); |
|
62 delete iServiceName; |
|
63 iServiceName = NULL; |
|
64 } |
|
65 |
|
66 // --------------------------------------------------------------------------- |
|
67 // CPresenceCacheServiceStore::CPresenceCacheServiceStore() |
|
68 // --------------------------------------------------------------------------- |
|
69 // |
|
70 CPresenceCacheServiceStore::CPresenceCacheServiceStore(): iServiceName(NULL) |
|
71 { |
|
72 } |
|
73 |
|
74 // --------------------------------------------------------------------------- |
|
75 // CPresenceCacheServiceStore::ConstructL() |
|
76 // --------------------------------------------------------------------------- |
|
77 // |
|
78 void CPresenceCacheServiceStore::ConstructL() |
|
79 { |
|
80 } |
|
81 |
|
82 // --------------------------------------------------------------------------- |
|
83 // CPresenceCacheServiceStore::SetServiceNameL() |
|
84 // --------------------------------------------------------------------------- |
|
85 // |
|
86 void CPresenceCacheServiceStore::SetServiceNameL(TDesC& aServiceName) |
|
87 { |
|
88 delete iServiceName; |
|
89 iServiceName = NULL; |
|
90 iServiceName = aServiceName.AllocL(); |
|
91 } |
|
92 |
|
93 // --------------------------------------------------------------------------- |
|
94 // CPresenceCacheServiceStore::GetObjectCollection() |
|
95 // --------------------------------------------------------------------------- |
|
96 // |
|
97 RPointerArray<CPresenceCacheBuddyStore>& CPresenceCacheServiceStore::GetObjectCollection() |
|
98 { |
|
99 return iBuddyStoreCollection; |
|
100 } |
|
101 |
|
102 // --------------------------------------------------------------------------- |
|
103 // CPresenceCacheServiceStore::ServiceName() |
|
104 // --------------------------------------------------------------------------- |
|
105 // |
|
106 TPtrC CPresenceCacheServiceStore::ServiceName() const |
|
107 { |
|
108 return *iServiceName; |
|
109 } |
|
110 |
|
111 // --------------------------------------------------------------------------- |
|
112 // CPresenceCacheServiceStore::Count() |
|
113 // --------------------------------------------------------------------------- |
|
114 // |
|
115 TInt CPresenceCacheServiceStore::Count() const |
|
116 { |
|
117 return iBuddyStoreCollection.Count(); |
|
118 } |
|
119 |
|
120 // --------------------------------------------------------------------------- |
|
121 // CPresenceCacheServiceStore::PresenceCount() |
|
122 // --------------------------------------------------------------------------- |
|
123 // |
|
124 TInt CPresenceCacheServiceStore::PresenceCount() const |
|
125 { |
|
126 return iPresenceCount; |
|
127 } |
|
128 |
|
129 // --------------------------------------------------------------------------- |
|
130 // CPresenceCacheServiceStore::AddBlind() |
|
131 // --------------------------------------------------------------------------- |
|
132 // |
|
133 TInt CPresenceCacheServiceStore::AddBlind(CPresenceCacheBuddyStore* aBuddyStore) |
|
134 { |
|
135 TRACE( _L("CPresenceCacheServiceStore::AddBlind - begin") ); |
|
136 // TRACE_1( _L("______identity: %S"), &aBuddyStore->BuddyId() ); |
|
137 // TRACE_1( _L("______iService: %S"), &iServiceName->Des()); |
|
138 if (!aBuddyStore) |
|
139 return KErrArgument; |
|
140 |
|
141 iBuddyStoreCollection.Append(aBuddyStore); |
|
142 |
|
143 // now update the presence count |
|
144 if (aBuddyStore->HasPresence()) |
|
145 { |
|
146 iPresenceCount++; |
|
147 } |
|
148 |
|
149 TRACE( _L("CPresenceCacheServiceStore::AddBlind - return")); |
|
150 return KErrNone; |
|
151 } |
|
152 |
|
153 |
|
154 // --------------------------------------------------------------------------- |
|
155 // CPresenceCacheServiceStore::AddOrReplacePresenceL() |
|
156 // --------------------------------------------------------------------------- |
|
157 // |
|
158 CPresenceCacheBuddyStore* CPresenceCacheServiceStore::AddOrReplacePresenceL |
|
159 (MPresenceBuddyInfo2* aBuddyPresInfo) |
|
160 { |
|
161 TRACE( _L("CPresenceCacheServiceStore::AddOrReplacePresence - begin") ); |
|
162 // TRACE_1( _L("______identity: %S"), &(aBuddyPresInfo->BuddyId() )); |
|
163 // TRACE_1( _L("______iService: %d"), &(iServiceName->Des())); |
|
164 |
|
165 CPresenceCacheBuddyStore* newBuddyStore(NULL); |
|
166 if(!aBuddyPresInfo) |
|
167 return newBuddyStore; |
|
168 |
|
169 TBool oldPresExists(EFalse); |
|
170 |
|
171 TInt count = iBuddyStoreCollection.Count(); |
|
172 for (TInt i=0;i<count;i++) |
|
173 { |
|
174 if( (iBuddyStoreCollection[i])->EqualsIdentity(aBuddyPresInfo) ) |
|
175 { |
|
176 oldPresExists = (iBuddyStoreCollection[i])->HasPresence(); |
|
177 iBuddyStoreCollection[i]->SetPresenceBuddyInfo(aBuddyPresInfo); |
|
178 newBuddyStore = iBuddyStoreCollection[i]; |
|
179 TRACE( _L("_________Match found") ); |
|
180 break; |
|
181 } |
|
182 } |
|
183 |
|
184 //if not edited, we need to add a new buddy store |
|
185 if (!newBuddyStore) |
|
186 { |
|
187 newBuddyStore = CPresenceCacheBuddyStore::NewLC(this, aBuddyPresInfo->BuddyId()); |
|
188 newBuddyStore->SetPresenceBuddyInfo(aBuddyPresInfo); |
|
189 iBuddyStoreCollection.Append(newBuddyStore); |
|
190 CleanupStack::Pop(newBuddyStore); //newBuddyStore |
|
191 } |
|
192 |
|
193 // new presence is certainly added, check whether it was existed |
|
194 if (!oldPresExists) |
|
195 { |
|
196 iPresenceCount++; |
|
197 } |
|
198 |
|
199 TRACE( _L("CPresenceCacheServiceStore::AddOrReplacePresence - return")); |
|
200 return newBuddyStore; |
|
201 } |
|
202 |
|
203 |
|
204 // --------------------------------------------------------------------------- |
|
205 // CPresenceCacheServiceStore::RemoveBlind() |
|
206 // --------------------------------------------------------------------------- |
|
207 // |
|
208 void CPresenceCacheServiceStore::RemoveBlind(CPresenceCacheBuddyStore* aPresenceBuddyInfo) |
|
209 { |
|
210 TInt index = iBuddyStoreCollection.Find(aPresenceBuddyInfo); |
|
211 if (index>-1) |
|
212 { |
|
213 delete iBuddyStoreCollection[index]; |
|
214 iBuddyStoreCollection.Remove(index); |
|
215 } |
|
216 } |
|
217 |
|
218 // --------------------------------------------------------------------------- |
|
219 // CPresenceCacheServiceStore::FindAndRemove() |
|
220 // --------------------------------------------------------------------------- |
|
221 // |
|
222 CPresenceCacheBuddyStore* CPresenceCacheServiceStore::FindAndRemove |
|
223 (const TDesC& aIdentity, TInt& aErr) |
|
224 { |
|
225 TRACE( _L("CPresenceCacheServiceStore::FindAndRemove - begin") ); |
|
226 // TRACE_1( _L("______iService: %S"), &(iServiceName->Des())); |
|
227 // TRACE_1( _L("______identity: %S"), &(aIdentity )); |
|
228 TBool oldPresExists(EFalse); |
|
229 CPresenceCacheBuddyStore* buddyStore(NULL); |
|
230 aErr = KErrNotFound; |
|
231 |
|
232 TInt count = iBuddyStoreCollection.Count(); |
|
233 for (TInt i=0;i<count;i++) |
|
234 { |
|
235 if ( (iBuddyStoreCollection[i])->EqualsIdentity(aIdentity) ) |
|
236 { |
|
237 oldPresExists = (iBuddyStoreCollection[i])->HasPresence(); |
|
238 |
|
239 //only delete presence if there are subscribers |
|
240 if ((iBuddyStoreCollection[i])->HasSubscribers()) |
|
241 { |
|
242 (iBuddyStoreCollection[i])->SetPresenceBuddyInfo(NULL); |
|
243 buddyStore = iBuddyStoreCollection[i]; |
|
244 } |
|
245 else //if there are no subscribers remove the whole package |
|
246 { |
|
247 delete iBuddyStoreCollection[i]; |
|
248 iBuddyStoreCollection.Remove(i); |
|
249 } |
|
250 break; |
|
251 } |
|
252 } |
|
253 |
|
254 // update presence info counts |
|
255 if(oldPresExists) |
|
256 { |
|
257 iPresenceCount--; |
|
258 aErr = KErrNone; |
|
259 } |
|
260 |
|
261 TRACE_1( _L("CPresenceCacheServiceStore::FindAndRemove - buddyStore=%d"),buddyStore ); |
|
262 return buddyStore; |
|
263 } |
|
264 |
|
265 // --------------------------------------------------------------------------- |
|
266 // CPresenceCacheServiceStore::RemoveExpiredBuddyInfos |
|
267 // --------------------------------------------------------------------------- |
|
268 // |
|
269 |
|
270 void CPresenceCacheServiceStore::RemoveExpiredBuddyInfos( |
|
271 RPointerArray<CPresenceCacheBuddyStore>& aBuddyStores ) |
|
272 { |
|
273 TInt count = iBuddyStoreCollection.Count(); |
|
274 for( TInt i = 0 ; i < count ; ++i ) |
|
275 { |
|
276 CPresenceCacheBuddyStore* buddyStore = iBuddyStoreCollection[i]; |
|
277 MPresenceBuddyInfo2* buddyInfo = buddyStore->PresenceBuddyInfo(); |
|
278 |
|
279 if( buddyInfo && |
|
280 PresenceCacheBuddyUtils::IsBuddyInfoExpired( *buddyInfo ) ) |
|
281 { |
|
282 // remove buddy info |
|
283 buddyStore->SetPresenceBuddyInfo( NULL ); |
|
284 --iPresenceCount; |
|
285 if( buddyStore->HasSubscribers() ) |
|
286 { |
|
287 aBuddyStores.Append( buddyStore ); // ignore error |
|
288 } |
|
289 else |
|
290 { |
|
291 delete buddyStore; |
|
292 iBuddyStoreCollection.Remove( i ); |
|
293 --i; |
|
294 --count; |
|
295 } |
|
296 } |
|
297 } |
|
298 } |
|
299 |
|
300 // --------------------------------------------------------------------------- |
|
301 // CPresenceCacheServiceStore::FindAndGet() |
|
302 // --------------------------------------------------------------------------- |
|
303 // |
|
304 CPresenceCacheBuddyStore* CPresenceCacheServiceStore::FindAndGet( |
|
305 const TDesC& aIdentity) const |
|
306 { |
|
307 TRACE( _L("CPresenceCacheServiceStore::FindAndGet - begin") ); |
|
308 // TRACE_1( _L("______iService: %S"), &(iServiceName->Des())); |
|
309 TRACE_1( _L("______identity: %S"), &(aIdentity )); |
|
310 CPresenceCacheBuddyStore* buddyStore(NULL); |
|
311 |
|
312 TInt count = iBuddyStoreCollection.Count(); |
|
313 for (TInt i=0;i<count;i++) |
|
314 { |
|
315 if ( (iBuddyStoreCollection[i])->EqualsIdentity(aIdentity)) |
|
316 { |
|
317 buddyStore = iBuddyStoreCollection[i]; |
|
318 break; |
|
319 } |
|
320 } |
|
321 TRACE_1( _L("CPresenceCacheServiceStore::FindAndGet - buddyStore=%d"),buddyStore ); |
|
322 return buddyStore; |
|
323 } |
|
324 |
|
325 // --------------------------------------------------------------------------- |
|
326 // CPresenceCacheServiceStore::RemoveAllPresences() |
|
327 // --------------------------------------------------------------------------- |
|
328 // |
|
329 void CPresenceCacheServiceStore::RemoveAllPresences() |
|
330 { |
|
331 TRACE( _L("CPresenceCacheServiceStore::RemoveAllPresences - begin") ); |
|
332 // TRACE_1( _L("______iService: %S"), &(iServiceName->Des())); |
|
333 |
|
334 TInt count = iBuddyStoreCollection.Count(); |
|
335 TRACE_1( _L("______store collection count: %d"), count); |
|
336 for (TInt i=0;i<count;i++) |
|
337 { |
|
338 (iBuddyStoreCollection[i])->RemovePresenceBuddyInfo(); |
|
339 //if it doesnt have subscribers delete this whole object |
|
340 if(!(iBuddyStoreCollection[i]->HasSubscribers())) |
|
341 { |
|
342 delete iBuddyStoreCollection[i]; |
|
343 iBuddyStoreCollection.Remove(i); |
|
344 count--; |
|
345 i--; |
|
346 } |
|
347 } |
|
348 iPresenceCount = 0; |
|
349 TRACE(_L("CPresenceCacheServiceStore::RemoveAllPresences - end")); |
|
350 } |
|
351 |
|
352 // eof |