Port to new KNotifications action API

This commit is contained in:
Nicolas Fella 2023-07-23 22:52:52 +02:00
parent 4b1b1b9b14
commit 0343c9401d
4 changed files with 62 additions and 15 deletions

View file

@ -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();
}

View file

@ -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();

View file

@ -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();

View file

@ -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();