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"
40 #define DATA_PROVIDER_MASTER_ID "data-provider-master"
43 using namespace tizen_base;
44 using namespace notification::item;
45 namespace notification {
47 Manager::Manager(unique_ptr<IEventSender> sender,
48 unique_ptr<IEventListener> listener, string receiver_group)
49 : impl_(new Impl(this, move(sender), move(listener), receiver_group)) {
51 Manager::~Manager() = default;
53 Manager::Impl::~Impl() {
54 listener_->UnRegisterObserver(parent_);
56 list<Bundle> dummy_list {};
57 EventInfo info(EventInfo::Unregister, util::GetAppId(), "", "");
58 sender_->Notify(info, dummy_list, DATA_PROVIDER_MASTER_ID);
60 Manager::Impl::Impl(Manager* parent,
61 unique_ptr<IEventSender> sender,
62 unique_ptr<IEventListener> listener, string receiver_group)
63 : sender_(move(sender)), listener_(move(listener)),
64 receiver_group_(receiver_group),
67 listener_->RegisterObserver(parent_);
69 list<Bundle> serialized_list {};
70 EventInfo info(EventInfo::Register, util::GetAppId(), receiver_group, "");
71 sender_->Notify(info, serialized_list, DATA_PROVIDER_MASTER_ID);
74 int Manager::Impl::SendNotify(shared_ptr<item::AbstractItem> noti,
75 IEventInfo::EventType type) {
76 Bundle serialized = noti->Serialize();
77 EventInfo info(type, util::GetAppId(), noti->GetChannel(), noti->GetId());
78 list<Bundle> serialized_list {serialized};
81 sender_->Notify(info, serialized_list, noti->GetSenderAppId());
82 return info.GetRequestId();
85 void Manager::SendError(const IEventInfo& info, NotificationError error) {
86 list<Bundle> serialized_list {};
87 IEventInfo& i = const_cast<IEventInfo&>(info);
88 static_cast<IEventInfoInternal&>(i).SetError(error);
89 static_cast<IEventInfoInternal&>(i).SetEventType(EventInfo::Error);
90 impl_->sender_->Notify(info, serialized_list, info.GetOwner());
93 int Manager::Update(shared_ptr<item::AbstractItem> noti) {
94 return impl_->SendNotify(noti, EventInfo::Update);
97 int Manager::Delete(shared_ptr<item::AbstractItem> noti) {
98 return impl_->SendNotify(noti, EventInfo::Delete);
101 int Manager::DeleteAll() {
103 EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "");
104 list<Bundle> serialized_list {serialized};
105 impl_->sender_->Notify(info, serialized_list, util::GetAppId());
106 return info.GetRequestId();
109 int Manager::DeleteByChannel(string channel) {
111 EventInfo info(EventInfo::DeleteAll, util::GetAppId(), channel);
112 list<Bundle> serialized_list {serialized};
113 impl_->sender_->Notify(info, serialized_list, util::GetAppId());
114 return info.GetRequestId();
117 int Manager::GetCount() const {
118 EventInfo info(EventInfo::Count, util::GetAppId(), "");
119 int num = impl_->sender_->RequestNumber(info);
123 int Manager::Hide(shared_ptr<item::AbstractItem> noti) {
124 (reinterpret_cast<IItemInfoInternal*>(noti->GetInfo().get()))->AddHideViewer(util::GetAppId());
125 return impl_->SendNotify(noti, EventInfo::Update);
128 unique_ptr<item::AbstractItem> Manager::FindByRootID(string id) {
129 EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
130 list<Bundle> result = impl_->sender_->Request(info);
131 if (result.size() == 0) {
132 LOGE("Fail to get noti");
133 return unique_ptr<item::AbstractItem>{};
135 Bundle b = result.front();
136 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
140 list<unique_ptr<item::AbstractItem>> Manager::Get(string channel) {
141 EventInfo info(EventInfo::Get, util::GetAppId(), channel);
142 list<Bundle> result = impl_->sender_->Request(info);
143 list<unique_ptr<item::AbstractItem>> gen_list;
144 for (auto& i : result) {
145 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
146 gen_list.push_back(move(gen_item));
151 int Manager::SendEvent(const IEventInfo& info,
152 shared_ptr<item::AbstractItem> noti) {
153 Bundle serialized = noti->Serialize();
154 Bundle serialized_info = info.Serialize();
155 list<Bundle> serialized_list {serialized};
156 impl_->sender_->Notify(info, serialized_list, noti->GetSenderAppId());
157 return info.GetRequestId();
160 list<Bundle> Manager::OnRequest(const IEventInfo& info) {
161 list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
162 list<Bundle> serialized_list;
163 for (auto& i : item_list) {
164 if (std::static_pointer_cast<IItemInfoInternal>(i->GetInfo())
165 ->CanReceive(impl_->receiver_group_))
166 serialized_list.push_back(i->Serialize());
168 return serialized_list;
171 int Manager::OnRequestNumber(const IEventInfo& info) {
172 return OnRequestNumberEvent(info);
175 void Manager::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
176 shared_ptr<AbstractItem> gen_item;
177 int type = info.GetEventType();
178 NotificationError error =
179 (static_cast<const IEventInfoInternal&>(info)).GetError();
180 if (error != ERROR_NONE) {
181 LOGE("Handling error event (%d)", error);
182 OnError(error, info.GetRequestId());
187 case EventInfo::Post: {
188 list<shared_ptr<item::AbstractItem>> added;
189 for (auto& i : serialized) {
190 gen_item = ItemInflator::Create(i);
191 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
192 ->CanReceive(impl_->receiver_group_))
193 added.emplace_back(gen_item);
195 if (added.size() > 0)
199 case EventInfo::Update: {
200 for (auto& i : serialized) {
201 gen_item = ItemInflator::Create(i);
202 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
203 ->CanReceive(impl_->receiver_group_))
204 OnUpdate(info, gen_item);
208 case EventInfo::Delete: {
209 for (auto& i : serialized) {
210 gen_item = ItemInflator::Create(i);
211 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
212 ->CanReceive(impl_->receiver_group_))
213 OnDelete(info, gen_item);
219 case EventInfo::Error:
221 case EventInfo::Count:
223 case EventInfo::DeleteAll:
224 OnDelete(info, shared_ptr<AbstractItem>(new NullItem()));
226 case EventInfo::Custom:
231 void Manager::OnAdd(const IEventInfo& info,
232 list<shared_ptr<item::AbstractItem>> addedItem) {
235 void Manager::OnUpdate(const IEventInfo& info,
236 shared_ptr<item::AbstractItem> updatedItem) {
239 void Manager::OnDelete(const IEventInfo& info,
240 shared_ptr<item::AbstractItem> deletedItem) {
243 void Manager::OnError(NotificationError error, int requestId) {
246 list<shared_ptr<item::AbstractItem>> Manager::OnRequestEvent(const IEventInfo& info) {
247 return list<shared_ptr<item::AbstractItem>>({});
250 int Manager::OnRequestNumberEvent(const IEventInfo& info) {
254 string Manager::GetPath() {
255 return NOTIFICATION_EX_MANAGER_OBJECT_PATH;
258 } // namespace notification