From 0343c9401dd46c8c5e304d3fd76cdf85725e8d1f Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Sun, 23 Jul 2023 22:52:52 +0200 Subject: [PATCH] Port to new KNotifications action API --- daemon/kdeconnectd.cpp | 24 ++++++++++++++--- plugins/notifications/notification.cpp | 12 +++++++++ plugins/share/shareplugin.cpp | 36 ++++++++++++++++++-------- plugins/telephony/telephonyplugin.cpp | 5 ++++ 4 files changed, 62 insertions(+), 15 deletions(-) diff --git a/daemon/kdeconnectd.cpp b/daemon/kdeconnectd.cpp index 525822f63..c0eea676d 100644 --- a/daemon/kdeconnectd.cpp +++ b/daemon/kdeconnectd.cpp @@ -54,18 +54,34 @@ public: notification->setTitle(QStringLiteral("KDE Connect")); notification->setText( i18n("Pairing request from %1\nKey: %2...", device->name().toHtmlEscaped(), QString::fromUtf8(device->verificationKey().left(8)))); - notification->setDefaultAction(i18n("Open")); - notification->setActions(QStringList{i18n("Accept"), i18n("Reject"), i18n("View key")}); - connect(notification, &KNotification::action1Activated, device, &Device::acceptPairing); - connect(notification, &KNotification::action2Activated, device, &Device::cancelPairing); QString deviceId = device->id(); auto openSettings = [deviceId, notification] { OpenConfig oc; oc.setXdgActivationToken(notification->xdgActivationToken()); oc.openConfiguration(deviceId); }; + +#if QT_VERSION_MAJOR == 6 + KNotificationAction *openSettingsAction = notification->addDefaultAction(i18n("Open")); + connect(openSettingsAction, &KNotificationAction::activated, openSettings); + + KNotificationAction *acceptAction = notification->addAction(i18n("Accept")); + connect(acceptAction, &KNotificationAction::activated, device, &Device::acceptPairing); + + KNotificationAction *rejectAction = notification->addAction(i18n("Reject")); + connect(rejectAction, &KNotificationAction::activated, device, &Device::cancelPairing); + + KNotificationAction *viewKeyAction = notification->addAction(i18n("View key")); + connect(viewKeyAction, &KNotificationAction::activated, openSettings); +#else + notification->setDefaultAction(i18n("Open")); + notification->setActions(QStringList() << i18n("Accept") << i18n("Reject") << i18n("View key")); + connect(notification, &KNotification::action1Activated, device, &Device::acceptPairing); + connect(notification, &KNotification::action2Activated, device, &Device::cancelPairing); connect(notification, &KNotification::action3Activated, openSettings); connect(notification, &KNotification::activated, openSettings); +#endif + notification->sendEvent(); } diff --git a/plugins/notifications/notification.cpp b/plugins/notifications/notification.cpp index ae24e1020..24ad8fbe0 100644 --- a/plugins/notifications/notification.cpp +++ b/plugins/notifications/notification.cpp @@ -45,10 +45,12 @@ Notification::Notification(const NetworkPacket &np, const Device *device, QObjec parseNetworkPacket(np); createKNotification(np); +#if QT_VERSION_MAJOR == 5 connect(m_notification, &KNotification::activated, this, [this](unsigned int actionIndex) { // Notification action indices start at 1 Q_EMIT actionTriggered(m_internalId, m_actions[actionIndex - 1]); }); +#endif } void Notification::dismiss() @@ -118,7 +120,17 @@ void Notification::createKNotification(const NetworkPacket &np) m_notification->setReplyAction(std::move(replyAction)); } +#if QT_VERSION_MAJOR == 6 + for (const QString &actionId : std::as_const(m_actions)) { + KNotificationAction *action = m_notification->addAction(actionId); + + connect(action, &KNotificationAction::activated, this, [this, actionId] { + Q_EMIT actionTriggered(m_internalId, actionId); + }); + } +#else m_notification->setActions(m_actions); +#endif m_hasIcon = m_hasIcon && !m_payloadHash.isEmpty(); diff --git a/plugins/share/shareplugin.cpp b/plugins/share/shareplugin.cpp index a14774fa0..2cd982d52 100644 --- a/plugins/share/shareplugin.cpp +++ b/plugins/share/shareplugin.cpp @@ -158,15 +158,8 @@ void SharePlugin::receivePacket(const NetworkPacket &np) notif->setComponentName(QStringLiteral("kdeconnect")); notif->setText(text); notif->setTitle(i18nc("@info Some piece of text was received from a connected device", "Shared text from %1 copied to clipboard", device()->name())); - QStringList actions; - actions << i18nc("@action:button Edit text with default text editor", "Open in Text Editor"); - if (url.isValid() && (url.scheme() == QStringLiteral("http") || url.scheme() == QStringLiteral("https"))) { - qDebug() << url; - actions << i18nc("@action:button Open URL with default app", "Open Link"); - } - notif->setActions(actions); - connect(notif, &KNotification::action1Activated, this, [this, text]() { + auto openTextEditor = [this, text] { KService::Ptr service = KApplicationTrader::preferredService(QStringLiteral("text/plain")); const QString defaultApp = service ? service->desktopEntryName() : QString(); @@ -188,12 +181,33 @@ void SharePlugin::receivePacket(const NetworkPacket &np) QDesktopServices::openUrl(QUrl::fromLocalFile(fileName)); Q_EMIT shareReceived(fileName); } - }); + }; - connect(notif, &KNotification::action2Activated, this, [this, url]() { + auto openUrl = [this, url] { QDesktopServices::openUrl(url); Q_EMIT shareReceived(url.toString()); - }); + }; + +#if QT_VERSION_MAJOR == 6 + KNotificationAction *textEditorAction = notif->addAction(i18nc("@action:button Edit text with default text editor", "Open in Text Editor")); + connect(textEditorAction, &KNotificationAction::activated, this, openTextEditor); + + if (url.isValid() && (url.scheme() == QStringLiteral("http") || url.scheme() == QStringLiteral("https"))) { + KNotificationAction *openLinkAction = notif->addAction(i18nc("@action:button Open URL with default app", "Open Link")); + connect(openLinkAction, &KNotificationAction::activated, this, openUrl); + } +#else + QStringList actions; + actions << i18nc("@action:button Edit text with default text editor", "Open in Text Editor"); + if (url.isValid() && (url.scheme() == QStringLiteral("http") || url.scheme() == QStringLiteral("https"))) { + actions << i18nc("@action:button Open URL with default app", "Open Link"); + } + notif->setActions(actions); + + connect(notif, &KNotification::action1Activated, this, openTextEditor); + + connect(notif, &KNotification::action2Activated, this, openUrl); +#endif notif->sendEvent(); diff --git a/plugins/telephony/telephonyplugin.cpp b/plugins/telephony/telephonyplugin.cpp index 1e5a72936..0701d3785 100644 --- a/plugins/telephony/telephonyplugin.cpp +++ b/plugins/telephony/telephonyplugin.cpp @@ -61,8 +61,13 @@ void TelephonyPlugin::createNotification(const NetworkPacket &np) m_currentCallNotification->setText(content); if (event == QLatin1String("ringing")) { +#if QT_VERSION_MAJOR == 6 + KNotificationAction *muteAction = m_currentCallNotification->addAction(i18n("Mute Call")); + connect(muteAction, &KNotificationAction::activated, this, &TelephonyPlugin::sendMutePacket); +#else m_currentCallNotification->setActions(QStringList(i18n("Mute Call"))); connect(m_currentCallNotification, &KNotification::action1Activated, this, &TelephonyPlugin::sendMutePacket); +#endif } m_currentCallNotification->sendEvent();