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 tizen_base;
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());
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   return impl_->SendNotify(noti, EventInfo::Post);
79 }
80
81 int Reporter::Post(std::list<std::shared_ptr<AbstractItem>> notiList) {
82   EventInfo info(EventInfo::Post, util::GetAppId(), "");
83   list<Bundle> serialized_list;
84   for (auto& i : notiList) {
85     Bundle b = i->Serialize();
86     serialized_list.push_back(b);
87   }
88   impl_->sender_->Notify(info, serialized_list);
89   return info.GetRequestId();
90 }
91
92 int Reporter::Update(std::shared_ptr<AbstractItem> noti) {
93   return impl_->SendNotify(noti, EventInfo::Update);
94 }
95
96 int Reporter::Delete(std::shared_ptr<AbstractItem> noti) {
97   return impl_->SendNotify(noti, EventInfo::Delete);
98 }
99
100 int Reporter::DeleteAll() {
101   //TODO
102   return -1;
103 }
104
105 std::unique_ptr<AbstractItem> Reporter::FindByRootID(std::string id) {
106   Bundle serialized;
107   EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
108   list<Bundle> result = impl_->sender_->Request(info);
109   if (result.size() == 0) {
110     LOGE("Fail to get noti");
111     return unique_ptr<item::AbstractItem>{};
112   }
113   Bundle b = result.front();
114   unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
115   return move(gen_item);
116 }
117
118 int Reporter::SendEvent(const IEventInfo& info,
119     shared_ptr<item::AbstractItem> noti) {
120   Bundle serialized = noti->Serialize();
121   list<Bundle> serialized_list {serialized};
122   impl_->sender_->Notify(info, serialized_list);
123   return info.GetRequestId();
124 }
125
126 void Reporter::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
127   NotificationError error =
128       (static_cast<const IEventInfoInternal&>(info)).GetError();
129   if (info.GetEventType() == EventInfo::Error) {
130     OnError(error, info.GetRequestId());
131     return;
132   }
133
134   list<shared_ptr<item::AbstractItem>> item_list;
135   for (auto& i : serialized) {
136     shared_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
137     item_list.emplace_back(gen_item);
138   }
139   OnEvent(info, item_list);
140 }
141
142 list<shared_ptr<item::AbstractItem>> Reporter::OnRequestEvent(const IEventInfo& info) {
143   return list<shared_ptr<item::AbstractItem>>({});
144 }
145
146 list<Bundle> Reporter::OnRequest(const IEventInfo& info) {
147   list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
148   list<Bundle> serialized_list;
149   for (auto& i : item_list) {
150     serialized_list.push_back(i->Serialize());
151   }
152   return serialized_list;
153 }
154
155 void Reporter::OnEvent(
156     const IEventInfo& info, list<shared_ptr<item::AbstractItem>> notiList) {
157 }
158
159 void Reporter::OnError(NotificationError error, int requestId) {
160 }
161
162 string Reporter::GetPath() {
163   return NOTIFICATION_EX_REPORTER_OBJECT_PATH;
164 }
165
166 }  // namespace notification