|
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 "CCopyFiles.h" |
|
17 #include "CCopyOneFile.h" |
|
18 |
|
19 |
|
20 |
|
21 CCopyFiles* CCopyFiles::NewL(RFs &aFs) |
|
22 { |
|
23 CCopyFiles *self=new (ELeave) CCopyFiles(aFs); |
|
24 CleanupStack::PushL(self); |
|
25 self->ConstructL(); |
|
26 CActiveScheduler::Add(self); |
|
27 CleanupStack::Pop(); |
|
28 return(self); |
|
29 } |
|
30 |
|
31 void CCopyFiles::CopyDir(const TFileName &aFrom, const TFileName &aTo,TRequestStatus& aStatus) |
|
32 { |
|
33 aStatus=KRequestPending; |
|
34 iReportStatus=&aStatus; |
|
35 iFromPath=&aFrom; |
|
36 iToPath=&aTo; |
|
37 iStartedCopy=EFalse; |
|
38 delete iNext; |
|
39 iNext=NULL; |
|
40 iPos=0; |
|
41 TRequestStatus *status=&iStatus; |
|
42 User::RequestComplete(status,KErrNone); |
|
43 SetActive(); |
|
44 } |
|
45 |
|
46 |
|
47 void CCopyFiles::RunL() |
|
48 { |
|
49 if(iStartedCopy==EFalse) |
|
50 { |
|
51 iStartedCopy=ETrue; |
|
52 iDirScan->SetScanDataL(*iFromPath,KEntryAttNormal,ESortNone); |
|
53 } |
|
54 |
|
55 if(iStatus!=KErrNone) |
|
56 User::RequestComplete(iReportStatus,iStatus.Int()); |
|
57 else |
|
58 if(iNext!=NULL && iPos<iNext->Count()) |
|
59 CopyFileL(); |
|
60 else |
|
61 NextDirL(); |
|
62 } |
|
63 |
|
64 |
|
65 void CCopyFiles::CopyFileL() |
|
66 { |
|
67 /* If you are copying c:\System\ to c:\copyto\ |
|
68 AbbrevitedPath() would return \Apps\Shell\ |
|
69 e.iName would contain Shell.ini |
|
70 and FullPath would contain c:\System\Apps\Shell\ |
|
71 so we copy from <FullPath><e.iName> |
|
72 to <Target dir><AbbriviatedPath><e.iName> |
|
73 it overflows if the target would be greater than 256 characters |
|
74 which is the maximum length of a filepath in epoc */ |
|
75 |
|
76 const TEntry& e = (*iNext)[iPos]; |
|
77 iTo->Zero(); |
|
78 TPtrC abrevPath=iDirScan->AbbreviatedPath(); |
|
79 if((*iToPath).Length() + abrevPath.Length()+e.iName.Length()> iTo->MaxLength()) |
|
80 User::Leave(KErrOverflow); |
|
81 iTo->Append(*iToPath); |
|
82 if((*iTo)[iTo->Length()-1]=='\\') |
|
83 iTo->SetLength(iTo->Length()-1); |
|
84 iTo->Append(abrevPath); |
|
85 iTo->Append(e.iName); |
|
86 |
|
87 iFrom->Zero(); |
|
88 TPtrC fullPath=iDirScan->FullPath(); |
|
89 if(fullPath.Length()+e.iName.Length()> iFrom->MaxLength()) |
|
90 User::Leave(KErrOverflow); |
|
91 |
|
92 iFrom->Append(fullPath); |
|
93 iFrom->Append(e.iName); |
|
94 iCopyOneFile->Copy(*iFrom,*iTo,iStatus); |
|
95 iPos++; |
|
96 SetActive(); |
|
97 } |
|
98 |
|
99 |
|
100 |
|
101 void CCopyFiles::NextDirL() |
|
102 { |
|
103 delete iNext; |
|
104 iNext=NULL; |
|
105 iDirScan->NextL(iNext); |
|
106 |
|
107 if(iNext!=NULL) |
|
108 { |
|
109 // Create the target directory, |
|
110 // see comment in void CCopyFiles::CopyFileL() for how this works, |
|
111 iTo->Zero(); |
|
112 TPtrC abrevPath=iDirScan->AbbreviatedPath(); |
|
113 if((*iToPath).Length() + abrevPath.Length() > iTo->MaxLength()) |
|
114 User::Leave(KErrOverflow); |
|
115 iTo->Append(*iToPath); |
|
116 if((*iTo)[iTo->Length()-1]=='\\') |
|
117 iTo->SetLength(iTo->Length()-1); |
|
118 iTo->Append(abrevPath); |
|
119 iFs.MkDirAll(*iTo); |
|
120 |
|
121 // kick off my RunL |
|
122 TRequestStatus *status=&iStatus; |
|
123 User::RequestComplete(status,KErrNone); |
|
124 SetActive(); |
|
125 } |
|
126 else // no more dirs so complete request |
|
127 Complete(KErrNone); |
|
128 } |
|
129 |
|
130 |
|
131 void CCopyFiles::Complete(TInt aError) |
|
132 { |
|
133 delete iNext; |
|
134 iNext=NULL; |
|
135 User::RequestComplete(iReportStatus, aError); |
|
136 } |
|
137 |
|
138 void CCopyFiles::DoCancel() |
|
139 { |
|
140 iCopyOneFile->Cancel(); |
|
141 Complete(KErrCancel); |
|
142 } |
|
143 |
|
144 TInt CCopyFiles::RunError(TInt aError) |
|
145 { |
|
146 if(iReportStatus) |
|
147 User::RequestComplete(iReportStatus,aError); |
|
148 return(KErrNone); |
|
149 } |
|
150 |
|
151 void CCopyFiles::ConstructL() |
|
152 { |
|
153 iDirScan= CDirScan::NewL(iFs); |
|
154 iCopyOneFile=CCopyOneFile::NewL(iFs); |
|
155 iTo=new (ELeave) TFileName; |
|
156 iFrom=new (ELeave) TFileName; |
|
157 } |
|
158 |
|
159 CCopyFiles::CCopyFiles(RFs &aFs) : CActive(EPriorityStandard), iFs(aFs) |
|
160 { |
|
161 } |
|
162 |
|
163 CCopyFiles::~CCopyFiles() |
|
164 { |
|
165 Cancel(); |
|
166 delete iDirScan; |
|
167 delete iCopyOneFile; |
|
168 delete iTo; |
|
169 delete iFrom; |
|
170 } |
|
171 |