2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include <PlatformException.h>
29 #include <appsvc/appsvc.h>
31 #include "NotificationManager.h"
34 namespace Notification {
36 extern "C" int service_to_bundle(service_h service, bundle **data);
38 static bool notification_package_equal(notification_h handle)
41 char* handle_package = NULL;
42 char cmdline[512] = {0,};
45 if(notification_get_pkgname(handle, &handle_package))
50 LoggerD("handle package=" << handle_package);
52 if (app_get_package(&package))
59 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
61 fd = open(buf, O_RDONLY);
66 ret = read(fd, cmdline, sizeof(cmdline) - 1);
75 LoggerD("package=" << package);
77 if (strlen(cmdline) == strlen(handle_package))
79 if (!strncmp(cmdline, handle_package, strlen(cmdline)))
87 LoggerD("package=" << package);
89 if (strlen(package) == strlen(handle_package))
91 if (!strncmp(package, handle_package, strlen(package)))
101 NotificationManager::NotificationManager()
105 NotificationManager::~NotificationManager()
109 int NotificationManager::post(StatusNotification *notification)
115 LoggerI(" notification id = " << notification->getID());
116 notification_h handle = (notification_h)notification->getNotificationHandle();
118 LoggerI("notification hanel :" << handle);
125 bundle *service_data = NULL;
126 service_h service = notification->getService();
128 LoggerI("Service :" << service << " Flag :" << notification->getLaunchFlag());
130 if (service && notification->getLaunchFlag())
132 if (service_to_bundle(service, &service_data)!= SERVICE_ERROR_NONE)
134 throw UnknownException("Can't create bundle");
138 LoggerI("Notification Launch Flag True");
139 notification_set_property(handle, 0);
140 if (notification_set_execute_option(handle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, service_data) != SERVICE_ERROR_NONE)
142 LoggerD("can't set notification option");
148 LoggerI("Notification Launch Flag False");
149 notification_set_property(handle, NOTIFICATION_PROP_DISABLE_APP_LAUNCH);
153 int type = (int)notification->getNotiType();
154 notification_ly_type_e noti_layout = NOTIFICATION_LY_NONE;
158 case NOTI_TYPE_SIMPLE:
160 if(notification->getNumber()>0)
161 noti_layout = NOTIFICATION_LY_NOTI_EVENT_MULTIPLE;
163 noti_layout = NOTIFICATION_LY_NOTI_EVENT_SINGLE;
166 case NOTI_TYPE_THUMBNAIL:
168 noti_layout = NOTIFICATION_LY_NOTI_THUMBNAIL;
171 case NOTI_TYPE_ONGOING:
173 noti_layout = NOTIFICATION_LY_ONGOING_EVENT;
176 case NOTI_TYPE_PROGRESS:
178 noti_layout = NOTIFICATION_LY_ONGOING_PROGRESS;
183 LoggerD(" Layout type = " << noti_layout);
184 ret = notification_set_layout(handle, noti_layout);
185 LoggerD("set Layout result= " << ret);
189 ret = notification_insert(handle, &privID); //if noti already exist, it is update.
190 if (NOTIFICATION_ERROR_NONE == ret)
193 LoggerD("private ID : " << privID);
194 notification->setUpdatedFlag(false);
198 throw UnknownException("Notification Object post fail");
201 if ( type == NOTI_TYPE_PROGRESS)
206 if (notification_get_size(handle, &value) != NOTIFICATION_ERROR_NONE)
208 throw UnknownException("get notification size error");
210 LoggerI("get Size : " << value);
211 notification_update_size(handle, NOTIFICATION_PRIV_ID_NONE, value);
213 if (notification_get_progress(handle, &value) != NOTIFICATION_ERROR_NONE)
215 throw UnknownException("get notification percentage error");
217 LoggerI("get Percentage : " << value);
218 notification_update_progress(handle, NOTIFICATION_PRIV_ID_NONE, value);
224 LoggerD("it has not notification handle.");
225 throw UnknownException("It is Empty Notification.");
230 throw UnknownException("Notification Handle is NULL.");
235 void NotificationManager::update(StatusNotification *notification)
239 LoggerD(" notification id = " << notification->getID());
240 notification_h handle = (notification_h)notification->getNotificationHandle();
242 LoggerD("handle = " << handle);
246 LoggerD(" noti type = " << notification->getNotiType());
247 if ( notification->getNotiType() == NOTI_TYPE_PROGRESS)
250 LoggerD(" progress type = " << notification->getProgressType());
251 LoggerD(" noti id = " << notification->getID());
252 LoggerD(" noti progress value = " << notification->getProgressValue());
254 if ( NOTI_PROGRESS_TYPE_PERCENTAGE == notification->getProgressType() )
256 LoggerD( " Percentage ");
257 ret = notification_update_progress(handle, NOTIFICATION_PRIV_ID_NONE, notification->getProgressValue());
259 else if ( NOTI_PROGRESS_TYPE_SIZE == notification->getProgressType())
262 ret = notification_update_size(handle, NOTIFICATION_PRIV_ID_NONE, notification->getProgressValue());
265 LoggerD(" notification_update_progress = " << ret);
267 if (NOTIFICATION_ERROR_NONE != ret)
269 if ( NOTIFICATION_ERROR_NOT_EXIST_ID == ret)
271 throw NotFoundException("maybe, notification is not inserted yet.");
275 throw UnknownException("Notification Object update grogress fail, Error Code : ");
281 LoggerD(" updated Flag = " << notification->getUpdatedFlag());
283 if ( notification->getUpdatedFlag())
286 int type = (int)notification->getNotiType();
287 notification_ly_type_e noti_layout = NOTIFICATION_LY_NONE;
291 case NOTI_TYPE_SIMPLE:
293 if(notification->getNumber()>0)
294 noti_layout = NOTIFICATION_LY_NOTI_EVENT_MULTIPLE;
296 noti_layout = NOTIFICATION_LY_NOTI_EVENT_SINGLE;
299 case NOTI_TYPE_THUMBNAIL:
301 noti_layout = NOTIFICATION_LY_NOTI_THUMBNAIL;
304 case NOTI_TYPE_ONGOING:
306 noti_layout = NOTIFICATION_LY_ONGOING_EVENT;
309 case NOTI_TYPE_PROGRESS:
311 noti_layout = NOTIFICATION_LY_ONGOING_PROGRESS;
316 LoggerD(" Layout type = " << noti_layout);
317 int ret = notification_set_layout(handle, noti_layout);
318 LoggerD("set Layout result= " << ret);
321 bundle *service_data = NULL;
322 service_h service = notification->getService();
326 if (service_to_bundle(service, &service_data)!= SERVICE_ERROR_NONE)
328 throw UnknownException("Can't create bundle");
332 if (notification->getLaunchFlag())
334 LoggerI("Notification Launch Flag True");
335 notification_set_property(handle, 0);
336 if (notification_set_execute_option(handle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, service_data)!= SERVICE_ERROR_NONE)
338 LoggerI("Can't set Service option");
343 LoggerI("Notification Launch Flag False");
344 notification_set_property(handle, NOTIFICATION_PROP_DISABLE_APP_LAUNCH);
350 notification->setLaunchFlag(false);
352 LoggerD(" get Title = " << notification->getTitle());
353 ret = notification_update(handle);
354 LoggerD(" notification_update = " << ret);
355 notification->setUpdatedFlag(false); //init
357 if (ret != NOTIFICATION_ERROR_NONE)
359 throw UnknownException("Notification Object update fail, Error Code : ");
362 LoggerD(" get Title = " << notification->getTitle());
367 LoggerD("it has not notification handle.");
368 throw UnknownException("It is Empty Notification.");
373 LoggerD(" INotification is NULL");
374 throw UnknownException("Notificaton Error.");
378 void NotificationManager::remove(std::string id)
382 bool existFalg = false;
384 LoggerD("id : " << id);
385 std::istringstream stream(id);
388 throw InvalidValuesException("Invalid notification ID, it don't match id format");
393 notification_h noti = NULL;
394 notification_list_h noti_list = NULL;
395 notification_list_h noti_list_iter = NULL;
397 if (notification_get_grouping_list( NOTIFICATION_TYPE_NONE , -1, ¬i_list))
399 LoggerD(" get notification list failed...");
400 throw UnknownException("Can't get noti list");
403 noti_list_iter = notification_list_get_head(noti_list);
405 while ( noti_list_iter != NULL)
407 noti = notification_list_get_data(noti_list_iter);
408 if (noti != NULL && notification_package_equal(noti))
411 notification_get_id(noti, NULL, ¬i_priv);
412 LoggerD(" notification id = " << noti_priv);
413 if (noti_priv == privID)
416 noti_list_iter = notification_list_get_next(noti_list_iter);
420 notification_free_list(noti_list);
422 LoggerD(" notification ID : "<< privID);
425 throw NotFoundException("not exist id");
427 ret = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_NONE, privID);
428 LoggerD("ret = " << ret);
429 if (ret != NOTIFICATION_ERROR_NONE)
431 if (ret == NOTIFICATION_ERROR_NOT_EXIST_ID)
433 throw NotFoundException("not exist id");
437 throw InvalidValuesException ("Invalid Data Error");
442 void NotificationManager::removeAll()
445 if (notification_delete_all_by_type(NULL,
446 NOTIFICATION_TYPE_NONE) != NOTIFICATION_ERROR_NONE)
448 throw UnknownException(" notification delete failed...");
452 StatusNotification* NotificationManager::get(std::string id)
456 LoggerD("id : " << id);
457 std::istringstream stream(id);
460 throw InvalidValuesException("Invalid notification ID, it don't match id format");
464 LoggerI("priv ID : " << privID);
466 notification_h notification = notification_load( NULL, privID); //load notification.
467 LoggerI(" notification " << notification);
469 if (notification != NULL && notification_package_equal(notification))
471 StatusNotification* noti = new StatusNotification(notification);
476 throw NotFoundException("It is not notification ID or removed notification");
481 std::vector<StatusNotification*> NotificationManager::getAll()
484 std::vector<StatusNotification*> data;
486 notification_h noti = NULL;
487 notification_list_h noti_list = NULL;
488 notification_list_h noti_list_iter = NULL;
490 if (notification_get_grouping_list( NOTIFICATION_TYPE_NONE , -1, ¬i_list))
492 LoggerD(" get notification list failed...");
493 throw UnknownException("Can't get noti list");
496 noti_list_iter = notification_list_get_head(noti_list);
498 while ( noti_list_iter != NULL)
500 noti = notification_list_get_data(noti_list_iter);
501 if (noti != NULL && notification_package_equal(noti))
504 notification_get_id(noti, NULL, ¬i_priv);
505 LoggerD(" notification id = " << noti_priv);
507 StatusNotification* notification = new StatusNotification(noti_priv);
509 notification_get_id((notification_h)notification->getNotificationHandle(), NULL, ¬i_priv);
510 LoggerD("loaded notification id = " << noti_priv);
512 data.push_back(notification);
515 noti_list_iter = notification_list_get_next(noti_list_iter);
519 notification_free_list(noti_list);
522 while ( noti_list != NULL)
524 noti = notification_list_get_data(noti_list);
525 if (noti != NULL && notification_package_equal(noti))
528 notification_get_id(noti, NULL, ¬i_priv);
529 LoggerD(" notification id = " << noti_priv);
531 StatusNotification* notification = new StatusNotification(noti_priv);
533 notification_get_id((notification_h)notification->getNotificationHandle(), NULL, ¬i_priv);
534 LoggerD("loaded notification id = " << noti_priv);
536 data.push_back(notification);
539 noti_list = notification_list_get_next(noti_list);
543 notification_free_list(noti_list);