3f1b57c4ff2d603fb43cabbd47556ad89c5597b8
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Contact / ContactDownloadManager.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        ContactDownloadManager.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief
22  */
23
24 #include "ContactDownloadManager.h"
25
26 #include <ctime>
27 #include <cstdlib>
28 #include <cstddef>
29 #include <fstream>
30 #include <pcrecpp.h>
31 #include <dpl/log/log.h>
32 #include <dpl/exception.h>
33 #include <Commons/Exception.h>
34 #include <Commons/Regex.h>
35
36 namespace TizenApis {
37 namespace Platform {
38 namespace Contact {
39
40 using namespace WrtDeviceApis::Commons;
41
42 ContactDownloadManager::ContactDownloadManager()
43 {
44 }
45
46 ContactDownloadManager::~ContactDownloadManager()
47 {
48 }
49
50 string ContactDownloadManager::downloadImage(const string &imgUrl) const
51 {
52         LogDebug("URL : " << imgUrl);
53
54         string result;
55         Try {
56                 DownImageInfo downImageInfo;
57                 url_download_h urlDownload;
58                 int ud_ret = -1;
59                 int id = 0;
60
61                 DPL::WaitableEvent waitableEvent;
62                 downImageInfo.setWaitableEvent(&waitableEvent);
63
64                 ud_ret = url_download_create(&urlDownload);
65                 if (ud_ret != URL_DOWNLOAD_ERROR_NONE)
66                 {
67                         ThrowMsg(PlatformException, "Error during url_download_create() ret: " << ud_ret);
68                 }
69
70                 ud_ret = url_download_set_url(urlDownload, imgUrl.c_str());
71                 if (ud_ret != URL_DOWNLOAD_ERROR_NONE)
72                 {
73                         url_download_destroy(urlDownload);
74                         ThrowMsg(PlatformException, "Error during url_download_set_url() ret: " << ud_ret);
75                 }
76
77                 ud_ret = url_download_set_stopped_cb(urlDownload, urlDownloadStoppedCB, static_cast<void*>(&downImageInfo));
78                 if (ud_ret != URL_DOWNLOAD_ERROR_NONE)
79                 {
80                         url_download_destroy(urlDownload);
81                         ThrowMsg(PlatformException, "Error during url_download_set_stopped_cb() ret: " << ud_ret);
82                 }
83
84                 ud_ret = url_download_set_completed_cb(urlDownload, urlDownloadCompletedCB, static_cast<void*>(&downImageInfo));
85                 if (ud_ret != URL_DOWNLOAD_ERROR_NONE)
86                 {
87                         url_download_destroy(urlDownload);
88                         ThrowMsg(PlatformException, "Error during url_download_set_completed_cb() ret: " << ud_ret);
89                 }
90
91                 ud_ret = url_download_start(urlDownload, &id);
92                 if (ud_ret != URL_DOWNLOAD_ERROR_NONE)
93                 {
94                         url_download_destroy(urlDownload);
95                         ThrowMsg(PlatformException, "Error during url_download_start() ret: " << ud_ret);
96                 }
97
98                 DPL::WaitForSingleHandle(waitableEvent.GetHandle());
99                 waitableEvent.Reset();
100                 if ((downImageInfo.getDownloadedImagePath()).empty()) {
101                         ThrowMsg(PlatformException, "Download failed");
102                 }
103
104                 url_download_destroy(urlDownload);
105                 result = "file://" + downImageInfo.getDownloadedImagePath();
106
107         } Catch (Exception) {
108                 LogError("Probably invalid URL(" << imgUrl << ") " << _rethrown_exception.GetMessage());
109         }
110
111         return result;
112 }
113
114 string ContactDownloadManager::getRealPath(const string &path) const
115 {
116         LogDebug("Path : " << path);
117
118         string result;
119         Try
120         {
121                 if(validate("^http(s)?\\://[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(/\\S*)?$", path, VALIDATE_MATCH_CASELESS))
122                 {
123                         // if path is URL then Image will be downloaded.
124                         result = downloadImage(path);
125                 }
126                 else if(validate("^file\\:///[a-zA-Z]{2,3}(/\\S*)?$", path, VALIDATE_MATCH_CASELESS))
127                 {
128                         result = path;
129                 }
130                 else if(validate("^/[a-zA-Z]{2,3}(/\\S*)?$", path, VALIDATE_MATCH_CASELESS))
131                 {
132                         result = "file://" + path;
133                 }
134         }
135         Catch(Exception)
136         {
137                 //probably path doesn't exist
138                 LogError("invalid path(" << path << ")");
139         }
140
141         return result;
142 }
143
144 void ContactDownloadManager::urlDownloadStoppedCB(url_download_h download, url_download_error_e error, void *user_data)
145 {
146         if (download == NULL)
147         {
148                 LogDebug("notify_info is NULL!!");
149         }
150
151         if (error != URL_DOWNLOAD_ERROR_NONE)
152         {
153                 if (user_data != NULL)
154                 {
155                         DownImageInfo *downImageInfo;
156                         downImageInfo = static_cast<DownImageInfo *>(user_data);
157                         (downImageInfo->getWaitableEvent())->Signal();
158                 }
159         }
160 }
161
162 void ContactDownloadManager::urlDownloadCompletedCB(url_download_h download, const char *installed_path, void *user_data)
163 {
164         if(download == NULL)
165         {
166                 LogDebug("download_info is NULL!!");
167                 return;
168         }
169
170         if (user_data == NULL)
171         {
172                 LogDebug("user_data is NULL!!");
173                 return;
174         }
175
176         if (installed_path)
177         {
178                 LogDebug("installed_path : " << installed_path);
179
180                 DownImageInfo *downImageInfo = static_cast<DownImageInfo*>(user_data);
181                 string savedPath = installed_path;
182                 downImageInfo->setDownloadedImagePath(savedPath);
183                 (downImageInfo->getWaitableEvent())->Signal();
184         }
185 }
186
187 ContactDownloadManagerPtr ContactDownloadManager::getInstance()
188 {
189         static ContactDownloadManagerPtr downloadManager(new ContactDownloadManager());
190
191         return downloadManager;
192 }
193
194 } // Contact
195 } // Platform
196 } // TizenApis