Release version 0.5.85
[platform/core/api/notification.git] / notification-ex / reporter.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <dlog.h>
18 #include <glib.h>
19 #include <unistd.h>
20
21 #include <list>
22
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"
31
32 #ifdef LOG_TAG
33 #undef LOG_TAG
34 #endif
35
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"
39
40 using namespace std;
41 using namespace tizen_base;
42 using namespace notification::item;
43 namespace notification {
44
45 Reporter::Reporter(
46     unique_ptr<IEventSender> sender, unique_ptr<IEventListener> listener)
47   : impl_(new Impl(this, move(sender), move(listener))) {
48 }
49 Reporter::~Reporter() = default;
50
51 Reporter::Impl::~Impl() {
52   listener_->UnRegisterObserver(parent_);
53 }
54 Reporter::Impl::Impl(Reporter* parent,
55     unique_ptr<IEventSender> sender, unique_ptr<IEventListener> listener)
56   : sender_(move(sender)), listener_(move(listener)), parent_(parent) {
57   LOGI("impl created");
58   listener_->RegisterObserver(parent_);
59 }
60
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();
68 }
69
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());
76 }
77
78 int Reporter::Post(std::shared_ptr<item::AbstractItem> noti, int& request_id) {
79   LOGI("Post noti");
80   static_pointer_cast<IItemInfoInternal>(noti->GetInfo())->SetTime(time(NULL));
81   Bundle serialized = noti->Serialize();
82
83   EventInfo info(EventInfo::Post, util::GetAppId(), noti->GetChannel(), noti->GetId());
84   list<Bundle> serialized_list {serialized};
85   SharedFile::CopyPrivateFile(noti);
86
87   request_id = info.GetRequestId();
88   return impl_->sender_->SendMessage(info, serialized_list);
89 }
90
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);
100   }
101
102   request_id = info.GetRequestId();
103   return impl_->sender_->SendMessage(info, serialized_list);
104 }
105
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();
109
110   EventInfo info(EventInfo::Update, util::GetAppId(), noti->GetChannel(), noti->GetId());
111   list<Bundle> serialized_list {serialized};
112   SharedFile::CopyPrivateFile(noti);
113
114   request_id = info.GetRequestId();
115   return impl_->sender_->SendMessage(info, serialized_list);
116 }
117
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);
126   }
127
128   request_id = info.GetRequestId();
129   return impl_->sender_->SendMessage(info, serialized_list);
130 }
131
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};
136
137   request_id = info.GetRequestId();
138   return impl_->sender_->SendMessage(info, serialized_list, noti->GetSenderAppId());
139 }
140
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);
149   }
150
151   request_id = info.GetRequestId();
152   return impl_->sender_->SendMessage(info, serialized_list);
153 }
154
155 int Reporter::DeleteAll(int& request_id) {
156   Bundle serialized;
157   EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "");
158   list<Bundle> serialized_list {serialized};
159
160   request_id = info.GetRequestId();
161   return impl_->sender_->SendMessage(info, serialized_list, util::GetAppId());
162 }
163
164 int Reporter::DeleteByChannel(string channel, int& request_id) {
165   Bundle serialized;
166   EventInfo info(EventInfo::DeleteAll, util::GetAppId(), channel);
167   list<Bundle> serialized_list {serialized};
168
169   request_id = info.GetRequestId();
170   return impl_->sender_->SendMessage(info, serialized_list, util::GetAppId());
171 }
172
173 std::unique_ptr<AbstractItem> Reporter::FindByRootID(std::string id) {
174   Bundle serialized;
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>{};
180   }
181   Bundle b = result.front();
182   unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
183   return gen_item;
184 }
185
186 list<unique_ptr<AbstractItem>> Reporter::FindByChannel(string channel) {
187   Bundle serialized;
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>>{};
193   }
194
195   list<unique_ptr<AbstractItem>> gen_item_list;
196   for (auto& i : result)
197     gen_item_list.push_back(ItemInflator::Create(i));
198
199   return gen_item_list;
200 }
201
202 list<unique_ptr<AbstractItem>> Reporter::FindAll() {
203   Bundle serialized;
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>>{};
209   }
210
211   list<unique_ptr<AbstractItem>> gen_item_list;
212   for (auto& i : result)
213     gen_item_list.push_back(ItemInflator::Create(i));
214
215   return gen_item_list;
216 }
217
218 int Reporter::GetCount(string channel) const {
219   EventInfo info(EventInfo::Count, util::GetAppId(), channel);
220   int num = impl_->sender_->RequestNumber(info);
221   return num;
222 }
223
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);
230   }
231   impl_->sender_->Notify(info, serialized_list);
232   return info.GetRequestId();
233 }
234
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());
241     return;
242   } else if (info.GetEventType() == EventInfo::DeleteAll) {
243     OnEvent(info, item_list);
244     return;
245   }
246
247   for (auto& i : serialized) {
248     shared_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
249     item_list.emplace_back(gen_item);
250   }
251   OnEvent(info, item_list);
252 }
253
254 list<shared_ptr<item::AbstractItem>> Reporter::OnRequestEvent(const IEventInfo& info) {
255   return list<shared_ptr<item::AbstractItem>>({});
256 }
257
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());
263   }
264   return serialized_list;
265 }
266
267 int Reporter::OnRequestNumber(const IEventInfo& info) {
268   return 0;
269 }
270
271 void Reporter::OnEvent(
272     const IEventInfo& info, list<shared_ptr<item::AbstractItem>> notiList) {
273 }
274
275 void Reporter::OnError(NotificationError error, int requestId) {
276 }
277
278 string Reporter::GetPath() {
279   return NOTIFICATION_EX_REPORTER_OBJECT_PATH;
280 }
281
282 void Reporter::OnRegister(const IEventInfo& info) {
283 }
284
285 }  // namespace notification