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/reporter.h"
24 #include "notification-ex/reporter_implementation.h"
25 #include "notification-ex/event_info_internal.h"
26 #include "notification-ex/item_inflator.h"
27 #include "notification-ex/dbus_connection_manager.h"
28 #include "notification-ex/ex_util.h"
29 #include "notification-ex/item_info_internal.h"
35 #define LOG_TAG "NOTIFICATION_EX"
36 #define MAX_PACKAGE_STR_SIZE 512
37 #define NOTIFICATION_EX_REPORTER_OBJECT_PATH "/org/tizen/notification_ex_reporter"
40 using namespace notification::item;
41 namespace notification {
44 unique_ptr<IEventSender> sender, unique_ptr<IEventListener> listener)
45 : impl_(new Impl(this, move(sender), move(listener))) {
47 Reporter::~Reporter() = default;
49 Reporter::Impl::~Impl() {
50 listener_->UnRegisterObserver(parent_);
52 Reporter::Impl::Impl(Reporter* parent,
53 unique_ptr<IEventSender> sender, unique_ptr<IEventListener> listener)
54 : sender_(move(sender)), listener_(move(listener)), parent_(parent) {
56 listener_->RegisterObserver(parent_);
59 int Reporter::Impl::SendNotify(shared_ptr<item::AbstractItem> noti,
60 IEventInfo::EventType type) {
61 Bundle serialized = noti->Serialize();
62 EventInfo info(type, util::GetAppId(), noti->GetChannel(), noti->GetId());
63 list<Bundle> serialized_list {serialized};
64 sender_->Notify(info, serialized_list);
65 return info.GetRequestId();
68 void Reporter::SendError(const IEventInfo& info, NotificationError error) {
69 list<Bundle> serialized_list {};
70 IEventInfo& i = const_cast<IEventInfo&>(info);
71 static_cast<IEventInfoInternal&>(i).SetError(error);
72 static_cast<IEventInfoInternal&>(i).SetEventType(EventInfo::Error);
73 impl_->sender_->Notify(info, serialized_list, info.GetOwner());
76 int Reporter::Post(std::shared_ptr<item::AbstractItem> noti) {
78 static_pointer_cast<IItemInfoInternal>(noti->GetInfo())->SetTime(time(NULL));
79 return impl_->SendNotify(noti, EventInfo::Post);
82 int Reporter::Post(std::list<std::shared_ptr<AbstractItem>> notiList) {
83 EventInfo info(EventInfo::Post, util::GetAppId(), "");
84 list<Bundle> serialized_list;
85 for (auto& i : notiList) {
86 static_pointer_cast<IItemInfoInternal>(i->GetInfo())->SetTime(time(NULL));
87 Bundle b = i->Serialize();
88 serialized_list.push_back(b);
90 impl_->sender_->Notify(info, serialized_list);
91 return info.GetRequestId();
94 int Reporter::Update(std::shared_ptr<AbstractItem> noti) {
95 static_pointer_cast<IItemInfoInternal>(noti->GetInfo())->SetTime(time(NULL));
96 return impl_->SendNotify(noti, EventInfo::Update);
99 int Reporter::Delete(std::shared_ptr<AbstractItem> noti) {
100 return impl_->SendNotify(noti, EventInfo::Delete);
103 int Reporter::DeleteAll() {
105 EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "");
106 list<Bundle> serialized_list {serialized};
107 impl_->sender_->Notify(info, serialized_list, util::GetAppId());
108 return info.GetRequestId();
111 std::unique_ptr<AbstractItem> Reporter::FindByRootID(std::string id) {
113 EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
114 list<Bundle> result = impl_->sender_->Request(info);
115 if (result.size() == 0) {
116 LOGE("Fail to get noti");
117 return unique_ptr<item::AbstractItem>{};
119 Bundle b = result.front();
120 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
121 return move(gen_item);
124 int Reporter::SendEvent(const IEventInfo& info,
125 shared_ptr<item::AbstractItem> noti) {
126 Bundle serialized = noti->Serialize();
127 list<Bundle> serialized_list {serialized};
128 impl_->sender_->Notify(info, serialized_list);
129 return info.GetRequestId();
132 int Reporter::SendEvent(const IEventInfo& info,
133 std::list<std::shared_ptr<item::AbstractItem>> notiList) {
134 list<Bundle> serialized_list;
135 for (auto& i : notiList) {
136 Bundle b = i->Serialize();
137 serialized_list.push_back(b);
139 impl_->sender_->Notify(info, serialized_list);
140 return info.GetRequestId();
143 void Reporter::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
144 NotificationError error =
145 (static_cast<const IEventInfoInternal&>(info)).GetError();
146 list<shared_ptr<item::AbstractItem>> item_list;
147 if (info.GetEventType() == EventInfo::Error) {
148 OnError(error, info.GetRequestId());
150 } else if (info.GetEventType() == EventInfo::DeleteAll) {
151 OnEvent(info, item_list);
155 for (auto& i : serialized) {
156 shared_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
157 item_list.emplace_back(gen_item);
159 OnEvent(info, item_list);
162 list<shared_ptr<item::AbstractItem>> Reporter::OnRequestEvent(const IEventInfo& info) {
163 return list<shared_ptr<item::AbstractItem>>({});
166 list<Bundle> Reporter::OnRequest(const IEventInfo& info) {
167 list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
168 list<Bundle> serialized_list;
169 for (auto& i : item_list) {
170 serialized_list.push_back(i->Serialize());
172 return serialized_list;
175 int Reporter::OnRequestNumber(const IEventInfo& info) {
179 void Reporter::OnEvent(
180 const IEventInfo& info, list<shared_ptr<item::AbstractItem>> notiList) {
183 void Reporter::OnError(NotificationError error, int requestId) {
186 string Reporter::GetPath() {
187 return NOTIFICATION_EX_REPORTER_OBJECT_PATH;
190 } // namespace notification