|
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 <liwvariant.h> |
|
22 #include <s32mem.h> |
|
23 #include <escapeutils.h> |
|
24 |
|
25 #include "cpactiondatacache.h" |
|
26 #include "cpglobals.h" |
|
27 |
|
28 using namespace LIW; |
|
29 |
|
30 _LIT8(KCachedMap, "cached_map"); |
|
31 |
|
32 static const int KMaxCacheItems = 18; |
|
33 |
|
34 |
|
35 // ======== MEMBER FUNCTIONS ======== |
|
36 |
|
37 |
|
38 // --------------------------------------------------------------------------- |
|
39 // |
|
40 // --------------------------------------------------------------------------- |
|
41 // |
|
42 CCPActionDataCache* CCPActionDataCache::NewL() |
|
43 { |
|
44 CCPActionDataCache* self = CCPActionDataCache::NewLC(); |
|
45 CleanupStack::Pop(self); |
|
46 return self; |
|
47 } |
|
48 |
|
49 // --------------------------------------------------------------------------- |
|
50 // |
|
51 // --------------------------------------------------------------------------- |
|
52 // |
|
53 CCPActionDataCache* CCPActionDataCache::NewLC() |
|
54 { |
|
55 CCPActionDataCache* self = new (ELeave) CCPActionDataCache; |
|
56 CleanupStack::PushL(self); |
|
57 self->ConstructL(); |
|
58 return self; |
|
59 } |
|
60 |
|
61 // --------------------------------------------------------------------------- |
|
62 // |
|
63 // --------------------------------------------------------------------------- |
|
64 // |
|
65 void CCPActionDataCache::ConstructL( ) |
|
66 { |
|
67 iInternalList = CLiwDefaultList::NewL(); |
|
68 } |
|
69 |
|
70 // --------------------------------------------------------------------------- |
|
71 // |
|
72 // --------------------------------------------------------------------------- |
|
73 // |
|
74 CCPActionDataCache::CCPActionDataCache() |
|
75 { |
|
76 } |
|
77 |
|
78 // --------------------------------------------------------------------------- |
|
79 // |
|
80 // --------------------------------------------------------------------------- |
|
81 // |
|
82 CCPActionDataCache::~CCPActionDataCache() |
|
83 { |
|
84 if (iInternalList) |
|
85 { |
|
86 iInternalList->Close(); |
|
87 } |
|
88 } |
|
89 |
|
90 // --------------------------------------------------------------------------- |
|
91 // |
|
92 // --------------------------------------------------------------------------- |
|
93 // |
|
94 void CCPActionDataCache::HandleCacheRemoveL(const CLiwMap* aMap) |
|
95 { |
|
96 TInt id = FindL(aMap); |
|
97 if (id != KErrNotFound) |
|
98 { |
|
99 iInternalList->Remove(id); |
|
100 } |
|
101 } |
|
102 |
|
103 // --------------------------------------------------------------------------- |
|
104 // |
|
105 // --------------------------------------------------------------------------- |
|
106 // |
|
107 void CCPActionDataCache::AppendL( const CLiwGenericParamList* aParamList) |
|
108 { |
|
109 const TLiwGenericParam* param; |
|
110 TInt pos( 0); |
|
111 param = aParamList->FindFirst( pos, KListMap ); |
|
112 |
|
113 if (param && param->Value().TypeId() == EVariantTypeMap) |
|
114 { |
|
115 const CLiwMap* inputMap = param->Value().AsMap(); |
|
116 CLiwDefaultMap* map = CLiwDefaultMap::NewLC(); |
|
117 CopyVariantL(KType, inputMap, map); |
|
118 CopyVariantL(KId, inputMap, map); |
|
119 CopyVariantL(KPublisherId, inputMap, map); |
|
120 CopyVariantL(KContentType, inputMap, map); |
|
121 CopyVariantL(KContentId, inputMap, map); |
|
122 map->InsertL(KCachedMap, TLiwVariant(inputMap)); |
|
123 iInternalList->AppendL(TLiwVariant(map)); |
|
124 CleanupStack::PopAndDestroy(map); |
|
125 |
|
126 if (iInternalList->Count() > KMaxCacheItems) |
|
127 { |
|
128 iInternalList->Remove(0); |
|
129 } |
|
130 } |
|
131 } |
|
132 |
|
133 // --------------------------------------------------------------------------- |
|
134 // |
|
135 // --------------------------------------------------------------------------- |
|
136 // |
|
137 void CCPActionDataCache::AppendEmptyL( const CLiwMap* aIdsMap ) |
|
138 { |
|
139 CLiwDefaultMap* map = CLiwDefaultMap::NewLC(); |
|
140 CopyVariantL(KType, aIdsMap, map); |
|
141 CopyVariantL(KId, aIdsMap, map); |
|
142 CopyVariantL(KPublisherId, aIdsMap, map); |
|
143 CopyVariantL(KContentType, aIdsMap, map); |
|
144 CopyVariantL(KContentId, aIdsMap, map); |
|
145 iInternalList->AppendL(TLiwVariant(map)); |
|
146 CleanupStack::PopAndDestroy(map); |
|
147 } |
|
148 |
|
149 // --------------------------------------------------------------------------- |
|
150 // |
|
151 // --------------------------------------------------------------------------- |
|
152 // |
|
153 TBool CCPActionDataCache::IsCacheableL(const CLiwMap* aMap) |
|
154 { |
|
155 TBool result(EFalse); |
|
156 TLiwVariant value; |
|
157 value.PushL(); |
|
158 |
|
159 if (aMap->FindL(KId, value) && value.AsTInt32() > 0) |
|
160 { |
|
161 result = ETrue; |
|
162 } |
|
163 else if (IsSpecifiedL(aMap, KPublisherId) && IsSpecifiedL(aMap, |
|
164 KContentType) && IsSpecifiedL(aMap, KContentId)) |
|
165 { |
|
166 result = ETrue; |
|
167 } |
|
168 |
|
169 CleanupStack::PopAndDestroy(&value); |
|
170 return result; |
|
171 } |
|
172 |
|
173 // --------------------------------------------------------------------------- |
|
174 // |
|
175 // --------------------------------------------------------------------------- |
|
176 // |
|
177 TBool CCPActionDataCache::ExistL(const CLiwMap* aMap) |
|
178 { |
|
179 TBool result(EFalse); |
|
180 if (FindL(aMap) != KErrNotFound) |
|
181 { |
|
182 result = ETrue; |
|
183 } |
|
184 return result; |
|
185 } |
|
186 |
|
187 // --------------------------------------------------------------------------- |
|
188 // |
|
189 // --------------------------------------------------------------------------- |
|
190 // |
|
191 void CCPActionDataCache::GetL(const CLiwMap* aMap, |
|
192 CLiwGenericParamList* aParamList) |
|
193 { |
|
194 TInt id = FindL(aMap); |
|
195 if (id != KErrNotFound) |
|
196 { |
|
197 TLiwVariant value; |
|
198 value.PushL(); |
|
199 iInternalList->AtL(id, value); |
|
200 const CLiwMap* map = value.AsMap(); |
|
201 if (map->FindL(KCachedMap, value)) |
|
202 { |
|
203 CLiwDefaultMap* outMap = CLiwDefaultMap::NewLC(); |
|
204 value.Get(*outMap); |
|
205 TLiwGenericParam genericParam(KListMap, TLiwVariant(outMap)); |
|
206 aParamList->AppendL(genericParam); |
|
207 CleanupStack::PopAndDestroy(outMap); |
|
208 } |
|
209 CleanupStack::PopAndDestroy(&value); |
|
210 } |
|
211 } |
|
212 |
|
213 // --------------------------------------------------------------------------- |
|
214 // |
|
215 // --------------------------------------------------------------------------- |
|
216 // |
|
217 TBool CCPActionDataCache::MatchL(const CLiwMap* aCachedMap, |
|
218 const CLiwMap* aInputMap) |
|
219 { |
|
220 TBool idMatch(EFalse); |
|
221 TLiwVariant l, r; |
|
222 l.PushL(); |
|
223 r.PushL(); |
|
224 if (TypesMatchL(aCachedMap,aInputMap)) |
|
225 { |
|
226 if (aCachedMap->FindL(KId, l) && aInputMap->FindL(KId, r) && l.AsTInt32() |
|
227 == r.AsTInt32()) |
|
228 { |
|
229 idMatch = ETrue; |
|
230 } |
|
231 if (!idMatch) |
|
232 { |
|
233 if (MatchL(aCachedMap, aInputMap, KPublisherId) && MatchL(aCachedMap, |
|
234 aInputMap, KContentType) && MatchL(aCachedMap, aInputMap, |
|
235 KContentId)) |
|
236 { |
|
237 idMatch = ETrue; |
|
238 } |
|
239 } |
|
240 } |
|
241 CleanupStack::PopAndDestroy(&r); |
|
242 CleanupStack::PopAndDestroy(&l); |
|
243 return idMatch; |
|
244 } |
|
245 |
|
246 // --------------------------------------------------------------------------- |
|
247 // |
|
248 // --------------------------------------------------------------------------- |
|
249 // |
|
250 TBool CCPActionDataCache::TypesMatchL(const CLiwMap* aCachedMap, |
|
251 const CLiwMap* aInputMap) |
|
252 { |
|
253 TBool typesMatch(EFalse); |
|
254 TLiwVariant cacheVariant; |
|
255 TLiwVariant inputVariant; |
|
256 cacheVariant.PushL(); |
|
257 inputVariant.PushL(); |
|
258 if (aCachedMap->FindL(KType, cacheVariant) && aInputMap->FindL(KType, |
|
259 inputVariant)) |
|
260 { |
|
261 RBuf typeCache; |
|
262 RBuf typeInput; |
|
263 CleanupClosePushL(typeCache); |
|
264 CleanupClosePushL(typeInput); |
|
265 ExtractRBufL(cacheVariant, typeCache); |
|
266 ExtractRBufL(inputVariant, typeInput); |
|
267 if (typeCache.Compare(typeInput)==0) |
|
268 { |
|
269 typesMatch = ETrue; |
|
270 } |
|
271 CleanupStack::PopAndDestroy( &typeInput ); |
|
272 CleanupStack::PopAndDestroy( &typeCache ); |
|
273 } |
|
274 CleanupStack::PopAndDestroy(&inputVariant); |
|
275 CleanupStack::PopAndDestroy(&cacheVariant); |
|
276 return typesMatch; |
|
277 } |
|
278 |
|
279 // --------------------------------------------------------------------------- |
|
280 // |
|
281 // --------------------------------------------------------------------------- |
|
282 // |
|
283 void CCPActionDataCache::ExtractRBufL(const TLiwVariant& aVariant, RBuf& aBuf) |
|
284 { |
|
285 TPtrC type( KNullDesC ); |
|
286 if( !aVariant.Get( type ) ) |
|
287 { |
|
288 TPtrC8 type8( KNullDesC8 ); |
|
289 if( !aVariant.Get( type8 ) ) |
|
290 { |
|
291 User::Leave( KErrBadName ); |
|
292 } |
|
293 aBuf.Assign( EscapeUtils::ConvertToUnicodeFromUtf8L( type8 ) ); |
|
294 } |
|
295 else |
|
296 { |
|
297 aBuf.CreateL( type ); |
|
298 } |
|
299 } |
|
300 |
|
301 // --------------------------------------------------------------------------- |
|
302 // |
|
303 // --------------------------------------------------------------------------- |
|
304 // |
|
305 TBool CCPActionDataCache::MatchL(const CLiwMap* aLeft, const CLiwMap* aRight, |
|
306 const TDesC8& aKey) |
|
307 { |
|
308 TBool match(EFalse); |
|
309 TLiwVariant l, r; |
|
310 l.PushL(); |
|
311 r.PushL(); |
|
312 if (aLeft->FindL(aKey, l) && aRight->FindL(aKey, r) |
|
313 && !l.AsDes().Compare(r.AsDes())) |
|
314 { |
|
315 match = ETrue; |
|
316 } |
|
317 CleanupStack::PopAndDestroy(&r); |
|
318 CleanupStack::PopAndDestroy(&l); |
|
319 return match; |
|
320 } |
|
321 |
|
322 // --------------------------------------------------------------------------- |
|
323 // |
|
324 // --------------------------------------------------------------------------- |
|
325 // |
|
326 TBool CCPActionDataCache::IsSpecifiedL(const CLiwMap* aMap, |
|
327 const TDesC8& aKey) |
|
328 { |
|
329 TBool result(EFalse); |
|
330 TLiwVariant value; |
|
331 value.PushL(); |
|
332 if (aMap->FindL(aKey, value) && value.AsDes().Compare(KNullDesC) != 0 |
|
333 && value.AsDes().Compare(KAll) != 0) |
|
334 { |
|
335 result = ETrue; |
|
336 } |
|
337 CleanupStack::PopAndDestroy(&value); |
|
338 return result; |
|
339 } |
|
340 |
|
341 // --------------------------------------------------------------------------- |
|
342 // |
|
343 // --------------------------------------------------------------------------- |
|
344 // |
|
345 TInt CCPActionDataCache::FindL(const CLiwMap* aKey) |
|
346 { |
|
347 TInt result(KErrNotFound); |
|
348 TLiwVariant value; |
|
349 value.PushL(); |
|
350 for (TInt i = 0; i < iInternalList->Count(); i++) |
|
351 { |
|
352 iInternalList->AtL(i, value); |
|
353 if (MatchL(value.AsMap(), aKey)) |
|
354 { |
|
355 result = i; |
|
356 break; |
|
357 } |
|
358 } |
|
359 CleanupStack::PopAndDestroy(&value); |
|
360 return result; |
|
361 } |
|
362 |
|
363 // ----------------------------------------------------------------------------- |
|
364 // |
|
365 // ----------------------------------------------------------------------------- |
|
366 // |
|
367 void CCPActionDataCache::CopyVariantL(const TDesC8& aKey, |
|
368 const CLiwMap* aInMap, CLiwDefaultMap* aOutMap) |
|
369 { |
|
370 //TODO: method exist also in data manager - should be refactored |
|
371 TLiwVariant variant; |
|
372 variant.PushL(); |
|
373 if (aInMap->FindL(aKey, variant)) |
|
374 { |
|
375 aOutMap->InsertL(aKey, variant); |
|
376 } |
|
377 CleanupStack::PopAndDestroy(&variant); |
|
378 } |