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