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"
34 #define LOG_TAG "NOTIFICATION_EX"
35 #define MAX_PACKAGE_STR_SIZE 512
36 #define NOTIFICATION_EX_REPORTER_OBJECT_PATH "/org/tizen/notification_ex_reporter"
39 using namespace tizen_base;
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());
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 return impl_->SendNotify(noti, EventInfo::Post);
81 int Reporter::Post(std::list<std::shared_ptr<AbstractItem>> notiList) {
82 EventInfo info(EventInfo::Post, util::GetAppId(), "");
83 list<Bundle> serialized_list;
84 for (auto& i : notiList) {
85 Bundle b = i->Serialize();
86 serialized_list.push_back(b);
88 impl_->sender_->Notify(info, serialized_list);
89 return info.GetRequestId();
92 int Reporter::Update(std::shared_ptr<AbstractItem> noti) {
93 return impl_->SendNotify(noti, EventInfo::Update);
96 int Reporter::Delete(std::shared_ptr<AbstractItem> noti) {
97 return impl_->SendNotify(noti, EventInfo::Delete);
100 int Reporter::DeleteAll() {
105 std::unique_ptr<AbstractItem> Reporter::FindByRootID(std::string id) {
107 EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
108 list<Bundle> result = impl_->sender_->Request(info);
109 if (result.size() == 0) {
110 LOGE("Fail to get noti");
111 return unique_ptr<item::AbstractItem>{};
113 Bundle b = result.front();
114 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
115 return move(gen_item);
118 int Reporter::SendEvent(const IEventInfo& info,
119 shared_ptr<item::AbstractItem> noti) {
120 Bundle serialized = noti->Serialize();
121 list<Bundle> serialized_list {serialized};
122 impl_->sender_->Notify(info, serialized_list);
123 return info.GetRequestId();
126 void Reporter::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
127 NotificationError error =
128 (static_cast<const IEventInfoInternal&>(info)).GetError();
129 if (info.GetEventType() == EventInfo::Error) {
130 OnError(error, info.GetRequestId());
134 list<shared_ptr<item::AbstractItem>> item_list;
135 for (auto& i : serialized) {
136 shared_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
137 item_list.emplace_back(gen_item);
139 OnEvent(info, item_list);
142 list<shared_ptr<item::AbstractItem>> Reporter::OnRequestEvent(const IEventInfo& info) {
143 return list<shared_ptr<item::AbstractItem>>({});
146 list<Bundle> Reporter::OnRequest(const IEventInfo& info) {
147 list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
148 list<Bundle> serialized_list;
149 for (auto& i : item_list) {
150 serialized_list.push_back(i->Serialize());
152 return serialized_list;
155 void Reporter::OnEvent(
156 const IEventInfo& info, list<shared_ptr<item::AbstractItem>> notiList) {
159 void Reporter::OnError(NotificationError error, int requestId) {
162 string Reporter::GetPath() {
163 return NOTIFICATION_EX_REPORTER_OBJECT_PATH;
166 } // namespace notification