[Notification] prepared stubs for UserNotification
[platform/core/api/webapi-plugins.git] / src / notification / notification_manager.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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 #include "notification/notification_manager.h"
18
19 #include <fcntl.h>
20 #include <unistd.h>
21
22 #include <app_control_internal.h>
23 #include <device/led.h>
24 #include <notification_internal.h>
25 #include <notification_list.h>
26 #include <app_common.h>
27
28 #include "common/converter.h"
29 #include "common/logger.h"
30 #include "common/scope_exit.h"
31
32 #include "notification/status_notification.h"
33 #include "notification/common_notification.h"
34
35 namespace extension {
36 namespace notification {
37
38 using namespace common;
39
40 NotificationManager::NotificationManager() {
41   LoggerD("Enter");
42 }
43
44 NotificationManager::~NotificationManager() {
45   LoggerD("Enter");
46 }
47
48 NotificationManager* NotificationManager::GetInstance() {
49   LoggerD("Enter");
50   static NotificationManager instance;
51   return &instance;
52 }
53
54 PlatformResult NotificationManager::Post(const picojson::object& args,
55                                          picojson::object& out) {
56   LoggerD("Enter");
57   return StatusNotification::FromJson(args, false, &out);
58 }
59
60 PlatformResult NotificationManager::PostUserNoti(const picojson::object& args,
61                                          picojson::object& out) {
62   LoggerD("Enter");
63   // TODO implement
64   return LogAndCreateResult(ErrorCode::NOT_SUPPORTED_ERR,
65                             "Not implemented yet",
66                             ("Not implemented yet"));
67 }
68
69 PlatformResult NotificationManager::Update(const picojson::object& args) {
70   LoggerD("Enter");
71   return StatusNotification::FromJson(args, true, NULL);
72 }
73
74 PlatformResult NotificationManager::UpdateUserNoti(const picojson::object& args) {
75   LoggerD("Enter");
76   // TODO implement
77   return LogAndCreateResult(ErrorCode::NOT_SUPPORTED_ERR,
78                             "Not implemented yet",
79                             ("Not implemented yet"));
80 }
81
82
83 PlatformResult NotificationManager::Remove(const picojson::object& args) {
84   LoggerD("Enter");
85   int id = std::stoi(FromJson<std::string>(args, "id"));
86
87   int ret = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_NONE, id);
88   if (ret != NOTIFICATION_ERROR_NONE) {
89     return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR,
90                           "Cannot remove notification error",
91                           ("Cannot remove notification error: %d", ret));
92   }
93
94   return PlatformResult(ErrorCode::NO_ERROR);
95 }
96
97 PlatformResult NotificationManager::RemoveAll() {
98   LoggerD("Enter");
99   int ret = notification_delete_all(NOTIFICATION_TYPE_NOTI);
100   if (ret != NOTIFICATION_ERROR_NONE) {
101     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
102                           "Notification noti remove all failed",
103                           ("Notification remove all failed: %d", ret));
104   }
105
106   ret = notification_delete_all(NOTIFICATION_TYPE_ONGOING);
107   if (ret != NOTIFICATION_ERROR_NONE) {
108     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
109                           "Notification ongoing remove all failed",
110                           ("Notification remove all failed: %d", ret));
111   }
112
113   return PlatformResult(ErrorCode::NO_ERROR);
114 }
115
116 PlatformResult NotificationManager::Get(const picojson::object& args,
117                                         picojson::object& out) {
118   LoggerD("Enter");
119   int id;
120   try {
121     id = std::stoi(FromJson<std::string>(args, "id"));
122   } catch (...) {
123     LoggerE("Failed to convert string to int");
124     return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Failed to convert string to int.");
125   }
126
127   app_control_h app_control = nullptr;
128   notification_h noti_handle = nullptr;
129
130   SCOPE_EXIT {
131     if (app_control) {
132       app_control_destroy(app_control);
133     }
134     free(noti_handle);
135   };
136
137   PlatformResult status = StatusNotification::GetNotiHandle(id, &noti_handle);
138   if (status.IsError())
139   {
140     LoggerE("Failed: GetNotiHandle");
141     return status;
142   }
143
144   status = StatusNotification::GetAppControl(noti_handle, &app_control);
145   if (status.IsError())
146   {
147     LoggerE("Failed: GetAppControl");
148     return status;
149   }
150   status = StatusNotification::ToJson(id, noti_handle, app_control, &out);
151   if (status.IsError())
152   {
153     LoggerE("Failed: ToJson");
154     return status;
155   }
156   return PlatformResult(ErrorCode::NO_ERROR);
157 }
158
159 PlatformResult NotificationManager::GetUserNoti(const picojson::object& args,
160                                         picojson::object& out) {
161   LoggerD("Enter");
162   // TODO implement
163   return LogAndCreateResult(ErrorCode::NOT_SUPPORTED_ERR,
164                             "Not implemented yet",
165                             ("Not implemented yet"));
166 }
167
168 PlatformResult NotificationManager::GetAll(picojson::array& out) {
169   LoggerD("Enter");
170   notification_h noti = nullptr;
171   notification_list_h noti_list = nullptr;
172   notification_list_h noti_list_iter = nullptr;
173   char* package = nullptr;
174
175   if (APP_ERROR_NONE == app_get_id(&package)) {
176     LoggerD("Package id: %s", package);
177   } else {
178     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
179                           "Could not get package id");
180   }
181   const std::string package_str = package;
182   free(package);
183
184   int ret = notification_get_detail_list(package_str.c_str(), NOTIFICATION_GROUP_ID_NONE,
185                                          NOTIFICATION_PRIV_ID_NONE, -1, &noti_list);
186   if (NOTIFICATION_ERROR_NONE != ret) {
187     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
188                           "Get notification list error",
189                           ("Get notification list error: %d", ret));
190   }
191
192   SCOPE_EXIT { notification_free_list(noti_list); };
193
194   noti_list_iter = notification_list_get_head(noti_list);
195
196   while (nullptr != noti_list_iter) {
197     noti = notification_list_get_data(noti_list_iter);
198     if (nullptr != noti) {
199       int noti_priv = -1;
200       ret = notification_get_id(noti, NULL, &noti_priv);
201       if (NOTIFICATION_ERROR_NONE != ret) {
202         return LogAndCreateResult(ErrorCode::UNKNOWN_ERR,
203                               "Cannot get notification id error",
204                               ("Cannot get notification id, error: %d", ret));
205       }
206
207       app_control_h app_control = nullptr;
208       PlatformResult status =
209           StatusNotification::GetAppControl(noti, &app_control);
210
211       SCOPE_EXIT {
212           if (app_control) {
213               app_control_destroy(app_control);
214           }
215       };
216
217       if (status.IsError())
218         return status;
219
220       picojson::object noti_item = picojson::object();
221
222       status =
223           StatusNotification::ToJson(noti_priv, noti, app_control, &noti_item);
224       if (status.IsError())
225         return status;
226
227       out.push_back(picojson::value(noti_item));
228     }
229
230     noti_list_iter = notification_list_get_next(noti_list_iter);
231   }
232
233   return PlatformResult(ErrorCode::NO_ERROR);
234 }
235
236 PlatformResult NotificationManager::GetAllUserNoti(picojson::array& out) {
237   LoggerD("Enter");
238   // TODO implement
239   return LogAndCreateResult(ErrorCode::NOT_SUPPORTED_ERR,
240                             "Not implemented yet",
241                             ("Not implemented yet"));
242 }
243
244 PlatformResult NotificationManager::PlayLEDCustomEffect(
245     const picojson::object& args) {
246   LoggerD("Enter");
247
248   int timeOn = FromJson<double>(args, "timeOn");
249   int timeOff = FromJson<double>(args, "timeOff");
250   unsigned int color = FromJson<double>(args, "color");
251
252   auto& flags = FromJson<picojson::array>(args, "flags");
253   unsigned int platformFlags = 0;
254   for (auto flag : flags) {
255     std::string flagStr = JsonCast<std::string>(flag);
256     if (flagStr == "LED_CUSTOM_DEFAULT")
257       platformFlags |= LED_CUSTOM_DEFAULT;
258     else if (flagStr == "LED_CUSTOM_DUTY_ON")
259       platformFlags |= LED_CUSTOM_DUTY_ON;
260   }
261
262   int ret;
263   ret = device_led_play_custom(timeOn, timeOff, color, platformFlags);
264   if (ret != DEVICE_ERROR_NONE) {
265     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot play LED custom effect",
266                               ("Cannot play LED custom effect: ",ret));
267   }
268
269   return PlatformResult(ErrorCode::NO_ERROR);
270 }
271
272 PlatformResult NotificationManager::StopLEDCustomEffect() {
273   LoggerD("Enter");
274
275   int ret = device_led_stop_custom();
276   if (ret != DEVICE_ERROR_NONE) {
277     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Cannot stop LED custom effect",
278                               ("Cannot stop LED custom effect: ",ret));
279   }
280
281   return PlatformResult(ErrorCode::NO_ERROR);
282 }
283
284 common::PlatformResult NotificationManager::SaveTemplate(const picojson::object& args) {
285   LoggerD("Enter");
286   std::string name = FromJson<std::string>(args, "name");
287   notification_h noti_handle = nullptr;
288   int ret;
289   SCOPE_EXIT {
290     notification_free(noti_handle);
291   };
292
293   PlatformResult status = StatusNotification::GetNotiHandleFromJson(args, false, &noti_handle);
294
295   if (status.IsError()){
296     return status;
297   }
298
299   ret = notification_save_as_template(noti_handle, name.c_str());
300   if (ret != NOTIFICATION_ERROR_NONE) {
301     LoggerD("Error: %d (%s)", ret, get_error_message(ret));
302     if (ret == NOTIFICATION_ERROR_MAX_EXCEEDED) {
303       return LogAndCreateResult(ErrorCode::QUOTA_EXCEEDED_ERR,
304                                 "Maximum number of templates exceeded",
305                                 ("Maximum number of templates exceeded, error: %d", ret));
306     } else {
307       return LogAndCreateResult(ErrorCode::ABORT_ERR,
308                                 "Saving template failed",
309                                 ("Saving template failed, error: %d", ret));
310     }
311   }
312
313   return PlatformResult(ErrorCode::NO_ERROR);
314 }
315
316
317 common::PlatformResult NotificationManager::CreateFromTemplate(const picojson::object& args,
318                                                                picojson::object& out) {
319   LoggerD("Enter");
320   std::string name = FromJson<std::string>(args, "name");
321
322   notification_h noti_handle = nullptr;
323   noti_handle = notification_create_from_template(name.c_str());
324   if (!noti_handle) {
325     return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR,
326                               "The template with given name not found",
327                               ("The template with given name not found, handle is NULL"));
328   }
329
330   SCOPE_EXIT {
331     notification_free(noti_handle);
332   };
333
334   PlatformResult status = StatusNotification::ToJson(0, noti_handle, nullptr, &out);
335   if (status.IsError())
336   {
337     LoggerE("Failed: ToJson");
338     return status;
339   }
340   return PlatformResult(ErrorCode::NO_ERROR);
341 }
342
343 }  // namespace notification
344 }  // namespace extension