|
1 /* |
|
2 * Copyright (c) 2005 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 the License "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: A task to update a Feed. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "FeedAttributes.h" |
|
20 #include "FeedHandler.h" |
|
21 #include "FeedsServer.h" |
|
22 #include "LeakTracker.h" |
|
23 #include "Logger.h" |
|
24 #include "PackedFeed.h" |
|
25 #include "UpdateFeedTask.h" |
|
26 #include "UrlHandlerFactory.h" |
|
27 |
|
28 |
|
29 // ----------------------------------------------------------------------------- |
|
30 // CUpdateFeedTask::NewL |
|
31 // |
|
32 // Two-phased constructor. |
|
33 // ----------------------------------------------------------------------------- |
|
34 // |
|
35 CUpdateFeedTask* CUpdateFeedTask::NewL(CFeedsServer& aFeedsServer, |
|
36 const TDesC& aUrl,TInt aFeedId, MUpdateFeedTaskObserver& aObserver) |
|
37 { |
|
38 CUpdateFeedTask* self = new (ELeave) CUpdateFeedTask(aFeedsServer, aObserver); |
|
39 |
|
40 CleanupStack::PushL(self); |
|
41 self->ConstructL(aUrl,aFeedId); |
|
42 CleanupStack::Pop(); |
|
43 |
|
44 return self; |
|
45 } |
|
46 |
|
47 |
|
48 // ----------------------------------------------------------------------------- |
|
49 // CUpdateFeedTask::NewL |
|
50 // |
|
51 // Two-phased constructor. |
|
52 // ----------------------------------------------------------------------------- |
|
53 // |
|
54 CUpdateFeedTask* CUpdateFeedTask::NewL(CFeedsServer& aFeedsServer, const TDesC& aUrl,TInt aFeedId, |
|
55 TDesC8* aFeedBuffer, MUpdateFeedTaskObserver& aObserver) |
|
56 { |
|
57 CUpdateFeedTask* self = new (ELeave) CUpdateFeedTask(aFeedsServer, aObserver); |
|
58 |
|
59 CleanupStack::PushL(self); |
|
60 self->ConstructL(aUrl,aFeedId); |
|
61 CleanupStack::Pop(); |
|
62 |
|
63 self->iFileBuffer = aFeedBuffer; |
|
64 return self; |
|
65 } |
|
66 |
|
67 |
|
68 // ----------------------------------------------------------------------------- |
|
69 // CUpdateFeedTask::CUpdateFeedTask |
|
70 // C++ default constructor can NOT contain any code, that |
|
71 // might leave. |
|
72 // ----------------------------------------------------------------------------- |
|
73 // |
|
74 CUpdateFeedTask::CUpdateFeedTask(CFeedsServer& aFeedsServer, MUpdateFeedTaskObserver& aObserver): |
|
75 CTask(aFeedsServer), iLeakTracker(CLeakTracker::EUpdateFeedTask), iObserver(aObserver) |
|
76 { |
|
77 } |
|
78 |
|
79 |
|
80 // ----------------------------------------------------------------------------- |
|
81 // CUpdateFeedTask::ConstructL |
|
82 // Symbian 2nd phase constructor can leave. |
|
83 // ----------------------------------------------------------------------------- |
|
84 // |
|
85 void CUpdateFeedTask::ConstructL(const TDesC& aUrl,TInt aFeedId) |
|
86 { |
|
87 BaseConstructL(ETrue); |
|
88 iFeedId = aFeedId; |
|
89 iUrl = aUrl.AllocL(); |
|
90 iPackedFeed = CPackedFeed::NewL(); |
|
91 iIdle = CIdle::NewL(CActive::EPriorityIdle); |
|
92 } |
|
93 |
|
94 |
|
95 // ----------------------------------------------------------------------------- |
|
96 // CUpdateFeedTask::~CUpdateFeedTask |
|
97 // Deconstructor. |
|
98 // ----------------------------------------------------------------------------- |
|
99 // |
|
100 CUpdateFeedTask::~CUpdateFeedTask() |
|
101 { |
|
102 delete iUrl; |
|
103 delete iIdle; |
|
104 delete iUrlHandler; |
|
105 |
|
106 delete iFileBuffer; |
|
107 delete iPackedFeed; |
|
108 } |
|
109 |
|
110 |
|
111 // ----------------------------------------------------------------------------- |
|
112 // CUpdateFeedTask::StartTaskL. |
|
113 // |
|
114 // Starts the task. |
|
115 // ----------------------------------------------------------------------------- |
|
116 // |
|
117 void CUpdateFeedTask::StartTaskL() |
|
118 { |
|
119 // If the feed was already provided then get ready to parse the buffer. |
|
120 if (iFileBuffer != NULL) |
|
121 { |
|
122 FEED_LOG(_L("Feeds"), _L("Feeds.log"), |
|
123 EFileLoggingModeAppend, _L("Processing Feed: External Buffer")); |
|
124 |
|
125 // Parse the buffer, but to be consistent with the asynchronous |
|
126 // behavior of using an UrlHandlers the buffer isn't parsed until |
|
127 // after the callstack unrolls. |
|
128 iIdle->Start(TCallBack(DelayedParse, this)); |
|
129 } |
|
130 |
|
131 // If the feed is coming from a UrlHandler, then start the load. |
|
132 else if (iUrl != NULL) |
|
133 { |
|
134 FEED_LOG1(_L("Feeds"), _L("Feeds.log"), |
|
135 EFileLoggingModeAppend, _L("Processing Feed: %S"), iUrl); |
|
136 |
|
137 iUrlHandler = UrlHandlerFactory::NewUrlHandlerL( iObserver.HttpConnection(), *iUrl ); |
|
138 iUrlHandler->LoadUrlL(*iUrl, *this); |
|
139 } |
|
140 |
|
141 // Otherwise, panic |
|
142 else |
|
143 { |
|
144 // TODO: |
|
145 } |
|
146 } |
|
147 |
|
148 |
|
149 // ----------------------------------------------------------------------------- |
|
150 // CUpdateFeedTask::StartWait |
|
151 // |
|
152 // Notifies the observer that the UrlHandler is about to start a lengthy operation. |
|
153 // In response the observer could display somekind of status indicator. |
|
154 // ----------------------------------------------------------------------------- |
|
155 // |
|
156 void CUpdateFeedTask::StartWait() |
|
157 { |
|
158 iObserver.StartWait(); |
|
159 } |
|
160 |
|
161 |
|
162 // ----------------------------------------------------------------------------- |
|
163 // CUpdateFeedTask::LoadCompleted |
|
164 // |
|
165 // Passes the status code and responseBody to the observer. The observer |
|
166 // adopts aResponseBody. |
|
167 // ----------------------------------------------------------------------------- |
|
168 // |
|
169 void CUpdateFeedTask::LoadCompleted(TInt aStatusCode, TDesC8* aResponseBody, |
|
170 const TDesC& aContentType, const TDesC& aCharSet) |
|
171 { |
|
172 TInt err; |
|
173 |
|
174 // Parse the buffer |
|
175 if (aResponseBody != NULL) |
|
176 { |
|
177 // TODO: Just pass iPackedFeed instead and remove the |
|
178 // whole observer interface altogether. |
|
179 TRAP(err, iFeedsServer.FeedHandler().ParseL(*aResponseBody, |
|
180 aContentType, aCharSet, *this)); |
|
181 if (err != KErrNone) |
|
182 { |
|
183 delete iPackedFeed; |
|
184 iPackedFeed = NULL; |
|
185 TRAP_IGNORE(iFeedsServer.ResetXmlUtilsL()); |
|
186 } |
|
187 else |
|
188 { |
|
189 iPackedFeed->Trim(); |
|
190 } |
|
191 |
|
192 delete aResponseBody; |
|
193 |
|
194 // Pass the feed to the observer. |
|
195 iObserver.Completed(iPackedFeed, *iUrl,iFeedId, err); |
|
196 iPackedFeed = NULL; |
|
197 } |
|
198 |
|
199 // The load failed, exit cleanly. |
|
200 else |
|
201 { |
|
202 // Pass the status to the observer. |
|
203 iObserver.Completed(NULL, *iUrl,iFeedId, aStatusCode); |
|
204 } |
|
205 } |
|
206 |
|
207 |
|
208 // ----------------------------------------------------------------------------- |
|
209 // CUpdateFeedTask::FeedBeginsL |
|
210 // |
|
211 // The beginning of a feed was found. |
|
212 // ----------------------------------------------------------------------------- |
|
213 // |
|
214 void CUpdateFeedTask::FeedBeginsL() |
|
215 { |
|
216 iPackedFeed->FeedBeginsL(); |
|
217 |
|
218 // Add the oringal url too. |
|
219 iPackedFeed->AddAttributeL(EFeedAttributeFeedUrl, *iUrl); |
|
220 } |
|
221 |
|
222 |
|
223 // ----------------------------------------------------------------------------- |
|
224 // CUpdateFeedTask::FeedEndsL |
|
225 // |
|
226 // The end of a feed was found. |
|
227 // ----------------------------------------------------------------------------- |
|
228 // |
|
229 void CUpdateFeedTask::FeedEndsL() |
|
230 { |
|
231 iPackedFeed->FeedEndsL(); |
|
232 } |
|
233 |
|
234 |
|
235 // ----------------------------------------------------------------------------- |
|
236 // CUpdateFeedTask::ItemBeginsL |
|
237 // |
|
238 // The beginning of a item was found. |
|
239 // ----------------------------------------------------------------------------- |
|
240 // |
|
241 void CUpdateFeedTask::ItemBeginsL() |
|
242 { |
|
243 iPackedFeed->ItemBeginsL(); |
|
244 } |
|
245 |
|
246 |
|
247 // ----------------------------------------------------------------------------- |
|
248 // CUpdateFeedTask::ItemEndsL |
|
249 // |
|
250 // The end of a item was found. |
|
251 // ----------------------------------------------------------------------------- |
|
252 // |
|
253 void CUpdateFeedTask::ItemEndsL() |
|
254 { |
|
255 iPackedFeed->ItemEndsL(); |
|
256 } |
|
257 |
|
258 |
|
259 // ----------------------------------------------------------------------------- |
|
260 // CUpdateFeedTask::EnclosureBeginsL |
|
261 // |
|
262 // The beginning of a enclosure was found. |
|
263 // ----------------------------------------------------------------------------- |
|
264 // |
|
265 void CUpdateFeedTask::EnclosureBeginsL() |
|
266 { |
|
267 iPackedFeed->EnclosureBeginsL(); |
|
268 } |
|
269 |
|
270 |
|
271 // ----------------------------------------------------------------------------- |
|
272 // CUpdateFeedTask::EnclosureEndsL |
|
273 // |
|
274 // The end of a enclosure was found. |
|
275 // ----------------------------------------------------------------------------- |
|
276 // |
|
277 void CUpdateFeedTask::EnclosureEndsL() |
|
278 { |
|
279 iPackedFeed->EnclosureEndsL(); |
|
280 } |
|
281 |
|
282 |
|
283 // ----------------------------------------------------------------------------- |
|
284 // CUpdateFeedTask::AddAttributeL |
|
285 // |
|
286 // A attribute was found. This attribute should be applied to |
|
287 // the enclosing entity (Feed, Item, or Enclosure). |
|
288 // ----------------------------------------------------------------------------- |
|
289 // |
|
290 void CUpdateFeedTask::AddAttributeL(TInt aAttribute, const TDesC& aValue) |
|
291 { |
|
292 if (aValue.Length() > 0) |
|
293 { |
|
294 iPackedFeed->AddAttributeL(aAttribute, aValue); |
|
295 } |
|
296 } |
|
297 |
|
298 |
|
299 // ----------------------------------------------------------------------------- |
|
300 // CUpdateFeedTask::OtherTitleL |
|
301 // |
|
302 // An unimportant element was found. The client can use this information for |
|
303 // feed validation. |
|
304 // ----------------------------------------------------------------------------- |
|
305 // |
|
306 void CUpdateFeedTask::OtherTitleL() |
|
307 { |
|
308 iPackedFeed->OtherTitleL(); |
|
309 } |
|
310 |
|
311 |
|
312 // ----------------------------------------------------------------------------- |
|
313 // CUpdateFeedTask::OtherDescriptionL |
|
314 // |
|
315 // An unimportant element was found. The client can use this information for |
|
316 // feed validation. |
|
317 // ----------------------------------------------------------------------------- |
|
318 // |
|
319 void CUpdateFeedTask::OtherDescriptionL() |
|
320 { |
|
321 iPackedFeed->OtherDescriptionL(); |
|
322 } |
|
323 |
|
324 |
|
325 // ----------------------------------------------------------------------------- |
|
326 // CUpdateFeedTask::OtherLinkL |
|
327 // |
|
328 // An unimportant element was found. The client can use this information for |
|
329 // feed validation. |
|
330 // ----------------------------------------------------------------------------- |
|
331 // |
|
332 void CUpdateFeedTask::OtherLinkL() |
|
333 { |
|
334 iPackedFeed->OtherLinkL(); |
|
335 } |
|
336 |
|
337 |
|
338 // ----------------------------------------------------------------------------- |
|
339 // CFileHandler::DelayedParse |
|
340 // |
|
341 // Parses the buffer to the observer now that the callstack has unrolled. |
|
342 // ----------------------------------------------------------------------------- |
|
343 // |
|
344 TInt CUpdateFeedTask::DelayedParse(TAny* aPtr) |
|
345 { |
|
346 CUpdateFeedTask* self = static_cast<CUpdateFeedTask*>(aPtr); |
|
347 |
|
348 self->LoadCompleted(KErrNone, self->iFileBuffer, KNullDesC, KNullDesC); |
|
349 self->iFileBuffer = NULL; |
|
350 |
|
351 return EFalse; |
|
352 } |