Merge branch 'tizen' into tizen_5.5
[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_->Notify(info, serialized_list, info.GetOwner());
76 }
77
78 int Reporter::Post(std::shared_ptr<item::AbstractItem> noti) {
79   LOGI("Post noti");
80   static_pointer_cast<IItemInfoInternal>(noti->GetInfo())->SetTime(time(NULL));
81   SharedFile* shared_file = new SharedFile();
82   shared_file->CopyPrivateFile(noti);
83   delete shared_file;
84
85   return impl_->SendNotify(noti, EventInfo::Post);
86 }
87
88 int Reporter::Post(std::list<std::shared_ptr<AbstractItem>> notiList) {
89   EventInfo info(EventInfo::Post, util::GetAppId(), "");
90   list<Bundle> serialized_list;
91   for (auto& i : notiList) {
92     static_pointer_cast<IItemInfoInternal>(i->GetInfo())->SetTime(time(NULL));
93     Bundle b = i->Serialize();
94     serialized_list.push_back(b);
95
96     SharedFile* shared_file = new SharedFile();
97     shared_file->CopyPrivateFile(i);
98     delete shared_file;
99   }
100   impl_->sender_->Notify(info, serialized_list);
101   return info.GetRequestId();
102 }
103
104 int Reporter::Update(std::shared_ptr<AbstractItem> noti) {
105   static_pointer_cast<IItemInfoInternal>(noti->GetInfo())->SetTime(time(NULL));
106   SharedFile* shared_file = new SharedFile();
107   shared_file->CopyPrivateFile(noti);
108   delete shared_file;
109
110   return impl_->SendNotify(noti, EventInfo::Update);
111 }
112
113 int Reporter::Delete(std::shared_ptr<AbstractItem> noti) {
114   return impl_->SendNotify(noti, EventInfo::Delete);
115 }
116
117 int Reporter::DeleteAll() {
118   Bundle serialized;
119   EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "");
120   list<Bundle> serialized_list {serialized};
121   impl_->sender_->Notify(info, serialized_list, util::GetAppId());
122   return info.GetRequestId();
123 }
124
125 int Reporter::DeleteByChannel(string channel) {
126   Bundle serialized;
127   EventInfo info(EventInfo::DeleteAll, util::GetAppId(), channel);
128   list<Bundle> serialized_list {serialized};
129   impl_->sender_->Notify(info, serialized_list, util::GetAppId());
130   return info.GetRequestId();
131 }
132
133 std::unique_ptr<AbstractItem> Reporter::FindByRootID(std::string id) {
134   Bundle serialized;
135   EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
136   list<Bundle> result = impl_->sender_->Request(info);
137   if (result.size() == 0) {
138     LOGE("Fail to get noti");
139     return unique_ptr<item::AbstractItem>{};
140   }
141   Bundle b = result.front();
142   unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
143   return gen_item;
144 }
145
146 list<unique_ptr<AbstractItem>> Reporter::FindByChannel(string channel) {
147   Bundle serialized;
148   EventInfo info(EventInfo::Get, util::GetAppId(), channel, "");
149   list<Bundle> result = impl_->sender_->Request(info);
150   if (result.size() == 0) {
151     LOGE("Fail to get noti");
152     return list<unique_ptr<item::AbstractItem>>{};
153   }
154
155   list<unique_ptr<AbstractItem>> gen_item_list;
156   for (auto& i : result)
157     gen_item_list.push_back(ItemInflator::Create(i));
158
159   return gen_item_list;
160 }
161
162 list<unique_ptr<AbstractItem>> Reporter::FindAll() {
163   Bundle serialized;
164   EventInfo info(EventInfo::Get, util::GetAppId(), "", "");
165   list<Bundle> result = impl_->sender_->Request(info);
166   if (result.size() == 0) {
167     LOGE("Fail to get noti");
168     return list<unique_ptr<item::AbstractItem>>{};
169   }
170
171   list<unique_ptr<AbstractItem>> gen_item_list;
172   for (auto& i : result)
173     gen_item_list.push_back(ItemInflator::Create(i));
174
175   return gen_item_list;
176 }
177
178 int Reporter::SendEvent(const IEventInfo& info,
179     shared_ptr<item::AbstractItem> noti) {
180   Bundle serialized = noti->Serialize();
181   list<Bundle> serialized_list {serialized};
182   impl_->sender_->Notify(info, serialized_list);
183   return info.GetRequestId();
184 }
185
186 int Reporter::SendEvent(const IEventInfo& info,
187     std::list<std::shared_ptr<item::AbstractItem>> notiList) {
188   list<Bundle> serialized_list;
189   for (auto& i : notiList) {
190     Bundle b = i->Serialize();
191     serialized_list.push_back(b);
192   }
193   impl_->sender_->Notify(info, serialized_list);
194   return info.GetRequestId();
195 }
196
197 void Reporter::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
198   NotificationError error =
199       (static_cast<const IEventInfoInternal&>(info)).GetError();
200   list<shared_ptr<item::AbstractItem>> item_list;
201   if (info.GetEventType() == EventInfo::Error) {
202     OnError(error, info.GetRequestId());
203     return;
204   } else if (info.GetEventType() == EventInfo::DeleteAll) {
205     OnEvent(info, item_list);
206     return;
207   }
208
209   for (auto& i : serialized) {
210     shared_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
211     item_list.emplace_back(gen_item);
212   }
213   OnEvent(info, item_list);
214 }
215
216 list<shared_ptr<item::AbstractItem>> Reporter::OnRequestEvent(const IEventInfo& info) {
217   return list<shared_ptr<item::AbstractItem>>({});
218 }
219
220 list<Bundle> Reporter::OnRequest(const IEventInfo& info) {
221   list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
222   list<Bundle> serialized_list;
223   for (auto& i : item_list) {
224     serialized_list.push_back(i->Serialize());
225   }
226   return serialized_list;
227 }
228
229 int Reporter::OnRequestNumber(const IEventInfo& info) {
230   return 0;
231 }
232
233 void Reporter::OnEvent(
234     const IEventInfo& info, list<shared_ptr<item::AbstractItem>> notiList) {
235 }
236
237 void Reporter::OnError(NotificationError error, int requestId) {
238 }
239
240 string Reporter::GetPath() {
241   return NOTIFICATION_EX_REPORTER_OBJECT_PATH;
242 }
243
244 void Reporter::OnRegister(const IEventInfo& info) {
245 }
246
247 }  // namespace notification