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"
30 #include "notification-ex/shared_file.h"
36 #define LOG_TAG "NOTIFICATION_EX"
37 #define MAX_PACKAGE_STR_SIZE 512
38 #define NOTIFICATION_EX_REPORTER_OBJECT_PATH "/org/tizen/notification_ex_reporter"
41 using namespace tizen_base;
42 using namespace notification::item;
43 namespace notification {
46 unique_ptr<IEventSender> sender, unique_ptr<IEventListener> listener)
47 : impl_(new Impl(this, move(sender), move(listener))) {
49 Reporter::~Reporter() = default;
51 Reporter::Impl::~Impl() {
52 listener_->UnRegisterObserver(parent_);
54 Reporter::Impl::Impl(Reporter* parent,
55 unique_ptr<IEventSender> sender, unique_ptr<IEventListener> listener)
56 : sender_(move(sender)), listener_(move(listener)), parent_(parent) {
58 listener_->RegisterObserver(parent_);
61 int Reporter::Impl::SendNotify(shared_ptr<item::AbstractItem> noti,
62 IEventInfo::EventType type) {
63 Bundle serialized = noti->Serialize();
64 EventInfo info(type, util::GetAppId(), noti->GetChannel(), noti->GetId());
65 list<Bundle> serialized_list {serialized};
66 sender_->Notify(info, serialized_list);
67 return info.GetRequestId();
70 void Reporter::SendError(const IEventInfo& info, NotificationError error) {
71 list<Bundle> serialized_list {};
72 IEventInfo& i = const_cast<IEventInfo&>(info);
73 static_cast<IEventInfoInternal&>(i).SetError(error);
74 static_cast<IEventInfoInternal&>(i).SetEventType(EventInfo::Error);
75 impl_->sender_->Notify(info, serialized_list, info.GetOwner());
78 int Reporter::Post(std::shared_ptr<item::AbstractItem> noti) {
80 static_pointer_cast<IItemInfoInternal>(noti->GetInfo())->SetTime(time(NULL));
81 SharedFile::CopyPrivateFile(noti);
83 return impl_->SendNotify(noti, EventInfo::Post);
86 int Reporter::Post(std::list<std::shared_ptr<AbstractItem>> notiList) {
87 EventInfo info(EventInfo::Post, util::GetAppId(), "");
88 list<Bundle> serialized_list;
89 for (auto& i : notiList) {
90 static_pointer_cast<IItemInfoInternal>(i->GetInfo())->SetTime(time(NULL));
91 Bundle b = i->Serialize();
92 serialized_list.push_back(b);
93 SharedFile::CopyPrivateFile(i);
95 impl_->sender_->Notify(info, serialized_list);
96 return info.GetRequestId();
99 int Reporter::Update(std::shared_ptr<AbstractItem> noti) {
100 static_pointer_cast<IItemInfoInternal>(noti->GetInfo())->SetTime(time(NULL));
101 SharedFile::CopyPrivateFile(noti);
103 return impl_->SendNotify(noti, EventInfo::Update);
106 int Reporter::Update(std::list<std::shared_ptr<AbstractItem>> notiList) {
107 EventInfo info(EventInfo::Update, util::GetAppId(), "");
108 list<Bundle> serialized_list;
109 for (auto& i : notiList) {
110 static_pointer_cast<IItemInfoInternal>(i->GetInfo())->SetTime(time(NULL));
111 Bundle b = i->Serialize();
112 serialized_list.push_back(b);
113 SharedFile::CopyPrivateFile(i);
115 impl_->sender_->Notify(info, serialized_list);
116 return info.GetRequestId();
119 int Reporter::Delete(std::shared_ptr<AbstractItem> noti) {
120 return impl_->SendNotify(noti, EventInfo::Delete);
123 int Reporter::Delete(std::list<std::shared_ptr<AbstractItem>> notiList) {
124 EventInfo info(EventInfo::Delete, util::GetAppId(), "");
125 list<Bundle> serialized_list;
126 for (auto& i : notiList) {
127 static_pointer_cast<IItemInfoInternal>(i->GetInfo())->SetTime(time(NULL));
128 Bundle b = i->Serialize();
129 serialized_list.push_back(b);
130 SharedFile::CopyPrivateFile(i);
132 impl_->sender_->Notify(info, serialized_list);
133 return info.GetRequestId();
136 int Reporter::DeleteAll() {
138 EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "");
139 list<Bundle> serialized_list {serialized};
140 impl_->sender_->Notify(info, serialized_list, util::GetAppId());
141 return info.GetRequestId();
144 int Reporter::DeleteByChannel(string channel) {
146 EventInfo info(EventInfo::DeleteAll, util::GetAppId(), channel);
147 list<Bundle> serialized_list {serialized};
148 impl_->sender_->Notify(info, serialized_list, util::GetAppId());
149 return info.GetRequestId();
152 std::unique_ptr<AbstractItem> Reporter::FindByRootID(std::string id) {
154 EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
155 list<Bundle> result = impl_->sender_->Request(info);
156 if (result.size() == 0) {
157 LOGE("Fail to get noti");
158 return unique_ptr<item::AbstractItem>{};
160 Bundle b = result.front();
161 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
165 list<unique_ptr<AbstractItem>> Reporter::FindByChannel(string channel) {
167 EventInfo info(EventInfo::Get, util::GetAppId(), channel, "");
168 list<Bundle> result = impl_->sender_->Request(info);
169 if (result.size() == 0) {
170 LOGE("Fail to get noti");
171 return list<unique_ptr<item::AbstractItem>>{};
174 list<unique_ptr<AbstractItem>> gen_item_list;
175 for (auto& i : result)
176 gen_item_list.push_back(ItemInflator::Create(i));
178 return gen_item_list;
181 list<unique_ptr<AbstractItem>> Reporter::FindAll() {
183 EventInfo info(EventInfo::Get, util::GetAppId(), "", "");
184 list<Bundle> result = impl_->sender_->Request(info);
185 if (result.size() == 0) {
186 LOGE("Fail to get noti");
187 return list<unique_ptr<item::AbstractItem>>{};
190 list<unique_ptr<AbstractItem>> gen_item_list;
191 for (auto& i : result)
192 gen_item_list.push_back(ItemInflator::Create(i));
194 return gen_item_list;
197 int Reporter::SendEvent(const IEventInfo& info,
198 std::list<std::shared_ptr<item::AbstractItem>> notiList) {
199 list<Bundle> serialized_list;
200 for (auto& i : notiList) {
201 Bundle b = i->Serialize();
202 serialized_list.push_back(b);
204 impl_->sender_->Notify(info, serialized_list);
205 return info.GetRequestId();
208 void Reporter::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
209 NotificationError error =
210 (static_cast<const IEventInfoInternal&>(info)).GetError();
211 list<shared_ptr<item::AbstractItem>> item_list;
212 if (info.GetEventType() == EventInfo::Error) {
213 OnError(error, info.GetRequestId());
215 } else if (info.GetEventType() == EventInfo::DeleteAll) {
216 OnEvent(info, item_list);
220 for (auto& i : serialized) {
221 shared_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
222 item_list.emplace_back(gen_item);
224 OnEvent(info, item_list);
227 list<shared_ptr<item::AbstractItem>> Reporter::OnRequestEvent(const IEventInfo& info) {
228 return list<shared_ptr<item::AbstractItem>>({});
231 list<Bundle> Reporter::OnRequest(const IEventInfo& info) {
232 list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
233 list<Bundle> serialized_list;
234 for (auto& i : item_list) {
235 serialized_list.push_back(i->Serialize());
237 return serialized_list;
240 int Reporter::OnRequestNumber(const IEventInfo& info) {
244 void Reporter::OnEvent(
245 const IEventInfo& info, list<shared_ptr<item::AbstractItem>> notiList) {
248 void Reporter::OnError(NotificationError error, int requestId) {
251 string Reporter::GetPath() {
252 return NOTIFICATION_EX_REPORTER_OBJECT_PATH;
255 void Reporter::OnRegister(const IEventInfo& info) {
258 } // namespace notification