|
1 /* |
|
2 * Copyright (c) 2005-2006 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: State machine for renaming files |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "rsfwrenamefilestatemachine.h" |
|
20 #include "rsfwfileentry.h" |
|
21 #include "rsfwfiletable.h" |
|
22 #include "rsfwfileengine.h" |
|
23 #include "rsfwlockmanager.h" |
|
24 #include "mdebug.h" |
|
25 |
|
26 |
|
27 // ---------------------------------------------------------------------------- |
|
28 // CRsfwRenameFileStateMachine::CRsfwRenameFileStateMachine |
|
29 // ---------------------------------------------------------------------------- |
|
30 // |
|
31 CRsfwRenameFileStateMachine::CRsfwRenameFileStateMachine() |
|
32 { |
|
33 } |
|
34 |
|
35 // ---------------------------------------------------------------------------- |
|
36 // CRsfwRenameFileStateMachine::~CRsfwRenameFileStateMachine |
|
37 // ---------------------------------------------------------------------------- |
|
38 // |
|
39 CRsfwRenameFileStateMachine::~CRsfwRenameFileStateMachine() |
|
40 { |
|
41 delete iLockToken; |
|
42 } |
|
43 |
|
44 // ---------------------------------------------------------------------------- |
|
45 // CRsfwRenameFileStateMachine::CompleteRequestL |
|
46 // ---------------------------------------------------------------------------- |
|
47 // |
|
48 CRsfwRfeStateMachine::TState* |
|
49 CRsfwRenameFileStateMachine::CompleteRequestL(TInt aError) |
|
50 { |
|
51 if (iSrcKidCreated) |
|
52 { |
|
53 if (aError) |
|
54 { |
|
55 delete iSrcKidFep; |
|
56 iSrcKidFep = NULL; |
|
57 } |
|
58 } |
|
59 if (iDstKidCreated) |
|
60 { |
|
61 delete iDstKidFep; |
|
62 iDstKidFep = NULL; |
|
63 } |
|
64 |
|
65 // it may happen that the new name is equal to iLastFailedLookup value |
|
66 FileEngine()->ResetFailedLookup(); |
|
67 |
|
68 CompleteAndDestroyState()->SetErrorCode(aError); |
|
69 return CompleteAndDestroyState(); |
|
70 } |
|
71 |
|
72 |
|
73 |
|
74 // Rename the file |
|
75 |
|
76 // ---------------------------------------------------------------------------- |
|
77 // CRsfwRenameFileStateMachine::TRenameFileState::TRenameFileState |
|
78 // ---------------------------------------------------------------------------- |
|
79 // |
|
80 CRsfwRenameFileStateMachine:: |
|
81 TRenameFileState::TRenameFileState(CRsfwRenameFileStateMachine* aParent) |
|
82 : iOperation(aParent) |
|
83 { |
|
84 } |
|
85 |
|
86 // ---------------------------------------------------------------------------- |
|
87 // CRsfwRenameFileStateMachine::TRenameFileState::EnterL |
|
88 // ---------------------------------------------------------------------------- |
|
89 // |
|
90 void CRsfwRenameFileStateMachine::TRenameFileState::EnterL() |
|
91 { |
|
92 // rename or replace |
|
93 TRfeRenameInArgs* inArgs = |
|
94 static_cast<TRfeRenameInArgs*>(iOperation->iInArgs); |
|
95 iOperation->iOverWrite = inArgs->iOverWrite; |
|
96 |
|
97 TFid* dstParentFidp = &(inArgs->iDstFid); |
|
98 TPtrC16 srcKidName(inArgs->iSrcName); |
|
99 iOperation->iDstKidName.Set(inArgs->iDstName); |
|
100 |
|
101 // Get the parent from which we are removing |
|
102 if (!iOperation->Node()) |
|
103 { |
|
104 User::Leave(KErrNotFound); |
|
105 } |
|
106 |
|
107 // Get the parent to which we are removing |
|
108 iOperation->iDstParentFep = |
|
109 iOperation->FileEngine()->iFileTable->Lookup(*dstParentFidp); |
|
110 if (!iOperation->iDstParentFep) |
|
111 { |
|
112 User::Leave(KErrNotFound); |
|
113 } |
|
114 |
|
115 // Do we know the target kid yet? |
|
116 iOperation->iDstKidFep = |
|
117 iOperation->iDstParentFep->FindKidByName(iOperation->iDstKidName); |
|
118 if (!iOperation->iDstKidFep) |
|
119 { |
|
120 // Create a temporary file entry for building the full path |
|
121 iOperation->iDstKidFep = |
|
122 CRsfwFileEntry::NewL(iOperation->iDstKidName, |
|
123 iOperation->iDstParentFep); |
|
124 iOperation->iDstKidCreated = ETrue; |
|
125 } |
|
126 |
|
127 // Do we know the source kid yet? |
|
128 iOperation->iSrcKidFep = iOperation->Node()->FindKidByName(srcKidName); |
|
129 if (!iOperation->iSrcKidFep) |
|
130 { |
|
131 iOperation->iSrcKidFep = CRsfwFileEntry::NewL(srcKidName, |
|
132 iOperation->Node()); |
|
133 iOperation->iSrcKidCreated = ETrue; |
|
134 } |
|
135 |
|
136 if (!iOperation->FileEngine()->Disconnected()) |
|
137 { |
|
138 // Do the rename |
|
139 HBufC* srcKidPath = |
|
140 iOperation->FileEngine()->FullNameLC(*iOperation->iSrcKidFep); |
|
141 if ((*iOperation->iSrcKidFep).Type() == KNodeTypeDir) |
|
142 { // if source is a directory, make sure the name ends with a slash |
|
143 if ((*srcKidPath)[(srcKidPath->Length() - 1)] != '/') |
|
144 { |
|
145 TPtr srcKidAppend = srcKidPath->Des(); |
|
146 srcKidAppend.Append('/'); |
|
147 } |
|
148 } |
|
149 |
|
150 HBufC* dstKidPath = |
|
151 iOperation->FileEngine()->FullNameLC(*iOperation->iDstKidFep); |
|
152 if ((*iOperation->iDstKidFep).Type() == KNodeTypeDir) |
|
153 { // if source is a directory, make sure the name ends with a slash |
|
154 if ((*dstKidPath)[(dstKidPath->Length() -1)] != '/') |
|
155 { |
|
156 TPtr dstKidAppend = dstKidPath->Des(); |
|
157 dstKidAppend.Append('/'); |
|
158 } |
|
159 } |
|
160 iOperation-> |
|
161 FileEngine()-> |
|
162 RemoteAccessL()->RenameL(*srcKidPath, |
|
163 *dstKidPath, |
|
164 iOperation->iOverWrite, |
|
165 iOperation); |
|
166 CleanupStack::PopAndDestroy(2, srcKidPath); // dstKidPath, srcKidPath |
|
167 } |
|
168 } |
|
169 |
|
170 // ---------------------------------------------------------------------------- |
|
171 // CRsfwRenameFileStateMachine::TRenameFileState::CompleteL |
|
172 // ---------------------------------------------------------------------------- |
|
173 // |
|
174 CRsfwRenameFileStateMachine::TState* |
|
175 CRsfwRenameFileStateMachine::TRenameFileState::CompleteL() |
|
176 { |
|
177 if (!iOperation->iSrcKidCreated) |
|
178 { |
|
179 // If we didn't create the srcKidFep, |
|
180 // we must remove it from parent's list |
|
181 iOperation->Node()->RemoveKidL(iOperation->iSrcKidFep); |
|
182 } |
|
183 else |
|
184 { |
|
185 // Enter source into the file table |
|
186 iOperation->Node()->iFileTable->AddL(iOperation->iSrcKidFep); |
|
187 } |
|
188 |
|
189 if (!iOperation->iDstKidCreated) |
|
190 { |
|
191 // If we didn't create the dstKidFep, we must remove and destroy it |
|
192 // unless the node has a removed marking set |
|
193 // (should not happen, except for removed nodes) |
|
194 iOperation->FileEngine()->iFileTable->RemoveL(iOperation->iDstKidFep); |
|
195 delete iOperation->iDstKidFep; |
|
196 iOperation->iDstKidFep = NULL; |
|
197 } |
|
198 |
|
199 // Change srcKidFep's name, |
|
200 // and insert the fep into the dstParentFep directory |
|
201 iOperation->iSrcKidFep->RenameL(iOperation->iDstKidName); |
|
202 iOperation->iDstParentFep->AddKid(*iOperation->iSrcKidFep); |
|
203 |
|
204 // Refresh both parent directories |
|
205 iOperation->Node()->SetLocallyDirty(); |
|
206 if (iOperation->Node() != iOperation->iDstParentFep) |
|
207 { |
|
208 iOperation->Node()->SetLocallyDirty(); |
|
209 } |
|
210 |
|
211 return new CRsfwRenameFileStateMachine::TAcquireLockState(iOperation); |
|
212 } |
|
213 |
|
214 // ---------------------------------------------------------------------------- |
|
215 // CRsfwRenameFileStateMachine::TRenameFileState::ErrorL |
|
216 // ---------------------------------------------------------------------------- |
|
217 // |
|
218 CRsfwRenameFileStateMachine::TState* |
|
219 CRsfwRenameFileStateMachine::TRenameFileState::ErrorL(TInt aCode) |
|
220 { |
|
221 DEBUGSTRING(("remote rename failed")); |
|
222 return iOperation->CompleteRequestL(aCode); |
|
223 } |
|
224 |
|
225 // ---------------------------------------------------------------------------- |
|
226 // CRsfwRenameFileStateMachine::TAcquireLockState::TAcquireLockState |
|
227 // ---------------------------------------------------------------------------- |
|
228 // |
|
229 CRsfwRenameFileStateMachine:: |
|
230 TAcquireLockState::TAcquireLockState(CRsfwRenameFileStateMachine* aParent) |
|
231 { |
|
232 iOperation = aParent; |
|
233 iRequestedLock = EFalse; |
|
234 } |
|
235 |
|
236 // ---------------------------------------------------------------------------- |
|
237 // CRsfwRenameFileStateMachine::TAcquireLockState::EnterL |
|
238 // ---------------------------------------------------------------------------- |
|
239 // |
|
240 void CRsfwRenameFileStateMachine::TAcquireLockState::EnterL() |
|
241 { |
|
242 if (!iOperation->FileEngine()->WriteDisconnected()) |
|
243 { |
|
244 // Now we have updated everything necessary in iSrcKidFep |
|
245 // possibly lock timer is ticking with etc. |
|
246 // However, at least WebDAV does not lock the new file in move, |
|
247 // so if the old file was locked we need to tell |
|
248 // the access protocol plug-in that it must lock the new file. |
|
249 if (iOperation->iSrcKidFep->IsLocked()) |
|
250 { |
|
251 iOperation->iSrcKidFep->iLockTimer->Cancel(); |
|
252 iOperation->FileEngine()->LockManager()-> |
|
253 ObtainLockL(iOperation->iSrcKidFep, |
|
254 EFileWrite, |
|
255 iOperation->iLockToken, |
|
256 iOperation); |
|
257 iRequestedLock = ETrue; |
|
258 } |
|
259 else |
|
260 { |
|
261 iOperation->HandleRemoteAccessResponse(0, KErrNone); |
|
262 } |
|
263 } |
|
264 else |
|
265 { |
|
266 iOperation->HandleRemoteAccessResponse(0, KErrNone); |
|
267 } |
|
268 } |
|
269 |
|
270 // ---------------------------------------------------------------------------- |
|
271 // CRsfwRenameFileStateMachine::TAcquireLockState::CompleteL |
|
272 // ---------------------------------------------------------------------------- |
|
273 // |
|
274 CRsfwRenameFileStateMachine::TState* |
|
275 CRsfwRenameFileStateMachine::TAcquireLockState::CompleteL() |
|
276 { |
|
277 if (iRequestedLock) |
|
278 { |
|
279 iOperation-> |
|
280 iSrcKidFep->SetLockedL(iOperation->FileEngine()->LockManager(), |
|
281 iOperation->iLockToken); |
|
282 iOperation->iLockToken = NULL; |
|
283 } |
|
284 return iOperation->CompleteRequestL(KErrNone); |
|
285 } |
|
286 |
|
287 // ---------------------------------------------------------------------------- |
|
288 // CRsfwRenameFileStateMachine::TAcquireLockState::ErrorL |
|
289 // ---------------------------------------------------------------------------- |
|
290 // |
|
291 CRsfwRenameFileStateMachine::TState* |
|
292 CRsfwRenameFileStateMachine::TAcquireLockState::ErrorL(TInt aCode) |
|
293 { |
|
294 return iOperation->CompleteRequestL(aCode); |
|
295 } |
|
296 |