172 lines
8.2 KiB
C++
172 lines
8.2 KiB
C++
#include "services/dbus/notification.hpp"
|
|
|
|
#include <spdlog/spdlog.h>
|
|
#include <sys/types.h>
|
|
|
|
#include "helpers/string.hpp"
|
|
#include "services/notificationController.hpp"
|
|
|
|
#include "glib.h"
|
|
#include "glibconfig.h"
|
|
|
|
void NotificationService::onBusAcquired(const Glib::RefPtr<Gio::DBus::Connection> &connection, const Glib::ustring &name) {
|
|
spdlog::info("Acquired bus name: {}", name.raw());
|
|
|
|
auto introspection_data = Gio::DBus::NodeInfo::create_for_xml(introspection_xml);
|
|
|
|
// Register the object at the standard path
|
|
connection->register_object(
|
|
"/org/freedesktop/Notifications",
|
|
introspection_data->lookup_interface("org.freedesktop.Notifications"),
|
|
getMessageInterfaceVTable());
|
|
}
|
|
|
|
const Gio::DBus::InterfaceVTable &NotificationService::getMessageInterfaceVTable() {
|
|
static Gio::DBus::InterfaceVTable vtable(
|
|
sigc::mem_fun(*this, &NotificationService::on_method_call));
|
|
return vtable;
|
|
}
|
|
|
|
void NotificationService::on_method_call(const Glib::RefPtr<Gio::DBus::Connection> &,
|
|
const Glib::ustring &,
|
|
const Glib::ustring &,
|
|
const Glib::ustring &,
|
|
const Glib::ustring &method_name,
|
|
const Glib::VariantContainerBase ¶meters,
|
|
const Glib::RefPtr<Gio::DBus::MethodInvocation> &invocation) {
|
|
|
|
if (method_name == "Notify") {
|
|
handle_notify(parameters, invocation);
|
|
} else if (method_name == "GetCapabilities") {
|
|
auto caps = std::vector<Glib::ustring>{
|
|
"body", "actions", "actions-icons", "persistence", "icon-static"
|
|
};
|
|
invocation->return_value(Glib::VariantContainerBase::create_tuple(
|
|
Glib::Variant<std::vector<Glib::ustring>>::create(caps)));
|
|
} else if (method_name == "GetServerInformation") {
|
|
invocation->return_value(Glib::VariantContainerBase::create_tuple({Glib::Variant<Glib::ustring>::create("MyGtkmmNotifier"),
|
|
Glib::Variant<Glib::ustring>::create("Custom"),
|
|
Glib::Variant<Glib::ustring>::create("1.0"),
|
|
Glib::Variant<Glib::ustring>::create("1.2")}));
|
|
} else {
|
|
// Handle other methods or return error
|
|
invocation->return_value(Glib::VariantContainerBase());
|
|
}
|
|
}
|
|
|
|
void NotificationService::handle_notify(const Glib::VariantContainerBase ¶meters,
|
|
const Glib::RefPtr<Gio::DBus::MethodInvocation> &invocation) {
|
|
|
|
Glib::VariantBase app_name_var, replaces_id_var, app_icon_var, summary_var, body_var, actions_var, hints_var, timeout_var;
|
|
parameters.get_child(app_name_var, 0);
|
|
parameters.get_child(replaces_id_var, 1);
|
|
parameters.get_child(app_icon_var, 2);
|
|
parameters.get_child(summary_var, 3);
|
|
parameters.get_child(body_var, 4);
|
|
parameters.get_child(actions_var, 5);
|
|
parameters.get_child(hints_var, 6);
|
|
parameters.get_child(timeout_var, 7);
|
|
|
|
Glib::ustring app_name = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(app_name_var).get();
|
|
guint32 replaces_id = Glib::VariantBase::cast_dynamic<Glib::Variant<guint32>>(replaces_id_var).get();
|
|
Glib::ustring app_icon = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(app_icon_var).get();
|
|
Glib::ustring summary = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(summary_var).get();
|
|
Glib::ustring body = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(body_var).get();
|
|
std::vector<Glib::ustring> actions = Glib::VariantBase::cast_dynamic<Glib::Variant<std::vector<Glib::ustring>>>(actions_var).get();
|
|
std::map<Glib::ustring, Glib::VariantBase> hints = Glib::VariantBase::cast_dynamic<Glib::Variant<std::map<Glib::ustring, Glib::VariantBase>>>(hints_var).get();
|
|
gint32 expire_timeout = Glib::VariantBase::cast_dynamic<Glib::Variant<gint32>>(timeout_var).get();
|
|
|
|
spdlog::info("Notification Received: {} - {}", summary.raw(), body.raw());
|
|
|
|
NotifyMessage notify;
|
|
notify.app_name = app_name;
|
|
notify.replaces_id = replaces_id;
|
|
notify.app_icon = app_icon;
|
|
notify.summary = static_cast<std::string>(summary);
|
|
notify.body = static_cast<std::string>(body);
|
|
notify.actionInvoked = std::make_shared<bool>(false);
|
|
|
|
std::vector<std::string> actions_converted;
|
|
actions_converted.reserve(actions.size());
|
|
for (ulong i = 0; i < actions.size(); i += 2) {
|
|
auto name = static_cast<std::string>(actions[i]);
|
|
auto label = static_cast<std::string>(actions[i + 1]);
|
|
|
|
if (name == "default") {
|
|
label = "Open";
|
|
}
|
|
|
|
actions_converted.push_back(name);
|
|
actions_converted.push_back(label);
|
|
}
|
|
notify.actions = actions_converted;
|
|
|
|
|
|
for (const auto &[key, value] : hints) {
|
|
if (key == "urgency") {
|
|
if (value.is_of_type(Glib::VariantType("y"))) {
|
|
auto urgency = Glib::VariantBase::cast_dynamic<Glib::Variant<guint8>>(value).get();
|
|
notify.urgency = static_cast<NotificationUrgency>(urgency);
|
|
}
|
|
}
|
|
|
|
if (key == "image-data") {
|
|
Glib::VariantBase image_value = value;
|
|
if (image_value.is_of_type(Glib::VariantType("v"))) {
|
|
try {
|
|
image_value = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::VariantBase>>(image_value).get();
|
|
} catch (const std::bad_cast &) {
|
|
spdlog::warn("Failed to unwrap image-data variant");
|
|
}
|
|
}
|
|
|
|
if (image_value.is_of_type(Glib::VariantType("(iiibiiay)"))) {
|
|
// This is a raw data image format which describes width, height, rowstride, has alpha,
|
|
// bits per sample, channels and image data respectively.
|
|
try {
|
|
auto image_data_variant = Glib::VariantBase::cast_dynamic<
|
|
Glib::Variant<std::tuple<gint32, gint32, gint32, bool, gint32, gint32, std::vector<guint8>>>>(image_value);
|
|
auto [width, height, rowstride, has_alpha, bits_per_sample, channels, data] = image_data_variant.get();
|
|
(void)channels;
|
|
auto pixbuf = Gdk::Pixbuf::create_from_data(
|
|
data.data(),
|
|
Gdk::Colorspace::RGB,
|
|
has_alpha,
|
|
bits_per_sample,
|
|
width,
|
|
height,
|
|
rowstride);
|
|
notify.imageData = pixbuf;
|
|
} catch (const std::bad_cast &e) {
|
|
spdlog::warn("Failed to decode image-data hint: {}", e.what());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
notify.expire_timeout = expire_timeout;
|
|
|
|
if (app_name == "Thunderbird") {
|
|
notify.expire_timeout = 10000; // 10 seconds for email notifications
|
|
}
|
|
|
|
guint id = notificationIdCounter++;
|
|
// Set up the callback to emit ActionInvoked on D-Bus
|
|
Glib::RefPtr<Gio::DBus::Connection> dbus_conn = invocation->get_connection();
|
|
notify.on_action = [dbus_conn, id](const std::string &action_id) {
|
|
try {
|
|
dbus_conn->emit_signal(
|
|
"/org/freedesktop/Notifications",
|
|
"org.freedesktop.Notifications",
|
|
"ActionInvoked",
|
|
"", // destination bus name (empty for broadcast)
|
|
Glib::VariantContainerBase::create_tuple({Glib::Variant<guint>::create(id),
|
|
Glib::Variant<Glib::ustring>::create(action_id)}));
|
|
} catch (const std::exception &e) {
|
|
spdlog::error("Failed to emit ActionInvoked: {}", e.what());
|
|
}
|
|
};
|
|
NotificationController::getInstance()->showNotificationOnAllMonitors(notify);
|
|
invocation->return_value(Glib::VariantContainerBase::create_tuple(
|
|
Glib::Variant<guint>::create(id)));
|
|
} |