[content] Add impl classes for ContentInfo
[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         int ret = 0;
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         int ret = 0;
463
464         // Resume the download request
465         ret = download_start((int)reqId);
466         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
467         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
468         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not paused or has failed.");
469         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
470
471         return E_SUCCESS;
472 }
473
474 result
475 _DownloadManagerImpl::Cancel(RequestId reqId)
476 {
477         int ret = 0;
478
479         // Stop the download request
480         ret = download_cancel((int)reqId);
481         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_PARAMETER, E_INVALID_ARG, "There is no download request for the request.");
482         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, E_INVALID_ARG, "The request ID is not valid.");
483         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
484
485         return E_SUCCESS;
486 }
487
488 DownloadRequest*
489 _DownloadManagerImpl::GetDownloadRequestN(RequestId reqId)
490 {
491         result r = E_SUCCESS;
492         DownloadRequest* pRequest = null;
493         int ret = 0;
494         char* pUrl = null;
495         char* pPath = null;
496         bool notification = false;
497         int length = 0;
498         char** pFields = null;
499         char* pValue = null;
500         char* pFileName = null;
501         download_network_type_e netType = (download_network_type_e)DOWNLOAD_NETWORK_DATA_NETWORK;
502
503         ret = download_get_url(reqId, &pUrl);
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_network_type(reqId, &netType);
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_destination(reqId, &pPath);
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_file_name(reqId, &pFileName);
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         ret = download_get_notification(reqId, &notification);
512         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");
513         ret = download_get_http_header_field_list(reqId, &pFields, &length);
514         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");
515         pRequest = new (std::nothrow) DownloadRequest(pUrl, pPath);
516         SysTryCatch(NID_CNT, pRequest != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
517         pRequest->SetFileName(pFileName);
518         pRequest->SetNotification(notification);
519         pRequest->SetNetworkType((DownloadNetworkType)netType);
520         //Get all the field values
521         for (int i = 0; i < length; i++)
522         {
523                 ret = download_get_http_header_field(reqId, pFields[i], &pValue);
524                 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");
525                 pRequest->AddRequestHeader(pFields[i], pValue);
526                 free(pValue);
527                 free(pFields[i]);
528                 pValue = null;
529         }
530
531 CATCH:
532         free(pUrl);
533         free(pPath);
534         free(pFileName);
535         SetLastResult(r);
536         return pRequest;
537 }
538
539 result
540 _DownloadManagerImpl::GetMimeType(RequestId reqId, String& mimeType)
541 {
542         int ret = 0;
543         char* pStr = null;
544
545         ret = download_get_mime_type((int)reqId, &pStr);
546         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.");
547         SysTryReturnResult(NID_CNT, ret != DOWNLOAD_ERROR_INVALID_STATE, E_INVALID_OPERATION, "The current download state is not downloading or paused.");
548         SysTryReturnResult(NID_CNT, ret >= 0, E_SYSTEM, "The internal system service is not available. %d", ret);
549
550         mimeType = pStr;
551         delete[] pStr;
552
553         return E_SUCCESS;
554 }
555
556 int
557 _DownloadManagerImpl::GetState(RequestId reqId) const
558 {
559         int ret = 0;
560
561         download_state_e download_state = (download_state_e)SLP_STATE_NONE;
562         int state = STATE_NONE;
563
564         ret = download_get_state((int)reqId, &download_state);
565         SysTryReturn(NID_CNT, ret != DOWNLOAD_ERROR_ID_NOT_FOUND, STATE_NONE, E_INVALID_ARG, "[E_INVALID_ARG] The request ID is not valid.");
566         SysTryReturn(NID_CNT, ret >= 0, STATE_NONE, E_SYSTEM, "[E_SYSTEM] The internal system service is not available. %d", ret);
567
568         switch(download_state)
569         {
570                 case SLP_STATE_NONE:
571                         state = STATE_NONE;
572                         break;
573
574                 case SLP_STATE_READY:
575                         state = STATE_QUEUED;
576                         break;
577
578                 case SLP_STATE_QUEUED:
579                         state = STATE_QUEUED;
580                         break;
581
582                 case SLP_STATE_DOWNLOADING:
583                         state = STATE_DOWNLOADING;
584                         break;
585
586                 case SLP_STATE_PAUSED:
587                         state = STATE_PAUSED;
588                         break;
589
590                 case SLP_STATE_COMPLETED:
591                         state = STATE_COMPLETED;
592                         break;
593
594                 case SLP_STATE_CANCELLED:
595                         state = STATE_CANCELLED;
596                         break;
597
598                 case SLP_STATE_FAILED:
599                         state = STATE_FAILED;
600                         break;
601
602                 default:
603                         state = STATE_NONE;
604                         break;
605         }
606
607         SysLog(NID_CNT, "download_state = %d, state = %d", download_state, state);
608
609         return state;
610 }
611
612 result
613 _DownloadManagerImpl::GetProgress(RequestId reqId, int& progress)
614 {
615         return E_SUCCESS;
616 }
617
618 result
619 _DownloadManagerImpl::SetAllowedNetwork(unsigned long flags)
620 {
621         return E_SUCCESS;
622 }
623
624 void
625 _DownloadManagerImpl::SetDownloadListener(IDownloadListener* pListener)
626 {
627         if (pListener != null)
628         {
629                 _DownloadEvent* pEvent = new (std::nothrow) _DownloadEvent();
630                 SysTryReturnVoidResult(NID_IO, pEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
631
632                 pEvent->AddListener(*pListener);
633
634                 __pEvent = pEvent;
635         }
636         else
637         {
638                 if (__pEvent != null)
639                 {
640                         delete __pEvent;
641                 }
642
643                 __pEvent = null;
644         }
645 }
646
647 void
648 _DownloadManagerImpl::DestroyResources(RequestId reqId)
649 {
650         int ret = 0;
651
652         // Cancel the callback
653         UnregisterCallback(reqId);
654
655         // Remove the resource from url_download
656         ret = download_destroy((int)reqId);
657         SysTryLog(NID_CNT, ret >= 0, "url_download_destory fails %d", ret);
658 }
659
660 result
661 _DownloadManagerImpl::RegisterCallback(RequestId reqId)
662 {
663         int ret = 0;
664
665         ret = download_set_state_changed_cb(reqId, OnStateChanged, this);
666         SysTryCatch(NID_CNT, ret >= 0, , E_SYSTEM, "[E_SYSTEM] Fails to set state_changed_cb: %d, id = %d", ret, reqId);
667
668         ret = download_set_progress_cb(reqId, OnProgress, this);
669         SysTryCatch(NID_CNT, ret >= 0, , E_SYSTEM, "[E_SYSTEM] Fails to set progress_cb: %d, id = %d", ret, reqId);
670
671         return E_SUCCESS;
672
673 CATCH:
674         UnregisterCallback(reqId);
675
676         return E_SYSTEM;
677 }
678
679 void
680 _DownloadManagerImpl::UnregisterCallback(RequestId reqId)
681 {
682         int ret = 0;
683
684         ret = download_unset_state_changed_cb(reqId);
685         SysTryLog(NID_CNT, ret >= 0, "download_unset_state_changed_cb fails %d, id = %d", ret, reqId);
686
687         ret = download_unset_progress_cb(reqId);
688         SysTryLog(NID_CNT, ret >= 0, "download_unset_progress_cb fails %d, id = %d", ret, reqId);
689 }
690
691 result
692 _DownloadManagerImpl::ConvertToResult(int error)
693 {
694         result r = E_SUCCESS;
695         download_error_e download_error = (download_error_e)error;
696
697         SysLog(NID_CNT, "Download error = %d", download_error);
698
699         switch(download_error)
700         {
701                 case DOWNLOAD_ERROR_NONE:
702                         r = E_SUCCESS;
703                         break;
704
705                 case DOWNLOAD_ERROR_OUT_OF_MEMORY:
706                         r = E_OUT_OF_MEMORY;
707                         break;
708
709                 case DOWNLOAD_ERROR_IO_ERROR:
710                         r = E_SYSTEM;
711                         break;
712
713                 case DOWNLOAD_ERROR_INVALID_URL:
714                         r = E_INVALID_URL;
715                         break;
716
717                 case DOWNLOAD_ERROR_ID_NOT_FOUND:
718                         r = E_INVALID_ARG;
719                         break;
720
721                 case DOWNLOAD_ERROR_CONNECTION_FAILED:
722                 case DOWNLOAD_ERROR_NETWORK_UNREACHABLE:
723                         r = E_CONNECTION_FAILED;
724                         break;
725
726                 case DOWNLOAD_ERROR_CONNECTION_TIMED_OUT:
727                         r = E_TIMEOUT;
728                         break;
729
730                 case DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS:
731                         r = E_MAX_EXCEEDED;
732                         break;
733
734                 case DOWNLOAD_ERROR_NO_SPACE:
735                         r = E_STORAGE_FULL;
736                         break;
737
738                 case DOWNLOAD_ERROR_QUEUE_FULL:
739
740                 //case URL_DOWNLOAD_ERROR_INVALID_STATE:
741                 //case URL_DOWNLOAD_ERROR_INVALID_PARAMETER:
742                 //case URL_DOWNLOAD_ERROR_INVALID_DESTINATION:
743                 //case URL_DOWNLOAD_ERROR_ALREADY_COMPLETED:
744                 default:
745                         r = E_SYSTEM;
746         }
747
748         return r;
749 }
750
751 } } // Tizen::Content