Notification icons from Android are now displayed

- FileTransferJob is now nonblocking.
- Files are stored based on the image MD5.
- Some improvements in displaying the notification, e.g. title is only
  displayed when different than the app name.
- Most of the notification display code moved to the Notification class.

REVIEW: 130050
This commit is contained in:
Nicolas Fella 2017-04-13 21:31:46 +02:00 committed by Albert Vaca
parent 34d419756b
commit 891f1bd355
6 changed files with 144 additions and 42 deletions

5
.gitignore vendored
View file

@ -8,3 +8,8 @@ src/*_automoc.cpp*
src/moc_*.cpp src/moc_*.cpp
CMakeTmp CMakeTmp
CMakeFiles CMakeFiles
KDEConnect.config
KDEConnect.creator
KDEConnect.files
KDEConnect.includes
.directory

View file

@ -54,6 +54,9 @@ public:
QUrl destination() const { return mDestination; } QUrl destination() const { return mDestination; }
void setOriginName(const QString& from) { mFrom = from; } void setOriginName(const QString& from) { mFrom = from; }
Q_SIGNALS:
void emitResult();
private Q_SLOTS: private Q_SLOTS:
void doStart(); void doStart();

View file

@ -20,16 +20,24 @@
#include "notification.h" #include "notification.h"
#include <QDBusConnection> #include <KNotification>
#include <QIcon>
#include <QString>
#include <QUrl>
#include <QPixmap>
Notification::Notification(const NetworkPackage& np, const QString& iconPath, QObject* parent) #include <core/filetransferjob.h>
Notification::Notification(const NetworkPackage& np, QObject* parent)
: QObject(parent) : QObject(parent)
{ {
mInternalId = np.get<QString>(QStringLiteral("id")); mImagesDir = QDir::temp().absoluteFilePath(QStringLiteral("kdeconnect"));
mAppName = np.get<QString>(QStringLiteral("appName")); mImagesDir.mkpath(mImagesDir.absolutePath());
mTicker = np.get<QString>(QStringLiteral("ticker")); mClosed = false;
mDismissable = np.get<bool>(QStringLiteral("isClearable"));
mIconPath = iconPath; parseNetworkPackage(np);
createKNotification(false, np);
} }
Notification::~Notification() Notification::~Notification()
@ -44,3 +52,87 @@ void Notification::dismiss()
} }
} }
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"));
}
mNotification->setTitle(mAppName);
if (mTitle.isEmpty() && mText.isEmpty()) {
mNotification->setText(mTicker);
} else if (mAppName==mTitle) {
mNotification->setText(mText);
} else if (mTitle.isEmpty()){
mNotification->setText(mText);
} else if (mText.isEmpty()){
mNotification->setText(mTitle);
} else {
mNotification->setText(mTitle+": "+mText);
}
if (!mHasIcon) {
mNotification->setIconName(QStringLiteral("preferences-desktop-notification"));
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();
connect(job, &FileTransferJob::emitResult, this, &Notification::applyIconAndShow);
}
}
connect(mNotification, &KNotification::closed, this, &Notification::closed);
return mNotification;
}
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"));
}

View file

@ -23,6 +23,8 @@
#include <QObject> #include <QObject>
#include <QString> #include <QString>
#include <KNotification>
#include <QDir>
#include <core/networkpackage.h> #include <core/networkpackage.h>
@ -34,32 +36,54 @@ class Notification
Q_PROPERTY(QString internalId READ internalId) Q_PROPERTY(QString internalId READ internalId)
Q_PROPERTY(QString appName READ appName) Q_PROPERTY(QString appName READ appName)
Q_PROPERTY(QString ticker READ ticker) 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(QString iconPath READ iconPath)
Q_PROPERTY(bool dismissable READ dismissable) Q_PROPERTY(bool dismissable READ dismissable)
Q_PROPERTY(bool hasIcon READ hasIcon)
Q_PROPERTY(bool silent READ silent)
public: public:
Notification(const NetworkPackage& np, const QString& iconPath, QObject* parent); Notification(const NetworkPackage& np, QObject* parent);
~Notification() override; ~Notification() override;
QString internalId() const { return mInternalId; } QString internalId() const { return mInternalId; }
QString appName() const { return mAppName; } QString appName() const { return mAppName; }
QString ticker() const { return mTicker; } QString ticker() const { return mTicker; }
QString title() const { return mTitle; }
QString text() const { return mText; }
QString iconPath() const { return mIconPath; } QString iconPath() const { return mIconPath; }
bool dismissable() const { return mDismissable; } bool dismissable() const { return mDismissable; }
bool hasIcon() const { return mHasIcon; }
void show();
bool silent() const { return mSilent; }
void update(const NetworkPackage &np);
KNotification* createKNotification(bool update, const NetworkPackage &np);
public Q_SLOTS: public Q_SLOTS:
Q_SCRIPTABLE void dismiss(); Q_SCRIPTABLE void dismiss();
Q_SCRIPTABLE void applyIconAndShow();
void closed();
Q_SIGNALS: Q_SIGNALS:
void dismissRequested(const QString& mInternalId); void dismissRequested(const QString& mInternalId);
private: private:
QString mInternalId; QString mInternalId;
QString mAppName; QString mAppName;
QString mTicker; QString mTicker;
QString mTitle;
QString mText;
QString mIconPath; QString mIconPath;
bool mDismissable; bool mDismissable;
bool mHasIcon;
KNotification* mNotification;
QDir mImagesDir;
bool mSilent;
bool mClosed;
QString mPayloadHash;
void parseNetworkPackage(const NetworkPackage& np);
}; };
#endif #endif

View file

@ -20,16 +20,11 @@
#include "notificationsdbusinterface.h" #include "notificationsdbusinterface.h"
#include "notification_debug.h" #include "notification_debug.h"
#include "notification.h"
#include <QDBusConnection> #include <QDBusConnection>
#include <KNotification>
#include <QIcon>
#include <QCryptographicHash>
#include <core/device.h> #include <core/device.h>
#include <core/kdeconnectplugin.h> #include <core/kdeconnectplugin.h>
#include <core/filetransferjob.h>
#include "notificationsplugin.h" #include "notificationsplugin.h"
@ -38,9 +33,8 @@ NotificationsDbusInterface::NotificationsDbusInterface(KdeConnectPlugin* plugin)
, mDevice(plugin->device()) , mDevice(plugin->device())
, mPlugin(plugin) , mPlugin(plugin)
, mLastId(0) , mLastId(0)
, imagesDir(QDir::temp().absoluteFilePath(QStringLiteral("kdeconnect")))
{ {
imagesDir.mkpath(imagesDir.absolutePath());
} }
NotificationsDbusInterface::~NotificationsDbusInterface() NotificationsDbusInterface::~NotificationsDbusInterface()
@ -79,32 +73,18 @@ void NotificationsDbusInterface::processPackage(const NetworkPackage& np)
}); });
mPlugin->sendPackage(np); mPlugin->sendPackage(np);
} }
} else if(np.get<bool>(QStringLiteral("requestAnswer"), false)) {
} else { } else {
QString id = np.get<QString>(QStringLiteral("id"));
//TODO: Uncoment when we are able to display app icon on plasmoid if (!mInternalIdToPublicId.contains(id)) {
QString destination; Notification* noti = new Notification(np, this);
/*
if (np.hasPayload()) {
QString filename = KMD5(np.get<QString>("appName").toLatin1()).hexDigest(); //TODO: Store with extension?
destination = imagesDir.absoluteFilePath(filename);
FileTransferJob* job = np.createPayloadTransferJob(destination);
job->start();
}
*/
Notification* noti = new Notification(np, destination, this);
//Do not show updates to existent notification nor answers to a initialization request
if (!mInternalIdToPublicId.contains(noti->internalId()) && !np.get<bool>(QStringLiteral("requestAnswer"), false) && !np.get<bool>(QStringLiteral("silent"), false)) {
KNotification* notification = new KNotification(QStringLiteral("notification"), KNotification::CloseOnTimeout, this);
notification->setIconName(QStringLiteral("preferences-desktop-notification"));
notification->setComponentName(QStringLiteral("kdeconnect"));
notification->setTitle(mDevice->name());
notification->setText(noti->appName() + ": " + noti->ticker());
notification->sendEvent();
}
addNotification(noti); addNotification(noti);
} else {
QString pubId = mInternalIdToPublicId[id];
mNotifications[pubId]->update(np);
}
} }
} }

View file

@ -65,8 +65,6 @@ private /*attributes*/:
QHash<QString, Notification*> mNotifications; QHash<QString, Notification*> mNotifications;
QHash<QString, QString> mInternalIdToPublicId; QHash<QString, QString> mInternalIdToPublicId;
int mLastId; int mLastId;
QDir imagesDir;
}; };
#endif #endif