|
1 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // $Workfile: obexMtmUtil.cpp $ |
|
15 // $Author: Stevep $ |
|
16 // $Revision: 7 $ |
|
17 // $Date: 8/02/02 10:11 $ |
|
18 // |
|
19 // |
|
20 |
|
21 //class include |
|
22 #include "obexMtmUtil.h" |
|
23 #include "obexheaderlist.h" |
|
24 |
|
25 #ifdef _DEBUG |
|
26 #define DEBPRINT(Z) User::InfoPrint(Z); |
|
27 #else |
|
28 #define DEBPRINT(Z) |
|
29 #endif |
|
30 |
|
31 // Uncomment this to test data compatibility with builds older than PHAR-5SDJG9. |
|
32 // (if uncommented, messages are externalised the same way it was done before the PHAR-5SDJG9 changes) |
|
33 //#define TEST_INC042468 |
|
34 |
|
35 static const TInt KObexArrayGranularity = 4; |
|
36 |
|
37 // |
|
38 // |
|
39 // Class for externalising a set of filenames to the same message entry store. Rolls back if not explicitely committed. |
|
40 // |
|
41 |
|
42 // Static functions that do the whole process in one go: |
|
43 EXPORT_C void CObexMtmFileNameExternaliser::ExternaliseAndCommit1FileNameL(CMsvEntry* aEntry, const TDesC16* aFileName) |
|
44 /** |
|
45 * Static function to write one and only one filename to the message store, then finalise and commit it. This function |
|
46 * should be called ONCE ONLY. To externalise more than one filename, instantiate this class and use ExternaliseFileNameL() |
|
47 * followed by CommitStoreL(). |
|
48 * @param aEntry the entry whose store is to be written to |
|
49 * @param aFileName The filename to write to the store |
|
50 * @leave KErrXXX system-wide error codes |
|
51 */ |
|
52 { |
|
53 CMsvStore* messageStore = aEntry->EditStoreL(); |
|
54 CleanupStack::PushL(messageStore); |
|
55 |
|
56 RMsvWriteStream messageWriteStream; |
|
57 CleanupClosePushL(messageWriteStream); |
|
58 |
|
59 messageWriteStream.AssignL(*messageStore, KObexFileNameStreamUid); |
|
60 |
|
61 // write the identifier so we know we are internalizing a stream later |
|
62 #ifndef TEST_INC042468 |
|
63 messageWriteStream.WriteInt32L(KObexFileNameStreamFileNameUid); |
|
64 #endif |
|
65 messageWriteStream.WriteInt32L(aFileName->Length()); |
|
66 messageWriteStream.WriteL(*aFileName); |
|
67 |
|
68 #ifndef TEST_INC042468 |
|
69 messageWriteStream.WriteInt32L(KObexFileNameStreamFileNameUid); |
|
70 #endif |
|
71 messageWriteStream.WriteInt32L(0); //Mark the end of the filename list |
|
72 |
|
73 messageWriteStream.CommitL(); |
|
74 |
|
75 CleanupStack::PopAndDestroy(); // trigger a call to Close(); on messageWriteStream as well as removing from the stack |
|
76 |
|
77 messageStore->CommitL(); |
|
78 CleanupStack::PopAndDestroy(messageStore); |
|
79 } |
|
80 |
|
81 |
|
82 EXPORT_C void CObexMtmFileNameExternaliser::Internalise1FileNameL(CMsvEntry* aEntry, TDes16* aFileName) |
|
83 /** |
|
84 * Static function to read in one filename from the store. |
|
85 * |
|
86 * @param aEntry entry to read the filename from |
|
87 * @param aFileName Descriptor to recieve the new filename. Any existing data will be overwritten. MUST be long enough to contain the name. |
|
88 * @leave KErrXXX system wide error codes. |
|
89 */ |
|
90 { |
|
91 CMsvStore* messageStore = aEntry->ReadStoreL(); |
|
92 CleanupStack::PushL(messageStore); |
|
93 |
|
94 RMsvReadStream messageReadStream; |
|
95 messageReadStream.OpenLC(*messageStore, KObexFileNameStreamUid); // If data hasn't been stored then this will provide the error. |
|
96 |
|
97 // verify that we're reading the right type of data from the stream |
|
98 |
|
99 |
|
100 // INC042468: "AV21: Data Compatibility break caused by "PHAR-5SDJG9"" |
|
101 // To regain compatibility, if the first integer read from the stream |
|
102 // is not the expected "KObexFileNameStreamFileNameUid", it is assumed it |
|
103 // is the filename length. An extra check is made that it is a valid filename |
|
104 // length (<= KMaxFileName). |
|
105 // "KObexFileNameStreamFileNameUid" has also been modified so that there cannot |
|
106 // be a file with a name length equal to KObexFileNameStreamFileNameUid (in which |
|
107 // case we would have been in trouble ;-) ) |
|
108 |
|
109 TInt32 firstInt = messageReadStream.ReadInt32L(); |
|
110 TInt32 fileNameLength = 0; |
|
111 if (firstInt == KObexFileNameStreamFileNameUid) |
|
112 { |
|
113 // next will be the filename length |
|
114 fileNameLength = messageReadStream.ReadInt32L(); |
|
115 } |
|
116 else if (firstInt <= KMaxFileName) |
|
117 { |
|
118 // let's assume the first integer in the stream was the filename length |
|
119 // (this happens if the message was saved by an old build (previous to PHAR-5SDJG9) |
|
120 fileNameLength = firstInt; |
|
121 } |
|
122 else |
|
123 { |
|
124 // neither the expected "KObexFileNameStreamFileNameUid" nor a valid filename length |
|
125 User::Leave(KErrArgument); |
|
126 } |
|
127 |
|
128 messageReadStream.ReadL(*aFileName, fileNameLength); |
|
129 |
|
130 CleanupStack::PopAndDestroy(2, messageStore); // messageStore, messageReadStream (calls Close() first) |
|
131 } |
|
132 |
|
133 // |
|
134 // |
|
135 // Class for storing multiple Filenames within a CMsvEntry store. |
|
136 // |
|
137 EXPORT_C CObexMtmFileNameExternaliser* CObexMtmFileNameExternaliser::NewL(CMsvSession* aSession, TMsvId aId) |
|
138 /** |
|
139 * Canonical NewL factory function. |
|
140 * |
|
141 * @param aSession A messaging server session |
|
142 * @param aId TMsvId of the entry to externalise/internalise to/from. This association is fixed for the lifetime of this instance of the class. |
|
143 * @return a newly constucted CObexMtmFileNameExternaliser |
|
144 * @leave KErrXXX system wide error codes |
|
145 */ |
|
146 { |
|
147 CObexMtmFileNameExternaliser* self = new(ELeave) CObexMtmFileNameExternaliser(aSession); |
|
148 CleanupStack::PushL(self); |
|
149 self->InitialiseStoreL(aId); |
|
150 CleanupStack::Pop(); // self |
|
151 return self; |
|
152 } |
|
153 CObexMtmFileNameExternaliser::CObexMtmFileNameExternaliser(CMsvSession* aSession) : iSession(aSession) |
|
154 /** |
|
155 * Constructor |
|
156 * |
|
157 * @param aSession A messaging server session |
|
158 */ |
|
159 {} |
|
160 CObexMtmFileNameExternaliser::~CObexMtmFileNameExternaliser() |
|
161 /** |
|
162 * Destructor. Will revert the stream to its original state if the store hasn't been comitted. |
|
163 */ |
|
164 { |
|
165 if(! iHaveCommitted) |
|
166 { |
|
167 iMessageWriteStream.Close(); |
|
168 iMessageStore->Revert(); // Don't commit. Should have been called explicitly using CommitStoreL(). |
|
169 } |
|
170 delete iMessageStore; |
|
171 delete iEntry; |
|
172 } |
|
173 |
|
174 void CObexMtmFileNameExternaliser::InitialiseStoreL(TMsvId aId) |
|
175 /** |
|
176 * Initialises the store of the given entry, ready for reading or writing. |
|
177 * |
|
178 * @param aId The TMsvId of the entry whose store should be initialised. |
|
179 * @leave KErrXXX system wide error codes. |
|
180 */ |
|
181 { |
|
182 iEntry = iSession->GetEntryL(aId); |
|
183 |
|
184 iMessageStore = iEntry->EditStoreL(); |
|
185 |
|
186 iMessageWriteStream.AssignL(*iMessageStore, KObexFileNameStreamUid); |
|
187 iHaveCommitted = EFalse; |
|
188 } |
|
189 |
|
190 EXPORT_C void CObexMtmFileNameExternaliser::ExternaliseFileNameL(const TDesC16* aFileName) |
|
191 /** |
|
192 * Externalises a single filename to the associated entry's message store. |
|
193 * |
|
194 * @param aFileName the filename to externalise. |
|
195 * @leave KErrXXX system-wide error codes. |
|
196 */ |
|
197 { |
|
198 #ifndef TEST_INC042468 |
|
199 iMessageWriteStream.WriteInt32L(KObexFileNameStreamFileNameUid); |
|
200 #endif |
|
201 iMessageWriteStream.WriteInt32L(aFileName->Length()); |
|
202 iMessageWriteStream.WriteL(*aFileName); |
|
203 } |
|
204 |
|
205 EXPORT_C void CObexMtmFileNameExternaliser::ExternaliseFileNameAndHeadersL(const TDesC16* aFileName, const CObexHeaderList* aHeaderList) |
|
206 /** |
|
207 * Externalises a single filename to the associated entry's message store. |
|
208 * |
|
209 * @param aFileName the filename to externalise. |
|
210 * @leave KErrXXX system-wide error codes. |
|
211 */ |
|
212 { |
|
213 ExternaliseFileNameL(aFileName); |
|
214 #ifndef TEST_INC042468 |
|
215 iMessageWriteStream.WriteInt32L(KObexFileNameStreamHeaderListUid); |
|
216 aHeaderList->ExternalizeL(iMessageWriteStream); |
|
217 #endif |
|
218 } |
|
219 |
|
220 EXPORT_C void CObexMtmFileNameExternaliser::CommitStoreL() |
|
221 /** |
|
222 * Finalises and commits the store. This function MUST be called once all of the filenames have been externalised, and not |
|
223 * before. If it is not called, any changes will be lost and the store will revert to its former state. |
|
224 * |
|
225 * @leave KErrXXX system wide error codes. |
|
226 */ |
|
227 { |
|
228 #ifndef TEST_INC042468 |
|
229 iMessageWriteStream.WriteInt32L(KObexFileNameStreamFileNameUid); |
|
230 #endif |
|
231 iMessageWriteStream.WriteInt32L(0); // length == 0 --> no more names in store. |
|
232 iMessageWriteStream.CommitL(); |
|
233 iHaveCommitted = ETrue; |
|
234 iMessageWriteStream.Close(); |
|
235 |
|
236 iMessageStore->CommitL(); |
|
237 |
|
238 delete iMessageStore; |
|
239 iMessageStore=NULL; |
|
240 } |
|
241 |
|
242 EXPORT_C RObexMtmFileNameWithHeadersArray* CObexMtmFileNameExternaliser::InternaliseFileNamesLC(CMsvStore& aMessageStore) |
|
243 /** |
|
244 * Static function to get the filenames stored within the message entry. The returned array is pushed onto the cleanup |
|
245 * stack. |
|
246 * |
|
247 * @param aMessageStore Store from which the names are to be read |
|
248 * @return Array of filenames, pushed onto the cleanup stack. |
|
249 * @leave System wide error codes |
|
250 */ |
|
251 { |
|
252 RObexMtmFileNameWithHeadersArray* fileNameArray = new(ELeave) RObexMtmFileNameWithHeadersArray(KObexArrayGranularity); |
|
253 CleanupStack::PushL(fileNameArray); |
|
254 |
|
255 RMsvReadStream newMessageReadStream; |
|
256 newMessageReadStream.OpenL(aMessageStore, KObexFileNameStreamUid); // If data hasn't been stored then this will provide the error. |
|
257 CleanupClosePushL(newMessageReadStream); |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 // INC042468: "AV21: Data Compatibility break caused by "PHAR-5SDJG9"" |
|
263 // To regain compatibility, if the first integer read from the stream |
|
264 // is not the expected "KObexFileNameStreamFileNameUid", it is assumed it |
|
265 // is the filename length. An extra check is made that it is a valid filename |
|
266 // length (<= KMaxFileName). |
|
267 // "KObexFileNameStreamFileNameUid" has also been modified so that there cannot |
|
268 // be a file with a name length equal to KObexFileNameStreamFileNameUid (in which |
|
269 // case we would have been in trouble ;-) ) |
|
270 |
|
271 TInt32 firstInt = newMessageReadStream.ReadInt32L(); |
|
272 |
|
273 TInt32 fileNameLength = 0; |
|
274 if (firstInt == KObexFileNameStreamFileNameUid) |
|
275 { |
|
276 // next will be the filename length |
|
277 fileNameLength = newMessageReadStream.ReadInt32L(); |
|
278 } |
|
279 else if (firstInt <= KMaxFileName) |
|
280 { |
|
281 // let's assume the first integer in the stream was the filename length |
|
282 // (this happens if the message was saved by an old build (previous to PHAR-5SDJG9) |
|
283 fileNameLength = firstInt; |
|
284 } |
|
285 else |
|
286 { |
|
287 // neither the expected "KObexFileNameStreamFileNameUid" nor a valid filename length |
|
288 User::Leave(KErrArgument); |
|
289 } |
|
290 |
|
291 TInt32 elementTypeID; |
|
292 TBool lengthAlreadyReadFlag; |
|
293 TFileName thisName; |
|
294 CObexMtmFileNameWithHeaders *thisPackage; |
|
295 while(fileNameLength) |
|
296 { |
|
297 |
|
298 thisPackage = CObexMtmFileNameWithHeaders::NewL(); |
|
299 CleanupStack::PushL(thisPackage); |
|
300 |
|
301 // read 'bodylength' bytes into a descriptor |
|
302 newMessageReadStream.ReadL(thisName, fileNameLength); |
|
303 |
|
304 // store this descriptor in the container |
|
305 thisPackage->iFileName = new(ELeave) TFileName(thisName); |
|
306 CleanupStack::PushL(thisPackage->iFileName); |
|
307 |
|
308 lengthAlreadyReadFlag = EFalse; |
|
309 // get the type of the next element |
|
310 elementTypeID = newMessageReadStream.ReadInt32L(); |
|
311 |
|
312 switch(elementTypeID) |
|
313 { |
|
314 case KObexFileNameStreamHeaderListUid: |
|
315 // this filename has some headers associated with it |
|
316 |
|
317 // read these headers in... |
|
318 thisPackage->iHeaderList = CObexHeaderList::NewLC(); |
|
319 thisPackage->iHeaderList->InternalizeL(newMessageReadStream); |
|
320 |
|
321 // get the type of the next element |
|
322 elementTypeID = newMessageReadStream.ReadInt32L(); |
|
323 |
|
324 // fall through to add this container to the array |
|
325 case KObexFileNameStreamFileNameUid: |
|
326 // this filename has no headers associated with it, this is OK. |
|
327 fileNameArray->Append(thisPackage); |
|
328 break; |
|
329 |
|
330 default: |
|
331 // builds previous to PHAR-5SDJG9: |
|
332 // no stream Uids are externalised and elementTypeID contains already the filename length |
|
333 if (elementTypeID <= KMaxFileName) |
|
334 { |
|
335 fileNameArray->Append(thisPackage); |
|
336 fileNameLength = elementTypeID; |
|
337 lengthAlreadyReadFlag = ETrue; |
|
338 } |
|
339 else |
|
340 { |
|
341 User::Leave(KErrArgument); |
|
342 } |
|
343 // if this value is not one of the two element types, then we can't read the stream |
|
344 |
|
345 break; |
|
346 } |
|
347 |
|
348 // this member will be zero if there were no headers on this filename |
|
349 if(thisPackage->iHeaderList) |
|
350 { |
|
351 CleanupStack::Pop(thisPackage->iHeaderList); |
|
352 } |
|
353 |
|
354 // safely added filename and headers to list, pop the allocations off the cleanup stack. |
|
355 CleanupStack::Pop(thisPackage->iFileName); |
|
356 |
|
357 CleanupStack::Pop(thisPackage); |
|
358 |
|
359 // get the length of the next element |
|
360 if (!lengthAlreadyReadFlag) |
|
361 { |
|
362 TRAPD (err, fileNameLength = newMessageReadStream.ReadInt32L();) // Length == 0 --> end of array |
|
363 } |
|
364 } |
|
365 |
|
366 newMessageReadStream.Close(); |
|
367 CleanupStack::PopAndDestroy(); //newMessageReadStream |
|
368 |
|
369 return fileNameArray; |
|
370 } |
|
371 |
|
372 // |
|
373 // CObexMtmFileNameWithHeaders |
|
374 // |
|
375 |
|
376 EXPORT_C CObexMtmFileNameWithHeaders* CObexMtmFileNameWithHeaders::NewL() |
|
377 /** |
|
378 * Canonical NewL factory function. |
|
379 * |
|
380 * @return an empty CObexMtmFileNameWithHeaders class |
|
381 * @leave KErrXXX system wide error codes |
|
382 */ |
|
383 { |
|
384 CObexMtmFileNameWithHeaders *self = new(ELeave)CObexMtmFileNameWithHeaders; |
|
385 return self; |
|
386 } |
|
387 |
|
388 CObexMtmFileNameWithHeaders::~CObexMtmFileNameWithHeaders() |
|
389 /** |
|
390 * Destructor. Will destroy the data pointed to by the 2 member pointers. |
|
391 */ |
|
392 { |
|
393 delete iFileName; |
|
394 delete iHeaderList; |
|
395 } |
|
396 |
|
397 CObexMtmFileNameWithHeaders::CObexMtmFileNameWithHeaders() |
|
398 /** |
|
399 * Constructor. |
|
400 */ |
|
401 { |
|
402 } |
|
403 |
|
404 |