refactor notifications
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
#include "services/dbus/notification.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "services/notificationController.hpp"
|
||||
#include "widgets/notification.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;
|
||||
@@ -52,23 +55,61 @@ void NotificationService::handle_notify(const Glib::VariantContainerBase ¶me
|
||||
|
||||
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 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();
|
||||
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();
|
||||
|
||||
std::cout << "Notification Received: " << summary << " - " << body << std::endl;
|
||||
|
||||
createNotificationPopup(summary, body);
|
||||
NotifyMessage notify;
|
||||
notify.app_name = app_name;
|
||||
notify.replaces_id = replaces_id;
|
||||
notify.app_icon = app_icon;
|
||||
notify.summary = summary;
|
||||
notify.body = 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));
|
||||
}
|
||||
|
||||
notify.actions = actions_converted;
|
||||
|
||||
// notify.hints = hints;
|
||||
|
||||
notify.expire_timeout = expire_timeout;
|
||||
|
||||
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) {
|
||||
std::cerr << "Failed to emit ActionInvoked: " << e.what() << std::endl;
|
||||
}
|
||||
};
|
||||
NotificationController::getInstance()->showNotificationOnAllMonitors(notify);
|
||||
invocation->return_value(Glib::VariantContainerBase::create_tuple(
|
||||
Glib::Variant<guint>::create(id)));
|
||||
}
|
||||
|
||||
void NotificationService::createNotificationPopup(const Glib::ustring &title, const Glib::ustring &message) {
|
||||
|
||||
auto controller = NotificationController::getInstance();
|
||||
controller->showNotificationOnAllMonitors(title, message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user