2013-08-13 23:09:46 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
|
2013-08-13 23:09:46 +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-13 23:09:46 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notificationsplugin.h"
|
|
|
|
|
2020-05-26 17:55:47 +01:00
|
|
|
#include "plugin_notification_debug.h"
|
2020-10-15 03:03:05 +01:00
|
|
|
#include "sendreplydialog.h"
|
|
|
|
#include <dbushelper.h>
|
2013-08-13 23:09:46 +01:00
|
|
|
|
2014-09-22 01:37:10 +01:00
|
|
|
#include <KPluginFactory>
|
2021-02-01 13:10:47 +00:00
|
|
|
#include <KStartupInfo>
|
|
|
|
|
|
|
|
#if !defined(Q_OS_WIN) && !defined(Q_OS_MAC)
|
|
|
|
#include <QX11Info>
|
|
|
|
#endif
|
2014-09-22 01:37:10 +01:00
|
|
|
|
2019-06-12 21:16:54 +01:00
|
|
|
K_PLUGIN_CLASS_WITH_JSON(NotificationsPlugin, "kdeconnect_notifications.json")
|
2013-08-13 23:09:46 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
NotificationsPlugin::NotificationsPlugin(QObject *parent, const QVariantList &args)
|
2013-08-13 23:09:46 +01:00
|
|
|
: KdeConnectPlugin(parent, args)
|
|
|
|
{
|
2013-08-22 02:21:08 +01:00
|
|
|
}
|
2013-08-20 12:53:36 +01:00
|
|
|
|
2013-08-22 02:21:08 +01:00
|
|
|
void NotificationsPlugin::connected()
|
|
|
|
{
|
2019-06-10 15:40:28 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_NOTIFICATION_REQUEST, {{QStringLiteral("request"), true}});
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2013-08-16 07:12:29 +01:00
|
|
|
}
|
2013-08-13 23:09:46 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool NotificationsPlugin::receivePacket(const NetworkPacket &np)
|
2013-08-13 23:09:46 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (np.get<bool>(QStringLiteral("request")))
|
|
|
|
return false;
|
2013-08-13 23:09:46 +01:00
|
|
|
|
2020-10-15 03:03:05 +01:00
|
|
|
if (np.get<bool>(QStringLiteral("isCancel"))) {
|
|
|
|
QString id = np.get<QString>(QStringLiteral("id"));
|
|
|
|
// cut off kdeconnect-android's prefix if there:
|
|
|
|
if (id.startsWith(QLatin1String("org.kde.kdeconnect_tp::")))
|
|
|
|
id = id.mid(id.indexOf(QLatin1String("::")) + 2);
|
|
|
|
removeNotification(id);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString id = np.get<QString>(QStringLiteral("id"));
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
Notification *noti = nullptr;
|
2020-10-15 03:03:05 +01:00
|
|
|
|
|
|
|
if (!m_internalIdToPublicId.contains(id)) {
|
|
|
|
noti = new Notification(np, device(), this);
|
|
|
|
|
|
|
|
if (noti->isReady()) {
|
|
|
|
addNotification(noti);
|
|
|
|
} else {
|
|
|
|
connect(noti, &Notification::ready, this, &NotificationsPlugin::notificationReady);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QString pubId = m_internalIdToPublicId.value(id);
|
|
|
|
noti = m_notifications.value(pubId);
|
|
|
|
noti->update(np);
|
|
|
|
}
|
2013-08-20 12:53:36 +01:00
|
|
|
|
2013-08-13 23:09:46 +01:00
|
|
|
return true;
|
|
|
|
}
|
2013-08-13 23:18:32 +01:00
|
|
|
|
2020-10-15 03:03:05 +01:00
|
|
|
void NotificationsPlugin::clearNotifications()
|
|
|
|
{
|
|
|
|
qDeleteAll(m_notifications);
|
|
|
|
m_notifications.clear();
|
|
|
|
Q_EMIT allNotificationsRemoved();
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList NotificationsPlugin::activeNotifications()
|
|
|
|
{
|
|
|
|
return m_notifications.keys();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationsPlugin::notificationReady()
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
Notification *noti = static_cast<Notification *>(sender());
|
2020-10-15 03:03:05 +01:00
|
|
|
disconnect(noti, &Notification::ready, this, &NotificationsPlugin::notificationReady);
|
|
|
|
addNotification(noti);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void NotificationsPlugin::addNotification(Notification *noti)
|
2020-10-15 03:03:05 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
const QString &internalId = noti->internalId();
|
2020-10-15 03:03:05 +01:00
|
|
|
|
|
|
|
if (m_internalIdToPublicId.contains(internalId)) {
|
|
|
|
removeNotification(internalId);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "addNotification" << internalId;
|
2020-10-15 03:03:05 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(noti, &Notification::dismissRequested, this, &NotificationsPlugin::dismissRequested);
|
2020-10-15 03:03:05 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(noti, &Notification::replyRequested, this, [this, noti] {
|
2020-10-15 03:03:05 +01:00
|
|
|
replyRequested(noti);
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(noti, &Notification::actionTriggered, this, &NotificationsPlugin::sendAction);
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(noti, &Notification::replied, this, [this, noti](const QString &message) {
|
2021-03-08 20:25:47 +00:00
|
|
|
sendReply(noti->replyId(), message);
|
|
|
|
});
|
2020-10-15 03:03:05 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
const QString &publicId = newId();
|
2020-10-15 03:03:05 +01:00
|
|
|
m_notifications[publicId] = noti;
|
|
|
|
m_internalIdToPublicId[internalId] = publicId;
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QDBusConnection::sessionBus().registerObject(device()->dbusPath() + QStringLiteral("/notifications/") + publicId,
|
|
|
|
noti,
|
|
|
|
QDBusConnection::ExportScriptableContents);
|
2020-10-15 03:03:05 +01:00
|
|
|
Q_EMIT notificationPosted(publicId);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void NotificationsPlugin::removeNotification(const QString &internalId)
|
2020-10-15 03:03:05 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
// qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "removeNotification" << internalId;
|
2020-10-15 03:03:05 +01:00
|
|
|
|
|
|
|
if (!m_internalIdToPublicId.contains(internalId)) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "Not found noti by internal Id: " << internalId;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString publicId = m_internalIdToPublicId.take(internalId);
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
Notification *noti = m_notifications.take(publicId);
|
2020-10-15 03:03:05 +01:00
|
|
|
if (!noti) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "Not found noti by public Id: " << publicId;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Deleting the notification will unregister it automatically
|
2020-10-15 03:03:05 +01:00
|
|
|
noti->deleteLater();
|
|
|
|
|
|
|
|
Q_EMIT notificationRemoved(publicId);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void NotificationsPlugin::dismissRequested(const QString &internalId)
|
2020-10-15 03:03:05 +01:00
|
|
|
{
|
|
|
|
NetworkPacket np(PACKET_TYPE_NOTIFICATION_REQUEST);
|
|
|
|
np.set<QString>(QStringLiteral("cancel"), internalId);
|
|
|
|
sendPacket(np);
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Workaround: we erase notifications without waiting a response from the
|
|
|
|
// phone because we won't receive a response if we are out of sync and this
|
|
|
|
// notification no longer exists. Ideally, each time we reach the phone
|
|
|
|
// after some time disconnected we should re-sync all the notifications.
|
2020-10-15 03:03:05 +01:00
|
|
|
removeNotification(internalId);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void NotificationsPlugin::replyRequested(Notification *noti)
|
2020-10-15 03:03:05 +01:00
|
|
|
{
|
|
|
|
QString replyId = noti->replyId();
|
|
|
|
QString appName = noti->appName();
|
|
|
|
QString originalMessage = noti->ticker();
|
2022-09-10 22:23:52 +01:00
|
|
|
SendReplyDialog *dialog = new SendReplyDialog(originalMessage, replyId, appName);
|
2020-10-15 03:03:05 +01:00
|
|
|
connect(dialog, &SendReplyDialog::sendReply, this, &NotificationsPlugin::sendReply);
|
|
|
|
dialog->show();
|
2021-02-01 13:10:47 +00:00
|
|
|
#if !defined(Q_OS_WIN) && !defined(Q_OS_MAC)
|
2022-09-10 22:23:52 +01:00
|
|
|
auto window = qobject_cast<QWindow *>(dialog->windowHandle());
|
2021-02-01 13:10:47 +00:00
|
|
|
if (window && QX11Info::isPlatformX11()) {
|
|
|
|
KStartupInfo::setNewStartupId(window, QX11Info::nextStartupId());
|
|
|
|
}
|
|
|
|
#endif
|
2020-10-15 03:03:05 +01:00
|
|
|
dialog->raise();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void NotificationsPlugin::sendReply(const QString &replyId, const QString &message)
|
2020-10-15 03:03:05 +01:00
|
|
|
{
|
|
|
|
NetworkPacket np(PACKET_TYPE_NOTIFICATION_REPLY);
|
|
|
|
np.set<QString>(QStringLiteral("requestReplyId"), replyId);
|
|
|
|
np.set<QString>(QStringLiteral("message"), message);
|
|
|
|
sendPacket(np);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void NotificationsPlugin::sendAction(const QString &key, const QString &action)
|
2020-10-15 03:03:05 +01:00
|
|
|
{
|
|
|
|
NetworkPacket np(PACKET_TYPE_NOTIFICATION_ACTION);
|
|
|
|
np.set<QString>(QStringLiteral("key"), key);
|
|
|
|
np.set<QString>(QStringLiteral("action"), action);
|
|
|
|
sendPacket(np);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString NotificationsPlugin::newId()
|
|
|
|
{
|
|
|
|
return QString::number(++m_lastId);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString NotificationsPlugin::dbusPath() const
|
|
|
|
{
|
|
|
|
return QStringLiteral("/modules/kdeconnect/devices/") + device()->id() + QStringLiteral("/notifications");
|
|
|
|
}
|
|
|
|
|
2014-06-16 19:02:07 +01:00
|
|
|
#include "notificationsplugin.moc"
|