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 notification::item;
40 namespace notification {
43 unique_ptr<IEventSender> sender, unique_ptr<IEventListener> listener)
44 : impl_(new Impl(this, move(sender), move(listener))) {
46 Reporter::~Reporter() = default;
48 Reporter::Impl::~Impl() {
49 listener_->UnRegisterObserver(parent_);
51 Reporter::Impl::Impl(Reporter* parent,
52 unique_ptr<IEventSender> sender, unique_ptr<IEventListener> listener)
53 : sender_(move(sender)), listener_(move(listener)), parent_(parent) {
55 listener_->RegisterObserver(parent_);
58 int Reporter::Impl::SendNotify(shared_ptr<item::AbstractItem> noti,
59 IEventInfo::EventType type) {
60 Bundle serialized = noti->Serialize();
61 EventInfo info(type, util::GetAppId(), noti->GetChannel());
62 list<Bundle> serialized_list {serialized};
63 sender_->Notify(info, serialized_list);
64 return info.GetRequestId();
67 void Reporter::SendError(const IEventInfo& info, NotificationError error) {
68 list<Bundle> serialized_list {};
69 IEventInfo& i = const_cast<IEventInfo&>(info);
70 static_cast<IEventInfoInternal&>(i).SetError(error);
71 static_cast<IEventInfoInternal&>(i).SetEventType(EventInfo::Error);
72 impl_->sender_->Notify(info, serialized_list, info.GetOwner());
75 int Reporter::Post(std::shared_ptr<item::AbstractItem> noti) {
77 return impl_->SendNotify(noti, EventInfo::Post);
80 int Reporter::Post(std::list<std::shared_ptr<AbstractItem>> notiList) {
81 EventInfo info(EventInfo::Post, util::GetAppId(), "");
82 list<Bundle> serialized_list;
83 for (auto& i : notiList) {
84 Bundle b = i->Serialize();
85 serialized_list.push_back(b);
87 impl_->sender_->Notify(info, serialized_list);
88 return info.GetRequestId();
91 int Reporter::Update(std::shared_ptr<AbstractItem> noti) {
92 return impl_->SendNotify(noti, EventInfo::Update);
95 int Reporter::Delete(std::shared_ptr<AbstractItem> noti) {
96 return impl_->SendNotify(noti, EventInfo::Delete);
99 int Reporter::DeleteAll() {
104 std::shared_ptr<AbstractItem> Reporter::FindByRootID(std::string id) {
106 EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
107 list<Bundle> result = impl_->sender_->Request(info);
108 if (result.size() == 0) {
109 LOGE("Fail to get noti");
110 return shared_ptr<item::AbstractItem>({});
112 Bundle b = result.front();
113 shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
117 int Reporter::SendEvent(const IEventInfo& info,
118 shared_ptr<item::AbstractItem> noti) {
119 Bundle serialized = noti->Serialize();
120 list<Bundle> serialized_list {serialized};
121 impl_->sender_->Notify(info, serialized_list);
122 return info.GetRequestId();
125 void Reporter::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
126 NotificationError error =
127 (static_cast<const IEventInfoInternal&>(info)).GetError();
128 if (info.GetEventType() == EventInfo::Error) {
129 OnError(error, info.GetRequestId());
133 list<shared_ptr<item::AbstractItem>> item_list;
134 for (auto& i : serialized) {
135 shared_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
136 item_list.emplace_back(gen_item);
138 OnEvent(info, item_list);
141 list<shared_ptr<item::AbstractItem>> Reporter::OnRequestEvent(const IEventInfo& info) {
142 return list<shared_ptr<item::AbstractItem>>({});
145 list<Bundle> Reporter::OnRequest(const IEventInfo& info) {
146 list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
147 list<Bundle> serialized_list;
148 for (auto& i : item_list) {
149 serialized_list.push_back(i->Serialize());
151 return serialized_list;
154 void Reporter::OnEvent(
155 const IEventInfo& info, list<shared_ptr<item::AbstractItem>> notiList) {
158 void Reporter::OnError(NotificationError error, int requestId) {
161 string Reporter::GetPath() {
162 return NOTIFICATION_EX_REPORTER_OBJECT_PATH;
165 } // namespace notification