Using unique_ptr for sender and listener
[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.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 void Reporter::Impl::SendNotify(shared_ptr<item::AbstractItem> noti,
59     EventInfo::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 }
65
66 void Reporter::Post(std::shared_ptr<item::AbstractItem> noti) {
67   LOGI("Post noti");
68   impl_->SendNotify(noti, EventInfo::Post);
69 }
70
71 void Reporter::Post(std::list<std::shared_ptr<AbstractItem>> notiList) {
72   EventInfo info(EventInfo::Post, util::GetAppId(), "");
73   list<Bundle> serialized_list;
74   for (auto& i : notiList) {
75     Bundle b = i->Serialize();
76     serialized_list.push_back(b);
77   }
78   impl_->sender_->Notify(info, serialized_list);
79 }
80
81 void Reporter::Update(std::shared_ptr<AbstractItem> noti) {
82   impl_->SendNotify(noti, EventInfo::Update);
83 }
84
85 void Reporter::Remove(std::shared_ptr<AbstractItem> noti) {
86   impl_->SendNotify(noti, EventInfo::Delete);
87 }
88
89 std::shared_ptr<AbstractItem> Reporter::FindByRootID(std::string id) {
90   Bundle serialized;
91   EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
92   list<Bundle> result = impl_->sender_->Request(info);
93   if (result.size() == 0) {
94     LOGE("Fail to get noti");
95     return shared_ptr<item::AbstractItem>({});
96   }
97   Bundle b = result.front();
98   shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
99   return gen_item;
100 }
101
102 void Reporter::SendEvent(const EventInfo& info,
103     shared_ptr<item::AbstractItem> noti) {
104   Bundle serialized = noti->Serialize();
105   list<Bundle> serialized_list {serialized};
106   impl_->sender_->Notify(info, serialized_list);
107 }
108
109 void Reporter::OnEvent(const EventInfo& info, list<Bundle> serialized) {
110   list<shared_ptr<item::AbstractItem>> item_list;
111   for (auto& i : serialized) {
112     shared_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
113     item_list.emplace_back(gen_item);
114   }
115   OnEvent(info, item_list);
116 }
117
118 list<shared_ptr<item::AbstractItem>> Reporter::OnRequestEvent(const EventInfo& info) {
119   return list<shared_ptr<item::AbstractItem>>({});
120 }
121
122 list<Bundle> Reporter::OnRequest(const EventInfo& info) {
123   list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
124   list<Bundle> serialized_list;
125   for (auto& i : item_list) {
126     serialized_list.push_back(i->Serialize());
127   }
128   return serialized_list;
129 }
130
131 void Reporter::OnEvent(
132     const EventInfo& info, list<shared_ptr<item::AbstractItem>> notiList) {
133 }
134
135 string Reporter::GetPath() {
136   return NOTIFICATION_EX_REPORTER_OBJECT_PATH;
137 }
138
139 }  // nampace notification