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();
130 if (service_to_bundle(service, &service_data)!= SERVICE_ERROR_NONE)
132 throw UnknownException("Can't create bundle");
136 if (notification->getLaunchFlag())
138 LoggerI("Notification Launch Flag True");
139 notification_set_property(handle, 0);
140 notification_set_execute_option(handle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, service_data);
144 LoggerI("Notification Launch Flag False");
145 notification_set_property(handle, NOTIFICATION_PROP_DISABLE_APP_LAUNCH);
148 //bundle_free(service_data);
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 notification_set_execute_option(handle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, service_data);
340 LoggerI("Notification Launch Flag False");
341 notification_set_property(handle, NOTIFICATION_PROP_DISABLE_APP_LAUNCH);
347 notification->setLaunchFlag(false);
349 LoggerD(" get Title = " << notification->getTitle());
350 ret = notification_update(handle);
351 LoggerD(" notification_update = " << ret);
352 notification->setUpdatedFlag(false); //init
354 if (ret != NOTIFICATION_ERROR_NONE)
356 throw UnknownException("Notification Object update fail, Error Code : ");
359 LoggerD(" get Title = " << notification->getTitle());
364 LoggerD("it has not notification handle.");
365 throw UnknownException("It is Empty Notification.");
370 LoggerD(" INotification is NULL");
371 throw UnknownException("Notificaton Error.");
375 void NotificationManager::remove(std::string id)
379 bool existFalg = false;
381 LoggerD("id : " << id);
382 std::istringstream stream(id);
385 throw InvalidValuesException("Invalid notification ID, it don't match id format");
390 notification_h noti = NULL;
391 notification_list_h noti_list = NULL;
392 notification_list_h noti_list_iter = NULL;
394 if (notification_get_grouping_list( NOTIFICATION_TYPE_NONE , -1, ¬i_list))
396 LoggerD(" get notification list failed...");
397 throw UnknownException("Can't get noti list");
400 noti_list_iter = notification_list_get_head(noti_list);
402 while ( noti_list_iter != NULL)
404 noti = notification_list_get_data(noti_list_iter);
405 if (noti != NULL /* && notification_package_equal(noti)*/)
408 notification_get_id(noti, NULL, ¬i_priv);
409 LoggerD(" notification id = " << noti_priv);
410 if (noti_priv == privID)
413 noti_list_iter = notification_list_get_next(noti_list_iter);
417 notification_free_list(noti_list);
419 LoggerD(" notification ID : "<< privID);
422 throw NotFoundException("not exist id");
424 ret = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_NONE, privID);
425 LoggerD("ret = " << ret);
426 if (ret != NOTIFICATION_ERROR_NONE)
428 if (ret == NOTIFICATION_ERROR_NOT_EXIST_ID)
430 throw NotFoundException("not exist id");
434 throw InvalidValuesException ("Invalid Data Error");
439 void NotificationManager::removeAll()
442 if (notification_delete_all_by_type(NULL,
443 NOTIFICATION_TYPE_NONE) != NOTIFICATION_ERROR_NONE)
445 throw UnknownException(" notification delete failed...");
449 StatusNotification* NotificationManager::get(std::string id)
453 LoggerD("id : " << id);
454 std::istringstream stream(id);
457 throw InvalidValuesException("Invalid notification ID, it don't match id format");
461 StatusNotification* notification = new StatusNotification(privID);
464 std::vector<StatusNotification*> NotificationManager::getAll()
467 std::vector<StatusNotification*> data;
469 notification_h noti = NULL;
470 notification_list_h noti_list = NULL;
471 notification_list_h noti_list_iter = NULL;
473 if (notification_get_grouping_list( NOTIFICATION_TYPE_NONE , -1, ¬i_list))
475 LoggerD(" get notification list failed...");
476 throw UnknownException("Can't get noti list");
479 noti_list_iter = notification_list_get_head(noti_list);
481 while ( noti_list_iter != NULL)
483 noti = notification_list_get_data(noti_list_iter);
484 if (noti != NULL && notification_package_equal(noti))
487 notification_get_id(noti, NULL, ¬i_priv);
488 LoggerD(" notification id = " << noti_priv);
490 StatusNotification* notification = new StatusNotification(noti_priv);
492 notification_get_id((notification_h)notification->getNotificationHandle(), NULL, ¬i_priv);
493 LoggerD("loaded notification id = " << noti_priv);
495 data.push_back(notification);
499 noti_list_iter = notification_list_get_next(noti_list_iter);
503 notification_free_list(noti_list);
506 while ( noti_list != NULL)
508 noti = notification_list_get_data(noti_list);
509 if (noti != NULL && notification_package_equal(noti))
512 notification_get_id(noti, NULL, ¬i_priv);
513 LoggerD(" notification id = " << noti_priv);
515 StatusNotification* notification = new StatusNotification(noti_priv);
517 notification_get_id((notification_h)notification->getNotificationHandle(), NULL, ¬i_priv);
518 LoggerD("loaded notification id = " << noti_priv);
520 data.push_back(notification);
523 noti_list = notification_list_get_next(noti_list);
527 notification_free_list(noti_list);