2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "notification-ex/dbus_connection_manager.h"
25 #include "notification-ex/dbus_sender.h"
26 #include "notification-ex/dbus_sender_implementation.h"
27 #include "notification-ex/event_info_internal.h"
28 #include "notification-ex/ex_util.h"
34 #define LOG_TAG "NOTIFICATION_EX"
35 #define MAX_PACKAGE_STR_SIZE 512
38 using namespace tizen_base;
39 namespace notification {
41 DBusSender::DBusSender(string path) : impl_(new Impl(this, path)) {
42 LOGW("Created (%s)", path.c_str());
45 DBusSender::Impl::~Impl() = default;
47 DBusSender::Impl::Impl(DBusSender* parent, string path)
48 : path_(path), parent_(parent) {
49 LOGI("Dbus_sender impl created");
52 DBusSender::~DBusSender() {
56 bool DBusSender::Impl::EmitSignal(string bus_name, string signal_name,
59 gboolean result = TRUE;
61 result = g_dbus_connection_emit_signal(
62 DBusConnectionManager::GetInst().GetConnection(),
63 bus_name.empty() ? NULL : bus_name.c_str(),
65 DBusConnectionManager::GetInst().GetInterfaceName().c_str(),
66 signal_name.c_str(), data, &err);
67 if (result == FALSE) {
68 LOGE("g_dbus_connection_emit_signal() is failed");
70 LOGE("g_dbus_connection_emit_signal() err : %s",
75 LOGI("Successfully emit signal to %s, %s, %s",
76 bus_name.c_str(), path_.c_str(), signal_name.c_str());
81 string DBusSender::Impl::GetBusName(string appid, string dest_appid) const {
82 if (!DBusConnectionManager::GetInst().IsDataProviderMaster(appid))
83 return DBusConnectionManager::GetInst().GetDataProviderMasterName();
85 return DBusConnectionManager::GetInst().GetBusName(dest_appid);
88 void DBusSender::Notify(const IEventInfo& info, list<Bundle> serialized,
90 string signal_name = EventInfo::GetString(info.GetEventType());
91 string appid = util::GetAppId();
93 GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
94 for (auto& i : serialized) {
95 g_variant_builder_add(builder, "(s)",
96 reinterpret_cast<char*>(i.ToRaw().first.get()));
99 GVariant* data = g_variant_new("(ssa(s))",
101 reinterpret_cast<char*>(info.Serialize().ToRaw().first.get()), builder);
102 string bus_name = impl_->GetBusName(appid, dest_appid);
103 LOGI("bus name %s, %s", dest_appid.c_str(), bus_name.c_str());
104 impl_->EmitSignal(bus_name, signal_name, data);
105 g_variant_builder_unref(builder);
108 GDBusMessage* DBusSender::Impl::MethodCall(string appid, string method_name,
110 GError* err = nullptr;
111 GDBusMessage* msg = g_dbus_message_new_method_call(
112 DBusConnectionManager::GetInst().GetDataProviderMasterName().c_str(),
114 DBusConnectionManager::GetInst().GetInterfaceName().c_str(),
115 method_name.c_str());
117 LOGE("Can't allocate new method call");
121 g_dbus_message_set_body(msg,
122 g_variant_new("(ss)",
124 reinterpret_cast<char*>(serialized.ToRaw().first.get())
128 LOGI("send message !! (%s) (%s) (%s)",
129 path_.c_str(), method_name.c_str(), appid.c_str());
130 GDBusMessage* reply = g_dbus_connection_send_message_with_reply_sync(
131 DBusConnectionManager::GetInst().GetConnection(), msg,
132 G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
137 std::list<Bundle> DBusSender::Request(const IEventInfo& info) {
138 string appid = info.GetOwner();
139 if (appid.length() == 0)
140 appid = util::GetAppId();
142 string method_name = EventInfo::GetString(info.GetEventType());
143 Bundle serialized = info.Serialize();
145 GDBusMessage* reply = impl_->MethodCall(appid, method_name, serialized);
146 GVariant *reply_body = g_dbus_message_get_body(reply);
148 GVariantIter *iter = nullptr;
149 list<Bundle> ret_list;
150 g_variant_get(reply_body, "(a(s))", &iter);
152 while (g_variant_iter_loop(iter, "(s)", &raw) && raw != nullptr) {
154 ret_list.emplace_back(ret);
160 int DBusSender::RequestNumber(const IEventInfo& info) {
161 string appid = info.GetOwner();
162 if (appid.length() == 0)
163 appid = util::GetAppId();
165 string method_name = EventInfo::GetString(info.GetEventType());
166 Bundle serialized = info.Serialize();
168 GDBusMessage* reply = impl_->MethodCall(appid, method_name, serialized);
169 GVariant *reply_body = g_dbus_message_get_body(reply);
172 g_variant_get(reply_body, "(i)", &num);
176 } // namespace notification