1 /* |
|
2 * Copyright (c) 2005 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: CXcapCacheIndexEntry |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 // INCLUDE FILES |
|
22 #include "ServerDefines.h" |
|
23 #include "XcapCacheServer.h" |
|
24 #include "XcapCacheIndexEntry.h" |
|
25 #include "XcapCacheEntryProperty.h" |
|
26 |
|
27 // ---------------------------------------------------------- |
|
28 // CXcapCacheIndexEntry::CXcapCacheIndexEntry |
|
29 // |
|
30 // ---------------------------------------------------------- |
|
31 // |
|
32 CXcapCacheIndexEntry::CXcapCacheIndexEntry() |
|
33 { |
|
34 } |
|
35 |
|
36 // ---------------------------------------------------------- |
|
37 // CXcapCacheIndexEntry::NewL |
|
38 // |
|
39 // ---------------------------------------------------------- |
|
40 // |
|
41 CXcapCacheIndexEntry* CXcapCacheIndexEntry::NewL( const TCacheEntryInfo* aHeader ) |
|
42 { |
|
43 CXcapCacheIndexEntry* self = new( ELeave ) CXcapCacheIndexEntry(); |
|
44 CleanupStack::PushL( self ); |
|
45 self->ConstructL( aHeader ); |
|
46 CleanupStack::Pop(); |
|
47 return self; |
|
48 } |
|
49 |
|
50 // ---------------------------------------------------------- |
|
51 // CXcapCacheIndexEntry::NewL |
|
52 // |
|
53 // ---------------------------------------------------------- |
|
54 // |
|
55 CXcapCacheIndexEntry* CXcapCacheIndexEntry::NewL( TPtr8& aEntryData ) |
|
56 { |
|
57 CXcapCacheIndexEntry* self = new( ELeave ) CXcapCacheIndexEntry(); |
|
58 CleanupStack::PushL( self ); |
|
59 self->ConstructL( aEntryData ); |
|
60 CleanupStack::Pop(); |
|
61 return self; |
|
62 } |
|
63 |
|
64 // ---------------------------------------------------------- |
|
65 // CXcapCacheIndexEntry::~CXcapCacheIndexEntry |
|
66 // |
|
67 // ---------------------------------------------------------- |
|
68 // |
|
69 CXcapCacheIndexEntry::~CXcapCacheIndexEntry() |
|
70 { |
|
71 iPropertyList.ResetAndDestroy(); |
|
72 iPropertyList.Close(); |
|
73 } |
|
74 |
|
75 // ---------------------------------------------------------- |
|
76 // CXcapCacheIndexEntry::ConstructL |
|
77 // |
|
78 // ---------------------------------------------------------- |
|
79 // |
|
80 void CXcapCacheIndexEntry::ConstructL( TPtr8& aEntryData ) |
|
81 { |
|
82 while( aEntryData.Length() > 0 ) |
|
83 { |
|
84 CXcapCacheEntryProperty* prop = NULL; |
|
85 TInt index = aEntryData.FindF( KValueFieldSeparator ); |
|
86 if( index > 0 ) |
|
87 { |
|
88 TPtrC8 propData = aEntryData.Left( index ); |
|
89 TInt separator = propData.FindF( KValuePropertySeparator ); |
|
90 if( separator > 0 ) |
|
91 { |
|
92 TPtrC8 name = propData.Left( separator ); |
|
93 TPtrC8 value = propData.Right( propData.Length() - separator - 1 ); |
|
94 const TInt nameId = FindPropNameId( name ); |
|
95 __ASSERT_DEBUG( nameId != KErrNotFound, User::Leave( KErrCorrupt ) ); |
|
96 prop = CreatePropertyL( nameId, value ); |
|
97 CleanupStack::PushL( prop ); |
|
98 User::LeaveIfError( iPropertyList.Append( prop ) ); |
|
99 CleanupStack::Pop(); //prop |
|
100 } |
|
101 aEntryData.Delete( 0, index + 1 ); |
|
102 } |
|
103 } |
|
104 } |
|
105 |
|
106 // ---------------------------------------------------------- |
|
107 // CXcapCacheIndexEntry::ConstructL |
|
108 // |
|
109 // ---------------------------------------------------------- |
|
110 // |
|
111 void CXcapCacheIndexEntry::ConstructL( const TCacheEntryInfo* aHeader ) |
|
112 { |
|
113 CreateFromHeaderL( aHeader ); |
|
114 } |
|
115 |
|
116 // ---------------------------------------------------------- |
|
117 // CXcapCacheIndexEntry::FindPropNameId |
|
118 // |
|
119 // ---------------------------------------------------------- |
|
120 // |
|
121 TInt CXcapCacheIndexEntry::FindPropNameId( const TDesC8& aNameString ) const |
|
122 { |
|
123 TBool found = EFalse; |
|
124 TInt ret = KErrNotFound; |
|
125 TInt arrayLength = sizeof( KCacheEntryArray ) / |
|
126 sizeof( KCacheEntryArray[0] ); |
|
127 for( TInt i = 0;!found && i < arrayLength;i++ ) |
|
128 { |
|
129 if( aNameString.CompareF( TPtrC8( KCacheEntryArray[i] ) ) == 0 ) |
|
130 { |
|
131 ret = i; |
|
132 found = ETrue; |
|
133 } |
|
134 } |
|
135 return ret; |
|
136 } |
|
137 |
|
138 // ---------------------------------------------------------- |
|
139 // CXcapCacheIndexEntry::UpdateEntryL |
|
140 // |
|
141 // ---------------------------------------------------------- |
|
142 // |
|
143 void CXcapCacheIndexEntry::UpdateEntryL( const TCacheEntryInfo* aHeader ) |
|
144 { |
|
145 TTime access = LastAccess(); |
|
146 DeleteFileL( FileNameL() ); |
|
147 iPropertyList.ResetAndDestroy(); |
|
148 CreateFromHeaderL( aHeader ); |
|
149 } |
|
150 |
|
151 // ---------------------------------------------------------- |
|
152 // CXcapCacheIndexEntry::DeleteFileL |
|
153 // |
|
154 // ---------------------------------------------------------- |
|
155 // |
|
156 void CXcapCacheIndexEntry::DeleteFileL( const TDesC8& aFileName ) |
|
157 { |
|
158 HBufC* buf = HBufC::NewLC( aFileName.Length() ); |
|
159 buf->Des().Copy( aFileName ); |
|
160 CFileMan* manager = CFileMan::NewL( CXcapCacheServer::FileSession() ); |
|
161 CleanupStack::PushL( manager ); |
|
162 TInt error = manager->Delete( buf->Des() ); |
|
163 #ifdef _DEBUG |
|
164 CXcapCacheServer::WriteToLog( _L8( "CXcapCacheServer::DeleteFileL() - Error: %d" ), error ); |
|
165 #endif |
|
166 CleanupStack::PopAndDestroy( 2 ); //manager, buf |
|
167 } |
|
168 |
|
169 // ---------------------------------------------------------- |
|
170 // CXcapCacheIndexEntry::CreatePropertyL |
|
171 // |
|
172 // ---------------------------------------------------------- |
|
173 // |
|
174 CXcapCacheEntryProperty* CXcapCacheIndexEntry::CreatePropertyL( const TInt aPropertyName, |
|
175 const TDesC8& aPropertyValue ) |
|
176 { |
|
177 return CXcapCacheEntryProperty::NewL( aPropertyName, aPropertyValue ); |
|
178 } |
|
179 |
|
180 // --------------------------------------------------------- |
|
181 // CXcapCacheIndexTableEntry::FindProperty |
|
182 // |
|
183 // --------------------------------------------------------- |
|
184 // |
|
185 const CXcapCacheEntryProperty& CXcapCacheIndexEntry::FindProperty( const TInt aPropId ) const |
|
186 { |
|
187 TBool found = EFalse; |
|
188 TInt count = iPropertyList.Count(); |
|
189 CXcapCacheEntryProperty* property = NULL; |
|
190 for( TInt i = 0;!found && i < count;i++ ) |
|
191 { |
|
192 property = iPropertyList[i]; |
|
193 if( property->Name() == aPropId ) |
|
194 found = ETrue; |
|
195 } |
|
196 return *property; |
|
197 } |
|
198 |
|
199 // --------------------------------------------------------- |
|
200 // CXcapCacheIndexTableEntry::LastAccess |
|
201 // |
|
202 // --------------------------------------------------------- |
|
203 // |
|
204 TTime CXcapCacheIndexEntry::LastAccess() const |
|
205 { |
|
206 const CXcapCacheEntryProperty& property = FindProperty( KCacheLastAccess ); |
|
207 return property.TimeValue(); |
|
208 } |
|
209 |
|
210 // --------------------------------------------------------- |
|
211 // CXcapCacheIndexTableEntry::LastModified |
|
212 // |
|
213 // --------------------------------------------------------- |
|
214 // |
|
215 TTime CXcapCacheIndexEntry::LastModified() const |
|
216 { |
|
217 const CXcapCacheEntryProperty& property = FindProperty( KCacheLastModified ); |
|
218 return property.TimeValue(); |
|
219 } |
|
220 |
|
221 // --------------------------------------------------------- |
|
222 // CXcapCacheIndexTableEntry::XmlSize |
|
223 // |
|
224 // --------------------------------------------------------- |
|
225 // |
|
226 TInt CXcapCacheIndexEntry::XmlSize() const |
|
227 { |
|
228 const CXcapCacheEntryProperty& property = FindProperty( KCacheXmlSize ); |
|
229 return property.IntValue(); |
|
230 } |
|
231 |
|
232 // --------------------------------------------------------- |
|
233 // CXcapCacheIndexTableEntry::FileNameL |
|
234 // |
|
235 // --------------------------------------------------------- |
|
236 // |
|
237 TPtrC8 CXcapCacheIndexEntry::FileNameL() const |
|
238 { |
|
239 const CXcapCacheEntryProperty& property = FindProperty( KCacheXmlFile ); |
|
240 return property.DesValue8(); |
|
241 } |
|
242 |
|
243 // --------------------------------------------------------- |
|
244 // CXcapCacheIndexTableEntry::FileName16L |
|
245 // |
|
246 // --------------------------------------------------------- |
|
247 // |
|
248 TPtrC CXcapCacheIndexEntry::FileName16L() const |
|
249 { |
|
250 const CXcapCacheEntryProperty& property = FindProperty( KCacheXmlFile ); |
|
251 return property.DesValue16(); |
|
252 } |
|
253 |
|
254 // --------------------------------------------------------- |
|
255 // CXcapCacheIndexTableEntry::ETag |
|
256 // |
|
257 // --------------------------------------------------------- |
|
258 // |
|
259 TPtrC8 CXcapCacheIndexEntry::ETag() const |
|
260 { |
|
261 const CXcapCacheEntryProperty& property = FindProperty( KCacheEntryETag ); |
|
262 return property.DesValue8(); |
|
263 } |
|
264 |
|
265 // ---------------------------------------------------------- |
|
266 // CXcapCacheIndexEntry::CreateFromHeaderL |
|
267 // |
|
268 // ---------------------------------------------------------- |
|
269 // |
|
270 void CXcapCacheIndexEntry::CreateFromHeaderL( const TCacheEntryInfo* aHeader ) |
|
271 { |
|
272 TInt arrayLength = sizeof( KCacheEntryArray ) / |
|
273 sizeof( KCacheEntryArray[0] ); |
|
274 HBufC8* hour = HourStringLC(); |
|
275 for( TInt i = 0;i < arrayLength;i++ ) |
|
276 { |
|
277 CXcapCacheEntryProperty* prop = NULL; |
|
278 switch( i ) |
|
279 { |
|
280 case KCacheEntryETag: |
|
281 prop = CreatePropertyL( KCacheEntryETag, *aHeader->iEtag ); |
|
282 break; |
|
283 case KCacheXmlSize: |
|
284 { |
|
285 TBuf8<10> sizeBuf; |
|
286 sizeBuf.AppendNum( aHeader->iDataLength ); |
|
287 prop = CreatePropertyL( KCacheXmlSize, sizeBuf ); |
|
288 } |
|
289 break; |
|
290 case KCacheXmlFile: |
|
291 { |
|
292 HBufC8* fileName = StoreXmlFileLC( *aHeader->iRespData ); |
|
293 prop = CreatePropertyL( KCacheXmlFile, fileName->Des() ); |
|
294 CleanupStack::PopAndDestroy(); //fileName |
|
295 } |
|
296 break; |
|
297 case KCacheLastAccess: |
|
298 prop = CreatePropertyL( KCacheLastAccess, hour->Des() ); |
|
299 break; |
|
300 case KCacheLastModified: |
|
301 prop = CreatePropertyL( KCacheLastModified, hour->Des() ); |
|
302 break; |
|
303 default: |
|
304 break; |
|
305 } |
|
306 if( prop != NULL ) |
|
307 User::LeaveIfError( iPropertyList.Append( prop ) ); |
|
308 } |
|
309 CleanupStack::PopAndDestroy(); //hour |
|
310 } |
|
311 |
|
312 // ---------------------------------------------------------- |
|
313 // CXcapCacheIndexEntry::HourStringLC |
|
314 // |
|
315 // ---------------------------------------------------------- |
|
316 // |
|
317 HBufC8* CXcapCacheIndexEntry::HourStringLC() |
|
318 { |
|
319 HBufC8* hourString = NULL; |
|
320 TPtrC date( CXcapCacheServer::Date() ); |
|
321 TPtrC time( CXcapCacheServer::Time() ); |
|
322 hourString = HBufC8::NewLC( date.Length() + time.Length() + 1 ); |
|
323 TPtr8 pointer( hourString->Des() ); |
|
324 pointer.Copy( date ); |
|
325 pointer.Append( KPageFileSeparator ); |
|
326 pointer.Append( time ); |
|
327 return hourString; |
|
328 } |
|
329 |
|
330 // ---------------------------------------------------------- |
|
331 // CXcapCacheIndexEntry::StoreXmlFileLC |
|
332 // |
|
333 // ---------------------------------------------------------- |
|
334 // |
|
335 HBufC8* CXcapCacheIndexEntry::StoreXmlFileLC( const TDesC8& aXmlData ) |
|
336 { |
|
337 RFile file; |
|
338 HBufC* nameBuf = NULL; |
|
339 TPtrC randomName( _L( "" ) ); |
|
340 TInt error = KErrAlreadyExists; |
|
341 //It is extremely unlikely that the random string generator |
|
342 //creates two exact same patterns, but let's check for it, anyway. |
|
343 while( error == KErrAlreadyExists ) |
|
344 { |
|
345 randomName.Set( CXcapCacheServer::RandomString() ); |
|
346 nameBuf = HBufC::NewLC( KCacheServerRoot().Length() + randomName.Length() ); |
|
347 nameBuf->Des().Copy( KCacheServerRoot ); |
|
348 nameBuf->Des().Append( randomName ); |
|
349 error = file.Create( CXcapCacheServer::FileSession(), |
|
350 nameBuf->Des(), EFileWrite ); |
|
351 if( error != KErrNone ) |
|
352 CleanupStack::PopAndDestroy(); //nameBuf |
|
353 } |
|
354 file.Write( aXmlData ); |
|
355 file.Close(); |
|
356 __ASSERT_ALWAYS( nameBuf != NULL, User::Leave( KErrCorrupt ) ); |
|
357 HBufC8* ret = HBufC8::NewLC( nameBuf->Des().Length() ); |
|
358 ret->Des().Copy( nameBuf->Des() ); |
|
359 CleanupStack::Pop(); //ret |
|
360 CleanupStack::PopAndDestroy(); //nameBuf |
|
361 CleanupStack::PushL( ret ); |
|
362 return ret; |
|
363 } |
|
364 |
|
365 // ---------------------------------------------------------- |
|
366 // CXcapCacheIndexEntry::PrintLC |
|
367 // |
|
368 // ---------------------------------------------------------- |
|
369 // |
|
370 HBufC8* CXcapCacheIndexEntry::PrintLC() const |
|
371 { |
|
372 TInt position = 0; |
|
373 CBufFlat* buffer = NULL; |
|
374 HBufC8* entryString = NULL; |
|
375 TInt count = iPropertyList.Count(); |
|
376 if( count > 0 ) |
|
377 { |
|
378 buffer = CBufFlat::NewL( 50 ); |
|
379 CleanupStack::PushL( buffer ); |
|
380 for( TInt i = 0;i < count;i++ ) |
|
381 { |
|
382 TPtrC8 name = TPtrC8( KCacheEntryArray[iPropertyList[i]->Name()] ); |
|
383 TPtrC8 value = iPropertyList[i]->DesValue8(); |
|
384 buffer->InsertL( position, name ); |
|
385 position = position + name.Length(); |
|
386 buffer->InsertL( position, KValuePropertySeparator ); |
|
387 position = position + KValuePropertySeparator().Length(); |
|
388 buffer->InsertL( position, value ); |
|
389 position = position + value.Length(); |
|
390 buffer->InsertL( position, KValueFieldSeparator ); |
|
391 position = position + KValueFieldSeparator().Length(); |
|
392 } |
|
393 buffer->InsertL( position, KIndexFileEndOfLine ); |
|
394 TPtr8 pointer( buffer->Ptr( 0 ) ); |
|
395 entryString = HBufC8::NewL( pointer.Length() ); |
|
396 entryString->Des().Copy( pointer ); |
|
397 CleanupStack::PopAndDestroy(); //buffer |
|
398 CleanupStack::PushL( entryString ); |
|
399 } |
|
400 return entryString; |
|
401 } |
|
402 |
|
403 |
|
404 |
|