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"
30 #include "notification-ex/null_item.h"
36 #define LOG_TAG "NOTIFICATION_EX"
38 #define MAX_PACKAGE_STR_SIZE 512
39 #define NOTIFICATION_EX_MANAGER_OBJECT_PATH "/org/tizen/notification_ex_manager"
42 using namespace tizen_base;
43 using namespace notification::item;
44 namespace notification {
46 Manager::Manager(unique_ptr<IEventSender> sender,
47 unique_ptr<IEventListener> listener, string receiver_group)
48 : impl_(new Impl(this, move(sender), move(listener), receiver_group)) {
50 Manager::~Manager() = default;
52 Manager::Impl::~Impl() {
53 listener_->UnRegisterObserver(parent_);
55 Manager::Impl::Impl(Manager* parent,
56 unique_ptr<IEventSender> sender,
57 unique_ptr<IEventListener> listener, string receiver_group)
58 : sender_(move(sender)), listener_(move(listener)),
59 receiver_group_(receiver_group),
62 listener_->RegisterObserver(parent_);
65 int Manager::Impl::SendNotify(shared_ptr<item::AbstractItem> noti,
66 IEventInfo::EventType type) {
67 Bundle serialized = noti->Serialize();
68 EventInfo info(type, util::GetAppId(), noti->GetChannel(), noti->GetId());
69 list<Bundle> serialized_list {serialized};
72 sender_->Notify(info, serialized_list, noti->GetSenderAppId());
73 return info.GetRequestId();
76 void Manager::SendError(const IEventInfo& info, NotificationError error) {
77 list<Bundle> serialized_list {};
78 IEventInfo& i = const_cast<IEventInfo&>(info);
79 static_cast<IEventInfoInternal&>(i).SetError(error);
80 static_cast<IEventInfoInternal&>(i).SetEventType(EventInfo::Error);
81 impl_->sender_->Notify(info, serialized_list, info.GetOwner());
84 int Manager::Update(shared_ptr<item::AbstractItem> noti) {
85 return impl_->SendNotify(noti, EventInfo::Update);
88 int Manager::Delete(shared_ptr<item::AbstractItem> noti) {
89 return impl_->SendNotify(noti, EventInfo::Delete);
92 int Manager::DeleteAll() {
94 EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "");
95 list<Bundle> serialized_list {serialized};
96 impl_->sender_->Notify(info, serialized_list, util::GetAppId());
97 return info.GetRequestId();
100 int Manager::GetCount() const {
101 EventInfo info(EventInfo::Count, util::GetAppId(), "");
102 int num = impl_->sender_->RequestNumber(info);
106 int Manager::Hide(shared_ptr<item::AbstractItem> noti) {
107 (reinterpret_cast<IItemInfoInternal*>(noti->GetInfo().get()))->AddHideViewer(util::GetAppId());
108 return impl_->SendNotify(noti, EventInfo::Update);
111 unique_ptr<item::AbstractItem> Manager::FindByRootID(string id) {
112 EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
113 list<Bundle> result = impl_->sender_->Request(info);
114 if (result.size() == 0) {
115 LOGE("Fail to get noti");
116 return unique_ptr<item::AbstractItem>{};
118 Bundle b = result.front();
119 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
120 return move(gen_item);
123 list<unique_ptr<item::AbstractItem>> Manager::Get(string channel) {
124 EventInfo info(EventInfo::Get, util::GetAppId(), channel);
125 list<Bundle> result = impl_->sender_->Request(info);
126 list<unique_ptr<item::AbstractItem>> gen_list;
127 for (auto& i : result) {
128 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
129 gen_list.push_back(move(gen_item));
131 return move(gen_list);
134 int Manager::SendEvent(const IEventInfo& info,
135 shared_ptr<item::AbstractItem> noti) {
136 Bundle serialized = noti->Serialize();
137 Bundle serialized_info = info.Serialize();
138 list<Bundle> serialized_list {serialized};
139 impl_->sender_->Notify(info, serialized_list, noti->GetSenderAppId());
140 return info.GetRequestId();
143 list<Bundle> Manager::OnRequest(const IEventInfo& info) {
144 list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
145 list<Bundle> serialized_list;
146 for (auto& i : item_list) {
147 if (std::static_pointer_cast<IItemInfoInternal>(i->GetInfo())
148 ->CanReceive(impl_->receiver_group_))
149 serialized_list.push_back(i->Serialize());
151 return serialized_list;
154 int Manager::OnRequestNumber(const IEventInfo& info) {
155 return OnRequestNumberEvent(info);
158 void Manager::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
159 shared_ptr<AbstractItem> gen_item;
160 int type = info.GetEventType();
161 NotificationError error =
162 (static_cast<const IEventInfoInternal&>(info)).GetError();
163 if (error != ERROR_NONE) {
164 LOGE("Handling error event (%d)", error);
165 OnError(error, info.GetRequestId());
170 case EventInfo::Post: {
171 list<shared_ptr<item::AbstractItem>> added;
172 for (auto& i : serialized) {
173 gen_item = ItemInflator::Create(i);
174 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
175 ->CanReceive(impl_->receiver_group_))
176 added.emplace_back(gen_item);
178 if (added.size() > 0)
182 case EventInfo::Update: {
183 for (auto& i : serialized) {
184 gen_item = ItemInflator::Create(i);
185 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
186 ->CanReceive(impl_->receiver_group_))
187 OnUpdate(info, gen_item);
191 case EventInfo::Delete: {
192 for (auto& i : serialized) {
193 gen_item = ItemInflator::Create(i);
194 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
195 ->CanReceive(impl_->receiver_group_))
196 OnDelete(info, gen_item);
202 case EventInfo::Error:
204 case EventInfo::Count:
206 case EventInfo::DeleteAll:
207 OnDelete(info, shared_ptr<AbstractItem>(new NullItem()));
209 case EventInfo::Custom:
214 void Manager::OnAdd(const IEventInfo& info,
215 list<shared_ptr<item::AbstractItem>> addedItem) {
218 void Manager::OnUpdate(const IEventInfo& info,
219 shared_ptr<item::AbstractItem> updatedItem) {
222 void Manager::OnDelete(const IEventInfo& info,
223 shared_ptr<item::AbstractItem> deletedItem) {
226 void Manager::OnError(NotificationError error, int requestId) {
229 list<shared_ptr<item::AbstractItem>> Manager::OnRequestEvent(const IEventInfo& info) {
230 return list<shared_ptr<item::AbstractItem>>({});
233 int Manager::OnRequestNumberEvent(const IEventInfo& info) {
237 string Manager::GetPath() {
238 return NOTIFICATION_EX_MANAGER_OBJECT_PATH;
241 } // namespace notification