|
1 /* |
|
2 * Copyright (c) 2007-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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "hnxmlmodelcache.h" |
|
20 #include "hnglobals.h" |
|
21 |
|
22 // --------------------------------------------------------------------------- |
|
23 // |
|
24 // --------------------------------------------------------------------------- |
|
25 // |
|
26 TUint32 HBufCHashCache( HBufC* const & aBuf ) |
|
27 { |
|
28 return DefaultHash::Des16(*aBuf); |
|
29 } |
|
30 |
|
31 // --------------------------------------------------------------------------- |
|
32 // |
|
33 // --------------------------------------------------------------------------- |
|
34 // |
|
35 TBool HBufCIdentCache( HBufC* const & aL, HBufC* const & aR ) |
|
36 { |
|
37 return DefaultIdentity::Des16(*aL, *aR); |
|
38 } |
|
39 |
|
40 |
|
41 // --------------------------------------------------------------------------- |
|
42 // |
|
43 // --------------------------------------------------------------------------- |
|
44 // |
|
45 CHnXmlModelCache::CHnXmlModelCache() : iDocuments( &HBufCHashCache, |
|
46 &HBufCIdentCache ) |
|
47 { |
|
48 |
|
49 } |
|
50 |
|
51 // --------------------------------------------------------------------------- |
|
52 // |
|
53 // --------------------------------------------------------------------------- |
|
54 // |
|
55 void CHnXmlModelCache::ConstructL() |
|
56 { |
|
57 |
|
58 } |
|
59 |
|
60 // --------------------------------------------------------------------------- |
|
61 // |
|
62 // --------------------------------------------------------------------------- |
|
63 // |
|
64 void CHnXmlModelCache::RemoveUnused() |
|
65 { |
|
66 // remove unnecessary (oldest and unused) xml cached documents |
|
67 if(iDocuments.Count() > KXmlModelCacheMaxLength) |
|
68 { |
|
69 HBufC *suiteToRemove = iOrder[0]; |
|
70 RXmlEngDocument* document = iDocuments.Find( suiteToRemove ); |
|
71 document->Close(); |
|
72 iDocuments.Remove(suiteToRemove); |
|
73 iOrder.Remove(0); |
|
74 delete suiteToRemove; |
|
75 } |
|
76 } |
|
77 |
|
78 // --------------------------------------------------------------------------- |
|
79 // |
|
80 // --------------------------------------------------------------------------- |
|
81 // |
|
82 TInt CHnXmlModelCache::AddL( const TDesC& aSuiteName, RXmlEngDocument& aDocument ) |
|
83 { |
|
84 HBufC* key = aSuiteName.AllocLC(); |
|
85 iDocuments.InsertL( key, aDocument ); |
|
86 CleanupStack::Pop( key ); |
|
87 iOrder.AppendL( key ); |
|
88 RemoveUnused(); |
|
89 return KErrNone; |
|
90 } |
|
91 |
|
92 // --------------------------------------------------------------------------- |
|
93 // |
|
94 // --------------------------------------------------------------------------- |
|
95 // |
|
96 TBool CHnXmlModelCache::IsCachedL( const TDesC& aSuiteName ) |
|
97 { |
|
98 HBufC* buf = aSuiteName.AllocLC(); |
|
99 const RXmlEngDocument* res = iDocuments.Find( buf ); |
|
100 CleanupStack::PopAndDestroy( buf ); |
|
101 |
|
102 if ( res ) |
|
103 return ETrue; |
|
104 else |
|
105 return EFalse; |
|
106 } |
|
107 |
|
108 // --------------------------------------------------------------------------- |
|
109 // |
|
110 // --------------------------------------------------------------------------- |
|
111 // |
|
112 void CHnXmlModelCache::MoveToTopL( const TDesC & aSuiteName ) |
|
113 { |
|
114 TInt pos( KErrNotFound ); |
|
115 |
|
116 for ( TInt i(0); i < iOrder.Count(); i++ ) |
|
117 { |
|
118 if ( !iOrder[i]->Compare( aSuiteName ) ) |
|
119 { |
|
120 pos = i; |
|
121 break; |
|
122 } |
|
123 } |
|
124 |
|
125 // if document was found and is not the latest in the array |
|
126 if ( pos != KErrNotFound && ( pos != iOrder.Count() - 1 ) ) |
|
127 { |
|
128 HBufC *tmp = iOrder[ pos ]; |
|
129 iOrder.Remove( pos ); |
|
130 iOrder.AppendL( tmp ); |
|
131 } |
|
132 } |
|
133 |
|
134 // --------------------------------------------------------------------------- |
|
135 // |
|
136 // --------------------------------------------------------------------------- |
|
137 // |
|
138 void CHnXmlModelCache::GetL( const TDesC& aSuiteName, |
|
139 RXmlEngDocument& aDocument ) |
|
140 { |
|
141 HBufC* buf = aSuiteName.AllocLC(); |
|
142 const RXmlEngDocument* res = iDocuments.Find( buf ); |
|
143 |
|
144 MoveToTopL( aSuiteName ); |
|
145 |
|
146 if ( res ) |
|
147 { |
|
148 aDocument = *res; |
|
149 } |
|
150 |
|
151 CleanupStack::PopAndDestroy( buf ); |
|
152 } |
|
153 |
|
154 // --------------------------------------------------------------------------- |
|
155 // |
|
156 // --------------------------------------------------------------------------- |
|
157 // |
|
158 CHnXmlModelCache* CHnXmlModelCache::NewLC() |
|
159 { |
|
160 CHnXmlModelCache* self = new (ELeave) CHnXmlModelCache(); |
|
161 CleanupStack::PushL( self ); |
|
162 self->ConstructL(); |
|
163 return self; |
|
164 } |
|
165 |
|
166 // --------------------------------------------------------------------------- |
|
167 // |
|
168 // --------------------------------------------------------------------------- |
|
169 // |
|
170 CHnXmlModelCache* CHnXmlModelCache::NewL() |
|
171 { |
|
172 CHnXmlModelCache* self = CHnXmlModelCache::NewLC(); |
|
173 CleanupStack::Pop( self ); |
|
174 return self; |
|
175 } |
|
176 |
|
177 // --------------------------------------------------------------------------- |
|
178 // |
|
179 // --------------------------------------------------------------------------- |
|
180 // |
|
181 void CHnXmlModelCache::Reset() |
|
182 { |
|
183 THashMapIter<HBufC*, RXmlEngDocument> iter( iDocuments ); |
|
184 while ( HBufC* const * ptr = iter.NextKey() ) |
|
185 { |
|
186 RXmlEngDocument* currentDocument = iter.CurrentValue(); |
|
187 currentDocument->Close(); |
|
188 delete *ptr; |
|
189 } |
|
190 iDocuments.Close(); |
|
191 // Names have already been destroyed when destroying iDocuments so |
|
192 // it is not necessay to destroy elements of the iOrder array. |
|
193 iOrder.Close(); |
|
194 } |
|
195 |
|
196 |
|
197 // --------------------------------------------------------------------------- |
|
198 // |
|
199 // --------------------------------------------------------------------------- |
|
200 // |
|
201 CHnXmlModelCache::~CHnXmlModelCache() |
|
202 { |
|
203 Reset(); |
|
204 } |