2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "DownloadManager.h"
19 #include <API/Download/URLDownload.h>
20 #include <dpl/log/log.h>
22 using namespace TizenApis::Api::Download;
23 using namespace WrtDeviceApis::Commons;
29 DownloadManager::DownloadManager()
33 DownloadManager::~DownloadManager()
37 static void on_url_download_started(url_download_h download, const char *content_name, const char *mime_type, void *user_data)
39 LogInfo("Download started with handle: "<<download<<", content_name: "<<content_name<<", mime_type: "<<mime_type);
43 OnDownloadStateChangedPtr eventPtr(new OnDownloadStateChanged());
44 DownloadManager* thisDownloadManager = (DownloadManager*) user_data;
46 eventPtr->setDownloadId((long) download);
47 eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_QUEUED);
48 eventPtr->setResult(true);
50 thisDownloadManager->m_changeEmitters[download]->emit(eventPtr);
54 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
58 static void on_url_download_paused(url_download_h download, void *user_data)
60 LogInfo("Download paused with handle: "<<download);
64 OnDownloadStateChangedPtr eventPtr(new OnDownloadStateChanged());
65 DownloadManager* thisDownloadManager = (DownloadManager*) user_data;
67 eventPtr->setDownloadId((long) download);
68 eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_PAUSED);
69 eventPtr->setResult(true);
71 thisDownloadManager->m_changeEmitters[download]->emit(eventPtr);
75 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
79 static void on_url_download_completed(url_download_h download, const char * path, void *user_data)
81 LogInfo("Download completed with handle: "<<download);
85 OnDownloadStateChangedPtr eventPtr(new OnDownloadStateChanged());
86 DownloadManager* thisDownloadManager = (DownloadManager*) user_data;
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);
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);
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);
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);
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);
112 std::string fullPath(path);
113 found = fullPath.find_last_of("/\\");
114 eventPtr->setFileName(fullPath.substr(found+1));
115 LogInfo("Full path: "<<path<<", file name: "<<eventPtr->getFileName());
118 eventPtr->setDownloadId((long) download);
119 eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_COMPLETED);
120 eventPtr->setResult(true);
122 thisDownloadManager->m_changeEmitters[download]->emit(eventPtr);
123 thisDownloadManager->m_changeEmitters.erase(download);
125 ret = url_download_destroy(download);
126 if (ret != URL_DOWNLOAD_ERROR_NONE) {
127 ThrowMsg(PlatformException, "Platform error while destroying download handle: "<<ret);
132 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
136 static void on_url_download_stopped(url_download_h download, url_download_error_e error, void *user_data)
138 LogInfo("Download stopped with handle: "<<download<<", error: "<<error);
140 OnDownloadStateChangedPtr eventPtr(new OnDownloadStateChanged());
141 DownloadManager* thisDownloadManager = (DownloadManager*) user_data;
146 ret = url_download_unset_started_cb(download);
147 if (ret != URL_DOWNLOAD_ERROR_NONE) {
148 ThrowMsg(PlatformException, "Platform error while unsetting started cb: "<<ret);
150 ret = url_download_unset_paused_cb(download);
151 if (ret != URL_DOWNLOAD_ERROR_NONE) {
152 ThrowMsg(PlatformException, "Platform error while unsetting paused cb: "<<ret);
154 ret = url_download_unset_stopped_cb(download);
155 if (ret != URL_DOWNLOAD_ERROR_NONE) {
156 ThrowMsg(PlatformException, "Platform error while unsetting stopped cb: "<<ret);
158 ret = url_download_unset_completed_cb(download);
159 if (ret != URL_DOWNLOAD_ERROR_NONE) {
160 ThrowMsg(PlatformException, "Platform error while unsetting completed cb: "<<ret);
162 ret = url_download_unset_progress_cb(download);
163 if (ret != URL_DOWNLOAD_ERROR_NONE) {
164 ThrowMsg(PlatformException, "Platform error while unsetting progress cb: "<<ret);
167 eventPtr->setDownloadId((long) download);
169 if( URL_DOWNLOAD_ERROR_NONE==error ) {
170 eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_ABORTED);
172 eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_FAILED);
173 eventPtr->setExceptionCode(ExceptionCodes::PlatformException);
175 eventPtr->setResult(true);
179 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
180 eventPtr->setResult(false);
181 eventPtr->setExceptionCode(ExceptionCodes::UnknownException);
184 thisDownloadManager->m_changeEmitters[download]->emit(eventPtr);
185 thisDownloadManager->m_changeEmitters.erase(download);
187 ret = url_download_destroy(download);
188 if (ret != URL_DOWNLOAD_ERROR_NONE) {
189 LogWarning("Platform error while destroying download handle: "<<ret);
193 static void on_url_download_progress(url_download_h download, unsigned long long received, unsigned long long total, void *user_data)
195 LogInfo("Download progress with handle: "<<download<<", received: "<<received<<", total: "<<total);
199 OnDownloadStateChangedPtr eventPtr(new OnDownloadStateChanged());
200 DownloadManager* thisDownloadManager = (DownloadManager*) user_data;
202 eventPtr->setDownloadId((long) download);
203 eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_DOWNLOADING);
204 eventPtr->setReceivedSize(received);
205 eventPtr->setTotalSize(total);
206 eventPtr->setResult(true);
208 thisDownloadManager->m_changeEmitters[download]->emit(eventPtr);
212 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
216 void DownloadManager::OnRequestReceived(const IEventStartDownloadPtr &event)
222 URLDownloadPtr urlDownload;
223 urlDownload = event->getDownloadObject();
225 ThrowMsg(NullPointerException, "Download object is NULL.");
228 std::string url = urlDownload->getUrl();
229 std::string destination = urlDownload->getDestination();
230 std::string fileName = urlDownload->getFileName();
231 LogInfo("url: "<<url<<", destination: "<<destination<<", fileName: "<<fileName);
234 url_download_h download;
236 ret = url_download_create(&download);
237 if (ret != URL_DOWNLOAD_ERROR_NONE) {
238 ThrowMsg(PlatformException, "Platform error while creating url download: "<<ret);
241 ret = url_download_set_url(download, url.c_str());
242 if (ret != URL_DOWNLOAD_ERROR_NONE) {
243 ThrowMsg(PlatformException, "Platform error while setting url: "<<ret);
246 if(!destination.empty()) {
247 ret = url_download_set_destination(download, destination.c_str());
248 if (ret != URL_DOWNLOAD_ERROR_NONE) {
249 ThrowMsg(PlatformException, "Platform error while setting destination: "<<ret);
253 if(!fileName.empty()) {
254 ret = url_download_set_file_name(download, fileName.c_str());
255 if (ret != URL_DOWNLOAD_ERROR_NONE) {
256 ThrowMsg(PlatformException, "Platform error while setting file name: "<<ret);
260 if(event->getEmitter()) {
261 ret = url_download_set_started_cb(download, on_url_download_started, this);
262 if (ret != URL_DOWNLOAD_ERROR_NONE) {
263 ThrowMsg(PlatformException, "Platform error while setting started cb: "<<ret);
265 ret = url_download_set_paused_cb(download, on_url_download_paused, this);
266 if (ret != URL_DOWNLOAD_ERROR_NONE) {
267 ThrowMsg(PlatformException, "Platform error while setting paused cb: "<<ret);
269 ret = url_download_set_stopped_cb(download, on_url_download_stopped, this);
270 if (ret != URL_DOWNLOAD_ERROR_NONE) {
271 ThrowMsg(PlatformException, "Platform error while setting stopped cb: "<<ret);
273 ret = url_download_set_completed_cb(download, on_url_download_completed, this);
274 if (ret != URL_DOWNLOAD_ERROR_NONE) {
275 ThrowMsg(PlatformException, "Platform error while setting completed cb: "<<ret);
277 ret = url_download_set_progress_cb(download, on_url_download_progress, this);
278 if (ret != URL_DOWNLOAD_ERROR_NONE) {
279 ThrowMsg(PlatformException, "Platform error while setting progress cb: "<<ret);
283 ret = url_download_start(download, &id);
284 if (ret != URL_DOWNLOAD_ERROR_NONE) {
285 ThrowMsg(PlatformException, "Platform error while starting download: "<<ret);
288 LogInfo("id: "<<id<<", handle: "<<download);
290 if(event->getEmitter()) {
291 LogInfo("Attaching the emitter...");
292 m_changeEmitters[download] = event->getEmitter();
294 LogInfo("Adding the handle...");
295 m_changeEmitters[download];
298 event->setDownloadId((long)download);
300 event->setResult(true);
304 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
305 event->setResult(false);
306 event->setExceptionCode(ExceptionCodes::PlatformException);
310 void DownloadManager::OnRequestReceived(const IEventSetListenerPtr &event)
316 long downloadId = event->getDownloadId();
319 url_download_h download;
321 ret = url_download_create_by_id((int)downloadId, &download);
322 if (ret != URL_DOWNLOAD_ERROR_NONE) {
323 ThrowMsg(NotFoundException, "Platform error while geetting download handle: "<<ret);
326 LogInfo("handle: "<<download);
328 ret = url_download_set_started_cb(download, on_url_download_started, this);
329 if (ret != URL_DOWNLOAD_ERROR_NONE) {
330 ThrowMsg(PlatformException, "Platform error while setting started cb: "<<ret);
332 ret = url_download_set_paused_cb(download, on_url_download_paused, this);
333 if (ret != URL_DOWNLOAD_ERROR_NONE) {
334 ThrowMsg(PlatformException, "Platform error while setting paused cb: "<<ret);
336 ret = url_download_set_stopped_cb(download, on_url_download_stopped, this);
337 if (ret != URL_DOWNLOAD_ERROR_NONE) {
338 ThrowMsg(PlatformException, "Platform error while setting stopped cb: "<<ret);
340 ret = url_download_set_completed_cb(download, on_url_download_completed, this);
341 if (ret != URL_DOWNLOAD_ERROR_NONE) {
342 ThrowMsg(PlatformException, "Platform error while setting completed cb: "<<ret);
344 ret = url_download_set_progress_cb(download, on_url_download_progress, this);
345 if (ret != URL_DOWNLOAD_ERROR_NONE) {
346 ThrowMsg(PlatformException, "Platform error while setting progress cb: "<<ret);
349 if(event->getEmitter()) {
350 LogInfo("Attaching the emitter with handle: "<<download);
351 m_changeEmitters[download] = event->getEmitter();
354 event->setResult(true);
356 Catch(NotFoundException)
358 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
359 event->setResult(false);
360 event->setExceptionCode(ExceptionCodes::NotFoundException);
364 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
365 event->setResult(false);
366 event->setExceptionCode(ExceptionCodes::PlatformException);
370 void DownloadManager::OnRequestReceived(const IEventAbortDownloadPtr &event)
376 long downloadId = event->getDownloadId();
380 std::map<url_download_h, OnDownloadStateChangedEmitterPtr>::iterator it;
381 it = m_changeEmitters.find((url_download_h)downloadId);
382 if( m_changeEmitters.end()==it ) {
383 ThrowMsg(NotFoundException, "Error while getting download handle.");
386 ret = url_download_stop((url_download_h)downloadId);
387 if (ret != URL_DOWNLOAD_ERROR_NONE) {
388 ThrowMsg(PlatformException, "Platform error while stopping download: "<<ret);
391 event->setResult(true);
393 Catch(NotFoundException)
395 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
396 event->setResult(false);
397 event->setExceptionCode(ExceptionCodes::NotFoundException);
401 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
402 event->setResult(false);
403 event->setExceptionCode(ExceptionCodes::UnknownException);
407 void DownloadManager::OnRequestReceived(const IEventPauseDownloadPtr &event)
413 long downloadId = event->getDownloadId();
417 std::map<url_download_h, OnDownloadStateChangedEmitterPtr>::iterator it;
418 it = m_changeEmitters.find((url_download_h)downloadId);
419 if( m_changeEmitters.end()==it ) {
420 ThrowMsg(NotFoundException, "Error while getting download handle.");
423 ret = url_download_pause((url_download_h)downloadId);
424 if (ret != URL_DOWNLOAD_ERROR_NONE) {
425 ThrowMsg(PlatformException, "Platform error while pausing download: "<<ret);
428 event->setResult(true);
430 Catch(NotFoundException)
432 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
433 event->setResult(false);
434 event->setExceptionCode(ExceptionCodes::NotFoundException);
438 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
439 event->setResult(false);
440 event->setExceptionCode(ExceptionCodes::UnknownException);
444 void DownloadManager::OnRequestReceived(const IEventResumeDownloadPtr &event)
450 long downloadId = event->getDownloadId();
454 std::map<url_download_h, OnDownloadStateChangedEmitterPtr>::iterator it;
455 it = m_changeEmitters.find((url_download_h)downloadId);
456 if( m_changeEmitters.end()==it ) {
457 ThrowMsg(NotFoundException, "Error while getting download handle.");
460 ret = url_download_start((url_download_h)downloadId, &id);
461 if (ret != URL_DOWNLOAD_ERROR_NONE) {
462 ThrowMsg(PlatformException, "Platform error while resuming download: "<<ret);
467 event->setResult(true);
469 Catch(NotFoundException)
471 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
472 event->setResult(false);
473 event->setExceptionCode(ExceptionCodes::NotFoundException);
477 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
478 event->setResult(false);
479 event->setExceptionCode(ExceptionCodes::UnknownException);
483 void DownloadManager::OnRequestReceived(const IEventGetStatePtr &event)
489 long downloadId = event->getDownloadId();
492 url_download_state_e state;
494 std::map<url_download_h, OnDownloadStateChangedEmitterPtr>::iterator it;
495 it = m_changeEmitters.find((url_download_h)downloadId);
496 if( m_changeEmitters.end()==it ) {
497 ThrowMsg(NotFoundException, "Error while getting download handle.");
500 ret = url_download_get_state((url_download_h)downloadId, &state);
501 if (ret != URL_DOWNLOAD_ERROR_NONE) {
502 ThrowMsg(PlatformException, "Platform error while getting state: "<<ret);
505 if( URL_DOWNLOAD_STATE_READY ==state ) {
506 event->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_QUEUED);
507 } else if( URL_DOWNLOAD_STATE_DOWNLOADING==state ) {
508 event->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_DOWNLOADING);
509 } else if( URL_DOWNLOAD_STATE_PAUSED==state ) {
510 event->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_PAUSED);
511 } else if( URL_DOWNLOAD_STATE_COMPLETED==state ) {
512 event->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_COMPLETED);
513 } else if( URL_DOWNLOAD_STATE_FAILED==state ) {
514 event->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_FAILED);
516 ThrowMsg(PlatformException, "Wrong download state: "<<state);
519 event->setResult(true);
521 Catch(NotFoundException)
523 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
524 event->setResult(false);
525 event->setExceptionCode(ExceptionCodes::NotFoundException);
529 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
530 event->setResult(false);
531 event->setExceptionCode(ExceptionCodes::UnknownException);
535 void DownloadManager::OnRequestReceived(const IEventGetURLDownloadPtr &event)
541 long downloadId = event->getDownloadId();
545 char* destination = NULL;
547 std::map<url_download_h, OnDownloadStateChangedEmitterPtr>::iterator it;
548 it = m_changeEmitters.find((url_download_h)downloadId);
549 if( m_changeEmitters.end()==it ) {
550 ThrowMsg(NotFoundException, "Error while getting download handle.");
553 ret = url_download_get_url((url_download_h)downloadId, &url);
554 if (ret != URL_DOWNLOAD_ERROR_NONE) {
555 ThrowMsg(PlatformException, "Platform error while getting url: "<<ret);
558 ret = url_download_get_destination((url_download_h)downloadId, &destination);
559 if (ret != URL_DOWNLOAD_ERROR_NONE) {
560 ThrowMsg(PlatformException, "Platform error while getting destination: "<<ret);
563 URLDownloadPtr urlDownload( new URLDownload() );
565 urlDownload->setUrl(std::string(url));
569 urlDownload->setDestination(std::string(destination));
573 LogInfo("url: "<<urlDownload->getUrl()<<", destination "<<urlDownload->getDestination());
575 event->setDownloadObject(urlDownload);
577 event->setResult(true);
579 Catch(NotFoundException)
581 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
582 event->setResult(false);
583 event->setExceptionCode(ExceptionCodes::NotFoundException);
587 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
588 event->setResult(false);
589 event->setExceptionCode(ExceptionCodes::UnknownException);