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