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_->SendMessage(info, serialized_list, info.GetOwner());
78 int Reporter::Post(std::shared_ptr<item::AbstractItem> noti, int& request_id) {
80 static_pointer_cast<IItemInfoInternal>(noti->GetInfo())->SetTime(time(NULL));
81 Bundle serialized = noti->Serialize();
83 EventInfo info(EventInfo::Post, util::GetAppId(), noti->GetChannel(), noti->GetId());
84 list<Bundle> serialized_list {serialized};
85 SharedFile::CopyPrivateFile(noti);
87 request_id = info.GetRequestId();
88 return impl_->sender_->SendMessage(info, serialized_list);
91 int Reporter::Post(std::list<std::shared_ptr<AbstractItem>> notiList, int& request_id) {
92 LOGI("Post noti list");
93 EventInfo info(EventInfo::Post, util::GetAppId(), "");
94 list<Bundle> serialized_list;
95 for (auto& i : notiList) {
96 static_pointer_cast<IItemInfoInternal>(i->GetInfo())->SetTime(time(NULL));
97 Bundle b = i->Serialize();
98 serialized_list.push_back(b);
99 SharedFile::CopyPrivateFile(i);
102 request_id = info.GetRequestId();
103 return impl_->sender_->SendMessage(info, serialized_list);
106 int Reporter::Update(std::shared_ptr<AbstractItem> noti, int& request_id) {
107 static_pointer_cast<IItemInfoInternal>(noti->GetInfo())->SetTime(time(NULL));
108 Bundle serialized = noti->Serialize();
110 EventInfo info(EventInfo::Update, util::GetAppId(), noti->GetChannel(), noti->GetId());
111 list<Bundle> serialized_list {serialized};
112 SharedFile::CopyPrivateFile(noti);
114 request_id = info.GetRequestId();
115 return impl_->sender_->SendMessage(info, serialized_list);
118 int Reporter::Update(std::list<std::shared_ptr<AbstractItem>> notiList, int& request_id) {
119 EventInfo info(EventInfo::Update, util::GetAppId(), "");
120 list<Bundle> serialized_list;
121 for (auto& i : notiList) {
122 static_pointer_cast<IItemInfoInternal>(i->GetInfo())->SetTime(time(NULL));
123 Bundle b = i->Serialize();
124 serialized_list.push_back(b);
125 SharedFile::CopyPrivateFile(i);
128 request_id = info.GetRequestId();
129 return impl_->sender_->SendMessage(info, serialized_list);
132 int Reporter::Delete(std::shared_ptr<AbstractItem> noti, int& request_id) {
133 Bundle serialized = noti->Serialize();
134 EventInfo info(EventInfo::Delete, util::GetAppId(), noti->GetChannel(), noti->GetId());
135 list<Bundle> serialized_list {serialized};
137 request_id = info.GetRequestId();
138 return impl_->sender_->SendMessage(info, serialized_list, noti->GetSenderAppId());
141 int Reporter::Delete(std::list<std::shared_ptr<AbstractItem>> notiList, int& request_id) {
142 EventInfo info(EventInfo::Delete, util::GetAppId(), "");
143 list<Bundle> serialized_list;
144 for (auto& i : notiList) {
145 static_pointer_cast<IItemInfoInternal>(i->GetInfo())->SetTime(time(NULL));
146 Bundle b = i->Serialize();
147 serialized_list.push_back(b);
148 SharedFile::CopyPrivateFile(i);
151 request_id = info.GetRequestId();
152 return impl_->sender_->SendMessage(info, serialized_list);
155 int Reporter::DeleteAll(int& request_id) {
157 EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "");
158 list<Bundle> serialized_list {serialized};
160 request_id = info.GetRequestId();
161 return impl_->sender_->SendMessage(info, serialized_list, util::GetAppId());
164 int Reporter::DeleteByChannel(string channel, int& request_id) {
166 EventInfo info(EventInfo::DeleteAll, util::GetAppId(), channel);
167 list<Bundle> serialized_list {serialized};
169 request_id = info.GetRequestId();
170 return impl_->sender_->SendMessage(info, serialized_list, util::GetAppId());
173 std::unique_ptr<AbstractItem> Reporter::FindByRootID(std::string id) {
175 EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
176 list<Bundle> result = impl_->sender_->Request(info);
177 if (result.size() == 0) {
178 LOGE("Fail to get noti");
179 return unique_ptr<item::AbstractItem>{};
181 Bundle b = result.front();
182 unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
186 list<unique_ptr<AbstractItem>> Reporter::FindByChannel(string channel) {
188 EventInfo info(EventInfo::Get, util::GetAppId(), channel, "");
189 list<Bundle> result = impl_->sender_->Request(info);
190 if (result.size() == 0) {
191 LOGE("Fail to get noti");
192 return list<unique_ptr<item::AbstractItem>>{};
195 list<unique_ptr<AbstractItem>> gen_item_list;
196 for (auto& i : result)
197 gen_item_list.push_back(ItemInflator::Create(i));
199 return gen_item_list;
202 list<unique_ptr<AbstractItem>> Reporter::FindAll() {
204 EventInfo info(EventInfo::Get, util::GetAppId(), "", "");
205 list<Bundle> result = impl_->sender_->Request(info);
206 if (result.size() == 0) {
207 LOGE("Fail to get noti");
208 return list<unique_ptr<item::AbstractItem>>{};
211 list<unique_ptr<AbstractItem>> gen_item_list;
212 for (auto& i : result)
213 gen_item_list.push_back(ItemInflator::Create(i));
215 return gen_item_list;
218 int Reporter::GetCount(string channel) const {
219 EventInfo info(EventInfo::Count, util::GetAppId(), channel);
220 int num = impl_->sender_->RequestNumber(info);
224 int Reporter::SendEvent(const IEventInfo& info,
225 std::list<std::shared_ptr<item::AbstractItem>> notiList) {
226 list<Bundle> serialized_list;
227 for (auto& i : notiList) {
228 Bundle b = i->Serialize();
229 serialized_list.push_back(b);
231 impl_->sender_->Notify(info, serialized_list);
232 return info.GetRequestId();
235 void Reporter::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
236 NotificationError error =
237 (static_cast<const IEventInfoInternal&>(info)).GetError();
238 list<shared_ptr<item::AbstractItem>> item_list;
239 if (info.GetEventType() == EventInfo::Error) {
240 OnError(error, info.GetRequestId());
242 } else if (info.GetEventType() == EventInfo::DeleteAll) {
243 OnEvent(info, item_list);
247 for (auto& i : serialized) {
248 shared_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
249 item_list.emplace_back(gen_item);
251 OnEvent(info, item_list);
254 list<shared_ptr<item::AbstractItem>> Reporter::OnRequestEvent(const IEventInfo& info) {
255 return list<shared_ptr<item::AbstractItem>>({});
258 list<Bundle> Reporter::OnRequest(const IEventInfo& info) {
259 list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
260 list<Bundle> serialized_list;
261 for (auto& i : item_list) {
262 serialized_list.push_back(i->Serialize());
264 return serialized_list;
267 int Reporter::OnRequestNumber(const IEventInfo& info) {
271 void Reporter::OnEvent(
272 const IEventInfo& info, list<shared_ptr<item::AbstractItem>> notiList) {
275 void Reporter::OnError(NotificationError error, int requestId) {
278 string Reporter::GetPath() {
279 return NOTIFICATION_EX_REPORTER_OBJECT_PATH;
282 void Reporter::OnRegister(const IEventInfo& info) {
285 } // namespace notification