525926946a3ad573ba2502498db342bfc48720e5
[platform/framework/native/content.git] / src / FCnt_DownloadManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FCnt_DownloadManagerImpl.cpp
20  * @brief       This is the implementation file for the _DownloadManagerImpl class.
21  *
22  */
23
24 #include <errno.h>
25 #include <unistd.h>
26 #include <cstdlib>
27 #include <pthread.h>
28 #include <unique_ptr.h>
29
30 #include <download.h>
31
32 #include <FBaseSysLog.h>
33 #include <FBase_StringConverter.h>
34 #include <FBaseInteger.h>
35 #include <FBaseRtIEventArg.h>
36 #include <FBaseColIMap.h>
37 #include <FApp_AppInfo.h>
38
39 #include <FCntDownloadRequest.h>
40 #include <FCntIDownloadListener.h>
41
42 #include <FCnt_DownloadRequestImpl.h>
43 #include "FCnt_DownloadManagerImpl.h"
44
45 using namespace std;
46
47 using namespace Tizen::Base;
48 using namespace Tizen::Base::Runtime;
49 using namespace Tizen::Base::Collection;
50
51 namespace Tizen { namespace Content
52 {
53
54 static const int STATE_NONE = 0;
55 static const int STATE_QUEUED = 1;
56 static const int STATE_DOWNLOADING = 2;
57 static const int STATE_PAUSED = 3;
58 static const int STATE_CANCELLED = 4;
59 static const int STATE_COMPLETED = 5;
60 static const int STATE_FAILED = 6;
61
62
63 static const int SLP_STATE_NONE = 0;
64 static const int SLP_STATE_READY = 1;
65 static const int SLP_STATE_QUEUED = 2;
66 static const int SLP_STATE_DOWNLOADING = 3;
67 static const int SLP_STATE_PAUSED = 4;
68 static const int SLP_STATE_COMPLETED = 5;
69 static const int SLP_STATE_FAILED = 6;
70 static const int SLP_STATE_CANCELLED = 7;
71
72 _DownloadManagerImpl* _DownloadManagerImpl::__pInstance = null;
73
74 class _DownloadEventArg
75         : public IEventArg
76 {
77 public:
78         _DownloadEventArg()
79                 : __id(-1)
80                 , __state(STATE_NONE)
81                 , __result(-1)
82                 , __received(-1)
83                 , __total(-1)
84         {
85         }
86         RequestId __id;
87         int __state;
88         String __path;
89         result __result;
90         String __errorCode;
91         unsigned long long __received;
92         unsigned long long __total;
93 };
94
95 class _DownloadEvent
96         : public Event
97 {
98 protected:
99         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
100         {
101                 IDownloadListener* pListener = dynamic_cast<IDownloadListener*> (&listener);
102                 if (pListener != null)
103                 {
104                         const _DownloadEventArg* pArg = dynamic_cast<const _DownloadEventArg*>(&arg);
105                         if (pArg != null)
106                         {
107                                 switch(pArg->__state)
108                                 {
109                                         case STATE_NONE:
110                                         case STATE_QUEUED:
111                                                 break;
112
113                                         case STATE_DOWNLOADING:
114                                                 pListener->OnDownloadInProgress(pArg->__id, pArg->__received, pArg->__total);
115                                                 break;
116
117                                         case STATE_PAUSED:
118                                                 pListener->OnDownloadPaused(pArg->__id);
119                                                 break;
120
121                                         case STATE_COMPLETED:
122                                                 pListener->OnDownloadCompleted(pArg->__id, pArg->__path);
123                                                 break;
124
125                                         case STATE_FAILED:
126                                                 pListener->OnDownloadFailed(pArg->__id, pArg->__result, pArg->__errorCode);
127                                                 break;
128
129                                         case STATE_CANCELLED:
130                                                 pListener->OnDownloadCanceled(pArg->__id);
131                                                 break;
132
133                                         default:
134                                                 break;
135                                 }
136                         }
137                 }
138         }
139 };
140
141 static void
142 OnStateChanged(int download_id, download_state_e state, void* data)
143 {
144         SysLog(NID_CNT, "OnStateChanged, id = %d, state = %d", download_id, state);
145
146         RequestId reqId = (long)download_id;
147
148         _DownloadManagerImpl* pDMImpl = (_DownloadManagerImpl*)data;
149
150         if (!data || !pDMImpl->__pEvent)
151         {
152                 return;
153         }
154
155         switch(state)
156         {
157                 case SLP_STATE_NONE:
158                 case SLP_STATE_QUEUED:
159                 case SLP_STATE_DOWNLOADING:
160                         break;
161
162                 case SLP_STATE_PAUSED:
163                 {
164                         _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
165                         pEventArg->__id = reqId;
166                         pEventArg->__state = STATE_PAUSED;
167
168                         pDMImpl->__pEvent->Fire(*pEventArg);
169
170                         break;
171                 }
172
173                 case SLP_STATE_COMPLETED:
174                 {
175                         _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
176                         pEventArg->__id = reqId;
177                         pEventArg->__state = STATE_COMPLETED;
178
179                         char* path = null;
180                         download_get_downloaded_file_path(download_id, &path);
181                         pEventArg->__path = path;
182
183                         delete[] path;
184
185                         pDMImpl->__pEvent->Fire(*pEventArg);
186
187                         // Remove the resource from url_download
188                         pDMImpl->DestroyResources(reqId);
189
190                         break;
191                 }
192
193                 case SLP_STATE_FAILED:
194                 {
195                         _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
196                         pEventArg->__id = reqId;
197                         pEventArg->__state = STATE_FAILED;
198
199                         download_error_e error;
200                         download_get_error(download_id, &error);
201                         pEventArg->__result = pDMImpl->ConvertToResult(error);
202
203                         int http_status = 0;
204                         download_get_http_status(download_id, &http_status);
205                         pEventArg->__errorCode = Integer::ToString(http_status);
206
207                         pDMImpl->__pEvent->Fire(*pEventArg);
208
209                         // Comment out due to resume the failed request
210                         //pDMImpl->DestroyResources(reqId);
211
212                         break;
213                 }
214
215                 case SLP_STATE_CANCELLED:
216                 {
217                         _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
218                         pEventArg->__id = reqId;
219                         pEventArg->__state = STATE_CANCELLED;
220
221                         pDMImpl->__pEvent->Fire(*pEventArg);
222
223                         // Remove the resource from url_download
224                         pDMImpl->DestroyResources(reqId);
225
226                         break;
227                 }
228
229                 default:
230                         break;
231         }
232
233 }
234
235 static void
236 OnProgress(int download_id, unsigned long long received, void* data)
237 {
238         RequestId reqId = (long)download_id;
239
240         _DownloadManagerImpl* pDMImpl = (_DownloadManagerImpl*)data;
241
242         if (data && pDMImpl->__pEvent)
243         {
244                 _DownloadEventArg* pEventArg = new (std::nothrow) _DownloadEventArg();
245                 pEventArg->__id = reqId;
246                 pEventArg->__state = STATE_DOWNLOADING;
247                 pEventArg->__received = received;
248
249                 unsigned long long total = 0;
250                 download_get_content_size(download_id, &total);
251                 pEventArg->__total = total;
252
253                 SysLog(NID_CNT, "OnProgress, id = %d, received = %lld, total = %lld", download_id, received, total);
254
255                 pDMImpl->__pEvent->Fire(*pEventArg);
256         }
257 }
258
259
260 _DownloadManagerImpl::_DownloadManagerImpl(void)
261         : __pEvent(null)
262 {
263 }
264
265 _DownloadManagerImpl::~_DownloadManagerImpl(void)
266 {
267         if (__pEvent)
268         {
269                 delete __pEvent;
270         }
271 }
272
273 result
274 _DownloadManagerImpl::Construct(void)
275 {
276         result r = E_SUCCESS;
277
278         r = __handleMap.Construct(100, 0);
279         SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "Failed to initialize a download manager.");
280
281         return E_SUCCESS;
282 }
283
284 void
285 _DownloadManagerImpl::InitSingleton(void)
286 {
287         unique_ptr<_DownloadManagerImpl> pImpl(new (std::nothrow) _DownloadManagerImpl);
288         SysTryReturnVoidResult(NID_CNT, pImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
289
290         result r = pImpl->Construct();
291         SysTryReturnVoidResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] Failed to initialize download manager.");
292
293         __pInstance = pImpl.release();
294
295         std::atexit(DestroySingleton);
296 }
297
298 void
299 _DownloadManagerImpl::DestroySingleton(void)
300 {
301         delete __pInstance;
302 }
303
304 _DownloadManagerImpl*
305 _DownloadManagerImpl::GetInstance(void)
306 {
307         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
308         if (__pInstance == null)
309         {
310                 ClearLastResult();
311
312                 pthread_once(&onceBlock, InitSingleton);
313                 result r = GetLastResult();
314                 if(IsFailed(r))
315                 {
316                         onceBlock = PTHREAD_ONCE_INIT;
317                 }
318         }
319
320         return __pInstance;
321 }
322
323 result
324 _DownloadManagerImpl::Start(const DownloadRequest& request, RequestId& reqId)
325 {
326         SysLog(NID_CNT, "Start a download from Url = %ls, Path = %ls", request.GetUrl().GetPointer(), request.GetDirectoryPath().GetPointer());
327
328         result r = E_SUCCESS;
329
330         int ret = 0;
331         int download_id = 0;
332
333         String url = request.GetUrl();
334         String dirPath = request.GetDirectoryPath();
335         String fileName = request.GetFileName();
336         NetworkType networkType = (NetworkType)request.GetNetworkType();
337
338         SysTryReturnResult(NID_CNT, !url.IsEmpty(), E_INVALID_ARG, "The url of the download request is empty.");
339
340         // Create a download id
341         ret = download_create(&download_id);
342         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
343
344         // Set the url
345         unique_ptr<char[]> pUrl(_StringConverter::CopyToCharArrayN(url));
346
347         ret = download_set_url(download_id, pUrl.get());
348         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
349
350         //Set network type
351         if (networkType == NETWORK_ALL)
352         {
353                 ret = download_set_network_type(download_id, (download_network_type_e)DOWNLOAD_NETWORK_ALL);
354         }
355         else
356         {
357                 ret = download_set_network_type(download_id, (download_network_type_e)networkType);
358         }
359         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
360
361         //Set notification
362         ret = download_set_notification(download_id, request.IsNotificationEnabled());
363         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
364
365         //Set notification extra data
366         std::unique_ptr<IMapEnumerator> pMapEnum(const_cast< IMap* >(request.GetNotificationExtraData())->GetMapEnumeratorN());
367         while (pMapEnum->MoveNext() == E_SUCCESS)
368         {
369                 String* pMapKey = dynamic_cast<String*>(pMapEnum->GetKey());
370                 unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(*pMapKey));
371                 String* pMapValue = dynamic_cast<String*>(pMapEnum->GetValue());
372                 const char* pValue = _StringConverter::CopyToCharArrayN(*pMapValue);
373
374                 ret = download_add_notification_extra_param(download_id, pKey.get(), &pValue, 1);
375                 delete[] pValue; 
376                 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
377         }
378
379         //Add http headers
380         IMap* pRequestHeader = _DownloadRequestImpl::GetInstance(&request)->GetRequestHeader();
381         std::unique_ptr<IMapEnumerator> pMapEnume(const_cast< IMap* >(pRequestHeader)->GetMapEnumeratorN());
382         while (pMapEnume->MoveNext() == E_SUCCESS)
383         {
384                 String* pMapKey = dynamic_cast<String*>(pMapEnume->GetKey());
385                 unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(*pMapKey));
386                 String* pMapValue = dynamic_cast<String*>(pMapEnume->GetValue());
387                 unique_ptr<char[]> pValue(_StringConverter::CopyToCharArrayN(*pMapValue));
388
389                 ret = download_add_http_header_field(download_id, pKey.get(), pValue.get());
390                 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
391         }
392
393         // Check the download path
394         if (!dirPath.IsEmpty())
395         {
396                 unique_ptr<char[]> pStr(_StringConverter::CopyToCharArrayN(dirPath));
397
398                 ret = access(pStr.get(), F_OK);
399                 if (ret == 0)
400                 {
401                         ret = access(pStr.get(), W_OK);
402                         SysTryReturnResult(NID_CNT, ret == 0, E_ILLEGAL_ACCESS, "Access to the requested path is denied due to insufficient permission.");
403                 }
404
405                 ret = download_set_destination(download_id, pStr.get());
406                 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
407         }
408
409         // Set the file name
410         if (!fileName.IsEmpty())
411         {
412                 unique_ptr<char[]> pStr(_StringConverter::CopyToCharArrayN(fileName));
413
414                 ret = download_set_file_name(download_id, pStr.get());
415                 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
416         }
417
418         // Set the callback functions
419         r = RegisterCallback((long)download_id);
420         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "The internal system service is not available.");
421
422         // Start the download request
423         ret = download_start(download_id);
424         SysTryCatch(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
425         SysTryCatch(NID_CNT, ret >= 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The internal system service is not available. %d", ret);
426
427         // Set a request Id
428         reqId = (long)download_id;
429
430         // Add a request Id to the handle map
431         __handleMap.Add(reqId, new DownloadRequest(request));
432
433         return E_SUCCESS;
434
435 CATCH:
436         UnregisterCallback((long)download_id);
437
438         return r;
439 }
440
441 result
442 _DownloadManagerImpl::Pause(RequestId reqId)
443 {
444         result r = E_SUCCESS;
445
446         int ret = 0;
447         int state = STATE_NONE;
448
449         // Pause the download request
450         ret = download_pause((int)reqId);
451         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
452         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
453         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not downloading.");
454         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
455
456         return E_SUCCESS;
457 }
458
459 result
460 _DownloadManagerImpl::Resume(RequestId reqId)
461 {
462         result r = E_SUCCESS;
463
464         int ret = 0;
465         int state = STATE_NONE;
466
467         // Resume the download request
468         ret = download_start((int)reqId);
469         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
470         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
471         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not paused or has failed.");
472         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
473
474         return E_SUCCESS;
475 }
476
477 result
478 _DownloadManagerImpl::Cancel(RequestId reqId)
479 {
480         int ret = 0;
481         int state = STATE_NONE;
482
483         // Stop the download request
484         ret = download_cancel((int)reqId);
485         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
486         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
487         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
488
489         return E_SUCCESS;
490 }
491
492 DownloadRequest*
493 _DownloadManagerImpl::GetDownloadRequestN(RequestId reqId)
494 {
495         result r = E_SUCCESS;
496         DownloadRequest* pRequest = null;
497
498         r = __handleMap.GetValue(reqId, pRequest);
499         SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_INVALID_ARG, "[E_INVALID_ARG] There is no download request for the request ID.");
500
501         pRequest = new (std::nothrow) DownloadRequest(*pRequest);
502         SysTryReturn(NID_CNT, pRequest != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
503
504         SetLastResult(E_SUCCESS);
505         return pRequest;
506 }
507
508 result
509 _DownloadManagerImpl::GetMimeType(RequestId reqId, String& mimeType)
510 {
511         result r = E_SUCCESS;
512         int ret = 0;
513
514         char* pStr = null;
515         DownloadRequest* pRequest = null;
516
517         // Check the request is valid
518         pRequest = GetDownloadRequestN(reqId);
519         if(pRequest == null)
520         {
521                 r = GetLastResult();
522                 SysPropagate(NID_CNT, r);
523                 return r;
524         }
525
526         delete pRequest;
527
528         ret = download_get_mime_type((int)reqId, &pStr);
529         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not downloading or paused.");
530         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
531
532         mimeType = pStr;
533         delete[] pStr;
534
535         return E_SUCCESS;
536 }
537
538 int
539 _DownloadManagerImpl::GetState(RequestId reqId) const
540 {
541         int ret = 0;
542
543         download_state_e download_state = (download_state_e)SLP_STATE_NONE;
544         int state = STATE_NONE;
545
546         ret = download_get_state((int)reqId, &download_state);
547         SysTryReturn(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, STATE_NONE, E_INVALID_ARG, "[E_INVALID_ARG] The request ID is not valid.");
548         SysTryReturn(NID_CNT, ret >= 0, STATE_NONE, E_SYSTEM, "[E_SYSTEM] The internal system service is not available. %d", ret);
549
550         switch(download_state)
551         {
552                 case SLP_STATE_NONE:
553                         state = STATE_NONE;
554                         break;
555
556                 case SLP_STATE_READY:
557                         state = STATE_QUEUED;
558                         break;
559
560                 case SLP_STATE_QUEUED:
561                         state = STATE_QUEUED;
562                         break;
563
564                 case SLP_STATE_DOWNLOADING:
565                         state = STATE_DOWNLOADING;
566                         break;
567
568                 case SLP_STATE_PAUSED:
569                         state = STATE_PAUSED;
570                         break;
571
572                 case SLP_STATE_COMPLETED:
573                         state = STATE_COMPLETED;
574                         break;
575
576                 case SLP_STATE_CANCELLED:
577                         state = STATE_CANCELLED;
578                         break;
579
580                 case SLP_STATE_FAILED:
581                         state = STATE_FAILED;
582                         break;
583
584                 default:
585                         state = STATE_NONE;
586                         break;
587         }
588
589         SysLog(NID_CNT, "download_state = %d, state = %d", download_state, state);
590
591         return state;
592 }
593
594 result
595 _DownloadManagerImpl::GetProgress(RequestId reqId, int& progress)
596 {
597         return E_SUCCESS;
598 }
599
600 result
601 _DownloadManagerImpl::SetAllowedNetwork(unsigned long flags)
602 {
603         return E_SUCCESS;
604 }
605
606 void
607 _DownloadManagerImpl::SetDownloadListener(IDownloadListener* pListener)
608 {
609         if (pListener != null)
610         {
611                 _DownloadEvent* pEvent = new (std::nothrow) _DownloadEvent();
612                 SysTryReturnVoidResult(NID_IO, pEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
613
614                 pEvent->AddListener(*pListener);
615
616                 __pEvent = pEvent;
617         }
618         else
619         {
620                 if (__pEvent != null)
621                 {
622                         delete __pEvent;
623                 }
624
625                 __pEvent = null;
626         }
627 }
628
629 void
630 _DownloadManagerImpl::DestroyResources(RequestId reqId)
631 {
632         int ret = 0;
633         result r = E_SUCCESS;
634         DownloadRequest* pRequest = null;
635
636         // Cancel the callback
637         UnregisterCallback(reqId);
638
639         // Remove the resource from url_download
640         ret = download_destroy((int)reqId);
641         SysTryLog(NID_CNT, ret >= 0, "url_download_destory fails %d", ret);
642
643         // Remove DownloadRequest from __handleMap
644         r = __handleMap.GetValue(reqId, pRequest);
645         SysTryLog(NID_CNT, r == E_SUCCESS, "[%s]", GetErrorMessage(r));
646
647         r = __handleMap.Remove(reqId);
648         SysTryLog(NID_CNT, r == E_SUCCESS, "[%s]", GetErrorMessage(r));
649
650         delete pRequest;
651 }
652
653 result
654 _DownloadManagerImpl::RegisterCallback(RequestId reqId)
655 {
656         int ret = 0;
657
658         ret = download_set_state_changed_cb(reqId, OnStateChanged, this);
659         SysTryCatch(NID_CNT, ret >= 0, , E_SYSTEM, "[E_SYSTEM] Fails to set state_changed_cb: %d, id = %d", ret, reqId);
660
661         ret = download_set_progress_cb(reqId, OnProgress, this);
662         SysTryCatch(NID_CNT, ret >= 0, , E_SYSTEM, "[E_SYSTEM] Fails to set progress_cb: %d, id = %d", ret, reqId);
663
664         return E_SUCCESS;
665
666 CATCH:
667         UnregisterCallback(reqId);
668
669         return E_SYSTEM;
670 }
671
672 void
673 _DownloadManagerImpl::UnregisterCallback(RequestId reqId)
674 {
675         int ret = 0;
676
677         ret = download_unset_state_changed_cb(reqId);
678         SysTryLog(NID_CNT, ret >= 0, "download_unset_state_changed_cb fails %d, id = %d", ret, reqId);
679
680         ret = download_unset_progress_cb(reqId);
681         SysTryLog(NID_CNT, ret >= 0, "download_unset_progress_cb fails %d, id = %d", ret, reqId);
682 }
683
684 result
685 _DownloadManagerImpl::ConvertToResult(int error)
686 {
687         result r = E_SUCCESS;
688         download_error_e download_error = (download_error_e)error;
689
690         SysLog(NID_CNT, "Download error = %d", download_error);
691
692         switch(download_error)
693         {
694                 case DOWNLOAD_ERROR_NONE:
695                         r = E_SUCCESS;
696                         break;
697
698                 case DOWNLOAD_ERROR_OUT_OF_MEMORY:
699                         r = E_OUT_OF_MEMORY;
700                         break;
701
702                 case DOWNLOAD_ERROR_IO_ERROR:
703                         r = E_SYSTEM;
704                         break;
705
706                 case DOWNLOAD_ERROR_INVALID_URL:
707                         r = E_INVALID_URL;
708                         break;
709
710                 case DOWNLOAD_ERROR_ID_NOT_FOUND:
711                         r = E_INVALID_ARG;
712                         break;
713
714                 case DOWNLOAD_ERROR_CONNECTION_FAILED:
715                 case DOWNLOAD_ERROR_NETWORK_UNREACHABLE:
716                         r = E_CONNECTION_FAILED;
717                         break;
718
719                 case DOWNLOAD_ERROR_CONNECTION_TIMED_OUT:
720                         r = E_TIMEOUT;
721                         break;
722
723                 case DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS:
724                         r = E_MAX_EXCEEDED;
725                         break;
726
727                 case DOWNLOAD_ERROR_NO_SPACE:
728                         r = E_STORAGE_FULL;
729                         break;
730
731                 case DOWNLOAD_ERROR_QUEUE_FULL:
732
733                 //case URL_DOWNLOAD_ERROR_INVALID_STATE:
734                 //case URL_DOWNLOAD_ERROR_INVALID_PARAMETER:
735                 //case URL_DOWNLOAD_ERROR_INVALID_DESTINATION:
736                 //case URL_DOWNLOAD_ERROR_ALREADY_COMPLETED:
737                 default:
738                         r = E_SYSTEM;
739         }
740
741         return r;
742 }
743
744 } } // Tizen::Content