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::DeleteByAppId(string appId) {
119 EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "", appId);
120 list<Bundle> serialized_list {serialized};
121 impl_->sender_->Notify(info, serialized_list, util::GetAppId());
122 return info.GetRequestId();
125 int Manager::GetCount() const {
126 EventInfo info(EventInfo::Count, util::GetAppId(), "");
127 int num = impl_->sender_->RequestNumber(info);
131 int Manager::Hide(shared_ptr<item::AbstractItem> noti) {
132 (reinterpret_cast<IItemInfoInternal*>(noti->GetInfo().get()))->AddHideViewer(util::GetAppId());
133 return impl_->SendNotify(noti, EventInfo::Update);
136 unique_ptr<item::AbstractItem> Manager::FindByRootID(string id) {
137 EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
138 list<Bundle> result = impl_->sender_->Request(info);
139 if (result.size() == 0) {
140 LOGE("Fail to get noti");
141 return unique_ptr<item::AbstractItem>{};
143 Bundle b = result.front();
144 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
148 list<unique_ptr<item::AbstractItem>> Manager::Get(string channel) {
149 EventInfo info(EventInfo::Get, util::GetAppId(), channel);
150 list<Bundle> result = impl_->sender_->Request(info);
151 list<unique_ptr<item::AbstractItem>> gen_list;
152 for (auto& i : result) {
153 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
154 gen_list.push_back(move(gen_item));
159 int Manager::SendEvent(const IEventInfo& info,
160 shared_ptr<item::AbstractItem> noti) {
161 Bundle serialized = noti->Serialize();
162 Bundle serialized_info = info.Serialize();
163 list<Bundle> serialized_list {serialized};
164 impl_->sender_->Notify(info, serialized_list, noti->GetSenderAppId());
165 return info.GetRequestId();
168 list<Bundle> Manager::OnRequest(const IEventInfo& info) {
169 list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
170 list<Bundle> serialized_list;
171 for (auto& i : item_list) {
172 if (std::static_pointer_cast<IItemInfoInternal>(i->GetInfo())
173 ->CanReceive(impl_->receiver_group_))
174 serialized_list.push_back(i->Serialize());
176 return serialized_list;
179 int Manager::OnRequestNumber(const IEventInfo& info) {
180 return OnRequestNumberEvent(info);
183 void Manager::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
184 shared_ptr<AbstractItem> gen_item;
185 int type = info.GetEventType();
186 NotificationError error =
187 (static_cast<const IEventInfoInternal&>(info)).GetError();
188 if (error != ERROR_NONE) {
189 LOGE("Handling error event (%d)", error);
190 OnError(error, info.GetRequestId());
195 case EventInfo::Post: {
196 list<shared_ptr<item::AbstractItem>> added;
197 for (auto& i : serialized) {
198 gen_item = ItemInflator::Create(i);
199 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
200 ->CanReceive(impl_->receiver_group_))
201 added.emplace_back(gen_item);
203 if (added.size() > 0)
207 case EventInfo::Update: {
208 for (auto& i : serialized) {
209 gen_item = ItemInflator::Create(i);
210 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
211 ->CanReceive(impl_->receiver_group_))
212 OnUpdate(info, gen_item);
216 case EventInfo::Delete: {
217 for (auto& i : serialized) {
218 gen_item = ItemInflator::Create(i);
219 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
220 ->CanReceive(impl_->receiver_group_))
221 OnDelete(info, gen_item);
227 case EventInfo::Error:
229 case EventInfo::Count:
231 case EventInfo::DeleteAll:
232 OnDelete(info, shared_ptr<AbstractItem>(new NullItem()));
234 case EventInfo::Custom:
239 void Manager::OnAdd(const IEventInfo& info,
240 list<shared_ptr<item::AbstractItem>> addedItem) {
243 void Manager::OnUpdate(const IEventInfo& info,
244 shared_ptr<item::AbstractItem> updatedItem) {
247 void Manager::OnDelete(const IEventInfo& info,
248 shared_ptr<item::AbstractItem> deletedItem) {
251 void Manager::OnError(NotificationError error, int requestId) {
254 list<shared_ptr<item::AbstractItem>> Manager::OnRequestEvent(const IEventInfo& info) {
255 return list<shared_ptr<item::AbstractItem>>({});
258 int Manager::OnRequestNumberEvent(const IEventInfo& info) {
262 string Manager::GetPath() {
263 return NOTIFICATION_EX_MANAGER_OBJECT_PATH;
266 } // namespace notification