2015-09-10 15:36:03 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2015 Holger Kaelberer <holger.k@elberer.de>
|
2015-09-10 15:36: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
|
2015-09-10 15:36:03 +01:00
|
|
|
*/
|
|
|
|
|
2023-08-13 20:43:59 +01:00
|
|
|
#include "notificationslistener.h"
|
2023-03-13 02:20:12 +00:00
|
|
|
|
2023-08-13 20:43:59 +01:00
|
|
|
#include <QBuffer>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QImage>
|
2023-07-30 18:05:15 +01:00
|
|
|
#include <QStandardPaths>
|
2016-01-07 16:37:35 +00:00
|
|
|
|
2023-07-30 18:05:15 +01:00
|
|
|
#include <KConfig>
|
|
|
|
#include <KConfigGroup>
|
2019-06-09 16:28:49 +01:00
|
|
|
|
2015-12-05 22:11:57 +00:00
|
|
|
#include "notifyingapplication.h"
|
2023-08-06 11:00:02 +01:00
|
|
|
#include "plugin_sendnotifications_debug.h"
|
2023-07-30 18:05:15 +01:00
|
|
|
#include <core/kdeconnectplugin.h>
|
|
|
|
#include <core/kdeconnectpluginconfig.h>
|
2023-08-13 20:43:59 +01:00
|
|
|
|
|
|
|
NotificationsListener::NotificationsListener(KdeConnectPlugin *aPlugin)
|
|
|
|
: QObject(aPlugin)
|
|
|
|
, m_plugin(aPlugin)
|
|
|
|
{
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
qRegisterMetaTypeStreamOperators<NotifyingApplication>("NotifyingApplication");
|
|
|
|
#endif
|
|
|
|
|
2023-07-30 18:05:15 +01:00
|
|
|
setTranslatedAppName();
|
2023-08-13 20:43:59 +01:00
|
|
|
loadApplications();
|
|
|
|
|
|
|
|
connect(m_plugin->config(), &KdeConnectPluginConfig::configChanged, this, &NotificationsListener::loadApplications);
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationsListener::~NotificationsListener()
|
|
|
|
{
|
2023-07-30 18:05:15 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SENDNOTIFICATIONS) << "Destroying NotificationsListener";
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationsListener::setTranslatedAppName()
|
|
|
|
{
|
|
|
|
QString filePath =
|
|
|
|
QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("knotifications5/kdeconnect.notifyrc"), QStandardPaths::LocateFile);
|
|
|
|
if (filePath.isEmpty()) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_SENDNOTIFICATIONS)
|
|
|
|
<< "Couldn't find kdeconnect.notifyrc to hide kdeconnect notifications on the devices. Using default name.";
|
|
|
|
m_translatedAppName = QStringLiteral("KDE Connect");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
KConfig config(filePath, KConfig::OpenFlag::SimpleConfig);
|
|
|
|
KConfigGroup globalgroup(&config, QStringLiteral("Global"));
|
|
|
|
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"));
|
2022-09-10 22:23:52 +01:00
|
|
|
for (const auto &a : list) {
|
2015-12-05 22:11:57 +00:00
|
|
|
NotifyingApplication app = a.value<NotifyingApplication>();
|
2023-08-13 20:43:59 +01:00
|
|
|
if (!m_applications.contains(app.name))
|
2017-09-03 20:39:44 +01:00
|
|
|
m_applications.insert(app.name, app);
|
2015-12-05 22:11:57 +00:00
|
|
|
}
|
2023-08-13 20:43:59 +01:00
|
|
|
// qCDebug(KDECONNECT_PLUGIN_SENDNOTIFICATIONS) << "Loaded" << m_applications.size() << " applications";
|
2015-12-05 22:11:57 +00:00
|
|
|
}
|
|
|
|
|
2023-07-30 18:05:15 +01:00
|
|
|
bool NotificationsListener::checkApplicationName(const QString &appName, std::optional<std::reference_wrapper<const QString>> iconName)
|
2016-08-27 12:59:25 +01:00
|
|
|
{
|
2023-07-30 18:05:15 +01:00
|
|
|
if (m_translatedAppName == appName) {
|
2016-08-27 12:59:25 +01:00
|
|
|
return false;
|
2023-08-13 20:43:59 +01:00
|
|
|
}
|
2016-08-27 12:59:25 +01:00
|
|
|
|
2023-07-30 18:05:15 +01:00
|
|
|
auto it = m_applications.constFind(appName);
|
|
|
|
if (it == m_applications.cend()) {
|
2015-12-05 22:11:57 +00:00
|
|
|
// new application -> add to config
|
2023-07-30 18:05:15 +01:00
|
|
|
NotifyingApplication app;
|
2015-12-05 22:11:57 +00:00
|
|
|
app.name = appName;
|
2023-07-30 18:05:15 +01:00
|
|
|
if (iconName.has_value()) {
|
|
|
|
app.icon = iconName.value();
|
|
|
|
}
|
2015-12-05 22:11:57 +00:00
|
|
|
app.active = true;
|
2023-08-13 20:43:59 +01:00
|
|
|
m_applications.insert(app.name, app);
|
2023-07-30 18:05:15 +01:00
|
|
|
// update config:
|
2015-12-05 22:11:57 +00:00
|
|
|
QVariantList list;
|
2023-07-30 18:05:15 +01:00
|
|
|
for (const auto &a : std::as_const(m_applications)) {
|
2015-12-05 22:11:57 +00:00
|
|
|
list << QVariant::fromValue<NotifyingApplication>(a);
|
2023-07-30 18:05:15 +01:00
|
|
|
}
|
|
|
|
m_plugin->config()->setList(QStringLiteral("applications"), list);
|
2023-03-13 02:20:12 +00:00
|
|
|
|
2023-07-30 18:05:15 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return it->active;
|
2023-03-13 02:20:12 +00:00
|
|
|
}
|
2023-07-30 18:05:15 +01:00
|
|
|
}
|
2015-12-05 22:11:57 +00:00
|
|
|
|
2023-07-30 18:05:15 +01:00
|
|
|
bool NotificationsListener::checkIsInBlacklist(const QString &appName, const QString &content)
|
|
|
|
{
|
|
|
|
auto appIt = m_applications.constFind(appName);
|
|
|
|
return appIt->blacklistExpression.isValid() && !appIt->blacklistExpression.pattern().isEmpty() && appIt->blacklistExpression.match(content).hasMatch();
|
|
|
|
}
|
2023-08-13 20:43:59 +01:00
|
|
|
|
2023-07-30 18:05:15 +01:00
|
|
|
QSharedPointer<QIODevice> NotificationsListener::iconFromQImage(const QImage &image) const
|
|
|
|
{
|
|
|
|
QSharedPointer<QBuffer> buffer = QSharedPointer<QBuffer>(new QBuffer);
|
|
|
|
if (!buffer->open(QIODevice::WriteOnly) && !image.save(buffer.data(), "PNG")) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_SENDNOTIFICATIONS) << "Could not initialize image buffer";
|
|
|
|
return QSharedPointer<QIODevice>();
|
2016-01-07 16:37:35 +00:00
|
|
|
}
|
|
|
|
|
2023-07-30 18:05:15 +01:00
|
|
|
return buffer;
|
2015-09-10 15:36:03 +01:00
|
|
|
}
|
2023-07-26 09:15:11 +01:00
|
|
|
|
|
|
|
#include "moc_notificationslistener.cpp"
|