#include "notifier.hpp" #include #include #include #include #include namespace strawberry::app { namespace { const QString kService = QStringLiteral("org.freedesktop.Notifications"); const QString kPath = QStringLiteral("/org/freedesktop/Notifications"); const QString kInterface = QStringLiteral("org.freedesktop.Notifications"); } Notifier::Notifier(QObject* parent) : QObject(parent) { auto bus = QDBusConnection::sessionBus(); bus.connect(kService, kPath, kInterface, QStringLiteral("ActionInvoked"), this, SLOT(on_action_invoked(uint, QString))); bus.connect(kService, kPath, kInterface, QStringLiteral("NotificationClosed"), this, SLOT(on_closed(uint, uint))); } void Notifier::notify(const QString& summary, const QString& body, Urgency urgency, const QString& finding_id) { auto message = QDBusMessage::createMethodCall(kService, kPath, kInterface, QStringLiteral("Notify")); QStringList actions; if (!finding_id.isEmpty()) { actions << QStringLiteral("default") << QStringLiteral("Open"); } QVariantMap hints; hints.insert(QStringLiteral("urgency"), QVariant::fromValue(static_cast(urgency))); hints.insert(QStringLiteral("desktop-entry"), QStringLiteral("strawberry-tray")); // Notification servers render the body as markup; escape it. message << QStringLiteral("Strawberry") << uint(0) << QStringLiteral("security-high") << summary << body.toHtmlEscaped() << actions << hints << int(urgency == Urgency::critical ? 0 : 8000); auto* watcher = new QDBusPendingCallWatcher(QDBusConnection::sessionBus().asyncCall(message), this); connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, watcher, finding_id]() { QDBusPendingReply reply = *watcher; if (reply.isValid() && !finding_id.isEmpty()) { finding_by_notification_.insert(reply.value(), finding_id); } watcher->deleteLater(); }); } void Notifier::attach_tray_icon(QSystemTrayIcon* icon) { tray_icon_ = icon; // unused: the notification server handles clicks } void Notifier::on_action_invoked(uint id, const QString& action_key) { const auto it = finding_by_notification_.constFind(id); if (it == finding_by_notification_.constEnd()) { return; // not ours } if (action_key == QStringLiteral("default")) { emit activated(*it); } } void Notifier::on_closed(uint id, uint) { finding_by_notification_.remove(id); } } // namespace strawberry::app