Release version 0.5.85
[platform/core/api/notification.git] / notification-ex / manager.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/item_inflator.h"
24 #include "notification-ex/manager.h"
25 #include "notification-ex/manager_implementation.h"
26 #include "notification-ex/event_info_internal.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/null_item.h"
31
32 #ifdef LOG_TAG
33 #undef LOG_TAG
34 #endif
35
36 #define LOG_TAG "NOTIFICATION_EX"
37
38 #define MAX_PACKAGE_STR_SIZE 512
39 #define NOTIFICATION_EX_MANAGER_OBJECT_PATH "/org/tizen/notification_ex_manager"
40 #define DATA_PROVIDER_MASTER_ID "data-provider-master"
41
42 using namespace std;
43 using namespace tizen_base;
44 using namespace notification::item;
45 namespace notification {
46
47 Manager::Manager(unique_ptr<IEventSender> sender,
48     unique_ptr<IEventListener> listener, string receiver_group)
49   : impl_(new Impl(this, move(sender), move(listener), receiver_group)) {
50 }
51 Manager::~Manager() = default;
52
53 Manager::Impl::~Impl() {
54   listener_->UnRegisterObserver(parent_);
55
56   list<Bundle> serialized_list {};
57   EventInfo info(EventInfo::Unregister, util::GetAppId(), "", "");
58   sender_->SendMessage(info, serialized_list, DATA_PROVIDER_MASTER_ID);
59 }
60 Manager::Impl::Impl(Manager* parent,
61     unique_ptr<IEventSender> sender,
62     unique_ptr<IEventListener> listener, string receiver_group)
63   : sender_(move(sender)), listener_(move(listener)),
64     receiver_group_(receiver_group),
65     parent_(parent) {
66   LOGI("impl created");
67   listener_->RegisterObserver(parent_);
68
69   if (DBusConnectionManager::GetInst().IsDataProviderMaster(util::GetAppId()))
70     return;
71
72   list<Bundle> serialized_list {};
73   EventInfo info(EventInfo::Register, util::GetAppId(), receiver_group, "");
74   sender_->SendMessage(info, serialized_list, DATA_PROVIDER_MASTER_ID);
75 }
76
77 int Manager::Impl::SendNotify(shared_ptr<item::AbstractItem> noti,
78     IEventInfo::EventType type) {
79   Bundle serialized = noti->Serialize();
80   EventInfo info(type, util::GetAppId(), noti->GetChannel(), noti->GetId());
81   list<Bundle> serialized_list {serialized};
82
83   /* Reply to Sender */
84   sender_->Notify(info, serialized_list, noti->GetSenderAppId());
85   return info.GetRequestId();
86 }
87
88 void Manager::SendError(const IEventInfo& info, NotificationError error) {
89   list<Bundle> serialized_list {};
90   IEventInfo& i = const_cast<IEventInfo&>(info);
91   static_cast<IEventInfoInternal&>(i).SetError(error);
92   static_cast<IEventInfoInternal&>(i).SetEventType(EventInfo::Error);
93   impl_->sender_->SendMessage(info, serialized_list, info.GetOwner());
94 }
95
96 int Manager::Update(shared_ptr<item::AbstractItem> noti, int& request_id) {
97   Bundle serialized = noti->Serialize();
98   EventInfo info(EventInfo::Update, util::GetAppId(), noti->GetChannel(), noti->GetId());
99   list<Bundle> serialized_list {serialized};
100
101   request_id = info.GetRequestId();
102   return impl_->sender_->SendMessage(info, serialized_list, noti->GetSenderAppId());
103 }
104
105 int Manager::Delete(shared_ptr<item::AbstractItem> noti, int& request_id) {
106   Bundle serialized = noti->Serialize();
107   EventInfo info(EventInfo::Delete, util::GetAppId(), noti->GetChannel(), noti->GetId());
108   list<Bundle> serialized_list {serialized};
109
110   request_id = info.GetRequestId();
111   return impl_->sender_->SendMessage(info, serialized_list, noti->GetSenderAppId());
112 }
113
114 int Manager::DeleteAll(int& request_id) {
115   Bundle serialized;
116   EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "");
117   list<Bundle> serialized_list {serialized};
118
119   request_id = info.GetRequestId();
120   return impl_->sender_->SendMessage(info, serialized_list, util::GetAppId());
121 }
122
123 int Manager::DeleteByChannel(string channel, int& request_id) {
124   Bundle serialized;
125   EventInfo info(EventInfo::DeleteAll, util::GetAppId(), channel);
126   list<Bundle> serialized_list {serialized};
127
128   request_id = info.GetRequestId();
129   return impl_->sender_->SendMessage(info, serialized_list, util::GetAppId());
130 }
131
132 int Manager::DeleteByAppId(string appId, int& request_id) {
133   Bundle serialized;
134   EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "", appId);
135   list<Bundle> serialized_list {serialized};
136
137   request_id = info.GetRequestId();
138   return impl_->sender_->SendMessage(info, serialized_list, util::GetAppId());
139 }
140
141 int Manager::GetCount() const {
142   EventInfo info(EventInfo::Count, util::GetAppId(), "");
143   int num = impl_->sender_->RequestNumber(info);
144   return num;
145 }
146
147 int Manager::Hide(shared_ptr<item::AbstractItem> noti, int& request_id) {
148   (reinterpret_cast<IItemInfoInternal*>(noti->GetInfo().get()))->AddHideViewer(util::GetAppId());
149
150   EventInfo info(EventInfo::Update, util::GetAppId(), noti->GetChannel(), noti->GetId());
151   Bundle serialized = noti->Serialize();
152   list<Bundle> serialized_list {serialized};
153
154   request_id = info.GetRequestId();
155   return impl_->sender_->SendMessage(info, serialized_list, noti->GetSenderAppId());
156 }
157
158 unique_ptr<item::AbstractItem> Manager::FindByRootID(string id) {
159   EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
160   list<Bundle> result = impl_->sender_->Request(info);
161   if (result.size() == 0) {
162     LOGE("Fail to get noti");
163     return unique_ptr<item::AbstractItem>{};
164   }
165   Bundle b = result.front();
166   unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
167   return gen_item;
168 }
169
170 list<unique_ptr<item::AbstractItem>> Manager::Get(string channel) {
171   EventInfo info(EventInfo::Get, util::GetAppId(), channel);
172   list<Bundle> result = impl_->sender_->Request(info);
173   list<unique_ptr<item::AbstractItem>> gen_list;
174   for (auto& i : result) {
175     unique_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
176     gen_list.push_back(move(gen_item));
177   }
178   return gen_list;
179 }
180
181 int Manager::SendEvent(const IEventInfo& info,
182     shared_ptr<item::AbstractItem> noti) {
183   LOGI("manager-sendevent");
184   Bundle serialized = noti->Serialize();
185   Bundle serialized_info = info.Serialize();
186   list<Bundle> serialized_list {serialized};
187   impl_->sender_->Notify(info, serialized_list, noti->GetSenderAppId());
188   return info.GetRequestId();
189 }
190
191 int Manager::SendEvent(const IEventInfo& info,
192     list<shared_ptr<item::AbstractItem>> notiList) {
193   LOGI("manager-sendevent - list");
194   list<Bundle> serialized_list;
195   for (auto& i : notiList) {
196     Bundle b = i->Serialize();
197     serialized_list.push_back(b);
198   }
199   impl_->sender_->Notify(info, serialized_list);
200   return info.GetRequestId();
201 }
202
203 list<Bundle> Manager::OnRequest(const IEventInfo& info) {
204   list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
205   list<Bundle> serialized_list;
206   for (auto& i : item_list) {
207     if (std::static_pointer_cast<IItemInfoInternal>(i->GetInfo())
208           ->CanReceive(impl_->receiver_group_))
209       serialized_list.push_back(i->Serialize());
210   }
211   return serialized_list;
212 }
213
214 int Manager::OnRequestNumber(const IEventInfo& info) {
215   return OnRequestNumberEvent(info);
216 }
217
218 void Manager::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
219   shared_ptr<AbstractItem> gen_item;
220   int type = info.GetEventType();
221   NotificationError error =
222       (static_cast<const IEventInfoInternal&>(info)).GetError();
223   if (error != ERROR_NONE) {
224     LOGE("Handling error event (%d)", error);
225     OnError(error, info.GetRequestId());
226     return;
227   }
228
229   switch (type) {
230     case EventInfo::Post: {
231       list<shared_ptr<item::AbstractItem>> added;
232       for (auto& i : serialized) {
233         gen_item = ItemInflator::Create(i);
234         if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
235               ->CanReceive(impl_->receiver_group_))
236           added.emplace_back(gen_item);
237       }
238       if (added.size() > 0)
239         OnAdd(info, added);
240       break;
241     }
242     case EventInfo::Update: {
243       list<shared_ptr<item::AbstractItem>> updated;
244       for (auto& i : serialized) {
245         gen_item = ItemInflator::Create(i);
246         if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
247               ->CanReceive(impl_->receiver_group_))
248           updated.emplace_back(gen_item);
249       }
250       if (updated.size() > 0)
251         OnUpdate(info, updated);
252       break;
253     }
254     case EventInfo::Delete: {
255       list<shared_ptr<item::AbstractItem>> deleted;
256       for (auto& i : serialized) {
257         gen_item = ItemInflator::Create(i);
258         if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
259               ->CanReceive(impl_->receiver_group_))
260           deleted.emplace_back(gen_item);
261       }
262       if (deleted.size() > 0)
263         OnDelete(info, deleted);
264       break;
265     }
266     case EventInfo::Get:
267       break;
268     case EventInfo::Error:
269       break;
270     case EventInfo::Count:
271       break;
272     case EventInfo::DeleteAll: {
273       list<shared_ptr<item::AbstractItem>> deleted;
274       deleted.emplace_back(new NullItem());
275       OnDelete(info, deleted);
276       break;
277     }
278     case EventInfo::Custom:
279       break;
280   }
281 }
282
283 void Manager::OnAdd(const IEventInfo& info,
284       list<shared_ptr<item::AbstractItem>> addedList) {
285 }
286
287 void Manager::OnUpdate(const IEventInfo& info,
288       list<shared_ptr<item::AbstractItem>> updatedList) {
289 }
290
291 void Manager::OnDelete(const IEventInfo& info,
292       list<shared_ptr<item::AbstractItem>> deletedList) {
293 }
294
295 void Manager::OnError(NotificationError error, int requestId) {
296 }
297
298 list<shared_ptr<item::AbstractItem>> Manager::OnRequestEvent(const IEventInfo& info) {
299   return list<shared_ptr<item::AbstractItem>>({});
300 }
301
302 int Manager::OnRequestNumberEvent(const IEventInfo& info) {
303   return 0;
304 }
305
306 string Manager::GetPath() {
307   return NOTIFICATION_EX_MANAGER_OBJECT_PATH;
308 }
309
310 }  // namespace notification