Add notification-ex feature
[platform/core/appfw/data-provider-master.git] / src / notification_ex_service.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 <stdio.h>
18 #include <stdlib.h>
19 #include <errno.h>
20 #include <glib.h>
21 #include <math.h>
22 #include <gio/gio.h>
23
24 #include <aul.h>
25 #include <dlog.h>
26
27 #include <notification-ex/manager.h>
28 #include <notification-ex/reporter.h>
29 #include <notification-ex/dbus_sender.h>
30 #include <notification-ex/dbus_event_listener.h>
31 #include <notification-ex/dbus_connection_manager.h>
32 #include <notification-ex/button_item.h>
33 #include <notification-ex/ex_util.h>
34
35 #include "debug.h"
36 #include "notification_ex_service.h"
37
38 using namespace notification;
39 using namespace std;
40
41 class DPMFacade {
42   public:
43    DPMFacade(unique_ptr<Reporter> reporter, unique_ptr<Manager> manager)
44     : reporter_(move(reporter)), manager_(move(manager)) {
45    }
46
47    void DelegateReporterEvent(const EventInfo& info, shared_ptr<item::AbstractItem> item) {
48      reporter_->SendEvent(info, item);
49    }
50
51    void DelegateManagerEvent(const EventInfo& info, shared_ptr<item::AbstractItem> noti) {
52      manager_->SendEvent(info, noti);
53    }
54
55    unique_ptr<Reporter> reporter_;
56    unique_ptr<Manager> manager_;
57 };
58
59 static DPMFacade* facade_;
60
61 class DPMReporter : public Reporter {
62  protected:
63   void OnEvent(const EventInfo& info,
64       list<shared_ptr<item::AbstractItem>> noti_list) override {
65     LOGI("Event received (%d) !!", (int)info.GetEventType());
66     for (auto& i : noti_list) {
67       facade_->DelegateManagerEvent(info, i);
68
69       /* noti to viewers */
70       facade_->DelegateReporterEvent(info, i);
71     }
72   }
73
74   list<shared_ptr<item::AbstractItem>> OnRequestEvent(const EventInfo& info) override {
75     DBG("Get report !!! %s", info.GetItemId().c_str());
76     list<shared_ptr<item::AbstractItem>> ret {
77       make_shared<item::ButtonItem>(info.GetItemId().c_str(), "title")
78     };
79     return ret;
80   }
81
82  public:
83   DPMReporter(std::unique_ptr<IEventSender> sender,
84       std::unique_ptr<IEventListener> listener)
85     : Reporter(move(sender), move(listener)) {
86   }
87 };
88
89 class DPMManager : public Manager {
90  protected:
91   void OnAdd(const EventInfo& info,
92       list<shared_ptr<item::AbstractItem>> addedItem) override {
93     DBG("Add !!!");
94     //TODO: store it to DB
95     for (auto& i : addedItem)
96       facade_->DelegateReporterEvent(info, i);
97   }
98
99   void OnUpdate(const EventInfo& info,
100       shared_ptr<item::AbstractItem> updatedItem) override {
101     DBG("Update !!!");
102     facade_->DelegateReporterEvent(info, updatedItem);
103   }
104
105   void OnDelete(const EventInfo& info,
106       shared_ptr<item::AbstractItem> deletedItem) override {
107     DBG("Delete !!!");
108     facade_->DelegateReporterEvent(info, deletedItem);
109   }
110
111   list<shared_ptr<item::AbstractItem>> OnRequestEvent(const EventInfo& info) override {
112     DBG("Get !!! %s", info.GetItemId().c_str());
113     //TODO: Get it from DB
114     list<shared_ptr<item::AbstractItem>> ret {
115       make_shared<item::ButtonItem>(info.GetItemId().c_str(), "title")
116     };
117     return ret;
118   }
119
120  public:
121   DPMManager(std::unique_ptr<IEventSender> sender,
122       std::unique_ptr<IEventListener> listener)
123     : Manager(move(sender), move(listener)) {
124   }
125 };
126
127 HAPI int notification_ex_service_init() {
128   facade_ = new DPMFacade(
129     unique_ptr<Reporter>(
130       new DPMReporter(
131         unique_ptr<DBusSender>(new DBusSender(Manager::GetPath())),
132         unique_ptr<DBusEventListener>(new DBusEventListener(Reporter::GetPath()))
133       )
134     ),
135     unique_ptr<Manager>(
136       new DPMManager(
137         unique_ptr<DBusSender>(new DBusSender(Reporter::GetPath())),
138         unique_ptr<DBusEventListener>(new DBusEventListener(Manager::GetPath()))
139       )
140     )
141   );
142
143   return 0;
144 }
145
146 HAPI int notification_ex_service_fini() {
147   delete facade_;
148   return 0;
149 }
150
151 HAPI GDBusConnection* notification_ex_service_get_gdbus_connection() {
152   return DBusConnectionManager::GetInst().GetConnection();
153 }