Update change log and spec for wrt-plugins-tizen_0.4.27
[framework/web/wrt-plugins-tizen.git] / src / Download / DownloadManager.cpp
1 //
2 // Tizen Web Device API
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 #include <glib.h>
19
20 #include <download.h>
21
22 #include <Logger.h>
23 #include <FilesystemUtils.h>
24
25 #include "DownloadState.h"
26 #include "DownloadNetworkType.h"
27 #include "DownloadManager.h"
28
29 namespace DeviceAPI {
30 namespace Download {
31
32 static std::string _get_download_error(int err)
33 {
34         bool success = false;
35     std::string msg = "";
36
37     switch (err) {
38         case DOWNLOAD_ERROR_INVALID_PARAMETER:
39             msg = "Invalid parameter";
40             break;
41         case DOWNLOAD_ERROR_OUT_OF_MEMORY:
42             msg = "Out of memory";
43             break;
44         case DOWNLOAD_ERROR_NETWORK_UNREACHABLE:
45             msg = "Network is unreachable";
46             break;
47         case DOWNLOAD_ERROR_CONNECTION_TIMED_OUT:
48             msg = "Http session time-out";
49             break;
50         case DOWNLOAD_ERROR_NO_SPACE:
51             msg = "No space left on device";
52             break;
53         case DOWNLOAD_ERROR_FIELD_NOT_FOUND:
54             msg = "Specified field not found";
55             break;
56         case DOWNLOAD_ERROR_INVALID_STATE:
57             msg = "Invalid state";
58             break;
59         case DOWNLOAD_ERROR_CONNECTION_FAILED:
60             msg = "Connection failed";
61             break;
62         case DOWNLOAD_ERROR_INVALID_URL:
63             msg = "Invalid URL";
64             break;
65         case DOWNLOAD_ERROR_INVALID_DESTINATION:
66             msg = "Invalid destination";
67             break;
68         case DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS:
69             msg = "Full of available simultaneous downloads";
70             break;
71         case DOWNLOAD_ERROR_QUEUE_FULL:
72             msg = "Full of available downloading items from server";
73             break;
74         case DOWNLOAD_ERROR_ALREADY_COMPLETED:
75             msg = "The download is already completed";
76             break;
77         case DOWNLOAD_ERROR_FILE_ALREADY_EXISTS:
78             msg = "It is failed to rename the downloaded file";
79             break;
80         case DOWNLOAD_ERROR_CANNOT_RESUME:
81             msg = "It cannot resume";
82             break;
83         case DOWNLOAD_ERROR_TOO_MANY_REDIRECTS:
84             msg = "In case of too may redirects from http response header";
85             break;
86         case DOWNLOAD_ERROR_UNHANDLED_HTTP_CODE:
87             msg = "The download cannot handle the http status value";
88             break;
89         case DOWNLOAD_ERROR_REQUEST_TIMEOUT:
90             msg = "There are no action after client create a download id";
91             break;
92         case DOWNLOAD_ERROR_RESPONSE_TIMEOUT:
93             msg = "It does not call start API in some time although the download is created";
94             break;
95         case DOWNLOAD_ERROR_SYSTEM_DOWN:
96             msg = "There are no response from client after rebooting download daemon";
97             break;
98         case DOWNLOAD_ERROR_ID_NOT_FOUND:
99             msg = "The download id is not existed in download service module";
100             break;
101         case DOWNLOAD_ERROR_NO_DATA:
102             msg = "No data because the set API is not called";
103             break;
104         case DOWNLOAD_ERROR_IO_ERROR:
105             msg = "Internal I/O error";
106             break;
107         case DOWNLOAD_ERROR_NONE:
108             success = true;
109             break;
110         default:
111             msg = "Unknown error";
112     }
113
114     if (!success) {
115         LOGE("Platform error %d <%s>", err, msg.c_str());
116     }
117
118     return msg;
119 }
120
121 typedef struct {
122         int downloadId;
123         download_state_e state;
124         unsigned long long received;
125         void *user_data;
126 } DOWNLOAD_EVENT_DATA_T;
127
128 static gboolean downloadEventCB(void *data) {
129         int ret;
130
131         DOWNLOAD_EVENT_DATA_T *fnData = static_cast<DOWNLOAD_EVENT_DATA_T*>(data);
132
133         try {
134                 long downloadId = (long)fnData->downloadId;
135
136                 DownloadManager *thisObj = (DownloadManager*)fnData->user_data;
137                 if (!thisObj) {
138                         throw UnknownException("UserData is NULL.");
139                 }
140
141                 DownloadCallback *callback = thisObj->getCallbackFromMap(downloadId);
142                 if (!callback) {
143                         throw UnknownException("Callback could not found.");
144                 }
145
146                 switch(fnData->state) {
147                         case DOWNLOAD_STATE_QUEUED:
148                         {
149                                 callback->onprogress(downloadId, 0, 0);
150                                 break;
151                         }
152                         case DOWNLOAD_STATE_PAUSED:
153                         {
154                                 callback->onpaused(downloadId);
155                                 break;
156                         }
157                         case DOWNLOAD_STATE_DOWNLOADING:
158                         {
159                                 unsigned long long totalSize = 0;
160                                 ret = download_get_content_size(downloadId, &totalSize);
161                                 if (ret != DOWNLOAD_ERROR_NONE) {
162                                         throw UnknownException(("Platform error while getting total file size. " + _get_download_error(ret)).c_str());
163                                 }
164
165                                 callback->onprogress(downloadId, fnData->received, totalSize);
166                                 break;
167                         }
168                         case DOWNLOAD_STATE_COMPLETED:
169                         {
170                                 ret = download_unset_state_changed_cb(downloadId);
171                                 if (ret != DOWNLOAD_ERROR_NONE) {
172                                         throw UnknownException(("Platform error while unsetting state changed callback. " + _get_download_error(ret)).c_str());
173                                 }
174
175                                 ret = download_unset_progress_cb(downloadId);
176                                 if (ret != DOWNLOAD_ERROR_NONE) {
177                                         throw UnknownException(("Platform error while unsetting progress callback. " + _get_download_error(ret)).c_str());
178                                 }
179
180                                 char *fullPath = NULL;
181                                 ret = download_get_downloaded_file_path(downloadId, &fullPath);
182                                 if (ret != DOWNLOAD_ERROR_NONE || !fullPath) {
183                                         throw UnknownException(("Platform error while getting downloaded full path. " + _get_download_error(ret)).c_str());
184                                 }
185
186                                 ret = download_destroy(downloadId);
187                                 if (ret != DOWNLOAD_ERROR_NONE) {
188                                         LOGW("Platform error while destroying download handle. downloadId=%d", downloadId);
189                                 }
190
191                                 std::string virtualPath;
192                                 try {
193                                         virtualPath = DeviceAPI::Filesystem::Utils::toVirtualPath(NULL, fullPath);
194                                 } catch (...) {
195                                         LOGW("Platform error while converting fullPath.");
196                                         virtualPath = fullPath;
197                                 }
198
199                                 callback->oncompleted(downloadId, virtualPath);
200                                 free(fullPath);
201
202                                 thisObj->removeCallbackFromMap(downloadId);
203                                 break;
204                         }
205                         case DOWNLOAD_STATE_FAILED:
206                         {
207                                 ret = download_unset_state_changed_cb(downloadId);
208                                 if (ret != DOWNLOAD_ERROR_NONE) {
209                                         throw UnknownException(("Platform error while unsetting state changed callback. " + _get_download_error(ret)).c_str());
210                                 }
211
212                                 ret = download_unset_progress_cb(downloadId);
213                                 if (ret != DOWNLOAD_ERROR_NONE) {
214                                         throw UnknownException(("Platform error while unsetting progress callback. " + _get_download_error(ret)).c_str());
215                                 }
216
217                                 int err = DOWNLOAD_ERROR_NONE;
218                                 std::string errMessage;
219                                 ret = download_get_error(downloadId, (download_error_e*)&err);
220                                 if (ret != DOWNLOAD_ERROR_NONE) {
221                                         LOGW("Platform error while getting download error. ");
222                                 } else {
223                                         errMessage = _get_download_error(err);
224                                 }
225
226                                 ret = download_destroy(downloadId);
227                                 if (ret != DOWNLOAD_ERROR_NONE) {
228                                         LOGW("Platform error while destroying download handle. downloadId=%d", downloadId);
229                                 }
230
231                                 UnknownException error(errMessage.c_str());
232                                 callback->onfailed(downloadId, error);
233                                 thisObj->removeCallbackFromMap(downloadId);
234                                 break;
235                         }
236                         case DOWNLOAD_STATE_CANCELED:
237                         {
238                                 ret = download_unset_state_changed_cb(downloadId);
239                                 if (ret != DOWNLOAD_ERROR_NONE) {
240                                         throw UnknownException(("Platform error while unsetting state changed callback. " + _get_download_error(ret)).c_str());
241                                 }
242
243                                 ret = download_unset_progress_cb(downloadId);
244                                 if (ret != DOWNLOAD_ERROR_NONE) {
245                                         throw UnknownException(("Platform error while unsetting progress callback. " + _get_download_error(ret)).c_str());
246                                 }
247
248                                 ret = download_destroy(downloadId);
249                                 if (ret != DOWNLOAD_ERROR_NONE) {
250                                         LOGW("Platform error while destroying download handle. downloadId=%d", downloadId);
251                                 }
252
253                                 callback->oncanceled(downloadId);
254                                 thisObj->removeCallbackFromMap(downloadId);
255                                 break;
256                         }
257                         default:
258                                 LOGW("State changed is ignored.");
259                                 break;
260                 }
261         } catch (const BasePlatformException &err) {
262                 LOGE("download_state_changed_cb: %s", err.getMessage().c_str());
263         }
264
265         delete fnData;
266         return false;
267 }
268
269 static void download_state_changed_cb(int downloadId, download_state_e state, void *user_data)
270 {
271         DOWNLOAD_EVENT_DATA_T *data = new DOWNLOAD_EVENT_DATA_T;
272         data->downloadId = downloadId;
273         data->state = state;
274         data->received = 0;
275         data->user_data = user_data;
276
277         // download core f/w calls this callback function in another thread.
278         // so we should use g_idle_add() to switch context to main thread.
279         g_idle_add(downloadEventCB, static_cast<void*>(data));
280 }
281
282 static void download_progress_cb(int downloadId, unsigned long long received, void *user_data)
283 {
284         DOWNLOAD_EVENT_DATA_T *data = new DOWNLOAD_EVENT_DATA_T;
285         data->downloadId = downloadId;
286         data->state = DOWNLOAD_STATE_DOWNLOADING;
287         data->received = received;
288         data->user_data = user_data;
289
290         // download core f/w calls this callback function in another thread.
291         // so we should use g_idle_add() to switch context to main thread.
292         g_idle_add(downloadEventCB, static_cast<void*>(data));
293 }
294
295 DownloadManager::DownloadManager()
296 {
297 }
298
299 DownloadManager::~DownloadManager()
300 {
301 }
302
303 void DownloadManager::setCallbackToMap(long downloadId, DownloadCallback *callback)
304 {
305         DownloadCallback *value = mDownloadCallbacks[downloadId];
306         if (value) {
307                 delete value;
308         }
309         mDownloadCallbacks[downloadId] = callback;
310 }
311
312 DownloadCallback* DownloadManager::getCallbackFromMap(long downloadId)
313 {
314         return mDownloadCallbacks[downloadId];
315 }
316
317 void DownloadManager::removeCallbackFromMap(long downloadId) {
318         DownloadCallback *value = mDownloadCallbacks[downloadId];
319         mDownloadCallbacks.erase(downloadId);
320         if (value) {
321                 delete value;
322         }
323 }
324
325 long DownloadManager::start(DownloadRequest *request, DownloadCallback *downloadCallback)
326 {
327         int ret;
328         int downloadId = 0;
329
330         if (!request) {
331                 throw TypeMismatchException("request is NULL.");
332         }
333
334         std::string url = request->getUrl();
335         std::string destination = request->getDestination();
336         std::string fileName = request->getFileName();
337         std::string networkType = request->getNetworkType();
338         std::map<std::string, std::string> httpHeader = request->getHttpHeader();
339
340         if (url.empty()) {
341                 throw InvalidValuesException("Invalid DownloadRequest.url.");
342         }
343
344         ret = download_create(&downloadId);
345         if (ret != DOWNLOAD_ERROR_NONE) {
346                 throw UnknownException(("Platform error while creating download. " + _get_download_error(ret)).c_str());
347         }
348
349         ret = download_set_url(downloadId, url.c_str());
350         if (ret != DOWNLOAD_ERROR_NONE) {
351                 throw UnknownException(("Platform error while setting url. " + _get_download_error(ret)).c_str());
352         }
353
354         if (!destination.empty()) {
355                 std::string fullPath;
356                 try {
357                         DeviceAPI::Filesystem::IPathPtr path = DeviceAPI::Filesystem::Utils::fromVirtualPath(NULL, destination);
358                         fullPath = path->getFullPath();
359                 } catch (...) {
360                         LOGW("Converting virtual path is failed. [%s]", destination.c_str());
361                         fullPath = destination;
362                 }
363                 ret = download_set_destination(downloadId, fullPath.c_str());
364                 if (ret != DOWNLOAD_ERROR_NONE) {
365                         throw UnknownException(("Platform error while setting destination. " + _get_download_error(ret)).c_str());
366                 }
367         }
368
369         if (!fileName.empty()) {
370                 ret = download_set_file_name(downloadId, fileName.c_str());
371                 if (ret != DOWNLOAD_ERROR_NONE) {
372                         throw UnknownException(("Platform error while setting fileName. " + _get_download_error(ret)).c_str());
373                 }
374         }
375
376         ret = download_set_state_changed_cb(downloadId, download_state_changed_cb, this);
377         if (ret != DOWNLOAD_ERROR_NONE) {
378                 throw UnknownException(("Platform error while setting state changed callback. " + _get_download_error(ret)).c_str());
379         }
380
381         ret = download_set_progress_cb(downloadId, download_progress_cb, this);
382         if (ret != DOWNLOAD_ERROR_NONE) {
383                 throw UnknownException(("Platform error while setting progress callback. " + _get_download_error(ret)).c_str());
384         }
385
386         if (!networkType.empty() && networkType != TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_ALL) {
387                 ret = DOWNLOAD_ERROR_NONE;
388                 if (networkType == TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_CELLULAR) {
389                         ret = download_set_network_type(downloadId, DOWNLOAD_NETWORK_DATA_NETWORK);
390                 } else if (networkType == TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_WIFI) {
391                         ret = download_set_network_type(downloadId, DOWNLOAD_NETWORK_WIFI);
392                 } else {
393                         throw TypeMismatchException("Wrong DownloadNetworkType.");
394                 }
395                 if (ret != DOWNLOAD_ERROR_NONE) {
396                         throw UnknownException(("Platform error while setting network type. " + _get_download_error(ret)).c_str());
397                 }
398         }
399
400         std::map<std::string, std::string>::const_iterator iter;
401         for (iter = httpHeader.begin(); iter != httpHeader.end(); ++iter) {
402                 ret = download_add_http_header_field(downloadId, iter->first.c_str(), iter->second.c_str());
403                 if (ret != DOWNLOAD_ERROR_NONE) {
404                         throw UnknownException(("Platform error while setting http header fields. " + _get_download_error(ret)).c_str());
405                 }
406         }
407
408         ret = download_start(downloadId);
409         if (ret != DOWNLOAD_ERROR_NONE) {
410                 throw UnknownException(("Platform error while starting download. " + _get_download_error(ret)).c_str());
411         }
412
413         setCallbackToMap(downloadId, downloadCallback);
414
415         return downloadId;
416 }
417
418 void DownloadManager::cancel(long downloadId)
419 {
420         int ret;
421
422         ret = download_cancel(downloadId);
423         if (ret != DOWNLOAD_ERROR_NONE) {
424                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
425                         throw NotFoundException("download id could not found.");
426                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
427                         throw InvalidValuesException("download id is not valid.");
428                 }
429                 throw UnknownException(("Platform error while canceling download. " + _get_download_error(ret)).c_str());
430         }
431 }
432
433 void DownloadManager::pause(long downloadId)
434 {
435         int ret;
436
437         ret = download_pause(downloadId);
438         if (ret != DOWNLOAD_ERROR_NONE) {
439                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
440                         throw NotFoundException("download id could not found.");
441                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
442                         throw InvalidValuesException("download id is not valid.");
443                 }
444                 throw UnknownException(("Platform error while pausing download. " + _get_download_error(ret)).c_str());
445         }
446 }
447
448 void DownloadManager::resume(long downloadId)
449 {
450         int ret;
451
452         ret = download_start(downloadId);
453         if (ret != DOWNLOAD_ERROR_NONE) {
454                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
455                         throw NotFoundException("download id could not found.");
456                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
457                         throw InvalidValuesException("download id is not valid.");
458                 }
459                 throw UnknownException(("Platform error while resuming download. " + _get_download_error(ret)).c_str());
460         }
461 }
462
463 std::string DownloadManager::getState(long downloadId)
464 {
465         int ret;
466         download_state_e state;
467         std::string result;
468
469         ret = download_get_state(downloadId, &state);
470         if (ret != DOWNLOAD_ERROR_NONE) {
471                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
472                         throw NotFoundException("download id could not found.");
473                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
474                         throw InvalidValuesException("download id is not valid.");
475                 }
476                 throw UnknownException(("Platform error while getting state. " + _get_download_error(ret)).c_str());
477         }
478
479         switch (state) {
480                 case DOWNLOAD_STATE_READY:
481                 case DOWNLOAD_STATE_QUEUED:
482                         result = TIZEN_ENUM_DOWNLOAD_STATE_QUEUED;
483                         break;
484                 case DOWNLOAD_STATE_DOWNLOADING:
485                         result = TIZEN_ENUM_DOWNLOAD_STATE_DOWNLOADING;
486                         break;
487                 case DOWNLOAD_STATE_PAUSED:
488                         result = TIZEN_ENUM_DOWNLOAD_STATE_PAUSED;
489                         break;
490                 case DOWNLOAD_STATE_COMPLETED:
491                         result = TIZEN_ENUM_DOWNLOAD_STATE_COMPLETED;
492                         break;
493                 case DOWNLOAD_STATE_FAILED:
494                         result = TIZEN_ENUM_DOWNLOAD_STATE_FAILED;
495                         break;
496                 case DOWNLOAD_STATE_CANCELED:
497                         result = TIZEN_ENUM_DOWNLOAD_STATE_CANCELED;
498                         break;
499                 default:
500                         result = "undefined";
501                         LOGW("Unknown DownloadState was returned.");
502                         break;
503         }
504
505         return result;
506 }
507
508 DownloadRequest* DownloadManager::getDownloadRequest(long downloadId)
509 {
510         int ret;
511         int i;
512
513         char *url = NULL;
514         char *destination = NULL;
515         char *fileName = NULL;
516         download_network_type_e networkTypeValue = DOWNLOAD_NETWORK_ALL;
517         char **fieldNames = NULL;
518         char *fieldValue = NULL;
519         int fieldLength = 0;
520
521         ret = download_get_url(downloadId, &url);
522         if (ret != DOWNLOAD_ERROR_NONE) {
523                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
524                         throw NotFoundException("download id could not found.");
525                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
526                         throw InvalidValuesException("download id is not valid.");
527                 }
528                 throw UnknownException(("Platform error while getting url. " + _get_download_error(ret)).c_str());
529         }
530
531         ret = download_get_destination(downloadId, &destination);
532         if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
533                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
534                         throw NotFoundException("download id could not found.");
535                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
536                         throw InvalidValuesException("download id is not valid.");
537                 }
538                 throw UnknownException(("Platform error while getting destination. " + _get_download_error(ret)).c_str());
539         }
540
541         ret = download_get_file_name(downloadId, &fileName);
542         if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
543                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
544                         throw NotFoundException("download id could not found.");
545                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
546                         throw InvalidValuesException("download id is not valid.");
547                 }
548                 throw UnknownException(("Platform error while getting fileName. " + _get_download_error(ret)).c_str());
549         }
550
551         ret = download_get_network_type(downloadId, &networkTypeValue);
552         if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
553                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
554                         throw NotFoundException("download id could not found.");
555                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
556                         throw InvalidValuesException("download id is not valid.");
557                 }
558                 throw UnknownException(("Platform error while getting network type. " + _get_download_error(ret)).c_str());
559         }
560
561         ret = download_get_http_header_field_list(downloadId, &fieldNames, &fieldLength);
562         if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
563                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
564                         throw NotFoundException("download id could not found.");
565                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
566                         throw InvalidValuesException("download id is not valid.");
567                 }
568                 throw UnknownException(("Platform error while getting http header fields. " + _get_download_error(ret)).c_str());
569         }
570
571         std::map<std::string, std::string> httpHeader;
572         for (i = 0; i < fieldLength; i++) {
573                 ret = download_get_http_header_field(downloadId, fieldNames[i], &fieldValue);
574                 if (ret != DOWNLOAD_ERROR_NONE) {
575                         LOGW("Platform error while getting http header field. %s", _get_download_error(ret).c_str());
576                 }
577                 httpHeader.insert(make_pair(std::string(fieldNames[i]), std::string(fieldValue)));
578                 free(fieldNames[i]);
579                 free(fieldValue);
580         }
581         free(fieldNames);
582
583         DownloadRequest *request = new DownloadRequest();
584
585         if (url) {
586                 request->setUrl(url);
587                 free(url);
588         }
589
590         if (destination) {
591                 std::string virtualPath;
592                 try {
593                         virtualPath = DeviceAPI::Filesystem::Utils::toVirtualPath(NULL, destination);
594                 } catch (...) {
595                         LOGW("Platform error while converting destination path.");
596                         virtualPath = destination;
597                 }
598                 request->setDestination(virtualPath);
599                 free(destination);
600         }
601
602         if (fileName) {
603                 request->setFileName(fileName);
604                 free(fileName);
605         }
606
607         switch(networkTypeValue) {
608         case DOWNLOAD_NETWORK_DATA_NETWORK:
609                 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_CELLULAR);
610                 break;
611         case DOWNLOAD_NETWORK_WIFI:
612                 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_WIFI);
613                 break;
614         default:
615                 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_ALL);
616                 break;
617         }
618
619         if (fieldLength) {
620                 request->setHttpHeader(httpHeader);
621         }
622
623
624         return request;
625 }
626
627 std::string DownloadManager::getMIMEType(long downloadId)
628 {
629         int ret;
630         char *mimeType = NULL;
631         std::string result("");
632
633         ret = download_get_mime_type(downloadId, &mimeType);
634         if (ret != DOWNLOAD_ERROR_NONE) {
635                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
636                         throw NotFoundException("download id could not found.");
637                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
638                         throw InvalidValuesException("download id is not valid.");
639                 } else if (ret == DOWNLOAD_ERROR_NO_DATA) {
640                         result = "";
641                 } else {
642                         throw UnknownException(("Platform error while getting MIME type. " + _get_download_error(ret)).c_str());
643                 }
644         } else {
645                 result = mimeType;
646         }
647
648         if (mimeType) {
649                 free(mimeType);
650         }
651
652         return result;
653 }
654
655 void DownloadManager::setListener(long downloadId, DownloadCallback *downloadCallback)
656 {
657         int ret;
658
659         ret = download_set_state_changed_cb(downloadId, download_state_changed_cb, this);
660         if (ret != DOWNLOAD_ERROR_NONE) {
661                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
662                         throw NotFoundException("download id could not found.");
663                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
664                         throw InvalidValuesException("download id is not valid.");
665                 }
666                 throw UnknownException(("Platform error while setting state changed callback. " + _get_download_error(ret)).c_str());
667         }
668
669         ret = download_set_progress_cb(downloadId, download_progress_cb, this);
670         if (ret != DOWNLOAD_ERROR_NONE) {
671                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
672                         throw NotFoundException("download id could not found.");
673                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
674                         throw InvalidValuesException("download id is not valid.");
675                 }
676                 throw UnknownException(("Platform error while setting progress callback. " + _get_download_error(ret)).c_str());
677         }
678
679         setCallbackToMap(downloadId, downloadCallback);
680 }
681
682 } // Download
683 } // DeviceAPI