Revert "Use tizen_base::Bundle"
[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());
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   //TODO
101   return -1;
102 }
103
104 std::unique_ptr<AbstractItem> Reporter::FindByRootID(std::string id) {
105   Bundle serialized;
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 int Reporter::SendEvent(const IEventInfo& info,
118     shared_ptr<item::AbstractItem> noti) {
119   Bundle serialized = noti->Serialize();
120   list<Bundle> serialized_list {serialized};
121   impl_->sender_->Notify(info, serialized_list);
122   return info.GetRequestId();
123 }
124
125 void Reporter::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
126   NotificationError error =
127       (static_cast<const IEventInfoInternal&>(info)).GetError();
128   if (info.GetEventType() == EventInfo::Error) {
129     OnError(error, info.GetRequestId());
130     return;
131   }
132
133   list<shared_ptr<item::AbstractItem>> item_list;
134   for (auto& i : serialized) {
135     shared_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
136     item_list.emplace_back(gen_item);
137   }
138   OnEvent(info, item_list);
139 }
140
141 list<shared_ptr<item::AbstractItem>> Reporter::OnRequestEvent(const IEventInfo& info) {
142   return list<shared_ptr<item::AbstractItem>>({});
143 }
144
145 list<Bundle> Reporter::OnRequest(const IEventInfo& info) {
146   list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
147   list<Bundle> serialized_list;
148   for (auto& i : item_list) {
149     serialized_list.push_back(i->Serialize());
150   }
151   return serialized_list;
152 }
153
154 void Reporter::OnEvent(
155     const IEventInfo& info, list<shared_ptr<item::AbstractItem>> notiList) {
156 }
157
158 void Reporter::OnError(NotificationError error, int requestId) {
159 }
160
161 string Reporter::GetPath() {
162   return NOTIFICATION_EX_REPORTER_OBJECT_PATH;
163 }
164
165 }  // namespace notification