Tizen 2.1 base
[platform/framework/native/app-service.git] / src / FApp_NotificationManagerService.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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         FApp_NotificationManagerService.cpp
20  * @brief       This is the implementation for the _NotificationManagerService class.
21  */
22
23 #include <unique_ptr.h>
24
25 #include <FShellNotificationManager.h>
26 #include <FShellNotificationRequest.h>
27
28 #include <FBaseSysLog.h>
29 #include <FBaseInternalTypes.h>
30 #include <FBase_StringConverter.h>
31 #include <FApp_AppArg.h>
32 #include "FApp_AppInfo.h"
33 #include "FApp_Aul.h"
34 #include "FApp_NotificationManagerService.h"
35 #include "FAppPkg_PackageManagerImpl.h"
36
37 using namespace Tizen::App;
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Io;
41 using namespace Tizen::Shell;
42
43 extern "C" int service_create_request(bundle *data, service_h *service);
44 extern "C" int service_to_bundle(service_h service, bundle** data);
45
46 namespace
47 {
48
49 result
50 ConvertNotificationResult(int error)
51 {
52         switch (error)
53         {
54         case NOTIFICATION_ERROR_NONE:
55                 return E_SUCCESS;
56         case NOTIFICATION_ERROR_INVALID_DATA:
57                 return E_INVALID_ARG;
58         case NOTIFICATION_ERROR_NO_MEMORY:
59                 return E_OUT_OF_MEMORY;
60         case NOTIFICATION_ERROR_FROM_DB:
61                 return E_DATABASE;
62         case NOTIFICATION_ERROR_ALREADY_EXIST_ID:
63                 return E_SYSTEM;
64         case NOTIFICATION_ERROR_NOT_EXIST_ID:
65                 return E_SYSTEM;
66         default:
67                 return E_SYSTEM;
68         }
69 }
70
71 static int
72 GetMessageLength()
73 {
74         static int length = (_AppInfo::GetApiVersion() < _API_VERSION_2_1) ? 127 : MAX_NOTIFICATION_MESSAGE_LENGTH;
75
76         return length;
77 }
78
79 }
80
81 namespace Tizen { namespace App {
82
83 _NotificationManagerService::_NotificationManagerService()
84
85 {
86 }
87
88 _NotificationManagerService::~_NotificationManagerService()
89 {
90 }
91
92 result
93 _NotificationManagerService::Construct(void)
94 {
95         return E_SUCCESS;
96 }
97
98 result
99 _NotificationManagerService::NotifyMessage(const AppId& appId,const NotificationRequest& notiMessage, bool isOngoing)
100 {
101         result r = E_SUCCESS;
102         int progress = -1;
103         char* pMsg = null;
104         notification_h core = NULL;
105         char* pTitleText = NULL;
106         char* pIconPath = NULL;
107         char* pSoundPath = NULL;
108         bundle* kb = NULL;
109         bundle* service_data = NULL;
110         service_h svc = NULL;
111         _AppArg arg;
112
113         
114         const String& messageText = notiMessage.GetAlertText();
115         const String& launchArguments = notiMessage.GetAppMessage();
116         const String& titleText = notiMessage.GetTitleText();
117         const String& iconPath = notiMessage.GetIconFilePath();
118         const String& soundPath = notiMessage.GetSoundFilePath();
119
120         bool isAppInstalled = _Aul::IsInstalled(appId);
121         SysTryReturnResult(NID_APP, isAppInstalled == true, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
122
123         std::unique_ptr<char[]> buffer(_StringConverter::CopyToCharArrayN(appId));
124
125         if (isOngoing || !messageText.IsEmpty())
126         {
127                 SysTryReturnResult(NID_APP,
128                                           messageText.GetLength() <=  GetMessageLength(), E_INVALID_ARG,
129                                           "MessageText is greater than MAX_NOTIFICATION_MESSAGE_LENGTH.");
130
131                 if (isOngoing)
132                 {
133                         core = notification_new(NOTIFICATION_TYPE_ONGOING, NOTIFICATION_GROUP_ID_DEFAULT, NOTIFICATION_PRIV_ID_NONE);
134                         SysTryReturnResult(NID_APP, core!= NULL , E_SYSTEM, "Notification creation failed ");
135                 }
136                 else
137                 {
138                         core = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_DEFAULT, NOTIFICATION_PRIV_ID_NONE);
139                         SysTryReturnResult(NID_APP, core!= NULL , E_SYSTEM, "Notification creation failed ");
140                 }
141
142                 pMsg = _StringConverter::CopyToCharArrayN(messageText);
143                 notification_set_text(core, NOTIFICATION_TEXT_TYPE_CONTENT, pMsg, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
144
145
146                 if (!titleText.IsEmpty())
147                 {
148                         pTitleText = _StringConverter::CopyToCharArrayN(titleText);
149                         r = ConvertNotificationResult(notification_set_text(core, NOTIFICATION_TEXT_TYPE_TITLE, pTitleText, NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
150                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set Title Text failed.", GetErrorMessage(r));
151                 }
152                 else
153                 {
154                         app_manager_get_app_name(buffer.get(), &pTitleText);
155                         r = ConvertNotificationResult(notification_set_text(core, NOTIFICATION_TEXT_TYPE_TITLE, pTitleText, NULL, NOTIFICATION_VARIABLE_TYPE_NONE));
156                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set Title Text failed.", GetErrorMessage(r));
157                 }
158                 if (!iconPath.IsEmpty())
159                 {
160                         pIconPath = _StringConverter::CopyToCharArrayN(iconPath);
161                         r = ConvertNotificationResult(notification_set_image(core, NOTIFICATION_IMAGE_TYPE_ICON, pIconPath));
162                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set icon path  failed.", GetErrorMessage(r));
163                 }
164                 else
165                 {
166                         app_manager_get_app_icon_path(buffer.get(), &pIconPath);
167                         r = ConvertNotificationResult(notification_set_image(core, NOTIFICATION_IMAGE_TYPE_ICON, pIconPath));
168                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set icon path  failed.", GetErrorMessage(r));
169                 }
170                 if (!soundPath.IsEmpty())
171                 {
172                         pSoundPath = _StringConverter::CopyToCharArrayN(soundPath);
173                         r = ConvertNotificationResult(notification_set_sound(core, NOTIFICATION_SOUND_TYPE_USER_DATA, pSoundPath));
174                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification Set sound  failed.", GetErrorMessage(r));
175                 }
176                 
177                 r = arg.Construct(launchArguments);
178                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
179
180                 kb = arg.GetBundle();
181                 service_create_request(kb, &svc);
182                 service_set_app_id(svc, buffer.get());
183
184                 if (service_to_bundle(svc, &service_data) == SERVICE_ERROR_NONE)
185                 {
186                         notification_set_property(core, 0);
187                         notification_set_execute_option(core, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, service_data);
188                         SysLog(NID_APP, "Sending notification[%ls] for package %s.", messageText.GetPointer(), buffer.get());
189                 }
190
191                 if (isOngoing)
192                 {
193                         OngoingActivityType activityType = notiMessage.GetOngoingActivityType();
194                         progress = notiMessage.GetOngoingActivityProgress();
195
196                         switch (activityType)
197                         {
198                         case ONGOING_ACTIVITY_TYPE_PROGRESS_PERCENTAGE:
199                                 r = ConvertNotificationResult(notification_insert(core,NULL));
200                                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
201
202                                 r = ConvertNotificationResult(notification_update_progress(core, NOTIFICATION_PRIV_ID_NONE, progress/100.));
203                                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification update failure.", GetErrorMessage(r));
204                                 break;
205                         case ONGOING_ACTIVITY_TYPE_PROGRESS_BYTE:
206                                 notification_insert(core,NULL);
207                                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
208
209                                 r = ConvertNotificationResult(notification_update_size(core,NOTIFICATION_PRIV_ID_NONE, progress));
210                                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification update failure.", GetErrorMessage(r));
211                                 break;
212                         case ONGOING_ACTIVITY_TYPE_TEXT:
213                                 r = ConvertNotificationResult(notification_insert(core,NULL));
214                                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
215                                 break;
216                         default:
217                                 r = E_OPERATION_FAILED;
218                                 //ui_notification_set_content() is done already
219                                 break;
220                         }
221                 }
222                 else
223                 {
224                         r = ConvertNotificationResult(notification_insert(core,NULL));
225                         SysTryLog(NID_APP, !IsFailed(r), "[%s] Notification post failure.", GetErrorMessage(r));
226                 }
227
228         }
229         else
230         {
231                 SysTryReturnResult(NID_APP, 0, E_INVALID_ARG, "MessageText is Empty");
232         }
233
234         if (notiMessage.GetBadgeNumber() >= 0)
235         {
236                 notification_set_badge(NULL, NOTIFICATION_GROUP_ID_NONE, notiMessage.GetBadgeNumber());
237         }
238
239 CATCH:
240         delete[] pMsg;
241         delete[] pIconPath;
242         delete[] pTitleText;
243         delete[] pSoundPath;
244
245         if (core)
246         {
247                 notification_free(core);
248         }
249         service_destroy(svc);
250         return r;
251 }
252
253 result
254 _NotificationManagerService::RemoveNotification(const AppId& appId, bool isOngoing)
255 {
256         int retCode = 0;
257         bool b = _Aul::IsInstalled(appId);
258         SysTryReturnResult(NID_APP, b == true, E_APP_NOT_INSTALLED, "The application %ls is not installed", appId.GetPointer());
259
260         std::unique_ptr<char[]> pAppid(_StringConverter::CopyToCharArrayN(appId));
261
262         if (isOngoing)
263         {
264                 retCode = notification_delete_all_by_type(pAppid.get(), NOTIFICATION_TYPE_ONGOING);
265         }
266         else
267         {
268                 retCode = notification_delete_all_by_type(pAppid.get(), NOTIFICATION_TYPE_NOTI);
269         }
270
271         result r = E_SUCCESS;
272
273         switch (retCode)
274         {
275         case NOTIFICATION_ERROR_NONE:
276                 r = E_SUCCESS;
277                 break;
278
279         case NOTIFICATION_ERROR_INVALID_DATA:
280                 r = E_INVALID_ARG;
281                 break;
282
283         default:
284                 r = E_SYSTEM;
285                 break;
286         }
287
288         SysLog(NID_APP, "[%s] Finished.", GetErrorMessage(r));
289         return r;
290 }
291 }} //namespace Tizen { namespace App {