Use notification hints from new Plasma notification system
This commit is contained in:
parent
283d014059
commit
96cd55920e
7 changed files with 148 additions and 14 deletions
|
@ -37,6 +37,7 @@ set(kdeconnectcore_SRCS
|
|||
daemon.cpp
|
||||
device.cpp
|
||||
core_debug.cpp
|
||||
notificationserverinfo.cpp
|
||||
)
|
||||
|
||||
add_library(kdeconnectcore ${kdeconnectcore_SRCS})
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
#include "core_debug.h"
|
||||
#include "kdeconnectconfig.h"
|
||||
#include "networkpacket.h"
|
||||
#include "notificationserverinfo.h"
|
||||
|
||||
#ifdef KDECONNECT_BLUETOOTH
|
||||
#include "backends/bluetooth/bluetoothlinkprovider.h"
|
||||
|
@ -110,6 +111,8 @@ void Daemon::init()
|
|||
QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.kdeconnect"));
|
||||
QDBusConnection::sessionBus().registerObject(QStringLiteral("/modules/kdeconnect"), this, QDBusConnection::ExportScriptableContents);
|
||||
|
||||
NotificationServerInfo::instance().init();
|
||||
|
||||
qCDebug(KDECONNECT_CORE) << "Daemon started";
|
||||
}
|
||||
|
||||
|
|
64
core/notificationserverinfo.cpp
Normal file
64
core/notificationserverinfo.cpp
Normal file
|
@ -0,0 +1,64 @@
|
|||
/**
|
||||
* Copyright 2019 Nicolas Fella <nicolas.fella@gmx.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of
|
||||
* the License or (at your option) version 3 or any later version
|
||||
* accepted by the membership of KDE e.V. (or its successor approved
|
||||
* by the membership of KDE e.V.), which shall act as a proxy
|
||||
* defined in Section 14 of version 3 of the license.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "notificationserverinfo.h"
|
||||
|
||||
#include <QDBusMessage>
|
||||
#include <QDBusPendingReply>
|
||||
#include <QDBusPendingCallWatcher>
|
||||
#include <QDBusConnection>
|
||||
|
||||
#include "core_debug.h"
|
||||
|
||||
NotificationServerInfo& NotificationServerInfo::instance()
|
||||
{
|
||||
static NotificationServerInfo instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void NotificationServerInfo::init()
|
||||
{
|
||||
QDBusMessage query = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.Notifications"), QStringLiteral("/org/freedesktop/Notifications"), QStringLiteral("org.freedesktop.Notifications"), QStringLiteral("GetCapabilities"));
|
||||
|
||||
QDBusPendingReply<QStringList> reply = QDBusConnection::sessionBus().asyncCall(query);
|
||||
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
|
||||
connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, reply, watcher] {
|
||||
watcher->deleteLater();
|
||||
|
||||
if (reply.isError()) {
|
||||
qCWarning(KDECONNECT_CORE) << "Could not query capabilities from notifications server";
|
||||
return;
|
||||
}
|
||||
|
||||
if (reply.value().contains(QLatin1String("x-kde-display-appname"))) {
|
||||
m_supportedHints |= X_KDE_DISPLAY_APPNAME;
|
||||
}
|
||||
|
||||
if (reply.value().contains(QLatin1String("x-kde-origin-name"))) {
|
||||
m_supportedHints |= X_KDE_ORIGIN_NAME;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
NotificationServerInfo::Hints NotificationServerInfo::supportedHints()
|
||||
{
|
||||
return m_supportedHints;
|
||||
}
|
||||
|
49
core/notificationserverinfo.h
Normal file
49
core/notificationserverinfo.h
Normal file
|
@ -0,0 +1,49 @@
|
|||
/**
|
||||
* Copyright 2019 Nicolas Fella <nicolas.fella@gmx.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of
|
||||
* the License or (at your option) version 3 or any later version
|
||||
* accepted by the membership of KDE e.V. (or its successor approved
|
||||
* by the membership of KDE e.V.), which shall act as a proxy
|
||||
* defined in Section 14 of version 3 of the license.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "kdeconnectcore_export.h"
|
||||
#include <QObject>
|
||||
|
||||
class KDECONNECTCORE_EXPORT NotificationServerInfo
|
||||
: public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum Hint {
|
||||
X_KDE_DISPLAY_APPNAME = 1,
|
||||
X_KDE_ORIGIN_NAME = 2
|
||||
};
|
||||
|
||||
Q_DECLARE_FLAGS(Hints, Hint)
|
||||
|
||||
static NotificationServerInfo& instance();
|
||||
|
||||
void init();
|
||||
|
||||
Hints supportedHints();
|
||||
|
||||
private:
|
||||
Hints m_supportedHints;
|
||||
};
|
||||
|
||||
Q_DECLARE_OPERATORS_FOR_FLAGS(NotificationServerInfo::Hints)
|
|
@ -29,15 +29,17 @@
|
|||
#include <QPixmap>
|
||||
#include <KLocalizedString>
|
||||
#include <QFile>
|
||||
|
||||
#include <knotifications_version.h>
|
||||
#include <QJsonArray>
|
||||
|
||||
#include <core/filetransferjob.h>
|
||||
#include <core/notificationserverinfo.h>
|
||||
|
||||
QMap<QString, FileTransferJob*> Notification::s_downloadsInProgress;
|
||||
|
||||
Notification::Notification(const NetworkPacket& np, QObject* parent)
|
||||
Notification::Notification(const NetworkPacket& np, const Device* device, QObject* parent)
|
||||
: QObject(parent)
|
||||
, m_device(device)
|
||||
{
|
||||
//Make a own directory for each user so noone can see each others icons
|
||||
QString username;
|
||||
|
@ -102,20 +104,33 @@ void Notification::createKNotification(const NetworkPacket& np)
|
|||
QString escapedText = m_text.toHtmlEscaped();
|
||||
QString escapedTicker = m_ticker.toHtmlEscaped();
|
||||
|
||||
m_notification->setTitle(m_appName.toHtmlEscaped());
|
||||
|
||||
if (m_title.isEmpty() && m_text.isEmpty()) {
|
||||
m_notification->setText(escapedTicker);
|
||||
} else if (m_appName == m_title) {
|
||||
#if KNOTIFICATIONS_VERSION >= QT_VERSION_CHECK(5, 57, 0)
|
||||
if (NotificationServerInfo::instance().supportedHints().testFlag(NotificationServerInfo::X_KDE_DISPLAY_APPNAME)) {
|
||||
m_notification->setTitle(escapedTitle);
|
||||
m_notification->setText(escapedText);
|
||||
} else if (m_title.isEmpty()) {
|
||||
m_notification->setText(escapedText);
|
||||
} else if (m_text.isEmpty()) {
|
||||
m_notification->setText(escapedTitle);
|
||||
m_notification->setHint(QStringLiteral("x-kde-display-appname"), m_appName.toHtmlEscaped());
|
||||
} else {
|
||||
m_notification->setText(escapedTitle + ": " + escapedText);
|
||||
#endif
|
||||
m_notification->setTitle(m_appName.toHtmlEscaped());
|
||||
|
||||
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 {
|
||||
m_notification->setText(escapedTitle + ": " + escapedText);
|
||||
}
|
||||
|
||||
#if KNOTIFICATIONS_VERSION >= QT_VERSION_CHECK(5, 57, 0)
|
||||
}
|
||||
|
||||
m_notification->setHint(QStringLiteral("x-kde-origin-name"), m_device->name());
|
||||
#endif
|
||||
|
||||
m_hasIcon = m_hasIcon && !m_payloadHash.isEmpty();
|
||||
|
||||
if (!m_hasIcon) {
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include <QPointer>
|
||||
|
||||
#include <core/networkpacket.h>
|
||||
#include <core/device.h>
|
||||
|
||||
class Notification
|
||||
: public QObject
|
||||
|
@ -46,7 +47,7 @@ class Notification
|
|||
Q_PROPERTY(QString replyId READ replyId NOTIFY ready)
|
||||
|
||||
public:
|
||||
Notification(const NetworkPacket& np, QObject* parent);
|
||||
Notification(const NetworkPacket& np, const Device* device, QObject* parent);
|
||||
~Notification() override;
|
||||
|
||||
QString internalId() const { return m_internalId; }
|
||||
|
@ -90,6 +91,7 @@ private:
|
|||
QString m_payloadHash;
|
||||
bool m_ready;
|
||||
QStringList m_actions;
|
||||
const Device* m_device;
|
||||
|
||||
void parseNetworkPacket(const NetworkPacket& np);
|
||||
void loadIcon(const NetworkPacket& np);
|
||||
|
|
|
@ -82,7 +82,7 @@ void NotificationsDbusInterface::processPacket(const NetworkPacket& np)
|
|||
Notification* noti = nullptr;
|
||||
|
||||
if (!m_internalIdToPublicId.contains(id)) {
|
||||
noti = new Notification(np, this);
|
||||
noti = new Notification(np, m_plugin->device(), this);
|
||||
|
||||
if (noti->isReady()) {
|
||||
addNotification(noti);
|
||||
|
|
Loading…
Reference in a new issue