2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <notification_type.h>
21 #include <notification_viewer.h>
28 #include <notification-ex/manager.h>
29 #include <notification-ex/reporter.h>
30 #include <notification-ex/dbus_sender.h>
31 #include <notification-ex/dbus_event_listener.h>
32 #include <notification-ex/dbus_connection_manager.h>
33 #include <notification-ex/db_manager.h>
34 #include <notification-ex/common.h>
35 #include <notification-ex/exception.h>
36 #include <notification-ex/item_info_internal.h>
37 #include <notification-ex/button_item.h>
38 #include <notification-ex/ex_util.h>
39 #include <notification-ex/ievent_info_internal.h>
40 #include <notification-ex/iitem_info_internal.h>
43 #include "notification_ex_service.h"
46 using namespace notification;
47 using namespace notification::item;
51 DPMFacade(unique_ptr<Reporter> reporter, unique_ptr<Manager> manager)
52 : reporter_(move(reporter)), manager_(move(manager)) {
53 DBManager::InitializeDB();
54 DBManager::InitializeData();
55 hide_map_ = DBManager::GetHideMap();
58 void DelegateReporterEvent(const IEventInfo& info, shared_ptr<item::AbstractItem> item) {
59 reporter_->SendEvent(info, item);
62 void DelegateReporterEvent(const IEventInfo& info,
63 std::list<std::shared_ptr<item::AbstractItem>> itemList) {
64 reporter_->SendEvent(info, itemList);
67 void DelegateManagerEvent(const IEventInfo& info, shared_ptr<item::AbstractItem> noti) {
68 manager_->SendEvent(info, noti);
71 void LaunchDefaultViewer(list<shared_ptr<item::AbstractItem>> item,
72 notification_op_type_e status) {
78 priv_id = NOTIFICATION_PRIV_ID_NONE;
80 priv_id = static_pointer_cast<IItemInfoInternal>(
81 item.front()->GetInfo())->GetPrivateId();
83 LaunchDefaultViewer(priv_id, status,
84 static_pointer_cast<IItemInfoInternal>(
85 item.front()->GetInfo())->GetUid());
87 void LaunchDefaultViewer(shared_ptr<item::AbstractItem> item,
88 notification_op_type_e status) {
90 static_pointer_cast<IItemInfoInternal>(item->GetInfo())->GetPrivateId(),
92 static_pointer_cast<IItemInfoInternal>(item->GetInfo())->GetUid());
95 void LaunchDefaultViewer(int64_t privId, notification_op_type_e status,
97 int ret = notification_launch_default_viewer(
98 static_cast<int>(privId), status, uid);
99 if (ret != ERROR_NONE)
100 ERR("Failed to launch default viewer [%d]", ret);
103 unique_ptr<Reporter> reporter_;
104 unique_ptr<Manager> manager_;
105 map<string, string> hide_map_;
108 static DPMFacade* facade_;
110 class DPMReporter : public Reporter {
112 void OnUpdate(const IEventInfo& info,
113 shared_ptr<item::AbstractItem> updatedItem) {
117 /* check Manager::Hide() */
118 ret = UpdateHideApp(updatedItem);
119 if (ret == ERROR_NONE)
122 if (ret != ERROR_NOT_EXIST_ID) {
123 SendError(info, static_cast<NotificationError>(ret));
127 ret = DBManager::UpdateNotification(updatedItem);
128 if (ret == ERROR_NONE) {
130 facade_->DelegateManagerEvent(info, updatedItem);
132 /* noti to viewers */
133 facade_->DelegateReporterEvent(info, updatedItem);
135 SendError(info, static_cast<NotificationError>(ret));
139 void DoDelete(const IEventInfo& info,
140 shared_ptr<item::AbstractItem> deletedItem) {
141 int ret = DBManager::DeleteNotification(deletedItem);
142 if (ret == ERROR_NONE) {
144 facade_->DelegateManagerEvent(info, deletedItem);
146 /* noti to viewers */
147 facade_->DelegateReporterEvent(info, deletedItem);
149 SendError(info, static_cast<NotificationError>(ret));
153 void OnDelete(const IEventInfo& info,
154 shared_ptr<item::AbstractItem> deletedItem) {
156 if (info.GetEventType() == IEventInfo::EventType::DeleteAll) {
157 list<shared_ptr<item::AbstractItem>> noti_list =
158 DBManager::GetNotificationList(((const IEventInfoInternal&)info).GetUid());
159 for (auto& i : noti_list) {
163 DoDelete(info, deletedItem);
167 void OnEvent(const IEventInfo& info,
168 list<shared_ptr<item::AbstractItem>> noti_list) override {
169 LOGI("Event received (%d) !!", (int)info.GetEventType());
170 int type = info.GetEventType();
171 if (type == IEventInfo::EventType::DeleteAll) {
172 OnDelete(info, nullptr);
176 for (auto& i : noti_list) {
178 case IEventInfo::EventType::Update:
181 case IEventInfo::EventType::Delete:
190 list<shared_ptr<item::AbstractItem>> OnRequestEvent(const IEventInfo& info) override {
191 DBG("Get report !!! %s", info.GetItemId().c_str());
192 return DBManager::GetNotificationList(
193 ((const IEventInfoInternal&)info).GetUid(),
194 ((const IEventInfoInternal&)info).GetChannel().c_str());
197 int OnRequestNumber(const IEventInfo& info) override {
198 DBG("Get count !!! %s", info.GetItemId().c_str());
199 return DBManager::GetNotificationList(
200 ((const IEventInfoInternal&)info).GetUid()).size();
203 int UpdateHideApp(shared_ptr<item::AbstractItem> updatedItem) {
206 int uid = static_pointer_cast<IItemInfoInternal>(updatedItem->GetInfo())->GetUid();
207 DBG("try update hide app_id(%s) sender(%s) uid(%s)",
208 updatedItem->GetId().c_str(),
209 updatedItem->GetSenderAppId().c_str(), string(to_string(uid)).c_str());
210 string map_key = updatedItem->GetId() + updatedItem->GetSenderAppId() + string(to_string(uid));
211 list<string> updated_hide_list = static_pointer_cast<IItemInfoInternal>(updatedItem->GetInfo())->GetHideViewerList();
213 /* Check new hide app */
214 auto noti = facade_->hide_map_.find(map_key);
215 if (noti == facade_->hide_map_.end()) {
216 if (updated_hide_list.size() > 0) {
217 for (auto& i : updated_hide_list)
218 hide_list += i + ";";
220 ret = DBManager::UpdateHideList(updatedItem, hide_list);
221 if (ret != ERROR_NONE)
224 facade_->hide_map_[map_key] = hide_list;
227 DBG("Empty hide viewer list");
228 return ERROR_NOT_EXIST_ID;
232 /* Check new hide app */
233 hide_list = noti->second;
234 for (auto& i : updated_hide_list) {
235 if (hide_list.find(i + ";") == string::npos) {
236 string new_hide_list = hide_list + i + ";";
237 ret = DBManager::UpdateHideList(updatedItem, new_hide_list);
238 if (ret != ERROR_NONE)
241 facade_->hide_map_[map_key] = new_hide_list;
246 return ERROR_NOT_EXIST_ID;
250 DPMReporter(std::unique_ptr<IEventSender> sender,
251 std::unique_ptr<IEventListener> listener)
252 : Reporter(move(sender), move(listener)) {
256 class DPMManager : public Manager {
258 void UpdateHideApp(shared_ptr<item::AbstractItem> updatedItem) {
260 int uid = static_pointer_cast<IItemInfoInternal>(updatedItem->GetInfo())->GetUid();
261 string map_key = updatedItem->GetId() + updatedItem->GetSenderAppId() + string(to_string(uid));
262 list<string> updated_hide_list =
263 static_pointer_cast<item::IItemInfoInternal>(updatedItem->GetInfo())->GetHideViewerList();
265 auto noti = facade_->hide_map_.find(map_key);
266 if (noti == facade_->hide_map_.end())
269 /* hide app list synchronization */
270 hide_list = noti->second;
271 istringstream stream(hide_list);
274 while (getline(stream, hide_app_id, ';')) {
275 list<string>::iterator iter = std::find(updated_hide_list.begin(),
276 updated_hide_list.end(), hide_app_id);
277 if (iter == updated_hide_list.end())
278 static_pointer_cast<item::IItemInfoInternal>(updatedItem->GetInfo())->AddHideViewer(hide_app_id);
282 void OnAdd(const IEventInfo& info,
283 list<shared_ptr<item::AbstractItem>> addedItem) override {
287 ret = DBManager::InsertNotification(addedItem);
288 if (ret == ERROR_NONE) {
289 facade_->DelegateReporterEvent(info, addedItem);
290 facade_->LaunchDefaultViewer(addedItem, NOTIFICATION_OP_INSERT);
292 DBG("SendError !!!");
293 SendError(info, static_cast<NotificationError>(ret));
297 void OnUpdate(const IEventInfo& info,
298 shared_ptr<item::AbstractItem> updatedItem) override {
302 UpdateHideApp(updatedItem);
304 ret = DBManager::UpdateNotification(updatedItem);
305 if (ret == ERROR_NONE) {
306 facade_->DelegateReporterEvent(info, updatedItem);
307 facade_->LaunchDefaultViewer(updatedItem, NOTIFICATION_OP_UPDATE);
309 SendError(info, static_cast<NotificationError>(ret));
313 int DoDelete(const IEventInfo& info,
314 shared_ptr<item::AbstractItem> deletedItem) {
315 int ret = DBManager::DeleteNotification(deletedItem);
316 if (ret == ERROR_NONE) {
317 facade_->DelegateReporterEvent(info, deletedItem);
319 SendError(info, static_cast<NotificationError>(ret));
324 void OnDelete(const IEventInfo& info,
325 shared_ptr<item::AbstractItem> deletedItem) override {
327 if (info.GetEventType() == IEventInfo::EventType::DeleteAll) {
328 list<shared_ptr<item::AbstractItem>> noti_list =
329 DBManager::GetNotificationList(info.GetOwner(),
330 ((const IEventInfoInternal&)info).GetUid());
331 for (auto& i : noti_list) {
334 facade_->LaunchDefaultViewer(noti_list, NOTIFICATION_OP_DELETE_ALL);
336 int ret = DoDelete(info, deletedItem);
337 if (ret == ERROR_NONE)
338 facade_->LaunchDefaultViewer(deletedItem, NOTIFICATION_OP_DELETE);
342 list<shared_ptr<item::AbstractItem>> OnRequestEvent(const IEventInfo& info) override {
343 DBG("Get !!! %s", info.GetOwner().c_str());
344 if (info.GetItemId().empty()) {
346 return DBManager::GetNotificationList(info.GetOwner(),
347 ((const IEventInfoInternal&)info).GetUid());
350 return DBManager::GetNotificationList(info.GetOwner(), info.GetItemId(),
351 ((const IEventInfoInternal&)info).GetUid());
356 DPMManager(std::unique_ptr<IEventSender> sender,
357 std::unique_ptr<IEventListener> listener)
358 : Manager(move(sender), move(listener)) {
362 HAPI int notification_ex_service_init() {
363 facade_ = new DPMFacade(
364 unique_ptr<Reporter>(
366 unique_ptr<DBusSender>(new DBusSender(Manager::GetPath())),
367 unique_ptr<DBusEventListener>(new DBusEventListener(Reporter::GetPath())))),
370 unique_ptr<DBusSender>(new DBusSender(Reporter::GetPath())),
371 unique_ptr<DBusEventListener>(new DBusEventListener(Manager::GetPath())))));
373 notification_init_default_viewer();
378 HAPI int notification_ex_service_fini() {
383 HAPI GDBusConnection* notification_ex_service_get_gdbus_connection() {
384 return DBusConnectionManager::GetInst().GetConnection();