service/DesktopNotifier.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "DesktopNotifier.h" | ||
| 2 | |||
| 3 | #include <QStandardPaths> | ||
| 4 | #include "DesktopIntegration.h" | ||
| 5 | |||
| 6 | #include <QDBusConnection> | ||
| 7 | #include <QDBusServiceWatcher> | ||
| 8 | #include <QDBusInterface> | ||
| 9 | #include <QDBusMessage> | ||
| 10 | #include <QDBusReply> | ||
| 11 | #include <QLoggingCategory> | ||
| 12 | #include <QPointer> | ||
| 13 | #include <QTimer> | ||
| 14 | |||
| 15 | #ifdef MAILJD_KDE_INTEGRATION | ||
| 16 | #include <KNotification> | ||
| 17 | #endif | ||
| 18 | |||
| 19 |
4/7✓ Branch 2 → 3 taken 23 times.
✓ Branch 2 → 8 taken 780 times.
✓ Branch 4 → 5 taken 23 times.
✗ Branch 4 → 8 not taken.
✓ Branch 5 → 6 taken 23 times.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
|
803 | Q_LOGGING_CATEGORY(lcNotifier, "mailjd.notifier") |
| 20 | |||
| 21 | namespace { | ||
| 22 | // S106-212: a notification daemon answers in milliseconds; one that does | ||
| 23 | // not within this time is treated as hung and left alone for a minute. | ||
| 24 | constexpr int kDaemonTimeoutMs = 2000; | ||
| 25 | constexpr qint64 kDaemonPauseMs = 60000; | ||
| 26 | } // namespace | ||
| 27 | |||
| 28 | 35 | QString DesktopNotifier::formatNotificationText(QString text, | |
| 29 | bool supportsMarkup) { | ||
| 30 |
2/2✓ Branch 12 → 4 taken 372 times.
✓ Branch 12 → 13 taken 35 times.
|
407 | for (qsizetype i = text.size() - 1; i >= 0; --i) { |
| 31 | 372 | const ushort code = text.at(i).unicode(); | |
| 32 |
8/8✓ Branch 6 → 7 taken 5 times.
✓ Branch 6 → 9 taken 367 times.
✓ Branch 7 → 8 taken 4 times.
✓ Branch 7 → 9 taken 1 time.
✓ Branch 8 → 9 taken 1 time.
✓ Branch 8 → 10 taken 3 times.
✓ Branch 9 → 10 taken 1 time.
✓ Branch 9 → 11 taken 368 times.
|
372 | if ((code < 0x20 && code != '\n' && code != '\t') || code == 0x7f) |
| 33 | 4 | text.remove(i, 1); | |
| 34 | } | ||
| 35 |
2/2✓ Branch 13 → 14 taken 5 times.
✓ Branch 13 → 15 taken 30 times.
|
35 | return supportsMarkup ? text.toHtmlEscaped() : text; |
| 36 | } | ||
| 37 | |||
| 38 | 802 | DesktopNotifier::DesktopNotifier(QObject *parent) : QObject(parent) { | |
| 39 |
0/4✗ Branch 88 → 89 not taken.
✗ Branch 88 → 91 not taken.
✗ Branch 89 → 90 not taken.
✗ Branch 89 → 92 not taken.
|
1604 | m_dbus = new QDBusInterface(QStringLiteral("org.freedesktop.Notifications"), |
| 40 | 1604 | QStringLiteral("/org/freedesktop/Notifications"), | |
| 41 | 1604 | QStringLiteral("org.freedesktop.Notifications"), | |
| 42 |
7/9✓ Branch 7 → 8 taken 4 times.
✓ Branch 8 → 9 taken 802 times.
✓ Branch 9 → 10 taken 798 times.
✓ Branch 18 → 19 taken 4 times.
✓ Branch 19 → 20 taken 798 times.
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 29 taken 4 times.
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 30 taken 798 times.
|
2406 | QDBusConnection::sessionBus(), this); |
| 43 | // S106-212: the calls run on the GUI thread; Qt's default 25 s timeout | ||
| 44 | // froze the window for that long per call when the daemon hung. | ||
| 45 |
2/2✓ Branch 29 → 30 taken 4 times.
✓ Branch 30 → 31 taken 798 times.
|
802 | m_dbus->setTimeout(kDaemonTimeoutMs); |
| 46 |
2/2✓ Branch 30 → 31 taken 4 times.
✓ Branch 31 → 32 taken 798 times.
|
802 | const DesktopBackends backends = DesktopIntegration::current(); |
| 47 | #ifdef MAILJD_KDE_INTEGRATION | ||
| 48 | 798 | m_useKde = backends.kdeNotifications; | |
| 49 | #else | ||
| 50 | Q_UNUSED(backends) | ||
| 51 | #endif | ||
| 52 |
2/2✓ Branch 31 → 32 taken 4 times.
✓ Branch 32 → 33 taken 798 times.
|
802 | m_available = m_dbus->isValid(); |
| 53 |
2/4✓ Branch 32 → 33 taken 4 times.
✗ Branch 32 → 49 not taken.
✓ Branch 33 → 34 taken 798 times.
✗ Branch 33 → 50 not taken.
|
802 | if (!m_useKde) { |
| 54 |
2/2✓ Branch 33 → 34 taken 4 times.
✓ Branch 34 → 35 taken 798 times.
|
802 | setupDBusConnections(); |
| 55 | // QDBusServiceWatcher instead of the deprecated | ||
| 56 | // QDBusConnectionInterface::serviceOwnerChanged: it watches only this | ||
| 57 | // name (one bus match rule) and prints no deprecation warning. | ||
| 58 | { | ||
| 59 | 1604 | auto *watcher = new QDBusServiceWatcher(QStringLiteral("org.freedesktop.Notifications"), | |
| 60 |
2/2✓ Branch 36 → 37 taken 4 times.
✓ Branch 37 → 38 taken 798 times.
|
1604 | QDBusConnection::sessionBus(), |
| 61 |
6/12✓ Branch 34 → 35 taken 4 times.
✓ Branch 35 → 36 taken 798 times.
✓ Branch 40 → 41 taken 4 times.
✓ Branch 41 → 42 taken 798 times.
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 4 times.
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 47 taken 798 times.
✗ Branch 102 → 103 not taken.
✗ Branch 102 → 104 not taken.
✗ Branch 103 → 104 not taken.
✗ Branch 103 → 105 not taken.
|
2406 | QDBusServiceWatcher::WatchForOwnerChange, this); |
| 62 | 802 | connect(watcher, &QDBusServiceWatcher::serviceOwnerChanged, this, | |
| 63 |
2/2✓ Branch 46 → 47 taken 4 times.
✓ Branch 47 → 48 taken 798 times.
|
1604 | [this](const QString &, const QString &, const QString &newOwner) { |
| 64 | 2 | m_capabilities.clear(); | |
| 65 | 2 | m_capabilitiesLoaded = false; | |
| 66 | 2 | m_daemonPause = QDeadlineTimer(); // a new daemon is asked again | |
| 67 | 2 | m_available = !newOwner.isEmpty(); | |
| 68 | // Notification IDs and their capability tokens belong to one | ||
| 69 | // daemon generation. They are invalid as soon as its unique | ||
| 70 | // owner changes. | ||
| 71 | 2 | resetBackendGeneration(); | |
| 72 | 2 | }); | |
| 73 | } | ||
| 74 | } | ||
| 75 |
8/8✓ Branch 49 → 50 taken 4 times.
✓ Branch 50 → 51 taken 798 times.
✓ Branch 53 → 54 taken 4 times.
✓ Branch 54 → 55 taken 798 times.
✓ Branch 65 → 51 taken 4 times.
✓ Branch 65 → 66 taken 4 times.
✓ Branch 66 → 52 taken 798 times.
✓ Branch 66 → 67 taken 798 times.
|
1604 | qCInfo(lcNotifier) << (m_available ? "notifications available via" |
| 76 |
5/6✓ Branch 54 → 55 taken 2 times.
✓ Branch 54 → 56 taken 2 times.
✗ Branch 55 → 56 not taken.
✓ Branch 55 → 57 taken 798 times.
✓ Branch 57 → 58 taken 4 times.
✓ Branch 58 → 59 taken 798 times.
|
802 | : "notifications unavailable via") |
| 77 |
4/6✗ Branch 58 → 59 not taken.
✓ Branch 58 → 60 taken 4 times.
✗ Branch 59 → 60 not taken.
✓ Branch 59 → 61 taken 798 times.
✓ Branch 61 → 62 taken 4 times.
✓ Branch 62 → 63 taken 798 times.
|
802 | << (m_useKde ? "KDE" : "freedesktop"); |
| 78 | 802 | } | |
| 79 | |||
| 80 | 802 | DesktopNotifier::~DesktopNotifier() = default; | |
| 81 | |||
| 82 | 802 | void DesktopNotifier::setupDBusConnections() { | |
| 83 |
3/3✓ Branch 2 → 3 taken 802 times.
✓ Branch 3 → 4 taken 802 times.
✓ Branch 16 → 17 taken 802 times.
|
1604 | QDBusConnection::sessionBus().connect( |
| 84 | 1604 | QStringLiteral("org.freedesktop.Notifications"), | |
| 85 | 1604 | QStringLiteral("/org/freedesktop/Notifications"), | |
| 86 | 1604 | QStringLiteral("org.freedesktop.Notifications"), | |
| 87 | 1604 | QStringLiteral("ActionInvoked"), this, | |
| 88 | SLOT(onDBusActionInvoked(uint, QString))); | ||
| 89 |
3/3✓ Branch 26 → 27 taken 802 times.
✓ Branch 27 → 28 taken 802 times.
✓ Branch 40 → 41 taken 802 times.
|
1604 | QDBusConnection::sessionBus().connect( |
| 90 | 1604 | QStringLiteral("org.freedesktop.Notifications"), | |
| 91 | 1604 | QStringLiteral("/org/freedesktop/Notifications"), | |
| 92 | 1604 | QStringLiteral("org.freedesktop.Notifications"), | |
| 93 | 1604 | QStringLiteral("NotificationClosed"), this, | |
| 94 | SLOT(onDBusNotificationClosed(uint, uint))); | ||
| 95 |
3/3✓ Branch 50 → 51 taken 802 times.
✓ Branch 51 → 52 taken 802 times.
✓ Branch 64 → 65 taken 802 times.
|
1604 | QDBusConnection::sessionBus().connect( |
| 96 | 1604 | QStringLiteral("org.freedesktop.Notifications"), | |
| 97 | 1604 | QStringLiteral("/org/freedesktop/Notifications"), | |
| 98 | 1604 | QStringLiteral("org.freedesktop.Notifications"), | |
| 99 | 1604 | QStringLiteral("ActivationToken"), this, | |
| 100 | SLOT(onDBusActivationToken(uint, QString))); | ||
| 101 | 802 | } | |
| 102 | |||
| 103 | 12 | const QStringList &DesktopNotifier::capabilities() { | |
| 104 |
2/2✓ Branch 2 → 3 taken 4 times.
✓ Branch 2 → 4 taken 8 times.
|
12 | if (m_capabilitiesLoaded) |
| 105 | 4 | return m_capabilities; | |
| 106 |
2/2✓ Branch 8 → 9 taken 8 times.
✓ Branch 9 → 10 taken 8 times.
|
16 | const QDBusReply<QStringList> reply = callDaemon(QStringLiteral("GetCapabilities"), {}); |
| 107 | // A daemon that did not answer is asked again after its pause. | ||
| 108 | 8 | m_capabilitiesLoaded = m_daemonPause.hasExpired(); | |
| 109 |
3/3✓ Branch 15 → 16 taken 8 times.
✓ Branch 16 → 17 taken 2 times.
✓ Branch 16 → 21 taken 6 times.
|
8 | if (reply.isValid()) |
| 110 | 2 | m_capabilities = reply.value(); | |
| 111 | 8 | return m_capabilities; | |
| 112 | 8 | } | |
| 113 | |||
| 114 | 19 | QDBusMessage DesktopNotifier::callDaemon(const QString &method, const QList<QVariant> &arguments) { | |
| 115 |
2/2✓ Branch 3 → 4 taken 11 times.
✓ Branch 3 → 12 taken 8 times.
|
19 | if (!m_daemonPause.hasExpired()) |
| 116 |
1/1✓ Branch 7 → 8 taken 11 times.
|
11 | return QDBusMessage::createError(QDBusError::NoReply, QStringLiteral("notification daemon paused")); |
| 117 |
1/1✓ Branch 12 → 13 taken 8 times.
|
8 | const QDBusMessage reply = m_dbus->callWithArgumentList(QDBus::Block, method, arguments); |
| 118 |
4/5✓ Branch 13 → 14 taken 8 times.
✓ Branch 14 → 15 taken 1 time.
✓ Branch 14 → 28 taken 7 times.
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 27 taken 1 time.
|
9 | if (reply.type() == QDBusMessage::ErrorMessage && |
| 119 |
6/12✓ Branch 15 → 16 taken 1 time.
✓ Branch 16 → 17 taken 1 time.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 27 not taken.
✓ Branch 39 → 40 taken 1 time.
✓ Branch 39 → 41 taken 7 times.
✓ Branch 41 → 42 taken 1 time.
✓ Branch 41 → 58 taken 7 times.
✗ Branch 85 → 86 not taken.
✗ Branch 85 → 87 not taken.
✗ Branch 89 → 90 not taken.
✗ Branch 89 → 91 not taken.
|
9 | (reply.errorName() == QDBusError::errorString(QDBusError::NoReply) || |
| 120 |
3/12✗ Branch 19 → 20 not taken.
✗ Branch 20 → 21 not taken.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
✗ Branch 35 → 36 not taken.
✓ Branch 35 → 37 taken 8 times.
✓ Branch 37 → 38 taken 1 time.
✓ Branch 37 → 39 taken 7 times.
✗ Branch 77 → 78 not taken.
✗ Branch 77 → 79 not taken.
✗ Branch 81 → 82 not taken.
✗ Branch 81 → 83 not taken.
|
8 | reply.errorName() == QDBusError::errorString(QDBusError::Timeout) || |
| 121 |
3/12✗ Branch 23 → 24 not taken.
✗ Branch 24 → 25 not taken.
✗ Branch 29 → 30 not taken.
✓ Branch 29 → 31 taken 8 times.
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 33 taken 8 times.
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 8 times.
✗ Branch 69 → 70 not taken.
✗ Branch 69 → 71 not taken.
✗ Branch 73 → 74 not taken.
✗ Branch 73 → 75 not taken.
|
8 | reply.errorName() == QDBusError::errorString(QDBusError::TimedOut))) { |
| 122 | 1 | m_daemonPause.setRemainingTime(kDaemonPauseMs); | |
| 123 |
6/6✓ Branch 43 → 44 taken 1 time.
✓ Branch 47 → 48 taken 1 time.
✓ Branch 48 → 49 taken 1 time.
✓ Branch 49 → 50 taken 1 time.
✓ Branch 56 → 45 taken 1 time.
✓ Branch 56 → 57 taken 1 time.
|
2 | qCWarning(lcNotifier) << "Notification daemon did not answer" << method |
| 124 |
3/3✓ Branch 50 → 51 taken 1 time.
✓ Branch 51 → 52 taken 1 time.
✓ Branch 52 → 53 taken 1 time.
|
1 | << "- notifications paused for" << kDaemonPauseMs / 1000 << "s"; |
| 125 | } | ||
| 126 |
1/1✓ Branch 58 → 59 taken 8 times.
|
8 | return reply; |
| 127 | 8 | } | |
| 128 | |||
| 129 | #ifdef MAILJD_KDE_INTEGRATION | ||
| 130 | // F93-17: MailJD's own events come from share/knotifications6/ | ||
| 131 | // mailjd.notifyrc (configurable in the system settings). A build-tree run | ||
| 132 | // has no installed file; the standard notification event then avoids | ||
| 133 | // "No event config could be found" and still shows the popup. | ||
| 134 | 5 | KNotification *DesktopNotifier::kdeEvent(const QString &eventId, const QString &title, | |
| 135 | const QString &text, const QString &iconName, bool persistent) { | ||
| 136 | static const bool configured = | ||
| 137 |
1/1✓ Branch 9 → 10 taken 1 time.
|
2 | !QStandardPaths::locate(QStandardPaths::GenericDataLocation, |
| 138 |
3/6✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 16 taken 4 times.
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 16 not taken.
✗ Branch 60 → 61 not taken.
✗ Branch 60 → 62 not taken.
|
8 | QStringLiteral("knotifications6/mailjd.notifyrc")).isEmpty(); |
| 139 | const KNotification::NotificationFlags flags = | ||
| 140 |
2/2✓ Branch 16 → 17 taken 1 time.
✓ Branch 16 → 18 taken 4 times.
|
5 | persistent ? KNotification::Persistent : KNotification::CloseOnTimeout; |
| 141 | // F100-27: not sent yet — KNotification::event() sends at once, and | ||
| 142 | // actions added afterwards never reach the notification server: a click | ||
| 143 | // then only activated the app (via its desktop file) and showed whatever | ||
| 144 | // mail was open. The caller adds the actions and calls sendEvent(). | ||
| 145 | // Without the installed notifyrc: KNotification's standard event. | ||
| 146 |
5/12✓ Branch 20 → 21 taken 5 times.
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 5 times.
✓ Branch 26 → 27 taken 5 times.
✓ Branch 28 → 29 taken 5 times.
✗ Branch 28 → 30 not taken.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 5 times.
✗ Branch 65 → 66 not taken.
✗ Branch 65 → 67 not taken.
✗ Branch 70 → 71 not taken.
✗ Branch 70 → 72 not taken.
|
10 | auto *notification = new KNotification(configured ? eventId : QStringLiteral("notification"), flags); |
| 147 |
4/11✗ Branch 32 → 33 not taken.
✓ Branch 32 → 36 taken 5 times.
✓ Branch 39 → 40 taken 5 times.
✓ Branch 41 → 42 taken 5 times.
✗ Branch 41 → 43 not taken.
✗ Branch 43 → 44 not taken.
✓ Branch 43 → 45 taken 5 times.
✗ Branch 75 → 76 not taken.
✗ Branch 75 → 77 not taken.
✗ Branch 79 → 80 not taken.
✗ Branch 79 → 81 not taken.
|
10 | notification->setComponentName(configured ? QStringLiteral("mailjd") : QStringLiteral("plasma_workspace")); |
| 148 | // S106-200: Plasma renders a KNotification's text as markup (the title | ||
| 149 | // stays plain), so the text is HTML-escaped like the freedesktop path does | ||
| 150 | // for a body-markup server — mail From/Subject and ICS fields are attacker | ||
| 151 | // controlled and must not become links or formatting in the popup. | ||
| 152 |
1/1✓ Branch 45 → 46 taken 5 times.
|
5 | notification->setTitle(title); |
| 153 |
2/2✓ Branch 46 → 47 taken 5 times.
✓ Branch 47 → 48 taken 5 times.
|
5 | notification->setText(text.toHtmlEscaped()); |
| 154 |
1/1✓ Branch 49 → 50 taken 5 times.
|
5 | notification->setIconName(iconName); |
| 155 | 5 | return notification; | |
| 156 | } | ||
| 157 | #endif | ||
| 158 | |||
| 159 | 7 | uint DesktopNotifier::notify(const QString &summary, const QString &body, | |
| 160 | const QString &iconName, int timeoutMs, | ||
| 161 | uint replacesId) { | ||
| 162 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 6 times.
|
7 | if (!m_available) |
| 163 | 1 | return 0; | |
| 164 | #ifdef MAILJD_KDE_INTEGRATION | ||
| 165 |
1/2✓ Branch 4 → 5 taken 2 times.
✗ Branch 4 → 37 not taken.
|
2 | if (m_useKde) { |
| 166 | Q_UNUSED(timeoutMs) | ||
| 167 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 2 times.
|
2 | if (replacesId != 0) |
| 168 | ✗ | close(replacesId); | |
| 169 |
1/1✓ Branch 20 → 21 taken 2 times.
|
2 | auto *notification = kdeEvent( |
| 170 |
1/1✓ Branch 16 → 17 taken 2 times.
|
6 | QStringLiteral("new-mail"), formatNotificationText(summary, false), |
| 171 |
1/1✓ Branch 14 → 15 taken 2 times.
|
4 | formatNotificationText(body, false), |
| 172 |
2/6✓ Branch 8 → 9 taken 2 times.
✗ Branch 8 → 12 not taken.
✓ Branch 28 → 29 taken 2 times.
✗ Branch 28 → 30 not taken.
✗ Branch 183 → 184 not taken.
✗ Branch 183 → 185 not taken.
|
6 | iconName.isEmpty() ? QStringLiteral("mail-message-new") : iconName); |
| 173 | 2 | const uint id = m_nextId++; | |
| 174 |
1/1✓ Branch 30 → 31 taken 2 times.
|
2 | m_activeNotifications.insert(id, notification); |
| 175 |
1/1✓ Branch 31 → 32 taken 2 times.
|
2 | configureKdeActions(notification, id, true); |
| 176 |
1/1✓ Branch 32 → 33 taken 2 times.
|
2 | connect(notification, &KNotification::closed, this, [this, id]() { |
| 177 | 1 | m_activeNotifications.remove(id); | |
| 178 | 1 | emit notificationClosed(id); | |
| 179 | 1 | }); | |
| 180 |
1/3✗ Branch 34 → 35 not taken.
✓ Branch 34 → 36 taken 2 times.
✗ Branch 35 → 36 not taken.
|
2 | if (m_sendKdeEvents) notification->sendEvent(); |
| 181 | 2 | return id; | |
| 182 | } | ||
| 183 | #endif | ||
| 184 | 4 | QVariantMap hints; | |
| 185 |
1/2✓ Branch 8 → 9 taken 4 times.
✗ Branch 41 → 42 not taken.
|
8 | hints[QStringLiteral("urgency")] = 1; |
| 186 |
1/2✓ Branch 20 → 21 taken 4 times.
✗ Branch 53 → 54 not taken.
|
8 | hints[QStringLiteral("category")] = QStringLiteral("email.arrived"); |
| 187 |
1/2✓ Branch 34 → 35 taken 4 times.
✗ Branch 67 → 68 not taken.
|
8 | hints[QStringLiteral("desktop-entry")] = QStringLiteral("mailjd"); |
| 188 |
1/2✓ Branch 41 → 42 taken 4 times.
✗ Branch 74 → 75 not taken.
|
4 | const QStringList &serviceCapabilities = capabilities(); |
| 189 | const bool supportsMarkup = | ||
| 190 | 4 | serviceCapabilities.contains(QStringLiteral("body-markup")); | |
| 191 | 4 | QStringList actions; | |
| 192 |
2/4✓ Branch 54 → 55 taken 2 times.
✓ Branch 54 → 74 taken 2 times.
✗ Branch 87 → 88 not taken.
✗ Branch 87 → 107 not taken.
|
4 | if (serviceCapabilities.contains(QStringLiteral("actions"))) |
| 193 | // The notification specification reserves "default" for activating the | ||
| 194 | // notification body itself. Shells such as GNOME do not treat an | ||
| 195 | // arbitrary first action key (previously "open") as the body click. | ||
| 196 |
3/6✓ Branch 58 → 59 taken 2 times.
✓ Branch 59 → 60 taken 2 times.
✓ Branch 60 → 61 taken 2 times.
✗ Branch 91 → 92 not taken.
✗ Branch 92 → 93 not taken.
✗ Branch 93 → 94 not taken.
|
4 | actions << QStringLiteral("default") << tr("Open") |
| 197 |
3/6✓ Branch 64 → 65 taken 2 times.
✓ Branch 65 → 66 taken 2 times.
✓ Branch 66 → 67 taken 2 times.
✗ Branch 97 → 98 not taken.
✗ Branch 98 → 99 not taken.
✗ Branch 99 → 100 not taken.
|
2 | << QStringLiteral("mark-read") << tr("Mark Read"); |
| 198 |
3/10✓ Branch 99 → 100 taken 4 times.
✓ Branch 106 → 107 taken 32 times.
✓ Branch 106 → 108 taken 4 times.
✗ Branch 132 → 133 not taken.
✗ Branch 139 → 140 not taken.
✗ Branch 139 → 141 not taken.
✗ Branch 198 → 199 not taken.
✗ Branch 198 → 200 not taken.
✗ Branch 260 → 261 not taken.
✗ Branch 260 → 262 not taken.
|
48 | const QDBusReply<uint> reply = callDaemon(QStringLiteral("Notify"), {QStringLiteral("MailJD"), replacesId, |
| 199 |
4/11✓ Branch 80 → 81 taken 3 times.
✓ Branch 80 → 84 taken 1 time.
✓ Branch 113 → 114 taken 3 times.
✓ Branch 113 → 115 taken 1 time.
✗ Branch 113 → 117 not taken.
✗ Branch 146 → 147 not taken.
✗ Branch 146 → 148 not taken.
✗ Branch 215 → 216 not taken.
✗ Branch 215 → 217 not taken.
✗ Branch 277 → 278 not taken.
✗ Branch 277 → 279 not taken.
|
11 | iconName.isEmpty() ? QStringLiteral("mail-message-new") : iconName, |
| 200 |
1/2✓ Branch 87 → 88 taken 4 times.
✗ Branch 120 → 121 not taken.
|
8 | formatNotificationText(summary, supportsMarkup), |
| 201 |
2/4✓ Branch 90 → 91 taken 4 times.
✓ Branch 100 → 101 taken 4 times.
✗ Branch 123 → 124 not taken.
✗ Branch 133 → 134 not taken.
|
12 | formatNotificationText(body, supportsMarkup), actions, hints, timeoutMs}); |
| 202 |
3/6✓ Branch 117 → 118 taken 4 times.
✓ Branch 118 → 119 taken 1 time.
✓ Branch 118 → 120 taken 3 times.
✗ Branch 150 → 151 not taken.
✗ Branch 151 → 152 not taken.
✗ Branch 151 → 153 not taken.
|
4 | if (!reply.isValid()) |
| 203 | 1 | return 0; | |
| 204 |
2/4✓ Branch 120 → 121 taken 1 time.
✓ Branch 120 → 122 taken 2 times.
✗ Branch 153 → 154 not taken.
✗ Branch 153 → 155 not taken.
|
3 | if (replacesId != 0) |
| 205 |
1/2✓ Branch 121 → 122 taken 1 time.
✗ Branch 154 → 155 not taken.
|
1 | clearNotificationState(replacesId); |
| 206 |
1/2✓ Branch 123 → 124 taken 3 times.
✗ Branch 156 → 157 not taken.
|
3 | m_activeNotifications.insert(reply.value(), nullptr); |
| 207 | 3 | return reply.value(); | |
| 208 | 24 | } | |
| 209 | |||
| 210 | 10 | uint DesktopNotifier::notifySummary(const QString &title, const QString &body, | |
| 211 | uint replacesId) { | ||
| 212 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 9 times.
|
10 | if (!m_available) |
| 213 | 1 | return 0; | |
| 214 | #ifdef MAILJD_KDE_INTEGRATION | ||
| 215 |
1/2✓ Branch 4 → 5 taken 2 times.
✗ Branch 4 → 33 not taken.
|
2 | if (m_useKde) { |
| 216 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 2 times.
|
2 | if (replacesId != 0) |
| 217 | ✗ | close(replacesId); | |
| 218 |
1/1✓ Branch 17 → 18 taken 2 times.
|
2 | auto *notification = kdeEvent( |
| 219 |
1/1✓ Branch 13 → 14 taken 2 times.
|
6 | QStringLiteral("new-mail"), formatNotificationText(title, false), |
| 220 |
1/1✓ Branch 11 → 12 taken 2 times.
|
4 | formatNotificationText(body, false), QStringLiteral("mail-message-new")); |
| 221 | 2 | const uint id = m_nextId++; | |
| 222 |
1/1✓ Branch 26 → 27 taken 2 times.
|
2 | m_activeNotifications.insert(id, notification); |
| 223 |
1/1✓ Branch 27 → 28 taken 2 times.
|
2 | configureKdeActions(notification, id, false); |
| 224 |
1/1✓ Branch 28 → 29 taken 2 times.
|
2 | connect(notification, &KNotification::closed, this, [this, id]() { |
| 225 | 1 | m_activeNotifications.remove(id); | |
| 226 | 1 | emit notificationClosed(id); | |
| 227 | 1 | }); | |
| 228 |
1/3✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 2 times.
✗ Branch 31 → 32 not taken.
|
2 | if (m_sendKdeEvents) notification->sendEvent(); |
| 229 | 2 | return id; | |
| 230 | } | ||
| 231 | #endif | ||
| 232 | 7 | QVariantMap hints; | |
| 233 |
1/2✓ Branch 8 → 9 taken 7 times.
✗ Branch 37 → 38 not taken.
|
14 | hints[QStringLiteral("urgency")] = 1; |
| 234 |
1/2✓ Branch 20 → 21 taken 7 times.
✗ Branch 49 → 50 not taken.
|
14 | hints[QStringLiteral("category")] = QStringLiteral("email.arrived"); |
| 235 |
1/2✓ Branch 34 → 35 taken 7 times.
✗ Branch 63 → 64 not taken.
|
14 | hints[QStringLiteral("desktop-entry")] = QStringLiteral("mailjd"); |
| 236 |
1/2✓ Branch 41 → 42 taken 7 times.
✗ Branch 70 → 71 not taken.
|
7 | const QStringList &serviceCapabilities = capabilities(); |
| 237 | const bool supportsMarkup = | ||
| 238 | 7 | serviceCapabilities.contains(QStringLiteral("body-markup")); | |
| 239 | 7 | QStringList actions; | |
| 240 |
2/4✓ Branch 54 → 55 taken 1 time.
✓ Branch 54 → 65 taken 6 times.
✗ Branch 83 → 84 not taken.
✗ Branch 83 → 94 not taken.
|
7 | if (serviceCapabilities.contains(QStringLiteral("actions"))) |
| 241 |
3/6✓ Branch 58 → 59 taken 1 time.
✓ Branch 59 → 60 taken 1 time.
✓ Branch 60 → 61 taken 1 time.
✗ Branch 87 → 88 not taken.
✗ Branch 88 → 89 not taken.
✗ Branch 89 → 90 not taken.
|
1 | actions << QStringLiteral("default") << tr("Open"); |
| 242 |
3/10✓ Branch 87 → 88 taken 7 times.
✓ Branch 94 → 95 taken 56 times.
✓ Branch 94 → 96 taken 7 times.
✗ Branch 116 → 117 not taken.
✗ Branch 123 → 124 not taken.
✗ Branch 123 → 125 not taken.
✗ Branch 176 → 177 not taken.
✗ Branch 176 → 178 not taken.
✗ Branch 233 → 234 not taken.
✗ Branch 233 → 235 not taken.
|
84 | const QDBusReply<uint> reply = callDaemon(QStringLiteral("Notify"), {QStringLiteral("MailJD"), replacesId, |
| 243 | 14 | QStringLiteral("mail-message-new"), | |
| 244 |
1/2✓ Branch 75 → 76 taken 7 times.
✗ Branch 104 → 105 not taken.
|
14 | formatNotificationText(title, supportsMarkup), |
| 245 |
2/4✓ Branch 78 → 79 taken 7 times.
✓ Branch 88 → 89 taken 7 times.
✗ Branch 107 → 108 not taken.
✗ Branch 117 → 118 not taken.
|
21 | formatNotificationText(body, supportsMarkup), actions, hints, 5000}); |
| 246 |
3/6✓ Branch 104 → 105 taken 7 times.
✓ Branch 105 → 106 taken 5 times.
✓ Branch 105 → 107 taken 2 times.
✗ Branch 133 → 134 not taken.
✗ Branch 134 → 135 not taken.
✗ Branch 134 → 136 not taken.
|
7 | if (!reply.isValid()) |
| 247 | 5 | return 0; | |
| 248 |
2/4✓ Branch 107 → 108 taken 1 time.
✓ Branch 107 → 109 taken 1 time.
✗ Branch 136 → 137 not taken.
✗ Branch 136 → 138 not taken.
|
2 | if (replacesId != 0) |
| 249 |
1/2✓ Branch 108 → 109 taken 1 time.
✗ Branch 137 → 138 not taken.
|
1 | clearNotificationState(replacesId); |
| 250 |
1/2✓ Branch 110 → 111 taken 2 times.
✗ Branch 139 → 140 not taken.
|
2 | m_activeNotifications.insert(reply.value(), nullptr); |
| 251 | 2 | return reply.value(); | |
| 252 | 42 | } | |
| 253 | |||
| 254 | #ifdef MAILJD_KDE_INTEGRATION | ||
| 255 | 5 | void DesktopNotifier::configureKdeActions(KNotification *notification, uint id, | |
| 256 | bool includeMarkRead) { | ||
| 257 |
1/1✓ Branch 2 → 3 taken 5 times.
|
5 | m_kdeActionsForTest.clear(); |
| 258 |
2/2✓ Branch 3 → 4 taken 5 times.
✓ Branch 4 → 5 taken 5 times.
|
5 | auto *openAction = notification->addDefaultAction(tr("Open")); |
| 259 |
1/1✓ Branch 6 → 7 taken 5 times.
|
5 | m_kdeActionsForTest.append(openAction); |
| 260 |
1/1✓ Branch 7 → 8 taken 5 times.
|
5 | const QPointer<KNotification> guardedNotification(notification); |
| 261 |
1/1✓ Branch 9 → 10 taken 5 times.
|
5 | connect(openAction, &KNotificationAction::activated, this, |
| 262 | 10 | [this, id, guardedNotification]() { | |
| 263 | const QString token = | ||
| 264 |
1/2✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 6 not taken.
|
1 | guardedNotification ? guardedNotification->xdgActivationToken() |
| 265 |
1/1✓ Branch 5 → 7 taken 1 time.
|
1 | : QString(); |
| 266 |
1/1✓ Branch 10 → 11 taken 1 time.
|
2 | emit actionInvoked(id, QStringLiteral("open"), token); |
| 267 | 1 | }); | |
| 268 |
2/2✓ Branch 12 → 13 taken 3 times.
✓ Branch 12 → 22 taken 2 times.
|
5 | if (includeMarkRead) { |
| 269 |
2/2✓ Branch 13 → 14 taken 3 times.
✓ Branch 14 → 15 taken 3 times.
|
3 | auto *markReadAction = notification->addAction(tr("Mark Read")); |
| 270 |
1/1✓ Branch 16 → 17 taken 3 times.
|
3 | m_kdeActionsForTest.append(markReadAction); |
| 271 |
1/1✓ Branch 18 → 19 taken 3 times.
|
3 | connect(markReadAction, &KNotificationAction::activated, this, |
| 272 | 6 | [this, id, guardedNotification]() { | |
| 273 | const QString token = | ||
| 274 | 1 | guardedNotification | |
| 275 |
1/2✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 6 not taken.
|
1 | ? guardedNotification->xdgActivationToken() |
| 276 |
1/1✓ Branch 5 → 7 taken 1 time.
|
1 | : QString(); |
| 277 |
1/1✓ Branch 10 → 11 taken 1 time.
|
2 | emit actionInvoked(id, QStringLiteral("mark-read"), token); |
| 278 | 1 | }); | |
| 279 | } | ||
| 280 | 5 | } | |
| 281 | #endif | ||
| 282 | |||
| 283 | 1 | uint DesktopNotifier::notifyReminder(const QString &title, const QString &body, | |
| 284 | const QList<QPair<QString, QString>> &actions, uint replacesId) { | ||
| 285 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1 time.
|
1 | if (!m_available) |
| 286 | ✗ | return 0; | |
| 287 | #ifdef MAILJD_KDE_INTEGRATION | ||
| 288 |
1/2✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 53 not taken.
|
1 | if (m_useKde) { |
| 289 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 1 time.
|
1 | if (replacesId != 0) |
| 290 | ✗ | close(replacesId); | |
| 291 |
2/2✓ Branch 13 → 14 taken 1 time.
✓ Branch 17 → 18 taken 1 time.
|
2 | auto *notification = kdeEvent(QStringLiteral("appointment-reminder"), formatNotificationText(title, false), |
| 292 |
1/1✓ Branch 11 → 12 taken 1 time.
|
2 | formatNotificationText(body, false), QStringLiteral("appointment-soon"), true); |
| 293 | 1 | const uint id = m_nextId++; | |
| 294 |
1/1✓ Branch 26 → 27 taken 1 time.
|
1 | m_activeNotifications.insert(id, notification); |
| 295 |
1/1✓ Branch 27 → 28 taken 1 time.
|
1 | const QPointer<KNotification> guarded(notification); |
| 296 |
2/2✓ Branch 46 → 29 taken 1 time.
✓ Branch 46 → 47 taken 1 time.
|
2 | for (int i = 0; i < actions.size(); ++i) { |
| 297 | 1 | const QString key = actions.at(i).first; | |
| 298 |
2/3✓ Branch 31 → 32 taken 1 time.
✗ Branch 31 → 35 not taken.
✓ Branch 33 → 34 taken 1 time.
|
1 | auto *action = i == 0 ? notification->addDefaultAction(actions.at(i).second) |
| 299 | ✗ | : notification->addAction(actions.at(i).second); | |
| 300 |
1/3✓ Branch 40 → 41 taken 1 time.
✗ Branch 198 → 199 not taken.
✗ Branch 198 → 200 not taken.
|
1 | connect(action, &KNotificationAction::activated, this, [this, id, key, guarded]() { |
| 301 | ✗ | emit actionInvoked(id, key, guarded ? guarded->xdgActivationToken() : QString()); | |
| 302 | ✗ | }); | |
| 303 | 1 | } | |
| 304 |
1/1✓ Branch 47 → 48 taken 1 time.
|
1 | connect(notification, &KNotification::closed, this, [this, id]() { |
| 305 | ✗ | m_activeNotifications.remove(id); | |
| 306 | ✗ | emit notificationClosed(id); | |
| 307 | ✗ | }); | |
| 308 |
1/3✗ Branch 49 → 50 not taken.
✓ Branch 49 → 51 taken 1 time.
✗ Branch 50 → 51 not taken.
|
1 | if (m_sendKdeEvents) notification->sendEvent(); |
| 309 | 1 | return id; | |
| 310 | 1 | } | |
| 311 | #endif | ||
| 312 | ✗ | QVariantMap hints; | |
| 313 | ✗ | hints[QStringLiteral("urgency")] = 2; // critical: stays until acted on | |
| 314 | ✗ | hints[QStringLiteral("category")] = QStringLiteral("x-mailjd.reminder"); | |
| 315 | ✗ | hints[QStringLiteral("desktop-entry")] = QStringLiteral("mailjd"); | |
| 316 | ✗ | QStringList actionList; | |
| 317 | ✗ | if (capabilities().contains(QStringLiteral("actions"))) | |
| 318 | ✗ | for (int i = 0; i < actions.size(); ++i) | |
| 319 | // The specification reserves "default" for the body click. | ||
| 320 | ✗ | actionList << (i == 0 ? QStringLiteral("default") : actions.at(i).first) << actions.at(i).second; | |
| 321 | ✗ | const bool markup = capabilities().contains(QStringLiteral("body-markup")); | |
| 322 | ✗ | const QDBusReply<uint> reply = callDaemon(QStringLiteral("Notify"), {QStringLiteral("MailJD"), replacesId, QStringLiteral("appointment-soon"), | |
| 323 | ✗ | formatNotificationText(title, markup), formatNotificationText(body, markup), actionList, hints, 0}); | |
| 324 | ✗ | if (!reply.isValid()) | |
| 325 | ✗ | return 0; | |
| 326 | ✗ | m_activeNotifications.insert(reply.value(), nullptr); | |
| 327 | ✗ | return reply.value(); | |
| 328 | ✗ | } | |
| 329 | |||
| 330 | 2 | void DesktopNotifier::close(uint id) { | |
| 331 | 2 | m_activationTokens.remove(id); | |
| 332 | #ifdef MAILJD_KDE_INTEGRATION | ||
| 333 | ✗ | if (m_useKde) { | |
| 334 | ✗ | const auto iterator = m_activeNotifications.find(id); | |
| 335 | ✗ | if (iterator != m_activeNotifications.end()) { | |
| 336 | ✗ | iterator.value()->deleteLater(); | |
| 337 | ✗ | m_activeNotifications.erase(iterator); | |
| 338 | } | ||
| 339 | ✗ | return; | |
| 340 | } | ||
| 341 | #endif | ||
| 342 | // Nothing waits for the answer (S106-212). | ||
| 343 |
4/12✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 7 taken 1 time.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 1 time.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 17 taken 2 times.
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 18 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 28 not taken.
|
2 | if (m_available && m_daemonPause.hasExpired()) |
| 344 | ✗ | m_dbus->asyncCall(QStringLiteral("CloseNotification"), id); | |
| 345 | 2 | } | |
| 346 | |||
| 347 | 34 | bool DesktopNotifier::isAvailable() const { return m_available; } | |
| 348 | |||
| 349 | 4 | void DesktopNotifier::onDBusActionInvoked(uint id, const QString &action) { | |
| 350 | const QString normalizedAction = | ||
| 351 |
4/4✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 10 taken 3 times.
✓ Branch 11 → 12 taken 1 time.
✓ Branch 11 → 13 taken 3 times.
|
5 | action == QStringLiteral("default") ? QStringLiteral("open") : action; |
| 352 |
2/2✓ Branch 15 → 16 taken 4 times.
✓ Branch 16 → 17 taken 4 times.
|
4 | emit actionInvoked(id, normalizedAction, takeActivationToken(id)); |
| 353 | 4 | } | |
| 354 | |||
| 355 | 2 | void DesktopNotifier::onDBusNotificationClosed(uint id, uint) { | |
| 356 | 2 | clearNotificationState(id); | |
| 357 | 2 | emit notificationClosed(id); | |
| 358 | 2 | } | |
| 359 | |||
| 360 | 15 | void DesktopNotifier::onDBusActivationToken(uint id, | |
| 361 | const QString &activationToken) { | ||
| 362 | // Accept only a live notification ID from this daemon generation. The | ||
| 363 | // capability is kept in memory for at most five seconds and is paired only | ||
| 364 | // with the next ActionInvoked carrying the same ID. | ||
| 365 |
8/8✓ Branch 3 → 4 taken 14 times.
✓ Branch 3 → 8 taken 1 time.
✓ Branch 5 → 6 taken 13 times.
✓ Branch 5 → 8 taken 1 time.
✓ Branch 7 → 8 taken 1 time.
✓ Branch 7 → 9 taken 12 times.
✓ Branch 10 → 11 taken 3 times.
✓ Branch 10 → 12 taken 12 times.
|
28 | if (!m_activeNotifications.contains(id) || activationToken.isEmpty() || |
| 366 | 13 | activationToken.size() > 4096) | |
| 367 | 3 | return; | |
| 368 |
2/2✓ Branch 23 → 14 taken 136 times.
✓ Branch 23 → 24 taken 10 times.
|
146 | for (const QChar character : activationToken) { |
| 369 |
6/6✓ Branch 15 → 16 taken 135 times.
✓ Branch 15 → 18 taken 1 time.
✓ Branch 17 → 18 taken 1 time.
✓ Branch 17 → 19 taken 134 times.
✓ Branch 20 → 21 taken 2 times.
✓ Branch 20 → 22 taken 134 times.
|
136 | if (character.isNull() || character.category() == QChar::Other_Control) |
| 370 | 2 | return; | |
| 371 | } | ||
| 372 | 20 | m_activationTokens.insert( | |
| 373 |
1/3✓ Branch 26 → 27 taken 10 times.
✗ Branch 35 → 36 not taken.
✗ Branch 35 → 37 not taken.
|
10 | id, PendingActivationToken{activationToken, QDeadlineTimer(5000)}); |
| 374 |
1/1✓ Branch 29 → 30 taken 10 times.
|
10 | QTimer::singleShot(5000, this, [this, id, activationToken]() { |
| 375 | ✗ | expireActivationToken(id, activationToken); | |
| 376 | ✗ | }); | |
| 377 | } | ||
| 378 | |||
| 379 | 4 | void DesktopNotifier::expireActivationToken(uint id, | |
| 380 | const QString &expectedValue) { | ||
| 381 |
1/1✓ Branch 2 → 3 taken 4 times.
|
4 | const auto iterator = m_activationTokens.find(id); |
| 382 |
7/7✓ Branch 3 → 4 taken 4 times.
✓ Branch 5 → 6 taken 3 times.
✓ Branch 5 → 10 taken 1 time.
✓ Branch 8 → 9 taken 2 times.
✓ Branch 8 → 10 taken 1 time.
✓ Branch 11 → 12 taken 2 times.
✓ Branch 11 → 15 taken 2 times.
|
4 | if (iterator != m_activationTokens.end() && iterator->value == expectedValue) |
| 383 |
1/1✓ Branch 13 → 14 taken 2 times.
|
2 | m_activationTokens.erase(iterator); |
| 384 | 4 | } | |
| 385 | |||
| 386 | 6 | QString DesktopNotifier::takeActivationToken(uint id) { | |
| 387 |
1/1✓ Branch 2 → 3 taken 6 times.
|
6 | const auto iterator = m_activationTokens.find(id); |
| 388 |
3/3✓ Branch 3 → 4 taken 6 times.
✓ Branch 5 → 6 taken 3 times.
✓ Branch 5 → 7 taken 3 times.
|
6 | if (iterator == m_activationTokens.end()) |
| 389 | 3 | return {}; | |
| 390 | 3 | const PendingActivationToken pending = iterator.value(); | |
| 391 |
1/1✓ Branch 10 → 11 taken 3 times.
|
3 | m_activationTokens.erase(iterator); |
| 392 |
2/2✓ Branch 12 → 13 taken 1 time.
✓ Branch 12 → 14 taken 2 times.
|
3 | if (pending.expiry.hasExpired()) |
| 393 | 1 | return {}; | |
| 394 | 2 | return pending.value; | |
| 395 | 3 | } | |
| 396 | |||
| 397 | 5 | void DesktopNotifier::clearNotificationState(uint id) { | |
| 398 | 5 | m_activationTokens.remove(id); | |
| 399 | 5 | m_activeNotifications.remove(id); | |
| 400 | 5 | } | |
| 401 | |||
| 402 | 3 | void DesktopNotifier::resetBackendGeneration() { | |
| 403 | 3 | m_activeNotifications.clear(); | |
| 404 | 3 | m_activationTokens.clear(); | |
| 405 | 3 | } | |
| 406 |