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