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.
23 #include "notification-ex/item_inflator.h"
24 #include "notification-ex/manager.h"
25 #include "notification-ex/manager_implementation.h"
26 #include "notification-ex/event_info_internal.h"
27 #include "notification-ex/dbus_connection_manager.h"
28 #include "notification-ex/ex_util.h"
29 #include "notification-ex/item_info_internal.h"
35 #define LOG_TAG "NOTIFICATION_EX"
37 #define MAX_PACKAGE_STR_SIZE 512
38 #define NOTIFICATION_EX_MANAGER_OBJECT_PATH "/org/tizen/notification_ex_manager"
41 using namespace notification::item;
42 namespace notification {
44 Manager::Manager(unique_ptr<IEventSender> sender,
45 unique_ptr<IEventListener> listener, string receiver_group)
46 : impl_(new Impl(this, move(sender), move(listener), receiver_group)) {
48 Manager::~Manager() = default;
50 Manager::Impl::~Impl() {
51 listener_->UnRegisterObserver(parent_);
53 Manager::Impl::Impl(Manager* parent,
54 unique_ptr<IEventSender> sender,
55 unique_ptr<IEventListener> listener, string receiver_group)
56 : sender_(move(sender)), listener_(move(listener)),
57 receiver_group_(receiver_group),
60 listener_->RegisterObserver(parent_);
63 int Manager::Impl::SendNotify(shared_ptr<item::AbstractItem> noti,
64 IEventInfo::EventType type) {
65 Bundle serialized = noti->Serialize();
66 EventInfo info(type, util::GetAppId(), noti->GetChannel());
67 list<Bundle> serialized_list {serialized};
70 sender_->Notify(info, serialized_list, noti->GetSenderAppId());
71 return info.GetRequestId();
74 void Manager::SendError(const IEventInfo& info, NotificationError error) {
75 list<Bundle> serialized_list {};
76 IEventInfo& i = const_cast<IEventInfo&>(info);
77 static_cast<IEventInfoInternal&>(i).SetError(error);
78 static_cast<IEventInfoInternal&>(i).SetEventType(EventInfo::Error);
79 impl_->sender_->Notify(info, serialized_list, info.GetOwner());
82 int Manager::Update(shared_ptr<item::AbstractItem> noti) {
83 return impl_->SendNotify(noti, EventInfo::Update);
86 int Manager::Delete(shared_ptr<item::AbstractItem> noti) {
87 return impl_->SendNotify(noti, EventInfo::Delete);
90 int Manager::DeleteAll() {
95 int Manager::GetCount() const {
100 int Manager::Hide(shared_ptr<item::AbstractItem> noti) {
101 ((IItemInfoInternal*)noti->GetInfo().get())->AddHideViewer(util::GetAppId());
102 return impl_->SendNotify(noti, EventInfo::Update);
105 unique_ptr<item::AbstractItem> Manager::FindByRootID(string id) {
106 EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
107 list<Bundle> result = impl_->sender_->Request(info);
108 if (result.size() == 0) {
109 LOGE("Fail to get noti");
110 return unique_ptr<item::AbstractItem>{};
112 Bundle b = result.front();
113 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
114 return move(gen_item);
117 list<unique_ptr<item::AbstractItem>> Manager::Get() {
118 EventInfo info(EventInfo::Get, util::GetAppId(), "");
119 list<Bundle> result = impl_->sender_->Request(info);
120 list<unique_ptr<item::AbstractItem>> gen_list;
121 for (auto& i : result) {
122 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
123 gen_list.push_back(move(gen_item));
125 return move(gen_list);
128 int Manager::SendEvent(const IEventInfo& info,
129 shared_ptr<item::AbstractItem> noti) {
130 Bundle serialized = noti->Serialize();
131 Bundle serialized_info = info.Serialize();
132 list<Bundle> serialized_list {serialized};
133 impl_->sender_->Notify(info, serialized_list, noti->GetSenderAppId());
134 return info.GetRequestId();
137 list<Bundle> Manager::OnRequest(const IEventInfo& info) {
138 list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
139 list<Bundle> serialized_list;
140 for (auto& i : item_list) {
141 if (std::static_pointer_cast<IItemInfoInternal>(i->GetInfo())
142 ->CanReceive(impl_->receiver_group_))
143 serialized_list.push_back(i->Serialize());
145 return serialized_list;
148 void Manager::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
149 shared_ptr<AbstractItem> gen_item;
150 int type = info.GetEventType();
151 NotificationError error =
152 (static_cast<const IEventInfoInternal&>(info)).GetError();
153 if (error != ERROR_NONE) {
154 LOGE("Handling error event (%d)", error);
155 OnError(error, info.GetRequestId());
160 case EventInfo::Post: {
161 list<shared_ptr<item::AbstractItem>> added;
162 for (auto& i : serialized) {
163 gen_item = ItemInflator::Create(i);
164 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
165 ->CanReceive(impl_->receiver_group_))
166 added.emplace_back(gen_item);
168 if (added.size() > 0)
172 case EventInfo::Update: {
173 for (auto& i : serialized) {
174 gen_item = ItemInflator::Create(i);
175 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
176 ->CanReceive(impl_->receiver_group_))
177 OnUpdate(info, gen_item);
181 case EventInfo::Delete: {
182 for (auto& i : serialized) {
183 gen_item = ItemInflator::Create(i);
184 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
185 ->CanReceive(impl_->receiver_group_))
186 OnDelete(info, gen_item);
192 case EventInfo::Error:
194 case EventInfo::Custom:
199 void Manager::OnAdd(const IEventInfo& info,
200 list<shared_ptr<item::AbstractItem>> addedItem) {
203 void Manager::OnUpdate(const IEventInfo& info,
204 shared_ptr<item::AbstractItem> updatedItem) {
207 void Manager::OnDelete(const IEventInfo& info,
208 shared_ptr<item::AbstractItem> deletedItem) {
211 void Manager::OnError(NotificationError error, int requestId) {
214 list<shared_ptr<item::AbstractItem>> Manager::OnRequestEvent(const IEventInfo& info) {
215 return list<shared_ptr<item::AbstractItem>>({});
218 string Manager::GetPath() {
219 return NOTIFICATION_EX_MANAGER_OBJECT_PATH;
222 } // namespace notification