2015-09-10 15:36:03 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2015 Holger Kaelberer <holger.k@elberer.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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QtDBus/QDBusConnection>
|
|
|
|
#include <QtDBus/QDBusInterface>
|
2016-08-27 12:59:25 +01:00
|
|
|
#include <QtDBus/QDBusArgument>
|
2015-09-10 15:36:03 +01:00
|
|
|
#include <QtDebug>
|
|
|
|
#include <QLoggingCategory>
|
2016-04-27 18:45:58 +01:00
|
|
|
#include <QStandardPaths>
|
2016-08-27 12:59:25 +01:00
|
|
|
#include <QImage>
|
2016-04-27 18:45:58 +01:00
|
|
|
#include <KConfig>
|
|
|
|
#include <KConfigGroup>
|
2015-09-10 15:36:03 +01:00
|
|
|
|
2016-01-07 16:37:35 +00:00
|
|
|
#include <kiconloader.h>
|
|
|
|
#include <kicontheme.h>
|
|
|
|
|
2015-09-10 15:36:03 +01:00
|
|
|
#include <core/device.h>
|
|
|
|
#include <core/kdeconnectplugin.h>
|
|
|
|
|
|
|
|
#include "notificationslistener.h"
|
2016-05-25 19:49:13 +01:00
|
|
|
#include "sendnotificationsplugin.h"
|
|
|
|
#include "sendnotification_debug.h"
|
2015-12-05 22:11:57 +00:00
|
|
|
#include "notifyingapplication.h"
|
2015-09-10 15:36:03 +01:00
|
|
|
|
2016-05-25 19:49:13 +01:00
|
|
|
NotificationsListener::NotificationsListener(KdeConnectPlugin* aPlugin)
|
2015-09-10 15:36:03 +01:00
|
|
|
: QDBusAbstractAdaptor(aPlugin),
|
2017-09-03 20:39:44 +01:00
|
|
|
m_plugin(aPlugin)
|
2015-09-10 15:36:03 +01:00
|
|
|
{
|
2015-12-05 22:11:57 +00:00
|
|
|
qRegisterMetaTypeStreamOperators<NotifyingApplication>("NotifyingApplication");
|
|
|
|
|
2015-09-10 15:36:03 +01:00
|
|
|
bool ret = QDBusConnection::sessionBus()
|
2016-11-26 14:38:08 +00:00
|
|
|
.registerObject(QStringLiteral("/org/freedesktop/Notifications"),
|
2015-09-10 15:36:03 +01:00
|
|
|
this,
|
|
|
|
QDBusConnection::ExportScriptableContents);
|
|
|
|
if (!ret)
|
2016-05-25 19:49:13 +01:00
|
|
|
qCWarning(KDECONNECT_PLUGIN_SENDNOTIFICATION)
|
2015-09-10 15:36:03 +01:00
|
|
|
<< "Error registering notifications listener for device"
|
2017-09-03 20:39:44 +01:00
|
|
|
<< m_plugin->device()->name() << ":"
|
2015-09-10 15:36:03 +01:00
|
|
|
<< QDBusConnection::sessionBus().lastError();
|
|
|
|
else
|
2016-05-25 19:49:13 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SENDNOTIFICATION)
|
2015-09-10 15:36:03 +01:00
|
|
|
<< "Registered notifications listener for device"
|
2017-09-03 20:39:44 +01:00
|
|
|
<< m_plugin->device()->name();
|
2015-09-10 15:36:03 +01:00
|
|
|
|
2016-11-26 14:38:08 +00:00
|
|
|
QDBusInterface iface(QStringLiteral("org.freedesktop.DBus"), QStringLiteral("/org/freedesktop/DBus"),
|
|
|
|
QStringLiteral("org.freedesktop.DBus"));
|
|
|
|
iface.call(QStringLiteral("AddMatch"),
|
2015-09-10 15:36:03 +01:00
|
|
|
"interface='org.freedesktop.Notifications',member='Notify',type='method_call',eavesdrop='true'");
|
2015-12-05 22:11:57 +00:00
|
|
|
|
2016-04-27 18:45:58 +01:00
|
|
|
setTranslatedAppName();
|
2015-12-05 22:11:57 +00:00
|
|
|
loadApplications();
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
connect(m_plugin->config(), &KdeConnectPluginConfig::configChanged, this, &NotificationsListener::loadApplications);
|
2015-09-10 15:36:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NotificationsListener::~NotificationsListener()
|
|
|
|
{
|
2016-05-25 19:49:13 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SENDNOTIFICATION) << "Destroying NotificationsListener";
|
2016-11-26 14:38:08 +00:00
|
|
|
QDBusInterface iface(QStringLiteral("org.freedesktop.DBus"), QStringLiteral("/org/freedesktop/DBus"),
|
|
|
|
QStringLiteral("org.freedesktop.DBus"));
|
|
|
|
QDBusMessage res = iface.call(QStringLiteral("RemoveMatch"),
|
2015-09-10 15:36:03 +01:00
|
|
|
"interface='org.freedesktop.Notifications',member='Notify',type='method_call',eavesdrop='true'");
|
2016-11-26 14:38:08 +00:00
|
|
|
QDBusConnection::sessionBus().unregisterObject(QStringLiteral("/org/freedesktop/Notifications"));
|
2015-09-10 15:36:03 +01:00
|
|
|
}
|
|
|
|
|
2016-04-27 18:45:58 +01:00
|
|
|
void NotificationsListener::setTranslatedAppName()
|
|
|
|
{
|
|
|
|
QString filePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("knotifications5/kdeconnect.notifyrc"), QStandardPaths::LocateFile);
|
|
|
|
if (filePath.isEmpty()) {
|
2016-05-31 16:48:55 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SENDNOTIFICATION) << "Couldn't find kdeconnect.notifyrc to hide kdeconnect notifications on the devices. Using default name.";
|
2017-09-03 20:39:44 +01:00
|
|
|
m_translatedAppName = QStringLiteral("KDE Connect");
|
2016-04-27 18:45:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
KConfig config(filePath, KConfig::OpenFlag::SimpleConfig);
|
|
|
|
KConfigGroup globalgroup(&config, QStringLiteral("Global"));
|
2017-09-03 20:39:44 +01:00
|
|
|
m_translatedAppName = globalgroup.readEntry(QStringLiteral("Name"), QStringLiteral("KDE Connect"));
|
2016-04-27 18:45:58 +01:00
|
|
|
}
|
|
|
|
|
2015-12-05 22:11:57 +00:00
|
|
|
void NotificationsListener::loadApplications()
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
m_applications.clear();
|
|
|
|
const QVariantList list = m_plugin->config()->getList(QStringLiteral("applications"));
|
2017-07-20 15:14:07 +01:00
|
|
|
for (const auto& a : list) {
|
2015-12-05 22:11:57 +00:00
|
|
|
NotifyingApplication app = a.value<NotifyingApplication>();
|
2017-09-03 20:39:44 +01:00
|
|
|
if (!m_applications.contains(app.name))
|
|
|
|
m_applications.insert(app.name, app);
|
2015-12-05 22:11:57 +00:00
|
|
|
}
|
2016-05-25 19:49:13 +01:00
|
|
|
//qCDebug(KDECONNECT_PLUGIN_SENDNOTIFICATION) << "Loaded" << applications.size() << " applications";
|
2015-12-05 22:11:57 +00:00
|
|
|
}
|
|
|
|
|
2016-08-27 12:59:25 +01:00
|
|
|
bool NotificationsListener::parseImageDataArgument(const QVariant& argument,
|
|
|
|
int& width, int& height,
|
|
|
|
int& rowStride, int& bitsPerSample,
|
|
|
|
int& channels, bool& hasAlpha,
|
|
|
|
QByteArray& imageData) const
|
|
|
|
{
|
|
|
|
if (!argument.canConvert<QDBusArgument>())
|
|
|
|
return false;
|
|
|
|
const QDBusArgument dbusArg = argument.value<QDBusArgument>();
|
|
|
|
dbusArg.beginStructure();
|
|
|
|
dbusArg >> width >> height >> rowStride >> hasAlpha >> bitsPerSample
|
|
|
|
>> channels >> imageData;
|
|
|
|
dbusArg.endStructure();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSharedPointer<QIODevice> NotificationsListener::iconForImageData(const QVariant& argument) const
|
|
|
|
{
|
|
|
|
int width, height, rowStride, bitsPerSample, channels;
|
|
|
|
bool hasAlpha;
|
|
|
|
QByteArray imageData;
|
|
|
|
|
|
|
|
if (!parseImageDataArgument(argument, width, height, rowStride, bitsPerSample,
|
|
|
|
channels, hasAlpha, imageData))
|
|
|
|
return QSharedPointer<QIODevice>();
|
|
|
|
|
|
|
|
if (bitsPerSample != 8) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_SENDNOTIFICATION) << "Unsupported image format:"
|
|
|
|
<< "width=" << width
|
|
|
|
<< "height=" << height
|
|
|
|
<< "rowStride=" << rowStride
|
|
|
|
<< "bitsPerSample=" << bitsPerSample
|
|
|
|
<< "channels=" << channels
|
|
|
|
<< "hasAlpha=" << hasAlpha;
|
|
|
|
return QSharedPointer<QIODevice>();
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage image(reinterpret_cast<uchar*>(imageData.data()), width, height, rowStride,
|
|
|
|
hasAlpha ? QImage::Format_ARGB32 : QImage::Format_RGB32);
|
|
|
|
if (hasAlpha)
|
|
|
|
image = image.rgbSwapped(); // RGBA --> ARGB
|
|
|
|
|
|
|
|
QSharedPointer<QBuffer> buffer = QSharedPointer<QBuffer>(new QBuffer);
|
|
|
|
if (!buffer || !buffer->open(QIODevice::WriteOnly) ||
|
|
|
|
!image.save(buffer.data(), "PNG")) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_SENDNOTIFICATION) << "Could not initialize image buffer";
|
|
|
|
return QSharedPointer<QIODevice>();
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
QSharedPointer<QIODevice> NotificationsListener::iconForIconName(const QString& iconName) const
|
2016-08-27 12:59:25 +01:00
|
|
|
{
|
|
|
|
int size = KIconLoader::SizeEnormous; // use big size to allow for good
|
|
|
|
// quality on high-DPI mobile devices
|
|
|
|
QString iconPath = KIconLoader::global()->iconPath(iconName, -size, true);
|
|
|
|
if (!iconPath.isEmpty()) {
|
|
|
|
if (!iconPath.endsWith(QLatin1String(".png")) &&
|
|
|
|
KIconLoader::global()->theme()->name() != QLatin1String("hicolor")) {
|
|
|
|
// try falling back to hicolor theme:
|
|
|
|
KIconTheme hicolor(QStringLiteral("hicolor"));
|
|
|
|
if (hicolor.isValid()) {
|
|
|
|
iconPath = hicolor.iconPath(iconName + ".png", size, KIconLoader::MatchBest);
|
|
|
|
//qCDebug(KDECONNECT_PLUGIN_SENDNOTIFICATION) << "Found non-png icon in default theme trying fallback to hicolor:" << iconPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iconPath.endsWith(QLatin1String(".png")))
|
|
|
|
return QSharedPointer<QIODevice>(new QFile(iconPath));
|
|
|
|
return QSharedPointer<QIODevice>();
|
|
|
|
}
|
2017-09-03 20:39:44 +01:00
|
|
|
uint NotificationsListener::Notify(const QString& appName, uint replacesId,
|
|
|
|
const QString& appIcon,
|
|
|
|
const QString& summary, const QString& body,
|
|
|
|
const QStringList& actions,
|
|
|
|
const QVariantMap& hints, int timeout)
|
2015-09-10 15:36:03 +01:00
|
|
|
{
|
|
|
|
static int id = 0;
|
|
|
|
Q_UNUSED(actions);
|
2015-12-05 22:11:57 +00:00
|
|
|
|
2016-05-25 19:49:13 +01:00
|
|
|
//qCDebug(KDECONNECT_PLUGIN_SENDNOTIFICATION) << "Got notification appName=" << appName << "replacesId=" << replacesId << "appIcon=" << appIcon << "summary=" << summary << "body=" << body << "actions=" << actions << "hints=" << hints << "timeout=" << timeout;
|
2015-09-10 15:36:03 +01:00
|
|
|
|
|
|
|
// skip our own notifications
|
2017-09-03 20:39:44 +01:00
|
|
|
if (appName == m_translatedAppName)
|
2015-09-10 15:36:03 +01:00
|
|
|
return 0;
|
|
|
|
|
2015-12-05 22:11:57 +00:00
|
|
|
NotifyingApplication app;
|
2017-09-03 20:39:44 +01:00
|
|
|
if (!m_applications.contains(appName)) {
|
2015-12-05 22:11:57 +00:00
|
|
|
// new application -> add to config
|
|
|
|
app.name = appName;
|
|
|
|
app.icon = appIcon;
|
|
|
|
app.active = true;
|
|
|
|
app.blacklistExpression = QRegularExpression();
|
2017-09-03 20:39:44 +01:00
|
|
|
m_applications.insert(app.name, app);
|
2015-12-05 22:11:57 +00:00
|
|
|
// update config:
|
|
|
|
QVariantList list;
|
2017-09-03 20:39:44 +01:00
|
|
|
for (const auto& a : qAsConst(m_applications))
|
2015-12-05 22:11:57 +00:00
|
|
|
list << QVariant::fromValue<NotifyingApplication>(a);
|
2017-09-03 20:39:44 +01:00
|
|
|
m_plugin->config()->setList(QStringLiteral("applications"), list);
|
2016-05-25 19:49:13 +01:00
|
|
|
//qCDebug(KDECONNECT_PLUGIN_SENDNOTIFICATION) << "Added new application to config:" << app;
|
2015-12-05 22:11:57 +00:00
|
|
|
} else
|
2017-09-03 20:39:44 +01:00
|
|
|
app = m_applications.value(appName);
|
2015-12-05 22:11:57 +00:00
|
|
|
|
|
|
|
if (!app.active)
|
|
|
|
return 0;
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
if (timeout > 0 && m_plugin->config()->get(QStringLiteral("generalPersistent"), false))
|
2015-12-05 22:11:57 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
int urgency = -1;
|
2016-11-26 14:38:08 +00:00
|
|
|
if (hints.contains(QStringLiteral("urgency"))) {
|
2015-12-07 02:21:35 +00:00
|
|
|
bool ok;
|
2016-11-26 14:38:08 +00:00
|
|
|
urgency = hints[QStringLiteral("urgency")].toInt(&ok);
|
2015-12-07 02:21:35 +00:00
|
|
|
if (!ok)
|
|
|
|
urgency = -1;
|
|
|
|
}
|
2017-09-03 20:39:44 +01:00
|
|
|
if (urgency > -1 && urgency < m_plugin->config()->get<int>(QStringLiteral("generalUrgency"), 0))
|
2015-12-05 22:11:57 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
QString ticker = summary;
|
2017-09-03 20:39:44 +01:00
|
|
|
if (!body.isEmpty() && m_plugin->config()->get(QStringLiteral("generalIncludeBody"), true))
|
2016-11-26 14:38:08 +00:00
|
|
|
ticker += QStringLiteral(": ") + body;
|
2015-12-05 22:11:57 +00:00
|
|
|
|
|
|
|
if (app.blacklistExpression.isValid() &&
|
|
|
|
!app.blacklistExpression.pattern().isEmpty() &&
|
|
|
|
app.blacklistExpression.match(ticker).hasMatch())
|
|
|
|
return 0;
|
|
|
|
|
2016-05-25 19:49:13 +01:00
|
|
|
//qCDebug(KDECONNECT_PLUGIN_SENDNOTIFICATION) << "Sending notification from" << appName << ":" <<ticker << "; appIcon=" << appIcon;
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_NOTIFICATION, {
|
2016-06-21 19:07:12 +01:00
|
|
|
{"id", QString::number(replacesId > 0 ? replacesId : ++id)},
|
|
|
|
{"appName", appName},
|
|
|
|
{"ticker", ticker},
|
|
|
|
{"isClearable", timeout == 0}
|
|
|
|
}); // KNotifications are persistent if
|
2015-09-10 15:36:03 +01:00
|
|
|
// timeout == 0, for other notifications
|
|
|
|
// clearability is pointless
|
|
|
|
|
2016-08-27 12:59:25 +01:00
|
|
|
// sync any icon data?
|
2017-09-03 20:39:44 +01:00
|
|
|
if (m_plugin->config()->get(QStringLiteral("generalSynchronizeIcons"), true)) {
|
2016-08-27 12:59:25 +01:00
|
|
|
QSharedPointer<QIODevice> iconSource;
|
|
|
|
// try different image sources according to priorities in notifications-
|
|
|
|
// spec version 1.2:
|
2016-11-26 14:38:08 +00:00
|
|
|
if (hints.contains(QStringLiteral("image-data")))
|
|
|
|
iconSource = iconForImageData(hints[QStringLiteral("image-data")]);
|
|
|
|
else if (hints.contains(QStringLiteral("image_data"))) // 1.1 backward compatibility
|
|
|
|
iconSource = iconForImageData(hints[QStringLiteral("image_data")]);
|
|
|
|
else if (hints.contains(QStringLiteral("image-path")))
|
|
|
|
iconSource = iconForIconName(hints[QStringLiteral("image-path")].toString());
|
|
|
|
else if (hints.contains(QStringLiteral("image_path"))) // 1.1 backward compatibility
|
|
|
|
iconSource = iconForIconName(hints[QStringLiteral("image_path")].toString());
|
2016-08-27 12:59:25 +01:00
|
|
|
else if (!appIcon.isEmpty())
|
|
|
|
iconSource = iconForIconName(appIcon);
|
2016-11-26 14:38:08 +00:00
|
|
|
else if (hints.contains(QStringLiteral("icon_data"))) // < 1.1 backward compatibility
|
|
|
|
iconSource = iconForImageData(hints[QStringLiteral("icon_data")]);
|
2016-08-27 12:59:25 +01:00
|
|
|
|
|
|
|
if (iconSource)
|
|
|
|
np.setPayload(iconSource, iconSource->size());
|
2016-01-07 16:37:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
m_plugin->sendPacket(np);
|
2015-09-10 15:36:03 +01:00
|
|
|
|
|
|
|
return (replacesId > 0 ? replacesId : id);
|
|
|
|
}
|