|
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: Delete a file or directory |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "rsfwdeletestatemachine.h" |
|
20 #include "rsfwfileentry.h" |
|
21 #include "rsfwfiletable.h" |
|
22 #include "rsfwinterface.h" |
|
23 #include "rsfwfileengine.h" |
|
24 #include "mdebug.h" |
|
25 #include "rsfwdirent.h" |
|
26 |
|
27 |
|
28 // ---------------------------------------------------------------------------- |
|
29 // CRsfwDeleteStateMachine::CRsfwDeleteStateMachine |
|
30 // ---------------------------------------------------------------------------- |
|
31 // |
|
32 CRsfwDeleteStateMachine::CRsfwDeleteStateMachine(TUint aNodeType) |
|
33 : iNodeType(aNodeType) |
|
34 { |
|
35 } |
|
36 |
|
37 // ---------------------------------------------------------------------------- |
|
38 // CRsfwDeleteStateMachine::~CRsfwDeleteStateMachine |
|
39 // ---------------------------------------------------------------------------- |
|
40 // |
|
41 CRsfwDeleteStateMachine::~CRsfwDeleteStateMachine() |
|
42 { |
|
43 iDirEnts.ResetAndDestroy(); |
|
44 } |
|
45 |
|
46 // ---------------------------------------------------------------------------- |
|
47 // CRsfwDeleteStateMachine::CompleteRequestL |
|
48 // ---------------------------------------------------------------------------- |
|
49 // |
|
50 CRsfwRfeStateMachine::TState* CRsfwDeleteStateMachine::CompleteRequestL(TInt aError) |
|
51 { |
|
52 if (iKidPath) |
|
53 { |
|
54 delete iKidPath; |
|
55 iKidPath = NULL; |
|
56 } |
|
57 |
|
58 // If the return code was KErrInUse, the directory we were about to delete |
|
59 // was not empty, and we couldn't delete it. In this case it has anyway |
|
60 //been added to the cache (as a side effect), |
|
61 // so we don't want to delete the fep. |
|
62 if (aError != KErrInUse && iKidCreated) |
|
63 { |
|
64 delete iKidFep; |
|
65 iKidFep = NULL; |
|
66 } |
|
67 |
|
68 CompleteAndDestroyState()->SetErrorCode(aError); |
|
69 return CompleteAndDestroyState(); |
|
70 } |
|
71 |
|
72 // Check if exists |
|
73 |
|
74 |
|
75 // ---------------------------------------------------------------------------- |
|
76 // CRsfwDeleteStateMachine::TCheckIfCanBeDeleted::TCheckIfCanBeDeleted |
|
77 // ---------------------------------------------------------------------------- |
|
78 // |
|
79 CRsfwDeleteStateMachine:: |
|
80 TCheckIfCanBeDeleted::TCheckIfCanBeDeleted(CRsfwDeleteStateMachine* aParent) |
|
81 : iOperation(aParent) |
|
82 { |
|
83 } |
|
84 |
|
85 // ---------------------------------------------------------------------------- |
|
86 // CRsfwDeleteStateMachine::TCheckIfCanBeDeleted::EnterL |
|
87 // ---------------------------------------------------------------------------- |
|
88 // |
|
89 void CRsfwDeleteStateMachine::TCheckIfCanBeDeleted::EnterL() |
|
90 { |
|
91 // Kidname is in the same place in remove and rmdir structures,,, |
|
92 TRfeRemoveInArgs* inArgs = |
|
93 static_cast<TRfeRemoveInArgs*>(iOperation->iInArgs); |
|
94 TPtrC kidName(inArgs->iName); |
|
95 |
|
96 // the parent from which we are removing |
|
97 if (!iOperation->Node()) |
|
98 { |
|
99 User::Leave(KErrNotFound); |
|
100 } |
|
101 |
|
102 DEBUGSTRING16(("removing entry '%S' from fid %d", |
|
103 &kidName, |
|
104 iOperation->Node()->Fid().iNodeId)); |
|
105 |
|
106 // Do we know about the kid yet? |
|
107 iOperation->iKidFep = iOperation->Node()->FindKidByName(kidName); |
|
108 if (!iOperation->iKidFep) |
|
109 { |
|
110 // Create a temporary file entry for the target |
|
111 iOperation->iKidFep = CRsfwFileEntry::NewL(kidName, iOperation->Node()); |
|
112 iOperation->iKidCreated = ETrue; |
|
113 } |
|
114 |
|
115 // Ensure the type matches with the operation (RmDir() or Delete()) |
|
116 if ((iOperation->iKidFep->Type() != KNodeTypeUnknown) && |
|
117 (iOperation->iKidFep->Type() != iOperation->iNodeType)) |
|
118 { |
|
119 DEBUGSTRING(("object type does not match the parameter type!")); |
|
120 User::Leave(KErrArgument); |
|
121 } |
|
122 |
|
123 // if the type is unknown, set it from the operation parameter |
|
124 if (iOperation->iKidFep->Type() == KNodeTypeUnknown) |
|
125 { |
|
126 iOperation->iKidFep->SetType(iOperation->iNodeType); |
|
127 } |
|
128 |
|
129 // If it is a directory, check that it is empty |
|
130 if (!iOperation->FileEngine()->Disconnected() && |
|
131 iOperation->iNodeType == KNodeTypeDir) |
|
132 { |
|
133 TInt zero = 0; |
|
134 iOperation->FileEngine()-> |
|
135 FetchAndCacheL(*iOperation->iKidFep, |
|
136 0, |
|
137 &zero, |
|
138 &iOperation->iDirEnts, |
|
139 iOperation); |
|
140 } |
|
141 else |
|
142 { |
|
143 iOperation->HandleRemoteAccessResponse(0, KErrNone); |
|
144 } |
|
145 } |
|
146 |
|
147 // ---------------------------------------------------------------------------- |
|
148 // CRsfwDeleteStateMachine::TCheckIfCanBeDeleted::CompleteL |
|
149 // ---------------------------------------------------------------------------- |
|
150 // |
|
151 CRsfwDeleteStateMachine::TState* |
|
152 CRsfwDeleteStateMachine::TCheckIfCanBeDeleted::CompleteL() |
|
153 { |
|
154 if ((iOperation->iNodeType == KNodeTypeDir) && |
|
155 ((iOperation->iDirEnts).Count() > 0)) |
|
156 { |
|
157 // Attach the directory itself if it was unknown |
|
158 if (iOperation->iKidCreated) |
|
159 { |
|
160 iOperation->FileEngine()->iFileTable->AddL(iOperation->iKidFep); |
|
161 iOperation->Node()->AddKid(*iOperation->iKidFep); |
|
162 } |
|
163 |
|
164 // if the directory is not empty we cannot delete it |
|
165 // however, let's add its entries to the cache |
|
166 iOperation->FileEngine()->AddToCacheL( |
|
167 *iOperation->iKidFep, |
|
168 &iOperation->iDirEnts, |
|
169 iOperation->FileEngine(), |
|
170 0); |
|
171 |
|
172 return iOperation->CompleteRequestL(KErrInUse); |
|
173 } |
|
174 else |
|
175 { |
|
176 return new CRsfwDeleteStateMachine::TDeleteNodeState(iOperation); |
|
177 } |
|
178 } |
|
179 |
|
180 // ---------------------------------------------------------------------------- |
|
181 // CRsfwDeleteStateMachine::TCheckIfCanBeDeleted::ErrorL |
|
182 // ---------------------------------------------------------------------------- |
|
183 // |
|
184 CRsfwDeleteStateMachine::TState* |
|
185 CRsfwDeleteStateMachine::TCheckIfCanBeDeleted::ErrorL(TInt aCode) |
|
186 { |
|
187 DEBUGSTRING(("kid didn't exist!")); |
|
188 return iOperation->CompleteRequestL(aCode); |
|
189 } |
|
190 |
|
191 // Remove directory |
|
192 |
|
193 // ---------------------------------------------------------------------------- |
|
194 // CRsfwDeleteStateMachine::TDeleteNodeState::TDeleteNodeState |
|
195 // ---------------------------------------------------------------------------- |
|
196 // |
|
197 CRsfwDeleteStateMachine:: |
|
198 TDeleteNodeState::TDeleteNodeState(CRsfwDeleteStateMachine* aParent) |
|
199 : iOperation(aParent) |
|
200 { |
|
201 } |
|
202 |
|
203 // ---------------------------------------------------------------------------- |
|
204 // CRsfwDeleteStateMachine::TDeleteNodeState::EnterL |
|
205 // ---------------------------------------------------------------------------- |
|
206 // |
|
207 void CRsfwDeleteStateMachine::TDeleteNodeState::EnterL() |
|
208 { |
|
209 // Get the path for the actual remove |
|
210 iOperation->iKidPath = |
|
211 iOperation->FileEngine()->FullNameL(*iOperation->iKidFep); |
|
212 if (!iOperation->FileEngine()->Disconnected()) |
|
213 { |
|
214 if (iOperation->iNodeType == KNodeTypeDir) |
|
215 { |
|
216 iOperation-> |
|
217 FileEngine()-> |
|
218 RemoteAccessL()->DeleteDirectoryL(*iOperation->iKidPath, |
|
219 iOperation); |
|
220 } |
|
221 else // KNodeTypFile |
|
222 { |
|
223 iOperation-> |
|
224 FileEngine()-> |
|
225 RemoteAccessL()->DeleteFileL(*iOperation->iKidPath, |
|
226 iOperation); |
|
227 } |
|
228 } |
|
229 else |
|
230 { |
|
231 // Disconnected |
|
232 iOperation->HandleRemoteAccessResponse(0, KErrNone); |
|
233 } |
|
234 } |
|
235 |
|
236 // ---------------------------------------------------------------------------- |
|
237 // CRsfwDeleteStateMachine::TDeleteNodeState::CompleteL |
|
238 // ---------------------------------------------------------------------------- |
|
239 // |
|
240 CRsfwDeleteStateMachine::TState* CRsfwDeleteStateMachine::TDeleteNodeState::CompleteL() |
|
241 { |
|
242 if (!iOperation->FileEngine()->Disconnected()) |
|
243 { |
|
244 // If the target was already known, remove and destroy it |
|
245 if (!iOperation->iKidCreated) |
|
246 { |
|
247 iOperation->FileEngine()->iFileTable->RemoveL(iOperation->iKidFep); |
|
248 delete iOperation->iKidFep; |
|
249 iOperation->iKidFep = NULL; |
|
250 } |
|
251 } |
|
252 |
|
253 iOperation->Node()->SetLocallyDirty(); |
|
254 return iOperation->CompleteRequestL(KErrNone); |
|
255 } |
|
256 |
|
257 // ---------------------------------------------------------------------------- |
|
258 // CRsfwDeleteStateMachine::TDeleteNodeState::ErrorL |
|
259 // ---------------------------------------------------------------------------- |
|
260 // |
|
261 CRsfwDeleteStateMachine::TState* |
|
262 CRsfwDeleteStateMachine::TDeleteNodeState::ErrorL(TInt aCode) |
|
263 { |
|
264 return iOperation->CompleteRequestL(aCode); |
|
265 } |
|
266 |
|
267 |
|
268 |