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 cancelWebNotification(WebNotificationDataPtr /*notiData*/)
68 LogInfo("cancelWebNotification called");
72 bool notificationShow(WebNotificationDataPtr notiData)
74 LogDebug("notificationShow called");
76 notification_h noti_h = NULL;
77 notification_error_e error = NOTIFICATION_ERROR_NONE;
80 // create notification
81 noti_h = notification_new(
82 NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_NONE, notiData->m_id);
84 LogError("Fail to notification_new");
88 // set notification title
89 error = notification_set_text(
91 NOTIFICATION_TEXT_TYPE_TITLE,
92 notiData->m_title.c_str(),
94 NOTIFICATION_VARIABLE_TYPE_NONE);
95 if (error != NOTIFICATION_ERROR_NONE) {
96 ThrowMsg(Exception::NotificationShowError, "Fail to set title");
99 // set notification content
100 error = notification_set_text(
102 NOTIFICATION_TEXT_TYPE_CONTENT,
103 notiData->m_body.c_str(),
105 NOTIFICATION_VARIABLE_TYPE_NONE);
106 if (error != NOTIFICATION_ERROR_NONE) {
107 ThrowMsg(Exception::NotificationShowError,
108 "Fail to set content:" << error);
111 //check uri is "http", https" or not
112 bool validIconURL = true;
113 LogInfo("url path is " << notiData->m_iconURL);
114 if (isExternalUri(PATTERN_CHECK_EXTERNAL, notiData->m_iconURL)) {
115 //download image from url
116 validIconURL = downloadImage(notiData);
120 // If fail to download external image, skip to set image.
121 // In this case, set to default package image.
122 if (true == validIconURL) {
123 error = notification_set_image(
125 NOTIFICATION_IMAGE_TYPE_ICON,
126 notiData->m_iconURL.c_str());
127 if (error != NOTIFICATION_ERROR_NONE) {
128 ThrowMsg(Exception::NotificationShowError,
129 "Fail to free notification: " << error);
133 // insert notification
134 int priv_id = NOTIFICATION_PRIV_ID_NONE;
135 error = notification_insert(noti_h, &priv_id);
136 if (error != NOTIFICATION_ERROR_NONE) {
137 ThrowMsg(Exception::NotificationShowError,
138 "Fail to insert notification: " << error);
141 LogInfo("Notification is inserted!");
142 LogInfo("noti id =[" << notiData->m_id << "] " <<
143 "priv_id =[" << priv_id << "]");
146 error = notification_free(noti_h);
147 if (error != NOTIFICATION_ERROR_NONE) {
148 ThrowMsg(Exception::NotificationShowError,
149 "Fail to free notification: " << error);
154 } Catch(Exception::NotificationShowError) {
155 LogError(_rethrown_exception.GetMessage());
156 notification_free(noti_h);
159 } Catch(DPL::Exception) {
160 LogError(_rethrown_exception.GetMessage());
165 bool notificationHide(WebNotificationDataPtr notiData)
167 LogInfo("notificationHide called");
173 bool isExternalUri(const std::string &pattern, const std::string &uri)
175 LogInfo("isExternalUri called");
177 pcrecpp::RE_Options pcreOpt;
178 pcreOpt.set_caseless(true);
179 pcrecpp::RE re(pattern, pcreOpt);
181 return re.FullMatch(uri);
184 bool downloadImage(WebNotificationDataPtr notiData)
186 LogInfo("downloadImage called");
190 // /opt/apps/pkgname/data + '/' + filename
191 std::string downloadPath =
193 notiData->m_widgetModel->PersistentStoragePath.Get()) + "/";
194 LogDebug("downloadPath " << downloadPath);
196 // Make valid filename
197 // If there is already exist filename, rename to "filename_%d"
198 std::string fileName =
199 notiData->m_iconURL.substr(notiData->m_iconURL.rfind('/') + 1);
200 LogDebug("fileName from URL: " << fileName);
201 std::string rename = fileName;
202 unsigned int renameSuffixNb = 0;
203 while (0 == access((downloadPath + rename).c_str(), F_OK)) {
204 std::ostringstream suffixOstr;
206 suffixOstr << fileName << "_" << renameSuffixNb++;
209 rename.insert(rename.find('.'), suffixOstr.str());
212 downloadPath += rename;
213 LogDebug("Valid file path : " << downloadPath);
215 // Download image by curl
220 curl = curl_easy_init();
222 ThrowMsg(Exception::InitError, "fail to curl_easy_init");
225 fp = fopen(downloadPath.c_str(), "wb");
227 ThrowMsg(Exception::InitError, "fail to open");
230 curl_easy_setopt(curl, CURLOPT_URL, notiData->m_iconURL.c_str());
231 curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
232 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlWriteData);
234 CURLcode err = curl_easy_perform(curl);
236 ThrowMsg(Exception::DownloadImageError,
237 "fail to curl_easy_perform: " << err);
240 curl_easy_cleanup(curl);
241 } Catch (DPL::Exception) {
242 LogError(_rethrown_exception.GetMessage());
244 curl_easy_cleanup(curl);
248 LogDebug("Download success.. downloadedImgPath: " << downloadPath);
249 notiData->m_iconURL = downloadPath;
253 size_t curlWriteData(void *ptr, size_t size, size_t nmemb, FILE *stream)
255 size_t written = fwrite(ptr, size, nmemb, stream);
259 } // namespace WebNotification
260 } //namespace ViewModule