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.
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
22 #include "view_logic_web_notification_support.h"
24 #include <dpl/log/log.h>
25 #include <dpl/string.h>
27 #include <notification.h>
30 #include <curl/curl.h>
31 #include <widget_model.h>
34 const char* PATTERN_CHECK_EXTERNAL = "^http(s)?://\\w+.*$";
37 namespace ViewModule {
38 namespace WebNotification {
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);
46 WebNotificationData::WebNotificationData(WidgetModel* widgetModel,
48 m_widgetModel(widgetModel),
51 Assert(m_widgetModel);
54 WebNotificationData::~WebNotificationData()
58 bool showWebNotification(WebNotificationDataPtr notiData)
60 LogInfo("showWebNotification called");
62 /* TODO : register notification to database */
63 return notificationShow(notiData);
66 bool notificationShow(WebNotificationDataPtr notiData)
68 LogDebug("notificationShow called");
70 notification_h noti_h = NULL;
71 notification_error_e error = NOTIFICATION_ERROR_NONE;
74 // create notification
75 noti_h = notification_new(
76 NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_NONE, notiData->m_id);
78 LogError("Fail to notification_new");
82 // set notification title
83 error = notification_set_text(
85 NOTIFICATION_TEXT_TYPE_TITLE,
86 notiData->m_title.c_str(),
88 NOTIFICATION_VARIABLE_TYPE_NONE);
89 if (error != NOTIFICATION_ERROR_NONE) {
90 ThrowMsg(Exception::NotificationShowError, "Fail to set title");
93 // set notification content
94 error = notification_set_text(
96 NOTIFICATION_TEXT_TYPE_CONTENT,
97 notiData->m_body.c_str(),
99 NOTIFICATION_VARIABLE_TYPE_NONE);
100 if (error != NOTIFICATION_ERROR_NONE) {
101 ThrowMsg(Exception::NotificationShowError,
102 "Fail to set content:" << error);
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);
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(
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);
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);
135 LogInfo("Notification is inserted!");
136 LogInfo("noti id =[" << notiData->m_id << "] " <<
137 "priv_id =[" << priv_id << "]");
140 error = notification_free(noti_h);
141 if (error != NOTIFICATION_ERROR_NONE) {
142 ThrowMsg(Exception::NotificationShowError,
143 "Fail to free notification: " << error);
148 } Catch(Exception::NotificationShowError) {
149 LogError(_rethrown_exception.GetMessage());
150 notification_free(noti_h);
153 } Catch(DPL::Exception) {
154 LogError(_rethrown_exception.GetMessage());
159 bool notificationHide(WebNotificationDataPtr notiData)
161 LogInfo("notificationHide called");
167 bool isExternalUri(const std::string &pattern, const std::string &uri)
169 LogInfo("isExternalUri called");
171 pcrecpp::RE_Options pcreOpt;
172 pcreOpt.set_caseless(true);
173 pcrecpp::RE re(pattern, pcreOpt);
175 return re.FullMatch(uri);
178 bool downloadImage(WebNotificationDataPtr notiData)
180 LogInfo("downloadImage called");
184 // /opt/apps/tizen_id/data + '/' + filename
185 std::string downloadPath =
187 notiData->m_widgetModel->PersistentStoragePath.Get()) + "/";
188 LogDebug("downloadPath " << downloadPath);
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;
200 suffixOstr << fileName << "_" << renameSuffixNb++;
203 rename.insert(rename.find('.'), suffixOstr.str());
206 downloadPath += rename;
207 LogDebug("Valid file path : " << downloadPath);
209 // Download image by curl
214 curl = curl_easy_init();
216 ThrowMsg(Exception::InitError, "fail to curl_easy_init");
219 fp = fopen(downloadPath.c_str(), "wb");
221 ThrowMsg(Exception::InitError, "fail to open");
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);
228 CURLcode err = curl_easy_perform(curl);
230 ThrowMsg(Exception::DownloadImageError,
231 "fail to curl_easy_perform: " << err);
234 curl_easy_cleanup(curl);
235 } Catch (DPL::Exception) {
236 LogError(_rethrown_exception.GetMessage());
238 curl_easy_cleanup(curl);
242 LogDebug("Download success.. downloadedImgPath: " << downloadPath);
243 notiData->m_iconURL = downloadPath;
247 size_t curlWriteData(void *ptr, size_t size, size_t nmemb, FILE *stream)
249 size_t written = fwrite(ptr, size, nmemb, stream);
253 } // namespace WebNotification
254 } //namespace ViewModule