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::GetCount() const {
110 EventInfo info(EventInfo::Count, util::GetAppId(), "");
111 int num = impl_->sender_->RequestNumber(info);
115 int Manager::Hide(shared_ptr<item::AbstractItem> noti) {
116 (reinterpret_cast<IItemInfoInternal*>(noti->GetInfo().get()))->AddHideViewer(util::GetAppId());
117 return impl_->SendNotify(noti, EventInfo::Update);
120 unique_ptr<item::AbstractItem> Manager::FindByRootID(string id) {
121 EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
122 list<Bundle> result = impl_->sender_->Request(info);
123 if (result.size() == 0) {
124 LOGE("Fail to get noti");
125 return unique_ptr<item::AbstractItem>{};
127 Bundle b = result.front();
128 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
132 list<unique_ptr<item::AbstractItem>> Manager::Get(string channel) {
133 EventInfo info(EventInfo::Get, util::GetAppId(), channel);
134 list<Bundle> result = impl_->sender_->Request(info);
135 list<unique_ptr<item::AbstractItem>> gen_list;
136 for (auto& i : result) {
137 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
138 gen_list.push_back(move(gen_item));
143 int Manager::SendEvent(const IEventInfo& info,
144 shared_ptr<item::AbstractItem> noti) {
145 Bundle serialized = noti->Serialize();
146 Bundle serialized_info = info.Serialize();
147 list<Bundle> serialized_list {serialized};
148 impl_->sender_->Notify(info, serialized_list, noti->GetSenderAppId());
149 return info.GetRequestId();
152 list<Bundle> Manager::OnRequest(const IEventInfo& info) {
153 list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
154 list<Bundle> serialized_list;
155 for (auto& i : item_list) {
156 if (std::static_pointer_cast<IItemInfoInternal>(i->GetInfo())
157 ->CanReceive(impl_->receiver_group_))
158 serialized_list.push_back(i->Serialize());
160 return serialized_list;
163 int Manager::OnRequestNumber(const IEventInfo& info) {
164 return OnRequestNumberEvent(info);
167 void Manager::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
168 shared_ptr<AbstractItem> gen_item;
169 int type = info.GetEventType();
170 NotificationError error =
171 (static_cast<const IEventInfoInternal&>(info)).GetError();
172 if (error != ERROR_NONE) {
173 LOGE("Handling error event (%d)", error);
174 OnError(error, info.GetRequestId());
179 case EventInfo::Post: {
180 list<shared_ptr<item::AbstractItem>> added;
181 for (auto& i : serialized) {
182 gen_item = ItemInflator::Create(i);
183 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
184 ->CanReceive(impl_->receiver_group_))
185 added.emplace_back(gen_item);
187 if (added.size() > 0)
191 case EventInfo::Update: {
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 OnUpdate(info, gen_item);
200 case EventInfo::Delete: {
201 for (auto& i : serialized) {
202 gen_item = ItemInflator::Create(i);
203 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
204 ->CanReceive(impl_->receiver_group_))
205 OnDelete(info, gen_item);
211 case EventInfo::Error:
213 case EventInfo::Count:
215 case EventInfo::DeleteAll:
216 OnDelete(info, shared_ptr<AbstractItem>(new NullItem()));
218 case EventInfo::Custom:
223 void Manager::OnAdd(const IEventInfo& info,
224 list<shared_ptr<item::AbstractItem>> addedItem) {
227 void Manager::OnUpdate(const IEventInfo& info,
228 shared_ptr<item::AbstractItem> updatedItem) {
231 void Manager::OnDelete(const IEventInfo& info,
232 shared_ptr<item::AbstractItem> deletedItem) {
235 void Manager::OnError(NotificationError error, int requestId) {
238 list<shared_ptr<item::AbstractItem>> Manager::OnRequestEvent(const IEventInfo& info) {
239 return list<shared_ptr<item::AbstractItem>>({});
242 int Manager::OnRequestNumberEvent(const IEventInfo& info) {
246 string Manager::GetPath() {
247 return NOTIFICATION_EX_MANAGER_OBJECT_PATH;
250 } // namespace notification