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<<", path: "<<path);
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);
111 std::string fullPath(path);
112 found = fullPath.find_last_of("/\\");
113 eventPtr->setFileName(fullPath.substr(found+1));
115 eventPtr->setDownloadId((long) download);
116 eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_COMPLETED);
117 eventPtr->setResult(true);
119 thisDownloadManager->m_changeEmitters[download]->emit(eventPtr);
120 //thisDownloadManager->m_changeEmitters.erase(download);
122 /*ret = url_download_destroy(download);
123 if (ret != URL_DOWNLOAD_ERROR_NONE) {
124 ThrowMsg(PlatformException, "Platform error while destroying download handle: "<<ret);
129 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
133 static void on_url_download_stopped(url_download_h download, url_download_error_e error, void *user_data)
135 LogInfo("Download stopped with handle: "<<download<<", error: "<<error);
137 OnDownloadStateChangedPtr eventPtr(new OnDownloadStateChanged());
138 DownloadManager* thisDownloadManager = (DownloadManager*) user_data;
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);
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);
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);
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);
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);
164 eventPtr->setDownloadId((long) download);
166 if( URL_DOWNLOAD_ERROR_NONE==error ) {
167 eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_ABORTED);
169 eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_FAILED);
170 eventPtr->setExceptionCode(ExceptionCodes::PlatformException);
172 eventPtr->setResult(true);
176 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
177 eventPtr->setResult(false);
178 eventPtr->setExceptionCode(ExceptionCodes::UnknownException);
181 thisDownloadManager->m_changeEmitters[download]->emit(eventPtr);
182 //thisDownloadManager->m_changeEmitters.erase(download);
184 /*ret = url_download_destroy(download);
185 if (ret != URL_DOWNLOAD_ERROR_NONE) {
186 LogWarning("Platform error while destroying download handle: "<<ret);
190 static void on_url_download_progress(url_download_h download, unsigned long long received, unsigned long long total, void *user_data)
192 LogInfo("Download progress with handle: "<<download<<", received: "<<received<<", total: "<<total);
196 OnDownloadStateChangedPtr eventPtr(new OnDownloadStateChanged());
197 DownloadManager* thisDownloadManager = (DownloadManager*) user_data;
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);
205 thisDownloadManager->m_changeEmitters[download]->emit(eventPtr);
209 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
213 void DownloadManager::OnRequestReceived(const IEventStartDownloadPtr &event)
219 URLDownloadPtr urlDownload;
220 urlDownload = event->getDownloadObject();
222 ThrowMsg(NullPointerException, "Download object is NULL.");
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);
231 url_download_h download;
233 ret = url_download_create(&download);
234 if (ret != URL_DOWNLOAD_ERROR_NONE) {
235 ThrowMsg(PlatformException, "Platform error while creating url download: "<<ret);
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);
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);
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);
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);
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);
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);
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);
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);
280 ret = url_download_start(download, &id);
281 if (ret != URL_DOWNLOAD_ERROR_NONE) {
282 ThrowMsg(PlatformException, "Platform error while starting download: "<<ret);
285 LogInfo("id: "<<id<<", handle: "<<download);
287 if(event->getEmitter()) {
288 LogInfo("Attaching the emitter...");
289 m_changeEmitters[download] = event->getEmitter();
291 LogInfo("Adding the handle...");
292 m_changeEmitters[download];
295 event->setDownloadId((long)download);
297 event->setResult(true);
301 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
302 event->setResult(false);
303 event->setExceptionCode(ExceptionCodes::PlatformException);
307 void DownloadManager::OnRequestReceived(const IEventSetListenerPtr &event)
313 long downloadId = event->getDownloadId();
316 url_download_h download;
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);
323 LogInfo("handle: "<<download);
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);
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);
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);
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);
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);
346 if(event->getEmitter()) {
347 LogInfo("Attaching the emitter with handle: "<<download);
348 m_changeEmitters[download] = event->getEmitter();
351 event->setResult(true);
353 Catch(NotFoundException)
355 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
356 event->setResult(false);
357 event->setExceptionCode(ExceptionCodes::NotFoundException);
361 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
362 event->setResult(false);
363 event->setExceptionCode(ExceptionCodes::PlatformException);
367 void DownloadManager::OnRequestReceived(const IEventAbortDownloadPtr &event)
373 long downloadId = event->getDownloadId();
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.");
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);
388 event->setResult(true);
390 Catch(NotFoundException)
392 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
393 event->setResult(false);
394 event->setExceptionCode(ExceptionCodes::NotFoundException);
398 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
399 event->setResult(false);
400 event->setExceptionCode(ExceptionCodes::UnknownException);
404 void DownloadManager::OnRequestReceived(const IEventPauseDownloadPtr &event)
410 long downloadId = event->getDownloadId();
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.");
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);
425 event->setResult(true);
427 Catch(NotFoundException)
429 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
430 event->setResult(false);
431 event->setExceptionCode(ExceptionCodes::NotFoundException);
435 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
436 event->setResult(false);
437 event->setExceptionCode(ExceptionCodes::UnknownException);
441 void DownloadManager::OnRequestReceived(const IEventResumeDownloadPtr &event)
447 long downloadId = event->getDownloadId();
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.");
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);
464 event->setResult(true);
466 Catch(NotFoundException)
468 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
469 event->setResult(false);
470 event->setExceptionCode(ExceptionCodes::NotFoundException);
474 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
475 event->setResult(false);
476 event->setExceptionCode(ExceptionCodes::UnknownException);
480 void DownloadManager::OnRequestReceived(const IEventGetStatePtr &event)
486 long downloadId = event->getDownloadId();
489 url_download_state_e state;
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.");
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);
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);
513 ThrowMsg(PlatformException, "Wrong download state: "<<state);
516 event->setResult(true);
518 Catch(NotFoundException)
520 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
521 event->setResult(false);
522 event->setExceptionCode(ExceptionCodes::NotFoundException);
526 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
527 event->setResult(false);
528 event->setExceptionCode(ExceptionCodes::UnknownException);
532 void DownloadManager::OnRequestReceived(const IEventGetURLDownloadPtr &event)
538 long downloadId = event->getDownloadId();
542 char* destination = NULL;
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.");
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);
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);
560 URLDownloadPtr urlDownload( new URLDownload() );
562 urlDownload->setUrl(std::string(url));
566 urlDownload->setDestination(std::string(destination));
570 LogInfo("url: "<<urlDownload->getUrl()<<", destination "<<urlDownload->getDestination());
572 event->setDownloadObject(urlDownload);
574 event->setResult(true);
576 Catch(NotFoundException)
578 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
579 event->setResult(false);
580 event->setExceptionCode(ExceptionCodes::NotFoundException);
584 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
585 event->setResult(false);
586 event->setExceptionCode(ExceptionCodes::UnknownException);