Fix group name for noti_ex
[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
31 #ifdef LOG_TAG
32 #undef LOG_TAG
33 #endif
34
35 #define LOG_TAG "NOTIFICATION_EX"
36 #define MAX_PACKAGE_STR_SIZE 512
37 #define NOTIFICATION_EX_REPORTER_OBJECT_PATH "/org/tizen/notification_ex_reporter"
38
39 using namespace std;
40 using namespace tizen_base;
41 using namespace notification::item;
42 namespace notification {
43
44 Reporter::Reporter(
45     unique_ptr<IEventSender> sender, unique_ptr<IEventListener> listener)
46   : impl_(new Impl(this, move(sender), move(listener))) {
47 }
48 Reporter::~Reporter() = default;
49
50 Reporter::Impl::~Impl() {
51   listener_->UnRegisterObserver(parent_);
52 }
53 Reporter::Impl::Impl(Reporter* parent,
54     unique_ptr<IEventSender> sender, unique_ptr<IEventListener> listener)
55   : sender_(move(sender)), listener_(move(listener)), parent_(parent) {
56   LOGI("impl created");
57   listener_->RegisterObserver(parent_);
58 }
59
60 int Reporter::Impl::SendNotify(shared_ptr<item::AbstractItem> noti,
61     IEventInfo::EventType type) {
62   Bundle serialized = noti->Serialize();
63   EventInfo info(type, util::GetAppId(), noti->GetChannel(), noti->GetId());
64   list<Bundle> serialized_list {serialized};
65   sender_->Notify(info, serialized_list);
66   return info.GetRequestId();
67 }
68
69 void Reporter::SendError(const IEventInfo& info, NotificationError error) {
70   list<Bundle> serialized_list {};
71   IEventInfo& i = const_cast<IEventInfo&>(info);
72   static_cast<IEventInfoInternal&>(i).SetError(error);
73   static_cast<IEventInfoInternal&>(i).SetEventType(EventInfo::Error);
74   impl_->sender_->Notify(info, serialized_list, info.GetOwner());
75 }
76
77 int Reporter::Post(std::shared_ptr<item::AbstractItem> noti) {
78   LOGI("Post noti");
79   static_pointer_cast<IItemInfoInternal>(noti->GetInfo())->SetTime(time(NULL));
80   return impl_->SendNotify(noti, EventInfo::Post);
81 }
82
83 int Reporter::Post(std::list<std::shared_ptr<AbstractItem>> notiList) {
84   EventInfo info(EventInfo::Post, util::GetAppId(), "");
85   list<Bundle> serialized_list;
86   for (auto& i : notiList) {
87     static_pointer_cast<IItemInfoInternal>(i->GetInfo())->SetTime(time(NULL));
88     Bundle b = i->Serialize();
89     serialized_list.push_back(b);
90   }
91   impl_->sender_->Notify(info, serialized_list);
92   return info.GetRequestId();
93 }
94
95 int Reporter::Update(std::shared_ptr<AbstractItem> noti) {
96   static_pointer_cast<IItemInfoInternal>(noti->GetInfo())->SetTime(time(NULL));
97   return impl_->SendNotify(noti, EventInfo::Update);
98 }
99
100 int Reporter::Delete(std::shared_ptr<AbstractItem> noti) {
101   return impl_->SendNotify(noti, EventInfo::Delete);
102 }
103
104 int Reporter::DeleteAll() {
105   Bundle serialized;
106   EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "");
107   list<Bundle> serialized_list {serialized};
108   impl_->sender_->Notify(info, serialized_list, util::GetAppId());
109   return info.GetRequestId();
110 }
111
112 std::unique_ptr<AbstractItem> Reporter::FindByRootID(std::string id) {
113   Bundle serialized;
114   EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
115   list<Bundle> result = impl_->sender_->Request(info);
116   if (result.size() == 0) {
117     LOGE("Fail to get noti");
118     return unique_ptr<item::AbstractItem>{};
119   }
120   Bundle b = result.front();
121   unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
122   return move(gen_item);
123 }
124
125 int Reporter::SendEvent(const IEventInfo& info,
126     shared_ptr<item::AbstractItem> noti) {
127   Bundle serialized = noti->Serialize();
128   list<Bundle> serialized_list {serialized};
129   impl_->sender_->Notify(info, serialized_list);
130   return info.GetRequestId();
131 }
132
133 int Reporter::SendEvent(const IEventInfo& info,
134     std::list<std::shared_ptr<item::AbstractItem>> notiList) {
135   list<Bundle> serialized_list;
136   for (auto& i : notiList) {
137     Bundle b = i->Serialize();
138     serialized_list.push_back(b);
139   }
140   impl_->sender_->Notify(info, serialized_list);
141   return info.GetRequestId();
142 }
143
144 void Reporter::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
145   NotificationError error =
146       (static_cast<const IEventInfoInternal&>(info)).GetError();
147   list<shared_ptr<item::AbstractItem>> item_list;
148   if (info.GetEventType() == EventInfo::Error) {
149     OnError(error, info.GetRequestId());
150     return;
151   } else if (info.GetEventType() == EventInfo::DeleteAll) {
152     OnEvent(info, item_list);
153     return;
154   }
155
156   for (auto& i : serialized) {
157     shared_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
158     item_list.emplace_back(gen_item);
159   }
160   OnEvent(info, item_list);
161 }
162
163 list<shared_ptr<item::AbstractItem>> Reporter::OnRequestEvent(const IEventInfo& info) {
164   return list<shared_ptr<item::AbstractItem>>({});
165 }
166
167 list<Bundle> Reporter::OnRequest(const IEventInfo& info) {
168   list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
169   list<Bundle> serialized_list;
170   for (auto& i : item_list) {
171     serialized_list.push_back(i->Serialize());
172   }
173   return serialized_list;
174 }
175
176 int Reporter::OnRequestNumber(const IEventInfo& info) {
177   return 0;
178 }
179
180 void Reporter::OnEvent(
181     const IEventInfo& info, list<shared_ptr<item::AbstractItem>> notiList) {
182 }
183
184 void Reporter::OnError(NotificationError error, int requestId) {
185 }
186
187 string Reporter::GetPath() {
188   return NOTIFICATION_EX_REPORTER_OBJECT_PATH;
189 }
190
191 }  // namespace notification