close one notification to close all
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
#include "services/dbus/notification.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#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) {
|
||||
std::cout << "Acquired bus name: " << name << std::endl;
|
||||
spdlog::info("Acquired bus name: {}", name.raw());
|
||||
|
||||
auto introspection_data = Gio::DBus::NodeInfo::create_for_xml(introspection_xml);
|
||||
|
||||
@@ -74,27 +76,79 @@ void NotificationService::handle_notify(const Glib::VariantContainerBase ¶me
|
||||
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();
|
||||
|
||||
std::cout << "Notification Received: " << summary << " - " << body << std::endl;
|
||||
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 = summary;
|
||||
notify.body = body;
|
||||
notify.summary = static_cast<std::string>(summary);
|
||||
notify.body = static_cast<std::string>(body);
|
||||
|
||||
std::vector<std::string> actions_converted;
|
||||
actions_converted.reserve(actions.size());
|
||||
for (const auto &a : actions) {
|
||||
actions_converted.emplace_back(static_cast<std::string>(a));
|
||||
}
|
||||
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;
|
||||
|
||||
// notify.hints = hints;
|
||||
|
||||
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();
|
||||
@@ -108,7 +162,7 @@ void NotificationService::handle_notify(const Glib::VariantContainerBase ¶me
|
||||
Glib::VariantContainerBase::create_tuple({Glib::Variant<guint>::create(id),
|
||||
Glib::Variant<Glib::ustring>::create(action_id)}));
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Failed to emit ActionInvoked: " << e.what() << std::endl;
|
||||
spdlog::error("Failed to emit ActionInvoked: {}", e.what());
|
||||
}
|
||||
};
|
||||
NotificationController::getInstance()->showNotificationOnAllMonitors(notify);
|
||||
|
||||
Reference in New Issue
Block a user