Fix group name for noti_ex
[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
41 using namespace std;
42 using namespace tizen_base;
43 using namespace notification::item;
44 namespace notification {
45
46 Manager::Manager(unique_ptr<IEventSender> sender,
47     unique_ptr<IEventListener> listener, string receiver_group)
48   : impl_(new Impl(this, move(sender), move(listener), receiver_group)) {
49 }
50 Manager::~Manager() = default;
51
52 Manager::Impl::~Impl() {
53   listener_->UnRegisterObserver(parent_);
54 }
55 Manager::Impl::Impl(Manager* parent,
56     unique_ptr<IEventSender> sender,
57     unique_ptr<IEventListener> listener, string receiver_group)
58   : sender_(move(sender)), listener_(move(listener)),
59     receiver_group_(receiver_group),
60     parent_(parent) {
61   LOGI("impl created");
62   listener_->RegisterObserver(parent_);
63 }
64
65 int Manager::Impl::SendNotify(shared_ptr<item::AbstractItem> noti,
66     IEventInfo::EventType type) {
67   Bundle serialized = noti->Serialize();
68   EventInfo info(type, util::GetAppId(), noti->GetChannel(), noti->GetId());
69   list<Bundle> serialized_list {serialized};
70
71   /* Reply to Sender */
72   sender_->Notify(info, serialized_list, noti->GetSenderAppId());
73   return info.GetRequestId();
74 }
75
76 void Manager::SendError(const IEventInfo& info, NotificationError error) {
77   list<Bundle> serialized_list {};
78   IEventInfo& i = const_cast<IEventInfo&>(info);
79   static_cast<IEventInfoInternal&>(i).SetError(error);
80   static_cast<IEventInfoInternal&>(i).SetEventType(EventInfo::Error);
81   impl_->sender_->Notify(info, serialized_list, info.GetOwner());
82 }
83
84 int Manager::Update(shared_ptr<item::AbstractItem> noti) {
85   return impl_->SendNotify(noti, EventInfo::Update);
86 }
87
88 int Manager::Delete(shared_ptr<item::AbstractItem> noti) {
89   return impl_->SendNotify(noti, EventInfo::Delete);
90 }
91
92 int Manager::DeleteAll() {
93   Bundle serialized;
94   EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "");
95   list<Bundle> serialized_list {serialized};
96   impl_->sender_->Notify(info, serialized_list, util::GetAppId());
97   return info.GetRequestId();
98 }
99
100 int Manager::GetCount() const {
101   EventInfo info(EventInfo::Count, util::GetAppId(), "");
102   int num = impl_->sender_->RequestNumber(info);
103   return num;
104 }
105
106 int Manager::Hide(shared_ptr<item::AbstractItem> noti) {
107   (reinterpret_cast<IItemInfoInternal*>(noti->GetInfo().get()))->AddHideViewer(util::GetAppId());
108   return impl_->SendNotify(noti, EventInfo::Update);
109 }
110
111 unique_ptr<item::AbstractItem> Manager::FindByRootID(string id) {
112   EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
113   list<Bundle> result = impl_->sender_->Request(info);
114   if (result.size() == 0) {
115     LOGE("Fail to get noti");
116     return unique_ptr<item::AbstractItem>{};
117   }
118   Bundle b = result.front();
119   unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
120   return move(gen_item);
121 }
122
123 list<unique_ptr<item::AbstractItem>> Manager::Get(string channel) {
124   EventInfo info(EventInfo::Get, util::GetAppId(), channel);
125   list<Bundle> result = impl_->sender_->Request(info);
126   list<unique_ptr<item::AbstractItem>> gen_list;
127   for (auto& i : result) {
128     unique_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
129     gen_list.push_back(move(gen_item));
130   }
131   return move(gen_list);
132 }
133
134 int Manager::SendEvent(const IEventInfo& info,
135     shared_ptr<item::AbstractItem> noti) {
136   Bundle serialized = noti->Serialize();
137   Bundle serialized_info = info.Serialize();
138   list<Bundle> serialized_list {serialized};
139   impl_->sender_->Notify(info, serialized_list, noti->GetSenderAppId());
140   return info.GetRequestId();
141 }
142
143 list<Bundle> Manager::OnRequest(const IEventInfo& info) {
144   list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
145   list<Bundle> serialized_list;
146   for (auto& i : item_list) {
147     if (std::static_pointer_cast<IItemInfoInternal>(i->GetInfo())
148           ->CanReceive(impl_->receiver_group_))
149       serialized_list.push_back(i->Serialize());
150   }
151   return serialized_list;
152 }
153
154 int Manager::OnRequestNumber(const IEventInfo& info) {
155   return OnRequestNumberEvent(info);
156 }
157
158 void Manager::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
159   shared_ptr<AbstractItem> gen_item;
160   int type = info.GetEventType();
161   NotificationError error =
162       (static_cast<const IEventInfoInternal&>(info)).GetError();
163   if (error != ERROR_NONE) {
164     LOGE("Handling error event (%d)", error);
165     OnError(error, info.GetRequestId());
166     return;
167   }
168
169   switch (type) {
170     case EventInfo::Post: {
171       list<shared_ptr<item::AbstractItem>> added;
172       for (auto& i : serialized) {
173         gen_item = ItemInflator::Create(i);
174         if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
175               ->CanReceive(impl_->receiver_group_))
176           added.emplace_back(gen_item);
177       }
178       if (added.size() > 0)
179         OnAdd(info, added);
180       break;
181     }
182     case EventInfo::Update: {
183       for (auto& i : serialized) {
184         gen_item = ItemInflator::Create(i);
185         if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
186               ->CanReceive(impl_->receiver_group_))
187           OnUpdate(info, gen_item);
188       }
189       break;
190     }
191     case EventInfo::Delete: {
192       for (auto& i : serialized) {
193         gen_item = ItemInflator::Create(i);
194         if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
195               ->CanReceive(impl_->receiver_group_))
196           OnDelete(info, gen_item);
197       }
198       break;
199     }
200     case EventInfo::Get:
201       break;
202     case EventInfo::Error:
203       break;
204     case EventInfo::Count:
205       break;
206     case EventInfo::DeleteAll:
207       OnDelete(info, shared_ptr<AbstractItem>(new NullItem()));
208       break;
209     case EventInfo::Custom:
210       break;
211   }
212 }
213
214 void Manager::OnAdd(const IEventInfo& info,
215       list<shared_ptr<item::AbstractItem>> addedItem) {
216 }
217
218 void Manager::OnUpdate(const IEventInfo& info,
219       shared_ptr<item::AbstractItem> updatedItem) {
220 }
221
222 void Manager::OnDelete(const IEventInfo& info,
223       shared_ptr<item::AbstractItem> deletedItem) {
224 }
225
226 void Manager::OnError(NotificationError error, int requestId) {
227 }
228
229 list<shared_ptr<item::AbstractItem>> Manager::OnRequestEvent(const IEventInfo& info) {
230   return list<shared_ptr<item::AbstractItem>>({});
231 }
232
233 int Manager::OnRequestNumberEvent(const IEventInfo& info) {
234   return 0;
235 }
236
237 string Manager::GetPath() {
238   return NOTIFICATION_EX_MANAGER_OBJECT_PATH;
239 }
240
241 }  // namespace notification