1 /** @file |
|
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: CUpnpBrowseCommand |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "upnpbrowsecommand.h" |
|
20 |
|
21 #include "upnppathresolver.h" |
|
22 #include "upnppathelement.h" |
|
23 #include "upnpavcpmanager.h" |
|
24 #include "upnpavcontrolpoint.h" |
|
25 |
|
26 #include "upnphttpmessagefactory.h" |
|
27 #include "upnpstring.h" |
|
28 #include "upnpcons.h" |
|
29 |
|
30 #include "upnpavcpenginehelper.h" |
|
31 using namespace UpnpAVCPEngine; |
|
32 |
|
33 // ----------------------------------------------------------------------------- |
|
34 // CUpnpBrowseCommand::NewL |
|
35 // Two-phased constructor. |
|
36 // ----------------------------------------------------------------------------- |
|
37 // |
|
38 CUpnpBrowseCommand* CUpnpBrowseCommand::NewL(CUpnpAVCPManager& aAVCPManager, CUpnpAVCPEngineSession& aSession, const RMessage2& aMessage) |
|
39 { |
|
40 CUpnpBrowseCommand* self = new( ELeave ) CUpnpBrowseCommand(aAVCPManager, aSession, aMessage); |
|
41 |
|
42 CleanupStack::PushL( self ); |
|
43 self->ConstructL(); |
|
44 CleanupStack::Pop( self ); |
|
45 |
|
46 return self; |
|
47 } |
|
48 |
|
49 // ----------------------------------------------------------------------------- |
|
50 // CUpnpBrowseCommand::ConstructL |
|
51 // Symbian 2nd phase constructor can leave. |
|
52 // ----------------------------------------------------------------------------- |
|
53 // |
|
54 void CUpnpBrowseCommand::ConstructL() |
|
55 { |
|
56 CUpnpCommand::BaseConstructL(); |
|
57 } |
|
58 |
|
59 // ----------------------------------------------------------------------------- |
|
60 // CUpnpBrowseCommand::CUpnpBrowseCommand |
|
61 // C++ default constructor can NOT contain any code, that |
|
62 // might leave. |
|
63 // ----------------------------------------------------------------------------- |
|
64 CUpnpBrowseCommand::CUpnpBrowseCommand(CUpnpAVCPManager& aAVCPManager, |
|
65 CUpnpAVCPEngineSession& aSession, |
|
66 const RMessage2& aMessage): |
|
67 CUpnpCommand( aAVCPManager, |
|
68 aSession, |
|
69 aMessage) |
|
70 { |
|
71 } |
|
72 |
|
73 // ----------------------------------------------------------------------------- |
|
74 // CUpnpBrowseCommand::~CUpnpBrowseCommand |
|
75 // desctructor |
|
76 // ----------------------------------------------------------------------------- |
|
77 // |
|
78 CUpnpBrowseCommand::~CUpnpBrowseCommand() |
|
79 { |
|
80 delete iObjectId; |
|
81 delete iObjectName; |
|
82 iResult.Reset(); |
|
83 } |
|
84 |
|
85 // ----------------------------------------------------------------------------- |
|
86 // CUpnpBrowseCommand::SessionId |
|
87 // ----------------------------------------------------------------------------- |
|
88 // |
|
89 TInt CUpnpBrowseCommand::SessionId() |
|
90 { |
|
91 return CUpnpCommand::SessionId(); |
|
92 } |
|
93 |
|
94 // ----------------------------------------------------------------------------- |
|
95 // CUpnpBrowseCommand::RunError |
|
96 // ----------------------------------------------------------------------------- |
|
97 // |
|
98 void CUpnpBrowseCommand::RunError(TInt aErrorCode) |
|
99 { |
|
100 return CUpnpCommand::RunError(aErrorCode); |
|
101 } |
|
102 |
|
103 // ----------------------------------------------------------------------------- |
|
104 // CUpnpBrowseCommand::ExecuteL |
|
105 // ----------------------------------------------------------------------------- |
|
106 // |
|
107 void CUpnpBrowseCommand::ExecuteL() |
|
108 { |
|
109 //ASSERT(iType == EPrepareDirList || iType == EDirAttributes || iType == EFileAttributes); |
|
110 |
|
111 HBufC8* uuid = NULL; |
|
112 HBufC8* path = NULL; |
|
113 if (iType == EPrepareDirList) |
|
114 { |
|
115 DEBUGSTRING(("Execute Browse command (dir listing)")); |
|
116 // set command id |
|
117 TPckg<TInt> numPckg(iId); |
|
118 iMessage.WriteL(0, numPckg); |
|
119 |
|
120 uuid = ReadDes8FromMessageLC(2); |
|
121 DEBUGSTRING8((" uuid: %S",uuid)); |
|
122 path = ReadDes8FromMessageLC(3); |
|
123 DEBUGSTRING8((" path: %S",path)); |
|
124 } |
|
125 else |
|
126 { |
|
127 DEBUGSTRING(("Execute Browse command (attributes)")); |
|
128 uuid = ReadDes8FromMessageLC(0); |
|
129 DEBUGSTRING8((" uuid: %S",uuid)); |
|
130 path = ReadDes8FromMessageLC(1); |
|
131 DEBUGSTRING8((" path: %S",path)); |
|
132 } |
|
133 |
|
134 iPathResolver = &(iAVCPManager.PathResolverL(*uuid, &iSession)); |
|
135 DEBUGSTRING(("Resolve path %S", path)); |
|
136 iPathResolver->ResolveIdL(*path, *this); |
|
137 |
|
138 CleanupStack::PopAndDestroy(path); |
|
139 CleanupStack::PopAndDestroy(uuid); |
|
140 } |
|
141 |
|
142 // ----------------------------------------------------------------------------- |
|
143 // CUpnpBrowseCommand::SetResultL |
|
144 // ----------------------------------------------------------------------------- |
|
145 // |
|
146 void CUpnpBrowseCommand::SetResultL(const RMessage2& aMessage) |
|
147 { |
|
148 //ASSERT(aMessage.Function() == EDownloadDirList); |
|
149 |
|
150 TInt size = iResult.Count(); |
|
151 TFileInfo* dirs = new TFileInfo[size]; |
|
152 for (TInt i = 0; i < size; i++) |
|
153 { |
|
154 dirs[i] = iResult[i]; |
|
155 } |
|
156 TPtr8 result(reinterpret_cast<TUint8*>(dirs), sizeof(TFileInfo)*size, sizeof(TFileInfo)*size); |
|
157 aMessage.WriteL(1, result); |
|
158 delete[] dirs; |
|
159 } |
|
160 |
|
161 |
|
162 // ----------------------------------------------------------------------------- |
|
163 // CUpnpBrowseCommand::SetAttForObject |
|
164 // ----------------------------------------------------------------------------- |
|
165 // |
|
166 TBool CUpnpBrowseCommand::SetAttForObject(RArray<TFileInfo>& aList, const TDesC8& aName) |
|
167 { |
|
168 TBool result(EFalse); |
|
169 for (TInt i = 0; i < aList.Count(); i++) |
|
170 { |
|
171 if (aList[i].iName.Compare(aName) == 0) |
|
172 { |
|
173 iAttr = aList[i]; |
|
174 result = ETrue; |
|
175 break; |
|
176 } |
|
177 } |
|
178 return result; |
|
179 } |
|
180 |
|
181 // ----------------------------------------------------------------------------- |
|
182 // CUpnpBrowseCommand::SendHeadRequestL |
|
183 // ----------------------------------------------------------------------------- |
|
184 // |
|
185 void CUpnpBrowseCommand::SendHeadRequestL(const TDesC8& aUrl) |
|
186 { |
|
187 DEBUGSTRING8(("Send HTTP HEAD request for %S", &aUrl)); |
|
188 |
|
189 HBufC8* resURIencoded = UpnpString::StringReplaceL(aUrl, _L8(" "), _L8("%20")); |
|
190 |
|
191 // we want wait for HEAD to get more info |
|
192 CleanupStack::PushL(resURIencoded); |
|
193 CUpnpHttpMessage* msg = RUpnpHttpMessageFactory::HttpHeadL( *resURIencoded ); |
|
194 CleanupStack::PopAndDestroy(resURIencoded); |
|
195 |
|
196 CleanupStack::PushL( msg ); |
|
197 |
|
198 iAVCPManager.SendL(msg); |
|
199 iSessionId = msg->SessionId(); |
|
200 iAVCPManager.RegisterForHttp(*this); |
|
201 |
|
202 CleanupStack::PopAndDestroy( msg ); |
|
203 } |
|
204 |
|
205 // ----------------------------------------------------------------------------- |
|
206 // CUpnpBrowseCommand::Interpret |
|
207 // ----------------------------------------------------------------------------- |
|
208 // |
|
209 void CUpnpBrowseCommand::InterpretL(TInt aErrorCode, CUpnpAction* aAction) |
|
210 { |
|
211 DEBUGSTRING(("Interpret action response %d", aErrorCode)); |
|
212 if (aErrorCode == EHttp200Ok ) |
|
213 { |
|
214 |
|
215 TInt startIndex = StrToIntL( aAction->ArgumentValue( KStartingIndex ) ); |
|
216 TInt requestCount = StrToIntL( aAction->ArgumentValue( KRequestedCount ) ); |
|
217 |
|
218 TInt numberReturned = StrToIntL( aAction->ArgumentValue( KNumberReturned ) ); |
|
219 TInt totalMatches = StrToIntL( aAction->ArgumentValue( KTotalMatches ) ); |
|
220 |
|
221 HBufC8* resURI = NULL; |
|
222 |
|
223 if (iType == EFileAttributes) |
|
224 { |
|
225 iPathResolver->ParseBrowseResponseL( aAction->ArgumentValue( KResult ), &iResult, *iObjectId, &resURI); |
|
226 } |
|
227 else |
|
228 { |
|
229 iPathResolver->LockReset(); |
|
230 iPathResolver->ParseBrowseResponseL( aAction->ArgumentValue( KResult ), &iResult); |
|
231 } |
|
232 |
|
233 if (iType == EPrepareDirList) |
|
234 { |
|
235 |
|
236 if ( startIndex+numberReturned < totalMatches) |
|
237 { |
|
238 iStartIndex += KRequestedCountLimit; |
|
239 iSessionId = iAVCPManager.CdsBrowseActionL(iPathResolver->UUID(),*iObjectId, KBrowseDirectChildren, KDefaultBrowseFilter, iStartIndex, KRequestedCountLimit, KNullDesC8); |
|
240 iAVCPManager.RegisterForAction(*this); |
|
241 } |
|
242 else |
|
243 { |
|
244 iPathResolver->UnlockReset(); |
|
245 TInt num(iResult.Count()); |
|
246 TPckg<TInt> numPckg(num); |
|
247 // size of directory list |
|
248 iMessage.WriteL(1, numPckg); |
|
249 iMessage.Complete(KErrNone); |
|
250 // don't destroy and wait for next command |
|
251 } |
|
252 } |
|
253 else |
|
254 { |
|
255 if (SetAttForObject(iResult, *iObjectName)) |
|
256 { |
|
257 if (iType == EFileAttributes && resURI) |
|
258 { |
|
259 CleanupStack::PushL(resURI); |
|
260 SendHeadRequestL(*resURI); |
|
261 CleanupStack::PopAndDestroy(resURI); |
|
262 resURI = NULL; |
|
263 } |
|
264 else |
|
265 { |
|
266 TPckg<TFileInfo> attrPckg(iAttr); |
|
267 iMessage.WriteL(2,attrPckg); |
|
268 iMessage.Complete(KErrNone); |
|
269 delete this; |
|
270 } |
|
271 } |
|
272 else |
|
273 { |
|
274 iMessage.Complete(KErrNotFound); |
|
275 delete resURI; |
|
276 delete this; |
|
277 } |
|
278 } |
|
279 } |
|
280 else |
|
281 { |
|
282 iPathResolver->UnlockReset(); |
|
283 iMessage.Complete(KErrAbort); |
|
284 delete this; |
|
285 } |
|
286 |
|
287 } |
|
288 |
|
289 // ----------------------------------------------------------------------------- |
|
290 // CUpnpBrowseCommand::Interpret |
|
291 // ----------------------------------------------------------------------------- |
|
292 // |
|
293 void CUpnpBrowseCommand::InterpretL(TInt aErrorCode, CUpnpHttpMessage* aMessage) |
|
294 { |
|
295 DEBUGSTRING(("HTTP Interpret %d", aErrorCode)); |
|
296 // transfer pending |
|
297 if (aErrorCode == KHttpGetStarted) |
|
298 { |
|
299 return; |
|
300 } |
|
301 iAVCPManager.UnregisterForHttp(*this); |
|
302 if (aErrorCode == EHttp200Ok || aErrorCode == EHttpPartialContent) |
|
303 { |
|
304 // set some attrs |
|
305 iAttr.iMimeType = aMessage->GetHeaderValue(UpnpHTTP::KHdrContentType); |
|
306 TDesC8& size = aMessage->GetHeaderValue(UpnpHTTP::KHdrContentLength); |
|
307 TLex8 intLex( size ); |
|
308 intLex.Val( iAttr.iSize ); |
|
309 } |
|
310 |
|
311 TPckg<TFileInfo> attrPckg(iAttr); |
|
312 iMessage.WriteL(2,attrPckg); |
|
313 iMessage.Complete(KErrNone); |
|
314 delete this; |
|
315 } |
|
316 |
|
317 // ----------------------------------------------------------------------------- |
|
318 // CUpnpBrowseCommand::ResolvedId |
|
319 // ----------------------------------------------------------------------------- |
|
320 // |
|
321 void CUpnpBrowseCommand::ResolvedIdL(TInt aErrCode, CUpnpPathElement* aIdElement) |
|
322 { |
|
323 DEBUGSTRING(("ResolvedId %d", aErrCode)); |
|
324 if (aErrCode == KErrNone) |
|
325 { |
|
326 iObjectId = aIdElement->Id().AllocL(); |
|
327 iObjectName = aIdElement->Name().AllocL(); |
|
328 DEBUGSTRING(("Resolved object %S %S", iObjectId, iObjectName)); |
|
329 if (iType == EPrepareDirList) |
|
330 { |
|
331 iSessionId = iAVCPManager.CdsBrowseActionL(iPathResolver->UUID(), *iObjectId, KBrowseDirectChildren, KDefaultBrowseFilter, iStartIndex, KRequestedCountLimit, KNullDesC8); |
|
332 } |
|
333 else |
|
334 { |
|
335 iSessionId = iAVCPManager.CdsBrowseActionL(iPathResolver->UUID(), *iObjectId, KBrowseMetadata, KDefaultBrowseFilter, 0, 0, KNullDesC8); |
|
336 } |
|
337 iAVCPManager.RegisterForAction(*this); |
|
338 } |
|
339 else |
|
340 { |
|
341 iMessage.Complete(KErrNotFound); |
|
342 delete this; |
|
343 } |
|
344 } |
|
345 // End of File |
|