2013-08-20 13:05:22 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
|
2013-08-20 12:55:03 +01:00
|
|
|
*
|
|
|
|
* 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 "notification.h"
|
2017-11-06 03:12:16 +00:00
|
|
|
#include "notification_debug.h"
|
2013-08-20 12:55:03 +01:00
|
|
|
|
2017-04-13 20:31:46 +01:00
|
|
|
#include <KNotification>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QPixmap>
|
2017-05-31 14:33:21 +01:00
|
|
|
#include <KLocalizedString>
|
2017-12-29 17:38:09 +00:00
|
|
|
#include <QFile>
|
2013-11-06 21:16:55 +00:00
|
|
|
|
2017-04-13 20:31:46 +01:00
|
|
|
#include <core/filetransferjob.h>
|
|
|
|
|
|
|
|
|
|
|
|
Notification::Notification(const NetworkPackage& np, QObject* parent)
|
2013-08-20 12:55:03 +01:00
|
|
|
: QObject(parent)
|
|
|
|
{
|
2017-12-29 17:38:09 +00:00
|
|
|
//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);
|
2017-09-03 20:39:44 +01:00
|
|
|
m_imagesDir.mkpath(m_imagesDir.absolutePath());
|
2017-12-29 17:38:09 +00:00
|
|
|
QFile(m_imagesDir.absolutePath()).setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner);
|
2017-09-03 20:39:44 +01:00
|
|
|
m_closed = false;
|
2017-11-06 03:12:16 +00:00
|
|
|
m_ready = false;
|
2017-04-13 20:31:46 +01:00
|
|
|
|
|
|
|
parseNetworkPackage(np);
|
|
|
|
createKNotification(false, np);
|
2013-08-20 12:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Notification::~Notification()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notification::dismiss()
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
if (m_dismissable) {
|
|
|
|
Q_EMIT dismissRequested(m_internalId);
|
2013-08-20 12:55:03 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-21 06:22:14 +00:00
|
|
|
|
2017-04-13 20:31:46 +01:00
|
|
|
void Notification::show()
|
|
|
|
{
|
2017-11-06 03:12:16 +00:00
|
|
|
m_ready = true;
|
|
|
|
Q_EMIT ready();
|
2017-09-03 20:39:44 +01:00
|
|
|
if (!m_silent) {
|
|
|
|
m_closed = false;
|
|
|
|
m_notification->sendEvent();
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
void Notification::update(const NetworkPackage& np)
|
2017-04-13 20:31:46 +01:00
|
|
|
{
|
|
|
|
parseNetworkPackage(np);
|
2017-09-03 20:39:44 +01:00
|
|
|
createKNotification(!m_closed, np);
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
KNotification* Notification::createKNotification(bool update, const NetworkPackage& np)
|
2017-04-13 20:31:46 +01:00
|
|
|
{
|
|
|
|
if (!update) {
|
2017-09-03 20:39:44 +01:00
|
|
|
m_notification = new KNotification(QStringLiteral("notification"), KNotification::CloseOnTimeout, this);
|
|
|
|
m_notification->setComponentName(QStringLiteral("kdeconnect"));
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
QString escapedTitle = m_title.toHtmlEscaped();
|
|
|
|
QString escapedText = m_text.toHtmlEscaped();
|
|
|
|
QString escapedTicker = m_ticker.toHtmlEscaped();
|
2017-05-07 19:22:57 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
m_notification->setTitle(m_appName.toHtmlEscaped());
|
2017-04-13 20:31:46 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
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);
|
2017-04-13 20:31:46 +01:00
|
|
|
} else {
|
2017-09-03 20:39:44 +01:00
|
|
|
m_notification->setText(escapedTitle+": "+escapedText);
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|
|
|
|
|
2017-11-06 03:12:16 +00:00
|
|
|
m_hasIcon = m_hasIcon && !m_payloadHash.isEmpty();
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
if (!m_hasIcon) {
|
2017-11-06 03:12:16 +00:00
|
|
|
applyNoIcon();
|
2017-04-13 20:31:46 +01:00
|
|
|
show();
|
|
|
|
} else {
|
|
|
|
|
2017-11-06 03:12:16 +00:00
|
|
|
m_iconPath = m_imagesDir.absoluteFilePath(m_payloadHash);
|
|
|
|
|
|
|
|
if (!QFile::exists(m_iconPath)) {
|
|
|
|
loadIcon(np);
|
2017-04-13 20:31:46 +01:00
|
|
|
} else {
|
2017-11-06 03:12:16 +00:00
|
|
|
applyIcon();
|
|
|
|
show();
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-11 22:26:14 +01:00
|
|
|
|
2017-11-06 03:12:16 +00:00
|
|
|
if (!m_requestReplyId.isEmpty()) {
|
|
|
|
m_notification->setActions(QStringList(i18n("Reply")));
|
2017-09-03 20:39:44 +01:00
|
|
|
connect(m_notification, &KNotification::action1Activated, this, &Notification::reply);
|
2017-07-11 22:26:14 +01:00
|
|
|
}
|
2017-04-13 20:31:46 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
connect(m_notification, &KNotification::closed, this, &Notification::closed);
|
2017-04-13 20:31:46 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
return m_notification;
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|
|
|
|
|
2017-11-06 03:12:16 +00:00
|
|
|
void Notification::loadIcon(const NetworkPackage& np)
|
|
|
|
{
|
|
|
|
m_ready = false;
|
|
|
|
FileTransferJob* job = np.createPayloadTransferJob(QUrl::fromLocalFile(m_iconPath));
|
|
|
|
job->start();
|
|
|
|
|
|
|
|
connect(job, &FileTransferJob::result, this, [this, job]{
|
|
|
|
|
|
|
|
if (job->error()) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_NOTIFICATION) << "Error in FileTransferJob: " << 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"));
|
|
|
|
}
|
|
|
|
|
2017-06-01 15:17:37 +01:00
|
|
|
void Notification::reply()
|
|
|
|
{
|
|
|
|
Q_EMIT replyRequested();
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:31:46 +01:00
|
|
|
void Notification::closed()
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
m_closed = true;
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
void Notification::parseNetworkPackage(const NetworkPackage& np)
|
2017-04-13 20:31:46 +01:00
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
m_internalId = np.get<QString>(QStringLiteral("id"));
|
|
|
|
m_appName = np.get<QString>(QStringLiteral("appName"));
|
|
|
|
m_ticker = np.get<QString>(QStringLiteral("ticker"));
|
|
|
|
m_title = np.get<QString>(QStringLiteral("title"));
|
|
|
|
m_text = np.get<QString>(QStringLiteral("text"));
|
|
|
|
m_dismissable = np.get<bool>(QStringLiteral("isClearable"));
|
|
|
|
m_hasIcon = np.hasPayload();
|
|
|
|
m_silent = np.get<bool>(QStringLiteral("silent"));
|
|
|
|
m_payloadHash = np.get<QString>(QStringLiteral("payloadHash"));
|
|
|
|
m_requestReplyId = np.get<QString>(QStringLiteral("requestReplyId"), QString());
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|