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