Skip to content

Commit

Permalink
[lipstick] Add support for the image-data role. Contributes to JB#45997
Browse files Browse the repository at this point in the history
On the consumption side return the image as a data URL from image-path
so it can be entered as the source of an Image in an interoperable way.
  • Loading branch information
adenexter committed Aug 13, 2020
1 parent ac48668 commit 35aa18a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/notifications/lipsticknotification.cpp
Expand Up @@ -26,6 +26,7 @@ const char *LipstickNotification::HINT_CATEGORY = "category";
const char *LipstickNotification::HINT_TRANSIENT = "transient";
const char *LipstickNotification::HINT_RESIDENT = "resident";
const char *LipstickNotification::HINT_IMAGE_PATH = "image-path";
const char *LipstickNotification::HINT_IMAGE_DATA = "image-data";
const char *LipstickNotification::HINT_SUPPRESS_SOUND = "suppress-sound";
const char *LipstickNotification::HINT_SOUND_FILE = "sound-file";
const char *LipstickNotification::HINT_APP_ICON = "app_icon";
Expand Down Expand Up @@ -446,7 +447,6 @@ void LipstickNotification::updateHintValues()
}

if (hint.compare(LipstickNotification::HINT_APP_ICON, Qt::CaseInsensitive) != 0 &&
hint.compare(LipstickNotification::HINT_IMAGE_PATH, Qt::CaseInsensitive) != 0 &&
hint.compare(LipstickNotification::HINT_TIMESTAMP, Qt::CaseInsensitive) != 0 &&
hint.compare(LipstickNotification::HINT_PREVIEW_SUMMARY, Qt::CaseInsensitive) != 0 &&
hint.compare(LipstickNotification::HINT_PREVIEW_BODY, Qt::CaseInsensitive) != 0 &&
Expand Down
3 changes: 3 additions & 0 deletions src/notifications/lipsticknotification.h
Expand Up @@ -73,6 +73,9 @@ class LIPSTICK_EXPORT LipstickNotification : public QObject
//! Standard hint: Icon of the notification: either a file:// URL, an absolute path, or a token to be satisfied by the 'theme' image provider.
static const char *HINT_IMAGE_PATH;

//! Standard hint: Icon of the notification: image data.
static const char *HINT_IMAGE_DATA;

//! Standard hint: If true, audible feedback should be should be suppressed during notification feedback.
static const char *HINT_SUPPRESS_SOUND;

Expand Down
50 changes: 50 additions & 0 deletions src/notifications/notificationmanager.cpp
Expand Up @@ -15,7 +15,10 @@
****************************************************************************/

#include <QCoreApplication>
#include <QDataStream>
#include <QDBusArgument>
#include <QDebug>
#include <QImage>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
Expand All @@ -24,6 +27,7 @@
#include <QStandardPaths>
#include <QFile>
#include <QFileInfo>
#include <QUrl>
#include <aboutsettings.h>
#include <mremoteaction.h>
#include <mdesktopentry.h>
Expand Down Expand Up @@ -238,6 +242,52 @@ uint NotificationManager::Notify(const QString &appName, uint replacesId, const
}
hints_.insert(LipstickNotification::HINT_TIMESTAMP, timestamp);

auto it = hints_.find(LipstickNotification::HINT_IMAGE_DATA);
if (it != hints_.end()) {
const QDBusArgument argument = it->value<QDBusArgument>();

hints_.erase(it);

int width = 0;
int height = 0;
int stride = 0;
bool alpha = false;
int bitsPerSample = 0;
int channels = 0;
QByteArray data;

argument.beginStructure();
argument >> width;
argument >> height;
argument >> stride;
argument >> alpha;
argument >> bitsPerSample;
argument >> channels;
argument >> data;
argument.endStructure();

if (bitsPerSample == 8 && channels == 4 && data.size() >= stride * height) {
const QImage image(
reinterpret_cast<const uchar *>(data.constData()),
width,
height,
stride,
alpha ? QImage::Format_ARGB32 : QImage::Format_RGB32);

QBuffer buffer;
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG");
buffer.close();

const QUrl url
= QLatin1String("data:image/png;base64,")
+ QString::fromUtf8(buffer.data().toBase64());

hints_.insert(LipstickNotification::HINT_IMAGE_PATH, url.toString());
}
}


QPair<QString, QString> pidProperties;
bool androidOrigin(false);
if (calledFromDBus()) {
Expand Down

0 comments on commit 35aa18a

Please sign in to comment.