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 tizen_base;
41 using namespace notification::item;
42 namespace notification {
45 unique_ptr<IEventSender> sender, unique_ptr<IEventListener> listener)
46 : impl_(new Impl(this, move(sender), move(listener))) {
48 Reporter::~Reporter() = default;
50 Reporter::Impl::~Impl() {
51 listener_->UnRegisterObserver(parent_);
53 Reporter::Impl::Impl(Reporter* parent,
54 unique_ptr<IEventSender> sender, unique_ptr<IEventListener> listener)
55 : sender_(move(sender)), listener_(move(listener)), parent_(parent) {
57 listener_->RegisterObserver(parent_);
60 int Reporter::Impl::SendNotify(shared_ptr<item::AbstractItem> noti,
61 IEventInfo::EventType type) {
62 Bundle serialized = noti->Serialize();
63 EventInfo info(type, util::GetAppId(), noti->GetChannel(), noti->GetId());
64 list<Bundle> serialized_list {serialized};
65 sender_->Notify(info, serialized_list);
66 return info.GetRequestId();
69 void Reporter::SendError(const IEventInfo& info, NotificationError error) {
70 list<Bundle> serialized_list {};
71 IEventInfo& i = const_cast<IEventInfo&>(info);
72 static_cast<IEventInfoInternal&>(i).SetError(error);
73 static_cast<IEventInfoInternal&>(i).SetEventType(EventInfo::Error);
74 impl_->sender_->Notify(info, serialized_list, info.GetOwner());
77 int Reporter::Post(std::shared_ptr<item::AbstractItem> noti) {
79 static_pointer_cast<IItemInfoInternal>(noti->GetInfo())->SetTime(time(NULL));
80 return impl_->SendNotify(noti, EventInfo::Post);
83 int Reporter::Post(std::list<std::shared_ptr<AbstractItem>> notiList) {
84 EventInfo info(EventInfo::Post, util::GetAppId(), "");
85 list<Bundle> serialized_list;
86 for (auto& i : notiList) {
87 static_pointer_cast<IItemInfoInternal>(i->GetInfo())->SetTime(time(NULL));
88 Bundle b = i->Serialize();
89 serialized_list.push_back(b);
91 impl_->sender_->Notify(info, serialized_list);
92 return info.GetRequestId();
95 int Reporter::Update(std::shared_ptr<AbstractItem> noti) {
96 static_pointer_cast<IItemInfoInternal>(noti->GetInfo())->SetTime(time(NULL));
97 return impl_->SendNotify(noti, EventInfo::Update);
100 int Reporter::Delete(std::shared_ptr<AbstractItem> noti) {
101 return impl_->SendNotify(noti, EventInfo::Delete);
104 int Reporter::DeleteAll() {
106 EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "");
107 list<Bundle> serialized_list {serialized};
108 impl_->sender_->Notify(info, serialized_list, util::GetAppId());
109 return info.GetRequestId();
112 std::unique_ptr<AbstractItem> Reporter::FindByRootID(std::string id) {
114 EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
115 list<Bundle> result = impl_->sender_->Request(info);
116 if (result.size() == 0) {
117 LOGE("Fail to get noti");
118 return unique_ptr<item::AbstractItem>{};
120 Bundle b = result.front();
121 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
122 return move(gen_item);
125 int Reporter::SendEvent(const IEventInfo& info,
126 shared_ptr<item::AbstractItem> noti) {
127 Bundle serialized = noti->Serialize();
128 list<Bundle> serialized_list {serialized};
129 impl_->sender_->Notify(info, serialized_list);
130 return info.GetRequestId();
133 int Reporter::SendEvent(const IEventInfo& info,
134 std::list<std::shared_ptr<item::AbstractItem>> notiList) {
135 list<Bundle> serialized_list;
136 for (auto& i : notiList) {
137 Bundle b = i->Serialize();
138 serialized_list.push_back(b);
140 impl_->sender_->Notify(info, serialized_list);
141 return info.GetRequestId();
144 void Reporter::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
145 NotificationError error =
146 (static_cast<const IEventInfoInternal&>(info)).GetError();
147 list<shared_ptr<item::AbstractItem>> item_list;
148 if (info.GetEventType() == EventInfo::Error) {
149 OnError(error, info.GetRequestId());
151 } else if (info.GetEventType() == EventInfo::DeleteAll) {
152 OnEvent(info, item_list);
156 for (auto& i : serialized) {
157 shared_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
158 item_list.emplace_back(gen_item);
160 OnEvent(info, item_list);
163 list<shared_ptr<item::AbstractItem>> Reporter::OnRequestEvent(const IEventInfo& info) {
164 return list<shared_ptr<item::AbstractItem>>({});
167 list<Bundle> Reporter::OnRequest(const IEventInfo& info) {
168 list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
169 list<Bundle> serialized_list;
170 for (auto& i : item_list) {
171 serialized_list.push_back(i->Serialize());
173 return serialized_list;
176 int Reporter::OnRequestNumber(const IEventInfo& info) {
180 void Reporter::OnEvent(
181 const IEventInfo& info, list<shared_ptr<item::AbstractItem>> notiList) {
184 void Reporter::OnError(NotificationError error, int requestId) {
187 string Reporter::GetPath() {
188 return NOTIFICATION_EX_REPORTER_OBJECT_PATH;
191 } // namespace notification