Set mime type for bundle in case of mmsto, sms
[platform/framework/web/wrt.git] / src / view / common / view_logic_web_notification_support.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  * @file    view_logic_web_notification_support.cpp
18  * @author  Jihoon Chung (jihoon.chung@samsung.com)
19  * @brief   Implementation file of web Notification API used by ViewLogic
20  */
21
22 #include "view_logic_web_notification_support.h"
23 #include <string>
24 #include <dpl/log/log.h>
25 #include <dpl/string.h>
26
27 #include <notification.h>
28 #include <pcrecpp.h>
29 #include <sstream>
30 #include <curl/curl.h>
31 #include <widget_model.h>
32
33 namespace {
34 const char* PATTERN_CHECK_EXTERNAL = "^http(s)?://\\w+.*$";
35 }
36
37 namespace ViewModule {
38 namespace WebNotification {
39
40 bool notificationShow(WebNotificationDataPtr notiData);
41 bool notificationHide(WebNotificationDataPtr notiData);
42 bool isExternalUri(const std::string &pattern, const std::string &uri);
43 bool downloadImage(WebNotificationDataPtr notiData);
44 size_t curlWriteData(void *ptr, size_t size, size_t nmemb, FILE *stream);
45
46 WebNotificationData::WebNotificationData(WidgetModel* widgetModel,
47                                          uint64_t id) :
48     m_widgetModel(widgetModel),
49     m_id(id)
50 {
51     Assert(m_widgetModel);
52 }
53
54 WebNotificationData::~WebNotificationData()
55 {
56 }
57
58 bool showWebNotification(WebNotificationDataPtr notiData)
59 {
60     LogInfo("showWebNotification called");
61
62     /* TODO : register notification to database */
63     return notificationShow(notiData);
64 }
65
66 bool notificationShow(WebNotificationDataPtr notiData)
67 {
68     LogDebug("notificationShow called");
69     Assert(notiData);
70     notification_h noti_h = NULL;
71     notification_error_e error = NOTIFICATION_ERROR_NONE;
72
73     Try {
74         // create notification
75         noti_h = notification_new(
76             NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_NONE, notiData->m_id);
77         if (!noti_h) {
78             LogError("Fail to notification_new");
79             return false;
80         }
81
82         // set notification title
83         error = notification_set_text(
84             noti_h,
85             NOTIFICATION_TEXT_TYPE_TITLE,
86             notiData->m_title.c_str(),
87             NULL,
88             NOTIFICATION_VARIABLE_TYPE_NONE);
89         if (error != NOTIFICATION_ERROR_NONE) {
90             ThrowMsg(Exception::NotificationShowError, "Fail to set title");
91         }
92
93         // set notification content
94         error = notification_set_text(
95             noti_h,
96             NOTIFICATION_TEXT_TYPE_CONTENT,
97             notiData->m_body.c_str(),
98             NULL,
99             NOTIFICATION_VARIABLE_TYPE_NONE);
100         if (error != NOTIFICATION_ERROR_NONE) {
101             ThrowMsg(Exception::NotificationShowError,
102                      "Fail to set content:" << error);
103         }
104
105         //check uri is "http", https" or not
106         bool validIconURL = true;
107         LogInfo("url path is " << notiData->m_iconURL);
108         if (isExternalUri(PATTERN_CHECK_EXTERNAL, notiData->m_iconURL)) {
109             //download image from url
110             validIconURL = downloadImage(notiData);
111         }
112
113         //set image
114         // If fail to download external image, skip to set image.
115         // In this case, set to default package image.
116         if (true == validIconURL) {
117             error = notification_set_image(
118                 noti_h,
119                 NOTIFICATION_IMAGE_TYPE_ICON,
120                 notiData->m_iconURL.c_str());
121             if (error != NOTIFICATION_ERROR_NONE) {
122                 ThrowMsg(Exception::NotificationShowError,
123                          "Fail to free notification: " << error);
124             }
125         }
126
127         // insert notification
128         int priv_id = NOTIFICATION_PRIV_ID_NONE;
129         error = notification_insert(noti_h, &priv_id);
130         if (error != NOTIFICATION_ERROR_NONE) {
131             ThrowMsg(Exception::NotificationShowError,
132                      "Fail to insert notification: " << error);
133         }
134
135         LogInfo("Notification is inserted!");
136         LogInfo("noti id =[" << notiData->m_id << "] " <<
137                 "priv_id =[" << priv_id << "]");
138
139         if (noti_h) {
140             error = notification_free(noti_h);
141             if (error != NOTIFICATION_ERROR_NONE) {
142                         ThrowMsg(Exception::NotificationShowError,
143                                  "Fail to free notification: " << error);
144             }
145             noti_h = NULL;
146         }
147         return true;
148     } Catch(Exception::NotificationShowError) {
149         LogError(_rethrown_exception.GetMessage());
150         notification_free(noti_h);
151         noti_h = NULL;
152         return false;
153     } Catch(DPL::Exception) {
154         LogError(_rethrown_exception.GetMessage());
155         return false;
156     }
157 }
158
159 bool notificationHide(WebNotificationDataPtr notiData)
160 {
161     LogInfo("notificationHide called");
162
163     Assert(notiData);
164     return true;
165 }
166
167 bool isExternalUri(const std::string &pattern, const std::string &uri)
168 {
169     LogInfo("isExternalUri called");
170
171     pcrecpp::RE_Options pcreOpt;
172     pcreOpt.set_caseless(true);
173     pcrecpp::RE re(pattern, pcreOpt);
174
175     return re.FullMatch(uri);
176 }
177
178 bool downloadImage(WebNotificationDataPtr notiData)
179 {
180     LogInfo("downloadImage called");
181
182     Assert(notiData);
183     // download path is
184     // /opt/apps/tizen_id/data + '/' + filename
185     std::string downloadPath =
186     DPL::ToUTF8String(
187             notiData->m_widgetModel->PersistentStoragePath.Get()) +  "/";
188     LogDebug("downloadPath " << downloadPath);
189
190     // Make valid filename
191     // If there is already exist filename, rename to "filename_%d"
192     std::string fileName =
193         notiData->m_iconURL.substr(notiData->m_iconURL.rfind('/') + 1);
194     LogDebug("fileName from URL: " << fileName);
195     std::string rename = fileName;
196     unsigned int renameSuffixNb = 0;
197     while (0 == access((downloadPath + rename).c_str(), F_OK)) {
198         std::ostringstream suffixOstr;
199         suffixOstr.str("");
200         suffixOstr << fileName << "_" << renameSuffixNb++;
201
202         rename = fileName;
203         rename.insert(rename.find('.'), suffixOstr.str());
204     }
205
206     downloadPath += rename;
207     LogDebug("Valid file path : " << downloadPath);
208
209     // Download image by curl
210     FILE *fp = NULL;
211     CURL *curl = NULL;
212
213     Try {
214         curl = curl_easy_init();
215         if (NULL == curl) {
216             ThrowMsg(Exception::InitError, "fail to curl_easy_init");
217         }
218
219         fp = fopen(downloadPath.c_str(), "wb");
220         if (fp == NULL) {
221             ThrowMsg(Exception::InitError, "fail to open");
222         }
223
224         curl_easy_setopt(curl, CURLOPT_URL, notiData->m_iconURL.c_str());
225         curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
226         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlWriteData);
227
228         CURLcode err = curl_easy_perform(curl);
229         if (0 != err) {
230             ThrowMsg(Exception::DownloadImageError,
231                      "fail to curl_easy_perform: " << err);
232         }
233         fclose(fp);
234         curl_easy_cleanup(curl);
235     } Catch (DPL::Exception) {
236         LogError(_rethrown_exception.GetMessage());
237         fclose(fp);
238         curl_easy_cleanup(curl);
239         return false;
240     }
241
242     LogDebug("Download success.. downloadedImgPath: " << downloadPath);
243     notiData->m_iconURL = downloadPath;
244     return true;
245 }
246
247 size_t curlWriteData(void *ptr, size_t size, size_t nmemb, FILE *stream)
248 {
249     size_t written = fwrite(ptr, size, nmemb, stream);
250     return written;
251 }
252
253 } // namespace WebNotification
254 } //namespace ViewModule