Update change log and spec for wrt-plugins-tizen_0.2.73
[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);
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         if( path ) {
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());
116         }
117
118         eventPtr->setDownloadId((long) download);
119         eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_COMPLETED);
120         eventPtr->setResult(true);
121
122         thisDownloadManager->m_changeEmitters[download]->emit(eventPtr);
123         thisDownloadManager->m_changeEmitters.erase(download);
124
125         ret = url_download_destroy(download);
126         if (ret != URL_DOWNLOAD_ERROR_NONE) {
127             ThrowMsg(PlatformException, "Platform error while destroying download handle: "<<ret);
128         }
129     }
130     Catch (Exception)
131     {
132         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
133     }
134 }
135
136 static void on_url_download_stopped(url_download_h download, url_download_error_e error, void *user_data)
137 {
138     LogInfo("Download stopped with handle: "<<download<<", error: "<<error);
139
140     OnDownloadStateChangedPtr eventPtr(new OnDownloadStateChanged());
141     DownloadManager* thisDownloadManager = (DownloadManager*) user_data;
142     int ret;
143
144     Try
145     {
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);
149         }
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);
153         }
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);
157         }
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);
161         }
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);
165         }
166
167         eventPtr->setDownloadId((long) download);
168
169         if( URL_DOWNLOAD_ERROR_NONE==error ) {
170             eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_ABORTED);
171         } else {
172             eventPtr->setDownloadState(TIZEN_ENUM_DOWNLOAD_STATE_FAILED);
173             eventPtr->setExceptionCode(ExceptionCodes::PlatformException);
174         }
175         eventPtr->setResult(true);
176     }
177     Catch (Exception)
178     {
179         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
180         eventPtr->setResult(false);
181         eventPtr->setExceptionCode(ExceptionCodes::UnknownException);
182     }
183
184     thisDownloadManager->m_changeEmitters[download]->emit(eventPtr);
185     thisDownloadManager->m_changeEmitters.erase(download);
186
187     ret = url_download_destroy(download);
188     if (ret != URL_DOWNLOAD_ERROR_NONE) {
189         LogWarning("Platform error while destroying download handle: "<<ret);
190     }
191 }
192
193 static void on_url_download_progress(url_download_h download, unsigned long long received, unsigned long long total, void *user_data)
194 {
195     LogInfo("Download progress with handle: "<<download<<", received: "<<received<<", total: "<<total);
196
197     Try
198     {   
199         OnDownloadStateChangedPtr eventPtr(new OnDownloadStateChanged());
200         DownloadManager* thisDownloadManager = (DownloadManager*) user_data;
201
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);
207
208         thisDownloadManager->m_changeEmitters[download]->emit(eventPtr);
209     }
210     Catch (Exception)
211     {
212         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
213     }
214 }
215
216 void DownloadManager::OnRequestReceived(const IEventStartDownloadPtr &event)
217 {
218     LogDebug("entered");
219
220     Try
221     {
222         URLDownloadPtr urlDownload;
223         urlDownload = event->getDownloadObject();
224         if (!urlDownload) {
225             ThrowMsg(NullPointerException, "Download object is NULL.");
226         }
227
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);
232
233         int ret, id;
234         url_download_h download;
235
236         ret = url_download_create(&download);
237         if (ret != URL_DOWNLOAD_ERROR_NONE) {
238             ThrowMsg(PlatformException, "Platform error while creating url download: "<<ret);
239         }
240
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);
244         }
245
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);
250             }
251         }
252
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);
257             }
258         }
259
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);
264             }
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);
268             }
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);
272             }
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);
276             }
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);
280             }
281         }
282
283         ret = url_download_start(download, &id);
284         if (ret != URL_DOWNLOAD_ERROR_NONE) {
285             ThrowMsg(PlatformException, "Platform error while starting download: "<<ret);
286         }
287
288         LogInfo("id: "<<id<<", handle: "<<download);
289
290         if(event->getEmitter()) {
291             LogInfo("Attaching the emitter...");
292             m_changeEmitters[download] = event->getEmitter();
293         } else {
294             LogInfo("Adding the handle...");
295             m_changeEmitters[download];
296         }
297
298         event->setDownloadId((long)download);
299
300         event->setResult(true);
301     }
302     Catch(Exception)
303     {
304                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
305         event->setResult(false);
306         event->setExceptionCode(ExceptionCodes::PlatformException);
307     }
308 }
309
310 void DownloadManager::OnRequestReceived(const IEventSetListenerPtr &event)
311 {
312     LogDebug("entered");
313
314     Try
315     {
316         long downloadId = event->getDownloadId();
317
318         int ret;
319         url_download_h download;
320
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);
324         }
325
326         LogInfo("handle: "<<download);
327
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);
331         }
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);
335         }
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);
339         }
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);
343         }
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);
347         }
348
349         if(event->getEmitter()) {
350             LogInfo("Attaching the emitter with handle: "<<download);
351             m_changeEmitters[download] = event->getEmitter();
352         }
353
354         event->setResult(true);
355     }
356     Catch(NotFoundException)
357     {
358         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
359         event->setResult(false);
360         event->setExceptionCode(ExceptionCodes::NotFoundException);
361     }
362     Catch(Exception)
363     {
364                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
365         event->setResult(false);
366         event->setExceptionCode(ExceptionCodes::PlatformException);
367     }
368 }
369
370 void DownloadManager::OnRequestReceived(const IEventAbortDownloadPtr &event)
371 {
372     LogDebug("entered");
373
374     Try
375     {
376         long downloadId = event->getDownloadId();
377
378         int ret;
379
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.");
384         }
385
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);
389         }
390
391         event->setResult(true);
392     }
393     Catch(NotFoundException)
394     {
395         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
396         event->setResult(false);
397         event->setExceptionCode(ExceptionCodes::NotFoundException);
398     }
399     Catch(Exception)
400     {
401                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
402         event->setResult(false);
403         event->setExceptionCode(ExceptionCodes::UnknownException);
404     }
405 }
406
407 void DownloadManager::OnRequestReceived(const IEventPauseDownloadPtr &event)
408 {
409     LogDebug("entered");
410
411     Try
412     {
413         long downloadId = event->getDownloadId();
414
415         int ret;
416
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.");
421         }
422
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);
426         }
427
428         event->setResult(true);
429     }
430     Catch(NotFoundException)
431     {
432         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
433         event->setResult(false);
434         event->setExceptionCode(ExceptionCodes::NotFoundException);
435     }
436     Catch(Exception)
437     {
438                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
439         event->setResult(false);
440         event->setExceptionCode(ExceptionCodes::UnknownException);
441     }
442 }
443
444 void DownloadManager::OnRequestReceived(const IEventResumeDownloadPtr &event)
445 {
446     LogDebug("entered");
447
448     Try
449     {
450         long downloadId = event->getDownloadId();
451
452         int ret, id;
453
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.");
458         }
459
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);
463         }
464
465         LogInfo("id: "<<id);
466
467         event->setResult(true);
468     }
469     Catch(NotFoundException)
470     {
471         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
472         event->setResult(false);
473         event->setExceptionCode(ExceptionCodes::NotFoundException);
474     }
475     Catch(Exception)
476     {
477                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
478         event->setResult(false);
479         event->setExceptionCode(ExceptionCodes::UnknownException);
480     }
481 }
482
483 void DownloadManager::OnRequestReceived(const IEventGetStatePtr &event)
484 {
485     LogDebug("entered");
486
487     Try
488     {
489         long downloadId = event->getDownloadId();
490
491         int ret;
492         url_download_state_e state;
493
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.");
498         }
499
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);
503         }
504
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);
515         } else {
516             ThrowMsg(PlatformException, "Wrong download state: "<<state);
517         }
518
519         event->setResult(true);
520     }
521     Catch(NotFoundException)
522     {
523         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
524         event->setResult(false);
525         event->setExceptionCode(ExceptionCodes::NotFoundException);
526     }
527     Catch(Exception)
528     {
529                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
530         event->setResult(false);
531         event->setExceptionCode(ExceptionCodes::UnknownException);
532     }
533 }
534
535 void DownloadManager::OnRequestReceived(const IEventGetURLDownloadPtr &event)
536 {
537     LogDebug("entered");
538
539     Try
540     {
541         long downloadId = event->getDownloadId();
542
543         int ret;
544         char* url = NULL;
545         char* destination = NULL;
546
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.");
551         }
552
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);
556         }
557
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);
561         }
562
563         URLDownloadPtr urlDownload( new URLDownload() );
564         if(url) {
565             urlDownload->setUrl(std::string(url));
566             free(url);
567         }
568         if(destination) {
569             urlDownload->setDestination(std::string(destination));
570             free(destination);
571         }
572
573         LogInfo("url: "<<urlDownload->getUrl()<<", destination "<<urlDownload->getDestination());
574
575         event->setDownloadObject(urlDownload);
576
577         event->setResult(true);
578     }
579     Catch(NotFoundException)
580     {
581         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
582         event->setResult(false);
583         event->setExceptionCode(ExceptionCodes::NotFoundException);
584     }
585     Catch(Exception)
586     {
587                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
588         event->setResult(false);
589         event->setExceptionCode(ExceptionCodes::UnknownException);
590     }
591 }
592
593 }
594 }
595 }