[content] Fix bug on ContentDirectory
[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 void
274 _DownloadManagerImpl::InitSingleton(void)
275 {
276         unique_ptr<_DownloadManagerImpl> pImpl(new (std::nothrow) _DownloadManagerImpl);
277         SysTryReturnVoidResult(NID_CNT, pImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
278
279         __pInstance = pImpl.release();
280
281         std::atexit(DestroySingleton);
282 }
283
284 void
285 _DownloadManagerImpl::DestroySingleton(void)
286 {
287         delete __pInstance;
288 }
289
290 _DownloadManagerImpl*
291 _DownloadManagerImpl::GetInstance(void)
292 {
293         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
294         if (__pInstance == null)
295         {
296                 ClearLastResult();
297
298                 pthread_once(&onceBlock, InitSingleton);
299                 result r = GetLastResult();
300                 if(IsFailed(r))
301                 {
302                         onceBlock = PTHREAD_ONCE_INIT;
303                 }
304         }
305
306         return __pInstance;
307 }
308
309 result
310 _DownloadManagerImpl::Start(const DownloadRequest& request, RequestId& reqId)
311 {
312         SysLog(NID_CNT, "Start a download from Url = %ls, Path = %ls", request.GetUrl().GetPointer(), request.GetDirectoryPath().GetPointer());
313
314         result r = E_SUCCESS;
315
316         int ret = 0;
317         int download_id = 0;
318
319         String url = request.GetUrl();
320         String dirPath = request.GetDirectoryPath();
321         String fileName = request.GetFileName();
322         NetworkType networkType = (NetworkType)request.GetNetworkType();
323
324         SysTryReturnResult(NID_CNT, !url.IsEmpty(), E_INVALID_ARG, "The url of the download request is empty.");
325
326         // Create a download id
327         ret = download_create(&download_id);
328         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
329
330         // Set the url
331         unique_ptr<char[]> pUrl(_StringConverter::CopyToCharArrayN(url));
332
333         ret = download_set_url(download_id, pUrl.get());
334         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
335
336         //Set network type
337         if (networkType == NETWORK_ALL)
338         {
339                 ret = download_set_network_type(download_id, (download_network_type_e)DOWNLOAD_NETWORK_ALL);
340         }
341         else
342         {
343                 ret = download_set_network_type(download_id, (download_network_type_e)networkType);
344         }
345         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
346
347         //Set notification
348         ret = download_set_notification(download_id, request.IsNotificationEnabled());
349         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
350
351         //Set notification extra data
352         std::unique_ptr<IMapEnumerator> pMapEnum(const_cast< IMap* >(request.GetNotificationExtraData())->GetMapEnumeratorN());
353
354         while (pMapEnum->MoveNext() == E_SUCCESS)
355         {
356                 String* pMapKey = dynamic_cast<String*>(pMapEnum->GetKey());
357                 String* pMapValue = dynamic_cast<String*>(pMapEnum->GetValue());
358
359                 if (pMapKey && pMapValue)
360                 {
361                         unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(*pMapKey));
362                         const char* pValue = _StringConverter::CopyToCharArrayN(*pMapValue);
363
364                         ret = download_add_notification_extra_param(download_id, pKey.get(), &pValue, 1);
365                         delete[] pValue;
366                         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
367                 }
368         }
369
370         //Add http headers
371         IMap* pRequestHeader = _DownloadRequestImpl::GetInstance(&request)->GetRequestHeader();
372         std::unique_ptr<IMapEnumerator> pMapEnume(const_cast< IMap* >(pRequestHeader)->GetMapEnumeratorN());
373         while (pMapEnume->MoveNext() == E_SUCCESS)
374         {
375                 String* pMapKey = dynamic_cast<String*>(pMapEnume->GetKey());
376                 String* pMapValue = dynamic_cast<String*>(pMapEnume->GetValue());
377                 if (pMapKey && pMapValue)
378                 {
379                         unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(*pMapKey));
380                         unique_ptr<char[]> pValue(_StringConverter::CopyToCharArrayN(*pMapValue));
381
382                         ret = download_add_http_header_field(download_id, pKey.get(), pValue.get());
383                         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
384
385                 }
386         }
387
388         // Check the download path
389         if (!dirPath.IsEmpty())
390         {
391                 unique_ptr<char[]> pStr(_StringConverter::CopyToCharArrayN(dirPath));
392
393                 ret = access(pStr.get(), F_OK);
394                 if (ret == 0)
395                 {
396                         ret = access(pStr.get(), W_OK);
397                         SysTryReturnResult(NID_CNT, ret == 0, E_ILLEGAL_ACCESS, "Access to the requested path is denied due to insufficient permission.");
398                 }
399
400                 ret = download_set_destination(download_id, pStr.get());
401                 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
402         }
403
404         // Set the file name
405         if (!fileName.IsEmpty())
406         {
407                 unique_ptr<char[]> pStr(_StringConverter::CopyToCharArrayN(fileName));
408
409                 ret = download_set_file_name(download_id, pStr.get());
410                 SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
411         }
412
413         // Set the callback functions
414         r = RegisterCallback((long)download_id);
415         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_SYSTEM, "The internal system service is not available.");
416
417         // Start the download request
418         ret = download_start(download_id);
419         SysTryCatch(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
420         SysTryCatch(NID_CNT, ret >= 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The internal system service is not available. %d", ret);
421
422         // Set a request Id
423         reqId = (long)download_id;
424
425         return E_SUCCESS;
426
427 CATCH:
428         UnregisterCallback((long)download_id);
429
430         return r;
431 }
432
433 result
434 _DownloadManagerImpl::Pause(RequestId reqId)
435 {
436         result r = E_SUCCESS;
437
438         int ret = 0;
439         int state = STATE_NONE;
440
441         // Pause the download request
442         ret = download_pause((int)reqId);
443         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
444         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
445         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not downloading.");
446         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
447
448         return E_SUCCESS;
449 }
450
451 result
452 _DownloadManagerImpl::Resume(RequestId reqId)
453 {
454         result r = E_SUCCESS;
455
456         int ret = 0;
457         int state = STATE_NONE;
458
459         // Resume the download request
460         ret = download_start((int)reqId);
461         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
462         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
463         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not paused or has failed.");
464         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
465
466         return E_SUCCESS;
467 }
468
469 result
470 _DownloadManagerImpl::Cancel(RequestId reqId)
471 {
472         int ret = 0;
473         int state = STATE_NONE;
474
475         // Stop the download request
476         ret = download_cancel((int)reqId);
477         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
478         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
479         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
480
481         return E_SUCCESS;
482 }
483
484 DownloadRequest*
485 _DownloadManagerImpl::GetDownloadRequestN(RequestId reqId)
486 {
487         result r = E_SUCCESS;
488         DownloadRequest* pRequest = null;
489         int ret = 0;
490         char* pUrl = null;
491         char* pPath = null;
492         bool notification = false;
493         int length = 0;
494         char** pFields = null;
495         char* pValue = null;
496         char* pFileName = null;
497         download_network_type_e netType = (download_network_type_e)DOWNLOAD_NETWORK_DATA_NETWORK;
498
499         ret = download_get_url(reqId, &pUrl);
500         SysTryCatch(NID_CNT, (ret != DOWNLOAD_ERROR_ID_NOT_FOUND) && (ret != DOWNLOAD_ERROR_INVALID_PARAMETER), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
501         ret = download_get_network_type(reqId, &netType);
502         SysTryCatch(NID_CNT, (ret != DOWNLOAD_ERROR_ID_NOT_FOUND) && (ret != DOWNLOAD_ERROR_INVALID_PARAMETER), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
503         ret = download_get_destination(reqId, &pPath);
504         SysTryCatch(NID_CNT, (ret != DOWNLOAD_ERROR_ID_NOT_FOUND) && (ret != DOWNLOAD_ERROR_INVALID_PARAMETER), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
505         ret = download_get_file_name(reqId, &pFileName);
506         SysTryCatch(NID_CNT, (ret != DOWNLOAD_ERROR_ID_NOT_FOUND) && (ret != DOWNLOAD_ERROR_INVALID_PARAMETER), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
507         ret = download_get_notification(reqId, &notification);
508         SysTryCatch(NID_CNT, (ret != DOWNLOAD_ERROR_ID_NOT_FOUND) && (ret != DOWNLOAD_ERROR_INVALID_PARAMETER), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
509         ret = download_get_http_header_field_list(reqId, &pFields, &length);
510         SysTryCatch(NID_CNT, (ret != DOWNLOAD_ERROR_ID_NOT_FOUND) && (ret != DOWNLOAD_ERROR_INVALID_PARAMETER), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
511         pRequest = new (std::nothrow) DownloadRequest(pUrl, pPath);
512         SysTryCatch(NID_CNT, pRequest != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
513         pRequest->SetFileName(pFileName);
514         pRequest->SetNotification(notification);
515         pRequest->SetNetworkType((DownloadNetworkType)netType);
516         //Get all the field values
517         for (int i = 0; i < length; i++)
518         {
519                 ret = download_get_http_header_field(reqId, pFields[i], &pValue);
520                 SysTryCatch(NID_CNT, (ret != DOWNLOAD_ERROR_ID_NOT_FOUND) && (ret != DOWNLOAD_ERROR_INVALID_PARAMETER), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is not valid");
521                 pRequest->AddRequestHeader(pFields[i], pValue);
522                 free(pValue);
523                 free(pFields[i]);
524                 pValue = null;
525         }
526
527 CATCH:
528         free(pUrl);
529         free(pPath);
530         free(pFileName);
531         SetLastResult(r);
532         return pRequest;
533 }
534
535 result
536 _DownloadManagerImpl::GetMimeType(RequestId reqId, String& mimeType)
537 {
538         int ret = 0;
539         char* pStr = null;
540
541         ret = download_get_mime_type((int)reqId, &pStr);
542         SysTryReturnResult(NID_CNT, (ret != DOWNLOAD_ERROR_ID_NOT_FOUND) && (ret != DOWNLOAD_ERROR_INVALID_PARAMETER), E_INVALID_ARG, "There is no download request for the request ID.");
543         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not downloading or paused.");
544         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
545
546         mimeType = pStr;
547         delete[] pStr;
548
549         return E_SUCCESS;
550 }
551
552 int
553 _DownloadManagerImpl::GetState(RequestId reqId) const
554 {
555         int ret = 0;
556
557         download_state_e download_state = (download_state_e)SLP_STATE_NONE;
558         int state = STATE_NONE;
559
560         ret = download_get_state((int)reqId, &download_state);
561         SysTryReturn(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, STATE_NONE, E_INVALID_ARG, "[E_INVALID_ARG] The request ID is not valid.");
562         SysTryReturn(NID_CNT, ret >= 0, STATE_NONE, E_SYSTEM, "[E_SYSTEM] The internal system service is not available. %d", ret);
563
564         switch(download_state)
565         {
566                 case SLP_STATE_NONE:
567                         state = STATE_NONE;
568                         break;
569
570                 case SLP_STATE_READY:
571                         state = STATE_QUEUED;
572                         break;
573
574                 case SLP_STATE_QUEUED:
575                         state = STATE_QUEUED;
576                         break;
577
578                 case SLP_STATE_DOWNLOADING:
579                         state = STATE_DOWNLOADING;
580                         break;
581
582                 case SLP_STATE_PAUSED:
583                         state = STATE_PAUSED;
584                         break;
585
586                 case SLP_STATE_COMPLETED:
587                         state = STATE_COMPLETED;
588                         break;
589
590                 case SLP_STATE_CANCELLED:
591                         state = STATE_CANCELLED;
592                         break;
593
594                 case SLP_STATE_FAILED:
595                         state = STATE_FAILED;
596                         break;
597
598                 default:
599                         state = STATE_NONE;
600                         break;
601         }
602
603         SysLog(NID_CNT, "download_state = %d, state = %d", download_state, state);
604
605         return state;
606 }
607
608 result
609 _DownloadManagerImpl::GetProgress(RequestId reqId, int& progress)
610 {
611         return E_SUCCESS;
612 }
613
614 result
615 _DownloadManagerImpl::SetAllowedNetwork(unsigned long flags)
616 {
617         return E_SUCCESS;
618 }
619
620 void
621 _DownloadManagerImpl::SetDownloadListener(IDownloadListener* pListener)
622 {
623         if (pListener != null)
624         {
625                 _DownloadEvent* pEvent = new (std::nothrow) _DownloadEvent();
626                 SysTryReturnVoidResult(NID_IO, pEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
627
628                 pEvent->AddListener(*pListener);
629
630                 __pEvent = pEvent;
631         }
632         else
633         {
634                 if (__pEvent != null)
635                 {
636                         delete __pEvent;
637                 }
638
639                 __pEvent = null;
640         }
641 }
642
643 void
644 _DownloadManagerImpl::DestroyResources(RequestId reqId)
645 {
646         int ret = 0;
647         result r = E_SUCCESS;
648
649         // Cancel the callback
650         UnregisterCallback(reqId);
651
652         // Remove the resource from url_download
653         ret = download_destroy((int)reqId);
654         SysTryLog(NID_CNT, ret >= 0, "url_download_destory fails %d", ret);
655 }
656
657 result
658 _DownloadManagerImpl::RegisterCallback(RequestId reqId)
659 {
660         int ret = 0;
661
662         ret = download_set_state_changed_cb(reqId, OnStateChanged, this);
663         SysTryCatch(NID_CNT, ret >= 0, , E_SYSTEM, "[E_SYSTEM] Fails to set state_changed_cb: %d, id = %d", ret, reqId);
664
665         ret = download_set_progress_cb(reqId, OnProgress, this);
666         SysTryCatch(NID_CNT, ret >= 0, , E_SYSTEM, "[E_SYSTEM] Fails to set progress_cb: %d, id = %d", ret, reqId);
667
668         return E_SUCCESS;
669
670 CATCH:
671         UnregisterCallback(reqId);
672
673         return E_SYSTEM;
674 }
675
676 void
677 _DownloadManagerImpl::UnregisterCallback(RequestId reqId)
678 {
679         int ret = 0;
680
681         ret = download_unset_state_changed_cb(reqId);
682         SysTryLog(NID_CNT, ret >= 0, "download_unset_state_changed_cb fails %d, id = %d", ret, reqId);
683
684         ret = download_unset_progress_cb(reqId);
685         SysTryLog(NID_CNT, ret >= 0, "download_unset_progress_cb fails %d, id = %d", ret, reqId);
686 }
687
688 result
689 _DownloadManagerImpl::ConvertToResult(int error)
690 {
691         result r = E_SUCCESS;
692         download_error_e download_error = (download_error_e)error;
693
694         SysLog(NID_CNT, "Download error = %d", download_error);
695
696         switch(download_error)
697         {
698                 case DOWNLOAD_ERROR_NONE:
699                         r = E_SUCCESS;
700                         break;
701
702                 case DOWNLOAD_ERROR_OUT_OF_MEMORY:
703                         r = E_OUT_OF_MEMORY;
704                         break;
705
706                 case DOWNLOAD_ERROR_IO_ERROR:
707                         r = E_SYSTEM;
708                         break;
709
710                 case DOWNLOAD_ERROR_INVALID_URL:
711                         r = E_INVALID_URL;
712                         break;
713
714                 case DOWNLOAD_ERROR_ID_NOT_FOUND:
715                         r = E_INVALID_ARG;
716                         break;
717
718                 case DOWNLOAD_ERROR_CONNECTION_FAILED:
719                 case DOWNLOAD_ERROR_NETWORK_UNREACHABLE:
720                         r = E_CONNECTION_FAILED;
721                         break;
722
723                 case DOWNLOAD_ERROR_CONNECTION_TIMED_OUT:
724                         r = E_TIMEOUT;
725                         break;
726
727                 case DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS:
728                         r = E_MAX_EXCEEDED;
729                         break;
730
731                 case DOWNLOAD_ERROR_NO_SPACE:
732                         r = E_STORAGE_FULL;
733                         break;
734
735                 case DOWNLOAD_ERROR_QUEUE_FULL:
736
737                 //case URL_DOWNLOAD_ERROR_INVALID_STATE:
738                 //case URL_DOWNLOAD_ERROR_INVALID_PARAMETER:
739                 //case URL_DOWNLOAD_ERROR_INVALID_DESTINATION:
740                 //case URL_DOWNLOAD_ERROR_ALREADY_COMPLETED:
741                 default:
742                         r = E_SYSTEM;
743         }
744
745         return r;
746 }
747
748 } } // Tizen::Content