2013-08-20 13:05:22 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
|
2013-08-20 12:55:03 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2013-08-20 12:55:03 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notification.h"
|
2023-08-06 11:00:02 +01:00
|
|
|
#include "plugin_notifications_debug.h"
|
2013-08-20 12:55:03 +01:00
|
|
|
|
2023-07-21 10:51:06 +01:00
|
|
|
#include <KLocalizedString>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <KNotification>
|
2021-03-08 20:25:47 +00:00
|
|
|
#include <KNotificationReplyAction>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QFile>
|
2017-04-13 20:31:46 +01:00
|
|
|
#include <QIcon>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QPixmap>
|
2017-04-13 20:31:46 +01:00
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QtGlobal>
|
2019-06-02 15:03:11 +01:00
|
|
|
#include <knotifications_version.h>
|
2019-02-08 22:47:41 +00:00
|
|
|
|
2017-04-13 20:31:46 +01:00
|
|
|
#include <core/filetransferjob.h>
|
2019-06-02 15:03:11 +01:00
|
|
|
#include <core/notificationserverinfo.h>
|
2017-04-13 20:31:46 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QMap<QString, FileTransferJob *> Notification::s_downloadsInProgress;
|
2017-04-13 20:31:46 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
Notification::Notification(const NetworkPacket &np, const Device *device, QObject *parent)
|
2013-08-20 12:55:03 +01:00
|
|
|
: QObject(parent)
|
2019-07-19 22:52:24 +01:00
|
|
|
, m_imagesDir()
|
2019-06-02 15:03:11 +01:00
|
|
|
, m_device(device)
|
2013-08-20 12:55:03 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
// Make a own directory for each user so no one can see each others icons
|
2017-12-29 17:38:09 +00:00
|
|
|
QString username;
|
2022-09-10 22:23:52 +01:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
username = QString::fromLatin1(qgetenv("USERNAME"));
|
|
|
|
#else
|
|
|
|
username = QString::fromLatin1(qgetenv("USER"));
|
|
|
|
#endif
|
2017-12-29 17:38:09 +00:00
|
|
|
|
2019-07-19 22:52:24 +01:00
|
|
|
m_imagesDir.setPath(QDir::temp().absoluteFilePath(QStringLiteral("kdeconnect_") + username));
|
2017-09-03 20:39:44 +01:00
|
|
|
m_imagesDir.mkpath(m_imagesDir.absolutePath());
|
2017-12-29 17:38:09 +00:00
|
|
|
QFile(m_imagesDir.absolutePath()).setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner);
|
2017-11-06 03:12:16 +00:00
|
|
|
m_ready = false;
|
2017-04-13 20:31:46 +01:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
parseNetworkPacket(np);
|
2019-01-24 08:11:24 +00:00
|
|
|
createKNotification(np);
|
2013-08-20 12:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Notification::dismiss()
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
if (m_dismissable) {
|
|
|
|
Q_EMIT dismissRequested(m_internalId);
|
2013-08-20 12:55:03 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-21 06:22:14 +00:00
|
|
|
|
2017-04-13 20:31:46 +01:00
|
|
|
void Notification::show()
|
|
|
|
{
|
2017-11-06 03:12:16 +00:00
|
|
|
m_ready = true;
|
|
|
|
Q_EMIT ready();
|
2017-09-03 20:39:44 +01:00
|
|
|
if (!m_silent) {
|
|
|
|
m_notification->sendEvent();
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void Notification::update(const NetworkPacket &np)
|
2017-04-13 20:31:46 +01:00
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
parseNetworkPacket(np);
|
2019-01-24 08:11:24 +00:00
|
|
|
createKNotification(np);
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void Notification::createKNotification(const NetworkPacket &np)
|
2017-04-13 20:31:46 +01:00
|
|
|
{
|
2019-01-24 08:11:24 +00:00
|
|
|
if (!m_notification) {
|
2017-09-03 20:39:44 +01:00
|
|
|
m_notification = new KNotification(QStringLiteral("notification"), KNotification::CloseOnTimeout, this);
|
|
|
|
m_notification->setComponentName(QStringLiteral("kdeconnect"));
|
2022-09-10 22:23:52 +01:00
|
|
|
m_notification->setHint(QStringLiteral("resident"),
|
|
|
|
true); // This means the notification won't be deleted automatically, but only with KNotifications 5.81
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
QString escapedTitle = m_title.toHtmlEscaped();
|
2021-02-02 12:54:16 +00:00
|
|
|
// notification title text does not have markup, but in some cases below it is used in body text so we escape it
|
2017-09-03 20:39:44 +01:00
|
|
|
QString escapedText = m_text.toHtmlEscaped();
|
|
|
|
QString escapedTicker = m_ticker.toHtmlEscaped();
|
2017-05-07 19:22:57 +01:00
|
|
|
|
2019-06-02 15:03:11 +01:00
|
|
|
if (NotificationServerInfo::instance().supportedHints().testFlag(NotificationServerInfo::X_KDE_DISPLAY_APPNAME)) {
|
2021-02-02 12:54:16 +00:00
|
|
|
m_notification->setTitle(m_title);
|
2017-09-03 20:39:44 +01:00
|
|
|
m_notification->setText(escapedText);
|
2019-06-02 15:03:11 +01:00
|
|
|
m_notification->setHint(QStringLiteral("x-kde-display-appname"), m_appName.toHtmlEscaped());
|
2017-04-13 20:31:46 +01:00
|
|
|
} else {
|
2021-02-02 12:54:16 +00:00
|
|
|
m_notification->setTitle(m_appName);
|
2019-06-02 15:03:11 +01:00
|
|
|
|
|
|
|
if (m_title.isEmpty() && m_text.isEmpty()) {
|
|
|
|
m_notification->setText(escapedTicker);
|
|
|
|
} else if (m_appName == m_title) {
|
|
|
|
m_notification->setText(escapedText);
|
|
|
|
} else if (m_title.isEmpty()) {
|
|
|
|
m_notification->setText(escapedText);
|
|
|
|
} else if (m_text.isEmpty()) {
|
|
|
|
m_notification->setText(escapedTitle);
|
|
|
|
} else {
|
2019-06-10 15:40:28 +01:00
|
|
|
m_notification->setText(escapedTitle + QStringLiteral(": ") + escapedText);
|
2019-06-02 15:03:11 +01:00
|
|
|
}
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|
|
|
|
|
2019-06-02 15:03:11 +01:00
|
|
|
m_notification->setHint(QStringLiteral("x-kde-origin-name"), m_device->name());
|
|
|
|
|
2020-04-13 13:24:14 +01:00
|
|
|
if (!m_requestReplyId.isEmpty()) {
|
2021-03-08 20:25:47 +00:00
|
|
|
auto replyAction = std::make_unique<KNotificationReplyAction>(i18nc("@action:button", "Reply"));
|
|
|
|
replyAction->setPlaceholderText(i18nc("@info:placeholder", "Reply to %1...", m_appName));
|
|
|
|
replyAction->setFallbackBehavior(KNotificationReplyAction::FallbackBehavior::UseRegularAction);
|
|
|
|
QObject::connect(replyAction.get(), &KNotificationReplyAction::replied, this, &Notification::replied);
|
|
|
|
QObject::connect(replyAction.get(), &KNotificationReplyAction::activated, this, &Notification::reply);
|
|
|
|
m_notification->setReplyAction(std::move(replyAction));
|
2020-04-13 13:24:14 +01:00
|
|
|
}
|
|
|
|
|
2023-12-10 23:45:21 +00:00
|
|
|
m_notification->clearActions();
|
2023-07-23 21:52:52 +01:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2020-04-13 13:24:14 +01:00
|
|
|
|
2017-11-06 03:12:16 +00:00
|
|
|
m_hasIcon = m_hasIcon && !m_payloadHash.isEmpty();
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
if (!m_hasIcon) {
|
2017-04-13 20:31:46 +01:00
|
|
|
show();
|
|
|
|
} else {
|
2017-11-06 03:12:16 +00:00
|
|
|
m_iconPath = m_imagesDir.absoluteFilePath(m_payloadHash);
|
2018-03-24 19:39:39 +00:00
|
|
|
loadIcon(np);
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void Notification::loadIcon(const NetworkPacket &np)
|
2017-11-06 03:12:16 +00:00
|
|
|
{
|
|
|
|
m_ready = false;
|
|
|
|
|
2018-03-24 19:39:39 +00:00
|
|
|
if (QFileInfo::exists(m_iconPath)) {
|
|
|
|
applyIcon();
|
2017-11-06 03:12:16 +00:00
|
|
|
show();
|
2018-03-24 19:39:39 +00:00
|
|
|
} else {
|
2022-09-10 22:23:52 +01:00
|
|
|
FileTransferJob *fileTransferJob = s_downloadsInProgress.value(m_iconPath);
|
2018-03-24 19:39:39 +00:00
|
|
|
if (!fileTransferJob) {
|
|
|
|
fileTransferJob = np.createPayloadTransferJob(QUrl::fromLocalFile(m_iconPath));
|
|
|
|
fileTransferJob->start();
|
|
|
|
s_downloadsInProgress[m_iconPath] = fileTransferJob;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(fileTransferJob, &FileTransferJob::result, this, [this, fileTransferJob] {
|
2018-03-24 19:39:39 +00:00
|
|
|
s_downloadsInProgress.remove(m_iconPath);
|
|
|
|
if (fileTransferJob->error()) {
|
2023-08-06 11:00:02 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_NOTIFICATIONS) << "Error in FileTransferJob: " << fileTransferJob->errorString();
|
2018-03-24 19:39:39 +00:00
|
|
|
} else {
|
|
|
|
applyIcon();
|
|
|
|
}
|
|
|
|
show();
|
|
|
|
});
|
|
|
|
}
|
2017-11-06 03:12:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Notification::applyIcon()
|
|
|
|
{
|
|
|
|
QPixmap icon(m_iconPath, "PNG");
|
|
|
|
m_notification->setPixmap(icon);
|
|
|
|
}
|
|
|
|
|
2017-06-01 15:17:37 +01:00
|
|
|
void Notification::reply()
|
|
|
|
{
|
|
|
|
Q_EMIT replyRequested();
|
|
|
|
}
|
|
|
|
|
2023-07-16 15:20:34 +01:00
|
|
|
void Notification::sendReply(const QString &message)
|
2022-09-26 20:51:28 +01:00
|
|
|
{
|
|
|
|
Q_EMIT replied(message);
|
|
|
|
}
|
|
|
|
|
2023-07-16 15:20:34 +01:00
|
|
|
void Notification::parseNetworkPacket(const NetworkPacket &np)
|
2017-04-13 20:31:46 +01:00
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
m_internalId = np.get<QString>(QStringLiteral("id"));
|
|
|
|
m_appName = np.get<QString>(QStringLiteral("appName"));
|
|
|
|
m_ticker = np.get<QString>(QStringLiteral("ticker"));
|
|
|
|
m_title = np.get<QString>(QStringLiteral("title"));
|
|
|
|
m_text = np.get<QString>(QStringLiteral("text"));
|
|
|
|
m_dismissable = np.get<bool>(QStringLiteral("isClearable"));
|
|
|
|
m_hasIcon = np.hasPayload();
|
|
|
|
m_silent = np.get<bool>(QStringLiteral("silent"));
|
|
|
|
m_payloadHash = np.get<QString>(QStringLiteral("payloadHash"));
|
|
|
|
m_requestReplyId = np.get<QString>(QStringLiteral("requestReplyId"), QString());
|
2019-02-08 22:47:41 +00:00
|
|
|
|
|
|
|
m_actions.clear();
|
|
|
|
|
2019-02-28 13:04:15 +00:00
|
|
|
const auto actions = np.get<QJsonArray>(QStringLiteral("actions"));
|
2022-09-10 22:23:52 +01:00
|
|
|
for (const QJsonValue &value : actions) {
|
2019-02-08 22:47:41 +00:00
|
|
|
m_actions.append(value.toString());
|
|
|
|
}
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|
2023-07-26 09:15:11 +01:00
|
|
|
|
|
|
|
#include "moc_notification.cpp"
|