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