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> serialized_list {};
57 EventInfo info(EventInfo::Unregister, util::GetAppId(), "", "");
58 sender_->SendMessage(info, serialized_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 if (DBusConnectionManager::GetInst().IsDataProviderMaster(util::GetAppId()))
72 list<Bundle> serialized_list {};
73 EventInfo info(EventInfo::Register, util::GetAppId(), receiver_group, "");
74 sender_->SendMessage(info, serialized_list, DATA_PROVIDER_MASTER_ID);
77 int Manager::Impl::SendNotify(shared_ptr<item::AbstractItem> noti,
78 IEventInfo::EventType type) {
79 Bundle serialized = noti->Serialize();
80 EventInfo info(type, util::GetAppId(), noti->GetChannel(), noti->GetId());
81 list<Bundle> serialized_list {serialized};
84 sender_->Notify(info, serialized_list, noti->GetSenderAppId());
85 return info.GetRequestId();
88 void Manager::SendError(const IEventInfo& info, NotificationError error) {
89 list<Bundle> serialized_list {};
90 IEventInfo& i = const_cast<IEventInfo&>(info);
91 static_cast<IEventInfoInternal&>(i).SetError(error);
92 static_cast<IEventInfoInternal&>(i).SetEventType(EventInfo::Error);
93 impl_->sender_->SendMessage(info, serialized_list, info.GetOwner());
96 int Manager::Update(shared_ptr<item::AbstractItem> noti, int& request_id) {
97 Bundle serialized = noti->Serialize();
98 EventInfo info(EventInfo::Update, util::GetAppId(), noti->GetChannel(), noti->GetId());
99 list<Bundle> serialized_list {serialized};
101 request_id = info.GetRequestId();
102 return impl_->sender_->SendMessage(info, serialized_list, noti->GetSenderAppId());
105 int Manager::Delete(shared_ptr<item::AbstractItem> noti, int& request_id) {
106 Bundle serialized = noti->Serialize();
107 EventInfo info(EventInfo::Delete, util::GetAppId(), noti->GetChannel(), noti->GetId());
108 list<Bundle> serialized_list {serialized};
110 request_id = info.GetRequestId();
111 return impl_->sender_->SendMessage(info, serialized_list, noti->GetSenderAppId());
114 int Manager::DeleteAll(int& request_id) {
116 EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "");
117 list<Bundle> serialized_list {serialized};
119 request_id = info.GetRequestId();
120 return impl_->sender_->SendMessage(info, serialized_list, util::GetAppId());
123 int Manager::DeleteByChannel(string channel, int& request_id) {
125 EventInfo info(EventInfo::DeleteAll, util::GetAppId(), channel);
126 list<Bundle> serialized_list {serialized};
128 request_id = info.GetRequestId();
129 return impl_->sender_->SendMessage(info, serialized_list, util::GetAppId());
132 int Manager::DeleteByAppId(string appId, int& request_id) {
134 EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "", appId);
135 list<Bundle> serialized_list {serialized};
137 request_id = info.GetRequestId();
138 return impl_->sender_->SendMessage(info, serialized_list, util::GetAppId());
141 int Manager::GetCount() const {
142 EventInfo info(EventInfo::Count, util::GetAppId(), "");
143 int num = impl_->sender_->RequestNumber(info);
147 int Manager::Hide(shared_ptr<item::AbstractItem> noti, int& request_id) {
148 (reinterpret_cast<IItemInfoInternal*>(noti->GetInfo().get()))->AddHideViewer(util::GetAppId());
150 EventInfo info(EventInfo::Update, util::GetAppId(), noti->GetChannel(), noti->GetId());
151 Bundle serialized = noti->Serialize();
152 list<Bundle> serialized_list {serialized};
154 request_id = info.GetRequestId();
155 return impl_->sender_->SendMessage(info, serialized_list, noti->GetSenderAppId());
158 unique_ptr<item::AbstractItem> Manager::FindByRootID(string id) {
159 EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
160 list<Bundle> result = impl_->sender_->Request(info);
161 if (result.size() == 0) {
162 LOGE("Fail to get noti");
163 return unique_ptr<item::AbstractItem>{};
165 Bundle b = result.front();
166 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
170 list<unique_ptr<item::AbstractItem>> Manager::Get(string channel) {
171 EventInfo info(EventInfo::Get, util::GetAppId(), channel);
172 list<Bundle> result = impl_->sender_->Request(info);
173 list<unique_ptr<item::AbstractItem>> gen_list;
174 for (auto& i : result) {
175 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
176 gen_list.push_back(move(gen_item));
181 int Manager::SendEvent(const IEventInfo& info,
182 shared_ptr<item::AbstractItem> noti) {
183 LOGI("manager-sendevent");
184 Bundle serialized = noti->Serialize();
185 Bundle serialized_info = info.Serialize();
186 list<Bundle> serialized_list {serialized};
187 impl_->sender_->Notify(info, serialized_list, noti->GetSenderAppId());
188 return info.GetRequestId();
191 int Manager::SendEvent(const IEventInfo& info,
192 list<shared_ptr<item::AbstractItem>> notiList) {
193 LOGI("manager-sendevent - list");
194 list<Bundle> serialized_list;
195 for (auto& i : notiList) {
196 Bundle b = i->Serialize();
197 serialized_list.push_back(b);
199 impl_->sender_->Notify(info, serialized_list);
200 return info.GetRequestId();
203 list<Bundle> Manager::OnRequest(const IEventInfo& info) {
204 list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
205 list<Bundle> serialized_list;
206 for (auto& i : item_list) {
207 if (std::static_pointer_cast<IItemInfoInternal>(i->GetInfo())
208 ->CanReceive(impl_->receiver_group_))
209 serialized_list.push_back(i->Serialize());
211 return serialized_list;
214 int Manager::OnRequestNumber(const IEventInfo& info) {
215 return OnRequestNumberEvent(info);
218 void Manager::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
219 shared_ptr<AbstractItem> gen_item;
220 int type = info.GetEventType();
221 NotificationError error =
222 (static_cast<const IEventInfoInternal&>(info)).GetError();
223 if (error != ERROR_NONE) {
224 LOGE("Handling error event (%d)", error);
225 OnError(error, info.GetRequestId());
230 case EventInfo::Post: {
231 list<shared_ptr<item::AbstractItem>> added;
232 for (auto& i : serialized) {
233 gen_item = ItemInflator::Create(i);
234 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
235 ->CanReceive(impl_->receiver_group_))
236 added.emplace_back(gen_item);
238 if (added.size() > 0)
242 case EventInfo::Update: {
243 list<shared_ptr<item::AbstractItem>> updated;
244 for (auto& i : serialized) {
245 gen_item = ItemInflator::Create(i);
246 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
247 ->CanReceive(impl_->receiver_group_))
248 updated.emplace_back(gen_item);
250 if (updated.size() > 0)
251 OnUpdate(info, updated);
254 case EventInfo::Delete: {
255 list<shared_ptr<item::AbstractItem>> deleted;
256 for (auto& i : serialized) {
257 gen_item = ItemInflator::Create(i);
258 if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
259 ->CanReceive(impl_->receiver_group_))
260 deleted.emplace_back(gen_item);
262 if (deleted.size() > 0)
263 OnDelete(info, deleted);
268 case EventInfo::Error:
270 case EventInfo::Count:
272 case EventInfo::DeleteAll: {
273 list<shared_ptr<item::AbstractItem>> deleted;
274 deleted.emplace_back(new NullItem());
275 OnDelete(info, deleted);
278 case EventInfo::Custom:
283 void Manager::OnAdd(const IEventInfo& info,
284 list<shared_ptr<item::AbstractItem>> addedList) {
287 void Manager::OnUpdate(const IEventInfo& info,
288 list<shared_ptr<item::AbstractItem>> updatedList) {
291 void Manager::OnDelete(const IEventInfo& info,
292 list<shared_ptr<item::AbstractItem>> deletedList) {
295 void Manager::OnError(NotificationError error, int requestId) {
298 list<shared_ptr<item::AbstractItem>> Manager::OnRequestEvent(const IEventInfo& info) {
299 return list<shared_ptr<item::AbstractItem>>({});
302 int Manager::OnRequestNumberEvent(const IEventInfo& info) {
306 string Manager::GetPath() {
307 return NOTIFICATION_EX_MANAGER_OBJECT_PATH;
310 } // namespace notification