2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file DownloadManager.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
30 #include <dpl/log/log.h>
31 #include <dpl/exception.h>
32 #include <Commons/Exception.h>
33 #include <Commons/Regex.h>
34 #include "DownloadManager.h"
40 using namespace WrtDeviceApis::Commons;
42 DownloadManager::DownloadManager()
44 int da_ret = DA_INVALID_ID;
46 da_client_cb_t da_cb = { 0 };
48 da_cb.user_noti_cb = &daNotifyCallback;
49 da_cb.send_dd_info_cb = &daGetDdInfoCallback;
50 da_cb.update_dl_info_cb = &daUpdateDownloadInfoCallback;
52 da_ret = da_init(&da_cb, DA_DOWNLOAD_MANAGING_METHOD_AUTO);
54 if (da_ret != DA_RESULT_OK)
56 LogDebug("Error during da_init() da_ret: " << da_ret);
60 DownloadManager::~DownloadManager()
65 string DownloadManager::downloadImage(const string &imgUrl) const
67 LogDebug("URL : " << imgUrl);
71 DownImageInfo downImageInfo;
72 int da_ret = DA_INVALID_ID;
75 DPL::WaitableEvent waitableEvent;
76 downImageInfo.setWaitableEvent(&waitableEvent);
77 da_ret = da_start_download_with_extension(imgUrl.c_str(),
78 &download_id, DA_FEATURE_USER_DATA,
79 static_cast<void*>(&downImageInfo), NULL);
80 if (da_ret != DA_RESULT_OK) {
81 ThrowMsg(PlatformException,
82 "Error during da_start_download_with_extension() da_ret: "
86 DPL::WaitForSingleHandle(waitableEvent.GetHandle());
87 waitableEvent.Reset();
88 if ((downImageInfo.getDownloadedImagePath()).empty()) {
89 ThrowMsg(PlatformException, "Download failed");
92 result = downImageInfo.getDownloadedImagePath();
95 LogError("Probably invalid URL(" << imgUrl << ") " << _rethrown_exception.GetMessage());
101 string DownloadManager::getRealPath(const string &path) const
103 LogDebug("Path : " << path);
108 if(validate("^http(s)?\\://[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(/\\S*)?$", path, VALIDATE_MATCH_CASELESS))
110 // if path is URL then Image will be downloaded.
111 result = downloadImage(path);
113 else if(validate("^file\\:///[a-zA-Z]{2,3}(/\\S*)?$", path, VALIDATE_MATCH_CASELESS))
117 else if(validate("^/[a-zA-Z]{2,3}(/\\S*)?$", path, VALIDATE_MATCH_CASELESS))
119 result = "file://" + path;
124 //probably path doesn't exist
125 LogError("invalid path(" << path << ")");
131 void DownloadManager::daNotifyCallback(user_notify_info_t *notify_info, void *user_data)
133 if (notify_info == NULL)
135 LogDebug("notify_info is NULL!!");
138 if (notify_info->state == DA_STATE_FAILED ||
139 notify_info->state == DA_STATE_CANCELED)
141 if (user_data != NULL)
143 DownImageInfo *downImageInfo;
144 downImageInfo = static_cast<DownImageInfo *>(user_data);
145 (downImageInfo->getWaitableEvent())->Signal();
150 void DownloadManager::daGetDdInfoCallback(user_dd_info_t *dd_info, void *user_data)
155 void DownloadManager::daUpdateDownloadInfoCallback(user_download_info_t *download_info, void *user_data)
157 if(download_info == NULL)
159 LogDebug("download_info is NULL!!");
163 if (user_data == NULL)
165 LogDebug("user_data is NULL!!");
169 if (download_info->saved_path)
171 LogDebug("download_info->saved_path : " << download_info->saved_path);
173 DownImageInfo *downImageInfo = static_cast<DownImageInfo*>(user_data);
174 string savedPath = download_info->saved_path;
175 downImageInfo->setDownloadedImagePath(savedPath);
176 (downImageInfo->getWaitableEvent())->Signal();
180 DownloadManagerPtr DownloadManager::getInstance()
182 static DownloadManagerPtr downloadManager(new DownloadManager());
184 return downloadManager;