|
1 /* |
|
2 * Copyright (c) 2009 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: File manager for managing file copy op |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "cmrfilemanager.h" |
|
19 #include "esmrhelper.h" |
|
20 #include <f32file.h> |
|
21 |
|
22 /// LOCAL NAMESPACE |
|
23 namespace |
|
24 { |
|
25 // File copying block size |
|
26 const TInt KWriteBlockSize( 1024 ); |
|
27 } |
|
28 |
|
29 // --------------------------------------------------------------------------- |
|
30 // CMRFileManager::NewL |
|
31 // --------------------------------------------------------------------------- |
|
32 // |
|
33 CMRFileManager* CMRFileManager::NewL( RFs& aFs ) |
|
34 { |
|
35 CMRFileManager* self = new ( ELeave ) CMRFileManager( aFs ); |
|
36 CleanupStack::PushL( self ); |
|
37 self->ConstructL(); |
|
38 CleanupStack::Pop( self ); |
|
39 return self; |
|
40 } |
|
41 |
|
42 // --------------------------------------------------------------------------- |
|
43 // CMRFileManager::~CMRFileManager |
|
44 // --------------------------------------------------------------------------- |
|
45 // |
|
46 CMRFileManager::~CMRFileManager() |
|
47 { |
|
48 delete iFileArray; |
|
49 delete iCurrentFilename; |
|
50 delete iTempFilename; |
|
51 delete iOutputFileArray; |
|
52 iReadStream.Close(); |
|
53 iWriteStream.Close(); |
|
54 } |
|
55 |
|
56 // --------------------------------------------------------------------------- |
|
57 // CMRFileManager::CMRFileManager |
|
58 // --------------------------------------------------------------------------- |
|
59 // |
|
60 CMRFileManager::CMRFileManager( RFs& aFs ) |
|
61 : CActive( CActive::EPriorityStandard ), iFs( aFs ) |
|
62 { |
|
63 |
|
64 } |
|
65 |
|
66 // --------------------------------------------------------------------------- |
|
67 // CMRFileManager::ConstructL |
|
68 // --------------------------------------------------------------------------- |
|
69 // |
|
70 void CMRFileManager::ConstructL() |
|
71 { |
|
72 CActiveScheduler::Add( this ); |
|
73 } |
|
74 |
|
75 // --------------------------------------------------------------------------- |
|
76 // CMRFileManager::CopyFilesL |
|
77 // --------------------------------------------------------------------------- |
|
78 // |
|
79 void CMRFileManager::CopyFilesL( MDesC16Array& aFilenameArray ) |
|
80 { |
|
81 // Take copy of the original filename array |
|
82 TInt count( aFilenameArray.MdcaCount() ); |
|
83 if( iFileArray ) |
|
84 { |
|
85 delete iFileArray; |
|
86 iFileArray = NULL; |
|
87 } |
|
88 iFileArray = new (ELeave) CDesCArrayFlat( count ); |
|
89 for( TInt i = 0; i < count; ++i ) |
|
90 { |
|
91 iFileArray->AppendL( aFilenameArray.MdcaPoint( i ) ); |
|
92 } |
|
93 |
|
94 if( iOutputFileArray ) |
|
95 { |
|
96 delete iOutputFileArray; |
|
97 iOutputFileArray = NULL; |
|
98 } |
|
99 iOutputFileArray = new (ELeave) CDesCArrayFlat( count ); |
|
100 if( iCurrentFilename ) |
|
101 { |
|
102 delete iCurrentFilename; |
|
103 iCurrentFilename = NULL; |
|
104 } |
|
105 |
|
106 PrepareNextFileL(); |
|
107 |
|
108 iState = ECopyFile; |
|
109 AsyncOp(); |
|
110 } |
|
111 |
|
112 // --------------------------------------------------------------------------- |
|
113 // CMRFileManager::CopyFileL |
|
114 // --------------------------------------------------------------------------- |
|
115 // |
|
116 void CMRFileManager::CopyFileL( |
|
117 const TDesC& aSourceFilename, |
|
118 const TDesC& aDestFilename ) |
|
119 { |
|
120 Cancel(); |
|
121 |
|
122 iReadStream.Close(); |
|
123 iWriteStream.Close(); |
|
124 iReadStream.Open( iFs, aSourceFilename, EFileShareReadersOnly ); |
|
125 iWriteStream.Open( iFs, aDestFilename, EFileWrite|EFileShareAny ); |
|
126 |
|
127 if ( iObserver ) |
|
128 { |
|
129 iObserver->NotifyStartL( aSourceFilename ); |
|
130 } |
|
131 |
|
132 iState = ECopyFile; |
|
133 |
|
134 AsyncOp(); |
|
135 } |
|
136 |
|
137 // --------------------------------------------------------------------------- |
|
138 // CMRFileManager::CopyFileL |
|
139 // --------------------------------------------------------------------------- |
|
140 // |
|
141 void CMRFileManager::CopyFileL( RFile& aSource, RFile& aDest ) |
|
142 { |
|
143 Cancel(); |
|
144 |
|
145 iReadStream.Close(); |
|
146 iWriteStream.Close(); |
|
147 iReadStream.Attach( aSource ); |
|
148 iWriteStream.Attach( aDest ); |
|
149 |
|
150 if ( iObserver ) |
|
151 { |
|
152 TFileName name; |
|
153 |
|
154 if ( KErrNone == aSource.Name( name ) ) |
|
155 { |
|
156 iObserver->NotifyStartL( name ); |
|
157 } |
|
158 } |
|
159 |
|
160 iState = ECopyFile; |
|
161 AsyncOp(); |
|
162 } |
|
163 |
|
164 // --------------------------------------------------------------------------- |
|
165 // CMRFileManager::CancelOp |
|
166 // --------------------------------------------------------------------------- |
|
167 // |
|
168 void CMRFileManager::CancelOp() |
|
169 { |
|
170 iState = ECancelled; |
|
171 } |
|
172 |
|
173 // --------------------------------------------------------------------------- |
|
174 // CMRFileManager::PrepareNextFileL |
|
175 // --------------------------------------------------------------------------- |
|
176 // |
|
177 void CMRFileManager::PrepareNextFileL() |
|
178 { |
|
179 // Take first item as current |
|
180 if( iCurrentFilename ) |
|
181 { |
|
182 delete iCurrentFilename; |
|
183 iCurrentFilename = NULL; |
|
184 } |
|
185 iCurrentFilename = (*iFileArray)[ 0 ].AllocL(); |
|
186 |
|
187 |
|
188 TParse parser; |
|
189 parser.Set( *iCurrentFilename , NULL, NULL ); |
|
190 TFileName attachmentName( parser.NameAndExt() ); |
|
191 |
|
192 User::LeaveIfError( ESMRHelper::CreateAndAppendPrivateDirToFileName( attachmentName ) ); |
|
193 |
|
194 iReadStream.Close(); |
|
195 iWriteStream.Close(); |
|
196 User::LeaveIfError( iWriteStream.Replace( iFs, attachmentName, EFileWrite|EFileShareAny ) ); |
|
197 iReadStream.Open( iFs, *iCurrentFilename, EFileRead ); |
|
198 iOutputFileArray->AppendL( attachmentName ); |
|
199 |
|
200 delete iTempFilename; |
|
201 iTempFilename = NULL; |
|
202 iTempFilename = attachmentName.AllocL(); |
|
203 |
|
204 if( iObserver ) |
|
205 { |
|
206 iObserver->NotifyStartL( *iCurrentFilename ); |
|
207 } |
|
208 } |
|
209 |
|
210 // --------------------------------------------------------------------------- |
|
211 // CMRFileManager::SetObserver |
|
212 // --------------------------------------------------------------------------- |
|
213 // |
|
214 void CMRFileManager::SetObserver( MMRFileManObserver& aObserver ) |
|
215 { |
|
216 iObserver = &aObserver; |
|
217 } |
|
218 |
|
219 // --------------------------------------------------------------------------- |
|
220 // CMRFileManager::RunL |
|
221 // --------------------------------------------------------------------------- |
|
222 // |
|
223 void CMRFileManager::RunL() |
|
224 { |
|
225 if( iStatus.Int() != KErrNone ) |
|
226 { |
|
227 iReadStream.Close(); |
|
228 iWriteStream.Close(); |
|
229 |
|
230 if( iObserver ) |
|
231 { |
|
232 iObserver->NotifyError( iStatus.Int() ); |
|
233 } |
|
234 return; |
|
235 } |
|
236 switch( iState ) |
|
237 { |
|
238 case ECopyFile: |
|
239 { |
|
240 CopyFileL(); |
|
241 |
|
242 if( iObserver ) |
|
243 { |
|
244 iObserver->NotifyProgress( KWriteBlockSize ); |
|
245 } |
|
246 AsyncOp(); |
|
247 break; |
|
248 } |
|
249 case ECancelled: |
|
250 { |
|
251 if( iObserver ) |
|
252 { |
|
253 iReadStream.Close(); |
|
254 iWriteStream.Close(); |
|
255 // Delete last because it was cancelled |
|
256 if ( iOutputFileArray ) |
|
257 { |
|
258 iOutputFileArray->Delete( iOutputFileArray->Count() - 1 ); |
|
259 } |
|
260 iObserver->NotifyEnd(); |
|
261 iState = EDone; |
|
262 } |
|
263 break; |
|
264 } |
|
265 case EDone: |
|
266 { |
|
267 break; |
|
268 } |
|
269 } |
|
270 } |
|
271 |
|
272 // --------------------------------------------------------------------------- |
|
273 // CMRFileManager::RunError |
|
274 // --------------------------------------------------------------------------- |
|
275 // |
|
276 TInt CMRFileManager::RunError( TInt aError ) |
|
277 { |
|
278 if ( aError == KErrEof ) // File has been copied fully |
|
279 { |
|
280 aError = KErrNone; |
|
281 if( iFileArray && iFileArray->Count() > 1 ) |
|
282 { |
|
283 iFileArray->Delete( 0 ); |
|
284 |
|
285 // Start new file copy op |
|
286 TRAP( aError, |
|
287 { |
|
288 PrepareNextFileL(); |
|
289 AsyncOp(); |
|
290 } ) |
|
291 } |
|
292 else |
|
293 { |
|
294 if( iObserver ) |
|
295 { |
|
296 iReadStream.Close(); |
|
297 iWriteStream.Close(); |
|
298 |
|
299 iObserver->NotifyEnd(); |
|
300 iState = EDone; |
|
301 } |
|
302 } |
|
303 } |
|
304 |
|
305 if ( aError ) |
|
306 { |
|
307 iReadStream.Close(); |
|
308 iWriteStream.Close(); |
|
309 |
|
310 if( iObserver ) |
|
311 { |
|
312 iObserver->NotifyError( aError ); |
|
313 iState = EDone; |
|
314 } |
|
315 } |
|
316 |
|
317 return KErrNone; |
|
318 } |
|
319 |
|
320 // --------------------------------------------------------------------------- |
|
321 // CMRFileManager::DoCancel |
|
322 // --------------------------------------------------------------------------- |
|
323 // |
|
324 void CMRFileManager::DoCancel() |
|
325 { |
|
326 iReadStream.Close(); |
|
327 iWriteStream.Close(); |
|
328 delete iFileArray; |
|
329 iFileArray = NULL; |
|
330 delete iOutputFileArray; |
|
331 iOutputFileArray = NULL; |
|
332 iFileIndex = KErrNotFound; |
|
333 delete iCurrentFilename; |
|
334 iCurrentFilename = NULL; |
|
335 delete iTempFilename; |
|
336 iTempFilename = NULL; |
|
337 } |
|
338 |
|
339 // --------------------------------------------------------------------------- |
|
340 // CMRFileManager::AsyncOp |
|
341 // --------------------------------------------------------------------------- |
|
342 // |
|
343 void CMRFileManager::AsyncOp() |
|
344 { |
|
345 SetActive(); |
|
346 TRequestStatus *stat = &iStatus; |
|
347 User::RequestComplete( stat, KErrNone ); |
|
348 } |
|
349 |
|
350 // --------------------------------------------------------------------------- |
|
351 // CMRFileManager::CopyFileL |
|
352 // --------------------------------------------------------------------------- |
|
353 // |
|
354 void CMRFileManager::CopyFileL() |
|
355 { |
|
356 iWriteStream.WriteL( iReadStream, KWriteBlockSize ); |
|
357 } |
|
358 |
|
359 // --------------------------------------------------------------------------- |
|
360 // CMRFileManager::CopiedFiles |
|
361 // --------------------------------------------------------------------------- |
|
362 // |
|
363 MDesC16Array& CMRFileManager::CopiedFiles() const |
|
364 { |
|
365 return *iOutputFileArray; |
|
366 } |
|
367 // End of file |
|
368 |