update exceptions and log messages
[platform/framework/native/shell.git] / src / core / FShell_NotificationManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file                FShell_NotificationManagerImpl.cpp
20  * @brief               This is the placeholder for _NotificationManagerImpl class.
21  */
22
23 #include <unique_ptr.h>
24 #include <appsvc/appsvc.h>
25 #include <bundle.h>
26 #include <notification/notification.h>
27 #include <appfw/app.h>
28 #include <appfw/app_manager.h>
29 #include <appfw/app_ui_notification.h>
30 #include <badge.h>
31
32 #include <FBaseSysLog.h>
33 #include <FApp.h>
34 #include <FShellNotificationManager.h>
35 #include <FShellNotificationRequest.h>
36 #include <FShellIBadgeEventListener.h>
37
38 #include "FBase_StringConverter.h"
39 #include "FApp_AppInfo.h"
40 #include "FApp_Aul.h"
41 #include "FIoFile.h"
42 #include "FAppPkg_PackageManagerImpl.h"
43 #include "FApp_AppArg.h"
44 #include "FAppPkgPackageAppInfo.h"
45 #include "FAppPkg_PackageAppInfoImpl.h"
46 #include "FShell_NotificationManagerImpl.h"
47 #include "FShell_NotificationRequestImpl.h"
48
49 using namespace Tizen::Base;
50 using namespace Tizen::Base::Collection;
51 using namespace Tizen::App;
52 using namespace Tizen::App::Package;
53 using namespace Tizen::Io;
54 using namespace Tizen::Shell;
55
56 extern "C" int service_create_request(bundle *data, service_h *service);
57 extern "C" int service_to_bundle(service_h service, bundle** data);
58
59 namespace
60 {
61
62 result
63 ConvertNotificationResult(int error)
64 {
65         switch (error)
66         {
67         case NOTIFICATION_ERROR_NONE:
68                 return E_SUCCESS;
69         case NOTIFICATION_ERROR_INVALID_DATA:
70                 return E_INVALID_ARG;
71         case NOTIFICATION_ERROR_NO_MEMORY:
72                 return E_OUT_OF_MEMORY;
73         case NOTIFICATION_ERROR_FROM_DB:
74                 // fall through
75         case NOTIFICATION_ERROR_ALREADY_EXIST_ID:
76                 // fall through
77         case NOTIFICATION_ERROR_NOT_EXIST_ID:
78                 return E_OPERATION_FAILED;
79         default:
80                 return E_OPERATION_FAILED;
81         }
82 }
83
84 result
85 ConvertAppManagerResult(int error)
86 {
87         switch (error)
88         {
89         case APP_MANAGER_ERROR_NONE:
90                 return E_SUCCESS;
91         case APP_MANAGER_ERROR_INVALID_PARAMETER:
92                 // fall through
93         case APP_MANAGER_ERROR_INVALID_PACKAGE:
94                 return E_INVALID_ARG;
95         case NOTIFICATION_ERROR_NO_MEMORY:
96                 return E_OUT_OF_MEMORY;
97         case APP_MANAGER_ERROR_DB_FAILED:
98                 return E_OPERATION_FAILED;
99         default:
100                 return E_OPERATION_FAILED;
101         }
102 }
103
104 } // namespace
105
106
107 namespace Tizen { namespace Shell
108 {
109
110 ///////////////////////////////////////////////////////////////////
111 // _BadgeManagerImpl
112 ///////////////////////////////////////////////////////////////////
113 void
114 BadgeChangedCallback(unsigned int action, const char *pkgname, unsigned int count, void *data)
115 {
116         Tizen::Base::Collection::LinkedListT<Tizen::Shell::IBadgeEventListener*>* pBadgeEventListenerList = static_cast<Tizen::Base::Collection::LinkedListT<Tizen::Shell::IBadgeEventListener*>*>(data);
117         SysAssert(pBadgeEventListenerList);
118
119         std::unique_ptr<IEnumeratorT<Tizen::Shell::IBadgeEventListener* > > pEnum(pBadgeEventListenerList->GetEnumeratorN());
120         SysTryReturnVoidResult(NID_SHELL, pEnum.get(), E_OUT_OF_MEMORY, "Failed to GetEnumeratorN()!");
121
122         IBadgeEventListener* pListener;
123         while (pEnum->MoveNext() == E_SUCCESS)
124         {
125                 pListener = null;
126                 pEnum->GetCurrent(pListener);
127                 if (!pListener)
128                 {
129                         SysLog(NID_SHELL, "pListener is null!");
130                         continue;
131                 }
132
133                 pListener->OnBadgeUpdated(pkgname, count);
134         }
135 }
136
137 _BadgeManagerImpl*
138 _BadgeManagerImpl::GetInstance(void)
139 {
140         static _BadgeManagerImpl* pTheInstance = null;
141         if (pTheInstance == null)
142         {
143                 pTheInstance = new (std::nothrow) _BadgeManagerImpl;
144                 SysTryReturn(NID_SHELL, pTheInstance != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
145
146                 pTheInstance->Construct();
147         }
148         return pTheInstance;
149 }
150
151 result
152 _BadgeManagerImpl::Construct(void)
153 {
154         return E_SUCCESS;
155 }
156
157 result
158 _BadgeManagerImpl::AddPrimaryBadgeEventListener(IBadgeEventListener& primaryListener)
159 {
160         if( __primaryBadgeEventListenerList.GetCount() == 0)
161         {
162                 int ret = badge_register_changed_cb(BadgeChangedCallback, &__primaryBadgeEventListenerList);
163                 SysTryReturnResult(NID_SHELL, ret == BADGE_ERROR_NONE, E_SYSTEM, "Failed to badge_register_changed_cb, (%d)", ret);
164         }
165
166         return __primaryBadgeEventListenerList.Add(&primaryListener);
167 }
168
169 result
170 _BadgeManagerImpl::RemovePrimaryBadgeEventListener(IBadgeEventListener& primaryListener)
171 {
172         __primaryBadgeEventListenerList.Remove(&primaryListener);
173         if( __primaryBadgeEventListenerList.GetCount() == 0)
174         {
175                 int ret = badge_unregister_changed_cb(BadgeChangedCallback);
176                 SysTryReturnResult(NID_SHELL, ret == BADGE_ERROR_NONE, E_SYSTEM, "Failed to badge_unregister_changed_cb, (%d)", ret);
177         }
178         return E_SUCCESS;
179 }
180
181
182 ///////////////////////////////////////////////////////////////////
183 // _NotificationManagerImpl
184 ///////////////////////////////////////////////////////////////////
185 _NotificationManagerImpl::_NotificationManagerImpl(void)
186         : __notifyPrivateId(-1)
187         , __notifyPrivateIdForOngoing(-1)
188 {
189 }
190
191 _NotificationManagerImpl::~_NotificationManagerImpl(void)
192 {
193         bool isListeningBadgeEvent = (__badgeEventListenerList.GetCount() > 0)? true : false;
194         if(isListeningBadgeEvent == true)
195         {
196                 _BadgeManagerImpl::GetInstance()->RemovePrimaryBadgeEventListener(*this);
197         }
198 }
199
200 result
201 _NotificationManagerImpl::Construct(void)
202 {
203         return E_SUCCESS;
204 }
205
206 const _NotificationManagerImpl*
207 _NotificationManagerImpl::GetInstance(const NotificationManager& notiMgr)
208 {
209         return notiMgr.__pNotificationManagerImpl;
210 }
211
212 _NotificationManagerImpl*
213 _NotificationManagerImpl::GetInstance(NotificationManager& notiMgr)
214 {
215         return notiMgr.__pNotificationManagerImpl;
216 }
217
218 int
219 _NotificationManagerImpl::GetBadgeNumber(void) const
220 {
221         ClearLastResult();
222
223         Tizen::App::App* pApp = Tizen::App::App::GetInstance();
224         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(pApp->GetAppId()));
225
226         int count = GetBadgeCount(pAppId.get());
227         SysTryReturn(NID_SHELL, count != -1, count, E_OPERATION_FAILED, "[E_OPERATION_FAILED] The operation has failed. Badge may not exist.");
228
229         return count;
230 }
231
232 result
233 _NotificationManagerImpl::NotifyMessageImpl(const NotificationRequest& notiMessage, bool isOngoing)
234 {
235         return NotifyMessage(NOTIFY_TYPE_SIMPLE, isOngoing, notiMessage);
236 }
237
238 result
239 _NotificationManagerImpl::NotifyMessageImpl(const AppId& appId, const NotificationRequest& notiMessage, bool isOngoing)
240 {
241         return NotifyMessage(NOTIFY_TYPE_APP_ID, isOngoing, notiMessage, &appId);
242 }
243
244
245 result
246 _NotificationManagerImpl::Notify(int badgeNumber)
247 {
248         SysTryReturnResult(NID_SHELL, badgeNumber >= 0 && badgeNumber <= MAX_NOTIFICATION_BADGE_NUMBER, E_INVALID_ARG,
249                                                 "badgeNumber is less than 0 or greater than MAX_NOTIFICATION_BADGE_NUMBER.");
250
251         Tizen::App::App* pApp = Tizen::App::App::GetInstance();
252         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(pApp->GetAppId()));
253
254         result r = E_SUCCESS;
255
256         if (badgeNumber == 0)
257         {
258                 r = RemoveBadge(pAppId.get());
259                 SysTryReturnResult(NID_SHELL, !IsFailed(r), r, "Failed to remove the badge.");
260         }
261         else
262         if (badgeNumber > 0)
263         {
264                 r = SetBadgeCount(pAppId.get(), badgeNumber);
265                 SysTryReturnResult(NID_SHELL, !IsFailed(r), r, "Failed to set the badge.");
266         }
267
268         return r;
269 }
270
271 result
272 _NotificationManagerImpl::Notify(const String& messageText)
273 {
274         SysTryReturnResult(NID_SHELL, messageText.GetLength() > 0 && messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
275                                                 "messageText is less than 1 or greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
276
277         NotificationRequest request;
278         request.SetAlertText(messageText);
279         request.SetAppMessage(L"");
280
281         return NotifyMessage(NOTIFY_TYPE_SIMPLE, false, request);
282 }
283
284 result
285 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber)
286 {
287         SysTryReturnResult(NID_SHELL, badgeNumber >= 0 && badgeNumber <= MAX_NOTIFICATION_BADGE_NUMBER, E_INVALID_ARG,
288                                                 "badgeNumber is less than 0 or greater than MAX_NOTIFICATION_BADGE_NUMBER.");
289         SysTryReturnResult(NID_SHELL, messageText.GetLength() > 0 && messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
290                                                 "messageText is less than 1 or greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
291
292         NotificationRequest request;
293         request.SetAlertText(messageText);
294         request.SetBadgeNumber(badgeNumber);
295         request.SetAppMessage(L"");
296
297         return NotifyMessage(NOTIFY_TYPE_SIMPLE, false, request);
298 }
299
300 result
301 _NotificationManagerImpl::Notify(const String& messageText, int badgeNumber, const String& launchArguments)
302 {
303         SysTryReturnResult(NID_SHELL, badgeNumber >= 0 && badgeNumber <= MAX_NOTIFICATION_BADGE_NUMBER, E_INVALID_ARG,
304                                                 "badgeNumber is less than 0 or greater than MAX_NOTIFICATION_BADGE_NUMBER.");
305         SysTryReturnResult(NID_SHELL, messageText.GetLength() > 0 && messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
306                                                 "messageText is less than 1 or greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
307         SysTryReturnResult(NID_SHELL, launchArguments.GetLength() > 0 && launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
308                                            "launchArguments is less than 1 or greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
309
310         NotificationRequest request;
311         request.SetAlertText(messageText);
312         request.SetBadgeNumber(badgeNumber);
313         request.SetAppMessage(launchArguments);
314
315         return NotifyMessage(NOTIFY_TYPE_SIMPLE, false, request);
316 }
317
318 int
319 _NotificationManagerImpl::GetBadgeNumber(const AppId& appId) const
320 {
321         ClearLastResult();
322
323         bool b = _Aul::IsInstalled(appId);
324         SysTryReturn(NID_SHELL, b == true, -1, E_APP_NOT_INSTALLED, "[E_APP_NOT_INSTALLED] The application %ls is not installed",
325                                  appId.GetPointer());
326         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
327         int count = GetBadgeCount(pAppId.get());
328
329         SysTryReturn(NID_SHELL, count != -1, count, E_OPERATION_FAILED, "[E_OPERATION_FAILED] The operation has failed. Badge may not exist.");
330
331         return count;
332 }
333
334 result
335 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText)
336 {
337         SysTryReturnResult(NID_SHELL, messageText.GetLength() > 0 && messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
338                                                 "messageText is less than 1 or greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
339
340         NotificationRequest request;
341         request.SetAlertText(messageText);
342
343         return NotifyMessage(NOTIFY_TYPE_SIMPLE, true, request);
344 }
345
346 result
347 _NotificationManagerImpl::NotifyOngoingActivity(const String& messageText, const String& launchArguments)
348 {
349         SysTryReturnResult(NID_SHELL, messageText.GetLength() > 0 && messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
350                                                 "messageText is less than 1 or greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
351         SysTryReturnResult(NID_SHELL, launchArguments.GetLength() > 0, E_INVALID_ARG, "launchArguments is less than 0.");
352         SysTryReturnResult(NID_SHELL, launchArguments.GetLength() <= MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH, E_INVALID_ARG,
353                                            "launchArguments is greater than MAX_NOTIFICATION_LAUNCH_ARGUMENTS_LENGTH.");
354
355         NotificationRequest request;
356         request.SetAlertText(messageText);
357         request.SetAppMessage(launchArguments);
358
359         return NotifyMessage(NOTIFY_TYPE_SIMPLE, true, request);
360 }
361
362 result
363 _NotificationManagerImpl::RemoveOngoingActivityNotificationByAppId(const AppId& appId)
364 {
365         return RemoveNotificationByAppId(appId, true);
366 }
367
368
369 result
370 _NotificationManagerImpl::RemoveNotificationByAppId(const AppId& appId)
371 {
372         return RemoveNotificationByAppId(appId, false);
373 }
374
375 result
376 _NotificationManagerImpl::NotifyTextMessage(const String& messageText) const
377 {
378         SysTryReturnResult(NID_SHELL, messageText.GetLength() > 0 && messageText.GetLength() <= MAX_NOTIFICATION_MESSAGE_LENGTH, E_INVALID_ARG,
379                                                 "messageText is less than 1 or greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
380
381         std::unique_ptr<char[]> pMsg(_StringConverter::CopyToCharArrayN(messageText));
382         int res = notification_status_message_post(pMsg.get());
383
384         result r = E_SUCCESS;
385         switch (res)
386         {
387         case NOTIFICATION_ERROR_NONE:
388                 // success
389                 break;
390         case NOTIFICATION_ERROR_INVALID_DATA:
391                 r = E_INVALID_ARG;
392                 break;
393         case NOTIFICATION_ERROR_IO:
394                 r = E_OPERATION_FAILED;
395                 break;
396         default:
397                 r = E_OPERATION_FAILED;
398                 break;
399         }
400
401         SysLog(NID_SHELL, "[%s] %ls posted.", GetErrorMessage(r), messageText.GetPointer());
402         return r;
403 }
404
405 result
406 _NotificationManagerImpl::NotifyByAppControl(const Tizen::Base::String& operationId, const Tizen::Base::String* pUriData, const Tizen::Base::String* pDataType,
407                                                                                          const Tizen::Base::Collection::IMap* pExtraData, const NotificationRequest& request)
408 {
409         result r = E_SUCCESS;
410
411         r = NotifyMessage(NOTIFY_TYPE_APP_CONTROL, false, request, null, &operationId, pUriData, pDataType, pExtraData);
412         return r;
413 }
414
415 result
416 _NotificationManagerImpl::NotifyOngoingActivityByAppControl(const Tizen::Base::String& operationId, const Tizen::Base::String* pUriData, const Tizen::Base::String* pDataType,
417                                                                                                                         const Tizen::Base::Collection::IMap* pExtraData, const NotificationRequest& request)
418 {
419         result r = E_SUCCESS;
420
421         r = NotifyMessage(NOTIFY_TYPE_APP_CONTROL, true, request, null, &operationId, pUriData, pDataType, pExtraData);
422         return r;
423 }
424
425 //
426 // E_SUCCESS
427 // E_INVALID_ARG
428 // E_APP_NOT_INSTALLED - If AppId is invalid, it can occur.
429 // E_OPERATION_FAILED
430 // E_OUT_OF_MEMORY
431 //
432 result
433 _NotificationManagerImpl::NotifyMessage(_NotifyType notifyType, bool isOngoing, const NotificationRequest& notifyRequest, const AppId* pAppId,
434                                                                                 const Tizen::Base::String* pOperationId, const Tizen::Base::String* pUriData, const Tizen::Base::String* pDataType, const Tizen::Base::Collection::IMap* pExtraData)
435 {
436         result r = E_SUCCESS;
437         std::unique_ptr<char[]> pAppIdChar(null);
438         const _NotificationRequestImpl* pRequestImpl = _NotificationRequestImpl::GetInstance(notifyRequest);
439         SysTryReturnResult(NID_SHELL, pRequestImpl != null, E_INVALID_ARG, "Invalid argument is used.");
440
441         // Set pAppIdChar
442         if ((notifyType == NOTIFY_TYPE_SIMPLE) || (notifyType == NOTIFY_TYPE_APP_CONTROL))
443         {
444                 Tizen::App::App* pApp = Tizen::App::App::GetInstance();
445                 const String currentAppId = pApp->GetAppId();
446                 std::unique_ptr<char[]> pAppIdTemp(_StringConverter::CopyToCharArrayN(currentAppId));
447                 pAppIdChar = std::move(pAppIdTemp);
448                 SysLog(NID_SHELL, "App ID is %ls.", currentAppId.GetPointer());
449         }
450         else
451         if (notifyType == NOTIFY_TYPE_APP_ID)
452         {
453                 bool isAppInstalled = _Aul::IsInstalled(*pAppId);
454                 SysTryReturnResult(NID_SHELL, isAppInstalled == true, E_APP_NOT_INSTALLED, "The application %ls is not installed.", pAppId->GetPointer());
455                 std::unique_ptr<char[]> pAppIdTemp(_StringConverter::CopyToCharArrayN(*pAppId));
456                 pAppIdChar = std::move(pAppIdTemp);
457         }
458         else
459         {
460                 SysTryReturnResult(NID_SHELL, false, E_INVALID_ARG, "Invalid argument is used.");
461         }
462
463         const int badgeNumber = notifyRequest.GetBadgeNumber();
464         const int badgeOffset = notifyRequest.GetBadgeOffset();
465         const String& contentText = notifyRequest.GetAlertText();
466         // Allow change the badge without other properties.
467         if (badgeNumber != -1 || badgeOffset != -1)
468         {
469                 // Set - Badge
470                 if (badgeOffset != 0)
471                 {
472                         int badgeNumber = GetBadgeCount(pAppIdChar.get());
473                         if (badgeNumber <= 0)
474                         {
475                                 SetBadgeCount(pAppIdChar.get(), badgeOffset);
476                         }
477                         else
478                         {
479                                 SetBadgeCount(pAppIdChar.get(), badgeNumber + badgeOffset);
480                         }
481                 }
482                 else
483                 {
484                         if (badgeNumber == 0)
485                         {
486                                 r = RemoveBadge(pAppIdChar.get());
487                                 SysTryReturnResult(NID_SHELL, !IsFailed(r), r, "Failed to remove the badge.");
488                         }
489                         else
490                         if (badgeNumber > 0)
491                         {
492                                 r = SetBadgeCount(pAppIdChar.get(), badgeNumber);
493                                 SysTryReturnResult(NID_SHELL, !IsFailed(r), r, "Failed to set the badge.");
494                         }
495                 }
496                 if (!(isOngoing || !contentText.IsEmpty()))
497                 {
498                         SysLog(NID_SHELL, "No valid for Notification, just for set a badgeNumber update.");
499                         return E_SUCCESS;
500                 }
501         }
502         SysTryReturnResult(NID_SHELL, (isOngoing || !contentText.IsEmpty()), E_INVALID_ARG, "Invalid argument is used. MessageText is Empty");
503
504         const String& titleText = notifyRequest.GetTitleText();
505         const String& launchArguments = notifyRequest.GetAppMessage();
506         const String& iconPath = notifyRequest.GetIconFilePath();
507         const String& soundPath = notifyRequest.GetSoundFilePath();
508         const notification_type_e notiType = isOngoing ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
509         int notiPrivateId = isOngoing ? __notifyPrivateIdForOngoing : __notifyPrivateId;
510         notification_h notiHandle = null;
511         bundle* pBundle = null;
512         bool needUpdate = false;
513         bundle* service_data = null;
514         _AppArg arg;
515         service_h hSvc = null;
516         notification_ly_type_e layout = NOTIFICATION_LY_NOTI_EVENT_SINGLE;
517
518         // Notification creation
519         if (notiPrivateId != -1)
520         {
521                 notiHandle = notification_load(pAppIdChar.get(), notiPrivateId);
522                 SysTryLog(NID_SHELL, notiHandle != null, "Get notiHandle(%d) from notiPrivateId(%d).", notiHandle, notiPrivateId);
523         }
524
525         if (notiHandle == null)
526         {
527                 SysLog(NID_SHELL, "Previous notification(%d) no more valid - create new notification", notiPrivateId);
528                 notiPrivateId = -1;             // reset
529                 notiHandle = notification_create(notiType);
530         }
531         else
532         {
533                 needUpdate = true;              // No need to notification_insert.
534         }
535         SysTryReturnResult(NID_SHELL, notiHandle != null, E_OPERATION_FAILED, "Notification creation or loading failed.");
536
537         // Content text(Alert text)
538         std::unique_ptr<char[]> pMsg(_StringConverter::CopyToCharArrayN(contentText));
539
540         // Title text
541         std::unique_ptr<char[]> pTitleText(null);
542         if (!titleText.IsEmpty())
543         {
544                 std::unique_ptr<char[]> pTitleTextTemp(_StringConverter::CopyToCharArrayN(titleText));
545                 pTitleText = std::move(pTitleTextTemp);
546         }
547         else
548         {
549                 char* pAppName = null;
550                 r = ConvertAppManagerResult(app_manager_get_app_name(pAppIdChar.get(), &pAppName));
551                 if (pAppName)
552                 {
553                         String appName(pAppName);
554                         std::unique_ptr<char[]> pTitleTextTemp(_StringConverter::CopyToCharArrayN(appName));
555                         pTitleText = std::move(pTitleTextTemp);
556                         free(pAppName);
557                         SysLog(NID_SHELL, "App name is %ls.", appName.GetPointer());
558                 }
559                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Failed to get title from app_manager_get_app_name for default setting.", GetErrorMessage(r));
560         }
561         // Icon file path
562         std::unique_ptr<char[]> pIconPath(null);
563         if (!iconPath.IsEmpty())
564         {
565                 std::unique_ptr<char[]> pIconPathTemp(_StringConverter::CopyToCharArrayN(iconPath));
566                 pIconPath = std::move(pIconPathTemp);
567         }
568         else
569         {
570                 char* pDefaultIconPath = null;
571                 r = ConvertAppManagerResult(app_manager_get_app_icon_path(pAppIdChar.get(), &pDefaultIconPath));
572                 if (pDefaultIconPath)
573                 {
574                         String iconPath(pDefaultIconPath);
575                         std::unique_ptr<char[]> pIconPathTemp(_StringConverter::CopyToCharArrayN(iconPath));
576                         pIconPath = std::move(pIconPathTemp);
577                         free(pDefaultIconPath);
578                 }
579                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the icon path failed.", GetErrorMessage(r));
580         }
581         // Sound file path
582         std::unique_ptr<char[]> pSoundPath(null);
583         if (!soundPath.IsEmpty())
584         {
585                 std::unique_ptr<char[]> pSoundPathTemp(_StringConverter::CopyToCharArrayN(soundPath));
586                 pSoundPath = std::move(pSoundPathTemp);
587         }
588         // Set - AppId
589         if (notifyType == NOTIFY_TYPE_APP_ID)
590         {
591                 r = ConvertNotificationResult(notification_set_pkgname(notiHandle, pAppIdChar.get()));
592                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the package name failed.", GetErrorMessage(r));
593         }
594         // Set - title text
595         if (pTitleText.get())
596         {
597                 r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_TITLE, pTitleText.get(), NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
598                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the title text failed.", GetErrorMessage(r));
599         }
600         // Set - content text
601         if (pMsg.get())
602         {
603                 r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_CONTENT, pMsg.get(), NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
604                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting content text failed.", GetErrorMessage(r));
605         }
606         // Set - icon file path
607         r = ConvertNotificationResult(notification_set_image(notiHandle, NOTIFICATION_IMAGE_TYPE_ICON, pIconPath.get()));
608         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the icon path failed.", GetErrorMessage(r));
609         // Set - sound file path
610         if (pSoundPath.get())
611         {
612                 if (isOngoing && needUpdate)
613                 {
614                         r = ConvertNotificationResult(notification_set_sound(notiHandle, NOTIFICATION_SOUND_TYPE_NONE, NULL));
615                 }
616                 else
617                 {
618                         r = ConvertNotificationResult(notification_set_sound(notiHandle, NOTIFICATION_SOUND_TYPE_USER_DATA, pSoundPath.get()));
619                 }
620         }
621         else
622         {
623                 if (isOngoing && needUpdate)
624                 {
625                         r = ConvertNotificationResult(notification_set_sound(notiHandle, NOTIFICATION_SOUND_TYPE_NONE, NULL));
626                 }
627                 else
628                 {
629                         r = ConvertNotificationResult(notification_set_sound(notiHandle, NOTIFICATION_SOUND_TYPE_DEFAULT, NULL));
630                 }
631         }
632         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the sound failed.", GetErrorMessage(r));
633
634         // Set extended - text, thumbnail and background image
635         if (!isOngoing)
636         {
637                 // Extended style set
638                 String countText = notifyRequest.GetNotificationCountText();
639                 NotificationStyle notiStyle= notifyRequest.GetNotificationStyle();
640                 if (notiStyle == NOTIFICATION_STYLE_THUMBNAIL)
641                 {
642                         layout = NOTIFICATION_LY_NOTI_THUMBNAIL;
643                 }
644                 else
645                 if (notiStyle == NOTIFICATION_STYLE_NORMAL && !countText.IsEmpty())
646                 {
647                         layout = NOTIFICATION_LY_NOTI_EVENT_MULTIPLE;
648                 }
649
650                 if (!countText.IsEmpty())
651                 {
652                         std::unique_ptr<char[]> text(_StringConverter::CopyToCharArrayN(countText));
653                         r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, text.get(),
654                                                                                                                                 NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
655                 }
656
657                 String bgImageFilePath = notifyRequest.GetBackgroundImageFilePath();
658                 if (!bgImageFilePath.IsEmpty() && File::IsFileExist(bgImageFilePath))
659                 {
660                         std::unique_ptr<char[]> pBgImageFilePath(_StringConverter::CopyToCharArrayN(bgImageFilePath));
661                         r = ConvertNotificationResult(notification_set_image(notiHandle, NOTIFICATION_IMAGE_TYPE_BACKGROUND, pBgImageFilePath.get()));
662                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the background image path failed.", GetErrorMessage(r));
663                 }
664
665
666                 if (notiStyle == NOTIFICATION_STYLE_THUMBNAIL)
667                 {
668                         const IList* pThumbnailList = pRequestImpl->GetMessageThumbnailFilePathList();
669                         if (pThumbnailList)
670                         {
671                                 const static notification_image_type_e thumbnailListEnum[] = {NOTIFICATION_IMAGE_TYPE_LIST_1, NOTIFICATION_IMAGE_TYPE_LIST_2,
672                                                 NOTIFICATION_IMAGE_TYPE_LIST_3, NOTIFICATION_IMAGE_TYPE_LIST_4, NOTIFICATION_IMAGE_TYPE_LIST_5 };
673                                 int itemCount = pThumbnailList->GetCount();
674                                 const int maxCount = sizeof(thumbnailListEnum)/sizeof(thumbnailListEnum[0]);
675                                 if (itemCount > maxCount)
676                                 {
677                                         itemCount = maxCount;
678                                 }
679                                 for (int i = 0; i < itemCount; i++)
680                                 {
681                                         const String* pThumbnailPath = static_cast<const String*>(pThumbnailList->GetAt(i));
682                                         std::unique_ptr<char[]> filePath(_StringConverter::CopyToCharArrayN(*pThumbnailPath));
683                                         r = ConvertNotificationResult(notification_set_image(notiHandle, thumbnailListEnum[i], filePath.get()));
684                                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the thumbnail path failed.", GetErrorMessage(r));
685                                 }
686                         }
687                 }
688                 else
689                 {
690                         const IList* pMessageTextList = pRequestImpl->GetMessageTextList();
691                         if (pMessageTextList && pMessageTextList->GetCount())
692                         {
693                                 const String* pText1 = static_cast<const String*>(pMessageTextList->GetAt(0));
694                                 if (pText1)
695                                 {
696                                         int matchIndex;
697                                         if (E_SUCCESS == pText1->IndexOf(L'\t', 0, matchIndex))
698                                         {
699                                                 // Make two token
700                                                 String subText;
701                                                 pText1->SubString(0, matchIndex+1, subText);
702                                                 std::unique_ptr<char[]> leftText(_StringConverter::CopyToCharArrayN(subText));
703                                                 r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_INFO_1, leftText.get(),
704                                                                                                                                                         NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
705                                                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the Info1 text failed.", GetErrorMessage(r));
706                                                 subText.Clear();
707                                                 if (E_SUCCESS == pText1->SubString(matchIndex+1, subText))
708                                                 {
709                                                         std::unique_ptr<char[]> rightText(_StringConverter::CopyToCharArrayN(subText));
710                                                         r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, rightText.get(),
711                                                                                                                                                                 NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
712                                                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the Info1 sub text failed.", GetErrorMessage(r));
713                                                 }
714                                         }
715                                         else
716                                         {
717                                                 std::unique_ptr<char[]> leftText(_StringConverter::CopyToCharArrayN(*pText1));
718                                                 r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_INFO_1, leftText.get(),
719                                                                                                                                                         NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
720                                                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the Info1 text failed.", GetErrorMessage(r));
721                                         }
722                                 }
723                                 const String* pText2 = static_cast<const String*>(pMessageTextList->GetAt(1));
724                                 if (pText2)
725                                 {
726                                         int matchIndex;
727                                         // 2.1: Multiple layout has single line text for 2nd information text.
728                                         if ((layout == NOTIFICATION_LY_NOTI_EVENT_MULTIPLE) && (E_SUCCESS == (pText2->IndexOf(L'\t', 0, matchIndex))))
729                                         {
730                                                 // Make two token
731                                                 String subText;
732                                                 pText2->SubString(0, matchIndex+1, subText);
733                                                 std::unique_ptr<char[]> leftText(_StringConverter::CopyToCharArrayN(subText));
734                                                 r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_INFO_2, leftText.get(),
735                                                                                                                                                         NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
736                                                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the Info2 text failed.", GetErrorMessage(r));
737                                                 subText.Clear();
738                                                 if (E_SUCCESS == pText2->SubString(matchIndex+1, subText))
739                                                 {
740                                                         std::unique_ptr<char[]> rightText(_StringConverter::CopyToCharArrayN(subText));
741                                                         r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_INFO_SUB_2, rightText.get(),
742                                                                                                                                                                 NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
743                                                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the Info2 sub text failed.", GetErrorMessage(r));
744                                                 }
745                                         }
746                                         else
747                                         {
748                                                 std::unique_ptr<char[]> leftText(_StringConverter::CopyToCharArrayN(*pText2));
749                                                 r = ConvertNotificationResult(notification_set_text(notiHandle, NOTIFICATION_TEXT_TYPE_INFO_2, leftText.get(),
750                                                                                                                                                         NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
751                                                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the Info2 text failed.", GetErrorMessage(r));
752                                         }
753                                 }
754                         }
755                         else
756                         if (needUpdate)
757                         {       // Reset text for update case. also must be checked the previous text with get_text
758                                 char* pRetStr = null;
759                                 notification_get_text(notiHandle, NOTIFICATION_TEXT_TYPE_INFO_1, &pRetStr);
760                                 if (pRetStr)
761                                 {
762                                         const static notification_text_type_e infoTextEnums[] = { NOTIFICATION_TEXT_TYPE_INFO_1, NOTIFICATION_TEXT_TYPE_INFO_SUB_1,
763                                                                                                                                                          NOTIFICATION_TEXT_TYPE_INFO_2, NOTIFICATION_TEXT_TYPE_INFO_SUB_2 };
764                                         for (unsigned int i = 0; i < sizeof(infoTextEnums)/sizeof(infoTextEnums[0]); i++)
765                                         {
766                                                 r = ConvertNotificationResult(notification_set_text(notiHandle, infoTextEnums[i], null, NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
767                                                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the initial text failed.", GetErrorMessage(r));
768                                         }
769                                 }
770                         }
771                 }
772         }
773
774         // Set - service
775         int retVal = SERVICE_ERROR_NONE;
776
777         r = arg.Construct(launchArguments);
778         SysTryCatch(NID_SHELL, !IsFailed(r), r = E_OPERATION_FAILED, r, "[E_OPERATION_FAILED] Failed to construct _AppArg.");
779
780         pBundle = arg.GetBundle();
781
782         service_create_request(pBundle, &hSvc);
783         SysTryCatch(NID_SHELL, hSvc != null, r = E_OPERATION_FAILED, r, "[E_OPERATION_FAILED] service_create_request failed.");
784
785         if (notifyType == NOTIFY_TYPE_SIMPLE || notifyType == NOTIFY_TYPE_APP_ID)
786         {
787                 retVal = service_set_app_id(hSvc, pRequestImpl->IsAppBinding() ? pAppIdChar.get() : null);
788                 SysTryCatch(NID_SHELL, retVal == SERVICE_ERROR_NONE, r = E_OPERATION_FAILED, r, "[E_OPERATION_FAILED] service_set_app_id failed(%d).", retVal);
789         }
790         else
791         if (notifyType == NOTIFY_TYPE_APP_CONTROL)
792         {
793                 std::unique_ptr<char[]> pOperationIdChar(_StringConverter::CopyToCharArrayN(*pOperationId));
794                 retVal = service_set_operation(hSvc, pOperationIdChar.get());
795                 SysTryCatch(NID_SHELL, retVal == SERVICE_ERROR_NONE, r = E_OPERATION_FAILED, r, "[E_OPERATION_FAILED] service_set_operation failed(%d).", retVal);
796
797                 if (pUriData)
798                 {
799                         std::unique_ptr<char[]> pUri(_StringConverter::CopyToCharArrayN(*pUriData));
800                         retVal = service_set_uri(hSvc, pUri.get());
801                         SysTryLog(NID_SHELL, retVal == SERVICE_ERROR_NONE, "service_set_uri failed(%d).", retVal);
802                 }
803                 if (pDataType)
804                 {
805                         std::unique_ptr<char[]> pMime(_StringConverter::CopyToCharArrayN(*pDataType));
806                         retVal = service_set_mime(hSvc, pMime.get());
807                         SysTryLog(NID_SHELL, retVal == SERVICE_ERROR_NONE, "service_set_mime failed(%d).", retVal);
808                 }
809                 if (pExtraData)
810                 {
811                         std::unique_ptr<Tizen::Base::Collection::IMapEnumerator> pMapEnum(pExtraData->GetMapEnumeratorN());
812                         if (pMapEnum)
813                         {
814                                 while (pMapEnum->MoveNext() == E_SUCCESS)
815                                 {
816                                         String* pKey = static_cast<String* > (pMapEnum->GetKey());
817                                         String* pValue = static_cast<String* > (pMapEnum->GetValue());
818                                         if (pKey && pValue)
819                                         {
820                                                 std::unique_ptr<char[]> pKeyString(_StringConverter::CopyToCharArrayN(*pKey));
821                                                 std::unique_ptr<char[]> pValueString(_StringConverter::CopyToCharArrayN(*pValue));
822                                                 retVal = service_add_extra_data(hSvc, pKeyString.get(), pValueString.get());
823                                                 SysTryLog(NID_SHELL, retVal == SERVICE_ERROR_NONE, "service_add_extra_data failed(%d).", retVal);
824                                         }
825                                         else
826                                         {
827                                                 SysLog(NID_SHELL, "pKey or pValue is invalid.");
828                                         }
829                                 }
830                         }
831                 }
832         }
833
834         retVal = service_to_bundle(hSvc, &service_data);
835         SysTryCatch(NID_SHELL, retVal == SERVICE_ERROR_NONE, r = E_OPERATION_FAILED, r, "[E_OPERATION_FAILED] service_to_bundle failed(%d).", retVal);
836
837         r = ConvertNotificationResult(notification_set_property(notiHandle, 0));
838         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification property failed.", GetErrorMessage(r));
839
840         r = ConvertNotificationResult(notification_set_execute_option(notiHandle, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, service_data));
841         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the execute option failed.", GetErrorMessage(r));
842
843         // Set layout
844         if (isOngoing)
845         {
846                 OngoingActivityType activityType = notifyRequest.GetOngoingActivityType();
847                 if (activityType == ONGOING_ACTIVITY_TYPE_TEXT)
848                 {
849                         layout = NOTIFICATION_LY_ONGOING_EVENT;
850                 }
851                 else
852                 {
853                         layout = NOTIFICATION_LY_ONGOING_PROGRESS;
854                 }
855         }
856         r = ConvertNotificationResult(notification_set_layout(notiHandle, layout));
857         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification layout failed.", GetErrorMessage(r));
858
859         // For ongoing
860         if (isOngoing)
861         {
862                 OngoingActivityType activityType = notifyRequest.GetOngoingActivityType();
863                 int progress = notifyRequest.GetOngoingActivityProgress();
864
865                 switch (activityType)
866                 {
867                 case ONGOING_ACTIVITY_TYPE_PROGRESS_PERCENTAGE:
868                         if (needUpdate)
869                         {
870                                 r = ConvertNotificationResult(notification_set_size(notiHandle, 0));    // Reset for override BYTE type
871                                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification size failed.", GetErrorMessage(r));
872                         }
873                         r = ConvertNotificationResult(notification_set_progress(notiHandle, progress/100.));
874                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification progress failed.", GetErrorMessage(r));
875                         break;
876
877                 case ONGOING_ACTIVITY_TYPE_PROGRESS_BYTE:
878                         if (needUpdate)
879                         {
880                                 r = ConvertNotificationResult(notification_set_progress(notiHandle, 0.0));      // Reset for override PERCENTAGE type
881                                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification progress failed.", GetErrorMessage(r));
882                         }
883                         r = ConvertNotificationResult(notification_set_size(notiHandle, progress));
884                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification size failed.", GetErrorMessage(r));
885                         break;
886
887                 case ONGOING_ACTIVITY_TYPE_TEXT:
888                         r = ConvertNotificationResult(notification_set_progress(notiHandle, 0.0));      // Reset the progress
889                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification progress failed.", GetErrorMessage(r));
890                         r = ConvertNotificationResult(notification_set_size(notiHandle, 0));
891                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification size failed.", GetErrorMessage(r));
892                         break;
893
894                 default:
895                         r = E_OPERATION_FAILED;
896                         break;
897                 }
898         }
899
900         // insert for new notification
901         if (!needUpdate)
902         {       // new
903                 r = ConvertNotificationResult(notification_insert(notiHandle, &notiPrivateId));
904                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting new notification failed.", GetErrorMessage(r));
905
906                 SysLog(NID_SHELL, "Getting new notiPrivateId(%d)", notiPrivateId);
907
908                 if (isOngoing)
909                 {
910                         __notifyPrivateIdForOngoing  = notiPrivateId;
911                 }
912                 else
913                 {
914                         __notifyPrivateId = notiPrivateId;
915                 }
916         }
917         else
918         {
919                 int previousDisplaySet = NOTIFICATION_DISPLAY_APP_ALL;
920                 if (isOngoing)
921                 {
922                         r = ConvertNotificationResult(notification_get_display_applist(notiHandle, &previousDisplaySet));
923                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification area failed.", GetErrorMessage(r));
924
925                         int newDisplaySet = previousDisplaySet & ~NOTIFICATION_DISPLAY_APP_TICKER;
926                         r = ConvertNotificationResult(notification_set_display_applist(notiHandle, newDisplaySet));
927                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification area failed.", GetErrorMessage(r));
928                 }
929
930                 r = ConvertNotificationResult(notification_update(notiHandle));
931                 SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Updating the notification failed. notiPrivateId(%d)", GetErrorMessage(r), notiPrivateId);
932
933                 if (isOngoing)
934                 {
935                         r = ConvertNotificationResult(notification_set_display_applist(notiHandle, previousDisplaySet));
936                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Setting the notification area failed.", GetErrorMessage(r));
937                 }
938         }
939
940 CATCH:
941         service_destroy(hSvc);
942         notification_free(notiHandle);
943         return r;
944 }
945
946 result
947 _NotificationManagerImpl::RemoveNotification(void)
948 {
949         return RemoveNotification(false);
950 }
951
952 result
953 _NotificationManagerImpl::RemoveOngoingActivityNotification(void)
954 {
955         return RemoveNotification(true);
956 }
957
958 result
959 _NotificationManagerImpl::RemoveNotification(bool onGoing)
960 {
961         result r = E_SUCCESS;
962         notification_error_e err = NOTIFICATION_ERROR_NONE;
963         const notification_type_e notiType = onGoing ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
964         const int notiPrivateId = onGoing ? __notifyPrivateIdForOngoing : __notifyPrivateId;
965
966         if (notiPrivateId != -1)
967         {
968                 notification_h notiHandle = notification_load(null, notiPrivateId);
969                 if (notiHandle)
970                 {
971                         err = notification_delete(notiHandle);
972                         SysLog(NID_SHELL, "Notification deleted.");
973                 }
974                 else
975                 {
976                         SysLog(NID_SHELL, "Notification already deleted.");
977                 }
978                 err = notification_free(notiHandle);
979         }
980         else
981         {
982                 err = notification_delete_all_by_type(null, notiType);
983                 SysLog(NID_SHELL, "All [%s] notifications deleted.", onGoing ? "Ongoing" : "Normal");
984         }
985
986         SysTryReturnResult(NID_SHELL, err == NOTIFICATION_ERROR_NONE, E_OPERATION_FAILED, "Failed to RemoveNotification with reason (%d).", err);
987         return r;
988 }
989
990 result
991 _NotificationManagerImpl::RemoveNotificationByAppId(const Tizen::App::AppId& appId, bool onGoing)
992 {
993         result r = E_SUCCESS;
994         notification_error_e err = NOTIFICATION_ERROR_NONE;
995         bool isValidAppId = _Aul::IsInstalled(appId);
996         SysTryReturnResult(NID_SHELL, isValidAppId == true, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
997
998         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
999         const notification_type_e notiType = onGoing ? NOTIFICATION_TYPE_ONGOING : NOTIFICATION_TYPE_NOTI;
1000         const int notiPrivateId = onGoing ? __notifyPrivateIdForOngoing : __notifyPrivateId;
1001
1002         if (notiPrivateId != -1)
1003         {
1004                 notification_h notiHandle = notification_load(pAppId.get(), notiPrivateId);
1005                 if (notiHandle)
1006                 {
1007                         err = notification_delete(notiHandle);
1008                         SysLog(NID_SHELL, "Notification deleted.");
1009                 }
1010                 else
1011                 {
1012                         SysLog(NID_SHELL, "Notification already deleted.");
1013                 }
1014                 err = notification_free(notiHandle);
1015         }
1016         else
1017         {
1018                 err = notification_delete_all_by_type(pAppId.get(), notiType);
1019                 SysLog(NID_SHELL, "All [%s] notifications deleted.", onGoing ? "Ongoing" : "Normal");
1020         }
1021
1022         SysTryReturnResult(NID_SHELL, err == NOTIFICATION_ERROR_NONE, E_OPERATION_FAILED, "Failed to RemoveNotificationByAppId with reason (%d).", err);
1023         return r;
1024 }
1025
1026 result
1027 _NotificationManagerImpl::RemoveBadge(const char* pkgName) const
1028 {
1029         bool badgeExist = false;
1030         badge_error_e badgeError = badge_is_existing(pkgName, &badgeExist);
1031         SysTryLog(NID_SHELL, badgeError == BADGE_ERROR_NONE, "badge_is_existing failed(%d).", badgeError);
1032
1033         if (badgeExist)
1034         {
1035                 badgeError = badge_remove(pkgName);
1036                 SysTryReturnResult(NID_SHELL, badgeError == BADGE_ERROR_NONE, E_OPERATION_FAILED, "badge_remove failed(%d).", badgeError);
1037         }
1038
1039         SysLog(NID_SHELL, "Badge is removed.");
1040         return E_SUCCESS;
1041 }
1042
1043 result
1044 _NotificationManagerImpl::SetBadgeCount(const char* pkgName, int badgeCount) const
1045 {
1046         bool badgeExist;
1047         badge_error_e badgeError = badge_is_existing(pkgName, &badgeExist);
1048         SysTryLog(NID_SHELL, badgeError == BADGE_ERROR_NONE, "badge_is_existing failed(%d).", badgeError);
1049
1050         if (!badgeExist)
1051         {
1052                 badgeError = badge_create(pkgName, pkgName);
1053                 SysTryReturnResult(NID_SHELL, badgeError == BADGE_ERROR_NONE, E_OPERATION_FAILED, "badge_create failed(%d).", badgeError);
1054         }
1055
1056         badgeError = badge_set_count(pkgName, badgeCount);
1057         SysTryLog(NID_SHELL, badgeError == BADGE_ERROR_NONE, "badge_set_count failed(%d).", badgeError);
1058
1059         SysLog(NID_SHELL, "Badge count is %d.", badgeCount);
1060         return E_SUCCESS;
1061 }
1062
1063 int
1064 _NotificationManagerImpl::GetBadgeCount(const char* pkgName) const
1065 {
1066         unsigned int count = 0;
1067
1068         badge_error_e badgeError = badge_get_count(pkgName, &count);
1069         if (badgeError == BADGE_ERROR_NONE)
1070         {
1071                 SysLog(NID_SHELL, "badge_get_count(%d)", count);
1072                 return count;
1073         }
1074         else
1075         {
1076                 SysLog(NID_SHELL, "badge_get_count(%d)", badgeError);
1077                 return -1;
1078         }
1079 }
1080
1081 result
1082 _NotificationManagerImpl::AddBadgeEventListener(IBadgeEventListener& listener)
1083 {
1084         SysTryReturnResult(NID_SHELL, !__badgeEventListenerList.Contains(&listener), E_OBJ_ALREADY_EXIST, "The listener is already added.");
1085         SysLog(NID_SHELL, "AddBadgeEventListener (%x)", &listener);
1086
1087         result r = _BadgeManagerImpl::GetInstance()->AddPrimaryBadgeEventListener(*this);
1088         SysTryReturnResult(NID_SHELL, !IsFailed(r), E_SYSTEM, "Failed to AddPrimaryBadgeEventListener with reason (%s)", GetErrorMessage(r) );
1089
1090         return __badgeEventListenerList.Add(&listener);
1091 }
1092
1093 result
1094 _NotificationManagerImpl::RemoveBadgeEventListener(IBadgeEventListener& listener)
1095 {
1096         SysLog(NID_SHELL, "RemoveBadgeEventListener (%x)", &listener);
1097
1098         result r = __badgeEventListenerList.Remove(&listener);  // Remove() can return E_SUCCESS and E_OBJ_NOT_FOUND.
1099         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "Failed to RemoveBadgeEventListener with reason (%s)", GetErrorMessage(r) );
1100
1101         return _BadgeManagerImpl::GetInstance()->RemovePrimaryBadgeEventListener(*this);
1102 }
1103
1104 void
1105 _NotificationManagerImpl::OnBadgeUpdated(const Tizen::App::AppId& appId, int badgeNumber)
1106 {
1107         std::unique_ptr<IEnumeratorT<Tizen::Shell::IBadgeEventListener* > > pEnum(__badgeEventListenerList.GetEnumeratorN());
1108         SysTryReturnVoidResult(NID_SHELL, pEnum.get(), E_OUT_OF_MEMORY, "Failed to GetEnumeratorN()!");
1109
1110         IBadgeEventListener* pListener;
1111         while (pEnum->MoveNext() == E_SUCCESS)
1112         {
1113                 pListener = null;
1114                 pEnum->GetCurrent(pListener);
1115                 if( !pListener)
1116                 {
1117                         SysLog(NID_SHELL, "pListener is null!");
1118                         continue;
1119                 }
1120                 pListener->OnBadgeUpdated(appId, badgeNumber);
1121         }
1122 }
1123
1124 } }    // Tizen::Shell