Independent check for image in Linux native notifications

This commit is contained in:
Ilya Fedin 2020-02-25 03:45:46 +04:00 committed by John Preston
parent 3a5c0976bb
commit 3b300f23e1
2 changed files with 49 additions and 40 deletions

View File

@ -83,6 +83,31 @@ QVersionNumber ParseSpecificationVersion(
return QVersionNumber(); return QVersionNumber();
} }
QString GetImageKey(
const std::shared_ptr<QDBusInterface> &notificationInterface) {
const auto specificationVersion = ParseSpecificationVersion(
GetServerInformation(notificationInterface));
if (!specificationVersion.isNull()) {
const auto majorVersion = specificationVersion.majorVersion();
const auto minorVersion = specificationVersion.minorVersion();
if ((majorVersion == 1 && minorVersion >= 2) || majorVersion > 1) {
return qsl("image-data");
} else if (majorVersion == 1 && minorVersion == 1) {
return qsl("image_data");
} else if ((majorVersion == 1 && minorVersion < 1)
|| majorVersion < 1) {
return qsl("icon_data");
} else {
LOG(("Native notification error: unknown specification version"));
}
} else {
LOG(("Native notification error: specification version is null"));
}
return QString();
}
} }
NotificationData::NotificationData( NotificationData::NotificationData(
@ -97,6 +122,7 @@ NotificationData::NotificationData(
: _notificationInterface(notificationInterface) : _notificationInterface(notificationInterface)
, _manager(manager) , _manager(manager)
, _title(title) , _title(title)
, _imageKey(GetImageKey(_notificationInterface))
, _peerId(peerId) , _peerId(peerId)
, _msgId(msgId) { , _msgId(msgId) {
const auto capabilities = GetCapabilities(_notificationInterface); const auto capabilities = GetCapabilities(_notificationInterface);
@ -161,7 +187,6 @@ NotificationData::NotificationData(
} }
_hints["category"] = qsl("im.received"); _hints["category"] = qsl("im.received");
_hints["desktop-entry"] = GetLauncherBasename(); _hints["desktop-entry"] = GetLauncherBasename();
_notificationInterface->connection().connect( _notificationInterface->connection().connect(
@ -173,14 +198,16 @@ NotificationData::NotificationData(
SLOT(notificationClosed(uint))); SLOT(notificationClosed(uint)));
} }
bool NotificationData::show(bool hideNameAndPhoto) { bool NotificationData::show() {
const auto iconName = _imageKey.isEmpty() || !_hints.contains(_imageKey)
? qsl("telegram")
: QString();
const QDBusReply<uint> notifyReply = _notificationInterface->call( const QDBusReply<uint> notifyReply = _notificationInterface->call(
qsl("Notify"), qsl("Notify"),
AppName.utf16(), AppName.utf16(),
uint(0), uint(0),
hideNameAndPhoto iconName,
? qsl("telegram")
: QString(),
_title, _title,
_body, _body,
_actions, _actions,
@ -198,8 +225,9 @@ bool NotificationData::show(bool hideNameAndPhoto) {
} }
bool NotificationData::close() { bool NotificationData::close() {
const QDBusReply<void> closeReply = _notificationInterface const QDBusReply<void> closeReply = _notificationInterface->call(
->call(qsl("CloseNotification"), _notificationId); qsl("CloseNotification"),
_notificationId);
if (!closeReply.isValid()) { if (!closeReply.isValid()) {
LOG(("Native notification error: %1") LOG(("Native notification error: %1")
@ -210,28 +238,7 @@ bool NotificationData::close() {
} }
void NotificationData::setImage(const QString &imagePath) { void NotificationData::setImage(const QString &imagePath) {
const auto specificationVersion = ParseSpecificationVersion( if (_imageKey.isEmpty()) {
GetServerInformation(_notificationInterface));
QString imageKey;
if (!specificationVersion.isNull()) {
const auto majorVersion = specificationVersion.majorVersion();
const auto minorVersion = specificationVersion.minorVersion();
if ((majorVersion == 1 && minorVersion >= 2) || majorVersion > 1) {
imageKey = qsl("image-data");
} else if (majorVersion == 1 && minorVersion == 1) {
imageKey = qsl("image_data");
} else if ((majorVersion == 1 && minorVersion < 1)
|| majorVersion < 1) {
imageKey = qsl("icon_data");
} else {
LOG(("Native notification error: unknown specification version"));
return;
}
} else {
LOG(("Native notification error: specification version is null"));
return; return;
} }
@ -246,16 +253,17 @@ void NotificationData::setImage(const QString &imagePath) {
image.sizeInBytes()); image.sizeInBytes());
#endif #endif
ImageData imageData; const auto imageData = ImageData{
imageData.width = image.width(); image.width(),
imageData.height = image.height(); image.height(),
imageData.rowStride = image.bytesPerLine(); image.bytesPerLine(),
imageData.hasAlpha = true; true,
imageData.bitsPerSample = 8; 8,
imageData.channels = 4; 4,
imageData.data = imageBytes; imageBytes
};
_hints[imageKey] = QVariant::fromValue(imageData); _hints[_imageKey] = QVariant::fromValue(imageData);
} }
void NotificationData::notificationClosed(uint id) { void NotificationData::notificationClosed(uint id) {
@ -410,7 +418,7 @@ void Manager::Private::showNotification(
i = _notifications.insert(peer->id, QMap<MsgId, Notification>()); i = _notifications.insert(peer->id, QMap<MsgId, Notification>());
} }
_notifications[peer->id].insert(msgId, notification); _notifications[peer->id].insert(msgId, notification);
if (!notification->show(hideNameAndPhoto)) { if (!notification->show()) {
i = _notifications.find(peer->id); i = _notifications.find(peer->id);
if (i != _notifications.cend()) { if (i != _notifications.cend()) {
i->remove(msgId); i->remove(msgId);

View File

@ -50,7 +50,7 @@ public:
NotificationData(NotificationData &&other) = delete; NotificationData(NotificationData &&other) = delete;
NotificationData &operator=(NotificationData &&other) = delete; NotificationData &operator=(NotificationData &&other) = delete;
bool show(bool hideNameAndPhoto); bool show();
bool close(); bool close();
void setImage(const QString &imagePath); void setImage(const QString &imagePath);
@ -69,6 +69,7 @@ private:
QString _body; QString _body;
QStringList _actions; QStringList _actions;
QVariantMap _hints; QVariantMap _hints;
QString _imageKey;
uint _notificationId; uint _notificationId;
PeerId _peerId; PeerId _peerId;