efccf3d6f3cd6af85d5446b28fe1731622ecb65a
[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()) {
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 if (networkType == TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_ALL) {
393                         ret = download_set_network_type(downloadId, DOWNLOAD_NETWORK_ALL);
394                 } else {
395                         throw TypeMismatchException("Wrong DownloadNetworkType.");
396                 }
397                 if (ret != DOWNLOAD_ERROR_NONE) {
398                         throw UnknownException(("Platform error while setting network type. " + _get_download_error(ret)).c_str());
399                 }
400         }
401
402         std::map<std::string, std::string>::const_iterator iter;
403         for (iter = httpHeader.begin(); iter != httpHeader.end(); ++iter) {
404                 ret = download_add_http_header_field(downloadId, iter->first.c_str(), iter->second.c_str());
405                 if (ret != DOWNLOAD_ERROR_NONE) {
406                         throw UnknownException(("Platform error while setting http header fields. " + _get_download_error(ret)).c_str());
407                 }
408         }
409
410         ret = download_start(downloadId);
411         if (ret != DOWNLOAD_ERROR_NONE) {
412                 throw UnknownException(("Platform error while starting download. " + _get_download_error(ret)).c_str());
413         }
414
415         setCallbackToMap(downloadId, downloadCallback);
416
417         return downloadId;
418 }
419
420 void DownloadManager::cancel(long downloadId)
421 {
422         int ret;
423
424         ret = download_cancel(downloadId);
425         if (ret != DOWNLOAD_ERROR_NONE) {
426                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
427                         throw NotFoundException("download id could not found.");
428                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
429                         throw InvalidValuesException("download id is not valid.");
430                 }
431                 throw UnknownException(("Platform error while canceling download. " + _get_download_error(ret)).c_str());
432         }
433 }
434
435 void DownloadManager::pause(long downloadId)
436 {
437         int ret;
438
439         ret = download_pause(downloadId);
440         if (ret != DOWNLOAD_ERROR_NONE) {
441                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
442                         throw NotFoundException("download id could not found.");
443                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
444                         throw InvalidValuesException("download id is not valid.");
445                 }
446                 throw UnknownException(("Platform error while pausing download. " + _get_download_error(ret)).c_str());
447         }
448 }
449
450 void DownloadManager::resume(long downloadId)
451 {
452         int ret;
453
454         ret = download_start(downloadId);
455         if (ret != DOWNLOAD_ERROR_NONE) {
456                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
457                         throw NotFoundException("download id could not found.");
458                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
459                         throw InvalidValuesException("download id is not valid.");
460                 }
461                 throw UnknownException(("Platform error while resuming download. " + _get_download_error(ret)).c_str());
462         }
463 }
464
465 std::string DownloadManager::getState(long downloadId)
466 {
467         int ret;
468         download_state_e state;
469         std::string result;
470
471         ret = download_get_state(downloadId, &state);
472         if (ret != DOWNLOAD_ERROR_NONE) {
473                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
474                         throw NotFoundException("download id could not found.");
475                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
476                         throw InvalidValuesException("download id is not valid.");
477                 }
478                 throw UnknownException(("Platform error while getting state. " + _get_download_error(ret)).c_str());
479         }
480
481         switch (state) {
482                 case DOWNLOAD_STATE_READY:
483                 case DOWNLOAD_STATE_QUEUED:
484                         result = TIZEN_ENUM_DOWNLOAD_STATE_QUEUED;
485                         break;
486                 case DOWNLOAD_STATE_DOWNLOADING:
487                         result = TIZEN_ENUM_DOWNLOAD_STATE_DOWNLOADING;
488                         break;
489                 case DOWNLOAD_STATE_PAUSED:
490                         result = TIZEN_ENUM_DOWNLOAD_STATE_PAUSED;
491                         break;
492                 case DOWNLOAD_STATE_COMPLETED:
493                         result = TIZEN_ENUM_DOWNLOAD_STATE_COMPLETED;
494                         break;
495                 case DOWNLOAD_STATE_FAILED:
496                         result = TIZEN_ENUM_DOWNLOAD_STATE_FAILED;
497                         break;
498                 case DOWNLOAD_STATE_CANCELED:
499                         result = TIZEN_ENUM_DOWNLOAD_STATE_CANCELED;
500                         break;
501                 default:
502                         result = "undefined";
503                         LOGW("Unknown DownloadState was returned.");
504                         break;
505         }
506
507         return result;
508 }
509
510 DownloadRequest* DownloadManager::getDownloadRequest(long downloadId)
511 {
512         int ret;
513         int i;
514
515         char *url = NULL;
516         char *destination = NULL;
517         char *fileName = NULL;
518         download_network_type_e networkTypeValue = DOWNLOAD_NETWORK_ALL;
519         char **fieldNames = NULL;
520         char *fieldValue = NULL;
521         int fieldLength = 0;
522
523         ret = download_get_url(downloadId, &url);
524         if (ret != DOWNLOAD_ERROR_NONE) {
525                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
526                         throw NotFoundException("download id could not found.");
527                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
528                         throw InvalidValuesException("download id is not valid.");
529                 }
530                 throw UnknownException(("Platform error while getting url. " + _get_download_error(ret)).c_str());
531         }
532
533         ret = download_get_destination(downloadId, &destination);
534         if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
535                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
536                         throw NotFoundException("download id could not found.");
537                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
538                         throw InvalidValuesException("download id is not valid.");
539                 }
540                 throw UnknownException(("Platform error while getting destination. " + _get_download_error(ret)).c_str());
541         }
542
543         ret = download_get_file_name(downloadId, &fileName);
544         if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
545                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
546                         throw NotFoundException("download id could not found.");
547                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
548                         throw InvalidValuesException("download id is not valid.");
549                 }
550                 throw UnknownException(("Platform error while getting fileName. " + _get_download_error(ret)).c_str());
551         }
552
553         ret = download_get_network_type(downloadId, &networkTypeValue);
554         if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
555                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
556                         throw NotFoundException("download id could not found.");
557                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
558                         throw InvalidValuesException("download id is not valid.");
559                 }
560                 throw UnknownException(("Platform error while getting network type. " + _get_download_error(ret)).c_str());
561         }
562
563         ret = download_get_http_header_field_list(downloadId, &fieldNames, &fieldLength);
564         if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
565                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
566                         throw NotFoundException("download id could not found.");
567                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
568                         throw InvalidValuesException("download id is not valid.");
569                 }
570                 throw UnknownException(("Platform error while getting http header fields. " + _get_download_error(ret)).c_str());
571         }
572
573         std::map<std::string, std::string> httpHeader;
574         for (i = 0; i < fieldLength; i++) {
575                 ret = download_get_http_header_field(downloadId, fieldNames[i], &fieldValue);
576                 if (ret != DOWNLOAD_ERROR_NONE) {
577                         LOGW("Platform error while getting http header field. %s", _get_download_error(ret).c_str());
578                 }
579                 httpHeader.insert(make_pair(std::string(fieldNames[i]), std::string(fieldValue)));
580                 free(fieldNames[i]);
581                 free(fieldValue);
582         }
583         free(fieldNames);
584
585         DownloadRequest *request = new DownloadRequest();
586
587         if (url) {
588                 request->setUrl(url);
589                 free(url);
590         }
591
592         if (destination) {
593                 std::string virtualPath;
594                 try {
595                         virtualPath = DeviceAPI::Filesystem::Utils::toVirtualPath(NULL, destination);
596                 } catch (...) {
597                         LOGW("Platform error while converting destination path.");
598                         virtualPath = destination;
599                 }
600                 request->setDestination(virtualPath);
601                 free(destination);
602         }
603
604         if (fileName) {
605                 request->setFileName(fileName);
606                 free(fileName);
607         }
608
609         switch(networkTypeValue) {
610         case DOWNLOAD_NETWORK_DATA_NETWORK:
611                 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_CELLULAR);
612                 break;
613         case DOWNLOAD_NETWORK_WIFI:
614                 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_WIFI);
615                 break;
616         default:
617                 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_ALL);
618                 break;
619         }
620
621         if (fieldLength) {
622                 request->setHttpHeader(httpHeader);
623         }
624
625
626         return request;
627 }
628
629 std::string DownloadManager::getMIMEType(long downloadId)
630 {
631         int ret;
632         char *mimeType = NULL;
633         std::string result("");
634
635         ret = download_get_mime_type(downloadId, &mimeType);
636         if (ret != DOWNLOAD_ERROR_NONE) {
637                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
638                         throw NotFoundException("download id could not found.");
639                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
640                         throw InvalidValuesException("download id is not valid.");
641                 } else if (ret == DOWNLOAD_ERROR_NO_DATA) {
642                         result = "";
643                 } else {
644                         throw UnknownException(("Platform error while getting MIME type. " + _get_download_error(ret)).c_str());
645                 }
646         } else {
647                 result = mimeType;
648         }
649
650         if (mimeType) {
651                 free(mimeType);
652         }
653
654         return result;
655 }
656
657 void DownloadManager::setListener(long downloadId, DownloadCallback *downloadCallback)
658 {
659         int ret;
660
661         ret = download_set_state_changed_cb(downloadId, download_state_changed_cb, this);
662         if (ret != DOWNLOAD_ERROR_NONE) {
663                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
664                         throw NotFoundException("download id could not found.");
665                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
666                         throw InvalidValuesException("download id is not valid.");
667                 }
668                 throw UnknownException(("Platform error while setting state changed callback. " + _get_download_error(ret)).c_str());
669         }
670
671         ret = download_set_progress_cb(downloadId, download_progress_cb, this);
672         if (ret != DOWNLOAD_ERROR_NONE) {
673                 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
674                         throw NotFoundException("download id could not found.");
675                 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
676                         throw InvalidValuesException("download id is not valid.");
677                 }
678                 throw UnknownException(("Platform error while setting progress callback. " + _get_download_error(ret)).c_str());
679         }
680
681         setCallbackToMap(downloadId, downloadCallback);
682 }
683
684 } // Download
685 } // DeviceAPI