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-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>
|
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-04-13 20:31:46 +01:00
|
|
|
mImagesDir = QDir::temp().absoluteFilePath(QStringLiteral("kdeconnect"));
|
|
|
|
mImagesDir.mkpath(mImagesDir.absolutePath());
|
|
|
|
mClosed = false;
|
|
|
|
|
|
|
|
parseNetworkPackage(np);
|
|
|
|
createKNotification(false, np);
|
2013-08-20 12:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Notification::~Notification()
|
|
|
|
{
|
2013-08-20 13:05:22 +01:00
|
|
|
|
2013-08-20 12:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Notification::dismiss()
|
|
|
|
{
|
|
|
|
if (mDismissable) {
|
2015-10-17 23:32:13 +01:00
|
|
|
Q_EMIT dismissRequested(mInternalId);
|
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()
|
|
|
|
{
|
|
|
|
if (!mSilent) {
|
|
|
|
mClosed = false;
|
|
|
|
mNotification->sendEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notification::applyIconAndShow()
|
|
|
|
{
|
|
|
|
if (!mSilent) {
|
|
|
|
QPixmap icon(mIconPath, "PNG");
|
|
|
|
mNotification->setPixmap(icon);
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notification::update(const NetworkPackage &np)
|
|
|
|
{
|
|
|
|
parseNetworkPackage(np);
|
|
|
|
createKNotification(!mClosed, np);
|
|
|
|
}
|
|
|
|
|
|
|
|
KNotification* Notification::createKNotification(bool update, const NetworkPackage &np)
|
|
|
|
{
|
|
|
|
if (!update) {
|
|
|
|
mNotification = new KNotification(QStringLiteral("notification"), KNotification::CloseOnTimeout, this);
|
|
|
|
mNotification->setComponentName(QStringLiteral("kdeconnect"));
|
|
|
|
}
|
|
|
|
|
2017-05-07 19:22:57 +01:00
|
|
|
QString escapedTitle = mTitle.toHtmlEscaped();
|
|
|
|
QString escapedText = mText.toHtmlEscaped();
|
|
|
|
QString escapedTicker = mTicker.toHtmlEscaped();
|
|
|
|
|
|
|
|
mNotification->setTitle(mAppName.toHtmlEscaped());
|
2017-04-13 20:31:46 +01:00
|
|
|
|
|
|
|
if (mTitle.isEmpty() && mText.isEmpty()) {
|
2017-05-07 19:22:57 +01:00
|
|
|
mNotification->setText(escapedTicker);
|
2017-04-13 20:31:46 +01:00
|
|
|
} else if (mAppName==mTitle) {
|
2017-05-07 19:22:57 +01:00
|
|
|
mNotification->setText(escapedText);
|
2017-04-13 20:31:46 +01:00
|
|
|
} else if (mTitle.isEmpty()){
|
2017-05-07 19:22:57 +01:00
|
|
|
mNotification->setText(escapedText);
|
2017-04-13 20:31:46 +01:00
|
|
|
} else if (mText.isEmpty()){
|
2017-05-07 19:22:57 +01:00
|
|
|
mNotification->setText(escapedTitle);
|
2017-04-13 20:31:46 +01:00
|
|
|
} else {
|
2017-05-07 19:22:57 +01:00
|
|
|
mNotification->setText(escapedTitle+": "+escapedText);
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mHasIcon) {
|
2017-07-11 22:26:14 +01:00
|
|
|
//HACK The only way to display no icon at all is trying to load a non-existant icon
|
|
|
|
mNotification->setIconName(QString("not_a_real_icon"));
|
2017-04-13 20:31:46 +01:00
|
|
|
show();
|
|
|
|
} else {
|
|
|
|
QString filename = mPayloadHash;
|
|
|
|
|
|
|
|
if (filename.isEmpty()) {
|
|
|
|
mHasIcon = false;
|
|
|
|
} else {
|
|
|
|
mIconPath = mImagesDir.absoluteFilePath(filename);
|
|
|
|
QUrl destinationUrl(mIconPath);
|
|
|
|
FileTransferJob* job = np.createPayloadTransferJob(destinationUrl);
|
|
|
|
job->start();
|
2017-05-07 19:15:23 +01:00
|
|
|
connect(job, &FileTransferJob::result, this, &Notification::applyIconAndShow);
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-11 22:26:14 +01:00
|
|
|
|
2017-05-31 14:33:21 +01:00
|
|
|
if(!mRequestReplyId.isEmpty()) {
|
|
|
|
mNotification->setActions( QStringList(i18n("Reply")) );
|
2017-06-01 15:17:37 +01:00
|
|
|
connect(mNotification, &KNotification::action1Activated, this, &Notification::reply);
|
2017-07-11 22:26:14 +01:00
|
|
|
}
|
2017-04-13 20:31:46 +01:00
|
|
|
|
|
|
|
connect(mNotification, &KNotification::closed, this, &Notification::closed);
|
|
|
|
|
|
|
|
return mNotification;
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
mClosed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notification::parseNetworkPackage(const NetworkPackage &np)
|
|
|
|
{
|
|
|
|
mInternalId = np.get<QString>(QStringLiteral("id"));
|
|
|
|
mAppName = np.get<QString>(QStringLiteral("appName"));
|
|
|
|
mTicker = np.get<QString>(QStringLiteral("ticker"));
|
|
|
|
mTitle = np.get<QString>(QStringLiteral("title"));
|
|
|
|
mText = np.get<QString>(QStringLiteral("text"));
|
|
|
|
mDismissable = np.get<bool>(QStringLiteral("isClearable"));
|
|
|
|
mHasIcon = np.hasPayload();
|
|
|
|
mSilent = np.get<bool>(QStringLiteral("silent"));
|
|
|
|
mPayloadHash = np.get<QString>(QStringLiteral("payloadHash"));
|
2017-05-31 14:33:21 +01:00
|
|
|
mRequestReplyId = np.get<QString>(QStringLiteral("requestReplyId"), QString());
|
2017-04-13 20:31:46 +01:00
|
|
|
}
|