261e2b5d5229cd797160d79e02a90e0eedd13e5a
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Download / DownloadManager.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include "DownloadManager.h"
19 #include <API/Download/URLDownload.h>
20 #include <dpl/log/log.h>
21
22 using namespace TizenApis::Api::Download;
23 using namespace WrtDeviceApis::Commons;
24
25 namespace TizenApis {
26 namespace Platform {
27 namespace Download {
28
29 DownloadManager::DownloadManager()
30 {
31 }
32
33 DownloadManager::~DownloadManager()
34 {
35 }
36
37 static void on_url_download_started(url_download_h download, const char *content_name, const char *mime_type, void *user_data)
38 {
39     LogInfo("Download started with handle: "<<download<<", content_name: "<<content_name<<", mime_type: "<<mime_type);
40
41     Try
42     {
43         OnDownloadStateChangedPtr eventPtr(new OnDownloadStateChanged());
44         DownloadManager* thisDownloadManager = (DownloadManager*) user_data;
45
46         eventPtr->setDownloadId((long) download);
47         eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_QUEUED);
48         eventPtr->setResult(true);
49
50         thisDownloadManager->m_changeEmitters[download]->emit(eventPtr);
51     }
52     Catch (Exception)
53     {
54         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
55     }
56 }
57
58 static void on_url_download_paused(url_download_h download, void *user_data)
59 {
60     LogInfo("Download paused with handle: "<<download);
61
62     Try
63     {   
64         OnDownloadStateChangedPtr eventPtr(new OnDownloadStateChanged());
65         DownloadManager* thisDownloadManager = (DownloadManager*) user_data;
66
67         eventPtr->setDownloadId((long) download);
68         eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_PAUSED);
69         eventPtr->setResult(true);
70
71         thisDownloadManager->m_changeEmitters[download]->emit(eventPtr);
72     }
73     Catch (Exception)
74     {
75         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
76     }
77 }
78
79 static void on_url_download_completed(url_download_h download, const char * path, void *user_data)
80 {
81     LogInfo("Download completed with handle: "<<download<<", path: "<<path);
82
83     Try
84     {
85         OnDownloadStateChangedPtr eventPtr(new OnDownloadStateChanged());
86         DownloadManager* thisDownloadManager = (DownloadManager*) user_data;
87
88         int ret, found;
89
90         ret = url_download_unset_started_cb(download);
91         if (ret != URL_DOWNLOAD_ERROR_NONE) {
92             ThrowMsg(PlatformException, "Platform error while unsetting started cb: "<<ret);
93         }
94         ret = url_download_unset_paused_cb(download);
95         if (ret != URL_DOWNLOAD_ERROR_NONE) {
96             ThrowMsg(PlatformException, "Platform error while unsetting paused cb: "<<ret);
97         }
98         ret = url_download_unset_stopped_cb(download);
99         if (ret != URL_DOWNLOAD_ERROR_NONE) {
100             ThrowMsg(PlatformException, "Platform error while unsetting stopped cb: "<<ret);
101         }
102         ret = url_download_unset_completed_cb(download);
103         if (ret != URL_DOWNLOAD_ERROR_NONE) {
104             ThrowMsg(PlatformException, "Platform error while unsetting completed cb: "<<ret);
105         }
106         ret = url_download_unset_progress_cb(download);
107         if (ret != URL_DOWNLOAD_ERROR_NONE) {
108             ThrowMsg(PlatformException, "Platform error while unsetting progress cb: "<<ret);
109         }
110
111         std::string fullPath(path);
112         found = fullPath.find_last_of("/\\");
113         eventPtr->setFileName(fullPath.substr(found+1));
114
115         eventPtr->setDownloadId((long) download);
116         eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_COMPLETED);
117         eventPtr->setResult(true);
118
119         thisDownloadManager->m_changeEmitters[download]->emit(eventPtr);
120         //thisDownloadManager->m_changeEmitters.erase(download);
121
122         /*ret = url_download_destroy(download);
123         if (ret != URL_DOWNLOAD_ERROR_NONE) {
124             ThrowMsg(PlatformException, "Platform error while destroying download handle: "<<ret);
125         }*/
126     }
127     Catch (Exception)
128     {
129         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
130     }
131 }
132
133 static void on_url_download_stopped(url_download_h download, url_download_error_e error, void *user_data)
134 {
135     LogInfo("Download stopped with handle: "<<download<<", error: "<<error);
136
137     OnDownloadStateChangedPtr eventPtr(new OnDownloadStateChanged());
138     DownloadManager* thisDownloadManager = (DownloadManager*) user_data;
139     int ret;
140
141     Try
142     {
143         ret = url_download_unset_started_cb(download);
144         if (ret != URL_DOWNLOAD_ERROR_NONE) {
145             ThrowMsg(PlatformException, "Platform error while unsetting started cb: "<<ret);
146         }
147         ret = url_download_unset_paused_cb(download);
148         if (ret != URL_DOWNLOAD_ERROR_NONE) {
149             ThrowMsg(PlatformException, "Platform error while unsetting paused cb: "<<ret);
150         }
151         ret = url_download_unset_stopped_cb(download);
152         if (ret != URL_DOWNLOAD_ERROR_NONE) {
153             ThrowMsg(PlatformException, "Platform error while unsetting stopped cb: "<<ret);
154         }
155         ret = url_download_unset_completed_cb(download);
156         if (ret != URL_DOWNLOAD_ERROR_NONE) {
157             ThrowMsg(PlatformException, "Platform error while unsetting completed cb: "<<ret);
158         }
159         ret = url_download_unset_progress_cb(download);
160         if (ret != URL_DOWNLOAD_ERROR_NONE) {
161             ThrowMsg(PlatformException, "Platform error while unsetting progress cb: "<<ret);
162         }
163
164         eventPtr->setDownloadId((long) download);
165
166         if( URL_DOWNLOAD_ERROR_NONE==error ) {
167             eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_ABORTED);
168         } else {
169             eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_FAILED);
170             eventPtr->setExceptionCode(ExceptionCodes::PlatformException);
171         }
172         eventPtr->setResult(true);
173     }
174     Catch (Exception)
175     {
176         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
177         eventPtr->setResult(false);
178         eventPtr->setExceptionCode(ExceptionCodes::UnknownException);
179     }
180
181     thisDownloadManager->m_changeEmitters[download]->emit(eventPtr);
182     //thisDownloadManager->m_changeEmitters.erase(download);
183
184     /*ret = url_download_destroy(download);
185     if (ret != URL_DOWNLOAD_ERROR_NONE) {
186         LogWarning("Platform error while destroying download handle: "<<ret);
187     }*/
188 }
189
190 static void on_url_download_progress(url_download_h download, unsigned long long received, unsigned long long total, void *user_data)
191 {
192     LogInfo("Download progress with handle: "<<download<<", received: "<<received<<", total: "<<total);
193
194     Try
195     {   
196         OnDownloadStateChangedPtr eventPtr(new OnDownloadStateChanged());
197         DownloadManager* thisDownloadManager = (DownloadManager*) user_data;
198
199         eventPtr->setDownloadId((long) download);
200         eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_DOWNLOADING);
201         eventPtr->setReceivedSize(received);
202         eventPtr->setTotalSize(total);
203         eventPtr->setResult(true);
204
205         thisDownloadManager->m_changeEmitters[download]->emit(eventPtr);
206     }
207     Catch (Exception)
208     {
209         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
210     }
211 }
212
213 void DownloadManager::OnRequestReceived(const IEventStartDownloadPtr &event)
214 {
215     LogDebug("entered");
216
217     Try
218     {
219         URLDownloadPtr urlDownload;
220         urlDownload = event->getDownloadObject();
221         if (!urlDownload) {
222             ThrowMsg(NullPointerException, "Download object is NULL.");
223         }
224
225         std::string url = urlDownload->getUrl();
226         std::string destination = urlDownload->getDestination();
227         std::string fileName = urlDownload->getFileName();
228         LogInfo("url: "<<url<<", destination: "<<destination<<", fileName: "<<fileName);
229
230         int ret, id;
231         url_download_h download;
232
233         ret = url_download_create(&download);
234         if (ret != URL_DOWNLOAD_ERROR_NONE) {
235             ThrowMsg(PlatformException, "Platform error while creating url download: "<<ret);
236         }
237
238         ret = url_download_set_url(download, url.c_str());
239         if (ret != URL_DOWNLOAD_ERROR_NONE) {
240             ThrowMsg(PlatformException, "Platform error while setting url: "<<ret);
241         }
242
243         if(!destination.empty()) {
244             ret = url_download_set_destination(download, destination.c_str());
245             if (ret != URL_DOWNLOAD_ERROR_NONE) {
246                 ThrowMsg(PlatformException, "Platform error while setting destination: "<<ret);
247             }
248         }
249
250         if(!fileName.empty()) {
251             ret = url_download_set_file_name(download, fileName.c_str());
252             if (ret != URL_DOWNLOAD_ERROR_NONE) {
253                 ThrowMsg(PlatformException, "Platform error while setting file name: "<<ret);
254             }
255         }
256
257         if(event->getEmitter()) {
258             ret = url_download_set_started_cb(download, on_url_download_started, this);
259             if (ret != URL_DOWNLOAD_ERROR_NONE) {
260                 ThrowMsg(PlatformException, "Platform error while setting started cb: "<<ret);
261             }
262             ret = url_download_set_paused_cb(download, on_url_download_paused, this);
263             if (ret != URL_DOWNLOAD_ERROR_NONE) {
264                 ThrowMsg(PlatformException, "Platform error while setting paused cb: "<<ret);
265             }
266             ret = url_download_set_stopped_cb(download, on_url_download_stopped, this);
267             if (ret != URL_DOWNLOAD_ERROR_NONE) {
268                 ThrowMsg(PlatformException, "Platform error while setting stopped cb: "<<ret);
269             }
270             ret = url_download_set_completed_cb(download, on_url_download_completed, this);
271             if (ret != URL_DOWNLOAD_ERROR_NONE) {
272                 ThrowMsg(PlatformException, "Platform error while setting completed cb: "<<ret);
273             }
274             ret = url_download_set_progress_cb(download, on_url_download_progress, this);
275             if (ret != URL_DOWNLOAD_ERROR_NONE) {
276                 ThrowMsg(PlatformException, "Platform error while setting progress cb: "<<ret);
277             }
278         }
279
280         ret = url_download_start(download, &id);
281         if (ret != URL_DOWNLOAD_ERROR_NONE) {
282             ThrowMsg(PlatformException, "Platform error while starting download: "<<ret);
283         }
284
285         LogInfo("id: "<<id<<", handle: "<<download);
286
287         if(event->getEmitter()) {
288             LogInfo("Attaching the emitter...");
289             m_changeEmitters[download] = event->getEmitter();
290         } else {
291             LogInfo("Adding the handle...");
292             m_changeEmitters[download];
293         }
294
295         event->setDownloadId((long)download);
296
297         event->setResult(true);
298     }
299     Catch(Exception)
300     {
301                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
302         event->setResult(false);
303         event->setExceptionCode(ExceptionCodes::PlatformException);
304     }
305 }
306
307 void DownloadManager::OnRequestReceived(const IEventSetListenerPtr &event)
308 {
309     LogDebug("entered");
310
311     Try
312     {
313         long downloadId = event->getDownloadId();
314
315         int ret;
316         url_download_h download;
317
318         ret = url_download_create_by_id((int)downloadId, &download);
319         if (ret != URL_DOWNLOAD_ERROR_NONE) {
320             ThrowMsg(NotFoundException, "Platform error while geetting download handle: "<<ret);
321         }
322
323         LogInfo("handle: "<<download);
324
325         ret = url_download_set_started_cb(download, on_url_download_started, this);
326         if (ret != URL_DOWNLOAD_ERROR_NONE) {
327             ThrowMsg(PlatformException, "Platform error while setting started cb: "<<ret);
328         }
329         ret = url_download_set_paused_cb(download, on_url_download_paused, this);
330         if (ret != URL_DOWNLOAD_ERROR_NONE) {
331             ThrowMsg(PlatformException, "Platform error while setting paused cb: "<<ret);
332         }
333         ret = url_download_set_stopped_cb(download, on_url_download_stopped, this);
334         if (ret != URL_DOWNLOAD_ERROR_NONE) {
335             ThrowMsg(PlatformException, "Platform error while setting stopped cb: "<<ret);
336         }
337         ret = url_download_set_completed_cb(download, on_url_download_completed, this);
338         if (ret != URL_DOWNLOAD_ERROR_NONE) {
339             ThrowMsg(PlatformException, "Platform error while setting completed cb: "<<ret);
340         }
341         ret = url_download_set_progress_cb(download, on_url_download_progress, this);
342         if (ret != URL_DOWNLOAD_ERROR_NONE) {
343             ThrowMsg(PlatformException, "Platform error while setting progress cb: "<<ret);
344         }
345
346         if(event->getEmitter()) {
347             LogInfo("Attaching the emitter with handle: "<<download);
348             m_changeEmitters[download] = event->getEmitter();
349         }
350
351         event->setResult(true);
352     }
353     Catch(NotFoundException)
354     {
355         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
356         event->setResult(false);
357         event->setExceptionCode(ExceptionCodes::NotFoundException);
358     }
359     Catch(Exception)
360     {
361                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
362         event->setResult(false);
363         event->setExceptionCode(ExceptionCodes::PlatformException);
364     }
365 }
366
367 void DownloadManager::OnRequestReceived(const IEventAbortDownloadPtr &event)
368 {
369     LogDebug("entered");
370
371     Try
372     {
373         long downloadId = event->getDownloadId();
374
375         int ret;
376
377         std::map<url_download_h, OnDownloadStateChangedEmitterPtr>::iterator it;
378         it = m_changeEmitters.find((url_download_h)downloadId);
379         if( m_changeEmitters.end()==it ) {
380             ThrowMsg(NotFoundException, "Error while getting download handle.");
381         }
382
383         ret = url_download_stop((url_download_h)downloadId);
384         if (ret != URL_DOWNLOAD_ERROR_NONE) {
385             ThrowMsg(PlatformException, "Platform error while stopping download: "<<ret);
386         }
387
388         event->setResult(true);
389     }
390     Catch(NotFoundException)
391     {
392         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
393         event->setResult(false);
394         event->setExceptionCode(ExceptionCodes::NotFoundException);
395     }
396     Catch(Exception)
397     {
398                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
399         event->setResult(false);
400         event->setExceptionCode(ExceptionCodes::UnknownException);
401     }
402 }
403
404 void DownloadManager::OnRequestReceived(const IEventPauseDownloadPtr &event)
405 {
406     LogDebug("entered");
407
408     Try
409     {
410         long downloadId = event->getDownloadId();
411
412         int ret;
413
414         std::map<url_download_h, OnDownloadStateChangedEmitterPtr>::iterator it;
415         it = m_changeEmitters.find((url_download_h)downloadId);
416         if( m_changeEmitters.end()==it ) {
417             ThrowMsg(NotFoundException, "Error while getting download handle.");
418         }
419
420         ret = url_download_pause((url_download_h)downloadId);
421         if (ret != URL_DOWNLOAD_ERROR_NONE) {
422             ThrowMsg(PlatformException, "Platform error while pausing download: "<<ret);
423         }
424
425         event->setResult(true);
426     }
427     Catch(NotFoundException)
428     {
429         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
430         event->setResult(false);
431         event->setExceptionCode(ExceptionCodes::NotFoundException);
432     }
433     Catch(Exception)
434     {
435                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
436         event->setResult(false);
437         event->setExceptionCode(ExceptionCodes::UnknownException);
438     }
439 }
440
441 void DownloadManager::OnRequestReceived(const IEventResumeDownloadPtr &event)
442 {
443     LogDebug("entered");
444
445     Try
446     {
447         long downloadId = event->getDownloadId();
448
449         int ret, id;
450
451         std::map<url_download_h, OnDownloadStateChangedEmitterPtr>::iterator it;
452         it = m_changeEmitters.find((url_download_h)downloadId);
453         if( m_changeEmitters.end()==it ) {
454             ThrowMsg(NotFoundException, "Error while getting download handle.");
455         }
456
457         ret = url_download_start((url_download_h)downloadId, &id);
458         if (ret != URL_DOWNLOAD_ERROR_NONE) {
459             ThrowMsg(PlatformException, "Platform error while resuming download: "<<ret);
460         }
461
462         LogInfo("id: "<<id);
463
464         event->setResult(true);
465     }
466     Catch(NotFoundException)
467     {
468         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
469         event->setResult(false);
470         event->setExceptionCode(ExceptionCodes::NotFoundException);
471     }
472     Catch(Exception)
473     {
474                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
475         event->setResult(false);
476         event->setExceptionCode(ExceptionCodes::UnknownException);
477     }
478 }
479
480 void DownloadManager::OnRequestReceived(const IEventGetStatePtr &event)
481 {
482     LogDebug("entered");
483
484     Try
485     {
486         long downloadId = event->getDownloadId();
487
488         int ret;
489         url_download_state_e state;
490
491         std::map<url_download_h, OnDownloadStateChangedEmitterPtr>::iterator it;
492         it = m_changeEmitters.find((url_download_h)downloadId);
493         if( m_changeEmitters.end()==it ) {
494             ThrowMsg(NotFoundException, "Error while getting download handle.");
495         }
496
497         ret = url_download_get_state((url_download_h)downloadId, &state);
498         if (ret != URL_DOWNLOAD_ERROR_NONE) {
499             ThrowMsg(PlatformException, "Platform error while getting state: "<<ret);
500         }
501
502         if( URL_DOWNLOAD_STATE_READY ==state ) {
503             event->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_QUEUED);
504         } else if( URL_DOWNLOAD_STATE_DOWNLOADING==state ) {
505             event->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_DOWNLOADING);
506         } else if( URL_DOWNLOAD_STATE_PAUSED==state ) {
507             event->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_PAUSED);
508         } else if( URL_DOWNLOAD_STATE_COMPLETED==state ) {
509             event->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_COMPLETED);
510         } else if( URL_DOWNLOAD_STATE_FAILED==state ) {
511             event->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_FAILED);
512         } else {
513             ThrowMsg(PlatformException, "Wrong download state: "<<state);
514         }
515
516         event->setResult(true);
517     }
518     Catch(NotFoundException)
519     {
520         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
521         event->setResult(false);
522         event->setExceptionCode(ExceptionCodes::NotFoundException);
523     }
524     Catch(Exception)
525     {
526                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
527         event->setResult(false);
528         event->setExceptionCode(ExceptionCodes::UnknownException);
529     }
530 }
531
532 void DownloadManager::OnRequestReceived(const IEventGetURLDownloadPtr &event)
533 {
534     LogDebug("entered");
535
536     Try
537     {
538         long downloadId = event->getDownloadId();
539
540         int ret;
541         char* url = NULL;
542         char* destination = NULL;
543
544         std::map<url_download_h, OnDownloadStateChangedEmitterPtr>::iterator it;
545         it = m_changeEmitters.find((url_download_h)downloadId);
546         if( m_changeEmitters.end()==it ) {
547             ThrowMsg(NotFoundException, "Error while getting download handle.");
548         }
549
550         ret = url_download_get_url((url_download_h)downloadId, &url);
551         if (ret != URL_DOWNLOAD_ERROR_NONE) {
552             ThrowMsg(PlatformException, "Platform error while getting url: "<<ret);
553         }
554
555         ret = url_download_get_destination((url_download_h)downloadId, &destination);
556         if (ret != URL_DOWNLOAD_ERROR_NONE) {
557             ThrowMsg(PlatformException, "Platform error while getting destination: "<<ret);
558         }
559
560         URLDownloadPtr urlDownload( new URLDownload() );
561         if(url) {
562             urlDownload->setUrl(std::string(url));
563             free(url);
564         }
565         if(destination) {
566             urlDownload->setDestination(std::string(destination));
567             free(destination);
568         }
569
570         LogInfo("url: "<<urlDownload->getUrl()<<", destination "<<urlDownload->getDestination());
571
572         event->setDownloadObject(urlDownload);
573
574         event->setResult(true);
575     }
576     Catch(NotFoundException)
577     {
578         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
579         event->setResult(false);
580         event->setExceptionCode(ExceptionCodes::NotFoundException);
581     }
582     Catch(Exception)
583     {
584                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
585         event->setResult(false);
586         event->setExceptionCode(ExceptionCodes::UnknownException);
587     }
588 }
589
590 }
591 }
592 }