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