|
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: CXcapCacheIndexAdmin |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 // INCLUDE FILES |
|
22 #include "ServerDefines.h" |
|
23 #include "XcapCacheServer.h" |
|
24 #include "XcapCacheIndexAdmin.h" |
|
25 #include "XcapCacheIndexTableEntry.h" |
|
26 |
|
27 // ---------------------------------------------------------- |
|
28 // CXcapCacheIndexAdmin::CXcapCacheIndexAdmin |
|
29 // |
|
30 // ---------------------------------------------------------- |
|
31 // |
|
32 CXcapCacheIndexAdmin::CXcapCacheIndexAdmin() |
|
33 { |
|
34 } |
|
35 |
|
36 // ---------------------------------------------------------- |
|
37 // CXcapCacheIndexAdmin::NewL |
|
38 // |
|
39 // ---------------------------------------------------------- |
|
40 // |
|
41 CXcapCacheIndexAdmin* CXcapCacheIndexAdmin::NewL() |
|
42 { |
|
43 CXcapCacheIndexAdmin* self = new( ELeave ) CXcapCacheIndexAdmin(); |
|
44 CleanupStack::PushL( self ); |
|
45 self->ConstructL(); |
|
46 CleanupStack::Pop(); |
|
47 return self; |
|
48 } |
|
49 |
|
50 // ---------------------------------------------------------- |
|
51 // CXcapCacheIndexAdmin::~CXcapCacheIndexAdmin |
|
52 // |
|
53 // ---------------------------------------------------------- |
|
54 // |
|
55 CXcapCacheIndexAdmin::~CXcapCacheIndexAdmin() |
|
56 { |
|
57 iEntryList.ResetAndDestroy(); |
|
58 iEntryList.Close(); |
|
59 } |
|
60 |
|
61 // ---------------------------------------------------------- |
|
62 // CXcapCacheIndexAdmin::ConstructL |
|
63 // |
|
64 // ---------------------------------------------------------- |
|
65 // |
|
66 void CXcapCacheIndexAdmin::ConstructL() |
|
67 { |
|
68 TInt length = 0; |
|
69 RFile indexTable = InitialisePageFileL(); |
|
70 CleanupClosePushL( indexTable ); |
|
71 User::LeaveIfError( indexTable.Size( length ) ); |
|
72 if( length > 0 ) |
|
73 { |
|
74 HBufC8* data = HBufC8::NewLC( length ); |
|
75 TPtr8 pointer( data->Des() ); |
|
76 User::LeaveIfError( indexTable.Read( pointer ) ); |
|
77 ReadEntriesL( pointer ); |
|
78 CleanupStack::PopAndDestroy(); //data |
|
79 } |
|
80 CleanupStack::PopAndDestroy(); //index |
|
81 } |
|
82 |
|
83 // ---------------------------------------------------------- |
|
84 // CXcapCacheIndex::InitialisePageFileL |
|
85 // |
|
86 // ---------------------------------------------------------- |
|
87 // |
|
88 RFile CXcapCacheIndexAdmin::InitialisePageFileL() |
|
89 { |
|
90 RFile ret; |
|
91 TInt error = ret.Open( CXcapCacheServer::FileSession(), KCacheServerPageFile, |
|
92 EFileShareExclusive | EFileRead | EFileWrite ); |
|
93 //No need to check the path at this time, because needed |
|
94 //directories have already been created by the CacheIndex |
|
95 //object if they didn't exist before the server was launched |
|
96 if( error != KErrNone ) |
|
97 User::LeaveIfError( ret.Create( CXcapCacheServer::FileSession(), KCacheServerPageFile, |
|
98 EFileShareExclusive | EFileRead | EFileWrite ) ); |
|
99 return ret; |
|
100 } |
|
101 |
|
102 // ---------------------------------------------------------- |
|
103 // CXcapCacheIndexAdmin::UpdateIndexTableL |
|
104 // |
|
105 // ---------------------------------------------------------- |
|
106 // |
|
107 void CXcapCacheIndexAdmin::UpdateIndexTableL( TInt aIndex, const TCacheEntryInfo* aHeader ) |
|
108 { |
|
109 CXcapCacheIndexTableEntry* entry = |
|
110 CXcapCacheIndexTableEntry::NewL( aIndex, *aHeader->iRootUri, |
|
111 *aHeader->iDocumentUri ); |
|
112 CleanupStack::PushL( entry ); |
|
113 User::LeaveIfError( iEntryList.Append( entry ) ); |
|
114 CleanupStack::Pop(); //entry |
|
115 } |
|
116 |
|
117 // ---------------------------------------------------------- |
|
118 // CXcapCacheSession::ReadIndexL |
|
119 // |
|
120 // ---------------------------------------------------------- |
|
121 // |
|
122 TInt CXcapCacheIndexAdmin::ReadIndexL( const CXcapCacheIndexTableEntry& aEntry ) |
|
123 { |
|
124 TBool found = EFalse; |
|
125 TInt ret = KErrNotFound; |
|
126 TInt count = iEntryList.Count(); |
|
127 CXcapCacheIndexTableEntry* entry = NULL; |
|
128 for( TInt i = 0;!found && i < count;i++ ) |
|
129 { |
|
130 entry = iEntryList[i]; |
|
131 if( *entry == aEntry ) |
|
132 { |
|
133 ret = entry->Index(); |
|
134 found = ETrue; |
|
135 } |
|
136 } |
|
137 return ret; |
|
138 } |
|
139 |
|
140 // ---------------------------------------------------------- |
|
141 // CXcapCacheSession::FindL |
|
142 // |
|
143 // ---------------------------------------------------------- |
|
144 // |
|
145 CXcapCacheIndexTableEntry* CXcapCacheIndexAdmin::FindL( TInt& aTableIndex, |
|
146 const TDesC8& aRootUri, |
|
147 const TDesC& aDocumentUri ) |
|
148 { |
|
149 #ifdef _DEBUG |
|
150 CXcapCacheServer::WriteToLog( _L8( "CXcapCacheIndexAdmin::FindL()" ) ); |
|
151 #endif |
|
152 TBool found = EFalse; |
|
153 TInt count = iEntryList.Count(); |
|
154 CXcapCacheIndexTableEntry* entry = NULL; |
|
155 if( count > 0 ) |
|
156 { |
|
157 CXcapCacheIndexTableEntry* newEntry = CXcapCacheIndexTableEntry::NewL( |
|
158 aRootUri, aDocumentUri ); |
|
159 for( TInt i = 0;!found && i < count;i++ ) |
|
160 { |
|
161 CXcapCacheIndexTableEntry* listEntry = iEntryList[i]; |
|
162 if( *newEntry == *listEntry ) |
|
163 { |
|
164 aTableIndex = i; |
|
165 entry = iEntryList[i]; |
|
166 found = ETrue; |
|
167 } |
|
168 } |
|
169 delete newEntry; |
|
170 newEntry = NULL; |
|
171 } |
|
172 return entry; |
|
173 } |
|
174 |
|
175 // ---------------------------------------------------------- |
|
176 // CXcapCacheSession::RemoveTableIndexL |
|
177 // |
|
178 // ---------------------------------------------------------- |
|
179 // |
|
180 void CXcapCacheIndexAdmin::RemoveTableIndex( TInt aIndex ) |
|
181 { |
|
182 TInt count = iEntryList.Count(); |
|
183 if( count > 0 && aIndex >= 0 && aIndex < count ) |
|
184 { |
|
185 CXcapCacheIndexTableEntry* entry = iEntryList[aIndex]; |
|
186 iEntryList.Remove( aIndex ); |
|
187 delete entry; |
|
188 entry = NULL; |
|
189 for( TInt i = aIndex;i < count - 1;i++ ) |
|
190 { |
|
191 CXcapCacheIndexTableEntry* z = iEntryList[i]; |
|
192 z->Decrease(); |
|
193 } |
|
194 } |
|
195 } |
|
196 |
|
197 // ---------------------------------------------------------- |
|
198 // CXcapCacheSession::StoreIndexTableL |
|
199 // |
|
200 // ---------------------------------------------------------- |
|
201 // |
|
202 void CXcapCacheIndexAdmin::StoreIndexTableL() |
|
203 { |
|
204 RFile indexTable; |
|
205 TInt count = iEntryList.Count(); |
|
206 if( count > 0 ) |
|
207 { |
|
208 TBuf8<7> numBuf; |
|
209 User::LeaveIfError( indexTable.Replace( CXcapCacheServer::FileSession(), |
|
210 KCacheServerPageFile, EFileWrite ) ); |
|
211 CleanupClosePushL( indexTable ); |
|
212 for( TInt i = 0;i < count;i++ ) |
|
213 { |
|
214 numBuf.AppendNum( iEntryList[i]->Index() ); |
|
215 indexTable.Write( iEntryList[i]->RootUri() ); |
|
216 indexTable.Write( KValueFieldSeparator ); |
|
217 indexTable.Write( iEntryList[i]->DocumentUri() ); |
|
218 indexTable.Write( KPageFileSeparator ); |
|
219 indexTable.Write( numBuf ); |
|
220 indexTable.Write( KIndexFileEndOfLine ); |
|
221 numBuf.Zero(); |
|
222 } |
|
223 CleanupStack::PopAndDestroy(); //indexTable |
|
224 } |
|
225 else |
|
226 { |
|
227 CFileMan* manager = CFileMan::NewL( CXcapCacheServer::FileSession() ); |
|
228 manager->Delete( KCacheServerPageFile ); |
|
229 delete manager; |
|
230 manager = NULL; |
|
231 } |
|
232 } |
|
233 |
|
234 // ---------------------------------------------------------- |
|
235 // CXcapCacheSession::UpdatePageFileL |
|
236 // |
|
237 // ---------------------------------------------------------- |
|
238 // |
|
239 void CXcapCacheIndexAdmin::ReadEntriesL( TPtr8& aDataDesc ) |
|
240 { |
|
241 TBool finished = EFalse; |
|
242 while( !finished && aDataDesc.Length() > 0 ) |
|
243 { |
|
244 //If we find that there is still more data, but no \r\n |
|
245 //to denote the end of an entry, simply quit. The data |
|
246 //format has most probably been corrupted at some point. |
|
247 TInt entryIndex = aDataDesc.FindF( KIndexFileEndOfLine ); |
|
248 if( entryIndex > 0 ) |
|
249 { |
|
250 TPtrC8 one = aDataDesc.Left( entryIndex ); |
|
251 CXcapCacheIndexTableEntry* entry = ParseOneEntryL( one ); |
|
252 CleanupStack::PushL( entry ); |
|
253 User::LeaveIfError( iEntryList.Append( entry ) ); |
|
254 CleanupStack::Pop(); |
|
255 aDataDesc.Delete( 0, entryIndex + 2 ); |
|
256 } |
|
257 else finished = ETrue; |
|
258 } |
|
259 } |
|
260 |
|
261 // ---------------------------------------------------------- |
|
262 // CXcapCacheSession::ParseOneEntryL |
|
263 // |
|
264 // ---------------------------------------------------------- |
|
265 // |
|
266 CXcapCacheIndexTableEntry* CXcapCacheIndexAdmin::ParseOneEntryL( const TDesC8& aEntryData ) |
|
267 { |
|
268 CXcapCacheIndexTableEntry* ret = NULL; |
|
269 TInt rootIndex = aEntryData.FindF( KValueFieldSeparator ); |
|
270 if( rootIndex > 0 ) |
|
271 { |
|
272 TPtrC8 root( aEntryData.Left( rootIndex ) ); |
|
273 TInt docIndex = aEntryData.FindF( KPageFileSeparator ); |
|
274 TPtrC8 doc( aEntryData.Mid( rootIndex + 1, docIndex - ( rootIndex + 1 ) ) ); |
|
275 TPtrC8 pos( aEntryData.Mid( docIndex + 1, aEntryData.Length() - ( docIndex + 1 ) ) ); |
|
276 TInt index( CXcapCacheServer::ConvertDesc( pos ) ); |
|
277 ret = CXcapCacheIndexTableEntry::NewL( index, root, doc ); |
|
278 } |
|
279 return ret; |
|
280 } |
|
281 |
|
282 |
|
283 |
|
284 |