|
1 // Copyright (c) 2000-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 // |
|
15 |
|
16 #include "CCopyOneFile.h" |
|
17 |
|
18 const TInt KCopyFileBufferSize=32768; |
|
19 |
|
20 CCopyOneFile* CCopyOneFile::NewL(RFs &aFs) |
|
21 { |
|
22 CCopyOneFile *self=new (ELeave) CCopyOneFile(aFs); |
|
23 CleanupStack::PushL(self); |
|
24 self->ConstructL(); |
|
25 CActiveScheduler::Add(self); |
|
26 CleanupStack::Pop(); |
|
27 return(self); |
|
28 } |
|
29 |
|
30 void CCopyOneFile::Copy(const TFileName &aFrom, const TFileName &aTo,TRequestStatus& aStatus) |
|
31 { |
|
32 aStatus=KRequestPending; |
|
33 iReportStatus=&aStatus; |
|
34 TInt error=iFrom.Open(iFs,aFrom,EFileRead|EFileShareReadersOnly); |
|
35 if(error==KErrNone) |
|
36 { |
|
37 error=iTo.Replace(iFs,aTo,EFileWrite|EFileShareExclusive); |
|
38 if(error==KErrNone) |
|
39 { |
|
40 iFrom.Read(*iFileBuffer,KCopyFileBufferSize,iStatus); |
|
41 iReading=ETrue; |
|
42 SetActive(); |
|
43 } |
|
44 } |
|
45 if(error!=KErrNone) |
|
46 Complete(error); |
|
47 } |
|
48 |
|
49 void CCopyOneFile::Copy(RFile& aFrom, RFile& aTo, TRequestStatus& aStatus) |
|
50 { |
|
51 aStatus=KRequestPending; |
|
52 iReportStatus=&aStatus; |
|
53 |
|
54 iFrom = aFrom; |
|
55 iTo = aTo; |
|
56 |
|
57 iFrom.Read(*iFileBuffer,KCopyFileBufferSize,iStatus); |
|
58 iReading=ETrue; |
|
59 SetActive(); |
|
60 } |
|
61 |
|
62 void CCopyOneFile::RunL() |
|
63 { |
|
64 User::LeaveIfError(iStatus.Int()); |
|
65 |
|
66 if(iReading==EFalse) |
|
67 { |
|
68 if(iFileBuffer->Length()<KCopyFileBufferSize) |
|
69 // didn't read a full buffer last time, so its the end |
|
70 Complete(KErrNone); |
|
71 else |
|
72 { |
|
73 iFrom.Read(*iFileBuffer,iStatus); |
|
74 iReading=ETrue; |
|
75 SetActive(); |
|
76 } |
|
77 } |
|
78 else |
|
79 { |
|
80 iTo.Write(*iFileBuffer,iStatus); |
|
81 iReading=EFalse; |
|
82 SetActive(); |
|
83 } |
|
84 } |
|
85 |
|
86 void CCopyOneFile::DoCancel() |
|
87 { |
|
88 Complete(KErrCancel); |
|
89 } |
|
90 |
|
91 TInt CCopyOneFile::RunError(TInt aError) |
|
92 { |
|
93 Complete(aError); |
|
94 return(KErrNone); |
|
95 } |
|
96 |
|
97 void CCopyOneFile::ConstructL() |
|
98 { |
|
99 iFileBuffer= new (ELeave) TBuf8<KCopyFileBufferSize>; |
|
100 } |
|
101 |
|
102 CCopyOneFile::CCopyOneFile(RFs &aFs) : CActive(EPriorityStandard), iFs(aFs) |
|
103 { |
|
104 } |
|
105 |
|
106 CCopyOneFile::~CCopyOneFile() |
|
107 { |
|
108 Cancel(); |
|
109 delete iFileBuffer; |
|
110 } |
|
111 |
|
112 void CCopyOneFile::Complete(TInt aError) |
|
113 { |
|
114 iFrom.Close(); |
|
115 iTo.Close(); |
|
116 if(iReportStatus) |
|
117 User::RequestComplete(iReportStatus,aError); |
|
118 } |
|
119 |