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 ContactDownloadManager.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
24 #include "ContactDownloadManager.h"
31 #include <dpl/log/log.h>
32 #include <dpl/exception.h>
33 #include <Commons/Exception.h>
34 #include <Commons/Regex.h>
40 using namespace WrtDeviceApis::Commons;
42 ContactDownloadManager::ContactDownloadManager()
46 ContactDownloadManager::~ContactDownloadManager()
50 string ContactDownloadManager::downloadImage(const string &imgUrl) const
52 LogDebug("URL : " << imgUrl);
56 DownImageInfo downImageInfo;
57 url_download_h urlDownload;
61 DPL::WaitableEvent waitableEvent;
62 downImageInfo.setWaitableEvent(&waitableEvent);
64 ud_ret = url_download_create(&urlDownload);
65 if (ud_ret != URL_DOWNLOAD_ERROR_NONE)
67 ThrowMsg(PlatformException, "Error during url_download_create() ret: " << ud_ret);
70 ud_ret = url_download_set_url(urlDownload, imgUrl.c_str());
71 if (ud_ret != URL_DOWNLOAD_ERROR_NONE)
73 url_download_destroy(urlDownload);
74 ThrowMsg(PlatformException, "Error during url_download_set_url() ret: " << ud_ret);
77 ud_ret = url_download_set_stopped_cb(urlDownload, urlDownloadStoppedCB, static_cast<void*>(&downImageInfo));
78 if (ud_ret != URL_DOWNLOAD_ERROR_NONE)
80 url_download_destroy(urlDownload);
81 ThrowMsg(PlatformException, "Error during url_download_set_stopped_cb() ret: " << ud_ret);
84 ud_ret = url_download_set_completed_cb(urlDownload, urlDownloadCompletedCB, static_cast<void*>(&downImageInfo));
85 if (ud_ret != URL_DOWNLOAD_ERROR_NONE)
87 url_download_destroy(urlDownload);
88 ThrowMsg(PlatformException, "Error during url_download_set_completed_cb() ret: " << ud_ret);
91 ud_ret = url_download_start(urlDownload, &id);
92 if (ud_ret != URL_DOWNLOAD_ERROR_NONE)
94 url_download_destroy(urlDownload);
95 ThrowMsg(PlatformException, "Error during url_download_start() ret: " << ud_ret);
98 DPL::WaitForSingleHandle(waitableEvent.GetHandle());
99 waitableEvent.Reset();
100 if ((downImageInfo.getDownloadedImagePath()).empty()) {
101 ThrowMsg(PlatformException, "Download failed");
104 url_download_destroy(urlDownload);
105 result = "file://" + downImageInfo.getDownloadedImagePath();
107 } Catch (Exception) {
108 LogError("Probably invalid URL(" << imgUrl << ") " << _rethrown_exception.GetMessage());
114 string ContactDownloadManager::getRealPath(const string &path) const
116 LogDebug("Path : " << path);
121 if(validate("^http(s)?\\://[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(/\\S*)?$", path, VALIDATE_MATCH_CASELESS))
123 // if path is URL then Image will be downloaded.
124 result = downloadImage(path);
126 else if(validate("^file\\:///[a-zA-Z]{2,3}(/\\S*)?$", path, VALIDATE_MATCH_CASELESS))
130 else if(validate("^/[a-zA-Z]{2,3}(/\\S*)?$", path, VALIDATE_MATCH_CASELESS))
132 result = "file://" + path;
137 //probably path doesn't exist
138 LogError("invalid path(" << path << ")");
144 void ContactDownloadManager::urlDownloadStoppedCB(url_download_h download, url_download_error_e error, void *user_data)
146 if (download == NULL)
148 LogDebug("notify_info is NULL!!");
151 if (error != URL_DOWNLOAD_ERROR_NONE)
153 if (user_data != NULL)
155 DownImageInfo *downImageInfo;
156 downImageInfo = static_cast<DownImageInfo *>(user_data);
157 (downImageInfo->getWaitableEvent())->Signal();
162 void ContactDownloadManager::urlDownloadCompletedCB(url_download_h download, const char *installed_path, void *user_data)
166 LogDebug("download_info is NULL!!");
170 if (user_data == NULL)
172 LogDebug("user_data is NULL!!");
178 LogDebug("installed_path : " << installed_path);
180 DownImageInfo *downImageInfo = static_cast<DownImageInfo*>(user_data);
181 string savedPath = installed_path;
182 downImageInfo->setDownloadedImagePath(savedPath);
183 (downImageInfo->getWaitableEvent())->Signal();
187 ContactDownloadManagerPtr ContactDownloadManager::getInstance()
189 static ContactDownloadManagerPtr downloadManager(new ContactDownloadManager());
191 return downloadManager;