diff --git a/plugins/notifications/notification.cpp b/plugins/notifications/notification.cpp index 01c73850..6e381667 100644 --- a/plugins/notifications/notification.cpp +++ b/plugins/notifications/notification.cpp @@ -1,187 +1,190 @@ /** * Copyright 2013 Albert Vaca * * 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 . */ #include "notification.h" #include "notification_debug.h" #include #include #include #include #include #include #include #include Notification::Notification(const NetworkPacket& np, QObject* parent) : QObject(parent) { //Make a own directory for each user so noone can see each others icons QString username; #ifdef Q_OS_WIN username = qgetenv("USERNAME"); #else username = qgetenv("USER"); #endif m_imagesDir = QDir::temp().absoluteFilePath(QStringLiteral("kdeconnect_") + username); m_imagesDir.mkpath(m_imagesDir.absolutePath()); QFile(m_imagesDir.absolutePath()).setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner); m_closed = false; m_ready = false; parseNetworkPacket(np); createKNotification(false, np); } Notification::~Notification() { } void Notification::dismiss() { if (m_dismissable) { Q_EMIT dismissRequested(m_internalId); } } void Notification::show() { m_ready = true; Q_EMIT ready(); if (!m_silent) { m_closed = false; m_notification->sendEvent(); } } void Notification::update(const NetworkPacket& np) { parseNetworkPacket(np); createKNotification(!m_closed, np); } KNotification* Notification::createKNotification(bool update, const NetworkPacket& np) { if (!update) { m_notification = new KNotification(QStringLiteral("notification"), KNotification::CloseOnTimeout, this); m_notification->setComponentName(QStringLiteral("kdeconnect")); } QString escapedTitle = m_title.toHtmlEscaped(); 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) { 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); } m_hasIcon = m_hasIcon && !m_payloadHash.isEmpty(); if (!m_hasIcon) { applyNoIcon(); show(); } else { m_iconPath = m_imagesDir.absoluteFilePath(m_payloadHash); if (!QFile::exists(m_iconPath)) { loadIcon(np); } else { applyIcon(); show(); } } if (!m_requestReplyId.isEmpty()) { m_notification->setActions(QStringList(i18n("Reply"))); connect(m_notification, &KNotification::action1Activated, this, &Notification::reply); } connect(m_notification, &KNotification::closed, this, &Notification::closed); return m_notification; } void Notification::loadIcon(const NetworkPacket& np) { + if (m_job) + return; + m_ready = false; - FileTransferJob* job = np.createPayloadTransferJob(QUrl::fromLocalFile(m_iconPath)); - job->start(); + m_job = np.createPayloadTransferJob(QUrl::fromLocalFile(m_iconPath)); + m_job->start(); - connect(job, &FileTransferJob::result, this, [this, job]{ + connect(m_job, &FileTransferJob::result, this, [this]{ - if (job->error()) { - qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "Error in FileTransferJob: " << job->errorString(); + if (m_job->error()) { + qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "Error in FileTransferJob: " << m_job->errorString(); applyNoIcon(); } else { applyIcon(); } show(); }); } void Notification::applyIcon() { QPixmap icon(m_iconPath, "PNG"); m_notification->setPixmap(icon); } void Notification::applyNoIcon() { //HACK The only way to display no icon at all is trying to load a non-existant icon m_notification->setIconName(QStringLiteral("not_a_real_icon")); } void Notification::reply() { Q_EMIT replyRequested(); } void Notification::closed() { m_closed = true; } void Notification::parseNetworkPacket(const NetworkPacket& np) { m_internalId = np.get(QStringLiteral("id")); m_appName = np.get(QStringLiteral("appName")); m_ticker = np.get(QStringLiteral("ticker")); m_title = np.get(QStringLiteral("title")); m_text = np.get(QStringLiteral("text")); m_dismissable = np.get(QStringLiteral("isClearable")); m_hasIcon = np.hasPayload(); m_silent = np.get(QStringLiteral("silent")); m_payloadHash = np.get(QStringLiteral("payloadHash")); m_requestReplyId = np.get(QStringLiteral("requestReplyId"), QString()); } diff --git a/plugins/notifications/notification.h b/plugins/notifications/notification.h index aacd4f9b..b03377be 100644 --- a/plugins/notifications/notification.h +++ b/plugins/notifications/notification.h @@ -1,99 +1,101 @@ /** * Copyright 2013 Albert Vaca * * 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 . */ #ifndef NOTIFICATION_H #define NOTIFICATION_H #include #include #include #include +#include #include class Notification : public QObject { Q_OBJECT Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device.notifications.notification") Q_PROPERTY(QString internalId READ internalId) Q_PROPERTY(QString appName READ appName) Q_PROPERTY(QString ticker READ ticker) Q_PROPERTY(QString title READ title) Q_PROPERTY(QString text READ text) Q_PROPERTY(QString iconPath READ iconPath) Q_PROPERTY(bool dismissable READ dismissable) Q_PROPERTY(bool hasIcon READ hasIcon) Q_PROPERTY(bool silent READ silent) Q_PROPERTY(QString replyId READ replyId) public: Notification(const NetworkPacket& np, QObject* parent); ~Notification() override; QString internalId() const { return m_internalId; } QString appName() const { return m_appName; } QString ticker() const { return m_ticker; } QString title() const { return m_title; } QString text() const { return m_text; } QString iconPath() const { return m_iconPath; } bool dismissable() const { return m_dismissable; } QString replyId() const { return m_requestReplyId; } bool hasIcon() const { return m_hasIcon; } void show(); bool silent() const { return m_silent; } void update(const NetworkPacket& np); bool isReady() const { return m_ready; } KNotification* createKNotification(bool update, const NetworkPacket& np); public Q_SLOTS: Q_SCRIPTABLE void dismiss(); Q_SCRIPTABLE void reply(); void closed(); Q_SIGNALS: void dismissRequested(const QString& m_internalId); void replyRequested(); void ready(); private: QString m_internalId; QString m_appName; QString m_ticker; QString m_title; QString m_text; QString m_iconPath; QString m_requestReplyId; bool m_dismissable; bool m_hasIcon; KNotification* m_notification; QDir m_imagesDir; bool m_silent; bool m_closed; QString m_payloadHash; bool m_ready; + QPointer m_job; void parseNetworkPacket(const NetworkPacket& np); void loadIcon(const NetworkPacket& np); void applyIcon(); void applyNoIcon(); }; #endif