merge wrt-plugins-tizen_0.2.0-3
[platform/framework/web/wrt-plugins-tizen.git] / src / platform / Tizen / Contact / DownloadManager.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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        DownloadManager.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief
22  */
23
24 #include <ctime>
25 #include <cstdlib>
26 #include <cstddef>
27 #include <fstream>
28 #include <sstream>
29 #include <pcrecpp.h>
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"
35
36 namespace TizenApis {
37 namespace Platform {
38 namespace Contact {
39
40 using namespace WrtDeviceApis::Commons;
41
42 DownloadManager::DownloadManager()
43 {
44         int da_ret = DA_INVALID_ID;
45
46         da_client_cb_t da_cb = { 0 };
47
48         da_cb.user_noti_cb = &daNotifyCallback;
49         da_cb.send_dd_info_cb = &daGetDdInfoCallback;
50         da_cb.update_dl_info_cb = &daUpdateDownloadInfoCallback;
51
52         da_ret = da_init(&da_cb, DA_DOWNLOAD_MANAGING_METHOD_AUTO);
53
54         if (da_ret != DA_RESULT_OK)
55         {
56                 LogDebug("Error during da_init() da_ret: " << da_ret);
57         }
58 }
59
60 DownloadManager::~DownloadManager()
61 {
62         da_deinit();
63 }
64
65 string DownloadManager::downloadImage(const string &imgUrl) const
66 {
67         LogDebug("URL : " << imgUrl);
68
69         string result;
70         Try {
71                 DownImageInfo downImageInfo;
72                 int da_ret = DA_INVALID_ID;
73                 int download_id = -1;
74
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: "
83                                         << da_ret);
84                 }
85
86                 DPL::WaitForSingleHandle(waitableEvent.GetHandle());
87                 waitableEvent.Reset();
88                 if ((downImageInfo.getDownloadedImagePath()).empty()) {
89                         ThrowMsg(PlatformException, "Download failed");
90                 }
91
92                 result = downImageInfo.getDownloadedImagePath();
93
94         } Catch (Exception) {
95                 LogError("Probably invalid URL(" << imgUrl << ") " << _rethrown_exception.GetMessage());
96         }
97
98         return result;
99 }
100
101 string DownloadManager::getRealPath(const string &path) const
102 {
103         LogDebug("Path : " << path);
104
105         string result;
106         Try
107         {
108                 if(validate("^http(s)?\\://[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(/\\S*)?$", path, VALIDATE_MATCH_CASELESS))
109                 {
110                         // if path is URL then Image will be downloaded.
111                         result = downloadImage(path);
112                 }
113                 else if(validate("^file\\:///[a-zA-Z]{2,3}(/\\S*)?$", path, VALIDATE_MATCH_CASELESS))
114                 {
115                         result = path;
116                 }
117                 else if(validate("^/[a-zA-Z]{2,3}(/\\S*)?$", path, VALIDATE_MATCH_CASELESS))
118                 {
119                         result = "file://" + path;
120                 }
121         }
122         Catch(Exception)
123         {
124                 //probably path doesn't exist
125                 LogError("invalid path(" << path << ")");
126         }
127
128         return result;
129 }
130
131 void DownloadManager::daNotifyCallback(user_notify_info_t *notify_info, void *user_data)
132 {
133         if (notify_info == NULL)
134         {
135                 LogDebug("notify_info is NULL!!");
136         }
137
138         if (notify_info->state == DA_STATE_FAILED ||
139                                 notify_info->state == DA_STATE_CANCELED)
140         {
141                 if (user_data != NULL)
142                 {
143                         DownImageInfo *downImageInfo;
144                         downImageInfo = static_cast<DownImageInfo *>(user_data);
145                         (downImageInfo->getWaitableEvent())->Signal();
146                 }
147         }
148 }
149
150 void DownloadManager::daGetDdInfoCallback(user_dd_info_t *dd_info, void *user_data)
151 {
152         /* Empty CB */
153 }
154
155 void DownloadManager::daUpdateDownloadInfoCallback(user_download_info_t *download_info, void *user_data)
156 {
157         if(download_info == NULL)
158         {
159                 LogDebug("download_info is NULL!!");
160                 return;
161         }
162
163         if (user_data == NULL)
164         {
165                 LogDebug("user_data is NULL!!");
166                 return;
167         }
168
169         if (download_info->saved_path)
170         {
171                 LogDebug("download_info->saved_path : " << download_info->saved_path);
172
173                 DownImageInfo *downImageInfo = static_cast<DownImageInfo*>(user_data);
174                 string savedPath = download_info->saved_path;
175                 downImageInfo->setDownloadedImagePath(savedPath);
176                 (downImageInfo->getWaitableEvent())->Signal();
177         }
178 }
179
180 DownloadManagerPtr DownloadManager::getInstance()
181 {
182         static DownloadManagerPtr downloadManager(new DownloadManager());
183
184         return downloadManager;
185 }
186
187 } // Contact
188 } // Platform
189 } // TizenApis