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