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(), noti->GetId());
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() {
101 EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "");
102 list<Bundle> serialized_list {serialized};
103 impl_->sender_->Notify(info, serialized_list, util::GetAppId());
104 return info.GetRequestId();
107 std::unique_ptr<AbstractItem> Reporter::FindByRootID(std::string id) {
109 EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
110 list<Bundle> result = impl_->sender_->Request(info);
111 if (result.size() == 0) {
112 LOGE("Fail to get noti");
113 return unique_ptr<item::AbstractItem>{};
115 Bundle b = result.front();
116 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
117 return move(gen_item);
120 int Reporter::SendEvent(const IEventInfo& info,
121 shared_ptr<item::AbstractItem> noti) {
122 Bundle serialized = noti->Serialize();
123 list<Bundle> serialized_list {serialized};
124 impl_->sender_->Notify(info, serialized_list);
125 return info.GetRequestId();
128 void Reporter::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
129 NotificationError error =
130 (static_cast<const IEventInfoInternal&>(info)).GetError();
131 list<shared_ptr<item::AbstractItem>> item_list;
132 if (info.GetEventType() == EventInfo::Error) {
133 OnError(error, info.GetRequestId());
135 } else if (info.GetEventType() == EventInfo::DeleteAll) {
136 OnEvent(info, item_list);
140 for (auto& i : serialized) {
141 shared_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
142 item_list.emplace_back(gen_item);
144 OnEvent(info, item_list);
147 list<shared_ptr<item::AbstractItem>> Reporter::OnRequestEvent(const IEventInfo& info) {
148 return list<shared_ptr<item::AbstractItem>>({});
151 list<Bundle> Reporter::OnRequest(const IEventInfo& info) {
152 list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
153 list<Bundle> serialized_list;
154 for (auto& i : item_list) {
155 serialized_list.push_back(i->Serialize());
157 return serialized_list;
160 int Reporter::OnRequestNumber(const IEventInfo& info) {
164 void Reporter::OnEvent(
165 const IEventInfo& info, list<shared_ptr<item::AbstractItem>> notiList) {
168 void Reporter::OnError(NotificationError error, int requestId) {
171 string Reporter::GetPath() {
172 return NOTIFICATION_EX_REPORTER_OBJECT_PATH;
175 } // namespace notification