|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +#if defined(__linux__) |
| 3 | + |
| 4 | + #include <contour/FreeDesktopNotifier.h> |
| 5 | + |
| 6 | + #include <crispy/logstore.h> |
| 7 | + |
| 8 | + #include <QtDBus/QDBusConnection> |
| 9 | + #include <QtDBus/QDBusReply> |
| 10 | + |
| 11 | +namespace contour |
| 12 | +{ |
| 13 | + |
| 14 | +namespace |
| 15 | +{ |
| 16 | + auto const notifierLog = logstore::category("gui.notifier", "Desktop notification backend"); |
| 17 | + |
| 18 | + /// Converts NotificationUrgency to the D-Bus urgency byte value. |
| 19 | + uint8_t toDBusUrgency(vtbackend::NotificationUrgency urgency) |
| 20 | + { |
| 21 | + switch (urgency) |
| 22 | + { |
| 23 | + case vtbackend::NotificationUrgency::Low: return 0; |
| 24 | + case vtbackend::NotificationUrgency::Normal: return 1; |
| 25 | + case vtbackend::NotificationUrgency::Critical: return 2; |
| 26 | + } |
| 27 | + return 1; |
| 28 | + } |
| 29 | +} // namespace |
| 30 | + |
| 31 | +FreeDesktopNotifier::FreeDesktopNotifier(QObject* parent): QObject(parent) |
| 32 | +{ |
| 33 | + _interface = std::make_unique<QDBusInterface>("org.freedesktop.Notifications", |
| 34 | + "/org/freedesktop/Notifications", |
| 35 | + "org.freedesktop.Notifications", |
| 36 | + QDBusConnection::sessionBus(), |
| 37 | + this); |
| 38 | + |
| 39 | + if (!_interface->isValid()) |
| 40 | + { |
| 41 | + notifierLog()("Failed to connect to org.freedesktop.Notifications D-Bus interface: {}", |
| 42 | + _interface->lastError().message().toStdString()); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + // Connect to the NotificationClosed and ActionInvoked signals from the notification server. |
| 47 | + auto bus = QDBusConnection::sessionBus(); |
| 48 | + bus.connect("org.freedesktop.Notifications", |
| 49 | + "/org/freedesktop/Notifications", |
| 50 | + "org.freedesktop.Notifications", |
| 51 | + "NotificationClosed", |
| 52 | + this, |
| 53 | + SLOT(onNotificationClosed(uint, uint))); |
| 54 | + |
| 55 | + bus.connect("org.freedesktop.Notifications", |
| 56 | + "/org/freedesktop/Notifications", |
| 57 | + "org.freedesktop.Notifications", |
| 58 | + "ActionInvoked", |
| 59 | + this, |
| 60 | + SLOT(onActionInvoked(uint, QString))); |
| 61 | +} |
| 62 | + |
| 63 | +void FreeDesktopNotifier::notify(vtbackend::DesktopNotification const& notification) |
| 64 | +{ |
| 65 | + if (!_interface || !_interface->isValid()) |
| 66 | + return; |
| 67 | + |
| 68 | + auto const appName = notification.applicationName.empty() |
| 69 | + ? QStringLiteral("contour") |
| 70 | + : QString::fromStdString(notification.applicationName); |
| 71 | + auto const title = QString::fromStdString(notification.title); |
| 72 | + auto const body = QString::fromStdString(notification.body); |
| 73 | + |
| 74 | + // Build hints map with urgency level. |
| 75 | + QVariantMap hints; |
| 76 | + hints["urgency"] = QVariant::fromValue(toDBusUrgency(notification.urgency)); |
| 77 | + |
| 78 | + // Check if we're replacing an existing notification. |
| 79 | + uint32_t replacesId = 0; |
| 80 | + if (auto it = _oscToDbus.find(notification.identifier); it != _oscToDbus.end()) |
| 81 | + replacesId = it->second; |
| 82 | + |
| 83 | + // Build actions list. The default action is triggered on click. |
| 84 | + QStringList actions; |
| 85 | + actions << QStringLiteral("default") << QStringLiteral("Activate"); |
| 86 | + |
| 87 | + // org.freedesktop.Notifications.Notify parameters: |
| 88 | + // STRING app_name, UINT32 replaces_id, STRING app_icon, STRING summary, |
| 89 | + // STRING body, ARRAY actions, DICT hints, INT32 expire_timeout |
| 90 | + QDBusReply<uint> reply = _interface->call("Notify", |
| 91 | + appName, |
| 92 | + replacesId, |
| 93 | + QStringLiteral(""), // app_icon (empty) |
| 94 | + title, |
| 95 | + body, |
| 96 | + actions, |
| 97 | + hints, |
| 98 | + notification.timeout); |
| 99 | + |
| 100 | + if (reply.isValid()) |
| 101 | + { |
| 102 | + auto const dbusId = reply.value(); |
| 103 | + notifierLog()("Notification sent: id='{}' -> dbus_id={}", notification.identifier, dbusId); |
| 104 | + |
| 105 | + // Remove stale reverse mapping when replacing a notification. |
| 106 | + if (replacesId != 0) |
| 107 | + _dbusToOsc.erase(replacesId); |
| 108 | + |
| 109 | + // Update the bidirectional ID mapping. |
| 110 | + _dbusToOsc[dbusId] = notification.identifier; |
| 111 | + _oscToDbus[notification.identifier] = dbusId; |
| 112 | + } |
| 113 | + else |
| 114 | + { |
| 115 | + notifierLog()("Failed to send notification: {}", reply.error().message().toStdString()); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +void FreeDesktopNotifier::close(std::string const& identifier) |
| 120 | +{ |
| 121 | + if (!_interface || !_interface->isValid()) |
| 122 | + return; |
| 123 | + |
| 124 | + auto it = _oscToDbus.find(identifier); |
| 125 | + if (it == _oscToDbus.end()) |
| 126 | + return; |
| 127 | + |
| 128 | + auto const dbusId = it->second; |
| 129 | + _interface->call("CloseNotification", dbusId); |
| 130 | + |
| 131 | + // Clean up mappings. |
| 132 | + _dbusToOsc.erase(dbusId); |
| 133 | + _oscToDbus.erase(it); |
| 134 | +} |
| 135 | + |
| 136 | +void FreeDesktopNotifier::onNotificationClosed(uint id, uint reason) |
| 137 | +{ |
| 138 | + auto it = _dbusToOsc.find(id); |
| 139 | + if (it == _dbusToOsc.end()) |
| 140 | + return; |
| 141 | + |
| 142 | + auto const identifier = QString::fromStdString(it->second); |
| 143 | + notifierLog()("Notification closed: dbus_id={} reason={}", id, reason); |
| 144 | + |
| 145 | + // Clean up mappings. |
| 146 | + auto const oscId = it->second; |
| 147 | + _dbusToOsc.erase(it); |
| 148 | + _oscToDbus.erase(oscId); |
| 149 | + |
| 150 | + emit notificationClosed(identifier, reason); |
| 151 | +} |
| 152 | + |
| 153 | +void FreeDesktopNotifier::onActionInvoked(uint id, QString const& actionKey) |
| 154 | +{ |
| 155 | + (void) actionKey; // We only register "default" action. |
| 156 | + |
| 157 | + auto it = _dbusToOsc.find(id); |
| 158 | + if (it == _dbusToOsc.end()) |
| 159 | + return; |
| 160 | + |
| 161 | + auto const identifier = QString::fromStdString(it->second); |
| 162 | + notifierLog()("Notification activated: dbus_id={}", id); |
| 163 | + |
| 164 | + emit actionInvoked(identifier); |
| 165 | +} |
| 166 | + |
| 167 | +} // namespace contour |
| 168 | + |
| 169 | +#endif // defined(__linux__) |
0 commit comments