File indexing completed on 2024-03-24 04:59:51
0001 /** 0002 * SPDX-FileCopyrightText: 2015 Holger Kaelberer <holger.k@elberer.de> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "notificationslistener.h" 0008 0009 #include <QBuffer> 0010 #include <QImage> 0011 #include <QStandardPaths> 0012 0013 #include <KConfig> 0014 #include <KConfigGroup> 0015 0016 #include "notifyingapplication.h" 0017 #include "plugin_sendnotifications_debug.h" 0018 #include <core/kdeconnectplugin.h> 0019 #include <core/kdeconnectpluginconfig.h> 0020 0021 NotificationsListener::NotificationsListener(KdeConnectPlugin *aPlugin) 0022 : QObject(aPlugin) 0023 , m_plugin(aPlugin) 0024 { 0025 setTranslatedAppName(); 0026 loadApplications(); 0027 0028 connect(m_plugin->config(), &KdeConnectPluginConfig::configChanged, this, &NotificationsListener::loadApplications); 0029 } 0030 0031 NotificationsListener::~NotificationsListener() 0032 { 0033 qCDebug(KDECONNECT_PLUGIN_SENDNOTIFICATIONS) << "Destroying NotificationsListener"; 0034 } 0035 0036 void NotificationsListener::setTranslatedAppName() 0037 { 0038 QString filePath = 0039 QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("knotifications5/kdeconnect.notifyrc"), QStandardPaths::LocateFile); 0040 if (filePath.isEmpty()) { 0041 qCDebug(KDECONNECT_PLUGIN_SENDNOTIFICATIONS) 0042 << "Couldn't find kdeconnect.notifyrc to hide kdeconnect notifications on the devices. Using default name."; 0043 m_translatedAppName = QStringLiteral("KDE Connect"); 0044 return; 0045 } 0046 0047 KConfig config(filePath, KConfig::OpenFlag::SimpleConfig); 0048 KConfigGroup globalgroup(&config, QStringLiteral("Global")); 0049 m_translatedAppName = globalgroup.readEntry(QStringLiteral("Name"), QStringLiteral("KDE Connect")); 0050 } 0051 0052 void NotificationsListener::loadApplications() 0053 { 0054 m_applications.clear(); 0055 const QVariantList list = m_plugin->config()->getList(QStringLiteral("applications")); 0056 for (const auto &a : list) { 0057 NotifyingApplication app = a.value<NotifyingApplication>(); 0058 if (!m_applications.contains(app.name)) 0059 m_applications.insert(app.name, app); 0060 } 0061 // qCDebug(KDECONNECT_PLUGIN_SENDNOTIFICATIONS) << "Loaded" << m_applications.size() << " applications"; 0062 } 0063 0064 bool NotificationsListener::checkApplicationName(const QString &appName, std::optional<std::reference_wrapper<const QString>> iconName) 0065 { 0066 if (m_translatedAppName == appName) { 0067 return false; 0068 } 0069 0070 auto it = m_applications.constFind(appName); 0071 if (it == m_applications.cend()) { 0072 // new application -> add to config 0073 NotifyingApplication app; 0074 app.name = appName; 0075 if (iconName.has_value()) { 0076 app.icon = iconName.value(); 0077 } 0078 app.active = true; 0079 m_applications.insert(app.name, app); 0080 // update config: 0081 QVariantList list; 0082 for (const auto &a : std::as_const(m_applications)) { 0083 list << QVariant::fromValue<NotifyingApplication>(a); 0084 } 0085 m_plugin->config()->setList(QStringLiteral("applications"), list); 0086 0087 return true; 0088 } else { 0089 return it->active; 0090 } 0091 } 0092 0093 bool NotificationsListener::checkIsInBlacklist(const QString &appName, const QString &content) 0094 { 0095 auto appIt = m_applications.constFind(appName); 0096 return appIt->blacklistExpression.isValid() && !appIt->blacklistExpression.pattern().isEmpty() && appIt->blacklistExpression.match(content).hasMatch(); 0097 } 0098 0099 QSharedPointer<QIODevice> NotificationsListener::iconFromQImage(const QImage &image) const 0100 { 0101 QSharedPointer<QBuffer> buffer = QSharedPointer<QBuffer>(new QBuffer); 0102 if (!buffer->open(QIODevice::WriteOnly) && !image.save(buffer.data(), "PNG")) { 0103 qCWarning(KDECONNECT_PLUGIN_SENDNOTIFICATIONS) << "Could not initialize image buffer"; 0104 return QSharedPointer<QIODevice>(); 0105 } 0106 0107 return buffer; 0108 } 0109 0110 #include "moc_notificationslistener.cpp"